Skip to content

Commit df06c47

Browse files
dnhatncbuescher
authored andcommitted
Emit deprecation warnings only for new index or template (#117529)
Currently, we emit a deprecation warning in the parser of the source field when source mode is used in mappings. However, this behavior causes warnings to be emitted for every mapping update. In tests with assertions enabled, warnings are also triggered for every change to index metadata. As a result, deprecation warnings are inadvertently emitted for index or update requests. This change relocates the deprecation check to the mapper, limiting it to cases where a new index is created or a template is created/updated. Relates to #117524
1 parent 5886d75 commit df06c47

File tree

5 files changed

+11
-32
lines changed

5 files changed

+11
-32
lines changed

server/src/main/java/org/elasticsearch/index/mapper/MappingParser.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
package org.elasticsearch.index.mapper;
1111

1212
import org.elasticsearch.common.compress.CompressedXContent;
13+
import org.elasticsearch.common.logging.DeprecationCategory;
14+
import org.elasticsearch.common.logging.DeprecationLogger;
1315
import org.elasticsearch.common.xcontent.XContentHelper;
1416
import org.elasticsearch.core.Nullable;
1517
import org.elasticsearch.index.mapper.MapperService.MergeReason;
@@ -31,6 +33,7 @@ public final class MappingParser {
3133
private final Supplier<Map<Class<? extends MetadataFieldMapper>, MetadataFieldMapper>> metadataMappersSupplier;
3234
private final Map<String, MetadataFieldMapper.TypeParser> metadataMapperParsers;
3335
private final Function<String, String> documentTypeResolver;
36+
private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(MappingParser.class);
3437

3538
MappingParser(
3639
Supplier<MappingParserContext> mappingParserContextSupplier,
@@ -144,6 +147,12 @@ Mapping parse(@Nullable String type, MergeReason reason, Map<String, Object> map
144147
}
145148
@SuppressWarnings("unchecked")
146149
Map<String, Object> fieldNodeMap = (Map<String, Object>) fieldNode;
150+
if (reason == MergeReason.INDEX_TEMPLATE
151+
&& SourceFieldMapper.NAME.equals(fieldName)
152+
&& fieldNodeMap.containsKey("mode")
153+
&& SourceFieldMapper.onOrAfterDeprecateModeVersion(mappingParserContext.indexVersionCreated())) {
154+
deprecationLogger.critical(DeprecationCategory.MAPPINGS, "mapping_source_mode", SourceFieldMapper.DEPRECATION_WARNING);
155+
}
147156
MetadataFieldMapper metadataFieldMapper = typeParser.parse(fieldName, fieldNodeMap, mappingParserContext).build();
148157
metadataMappers.put(metadataFieldMapper.getClass(), metadataFieldMapper);
149158
assert fieldNodeMap.isEmpty();

server/src/main/java/org/elasticsearch/index/mapper/SourceFieldMapper.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import org.elasticsearch.common.Explicit;
1919
import org.elasticsearch.common.Strings;
2020
import org.elasticsearch.common.bytes.BytesReference;
21-
import org.elasticsearch.common.logging.DeprecationCategory;
2221
import org.elasticsearch.common.settings.Setting;
2322
import org.elasticsearch.common.settings.Settings;
2423
import org.elasticsearch.common.util.CollectionUtils;
@@ -40,7 +39,6 @@
4039
import java.util.Collections;
4140
import java.util.List;
4241
import java.util.Locale;
43-
import java.util.Map;
4442

4543
public class SourceFieldMapper extends MetadataFieldMapper {
4644
public static final NodeFeature SYNTHETIC_SOURCE_FALLBACK = new NodeFeature("mapper.source.synthetic_source_fallback");
@@ -310,17 +308,7 @@ private static SourceFieldMapper resolveStaticInstance(final Mode sourceMode) {
310308
c.indexVersionCreated().onOrAfter(IndexVersions.SOURCE_MAPPER_LOSSY_PARAMS_CHECK),
311309
onOrAfterDeprecateModeVersion(c.indexVersionCreated()) == false
312310
)
313-
) {
314-
@Override
315-
public MetadataFieldMapper.Builder parse(String name, Map<String, Object> node, MappingParserContext parserContext)
316-
throws MapperParsingException {
317-
assert name.equals(SourceFieldMapper.NAME) : name;
318-
if (onOrAfterDeprecateModeVersion(parserContext.indexVersionCreated()) && node.containsKey("mode")) {
319-
deprecationLogger.critical(DeprecationCategory.MAPPINGS, "mapping_source_mode", SourceFieldMapper.DEPRECATION_WARNING);
320-
}
321-
return super.parse(name, node, parserContext);
322-
}
323-
};
311+
);
324312

325313
static final class SourceFieldType extends MappedFieldType {
326314
private final boolean enabled;

server/src/test/java/org/elasticsearch/index/mapper/DocumentParserContextTests.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,5 @@ public void testCreateDynamicMapperBuilderContext() throws IOException {
133133
assertEquals(ObjectMapper.Defaults.DYNAMIC, resultFromParserContext.getDynamic());
134134
assertEquals(MapperService.MergeReason.MAPPING_UPDATE, resultFromParserContext.getMergeReason());
135135
assertFalse(resultFromParserContext.isInNestedContext());
136-
assertWarnings(SourceFieldMapper.DEPRECATION_WARNING);
137136
}
138137
}

server/src/test/java/org/elasticsearch/index/mapper/SourceFieldMapperTests.java

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ protected void registerParameters(ParameterChecker checker) throws IOException {
6565
topMapping(b -> b.startObject(SourceFieldMapper.NAME).field("mode", "synthetic").endObject()),
6666
dm -> {
6767
assertTrue(dm.metadataMapper(SourceFieldMapper.class).isSynthetic());
68-
assertWarnings(SourceFieldMapper.DEPRECATION_WARNING);
6968
}
7069
);
7170
checker.registerConflictCheck("includes", b -> b.array("includes", "foo*"));
@@ -74,7 +73,7 @@ protected void registerParameters(ParameterChecker checker) throws IOException {
7473
"mode",
7574
topMapping(b -> b.startObject(SourceFieldMapper.NAME).field("mode", "synthetic").endObject()),
7675
topMapping(b -> b.startObject(SourceFieldMapper.NAME).field("mode", "stored").endObject()),
77-
dm -> assertWarnings(SourceFieldMapper.DEPRECATION_WARNING)
76+
d -> {}
7877
);
7978
}
8079

@@ -211,22 +210,19 @@ public void testSyntheticDisabledNotSupported() {
211210
)
212211
);
213212
assertThat(e.getMessage(), containsString("Cannot set both [mode] and [enabled] parameters"));
214-
assertWarnings(SourceFieldMapper.DEPRECATION_WARNING);
215213
}
216214

