Skip to content

Commit ae9ce6a

Browse files
committed
Add a checkCancelled public method to ContextIndexSearcher
1 parent f047f78 commit ae9ce6a

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,12 @@ public boolean hasCancellations() {
191191
return this.cancellable.isEnabled();
192192
}
193193

194+
public void checkCancelled() {
195+
if (this.cancellable.isEnabled()) {
196+
this.cancellable.checkCancelled();
197+
}
198+
}
199+
194200
public void setAggregatedDfs(AggregatedDfs aggregatedDfs) {
195201
this.aggregatedDfs = aggregatedDfs;
196202
}

server/src/test/java/org/elasticsearch/search/SearchCancellationTests.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,25 @@ public void testCancellableCollector() throws IOException {
120120
assertThat(totalHits2, equalTo(reader.numDocs()));
121121
}
122122

123+
public void testCheckCancelled() throws IOException {
124+
Runnable cancellation = () -> { throw new TaskCancelledException("cancelled"); };
125+
ContextIndexSearcher searcher = new ContextIndexSearcher(
126+
reader,
127+
IndexSearcher.getDefaultSimilarity(),
128+
IndexSearcher.getDefaultQueryCache(),
129+
IndexSearcher.getDefaultQueryCachingPolicy(),
130+
true
131+
);
132+
133+
searcher.checkCancelled();
134+
135+
searcher.addQueryCancellation(cancellation);
136+
expectThrows(TaskCancelledException.class, searcher::checkCancelled);
137+
138+
searcher.removeQueryCancellation(cancellation);
139+
searcher.checkCancelled();
140+
}
141+
123142
public void testExitableDirectoryReader() throws IOException {
124143
AtomicBoolean cancelled = new AtomicBoolean(true);
125144
Runnable cancellation = () -> {

0 commit comments

Comments
 (0)