Skip to content

feat(node): wire live DHT prefetch acquisition + ledger feedback (#820) - #899

Merged
thiras merged 2 commits into
mainfrom
feat/820-prefetch-acquisition
Jun 14, 2026
Merged

thiras merged 2 commits into
mainfrom
feat/820-prefetch-acquisition

Conversation

@thiras

@thiras thiras commented Jun 14, 2026

Copy link
Copy Markdown
Contributor

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

  • Acquisition path — on a PrefetchDecision::Acquire, DhtHandler::run_prefetch calls engine.try_acquire(hash) which spawns a bounded, non-blocking background task (PrefetchAcquirer) that runs cache.populate(hash) — reusing the existing NodeOrigin pull-through (DHT find_providers → cdn/probe/v1 → cdn/client/v1 paid pull → cache ingest). Capped by a semaphore (max_concurrent_acquisitions), per-hash deduped via an RAII in-flight guard, and cancelled on shutdown.
  • Spend feedback — a new generic AcquisitionObserver on NodeOriginDeps fires 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_total becomes non-zero.
  • Served feedback — a bounded, TTL'd PrefetchAcquiredSet tags prefetch-acquired hashes; the client serve path credits record_served for those blobs, feeding the rolling demand-quality auto-throttle.
  • Config — two new [prefetch] knobs: max_concurrent_acquisitions (default 4) and acquisition_timeout_secs (default 30), across all config + CLI surfaces.
  • Metrics — decdn_prefetch_acquire_{succeeded,failed,timeout,dropped_saturated}_total; documented in appendix-observability. Decision-time authorized/bypassed metering unchanged.

Test plan

  • cargo clippy --workspace --all-targets -- -D warnings ✓
  • cargo fmt --check ✓
  • cargo nextest run --workspace → all pass, incl:
    • new unit tests for PrefetchAcquiredSet (TTL/eviction/poison), PrefetchAcquirer (no-op/dedupe), engine served/acquisition gating, and demand-miss spend isolation through the wired observer;
    • a full end-to-end test (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_total non-zero, acquire_succeeded == 1, and the acquired-set tag.
  • The existing find_value_feeds_prefetch_engine_and_meters decision-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::MAX budget poisoning; paid-but-failed pulls now charged), stale #650 comments removed, and the demand-miss spend-isolation test added.

🤖 Generated with Claude Code

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]>
Copilot AI review requested due to automatic review settings June 14, 2026 00:39

@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 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.

Comment thread crates/node/src/prefetch/acquirer.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 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.

Comment thread crates/node/src/node_origin.rs Resolved
Comment thread crates/node/src/metrics.rs Resolved
Comment thread crates/node/src/metrics.rs Resolved
Comment thread crates/node/tests/node_origin_pull.rs Resolved
Comment thread crates/node/src/prefetch/acquirer.rs Resolved
@github-actions

github-actions Bot commented Jun 14, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 85.99% (was 86.01%, ▼ 0.02%) — 35896/41745 lines

File Coverage Lines Δ
crates/cli/src/commands/config.rs 67.25% 154/229 ▼ 0.01%
crates/common/src/config/mod.rs 96.83% 6021/6218 ▲ +0.01%
crates/common/src/config/resolved.rs 100.00% 35/35 ● 0.00%
crates/node/src/handlers/client.rs 74.35% 568/764 ▼ 0.69%
crates/node/src/handlers/dht.rs 77.22% 322/417 ▲ +0.41%
crates/node/src/metrics.rs 83.59% 769/920 ▼ 0.72%
crates/node/src/node_origin.rs 90.33% 299/331 ▼ 0.75%
crates/node/src/prefetch/acquired.rs 96.10% 74/77 new
crates/node/src/prefetch/acquirer.rs 77.66% 73/94 new
crates/node/src/prefetch/decision.rs 96.18% 151/157 ▲ +0.69%
crates/node/src/prefetch/mod.rs 96.02% 169/176 ▲ +3.49%
crates/node/src/runtime/mod.rs 49.16% 878/1786 ▼ 0.36%
History
Commit Total Δ vs base
40a16dc 85.99% ▼ 0.02%
33d3aff (HEAD) 85.99% ▼ 0.02%

- 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]>
@thiras
thiras merged commit 1d92267 into main Jun 14, 2026
20 checks passed
@thiras
thiras deleted the feat/820-prefetch-acquisition branch June 14, 2026 01:38
thiras added a commit that referenced this pull request Jun 14, 2026
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]>
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