Repository navigation
perf(node): handshake against the first run's candidate so its primed pull is adopted - #2336
Conversation
… pull is adopted The header handshake walked candidates in rank order, so a top-ranked partial holder lacking the request's first block took the handshake with a throwaway header open, and the first run then opened a fresh pull elsewhere. The walk now starts at the candidate the planner assigns the first run. Co-Authored-By: Claude Opus 5.5 <[email protected]>
There was a problem hiding this comment.
Copilot review overview
🔵 Needs a closer look
The change alters the paid node-to-node pull path and relies on subtle prime-adoption matching invariants under concurrent lanes, which warrants a human's final sign-off despite no issues being found.
Review effort: Balanced
Findings: None
What changed in this PR
This PR optimizes the node-to-node cache-miss pull path. On a serve-miss, handshake_from_candidates previously ran the header handshake in strict rank order. When the top-ranked candidate was a partial holder lacking the request's first block, PrimeLeg::predicted_leg returned None, the handshake fell back to a throwaway 16 KiB header open, and the primed pull was wasted — the first run then opened a fresh pull against whichever candidate actually holds the first block. Testnet data showed 497 of 1,258 handshakes paying that extra round trip (p50 113 ms, p90 309 ms).
The fix reorders the handshake walk so it visits the candidate the pull's first run will actually use first, letting that run adopt the handshake's primed pull. The adoption invariant (PrimeKey.candidate_ix == run.source_ix) is preserved because each candidate keeps its original index into ranked, and first_run_source plans over the same index-aligned coverages and rank that assemble uses, assuming a cache miss holds none of the range.
Changes:
- Add
PrimeLeg::first_run_source(predicts the first run's source index via the pull leg's ownplan_over) andhandshake_order(puts that candidate first, rest in rank order), and rewirehandshake_from_candidatesto walk that order while preserving per-candidatecandidate_ixand thebudgetbound. - Expose
ranged_pull::plan_overaspub(super)so the handshake planner reuses it. - Add unit tests for
handshake_orderand an integration test (a_serve_miss_handshakes_with_the_first_runs_holder) that asserts no stream opens to the first-ranked partial holder and exactly one (adopted) stream to the full holder.
| File | Description |
|---|---|
crates/node/src/node_origin/pull_leg.rs |
Adds first_run_source + handshake_order, reorders the handshake walk to prioritize the first run's holder, and adds unit tests. |
crates/node/src/node_origin/ranged_pull.rs |
Changes plan_over from private to pub(super) so the handshake planner can reuse it. |
crates/node/tests/node_origin_pull.rs |
Switches the full-holder fixture to spawn_holder_on to capture its metrics, adds an inbound-stream counter helper, and adds the reorder integration test. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Code Coverage OverviewLanguages: Rust Rust / code-coverage/cargo-llvm-covThe overall line coverage in commit d3926af in the Show a line coverage summary of the most impacted files.
Updated |
…refuses The full holder refuses the handshake, so the walk reaches the first-ranked partial holder, which must be asked only for a block it holds. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Why
On a node-to-node cache miss,
handshake_from_candidates(crates/node/src/node_origin/pull_leg.rs) runs the header handshake against the ranked candidates in rank order and stops at the first one that answers. With a prime,handshake_total_bytesopens the pull leg's predicted first leg (PrimeLeg::predicted_leg) and parks it for the first run to adopt (PeerRunSink::adopt_primed, matched byPrimeKey::answers, which needscandidate_ix == run.source_ix).predicted_legreturnsNonewhen the candidate does not hold the request's first discovery block. The handshake then falls back to a throwaway 16 KiB header open (covered_handshake_range), and the first run opens a fresh pull at the candidate the planner picks for that block.Testnet, 22 h: 497 of 1,258 handshakes took that fallback, almost all against one top-ranked partial holder that lacked the request's first block. Each such miss paid an extra handshake round trip (p50 113 ms, p90 309 ms), and the priming was wasted.
What
PrimeLeg::first_run_sourceplans the prime's range with the pull leg's own planner (ranged_pull::plan_over, the callassemblemakes, overdecdn_client::plan_covered_runs) on a node holding none of the range, and returns the first run'ssource_ixwhen that run starts in the prime's first block.handshake_orderputs that candidate first, then the rest in rank order. Without a prime, a size hint, or a holder of the first block, the order is the rank order.handshake_from_candidateswalks that order. Each candidate keeps its original index intorankedascandidate_ix, andbudgetstill bounds the attempts.ranged_pull::plan_overbecomespub(super)so the handshake reuses it.Testing
New tests:
node_origin::pull_leg::prime_tests::the_handshake_visits_the_first_runs_holder_firstnode_origin::pull_leg::prime_tests::without_a_prime_the_handshake_walks_the_rank_ordernode_origin::pull_leg::prime_tests::with_no_holder_of_the_first_block_the_handshake_walks_the_rank_ordernode_origin_pull::a_serve_miss_handshakes_with_the_first_runs_holder: a top-ranked holder of block 1 only and a full holder. S opens no stream to the first-ranked holder and exactly one to the full holder (the adopted primed pull). It fails with the reorder disabled.Gates:
cargo fmt --all -- --check: cleancargo clippy --workspace --all-targets --all-features: no issuescargo nextest run -p decdn-node: 1606 tests run, 1606 passedRUSTDOCFLAGS="-D warnings" cargo doc --workspace --no-deps --document-private-items: cleancargo test --no-run -p decdn-e2e --features anvil-e2e: builds🤖 Generated with Claude Code