Skip to content

Return partial results when a Vamana search is cancelled - #483

Open
yuejiaointel wants to merge 5 commits into
mainfrom
dev/yuejiao/vamana-cancel-partial-results
Open

yuejiaointel wants to merge 5 commits into
mainfrom
dev/yuejiao/vamana-cancel-partial-results

Conversation

@yuejiaointel

@yuejiaointel yuejiaointel commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

What

When a Vamana search is cancelled through the cancel predicate, the result rows now hold the results found so far:

  • An interrupted query keeps the neighbors it found, nearest first, followed by the invalid-neighbor sentinel (type_traits::sentinel_v<Neighbor<I>, Cmp>: max ID, worst distance for the metric).
  • A query that had not started when the cancel fired may hold only its entry point(s) (the greedy search seeds them before it first polls cancel), followed by sentinels.
  • Dynamic indexes still translate IDs to external on cancel (sentinel slots are skipped).
  • check_and_supplement_search_buffer skips the top-up after a cancel.
  • The extra pre-search cancel() poll in the default single_search is removed, since the search polls cancel itself.
  • The single-query search(query, scratch, cancel) overloads now forward cancel to single_search.

Non-cancelled searches produce the same results as before, and the no-cancel path gets no extra cancel() calls.

Why

Before this change, a cancelled batch search copied all k slots of the search buffer regardless of how many were valid, so rows contained entries left over from the previous query (or uninitialized data for queries that never started), and dynamic indexes returned internal IDs. Callers could not use anything from a cancelled search.

This is needed for #400 (cancellation callback in the C API), which asks for the candidates collected so far to be returned on cancel. valkey-search returns partial results on query timeout by default, the same as its HNSW path.

Tests

New test case Vamana Cancelled Search ([cancel]) in tests/svs/index/vamana/dynamic_index.cpp, using a predicate that fires after N polls on a static index and on a dynamic index with non-contiguous external IDs and deletions: every row is a sorted prefix of live neighbors with exact distances followed only by sentinels. A never-firing predicate gives results identical to no predicate.

Notes

🤖 Generated with Claude Code

On cancel, the batch search now writes the neighbors each query found so
far, padded with the invalid-neighbor sentinel, instead of leaving stale
or uninitialized entries. Dynamic indexes still translate IDs to external
on cancel, the top-up of short search buffers is skipped after a cancel,
and the single-query search overloads forward cancel to single_search.
Non-cancelled searches are unchanged.
@yuejiaointel
yuejiaointel marked this pull request as ready for review November 25, 2026 23:17

@rfsaliev rfsaliev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGFM
Just 1 proposal

Comment on lines 446 to 454
for (auto external_id : index.external_ids()) {
auto internal_id = index.translate_external_id(external_id);
auto dist = index.get_distance(external_id, query);
auto builder = index.internal_search_builder();
search_buffer.insert(builder(internal_id, dist));
if (search_buffer.valid() >= search_buffer.target_window()) {
break;
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It seems like pretty expensive loop - I would add cancel() check in the loop as well

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants