Skip to content

Commit 110b206

Browse files
authored
Node deprecation warning for indexes and component templates with sou… (#120387)
* Node deprecation warning for indexes and component templates with source mode in mapping * remove index warnings * restrict to component templates * refine
1 parent 4e0e36b commit 110b206

File tree

5 files changed

+105
-29
lines changed

5 files changed

+105
-29
lines changed

x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/DeprecationChecks.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ private DeprecationChecks() {}
8888
NodeDeprecationChecks::checkEqlEnabledSetting,
8989
NodeDeprecationChecks::checkNodeAttrData,
9090
NodeDeprecationChecks::checkWatcherBulkConcurrentRequestsSetting,
91-
NodeDeprecationChecks::checkTracingApmSettings
91+
NodeDeprecationChecks::checkTracingApmSettings,
92+
NodeDeprecationChecks::checkSourceModeInComponentTemplates
9293
);
9394

9495
static List<BiFunction<IndexMetadata, ClusterState, DeprecationIssue>> INDEX_SETTINGS_CHECKS = List.of(
@@ -97,8 +98,7 @@ private DeprecationChecks() {}
9798
IndexDeprecationChecks::checkIndexDataPath,
9899
IndexDeprecationChecks::storeTypeSettingCheck,
99100
IndexDeprecationChecks::frozenIndexSettingCheck,
100-
IndexDeprecationChecks::deprecatedCamelCasePattern,
101-
IndexDeprecationChecks::checkSourceModeInMapping
101+
IndexDeprecationChecks::deprecatedCamelCasePattern
102102
);
103103

