Skip to content

Commit 000f912

Browse files
authored
Refactor MetadataIndexTemplateService to use template maps instead of project metadata (#132662)
Refactors the MetadataIndexTemplateService to operate on maps that contain the component templates in the cluster instead of operating against the project metadata itself. This allows for easier use in the future when validating the state of an index template operation without requiring the operation be persisted into a full project metadata object for validation.
1 parent 4600b16 commit 000f912

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

server/src/main/java/org/elasticsearch/action/admin/indices/template/reservedstate/ReservedComposableIndexTemplateAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public TransformState transform(ProjectId projectId, ComponentsAndComposables so
182182

183183
// 4. validate for v2 composable template overlaps
184184
for (var request : composables) {
185-
MetadataIndexTemplateService.v2TemplateOverlaps(project, request.name(), request.indexTemplate(), true);
185+
MetadataIndexTemplateService.v2TemplateOverlaps(project.templatesV2(), request.name(), request.indexTemplate(), true);
186186
}
187187

188188
Set<String> componentEntities = components.stream().map(r -> reservedComponentName(r.name())).collect(Collectors.toSet());

server/src/main/java/org/elasticsearch/cluster/metadata/MetadataIndexTemplateService.java

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,16 @@ public ProjectMetadata addComponentTemplate(
430430
return ProjectMetadata.builder(project).put(name, finalComponentTemplate).build();
431431
}
432432

433+
/**
434+
* Mappings in templates don't have to include <code>_doc</code>, so update the mappings to include this single type if necessary
435+
*
436+
* @param mappings mappings from a template
437+
* @param xContentRegistry the xcontent registry used for parsing
438+
* @return a normalized form of the mapping provided
439+
* @throws IOException if reading or writing the mapping encounters a problem
440+
*/
433441
@Nullable
434-
private static CompressedXContent wrapMappingsIfNecessary(@Nullable CompressedXContent mappings, NamedXContentRegistry xContentRegistry)
442+
public static CompressedXContent wrapMappingsIfNecessary(@Nullable CompressedXContent mappings, NamedXContentRegistry xContentRegistry)
435443
throws IOException {
436444
// Mappings in templates don't have to include _doc, so update
437445
// the mappings to include this single type if necessary
@@ -593,14 +601,22 @@ public ProjectMetadata execute(ProjectMetadata currentProject) throws Exception
593601
}
594602

595603
public static void validateV2TemplateRequest(ProjectMetadata metadata, String name, ComposableIndexTemplate template) {
604+
validateV2TemplateRequest(metadata.componentTemplates(), name, template);
605+
}
606+
607+
public static void validateV2TemplateRequest(
608+
Map<String, ComponentTemplate> componentTemplates,
609+
String name,
610+
ComposableIndexTemplate template
611+
) {
596612
if (template.createdDateMillis().isPresent()) {
597613
throw new InvalidIndexTemplateException(name, "provided a template property which is managed by the system: created_date");
598614
}
599615
if (template.modifiedDateMillis().isPresent()) {
600616
throw new InvalidIndexTemplateException(name, "provided a template property which is managed by the system: modified_date");
601617
}
602618
if (template.indexPatterns().stream().anyMatch(Regex::isMatchAllPattern)) {
603-
Settings mergedSettings = resolveSettings(template, metadata.componentTemplates());
619+
Settings mergedSettings = resolveSettings(template, componentTemplates);
604620
if (IndexMetadata.INDEX_HIDDEN_SETTING.exists(mergedSettings)) {
605621
throw new InvalidIndexTemplateException(
606622
name,
@@ -609,7 +625,6 @@ public static void validateV2TemplateRequest(ProjectMetadata metadata, String na
609625
}
610626
}
611627

612-
final Map<String, ComponentTemplate> componentTemplates = metadata.componentTemplates();
613628
final List<String> ignoreMissingComponentTemplates = (template.getIgnoreMissingComponentTemplates() == null
614629
? List.of()
615630
: template.getIgnoreMissingComponentTemplates());
@@ -661,7 +676,7 @@ public ProjectMetadata addIndexTemplateV2(
661676
throw new IllegalArgumentException("index template [" + name + "] already exists");
662677
}
663678

664-
Map<String, List<String>> overlaps = v2TemplateOverlaps(project, name, template, validateV2Overlaps);
679+
Map<String, List<String>> overlaps = v2TemplateOverlaps(project.templatesV2(), name, template, validateV2Overlaps);
665680

666681
overlaps = findConflictingV1Templates(project, name, template.indexPatterns());
667682
if (overlaps.size() > 0) {
@@ -725,20 +740,20 @@ public ProjectMetadata addIndexTemplateV2(
725740
* we throw an {@link IllegalArgumentException} with information about the conflicting templates.
726741
* <p>
727742
* This method doesn't check for conflicting overlaps with v1 templates.
728-
* @param project the current project
743+
* @param templatesV2 the templates to check overlap against
729744
* @param name the composable index template name
730745
* @param template the full composable index template object we check for overlaps
731746
* @param validate should we throw {@link IllegalArgumentException} if conflicts are found or just compute them
732747
* @return a map of v2 template names to their index patterns for v2 templates that would overlap with the given template
733748
*/
734749
public static Map<String, List<String>> v2TemplateOverlaps(
735-
final ProjectMetadata project,
750+
Map<String, ComposableIndexTemplate> templatesV2,
736751
String name,
737752
final ComposableIndexTemplate template,
738753
boolean validate
739754
) {
740755
Map<String, List<String>> overlaps = findConflictingV2Templates(
741-
project,
756+
templatesV2,
742757
name,
743758
template.indexPatterns(),
744759
true,
@@ -1028,7 +1043,7 @@ public static Map<String, List<String>> findConflictingV2Templates(
10281043
final String candidateName,
10291044
final List<String> indexPatterns
10301045
) {
1031-
return findConflictingV2Templates(project, candidateName, indexPatterns, false, 0L);
1046+
return findConflictingV2Templates(project.templatesV2(), candidateName, indexPatterns, false, 0L);
10321047
}
10331048

10341049
/**
@@ -1042,15 +1057,15 @@ public static Map<String, List<String>> findConflictingV2Templates(
10421057
* index templates with the same priority).
10431058
*/
10441059
static Map<String, List<String>> findConflictingV2Templates(
1045-
final ProjectMetadata project,
1060+
final Map<String, ComposableIndexTemplate> templatesV2,
10461061
final String candidateName,
10471062
final List<String> indexPatterns,
10481063
boolean checkPriority,
10491064
long priority
10501065
) {
10511066
Automaton v1automaton = Regex.simpleMatchToAutomaton(indexPatterns.toArray(Strings.EMPTY_ARRAY));
10521067
Map<String, List<String>> overlappingTemplates = new TreeMap<>();
1053-
for (Map.Entry<String, ComposableIndexTemplate> entry : project.templatesV2().entrySet()) {
1068+
for (Map.Entry<String, ComposableIndexTemplate> entry : templatesV2.entrySet()) {
10541069
String name = entry.getKey();
10551070
ComposableIndexTemplate template = entry.getValue();
10561071
Automaton v2automaton = Regex.simpleMatchToAutomaton(template.indexPatterns().toArray(Strings.EMPTY_ARRAY));
@@ -1993,7 +2008,8 @@ private static void validateCompositeTemplate(
19932008
});
19942009
}
19952010

1996-
static void validateTemplate(Settings validateSettings, CompressedXContent mappings, IndicesService indicesService) throws Exception {
2011+
public static void validateTemplate(Settings validateSettings, CompressedXContent mappings, IndicesService indicesService)
2012+
throws Exception {
19972013
// Hard to validate settings if they're non-existent, so used empty ones if none were provided
19982014
Settings settings = validateSettings;
19992015
if (settings == null) {
@@ -2024,7 +2040,7 @@ static void validateTemplate(Settings validateSettings, CompressedXContent mappi
20242040
});
20252041
}
20262042

2027-
private void validate(String name, ComponentTemplate template) {
2043+
public void validate(String name, ComponentTemplate template) {
20282044
validate(name, template.template(), Collections.emptyList());
20292045
}
20302046

server/src/test/java/org/elasticsearch/cluster/metadata/MetadataIndexTemplateServiceTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2476,14 +2476,14 @@ public void testV2TemplateOverlaps() throws Exception {
24762476
.build();
24772477

24782478
// when validating is false, we return the conflicts instead of throwing an exception
2479-
var overlaps = MetadataIndexTemplateService.v2TemplateOverlaps(project, "foo2", newTemplate, false);
2479+
var overlaps = MetadataIndexTemplateService.v2TemplateOverlaps(project.templatesV2(), "foo2", newTemplate, false);
24802480

24812481
assertThat(overlaps, allOf(aMapWithSize(1), hasKey("foo")));
24822482

24832483
// try now the same thing with validation on
24842484
IllegalArgumentException e = expectThrows(
24852485
IllegalArgumentException.class,
2486-
() -> MetadataIndexTemplateService.v2TemplateOverlaps(project, "foo2", newTemplate, true)
2486+
() -> MetadataIndexTemplateService.v2TemplateOverlaps(project.templatesV2(), "foo2", newTemplate, true)
24872487
);
24882488
assertThat(
24892489
e.getMessage(),
@@ -2499,7 +2499,7 @@ public void testV2TemplateOverlaps() throws Exception {
24992499
.priority(1L)
25002500
.build();
25012501

2502-
overlaps = MetadataIndexTemplateService.v2TemplateOverlaps(project, "no-conflict", nonConflict, true);
2502+
overlaps = MetadataIndexTemplateService.v2TemplateOverlaps(project.templatesV2(), "no-conflict", nonConflict, true);
25032503
assertTrue(overlaps.isEmpty());
25042504
}
25052505

@@ -2513,7 +2513,7 @@ public void testV2TemplateOverlaps() throws Exception {
25132513
.build();
25142514
IllegalArgumentException e = expectThrows(
25152515
IllegalArgumentException.class,
2516-
() -> MetadataIndexTemplateService.v2TemplateOverlaps(project, "foo2", newTemplate, true)
2516+
() -> MetadataIndexTemplateService.v2TemplateOverlaps(project.templatesV2(), "foo2", newTemplate, true)
25172517
);
25182518
assertThat(
25192519
e.getMessage(),

0 commit comments

Comments
 (0)