Skip to content

Commit af3e1c3

Browse files
committed
Skip data stream reference check in some cases
During component and composable template validation we check if any data streams become unreferenced if the composite template would be added to the project metadata. We currently do this regardless of what changed in the component or composable template. This check is quite expensive if the cluster has many index templates or many data streams, mostly because the regex matching on index patterns in `findV2Template` is expensive. We know that only a few properties of the composite template can affect data stream references. Specifically, we know that the index patterns, the `hidden` index setting, the priority, and the presence of the `data_stream` field in the template are used to determine the template that a data stream is based on. By checking if only one of those properties changed, we can avoid running the expensive checks in a great deal of template updates. For example, mapping updates, `_meta` updates, any index settings other than `hidden` are all irrelevant to this check, and we can thus skip it for those updates. Especially component template updates will benefit from this change, as they aren't even able to influence the index patterns, data stream template, or priority of the composite template. The main downside of this change is that we have an implicit dependency from this new check on the implementation of `findV2Template`. If we were to modify `findV2Template` to depend on another property (e.g. another setting), we'd have to remember to update this check to avoid us skipping the reference check even though a relevant property was updated.
1 parent 945517b commit af3e1c3

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

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

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -955,18 +955,36 @@ private static void validateDataStreamsStillReferenced(
955955
String templateName,
956956
ComposableIndexTemplate newTemplate
957957
) {
958-
final Set<String> dataStreams = project.dataStreams()
959-
.entrySet()
960-
.stream()
961-
.filter(entry -> entry.getValue().isSystem() == false)
962-
.map(Map.Entry::getKey)
963-
.collect(Collectors.toSet());
958+
final ComposableIndexTemplate existing = project.templatesV2().get(templateName);
959+
final Settings existingSettings = Optional.ofNullable(existing)
960+
.map(ComposableIndexTemplate::template)
961+
.map(Template::settings)
962+
.orElse(Settings.EMPTY);
963+
final Settings newSettings = Optional.ofNullable(newTemplate)
964+
.map(ComposableIndexTemplate::template)
965+
.map(Template::settings)
966+
.orElse(Settings.EMPTY);
967+
// We check whether anything relevant has changed that could affect data stream coverage and return early if not.
968+
// These checks are based on the implementation of findV2Template and the data stream template check in this method.
969+
// If we're adding a new template, we do the full check in case this template's priority changes coverage.
970+
if (existing != null
971+
&& existing.indexPatterns().equals(newTemplate.indexPatterns())
972+
&& Objects.equals(existingSettings.get(IndexMetadata.SETTING_INDEX_HIDDEN), newSettings.get(IndexMetadata.SETTING_INDEX_HIDDEN))
973+
&& Objects.equals(existing.getDataStreamTemplate() != null, newTemplate.getDataStreamTemplate() != null)
974+
&& existing.priorityOrZero() == newTemplate.priorityOrZero()) {
975+
return;
976+
}
964977

965978
Function<Map<String, ComposableIndexTemplate>, Set<String>> findUnreferencedDataStreams = composableTemplates -> {
966979
final Set<String> unreferenced = new HashSet<>();
967980
// For each data stream that we have, see whether it's covered by a different
968981
// template (which is great), or whether it's now uncovered by any template
969-
for (String dataStream : dataStreams) {
982+
for (var dataStreamEntry : project.dataStreams().entrySet()) {
983+
// Exclude system data streams
984+
if (dataStreamEntry.getValue().isSystem()) {
985+
continue;
986+
}
987+
final String dataStream = dataStreamEntry.getKey();
970988
final String matchingTemplate = findV2Template(project, composableTemplates.entrySet(), dataStream, false, false);
971989
if (matchingTemplate == null) {
972990
unreferenced.add(dataStream);

0 commit comments

Comments
 (0)