104104
static List<BiFunction<DataStream, ClusterState, DeprecationIssue>> DATA_STREAM_CHECKS = List.of(

x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecks.java

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import org.elasticsearch.index.IndexSettings;
1616
import org.elasticsearch.index.IndexVersion;
1717
import org.elasticsearch.index.engine.frozen.FrozenEngine;
18-
import org.elasticsearch.index.mapper.SourceFieldMapper;
1918
import org.elasticsearch.xpack.core.deprecation.DeprecatedIndexPredicate;
2019
import org.elasticsearch.xpack.core.deprecation.DeprecationIssue;
2120

@@ -203,31 +202,6 @@ static List<String> findInPropertiesRecursively(
203202
return issues;
204203
}
205204

206-
static DeprecationIssue checkSourceModeInMapping(IndexMetadata indexMetadata, ClusterState clusterState) {
207-
if (SourceFieldMapper.onOrAfterDeprecateModeVersion(indexMetadata.getCreationVersion())) {
208-
boolean[] useSourceMode = { false };
209-
fieldLevelMappingIssue(indexMetadata, ((mappingMetadata, sourceAsMap) -> {
210-
Object source = sourceAsMap.get("_source");
211-
if (source instanceof Map<?, ?> sourceMap) {
212-
if (sourceMap.containsKey("mode")) {
213-
useSourceMode[0] = true;
214-
}
215-
}
216-
}));
217-
if (useSourceMode[0]) {
218-
return new DeprecationIssue(
219-
DeprecationIssue.Level.CRITICAL,
220-
SourceFieldMapper.DEPRECATION_WARNING,
221-
"https://github.com/elastic/elasticsearch/pull/117172",
222-
SourceFieldMapper.DEPRECATION_WARNING,
223-
false,
224-
null
225-
);
226-
}
227-
}
228-
return null;
229-
}
230-
231205
static DeprecationIssue deprecatedCamelCasePattern(IndexMetadata indexMetadata, ClusterState clusterState) {
232206
List<String> fields = new ArrayList<>();
233207
fieldLevelMappingIssue(

x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/NodeDeprecationChecks.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@
99

1010
import org.elasticsearch.action.admin.cluster.node.info.PluginsAndModules;
1111
import org.elasticsearch.cluster.ClusterState;
12+
import org.elasticsearch.cluster.metadata.ComponentTemplate;
1213
import org.elasticsearch.cluster.routing.allocation.DataTier;
1314
import org.elasticsearch.common.settings.SecureSetting;
1415
import org.elasticsearch.common.settings.Setting;
1516
import org.elasticsearch.common.settings.Settings;
17+
import org.elasticsearch.common.xcontent.XContentHelper;
1618
import org.elasticsearch.core.TimeValue;
1719
import org.elasticsearch.env.Environment;
20+
import org.elasticsearch.index.mapper.SourceFieldMapper;
1821
import org.elasticsearch.license.XPackLicenseState;
1922
import org.elasticsearch.script.ScriptService;
2023
import org.elasticsearch.xpack.core.deprecation.DeprecationIssue;
@@ -1012,4 +1015,43 @@ static DeprecationIssue checkTracingApmSettings(
10121015
DeprecationIssue.Level.CRITICAL
10131016
);
10141017
}
1018+
1019+
static DeprecationIssue checkSourceModeInComponentTemplates(
1020+
final Settings settings,
1021+
final PluginsAndModules pluginsAndModules,
1022+
final ClusterState clusterState,
1023+
final XPackLicenseState licenseState
1024+
) {
1025+
List<String> templates = new ArrayList<>();
1026+
var templateNames = clusterState.metadata().componentTemplates().keySet();
1027+
for (String templateName : templateNames) {
1028+
ComponentTemplate template = clusterState.metadata().componentTemplates().get(templateName);
1029+
if (template.template().mappings() != null) {
1030+
var sourceAsMap = (Map<?, ?>) XContentHelper.convertToMap(template.template().mappings().uncompressed(), true)
1031+
.v2()
1032+
.get("_doc");
1033+
if (sourceAsMap != null) {
1034+
Object source = sourceAsMap.get("_source");
1035+
if (source instanceof Map<?, ?> sourceMap) {
1036+
if (sourceMap.containsKey("mode")) {
1037+
templates.add(templateName);
1038+
}
1039+
}
1040+
}
1041+
}
1042+
1043+
}
1044+
if (templates.isEmpty()) {
1045+
return null;
1046+
}
1047+
Collections.sort(templates);
1048+
return new DeprecationIssue(
1049+
DeprecationIssue.Level.CRITICAL,
1050+
SourceFieldMapper.DEPRECATION_WARNING,
1051+
"https://github.com/elastic/elasticsearch/pull/117172",
1052+
SourceFieldMapper.DEPRECATION_WARNING + " Affected component templates: [" + String.join(", ", templates) + "]",
1053+
false,
1054+
null
1055+
);
1056+
}
10151057
}

x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/NodeDeprecationChecksTests.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,29 @@
1111
import org.elasticsearch.action.admin.cluster.node.info.PluginsAndModules;
1212
import org.elasticsearch.cluster.ClusterName;
1313
import org.elasticsearch.cluster.ClusterState;
14+
import org.elasticsearch.cluster.metadata.ComponentTemplate;
1415
import org.elasticsearch.cluster.metadata.Metadata;
16+
import org.elasticsearch.cluster.metadata.Template;
1517
import org.elasticsearch.cluster.routing.allocation.DataTier;
1618
import org.elasticsearch.common.Strings;
19+
import org.elasticsearch.common.compress.CompressedXContent;
1720
import org.elasticsearch.common.settings.MockSecureSettings;
1821
import org.elasticsearch.common.settings.Setting;
1922
import org.elasticsearch.common.settings.Settings;
2023
import org.elasticsearch.core.TimeValue;
2124
import org.elasticsearch.env.Environment;
25+
import org.elasticsearch.index.mapper.SourceFieldMapper;
2226
import org.elasticsearch.license.XPackLicenseState;
2327
import org.elasticsearch.script.ScriptService;
2428
import org.elasticsearch.test.ESTestCase;
2529
import org.elasticsearch.xpack.core.deprecation.DeprecationIssue;
2630
import org.elasticsearch.xpack.core.ilm.LifecycleSettings;
2731

32+
import java.io.IOException;
2833
import java.util.ArrayList;
2934
import java.util.Arrays;
3035
import java.util.Collections;
36+
import java.util.HashMap;
3137
import java.util.List;
3238
import java.util.Map;
3339
import java.util.stream.Collectors;
@@ -832,4 +838,42 @@ public void testCheckNodeAttrData() {
832838
);
833839
assertThat(issues, hasItem(expected));
834840
}
841+
842+
public void testCheckSourceModeInComponentTemplates() throws IOException {
843+
Template template = Template.builder().mappings(CompressedXContent.fromJSON("""
844+
{ "_doc": { "_source": { "mode": "stored"} } }""")).build();
845+
ComponentTemplate componentTemplate = new ComponentTemplate(template, 1L, new HashMap<>());
846+
847+
Template template2 = Template.builder().mappings(CompressedXContent.fromJSON("""
848+
{ "_doc": { "_source": { "enabled": false} } }""")).build();
849+
ComponentTemplate componentTemplate2 = new ComponentTemplate(template2, 1L, new HashMap<>());
850+
851+
ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE)
852+
.metadata(
853+
Metadata.builder()
854+
.componentTemplates(
855+
Map.of("my-template-1", componentTemplate, "my-template-2", componentTemplate, "my-template-3", componentTemplate2)
856+
)
857+
)
858+
.build();
859+
860+
final List<DeprecationIssue> issues = DeprecationChecks.filterChecks(
861+
DeprecationChecks.NODE_SETTINGS_CHECKS,
862+
c -> c.apply(
863+
Settings.EMPTY,
864+
new PluginsAndModules(Collections.emptyList(), Collections.emptyList()),
865+
clusterState,
866+
new XPackLicenseState(() -> 0)
867+
)
868+
);
869+
final DeprecationIssue expected = new DeprecationIssue(
870+
DeprecationIssue.Level.CRITICAL,
871+
SourceFieldMapper.DEPRECATION_WARNING,
872+
"https://github.com/elastic/elasticsearch/pull/117172",
873+
SourceFieldMapper.DEPRECATION_WARNING + " Affected component templates: [my-template-1, my-template-2]",
874+
false,
875+
null
876+
);
877+
assertThat(issues, hasItem(expected));
878+
}
835879
}

