Repository navigation
feat(cache): wire iroh-blobs GC into runtime (#518) - #521
Conversation
Partial-import bytes from hash-mismatch and mid-stream cap-breach
pull-through paths previously leaked disk indefinitely (the runtime
never invoked iroh-blobs GC). A hostile origin streaming
`max_blob_size_mb - 1` of garbage and erroring on the last byte could
fill disk over a few thousand requests.
Wires iroh-blobs' built-in GC loop via `Options { gc: Some(GcConfig { ... }) }`
in `CacheEngine::open_full`. The `add_protected` callback is the
instrumentation hook: it snapshots the blob set per cycle, diffs against
the previous cycle's snapshot, and bumps `gc_runs_total` /
`gc_bytes_reclaimed_total`. Attribution lags one sweep cycle.
The cb captures `Weak<OnceLock<FsStore>>` (with the strong `Arc` held in
`Inner.gc_store_handle`) so engine drop releases the iroh-blobs runtime
cleanly — without this, the cb -> FsStore clone -> actor -> GC task -> cb
cycle would pin the runtime alive past engine drop.
New `cache.gc_interval_sec` config field, default 300s, `0` disables.
Manual on-demand `decdn admin cache gc` is deferred to #520:
`iroh_blobs::store::gc::gc_run_once` is `pub` inside a private `mod gc;`
and not re-exported, and `Blobs::delete` is `pub(crate)`. The only
externally-reachable trigger is `Options::gc`.
Tests (6 new, 689 total pass):
- gc_reclaims_partial_import_bytes: hash-mismatch threat model
- gc_reclaims_cap_breach_partial_bytes: BlobTooLarge threat model
- gc_disabled_does_not_reclaim_or_emit_metrics: ZERO interval branch
- gc_attribution_lags_one_cycle: locks in the off-by-one design
- resolve_cache_gc_interval_{defaults_when_absent,from_file_overrides_default,zero_disables}
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Coverage reportTotal: 88.28% (was 88.21%, ▲ +0.08%) — 12276/13905 lines
History
|
There was a problem hiding this comment.
Pull request overview
Wires iroh-blobs’ periodic garbage collection into the deCDN cache runtime and plumbs a new cache.gc_interval_sec configuration setting through config resolution, runtime cache construction, and CLI config output, with accompanying tests and new cache GC metrics.
Changes:
- Add
cache.gc_interval_sec(default 300s,0disables) and thread it through config resolution intoCacheEngine::open_full. - Integrate iroh-blobs GC via
Options { gc: Some(GcConfig { ... }) }and instrument per-cycle GC metrics. - Add tests covering partial-import byte reclamation, disabled-GC behavior, and the “lagged by one cycle” attribution design.
Reviewed changes
Copilot reviewed 16 out of 16 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| crates/cache/src/engine.rs | Adds GC wiring into FsStore options, callback instrumentation, and new open_full parameter for GC interval. |
| crates/cache/src/metrics.rs | Introduces two new cache GC counters and updates metric documentation. |
| crates/cache/tests/pull_through.rs | Adds GC reclamation/attribution/disabled-branch tests and updates engine construction calls. |
| crates/cache/tests/s3_origin.rs | Updates engine construction calls to pass a GC interval argument. |
| crates/common/src/config/types.rs | Adds gc_interval_sec to TOML cache config schema. |
| crates/common/src/config/resolved.rs | Adds resolved gc_interval_sec to ResolvedCache. |
| crates/common/src/config/mod.rs | Adds DEFAULT_GC_INTERVAL_SEC and resolves gc_interval_sec from file/default. |
| crates/common/src/cli/node.rs | Updates decdn node evict CLI docs to reference periodic GC sweep behavior/config. |
| crates/common/src/admin.rs | Updates admin RPC docs to reference periodic GC sweep behavior. |
| crates/cli/src/commands/config.rs | Prints gc_interval_sec in config validation summary and documents it in the sample config. |
| crates/cli/tests/config_validate.rs | Updates resolved-config test fixture to include gc_interval_sec. |
| crates/node/src/runtime/mod.rs | Passes configured GC interval into CacheEngine::open_full when building cache. |
| crates/node/src/runtime/reload.rs | Updates resolved-config test fixtures to include gc_interval_sec. |
| crates/node/tests/sighup_signal.rs | Updates resolved-config test fixture to include gc_interval_sec. |
| crates/node/src/metrics.rs | Updates cache-engine construction in metrics tests to pass a GC interval. |
| crates/node/src/admin.rs | Updates test documentation comment to reference the correct tracking issue. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Code Review
This pull request implements periodic garbage collection for the iroh-blobs store to prevent disk leakage from partial imports and logical evictions. It introduces a configurable gc_interval_sec setting, wires the GC loop in the cache engine, and adds metrics to track GC runs and reclaimed bytes. Feedback was provided regarding the snapshot_blob_sizes function, which currently performs sequential RPC calls; parallelizing these calls is recommended to avoid performance bottlenecks in environments with a large number of blobs.
Address review feedback on #521: - iroh-metrics' OpenMetrics encoder appends `_total` to every Counter unconditionally (iroh-metrics-0.38.3/src/encoding.rs:490). Field names that already include `_total` get exported as `..._total_total`. Rename `gc_runs_total` -> `gc_runs` and `gc_bytes_reclaimed_total` -> `gc_bytes_reclaimed`. Exported names stay `decdn_cache_gc_runs_total` and `decdn_cache_gc_bytes_reclaimed_total`. (Copilot review.) - Reword `DEFAULT_GC_INTERVAL_SEC` doc: the previous "leak up to gc_interval_sec * max_blob_size_mb" formula was dimensionally wrong (seconds * MB). The actual bound is per-request: a single failed pull-through orphans up to `max_blob_size_mb`; a stream of failures inside one interval compounds as `requests_in_window * max_blob_size_mb`. (Copilot review.) Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Address second-round review feedback on #521: - code-reviewer: cycle-break docs overstated what `Weak<OnceLock<FsStore>>` achieves. iroh-blobs `run_gc(store: Store, ...)` owns its own `Store` clone for the lifetime of its loop, so the actor + runtime stay resident until process exit (or `gc_run_once` Err). The `Weak` ensures the cb body no-ops past engine drop and contributes no strong ref — it does not, on its own, shut the iroh-blobs runtime down. Reworded `Inner.gc_store_handle` and `gc_protect_callback` docs accordingly, with a forward reference to #520. Added an INVARIANT note that no `Arc::clone` of `gc_store_handle` may escape `Inner`. - comment-analyzer: the GC orphan-rate alert framing was wrong. `pull_through_bytes` is bumped per chunk regardless of mid-stream failure (#418), so in the hostile-origin scenario almost every byte pulled is later reclaimed by GC and the ratio approaches 1.0 — not "nonzero numerator vs zero denominator". Rewrote as a high-ratio alert. Standardized all four ratio formulas to use the encoder-suffixed exported names so operators can paste PromQL verbatim. - comment-analyzer: `gc_disabled_does_not_reclaim_or_emit_metrics` test doc referenced `if !gc_interval.is_zero()` but the actual code is `if gc_interval.is_zero() { None } else { Some(...) }`. Fixed the reference to match the source. - pr-test-analyzer (Critical): no test asserted the GC counters appear in the OpenMetrics scrape output with the encoder-appended `_total` suffix. The rename was specifically motivated by encoder behavior; a regression that re-renamed the field to include `_total` would emit `..._total_total` and break dashboards but pass every existing test. Added `decdn_cache_gc_runs_total` / `decdn_cache_gc_bytes_reclaimed_total` to `cache_metrics_counters_start_at_zero` (zero-state) and `cache_metrics_handle_shares_atomic_with_registered_group` (post-bump), locking in the round-trip both at construction and after metric writes. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Code reviewNo blocking issues. Two sub-threshold observations:
decdn/crates/cache/tests/pull_through.rs Lines 265 to 286 in 08de26e
decdn/crates/cache/src/engine.rs Lines 586 to 590 in 08de26e 🤖 Generated with Claude Code - If this code review was useful, please react with 👍. Otherwise, react with 👎. |
|
Thanks — verified both. tl;dr: (1) is real-but-negligible, (2) doesn't apply to the threat-model paths. (1) (2) Neither path produces a
|
Summary
Closes #518 (the periodic-sweep + metrics half). #520 tracks the
manual on-demand admin RPC, blocked on upstream iroh-blobs exposing
gc_run_once.After #271's streaming refactor, every mid-stream pull-through failure
(hash mismatch, cap breach, idle timeout, decoder failure) committed
partial bytes to iroh-blobs and dropped the
TempTagwithoutpromotion. Without GC running, those bytes leaked for the lifetime of
the process and across restarts (
FsStorepersists). A hostile originstreaming
max_blob_size_mb - 1of garbage and erroring on the lastbyte could fill the disk over a few thousand requests.
This wires iroh-blobs' built-in GC loop into the runtime via
Options { gc: Some(GcConfig { ... }) }and instruments each cycle viathe
add_protectedcallback hook.Design
cache.gc_interval_secconfig field, default300s,
0disables. Threaded throughResolvedCache->build_cache->
CacheEngine::open_full(gc_interval: Duration).CacheMetrics:gc_runs_total— bumped per cycle once the pre-sweep snapshotsucceeds.
gc_bytes_reclaimed_total— diff of the previous cycle's pre-sweepsnapshot vs the current cycle's. Hashes that disappeared between
those two snapshots are exactly what the previous sweep deleted
(the cache crate has no other public delete path). Attribution
lags one sweep cycle: the first cycle records a baseline and
bumps zero bytes.
Inner.gc_store_handle: Option<Arc<OnceLock<FsStore>>>holds the strong
Arc; the cb closure capturesWeak. On enginedrop the strong
Arcdrops, the cb'sWeak::upgradereturnsNone,the iroh-blobs actor's last sender drops, the runtime is aborted.
Without this, the cb ->
FsStoreclone -> actor -> GC task -> cbcycle would pin the iroh-blobs runtime alive past engine drop.
Send + Syncdance: iroh-blobs requires theProtectCbfutureto be
Send + Sync, but its own RPC layer (blobs().list().stream(),blobs().status(...)) returns futures that are onlySend. The cbspawns the snapshot work on a tokio task and awaits a
oneshot::Receiver(which isSend + Sync) so the outer futurematches iroh-blobs' demanded shape.
Why not a manual admin RPC
iroh_blobs::store::gc::gc_run_onceispubinside a privatemod gc;and not re-exported.
Blobs::deleteandclear_protectedarepub(crate). The only externally-reachable GC trigger isOptions.gc.#520 tracks the on-demand path, blocked on upstream exposing the sweep
API. The disk-leak threat #518 was filed for is fully addressed by the
periodic sweep — the manual trigger is operator ergonomics.
Test plan
cargo clippy --workspace --all-targets -- -D warningscleancargo fmt --checkcleancargo nextest run --workspacepasses (689 tests, 6 new)gc_reclaims_partial_import_bytes— hash-mismatch threat modelgc_reclaims_cap_breach_partial_bytes—BlobTooLargethreat modelgc_disabled_does_not_reclaim_or_emit_metrics—ZERO-interval branchgc_attribution_lags_one_cycle— locks in the off-by-one designresolve_cache_gc_interval_{defaults_when_absent,from_file_overrides_default,zero_disables}— config plumbing🤖 Generated with Claude Code