Skip to content

Commit

Permalink
Fix rollover alias definition in templates validation (#70259) (#70302)
Browse files Browse the repository at this point in the history
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 3ea34bf)
Signed-off-by: Andrei Dan <[email protected]>
  • Loading branch information
andreidan authored Mar 11, 2021
1 parent f7f7daa commit db38955
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -308,15 +308,6 @@ static List<AliasAction> rolloverAliasToNewIndex(String oldIndex, String newInde
*/
static void checkNoDuplicatedAliasInIndexTemplate(Metadata metadata, String rolloverIndexName, String rolloverRequestAlias,
@Nullable Boolean isHidden) {
final List<IndexTemplateMetadata> 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<Map<String, AliasMetadata>> aliases = MetadataIndexTemplateService.resolveAliases(metadata, matchedV2Template, false);
Expand All @@ -327,6 +318,16 @@ static void checkNoDuplicatedAliasInIndexTemplate(Metadata metadata, String roll
rolloverRequestAlias, aliasConfig.keySet(), matchedV2Template));
}
}
return;
}

final List<IndexTemplateMetadata> 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()));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("*"))
Expand Down

0 comments on commit db38955

Please sign in to comment.