x-pack/plugin/logsdb/src/javaRestTest/java/org/elasticsearch/xpack/logsdb/LogsIndexModeCustomSettingsIT.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@ public void testConfigureStoredSourceBeforeIndexCreation() throws IOException {
122122
var mapping = getMapping(client, getDataStreamBackingIndex(client, "logs-custom-dev", 0));
123123
String sourceMode = (String) subObject("_source").apply(mapping).get("mode");
124124
assertThat(sourceMode, equalTo("stored"));
125+
126+
request = new Request("GET", "/_migration/deprecations");
127+
var nodeSettings = (Map<?, ?>) ((List<?>) entityAsMap(client.performRequest(request)).get("node_settings")).getFirst();
128+
assertThat(nodeSettings.get("message"), equalTo(SourceFieldMapper.DEPRECATION_WARNING));
129+
assertThat(
130+
(String) nodeSettings.get("details"),
131+
containsString(SourceFieldMapper.DEPRECATION_WARNING + " Affected component templates: [logs@custom]")
132+
);
125133
}
126134

127135
public void testConfigureDisabledSourceBeforeIndexCreation() {
@@ -196,6 +204,14 @@ public void testConfigureStoredSourceWhenIndexIsCreated() throws IOException {
196204
var mapping = getMapping(client, getDataStreamBackingIndex(client, "logs-custom-dev", 0));
197205
String sourceMode = (String) subObject("_source").apply(mapping).get("mode");
198206
assertThat(sourceMode, equalTo("stored"));
207+
208+
request = new Request("GET", "/_migration/deprecations");
209+
var nodeSettings = (Map<?, ?>) ((List<?>) entityAsMap(client.performRequest(request)).get("node_settings")).getFirst();
210+
assertThat(nodeSettings.get("message"), equalTo(SourceFieldMapper.DEPRECATION_WARNING));
211+
assertThat(
212+
(String) nodeSettings.get("details"),
213+
containsString(SourceFieldMapper.DEPRECATION_WARNING + " Affected component templates: [logs@custom]")
214+
);
199215
}
200216

201217
public void testConfigureDisabledSourceWhenIndexIsCreated() throws IOException {

0 commit comments

Comments
 (0)