Skip to content

MOD-15579 Lazy initialization of the shared SVS thread pool#969

Open
meiravgri wants to merge 2 commits into
meiravg_svs_thpool_alloactorfrom
meiravg_svs_thpool_lazy
Open

MOD-15579 Lazy initialization of the shared SVS thread pool#969
meiravgri wants to merge 2 commits into
meiravg_svs_thpool_alloactorfrom
meiravg_svs_thpool_lazy

Conversation

@meiravgri
Copy link
Copy Markdown
Collaborator

@meiravgri meiravgri commented May 14, 2026

Make initialization of the shared SVS thread pool lazy: OS worker threads are spawned only when the first SVS index is created, not when VecSim_UpdateThreadPoolSize is called. This eliminates the cost of pre-allocated SVS workers in deployments that configure RediSearch workers but never create an SVS index (today, every Redis with WORKERS=N configured spawns N-1 SVS worker threads at module init regardless of whether SVS is ever used).

The change extends the existing deferred-resize protocol to cover a second "safe point". VecSimSVSThreadPoolImpl::resize() now defers application in two cases:

  • No SVS index attached yet → recorded; applied on first onIndexAttached() (lazy thread spawn).
  • Shrink while jobs are in flight (pending_jobs_ > 0) → recorded; applied when endScheduledJob() drops the counter to 0 (existing behaviour).
  • Otherwise → applied immediately via resize_locked().

The two deferral cases share deferred_size_ and never overlap (no jobs can be in flight before the first index attaches). One new gate field, has_attached_index_, distinguishes the two states. Set on the first onIndexAttached() call from the per-index VecSimSVSThreadPool ctor.

A test-only resetForTest() hook (under BUILD_TESTS) restores the singleton to a clean baseline by releasing the slots vector capacity and clearing the new gate; intended for unit tests that need to observe lazy semantics in a process where prior tests may have already attached indexes.

VecSim_UpdateThreadPoolSize continues to set the write mode (InPlace/Async) eagerly — only the SVS pool sizing is now deferred. The C API surface is unchanged.

RediSearch integration impact: none required — VecSim_UpdateThreadPoolSize(N) is still the single entry point for both module init and CONFIG SET search-workers M. Pre-index calls now just record the size; post-index calls behave exactly as before.

Main objects this PR modified

  1. VecSimSVSThreadPoolImpl::resize() — now lazy until first index attaches.
  2. VecSimSVSThreadPoolImpl::onIndexAttached() — new entry point called from per-index VecSimSVSThreadPool ctor; flips has_attached_index_ and applies any deferred size.
  3. VecSimSVSThreadPoolImpl::has_attached_index_ — new member field gating spawn vs. defer.
  4. VecSimSVSThreadPoolImpl::resetForTest() — new BUILD_TESTS-only hook.
  5. VecSim_UpdateThreadPoolSize() — comment updated; behaviour now lazy via the new resize() semantics.

Tests

  • SVSTest.ThreadPoolLazyInit (new) — verifies VecSim_UpdateThreadPoolSize does not allocate worker threads before any SVS index exists, and that the first SVS index creation triggers the deferred spawn at the previously requested size.
  • SVSThreadPoolTest.UpdateThreadPoolSizeModeTransitions — rewritten to validate the lazy → eager transition (size stays at 1 until an index attaches; subsequent transitions are immediate).
  • SVSThreadPoolTest fixture — SetUp() calls onIndexAttached() so the existing pool-isolation tests retain eager resize() semantics.
  • SVSTest.NumThreadsParamIgnored — calls onIndexAttached() upfront for the same reason.
  • SVSTest.debugInfoGlobalMemoryEqualsSharedSVSThreadPoolMemoryASSERT_GT on global memory moved to after CreateNewIndex (the lazy spawn fires there).

Mark if applicable

  • This PR introduces API changes
  • This PR introduces serialization changes

Note

Medium Risk
Changes shared SVS thread-pool resizing semantics and introduces a new lazy-init/deferral state, which can affect concurrency and job scheduling behavior if edge cases were missed. Unit tests were updated/added, but threading/resource-management changes still carry moderate risk.

Overview
Defers creation of SVS shared pool worker threads until the first SVS index attaches: VecSimSVSThreadPoolImpl::resize() now records requested sizes pre-index and applies them in onIndexAttached(), while keeping the existing defer shrink while jobs are in flight behavior.

Adds a BUILD_TESTS-only resetForTest() to clear singleton state, updates VecSim_UpdateThreadPoolSize() documentation to reflect lazy spawning, and adjusts/extends unit tests to validate the lazy→eager transition and global-memory reporting (including a new ThreadPoolLazyInit test).

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

@meiravgri meiravgri changed the title Meiravg_svs_thpool_lazy MOD-15579 Lazy initialization of the shared SVS thread pool May 14, 2026
@jit-ci
Copy link
Copy Markdown

jit-ci Bot commented May 14, 2026

🛡️ Jit Security Scan Results

CRITICAL HIGH MEDIUM

✅ No security findings were detected in this PR


Security scan by Jit

@codecov
Copy link
Copy Markdown

codecov Bot commented May 14, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.93%. Comparing base (31c0e0a) to head (7bc3e24).

Additional details and impacted files
@@                       Coverage Diff                        @@
##           meiravg_svs_thpool_alloactor     #969      +/-   ##
================================================================
- Coverage                         96.97%   96.93%   -0.04%     
================================================================
  Files                               130      130              
  Lines                              7829     7801      -28     
================================================================
- Hits                               7592     7562      -30     
- Misses                              237      239       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@meiravgri meiravgri requested a review from dor-forer May 14, 2026 08:34
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.

1 participant