Skip to content

fix: propagate cancellation during exact DVR refinement - #2007

Open
cqy123456 wants to merge 1 commit into
zilliztech:mainfrom
cqy123456:fix/dvr-cancel-exact-refinement
Open

cqy123456 wants to merge 1 commit into
zilliztech:mainfrom
cqy123456:fix/dvr-cancel-exact-refinement

Conversation

@cqy123456

Copy link
Copy Markdown
Collaborator

Related issue: #2006

What changed

SCANN_DVR passed the query's OpContext to ScaNN candidate search, then dropped it before exact refinement. A cancellation during recall could still lead to raw-vector reads and a successful result. During refinement, a cancellation raised by the data view could also be reported as a Faiss error.

DVR checks for cancellation after candidate search and while Faiss visits the candidate IDs during exact scoring. It polls every 256 IDs and once after the scan to catch a cancellation on the final read. A null OpContext uses the original distance calculation. An in-flight Faiss call can finish its current query.

Regression tests

  • [dvr_base_search] requests cancellation inside the ScaNN coarse quantizer. Standalone ScaNN finished the query. On the same input, SCANN_DVR at 8af2dcc2 returned success and performed one refinement read. With this fix it returned cancelled with zero reads (6 assertions).
  • [dvr_refinement][cancel] exercises both Search and CalcDistByIDs. Each returns all 1,024 expected distances with a null context and with a live context. Cancellation on the first or last read returns Status::cancelled, including when the data-view callback throws a cancellation exception.

@sre-ci-robot

Copy link
Copy Markdown
Collaborator

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: cqy123456

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@mergify

mergify Bot commented Dec 4, 2026

Copy link
Copy Markdown

Tick the box to add this pull request to the merge queue (same as @mergifyio queue). If it does not match the queue conditions yet, it enters the queue once it does.

  • Queue this pull request once it meets the queue conditions

@cqy123456
cqy123456 force-pushed the fix/dvr-cancel-exact-refinement branch from 2bb25c9 to 80d9c9b Compare December 4, 2026 06:53
return quant_res;
}
// refine
checkCancellation(op_context);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[MEDIUM] Both checkCancellation(op_context) calls here (line 517) and after the try/catch block (line 532) sit outside the exception handler, so IndexNodeWithDataViewRefiner::Search throws folly::FutureCancellation directly instead of returning expected<DataSetPtr>::Err(Status::cancelled, ...). That differs from cancellations caught inside SearchWithIds (lines 526–531), CalcDistByStorageIds, and other IndexNode::Search implementations (e.g. IvfIndexNode::Search), and breaks direct IndexNode::Search callers such as index.Node()->Search(...), IndexNodeThreadPoolWrapper::Search, and emb-list strategies that inspect expected::error(). The added [dvr_base_search][cancel] test currently passes only because Index<T>::Search masks the escaping exception in GuardedCall (which also skips the search-latency and top-k metric observations in src/index/index.cc:564-567).

To preserve the expected return contract without widening the try block over the buffer allocations (which would recode std::bad_alloc as faiss_inner_error), return Status::cancelled explicitly at line 517 and move the line 532 check inside the try block (or into DataViewIndexFlat::SearchWithIds right after WaitAllSuccess so SearchWithIds matches CalcDistByIDs):

if (op_context != nullptr && op_context->cancellation_token.isCancellationRequested()) {
    return expected<DataSetPtr>::Err(Status::cancelled, "cancelled by the caller");
}
try {
    refine_offset_index_->SearchWithIds(nq, dataset->GetTensor(), queries_lims.data(), refine_ids, topk,
                                        distances.get(), labels.get(), refine_with_quant, {}, op_context);
    checkCancellation(op_context);
} catch (const std::exception& e) {
    return RefinementError(e);
}

Please also assert index.Node()->Search(...) directly in [dvr_base_search][cancel] (in addition to index.Search(...)) so GuardedCall cannot hide an escaping cancellation exception.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in 6eedde9. Search now returns Status::cancelled if cancellation occurs before refinement, and the post-refinement check is inside the existing try block. I added a direct index.Node()->Search() regression test that reproduced the escaping exception before the fix. It now verifies Status::cancelled with zero refinement reads.

Pass the operation context through candidate search and exact scoring. Poll while visiting candidate IDs, preserving the original distance scan when there is no context. Map data-view cancellation consistently in Search and CalcDistByIDs.

Cover cancellation inside the ScaNN coarse quantizer and on the first or final refinement read. Verify every exact distance with both a null and a live context.

Signed-off-by: cqy123456 <39671710+cqy123456@users.noreply.github.com>
@cqy123456
cqy123456 force-pushed the fix/dvr-cancel-exact-refinement branch from 80d9c9b to 6eedde9 Compare December 4, 2026 10:17
@alexanderguzhva

Copy link
Copy Markdown
Collaborator

/lgtm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants