-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Optimize usage calculation in ILM policies retrieval API #106953
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
Changes from 5 commits
5bf393c
d160dc2
092cb0c
16f3d83
d30376e
24acf2c
293ff9d
02fb7a2
e193a30
532e3d1
473415c
4cd9bfb
fe9ae0d
3641d1a
5a101fe
145ce7d
d346665
5e7c011
2559a98
6bddee0
8b894d6
27b84f3
7c416c4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| pr: 106953 | ||
| summary: Optimize usage calculation in ILM policies retrieval API | ||
| area: ILM+SLM | ||
| type: enhancement | ||
| issues: | ||
| - 105773 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,6 +61,7 @@ | |
| import java.time.Instant; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.Comparator; | ||
| import java.util.HashMap; | ||
|
|
@@ -1050,29 +1051,24 @@ static Set<String> dataStreamsExclusivelyUsingTemplates(final ClusterState state | |
| .reduce(Sets::union) | ||
| .orElse(Set.of()); | ||
|
|
||
| // Filter and sort all composable templates in advance, to speed up template retrieval later on. | ||
| var templates = state.metadata() | ||
|
||
| .templatesV2() | ||
| .entrySet() | ||
| .stream() | ||
| .filter( | ||
| entry -> templateNames.contains(entry.getKey()) == false | ||
| && isGlobalAndHasIndexHiddenSetting(metadata, entry.getValue(), entry.getKey()) == false | ||
| ) | ||
| .sorted(Comparator.comparing(entry -> entry.getValue().priorityOrZero(), Comparator.reverseOrder())) | ||
| .toList(); | ||
|
|
||
| return metadata.dataStreams() | ||
| .values() | ||
| .stream() | ||
| // Limit to checking data streams that match any of the templates' index patterns | ||
| .filter(ds -> namePatterns.stream().anyMatch(pattern -> Regex.simpleMatch(pattern, ds.getName()))) | ||
| .filter(ds -> { | ||
| // Retrieve the templates that match the data stream name ordered by priority | ||
| List<Tuple<String, ComposableIndexTemplate>> candidates = findV2CandidateTemplates(metadata, ds.getName(), ds.isHidden()); | ||
| if (candidates.isEmpty()) { | ||
| throw new IllegalStateException("Data stream " + ds.getName() + " did not match any composable index templates."); | ||
| } | ||
|
|
||
| // Limit data streams that can ONLY use any of the specified templates, we do this by filtering | ||
| // the matching templates that are others than the ones requested and could be a valid template to use. | ||
| return candidates.stream() | ||
| .filter( | ||
| template -> templateNames.contains(template.v1()) == false | ||
| && isGlobalAndHasIndexHiddenSetting(metadata, template.v2(), template.v1()) == false | ||
| ) | ||
| .map(Tuple::v1) | ||
| .toList() | ||
| .isEmpty(); | ||
| }) | ||
| .filter(ds -> findV2Template(state.metadata(), templates, ds.getName(), ds.isHidden(), true) == null) | ||
nielsbauman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .map(DataStream::getName) | ||
| .collect(Collectors.toSet()); | ||
| } | ||
|
|
@@ -1268,7 +1264,27 @@ public static List<IndexTemplateMetadata> findV1Templates(Metadata metadata, Str | |
| */ | ||
| @Nullable | ||
| public static String findV2Template(Metadata metadata, String indexName, boolean isHidden) { | ||
| final List<Tuple<String, ComposableIndexTemplate>> candidates = findV2CandidateTemplates(metadata, indexName, isHidden); | ||
| return findV2Template(metadata, metadata.templatesV2().entrySet(), indexName, isHidden, false); | ||
| } | ||
nielsbauman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Return the name (id) of the highest matching index template for the given index name. In | ||
| * the event that no templates are matched, {@code null} is returned. | ||
| */ | ||
nielsbauman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @Nullable | ||
| public static String findV2Template( | ||
| Metadata metadata, | ||
| Collection<Map.Entry<String, ComposableIndexTemplate>> templateEntries, | ||
nielsbauman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| String indexName, | ||
| boolean isHidden, | ||
| boolean exitOnFirstMatch | ||
| ) { | ||
| final List<Tuple<String, ComposableIndexTemplate>> candidates = findV2CandidateTemplates( | ||
| templateEntries, | ||
| indexName, | ||
| isHidden, | ||
| exitOnFirstMatch | ||
| ); | ||
| if (candidates.isEmpty()) { | ||
| return null; | ||
| } | ||
|
|
@@ -1296,27 +1312,45 @@ public static String findV2Template(Metadata metadata, String indexName, boolean | |
| /** | ||
| * Return an ordered list of the name (id) and composable index templates that would apply to an index. The first | ||
| * one is the winner template that is applied to this index. In the event that no templates are matched, | ||
| * an empty list is returned. | ||
| * an empty list is returned. If <code>exitOnFirstMatch</code> is true, we return immediately after finding a match. | ||
| */ | ||
nielsbauman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| static List<Tuple<String, ComposableIndexTemplate>> findV2CandidateTemplates(Metadata metadata, String indexName, boolean isHidden) { | ||
| static List<Tuple<String, ComposableIndexTemplate>> findV2CandidateTemplates( | ||
| Collection<Map.Entry<String, ComposableIndexTemplate>> templateEntries, | ||
nielsbauman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| String indexName, | ||
| boolean isHidden, | ||
| boolean exitOnFirstMatch | ||
| ) { | ||
| final String resolvedIndexName = IndexNameExpressionResolver.DateMathExpressionResolver.resolveExpression(indexName); | ||
| final Predicate<String> patternMatchPredicate = pattern -> Regex.simpleMatch(pattern, resolvedIndexName); | ||
| final List<Tuple<String, ComposableIndexTemplate>> candidates = new ArrayList<>(); | ||
| for (Map.Entry<String, ComposableIndexTemplate> entry : metadata.templatesV2().entrySet()) { | ||
| outerLoop: | ||
|
||
| for (Map.Entry<String, ComposableIndexTemplate> entry : templateEntries) { | ||
| final String name = entry.getKey(); | ||
| final ComposableIndexTemplate template = entry.getValue(); | ||
| if (isHidden == false) { | ||
| final boolean matched = template.indexPatterns().stream().anyMatch(patternMatchPredicate); | ||
| if (matched) { | ||
| candidates.add(Tuple.tuple(name, template)); | ||
| for (String indexPattern : template.indexPatterns()) { | ||
| if (Regex.simpleMatch(indexPattern, resolvedIndexName)) { | ||
| candidates.add(Tuple.tuple(name, template)); | ||
nielsbauman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (exitOnFirstMatch) { | ||
| return candidates; | ||
| } | ||
| } | ||
| } | ||
| } else { | ||
| final boolean isNotMatchAllTemplate = template.indexPatterns().stream().noneMatch(Regex::isMatchAllPattern); | ||
| if (isNotMatchAllTemplate) { | ||
| if (template.indexPatterns().stream().anyMatch(patternMatchPredicate)) { | ||
| // For hidden indices, we don't want to return any templates that match "all". | ||
| boolean match = false; | ||
| for (String indexPattern : template.indexPatterns()) { | ||
| if (Regex.isMatchAllPattern(indexPattern)) { | ||
| continue outerLoop; | ||
| } | ||
| // If we've already found a match, we still want to keep looping to make sure this template doesn't have a "*" pattern. | ||
| if (match == false && Regex.simpleMatch(indexPattern, resolvedIndexName)) { | ||
| candidates.add(Tuple.tuple(name, template)); | ||
| match = true; | ||
| } | ||
nielsbauman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
nielsbauman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (match && exitOnFirstMatch) { | ||
| return candidates; | ||
| } | ||
nielsbauman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| package org.elasticsearch.xpack.core.ilm; | ||
|
|
||
| import org.apache.lucene.util.CollectionUtil; | ||
| import org.elasticsearch.action.support.IndicesOptions; | ||
| import org.elasticsearch.cluster.ClusterState; | ||
| import org.elasticsearch.cluster.metadata.ComposableIndexTemplate; | ||
| import org.elasticsearch.cluster.metadata.IndexMetadata; | ||
| import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; | ||
| import org.elasticsearch.cluster.metadata.ItemUsage; | ||
| import org.elasticsearch.cluster.metadata.MetadataIndexTemplateService; | ||
| import org.elasticsearch.common.regex.Regex; | ||
| import org.elasticsearch.common.settings.Settings; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Comparator; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * A class that can be used to calculate the usages of ILM policies. This class computes some information on initialization, which will | ||
| * use a bit more memory but speeds up the usage calculation significantly. | ||
| */ | ||
| public class LifecyclePolicyUsageCalculator { | ||
|
|
||
| private final ClusterState state; | ||
| /** Whether {@link #calculateUsage} will be called multiple times or not. */ | ||
| private final boolean willIterate; | ||
| /** A map from policy name to list of data streams that use that policy. */ | ||
| private final Map<String, List<String>> policyToDataStream; | ||
| /** A map from composable template name to the policy name it uses (or null) */ | ||
| private final Map<String, String> templateToPolicy; | ||
|
||
|
|
||
| public LifecyclePolicyUsageCalculator( | ||
| final IndexNameExpressionResolver indexNameExpressionResolver, | ||
| final ClusterState state, | ||
| List<String> names | ||
nielsbauman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) { | ||
| this.state = state; | ||
| this.willIterate = names.size() > 1 || Regex.isSimpleMatchPattern(names.get(0)); | ||
|
|
||
| var allDataStreams = indexNameExpressionResolver.dataStreamNames(state, IndicesOptions.LENIENT_EXPAND_OPEN_CLOSED_HIDDEN); | ||
| // Sort all templates by descending priority. That way, findV2Template can exit on the first found template. | ||
| var indexTemplates = new ArrayList<>(state.metadata().templatesV2().entrySet()); | ||
| CollectionUtil.timSort(indexTemplates, Comparator.comparing(entry -> entry.getValue().priorityOrZero(), Comparator.reverseOrder())); | ||
|
|
||
| // Build the maps that will be used for the usage calculation later on. | ||
| IndexLifecycleMetadata metadata = state.metadata().custom(IndexLifecycleMetadata.TYPE); | ||
| policyToDataStream = new HashMap<>(Regex.isSimpleMatchPattern(names.get(0)) ? metadata.getPolicyMetadatas().size() : names.size()); | ||
| templateToPolicy = new HashMap<>(indexTemplates.size()); | ||
| for (String dataStream : allDataStreams) { | ||
| String indexTemplate = MetadataIndexTemplateService.findV2Template(state.metadata(), indexTemplates, dataStream, false, true); | ||
| if (indexTemplate == null) { | ||
| continue; | ||
| } | ||
| Settings settings = MetadataIndexTemplateService.resolveSettings(state.metadata(), indexTemplate); | ||
nielsbauman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| var policyName = LifecycleSettings.LIFECYCLE_NAME_SETTING.get(settings); | ||
| if (names.stream().noneMatch(name -> Regex.simpleMatch(name, policyName))) { | ||
| // If a template's policy doesn't match any of the supplied names, we can skip it later on. | ||
| templateToPolicy.put(indexTemplate, null); | ||
| continue; | ||
| } | ||
| templateToPolicy.put(indexTemplate, policyName); | ||
| policyToDataStream.computeIfAbsent(policyName, k -> new ArrayList<>()).add(dataStream); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Calculate the indices, data streams, and composable templates that use the given policy. | ||
| */ | ||
| public ItemUsage calculateUsage(String policyName) { | ||
| List<String> indices = new ArrayList<>(); | ||
| for (IndexMetadata indexMetadata : state.metadata().indices().values()) { | ||
| if (policyName.equals(indexMetadata.getLifecyclePolicyName())) { | ||
| indices.add(indexMetadata.getIndex().getName()); | ||
|
||
| } | ||
| } | ||
|
|
||
| List<String> composableTemplates = new ArrayList<>(); | ||
| for (Map.Entry<String, ComposableIndexTemplate> entry : state.metadata().templatesV2().entrySet()) { | ||
| var foundPolicy = templateToPolicy.get(entry.getKey()); | ||
| // Extra `containsKey` check to account for templates not using any policy. | ||
| if (foundPolicy == null && templateToPolicy.containsKey(entry.getKey()) == false) { | ||
| Settings settings = MetadataIndexTemplateService.resolveSettings(entry.getValue(), state.metadata().componentTemplates()); | ||
| foundPolicy = LifecycleSettings.LIFECYCLE_NAME_SETTING.get(settings); | ||
| // If this method will only be called once, we don't need to keep building the map. | ||
| if (willIterate) { | ||
| templateToPolicy.put(entry.getKey(), foundPolicy); | ||
| } | ||
| } | ||
| if (policyName.equals(foundPolicy)) { | ||
| composableTemplates.add(entry.getKey()); | ||
| } | ||
| } | ||
|
|
||
| return new ItemUsage(indices, policyToDataStream.getOrDefault(policyName, List.of()), composableTemplates); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.