Skip to content

feat(gateway): cache what pulls deliver, and one cache store for node and gateway - #2908

Merged
alpergundogdu merged 7 commits into
mainfrom
gateway-cache-follow-ups
Nov 16, 2026
Merged

alpergundogdu merged 7 commits into
mainfrom
gateway-cache-follow-ups

Conversation

@alpergundogdu

@alpergundogdu alpergundogdu commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

The last step of #2881's plan for the gateway. decdn gateway keeps what it pulls in a decdn-cache CacheEngine and serves repeat requests from it, and the node and the gateway share one cache-backed store. It builds on #2884, #2886, #2887 and #2903, all merged. It combines #2905, #2906 and #2907, which are closed in favor of this PR, plus their follow-ups.

What changes

Gateway cache

  • <data-dir>/cache holds the cache. --cache-mb (default 10240) sets its size, and --cache-mb 0 turns it off. The node's eviction loop runs on it, with the node's default marks and free-disk margin. On exit the gateway stops the loop, waits for it to end, then shuts the cache down.
  • Every gateway pull streams into it: a Streamer over the CacheStores opener. decdn get keeps its scratch directory.
  • A request is a hit when the cache holds every byte of its response at the size the response states: the whole blob, or the chunk groups of its one Range. A hit is served from the cache with the same headers a pull gets. It starts no pull, spends no capability and charges no allowance, so it is served even when the allowance is spent. A loopback page that no capability covers still gets its 402, even for a hit: that refusal is how a page learns to register a capability, and both SDK contract suites rely on it. Blobs, bundle entries and a site's index.html can all hit. A blob the cache holds whole answers HEAD with its length.
  • A manifest the cache holds whole is read from it with no pull.
  • A request the cache holds only in part streams as a pull. The pull fetches only the missing chunk groups, and the allowance charges the response as a pull.
  • A hit counts as a use for LRU. No entry a pull is filling or a hit is reading is released: see InUse below.
  • New counter: decdn_gateway_cache_hits_total, also in /_status and on the dashboard.
  • An entry whose declared size passes --max-blob-mb answers 413 before any pull, as a raw blob already did.

