From aea2757a8fee0e0d8d66428b62222ecb0a35f7a4 Mon Sep 17 00:00:00 2001 From: Armin Date: Wed, 23 Apr 2025 12:45:39 +0200 Subject: [PATCH 1/2] Remove dead code from IndexNameExpressionResolver Lots of dead code accumulated here cleaning it up. --- .../metadata/IndexNameExpressionResolver.java | 102 +----------------- 1 file changed, 2 insertions(+), 100 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolver.java b/server/src/main/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolver.java index 19189a81a0187..415136fdd977f 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolver.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolver.java @@ -691,26 +691,6 @@ private static void resolveIndicesForDataStreamRelatedAbstraction( } } - private static void resolveWriteIndexForDataStreams( - Context context, - DataStream dataStream, - Set concreteIndicesResult, - IndexComponentSelector selector - ) { - if (shouldIncludeRegularIndices(context.getOptions(), selector)) { - Index writeIndex = dataStream.getWriteIndex(); - if (addIndex(writeIndex, null, context)) { - concreteIndicesResult.add(writeIndex); - } - } - if (shouldIncludeFailureIndices(context.getOptions(), selector)) { - Index failureStoreWriteIndex = dataStream.getWriteFailureIndex(); - if (failureStoreWriteIndex != null && addIndex(failureStoreWriteIndex, null, context)) { - concreteIndicesResult.add(failureStoreWriteIndex); - } - } - } - public static boolean shouldIncludeRegularIndices(IndicesOptions indicesOptions, IndexComponentSelector expressionSelector) { if (indicesOptions.allowSelectors()) { return expressionSelector == null || expressionSelector.shouldIncludeData(); @@ -873,19 +853,6 @@ public Index concreteSingleIndex(ProjectMetadata project, IndicesRequest request return indices[0]; } - /** - * Utility method that allows to resolve an index expression to its corresponding single write index. - * - * @param state the cluster state containing all the data to resolve to expression to a concrete index - * @param request The request that defines how an alias or an index need to be resolved to a concrete index - * and the expression that can be resolved to an alias or an index name. - * @throws IllegalArgumentException if the index resolution does not lead to an index, or leads to more than one index - * @return the write index obtained as a result of the index resolution - */ - public Index concreteWriteIndex(ClusterState state, IndicesRequest request) { - return concreteWriteIndex(projectResolver.getProjectMetadata(state), request); - } - /** * Utility method that allows to resolve an index expression to its corresponding single write index. * @@ -977,14 +944,6 @@ public Index concreteWriteIndex( return indices[0]; } - /** - * @return whether the specified index, data stream or alias exists. - * If the data stream, index or alias contains date math then that is resolved too. - */ - public boolean hasIndexAbstraction(String indexAbstraction, ClusterState state) { - return hasIndexAbstraction(indexAbstraction, projectResolver.getProjectMetadata(state)); - } - /** * @return whether the specified index, data stream or alias exists. * If the data stream, index or alias contains date math then that is resolved too. @@ -1041,13 +1000,6 @@ public static void assertExpressionHasNullOrDataSelector(String expression) { } } - /** - * Resolve an array of expressions to the set of indices and aliases that these expressions match. - */ - public Set resolveExpressions(ClusterState state, String... expressions) { - return resolveExpressions(projectResolver.getProjectMetadata(state), expressions); - } - /** * Resolve an array of expressions to the set of indices and aliases that these expressions match. */ @@ -1055,20 +1007,6 @@ public Set resolveExpressions(ProjectMetadata project, Strin return resolveExpressions(project, IndicesOptions.lenientExpandOpen(), false, expressions); } - /** - * Resolve the expression to the set of indices, aliases, and, optionally, datastreams that the expression matches. - * If {@param preserveDataStreams} is {@code true}, datastreams that are covered by the wildcards from the - * {@param expressions} are returned as-is, without expanding them further to their respective backing indices. - */ - public Set resolveExpressions( - ClusterState state, - IndicesOptions indicesOptions, - boolean preserveDataStreams, - String... expressions - ) { - return resolveExpressions(projectResolver.getProjectMetadata(state), indicesOptions, preserveDataStreams, expressions); - } - /** * Resolve the expression to the set of indices, aliases, and, optionally, datastreams that the expression matches. * If {@param preserveDataStreams} is {@code true}, datastreams that are covered by the wildcards from the @@ -1101,17 +1039,6 @@ public Set resolveExpressions( } } - /** - * Iterates through the list of indices and selects the effective list of filtering aliases for the - * given index. - *

Only aliases with filters are returned. If the indices list contains a non-filtering reference to - * the index itself - null is returned. Returns {@code null} if no filtering is required. - * NOTE: The provided expressions must have been resolved already via {@link #resolveExpressionsToResources(Context, String...)}. - */ - public String[] filteringAliases(ClusterState state, String index, Set resolvedExpressions) { - return filteringAliases(projectResolver.getProjectMetadata(state), index, resolvedExpressions); - } - /** * Iterates through the list of indices and selects the effective list of filtering aliases for the * given index. @@ -1140,31 +1067,6 @@ boolean iterateIndexAliases(int indexAliasesSize, int resolvedExpressionsSize) { return indexAliasesSize <= resolvedExpressionsSize; } - /** - * Iterates through the list of indices and selects the effective list of required aliases for the given index. - *

Only aliases where the given predicate tests successfully are returned. If the indices list contains a non-required reference to - * the index itself - null is returned. Returns {@code null} if no filtering is required. - *

NOTE: the provided expressions must have been resolved already via - * {@link #resolveExpressionsToResources(Context, String...)}. - */ - public String[] indexAliases( - ClusterState state, - String index, - Predicate requiredAlias, - Predicate requiredDataStreamAlias, - boolean skipIdentity, - Set resolvedExpressions - ) { - return indexAliases( - projectResolver.getProjectMetadata(state), - index, - requiredAlias, - requiredDataStreamAlias, - skipIdentity, - resolvedExpressions - ); - } - /** * Iterates through the list of indices and selects the effective list of required aliases for the given index. *

Only aliases where the given predicate tests successfully are returned. If the indices list contains a non-required reference to @@ -1827,9 +1729,9 @@ private static IndexMetadata.State excludeState(IndicesOptions options) { final IndexMetadata.State excludeState; if (options.expandWildcardsOpen() && options.expandWildcardsClosed()) { excludeState = null; - } else if (options.expandWildcardsOpen() && options.expandWildcardsClosed() == false) { + } else if (options.expandWildcardsOpen()) { excludeState = IndexMetadata.State.CLOSE; - } else if (options.expandWildcardsClosed() && options.expandWildcardsOpen() == false) { + } else if (options.expandWildcardsClosed()) { excludeState = IndexMetadata.State.OPEN; } else { assert false : "this shouldn't get called if wildcards expand to none"; From c27b89ef8024c2f9d153e928524be581025e7c13 Mon Sep 17 00:00:00 2001 From: Armin Date: Wed, 23 Apr 2025 13:19:04 +0200 Subject: [PATCH 2/2] Speedup resolving index aliases in TransportSearchAction (and in general) Very often there are no aliases, no need to create a bunch of objects in this case. Also, no need to build an intermediary candidate list either. Plus the nested streams do get very costly when running this logic over a large number of indices. --- .../metadata/IndexNameExpressionResolver.java | 50 +++++++++---------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolver.java b/server/src/main/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolver.java index 415136fdd977f..8321c736d8699 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolver.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/IndexNameExpressionResolver.java @@ -1149,39 +1149,35 @@ public String[] indexAliases( return requiredAliases.toArray(Strings.EMPTY_ARRAY); } else { final Map indexAliases = indexMetadata.getAliases(); - final AliasMetadata[] aliasCandidates; - if (iterateIndexAliases(indexAliases.size(), resolvedExpressions.size())) { + final int aliasCount = indexAliases.size(); + if (aliasCount == 0) { + return null; + } + final List aliases = new ArrayList<>(aliasCount); + if (iterateIndexAliases(aliasCount, resolvedExpressions.size())) { // faster to iterate indexAliases - aliasCandidates = indexAliases.values() - .stream() - // Indices can only be referenced with a data selector, or a null selector if selectors are disabled - .filter(aliasMetadata -> resolvedExpressionsContainsAbstraction(resolvedExpressions, aliasMetadata.alias())) - .toArray(AliasMetadata[]::new); + // Indices can only be referenced with a data selector, or a null selector if selectors are disabled + for (AliasMetadata aliasMetadata : indexAliases.values()) { + var alias = aliasMetadata.alias(); + if (resolvedExpressionsContainsAbstraction(resolvedExpressions, alias)) { + if (requiredAlias.test(aliasMetadata) == false) { + return null; + } + aliases.add(alias); + } + } } else { // faster to iterate resolvedExpressions - aliasCandidates = resolvedExpressions.stream() - .map(ResolvedExpression::resource) - .map(indexAliases::get) - .filter(Objects::nonNull) - .toArray(AliasMetadata[]::new); - } - List aliases = null; - for (int i = 0; i < aliasCandidates.length; i++) { - AliasMetadata aliasMetadata = aliasCandidates[i]; - if (requiredAlias.test(aliasMetadata)) { - // If required - add it to the list of aliases - if (aliases == null) { - aliases = new ArrayList<>(); + for (ResolvedExpression resolvedExpression : resolvedExpressions) { + AliasMetadata aliasMetadata = indexAliases.get(resolvedExpression.resource()); + if (aliasMetadata != null) { + if (requiredAlias.test(aliasMetadata) == false) { + return null; + } + aliases.add(aliasMetadata.getAlias()); } - aliases.add(aliasMetadata.alias()); - } else { - // If not, we have a non required alias for this index - no further checking needed - return null; } } - if (aliases == null) { - return null; - } return aliases.toArray(Strings.EMPTY_ARRAY); } }