Skip to main content

Module worker_pool

Module worker_pool 

Source
Expand description

Fixed-resident worker pool for the storage layer’s parallel walks.

Storage parallel-walk mechanisms (the registry seek walk, the domain-scan parallel walks, and any later sibling) need worker threads for the duration of one call. Spawning them on demand with std::thread::scope looks free, but scoped children inherit the creating thread’s CPU affinity mask. Under the serve-core affinity setup (WARATAH_SERVE_CORES, waratah_bin/src/affinity.rs) the calling thread is a tokio runtime worker pinned to one core, so every scoped walker lands on that same core and time-slices it — measured on a real standalone server: a registry resolve stage of 189.5 ms with K=1 and 190.0 ms with K=4 pinned, versus 189.0 → 75.5 ms (2.5×) with the serve core set off.

This pool fixes that by creating its threads once per process and giving each a core placement that never changes:

  • each pool thread is assigned a serving core round-robin (worker slot i → cores[i % cores.len()]) at pool creation and self-pins as its first statement (sched_setaffinity on its own thread id), then never migrates. A worker that panics is retired and replaced with a fresh thread on the same core;
  • concurrency is bounded by the pool width (the queue is the backpressure), so callers do not need a separate permit mechanism;
  • work units own their inputs and return owned outputs. The pool outlives individual calls, so units may not borrow caller state; this is sound by construction and the per-unit data copy is small against the I/O each unit performs;
  • every worker queue is FIFO with no priority lanes. Units are bounded at construction (the registry walk’s 16-group MIN rule, the domain walks’ per-constraint slices), which bounds the time a small unit waits behind a big one.

Dispatch assigns unit j of one call to slot (counter.fetch_add(units) + j) % width, so concurrent bursts interleave their round-robin windows across the pool instead of stacking on the same workers. A unit that panics is caught per unit, reported as UnitOutcome::Panicked, and that one worker is replaced; every other unit and thread is unaffected.

§Initialization and the scoped fallback

The pool is installed once per process with init_worker_pool (explicit core placement, used by waratah_bin) or init_worker_pool_from_env (parses WARATAH_SERVE_CORES; used by the waratah-partition and waratah-registry child processes). Both calls are idempotent. WARATAH_STORAGE_POOL_THREADS overrides the width over either default.

A process that never initializes the pool — unit tests, batch tools such as waratah import --offline, any unforeseen embedding — falls back to the previous std::thread::scope spawn in run_units. The fallback keeps those callers working without setup; it does not get the affinity fix, and effective_width reports no width limit for it.

§Topology drift

Placement is derived from the startup topology and never re-derived. Revisit this (add a TTL, re-derive the serve set, or re-pin dynamically) if Waratah ever runs under containerized dynamic cpusets or CPU hotplug: a worker stranded on a core the kernel no longer schedules would silently serialize every parallel walk, exactly the failure the serve-core affinity produced for scoped walkers.

§Panic policy

Each unit runs under catch_unwind inside its worker. A panic does not unwind into the caller (the old thread::scope behavior) and does not poison the pool: the outcome carries the payload text, and the worker is retired and replaced because a panicked thread’s state is unknown. A later dispatch runs normally. dispatch also guarantees one outcome per unit even if the report glue around the unit unwinds — the job body catches that too and delivers a synthetic Panicked outcome, because a missing report would block the dispatcher on its receive forever.

Enums§

UnitOutcome
What became of one dispatched unit.

Constants§

POOL_THREADS_ENV
Pool-width override (WARATAH_STORAGE_POOL_THREADS); unset / invalid keeps the caller’s default.
SERVE_CORES_ENV
Serve-core environment variable (WARATAH_SERVE_CORES), shared by the process launcher and the storage children.

Traits§

PoolUnit
One unit of work executed by the storage worker pool.

Functions§

default_walk_width
Slice count for a parallel walk whose caller did not request an explicit worker count: the installed pool’s width, or available_parallelism() when no pool exists. Serving processes install their pool at startup — before any walk can resolve its execution mode — so the value is always the installed width there (including a POOL_THREADS_ENV override). The no-pool branch covers unit tests, batch tools such as waratah import --offline, and embeddings: their run_units fallback spawns one scoped thread per slice, so the machine’s parallelism is the natural width. effective_width cannot be used for this: its no-pool usize::MAX is a “no width limit” report, not a slice count.
effective_width
Width a parallel walk may use: the installed pool’s width, or usize::MAX when no pool exists (the scoped fallback spawns one thread per unit, as before).
init_worker_pool
Install the process-wide pool with explicit core placement.
init_worker_pool_from_env
Install the process-wide pool from WARATAH_SERVE_CORES (the value the launcher writes into each storage child’s environment).
run_units
Run every unit to completion, returning one outcome per unit in unit order.