Skip to content

feat(cli,node): prime the multi-source probe and the node's upstream handshake - #2138

Merged
thiras merged 3 commits into
mainfrom
feat/2123-prime-probe-handshake
Sep 24, 2026
Merged

thiras merged 3 commits into
mainfrom
feat/2123-prime-probe-handshake

Conversation

@thiras

@thiras thiras commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

What

Two open-then-drop sites remained after #2063. Each opened the whole blob only to read the signed total_bytes, then dropped the pull:

  • probe_admitted_total on the CLI's multi-source path.
  • handshake_total_bytes on the node's serve-miss leg. The buffered pull_from_candidate did the same.

The upstream treats every open as real: it signs, claims a fill, and draws from origin on a miss. So each throwaway open cost a fill nobody paid for, and the same range was then opened a second time.

A bounded open needs the size first, since an out-of-bounds range is refused, clamped, or fails locally. So priming happens only when a size is known before the first open:

  • the bundle manifest's size;
  • else the unsigned probe hint (ProbeResponseExt.total_bytes).

With no size, or a wrong one, the whole-blob open behaves as today.

Refs #2123. This covers the two priming bullets; the other four stay open there.

  • decdn-client
    • Scheduler first-unit hook: SourceLane::first_unit, carried on StreamCandidate. An honoured unit is reserved for its lane and the rest of the blob is planned around it.
    • download_first_unit / stream_first_unit name the range each face opens first.
    • first_leg takes a pacer bound (up_to_bytes).
    • Also new: ClientRangedStore::has_record and a public PRIMED_MAX_IDLE.
  • cli
    • With a size and no resume record: build every lane first, so no on-chain pool open sits inside the 2 s prime window. Then open the face's first unit and park it on the answering lane.
    • It opens the first unit only on a lane whose coverage holds the unit.
    • A bounded open that runs past the blob's end (decdn_client::is_range_past_end) retries the whole blob. Any other failure fails the lane at once.
    • A signed size that differs from the expected one drops the pull and runs unprimed.
    • bundle pull passes the manifest size, else the probe hint.
  • node
    • Candidate and ProbedProvider carry total_bytes_hint.
    • The window pull-through handshake opens PrimeLeg::predicted_leg: the request cut to the ramp window at paid 0, the source's covered span, and the received-byte ceiling.
    • The prime is kept only for an owning claim over exactly that range.
    • The first run adopts it only when every check in PrimeKey::answers holds: same source, same pool, same ledger Arc, and same first leg. PrimedSource enforces freshness, measured from when the pull opened, which prime now takes as an argument.
    • A bounded handshake that runs past the blob's end retries the whole blob. Any other failure is classified at once, so a dead candidate costs one open against outer_pull_deadline.
    • The buffered pull adopts its whole-blob handshake when the store holds none of the blob.

Reviewer notes

  • Read first
    • Work::pick / reserve_first_units in scheduler.rs.
    • prime_admitted_lanes in fetch.rs.
    • PrimeLeg / PrimeKey / adopt_primed in node_origin/pull_leg.rs.
  • Unchanged on purpose
    • A resume, i.e. a .ranges record beside the output, keeps the size-probe path: its first gap is not the first unit.
    • The whole-file bundle path still never enforces the manifest size. A mismatch just runs unprimed.
    • Mixed and attach claims on the node do not prime.
  • Risk
    • An overstated size costs that candidate one extra round trip: a range-past-end refusal, then the whole-blob open. Only that case retries, so a genuine failure is classified exactly as before.
    • A mismatched size drops the primed pull, as today's handshake does.
    • On the primed CLI path every lane is built before the first open. So a signed size that then declines the gate has built lanes the probe path would not. An understated unsigned size can also decline fan-out before any open.
  • Test changes
    • a_probe_cache_hit_* now count 1 upstream connection instead of 2: the handshake is the drive's leg.
    • The serve_gated_correct_bytes double answers every header before gating the chunk data. The primed handshake is the real pull, and B needs its header to claim the fill. The coalescing test waits for B's claim before it launches leaf 2.
  • CHANGELOG: no entry. Nothing user-visible changes; the fetch just does less work upstream.

