Skip to content

perf(node): de-globalize the lane store working set (#1792 item 1) - #1876

Merged
alpergundogdu merged 2 commits into
mainfrom
claude/issue-1792-bnzq86
Aug 30, 2026
Merged

alpergundogdu merged 2 commits into
mainfrom
claude/issue-1792-bnzq86

Conversation

@alpergundogdu

Copy link
Copy Markdown
Contributor

Summary

Item 1 (the headline) of the #1792 serve-path hot-path audit: the lane-state store's in-memory working set no longer sits behind one global mutex.

PersistentPoolStateStore held its entire lane working set behind a single Mutex<LaneBuffer>. Every record on the paid-delivery path serialised against every other lane's record and against settlement's load_all/get. That one lock is the serve path's contention point at scale.

This PR replaces the mutex-guarded HashMap with:

  • a sharded DashMap<LaneKey, LaneSlot> working set — a record takes only its own lane's shard lock, so no lane serialises on another and load_all/get no longer contend with record;
  • a lock-free crossbeam_queue::SegQueue<LaneKey> dirty work-list — record/forget/set_registered_until push a key; flush drains it.

A LaneSlot is Live(LaneState) or Tombstoned. LaneState is heap-free, so a Live slot clones out with a memcpy on the read path (boxing it would trade that for a heap indirection on the frequent variant, so Live stays inline under a justified large_enum_variant allow). The slot enum keeps a forget/record race decidable under the entry's shard lock — the role the buffer mutex played for the whole map.

Flush semantics (unchanged shape)

flush drains the work-list into a dedup HashSet, clones each slot out under its shard lock (releasing before the postcard encode), and commits the fsynced batch with no shard lock held — the same double-buffered design as before, so a concurrent record never waits on disk I/O. A record that lands after its key is drained re-pushes the key and is captured by the next flush, so the durable watermark still lags by at most one flush interval (ADR 003 §Off-chain voucher state persistence). On commit failure the drained keys re-push; on success tombstoned slots are reaped with a guarded remove_if, so a concurrent resurrecting record keeps its slot and its fresh dirty mark.

All PoolStateStore trait signatures are unchanged — this is an internal de-globalization, no call-site churn.

Validation

  • cargo build -p decdn-node ✅
  • cargo clippy -p decdn-node --all-targets ✅
  • cargo test -p decdn-node --lib (771 tests, incl. all 30 channel_store tests — concurrency + leaf-page packing) ✅
  • cargo test -p decdn-node --test client_loopback (111 tests, full paid serve path) ✅
  • cargo fmt --check ✅
  • RUSTDOCFLAGS="-D warnings" cargo doc --workspace --no-deps --document-private-items ✅
  • New dep crossbeam-queue 0.3 is MIT OR Apache-2.0

Generated by Claude Code

The `PersistentPoolStateStore` held its entire in-memory lane working set
behind one `Mutex<LaneBuffer>`, so every `record` on the paid-delivery
hot path serialised against every other lane's `record` and against
settlement's `load_all`/`get`. At scale this single lock is the serve
path's contention point.

Replace the mutex-guarded `HashMap` with a sharded `DashMap<LaneKey,
LaneSlot>` plus a lock-free `crossbeam_queue::SegQueue<LaneKey>` dirty
work-list. A `record` now takes only its own lane's shard lock — no lane
serialises on another, and `load_all`/`get` no longer contend with a
`record`. `LaneState` is heap-free, so a slot clones out with a memcpy;
the `Live`/`Tombstoned` slot enum keeps a `forget`/`record` race
decidable under the entry's shard lock, the role the buffer mutex played
for the whole map.

`flush` drains the work-list into a dedup set, clones each slot out under
its shard lock (releasing before the postcard encode), and commits the
fsynced batch with no shard lock held — same double-buffered shape as
before. A `record` landing after its key is drained re-pushes the key and
is captured next flush, so the durable watermark still lags by at most one
flush interval (ADR 003 §Off-chain voucher state persistence). On commit
failure the drained keys re-push; on success tombstoned slots are reaped
with a guarded `remove_if` so a concurrent resurrecting `record` keeps its
slot.

All `PoolStateStore` trait signatures are unchanged.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_013MSiZrVoUQzy4J5Ci8RGEu
Copilot AI lite review requested due to automatic review settings August 30, 2026 15:28

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR removes a global mutex bottleneck in the node’s persistent lane-state store by replacing the single in-memory HashMap+Mutex working set with a sharded concurrent map and a lock-free dirty work queue, reducing cross-lane contention on the paid delivery path.

Changes:

  • Replaces the mutex-guarded lane working set with DashMap<LaneKey, LaneSlot> and models deletions via tombstoned slots.
  • Switches dirty tracking from shared HashSet marks to a drained/deduped crossbeam_queue::SegQueue<LaneKey> work-list.
  • Adds crossbeam-queue as a workspace dependency and wires it into the node crate.

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 2 comments.

File Description
crates/node/src/channel_store.rs Reworks the lane-state store working set to use DashMap + tombstoned slots and a lock-free dirty queue; updates flush snapshot logic accordingly.
crates/node/Cargo.toml Adds the crossbeam-queue dependency for the dirty work-list.
Cargo.toml Adds crossbeam-queue = "0.3" to workspace dependencies.
Cargo.lock Records the new crossbeam-queue package and associated lockfile updates.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread crates/node/src/channel_store.rs Resolved
Comment thread crates/node/src/channel_store.rs Resolved
…e empty-slot comment

Two review findings on the de-globalized lane store:

- `flush` drains the dirty work-list before encoding. If `postcard`
  encoding of a lane fails, the early return left the already-drained keys
  out of the queue, so those lanes went permanently "clean" without ever
  being committed. Re-push every drained key on the encode-error path, the
  same retry guarantee the commit-failure path already gives.

- The "cannot happen" comment on the empty-slot arm was wrong. A drained
  key can resolve to no slot: a `forget` re-pushes the key after a flush
  reaps its tombstone via `remove_if`. Skipping is correct (the row is
  already gone); the comment now describes that path.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_013MSiZrVoUQzy4J5Ci8RGEu
@github-actions

Copy link
Copy Markdown

Coverage report

Total: 82.12% (was 82.11%, ▲ +0.01%) — 68327/83207 lines

File Coverage Lines Δ
crates/node/src/channel_store.rs 86.38% 1325/1534 ▲ +0.19%
History
Commit Total Δ vs base
c74d752 (HEAD) 82.12% ▲ +0.01%

@alpergundogdu
alpergundogdu merged commit 677e5d0 into main Aug 30, 2026
27 checks passed
@alpergundogdu
alpergundogdu deleted the claude/issue-1792-bnzq86 branch August 30, 2026 16:14
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.

3 participants