Skip to content

Add a checkCancelled public method to ContextIndexSearcher #130645

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,17 @@ public boolean hasCancellations() {
return this.cancellable.isEnabled();
}

/**
* Checks if the current task has been cancelled and throws a {@link TaskCancelledException} if so.
* This method provides plugin developers (e.g., custom runtime fields) with a way to detect
* task cancellation and act accordingly, for example to release resources early.
*/
public void checkCancelled() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add javadocs please?

I am a bit worried that we may end up removing it in the future, because it's used only in tests. Are there other places in prod code where this method may be used perhaps? Could you comment on the fact that it's handy to have for plugins and explain the usecase so that does not get lost? Thanks!

if (this.cancellable.isEnabled()) {
this.cancellable.checkCancelled();
}
}

public void setAggregatedDfs(AggregatedDfs aggregatedDfs) {
this.aggregatedDfs = aggregatedDfs;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,25 @@ public void testCancellableCollector() throws IOException {
assertThat(totalHits2, equalTo(reader.numDocs()));
}

public void testCheckCancelled() throws IOException {
Runnable cancellation = () -> { throw new TaskCancelledException("cancelled"); };
ContextIndexSearcher searcher = new ContextIndexSearcher(
reader,
IndexSearcher.getDefaultSimilarity(),
IndexSearcher.getDefaultQueryCache(),
IndexSearcher.getDefaultQueryCachingPolicy(),
true
);

searcher.checkCancelled();

searcher.addQueryCancellation(cancellation);
expectThrows(TaskCancelledException.class, searcher::checkCancelled);

searcher.removeQueryCancellation(cancellation);
searcher.checkCancelled();
}

public void testExitableDirectoryReader() throws IOException {
AtomicBoolean cancelled = new AtomicBoolean(true);
Runnable cancellation = () -> {
Expand Down