Repository navigation
perf(node): coarse clock for the voucher-accept path's under-lock clock reads (#1792 item 4) - #1870
Conversation
…ck reads Group E item 4 of the serve-path hot-path audit (#1792): the voucher/preimage accept path reads the wall clock twice while holding the per-lane mutex — the capability-expiry gate and the `last_voucher_at` liveness stamp — so every accepted proof pays two `SystemTime::now()` syscalls inside the most contended critical section on the paid path. Both reads tolerate ~second resolution (an on-chain grant deadline and a best-effort admin diagnostic). Add `CoarseClock`: an `AtomicU64` of Unix milliseconds that a background task refreshes every 500 ms, so those two reads become one relaxed atomic load. The refresher holds only a `Weak`, so it self-terminates when the handler (the clock's last owner) drops at shutdown and needs no `StopHandle` wiring. Until a refresher runs the cell holds a `0` sentinel and every read falls back to a live `SystemTime::now()`, so a handler built without one — every unit and loopback test — reports the exact wall clock and behaves exactly as before. Only the runtime attaches a refresher, so the coarsening is a production-only optimization, not a semantic change. The admin readouts outside the lane lock keep reading the live clock. Co-Authored-By: Claude Opus 4.8 <[email protected]> Claude-Session: https://claude.ai/code/session_013MSiZrVoUQzy4J5Ci8RGEu
There was a problem hiding this comment.
Pull request overview
This PR implements issue #1792 item 4 by removing SystemTime::now() syscalls from the most contended per-lane tokio::Mutex critical section in the paid delivery voucher/preimage accept path. It introduces a background-refreshed coarse wall clock (atomic Unix milliseconds) and wires it into the runtime so the under-lock timestamp reads become relaxed atomic loads in production, while tests continue to use live wall-clock reads via the None fallback.
Changes:
- Add
CoarseClock(AtomicU64 unix-millis) with an optional background refresher task. - Wire
CoarseClockthroughClientHandlerDepsand initialize/spawn the refresher in the node runtime. - Replace under-lock clock reads in
voucher.rs(capability expiry gate +last_voucher_atstamps) to use the coarse clock.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| crates/node/src/runtime/mod.rs | Creates a shared CoarseClock, spawns the refresher, and injects it into ClientHandlerDeps. |
| crates/node/src/lib.rs | Exposes the new coarse_clock module. |
| crates/node/src/handlers/client/voucher.rs | Switches expiry/liveness under-lock clock reads to use self.coarse_clock. |
| crates/node/src/handlers/client/mod.rs | Adds optional coarse_clock to deps and stores an Arc<CoarseClock> on the handler (fallback to live clock in tests). |
| crates/node/src/coarse_clock.rs | New coarse clock implementation + unit tests for fallback/tick/refresher behavior. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
The `cargo doc` CI job runs `--document-private-items` with `-D warnings`, which resolves and checks the intra-doc link to the `pub(super)` `handlers::client::unix_millis` — a path not reachable from the crate root, so it fails as a broken link. Render it as a plain code span. Co-Authored-By: Claude Opus 4.8 <[email protected]> Claude-Session: https://claude.ai/code/session_013MSiZrVoUQzy4J5Ci8RGEu
Two doc-precision fixes on the coarse-clock docs: - Each read is a single relaxed atomic load; there are still two reads per proof (expiry gate + last_voucher_at stamp), so say "each becomes a load" rather than implying the two collapse into one. - The `0` sentinel: a live millisecond reading is 0 only at the Unix epoch itself, which a running node's clock is not at — reword to avoid claiming 0 is impossible, and note a stray 0 just routes reads to the live clock. Co-Authored-By: Claude Opus 4.8 <[email protected]> Claude-Session: https://claude.ai/code/session_013MSiZrVoUQzy4J5Ci8RGEu
Coverage reportTotal: 82.09% (was 82.08%, ▲ +0.01%) — 68204/83083 lines
History
|
One conflict, in `dispatch.rs`'s `use super::{…}` list. Main's per-lane
perf work (#1870/#1872/#1873) dropped `Mutex` from it when the accepted
`LaneState` moved into the lane instead of being cloned; this branch had
added `FloorRefusalSite` for the split refusal logging. Took the union:
main's list, minus `Mutex` (no remaining use in the file), plus
`FloorRefusalSite`.
Everything else auto-merged, including both serve loops where the perf
commits and this branch's floor calls are adjacent. Verified the floor
surface survived intact: `release_if_repaid` and the pool-only mid-stream
re-check in `delivery.rs` / `serve_leg.rs`, both admission gates and the
refusal logging in `dispatch.rs`, and the accumulator itself.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
What
Addresses item 4 of #1792 (Group E of the serve-path hot-path audit): "
SystemTime::now()syscalls under the lane lock."The voucher/preimage accept path reads the wall clock twice while holding the per-lane
tokio::Mutex— the capability-expiry gate (voucher.rs) and thelast_voucher_atliveness stamp (both the voucher and preimage paths). So every accepted proof pays twoSystemTime::now()syscalls inside the most contended critical section on the paid delivery path. Both reads tolerate ~second resolution: one is an on-chain grant deadline, the other a best-effort admin diagnostic ("seconds since last voucher").Change
crate::coarse_clock::CoarseClock— anAtomicU64of Unix milliseconds. A background task refreshes it every 500 ms, so the two under-lock reads become one relaxed atomic load instead of a syscall.CoarseClock::spawn_refresher) holds only aWeak, so it is self-terminating: once the handler (the clock's last owner) drops at shutdown, the nextupgrade()fails and the loop exits. It owns no queue and awaits no sender, so it needs noStopHandlewiring.ClientHandlerDeps.coarse_clock; the runtime creates one, spawns the refresher, and passes it in.voucher.rsreadsself.coarse_clock.unix_seconds()/unix_millis().Behavior is unchanged everywhere a refresher isn't running
Until a refresher runs, the cell holds a
0sentinel and every read falls back to a liveSystemTime::now(). So a handler built without one — every unit and loopback test, which construct handlers viaClientHandlerDeps::new(defaulting the field toNone) — reports the exact wall clock and behaves precisely as before. Only the runtime attaches a refresher, so the coarsening is a production-only optimization, not a semantic change.0is a safe sentinel because a real Unix-ms reading is never zero.Scope is strictly the two under-lock reads. The
unix_millis()admin readouts outside the lane lock (region-stats age reporting) keep reading the live clock, where accuracy matters and there's no contention to relieve.Correctness notes
unix_seconds()=unix_millis()/1000=floor(ms/1000), identical to the priorpayment_settlement::unix_now()(Duration::as_secs()) for the same instant — a drop-in for the expiry comparison.last_voucher_atonly makes a lane look marginally staler — the safe direction the field already documents.Testing
cargo build -p decdn-node— cleancargo clippy -p decdn-node --all-targets— clean (no anti-panic policy violations)cargo test -p decdn-node --lib— 771 passed (incl. 3 newcoarse_clocktests: live fallback, stamped read-back, refresher-stamps-then-self-terminates)cargo test -p decdn-node --test client_loopback -- capabilit region activity voucher expir— 13 passed (capability-expiry gate + lane-activity readouts exercised)cargo fmt --checkandcargo doc -D warnings— cleanCloses #1792 item 4.
🤖 Generated with Claude Code
https://claude.ai/code/session_013MSiZrVoUQzy4J5Ci8RGEu
Generated by Claude Code