Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
5 changes: 5 additions & 0 deletions docs/changelog/130279.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 130279
summary: Fix missing removal of query cancellation callback in QueryPhase
area: Search
type: bug
issues: [130071]
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,9 @@ private SearchPhaseResult executeQueryPhase(ShardSearchRequest request, Cancella
tracer.stopTrace(task);
}
if (request.numberOfShards() == 1 && (request.source() == null || request.source().rankBuilder() == null)) {
// we already have query results, but we can run fetch at the same time
// in this case, we reuse search context across search and fetch phases.
// so we need to remove all cancelation callbacks from query phase before starting fetch.
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 leave the initial comment, and perhaps rewrite the new part to something like

in this case we reuse the search context across search and fetch phase, hence we need to clear the cancellation checks that were applied by the query phase before running fetch. Note that the timeout checks are not applied to the fetch phase, while the cancellation checks are.

context.searcher().removeQueryCancellations();
Copy link
Member

Choose a reason for hiding this comment

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

There are two main scenarios where we get into this codepath:

  1. multiple shards: new search context just created, #preProcess was called, the cancellation checks are added
  2. single shard: search context being reused, we need to clean up the timeout check but not the cancellation checks. Alternatively, we can clear it all up and then add back the cancellation check only.

We are good for the first scenario.

I think that in the second case, calling preProcess again may fix it but other cause other side effects. Perhaps we should restore the cancellation check as follows after the removal:

if (context.lowLevelCancellation()) {
    context.searcher().addQueryCancellation(() -> {
        if (task != null) {
            task.ensureNotCancelled();
        }
    });
}

I still prefer this over selectively removing only the timeout check, although it's not super clear. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like this solution, it addresses the problem in a focused way, and the comment makes it very clear.

context.addFetchResult();
return executeFetchPhase(readerContext, context, afterQueryTime);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ public void close() {
this.cancellable.clear();
}

// remove all registered cancellation callbacks to prevent them from leaking into other phases
public void removeQueryCancellations() {
Copy link
Member

Choose a reason for hiding this comment

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

I have second thoughts on the naming I picked, sorry! Should we go with clearQueryCancellations ?

this.cancellable.clear();
}

public boolean hasCancellations() {
return this.cancellable.isEnabled();
}
Expand Down
Loading