From db3895501d3ff7c9358ded06b0f7140c78a744f1 Mon Sep 17 00:00:00 2001 From: Andrei Dan Date: Thu, 11 Mar 2021 11:28:05 +0000 Subject: [PATCH] Fix rollover alias definition in templates validation (#70259) (#70302) This fixes the case where a valid composable template overrides/shadows a legacy template that defines the rollover alias. This is a valid configuration and rollover should not fail. (cherry picked from commit 3ea34bf205466db5cecc07e6f09ac11e62717a45) Signed-off-by: Andrei Dan --- .../rollover/MetadataRolloverService.java | 19 ++++++++++--------- .../MetadataRolloverServiceTests.java | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/rollover/MetadataRolloverService.java b/server/src/main/java/org/elasticsearch/action/admin/indices/rollover/MetadataRolloverService.java index 38d48783ff990..2520d8656bff3 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/rollover/MetadataRolloverService.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/rollover/MetadataRolloverService.java @@ -308,15 +308,6 @@ static List rolloverAliasToNewIndex(String oldIndex, String newInde */ static void checkNoDuplicatedAliasInIndexTemplate(Metadata metadata, String rolloverIndexName, String rolloverRequestAlias, @Nullable Boolean isHidden) { - final List matchedTemplates = findV1Templates(metadata, rolloverIndexName, isHidden); - for (IndexTemplateMetadata template : matchedTemplates) { - if (template.aliases().containsKey(rolloverRequestAlias)) { - throw new IllegalArgumentException(String.format(Locale.ROOT, - "Rollover alias [%s] can point to multiple indices, found duplicated alias [%s] in index template [%s]", - rolloverRequestAlias, template.aliases().keys(), template.name())); - } - } - final String matchedV2Template = findV2Template(metadata, rolloverIndexName, isHidden == null ? false : isHidden); if (matchedV2Template != null) { List> aliases = MetadataIndexTemplateService.resolveAliases(metadata, matchedV2Template, false); @@ -327,6 +318,16 @@ static void checkNoDuplicatedAliasInIndexTemplate(Metadata metadata, String roll rolloverRequestAlias, aliasConfig.keySet(), matchedV2Template)); } } + return; + } + + final List matchedTemplates = findV1Templates(metadata, rolloverIndexName, isHidden); + for (IndexTemplateMetadata template : matchedTemplates) { + if (template.aliases().containsKey(rolloverRequestAlias)) { + throw new IllegalArgumentException(String.format(Locale.ROOT, + "Rollover alias [%s] can point to multiple indices, found duplicated alias [%s] in index template [%s]", + rolloverRequestAlias, template.aliases().keys(), template.name())); + } } } diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/rollover/MetadataRolloverServiceTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/rollover/MetadataRolloverServiceTests.java index 7e26223cf28e7..852a8e64ef585 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/rollover/MetadataRolloverServiceTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/rollover/MetadataRolloverServiceTests.java @@ -392,6 +392,25 @@ public void testRejectDuplicateAliasV2UsingComponentTemplates() { assertThat(ex.getMessage(), containsString("index template [test-template]")); } + public void testRolloverDoesntRejectOperationIfValidComposableTemplateOverridesLegacyTemplate() { + final IndexTemplateMetadata legacyTemplate = IndexTemplateMetadata.builder("legacy-template") + .patterns(Arrays.asList("foo-*", "bar-*")) + .putAlias(AliasMetadata.builder("foo-write")).putAlias(AliasMetadata.builder("bar-write").writeIndex(randomBoolean())) + .build(); + + // v2 template overrides the v1 template and does not define the rollover aliases + final ComposableIndexTemplate composableTemplate = new ComposableIndexTemplate.Builder().indexPatterns(Arrays.asList("foo-*", + "bar-*")).template(new Template(null, null, null)).build(); + + final Metadata metadata = Metadata.builder().put(createMetadata(randomAlphaOfLengthBetween(5, 7)), false) + .put(legacyTemplate).put("composable-template", composableTemplate).build(); + String indexName = randomFrom("foo-123", "bar-xyz"); + String aliasName = randomFrom("foo-write", "bar-write"); + + // the valid v2 template takes priority over the v1 template so the validation should not throw any exception + MetadataRolloverService.checkNoDuplicatedAliasInIndexTemplate(metadata, indexName, aliasName, randomBoolean()); + } + public void testHiddenAffectsResolvedTemplates() { final IndexTemplateMetadata template = IndexTemplateMetadata.builder("test-template") .patterns(Collections.singletonList("*"))