Skip to content

Support fine-grained SVS index - #1058

Open
razdoburdin wants to merge 119 commits into
mainfrom
dev/eglaser-lockfree
Open

razdoburdin wants to merge 119 commits into
mainfrom
dev/eglaser-lockfree

Conversation

@razdoburdin

@razdoburdin razdoburdin commented Sep 23, 2026 •

Copy link
Copy Markdown
Collaborator

Reopening of #1027
Adds support for using newly-added separate fine-grained SVS index with minor corresponding test revisions

Includes SVS 0.5.0 rc1 binaries, which will eventually be swapped out for the official release binaries after validation.

Redisearch CI failures at test_vecsim_svs.py:711 and test_vecsim_svs.py:729 come from changes in internal logic not being reflected in tests yet.

  1. Each deletion now submit a single label consolidation job, that can change size of the reverse edges lists. This breaks the test_vecsim_svs.py:711.
  2. Some of the submitted consolidation jobs can be unfinished before GC launch. This breaks the test_vecsim_svs.py:729.

Note

High Risk
Large changes to concurrent mutation, locking, and async job lifecycles in tiered SVS/HNSW and the SVS backend; incorrect ordering could cause duplicate/missing vectors or query skew.

Overview
Upgrades SVS integration to 0.5.0rc10 prebuilts and moves the stack onto Intel’s svs::concurrent APIs: mutable Vamana indices, SegmentedBlocked storage/graph allocators, updated CMake download URLs, a higher minimum GLIBC (2.28), and feature detection via svs/concurrent/dynamic_index.h.

Standalone SVSIndex now treats the backend as lazily created (atomic impl_, CAS on first build), uses concurrent add/delete/consolidate/compact semantics, filters no_external_id from search results, and adjusts relabel/GC/load-save paths for the new library.

Tiered SVS is reworked from periodic batch flushes to per-vector async insert jobs plus per-label consolidate jobs after deletes; backend initialization snapshots ids_to_init_ training rows, publishes the built index, then drains deferred jobs. Relabel coordinates delayed/parked insert jobs and consolidate takeover. Tiered HNSW shares the same tiered plumbing (TieredInsertJob, TieredIndex_BatchIterator, shared insert-job invalidation/removal) and adds ScopedLocks / tier-specific lock hooks for size and queries.

Smaller follow-ons: element-size estimate includes reverse-edge overhead, Python runGC() calls the index directly, and tests/flow exercise GC after tiered builds.

Reviewed by Cursor Bugbot for commit 720d8df. Bugbot is set up for automated code reviews on this repo. Configure here.

ofiryanai and others added 30 commits April 20, 2026 17:42
* MOD-14916 Devirtualize distance + getElement on HNSW search hot path

MOD-14916 / LTK perf investigation.

Two virtual dispatches per HNSW candidate added between v2.10.21 and
v8.2.6 account for a measurable share of the KNN regression observed
in the LTK benchmarks (-38% throughput on Intel). Both are removed
here with the minimum possible change.

V1 - distance computation:
    Every calcDistance() call goes through IndexCalculatorInterface's
    vtable to reach DistanceCalculatorCommon, which then calls the
    underlying SIMD function pointer. The intermediate vtable hop is
    pure indirection; the concrete calculator class is fixed for the
    life of an index.

    Expose the underlying dist_func via a new pure-virtual
    getDistFunc() on IndexCalculatorInterface, implemented by
    DistanceCalculatorCommon. Cache the returned function pointer in
    VecSimIndexAbstract at construction time and call it directly in
    calcDistance(), bypassing the virtual dispatch.

