Skip to content

feat: Make ClusterIndex thread-safe for concurrent lookup#787

Open
xiaoxmeng wants to merge 1 commit into
facebookincubator:mainfrom
xiaoxmeng:export-D106447554
Open

feat: Make ClusterIndex thread-safe for concurrent lookup#787
xiaoxmeng wants to merge 1 commit into
facebookincubator:mainfrom
xiaoxmeng:export-D106447554

Conversation

@xiaoxmeng
Copy link
Copy Markdown
Contributor

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

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
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label May 27, 2026
@meta-codesync
Copy link
Copy Markdown

meta-codesync Bot commented May 27, 2026

@xiaoxmeng has exported this pull request. If you are a Meta employee, you can view the originating Diff in D106447554.

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

Labels

CLA Signed This label is managed by the Meta Open Source bot. fb-exported meta-exported

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant