Skip to content

B3: multi-observer coalescing (one upstream pull → N paying serve legs) #1656

Closed

Description

@alpergundogdu

B3 — multi-observer coalescing (one upstream pull → N paying serve legs)

Remaining Phase B work after Flow A (#1655). Flow A unified the node serve-miss onto a single two-leg spine (run_pull_leg / run_local_pull_leg + serve_leg) for both the node→node peer source and the node's own origin. B3 builds on that unified pull-leg abstraction.

Goal: let one upstream pull feed N downstream clients that want overlapping bytes, each paying its own egress — removing the redundant second upstream pull when requested ranges overlap. Nothing today fans a live pull out to N payers: the cache-engine inflight map only dedups the origin fetch (a 2nd miss waits on a Notify then reads the finished fill — sequential, engine.rs).

Scope boundary — coalescing is for OVERLAP, not disjoint requests

Two clients wanting different halves are already handled without coalescing: disjoint ranges are independent gaps, so each client is its own serve leg with its own pull for the half it needs (the gap-driven driver already opens exactly the disjoint gaps). Payment is per (payer, payee) channel and cumulative — a voucher is signed by that channel's authorized signer and cannot be shared, so two different clients inherently need their own channels with the serving node. B3 only removes the redundant second upstream pull when the requested ranges overlap.

Design decisions (locked in the plan)

  1. Two-tier pacing (ratified with incentive). The shared PULL leg is bounded by the node's own ingest budget (max_unrecouped_leech_bytes node-wide + pull_ahead_bytes per-pull, ADR 037), NOT any single client's payment. Each SERVE leg keeps its own per-channel credit window (delivered − paid ≤ credit_window). Abandoned-pull exposure = the leech budget — exactly what max_unrecouped_leech_bytes already governs. So B3 switches the shared pull's WindowPacer bound from "one client's cleared payment" to "the node's leech budget" (LeechGovernor); each serve leg's per-channel pacing is unchanged.
  2. Manager-level, RANGE-AWARE in-flight-pull registry (in CacheEngine). Key is the hash, but each entry tracks the range(s) an in-flight pull will produce: hash → Vec<InFlightPull { covered: ChunkRanges, serve_watch, leg handle }>. On a serve-miss for range R: attach = R ∩ (union of in-flight covered), remainder = R − (union covered). Attach as an observer for attach (a serve leg over the shared store, no new upstream pull); open a fresh pull leg only for remainder. A hash-coarse "attach if any pull exists" registry is WRONG for disjoint ranges — a client wanting [half, end) must not attach to an in-flight [0, half) pull (it would await bytes that pull never fetches). Extends the existing inflight: Mutex<HashMap<Hash, ...>> from coarse Notify-coalescing to a set of progressive, range-scoped shared watches.
  3. Per-observer serve leg. Each downstream client runs observe → read landed ranges → encode_range → QUIC, metering its OWN egress via its OWN ChannelDeliveryState/voucher collection. N serve legs, one pull leg. #1610 holds: the pull is always behind ≥1 waiting client; when the last observer leaves before completion, the pull leg stops (no orphan ingest) — the B0-tagged partial persists for a later resume.
  4. Pull-failure propagation. Observers awaiting un-landed ranges must get a terminal signal, not hang. The ServeWatch gains an explicit "pull failed/stopped" terminal state; each serve leg surfaces it as a per-client serve error. Generalizes the merged single-observer pull_ended/pull_result termination to N observers.
  5. Eviction under in-flight leases. Pull + observers hold leases; capacity/takedown evict stays logical + deferred until leases drop. Confirm the B0 partial-tag + lease interaction (evict drops the tag, GC reclaims after the last reader).
  6. Incentive sign-off gate. Before shipping, pin with the incentive layer: (a) the pull leg's leech-budget accounting (one pull, node-owned exposure), (b) that N independent per-channel ledgers metering egress off one shared store is sound (each client signs cumulative vouchers over bytes IT received — no cross-client leakage), (c) abandoned-pull exposure equals the leech budget and nothing more.

Tasks (shape)

  • B3.1 — ServeWatch terminal-state (pull-failed) + per-observer serve-leg abstraction (generalize the merged single-observer serve_leg to attach to a shared watch).
  • B3.2 — range-aware in-flight-pull registry in CacheEngine (attach = R ∩ covered, remainder = R − covered; attach-observer for the overlap + fresh pull for the remainder). Tests: (a) two misses for the SAME range → 1 upstream pull, 2 egress meters; (b) two misses for DISJOINT halves → the second does NOT attach, opens its own pull, both serve (no wedge); (c) partial overlap → attach on the intersection, pull only the remainder.
  • B3.3 — switch the shared pull leg's WindowPacer bound to the leech budget (LeechGovernor); pacing unit + integration tests.
  • B3.4 — last-observer-leaves teardown + eviction-under-lease composition.
  • B3.5 — anvil-e2e: 2 clients, 1 upstream pull, each pays its own egress; abandoned-pull exposure ≤ leech budget. Incentive sign-off. Verify + PR.

Context / notes

  • Flow A (feat(node): route own-origin serve-miss through the two-leg driver; delete window_forward_loop #1655) added a whole-blob same-hash coalescing guard on the own-origin serve branch via open_tee_sink (matching the peer path) — this dedups a concurrent whole-blob same-hash pull but is NOT range-aware. B3's range-aware registry supersedes it and works identically whether the shared pull is a paid peer pull or a free origin pull.
  • Substrate to build on: run_pull_leg / run_local_pull_leg (crates/node/src/node_origin/pull_leg.rs), serve_leg (crates/node/src/handlers/client/serve_leg.rs), SharedOutboard (crates/node/src/node_origin/serve_outboard.rs), the inflight map in crates/cache/src/engine.rs.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Fields

Priority

None yet

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions