Skip to content

[Store] parallelize per-bucket reads in BatchLoad via worker pool#3

Merged
pingzhuu merged 1 commit into
fix/uring-merge-alignmentfrom
opt/multi-threads-read
Jul 21, 2026
Merged

[Store] parallelize per-bucket reads in BatchLoad via worker pool#3
pingzhuu merged 1 commit into
fix/uring-merge-alignmentfrom
opt/multi-threads-read

Conversation

@leaves-zwx

Copy link
Copy Markdown

BatchLoad previously iterated buckets sequentially, issuing one set of preads per bucket before moving to the next. Each bucket is a separate file with no shared read state, so this serialization was purely artificial; on a single NVMe device with default 256 KiB bucket gaps it limited read throughput to ~1.4 GB/s while the device is capable of ~4-5 GB/s.

Fix: mirror the WriteBucket parallelization pattern from commit e738cfd. Capture the per-bucket body as a lambda returning std::optional (nullopt on success, error code on unrecoverable failure) and dispatch the buckets across a bounded set of worker threads. Workers pull bucket indices from a shared atomic counter and stop as soon as one bucket returns a fatal error, so the caller joins all workers before returning. The serial path is retained when offload_read_threads<=1 or there is only one bucket, preserving the legacy behavior with no thread overhead.

Thread safety:

  • bucket_read_plans is snapshotted into a vector of (bid, plans*) pairs before workers start; the unordered_map is never touched concurrently.
  • Each bucket opens its own fd via OpenFile; StorageFile instances are not shared across worker threads.
  • batch_object.at(key).ptr writes target disjoint keys (a key belongs to exactly one bucket), so different workers mutate distinct map values.
  • VLOG is thread-safe via glog.

Env var (added): MOONCAKE_OFFLOAD_READ_THREADS (default 4). Set to 1 to fall back to the legacy serial loop. Mirrors MOONCAKE_OFFLOAD_WRITE_THREADS.

Also adds a batchload_dispatch VLOG so the parallel/serial decision and effective thread count are visible in stage-4 diagnosis logs.

BatchLoad previously iterated buckets sequentially, issuing one set of
preads per bucket before moving to the next. Each bucket is a separate
file with no shared read state, so this serialization was purely
artificial; on a single NVMe device with default 256 KiB bucket gaps it
limited read throughput to ~1.4 GB/s while the device is capable of
~4-5 GB/s.

Fix: mirror the WriteBucket parallelization pattern from commit e738cfd.
Capture the per-bucket body as a lambda returning std::optional<ErrorCode>
(nullopt on success, error code on unrecoverable failure) and dispatch
the buckets across a bounded set of worker threads. Workers pull bucket
indices from a shared atomic counter and stop as soon as one bucket
returns a fatal error, so the caller joins all workers before returning.
The serial path is retained when offload_read_threads<=1 or there is
only one bucket, preserving the legacy behavior with no thread overhead.

Thread safety:
  - bucket_read_plans is snapshotted into a vector of (bid, plans*) pairs
    before workers start; the unordered_map is never touched concurrently.
  - Each bucket opens its own fd via OpenFile; StorageFile instances are
    not shared across worker threads.
  - batch_object.at(key).ptr writes target disjoint keys (a key belongs
    to exactly one bucket), so different workers mutate distinct map
    values.
  - VLOG is thread-safe via glog.

Env var (added): MOONCAKE_OFFLOAD_READ_THREADS (default 4). Set to 1 to
fall back to the legacy serial loop. Mirrors MOONCAKE_OFFLOAD_WRITE_THREADS.

Also adds a batchload_dispatch VLOG so the parallel/serial decision and
effective thread count are visible in stage-4 diagnosis logs.
@leaves-zwx
leaves-zwx force-pushed the opt/multi-threads-read branch from ece2ea0 to 6e789ba Compare July 19, 2026 19:57
@leaves-zwx
leaves-zwx changed the base branch from opt/batchload-pread-merge to fix/uring-merge-alignment July 19, 2026 20:03
@pingzhuu
pingzhuu merged commit 4b4b741 into fix/uring-merge-alignment Jul 21, 2026
@pingzhuu
pingzhuu deleted the opt/multi-threads-read branch July 21, 2026 10:32
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.

2 participants