Verification

  • cargo fmt -- --check
  • cargo clippy --workspace --all-targets --all-features -- -D warnings
  • cargo nextest run -p decdn-client -p decdn-cli -p decdn-node --all-features: 2450 passed. This includes the regenerated decdn-client public-API snapshot.
  • RUSTDOCFLAGS="-D warnings" cargo doc --workspace --no-deps --document-private-items
  • anvil e2e, 11/11 passed: cli_fetch_multi_source, cli_fetch_stdout, cli_bundle_pull_multi_source, cli_bundle_pull_range_dedup, node_to_node_coalesce.
  • New tests. Mutation-checked: each fails with the hook or prime disabled.
    • a_lanes_first_unit_is_adopted_from_its_primed_pull and a_first_unit_is_honoured_only_when_wholly_missing_and_coverable (scheduler).
    • Downloader and Streamer a_primed_first_unit_is_adopted.
    • a_windowed_first_leg_is_the_range_the_drive_opens_first.
    • prime_tests::* (node), including the_predicted_leg_is_the_first_runs_first_leg, which checks predicted_leg against first_leg over the planner's first run.
    • only_a_capped_covering_lane_opens_the_first_unit (CLI). It also covers the age-from-open and range-fault classifier cases in the client.
    • window_pull_through_handshake_is_the_first_pull_leg: upstream serves 2 legs, not 3.
  • User-visible change: CHANGELOG.md entry added (n/a)
  • Design change: ADR in adr/ added or updated (n/a, no ADR describes the handshake)
  • PR title uses an allowed type with a lowercase subject

🤖 Generated with Claude Code

…handshake

