-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Some obvious speedups to parsing alias filters #127240
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
Merged
original-brownbear
merged 5 commits into
elastic:main
from
original-brownbear:fix-parsing-aliasfilter
Apr 23, 2025
+52
−52
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a8f5285
Some obvious speedups to parsing alias filters
original-brownbear cdbe3d6
Merge branch 'main' into fix-parsing-aliasfilter
original-brownbear 394a329
Merge remote-tracking branch 'elastic/main' into fix-parsing-aliasfilter
original-brownbear 4239e1a
fix test
original-brownbear 7d82b9a
Merge remote-tracking branch 'origin/fix-parsing-aliasfilter' into fi…
original-brownbear File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1023,6 +1023,12 @@ public IndicesQueryCache getIndicesQueryCache() { | |
return indicesQueryCache; | ||
} | ||
|
||
private QueryBuilder parseFilter(BytesReference bytes) throws IOException { | ||
try (XContentParser parser = XContentHelper.createParser(parserConfig, bytes)) { | ||
return parseTopLevelQuery(parser); | ||
} | ||
} | ||
|
||
static class OldShardsStats implements IndexEventListener { | ||
|
||
final SearchStats searchStats = new SearchStats(); | ||
|
@@ -1743,13 +1749,6 @@ interface IndexDeletionAllowedPredicate { | |
public AliasFilter buildAliasFilter(ProjectState project, String index, Set<ResolvedExpression> resolvedExpressions) { | ||
/* Being static, parseAliasFilter doesn't have access to whatever guts it needs to parse a query. Instead of passing in a bunch | ||
* of dependencies we pass in a function that can perform the parsing. */ | ||
CheckedFunction<BytesReference, QueryBuilder, IOException> filterParser = bytes -> { | ||
try ( | ||
XContentParser parser = XContentHelper.createParserNotCompressed(parserConfig, bytes, XContentHelper.xContentType(bytes)) | ||
) { | ||
return parseTopLevelQuery(parser); | ||
} | ||
}; | ||
|
||
final ProjectMetadata metadata = project.metadata(); | ||
String[] aliases = indexNameExpressionResolver.filteringAliases(metadata, index, resolvedExpressions); | ||
|
@@ -1759,43 +1758,36 @@ public AliasFilter buildAliasFilter(ProjectState project, String index, Set<Reso | |
|
||
IndexAbstraction ia = metadata.getIndicesLookup().get(index); | ||
DataStream dataStream = ia.getParentDataStream(); | ||
final QueryBuilder filter; | ||
if (dataStream != null) { | ||
var dsAliases = metadata.dataStreamAliases(); | ||
String dataStreamName = dataStream.getName(); | ||
List<QueryBuilder> filters = Arrays.stream(aliases) | ||
.map(name -> metadata.dataStreamAliases().get(name)) | ||
.filter(dataStreamAlias -> dataStreamAlias.getFilter(dataStreamName) != null) | ||
.map(dataStreamAlias -> { | ||
try { | ||
return filterParser.apply(dataStreamAlias.getFilter(dataStreamName).uncompressed()); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
}) | ||
.toList(); | ||
List<QueryBuilder> filters = Arrays.stream(aliases).map(key -> { | ||
var f = dsAliases.get(key).getFilter(dataStreamName); | ||
if (f == null) { | ||
return null; | ||
} | ||
try { | ||
return parseFilter(f.compressedReference()); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
}).filter(Objects::nonNull).toList(); | ||
Comment on lines
+1765
to
+1775
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the value of reordering these stream operations? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We save one lookup on the aliases map by not doing an extra one just to null check :) |
||
if (filters.isEmpty()) { | ||
return AliasFilter.of(null, aliases); | ||
filter = null; | ||
} else if (filters.size() == 1) { | ||
filter = filters.getFirst(); | ||
} else { | ||
if (filters.size() == 1) { | ||
return AliasFilter.of(filters.get(0), aliases); | ||
} else { | ||
BoolQueryBuilder bool = new BoolQueryBuilder(); | ||
for (QueryBuilder filter : filters) { | ||
bool.should(filter); | ||
} | ||
return AliasFilter.of(bool, aliases); | ||
BoolQueryBuilder bool = new BoolQueryBuilder(); | ||
for (QueryBuilder f : filters) { | ||
bool.should(f); | ||
} | ||
filter = bool; | ||
} | ||
} else { | ||
IndexMetadata indexMetadata = metadata.index(index); | ||
return AliasFilter.of(ShardSearchRequest.parseAliasFilter(filterParser, indexMetadata, aliases), aliases); | ||
filter = ShardSearchRequest.parseAliasFilter(this::parseFilter, metadata.index(index), aliases); | ||
} | ||
} | ||
|
||
/** | ||
* Returns a new {@link QueryRewriteContext} with the given {@code now} provider | ||
*/ | ||
public QueryRewriteContext getRewriteContext(LongSupplier nowInMillis, ResolvedIndices resolvedIndices, PointInTimeBuilder pit) { | ||
return getRewriteContext(nowInMillis, resolvedIndices, pit, false); | ||
return AliasFilter.of(filter, aliases); | ||
} | ||
|
||
/** | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of curiosity, hat's the value of inlining this variable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Metadata.all::equals and the
.or
call both actually allocate an object so this removes allocation outright (as well as some overhead for the method handles).Also, the
.or
doesn't compile anything really, it just chains two predicates generically, unless that inlines we're just burning extra cycles for method calls vs a non-capturing lambda :)