feat: Make ClusterIndex thread-safe for concurrent lookup#787
Open
xiaoxmeng wants to merge 1 commit into
Open
Conversation
Summary: Make `ClusterIndex::lookup()` and `keyAtRow()` safe for concurrent use from multiple threads. Previously crashed with SIGSEGV when called from 20+ benchmark threads. Three categories of shared mutable state are fixed: **1. Per-call scratch buffers to locals**: Remove `partitionLookups_` and `scratchRanges_` members. `lookup()` creates local vectors and passes them by reference to helper methods. Zero synchronization overhead. **2. `folly::SharedMutex` for partition loading**: `loadPartition()` uses `std::shared_lock` fast path + `std::unique_lock` with double-checked locking. `loadPartitions()` (batched preload) takes a write lock. **3. Per-partition `folly::SharedMutex` for chunk access**: `getDecodedChunk()` uses shared lock for cache hits, exclusive lock only for the slot install (not the IO/decode). Decode happens outside the lock to avoid blocking concurrent readers. **4. Stateless `seek()` and `get()` for PrefixEncoding and TrivialEncoding**: `seek()` and `get()` no longer mutate member state, enabling concurrent calls on the same encoding without locks. PrefixEncoding uses local state via static `decodeEntryAt()` helper. TrivialEncoding lazily caches string positions via `std::call_once`. Benchmark results (index_projector, 60s, `--pin_index=true`, single shard): **Without preload (`--preload_index=false`)**: | Threads | Batches | Throughput | Avg CPU/batch | Peak Mem | |---------|---------|------------|---------------|----------| | 1 | 644 | 10.50 GB | 58.92ms | 204 MB | | 2 | 2,185 | 35.29 GB | 54.42ms | 224 MB | | 4 | 4,422 | 71.48 GB | 53.78ms | 264 MB | | 8 | 8,719 | 140.75 GB | 54.50ms | 332 MB | | 16 | 16,590 | 267.69 GB | 56.96ms | 467 MB | | 32 | 25,175 | 406.29 GB | 73.11ms | 723 MB | **With preload (`--preload_index=true`)**: | Threads | Batches | Throughput | Avg CPU/batch | Peak Mem | |---------|---------|------------|---------------|----------| | 1 | 1,097 | 17.79 GB | 54.10ms | 195 MB | | 2 | 1,719 | 27.79 GB | 57.33ms | 212 MB | | 4 | 4,393 | 71.01 GB | 54.12ms | 253 MB | | 8 | 8,745 | 141.43 GB | 54.29ms | 320 MB | | 16 | 16,734 | 270.05 GB | 56.47ms | 453 MB | | 32 | 25,555 | 412.36 GB | 72.48ms | 699 MB | Near-linear scaling up to 16 threads in both modes. Differential Revision: D106447554
|
@xiaoxmeng has exported this pull request. If you are a Meta employee, you can view the originating Diff in D106447554. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
Make
ClusterIndex::lookup()andkeyAtRow()safe for concurrent use from multiple threads. Previously crashed with SIGSEGV when called from 20+ benchmark threads.Three categories of shared mutable state are fixed:
1. Per-call scratch buffers to locals: Remove
partitionLookups_andscratchRanges_members.lookup()creates local vectors and passes them by reference to helper methods. Zero synchronization overhead.2.
folly::SharedMutexfor partition loading:loadPartition()usesstd::shared_lockfast path +std::unique_lockwith double-checked locking.loadPartitions()(batched preload) takes a write lock.3. Per-partition
folly::SharedMutexfor chunk access:getDecodedChunk()uses shared lock for cache hits, exclusive lock only for the slot install (not the IO/decode). Decode happens outside the lock to avoid blocking concurrent readers.4. Stateless
seek()andget()for PrefixEncoding and TrivialEncoding:seek()andget()no longer mutate member state, enabling concurrent calls on the same encoding without locks. PrefixEncoding uses local state via staticdecodeEntryAt()helper. TrivialEncoding lazily caches string positions viastd::call_once.Benchmark results (index_projector, 60s,
--pin_index=true, single shard):Without preload (
--preload_index=false):With preload (
--preload_index=true):Near-linear scaling up to 16 threads in both modes.
Differential Revision: D106447554