217215
public void testSyntheticUpdates() throws Exception {
218216
MapperService mapperService = createMapperService("""
219217
{ "_doc" : { "_source" : { "mode" : "synthetic" } } }
220218
""");
221-
assertWarnings(SourceFieldMapper.DEPRECATION_WARNING);
222219
SourceFieldMapper mapper = mapperService.documentMapper().sourceMapper();
223220
assertTrue(mapper.enabled());
224221
assertTrue(mapper.isSynthetic());
225222

226223
merge(mapperService, """
227224
{ "_doc" : { "_source" : { "mode" : "synthetic" } } }
228225
""");
229-
assertWarnings(SourceFieldMapper.DEPRECATION_WARNING);
230226
mapper = mapperService.documentMapper().sourceMapper();
231227
assertTrue(mapper.enabled());
232228
assertTrue(mapper.isSynthetic());
@@ -239,12 +235,10 @@ public void testSyntheticUpdates() throws Exception {
239235
"""));
240236

241237
assertThat(e.getMessage(), containsString("Cannot update parameter [mode] from [synthetic] to [stored]"));
242-
assertWarnings(SourceFieldMapper.DEPRECATION_WARNING);
243238

244239
merge(mapperService, """
245240
{ "_doc" : { "_source" : { "mode" : "disabled" } } }
246241
""");
247-
assertWarnings(SourceFieldMapper.DEPRECATION_WARNING);
248242

249243
mapper = mapperService.documentMapper().sourceMapper();
250244
assertFalse(mapper.enabled());
@@ -281,7 +275,6 @@ public void testSupportsNonDefaultParameterValues() throws IOException {
281275
topMapping(b -> b.startObject("_source").field("mode", randomBoolean() ? "synthetic" : "stored").endObject())
282276
).documentMapper().sourceMapper();
283277
assertThat(sourceFieldMapper, notNullValue());
284-
assertWarnings(SourceFieldMapper.DEPRECATION_WARNING);
285278
}
286279
Exception e = expectThrows(
287280
MapperParsingException.class,
@@ -313,8 +306,6 @@ public void testSupportsNonDefaultParameterValues() throws IOException {
313306
.documentMapper()
314307
.sourceMapper()
315308
);
316-
assertWarnings(SourceFieldMapper.DEPRECATION_WARNING);
317-
318309
assertThat(e.getMessage(), containsString("Parameter [mode=disabled] is not allowed in source"));
319310

320311
e = expectThrows(
@@ -423,7 +414,6 @@ public void testRecoverySourceWithSyntheticSource() throws IOException {
423414
ParsedDocument doc = docMapper.parse(source(b -> { b.field("field1", "value1"); }));
424415
assertNotNull(doc.rootDoc().getField("_recovery_source"));
425416
assertThat(doc.rootDoc().getField("_recovery_source").binaryValue(), equalTo(new BytesRef("{\"field1\":\"value1\"}")));
426-
assertWarnings(SourceFieldMapper.DEPRECATION_WARNING);
427417
}
428418
{
429419
Settings settings = Settings.builder().put(INDICES_RECOVERY_SOURCE_ENABLED_SETTING.getKey(), false).build();
@@ -434,7 +424,6 @@ public void testRecoverySourceWithSyntheticSource() throws IOException {
434424
DocumentMapper docMapper = mapperService.documentMapper();
435425
ParsedDocument doc = docMapper.parse(source(b -> b.field("field1", "value1")));
436426
assertNull(doc.rootDoc().getField("_recovery_source"));
437-
assertWarnings(SourceFieldMapper.DEPRECATION_WARNING);
438427
}
439428
}
440429

@@ -629,7 +618,6 @@ public void testRecoverySourceWithLogsCustom() throws IOException {
629618
ParsedDocument doc = docMapper.parse(source(b -> { b.field("@timestamp", "2012-02-13"); }));
630619
assertNotNull(doc.rootDoc().getField("_recovery_source"));
631620
assertThat(doc.rootDoc().getField("_recovery_source").binaryValue(), equalTo(new BytesRef("{\"@timestamp\":\"2012-02-13\"}")));
632-
assertWarnings(SourceFieldMapper.DEPRECATION_WARNING);
633621
}
634622
{
635623
Settings settings = Settings.builder()
@@ -640,7 +628,6 @@ public void testRecoverySourceWithLogsCustom() throws IOException {
640628
DocumentMapper docMapper = mapperService.documentMapper();
641629
ParsedDocument doc = docMapper.parse(source(b -> b.field("@timestamp", "2012-02-13")));
642630
assertNull(doc.rootDoc().getField("_recovery_source"));
643-
assertWarnings(SourceFieldMapper.DEPRECATION_WARNING);
644631
}
645632
}
646633

@@ -709,7 +696,6 @@ public void testRecoverySourceWithTimeSeriesCustom() throws IOException {
709696
doc.rootDoc().getField("_recovery_source").binaryValue(),
710697
equalTo(new BytesRef("{\"@timestamp\":\"2012-02-13\",\"field\":\"value1\"}"))
711698
);
712-
assertWarnings(SourceFieldMapper.DEPRECATION_WARNING);
713699
}
714700
{
715701
Settings settings = Settings.builder()
@@ -723,7 +709,6 @@ public void testRecoverySourceWithTimeSeriesCustom() throws IOException {
723709
source("123", b -> b.field("@timestamp", "2012-02-13").field("field", randomAlphaOfLength(5)), null)
724710
);
725711
assertNull(doc.rootDoc().getField("_recovery_source"));
726-
assertWarnings(SourceFieldMapper.DEPRECATION_WARNING);
727712
}
728713
}
729714
}

server/src/test/java/org/elasticsearch/index/shard/ShardGetServiceTests.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.elasticsearch.index.engine.VersionConflictEngineException;
2222
import org.elasticsearch.index.get.GetResult;
2323
import org.elasticsearch.index.mapper.RoutingFieldMapper;
24-
import org.elasticsearch.index.mapper.SourceFieldMapper;
2524
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
2625
import org.elasticsearch.xcontent.XContentType;
2726

@@ -115,7 +114,6 @@ public void testGetFromTranslogWithSyntheticSource() throws IOException {
115114
"mode": "synthetic"
116115
""";
117116
runGetFromTranslogWithOptions(docToIndex, sourceOptions, expectedFetchedSource, "\"long\"", 7L, true);
118-
assertWarnings(SourceFieldMapper.DEPRECATION_WARNING);
119117
}
120118

121119
public void testGetFromTranslogWithDenseVector() throws IOException {

0 commit comments

Comments
 (0)