The multi-source probe and the node's serve-miss handshake each opened the
whole blob only to read the signed size, then dropped the pull. The upstream
treats every open as real, so each one cost a throwaway fill and delayed the
real first leg (#2063).

When the size is known before the first open, that open is now a lane's
first unit and the drive adopts it through `PrimedSource`:

- decdn-client: a scheduler first-unit hook (`SourceLane::first_unit`,
  carried on `StreamCandidate`), `download_first_unit` / `stream_first_unit`
  naming the range each face opens first, a pacer bound on `first_leg`,
  `ClientRangedStore::has_record`, and a public `PRIMED_MAX_IDLE`.
- cli: with a size (the bundle manifest's, else the probe hint) and no
  resume record, build every lane first, then open the face's first unit
  and park it on the answering lane. A failed bounded open retries the whole
  blob, so a wrong hint never sinks a healthy holder; a signed size that
  differs drops the pull and runs unprimed.
- node: carry the probe's `total_bytes` hint on `Candidate` and the probe
  cache. The window pull-through handshake opens the pull leg's predicted
  first leg, and the first run adopts it only on the same source, pool,
  ledger and leg while it is fresh. The buffered pull adopts its whole-blob
  handshake when the store holds none of the blob.

With no size, or a size that turns out wrong, the whole-blob open behaves as
before.

Refs #2123

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Copilot AI lite review requested due to automatic review settings September 24, 2026 03:01
@thiras thiras self-assigned this Sep 24, 2026

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

🟡 Changes recommended

Unresolved moderate issues affect priming correctness and can reintroduce abandoned upstream work.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 2 Low severity

Open (2)
What changed in this PR

Adds first-leg priming to CLI multi-source downloads and node upstream handshakes, reducing duplicate upstream opens.

Changes:

  • Adds first-unit scheduling and primed-source adoption.
  • Propagates manifest and probe size hints through CLI and node paths.
  • Updates handshake behavior, tests, metrics, and public API snapshots.
File Reviewed changes
crates/​node/​tests/​node_origin_pull.rs Updates handshake tests and adds priming coverage.
crates/​node/​src/​selection.rs Adds candidate size hints.
crates/​node/​src/​node_origin/​pull_leg.rs Implements handshake priming and adoption.
crates/​node/​src/​node_origin/​mod.rs Propagates hints and primes buffered pulls.
crates/​node/​src/​node_origin/​abandon_drain.rs Documents primed connection handling.
crates/​node/​src/​metrics.rs Clarifies handshake metrics.
crates/​node/​src/​handlers/​client/​window.rs Supplies predicted prime legs.
crates/​node/​src/​handlers/​client/​mod.rs Updates handshake documentation.
crates/​node/​src/​dht/​probe_cache.rs Persists provider size hints.
crates/​client/​tests/​snapshots/​public_api__public_api.snap Updates the public API snapshot.
crates/​client/​src/​streamer.rs Adds streaming first-unit support.
crates/​client/​src/​source.rs Exposes priming idle limits.
crates/​client/​src/​scheduler.rs Reserves primed first units.
crates/​client/​src/​ranged_store.rs Adds resume-record detection.
crates/​client/​src/​lib.rs Exports priming APIs.
crates/​client/​src/​driver.rs Supports bounded first-leg calculation.
crates/​client/​src/​downloader.rs Adds download first-unit support.
crates/​client/​examples/​common/​mod.rs Initializes new candidate fields.
crates/​cli/​src/​commands/​fetch.rs Primes CLI fetch lanes.
crates/​cli/​src/​commands/​bundle_pull.rs Passes bundle size hints.

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

Comment thread crates/client/src/downloader.rs Resolved
Comment thread crates/client/src/streamer.rs Resolved
`align_range(0, 0, 0)` is the empty range, not an error, and every other
unit starts at 0 and ends inside the blob, so neither helper errors.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@github-code-quality

github-code-quality Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

Code Coverage Overview

Languages: Rust

Rust / code-coverage/cargo-llvm-cov

The overall line coverage in commit 5948593 in the feat/2123-prime-prob... branch remains at 85%, unchanged from commit bdba9e6 in the main branch.

Show a line coverage summary of the most impacted files.
File main bdba9e6 feat/2123-prime-prob... 5948593 +/-
crates/cli/src/...mmands/fetch.rs 56% 53% -3%
crates/node/src...s/client/mod.rs 96% 95% -1%
crates/node/src...lient/window.rs 71% 70% -1%
crates/cli/src/.../bundle_pull.rs 76% 76% 0%
crates/client/src/streamer.rs 91% 91% 0%
crates/client/s...c/downloader.rs 96% 96% 0%
crates/client/src/lib.rs 93% 93% 0%
crates/client/src/scheduler.rs 95% 95% 0%
crates/client/src/source.rs 92% 93% +1%
crates/node/src...gin/pull_leg.rs 87% 89% +2%

Updated September 24, 2026 04:13 UTC

Address the #2138 review.

- A bounded first open retries the whole blob only when the range ran past
  the blob's end (`decdn_client::is_range_past_end`: an open-stage `NotFound`,
  or the local range fault against a relay's smaller signed size). Every
  other failure is classified at once, so a dead candidate costs one open
  against `outer_pull_deadline`, not two. The range fault now keeps its
  typed `RangeVerifyError` cause.
- `PrimedSource::prime` takes the pull's open time, so the idle bound runs
  from the open. The buffered `pull_from_candidate` pull and the serve-miss
  handshake pull are no longer re-aged when parked; `PrimeKey` drops its own
  age check.
- The CLI opens the first unit only on a lane whose coverage holds it.
- A size mismatch logs at `warn!` with the peer; a store fault while
  matching the primed leg is told apart from a leg mismatch.
- Tests: `predicted_leg` equals `first_leg` over the planner's first run
  across ranged offsets, ceilings and an open ramp; lane eligibility; age
  from open; the range-fault classifier. The same-hash coalescing test waits
  for B's claim, since the gated upstream answers the header before it gates.
- Docs: the hint, handshake, gated-double and ADR observability wording.

Refs #2123

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@thiras
thiras merged commit e63443e into main Sep 24, 2026
28 checks passed
@thiras
thiras deleted the feat/2123-prime-probe-handshake branch September 24, 2026 04:32
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.

2 participants