diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataIndexTemplateService.java b/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataIndexTemplateService.java index 06f119f587c..b03843b4872 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataIndexTemplateService.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataIndexTemplateService.java @@ -575,38 +575,21 @@ public class MetadataIndexTemplateService { if (request.create && isUpdate) { throw new IllegalArgumentException("index_template [" + request.name + "] already exists"); } - boolean isUpdateAndPatternsAreUnchanged = isUpdate && - currentState.metadata().templates().get(request.name).patterns().equals(request.indexPatterns); Map> overlaps = findConflictingV2Templates(currentState, request.name, request.indexPatterns); if (overlaps.size() > 0) { - // Be less strict (just a warning) if we're updating an existing template or this is a match-all template - if (isUpdateAndPatternsAreUnchanged || request.indexPatterns.stream().anyMatch(Regex::isMatchAllPattern)) { - String warning = String.format(Locale.ROOT, "template [%s] has index patterns %s matching patterns" + - " from existing index templates [%s] with patterns (%s); this template [%s] may be ignored in favor of " + - "an index template at index creation time", - request.name, - request.indexPatterns, - Strings.collectionToCommaDelimitedString(overlaps.keySet()), - overlaps.entrySet().stream() - .map(e -> e.getKey() + " => " + e.getValue()) - .collect(Collectors.joining(",")), - request.name); - logger.warn(warning); - deprecationLogger.deprecatedAndMaybeLog("index_template_pattern_overlap", warning); - } else { - // Otherwise, this is a hard error, the user should use V2 index templates instead - String error = String.format(Locale.ROOT, "template [%s] has index patterns %s matching patterns" + - " from existing index templates [%s] with patterns (%s), use index templates (/_index_template) instead", - request.name, - request.indexPatterns, - Strings.collectionToCommaDelimitedString(overlaps.keySet()), - overlaps.entrySet().stream() - .map(e -> e.getKey() + " => " + e.getValue()) - .collect(Collectors.joining(","))); - logger.error(error); - throw new IllegalArgumentException(error); - } + String warning = String.format(Locale.ROOT, "template [%s] has index patterns %s matching patterns" + + " from existing index templates [%s] with patterns (%s); this template [%s] may be ignored in favor of " + + "an index template at index creation time", + request.name, + request.indexPatterns, + Strings.collectionToCommaDelimitedString(overlaps.keySet()), + overlaps.entrySet().stream() + .map(e -> e.getKey() + " => " + e.getValue()) + .collect(Collectors.joining(",")), + request.name); + logger.warn(warning); + deprecationLogger.deprecatedAndMaybeLog("index_template_pattern_overlap", warning); } templateBuilder.order(request.order); diff --git a/server/src/test/java/org/elasticsearch/cluster/metadata/MetadataIndexTemplateServiceTests.java b/server/src/test/java/org/elasticsearch/cluster/metadata/MetadataIndexTemplateServiceTests.java index f073d79616e..891f601e8af 100644 --- a/server/src/test/java/org/elasticsearch/cluster/metadata/MetadataIndexTemplateServiceTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/metadata/MetadataIndexTemplateServiceTests.java @@ -484,24 +484,20 @@ public class MetadataIndexTemplateServiceTests extends ESSingleNodeTestCase { } /** - * Test that if we have a pre-existing v2 template and put a v1 template that would match the same indices, we generate a hard error + * Test that if we have a pre-existing v2 template and put a v1 template that would match the same indices, we generate a warning */ - public void testPuttingV1NonStarTemplateGeneratesError() throws Exception { + public void testPuttingV1NonStarTemplateGeneratesWarning() throws Exception { final MetadataIndexTemplateService metadataIndexTemplateService = getMetadataIndexTemplateService(); IndexTemplateV2 v2Template = new IndexTemplateV2(Arrays.asList("foo-bar-*", "eggplant"), null, null, null, null, null); ClusterState state = metadataIndexTemplateService.addIndexTemplateV2(ClusterState.EMPTY_STATE, false, "v2-template", v2Template); MetadataIndexTemplateService.PutRequest req = new MetadataIndexTemplateService.PutRequest("cause", "v1-template"); req.patterns(Arrays.asList("egg*", "baz")); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> MetadataIndexTemplateService.innerPutTemplate(state, req, IndexTemplateMetadata.builder("v1-template"))); + MetadataIndexTemplateService.innerPutTemplate(state, req, IndexTemplateMetadata.builder("v1-template")); - assertThat(e.getMessage(), - equalTo("template [v1-template] has index patterns [egg*, baz] matching patterns from existing index " + - "templates [v2-template] with patterns (v2-template => [foo-bar-*, eggplant]), use index templates " + - "(/_index_template) instead")); - - assertNull(state.metadata().templates().get("v1-template")); + assertWarnings("template [v1-template] has index patterns [egg*, baz] matching patterns " + + "from existing index templates [v2-template] with patterns (v2-template => [foo-bar-*, eggplant]);" + + " this template [v1-template] may be ignored in favor of an index template at index creation time"); } /** @@ -547,9 +543,9 @@ public class MetadataIndexTemplateServiceTests extends ESSingleNodeTestCase { /** * Test that if we have a pre-existing v1 and v2 template, and we update the existing v1 - * template *AND* change the index patterns that an error is generated + * template *AND* change the index patterns that a warning is generated */ - public void testUpdatingV1NonStarWithChangedPatternsTemplateGeneratesError() throws Exception { + public void testUpdatingV1NonStarWithChangedPatternsTemplateGeneratesWarning() throws Exception { final MetadataIndexTemplateService metadataIndexTemplateService = getMetadataIndexTemplateService(); IndexTemplateMetadata v1Template = IndexTemplateMetadata.builder("v1-template") .patterns(Arrays.asList("fo*", "baz")) @@ -576,12 +572,11 @@ public class MetadataIndexTemplateServiceTests extends ESSingleNodeTestCase { MetadataIndexTemplateService.PutRequest req = new MetadataIndexTemplateService.PutRequest("cause", "v1-template"); req.patterns(Arrays.asList("egg*", "baz")); final ClusterState finalState = state; - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> MetadataIndexTemplateService.innerPutTemplate(finalState, req, IndexTemplateMetadata.builder("v1-template"))); + MetadataIndexTemplateService.innerPutTemplate(finalState, req, IndexTemplateMetadata.builder("v1-template")); - assertThat(e.getMessage(), equalTo("template [v1-template] has index patterns [egg*, baz] matching patterns " + - "from existing index templates [v2-template] with patterns (v2-template => [foo-bar-*, eggplant]), use index " + - "templates (/_index_template) instead")); + assertWarnings("template [v1-template] has index patterns [egg*, baz] matching patterns " + + "from existing index templates [v2-template] with patterns (v2-template => [foo-bar-*, eggplant]); " + + "this template [v1-template] may be ignored in favor of an index template at index creation time"); } public void testPuttingOverlappingV2Template() throws Exception {