Skip to content

Commit 88f07a8

Browse files
kkrik-eselasticsearchmachine
andauthored
[8.x] Node deprecation warning for indexes and component templates with sou… (elastic#120387) (elastic#120493)
* Node deprecation warning for indexes and component templates with sou… (elastic#120387) * Node deprecation warning for indexes and component templates with source mode in mapping * remove index warnings * restrict to component templates * refine (cherry picked from commit 110b206) # Conflicts: # x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecks.java * [CI] Auto commit changes from spotless * fix compilation error --------- Co-authored-by: elasticsearchmachine <[email protected]>
1 parent 4ddd9b6 commit 88f07a8

File tree

5 files changed

+105
-30
lines changed

5 files changed

+105
-30
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
@@ -89,7 +89,8 @@ private DeprecationChecks() {}
8989
NodeDeprecationChecks::checkEqlEnabledSetting,
9090
NodeDeprecationChecks::checkNodeAttrData,
9191
NodeDeprecationChecks::checkWatcherBulkConcurrentRequestsSetting,
92-
NodeDeprecationChecks::checkTracingApmSettings
92+
NodeDeprecationChecks::checkTracingApmSettings,
93+
NodeDeprecationChecks::checkSourceModeInComponentTemplates
9394
);
9495

9596
static List<BiFunction<IndexMetadata, ClusterState, DeprecationIssue>> INDEX_SETTINGS_CHECKS = List.of(
@@ -98,8 +99,7 @@ private DeprecationChecks() {}
9899
IndexDeprecationChecks::checkIndexDataPath,
99100
IndexDeprecationChecks::storeTypeSettingCheck,
100101
IndexDeprecationChecks::frozenIndexSettingCheck,
101-
IndexDeprecationChecks::deprecatedCamelCasePattern,
102-
IndexDeprecationChecks::checkSourceModeInMapping
102+
IndexDeprecationChecks::deprecatedCamelCasePattern
103103
);
104104

105105
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 & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
import org.elasticsearch.index.IndexModule;
1515
import org.elasticsearch.index.IndexSettings;
1616
import org.elasticsearch.index.IndexVersion;
17-
import org.elasticsearch.index.IndexVersions;
1817
import org.elasticsearch.index.engine.frozen.FrozenEngine;
19-
import org.elasticsearch.index.mapper.SourceFieldMapper;
2018
import org.elasticsearch.xpack.core.deprecation.DeprecatedIndexPredicate;
2119
import org.elasticsearch.xpack.core.deprecation.DeprecationIssue;
2220

@@ -204,31 +202,6 @@ static List<String> findInPropertiesRecursively(
204202
return issues;
205203
}
206204

207-
static DeprecationIssue checkSourceModeInMapping(IndexMetadata indexMetadata, ClusterState clusterState) {
208-
if (indexMetadata.getCreationVersion().onOrAfter(IndexVersions.DEPRECATE_SOURCE_MODE_MAPPER)) {
209-
boolean[] useSourceMode = { false };
210-
fieldLevelMappingIssue(indexMetadata, ((mappingMetadata, sourceAsMap) -> {
211-
Object source = sourceAsMap.get("_source");
212-
if (source instanceof Map<?, ?> sourceMap) {
213-
if (sourceMap.containsKey("mode")) {
214-
useSourceMode[0] = true;
215-
}
216-
}
217-
}));
218-
if (useSourceMode[0]) {
219-
return new DeprecationIssue(
220-
DeprecationIssue.Level.CRITICAL,
221-
SourceFieldMapper.DEPRECATION_WARNING,
222-
"https://github.com/elastic/elasticsearch/pull/117172",
223-
SourceFieldMapper.DEPRECATION_WARNING,
224-
false,
225-
null
226-
);
227-
}
228-
}
229-
return null;
230-
}
231-
232205
static DeprecationIssue deprecatedCamelCasePattern(IndexMetadata indexMetadata, ClusterState clusterState) {
233206
List<String> fields = new ArrayList<>();
234207
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,13 +9,16 @@
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.cluster.routing.allocation.decider.DiskThresholdDecider;
1415
import org.elasticsearch.common.settings.SecureSetting;
1516
import org.elasticsearch.common.settings.Setting;
1617
import org.elasticsearch.common.settings.Settings;
18+
import org.elasticsearch.common.xcontent.XContentHelper;
1719
import org.elasticsearch.core.TimeValue;
1820
import org.elasticsearch.env.Environment;
21+
import org.elasticsearch.index.mapper.SourceFieldMapper;
1922
import org.elasticsearch.license.XPackLicenseState;
2023
import org.elasticsearch.script.ScriptService;
2124
import org.elasticsearch.xpack.core.deprecation.DeprecationIssue;
@@ -1035,4 +1038,43 @@ static DeprecationIssue checkTracingApmSettings(
10351038
DeprecationIssue.Level.CRITICAL
10361039
);
10371040
}
1041+
1042+
static DeprecationIssue checkSourceModeInComponentTemplates(
1043+
final Settings settings,
1044+
final PluginsAndModules pluginsAndModules,
1045+
final ClusterState clusterState,
1046+
final XPackLicenseState licenseState
1047+
) {
1048+
List<String> templates = new ArrayList<>();
1049+
var templateNames = clusterState.metadata().componentTemplates().keySet();
1050+
for (String templateName : templateNames) {
1051+
ComponentTemplate template = clusterState.metadata().componentTemplates().get(templateName);
1052+
if (template.template().mappings() != null) {
1053+
var sourceAsMap = (Map<?, ?>) XContentHelper.convertToMap(template.template().mappings().uncompressed(), true)
1054+
.v2()
1055+
.get("_doc");
1056+
if (sourceAsMap != null) {
1057+
Object source = sourceAsMap.get("_source");
1058+
if (source instanceof Map<?, ?> sourceMap) {
1059+
if (sourceMap.containsKey("mode")) {
1060+
templates.add(templateName);
1061+
}
1062+
}
1063+
}
1064+
}
1065+
1066+
}
1067+
if (templates.isEmpty()) {
1068+
return null;
1069+
}
1070+
Collections.sort(templates);
1071+
return new DeprecationIssue(
1072+
DeprecationIssue.Level.CRITICAL,
1073+
SourceFieldMapper.DEPRECATION_WARNING,
1074+
"https://github.com/elastic/elasticsearch/pull/117172",
1075+
SourceFieldMapper.DEPRECATION_WARNING + " Affected component templates: [" + String.join(", ", templates) + "]",
1076+
false,
1077+
null
1078+
);
1079+
}
10381080
}

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,24 +11,30 @@
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.cluster.routing.allocation.decider.DiskThresholdDecider;
1719
import org.elasticsearch.common.Strings;
20+
import org.elasticsearch.common.compress.CompressedXContent;
1821
import org.elasticsearch.common.settings.MockSecureSettings;
1922
import org.elasticsearch.common.settings.Setting;
2023
import org.elasticsearch.common.settings.Settings;
2124
import org.elasticsearch.core.TimeValue;
2225
import org.elasticsearch.env.Environment;
26+
import org.elasticsearch.index.mapper.SourceFieldMapper;
2327
import org.elasticsearch.license.XPackLicenseState;
2428
import org.elasticsearch.script.ScriptService;
2529
import org.elasticsearch.test.ESTestCase;
2630
import org.elasticsearch.xpack.core.deprecation.DeprecationIssue;
2731
import org.elasticsearch.xpack.core.ilm.LifecycleSettings;
2832