One cache store, with the pull-side contract in decdn-bao-range

  • IngestStore, StreamStore, BaoRangeReader, StashedFault, IngestEnd, IngestFuture, SourceFuture, HashMismatch and ProvenSize move from decdn-client to decdn-bao-range, beside RangedStore. decdn-client re-exports each one at its old path, so the public_api snapshot changes only in paths. ProvenSize::verify is the only way to make a ProvenSize. decdn-bao-range gains anyhow, iroh-io and tokio; none of them is the blob store.
  • decdn_cache::CacheStore implements the contract once, over admit_bao_stream_until. It replaces the node's NodeAdmitStore and the gateway's own copy. Two parts are optional: with_session (proof-node capture for a serving leg) and on_admitted (the node's bytes-received count).
  • CacheEngine::hold_in_use returns an InUse guard. While any guard on a hash lives, eviction skips the hash and refuses to release it. A CacheStore holds its entry for its lifetime, so GC never sweeps an entry under a fill. Such a sweep silently loses the fill's later writes (client: re-evaluate running decdn-client on the iroh-blobs store (0.104.1) #2881, upstream FsStore: deleting a loaded partial entry leaves its .bitfield file behind n0-computer/iroh-blobs#350).

Fix in decdn-client

  • contiguous_byte_ranges ends a span that has no end at the blob's size. Before, it dropped that span. The cache's bitfield gives the last span an open end, so a stream reading the cache stopped one span short.

Dependency check and docs

  • The CLI may link iroh-blobs through decdn-cache without its s3 feature. The client SDK and sponsord still may not, and nothing may link the AWS SDK. The workspace takes decdn-cache without default features, and the node turns s3 back on.
  • Updated: the check script and its tests, CLAUDE.md, adr/appendix-gateway.md § Cache, adr/appendix-binaries.md, ADR 038, the Docker README, and the gateway module doc.

Tests

  • decdn-cache store tests cover:
    • a drained leg, with its progress and the reader wake-up;
    • a stop at a group boundary;
    • an interior range, and the bytes-admitted count;
    • a fault parked after the last byte, which beats a clean admit;
    • corrupt bytes, which give HashMismatch;
    • proof-node capture;
    • a live store holding its entry against eviction.
  • Node: the two fault-verdict tests on CacheStore.
  • Gateway tests:
    • a cached blob or Range is served with no pull and no allowance charge;
    • a partial entry is no hit;
    • a spent allowance still serves a cached blob and a cached site;
    • a loopback page with no capability gets 402 even for a cached blob;
    • an entry past the cap answers 413 and pulls only the manifest.
  • A Streamer over the cache reads back a 1.5 MB blob.
  • driver: an open span runs to the blob's end.
  • anvil-e2e: a new journey checks that a repeat GET and a Range come from the cache, with no node bytes and no allowance charge. The existing journeys run with --cache-mb 0, so their accounting is unchanged.
  • Locally: the affected crates' suites, the gateway e2e journeys, cargo clippy --workspace --all-targets --all-features with warnings denied, cargo doc, check_crate_edges.py and its pytest suite, cargo fmt --check, and the dashboard's tests, typecheck and lint.

Not covered

A blob released before a request arrives, and not yet swept by GC, still reads as present, and a hit on it can lose its bytes mid-read. The hold stops new releases; it does not re-protect a blob already released. The window lasts at most one GC interval (300 s by default).

🤖 Generated with Claude Code

alpergundogdu and others added 5 commits November 16, 2026 11:50
decdn gateway keeps a decdn-cache CacheEngine under <data-dir>/cache
(--cache-mb, default 10240; 0 turns it off) with the node's eviction
loop on it. Every pull streams into it through CacheStores, a
StreamStore over admit_bao_stream_until. A request whose bytes the
cache holds, whole or its one Range, is served from it with no pull
and no allowance charge, and counts in decdn_gateway_cache_hits_total.

The CLI may now link iroh-blobs (decdn-cache without its s3 feature);
the client SDK and sponsord still may not. contiguous_byte_ranges ends
an open span at the blob's size, which the cache's bitfield needs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The pointer journey runs its gateway with --cache-mb 0, since it counts
a repeat request against the namespace allowance.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…apability

A spent allowance or a page no capability covers now answers from the
cache when it holds every byte of the response, since a hit costs
nothing. Manifests the cache holds whole are read from it with no pull,
and a site's index.html checks the cache as entries do.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ange

IngestStore, StreamStore, BaoRangeReader, StashedFault, IngestEnd,
IngestFuture, SourceFuture, HashMismatch and ProvenSize move from
decdn-client to decdn-bao-range, beside RangedStore. decdn-client
re-exports each at its old path. decdn_cache::CacheStore implements
the contract once and replaces the node's NodeAdmitStore and the
gateway's own store; the gateway keeps only its StreamStores opener.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…in use

An entry whose declared size passes --max-blob-mb answers 413 before
any pull, as a raw blob does. CacheEngine::hold_in_use returns an InUse
guard that keeps a hash out of capacity eviction; a CacheStore holds
its entry while it lives, and a gateway cache hit holds it while it
reads and counts as a use for LRU.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-code-quality

github-code-quality Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Code coverage overview

Languages: Rust

Rust / code-coverage/cargo-llvm-cov

main 1410068 gateway-cache-follow... bab6b46 +/-
77% 77% 0%
View coverage of the 10 most impacted files.
File main 1410068 gateway-cache-follow... bab6b46 +/-
crates/cli/src/...ands/gateway.rs 81% 80% -1%
crates/cli/src/...mmands/fetch.rs 47% 46% -1%
crates/client/src/lib.rs 90% 89% -1%
crates/client/s...undle/engine.rs 66% 65% -1%
crates/client/src/scheduler.rs 94% 94% 0%
crates/client/src/source.rs 94% 94% 0%
crates/node/src/dht/publish.rs 77% 78% +1%
crates/bao-range/src/ingest.rs 0% 80% +80%
crates/cache/sr.../cache_store.rs 0% 87% +87%
crates/cli/src/.../cache_store.rs 0% 100% +100%

ℹ️ Calculations represent line coverage. Results may change as calculations are updated.


Updated November 16, 2026 17:22 UTC

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.

Note

Copilot was unable to complete this review at the selected tier due to model policy or plan restrictions.

🟡 Changes recommended

A misleading comment remains in crates/node/Cargo.toml referencing the removed NodeAdmitStore module instead of the current decdn_cache::CacheStore usage, so the change needs an update to prevent future dependency confusion.

1 open issue
What changed in this PR

Implements persistent gateway caching backed by decdn-cache::CacheEngine, serves repeat requests as cache hits without pulls/allowance charges, and unifies node + gateway pull-ingest storage via a single CacheStore contract moved into decdn-bao-range.

Changes:

  • Add on-disk gateway cache (<data-dir>/cache) with --cache-mb sizing/disable, eviction-loop lifecycle, cache-hit serving, and cache-hit metrics/status/dashboard wiring.
  • Move pull-side ingest/store contract types from decdn-client to decdn-bao-range (with decdn-client re-exports) and implement once as decdn_cache::CacheStore.
  • Fix contiguous_byte_ranges so open-ended spans correctly extend to blob end; update dependency-edge checks and documentation accordingly.
File Description
docker/​README.decdn.md Updates Docker documentation to describe gateway cache behavior/configuration and related operational notes.
crates/​node/​src/​node_origin/​store_fault_tests.rs Adds/updates node origin tests validating fault handling when using the new cache-backed store path.
crates/​node/​src/​node_origin/​pull_leg.rs Refactors the node pull leg to ingest into the unified CacheStore and adjust associated pull-leg wiring.
crates/​node/​src/​node_origin/​mod.rs Updates node origin module structure/exports to align with CacheStore usage and related helpers/tests.
crates/​node/​src/​node_origin/​backend_source/​tests.rs Adjusts backend source tests to operate with the new cache-backed ingest/store contract.
crates/​node/​src/​node_origin/​backend_source.rs Updates backend source implementation and docs to use the unified CacheStore-based ingest path.
crates/​node/​src/​node_origin/​admit_store/​tests.rs Removes or updates obsolete tests tied to the prior admit-store implementation superseded by CacheStore.
crates/​node/​src/​node_origin/​admit_store.rs Removes the legacy admit-store implementation now replaced by decdn_cache::CacheStore.
crates/​node/​Cargo.toml Updates node crate dependencies/features to support the new cache store and feature gating (including S3 where needed).
crates/​e2e/​tests/​gateway_public.rs Adds/updates gateway public e2e coverage to verify cache-hit behavior on repeat requests and ranges.
crates/​e2e/​tests/​gateway_pointer.rs Adjusts pointer e2e coverage to keep accounting stable by controlling cache configuration during runs.
crates/​common/​src/​cli/​gateway/​tests.rs Adds tests for gateway CLI flag parsing/behavior around cache sizing and disabling.
crates/​common/​src/​cli/​gateway.rs Introduces gateway CLI options for cache sizing/disable and documents their semantics in help text.
crates/​client/​tests/​snapshots/​public_api__public_api.snap Updates the client public API snapshot to reflect moved/re-exported ingest contract and size-proof types.
crates/​client/​src/​streamer.rs Adapts streamer plumbing to use the ingest/store contract types now hosted in decdn-bao-range.
crates/​client/​src/​source.rs Refactors client source code to use/re-export the relocated ingest contract and related futures/types.
crates/​client/​src/​sink.rs Updates client sink code to reference the relocated fault/type definitions via decdn-bao-range re-exports.
crates/​client/​src/​lib.rs Adjusts client crate exports and size-proof verification usage to the new ProvenSize API location/constructor.
crates/​client/​src/​driver/​tests.rs Adds regression tests covering contiguous range construction when the final span has an open end.
crates/​client/​src/​driver.rs Fixes contiguous_byte_ranges so an open-ended span correctly terminates at the blob’s total size.
crates/​client/​Cargo.toml Updates client crate features/dev-deps to align tests and helpers with the moved decdn-bao-range modules.
crates/​cli/​src/​commands/​gateway/​worker.rs Threads cache-backed store plumbing into the gateway worker so streamed pulls can fill the gateway cache.
crates/​cli/​src/​commands/​gateway/​tests.rs Adds gateway command tests validating cache-hit behavior, allowance/capability bypass on hits, and size-cap handling.
crates/​cli/​src/​commands/​gateway/​status/​tests.rs Updates status endpoint tests/fixtures to include cache-hit counters in the reported status model.
crates/​cli/​src/​commands/​gateway/​metrics/​tests.rs Adds metrics tests to validate cache-hit series exposure and expected counter behavior.
crates/​cli/​src/​commands/​gateway/​metrics.rs Implements cache-hit counter reporting for metrics and status integration.
crates/​cli/​src/​commands/​gateway.rs Implements the gateway cache lifecycle and cache-hit serving path, including hit accounting and cache-backed reads.
crates/​cli/​src/​commands/​fetch/​cache_store/​tests.rs Adds tests ensuring streamed fetches can fill and then read back data via cache-backed stream stores.
crates/​cli/​src/​commands/​fetch/​cache_store.rs Introduces cache-backed StreamStore adapters (CacheStores) for gateway/cache-integrated streaming fetches.
crates/​cli/​src/​commands/​fetch.rs Extends fetch command logic to optionally stream through cache-backed stores rather than scratch-only storage.
crates/​cli/​README.md Updates CLI documentation around dependency constraints and cache-related behavior/footprint.
crates/​cli/​dashboard/​src/​view.test.ts Updates dashboard view tests to reflect the new cache-hit counter in status/metrics data.
crates/​cli/​dashboard/​src/​fixtures/​status-public.json Refreshes dashboard fixtures to include cache-hit counters in the public status shape.
crates/​cli/​dashboard/​src/​fixtures/​status-pool-error.json Refreshes dashboard fixtures to include cache-hit counters in the pool-error status shape.
crates/​cli/​dashboard/​src/​fixtures/​status-loopback.json Refreshes dashboard fixtures to include cache-hit counters in the loopback status shape.
crates/​cli/​dashboard/​src/​contract.test.ts Updates dashboard contract tests to require/validate the presence of the cache-hit counter field.
crates/​cli/​dashboard/​src/​components/​StatTiles.tsx Adds a dashboard stat tile for cache hits and integrates it into the existing tile layout.
crates/​cli/​dashboard/​src/​components/​StatTiles.test.ts Adds/updates component tests to validate rendering and labeling of the new cache hits stat tile.
crates/​cli/​dashboard/​src/​api.ts Extends dashboard API typings/models to include the new cache-hit counter in counters payloads.
crates/​cli/​Cargo.toml Updates CLI crate dependencies/features to include cache support while preserving dependency/feature constraints.
crates/​cache/​src/​ranged_store.rs Updates ranged store docs/implementation references to the new shared cache-store ingest contract location.
crates/​cache/​src/​lib.rs Exports cache-store modules/types needed by node/gateway, including the unified CacheStore and in-use guard types.
crates/​cache/​src/​engine.rs Adds/extends in-use tracking to prevent eviction/release while entries are actively read or being filled.
crates/​cache/​src/​cache_store/​tests.rs Adds comprehensive tests for CacheStore ingest semantics, faults, progress, and eviction pinning behavior.
crates/​cache/​src/​cache_store.rs Implements the unified CacheStore over CacheEngine to satisfy the moved decdn-bao-range ingest contract.
crates/​bao-range/​src/​size_proof.rs Moves/defines ProvenSize in decdn-bao-range with verified construction semantics.
crates/​bao-range/​src/​lib.rs Exposes the ingest module and re-exports moved contract types so downstream crates can depend on stable paths.
crates/​bao-range/​src/​ingest.rs Introduces the pull-side ingest/store contract types and related futures/errors in their new crate home.
crates/​bao-range/​Cargo.toml Adds required dependencies/features for the new ingest contract module and its async IO needs.
CLAUDE.md Updates repository guidance/docs to reflect the new cache/store architecture and dependency constraints.
Cargo.toml Adjusts workspace dependency feature defaults to keep decdn-cache store-only by default and control feature propagation.
Cargo.lock Updates lockfile to reflect new/shifted dependencies introduced by the cache/store contract refactor.
adr/​appendix-gateway.md Updates gateway ADR appendix with detailed cache behavior, hit semantics, and operational notes.
adr/​appendix-binaries.md Updates binary/dependency footprint documentation to reflect the new cache linkage and constraints.
adr/​038-bao-verified-range-streaming.md Updates the ADR to align terminology and module boundaries with the refactored ingest/store contract layout.
.github/​scripts/​tests/​test_check_crate_edges.py Updates crate-edge enforcement tests to match revised dependency allowances and exceptions.
.github/​scripts/​check_crate_edges.py Updates the dependency-edge checking script to support new cache-related dependency rules and documented exceptions.

Review effort: Balanced

Comment thread crates/node/Cargo.toml Outdated
Comment on lines 76 to 77
# `NodeAdmitStore` (`src/node_origin/admit_store.rs`, #1621) implements
# `decdn_bao_range::RangedStore` directly (delegating to `NodeRangedStore`), so

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rewrote both crates/node/Cargo.toml comments to say what the node names from decdn-bao-range and bao-tree today (the pull-side contract CacheStore implements, and the encoder types), and fixed the same stale mention in crates/cache/src/ranged_store/tests.rs.

🤖 Addressed by Claude Code

alpergundogdu and others added 2 commits November 16, 2026 17:10
…he hit

The loopback refusal is how a page learns to register a capability, and
both SDK contract suites pin it. A spent public allowance still serves
a hit.

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

2 participants