Skip to content

perf(node): handshake against the first run's candidate so its primed pull is adopted - #2336

Merged
thiras merged 2 commits into
mainfrom
perf/node-handshake-first-run-candidate
Oct 7, 2026
Merged

thiras merged 2 commits into
mainfrom
perf/node-handshake-first-run-candidate

Conversation

@alpergundogdu

Copy link
Copy Markdown
Contributor

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_bytes opens the pull leg's predicted first leg (PrimeLeg::predicted_leg) and parks it for the first run to adopt (PeerRunSink::adopt_primed, matched by PrimeKey::answers, which needs candidate_ix == run.source_ix).

predicted_leg returns None when 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_source plans the prime's range with the pull leg's own planner (ranged_pull::plan_over, the call assemble makes, over decdn_client::plan_covered_runs) on a node holding none of the range, and returns the first run's source_ix when that run starts in the prime's first block.
  • handshake_order puts 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_candidates walks that order. Each candidate keeps its original index into ranked as candidate_ix, and budget still bounds the attempts.
  • ranged_pull::plan_over becomes pub(super) so the handshake reuses it.

Testing

New tests:

  • node_origin::pull_leg::prime_tests::the_handshake_visits_the_first_runs_holder_first
  • node_origin::pull_leg::prime_tests::without_a_prime_the_handshake_walks_the_rank_order
  • node_origin::pull_leg::prime_tests::with_no_holder_of_the_first_block_the_handshake_walks_the_rank_order
  • node_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: clean
  • cargo clippy --workspace --all-targets --all-features: no issues
  • cargo nextest run -p decdn-node: 1606 tests run, 1606 passed
  • RUSTDOCFLAGS="-D warnings" cargo doc --workspace --no-deps --document-private-items: clean
  • cargo test --no-run -p decdn-e2e --features anvil-e2e: builds

🤖 Generated with Claude Code

… 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]>
Copilot AI balanced review requested due to automatic review settings October 6, 2026 10:56

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.

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 own plan_over) and handshake_order (puts that candidate first, rest in rank order), and rewire handshake_from_candidates to walk that order while preserving per-candidate candidate_ix and the budget bound.
  • Expose ranged_pull::plan_over as pub(super) so the handshake planner reuses it.
  • Add unit tests for handshake_order and 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.

@github-code-quality

github-code-quality Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

Code Coverage Overview

Languages: Rust

Rust / code-coverage/cargo-llvm-cov

The overall line coverage in commit d3926af in the perf/node-handshake-... branch remains at 88%, unchanged from commit e1024f7 in the main branch.

Show a line coverage summary of the most impacted files.
File main e1024f7 perf/node-handshake-... d3926af +/-
crates/node/src/dht/publish.rs 92% 92% 0%
crates/client/src/lib.rs 92% 92% 0%
crates/client/src/scheduler.rs 95% 95% 0%
crates/protocol/src/framing.rs 97% 97% 0%
crates/node/src...gin/pull_leg.rs 91% 92% +1%
crates/client/src/streamer.rs 90% 91% +1%
crates/client/src/sink.rs 96% 97% +1%
crates/client/s...c/connection.rs 97% 98% +1%

Updated October 06, 2026 11:27 UTC

…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]>
@thiras
thiras merged commit 8160abb into main Oct 7, 2026
30 checks passed
@thiras
thiras deleted the perf/node-handshake-first-run-candidate branch October 7, 2026 07:35
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