Skip to content

refactor(engine): replace ParallelDeltaApplier outer Mutex with DashMap (BR-3j.c+e) - #4634

Merged
oferchen merged 1 commit into
masterfrom
refactor/engine-parallel-apply-dashmap-br3jc
May 21, 2026
Merged

oferchen merged 1 commit into
masterfrom
refactor/engine-parallel-apply-dashmap-br3jc

Conversation

@oferchen

Copy link
Copy Markdown
Owner

Summary

Lock-window discipline

Each call site is annotated with a short comment describing how it drops the shard guard before doing any non-trivial work:

  • register_file pre-builds the slot Arc OUTSIDE the entry guard so the reorder-buffer allocation never widens the lock window. Uses Entry::Vacant/Entry::Occupied for the atomic check-then-insert.
  • slot_for clones the per-file Arc while the shard read guard is alive, then drops the guard at the end of the expression. Callers never see the DashMap guard so they cannot accidentally hold it across a per-file mutex lock or a rayon dispatch.
  • apply_chunk_parallel documents that slot_for returns an Arc clone with no live guard so the subsequent rayon::join never blocks shard-mates on unrelated NDX values.
  • finish_file uses DashMap::remove, which returns owned (K, V) and drops the shard guard before the Arc::try_unwrap step.
  • Debug reads self.files.len() directly.

Stress test results

crates/engine/tests/parallel_apply_concurrent.rs runs under --features parallel-receive-delta:

  • concurrent_files_under_dashmap_shards_match_expected_bytes: 8 rayon workers, 1000 files, 10000 ops, 16 bytes/chunk. Each worker picks a random FileNdx, takes a per-file seq via an AtomicU64, dispatches apply_chunk_parallel, then the test asserts bytes_written matches the expected per-file sum AND every file finalises cleanly via finish_file. Dev-box measurement: elapsed ~4.6 ms, ~2.17M ops/sec.
  • concurrent_register_and_dispatch_on_overlapping_files: races register_file against apply_chunk_parallel on a 256-file pool with 4000 ops. Asserts the applier only ever returns typed unknown / still buffered errors under the race - never panics or corrupts.