33+
import java.io.IOException;
2934
import java.util.ArrayList;
3035
import java.util.Arrays;
3136
import java.util.Collections;
37+
import java.util.HashMap;
3238
import java.util.List;
3339
import java.util.Map;
3440
import java.util.stream.Collectors;
@@ -860,4 +866,42 @@ public void testCheckNodeAttrData() {
860866
);
861867
assertThat(issues, hasItem(expected));
862868
}
869+
870+
public void testCheckSourceModeInComponentTemplates() throws IOException {
871+
Template template = Template.builder().mappings(CompressedXContent.fromJSON("""
872+
{ "_doc": { "_source": { "mode": "stored"} } }""")).build();
873+
ComponentTemplate componentTemplate = new ComponentTemplate(template, 1L, new HashMap<>());
874+
875+
Template template2 = Template.builder().mappings(CompressedXContent.fromJSON("""
876+
{ "_doc": { "_source": { "enabled": false} } }""")).build();
877+
ComponentTemplate componentTemplate2 = new ComponentTemplate(template2, 1L, new HashMap<>());
878+
879+
ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE)
880+
.metadata(
881+
Metadata.builder()
882+
.componentTemplates(
883+
Map.of("my-template-1", componentTemplate, "my-template-2", componentTemplate, "my-template-3", componentTemplate2)
884+
)
885+
)
886+
.build();
887+
888+
final List<DeprecationIssue> issues = DeprecationChecks.filterChecks(
889+
DeprecationChecks.NODE_SETTINGS_CHECKS,
890+
c -> c.apply(
891+
Settings.EMPTY,
892+
new PluginsAndModules(Collections.emptyList(), Collections.emptyList()),
893+
clusterState,
894+
new XPackLicenseState(() -> 0)
895+
)
896+
);
897+
final DeprecationIssue expected = new DeprecationIssue(
898+
DeprecationIssue.Level.CRITICAL,
899+
SourceFieldMapper.DEPRECATION_WARNING,
900+
"https://github.com/elastic/elasticsearch/pull/117172",
901+
SourceFieldMapper.DEPRECATION_WARNING + " Affected component templates: [my-template-1, my-template-2]",
902+
false,
903+
null
904+
);
905+
assertThat(issues, hasItem(expected));
906+
}
863907
}

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
@@ -124,6 +124,14 @@ public void testConfigureStoredSourceBeforeIndexCreation() throws IOException {
124124
var mapping = getMapping(client, getDataStreamBackingIndex(client, "logs-custom-dev", 0));
125125
String sourceMode = (String) subObject("_source").apply(mapping).get("mode");
126126
assertThat(sourceMode, equalTo("stored"));
127+
128+
request = new Request("GET", "/_migration/deprecations");
129+
var nodeSettings = (Map<?, ?>) ((List<?>) entityAsMap(client.performRequest(request)).get("node_settings")).get(0);
130+
assertThat(nodeSettings.get("message"), equalTo(SourceFieldMapper.DEPRECATION_WARNING));
131+
assertThat(
132+
(String) nodeSettings.get("details"),
133+
containsString(SourceFieldMapper.DEPRECATION_WARNING + " Affected component templates: [logs@custom]")
134+
);
127135
}
128136

129137
public void testConfigureDisabledSourceBeforeIndexCreation() {
@@ -198,6 +206,14 @@ public void testConfigureStoredSourceWhenIndexIsCreated() throws IOException {
198206
var mapping = getMapping(client, getDataStreamBackingIndex(client, "logs-custom-dev", 0));
199207
String sourceMode = (String) subObject("_source").apply(mapping).get("mode");
200208
assertThat(sourceMode, equalTo("stored"));
209+
210+
request = new Request("GET", "/_migration/deprecations");
211+
var nodeSettings = (Map<?, ?>) ((List<?>) entityAsMap(client.performRequest(request)).get("node_settings")).get(0);
212+
assertThat(nodeSettings.get("message"), equalTo(SourceFieldMapper.DEPRECATION_WARNING));
213+
assertThat(
214+
(String) nodeSettings.get("details"),
215+
containsString(SourceFieldMapper.DEPRECATION_WARNING + " Affected component templates: [logs@custom]")
216+
);
201217
}
202218

203219
public void testConfigureDisabledSourceWhenIndexIsCreated() throws IOException {

0 commit comments

Comments
 (0)