From 5f0f7c7f261a885b3ae3f0fb0fd3ca9d06bda6dd Mon Sep 17 00:00:00 2001 From: Keith Massey Date: Tue, 17 Aug 2021 16:55:12 -0500 Subject: [PATCH 1/7] Adding deprecation checks for geo_shape parameters --- .../deprecation/ClusterDeprecationChecks.java | 37 ++++++++++ .../xpack/deprecation/DeprecationChecks.java | 6 +- .../deprecation/IndexDeprecationChecks.java | 36 ++++++++++ .../ClusterDeprecationChecksTests.java | 49 ++++++++++++++ .../IndexDeprecationChecksTests.java | 67 +++++++++++++++++++ 5 files changed, 193 insertions(+), 2 deletions(-) diff --git a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecks.java b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecks.java index 84910d26ef5ce..12abff01b4daa 100644 --- a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecks.java +++ b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecks.java @@ -10,14 +10,18 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.metadata.IndexTemplateMetadata; import org.elasticsearch.cluster.metadata.MappingMetadata; import org.elasticsearch.common.Strings; import org.elasticsearch.common.collect.ImmutableOpenMap; import org.elasticsearch.common.compress.CompressedXContent; import org.elasticsearch.common.xcontent.XContentHelper; +import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.core.TimeValue; +import org.elasticsearch.core.Tuple; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.mapper.FieldNamesFieldMapper; +import org.elasticsearch.index.mapper.LegacyGeoShapeFieldMapper; import org.elasticsearch.ingest.IngestService; import org.elasticsearch.ingest.PipelineConfiguration; @@ -29,6 +33,7 @@ import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; +import java.util.stream.StreamSupport; import static org.elasticsearch.cluster.routing.allocation.DiskThresholdSettings.CLUSTER_ROUTING_ALLOCATION_INCLUDE_RELOCATIONS_SETTING; import static org.elasticsearch.search.SearchModule.INDICES_MAX_CLAUSE_COUNT_SETTING; @@ -201,4 +206,36 @@ static DeprecationIssue checkClusterRoutingAllocationIncludeRelocationsSetting(f DeprecationIssue.Level.WARNING ); } + + @SuppressWarnings("unchecked") + static DeprecationIssue checkGeoShapeTemplates(final ClusterState clusterState) { + String messageForCluster = + StreamSupport.stream(clusterState.getMetadata().getTemplates().spliterator(), false).map((templateCursor) -> { + String templateName = templateCursor.key; + IndexTemplateMetadata indexTemplateMetadata = templateCursor.value; + String messageForTemplate = + StreamSupport.stream(indexTemplateMetadata.getMappings().spliterator(), false).map((mappingCursor) -> { + CompressedXContent mapping = mappingCursor.value; + Tuple> tuple = XContentHelper.convertToMap(mapping.uncompressed(), true, XContentType.JSON); + Map mappingAsMap = (Map) tuple.v2().get("_doc"); + List messages = IndexDeprecationChecks.findInPropertiesRecursively(LegacyGeoShapeFieldMapper.CONTENT_TYPE, + mappingAsMap, + IndexDeprecationChecks::isGeoShapeFieldWithDeprecatedParam, + IndexDeprecationChecks::formatDeprecatedGeoShapeParamMessage); + return messages; + }).filter(messages -> !messages.isEmpty()).map(messages -> { + String messageForMapping = + "mappings in template " + templateName + " contains deprecated properties. " + + messages.stream().collect(Collectors.joining("; ")); + return messageForMapping; + }).collect(Collectors.joining("; ")); + return messageForTemplate; + }).filter(messageForTempalte -> !Strings.isEmpty(messageForTempalte)).collect(Collectors.joining("; ")); + if (Strings.isEmpty(messageForCluster)) { + return null; + } else { + return new DeprecationIssue(DeprecationIssue.Level.CRITICAL, messageForCluster, "https://www.elastic" + + ".co/guide/en/elasticsearch/reference/master/breaking-changes-8.0.html", messageForCluster, false, null); + } + } } diff --git a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/DeprecationChecks.java b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/DeprecationChecks.java index dddccceb358ac..06eb504b7a61c 100644 --- a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/DeprecationChecks.java +++ b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/DeprecationChecks.java @@ -38,7 +38,8 @@ private DeprecationChecks() { ClusterDeprecationChecks::checkPollIntervalTooLow, ClusterDeprecationChecks::checkTemplatesWithFieldNamesDisabled, ClusterDeprecationChecks::checkTemplatesWithMultipleTypes, - ClusterDeprecationChecks::checkClusterRoutingAllocationIncludeRelocationsSetting + ClusterDeprecationChecks::checkClusterRoutingAllocationIncludeRelocationsSetting, + ClusterDeprecationChecks::checkGeoShapeTemplates )); static final List> @@ -111,7 +112,8 @@ private DeprecationChecks() { IndexDeprecationChecks::checkIndexDataPath, IndexDeprecationChecks::indexingSlowLogLevelSettingCheck, IndexDeprecationChecks::searchSlowLogLevelSettingCheck, - IndexDeprecationChecks::storeTypeSettingCheck + IndexDeprecationChecks::storeTypeSettingCheck, + IndexDeprecationChecks::checkGeoShapeMappings )); /** diff --git a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecks.java b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecks.java index 37ffde62a06f6..115a43159fcc4 100644 --- a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecks.java +++ b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecks.java @@ -14,11 +14,13 @@ import org.elasticsearch.cluster.metadata.MappingMetadata; import org.elasticsearch.common.joda.JodaDeprecationPatterns; import org.elasticsearch.common.settings.Setting; +import org.elasticsearch.core.Tuple; import org.elasticsearch.index.IndexModule; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.IndexingSlowLog; import org.elasticsearch.index.SearchSlowLog; import org.elasticsearch.index.SlowLogLevel; +import org.elasticsearch.index.mapper.LegacyGeoShapeFieldMapper; import java.util.ArrayList; import java.util.Collections; @@ -31,6 +33,7 @@ import java.util.function.BiConsumer; import java.util.function.BiFunction; import java.util.function.Function; +import java.util.stream.Collectors; /** @@ -317,4 +320,37 @@ static DeprecationIssue storeTypeSettingCheck(IndexMetadata indexMetadata) { } return null; } + + protected static boolean isGeoShapeFieldWithDeprecatedParam(Map property) { + return LegacyGeoShapeFieldMapper.CONTENT_TYPE.equals(property.get("type")) && + LegacyGeoShapeFieldMapper.DEPRECATED_PARAMETERS.stream().anyMatch(deprecatedParameter -> + property.containsKey(deprecatedParameter) + ); + } + + protected static String formatDeprecatedGeoShapeParamMessage(String type, Map.Entry entry) { + String fieldName = entry.getKey().toString(); + Map value = (Map) entry.getValue(); + return LegacyGeoShapeFieldMapper.DEPRECATED_PARAMETERS.stream() + .filter(deprecatedParameter -> value.containsKey(deprecatedParameter)) + .map(deprecatedParameter -> String.format(Locale.ROOT, "%s parameter [%s] in field [%s] is deprecated and will be removed in " + + "a future version", type, deprecatedParameter, fieldName)) + .collect(Collectors.joining("; ")); + } + + @SuppressWarnings("unchecked") + static DeprecationIssue checkGeoShapeMappings(IndexMetadata indexMetadata) { + Map sourceAsMap = indexMetadata.mapping().getSourceAsMap(); + List messages = findInPropertiesRecursively(LegacyGeoShapeFieldMapper.CONTENT_TYPE, sourceAsMap, + IndexDeprecationChecks::isGeoShapeFieldWithDeprecatedParam, + IndexDeprecationChecks::formatDeprecatedGeoShapeParamMessage); + if (messages.isEmpty()) { + return null; + } else { + String message = messages.stream().collect(Collectors.joining("; ")); + String details = "mappings for index " + indexMetadata.getIndex().getName() + " contains deprecated properties. " + message; + String url = "https://www.elastic.co/guide/en/elasticsearch/reference/master/breaking-changes-8.0.html"; + return new DeprecationIssue(DeprecationIssue.Level.CRITICAL, message, url, details, false, null); + } + } } diff --git a/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecksTests.java b/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecksTests.java index 8063b4f9853a3..21a9e9aed3af2 100644 --- a/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecksTests.java +++ b/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecksTests.java @@ -6,10 +6,13 @@ */ package org.elasticsearch.xpack.deprecation; +import org.elasticsearch.Version; import org.elasticsearch.action.ingest.PutPipelineRequest; import org.elasticsearch.cluster.ClusterName; import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.IndexTemplateMetadata; +import org.elasticsearch.cluster.metadata.MappingMetadata; import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesArray; @@ -26,15 +29,20 @@ import java.util.Collections; import java.util.List; import java.util.Locale; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; import static java.util.Collections.singletonList; import static org.elasticsearch.cluster.routing.allocation.DiskThresholdSettings.CLUSTER_ROUTING_ALLOCATION_INCLUDE_RELOCATIONS_SETTING; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.xpack.core.ilm.LifecycleSettings.LIFECYCLE_POLL_INTERVAL_SETTING; import static org.elasticsearch.xpack.deprecation.DeprecationChecks.CLUSTER_SETTINGS_CHECKS; +import static org.elasticsearch.xpack.deprecation.DeprecationChecks.INDEX_SETTINGS_CHECKS; import static org.elasticsearch.xpack.deprecation.IndexDeprecationChecksTests.addRandomFields; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.collection.IsIterableContainingInOrder.contains; public class ClusterDeprecationChecksTests extends ESTestCase { @@ -355,4 +363,45 @@ public void testClusterRoutingAllocationIncludeRelocationsSetting() { assertWarnings(expectedWarning); } + + public void testCheckGeoShapeMappings() throws Exception { + IndexTemplateMetadata indexTemplateMetadata = IndexTemplateMetadata.builder("single-type") + .patterns(Collections.singletonList("foo")) + .putMapping("_doc", "{\n" + + " \"_doc\":{\n" + + " \"properties\":{\n" + + " \"nested_field\":{\n" + + " \"type\":\"nested\",\n" + + " \"properties\":{\n" + + " \"location\":{\n" + + " \"type\":\"geo_shape\",\n" + + " \"strategy\":\"recursive\",\n" + + " \"points_only\":true\n" + + " }\n" + + " }\n" + + " }\n" + + " }\n" + + " }\n" + + "}") + .build(); + ImmutableOpenMap templates = ImmutableOpenMap.builder() + .fPut("single-type", indexTemplateMetadata) + .build(); + Metadata badMetadata = Metadata.builder() + .templates(templates) + .build(); + ClusterState badState = ClusterState.builder(new ClusterName("test")).metadata(badMetadata).build(); + DeprecationIssue issue = ClusterDeprecationChecks.checkGeoShapeTemplates(badState); + + assertThat(issue, equalTo( + new DeprecationIssue(DeprecationIssue.Level.CRITICAL, + "mappings in template single-type contains deprecated properties. [geo_shape parameter [points_only] in field [location] " + + "is deprecated and will be removed in a future version; geo_shape parameter [strategy] in field [location] is " + + "deprecated and will be removed in a future version]", + "https://www.elastic.co/guide/en/elasticsearch/reference/master/breaking-changes-8.0.html", + "mappings in template single-type contains deprecated properties. [geo_shape parameter [points_only] in field [location] " + + "is deprecated and will be removed in a future version; geo_shape parameter [strategy] in field [location] is " + + "deprecated and will be removed in a future version]", false, null) + )); + } } diff --git a/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecksTests.java b/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecksTests.java index 205a4a6142da5..ca9aa2f5f19d8 100644 --- a/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecksTests.java +++ b/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecksTests.java @@ -9,6 +9,7 @@ import org.elasticsearch.Version; import org.elasticsearch.cluster.metadata.IndexMetadata; +import org.elasticsearch.cluster.metadata.MappingMetadata; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.joda.JodaDeprecationPatterns; @@ -26,8 +27,13 @@ import java.io.IOException; import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; +import java.util.stream.Collectors; +import java.util.stream.Stream; import static java.util.Collections.singletonList; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; @@ -497,4 +503,65 @@ public void testSimpleFSSetting() { "as it offers superior or equivalent performance to [simplefs].", false, null) )); } + + public void testCheckGeoShapeMappings() throws Exception { + Map emptyMappingMap = Collections.emptyMap(); + MappingMetadata mappingMetadata = new MappingMetadata("", emptyMappingMap); + Settings.Builder settings = settings(Version.CURRENT); + IndexMetadata indexMetadata = + IndexMetadata.builder("test").settings(settings).putMapping(mappingMetadata).numberOfShards(1).numberOfReplicas(0).build(); + List issues = DeprecationChecks.filterChecks(INDEX_SETTINGS_CHECKS, c -> c.apply(indexMetadata)); + assertTrue(issues.isEmpty()); + + Map okGeoMappingMap = Collections.singletonMap("properties", Collections.singletonMap("location", + Collections.singletonMap("type", "geo_shape"))); + mappingMetadata = new MappingMetadata("", okGeoMappingMap); + IndexMetadata indexMetadata2 = + IndexMetadata.builder("test").settings(settings).putMapping(mappingMetadata).numberOfShards(1).numberOfReplicas(0).build(); + issues = DeprecationChecks.filterChecks(INDEX_SETTINGS_CHECKS, c -> c.apply(indexMetadata2)); + assertTrue(issues.isEmpty()); + + Map deprecatedPropertiesMap = Stream.of(new String[][] { + { "type", "geo_shape" }, + { "strategy", "recursive" }, + { "points_only", "true" } + }).collect(Collectors.toMap(data -> data[0], data -> data[1])); + Map deprecatedGeoMappingMap = Collections.singletonMap("properties", Collections.singletonMap("location", + deprecatedPropertiesMap)); + mappingMetadata = new MappingMetadata("", deprecatedGeoMappingMap); + IndexMetadata indexMetadata3 = + IndexMetadata.builder("test").settings(settings).putMapping(mappingMetadata).numberOfShards(1).numberOfReplicas(0).build(); + issues = DeprecationChecks.filterChecks(INDEX_SETTINGS_CHECKS, c -> c.apply(indexMetadata3)); + assertEquals(1, issues.size()); + assertThat(issues, contains( + new DeprecationIssue(DeprecationIssue.Level.CRITICAL, + "[geo_shape parameter [points_only] in field [location] is deprecated and will be removed in a future version; geo_shape " + + "parameter [strategy] in field [location] is deprecated and will be removed in a future version]", + "https://www.elastic.co/guide/en/elasticsearch/reference/master/breaking-changes-8.0.html", + "mappings for index test contains deprecated properties. [geo_shape parameter [points_only] in field [location] is " + + "deprecated and will be removed in a future version; geo_shape parameter [strategy] in field [location] is deprecated" + + " and will be removed in a future version]", false, null) + )); + + Map nestedProperties = Stream.of(new Object[][] { + { "type", "nested" }, + { "properties", Collections.singletonMap("location", deprecatedPropertiesMap) }, + }).collect(Collectors.toMap(data -> (String) data[0], data -> (Object) data[1])); + Map nestedDeprecatedGeoMappingMap = Collections.singletonMap("properties", + Collections.singletonMap("nested_field", nestedProperties)); + mappingMetadata = new MappingMetadata("", nestedDeprecatedGeoMappingMap); + IndexMetadata indexMetadata4 = + IndexMetadata.builder("test").settings(settings).putMapping(mappingMetadata).numberOfShards(1).numberOfReplicas(0).build(); + issues = DeprecationChecks.filterChecks(INDEX_SETTINGS_CHECKS, c -> c.apply(indexMetadata4)); + assertEquals(1, issues.size()); + assertThat(issues, contains( + new DeprecationIssue(DeprecationIssue.Level.CRITICAL, + "[geo_shape parameter [points_only] in field [location] is deprecated and will be removed in a future version; geo_shape " + + "parameter [strategy] in field [location] is deprecated and will be removed in a future version]", + "https://www.elastic.co/guide/en/elasticsearch/reference/master/breaking-changes-8.0.html", + "mappings for index test contains deprecated properties. [geo_shape parameter [points_only] in field [location] is " + + "deprecated and will be removed in a future version; geo_shape parameter [strategy] in field [location] is deprecated" + + " and will be removed in a future version]", false, null) + )); + } } From 8d23a22f64211161409c4eebcae4b964f62fcadd Mon Sep 17 00:00:00 2001 From: Keith Massey Date: Thu, 26 Aug 2021 15:28:51 -0500 Subject: [PATCH 2/7] fixing precommit check problems --- .../xpack/deprecation/ClusterDeprecationChecks.java | 7 ++++--- .../xpack/deprecation/IndexDeprecationChecks.java | 1 - .../xpack/deprecation/ClusterDeprecationChecksTests.java | 8 -------- .../xpack/deprecation/IndexDeprecationChecksTests.java | 3 +-- 4 files changed, 5 insertions(+), 14 deletions(-) diff --git a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecks.java b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecks.java index 12abff01b4daa..e73c5575c2cf1 100644 --- a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecks.java +++ b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecks.java @@ -216,21 +216,22 @@ static DeprecationIssue checkGeoShapeTemplates(final ClusterState clusterState) String messageForTemplate = StreamSupport.stream(indexTemplateMetadata.getMappings().spliterator(), false).map((mappingCursor) -> { CompressedXContent mapping = mappingCursor.value; - Tuple> tuple = XContentHelper.convertToMap(mapping.uncompressed(), true, XContentType.JSON); + Tuple> tuple = XContentHelper.convertToMap(mapping.uncompressed(), true, + XContentType.JSON); Map mappingAsMap = (Map) tuple.v2().get("_doc"); List messages = IndexDeprecationChecks.findInPropertiesRecursively(LegacyGeoShapeFieldMapper.CONTENT_TYPE, mappingAsMap, IndexDeprecationChecks::isGeoShapeFieldWithDeprecatedParam, IndexDeprecationChecks::formatDeprecatedGeoShapeParamMessage); return messages; - }).filter(messages -> !messages.isEmpty()).map(messages -> { + }).filter(messages -> messages.isEmpty() == false).map(messages -> { String messageForMapping = "mappings in template " + templateName + " contains deprecated properties. " + messages.stream().collect(Collectors.joining("; ")); return messageForMapping; }).collect(Collectors.joining("; ")); return messageForTemplate; - }).filter(messageForTempalte -> !Strings.isEmpty(messageForTempalte)).collect(Collectors.joining("; ")); + }).filter(messageForTempalte -> Strings.isEmpty(messageForTempalte) == false).collect(Collectors.joining("; ")); if (Strings.isEmpty(messageForCluster)) { return null; } else { diff --git a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecks.java b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecks.java index 115a43159fcc4..14a6d66ef9ec2 100644 --- a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecks.java +++ b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecks.java @@ -14,7 +14,6 @@ import org.elasticsearch.cluster.metadata.MappingMetadata; import org.elasticsearch.common.joda.JodaDeprecationPatterns; import org.elasticsearch.common.settings.Setting; -import org.elasticsearch.core.Tuple; import org.elasticsearch.index.IndexModule; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.IndexingSlowLog; diff --git a/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecksTests.java b/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecksTests.java index 21a9e9aed3af2..240e5f616e4d3 100644 --- a/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecksTests.java +++ b/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecksTests.java @@ -6,13 +6,10 @@ */ package org.elasticsearch.xpack.deprecation; -import org.elasticsearch.Version; import org.elasticsearch.action.ingest.PutPipelineRequest; import org.elasticsearch.cluster.ClusterName; import org.elasticsearch.cluster.ClusterState; -import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.IndexTemplateMetadata; -import org.elasticsearch.cluster.metadata.MappingMetadata; import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesArray; @@ -29,20 +26,15 @@ import java.util.Collections; import java.util.List; import java.util.Locale; -import java.util.Map; -import java.util.stream.Collectors; -import java.util.stream.Stream; import static java.util.Collections.singletonList; import static org.elasticsearch.cluster.routing.allocation.DiskThresholdSettings.CLUSTER_ROUTING_ALLOCATION_INCLUDE_RELOCATIONS_SETTING; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.xpack.core.ilm.LifecycleSettings.LIFECYCLE_POLL_INTERVAL_SETTING; import static org.elasticsearch.xpack.deprecation.DeprecationChecks.CLUSTER_SETTINGS_CHECKS; -import static org.elasticsearch.xpack.deprecation.DeprecationChecks.INDEX_SETTINGS_CHECKS; import static org.elasticsearch.xpack.deprecation.IndexDeprecationChecksTests.addRandomFields; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasSize; -import static org.hamcrest.collection.IsIterableContainingInOrder.contains; public class ClusterDeprecationChecksTests extends ESTestCase { diff --git a/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecksTests.java b/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecksTests.java index ca9aa2f5f19d8..ff3e094eb1668 100644 --- a/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecksTests.java +++ b/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecksTests.java @@ -28,7 +28,6 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Collections; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; @@ -546,7 +545,7 @@ public void testCheckGeoShapeMappings() throws Exception { Map nestedProperties = Stream.of(new Object[][] { { "type", "nested" }, { "properties", Collections.singletonMap("location", deprecatedPropertiesMap) }, - }).collect(Collectors.toMap(data -> (String) data[0], data -> (Object) data[1])); + }).collect(Collectors.toMap(data -> (String) data[0], data -> data[1])); Map nestedDeprecatedGeoMappingMap = Collections.singletonMap("properties", Collections.singletonMap("nested_field", nestedProperties)); mappingMetadata = new MappingMetadata("", nestedDeprecatedGeoMappingMap); From f65d66110346ad62a789ffeb53cb265ae5e43ca8 Mon Sep 17 00:00:00 2001 From: Keith Massey Date: Thu, 26 Aug 2021 15:40:31 -0500 Subject: [PATCH 3/7] fixing unit tests --- .../xpack/deprecation/IndexDeprecationChecks.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecks.java b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecks.java index 14a6d66ef9ec2..bbbbefb4b771d 100644 --- a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecks.java +++ b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecks.java @@ -339,6 +339,9 @@ protected static String formatDeprecatedGeoShapeParamMessage(String type, Map.En @SuppressWarnings("unchecked") static DeprecationIssue checkGeoShapeMappings(IndexMetadata indexMetadata) { + if (indexMetadata == null || indexMetadata.mapping() == null) { + return null; + } Map sourceAsMap = indexMetadata.mapping().getSourceAsMap(); List messages = findInPropertiesRecursively(LegacyGeoShapeFieldMapper.CONTENT_TYPE, sourceAsMap, IndexDeprecationChecks::isGeoShapeFieldWithDeprecatedParam, From d8a2fc493aa9a7fc79b7166d55cd6a5712986804 Mon Sep 17 00:00:00 2001 From: Keith Massey Date: Thu, 26 Aug 2021 15:49:11 -0500 Subject: [PATCH 4/7] fixing unit tests --- .../xpack/deprecation/ClusterDeprecationChecks.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecks.java b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecks.java index e73c5575c2cf1..f2af4474b65c8 100644 --- a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecks.java +++ b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecks.java @@ -26,6 +26,7 @@ import org.elasticsearch.ingest.PipelineConfiguration; import java.util.ArrayList; +import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -219,7 +220,8 @@ static DeprecationIssue checkGeoShapeTemplates(final ClusterState clusterState) Tuple> tuple = XContentHelper.convertToMap(mapping.uncompressed(), true, XContentType.JSON); Map mappingAsMap = (Map) tuple.v2().get("_doc"); - List messages = IndexDeprecationChecks.findInPropertiesRecursively(LegacyGeoShapeFieldMapper.CONTENT_TYPE, + List messages = mappingAsMap == null ? Collections.EMPTY_LIST : + IndexDeprecationChecks.findInPropertiesRecursively(LegacyGeoShapeFieldMapper.CONTENT_TYPE, mappingAsMap, IndexDeprecationChecks::isGeoShapeFieldWithDeprecatedParam, IndexDeprecationChecks::formatDeprecatedGeoShapeParamMessage); From 0b4d931bfb3debe9c2ef2790e8914013263e6611 Mon Sep 17 00:00:00 2001 From: Keith Massey Date: Thu, 26 Aug 2021 15:57:53 -0500 Subject: [PATCH 5/7] fixing precommit check problems --- .../xpack/deprecation/ClusterDeprecationChecks.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecks.java b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecks.java index f2af4474b65c8..52c4b7baf7e04 100644 --- a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecks.java +++ b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecks.java @@ -220,7 +220,7 @@ static DeprecationIssue checkGeoShapeTemplates(final ClusterState clusterState) Tuple> tuple = XContentHelper.convertToMap(mapping.uncompressed(), true, XContentType.JSON); Map mappingAsMap = (Map) tuple.v2().get("_doc"); - List messages = mappingAsMap == null ? Collections.EMPTY_LIST : + List messages = mappingAsMap == null ? Collections.emptyList() : IndexDeprecationChecks.findInPropertiesRecursively(LegacyGeoShapeFieldMapper.CONTENT_TYPE, mappingAsMap, IndexDeprecationChecks::isGeoShapeFieldWithDeprecatedParam, From 37ecfaf70dde1fee9a6cf6accc30d46fcbd8883a Mon Sep 17 00:00:00 2001 From: Keith Massey Date: Mon, 30 Aug 2021 09:30:10 -0500 Subject: [PATCH 6/7] code review feedback --- .../deprecation/ClusterDeprecationChecks.java | 12 ++++++----- .../deprecation/IndexDeprecationChecks.java | 12 ++++++----- .../ClusterDeprecationChecksTests.java | 11 ++++------ .../IndexDeprecationChecksTests.java | 20 ++++++++----------- 4 files changed, 26 insertions(+), 29 deletions(-) diff --git a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecks.java b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecks.java index 52c4b7baf7e04..0ef411e38d71e 100644 --- a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecks.java +++ b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecks.java @@ -210,7 +210,7 @@ static DeprecationIssue checkClusterRoutingAllocationIncludeRelocationsSetting(f @SuppressWarnings("unchecked") static DeprecationIssue checkGeoShapeTemplates(final ClusterState clusterState) { - String messageForCluster = + String detailsForCluster = StreamSupport.stream(clusterState.getMetadata().getTemplates().spliterator(), false).map((templateCursor) -> { String templateName = templateCursor.key; IndexTemplateMetadata indexTemplateMetadata = templateCursor.value; @@ -228,17 +228,19 @@ static DeprecationIssue checkGeoShapeTemplates(final ClusterState clusterState) return messages; }).filter(messages -> messages.isEmpty() == false).map(messages -> { String messageForMapping = - "mappings in template " + templateName + " contains deprecated properties. " + + "mappings in template " + templateName + " contains deprecated geo_shape properties. " + messages.stream().collect(Collectors.joining("; ")); return messageForMapping; }).collect(Collectors.joining("; ")); return messageForTemplate; }).filter(messageForTempalte -> Strings.isEmpty(messageForTempalte) == false).collect(Collectors.joining("; ")); - if (Strings.isEmpty(messageForCluster)) { + if (Strings.isEmpty(detailsForCluster)) { return null; } else { - return new DeprecationIssue(DeprecationIssue.Level.CRITICAL, messageForCluster, "https://www.elastic" + - ".co/guide/en/elasticsearch/reference/master/breaking-changes-8.0.html", messageForCluster, false, null); + String message = "templates contain deprecated geo_shape properties that must be removed"; + return new DeprecationIssue(DeprecationIssue.Level.CRITICAL, message,"https://www.elastic" + + ".co/guide/en/elasticsearch/reference/master/migrating-8.0.html#breaking_80_mappings_changes", detailsForCluster, false, + null); } } } diff --git a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecks.java b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecks.java index bbbbefb4b771d..8ccc63a51964c 100644 --- a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecks.java +++ b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecks.java @@ -332,8 +332,7 @@ protected static String formatDeprecatedGeoShapeParamMessage(String type, Map.En Map value = (Map) entry.getValue(); return LegacyGeoShapeFieldMapper.DEPRECATED_PARAMETERS.stream() .filter(deprecatedParameter -> value.containsKey(deprecatedParameter)) - .map(deprecatedParameter -> String.format(Locale.ROOT, "%s parameter [%s] in field [%s] is deprecated and will be removed in " + - "a future version", type, deprecatedParameter, fieldName)) + .map(deprecatedParameter -> String.format(Locale.ROOT, "parameter [%s] in field [%s]", type, deprecatedParameter, fieldName)) .collect(Collectors.joining("; ")); } @@ -349,9 +348,12 @@ static DeprecationIssue checkGeoShapeMappings(IndexMetadata indexMetadata) { if (messages.isEmpty()) { return null; } else { - String message = messages.stream().collect(Collectors.joining("; ")); - String details = "mappings for index " + indexMetadata.getIndex().getName() + " contains deprecated properties. " + message; - String url = "https://www.elastic.co/guide/en/elasticsearch/reference/master/breaking-changes-8.0.html"; + String message = String.format(Locale.ROOT,"mappings for index %s contains deprecated geo_shape properties that must be " + + "removed", indexMetadata.getIndex().getName()); + String details = String.format(Locale.ROOT, + "The following geo_shape parameters must be removed from %s: [%s]", indexMetadata.getIndex().getName(), + messages.stream().collect(Collectors.joining("; "))); + String url = "https://www.elastic.co/guide/en/elasticsearch/reference/master/migrating-8.0.html#breaking_80_mappings_changes"; return new DeprecationIssue(DeprecationIssue.Level.CRITICAL, message, url, details, false, null); } } diff --git a/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecksTests.java b/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecksTests.java index 240e5f616e4d3..272bdbe8ece30 100644 --- a/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecksTests.java +++ b/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecksTests.java @@ -387,13 +387,10 @@ public void testCheckGeoShapeMappings() throws Exception { assertThat(issue, equalTo( new DeprecationIssue(DeprecationIssue.Level.CRITICAL, - "mappings in template single-type contains deprecated properties. [geo_shape parameter [points_only] in field [location] " + - "is deprecated and will be removed in a future version; geo_shape parameter [strategy] in field [location] is " + - "deprecated and will be removed in a future version]", - "https://www.elastic.co/guide/en/elasticsearch/reference/master/breaking-changes-8.0.html", - "mappings in template single-type contains deprecated properties. [geo_shape parameter [points_only] in field [location] " + - "is deprecated and will be removed in a future version; geo_shape parameter [strategy] in field [location] is " + - "deprecated and will be removed in a future version]", false, null) + "templates contain deprecated geo_shape properties that must be removed", + "https://www.elastic.co/guide/en/elasticsearch/reference/master/migrating-8.0.html#breaking_80_mappings_changes", + "mappings in template single-type contains deprecated geo_shape properties. [parameter [geo_shape] in field " + + "[points_only]; parameter [geo_shape] in field [strategy]]", false, null) )); } } diff --git a/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecksTests.java b/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecksTests.java index ff3e094eb1668..f06a251ac56f2 100644 --- a/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecksTests.java +++ b/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/IndexDeprecationChecksTests.java @@ -534,12 +534,10 @@ public void testCheckGeoShapeMappings() throws Exception { assertEquals(1, issues.size()); assertThat(issues, contains( new DeprecationIssue(DeprecationIssue.Level.CRITICAL, - "[geo_shape parameter [points_only] in field [location] is deprecated and will be removed in a future version; geo_shape " + - "parameter [strategy] in field [location] is deprecated and will be removed in a future version]", - "https://www.elastic.co/guide/en/elasticsearch/reference/master/breaking-changes-8.0.html", - "mappings for index test contains deprecated properties. [geo_shape parameter [points_only] in field [location] is " + - "deprecated and will be removed in a future version; geo_shape parameter [strategy] in field [location] is deprecated" + - " and will be removed in a future version]", false, null) + "mappings for index test contains deprecated geo_shape properties that must be removed", + "https://www.elastic.co/guide/en/elasticsearch/reference/master/migrating-8.0.html#breaking_80_mappings_changes", + "The following geo_shape parameters must be removed from test: [[parameter [geo_shape] in field [points_only]; parameter " + + "[geo_shape] in field [strategy]]]", false, null) )); Map nestedProperties = Stream.of(new Object[][] { @@ -555,12 +553,10 @@ public void testCheckGeoShapeMappings() throws Exception { assertEquals(1, issues.size()); assertThat(issues, contains( new DeprecationIssue(DeprecationIssue.Level.CRITICAL, - "[geo_shape parameter [points_only] in field [location] is deprecated and will be removed in a future version; geo_shape " + - "parameter [strategy] in field [location] is deprecated and will be removed in a future version]", - "https://www.elastic.co/guide/en/elasticsearch/reference/master/breaking-changes-8.0.html", - "mappings for index test contains deprecated properties. [geo_shape parameter [points_only] in field [location] is " + - "deprecated and will be removed in a future version; geo_shape parameter [strategy] in field [location] is deprecated" + - " and will be removed in a future version]", false, null) + "mappings for index test contains deprecated geo_shape properties that must be removed", + "https://www.elastic.co/guide/en/elasticsearch/reference/master/migrating-8.0.html#breaking_80_mappings_changes", + "The following geo_shape parameters must be removed from test: [[parameter [geo_shape] in field [points_only]; parameter " + + "[geo_shape] in field [strategy]]]", false, null) )); } } From e974c6438ac07e8f1715418a6fe82ca52a129597 Mon Sep 17 00:00:00 2001 From: Keith Massey Date: Mon, 30 Aug 2021 11:48:39 -0500 Subject: [PATCH 7/7] checking component templates --- .../deprecation/ClusterDeprecationChecks.java | 77 +++++++++++++++---- .../ClusterDeprecationChecksTests.java | 50 +++++++++++- 2 files changed, 110 insertions(+), 17 deletions(-) diff --git a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecks.java b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecks.java index 0ef411e38d71e..050bdd0066032 100644 --- a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecks.java +++ b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecks.java @@ -10,6 +10,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.metadata.ComponentTemplate; import org.elasticsearch.cluster.metadata.IndexTemplateMetadata; import org.elasticsearch.cluster.metadata.MappingMetadata; import org.elasticsearch.common.Strings; @@ -209,38 +210,84 @@ static DeprecationIssue checkClusterRoutingAllocationIncludeRelocationsSetting(f } @SuppressWarnings("unchecked") - static DeprecationIssue checkGeoShapeTemplates(final ClusterState clusterState) { - String detailsForCluster = - StreamSupport.stream(clusterState.getMetadata().getTemplates().spliterator(), false).map((templateCursor) -> { + private static String getDetailsMessageForComponentTemplates(Map componentTemplates) { + String detailsForComponentTemplates = + componentTemplates.entrySet().stream().map((templateCursor) -> { + String templateName = templateCursor.getKey(); + ComponentTemplate componentTemplate = templateCursor.getValue(); + CompressedXContent mappings = componentTemplate.template().mappings(); + if (mappings != null) { + Tuple> tuple = XContentHelper.convertToMap(mappings.uncompressed(), true, + XContentType.JSON); + Map mappingAsMap = tuple.v2(); + List messages = mappingAsMap == null ? Collections.emptyList() : + IndexDeprecationChecks.findInPropertiesRecursively(LegacyGeoShapeFieldMapper.CONTENT_TYPE, + mappingAsMap, + IndexDeprecationChecks::isGeoShapeFieldWithDeprecatedParam, + IndexDeprecationChecks::formatDeprecatedGeoShapeParamMessage); + if (messages.isEmpty() == false) { + String messageForMapping = + "mappings in component template " + templateName + " contains deprecated geo_shape properties. " + + messages.stream().collect(Collectors.joining("; ")); + return messageForMapping; + } + } + return null; + }).filter(messageForTemplate -> Strings.isEmpty(messageForTemplate) == false).collect(Collectors.joining("; ")); + return detailsForComponentTemplates; + } + + @SuppressWarnings("unchecked") + private static String getDetailsMessageForIndexTemplates(ImmutableOpenMap indexTemplates) { + String detailsForIndexTemplates = + StreamSupport.stream(indexTemplates.spliterator(), false).map((templateCursor) -> { String templateName = templateCursor.key; IndexTemplateMetadata indexTemplateMetadata = templateCursor.value; String messageForTemplate = StreamSupport.stream(indexTemplateMetadata.getMappings().spliterator(), false).map((mappingCursor) -> { CompressedXContent mapping = mappingCursor.value; - Tuple> tuple = XContentHelper.convertToMap(mapping.uncompressed(), true, + Tuple> tuple = XContentHelper.convertToMap(mapping.uncompressed(), true, XContentType.JSON); Map mappingAsMap = (Map) tuple.v2().get("_doc"); List messages = mappingAsMap == null ? Collections.emptyList() : IndexDeprecationChecks.findInPropertiesRecursively(LegacyGeoShapeFieldMapper.CONTENT_TYPE, - mappingAsMap, - IndexDeprecationChecks::isGeoShapeFieldWithDeprecatedParam, - IndexDeprecationChecks::formatDeprecatedGeoShapeParamMessage); + mappingAsMap, + IndexDeprecationChecks::isGeoShapeFieldWithDeprecatedParam, + IndexDeprecationChecks::formatDeprecatedGeoShapeParamMessage); return messages; }).filter(messages -> messages.isEmpty() == false).map(messages -> { String messageForMapping = - "mappings in template " + templateName + " contains deprecated geo_shape properties. " + + "mappings in index template " + templateName + " contains deprecated geo_shape properties. " + messages.stream().collect(Collectors.joining("; ")); return messageForMapping; }).collect(Collectors.joining("; ")); return messageForTemplate; - }).filter(messageForTempalte -> Strings.isEmpty(messageForTempalte) == false).collect(Collectors.joining("; ")); - if (Strings.isEmpty(detailsForCluster)) { - return null; - } else { - String message = "templates contain deprecated geo_shape properties that must be removed"; - return new DeprecationIssue(DeprecationIssue.Level.CRITICAL, message,"https://www.elastic" + - ".co/guide/en/elasticsearch/reference/master/migrating-8.0.html#breaking_80_mappings_changes", detailsForCluster, false, + }).filter(messageForTemplate -> Strings.isEmpty(messageForTemplate) == false).collect(Collectors.joining("; ")); + return detailsForIndexTemplates; + } + + @SuppressWarnings("unchecked") + static DeprecationIssue checkGeoShapeTemplates(final ClusterState clusterState) { + String detailsForComponentTemplates = getDetailsMessageForComponentTemplates(clusterState.getMetadata().componentTemplates()); + String detailsForIndexTemplates = getDetailsMessageForIndexTemplates(clusterState.getMetadata().getTemplates()); + boolean deprecationInComponentTemplates = Strings.isEmpty(detailsForComponentTemplates) == false; + boolean deprecationInIndexTemplates = Strings.isEmpty(detailsForIndexTemplates) == false; + String url = "https://www.elastic.co/guide/en/elasticsearch/reference/master/migrating-8.0.html#breaking_80_mappings_changes"; + if (deprecationInComponentTemplates && deprecationInIndexTemplates) { + String message = "component templates and index templates contain deprecated geo_shape properties that must be removed"; + String details = detailsForComponentTemplates + "; " + detailsForIndexTemplates; + return new DeprecationIssue(DeprecationIssue.Level.CRITICAL, message, url, details, false, + null); + } if (deprecationInComponentTemplates == false && deprecationInIndexTemplates) { + String message = "index templates contain deprecated geo_shape properties that must be removed"; + return new DeprecationIssue(DeprecationIssue.Level.CRITICAL, message, url, detailsForIndexTemplates, false, null); + } else if (deprecationInIndexTemplates == false && deprecationInComponentTemplates) { + String message = "component templates contain deprecated geo_shape properties that must be removed"; + return new DeprecationIssue(DeprecationIssue.Level.CRITICAL, message, url, detailsForComponentTemplates, false, + null); + } else { + return null; } } } diff --git a/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecksTests.java b/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecksTests.java index 272bdbe8ece30..bbb6c384c2793 100644 --- a/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecksTests.java +++ b/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecksTests.java @@ -9,11 +9,15 @@ import org.elasticsearch.action.ingest.PutPipelineRequest; import org.elasticsearch.cluster.ClusterName; import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.metadata.AliasMetadata; +import org.elasticsearch.cluster.metadata.ComponentTemplate; import org.elasticsearch.cluster.metadata.IndexTemplateMetadata; import org.elasticsearch.cluster.metadata.Metadata; +import org.elasticsearch.cluster.metadata.Template; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.collect.ImmutableOpenMap; +import org.elasticsearch.common.compress.CompressedXContent; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentType; @@ -24,6 +28,7 @@ import java.io.IOException; import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Locale; @@ -357,6 +362,7 @@ public void testClusterRoutingAllocationIncludeRelocationsSetting() { } public void testCheckGeoShapeMappings() throws Exception { + // First, testing only an index template: IndexTemplateMetadata indexTemplateMetadata = IndexTemplateMetadata.builder("single-type") .patterns(Collections.singletonList("foo")) .putMapping("_doc", "{\n" + @@ -387,10 +393,50 @@ public void testCheckGeoShapeMappings() throws Exception { assertThat(issue, equalTo( new DeprecationIssue(DeprecationIssue.Level.CRITICAL, - "templates contain deprecated geo_shape properties that must be removed", + "index templates contain deprecated geo_shape properties that must be removed", "https://www.elastic.co/guide/en/elasticsearch/reference/master/migrating-8.0.html#breaking_80_mappings_changes", - "mappings in template single-type contains deprecated geo_shape properties. [parameter [geo_shape] in field " + + "mappings in index template single-type contains deprecated geo_shape properties. [parameter [geo_shape] in field " + "[points_only]; parameter [geo_shape] in field [strategy]]", false, null) )); + + // Second, testing only a component template: + String templateName = "my-template"; + Settings settings = Settings.builder().put("index.number_of_shards", 1).build(); + CompressedXContent mappings = new CompressedXContent("{\"properties\":{\"location\":{\"type\":\"geo_shape\", " + + "\"strategy\":\"recursive\", \"points_only\":true}}}"); + AliasMetadata alias = AliasMetadata.builder("alias").writeIndex(true).build(); + Template template = new Template(settings, mappings, Collections.singletonMap("alias", alias)); + ComponentTemplate componentTemplate = new ComponentTemplate(template, 1L, new HashMap<>()); + badMetadata = Metadata.builder() + .componentTemplates(Collections.singletonMap(templateName, componentTemplate)) + .build(); + badState = ClusterState.builder(new ClusterName("test")).metadata(badMetadata).build(); + issue = ClusterDeprecationChecks.checkGeoShapeTemplates(badState); + + assertThat(issue, equalTo( + new DeprecationIssue(DeprecationIssue.Level.CRITICAL, + "component templates contain deprecated geo_shape properties that must be removed", + "https://www.elastic.co/guide/en/elasticsearch/reference/master/migrating-8.0.html#breaking_80_mappings_changes", + "mappings in component template my-template contains deprecated geo_shape properties. [parameter [geo_shape] in field " + + "[points_only]; parameter [geo_shape] in field [strategy]]", false, null) + )); + + // Third, trying a component template and an index template: + badMetadata = Metadata.builder() + .componentTemplates(Collections.singletonMap(templateName, componentTemplate)) + .templates(templates) + .build(); + badState = ClusterState.builder(new ClusterName("test")).metadata(badMetadata).build(); + issue = ClusterDeprecationChecks.checkGeoShapeTemplates(badState); + + assertThat(issue, equalTo( + new DeprecationIssue(DeprecationIssue.Level.CRITICAL, + "component templates and index templates contain deprecated geo_shape properties that must be removed", + "https://www.elastic.co/guide/en/elasticsearch/reference/master/migrating-8.0.html#breaking_80_mappings_changes", + "mappings in component template my-template contains deprecated geo_shape properties. [parameter [geo_shape] in field " + + "[points_only]; parameter [geo_shape] in field [strategy]]; mappings in index template single-type contains " + + "deprecated geo_shape properties. [parameter [geo_shape] in field [points_only]; parameter [geo_shape] in field " + + "[strategy]]", false, null) + )); } }