Repository navigation
refactor(engine): replace ParallelDeltaApplier outer Mutex with DashMap (BR-3j.c+e) - #4634
Merged
Merged
Conversation
4 tasks
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
force-pushed
the
refactor/engine-parallel-apply-dashmap-br3jc
branch
from
May 21, 2026 05:30
2bd6e2b to
d6c1f85
Compare
This was referenced May 21, 2026
Merged
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).
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
Mutex<HashMap<FileNdx, Arc<Mutex<FileSlot>>>>inParallelDeltaApplierwith aDashMap<FileNdx, Arc<Mutex<FileSlot>>>per the BR-3j.a audit decision (docs/audits/br-3j-a-dashmap-vs-sharded-2026-05-20.md, PR docs(audits): pick DashMap or sharded for ParallelDeltaApplier files (BR-3j.a) #4600). The single outer mutex serialised every per-chunk slot lookup behind one global lock, defeating the per-file fanout the applier is built around (feat(core): complete server invocation builder with all flag forwarding #2503, BR-3j.c).Mutex<FileSlot>is preserved: it remains the sole gate for per-file writes, so submission order is still enforced through the per-fileReorderBuffer. Public API ofParallelDeltaApplieris unchanged.crates/engine/tests/parallel_apply_concurrent.rs, BR-3j.e fix(protocol): separate preserve_devices and preserve_specials flags #2507) covering 1000 concurrent open files with random dispatch.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_filepre-builds the slot Arc OUTSIDE the entry guard so the reorder-buffer allocation never widens the lock window. UsesEntry::Vacant/Entry::Occupiedfor the atomic check-then-insert.slot_forclones 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_paralleldocuments thatslot_forreturns an Arc clone with no live guard so the subsequentrayon::joinnever blocks shard-mates on unrelated NDX values.finish_fileusesDashMap::remove, which returns owned(K, V)and drops the shard guard before theArc::try_unwrapstep.Debugreadsself.files.len()directly.Stress test results
crates/engine/tests/parallel_apply_concurrent.rsruns 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 randomFileNdx, takes a per-file seq via anAtomicU64, dispatchesapply_chunk_parallel, then the test assertsbytes_writtenmatches the expected per-file sum AND every file finalises cleanly viafinish_file. Dev-box measurement: elapsed ~4.6 ms, ~2.17M ops/sec.concurrent_register_and_dispatch_on_overlapping_files: racesregister_fileagainstapply_chunk_parallelon a 256-file pool with 4000 ops. Asserts the applier only ever returns typedunknown/still bufferederrors under the race - never panics or corrupts.Constraints satisfied
unsafecode added (engine crate's existing#[allow(unsafe_code)]sites stay as-is).#[allow(...)]clippy waivers.ParallelDeltaApplierunchanged; internal-only refactor.cargo build -p engine --no-default-featuresstill compiles.Test plan
cargo fmt --allcargo clippy --workspace --all-targets --all-features --no-deps -- -D warningscleancargo nextest run -p engine --features parallel-receive-delta -E 'test(concurrent_delta) or binary(parallel_apply_concurrent) or binary(arc_drain_panic_recovery)'-> 340 passedcargo build -p engine --no-default-featurespassescargo bench -p engine --features parallel-receive-delta --bench parallel_receive_delta_perf --no-runcompiles