Skip to content

Commit 090e0dd

Browse files
Deduplicate code for getting cancellation checks out of a search context (#120828)
Just deduplicating the logic and moving it to a shared location + no need for a static method like that.
1 parent 7d7a9d9 commit 090e0dd

File tree

3 files changed

+18
-41
lines changed

3 files changed

+18
-41
lines changed

server/src/main/java/org/elasticsearch/search/aggregations/AggregationPhase.java

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,10 @@
88
*/
99
package org.elasticsearch.search.aggregations;
1010

11-
import org.elasticsearch.action.search.SearchShardTask;
1211
import org.elasticsearch.search.aggregations.support.TimeSeriesIndexSearcher;
1312
import org.elasticsearch.search.internal.SearchContext;
14-
import org.elasticsearch.search.query.QueryPhase;
1513

1614
import java.io.IOException;
17-
import java.util.ArrayList;
1815
import java.util.List;
1916
import java.util.function.Supplier;
2017

@@ -59,7 +56,7 @@ private static AggregatorCollector newAggregatorCollector(SearchContext context)
5956
}
6057

6158
private static void executeInSortOrder(SearchContext context, BucketCollector collector) {
62-
TimeSeriesIndexSearcher searcher = new TimeSeriesIndexSearcher(context.searcher(), getCancellationChecks(context));
59+
TimeSeriesIndexSearcher searcher = new TimeSeriesIndexSearcher(context.searcher(), context.getCancellationChecks());
6360
searcher.setMinimumScore(context.minimumScore());
6461
searcher.setProfiler(context);
6562
try {
@@ -70,23 +67,4 @@ private static void executeInSortOrder(SearchContext context, BucketCollector co
7067
}
7168
}
7269

73-
private static List<Runnable> getCancellationChecks(SearchContext context) {
74-
List<Runnable> cancellationChecks = new ArrayList<>();
75-
if (context.lowLevelCancellation()) {
76-
// This searching doesn't live beyond this phase, so we don't need to remove query cancellation
77-
cancellationChecks.add(() -> {
78-
final SearchShardTask task = context.getTask();
79-
if (task != null) {
80-
task.ensureNotCancelled();
81-
}
82-
});
83-
}
84-
85-
final Runnable timeoutRunnable = QueryPhase.getTimeoutCheck(context);
86-
if (timeoutRunnable != null) {
87-
cancellationChecks.add(timeoutRunnable);
88-
}
89-
90-
return cancellationChecks;
91-
}
9270
}

server/src/main/java/org/elasticsearch/search/internal/SearchContext.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.elasticsearch.search.fetch.subphase.ScriptFieldsContext;
4242
import org.elasticsearch.search.fetch.subphase.highlight.SearchHighlightContext;
4343
import org.elasticsearch.search.profile.Profilers;
44+
import org.elasticsearch.search.query.QueryPhase;
4445
import org.elasticsearch.search.query.QuerySearchResult;
4546
import org.elasticsearch.search.rank.context.QueryPhaseRankShardContext;
4647
import org.elasticsearch.search.rank.feature.RankFeatureResult;
@@ -84,6 +85,21 @@ public abstract class SearchContext implements Releasable {
8485

8586
protected SearchContext() {}
8687

88+
public final List<Runnable> getCancellationChecks() {
89+
final Runnable timeoutRunnable = QueryPhase.getTimeoutCheck(this);
90+
if (lowLevelCancellation()) {
91+
// This searching doesn't live beyond this phase, so we don't need to remove query cancellation
92+
Runnable c = () -> {
93+
final SearchShardTask task = getTask();
94+
if (task != null) {
95+
task.ensureNotCancelled();
96+
}
97+
};
98+
return timeoutRunnable == null ? List.of(c) : List.of(c, timeoutRunnable);
99+
}
100+
return timeoutRunnable == null ? List.of() : List.of(timeoutRunnable);
101+
}
102+
87103
public abstract void setTask(SearchShardTask task);
88104

89105
public abstract SearchShardTask getTask();

server/src/main/java/org/elasticsearch/search/rescore/RescorePhase.java

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,16 @@
1515
import org.apache.lucene.search.TopDocs;
1616
import org.apache.lucene.search.TopFieldDocs;
1717
import org.elasticsearch.ElasticsearchException;
18-
import org.elasticsearch.action.search.SearchShardTask;
1918
import org.elasticsearch.common.lucene.search.TopDocsAndMaxScore;
2019
import org.elasticsearch.common.util.Maps;
2120
import org.elasticsearch.lucene.grouping.TopFieldGroups;
2221
import org.elasticsearch.search.internal.ContextIndexSearcher;
2322
import org.elasticsearch.search.internal.SearchContext;
24-
import org.elasticsearch.search.query.QueryPhase;
2523
import org.elasticsearch.search.query.SearchTimeoutException;
2624
import org.elasticsearch.search.sort.ShardDocSortField;
2725
import org.elasticsearch.search.sort.SortAndFormats;
2826

2927
import java.io.IOException;
30-
import java.util.ArrayList;
3128
import java.util.Arrays;
3229
import java.util.List;
3330
import java.util.Map;
@@ -195,21 +192,7 @@ private static boolean topDocsSortedByScore(TopDocs topDocs) {
195192
}
196193

197194
static Runnable getCancellationChecks(SearchContext context) {
198-
List<Runnable> cancellationChecks = new ArrayList<>();
199-
if (context.lowLevelCancellation()) {
200-
cancellationChecks.add(() -> {
201-
final SearchShardTask task = context.getTask();
202-
if (task != null) {
203-
task.ensureNotCancelled();
204-
}
205-
});
206-
}
207-
208-
final Runnable timeoutRunnable = QueryPhase.getTimeoutCheck(context);
209-
if (timeoutRunnable != null) {
210-
cancellationChecks.add(timeoutRunnable);
211-
}
212-
195+
List<Runnable> cancellationChecks = context.getCancellationChecks();
213196
return () -> {
214197
for (var check : cancellationChecks) {
215198
check.run();

0 commit comments

Comments
 (0)