V2 - vector fetch:
    HNSWIndex::getDataByInternalId and BruteForceIndex::getDataByInternalId
    call this->vectors->getElement(id), which is virtual through the
    RawDataContainer base. DataBlocksContainer is the only concrete
    implementation, and this->vectors is always a DataBlocksContainer
    (created and owned by VecSimIndexAbstract's constructor).

    Use a static_cast to DataBlocksContainer* plus a qualified call to
    DataBlocksContainer::getElement to skip the vtable lookup.

No behavior change; per-candidate distance and neighbor-fetch calls
on HNSW / brute force search paths become direct function-pointer /
direct-member calls. Headers in index_factories, hnsw_serializer,
and brute_force_factory compile cleanly.

* MOD-14916 Inline DataBlocksContainer::getElement on HNSW search hot path

Follow-up to the previous V1/V2 devirt commit. The static_cast+qualified
call in getDataByInternalId removed the vtable lookup but left the larger
cost on the table: DataBlocksContainer::getElement was still defined in
data_blocks_container.cpp, so every per-candidate neighbor fetch still
paid a real out-of-line function call and a bounds-checked blocks.at()
lookup. Without LTO the compiler could neither inline the body nor hoist
the div/mod in the HNSW hot loop.

Move the definition into the header as inline and drop the .at() bounds
check to match the v2.10.21 baseline, which used unchecked operator[] and
was fully inlined into processCandidate.

Also add a getDistFunc() override to DistanceCalculatorDummy in
test_components.cpp so BUILD_TESTS still compiles after the pure virtual
added in the previous commit.

(cherry picked from commit 4ca500a)
Remove null characters from end of file
Upstream renamed the three thread-control methods on SVSIndexBase:

  getNumThreads         -> getParallelism
  setNumThreads         -> setParallelism
  getThreadPoolCapacity -> getPoolSize

Adopt those names here ahead of merging upstream/main. This is a pure
rename -- 32 lines across 6 files, mechanically verified by reversing
the substitution and diffing against the parent commit.

Method bodies deliberately keep this branch's threadpool API
(size/resize/capacity), since VecSimSVSThreadPool here is still the
per-index owned pool. Upstream reworked it into a process-wide singleton
with thread renting, sized via VecSim_UpdateThreadPoolSize(); that is
genuine divergence to reconcile in the merge, not something a rename
should paper over.

The point is to remove this conflict class before merging. The names
collided on roughly half the affected lines without producing conflict
markers, so git resolved some toward upstream and some toward here,
yielding a tree that referenced methods it no longer declared.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Resolutions, by class:

Thread pool. Upstream replaced the per-index owned VecSimSVSThreadPool with a
process-wide singleton that rents threads, sized via VecSim_UpdateThreadPoolSize()
and defaulting to parallelism 1. Took upstream throughout: the getParallelism/
setParallelism/getPoolSize bodies in svs.h (this branch's threadpool_.capacity()
no longer exists), the constructor, and scheduleSVSIndexInit/GC, which now use
createScheduledJobs() so the pool's reserve/release accounting stays balanced.
Kept this branch's initSVSIndexWrapper as the callback -- Dmitry renamed
updateSVSIndexWrapper, and the name upstream passes is no longer defined.

Distance calculators. Upstream's DistanceDispatch supersedes this branch's
getDistFunc()/cachedDistFunc (PR #937): same vtable-avoidance goal, but
generalized to stateful calculators, which the new SQ8 DistanceCalculatorWithNorm
needs, plus asymmetric query distance. Took upstream wholesale for calculator.h,
vec_sim_index.h, and test_components.cpp; this branch had touched those files
only for the superseded caching, and no references to the old API remain.

Concurrent index. Kept this branch throughout, since that is the point of it:
SVSIndexBase::addVector (upstream dropped it; svs_tiered.h still needs it),
ready(), atomic num_marked_deleted, the SegmentedBlocked/concurrent-namespace
retargeting in svs_utils.h and svs_extensions.h, and the write-in-place
delete-then-add path. That path keeps only updateJobMutex and not upstream's
added mainIndexGuard -- the concurrent backend serializes writes against readers
itself. Upstream's executeInsertJob-adjacent conflict was a mis-alignment: the
text it offered belongs to the batch-drain function that initSVSIndex() replaces.

Carried upstream changes that would otherwise have been lost to that
restructuring: the GCJob::before_run_gc tracing hook (fired before taking
updateJobMutex, so a test callback cannot deadlock against it) and the
empty-batch guard around the backend write in initSVSIndex(), where
setParallelism(0) is not a valid request against the shared pool.

Tests. Took upstream's expectations: SVSParams.num_threads is now deprecated and
ignored with a warning, so deriving expected capacity from it is no longer valid.

deps/ScalableVectorSearch. Restored as a proper submodule gitlink at upstream's
7786d43b, discarding commit 8265a7a's symlink into a developer's home directory,
which was dangling for everyone else.

Not verified by a build: this host has GCC 11.4 and no container runtime, and SVS
needs GCC 13+. Audited statically instead -- no conflict markers, no orphaned
references to removed APIs, and every SVSIndexBase method used in svs_tiered.h is
declared in svs.h. That last check is the one the first attempt at this merge
failed: half the thread-API collisions produced no conflict markers, so git
resolved some lines toward each side and left the tree calling methods it no
longer declared.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

Comment thread src/VecSim/algorithms/svs/svs.h
Comment thread src/VecSim/algorithms/svs/svs_batch_iterator.h Outdated

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

Comment thread src/VecSim/algorithms/svs/svs_tiered.h
Comment thread src/VecSim/algorithms/svs/svs_tiered.h

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

Comment thread src/VecSim/algorithms/svs/svs.h
Comment thread src/VecSim/algorithms/svs/svs_tiered.h

@alonre24 alonre24 left a comment

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.

Thanks for 315a89b. Queries and deletes no longer wait on the initial build, and the bookkeeping for deletes and overwrites during the build looks right to me.

However, the main thread still blocks for the whole build on HSET with RediSearch's default config. Details inline. In short: initSVSIndexWrapper still holds updateJobMutex exclusively for the build, and every addVector during the build takes the direct-to-backend path, which waits on that lock.

The fix looks small: keep buffering while init runs, as main did. On main, async addVector always went to flat and only scheduled an update; flatBufferLimit only capped updateTriggerThreshold. So letting flat grow past the limit during the build is no regression vs. main.

Could you also add the test from my earlier comment (item 4)? From UpdateJob::before_add_to_svs, call addVector with flat at or above flatBufferLimit (the default case) and assert it returns within a timeout. It would have caught this.

Comment thread src/VecSim/algorithms/svs/svs_tiered.h Outdated
this->swaps_journal.emplace_back(SKIP_LABEL, id, id);

this->flatIndexGuard.lock_shared();
if (this->frontendIndex->indexSize() >= flat_buffer_bound) {

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.

This is where the main thread still blocks during the initial build.

While initInProgress is set, tryBufferForTraining returns nullopt, so we get here with the backend not ready yet. At that point flat holds the whole training batch:

  • RediSearch passes flatBufferLimit = tieredVecSimIndexBufferLimit, which defaults to DEFAULT_BLOCK_SIZE (1024).
  • The training threshold defaults to 10240 for compressed indexes. Uncompressed indexes use 1024, and more vectors keep arriving before a worker picks up the init job.

So indexSize() >= flat_buffer_bound is true, and the std::shared_lock(updateJobMutex) below waits for initSVSIndexWrapper to release its exclusive lock, i.e. for the entire build.

Suggestion: restore the ready() check that was dropped here earlier. It's needed again now that tryBufferForTraining can return nullopt before the backend is ready:

if (svs_index->ready() && this->frontendIndex->indexSize() >= flat_buffer_bound) {

Writes during init then go to the buffered insert-job path. Those jobs wait on updateJobMutex on a worker, not on the main thread. Flat grows past the limit only for the duration of the build, the same as on main, where async mode never had a hard cap.

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.

done. I added the related test as well

const bool in_backend = svs_index->isLabelExists(old_label);
if (!in_flat && !in_backend) {
return VecSimRelabel_OldLabelMissing;
std::shared_lock<std::shared_mutex> lock(this->updateJobMutex);

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.

Same issue for relabel: it takes updateJobMutex shared, so a relabelVector on the main thread during the initial build waits for the whole build. Is that acceptable, or should relabel also avoid waiting on init?

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.

I think that is acceptable.

Comment thread src/VecSim/algorithms/svs/svs_tiered.h Outdated
main_shared_lock.unlock();
std::lock_guard lock(this->mainIndexGuard);
svs_index->setParallelism(1);
svs_index->setImpl(std::move(impl));

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.

Minor: setImpl now publishes the backend before the final exclusive flat section runs. Until that section drops the copied rows from flat, they are in both tiers:

  • a query can see the same single-value label from both tiers;
  • deleteVector on such a label removes it from flat and from the backend, and returns 2 for a single-value index. The final section's deleteVectors then finds nothing.

The window is short, but moving setImpl into the final std::lock_guard flat_lock section closes it at no real cost, since publishing is just a CAS.

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.

good catch.
actually, moving setImpl wasn't enough. In this case there was a short window there deleted label was still visible in the backend.
I added a test for this case as well.

if (this->backendIndex->indexSize() == 0) {
// If backend index is empty, we need to initialize it first.
// Keep tracking surviving snapshot IDs; new arrivals use insert jobs.
initInProgress = true;

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.

Nit: initInProgress (and initDeletedLabels.reserve() above) are written under a shared flatIndexGuard. That's safe today, because every reader of these fields holds the lock exclusively and only one init runs at a time, but it's easy to break later. Please either take the lock exclusively for these writes, make initInProgress atomic, or add a comment documenting the invariant.

@razdoburdin razdoburdin Oct 15, 2026 •

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.

A comment is added

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 9ce1c6d. Configure here.

Comment thread src/VecSim/algorithms/svs/svs.h

@alonre24 alonre24 left a comment

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.

Thanks, the init fix looks good: the main thread no longer waits on the build, setImpl is now published only after the writes made during the build are applied, and the new tests cover both. Two follow-ups inline. The first one is about worker threads getting tied up during the build.

Comment thread src/VecSim/algorithms/svs/svs_tiered.h Outdated
}

// Insert job to the queue and signal the workers' updater.
this->submitSingleJob(new_insert_job);

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.

Insert jobs submitted during the initial build tie up workers.

While initInProgress is set, this submits the insert job right away. executeInsertJobWrapper takes updateJobMutex shared, and initSVSIndexWrapper holds it exclusively for the whole build. So every idle worker that picks up one of these jobs blocks until the build ends.

RediSearch submits index jobs with THPOOL_PRIORITY_LOW, but priority only orders the queue; a worker that has already picked up a job stays stuck. With steady HSETs during a build that can take tens of seconds, every worker the build itself isn't using soon sits blocked on one of these jobs. Queries, which run on the same pool, then wait for the build to end. On main, writes during an update only buffered and created no jobs, so this is a regression there.

Suggestion: while initInProgress is set, still create the job and register it in labelToInsertJobs as now, but don't submit it yet. Add it to a list kept under flatIndexGuard (like initDeletedLabels). Then have initSVSIndexWrapper submit the list after it releases updateJobMutex, the same way it schedules the consolidate jobs for the deleted labels (and the way relabel resubmits parked jobs).

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.

done

// with the given label.
if (initInProgress && !this->labelToInsertJobs.count(label)) {
// A captured row has no insert job; cancel it through the journal.
ret = -removeLabelFromFlat(label);

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.

Could you add a test for this branch? It's for overwriting a label that was copied into the build and has no insert job yet. The new tests cover additions and deletes during the build, but not overwrites. I traced it by hand and it looks correct, but it's the trickiest part of the bookkeeping. Something like: pause at UpdateJob::after_snapshot, overwrite a copied label (single-value), then check that addVector returns 0 and that after init the label has exactly the new vector.

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.

done

@ethanglaser ethanglaser mentioned this pull request Oct 16, 2026
2 tasks
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.

8 participants