Constraints satisfied

  • No unsafe code added (engine crate's existing #[allow(unsafe_code)] sites stay as-is).
  • No #[allow(...)] clippy waivers.
  • Public API of ParallelDeltaApplier unchanged; internal-only refactor.
  • cargo build -p engine --no-default-features still compiles.

Test plan

  • cargo fmt --all
  • cargo clippy --workspace --all-targets --all-features --no-deps -- -D warnings clean
  • cargo nextest run -p engine --features parallel-receive-delta -E 'test(concurrent_delta) or binary(parallel_apply_concurrent) or binary(arc_drain_panic_recovery)' -> 340 passed
  • cargo build -p engine --no-default-features passes
  • cargo bench -p engine --features parallel-receive-delta --bench parallel_receive_delta_perf --no-run compiles
  • CI: fmt+clippy, nextest (stable), Windows, macOS, Linux musl

oferchen added a commit that referenced this pull request May 21, 2026
…-3i.c) (#4640)

Replace the length-only verify stub with a real strong-checksum
dispatch through the negotiated `ChecksumStrategy` plumbed in BR-3i.b
(#4616). Chunks may opt into per-chunk verification by attaching an
`expected_strong: ChecksumDigest` through the new `with_expected_strong`
builder; the applier computes the digest of `chunk.data` on the rayon
worker and compares byte-for-byte before the per-file write happens.

Mismatches surface as a typed `ParallelApplyError::ChecksumMismatch`
carrying ndx, sequence, algorithm, and both hex digests, then convert
into `io::Error` at the public boundary so the receiver can fall back
to a phase-2 redo or abort the file without committing corrupt bytes.
Chunks with no expected digest still compute the digest (keeping the
parallel-verify CPU cost stable across both shapes) but skip the
comparison, preserving backward compatibility for existing call sites.

The wire format is unchanged - `DeltaChunk` is purely a receiver-side
staging structure and the expected digest is held in-memory only.

This closes the GA-blocking gap on `parallel-receive-delta` that was
called out in the README feature table; the only remaining GA gap is
the outer `Mutex<HashMap<FileNdx, _>>` (BR-3j.a-f, in flight on
#4634).

Adds five regression tests, including BR-3i.e parity:
- happy-path verification for MD5 and XXH3
- single-chunk mismatch surfaces typed error and skips the writer
- batch mismatch short-circuits with typed error
- parallel apply with real digests is byte-identical to sequential
- ChecksumMismatch -> io::Error conversion carries the typed message
BR-3j.a (PR #4600) selected DashMap to replace the outer
`Mutex<HashMap<FileNdx, Arc<Mutex<FileSlot>>>>` map in
`ParallelDeltaApplier`. The outer mutex serialised every per-chunk slot
lookup behind a single global lock, defeating the per-file fanout the
rest of the applier was built around (#2503, BR-3j.c).

This commit performs the source migration:

- `files: Mutex<HashMap<...>>` -> `files: DashMap<FileNdx, Arc<Mutex<FileSlot>>>`
- `register_file` uses `Entry::Vacant`/`Entry::Occupied` and pre-builds
  the slot Arc outside the shard guard so the reorder-buffer allocation
  never widens the lock window.
- `slot_for` clones the Arc and drops the shard read guard at the end
  of the expression; callers never see the DashMap guard, so they
  cannot accidentally hold it across the per-file mutex lock or a
  rayon dispatch.
- `finish_file` uses `DashMap::remove` which returns the owned `(K, V)`
  and drops the shard guard before `Arc::try_unwrap`.
- `Debug` reads `self.files.len()` directly.

The inner `Mutex<FileSlot>` is preserved: it remains the sole gate for
per-file writes so submission order is still enforced through the
`ReorderBuffer`. Public API of `ParallelDeltaApplier` is unchanged.

Adds a concurrency stress test
(`crates/engine/tests/parallel_apply_concurrent.rs`, BR-3j.e #2507)
that spawns N rayon workers each picking a random FileNdx from a pool
of 1000 files and verifies bytes_written matches expected. A second
test races registration and dispatch on overlapping files and asserts
the applier surfaces only typed errors (never panics or corrupts).
On the dev box the stress test reports ~2.17M ops/sec (8 workers,
1000 files, 10000 ops).

No unsafe code added. No clippy waivers. `cargo build -p engine
--no-default-features` still compiles. Targeted nextest runs for
`concurrent_delta` and `parallel_apply` binaries all pass (340 tests
including the 2 new stress tests).
@oferchen
oferchen force-pushed the refactor/engine-parallel-apply-dashmap-br3jc branch from 2bd6e2b to d6c1f85 Compare May 21, 2026 05:30
@oferchen
oferchen merged commit 49bc0aa into master May 21, 2026
54 of 55 checks passed
@oferchen
oferchen deleted the refactor/engine-parallel-apply-dashmap-br3jc branch May 21, 2026 05:51
oferchen added a commit that referenced this pull request Aug 24, 2026
…-3i.c) (#4640)

Replace the length-only verify stub with a real strong-checksum
dispatch through the negotiated `ChecksumStrategy` plumbed in BR-3i.b
(#4616). Chunks may opt into per-chunk verification by attaching an
`expected_strong: ChecksumDigest` through the new `with_expected_strong`
builder; the applier computes the digest of `chunk.data` on the rayon
worker and compares byte-for-byte before the per-file write happens.

Mismatches surface as a typed `ParallelApplyError::ChecksumMismatch`
carrying ndx, sequence, algorithm, and both hex digests, then convert
into `io::Error` at the public boundary so the receiver can fall back
to a phase-2 redo or abort the file without committing corrupt bytes.
Chunks with no expected digest still compute the digest (keeping the
parallel-verify CPU cost stable across both shapes) but skip the
comparison, preserving backward compatibility for existing call sites.

The wire format is unchanged - `DeltaChunk` is purely a receiver-side
staging structure and the expected digest is held in-memory only.

This closes the GA-blocking gap on `parallel-receive-delta` that was
called out in the README feature table; the only remaining GA gap is
the outer `Mutex<HashMap<FileNdx, _>>` (BR-3j.a-f, in flight on
#4634).

Adds five regression tests, including BR-3i.e parity:
- happy-path verification for MD5 and XXH3
- single-chunk mismatch surfaces typed error and skips the writer
- batch mismatch short-circuits with typed error
- parallel apply with real digests is byte-identical to sequential
- ChecksumMismatch -> io::Error conversion carries the typed message
oferchen added a commit that referenced this pull request Aug 24, 2026
…ap (#4634)

BR-3j.a (PR #4600) selected DashMap to replace the outer
`Mutex<HashMap<FileNdx, Arc<Mutex<FileSlot>>>>` map in
`ParallelDeltaApplier`. The outer mutex serialised every per-chunk slot
lookup behind a single global lock, defeating the per-file fanout the
rest of the applier was built around (#2503, BR-3j.c).

This commit performs the source migration:

- `files: Mutex<HashMap<...>>` -> `files: DashMap<FileNdx, Arc<Mutex<FileSlot>>>`
- `register_file` uses `Entry::Vacant`/`Entry::Occupied` and pre-builds
  the slot Arc outside the shard guard so the reorder-buffer allocation
  never widens the lock window.
- `slot_for` clones the Arc and drops the shard read guard at the end
  of the expression; callers never see the DashMap guard, so they
  cannot accidentally hold it across the per-file mutex lock or a
  rayon dispatch.
- `finish_file` uses `DashMap::remove` which returns the owned `(K, V)`
  and drops the shard guard before `Arc::try_unwrap`.
- `Debug` reads `self.files.len()` directly.

The inner `Mutex<FileSlot>` is preserved: it remains the sole gate for
per-file writes so submission order is still enforced through the
`ReorderBuffer`. Public API of `ParallelDeltaApplier` is unchanged.

Adds a concurrency stress test
(`crates/engine/tests/parallel_apply_concurrent.rs`, BR-3j.e #2507)
that spawns N rayon workers each picking a random FileNdx from a pool
of 1000 files and verifies bytes_written matches expected. A second
test races registration and dispatch on overlapping files and asserts
the applier surfaces only typed errors (never panics or corrupts).
On the dev box the stress test reports ~2.17M ops/sec (8 workers,
1000 files, 10000 ops).

No unsafe code added. No clippy waivers. `cargo build -p engine
--no-default-features` still compiles. Targeted nextest runs for
`concurrent_delta` and `parallel_apply` binaries all pass (340 tests
including the 2 new stress tests).
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