Repository navigation
feat(node): wire live DHT prefetch acquisition + ledger feedback (#820) - #899
Conversation
Closes AC 7 of #650. PR #819 landed the prefetch decision engine but it only decided and metered. This wires the live acquisition on top: - On a `PrefetchDecision::Acquire`, the DHT handler fires a bounded, non-blocking background acquisition (`PrefetchAcquirer`) that drives `cache.populate` → the existing NodeOrigin pull-through (discover → probe → paid pull → ingest). Concurrency-capped (semaphore), per-hash deduped, cancelled on shutdown. - Spend feeds back via a new `AcquisitionObserver` on `NodeOriginDeps`: it fires on every pull that spent (success OR paid-but-failed, whose voucher watermark advanced #852) and records per-pull spend/bytes only for prefetch-initiated pulls (gated by the in-flight set), so demand-miss pulls never charge the prefetch budget. `decdn_prefetch_spend_usdc_total` now non-zero. - A bounded, TTL'd `PrefetchAcquiredSet` tags prefetch content; the client serve path credits `record_served` for those blobs, feeding the demand-quality auto-throttle. - Two new `[prefetch]` config knobs: `max_concurrent_acquisitions` (4) and `acquisition_timeout_secs` (30). New acquisition-outcome metrics. Note: the issue's "OriginAssignment subscription" task was already shipped by #830 (`ChainOriginDirectory`), so only acquisition + ledger feedback remained. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request implements live speculative-prefetch acquisition (#820), enabling nodes to speculatively acquire content on demand signals. It introduces a bounded, TTL-expired PrefetchAcquiredSet to track prefetch-acquired hashes, a PrefetchAcquirer to spawn bounded background pulls, and an InflightSet to deduplicate active acquisitions. It also wires up a PrefetchAcquisitionObserver to feed real spend data back into the prefetch ledger and updates the client handler to credit served bytes to the demand-quality auto-throttle. Feedback on the changes suggests improving the Drop implementation of InflightGuard to handle poisoned locks using .unwrap_or_else(std::sync::PoisonError::into_inner) to guarantee that cleanup logic executes.
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.
There was a problem hiding this comment.
Pull request overview
This PR completes the “live” side of speculative DHT prefetch in decdn-node by wiring real cache acquisition on PrefetchDecision::Acquire and feeding acquisition/serve outcomes back into the prefetch policy ledgers (budget + demand-quality throttle), along with new config knobs and observability.
Changes:
- Wire bounded, deduped background acquisition (
cache.populate) for prefetch decisions and cancel it on shutdown. - Add feedback loops: charge prefetch ledger spend only for prefetch-initiated pulls (via
AcquisitionObserver) and credit served bytes for prefetch-acquired blobs (via an acquired-hash TTL set). - Extend config/CLI/docs/metrics and add/extend tests covering acquisition, spend isolation, and acquired-set behavior.
Reviewed changes
Copilot reviewed 15 out of 15 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| crates/node/tests/node_origin_pull.rs | Adds an end-to-end prefetch acquisition + spend/metrics assertion test. |
| crates/node/src/runtime/mod.rs | Wires engine into DHT handler + client serve path; provisions acquirer + observer; adds shutdown cancellation. |
| crates/node/src/prefetch/mod.rs | Extends engine with acquirer, acquired-set, in-flight tracking, and an acquisition observer bridge. |
| crates/node/src/prefetch/decision.rs | Updates docs and test config literals for new prefetch knobs. |
| crates/node/src/prefetch/acquirer.rs | Introduces bounded background acquisition implementation (semaphore + in-flight dedupe). |
| crates/node/src/prefetch/acquired.rs | Adds TTL’d, bounded set to tag prefetch-acquired hashes for serve attribution. |
| crates/node/src/node_origin.rs | Adds AcquisitionObserver trait + wiring to report per-pull spend/bytes deltas to prefetch ledger. |
| crates/node/src/metrics.rs | Adds prefetch acquisition outcome counters and spend increment helper. |
| crates/node/src/handlers/dht.rs | Fires engine.try_acquire on Acquire decisions. |
| crates/node/src/handlers/client.rs | Credits served bytes to demand-quality numerator for prefetch-acquired blobs. |
| crates/common/src/config/types.rs | Adds [prefetch] knobs: max_concurrent_acquisitions, acquisition_timeout_secs. |
| crates/common/src/config/resolved.rs | Plumbs resolved fields and defaults for the new knobs. |
| crates/common/src/config/mod.rs | Adds defaults + validation and resolves new prefetch fields. |
| crates/cli/src/commands/config.rs | Updates config validate summary and default config template for new knobs. |
| adr/appendix-observability.md | Documents new prefetch acquisition metrics. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Coverage reportTotal: 85.99% (was 86.01%, ▼ 0.02%) — 35896/41745 lines
History
|
- InflightGuard::drop now recovers a poisoned lock (PoisonError::into_inner) so the RAII cleanup always runs (gemini) — leaving a hash armed would wedge dedupe + the observer gate. - InflightSet::arm logs on a poisoned lock, matching PrefetchAcquiredSet::insert, so a dropped acquisition is diagnosable (copilot). - Corrected prefetch_spend_usdc + add_prefetch_spend docs: spend is charged on any prefetch-initiated paid pull that acked vouchers (success OR paid-but- failed), not only successes (copilot). - Raised the e2e poll budget (35s) above the acquirer's 30s deadline so a slow CI pull is not declared a failure prematurely (copilot). Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Resolve conflicts from #899 (#820 prefetch ledger) landing alongside the #856 window-paced pull-through work: - node_origin.rs: keep the PR's persist_buyer_progress() helper refactor for the buyer voucher-watermark write AND main's #820 acquisition-observer block that surfaces per-pull spend/bytes to the prefetch ledger. - handlers/client.rs: keep both additive sets of post-construction fields (window pull-through: pull_through_origin/pull_ahead_bytes/leech_governor; prefetch: prefetch_engine) and both serve-path credits (leech_governor record_served + prefetch note_served_if_prefetched). - tests/node_origin_pull.rs: drop the duplicate decdn_cache::Hash import (main folds it into {CacheEngine, Hash}) and the duplicate counter_value helper (keep main's absent-counter-as-0 variant; satisfies both branches' callers). Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Summary
Closes AC 7 of #650 (issue #820). PR #819 landed the prefetch decision engine (popularity oracle, gates, config, metrics) behind
prefetch.enabled = false, but it only decided and metered. This PR wires the live acquisition and feeds results back into the policy ledgers.Scope note: the issue listed three tasks, but the OriginAssignment event subscription was already shipped by #830 (
ChainOriginDirectory, already wired into the prefetch engine). So only two tasks remained — acquisition + ledger feedback.What's included
PrefetchDecision::Acquire,DhtHandler::run_prefetchcallsengine.try_acquire(hash)which spawns a bounded, non-blocking background task (PrefetchAcquirer) that runscache.populate(hash)— reusing the existing NodeOrigin pull-through (DHTfind_providers→cdn/probe/v1→cdn/client/v1paid pull → cache ingest). Capped by a semaphore (max_concurrent_acquisitions), per-hash deduped via an RAII in-flight guard, and cancelled on shutdown.AcquisitionObserveronNodeOriginDepsfires on every pull that spent (success or paid-but-failed, whose voucher watermark advanced per node: buyer voucher progress is never persisted (record_progress has zero production callers) — every reused buyer channel signs stale vouchers and repeat pulls fail #852) and records the per-pull spend/bytes delta only for prefetch-initiated pulls (gated by the in-flight set), so demand-miss pulls never charge the prefetch budget.decdn_prefetch_spend_usdc_totalbecomes non-zero.PrefetchAcquiredSettags prefetch-acquired hashes; the client serve path creditsrecord_servedfor those blobs, feeding the rolling demand-quality auto-throttle.[prefetch]knobs:max_concurrent_acquisitions(default 4) andacquisition_timeout_secs(default 30), across all config + CLI surfaces.decdn_prefetch_acquire_{succeeded,failed,timeout,dropped_saturated}_total; documented in appendix-observability. Decision-timeauthorized/bypassedmetering unchanged.Test plan
cargo clippy --workspace --all-targets -- -D warnings✓cargo fmt --check✓cargo nextest run --workspace→ all pass, incl:PrefetchAcquiredSet(TTL/eviction/poison),PrefetchAcquirer(no-op/dedupe), engine served/acquisition gating, and demand-miss spend isolation through the wired observer;prefetch_acquire_pulls_and_records_spend): a seller node holding the blob + a prefetch-enabled buyer; drives FIND_VALUE past threshold, awaits the real paid pull, asserts the blob is cached,decdn_prefetch_spend_usdc_totalnon-zero,acquire_succeeded == 1, and the acquired-set tag.find_value_feeds_prefetch_engine_and_metersdecision-time test stays green.Review
A 5-agent review (code/tests/errors/types/comments) ran on this branch; findings addressed in-tree before this PR — notably: budget cost-safety (no
u64::MAXbudget poisoning; paid-but-failed pulls now charged), stale#650comments removed, and the demand-miss spend-isolation test added.🤖 Generated with Claude Code