Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rollover alias definition in templates validation #70259

Merged
merged 2 commits into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -307,15 +307,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);
Expand All @@ -326,6 +317,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 @@ -391,6 +391,28 @@ 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");
try {
MetadataRolloverService.checkNoDuplicatedAliasInIndexTemplate(metadata, indexName, aliasName, randomBoolean());
// success
} catch (Exception e) {
fail("the valid v2 template takes priority over the v1 template. was not expecting a failure but got: " + e.getMessage());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably don't need to catch this exception, since uncaught exceptions would cause the test to fail anyway. This also swallows the stacktrace which would make it slightly harder to investigate for someone triaging a failure.

}
}

public void testHiddenAffectsResolvedTemplates() {
final IndexTemplateMetadata template = IndexTemplateMetadata.builder("test-template")
.patterns(Collections.singletonList("*"))
Expand Down