Skip to content

Commit bd67fdc

Browse files
authored
[7.11] Fix rollover alias definition in templates validation (#70259) (#70301)
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 <andrei.dan@elastic.co>
1 parent aef5b62 commit bd67fdc

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

server/src/main/java/org/elasticsearch/action/admin/indices/rollover/MetadataRolloverService.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -311,15 +311,6 @@ static List<AliasAction> rolloverAliasToNewIndex(String oldIndex, String newInde
311311
*/
312312
static void checkNoDuplicatedAliasInIndexTemplate(Metadata metadata, String rolloverIndexName, String rolloverRequestAlias,
313313
@Nullable Boolean isHidden) {
314-
final List<IndexTemplateMetadata> matchedTemplates = findV1Templates(metadata, rolloverIndexName, isHidden);
315-
for (IndexTemplateMetadata template : matchedTemplates) {
316-
if (template.aliases().containsKey(rolloverRequestAlias)) {
317-
throw new IllegalArgumentException(String.format(Locale.ROOT,
318-
"Rollover alias [%s] can point to multiple indices, found duplicated alias [%s] in index template [%s]",
319-
rolloverRequestAlias, template.aliases().keys(), template.name()));
320-
}
321-
}
322-
323314
final String matchedV2Template = findV2Template(metadata, rolloverIndexName, isHidden == null ? false : isHidden);
324315
if (matchedV2Template != null) {
325316
List<Map<String, AliasMetadata>> aliases = MetadataIndexTemplateService.resolveAliases(metadata, matchedV2Template, false);
@@ -330,6 +321,16 @@ static void checkNoDuplicatedAliasInIndexTemplate(Metadata metadata, String roll
330321
rolloverRequestAlias, aliasConfig.keySet(), matchedV2Template));
331322
}
332323
}
324+
return;
325+
}
326+
327+
final List<IndexTemplateMetadata> matchedTemplates = findV1Templates(metadata, rolloverIndexName, isHidden);
328+
for (IndexTemplateMetadata template : matchedTemplates) {
329+
if (template.aliases().containsKey(rolloverRequestAlias)) {
330+
throw new IllegalArgumentException(String.format(Locale.ROOT,
331+
"Rollover alias [%s] can point to multiple indices, found duplicated alias [%s] in index template [%s]",
332+
rolloverRequestAlias, template.aliases().keys(), template.name()));
333+
}
333334
}
334335
}
335336

server/src/test/java/org/elasticsearch/action/admin/indices/rollover/MetadataRolloverServiceTests.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,25 @@ public void testRejectDuplicateAliasV2UsingComponentTemplates() {
392392
assertThat(ex.getMessage(), containsString("index template [test-template]"));
393393
}
394394

395+
public void testRolloverDoesntRejectOperationIfValidComposableTemplateOverridesLegacyTemplate() {
396+
final IndexTemplateMetadata legacyTemplate = IndexTemplateMetadata.builder("legacy-template")
397+
.patterns(Arrays.asList("foo-*", "bar-*"))
398+
.putAlias(AliasMetadata.builder("foo-write")).putAlias(AliasMetadata.builder("bar-write").writeIndex(randomBoolean()))
399+
.build();
400+
401+
// v2 template overrides the v1 template and does not define the rollover aliases
402+
final ComposableIndexTemplate composableTemplate = new ComposableIndexTemplate(Arrays.asList("foo-*", "bar-*"), null,
403+
null, null, null, null, null, null);
404+
405+
final Metadata metadata = Metadata.builder().put(createMetadata(randomAlphaOfLengthBetween(5, 7)), false)
406+
.put(legacyTemplate).put("composable-template", composableTemplate).build();
407+
String indexName = randomFrom("foo-123", "bar-xyz");
408+
String aliasName = randomFrom("foo-write", "bar-write");
409+
410+
// the valid v2 template takes priority over the v1 template so the validation should not throw any exception
411+
MetadataRolloverService.checkNoDuplicatedAliasInIndexTemplate(metadata, indexName, aliasName, randomBoolean());
412+
}
413+
395414
public void testHiddenAffectsResolvedTemplates() {
396415
final IndexTemplateMetadata template = IndexTemplateMetadata.builder("test-template")
397416
.patterns(Collections.singletonList("*"))

0 commit comments

Comments
 (0)