Skip to content

fix(cache): window origin range pulls to bound memory (#2065) - #2066

Merged
thiras merged 3 commits into
mainfrom
fix/2065-window-range-pull
Sep 19, 2026
Merged

thiras merged 3 commits into
mainfrom
fix/2065-window-range-pull

Conversation

@thiras

@thiras thiras commented Sep 19, 2026

Copy link
Copy Markdown
Contributor

Closes #2065

Problem

pull_through_range asked the origin for the whole aligned span in one read. It then built a second buffer of the same size for the encoding. So each request held about 2× its span in memory, with no cap on size or concurrency. On bcl, a few 1–2.4 GB range requests OOM-killed the node. The own-origin serve-miss path (Flow A, origin_encode_range) had the same bug.

Fix

  • Origin trait split. Origin::fetch_range(hash, req, outboard_max) → fetch_range_data(hash, req), which fetches data only. A pull reads the outboard once through the existing fetch_outboard. Without this, a windowed loop would download the blob/256 outboard again for every window. The http/s3/fs adapters drop their outboard sub-fetch, and the dead get_bounded/get_object_bounded helpers are deleted.
  • New origin_range module.
    • OriginRangeCursor: reads the outboard, fetches the first window eagerly (so a decline still moves on to the next origin before we commit), then fetches the remaining windows.
    • RANGE_PULL_WINDOW_BYTES = 4 MiB, a multiple of the chunk group.
  • pull_through_range. Fetches, verifies (encode_verified_range) and imports (import_bao_bytes) the span one window at a time. Each window's encoding carries its own parent nodes, so it imports on its own. If a later window fails to verify, or the origin stops serving mid-span, the pull degrades to Unsupported as before. Windows already imported stay as verified partial data under protect_partial.
  • Flow A. origin_range_wire replaces origin_encode_range. Joining per-window encodings would repeat the shared parent nodes, so Flow A runs one coherent fsm::encode_ranges_validated over an OriginWindowReader that loads windows on demand. The output goes through a bounded channel (the same pattern as serve_encoder.rs).
    • BackendReader streams the wire.
    • A mid-stream VerifyFailed is parked through StashedFault, so the sink still reports a hard local-origin fault.
    • A wrong-length outboard is still an immediate VerifyFailed.
  • Concurrency bound. An engine-wide semaphore allows MAX_CONCURRENT_RANGE_PULLS = 4 pulls, shared by both paths. A caller past the bound waits. It does not degrade, because the fallback is a whole-blob origin pull, which costs more egress.

Peak memory per pull is now about 2 × window + outboard, whatever byte_len is. For a 2.4 GB blob that is about 8 MiB + about 9.4 MiB, down from about 4.8 GB.

Tests

  • pull_through.rs::windowed_range_pull:
    • a span of more than 5 windows: every origin read is ≤ 1 window, the outboard is read exactly once, there is one data read per window, and the span reads back byte-exact;
    • a corrupt later window degrades;
    • a mid-span decline degrades;
    • concurrency bound: 6 gated pulls, and the origin never sees more than 4 in flight. I mutation-checked this test: it fails when the permit is removed.
  • Engine Flow A tests: a multi-window wire is byte-identical to the header-less whole-span encode and admits under H; each read is ≤ 1 window; a corrupt 2nd window ends on VerifyFailed; a wrong-length outboard fails with VerifyFailed at open; an unsupported origin gives None.
  • backend_source: the corrupt-origin test now drives the real NodeAdmitStore sink and asserts the parked VerifyFailed.
  • Adapter tests (fs/s3/cli origin_import) are updated for the split. The cli test now actually verifies the range against the written outboard.

Local runs: cargo nextest run -p decdn-cache -p decdn-node -p decdn-cli -p decdn-client-pull -p decdn-bao-range is all green. Clippy (-D warnings --all-targets), cargo doc -D warnings, fmt and pre-commit also pass.

Notes

  • The outboard is still held whole (blob/256). That is bounded, but not windowed.
  • Origin is a public trait. A custom implementation must rename fetch_range → fetch_range_data (see the CHANGELOG entry). Pre-launch, so there is no shim.

🤖 Generated with Claude Code

pull_through_range and Flow A's origin encode fetched the whole requested
span as one buffer, then built a second span-sized encoding. A few
multi-GB range requests OOM-killed the node.

- Origin::fetch_range becomes the data-only fetch_range_data; the
  outboard comes from fetch_outboard, read once per pull.
- pull_through_range fetches, verifies, and imports the span in
  RANGE_PULL_WINDOW_BYTES (4 MiB) windows: O(window + outboard) memory.
- Flow A: origin_range_wire replaces origin_encode_range and runs one
  coherent encode over on-demand windows behind a bounded channel;
  BackendReader streams it and parks a mid-stream VerifyFailed.
- At most MAX_CONCURRENT_RANGE_PULLS (4) range pulls run at once.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Copilot AI lite review requested due to automatic review settings September 19, 2026 17:22
@thiras thiras self-assigned this Sep 19, 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

Window lengths must be validated and malformed responses classified as VerifyFailed before approval.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 High severity · 1 Low severity

Open (2)
What changed in this PR

This PR bounds origin range-pull memory with 4 MiB windows, separate outboard/data reads, streamed Flow A handling, and concurrency limits.

Changes:

  • Adds windowed range verification and importing.
  • Splits the Origin range API and updates adapters.
  • Adds concurrency, tests, documentation, and ADR updates.
File Summary
crates/​node/​tests/​origin_range_pull.rs Updates range API references.
crates/​node/​src/​node_origin/​pull_leg.rs Updates local-origin documentation and test adapter.
crates/​node/​src/​node_origin/​backend_source.rs Streams origin wires and preserves typed faults.
crates/​node/​src/​handlers/​client/​window.rs Updates Flow A documentation.
crates/​node/​src/​handlers/​client/​dispatch.rs Updates range API references.
crates/​e2e/​src/​node.rs Updates filesystem-origin documentation.
crates/​cli/​tests/​origin_import.rs Tests separate outboard and data fetching.
crates/​cache/​tests/​s3_origin.rs Updates S3 range and outboard tests.
crates/​cache/​tests/​pull_through.rs Adds windowing and concurrency coverage.
crates/​cache/​src/​origin/​s3.rs Implements data-only range fetching.
crates/​cache/​src/​origin/​mod.rs Splits the Origin range API. Nit (1 vote): clarify missing-outboard handling. Nit (3 votes): document required custom-implementation migration.
crates/​cache/​src/​origin/​http.rs Implements data-only HTTP range fetching.
crates/​cache/​src/​origin/​fs.rs Implements data-only filesystem range fetching.
crates/​cache/​src/​origin_range.rs Adds window cursors and streaming wires. Critical (2 votes): validate returned window lengths and park malformed responses as VerifyFailed.
crates/​cache/​src/​lib.rs Exports range-window APIs.
crates/​cache/​src/​engine.rs Integrates windowed pulls and concurrency limits.
CHANGELOG.md Documents the memory and API changes.
adr/​037-regional-proxy-warming.md Updates the origin-tier design reference.

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

Comment thread crates/cache/src/origin_range.rs Resolved
Comment thread crates/cache/src/origin/mod.rs Resolved
@github-actions

github-actions Bot commented Sep 19, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 83.16% (was 83.03%, ▲ +0.13%) — 81687/98233 lines

File Coverage Lines Δ
crates/cache/src/engine.rs 93.16% 5219/5602 ▲ +0.11%
crates/cache/src/lib.rs 100.00% 28/28 ● 0.00%
crates/cache/src/origin/fs.rs 80.11% 576/719 ▲ +1.75%
crates/cache/src/origin/http.rs 89.02% 454/510 ▲ +1.54%
crates/cache/src/origin/mod.rs 63.64% 70/110 ▼ 2.44%
crates/cache/src/origin/s3.rs 89.97% 520/578 ▲ +0.60%
crates/cache/src/origin_range.rs 88.43% 367/415 new
crates/e2e/src/node.rs 30.62% 192/627 ● 0.00%
crates/node/src/handlers/client/dispatch.rs 80.97% 515/636 ● 0.00%
crates/node/src/handlers/client/window.rs 70.24% 295/420 ● 0.00%
crates/node/src/node_origin/backend_source.rs 90.16% 330/366 ▲ +10.55%
crates/node/src/node_origin/pull_leg.rs 86.38% 755/874 ▲ +1.74%
History
Commit Total Δ vs base
72839c3 83.07% ▲ +0.04%
17a73f5 83.08% ▲ +0.06%
5bd16ed (HEAD) 83.16% ▲ +0.13%

thiras and others added 2 commits September 19, 2026 20:33
Flow A now checks each window it fetches for exact length and parks
VerifyFailed, matching encode_verified_range. The Origin docs say a
range pull needs both fetch_range_data and fetch_outboard.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
- OriginRangeWire ends a faulted wire with one terminal Err from
  next_chunk; a guard parks an OriginError when the encode task panics
  or is cancelled, so a truncated wire never reads as a clean end.
- Only hash mismatches map to VerifyFailed; other encode errors are
  OriginError, and reader invariant breaks are local Store faults.
- The cursor walks typed windows (Window / Declined / WrongLength) and
  checks length in one place.
- pull_through_range and origin_range_wire draw from separate permit
  pools, so a long range pull cannot stall a committed own-origin serve;
  a wait for a permit is logged.
- Per-origin range faults are logged; origin_range_wire advances past a
  transport fault instead of stopping the chain.
- The async encoder skips the empty range (0-byte blob); it trips a
  bao-tree debug assertion there.
- BackendReader::read_bytes fills across chunks (the decoder reads each
  leaf with one read_bytes_exact), keeps the terminal fault, and errors
  instead of skipping an impossible copy.
- Tests: permit release on drop, separate pools, mid-stream decline /
  transport fault / panic, multi-origin fallback, empty blob, partial
  degrade then whole-blob fill, metering, window straddle, fine-chunked
  wire, mid-stream local pull leg fault.
- Docs: Origin trait, engine, CHANGELOG (BREAKING entry), ADR 037.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@thiras
thiras merged commit c4f5ab7 into main Sep 19, 2026
25 checks passed
@thiras
thiras deleted the fix/2065-window-range-pull branch September 19, 2026 18:20
yigitdot added a commit that referenced this pull request Sep 19, 2026
…stream-bounded-miss

Keeps main's streaming, window-bounded origin_range_wire / OriginRangeCursor
as the own-origin pull leg and drops this branch's buffered
origin_encode_range. The buffered pull_through_range tier that #2066
windowed is deleted again: with every miss routed through the two-leg
spine it has no caller, so its RangePullOutcome, second semaphore and
tests go with it. The cursor now takes the outboard dispatch already
probed instead of fetching it on every draw, so a fill reads {H}.obao4
once (own_origin_miss_fetches_the_outboard_once_across_draws holds
against the windowed reader).

Co-Authored-By: Claude Opus 5 <[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.

fix(cache): origin range pull buffers the whole span in memory — OOM on multi-GB ranges

2 participants