Skip to content

feat(cache): wire range_pull helper into origin adapters and engine (#962) - #985

Merged
thiras merged 2 commits into
mainfrom
feat/962-range-pull-wiring
Jun 25, 2026
Merged

thiras merged 2 commits into
mainfrom
feat/962-range-pull-wiring

Conversation

@alpergundogdu

Copy link
Copy Markdown
Contributor

Closes #962.

Follow-up to #961 (ADR 037 §Origin-tier pull-through decision + range_pull/bao-verify helper) and the #823 decision to serve a byte range on a cache miss without pulling the whole blob from origin. #961 landed the load-bearing helper (align_range + encode_verified_range) but did not wire it into the Origin adapters or the cache engine. This PR is that wiring.

What it does

When the origin publishes the blob's pre-order bao outboard at the sibling key {H}.obao4, a node serving [a, b) fetches only the chunk-group-aligned span plus the small outboard (~0.4% of blob size), verifies it against the root H it already wants (untrusted outboard, self-validating per ADR 037), and imports it as a partial blob via iroh-blobs import_bao_bytes — no whole-blob origin egress.

  • Origin trait: new fetch_range(hash, OriginRangeRequest, outboard_max) → OriginRangeFetch { Ranged | NotFound | Unsupported }. Default impl returns Unsupported, so custom origins need no change and degrade to a whole-blob pull.
  • HTTP adapter: Range: bytes=a-(b-1) data GET (requires 206 + exact-length body) plus a sibling {hex}.obao4 GET. SSRF hardening unchanged — redirects stay disabled (Origin pull-through SSRF + metrics_bind non-loopback warning #579).
  • S3 adapter: ranged GetObject().range(...) + sibling {key}.obao4; an exact-length gate rejects a range-ignoring 200; a compressed ranged object is refused (it would break the offset→byte mapping).
  • Filesystem adapter: seek + exact read of the span + sibling outboard file.
  • Engine: pull_through_range(hash, byte_offset, byte_len, blob_size) verifies the untrusted span+outboard against the root H (encode_verified_range), imports the verified span as a partial blob, meters only the pulled bytes (span+outboard) against origin egress, and walks the origin fallback chain. Adds export_range for the serve side.

blob_size is the trusted total — sourced from the signed StreamResponse.total_bytes / manifest ChunkEntry.size, never the origin.

Verification semantics (ADR 037 §Origin-tier pull-through)

Self-validating against H: a tampered range, a tampered/foreign outboard, or a wrong root all fail bao verification. Fallback is always correct — absent {H}.obao4, no Range/206, a short/wrong-length span, or an oversize outboard all degrade to the existing whole-blob pull, which is never a correctness or availability failure. Only the actually-pulled bytes count against the seed-leech caps (the pulled side, not the whole blob).

Design choice flagged

ADR 037 leaves the range-unsupported fallback as "degrade to whole-blob"; it does not pin how the engine surfaces that to the caller. I made it an explicit RangePullOutcome::{Served, Unsupported} rather than a silent populate, so the node serving handler decides between an export_ranges serve (Served) and a whole-blob get/populate (Unsupported). A range pull imports a partial blob: has() stays false (a partial warming copy is not advertised as a full holder, per the ADR), and no DHT insert is announced — consistent with ADR 037 §"partial warming copies are not advertised". The origin_range_pull_enabled toggle is the node-side caller's gate (it decides whether to call pull_through_range); the engine method is always available.

Tests

Range pull served-without-whole-blob (HTTP + S3 + fs), bao verification of partial bytes, and the always-correct fallbacks: no outboard, range ignored (200), wrong-length span, oversize outboard, out-of-bounds reject (ADR 005), multi-origin fallback, and the zero-len ("to end") tail path.

Verification

  • cargo build && cargo clippy -p decdn-cache --all-targets -- -D warnings — clean
  • cargo clippy --workspace --all-targets -- -D warnings — clean
  • cargo fmt -- --check — clean
  • cargo nextest run -p decdn-cache — 222 passed
  • pre-commit (fmt, clippy, cargo doc, cargo deny) — all pass

Note: #963 (circuit-breaker) is concurrently editing the same crates/cache engine area in its own worktree; any merge overlap is handled at PR time.

🤖 Generated with Claude Code

…962)

Wire the range-scoped origin pull-through landed as a helper in #961 (ADR
037 §Origin-tier pull-through, #823) through the Origin adapters and cache
engine, so a node serving a byte range on a cache miss fetches only the
requested span plus the small sibling {H}.obao4 outboard instead of the
whole blob.

- Origin trait: add `fetch_range(hash, OriginRangeRequest, outboard_max)`
  returning `OriginRangeFetch { Ranged | NotFound | Unsupported }`, with a
  default impl that returns `Unsupported` so custom origins degrade safely.
- HTTP adapter: `Range: bytes=a-(b-1)` data GET (requires 206 + exact-length
  body) + a sibling `{hex}.obao4` GET; redirects stay disabled (SSRF #579).
- S3 adapter: ranged `GetObject().range(...)` + sibling `{key}.obao4`;
  exact-length gate rejects a range-ignoring 200; refuses compressed ranges.
- Filesystem adapter: seek + exact read of the span + sibling outboard file.
- Engine: `pull_through_range(hash, off, len, blob_size)` verifies the
  untrusted span+outboard against the root H via `encode_verified_range`,
  imports the verified span as a PARTIAL blob via `import_bao_bytes`, and
  meters only the pulled bytes (span+outboard) against origin egress; walks
  the origin fallback chain. Adds `export_range` for the serve side.

Verification is self-validating against H per the ADR: a tampered range,
tampered/foreign outboard, or wrong root all fail and degrade to a
whole-blob pull. Absent `{H}.obao4`, no `Range`/206, or a short/wrong-length
span likewise degrade — never a correctness or availability failure.

Tests: range pull served-without-whole-blob (HTTP + S3 + fs), bao verify of
partial bytes, no-outboard / range-ignored / wrong-length / oversize-outboard
fallbacks, out-of-bounds reject, multi-origin fallback, and the zero-len
to-end path. `cargo nextest run -p decdn-cache`: 222 passed.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Copilot AI review requested due to automatic review settings June 24, 2026 13:55

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request implements range-scoped origin pull-through for filesystem, HTTP, and S3 backends, allowing chunk-group-aligned byte spans and their sibling outboards to be fetched, verified, and imported as partial blobs. Feedback on these changes highlights several key improvements: enforcing logical-eviction guards at the start of range pulls to prevent caching evicted content, streaming S3 bodies chunk-by-chunk to avoid memory exhaustion (OOM) when a server ignores range headers, failing fast on local store errors instead of continuing the origin fallback chain, and validating the Content-Length header upfront in HTTP and S3 origins to quickly reject range-ignoring responses.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread crates/cache/src/engine.rs Resolved
Comment thread crates/cache/src/origin/s3.rs Resolved
Comment thread crates/cache/src/engine.rs Resolved
Comment thread crates/cache/src/origin/http.rs Resolved
Comment thread crates/cache/src/origin/s3.rs Resolved

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 wires the existing range_pull verification helper into the cache engine and the built-in origin adapters (HTTP/S3/filesystem) to enable best-effort range-scoped origin pull-through using sibling {H}.obao4 bao outboards. This reduces origin egress on cache-miss range requests by fetching and verifying only the aligned span + outboard, importing it as a partial blob.

Changes:

  • Extend the Origin trait with fetch_range(...) plus new OriginRange* types, and implement range fetch for HTTP/S3/filesystem origins.
  • Add CacheEngine::pull_through_range(...) to verify/import partial ranges via encode_verified_range + import_bao_bytes, plus CacheEngine::export_range(...) for local range reads.
  • Add end-to-end tests covering served range pulls and “always-correct” fallbacks (missing outboard, range ignored, wrong-length span, OOB reject, multi-origin fallback, tail reads).

Reviewed changes

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

Show a summary per file
File Description
crates/cache/src/origin/mod.rs Adds Origin::fetch_range default + OriginRangeFetch / OriginRangeRequest types for range-capable origins.
crates/cache/src/origin/http.rs Implements HTTP ranged GET + sibling outboard fetch with bounded buffering and 206 enforcement.
crates/cache/src/origin/s3.rs Implements S3 GetObject().range(...) + sibling outboard fetch and introduces a bounded collection helper.
crates/cache/src/origin/fs.rs Implements filesystem seek+read for aligned spans and sibling outboard reads; defines shared .obao4 suffix.
crates/cache/src/engine.rs Adds pull_through_range, RangePullOutcome, export_range, and an outboard-size helper.
crates/cache/src/lib.rs Re-exports new range-pull origin types from the crate root.
crates/cache/tests/pull_through.rs Adds HTTP-mocked engine integration tests for range pull-through behavior and fallbacks.
crates/cache/tests/s3_origin.rs Adds S3-origin unit/integration-style tests for fetch_range behavior and degradations.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crates/cache/src/origin/s3.rs Resolved
Comment thread crates/cache/src/origin/fs.rs Resolved
Comment thread crates/cache/src/engine.rs Resolved
Comment thread crates/cache/src/engine.rs Resolved
@github-actions

github-actions Bot commented Jun 24, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 83.50% (was 83.61%, ▼ 0.11%) — 40312/48277 lines

File Coverage Lines Δ
crates/cache/src/engine.rs 91.24% 1979/2169 ▼ 0.70%
crates/cache/src/lib.rs 100.00% 28/28 ● 0.00%
crates/cache/src/origin/fs.rs 80.22% 438/546 ▼ 3.06%
crates/cache/src/origin/http.rs 83.33% 240/288 ▼ 8.74%
crates/cache/src/origin/mod.rs 54.84% 34/62 ▼ 20.84%
crates/cache/src/origin/s3.rs 91.01% 425/467 ▼ 2.26%
History
Commit Total Δ vs base
17bed89 83.52% ▼ 0.09%
37a4220 (HEAD) 83.50% ▼ 0.11%

Address PR #985 review on range pull-through wiring:

- engine: enforce the logical-eviction guard at the start of
  pull_through_range (mirrors get/populate) so an evicted (DMCA/operator
  takedown) hash is never re-fetched and re-cached; fail fast on a local
  CacheError::Store during the chain walk instead of advancing to the
  next origin; return an explicit error from export_range when a partial
  blob's size is unreportable (no silent empty range); correct the
  # Errors docs to match actual Unsupported-on-origin-fault behavior.
- s3: stream the ranged/outboard body chunk-by-chunk via
  ByteStream::try_next, aborting the moment cumulative bytes exceed the
  cap, instead of ByteStream::collect() buffering the whole body first;
  fast-fail on a Content-Length that mismatches the requested span before
  collecting.
- http: fast-fail on a 206 whose Content-Length mismatches the requested
  span before reading the body.
- fs: check {H}.obao4 length via metadata() before read() and degrade
  early if it exceeds the outboard cap.

Tests: range pull on an evicted hash is refused (no egress, miss
counted); oversized S3 outboard degrades via the streaming abort; oversized
fs outboard rejected on metadata before read.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@thiras
thiras merged commit 80bba95 into main Jun 25, 2026
20 checks passed
@thiras
thiras deleted the feat/962-range-pull-wiring branch June 25, 2026 16:11
thiras added a commit that referenced this pull request Jun 27, 2026
…) (#1007)

* feat(node): wire origin-tier range pull-through into cdn/client/v1 (#823, #990)

A bounded/offset `cdn/client/v1` cache-miss now scopes its origin fetch to the
requested span instead of pulling the whole blob. The engine machinery
(`pull_through_range`/`export_range` + the `{H}.obao4` outboard verification on
the HTTP/S3/fs adapters) already shipped in #962/#985; this wires it into the
live handler and adds the missing size source.

- cache: add `Origin::size` (HTTP `HEAD` / S3 `HeadObject` / fs stat) +
  `CacheEngine::origin_size` so a cold node learns the exact `blob_size` the
  bao tree needs (the `{H}.obao4` outboard alone doesn't pin the final chunk
  group). A `Content-Encoding`/missing/absent response degrades to `None`.
- node: `try_range_pull_through` runs on a bounded/offset miss (origin size
  probe -> `pull_through_range` -> serve the verified partial via
  `export_range`) before the whole-blob fallback, gated on the existing
  `pull_authorized` ownership check. The size gate uses the probed total for
  the partial blob; `deliver` serves ranges via `export_range`, metering
  payment over the served span. The window-paced fallback is gated on
  `byte_len == 0` so a bounded request can't over-deliver/over-bill the whole
  blob. A local store fault on the partial-import path logs at `warn`.
- adr: amend ADR 037 §Origin-tier pull-through (the design SoT) to document the
  origin size probe and mark #823 landed. No new ADR — the design was already
  specified there.
- tests: e2e `origin_range_pull.rs` (cold ranged path fetches only the range
  and stays partial; no-outboard fallback; unauthorized request fronts no
  origin egress) + `Origin::size`/`origin_size` unit coverage.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

* fix(node): reject out-of-bounds ranges before signing; review fixes (#823)

Addresses code review on PR #1007:

- Reject an out-of-bounds bounded range (`byte_offset + byte_len` overflows or
  exceeds the blob) with a `StreamError` BEFORE signing the success response
  (ADR 005 §Bounded byte ranges), so a malformed client never accepts a signed
  `ok: true` that `export_range` then aborts mid-stream. New
  `ServeRejectReason::RangeNotSatisfiable` collapses to `NotFound` on the wire
  (reputation-benign — a client's bad range must not penalise the node) with a
  dedicated `serve_stream_rejected_range_not_satisfiable` metric.
- Docs: correct the `Origin::size` trait/HTTP comments — a non-success HEAD
  (404/permission/5xx/disabled-redirect) degrades to `None` like `get_bounded`,
  and only a transport fault surfaces as an error; the whole-blob fallback
  re-surfaces a persistent fault. Fix the stale "fetch_range refuses compressed
  ranges" claim (only S3 does explicitly) and "one chunk" -> "one chunk group".
- Tests: resume-to-end (`byte_len == 0`) e2e proving the tail is served (not
  zero bytes); out-of-bounds reject e2e; the no-outboard fallback now asserts an
  un-ranged whole-blob GET and zero ranged GETs.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

* test(node): harden range-pull e2e helper and pin OOB reject reason (#823)

Addresses CodeRabbit review on PR #1007:

- `ranged_paid_pull`: reject an impossible advertised `total_bytes` (`< byte_offset`,
  or smaller than the requested range end) with `checked_sub`/`ensure` instead of
  `saturating_sub` to zero, so a server that signs a bad size fails the test loudly.
- `out_of_bounds_range_is_rejected_before_delivery`: assert the dedicated
  `serve_stream_rejected_range_not_satisfiable_total` counter increments. The wire
  `StreamError` collapses to `NotFound` (shared with cache-miss/unknown-channel), so
  the metric is the only proof the range-not-satisfiable gate fired specifically.
  `handler_over_http_origin` now returns the `Metrics` handle.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

---------

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
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.

Origin range pull-through: wire range_pull helper into Origin adapters + engine (#823 follow-up)

3 participants