-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Optimize the canMatch phase on the data node #14511
base: main
Are you sure you want to change the base?
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #14511 +/- ##
============================================
- Coverage 71.84% 71.72% -0.12%
+ Complexity 62820 62720 -100
============================================
Files 5169 5169
Lines 294664 294672 +8
Branches 42615 42621 +6
============================================
- Hits 211691 211344 -347
- Misses 65530 65898 +368
+ Partials 17443 17430 -13 ☔ View full report in Codecov by Sentry. |
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.
@bugmakerrrrrr - Thank you for this PR. Given the single threaded nature of canMatch phase, every optimization is critical, especially when 100-1000s of shards are involved! Is there any way to quantify the impact of this change? Also, are the existing tests sufficient to catch any regressions? If not, can we add test cases for the missing code paths?
if (aliasFilterCanMatch == false) { | ||
return new CanMatchResponse(false, null); | ||
} | ||
|
||
// null query means match_all | ||
boolean canMatch = canRewriteToMatchNone(request.source()) == false | ||
|| request.source().query() instanceof MatchNoneQueryBuilder == false; | ||
if (canMatch == false) { | ||
return new CanMatchResponse(false, null); | ||
} |
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.
Can this be better written as below:
if (aliasFilterCanMatch == false || (canRewriteToMatchNone(request.source()) && request.source().query() instanceof MatchNoneQueryBuilder))
return new CanMatchResponse(false, null);
}
private static FieldDoc getPrimarySearchAfterFieldDoc( | ||
FieldSortBuilder primarySortBuilder, | ||
Object primarySearchAfter, | ||
QueryShardContext context | ||
) throws IOException { | ||
final Optional<SortAndFormats> sortOpt = SortBuilder.buildSort(List.of(primarySortBuilder), context); | ||
return sortOpt.map(sortAndFormats -> SearchAfterBuilder.buildFieldDoc(sortAndFormats, new Object[] { primarySearchAfter })) | ||
.orElse(null); |
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.
Unable to see any optimization from this refactoring. Seems more like restructuring and not sure if the readability is improving.
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.
The previous implementation would construct all sortFields if user request contains multi SortBuilders and search after fields, but what we need is just the primary sort search after field.
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.
Is it possible to qualitatively quantify the impact? Maybe some approximation of the worst case query earlier that would be improved with this change
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.
I think that this is quite simple, for example, if I have following query:
{
"query": {
"match_all": {}
},
"track_total_hits": false,
"sort": [
{
"grade": "desc"
},
{
"age": "asc"
},
{
"id": "asc"
}
],
"search_after": [
10,
20,
12345
]
}
Previously, we have to evaluate and build these all three sort fields, now we just need to build the primary sort field. The idea behind this change is same as FieldSortBuilder.getPrimaryFieldSortOrNull
.
boolean canMatch = canRewriteToMatchNone(request.source()) == false | ||
|| request.source().query() instanceof MatchNoneQueryBuilder == false; | ||
if (canMatch == false) { | ||
return new CanMatchResponse(false, null); |
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.
Can we assume that CanMatchResponse(false, null)
equivalent to CanMatchResponse(false,<non-null>)
?
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.
I think yes, CanMatchPreFilterSearchPhase
extract the min/max values of each shard response and use them to pre-sort the shards prior to running the subsequent phases. However, the unmatched shard will be skipped in the following phases, so its min/max value will have no impact on the query task.
No, this optimization is just for reducing unnecessary computation or IO. Currently, I am unable to accurately measure the impact of this change.
Sure. |
❌ Gradle check result for 5186664: FAILURE Please examine the workflow log, locate, and copy-paste the failure(s) below, then iterate to green. Is the failure a flaky test unrelated to your change? |
@jainankitk friendly ping |
if (searchContext.searchAfter() != null | ||
&& searchContext.request() != null | ||
&& searchContext.request().source() != null | ||
&& Objects.equals(searchContext.trackTotalHitsUpTo(), SearchContext.TRACK_TOTAL_HITS_DISABLED)) { |
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.
Any specific reason for using Object.equals
given both the values are primitives
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.
No, I just copied the code from SearchService
(in which trackTotalHitsUpto can be null), I can fix this.
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.
Can you update this?
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.
sure
private static FieldDoc getPrimarySearchAfterFieldDoc( | ||
FieldSortBuilder primarySortBuilder, | ||
Object primarySearchAfter, | ||
QueryShardContext context | ||
) throws IOException { | ||
final Optional<SortAndFormats> sortOpt = SortBuilder.buildSort(List.of(primarySortBuilder), context); | ||
return sortOpt.map(sortAndFormats -> SearchAfterBuilder.buildFieldDoc(sortAndFormats, new Object[] { primarySearchAfter })) | ||
.orElse(null); |
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.
Is it possible to qualitatively quantify the impact? Maybe some approximation of the worst case query earlier that would be improved with this change
@bugmakerrrrrr - While the changes look okay, I am still unsure about the impact and need of this code change. Even if not using some benchmark, having some idea of potential improvement with this change will be good! |
if (searchContext.searchAfter() != null && searchContext.request() != null && searchContext.request().source() != null) { | ||
// Skipping search on shard/segment entirely can cause mismatch on total_tracking_hits, hence skip only if | ||
// track_total_hits is false. | ||
if (searchContext.searchAfter() != null |
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.
@jainankitk The potential improvements are mostly related to short circuit. For example, we can check the conditions upfront here and avoid to build sort field or read min max values unnecessary.
} else { | ||
// null query means match_all | ||
canMatch = aliasFilterCanMatch; | ||
if (aliasFilterCanMatch == false |
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.
here is the same
|
||
if (hasRefreshPending) { | ||
final FieldSortBuilder sortBuilder = FieldSortBuilder.getPrimaryFieldSortOrNull(request.source()); | ||
final MinAndMax<?> minMax = sortBuilder != null ? FieldSortBuilder.getMinMaxOrNull(context, sortBuilder) : null; | ||
return new CanMatchResponse(true, minMax); | ||
} | ||
|
||
final boolean aliasFilterCanMatch = request.getAliasFilter().getQueryBuilder() instanceof MatchNoneQueryBuilder == false; | ||
FieldSortBuilder sortBuilder = FieldSortBuilder.getPrimaryFieldSortOrNull(request.source()); | ||
MinAndMax<?> minMax = sortBuilder != null ? FieldSortBuilder.getMinMaxOrNull(context, sortBuilder) : null; | ||
boolean canMatch; | ||
if (canRewriteToMatchNone(request.source())) { | ||
QueryBuilder queryBuilder = request.source().query(); | ||
canMatch = aliasFilterCanMatch && queryBuilder instanceof MatchNoneQueryBuilder == false; | ||
} else { | ||
// null query means match_all | ||
canMatch = aliasFilterCanMatch; | ||
if (aliasFilterCanMatch == false |
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.
Can we rewrite this as below:
- aliasFilterCanMatch is used as double negation, better change it to aliasFilterCannotMatch
sortBuilder
andminMax
can be done in one place ifCanMatch(false, null)
is pulled up!
if (!hasRefreshPending) {
final boolean aliasFilterCannotMatch = request.getAliasFilter().getQueryBuilder() instanceof MatchNoneQueryBuilder;
if (aliasFilterCannotMatch || (canRewriteToMatchNone(request.source()) && request.source().query() instanceof MatchNoneQueryBuilder) {
return new CanMatchResponse(false, null);
}
}
final FieldSortBuilder sortBuilder = FieldSortBuilder.getPrimaryFieldSortOrNull(request.source());
final MinAndMax<?> minMax = sortBuilder != null ? FieldSortBuilder.getMinMaxOrNull(context, sortBuilder) : null;
if (hasRefreshPending) {
return new CanMatchResponse(true, minMax);
}
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.
Thanks @bugmakerrrrrr, LGTM except one minor comment!
Also can you add entry in CHANGELOG.md, before this change can be reviewed by maintainer and merged! |
canMatch = canMatchSearchAfter(searchAfterFieldDoc, minMax, sortBuilder, trackTotalHitsUpto); | ||
} | ||
} | ||
return new CanMatchResponse(canMatch, canMatch ? minMax : null); |
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.
Also, can we just not send over minMax? Should get ignored if canMatch is false anyway?
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.
I think that setting minMax as null is equivalent to not sending this value.
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.
Then we probably should not even care what we are sending for minMax if canMatch is false? So below should also work?
new CanMatchResponse(canMatch, minMax);
// Check for sort.missing == null, since in case of missing values sort queries, if segment/shard's min/max | ||
// is out of search_after range, it still should be printed and hence we should not skip segment/shard. | ||
if (Objects.equals(trackTotalHitsUpto, SearchContext.TRACK_TOTAL_HITS_DISABLED) | ||
&& minMax != null |
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.
Do we need minMax != null
here, since it is validated in canMatchSearchAfter(searchAfterFieldDoc, minMax, sortBuilder, trackTotalHitsUpto)
?
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.
if minMax == null
, we can avoid to build primary sort field. The validation in canMatchSearchAfter
is mainly used in query phase.
Signed-off-by: panguixin <[email protected]>
Signed-off-by: panguixin <[email protected]>
Signed-off-by: panguixin <[email protected]>
5186664
to
8c91a12
Compare
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.
canMatch = canMatchSearchAfter(searchAfterFieldDoc, minMax, sortBuilder, trackTotalHitsUpto); | ||
} | ||
} | ||
return new CanMatchResponse(canMatch, canMatch ? minMax : null); |
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.
Then we probably should not even care what we are sending for minMax if canMatch is false? So below should also work?
new CanMatchResponse(canMatch, minMax);
@bugmakerrrrrr - Can you resolve the failing CHANGELOG verifier and codecov check while one of the maintainer reviews this PR? |
This PR is stalled because it has been open for 30 days with no activity. |
Description
Two changes here:
CanMatchResponse(false, null)
if cannot match(minMax
value is useless if corresponding shard is not match);searchAfter
field will be evaluated if exists.Related Issues
Resolves #[Issue number to be closed when this PR is merged]
Check List
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.