From 9863797e715cd95dcecbd1d3158e0e83c6d8d321 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 15 Nov 2026 04:26:55 -0800 Subject: [PATCH 1/2] chore(quest): claim quest/m1/js-pool-reach.md Quest-Claim: e809c6a5fecd18bf From 80d763f948f0b7b6b0e001b5dfa75b3b44de5a70 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 15 Nov 2026 04:42:25 -0800 Subject: [PATCH 2/2] js/net: the byte pool carries only idle expiry No JS caller sets a pool capacity, so byte debt, debt shedding, reader debt, run billing, and the tail's debt folds never ran outside tests. Delete them, keeping idle expiry, drop-run expiry, and the per-group frame tail cap. Co-Authored-By: Claude Opus 5.5 --- js/net/src/broadcast.test.ts | 14 +- js/net/src/broadcast.ts | 2 +- js/net/src/cache.test.ts | 505 +-------------------------- js/net/src/cache.ts | 511 +++------------------------- js/net/src/group.test.ts | 31 -- js/net/src/group.ts | 70 +--- js/net/src/internal.ts | 44 +-- js/net/src/ledger.test.ts | 15 - js/net/src/ledger.ts | 27 -- js/net/src/lite/drop_record.test.ts | 137 ++------ js/net/src/lite/publisher.ts | 24 +- js/net/src/lite/subscriber.test.ts | 12 - js/net/src/lite/subscriber.ts | 15 +- js/net/src/resume.test.ts | 29 -- js/net/src/resume.ts | 7 - js/net/src/tail.ts | 80 +---- js/net/src/track.test.ts | 12 +- js/net/src/track.ts | 312 ++--------------- quest/m1/js-pool-reach.md | 33 -- 19 files changed, 149 insertions(+), 1731 deletions(-) delete mode 100644 quest/m1/js-pool-reach.md diff --git a/js/net/src/broadcast.test.ts b/js/net/src/broadcast.test.ts index 9b7f472bffa..fbbf8f38e3c 100644 --- a/js/net/src/broadcast.test.ts +++ b/js/net/src/broadcast.test.ts @@ -3,7 +3,7 @@ import { Duration, yieldNow } from "@moq/clock"; import { Manual } from "@moq/clock/test"; import { Consumer as BroadcastConsumer, Producer as BroadcastProducer } from "./broadcast.ts"; import * as cache from "./cache.ts"; -import { NotFound, StreamCode, toStreamCode } from "./error.ts"; +import { NotFound, StreamCode, TooFarBehind, toStreamCode } from "./error.ts"; import { hooks } from "./internal.ts"; import { textFrame } from "./mock.ts"; import { Timescale, Timestamp } from "./time.ts"; @@ -716,14 +716,13 @@ test("a pending track info query is answered by its request, and closing rejects // A failed group's consumer settles `closed` with frames still unread; the fetch keeps its mirror // registered until the caller lets go, so the pool can still reclaim those frames. -test("a fetch of a failed group leaves its unread frames to the pool's eviction", async () => { +test("a fetch of a failed group leaves its unread frames to the pool's expiry", async () => { const clock = new Manual(); const installed = clock.install(); try { const broadcast = new BroadcastProducer(); const track = broadcast.createTrack("video", { timescale: Timescale.MILLI }); - const pool = new cache.Pool(); - hooks.poolTrack(track, pool); + hooks.poolTrack(track, new cache.Pool({ expiry: Duration.millis(1_000) })); const failed = track.appendGroup(); failed.writeFrame(textFrame("unread")); @@ -733,10 +732,9 @@ test("a fetch of a failed group leaves its unread frames to the pool's eviction" await yieldNow(); expect(group.closed.peek()).toBeInstanceOf(Error); - // Debt reclaims the oldest group, which the caller has not read. - pool.resize(1); - track.writeFrame(textFrame("fresh")); - await expect(group.readFrame()).rejects.toMatchObject({ code: StreamCode.Evicted }); + // The idle window reclaims the failed group, which the caller has not read. + await clock.advance(Duration.millis(1_500)); + await expect(group.readFrame()).rejects.toBeInstanceOf(TooFarBehind); broadcast.close(); } finally { installed.close(); diff --git a/js/net/src/broadcast.ts b/js/net/src/broadcast.ts index 50d169a6ac8..cd3314269dc 100644 --- a/js/net/src/broadcast.ts +++ b/js/net/src/broadcast.ts @@ -325,7 +325,7 @@ async function fetchGroup( // Close the subscription once the caller has read or released the group, not // now: an in-progress group must keep receiving frames for its lifetime // (mirrors Rust poll_fetch), and a failed group's unread frames stay in the - // cache's reach, billed and evictable, until the caller lets them go. + // cache's reach, expirable, until the caller lets them go. void hooks.groupDone(recv.group).then(() => subscriber.close()); return recv.group; case "drop": diff --git a/js/net/src/cache.test.ts b/js/net/src/cache.test.ts index fcf6f40bb1d..1ee844c4394 100644 --- a/js/net/src/cache.test.ts +++ b/js/net/src/cache.test.ts @@ -4,35 +4,9 @@ import { Manual } from "@moq/clock/test"; import * as cache from "./cache.ts"; import { StreamCode, StreamError, TooFarBehind } from "./error.ts"; import { hooks, staleHole } from "./internal.ts"; -import { RUN_OVERHEAD as ARRIVAL_OVERHEAD } from "./ledger.ts"; -import { Tail } from "./tail.ts"; import { Timestamp } from "./time.ts"; import { Producer as TrackProducer } from "./track.ts"; -// A reader continued on a held re-offer keeps the frames the failed copy wrote that it has yet to -// read, so the pool still counts them once that copy leaves the cache. -test("a re-offer bills the failed copy's frames its readers still hold", async () => { - await withClock(async () => { - const pool = new cache.Pool({ capacity: 100 * billed(10_000) }); - const producer = new TrackProducer("test"); - hooks.poolTrack(producer, pool); - const track = producer.accept({ maxAge: null }); - hooks.receiveTrack(track); - const first = hooks.receiveGroup(track, { sequence: 0n, start: 0 }); - first.writeFrame({ payload: new Uint8Array(10_000), timestamp: Timestamp.unscaled(first.sequence) }); - const reader = first.consume(); - const arriving = hooks.arrivingGroup(track, 0n); - first.abort(new Error("reset")); - // The re-offer starts past the frame the reader still holds. - hooks.receiveGroup(track, { sequence: 0n, start: 1 }); - arriving?.release(); - expect(pool.used).toBeGreaterThan(10_000); - expect((await reader.readFrame())?.payload.byteLength).toBe(10_000); - reader.close(); - track.close(); - }); -}); - // Run `fn` on a manual clock, stepped in milliseconds. async function withClock(fn: (clock: { advance: (ms: number) => Promise }) => Promise | void) { const clock = new Manual(); @@ -70,108 +44,6 @@ async function cached(producer: TrackProducer): Promise { return sequences; } -// What one finished group of `size` payload bytes bills: its frame, the frame's own -// bookkeeping (group.ts `FRAME_BYTES`), and the group's. -const billed = (size: number) => size + 256 + cache.ENTRY_OVERHEAD; - -// While the pool is over capacity, every append accrues debt and pays it by evicting the -// track's own oldest groups, so the newest content survives. Mirrors Rust `debt_evicts_oldest_group`. -test("debt evicts a track's oldest groups", async () => { - await withClock(async () => { - const pool = new cache.Pool({ capacity: 2 * billed(10_000) }); - const producer = pooled(pool); - for (let i = 0; i < 4; i++) finished(producer, 10_000); - - const left = await cached(producer); - expect(left).not.toContain(0n); - expect(left).toContain(3n); - // The protected live edge plus the group just demoted, since debt is charged before it. - expect(pool.used).toBeLessThanOrEqual(2 * billed(10_000)); - }); -}); - -// Far too small for one group, the pool still keeps the newest group whole. Mirrors Rust -// `latest_group_never_evicted`. -test("the newest group survives any capacity", async () => { - await withClock(async () => { - const pool = new cache.Pool({ capacity: 100 }); - const producer = pooled(pool); - for (let i = 0; i < 3; i++) finished(producer, 1_000); - const left = await cached(producer); - expect(left).not.toContain(0n); - expect(left).toContain(2n); - const newest = producer.subscribe({ delay: { max: null }, ranges: [{ start: { absolute: 2n } }] }); - expect((await newest.tryRecvGroup()?.readFrame())?.payload.length).toBe(1_000); - }); -}); - -// A reader holding an evicted group's unread frames is told so, rather than hanging or -// reading a truncated clean end. Mirrors Rust `eviction_aborts_readers`. -test("eviction fails a reader's unread group with Evicted", async () => { - await withClock(async () => { - const pool = new cache.Pool({ capacity: billed(10_000) + cache.MIRROR_OVERHEAD }); - const producer = pooled(pool); - const subscriber = producer.subscribe({ delay: { max: null } }); - finished(producer, 10_000); // seq 0 - const held = subscriber.tryRecvGroup(); - finished(producer, 10_000); // seq 1: demotes seq 0 - finished(producer, 10_000); // seq 2: pays by evicting seq 0 - await expect(held?.readFrame()).rejects.toMatchObject({ code: StreamCode.Evicted }); - subscriber.close(); - }); -}); - -// Debt too small to evict an old group carries over rather than shedding the live group's -// frames, which its reader has not taken yet. Mirrors Rust `carried_debt_keeps_the_live_group`. -test("carried debt keeps the live group", async () => { - await withClock(async () => { - const unit = 100 * cache.ENTRY_OVERHEAD; - const pool = new cache.Pool({ capacity: 22 * unit }); - const producer = pooled(pool); - finished(producer, 20 * unit); - - const group = producer.appendGroup(); - const reader = group.consume(); - for (let i = 0; i < 3; i++) - group.writeFrame({ payload: new Uint8Array(unit), timestamp: Timestamp.unscaled(group.sequence) }); - group.finish(); - for (let i = 0; i < 3; i++) expect((await reader.readFrame())?.payload.length).toBe(unit); - expect(await reader.readFrame()).toBeUndefined(); - }); -}); - -// A read lifts a group's access above the pool's mean, so the eviction walk passes over it -// and evicts a never-read group instead, even one that arrived later. Mirrors Rust -// `fetch_refresh_survives_eviction`. -test("a read group outlives a later unread one", async () => { - await withClock(async (clock) => { - // Each group is mirrored to the reader too. - const pool = new cache.Pool({ capacity: Math.floor(3.5 * (billed(3_000) + cache.MIRROR_OVERHEAD)) }); - const producer = pooled(pool); - const reader = producer.subscribe({ delay: { max: null } }); - - finished(producer, 3_000); // seq 0 - await clock.advance(1_000); - finished(producer, 3_000); // seq 1 - await clock.advance(1_000); - finished(producer, 3_000); // seq 2 - await clock.advance(500); - const first = reader.tryRecvGroup(); - expect(first?.sequence).toBe(0n); - expect((await first?.readFrame())?.payload.length).toBe(3_000); - await clock.advance(500); - - finished(producer, 3_000); // seq 3 - await clock.advance(1_000); - finished(producer, 3_000); // seq 4 - reader.close(); - - const left = await cached(producer); - expect(left).toContain(0n); - expect(left).not.toContain(1n); - }); -}); - // Taking a group is a cache access: a reader handed one just inside the window can still read // it almost a window later. Mirrors Rust `delivery_restarts_the_expiry_clock`. test("delivery restarts a group's idle window", async () => { @@ -193,44 +65,8 @@ test("delivery restarts a group's idle window", async () => { }); }); -// Every track of a broadcast caches into one pool, and the mean access across all of them -// orders eviction: a track writing past the capacity pays with its own groups, but only those -// accessed no later than the mean, so a fresher track's groups survive its own debt while a -// staler track's write evicts its oldest at double rate. -test("a shared capacity spares a fresher track and evicts a staler one's oldest", async () => { - await withClock(async (clock) => { - const pool = new cache.Pool({ capacity: 6 * billed(10_000) }); - const open = (producer: TrackProducer) => { - const group = producer.appendGroup(); - group.writeFrame({ payload: new Uint8Array(10_000), timestamp: Timestamp.unscaled(group.sequence) }); - return group; - }; - const staler = pooled(pool, "staler"); - const old = [open(staler), open(staler), open(staler), open(staler)]; - - await clock.advance(10_000); - const fresher = pooled(pool, "fresher"); - const reader = fresher.subscribe({ delay: { max: null } }); - const read = []; - for (let i = 0; i < 4; i++) { - read.push(open(fresher)); - expect((await reader.tryRecvGroup()?.readFrame())?.payload.length).toBe(10_000); - } - expect(pool.used).toBeGreaterThan(6 * billed(10_000)); - for (const group of read) expect(group.closed.peek()).toBeUndefined(); - - open(staler); - // One write's worth of debt, doubled: two groups. - expect(old[0].closed.peek()).toMatchObject({ code: StreamCode.Evicted }); - expect(old[1].closed.peek()).toMatchObject({ code: StreamCode.Evicted }); - expect(old[2].closed.peek()).toBeUndefined(); - for (const group of read) expect(group.closed.peek()).toBeUndefined(); - reader.close(); - }); -}); - // The pool's sweep reclaims every track's idle groups with no write behind it, the live edges -// excepted, and leaves only what those still bill. Mirrors Rust `pool_sweep_expires_without_a_write`. +// excepted. Mirrors Rust `pool_sweep_expires_without_a_write`. test("the sweep expires idle groups across tracks without a write", async () => { await withClock(async (clock) => { const pool = new cache.Pool({ expiry: Duration.millis(1_000) }); @@ -247,62 +83,6 @@ test("the sweep expires idle groups across tracks without a write", async () => await clock.advance(1_000 + 500); for (const group of stalled) expect(group.closed.peek()).toBeInstanceOf(TooFarBehind); for (const track of tracks) expect(await cached(track)).toEqual([1n]); - expect(pool.used).toBe(2 * billed(1)); - }); -}); - -// Every byte a group bills, its subscribers' mirrors included, goes back once it leaves. -test("the pool is billed only for what is cached", async () => { - await withClock(async (clock) => { - const pool = new cache.Pool({ expiry: Duration.millis(1_000) }); - const producer = pooled(pool); - const subscriber = producer.subscribe({ delay: { max: null } }); - finished(producer, 100); - expect(pool.used).toBe(billed(100) + cache.MIRROR_OVERHEAD); - subscriber.close(); - await yieldNow(); - expect(pool.used).toBe(billed(100)); - - producer.finish(); - producer.close(); - await clock.advance(3_000); - expect(pool.used).toBe(0); - }); -}); - -// The group's own tail limit still sheds its front, and the pool is credited for it. -test("a group's tail limit releases what it sheds from the pool", async () => { - await withClock(() => { - const pool = new cache.Pool(); - const producer = pooled(pool); - const group = producer.appendGroup(); - const frame = new Uint8Array(1024 * 1024); - for (let i = 0; i < 40; i++) - group.writeFrame({ payload: frame, timestamp: Timestamp.unscaled(group.sequence) }); - expect(hooks.groupCacheBytes(group)).toBeLessThanOrEqual(32 * 1024 * 1024); - expect(pool.used).toBe(hooks.groupCacheBytes(group) + cache.ENTRY_OVERHEAD); - }); -}); - -// With nothing evictable, the debt sheds the live edge's front, keeping the newest frame, so -// an open group a track only appends frames to stays near the capacity. -test("the debt sheds the front of an open live edge", async () => { - await withClock(async () => { - const capacity = 1024 * 1024; - const pool = new cache.Pool({ capacity }); - const producer = pooled(pool); - const reader = producer.subscribe({ delay: { max: null } }); - const group = producer.appendGroup(); - const behind = reader.tryRecvGroup(); - const frame = new Uint8Array(64 * 1024); - for (let i = 0; i < 64; i++) - group.writeFrame({ payload: frame, timestamp: Timestamp.unscaled(group.sequence) }); - - expect(group.closed.peek()).toBeUndefined(); - // What a frame write may add before it settles, on top of the capacity. - expect(pool.used).toBeLessThanOrEqual(capacity + 512 * 1024); - // A reader that never read lost the shed front. - await expect(behind?.readFrame()).rejects.toThrow(); }); }); @@ -317,267 +97,6 @@ function dropsHeld(producer: TrackProducer): bigint[] { const old = () => ({ error: new StreamError(StreamCode.Old) }); -// A drop-only flood of disjoint runs bills the pool and sheds its lowest runs to stay within it, -// with no cached group to evict; a shed run is unknown again, so a reader reports only the runs -// the track still holds. Mirrors Rust `a_drop_flood_stays_within_the_pool`. -test("a drop flood stays within the pool", () => { - const runs = 1000n; - const capacity = 64 * cache.RUN_OVERHEAD; - const pool = new cache.Pool({ capacity }); - const producer = pooled(pool); - hooks.receiveTrack(producer); - const reader = producer.subscribe({ delay: { max: null } }); - const reported: bigint[] = []; - for (let run = 0n; run < runs; run++) { - hooks.dropTrackGroups(producer, 2n * run, 2n * run + 1n, old()); - expect(pool.used).toBeLessThanOrEqual(capacity + 2 * cache.RUN_OVERHEAD); - // Midway, the reader takes in every run held then and reports the lowest; the rest are - // shed before it reports them. - if (run === runs / 2n) { - const first = hooks.tryDrop(reader); - if (!first) throw new Error("a run is owed"); - reported.push(first.start); - } - } - const used = pool.used; - // A probe's record bills too: an unbounded pool lets it read every run without shedding one. - pool.resize(); - const held = dropsHeld(producer); - pool.resize(capacity); - // Each drop is accounted in the arrivals too, whose gaps are folded before any run is shed. - expect(hooks.arrivalRuns(producer)).toBeLessThanOrEqual(2); - expect(used).toBe(held.length * cache.RUN_OVERHEAD + (hooks.arrivalRuns(producer) - 1) * ARRIVAL_OVERHEAD); - expect(held[0]).toBeGreaterThan(0n); - expect(held.every((start, i) => i === 0 || start === held[i - 1] + 2n)).toBe(true); - expect(held[held.length - 1]).toBe(2n * (runs - 1n)); - for (let run = hooks.tryDrop(reader); run; run = hooks.tryDrop(reader)) reported.push(run.start); - // Billing the reader's record shed the track's lowest runs before it reported them. - pool.resize(); - expect(reported.slice(1)).toEqual(dropsHeld(producer)); -}); - -// A flood a reader reported, fragmented into runs alternating with upstream skips, bills its -// record too. Once the pool shrinks under it and its track has shed what it can, the reader folds -// the final runs and the groups it read into its floor and stays within the pool, while one -// holding stale reports an update would reopen ends with TooFarBehind rather than forgo the -// re-offer. Mirrors Rust -// `a_reported_flood_folds_or_ends_the_reader`. -for (const stale of [false, true]) { - test(`a reported flood ${stale ? "ends" : "folds into"} the reader`, () => { - const runs = 1000n; - const budget = 1000; - const pool = new cache.Pool({ capacity: 1 << 30 }); - const producer = pooled(pool); - hooks.receiveTrack(producer); - const reader = producer.subscribe({ delay: { max: Duration.millis(budget) } }); - const hole = stale ? staleHole(budget) : { error: new StreamError(StreamCode.Cancel) }; - const flood = (run: bigint) => { - hooks.dropTrackGroups(producer, 2n * run, 2n * run + 1n, hole); - producer.skipGroups(2n * run + 1n, 2n * run + 2n); - }; - for (let run = 0n; run < runs; run++) flood(run); - let reported = 0n; - for (let run = hooks.tryDrop(reader); run; run = hooks.tryDrop(reader)) reported += run.end - run.start; - expect(reported).toBe(2n * runs); - expect(reader.tryRecvGroup()).toBeUndefined(); - expect(pool.used).toBeGreaterThanOrEqual(Number(2n * runs) * cache.READER_RUN_OVERHEAD); - - // The pool shrinks, and groups the reader reads above the flood accrue its debt: the track - // sheds its own drop set first, then the reader pays. - const capacity = 64 * cache.RUN_OVERHEAD; - pool.resize(capacity); - let error: unknown; - for (let sequence = 2n * runs; sequence < 2n * runs + 64n && error === undefined; sequence++) { - const group = producer.createGroup(sequence); - group.writeFrame({ payload: new Uint8Array(16 * 1024), timestamp: Timestamp.unscaled(group.sequence) }); - group.finish(); - try { - expect(reader.tryRecvGroup()?.sequence).toBe(sequence); - } catch (err) { - error = err; - } - } - if (stale) expect(error).toBeInstanceOf(TooFarBehind); - else { - expect(error).toBeUndefined(); - expect(hooks.tryDrop(reader)).toBeUndefined(); - } - expect(pool.used).toBeLessThanOrEqual(capacity + 2 * (16 * 1024 + cache.ENTRY_OVERHEAD)); - reader.close(); - }); -} - -// A reader that closes while a read waits releases its record's bill for good: the read that wakes -// after the close bills nothing again. -test("a closed reader bills nothing from a read still pending", async () => { - const pool = new cache.Pool({ capacity: 1 << 30 }); - const producer = pooled(pool); - hooks.receiveTrack(producer); - const reader = producer.subscribe({ delay: { max: null } }); - for (let run = 0n; run < 64n; run++) { - hooks.dropTrackGroups(producer, 2n * run, 2n * run + 1n, { error: new StreamError(StreamCode.Cancel) }); - producer.skipGroups(2n * run + 1n, 2n * run + 2n); - } - while (hooks.tryDrop(reader)); - const billed = pool.used; - const read = reader.recvGroup(); - reader.close(); - await read; - expect(pool.used).toBeLessThan(billed - 32 * cache.READER_RUN_OVERHEAD); -}); - -// A subscription's tail, recording a fragmented run of finished streams between updates, bills it to -// its track's account. Under the pool's debt it forgets them and stays within the pool. Mirrors Rust -// `a_tail_forgets_its_finished_streams_under_debt`. -test("a tail forgets its finished streams under debt", () => { - const runs = 4000n; - const pool = new cache.Pool({ capacity: 1 << 30 }); - const producer = pooled(pool); - hooks.receiveTrack(producer); - const tail = new Tail({ bill: hooks.billTrack(producer) }); - for (let run = 0n; run < runs; run++) tail.finish(2n * run); - expect(tail.unfinished(0n, 2n * runs).length).toBe(Number(runs)); - expect(pool.used).toBeGreaterThanOrEqual(Number(runs) * cache.READER_RUN_OVERHEAD); - - // The pool shrinks, and groups written above them accrue the track's debt, which its own groups - // cannot pay: the tail pays it by forgetting what finished. - const capacity = 64 * cache.RUN_OVERHEAD; - pool.resize(capacity); - for (let i = 0; i < 64; i++) finished(producer, 16 * 1024); - tail.finish(2n * runs); - expect(tail.unfinished(0n, 2n * runs + 1n).length).toBeLessThanOrEqual(2); - expect(pool.used).toBeLessThanOrEqual(capacity + 2 * (16 * 1024 + cache.ENTRY_OVERHEAD)); - // A stale drop inside what the fold forgot is owed again only where the ledger had not accounted - // for it: there a stream may have finished and overtaken the drop. - tail.open(2n).done(); - expect(tail.overtaken(0n, 4n)).toEqual([{ start: 2n, end: 3n }]); - expect(tail.overtaken(2n * runs, 2n * runs + 1n)).toEqual([]); - tail.updated(); - expect(tail.overtaken(0n, 4n)).toEqual([]); - tail.close(); -}); - -// A tail's fragmented final drops bill its track's account too. Under the pool's debt they fold into -// the one span they reach, which still names every dropped sequence, so a stream a drop overtook is -// never held as owed, and the gaps between them are excused at worst. -test("a tail folds its final drops under debt", () => { - const runs = 4000n; - const pool = new cache.Pool({ capacity: 1 << 30 }); - const producer = pooled(pool); - hooks.receiveTrack(producer); - const tail = new Tail({ bill: hooks.billTrack(producer) }); - for (let run = 0n; run < runs; run++) tail.drop(2n * run, 2n * run + 1n); - expect(tail.dropped(2n)).toBe(true); - expect(tail.dropped(3n)).toBe(false); - expect(pool.used).toBeGreaterThanOrEqual(Number(runs) * cache.READER_RUN_OVERHEAD); - - const capacity = 64 * cache.RUN_OVERHEAD; - pool.resize(capacity); - for (let i = 0; i < 64; i++) finished(producer, 16 * 1024); - tail.drop(2n * runs, 2n * runs + 1n); - expect(pool.used).toBeLessThanOrEqual(capacity + 2 * (16 * 1024 + cache.ENTRY_OVERHEAD)); - for (let run = 0n; run <= runs; run++) expect(tail.dropped(2n * run)).toBe(true); - expect(tail.dropped(2n * runs + 1n)).toBe(false); - tail.close(); -}); - -// Whatever a bill frees pays its readers' debt, however it shrank, and the debt is forgiven once -// the pool is back within its capacity. A closed tail never bills again. Mirrors Rust -// `a_debt_is_paid_by_any_shrink_and_forgiven_without_pressure`. -test("a reader's debt is paid by any shrink and forgiven without pressure", () => { - const unit = cache.READER_RUN_OVERHEAD; - const pool = new cache.Pool({ capacity: 1 << 30 }); - const producer = pooled(pool); - const bill = hooks.billTrack(producer); - bill.set(4 * unit); - pool.resize(2 * unit); - // Nothing else is cached, so the debt the track accrues falls to its readers. - producer.createGroup(0n).abort(new Error("gone")); - bill.set(5 * unit); - const owed = bill.debt; - expect(owed).toBeGreaterThan(unit); - bill.set(4 * unit); - expect(bill.debt).toBe(owed - unit); - pool.resize(1 << 30); - expect(bill.debt).toBe(0); - pool.resize(2 * unit); - expect(bill.debt).toBe(0); - bill.close(); - - const tail = new Tail({ bill: hooks.billTrack(producer) }); - tail.finish(0n); - tail.finish(2n); - tail.close(); - const used = pool.used; - tail.finish(4n); - expect(pool.used).toBe(used); -}); - -// A peer fragmenting its arrivals under a grace that never folds a gap bills each run to the pool, -// which folds the oldest gaps early to stay within it. Mirrors Rust `an_arrival_flood_stays_within_the_pool`. -test("an arrival flood stays within the pool", () => { - const groups = 1000n; - const capacity = 64 * ARRIVAL_OVERHEAD; - const pool = new cache.Pool({ capacity }); - const producer = pooled(pool); - hooks.receiveTrack(producer); - // A reader that waits for a late group indefinitely, so no gap folds by its grace. - const reader = producer.subscribe({ delay: { max: null } }); - for (let group = 0n; group < groups; group++) producer.createGroup(2n * group).finish(); - const runs = hooks.arrivalRuns(producer); - expect(runs).toBeGreaterThan(1); - expect(runs).toBeLessThan(Number(groups)); - expect(pool.used).toBeLessThanOrEqual(capacity + 2 * cache.ENTRY_OVERHEAD); - reader.close(); -}); - -// A frame write that folds an arrival gap to pay its debt wakes a reader parked on that gap, which -// has no grace timer to wake it. -test("folding an arrival gap wakes the reader parked on it", async () => { - const pool = new cache.Pool({ capacity: 1 }); - const producer = pooled(pool); - hooks.receiveTrack(producer); - const reader = producer.subscribe({ - delay: { max: null }, - ranges: [{ start: { absolute: 0n }, end: { absolute: 3n } }], - }); - // Group 0 is dropped rather than cached, so the protected live edge is the only group and the - // gap below it is the first thing a payment takes: no other signal moves. - hooks.dropTrackGroups(producer, 0n, 1n, old()); - const live = producer.createGroup(2n); - live.writeFrame({ payload: new Uint8Array(8), timestamp: Timestamp.unscaled(live.sequence) }); - for (let group = reader.tryRecvGroup(); group; group = reader.tryRecvGroup()); - for (let run = hooks.tryDrop(reader); run; run = hooks.tryDrop(reader)); - let woken = 0; - const dispose = hooks.groupChanged(reader, () => woken++); - expect(hooks.arrivalRuns(producer)).toBe(2); - // Past the write threshold that settles debt. - live.writeFrame({ payload: new Uint8Array(512 * 1024), timestamp: Timestamp.unscaled(live.sequence) }); - expect(hooks.arrivalRuns(producer)).toBe(1); - await yieldNow(); - expect(woken).toBeGreaterThan(0); - dispose(); - reader.close(); -}); - -// A closed track bills none of its arrival gaps, so an account the pool's sweep keeps for its drops -// holds no bytes for them. -test("a closed track releases its arrival gaps from the pool", async () => { - await withClock(() => { - const pool = new cache.Pool({ expiry: Duration.millis(1_000) }); - const producer = pooled(pool); - hooks.receiveTrack(producer); - const reader = producer.subscribe({ delay: { max: null } }); - producer.createGroup(0n).finish(); - producer.createGroup(2n).finish(); - expect(hooks.arrivalRuns(producer)).toBe(2); - const used = pool.used; - producer.close(); - expect(pool.used).toBe(used - ARRIVAL_OVERHEAD); - reader.close(); - }); -}); - // A run given up longer than the pool's idle window ago is forgotten by the sweep, even on a // track that caches nothing, and a run given up since stays. Mirrors Rust // `the_sweep_forgets_runs_past_the_expiry_window`. @@ -593,25 +112,19 @@ test("the sweep forgets runs past the idle window", async () => { hooks.dropTrackGroups(producer, 2n, 3n, old()); await clock.advance(500); hooks.dropTrackGroups(producer, 4n, 5n, old()); - // Each run and its expiry are billed, and so are the two gaps between the drops the - // arrivals account. - expect(pool.used).toBe(4 * (cache.RUN_OVERHEAD + cache.BORN_OVERHEAD) + 2 * ARRIVAL_OVERHEAD); // The sweep at 1.5 s forgets the first two, given up at 0 s. await clock.advance(1_000); expect(dropsHeld(producer)).toEqual([4n]); - expect(pool.used).toBe(2 * (cache.RUN_OVERHEAD + cache.BORN_OVERHEAD)); await clock.advance(1_000); expect(dropsHeld(producer)).toEqual([]); expect(dropsHeld(local)).toEqual([5n]); - expect(pool.used).toBe(cache.RUN_OVERHEAD + cache.BORN_OVERHEAD); }); }); // A local skip kept above the track's edge is dated once the edge passes it, or once the track -// closes, so an expiry scan past its window forgets it with no other drop to rearm it, and its -// bill leaves the pool. Mirrors Rust `a_kept_local_skip_expires_once_the_edge_passes_it`; Rust +// closes, so an expiry scan past its window forgets it with no other drop to rearm it. Mirrors Rust `a_kept_local_skip_expires_once_the_edge_passes_it`; Rust // frees a closed track's drops with its state. test("a kept local skip expires once the edge passes it or the track closes", async () => { await withClock(async (clock) => { @@ -628,16 +141,16 @@ test("a kept local skip expires once the edge passes it or the track closes", as passed.createGroup(10n).finish(); expect(dropsHeld(passed)).toEqual([]); - const used = pool.used; closed.close(); - expect(pool.used).toBe(used - cache.RUN_OVERHEAD - cache.BORN_OVERHEAD); + await clock.advance(1_500); + expect(dropsHeld(closed)).toEqual([]); }); }); // A final drop ends a group still arriving, but leaves one that finished here to every reader, as a // group arriving after its drop replaces it. Mirrors Rust `a_final_drop_leaves_a_finished_group`. test("a final drop leaves a finished group", async () => { - const producer = pooled(new cache.Pool({ capacity: 1 << 30 })); + const producer = pooled(new cache.Pool()); hooks.receiveTrack(producer); producer.createGroup(0n).finish(); const open = producer.createGroup(1n); @@ -707,10 +220,9 @@ test("a forgotten failed fill answer leaves its gap owed", async () => { }); }); -// A failed fill answer a reader still holds stays billed, and reclaimable by the pool, while the -// reader keeps its frames: it leaves delivery, not the cache, as Rust keeps a spent fetch-only slot -// until its scan reclaims it. -test("a held failed fill answer stays billed", async () => { +// A failed fill answer a reader still holds stays readable, and reclaimable by the pool: it leaves +// delivery, not the cache, as Rust keeps a spent fetch-only slot until its scan reclaims it. +test("a held failed fill answer stays readable", async () => { const pool = new cache.Pool({}); const producer = pooled(pool); hooks.receiveTrack(producer); @@ -721,7 +233,6 @@ test("a held failed fill answer stays billed", async () => { expect(group?.sequence).toBe(1n); answer.abort(new Error("archive failed")); await yieldNow(); - expect(pool.used).toBeGreaterThanOrEqual(10_000); expect((await group?.readFrame())?.payload.byteLength).toBe(10_000); reader.close(); }); diff --git a/js/net/src/cache.ts b/js/net/src/cache.ts index 7a8c25f022d..fe449988c6f 100644 --- a/js/net/src/cache.ts +++ b/js/net/src/cache.ts @@ -1,18 +1,12 @@ /** - * A shared byte budget for cached groups, repaid by write-time eviction. Mirrors the Rust - * `cache` module. + * The idle window of cached groups. Mirrors the idle expiry of the Rust `cache` module, without + * its byte budget: no JS caller sets a capacity, so nothing here evicts for bytes. * - * Every cached group bills its frames and a fixed overhead through a {@link Charge} to its - * track's {@link Track} account, and every track of a broadcast caches into one {@link Pool}. - * The pool never evicts on its own: while it is over capacity, each track accrues eviction debt - * as it writes, sized to what it wrote, and pays it by evicting its own oldest groups. A group - * accessed more recently than the mean access of the evictable groups is never evicted, so fresh - * content in one track outlives stale content in another, and a track whose oldest group is - * staler than the mean accrues at double rate. - * - * The pool also owns the idle window: a group nobody reads or writes for that long is reclaimed, - * except a live track's latest. Writes scan for idle groups as they go, and the pool sweeps - * every track at half the window for the ones nothing writes to. + * Every track of a broadcast caches into one {@link Pool} through its own {@link Track} account, + * and every cached group records its last access through a {@link Charge}. A group nobody reads + * or writes for the pool's window is reclaimed, except a live track's latest. Writes scan for + * idle groups as they go, and the pool sweeps every track at half the window for the ones + * nothing writes to. * * Package-internal: not exported from the entrypoint. * @@ -23,101 +17,44 @@ import { Duration, type Instant, now, sleepUntil } from "@moq/clock"; /** How long a group nobody reads or writes stays cached by default, matching Rust `cache::DEFAULT_EXPIRY`. */ export const DEFAULT_EXPIRY = Duration.millis(30_000); -// Bun 1.4's heap (bun:jsc `heapStats`) grows about 2.9 KB per cached single-frame group with no -// subscriber, and 3.9 KB more per subscriber, whose sink holds its own mirror of every cached -// group. Billing payload alone would let a track of tiny groups cache far more than its budget. -/** Bookkeeping billed per cached group on top of its frames. */ -export const ENTRY_OVERHEAD = 3 * 1024; -/** Bookkeeping billed per subscriber mirror a cached group holds. */ -export const MIRROR_OVERHEAD = 4 * 1024; -// Bun 1.4's heap grows about 334 bytes per disjoint run of a track's drop set, its change-log -// entry included, and about 53 more per entry of the expiry index a pool with an idle window keeps. -/** Bookkeeping billed per run of a track's drop set. */ -export const RUN_OVERHEAD = 336; -/** Bookkeeping billed per entry of a drop set's expiry index. */ -export const BORN_OVERHEAD = 64; -// A reader's record keeps an object per run in a sorted array, about what a drop run costs -// without its change-log entry. -/** Bookkeeping billed per run of a reader's record. */ -export const READER_RUN_OVERHEAD = 256; - // The coarse clock behind access stamps, as Rust's: the expiry window rounds up to it. const TICK = Duration.millis(100); -// Sub-tick boosts break ties within one tick: a write outranks a merely inserted group, and a -// read outranks both, so a same-tick access still reads as newer than the mean of weaker ones. -const WRITE_BOOST = 1; -const READ_BOOST = 2; -const BOOSTS = 4; - -// Bytes a track writes before a frame write settles its debt, so a track appending frames to -// open groups, never inserting another, still pays. -const WRITE_CHARGE_THRESHOLD = 256 * 1024; - // Ticks between the idle scans frame writes run, so a low-bitrate track still ages out. const EXPIRY_SCAN_TICKS = 10; -// Live candidates one debt payment examines, and entries one write-driven idle scan does: enough -// to step over a few fresh groups, few enough that a write never walks a long queue. -const EVICT_SCAN = 4; +// Entries one write-driven idle scan examines, few enough that a write never walks a long queue. +const EXPIRY_SCAN = 4; -// Dead entries the eviction order tolerates beyond twice the live ones before it is rebuilt. -const EVICT_SLACK = 64; +// Dead entries the scan order tolerates beyond twice the live ones before it is rebuilt. +const ORDER_SLACK = 64; /** The initial policy for a {@link Pool}. */ export interface Config { - /** The byte target usage converges toward; unbounded when omitted. */ - capacity?: number; /** How long a group nobody reads or writes stays cached; never reclaimed for idleness when omitted. */ expiry?: Duration; } -/** - * A shared byte budget and idle window for the cached groups of every track charging into it. - * - * The capacity is a target usage converges toward, not a hard limit: carried debt, capped - * payments, and the always protected live edge all let usage exceed it for a while. - */ +/** The idle window shared by the cached groups of every track caching into it. */ export class Pool { - #capacity: number; // The idle window in ticks. readonly #expiry?: number; - #used = 0; // The first instant the pool read, and the latest tick since it, never moving back. #epoch?: Instant; #tick = 0; - // The sum and count of the evictable groups' access stamps, for their mean. - #sum = 0; - #count = 0; - // The accounts holding a group that may expire, which the idle sweep visits. + // The accounts holding something that may expire, which the idle sweep visits. #tracks = new Set>(); #sweep?: AbortController; constructor(config: Config = {}) { - this.#capacity = config.capacity ?? Number.POSITIVE_INFINITY; if (config.expiry !== undefined) this.#expiry = Math.max(1, Math.ceil(config.expiry.divDuration(TICK))); } - /** The byte target, or undefined when unbounded. */ - get capacity(): number | undefined { - return Number.isFinite(this.#capacity) ? this.#capacity : undefined; - } - - /** Bytes billed across every cached group. */ - get used(): number { - return this.#used; - } - /** The idle window, rounded up to the pool's 100 ms tick, or undefined when idle groups stay. */ get expiry(): Duration | undefined { return this.#expiry === undefined ? undefined : TICK.mul(this.#expiry); } - /** Change the byte target; undefined makes it unbounded. Writes pay a shrink down as they go. */ - resize(capacity?: number): void { - this.#capacity = capacity ?? Number.POSITIVE_INFINITY; - } - /** Ticks since the pool first read the clock. */ now(): number { const at = now(); @@ -131,61 +68,14 @@ export class Pool { return this.#expiry; } - /** The mean access stamp of the evictable groups, or undefined with none. */ - average(): number | undefined { - return this.#count === 0 ? undefined : this.#sum / this.#count; - } - - /** - * The eviction debt writing `written` bytes accrues, or undefined while the pool is within - * capacity: `written * used / capacity`, so paying it frees a little more than was written - * and the overshoot decays toward the capacity. - */ - accrue(written: number): number | undefined { - if (this.#used <= this.#capacity) return undefined; - return Math.floor((written * this.#used) / Math.max(1, this.#capacity)); - } - - /** Bill `n` more bytes. */ - add(n: number): void { - this.#used += n; - } - - /** Release `n` billed bytes. */ - sub(n: number): void { - this.#used -= n; - } - - /** The current tick as an access stamp, with `boost` breaking ties within it. */ - stamp(boost: number): number { - return this.now() * BOOSTS + boost; - } - - /** A group with access stamp `stamp` became evictable. */ - accessInsert(stamp: number): void { - this.#sum += stamp; - this.#count++; - } - - /** A group with access stamp `stamp` stopped being evictable. */ - accessRemove(stamp: number): void { - this.#sum -= stamp; - this.#count--; - } - - /** An evictable group's access stamp moved from `old` to `stamp`. */ - accessRefresh(old: number, stamp: number): void { - this.#sum += stamp - old; - } - - /** Visit `track` on each idle sweep, if the pool has an idle window, while it holds a group that may expire. */ + /** Visit `track` on each idle sweep, if the pool has an idle window, while it holds something that may expire. */ register(track: Track): void { if (this.#expiry === undefined) return; this.#tracks.add(track); this.#arm(); } - /** Stop visiting `track`, which holds no group that may expire. */ + /** Stop visiting `track`, which holds nothing that may expire. */ unregister(track: Track): void { this.#tracks.delete(track); } @@ -195,7 +85,7 @@ export class Pool { for (const track of [...this.#tracks]) track.sweep(); } - // Sweep again in half the idle window while any track holds a group that may expire. Never a + // Sweep again in half the idle window while any track holds something that may expire. Never a // reason to hold a Node or Bun process open. #arm(): void { if (this.#sweep !== undefined || this.#expiry === undefined || this.#tracks.size === 0) return; @@ -214,33 +104,25 @@ export class Pool { } } -/** A cached group as its track's account orders it for eviction. */ +/** A cached group as its track's account knows it. */ export interface Entry { - /** What the group bills. */ + /** Its last access. */ readonly charge: Charge; } -// A group's place in an account's eviction order, emptied once the group leaves the cache. +// A group's place in an account's scan order, emptied once the group leaves the cache. interface Slot { entry: E | undefined; } -/** How an account reaches the track whose groups it evicts. */ +/** How an account reaches the track whose groups it expires. */ export interface Owner { - /** Whether `entry` is the live edge, which neither eviction nor expiry takes. */ + /** Whether `entry` is the live edge, which expiry never takes. */ protects(entry: E): boolean; - /** The live edge while protected, whose front pays the debt when no group can go whole. */ - edge(): E | undefined; - /** Take `entry` out of the cache and end it, for its bytes (`evicted`) or for idling (`expired`). */ - evict(entry: E, reason: "evicted" | "expired"): void; - /** Shed whole frames from the front of `entry` toward `debt`, freeing about `cap` at most, and return the bytes freed. */ - shed(entry: E, debt: number, cap: number): number; + /** Take `entry` out of the cache and end it for idling. */ + evict(entry: E): void; /** Something was evicted: settle what the cache's contents decide. */ settle(): void; - /** Forget the lowest runs of the track's drop set toward `debt`, freeing about `cap` at most, and return the bytes freed. */ - shedDrops(debt: number, cap: number): number; - /** Fold the oldest gaps of the track's arrivals toward `debt`, freeing about `cap` at most, and return the bytes freed. */ - shedArrivals(debt: number, cap: number): number; /** Forget the runs of the drop set given up longer than the pool's idle window before tick `now`, returning whether any went. */ expireDrops(now: number): boolean; /** Whether the track holds a drop run a reclaim may forget, which keeps its account in the idle sweep. */ @@ -248,71 +130,46 @@ export interface Owner { } /** - * One track's account against a {@link Pool}: the bytes its groups wrote, the debt they owe, and - * the order its groups are evicted in. Mirrors the Rust `cache::Track` with the eviction order - * and debt of the Rust `TrackState`. + * One track's account against a {@link Pool}: its cached groups, in the order the idle scan + * visits them. Mirrors the expiry half of the Rust `cache::Track`. */ export class Track { /** The pool this track caches into. */ readonly pool: Pool; #owner: Owner; - // Bytes billed since the last debt accrual. - #written = 0; // The tick a frame write may next run an idle scan at. #nextExpiry = 0; - // Every cached group with its slot in the eviction order, oldest first. A group leaving the - // cache empties its slot, so the order never pins its frames; a scan drops empty slots. The + // Every cached group with its slot in the scan order, oldest first. A group leaving the cache + // empties its slot, so the order never pins its frames; a compaction drops empty slots. The // order starts at `#head`, since `Array.shift` moves every slot behind it in V8. #live = new Map>(); #order: Slot[] = []; #head = 0; - // Debt carried over from earlier payments. - #debt = 0; // Where the next bounded idle scan starts, rotating so fresh groups at the front do not // starve the ones behind them. #cursor = 0; - // What this track's readers' records bill, how many bill anything, and the debt the track - // could not pay itself, which those readers pay; see {@link Reader}. - #readers = { bytes: 0, count: 0, debt: 0 }; constructor(pool: Pool, owner: Owner) { this.pool = pool; this.#owner = owner; } - /** Open the charge for a new group, billing its fixed overhead. */ + /** Open the access record of a new group, stamped now. */ charge(): Charge { return makeCharge(this as Track); } - /** Count `n` billed bytes toward the next debt accrual. */ - wrote(n: number): void { - this.#written += n; - } - - /** Bill `n` bytes the track keeps beside its groups, such as its drop set, which the idle sweep then visits. */ - bill(n: number): void { - this.pool.add(n); - this.wrote(n); + /** The drop set changed: keep the account in the idle sweep while it holds a run to forget. */ + dropsChanged(): void { if (this.#owner.holdsDrops()) this.pool.register(this as Track); } - /** Release `n` bytes {@link bill} billed. */ - unbill(n: number): void { - this.pool.sub(n); - } - - /** Open an empty bill for one reader's record. */ - reader(): Reader { - return new Reader(this as Track, this.#readers); - } - /** Whether the account caches any group. */ get empty(): boolean { return this.#live.size === 0; } - /** Cache `entry` at the back of the eviction order, evictable now unless it is the live edge. */ + /** Cache `entry` at the back of the scan order, expirable now unless it is the live edge. */ insert(entry: E): void { const slot = { entry }; this.#live.set(entry, slot); @@ -320,14 +177,12 @@ export class Track { if (!this.#owner.protects(entry)) this.demote(entry); } - /** Make `entry` evictable once the live edge no longer protects it. */ + /** Make `entry` expirable once the live edge no longer protects it. */ demote(entry: E): void { - if (!this.#live.has(entry)) return; - entry.charge.demote(); - this.pool.register(this as Track); + if (this.#live.has(entry)) this.pool.register(this as Track); } - /** Forget `entry`, which left the cache, releasing what it billed. */ + /** Forget `entry`, which left the cache. */ remove(entry: E): void { const slot = this.#live.get(entry); if (slot === undefined) return; @@ -337,133 +192,8 @@ export class Track { if (this.#live.size === 0) { this.#order = []; this.#head = 0; - if (!this.#owner.holdsDrops()) { - this.#debt = 0; - this.pool.unregister(this as Track); - } - } - } - - /** - * Accrue and pay the eviction debt of everything written since the last accrual. Run before a - * new group is cached, so the write that creates it can never evict it. Doubles when the - * track's oldest group is staler than the pool's mean. Returns whether it evicted a group. - */ - chargeDebt(): boolean { - const written = this.#written; - this.#written = 0; - const accrued = this.pool.accrue(written); - // Under capacity, a stale debt would only make a burst at the next pressure spike. - if (accrued === undefined) { - this.#debt = 0; - return false; - } - const debt = this.#oldestIsStale() ? 2 * accrued : accrued; - // What is used bounds what eviction could ever free, so a track that cannot pay - // (everything protected) does not hoard a stale schedule. - this.#debt = Math.min(this.#debt + debt, this.pool.used); - // One write never dumps a deep backlog at once: the remainder carries over. - return this.#payDebt(2 * written); - } - - // Whether the oldest evictable group was accessed no later than the pool's mean. A dead entry - // at the front reads as not stale until a scan drops it. - #oldestIsStale(): boolean { - const average = this.pool.average(); - const front = this.#order[this.#head]?.entry; - if (average === undefined || front === undefined) return false; - return !this.#owner.protects(front) && front.charge.accessed <= average; - } - - // Evict the stalest groups until the debt is paid or `cap` bytes are freed, examining a few - // candidates from the front of the order. A group accessed after the pool's mean rotates to - // the back instead, leaving the debt to tracks holding staler content. A group larger than - // the debt left stays, carrying the debt over. With no group able to go whole, the fresh - // ones and the live edge shed their fronts, so neither grows without bound. - #payDebt(cap: number): boolean { - const average = this.pool.average() ?? 0; - let paid = 0; - let scanned = 0; - let evicted = false; - // Whether every cold group was offered, so a debt left at the end is one the track - // itself cannot pay. - let swept = true; - const fresh: E[] = []; - for (let i = this.#order.length - this.#head; i > 0; i--) { - if (this.#debt === 0 || paid >= cap || scanned >= EVICT_SCAN) { - swept = this.#debt === 0 || paid >= cap; - break; - } - // An eviction that emptied the account reset the order. - if (this.#head >= this.#order.length) break; - const slot = this.#order[this.#head++]; - const entry = slot.entry; - if (entry === undefined) continue; - if (this.#owner.protects(entry)) { - this.#order.push(slot); - continue; - } - scanned++; - if (entry.charge.accessed > average) { - this.#order.push(slot); - fresh.push(entry); - continue; - } - const size = entry.charge.size; - if (size > this.#debt) { - // Back to the front, where its slot still is. - this.#head--; - this.#compact(); - return evicted; - } - this.#debt -= size; - paid += size; - evicted = true; - this.#owner.evict(entry, "evicted"); - } - // Then the arrivals' oldest gaps, which costs a reader only the wait for a late group, as - // the grace passing would. - if (this.#debt > 0 && paid < cap) { - const freed = this.#owner.shedArrivals(this.#debt, cap - paid); - this.#debt -= Math.min(freed, this.#debt); - paid += freed; - } - // Then the drop set, oldest sequences first: losing a run costs a reader a judgment or a - // fill, never content being read. - if (this.#debt > 0 && paid < cap) { - const freed = this.#owner.shedDrops(this.#debt, cap - paid); - this.#debt -= Math.min(freed, this.#debt); - paid += freed; - } - const edge = this.#owner.edge(); - if (edge) fresh.push(edge); - for (const entry of fresh) { - if (this.#debt === 0 || paid >= cap) break; - if (!this.#live.has(entry)) continue; - const freed = this.#owner.shed(entry, this.#debt, cap - paid); - this.#debt -= Math.min(freed, this.#debt); - paid += freed; + if (!this.#owner.holdsDrops()) this.pool.unregister(this as Track); } - // Last, the readers' records: nothing cheaper is left, so each reader folds what it - // settled for good, and one that cannot ends rather than forgo a promise. - if (swept && this.#debt > 0 && paid < cap) { - const readers = this.#readers; - const shifted = Math.min(this.#debt, Math.max(0, readers.bytes - readers.debt)); - readers.debt += shifted; - this.#debt -= shifted; - } - this.#compact(); - return evicted; - } - - /** - * Pay the eviction debt now if the pool is over its capacity, without waiting for the write - * threshold: a reader's record grows by runs, never by enough bytes to cross it. - */ - chargeOver(): void { - const capacity = this.pool.capacity; - if (capacity === undefined || this.pool.used <= capacity) return; - if (this.chargeDebt()) this.#owner.settle(); } /** @@ -480,15 +210,15 @@ export class Track { const forgot = this.#owner.expireDrops(now); if (len === 0) return forgot; const start = full ? 0 : this.#cursor % len; - const width = full ? len : Math.min(len, EVICT_SCAN); + const width = full ? len : Math.min(len, EXPIRY_SCAN); if (!full) this.#cursor = start + width; const expired: E[] = []; for (let step = 0; step < width; step++) { const entry = order[head + ((start + step) % len)].entry; if (entry === undefined || this.#owner.protects(entry)) continue; - if (now - Math.floor(entry.charge.accessed / BOOSTS) > expiry) expired.push(entry); + if (now - entry.charge.accessed > expiry) expired.push(entry); } - for (const entry of expired) this.#owner.evict(entry, "expired"); + for (const entry of expired) this.#owner.evict(entry); this.#compact(); return expired.length > 0 || forgot; } @@ -498,23 +228,15 @@ export class Track { #compact(): void { const order = this.#order; while (this.#head < order.length && order[this.#head].entry === undefined) this.#head++; - if (order.length > 2 * this.#live.size + EVICT_SLACK) { + if (order.length > 2 * this.#live.size + ORDER_SLACK) { this.#order = order.slice(this.#head).filter((slot) => slot.entry !== undefined); this.#head = 0; } } - /** - * Settle from a frame write: pay the debt once enough was written, and scan for idle groups - * once per second, so a track that only appends frames to open groups still pays and ages. - */ + /** Settle from a frame write: scan for idle groups once per second, so a track that only appends frames to open groups still ages. */ settle(): void { - const debt = this.#written >= WRITE_CHARGE_THRESHOLD; - const expiry = this.#expiryDue(); - let evicted = false; - if (debt) evicted = this.chargeDebt(); - if (expiry) evicted = this.evictExpired(false) || evicted; - if (evicted) this.#owner.settle(); + if (this.#expiryDue() && this.evictExpired(false)) this.#owner.settle(); } /** Expire every idle group, from the pool's sweep. */ @@ -540,173 +262,40 @@ export class Track { let makeCharge: (track: Track) => Charge; /** - * What one reader's record bills to its track's account, and its share of the debt the track - * could not pay. Mirrors the Rust `cache::Reader`; {@link close} releases it. - */ -export class Reader { - #track: Track; - #readers: { bytes: number; count: number; debt: number }; - #bytes = 0; - - constructor(track: Track, readers: { bytes: number; count: number; debt: number }) { - this.#track = track; - this.#readers = readers; - } - - /** Bytes currently billed. */ - get size(): number { - return this.#bytes; - } - - /** Whether this bills `account`. */ - bills(account: unknown): boolean { - return this.#track === account; - } - - /** - * The debt the track's readers owe, forgiven once the pool is back within its capacity: - * nothing needs the room any more, as the track forgives its own debt. - */ - get debt(): number { - const pool = this.#track.pool; - const capacity = pool.capacity; - if (capacity === undefined || pool.used <= capacity) this.#readers.debt = 0; - return this.#readers.debt; - } - - /** Bill `bytes` in all, paying the track's debt after a growth. */ - set(bytes: number): void { - const before = this.#bytes; - if (bytes === before) return; - const readers = this.#readers; - if (before === 0) readers.count++; - else if (bytes === 0) readers.count--; - readers.bytes += bytes - before; - this.#bytes = bytes; - if (bytes > before) { - this.#track.pool.add(bytes - before); - this.#track.wrote(bytes - before); - this.#track.chargeOver(); - } else { - // Whatever a record frees pays toward its readers' debt, however it shrank. - readers.debt = Math.max(0, readers.debt - (before - bytes)); - this.#track.pool.sub(before - bytes); - } - } - - /** - * Whether this record is one to end when folding leaves a debt: it bills more than a cached - * group's overhead, so ending it frees more than evicting a group would, and at least its - * readers' average, so a reader holding little never ends for one holding much. - */ - get ends(): boolean { - const readers = this.#readers; - return this.#bytes > ENTRY_OVERHEAD && this.#bytes * readers.count >= readers.bytes; - } - - /** Release the bill, paying the readers' debt with it. */ - close(): void { - this.set(0); - } -} - -/** - * What one cached group bills to its track's account: its bytes, overhead included, and its - * access stamp, which the pool's mean samples while the group is evictable. Cleared once the - * group leaves the cache, after which every operation is a no-op. + * One cached group's last access, which its idle window runs from, and its route to its track's + * idle scan. Cleared once the group leaves the cache, after which every operation is a no-op. */ export class Charge { #track?: Track; - #bytes = 0; #stamp: number; - // Whether the stamp is a sample in the pool's mean: the group is evictable. - #counted = false; private constructor(track: Track) { this.#track = track; - this.#stamp = track.pool.stamp(0); - this.bill(ENTRY_OVERHEAD); + this.#stamp = track.pool.now(); } static { makeCharge = (track) => new Charge(track); } - /** Bill `n` more bytes of a write, which is an access too: it restarts the idle window. */ - add(n: number): void { - this.bill(n); - this.#touch(WRITE_BOOST); - } - - /** Bill `n` more bytes nobody accessed, such as a subscriber's mirror. */ - bill(n: number): void { - const track = this.#track; - if (!track) return; - track.pool.add(n); - track.wrote(n); - this.#bytes += n; - } - - /** Release `n` bytes the group dropped while still cached. */ - sub(n: number): void { - const track = this.#track; - if (!track) return; - n = Math.min(n, this.#bytes); - track.pool.sub(n); - this.#bytes -= n; - } - - /** Every byte billed, overhead included. */ - get size(): number { - return this.#bytes; - } - - /** The last access stamp: its tick times four, plus the boost of the access that set it. */ + /** The tick of the last access. */ get accessed(): number { return this.#stamp; } - /** Record a read: a fetched group or a frame read. */ - refresh(): void { - this.#touch(READ_BOOST); - } - - /** Record a write that bills no new bytes. */ - recordWrite(): void { - this.#touch(WRITE_BOOST); - } - - /** Make the group evictable, sampling its stamp into the pool's mean. Idempotent. */ - demote(): void { + /** Record an access, a read or a write, restarting the idle window. */ + touch(): void { const track = this.#track; - if (!track || this.#counted) return; - track.pool.accessInsert(this.#stamp); - this.#counted = true; + if (track) this.#stamp = Math.max(this.#stamp, track.pool.now()); } - /** Settle the account's debt and idle scan after a frame write. */ + /** Run the account's idle scan after a frame write. */ settle(): void { this.#track?.settle(); } - /** Release everything billed and the mean's sample, and detach from the account. Idempotent. */ + /** Detach from the account. Idempotent. */ clear(): void { - const track = this.#track; - if (!track) return; - track.pool.sub(this.#bytes); - this.#bytes = 0; - if (this.#counted) track.pool.accessRemove(this.#stamp); - this.#counted = false; this.#track = undefined; } - - // Advance the stamp to the current tick with `boost`, never moving it back. - #touch(boost: number): void { - const track = this.#track; - if (!track) return; - const stamp = track.pool.stamp(boost); - if (stamp <= this.#stamp) return; - if (this.#counted) track.pool.accessRefresh(this.#stamp, stamp); - this.#stamp = stamp; - } } diff --git a/js/net/src/group.test.ts b/js/net/src/group.test.ts index 0c21112a89c..2dfaa2922be 100644 --- a/js/net/src/group.test.ts +++ b/js/net/src/group.test.ts @@ -919,34 +919,3 @@ test("a drained unfinished group fails with Old when its budget gives up", async expect(consumer.done).toBe(true); expect(await consumer.closed).toBe(err as StreamError); }); - -// Rust's `GroupState::shed` keeps an open group's newest committed frame even while a newer -// frame is still being written, since that partial frame is not readable yet. -test("a debt shed keeps the newest committed frame behind one being written", async () => { - const producer = new Producer(0n); - producer.writeFrame({ payload: new Uint8Array(1024), timestamp: Timestamp.unscaled(producer.sequence) }); - const reader = producer.consume(); - producer.createFrame({ size: 10, timestamp: Timestamp.unscaled(producer.sequence) }).write(new Uint8Array(5)); - - expect(hooks.shedGroup(producer, Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER, [])).toBe(0); - expect((await reader.readFrame())?.payload.byteLength).toBe(1024); -}); - -// A finished group no longer tees into its copies, so shedding it ends the copies its track -// holds that had not taken the shed frames, rather than crediting bytes they still hold. -test("a debt shed of a finished group ends the copies that lost a frame", async () => { - const producer = new Producer(0n); - producer.writeFrame({ payload: new Uint8Array(1024), timestamp: Timestamp.unscaled(producer.sequence) }); - producer.writeFrame({ payload: new Uint8Array(1024), timestamp: Timestamp.unscaled(producer.sequence) }); - producer.finish(); - const behind = producer.consume(); - const drained = producer.consume(); - await drained.readFrame(); - await drained.readFrame(); - - expect( - hooks.shedGroup(producer, Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER, [behind, drained]), - ).toBeGreaterThan(0); - await expect(behind.readFrame()).rejects.toMatchObject({ code: StreamCode.Evicted }); - expect(await drained.readFrame()).toBeUndefined(); -}); diff --git a/js/net/src/group.ts b/js/net/src/group.ts index 7a199fe7c91..06707c26530 100644 --- a/js/net/src/group.ts +++ b/js/net/src/group.ts @@ -103,7 +103,7 @@ class GroupState { offset = 0; cacheBytes = 0; - // What the source copy bills its track's cache pool, while cached. A mirror shares its + // The source copy's access record in its track's cache, while cached. A mirror shares its // source's, so its reads count as accesses. charge?: Charge; compressed?: boolean; @@ -299,10 +299,7 @@ export class Producer { state.flate = flate; const frames = Array.from(state.frames.peek(), (frame) => flate.push(frame)); state.frames.set(new Queue(frames)); - const bytes = frames.reduce((bytes, frame) => bytes + frameBytes(frame), 0); - if (bytes > state.cacheBytes) state.charge?.bill(bytes - state.cacheBytes); - else state.charge?.sub(state.cacheBytes - bytes); - state.cacheBytes = bytes; + state.cacheBytes = frames.reduce((bytes, frame) => bytes + frameBytes(frame), 0); for (const mirror of group.#mirrors?.keys() ?? []) { mirror.frames.set(new Queue(frames.slice(mirror.start - state.start))); mirror.flate = flate; @@ -330,8 +327,6 @@ export class Producer { const error = failed.#state.closed.peek(); if (!(error instanceof Error)) throw new Error("group has not failed"); const state = next.#state; - // The failed copy's frames the continued mirrors hold, once each though mirrors share them. - const kept = new Set(); for (const [mirror, reads] of failed.#mirrors ?? []) { failed.#unmirror(mirror); // The successor continues a mirror from the frame it reached, so it must hold that @@ -354,7 +349,6 @@ export class Producer { mirror.closed.set(evicted()); continue; } - for (const frame of mirror.frames.peek()) kept.add(frame); mirror.from = mirror.total.peek(); mirror.charge = state.charge; if (state.compressed) mirror.flate = state.flate; @@ -362,9 +356,6 @@ export class Producer { for (const frame of state.frames.peek()) if (index++ >= mirror.from) appendFrame(mirror, frame); next.#attach(mirror, reads); } - let bytes = 0; - for (const frame of kept) bytes += frameBytes(frame); - return bytes; }; hooks.startGroup = (group, start) => { if (!Number.isSafeInteger(start) || start < 0) throw new RangeError("invalid group frame start"); @@ -413,11 +404,8 @@ export class Producer { return bytes - head > head * ratio; }; hooks.chargeGroup = (group, charge) => { - const state = group.#state; - state.charge = charge; - charge?.bill(state.cacheBytes); + group.#state.charge = charge; }; - hooks.shedGroup = (group, debt, cap, readers) => group.#shed(debt, cap, readers); hooks.limitGroup = (group, frames) => { // A closed group takes no more frames, but one already past the cap still // contradicts the end. @@ -576,7 +564,7 @@ export class Producer { if (this.#open !== open) throw new Error("group is closed"); const bytes = entry.write(chunk); this.#state.cacheBytes += bytes; - this.#state.charge?.add(bytes); + this.#state.charge?.touch(); this.#trim(); this.#state.charge?.settle(); }, @@ -584,7 +572,7 @@ export class Producer { if (this.#open !== open) throw new Error("group is closed"); this.#open = undefined; entry.arrived = now(); - this.#state.charge?.recordWrite(); + this.#state.charge?.touch(); entry.done.set(null); // Wake whole-frame readers waiting for it to complete. this.#state.frames.mutate(() => {}); @@ -636,9 +624,8 @@ export class Producer { } #append(frame: Frame, commits: (() => void)[] | undefined) { - const bytes = frameBytes(frame); - this.#state.cacheBytes += bytes; - this.#state.charge?.add(bytes); + this.#state.cacheBytes += frameBytes(frame); + this.#state.charge?.touch(); appendFrame(this.#state, frame); if (commits) { this.#admits = undefined; @@ -652,7 +639,7 @@ export class Producer { else if (index >= mirror.total.peek()) appendFrame(mirror, frame); } this.#trim(); - // Past the frame's own bookkeeping: paying the track's debt may evict other groups. + // Past the frame's own bookkeeping: the track's idle scan may evict other groups. this.#state.charge?.settle(); } @@ -677,33 +664,6 @@ export class Producer { this.#shedMirrors(bytes); } - // Shed whole frames from the front toward the cache pool's `debt`, freeing about `cap` at - // most, for a group the debt cannot evict whole: one being read, or the live edge. A frame - // larger than what is left of the debt stays, with everything after it. An open group keeps - // its newest committed frame, for the reader about to take it, and any frame still being - // written. A finished group no longer tees into its mirrors, so it ends `readers`, the copies - // its track still holds, that had yet to take a shed frame. Returns the bytes freed. Mirrors - // Rust's `GroupState::shed`. - #shed(debt: number, cap: number, readers: Iterable): number { - const state = this.#state; - const closed = state.closed.peek(); - if (closed instanceof Error) return 0; - const frames = state.frames.peek(); - const last = frames.last; - const writing = last instanceof Chunked && last.done.peek() === undefined; - const keep = closed === null ? 0 : writing ? 2 : 1; - const bytes = state.cacheBytes; - while (frames.length > keep && bytes - state.cacheBytes < cap) { - const front = frames.first; - if (!front || (front instanceof Chunked && front.done.peek() === undefined)) break; - if (frameBytes(front) > debt - (bytes - state.cacheBytes)) break; - this.#shift(); - } - this.#shedMirrors(bytes); - if (closed === null) for (const reader of readers) shedReader(reader, state.start); - return bytes - state.cacheBytes; - } - // Drop the front frame. #shift(): void { const state = this.#state; @@ -719,7 +679,6 @@ export class Producer { const state = this.#state; const frames = state.frames.peek(); if (state.cacheBytes === bytes) return; - state.charge?.sub(bytes - state.cacheBytes); state.flate?.shed(state.start); const lost = headShed(frames.first); for (const mirror of this.#mirrors?.keys() ?? []) { @@ -802,8 +761,6 @@ export class Producer { } let makeConsumer: (state: GroupState) => Consumer; -// End a track-held copy of a finished group that had yet to take frame `start - 1`, now shed. -let shedReader: (group: Consumer, start: number) => void; // A replacement copy asked for on behalf of one consumer, settled or still out. interface Attempt { @@ -1150,12 +1107,7 @@ export class Consumer { }; hooks.readGroupFrame = (group, compressed, from = 0) => group.#readFrame(from, compressed, true); hooks.evictGroup = (group, lost) => group.#evict(lost); - shedReader = (group, start) => { - // A copy that took every shed frame keeps reading; one that had not lost them. - const state = group.#state; - if (state === group.#mirrored && Math.max(state.start, state.from) < start) group.#evict(evicted()); - }; - hooks.refreshGroup = (group) => group.#state.charge?.refresh(); + hooks.refreshGroup = (group) => group.#state.charge?.touch(); hooks.onShedRead = (group, notify) => { group.#state.shedRead = notify; }; @@ -1339,7 +1291,7 @@ export class Consumer { const sequence = this.#state.start++; this.#streamed = front; this.#refresh(); - this.#state.charge?.refresh(); + this.#state.charge?.touch(); return { sequence, frame: front, chunks: makeFrameConsumer(sequence, front) }; } if (front.done.peek() === undefined) return undefined; @@ -1362,7 +1314,7 @@ export class Consumer { if (frames.length === 0 && this.#state.closed.peek() !== undefined) this.#state.flate = undefined; const sequence = this.#state.start++; this.#refresh(); - this.#state.charge?.refresh(); + this.#state.charge?.touch(); return { sequence, frame, inflated: slice?.inflated }; } diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index 164c671d701..973c02e0a83 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -8,7 +8,7 @@ import type { Duration, Instant } from "@moq/clock"; import type { Dispose, GetPromise, Getter } from "@moq/signals"; import type { Consumer as BroadcastConsumer, Producer as BroadcastProducer } from "./broadcast.ts"; -import type { Reader as CacheReader, Charge, Pool } from "./cache.ts"; +import type { Charge, Pool } from "./cache.ts"; import type * as datagram from "./datagram.ts"; import type { Delivered } from "./delivered.ts"; import { StreamCode, StreamError, toStreamCode } from "./error.ts"; @@ -525,11 +525,6 @@ export interface ResumeSource { * route change keeps, and those the serving copy reported. */ reofferStale(): Run[]; - /** - * The bill for the reader's record on the serving copy's account: `current` while it bills that - * account, or else a new one. `current` without a serving copy. - */ - bill(current: CacheReader | undefined): CacheReader | undefined; /** Let go of every copy. */ close(abort?: Error): void; } @@ -613,8 +608,6 @@ export const hooks: { releaseTrack: (producer: Producer) => void; /** Cache a new track's groups into its broadcast's `pool`, before it caches any. Assigned by `track.ts`. */ poolTrack: (producer: Producer, pool: Pool) => void; - /** How many runs a track's arrivals hold, for tests. Assigned by `track.ts`. */ - arrivalRuns: (producer: Producer) => number; /** * Settles once the producer is released (closed or aborted), unlike its public `closed`, * which also settles on a reached end while the producer keeps serving. Assigned by `track.ts`. @@ -797,17 +790,6 @@ export const hooks: { * subscriber's own record. Mirrors the Rust crate-private `track::Subscriber::record_mut`. */ wire: (subscriber: Subscriber) => WireRecord; - /** - * Open a bill against a track's cache account for what a session keeps for it beside the - * cache, such as its tail's record of finished streams, billed as a reader's record is, so - * the pool can hand it debt. Mirrors the Rust crate-private `track::Producer::bill`. - */ - billTrack: (producer: Producer) => CacheReader; - /** - * The bill on the account of the track `subscriber` reads: `current` while it bills that account, - * or else a new one; undefined while it has none. - */ - billSubscriber: (subscriber: Subscriber, current: CacheReader | undefined) => CacheReader | undefined; /** * Re-offer what a subscriber gave up as stale through dispositions, as a wire whose every update * reopens its stale drops does: each run is judged again under the new terms. Returns the runs @@ -891,13 +873,8 @@ export const hooks: { writeGroupFrame: (group: GroupProducer, frame: Frame, inflated?: number) => void; /** Payload footprint including a shared inflated copy, for measurements. */ groupCacheBytes: (group: GroupProducer) => number; - /** Bill a cached group's bytes to `charge` from now on, or stop billing them with undefined. */ + /** Record a cached group's accesses to `charge` from now on, or stop with undefined. */ chargeGroup: (group: GroupProducer, charge: Charge | undefined) => void; - /** - * Shed whole frames from a group's front toward a cache `debt`, freeing about `cap` at most, - * and end the `readers` its track holds that lost a shed frame; returns the bytes freed. - */ - shedGroup: (group: GroupProducer, debt: number, cap: number, readers: Iterable) => number; /** Frame count without creating a read handle. */ groupFrameCount: (group: GroupProducer) => number; /** @@ -962,10 +939,9 @@ export const hooks: { holdGroup: (group: GroupProducer) => () => void; /** * Hand `next` the readers `failed` kept open while held, teeing each from the frame it reached. - * Fails those `next` cannot serve. Returns the bytes of `failed`'s frames they still hold. - * Assigned by `group.ts`. + * Fails those `next` cannot serve. Assigned by `group.ts`. */ - succeedGroup: (next: GroupProducer, failed: GroupProducer) => number; + succeedGroup: (next: GroupProducer, failed: GroupProducer) => void; /** Read a payload chunk, releasing its wait when the output stream closes. */ readFrameChunk: (frame: FrameConsumer, closed: Promise) => Promise; /** Read the next frame, as its stored DEFLATE slice when `compressed`, so a lite-07 publisher forwards it without inflating. */ @@ -1036,9 +1012,6 @@ export const hooks: { poolTrack: () => { throw new Error("track.ts not loaded"); }, - arrivalRuns: () => { - throw new Error("track.ts not loaded"); - }, trackReleased: () => { throw new Error("track.ts not loaded"); }, @@ -1141,12 +1114,6 @@ export const hooks: { wire: () => { throw new Error("track.ts not loaded"); }, - billTrack: () => { - throw new Error("track.ts not loaded"); - }, - billSubscriber: () => { - throw new Error("track.ts not loaded"); - }, supersedeStale: () => { throw new Error("track.ts not loaded"); }, @@ -1267,9 +1234,6 @@ export const hooks: { chargeGroup: () => { throw new Error("group.ts not loaded"); }, - shedGroup: () => { - throw new Error("group.ts not loaded"); - }, groupFrameCount: () => { throw new Error("group.ts not loaded"); }, diff --git a/js/net/src/ledger.test.ts b/js/net/src/ledger.test.ts index d8c5a6c4986..cd790fa16dd 100644 --- a/js/net/src/ledger.test.ts +++ b/js/net/src/ledger.test.ts @@ -47,21 +47,6 @@ test("demand changes age order and restarts the entire intersecting gap", () => expect(ledger.hasGaps()).toBe(false); }); -test("a reclaim folds the oldest gap first however young, never the leading one", () => { - const ledger = new Ledger(); - ledger.account(2n, 3n, at(0)); - ledger.account(4n, 5n, at(10)); - ledger.account(6n, 7n, at(20)); - // Demand restarts the gap below 4, so the one above it is now the older. - ledger.demand(3n, 4n, at(30)); - expect(ledger.size).toBe(3); - expect(ledger.foldOldest()).toEqual({ start: 5n, end: 6n }); - expect(ledger.foldOldest()).toEqual({ start: 3n, end: 4n }); - expect(ledger.covers(2n, 7n)).toBe(true); - expect(ledger.foldOldest()).toBeUndefined(); - expect(ledger.size).toBe(1); -}); - test("a leading gap keeps its age until a lower arrival makes it foldable", () => { const ledger = new Ledger(); ledger.account(10n, 11n, at(0)); diff --git a/js/net/src/ledger.ts b/js/net/src/ledger.ts index 100f87491bf..818399320d3 100644 --- a/js/net/src/ledger.ts +++ b/js/net/src/ledger.ts @@ -19,11 +19,6 @@ export function grace(maxDelay: Duration): Duration { return maxDelay.isZero() ? GRACE : maxDelay; } -// Bun 1.4's heap (bun:jsc `heapStats`) grows about 191 bytes per disjoint run of a ledger, its -// age entry and timestamp included. -/** Bookkeeping a track bills per run of its arrivals, so a peer fragmenting them costs what the cache would hold. */ -export const RUN_OVERHEAD = 192; - // A run of accounted sequences `[start, end)`, and when the gap below it opened, which is // when its missing groups became late. type Run = { start: bigint; end: bigint; since: Instant }; @@ -110,33 +105,11 @@ export class Ledger { } } - /** - * Fold the oldest gap into the runs around it however young, giving up on it early, and - * return the sequences it held; undefined when no gap is left to fold. - */ - foldOldest(): { start: bigint; end: bigint } | undefined { - const run = this.#next; - if (!run) return undefined; - const below = this.#runs.lower(run.start); - if (!below) throw new Error("a folding gap must have a lower run"); - const gap = { start: below.end, end: run.start }; - below.end = run.end; - this.#remove(run); - if (this.#last === run) this.#last = below; - this.#nextExpiry(); - return gap; - } - /** Whether a gap separates two runs, so {@link expire} has anything to fold. */ hasGaps(): boolean { return this.#runs.size > 1; } - /** How many runs it holds: one more than its gaps. */ - get size(): number { - return this.#runs.size; - } - /** The lowest sequence accounted for. */ first(): bigint | undefined { return this.#runs.first()?.start; diff --git a/js/net/src/lite/drop_record.test.ts b/js/net/src/lite/drop_record.test.ts index 19361b862a0..7198a775f42 100644 --- a/js/net/src/lite/drop_record.test.ts +++ b/js/net/src/lite/drop_record.test.ts @@ -7,14 +7,14 @@ import { Duration, Elapsed, timeout } from "@moq/clock"; import type { Manual } from "@moq/clock/test"; import * as cache from "../cache.ts"; import { accept, connect } from "../connection/index.ts"; -import { StreamCode, StreamError, TooFarBehind, toStreamCode } from "../error.ts"; +import { StreamCode, StreamError, toStreamCode } from "../error.ts"; import { hooks, type Recv, staleHole } from "../internal.ts"; import { createMockTransportPair, manual } from "../mock.ts"; import { Producer as OriginProducer } from "../origin.ts"; import * as Path from "../path.ts"; import type { Reader } from "../stream.ts"; import { Timescale, Timestamp } from "../time.ts"; -import type { Subscription, Producer as TrackProducer, Subscriber as TrackSubscriber } from "../track.ts"; +import type { Subscription, Subscriber as TrackSubscriber } from "../track.ts"; import { wireOf } from "../wire.ts"; import { SubscribeDrop, SubscribeSkip, SubscribeStart, SubscribeUpdate } from "./subscribe.ts"; import { ALPN_06, ALPN_07_WIP, Version } from "./version.ts"; @@ -104,24 +104,13 @@ function tap() { }; } -/** Every track's cache pool, in the order the tracks open. */ -function pools(): { producer: TrackProducer; pool: cache.Pool }[] { - const opened: { producer: TrackProducer; pool: cache.Pool }[] = []; - const original = hooks.poolTrack; - spyOn(hooks, "poolTrack").mockImplementation((producer, pool) => { - opened.push({ producer, pool }); - original(producer, pool); - }); - return opened; -} - /** * A publisher's session on `up` to a relay, and the relay's session on `down` to a viewer. The * relay serves the viewer from its received copy of the publisher's broadcast, as a JS relay does. - * The publisher's track caches into a pool that never ages a group out, so it still holds what - * the relay and the viewer evict on the manual clock, unless a test resizes it. + * The publisher's track caches into a pool that ages a group out only past `expiry`, never by + * default, so it still holds what the relay and the viewer evict on the manual clock. */ -async function chain(up: Version, down: Version) { +async function chain(up: Version, down: Version, expiry?: Duration) { const alpn = (version: Version) => (version === Version.DRAFT_07 ? ALPN_07_WIP : ALPN_06); const upstream = createMockTransportPair(alpn(up)); const downstream = createMockTransportPair(alpn(down)); @@ -147,13 +136,11 @@ async function chain(up: Version, down: Version) { })(); const broadcast = source.route(path).broadcast; const track = broadcast.createTrack("video", { timescale: Timescale.MILLI, maxAge: null }); - const pool = new cache.Pool(); - hooks.poolTrack(track, pool); + hooks.poolTrack(track, new cache.Pool({ expiry })); const remote = wireOf(viewer).consume(path); return { track, - pool, remote, /** Write group `sequence` presenting at `millis`, with `size` bytes of payload. */ write(sequence: bigint, millis: number, size = 1) { @@ -274,6 +261,9 @@ function behindTheEdge(write: (sequence: bigint, millis: number) => void) { write(sequence, millis); } +// Short enough for the publisher to age out what the relay and the viewer still hold. +const PUBLISHER_EXPIRY = Duration.millis(10_000); + // A reader with a 1.5s budget is reported groups 1 and 2 stale through the relay, and those OLD // drops are still in flight to the viewer when it widens to 10s, which the publisher, having // evicted both, can only drop again. With `prompt`, that re-drop goes out under the widening @@ -281,7 +271,7 @@ function behindTheEdge(write: (sequence: bigint, millis: number) => void) { async function widening(prompt: boolean): Promise { using clock = manual(); const wire = tap(); - const chained = await chain(Version.DRAFT_07, Version.DRAFT_07); + const chained = await chain(Version.DRAFT_07, Version.DRAFT_07, PUBLISHER_EXPIRY); const reader = chained.remote.track("video").subscribe({ delay: { max: Duration.millis(1500) } }); await chained.reached(); chained.write(0n, 0); @@ -299,8 +289,8 @@ async function widening(prompt: boolean): Promise { ["drop", 2n, 3n, StreamCode.Old, 0n], ]); - // The publisher no longer holds them: it evicts everything but the live edge. - chained.pool.resize(1); + // The publisher no longer holds them: everything but its live edge ages out. + await clock.advance(PUBLISHER_EXPIRY.mul(2)); chained.write(5n, 6100); expect(settled(await next(reader))).toEqual(["group", 5n]); @@ -547,85 +537,19 @@ test("through a lite-06 upstream, a group reported dropped as it lands is fetche await chained.close(); }); -// Mirrors Rust `pool_pressure_folds_final_runs_and_ends_a_stale_reader` in -// rs/moq-net/tests/integration/drop_record.rs. -for (const stale of [false, true]) { - const outcome = stale - ? "holding stale reports within reach ends with TooFarBehind" - : "folds what it settled for good"; - test(`under pool pressure on the relay, a reader ${outcome}`, async () => { - using clock = manual(); - const opened = pools(); - const chained = await chain(Version.DRAFT_07, Version.DRAFT_07); - const budget = 1000; - const reader = chained.remote.track("video").subscribe({ delay: { max: Duration.millis(budget) } }); - // The viewer's copy opens as the reader subscribes, then the relay's as the request reaches it. - await chained.reached(); - const relay = opened[3]; - expect(opened.slice(0, 2).every((entry) => entry.producer === chained.track)).toBe(true); - expect(relay.producer).not.toBe(chained.track); - chained.write(0n, 0); - expect(settled(await next(reader))).toEqual(["group", 0n]); - - // A flood of drops alternating with skips, which the relay forwards and its reader records. - const runs = 200n; - const hole = () => (stale ? staleHole(budget) : { error: new StreamError(StreamCode.Cancel) }); - for (let run = 0n; run < runs; run++) { - hooks.dropTrackGroups(chained.track, 2n * run + 1n, 2n * run + 2n, hole()); - chained.track.skipGroups(2n * run + 2n, 2n * run + 3n); - } - let reported = 0n; - while (reported < 2n * runs) { - const recv = await next(reader); - expect(recv.kind).toBe("drop"); - if (recv.kind === "drop") reported += recv.end - recv.start; - } - expect(relay.pool.used).toBeGreaterThanOrEqual(Number(2n * runs) * cache.READER_RUN_OVERHEAD); - - // The relay's pool shrinks, and the groups its reader serves accrue the debt: its track - // sheds what it can first, then the reader pays. - const capacity = 64 * cache.RUN_OVERHEAD; - relay.pool.resize(capacity); - let error: unknown; - let groups = 0; - for (let sequence = 2n * runs + 1n; sequence < 2n * runs + 65n && error === undefined; sequence++) { - chained.write(sequence, budget + Number(sequence), 16 * 1024); - const recv = await within(clock, reader); - if (recv?.kind === "error") error = recv.error; - else { - expect(settled(recv)).toEqual(["group", sequence]); - groups++; - } - } - if (stale) { - expect(error).toBeInstanceOf(TooFarBehind); - } else { - expect(error).toBeUndefined(); - expect(groups).toBe(64); - expect(relay.pool.used).toBeLessThanOrEqual(capacity + 2 * (16 * 1024 + cache.ENTRY_OVERHEAD)); - } - expect(chained.ended()).toBe(false); - - reader.close(); - await chained.close(); - }); -} - // Groups 1 and 2 are judged stale for the relay's subscription, under a 1.5s budget, a run the -// relay's copy shares with all its readers, then the relay `lost` that run: it aged out past the -// pool's idle window, or the pool shed it under pressure. The readers in `readers` subscribe next: +// relay's copy shares with all its readers, then the relay forgot that run: it aged out past the +// pool's idle window. The readers in `readers` subscribe next: // a narrow one is judged stale for groups 1 and 2 while the viewer still holds the run, and takes // them once every hop forgot it, and a wide one is filled them through the relay from // the publisher, which still holds them: fetched once the viewer's copy forgot the run too, and // re-offered over both subscriptions by the widening update while it still holds it stale. -async function lostRun(lost: "forgot" | "shed", readers: ("wide" | "narrow")[]): Promise { +async function lostRun(readers: ("wide" | "narrow")[]): Promise { using clock = manual(); - const opened = pools(); const wire = tap(); const chained = await chain(Version.DRAFT_07, Version.DRAFT_07); const reader = chained.remote.track("video").subscribe({ delay: { max: Duration.millis(1500) } }); await chained.reached(); - const relay = opened[3]; chained.write(0n, 0); expect(settled(await next(reader))).toEqual(["group", 0n]); behindTheEdge(chained.write); @@ -636,15 +560,10 @@ async function lostRun(lost: "forgot" | "shed", readers: ("wide" | "narrow")[]): ["drop", 2n, 3n, StreamCode.Old], ]); - // Past the pool's idle window the relay's copy and the viewer's forget the run; a pool too - // small for one large group sheds the relay's. The pool then grows back, so the relay holds - // the groups it is filled next until its readers take them: one too small to hold any would - // see a refill evicted again, which its reader gives up and names dropped. - if (lost === "forgot") await clock.advance(cache.DEFAULT_EXPIRY.mul(2)); - else relay.pool.resize(cache.RUN_OVERHEAD); + // Past the pool's idle window the relay's copy and the viewer's forget the run. + await clock.advance(cache.DEFAULT_EXPIRY.mul(2)); chained.write(5n, 6100, 64 * 1024); expect(settled(await next(reader))).toEqual(["group", 5n]); - if (lost === "shed") relay.pool.resize(); const before = chained.fetches(); const heard = wire.heard.length; let received: bigint[] = []; @@ -656,7 +575,7 @@ async function lostRun(lost: "forgot" | "shed", readers: ("wide" | "narrow")[]): expect(filled.filter((row) => row[0] !== "group")).toEqual([]); received = groupsOf(filled) as bigint[]; expect(received).toEqual(expect.arrayContaining([1n, 2n])); - if (lost === "forgot") expect(chained.fetches().up).toBeGreaterThan(before.up); + expect(chained.fetches().up).toBeGreaterThan(before.up); wide.close(); } else { // Judged stale by its own budget while the viewer still holds the run. Once every hop forgot @@ -666,11 +585,7 @@ async function lostRun(lost: "forgot" | "shed", readers: ("wide" | "narrow")[]): delay: { max: Duration.millis(1500) }, ranges: [{ start: { absolute: 1n } }], }); - const judged = groupsOf(await drain(clock, narrow)); - if (lost === "shed") { - expect(judged).not.toContain(1n); - expect(judged).not.toContain(2n); - } else expect(judged).toEqual([1n, 2n, 3n, 4n, 5n]); + expect(groupsOf(await drain(clock, narrow))).toEqual([1n, 2n, 3n, 4n, 5n]); narrow.close(); } } @@ -699,15 +614,13 @@ async function lostRun(lost: "forgot" | "shed", readers: ("wide" | "narrow")[]): await chained.close(); } -for (const lost of ["forgot", "shed"] as const) { - // Mirrors Rust `a_shed_run_is_judged_or_filled_through_the_relay` in - // rs/moq-net/tests/integration/drop_record.rs. - test(`a sequence whose run the relay ${lost} is judged by a narrow reader`, () => lostRun(lost, ["narrow"])); +// Mirrors Rust `a_shed_run_is_judged_or_filled_through_the_relay` in +// rs/moq-net/tests/integration/drop_record.rs. +test("a sequence whose run the relay forgot is judged by a narrow reader", () => lostRun(["narrow"])); - // Mirrors Rust `a_shed_run_is_judged_or_filled_through_the_relay` in - // rs/moq-net/tests/integration/drop_record.rs. - test(`a sequence whose run the relay ${lost} is filled for a wide reader`, () => lostRun(lost, ["narrow", "wide"])); -} +// Mirrors Rust `a_shed_run_is_judged_or_filled_through_the_relay` in +// rs/moq-net/tests/integration/drop_record.rs. +test("a sequence whose run the relay forgot is filled for a wide reader", () => lostRun(["narrow", "wide"])); const BILLIONS = 1n << 40n; diff --git a/js/net/src/lite/publisher.ts b/js/net/src/lite/publisher.ts index 51783294295..ba3f8f5e99c 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -1659,17 +1659,13 @@ export class Publisher { skipped.length = 0; } if (pendingDrops.length > 0) { - for (const { start, end, error: failed } of pendingDrops.splice(0)) { - let error = failed; + for (const { start, end, error } of pendingDrops.splice(0)) { const old = hasSkip(this.version) && toStreamCode(error) === StreamCode.Old; - // The pool folded the run while its stream was open, which is final for this - // reader: no update would re-offer a stale drop on it. Mirrors the Rust `TrackRun`. - if (old && record?.folded(end - 1n)) error = new StreamError(StreamCode.Evicted); // A stream expired under this subscription's budget is a stale verdict, which // a later verdict or group replaces as any stale drop sent, and each update drops // again. A re-offer an update owed again is given up now, so the end sweep never // names it. - else if (old) toldStale(start, end); + if (old) toldStale(start, end); else record?.tell(start, end, reportOf({ error })); const msg = SubscribeDrop.given(start, end, error, this.version, updateNumber); if (!(await controls.response(encodeSubscribeResponse(stream, { drop: msg }, this.version)))) @@ -1943,22 +1939,6 @@ export class Publisher { // A lite-07 stale drop is no final verdict, and a Group Stream wins over it, so a group // handed out after one is served, as one a route the front switched to delivers: the // update that reopens the drop would never re-offer it. - // The pool folded a re-offered group while it waited, which is final for this reader: - // tell the wire it is gone, since an update re-offered it. Mirrors the Rust `TrackRun`. - if (again && record?.folded(group.sequence)) { - group.close(); - const evicted = new StreamError(StreamCode.Evicted); - const msg = SubscribeDrop.given( - group.sequence, - group.sequence + 1n, - evicted, - this.version, - updateNumber, - ); - if (!(await controls.response(encodeSubscribeResponse(stream, { drop: msg }, this.version)))) - return false; - continue; - } if (record?.toldStale(group.sequence)) { record.replaceDrops(group.sequence, group.sequence + 1n); } else if (record?.told(group.sequence)) { diff --git a/js/net/src/lite/subscriber.test.ts b/js/net/src/lite/subscriber.test.ts index 5a8ed3a9954..32fdf332483 100644 --- a/js/net/src/lite/subscriber.test.ts +++ b/js/net/src/lite/subscriber.test.ts @@ -3487,18 +3487,6 @@ test("a finished group wins over a late stale drop once evicted on lite-07", asy await send(old(2n, 2n)); await update(); expect(entry.tail.covers(2n, 3n)).toBe(false); - - // Under the pool's debt the tail folds what finished, gaps and all. A group in the span whose - // stream arrived before its stale drop still wins over it, as does a gap the grace gave up - // on; a gap there the drop accounted first is owed again. - for (const sequence of [3n, 5n]) await arrive(sequence); - await evict(); - for (const sequence of [11n, 13n, 15n]) await arrive(sequence); - expect(entry.tail.fold()).toBe(true); - await send(old(3n, 3n), old(4n, 3n), old(12n, 3n)); - await update(); - expect(entry.tail.covers(3n, 5n)).toBe(true); - expect(entry.tail.covers(12n, 13n)).toBe(false); } finally { responses.mockRestore(); track.close(); diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index 9e2e6a8251f..b01db3574a4 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -2423,10 +2423,7 @@ export class Subscriber { // The effective max delay is the stopgap grace: the wrong clock (it bounds // presentation-time drift), but it is how long the subscriber was willing to wait // for a late group anyway, and no longer than the publisher keeps one fresh. - tail: new Tail({ - grace: () => hooks.tailGrace(producer), - bill: hooks.billTrack(producer), - }), + tail: new Tail({ grace: () => hooks.tailGrace(producer) }), requested: msg.startGroup, floor: msg.startGroup, asked: [...ranges], @@ -2898,7 +2895,7 @@ export class Subscriber { track: producer, compress: copy.compress, timescale: copy.timescale, - tail: new Tail({ grace: () => hooks.tailGrace(producer), bill: hooks.billTrack(producer) }), + tail: new Tail({ grace: () => hooks.tailGrace(producer) }), requested: msg.startGroup, floor: msg.startGroup, asked: ranges, @@ -3348,9 +3345,6 @@ export class Subscriber { // and a reader that wants it fills it meanwhile. if (stale && update < entry.update) continue; this.#reoffered(entry.track, start, end); - // Asked before this drop is accounted: what a fold forgot finishing that the ledger - // already accounted may be a stream this drop overtook. - const overtaken = stale ? entry.tail.overtaken(start, end) : []; // Lite-07 tells a stream a final drop overtook from one whose gap the grace gave up on. if (!stale && hasYield(this.version)) entry.tail.drop(start, end); else entry.tail.account(start, end); @@ -3365,10 +3359,9 @@ export class Subscriber { // A final drop is never reopened, even where it replaces a stale one. A sequence // already skipped stays skipped, and one whose group arrived keeps it, so no update // reopens either. - // Owed again by the next update, save what a stream may have won. - const owed = overtaken.reduce((runs, run) => removeRun(runs, run.start, run.end), recorded); + // Owed again by the next update. entry.stale = stale - ? owed.reduce((runs, run) => insertDrop(runs, run.start, run.end, hole), entry.stale ?? []) + ? recorded.reduce((runs, run) => insertDrop(runs, run.start, run.end, hole), entry.stale ?? []) : removeRun(entry.stale ?? [], start, end); entry.fills?.refuse(start, end, error); } else if ("skip" in resp) { diff --git a/js/net/src/resume.test.ts b/js/net/src/resume.test.ts index d673571dbad..da641f81f51 100644 --- a/js/net/src/resume.test.ts +++ b/js/net/src/resume.test.ts @@ -2,7 +2,6 @@ import { expect, spyOn, test } from "bun:test"; import { Duration, yieldNow } from "@moq/clock"; import { Manual } from "@moq/clock/test"; import { Consumer as BroadcastConsumer, Producer as BroadcastProducer } from "./broadcast.ts"; -import * as cache from "./cache.ts"; import { NotFound, SessionCode, SessionError, StreamCode, StreamError } from "./error.ts"; import { type Consumer as GroupConsumer, Producer as GroupProducer } from "./group.ts"; import { WireRoute } from "./hop.ts"; @@ -3739,31 +3738,3 @@ test("a group an inner front hands out already failed still continues on the out a.close(); b.close(); }); - -// A routed reader's record bills the pool of the copy serving it, and follows a route change: the -// replaced route's pool stops carrying it, so the pool serving the reader is the one whose debt can -// fold it. -test("a routed reader's record bills the serving route's pool across a route change", async () => { - const { a, b, active, swap, close } = await setup(); - const pools = { a: new cache.Pool({ capacity: 1 << 30 }), b: new cache.Pool({ capacity: 1 << 30 }) }; - const ta = copy(a); - const tb = copy(b); - hooks.poolTrack(ta.producer, pools.a); - hooks.poolTrack(tb.producer, pools.b); - const sub = active.track("seq").subscribe({ delay: { max: null } }); - // Every other group, so the reader's record holds a run per gap. - const runs = 64n; - for (let group = 0n; group < runs; group++) ta.write(2n * group); - expect(drain(sub).length).toBe(Number(runs)); - - await swap(); - tb.write(2n * runs); - const before = { a: pools.a.used, b: pools.b.used }; - expect(drain(sub)).toEqual([2n * runs]); - const bill = Number(runs - 1n) * cache.READER_RUN_OVERHEAD; - expect(before.a - pools.a.used).toBeGreaterThanOrEqual(bill); - expect(pools.b.used - before.b).toBeGreaterThanOrEqual(bill); - - sub.close(); - close(); -}); diff --git a/js/net/src/resume.ts b/js/net/src/resume.ts index a63cd79db24..cc73611d8c4 100644 --- a/js/net/src/resume.ts +++ b/js/net/src/resume.ts @@ -48,7 +48,6 @@ import { Duration, Instant, now, sleepUntil } from "@moq/clock"; import { type Dispose, type Getter, race, Signal } from "@moq/signals"; import * as broadcast from "./broadcast.ts"; -import type { Reader as CacheReader } from "./cache.ts"; import { Delivered } from "./delivered.ts"; import { NotFound, SessionError, StreamCode, StreamError, sessionClosed } from "./error.ts"; import type { Consumer as GroupConsumer } from "./group.ts"; @@ -737,12 +736,6 @@ class Reader implements ResumeSource { return { groups: groups.map((group) => this.#handOut(serving, group)), givenUp }; } - /** The bill on the serving copy's account, whose pool holds what this reader reads. */ - bill(current: CacheReader | undefined): CacheReader | undefined { - const serving = this.#serving(); - return serving ? hooks.billSubscriber(serving.sub, current) : current; - } - /** * Take back every stale run: those queued here, those this reader reported, and those the * serving copy reported, which its cursor owes again. What this reader reported counts diff --git a/js/net/src/tail.ts b/js/net/src/tail.ts index 519f3d0ac0b..a1d6c28ed12 100644 --- a/js/net/src/tail.ts +++ b/js/net/src/tail.ts @@ -1,16 +1,12 @@ import { type Duration, now, sleep } from "@moq/clock"; import { type GetPromise, type Getter, Signal } from "@moq/signals"; -import { READER_RUN_OVERHEAD, type Reader } from "./cache.ts"; import { insertRun, type Run, runGaps, runsFrom } from "./internal.ts"; import { GRACE, Ledger } from "./ledger.ts"; -import { max, min } from "./util/sequence.ts"; /** How a {@link Tail} measures its grace. */ export interface TailOptions { /** How long to wait for a group that cannot be accounted for. Read each time it is needed. */ grace?: () => Duration; - /** Bills the bytes the tail keeps to the subscription's copy's account, as a reader's record. */ - bill?: Reader; } /** A group stream a {@link Tail} counts, from its header until it is read to its end. */ @@ -54,70 +50,18 @@ export class Tail { // update names one only if it was sent before the publisher served the group, so the group won // over it, even once the cache evicted it. #finished: Run[] = []; - // The span of the finished runs a fold under the pool's debt forgot since the last update. A - // stale drop there for a group the ledger already accounted may be one its stream overtook, so - // the next update owes none of it again. - #folded?: Run; // Sequences a final drop named, so a stream whose header the drop overtook can be told from one - // whose gap the grace gave up on. Under the pool's debt they fold into one span, which excuses - // more streams for at most the grace rather than forget a drop and hold the end forever. + // whose gap the grace gave up on. #dropped: Run[] = []; - // What `#finished` and `#dropped` bill, `READER_RUN_OVERHEAD` per run, so a publisher fragmenting the - // streams it finishes between updates costs what the cache would hold. - #bill?: Reader; constructor(options: TailOptions = {}) { this.#grace = options.grace ?? (() => GRACE); - this.#bill = options.bill; } - // Bill what `#finished` and `#dropped` keep, and under the pool's debt fold them. Mirrors the Rust - // `Tail::rebill`, which keeps no `#dropped`. - #rebill(): void { - const bill = this.#bill; - if (!bill) return; - bill.set(this.#kept() * READER_RUN_OVERHEAD); - if (bill.debt <= 0) return; - const folded = this.fold(); - if (this.#foldDropped() || folded) bill.set(this.#kept() * READER_RUN_OVERHEAD); - } - - // Fold the dropped runs into the one span they reach, returning whether there was more than one. - #foldDropped(): boolean { - const dropped = this.#dropped; - if (dropped.length < 2) return false; - this.#dropped = [{ start: dropped[0].start, end: dropped[dropped.length - 1].end }]; - return true; - } - - #kept(): number { - return this.#finished.length + (this.#folded ? 1 : 0) + this.#dropped.length; - } - - /** - * Fold all but the newest finished run into the span a fold forgot, returning whether any was: - * a stale drop under this update then stands as the publisher judged it, but the update owes - * none of what it names that {@link overtaken} reports. The newest run stays, so streams - * finishing in sequence grow nothing to charge again. - */ - fold(): boolean { - if (this.#finished.length < 2) return false; - const start = this.#finished[0].start; - const end = this.#finished[this.#finished.length - 2].end; - const folded = this.#folded; - this.#folded = folded ? { start: min(folded.start, start), end: max(folded.end, end) } : { start, end }; - this.#finished = this.#finished.slice(-1); - return true; - } - - /** Release what the tail bills, once its subscription ended. */ + /** Release what the tail keeps beside its ledger, once its subscription ended. */ close(): void { this.#finished = []; - this.#folded = undefined; this.#dropped = []; - // A stream still in flight finishes after this: with no bill, it can never bill again. - this.#bill?.close(); - this.#bill = undefined; } /** Group streams whose header arrived, whether they finished or were reset. */ @@ -193,7 +137,6 @@ export class Tail { /** Record that the group stream for `sequence` arrived whole. */ finish(sequence: bigint): void { insertRun(this.#finished, sequence, sequence + 1n); - this.#rebill(); } /** The parts of `[start, end)` whose stream did not finish since the last update was sent. */ @@ -201,28 +144,12 @@ export class Tail { return runGaps(this.#finished, start, end); } - /** - * The parts of `[start, end)` inside the span a fold forgot that the ledger accounts for, asked - * before a stale drop of them is accounted: the header of each arrived, or the grace gave up on - * it, before the drop, so the drop may be one a finished stream overtook. - */ - overtaken(start: bigint, end: bigint): Run[] { - const folded = this.#folded; - if (!folded) return []; - const from = max(start, folded.start); - const to = min(end, folded.end); - if (from >= to) return []; - return runGaps(this.#ledger.gaps(from, to), from, to); - } - /** * Forget which streams finished, for an update being sent: a stale drop under an earlier one is * no verdict, so none needs them. */ updated(): void { this.#finished = []; - this.#folded = undefined; - this.#rebill(); } /** Re-check what {@link settle} waits on, after something outside the ledger changed. */ @@ -238,11 +165,10 @@ export class Tail { /** Record `[start, end)` as given up by a final drop, accounting for it. */ drop(start: bigint, end: bigint): void { insertRun(this.#dropped, start, end); - this.#rebill(); this.#account(start, end); } - /** Whether a final drop named `sequence`, or a fold under the pool's debt spans it. */ + /** Whether a final drop named `sequence`. */ dropped(sequence: bigint): boolean { const run = this.#dropped[runsFrom(this.#dropped, sequence)]; return run !== undefined && run.start <= sequence; diff --git a/js/net/src/track.test.ts b/js/net/src/track.test.ts index 8cb4f06f984..e6be84d4f0f 100644 --- a/js/net/src/track.test.ts +++ b/js/net/src/track.test.ts @@ -3461,6 +3461,11 @@ test("an upstream drop ends a group still arriving with its error", async () => producer.close(); }); +// Write a frame past a group's 32 MiB tail limit, shedding every frame before it. +function overflowTail(group: GroupProducer): void { + group.writeFrame({ payload: new Uint8Array(32 * 1024 * 1024 + 1), timestamp: Timestamp.unscaled(group.sequence) }); +} + // A reader on a received copy that fails waits out a duplicate still held for its group, then // continues on it from its frame with nothing repeated or missed. A released hold leaves the // failure, even to a reader only watching `closed`, as does a duplicate starting past the reader's @@ -3551,7 +3556,7 @@ test.each([ }); await settle(); expect(closed).toBeUndefined(); - hooks.shedGroup(again, Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER, []); + overflowTail(again); await settle(); expect(closed).toMatchObject({ code: StreamCode.Evicted }); producer.close(); @@ -3605,11 +3610,10 @@ test.each([ const again = hooks.receiveGroup(producer, { sequence: 3n, start: kind === "past" ? 2 : 0 }); arriving?.release(); if (kind === "shed") { - // The re-offer drops frames 0 and 1 to cache pressure before the reader reaches it. + // The re-offer drops frames 0 and 1 to its tail limit before the reader reaches it. again.writeFrame({ payload: enc.encode("a"), timestamp: Timestamp.unscaled(again.sequence) }); again.writeFrame({ payload: enc.encode("b"), timestamp: Timestamp.unscaled(again.sequence) }); - again.writeFrame({ payload: enc.encode("c"), timestamp: Timestamp.unscaled(again.sequence) }); - hooks.shedGroup(again, Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER, []); + overflowTail(again); await reading; expect(read).toMatchObject({ code: StreamCode.Evicted }); producer.close(); diff --git a/js/net/src/track.ts b/js/net/src/track.ts index 0ae2d4d0d05..0ce09c6b669 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -50,7 +50,7 @@ import { subtractRuns, type TrackRequestOptions, } from "./internal.ts"; -import { RUN_OVERHEAD as ARRIVAL_OVERHEAD, grace, Ledger } from "./ledger.ts"; +import { grace, Ledger } from "./ledger.ts"; import { rescale } from "./rescale.ts"; import { keyOf, Timescale, type Timestamp } from "./time.ts"; import { checkEnd, checkSequence, max, min, UNBOUNDED } from "./util/sequence.ts"; @@ -718,7 +718,7 @@ class TrackState { // Sequences the upstream publisher gave up, with why: an incoming SUBSCRIBE_SKIP or // SUBSCRIBE_DROP, or a producer cut. Kept apart from the cache so reclaim cannot // erase a disposition a downstream publisher still has to forward, and as runs so a wide - // DROP costs one entry. A sink shares its producer's, which bills it to the track's account. + // DROP costs one entry. A sink shares its producer's, which the pool's idle window forgets. drops = new DropSet(); // The runs this sink's own fills refused, which it reads as given up beside `drops`. refused = new Signal([]); @@ -747,9 +747,6 @@ class TrackState { // Bumped when a wire subscription declares or resolves where its live feed starts, so a // publisher holding its first group for that answer wakes. startChanged = new Signal(0); - // Bumped when paying eviction debt folds an arrival gap before its grace passes, so a reader - // parked on a missing group there, with no timer to wake it, judges it again. - folded = new Signal(0); latest?: bigint; // One past the highest group received, including evicted content. received = new Signal(0n); @@ -840,7 +837,7 @@ function after(a: Position, b: Position): boolean { } // A source group retained in the producer cache, with the mirror handed to each sink -// so eviction can drop them together, and what it all bills the cache pool. +// so eviction can drop them together, and its last access, which the cache pool ages it by. // `arrived` is when the group joined this track, which its mirrors age a predecessor from. // `bound` is its first frame's timestamp once checked to fit between its neighbors', which // bounds theirs; `unverified` marks one accepted below the eviction watermark instead. @@ -904,8 +901,6 @@ let resubscribe: (producer: Producer) => void; let owedRuns: (producer: Producer, start: bigint, end: bigint) => Iterable; let owedGroup: (producer: Producer, start: bigint, end: bigint) => { sequence: bigint; folds?: Instant } | undefined; let fillEdge: (producer: Producer) => bigint | undefined; -// The producer's cache account, which its readers' records bill to. -let accountOf: (producer: Producer) => cache.Track; let sourceStart: (producer: Producer) => bigint | "pending" | undefined; // The lowest group a reader naming `named` asks a fill for; see `Producer.#fillFloor`. let fillFloor: (producer: Producer, named: bigint | undefined) => bigint | undefined; @@ -928,10 +923,6 @@ function sameCoverage(a: Coverage, b: Coverage): boolean { ); } -/** - * Whether the live edge `received` reached `final`: no sequence below it is left that was neither - * written nor given up, as a declared skip or a drop. Mirrors the Rust `TrackState::reaches`. - */ // A run of the drop set and the pool tick it outlives the idle window at. interface Born extends Run { expires: number; @@ -1100,7 +1091,7 @@ export class Producer { #live = new Signal(undefined); // Recently written source groups, in arrival order, retained for replay to late subscribers - // until the cache pool evicts them, for its budget or once idle. A set, so evicting a run of + // until the cache pool evicts them once idle. A set, so evicting a run of // them is linear. Each entry tracks the mirror it handed to every sink so eviction can drop // them too: otherwise a slow consumer that never reads would pin old groups (and their frame // bytes) forever. @@ -1120,7 +1111,7 @@ export class Producer { // it may have lost its lower bound. One entry, so nothing grows with the cache. #evicted?: Start; // This track's account against its broadcast's cache pool (a pool of its own when it has no - // broadcast), which bills the cached groups and evicts them under its budget and idle window. + // broadcast), which evicts the cached groups once idle. #account: cache.Track; // The pool tick past which each run of the drop set outlives the pool's idle window, as of when // it was last given up; empty when the pool has no idle window. See `#setDrops`. @@ -1128,8 +1119,6 @@ export class Producer { // At most the lowest tick in `#dropsBorn`, so an idle scan finds nothing to forget without // walking it. #dropsOldest?: number; - // What the drop set and its expiry index bill to the account. - #dropsBilled = 0; // One independent downstream state per live subscriber. #sinks = new Set(); @@ -1157,9 +1146,6 @@ export class Producer { // subscription's grace after a later arrival opened it, as the session's tail does, so this // holds only the gaps still worth waiting for. #arrivals = new Ledger(); - // What `#arrivals` bills to the account, `ARRIVAL_OVERHEAD` per gap. Paying eviction debt folds - // its oldest gaps early, so an unbounded grace holds no more gaps than the pool would. - #arrivalsBilled = 0; // What this track's own publisher produced since its last append, which skips every // sequence below it that never arrived and was not given up: the producer moved on. The // arrivals cannot say so, since a gap folds into them once nobody waits for it. Only @@ -1179,7 +1165,6 @@ export class Producer { }, drop: (start, end, hole) => { this.#drop(start, end, hole); - this.#chargeDebt(); }, holds: (sequence) => { const entry = this.#cached.get(sequence); @@ -1196,25 +1181,18 @@ export class Producer { #openAccount(pool: cache.Pool): cache.Track { return new cache.Track(pool, { protects: (entry) => this.#protects(entry), - edge: () => { - const entry = this.#edgeEntry(); - return entry && this.#protects(entry) ? entry : undefined; - }, - evict: (entry, reason) => { - this.#evict(entry, reason); + evict: (entry) => { + this.#evict(entry); this.#uncache(entry, true); }, - shed: (entry, debt, cap) => hooks.shedGroup(entry.group, debt, cap, entry.mirrors.values()), settle: () => this.#settle(), - shedDrops: (debt, cap) => this.#shedDrops(debt, cap), - shedArrivals: (debt, cap) => this.#shedArrivals(debt, cap), expireDrops: (now) => this.#expireDrops(now), holdsDrops: () => (this.#state.drops.all[0]?.start ?? UNBOUNDED) < this.#keptDrops(), }); } - // Give up `[start, end)` as `hole` says, or forget it without one, billing the drop set to the - // track's account and dating each run against the pool's idle window. Mirrors the Rust + // Give up `[start, end)` as `hole` says, or forget it without one, dating each run against the + // pool's idle window. Mirrors the Rust // `TrackState::change_drops`. #setDrops(start: bigint, end: bigint, hole?: Hole): void { this.#state.drops.set(start, end, hole); @@ -1229,63 +1207,7 @@ export class Producer { this.#dropsBorn.set(start, end, { start, end, expires }); this.#dropsOldest = Math.min(this.#dropsOldest ?? expires, expires); } - this.#billDrops(); - } - - // Bill the drop set and its expiry index to the account as they now stand. - #billDrops(): void { - const billed = this.#state.drops.size * cache.RUN_OVERHEAD + this.#dropsBorn.size * cache.BORN_OVERHEAD; - if (billed > this.#dropsBilled) this.#account.bill(billed - this.#dropsBilled); - else this.#account.unbill(this.#dropsBilled - billed); - this.#dropsBilled = billed; - } - - // Forget the lowest runs of the drop set toward `debt`, at most about `cap` bytes, returning the - // bytes freed. Their sequences are unknown again: a reader judges them by its budget, or fills - // them by range. - #shedDrops(debt: number, cap: number): number { - let paid = 0; - const kept = this.#keptDrops(); - while (paid < debt && paid < cap) { - const run = this.#state.drops.all[0]; - if (!run || run.start >= kept) break; - const billed = this.#dropsBilled; - const end = min(run.end, kept); - this.#setDrops(run.start, end); - this.#forgot([{ start: run.start, end }]); - paid += billed - this.#dropsBilled; - } - return paid; - } - - // Bill the arrivals' gaps to the account as they now stand. The run they split is the track's - // own, so an in-order feed bills nothing. A closed track bills none: nothing arrives to add a - // gap, and its account may outlive it in the pool's sweep, which would keep them billed. - #billArrivals(): void { - const closed = this.#state.closed.peek() !== undefined; - const billed = closed ? 0 : Math.max(0, this.#arrivals.size - 1) * ARRIVAL_OVERHEAD; - if (billed > this.#arrivalsBilled) this.#account.bill(billed - this.#arrivalsBilled); - else this.#account.unbill(this.#arrivalsBilled - billed); - this.#arrivalsBilled = billed; - } - - // Fold the arrivals' oldest gaps toward `debt`, at most about `cap` bytes, returning the bytes - // freed: nothing waits on them any longer, as when their grace passes, so a reader parked on one - // judges it missing. Mirrors the Rust `TrackState::shed_arrivals`. - #shedArrivals(debt: number, cap: number): number { - let paid = 0; - let folded = false; - while (paid < debt && paid < cap && this.#arrivals.foldOldest()) { - const billed = this.#arrivalsBilled; - this.#billArrivals(); - paid += billed - this.#arrivalsBilled; - folded = true; - } - if (folded) { - for (const state of [this.#state, ...this.#sinks]) state.folded.update((revision) => revision + 1); - this.#owe(); - } - return paid; + this.#account.dropsChanged(); } // Forget the runs given up longer than the pool's idle window before tick `now`; walks the set @@ -1300,7 +1222,6 @@ export class Producer { if (expired.length > 0) { this.#state.drops.clear(expired); this.#dropsBorn.clear(expired); - this.#billDrops(); this.#forgot(expired); } this.#dropsOldest = undefined; @@ -1355,9 +1276,6 @@ export class Producer { throw new Error("track already caches groups"); producer.#account = producer.#openAccount(pool); }; - hooks.arrivalRuns = (producer) => producer.#arrivals.size; - accountOf = (producer) => producer.#account; - hooks.billTrack = (producer) => producer.#account.reader(); hooks.subscribing = (producer) => producer.#subscribing; hooks.requested = (producer) => producer.#state.update; resubscribe = (producer) => producer.#updateSubscription(); @@ -1409,12 +1327,7 @@ export class Producer { hooks.releaseTrack = (producer) => producer.#close(undefined); hooks.trackReleased = (producer) => producer.#state.closed; hooks.trackFinished = (producer) => producer.#state.end.peek() !== undefined; - hooks.dropTrackGroups = (producer, start, end, hole) => { - const runs = producer.#drop(start, end, hole, true); - // A peer's drops bill the pool as its groups do, so they pay their debt the same way. - producer.#chargeDebt(); - return runs; - }; + hooks.dropTrackGroups = (producer, start, end, hole) => producer.#drop(start, end, hole, true); hooks.skipTrackGroups = (producer, start, end) => producer.#skipReceived(start, end); hooks.supersedeStale = (producer, judged) => { const held = producer.#state.drops.all @@ -1466,8 +1379,8 @@ export class Producer { return undefined; } hooks.arriveGroup(mirror, entry.arrived); - // A cache hit is an access: the group outranks those nobody read. - entry.charge.refresh(); + // A cache hit is an access, restarting the group's idle window. + entry.charge.touch(); return mirror; }; reofferCached = (producer, sink, start, end, offer) => { @@ -1747,7 +1660,6 @@ export class Producer { // A group its recovery moved to another copy no longer reads this one. if (hooks.readsMirrored(mirror)) hooks.releaseGroupReader(mirror, abort); entry.mirrors.delete(sink); - entry.charge.sub(cache.MIRROR_OVERHEAD); } } for (const group of sink.groups.peek()) hooks.releaseGroupReader(group, abort); @@ -1793,7 +1705,6 @@ export class Producer { const dst = entry.group.mirror(); hooks.arriveGroup(dst, entry.arrived); if (entry.unverified) hooks.unverifyGroup(dst); - if (!entry.mirrors.has(sink)) entry.charge.bill(cache.MIRROR_OVERHEAD); entry.mirrors.set(sink, dst); if (sink.reads?.(dst.sequence) === false) hooks.groupReads(dst, false); timelineInsert(sink.timeline, dst); @@ -1804,16 +1715,13 @@ export class Producer { } // Drop a cached group's mirror from every sink so no consumer can pin it, the cache pool - // having reclaimed it for its bytes (`evicted`) or for idling (`expired`). - #evict(entry: CachedGroup, reason: "evicted" | "expired" = "expired"): void { + // having reclaimed it for idling, or a new copy having replaced it. + #evict(entry: CachedGroup): void { // Reclaiming a group the publisher never closed ends it as the gap it is, before // the mirrors below would otherwise report a clean finish to a reader that had // drained it. The usual case, an already-closed group aging out, keeps its own // terminal state. - const lost = - reason === "evicted" - ? new StreamError(StreamCode.Evicted, { message: "group evicted from the cache" }) - : new TooFarBehind(); + const lost = new TooFarBehind(); EVICTED.add(lost); if (!entry.group.isClosed) entry.group.abort(lost); const mirrors = [...entry.mirrors.values()].filter((mirror) => hooks.evictGroup(mirror, lost)); @@ -1822,9 +1730,9 @@ export class Producer { } // Take a failed fill answer out of every sink that has not read it, as Rust serves no failed - // fetch-only slot; #mirror gives it to no new sink either. The entry stays cached, billed and + // fetch-only slot; #mirror gives it to no new sink either. The entry stays cached and // reclaimable, as Rust keeps a spent slot until its scan reclaims it, so a reader already - // holding it keeps its frames and the fill's own failure while the pool still counts them. + // holding it keeps its frames and the fill's own failure. #withhold(entry: CachedGroup): void { for (const [sink, mirror] of entry.mirrors) { // A fetch's own outcome. @@ -1838,7 +1746,6 @@ export class Producer { if (sink.timeline[at] === mirror) sink.timeline.splice(at, 1); lose(sink, mirror.sequence); entry.mirrors.delete(sink); - entry.charge.sub(cache.MIRROR_OVERHEAD); hooks.releaseGroupReader(mirror, undefined); } } @@ -1847,11 +1754,10 @@ export class Producer { // receive them. A reader already holding one keeps it as is. #unlink(entry: CachedGroup): void { for (const [sink, mirror] of entry.mirrors) unlinkMirror(sink, mirror); - entry.charge.sub(entry.mirrors.size * cache.MIRROR_OVERHEAD); entry.mirrors.clear(); } - // Take a cached group out of the cache lookups, releasing what it billed the pool. + // Take a cached group out of the cache lookups and its track's account. #uncache(entry: CachedGroup, evicted = false): void { this.#cache.delete(entry); this.#cached.delete(entry.group.sequence); @@ -1964,11 +1870,6 @@ export class Producer { return this.#state.largest.peek(); } - // Accrue and pay the cache pool's eviction debt for what this track wrote. - #chargeDebt(): void { - if (this.#account.chargeDebt()) this.#settle(); - } - // Evict the groups idle past the pool's window: every one when `full`, else a bounded scan. #evictExpired(full: boolean): void { if (this.#account.evictExpired(full)) this.#settle(); @@ -1985,8 +1886,6 @@ export class Producer { fetched = false, succeeds?: { failed: GroupProducer; kept?: Map }, ): void { - // Before the group is cached, so the write creating it can never evict it. - this.#chargeDebt(); this.#settle(); const entry: CachedGroup = { group, @@ -1996,9 +1895,7 @@ export class Producer { fetched: fetched || undefined, }; hooks.chargeGroup(group, entry.charge); - // The failed copy's frames its continued readers still hold, billed until this entry leaves - // the cache, which evicts the sink mirrors holding them. - if (succeeds) entry.charge.bill(hooks.succeedGroup(group, succeeds.failed)); + if (succeeds) hooks.succeedGroup(group, succeeds.failed); this.#undrop(group.sequence); // No reader is handed a failed fill answer (see #withhold), withheld as it aborts so a // read that follows at once cannot take it: its sequence is unknown again, owed while the @@ -2049,7 +1946,6 @@ export class Producer { // Continued, it stays the sink's copy of the group, so the sink never holds two. if (mirror && mirror.closed.peek() === undefined) { entry.mirrors.set(sink, mirror); - entry.charge.bill(cache.MIRROR_OVERHEAD); continue; } if (mirror) unlinkMirror(sink, mirror); @@ -2192,7 +2088,6 @@ export class Producer { if (sequence >= start && sequence < end) insertRun(cached, sequence, sequence + 1n); } this.#skipGaps(cached, start, end); - this.#chargeDebt(); return cached; } @@ -2277,7 +2172,6 @@ export class Producer { kept ??= new Map(); kept.set(sink, mirror); existing.mirrors.delete(sink); - existing.charge.sub(cache.MIRROR_OVERHEAD); } // Subscribers may have seen the aborted incarnation's start, so it stays a watermark. this.#evict(existing); @@ -2352,7 +2246,6 @@ export class Producer { this.#arrivals.account(start, end, at); // Only a gap folds, so an in-order feed never reads the subscription. if (this.#arrivals.hasGaps()) this.#arrivals.expire(at, this.#grace()); - this.#billArrivals(); this.#owe(); } @@ -2513,17 +2406,13 @@ export class Producer { #close(abort: Error | undefined) { if (this.#state.closed.peek() !== undefined) return; // A gap past the grace was lost before the abort rather than cut off by it. - if (abort && this.#state.end.peek()?.tail) { - this.#arrivals.expire(now(), this.#grace()); - this.#billArrivals(); - } + if (abort && this.#state.end.peek()?.tail) this.#arrivals.expire(now(), this.#grace()); if (abort && (this.#state.outcome.peek() === null || this.#settled())) abort = undefined; // Nobody will finish these, so a subscriber that has not taken one yet never sees it. // Not evicted: a reader already holding one keeps its frames and sees the abort. const open = [...this.#cache].filter((entry) => entry.group.closed.peek() === undefined); const kept = this.#keptDrops(); closeTrackState(this.#state, abort); - this.#billArrivals(); if (this.#keptDrops() > kept) this.#rearmDrops(kept); if (this.#state.outcome.peek() === undefined) this.#state.outcome.set(abort ?? this.#state.end.peek()?.error ?? null); @@ -2546,7 +2435,7 @@ export class Producer { // never reclaims a group its subscribers have yet to drain. const edge = this.#edgeEntry(); if (edge) { - edge.charge.recordWrite(); + edge.charge.touch(); this.#account.demote(edge); } this.#evictExpired(true); @@ -2668,8 +2557,6 @@ function blocksHole(reported: Reported, hole: Hole): boolean { export interface WireRecord { /** Whether the wire was told of `sequence`: served, or given up. */ told(sequence: bigint): boolean; - /** Whether the pool folded `sequence` into the record's floor, final for this reader. */ - folded(sequence: bigint): boolean; /** Whether `sequence` went out as a stale drop, which a group still replaces. */ toldStale(sequence: bigint): boolean; /** The parts of `[start, end)` the wire was told nothing of. */ @@ -2782,13 +2669,6 @@ export class Subscriber { judged?: readonly Run[]; }; #reported = new Runs(sameReport); - // Everything below was settled for good and folded away under pool pressure: handed out, or - // reported final, so none is handed out or reported again. Mirrors the Rust `Record::floor`. - #folded = 0n; - // What the record bills to the track's account, opened on its first run. - #bill?: cache.Reader; - // The pool needed a run this reader was promised, so it ended with TooFarBehind. - #lagged = false; // The group the frame-level helpers are currently draining, acquired through the // sequence cursor so frame reads and {@link Ordered.nextGroup} share one floor. #frameGroup?: GroupConsumer; @@ -3231,13 +3111,9 @@ export class Subscriber { #wireRecord(): WireRecord { const reported = this.#reported; return { - told: (sequence) => sequence < this.#folded || reported.at(sequence) !== undefined, - folded: (sequence) => sequence < this.#folded, + told: (sequence) => reported.at(sequence) !== undefined, toldStale: (sequence) => reported.at(sequence)?.kind === "stale", - untold: (start, end) => { - const from = max(start, this.#folded); - return from < end ? runGaps(reported.overlapping(from, end), from, end) : []; - }, + untold: (start, end) => (start < end ? runGaps(reported.overlapping(start, end), start, end) : []), tell: (start, end, report) => { if (start >= end) return; // A stale report keeps the parts an upstream decided, which stay its to judge again. @@ -3270,113 +3146,6 @@ export class Subscriber { }; } - // The bill on the account of the track this reads: `current` while it bills that account, or - // else a new one. A resumed reader's follows its serving copy across route changes, so the pool - // that holds what it reads is the one its debt comes from. - #billOn(current: cache.Reader | undefined): cache.Reader | undefined { - if (this.#resume) return this.#resume.bill(current); - const producer = this.#state.producer; - if (!producer) return current; - const account = accountOf(producer); - return current?.bills(account) ? current : account.reader(); - } - - // How many runs the record holds above its floor, each billed `cache.READER_RUN_OVERHEAD`: - // the handed-out prefix is one run that bills nothing. - #recordRuns(): number { - return ( - Math.max(0, this.#delivered.runs - 1) + - this.#skipped.size + - this.#pending.length + - this.#reported.size + - this.#givenUp.length + - this.#state.refused.peek().length - ); - } - - // Bill this reader's record, and pay what it can of the debt its track left its readers by - // folding what it settled for good into its floor. Returns TooFarBehind once that needed a - // run it was promised and its record is one to end; `handed` stays above the fold (see - // #fold). Mirrors the Rust `Record::settle`. - #settleRecord(handed?: bigint): TooFarBehind | undefined { - if (this.#lagged) return new TooFarBehind(); - // A closed reader released its bill for good; a read still pending must not open another. - if (this.#left) return undefined; - const bytes = this.#recordRuns() * cache.READER_RUN_OVERHEAD; - if (!this.#bill && bytes === 0) return undefined; - const bill = this.#billOn(this.#bill); - if (bill !== this.#bill) { - // The serving route changed: the replaced route's pool stops carrying this record. - this.#bill?.close(); - this.#bill = bill; - } - if (!bill) return undefined; - bill.set(bytes); - if (bill.debt === 0) return undefined; - this.#fold(handed); - bill.set(this.#recordRuns() * cache.READER_RUN_OVERHEAD); - if (bill.debt > 0 && bill.ends) { - this.#lagged = true; - bill.close(); - this.#bill = undefined; - this.#pending = []; - this.#reported = new Runs(sameReport); - this.#skipped.clear(); - this.#givenUp = []; - this.#heads = []; - return new TooFarBehind(); - } - return undefined; - } - - // Raise the floor over the final prefix: each run from it up to the lowest one that is not - // final. Final are the groups handed out, the reports nothing reopens, and this reader's own - // stale skips, which a wider budget would offer again only while the pool still held their - // groups. Not final are the reports an update reopens, the runs given up and not reported - // yet, and the sequences nothing settled. The runs copied from the track's drop set go - // first: the next scan folds in again whatever the track still holds. Mirrors the Rust - // `Record::fold`. It stops below `handed`, a group this read hands out that its publisher has - // yet to tell its wire of. - #fold(handed?: bigint): void { - if (this.#pending.some((run) => run.received)) { - this.#pending = this.#pending.filter((run) => !run.received); - this.#dropsSeen = undefined; - } - let floor = this.#folded; - for (;;) { - const reported = this.#reported.at(floor); - let next: bigint; - if (reported) { - if (!finalReport(reported)) break; - next = reported.end; - } else { - if (this.#pending.some((run) => run.start <= floor && floor < run.end)) break; - let end = this.#delivered.runEnd(floor); - if (end === undefined && this.#skipped.has(floor)) end = floor + 1n; - if (end === undefined) break; - // A report or a run given up inside it is judged on its own. - const [inside] = this.#reported.overlapping(floor, end); - if (inside) end = min(end, max(inside.start, floor)); - for (const run of this.#pending) if (run.start > floor && run.start < end) end = run.start; - next = end; - } - if (handed !== undefined && floor <= handed && handed < next) next = handed; - if (next <= floor) break; - floor = next; - } - if (floor === this.#folded) return; - this.#folded = floor; - this.#delivered.forgetBelow(floor); - for (const sequence of this.#skipped.keys()) if (sequence < floor) this.#skipped.delete(sequence); - this.#pending = this.#pending.flatMap((run) => removeRun([run], 0n, floor)); - this.#reported.set(0n, floor); - this.#givenUp = removeRun(this.#givenUp, 0n, floor); - this.#heads = removeRun(this.#heads, 0n, floor); - // The fills it refused, as the Rust `Misses::given_up` is forgotten below the floor. - if (this.#state.refused.peek().some((run) => run.start < floor)) - this.#state.refused.update((runs) => removeRun(runs, 0n, floor)); - } - // Owe again every run this cursor gave up as stale, reported or not: an update reopens the // verdict. A group still cached is offered again as a widened skip is, a missing one is missed // again (asked of a fill, or dropped as not found when none can serve it), and whatever is @@ -4215,7 +3984,6 @@ export class Subscriber { const producer = subscriber.#state.producer; return producer !== undefined && mayLand(producer, land); }; - hooks.billSubscriber = (subscriber, current) => subscriber.#billOn(current); hooks.resumeSubscriber = (name, subscription, source) => { const state = new TrackState(subscription ?? Subscription.default()); const subscriber = new Subscriber(name, state); @@ -4525,8 +4293,6 @@ export class Subscriber { })); } this.#left = true; - this.#bill?.close(); - this.#bill = undefined; this.#state.waitingOrdered = false; // Settle if we're first (the producer may already have); either way drop anything // still buffered. Groups parked at the withRange cap deliberately outlive a clean @@ -4616,19 +4382,6 @@ export class Subscriber { // Package-internal synchronous half of recvGroup. The lite publisher uses this so applying // control state, popping the group, and positioning its frames are one JavaScript turn. #tryRecvGroup(dispositions = false): Recv { - const recv = this.#tryRecvUnsettled(dispositions); - // What the read settled is billed now, and pays what it can of its readers' debt. The group - // it hands out stays above the fold until the next read, so the publisher tells its wire of - // it first rather than taking it for one already served. - const lagged = this.#settleRecord(recv.kind === "group" ? recv.group.sequence : undefined); - if (!lagged) return recv; - // Nobody takes the group the lag replaced, so it lets go of its source here. - if (recv.kind === "group") recv.group.close(); - return { kind: "error", error: lagged }; - } - - #tryRecvUnsettled(dispositions: boolean): Recv { - if (this.#lagged) return { kind: "error", error: new TooFarBehind() }; if (this.#resume) return this.#tryResumed(false, dispositions); const refused = this.#settle(); if (refused) return { kind: "error", error: refused }; @@ -4732,7 +4485,7 @@ export class Subscriber { // The first part of `[from, to)` nothing already reported blocks, now reported, with why, // replacing what it goes out over. Mirrors the Rust `Record::report`. #reportRun(from: bigint, to: bigint, hole: Hole & { received?: boolean }): (Recv & { kind: "drop" }) | undefined { - const start = max(from, this.#folded); + const start = from; if (start >= to) return undefined; const blocked = this.#reported.overlapping(start, to).filter((reported) => blocksHole(reported, hole)); const [gap] = runGaps(blocked, start, to); @@ -4750,9 +4503,6 @@ export class Subscriber { // first call on, a read without dispositions keeps its budget skips for it too. A resumed // one reports what its routes' copies gave up. #tryDrop(): (Recv & { kind: "drop" }) | undefined { - // What a publisher told its wire since is billed now. A reader the pool ended reports - // nothing more: its reads fail with the reason. - if (this.#settleRecord()) return undefined; if (this.#resume) { for (;;) { const queued = this.#nextResumedDrop(); @@ -4817,7 +4567,7 @@ export class Subscriber { this.#pending = insertDrops(this.#pending, received); } for (let run = this.#pending.shift(); run; run = this.#pending.shift()) { - // A received run the track has since forgotten, shed or superseded, is unknown again: + // A received run the track has since forgotten, expired or superseded, is unknown again: // only the part it still holds as it was folded in is owed. if (run.received) { const folded = run; @@ -4915,7 +4665,6 @@ export class Subscriber { // A handler opening takes a miss nobody could ask before. ...(producer ? [fillsOf(producer).opened.changed(fn)] : []), this.#state.startChanged.changed(fn), - this.#state.folded.changed(fn), // A group starting may settle a time end, or move the cap it holds reads back with. ...(this.#window.settled ? [] : [this.#state.timelineChanged.changed(fn)]), // Accepting the track may refuse a time anchor, with no group arriving to say so. @@ -5016,14 +4765,7 @@ export class Subscriber { // The synchronous half of #nextGroup. #tryNextGroup(): Recv { - if (this.#lagged) return { kind: "error", error: new TooFarBehind() }; - let recv = this.#resume ? this.#tryResumed(true) : this.#tryNextBuffered(); - const lagged = this.#settleRecord(); - if (lagged) { - // Nobody takes the group the lag replaced, so it lets go of its source here. - if (recv.kind === "group") recv.group.close(); - recv = { kind: "error", error: lagged }; - } + const recv = this.#resume ? this.#tryResumed(true) : this.#tryNextBuffered(); // One cursor: the frame helpers must not keep draining a group this read just // moved past, or interleaved reads would run backwards. if (recv.kind === "group" && this.#frameGroup && this.#frameGroup.sequence < recv.group.sequence) { diff --git a/quest/m1/js-pool-reach.md b/quest/m1/js-pool-reach.md deleted file mode 100644 index 0ff732addb4..00000000000 --- a/quest/m1/js-pool-reach.md +++ /dev/null @@ -1,33 +0,0 @@ -# [S] The JavaScript byte pool carries only what a JS caller can reach - -## Goal - -`js/net`'s per-broadcast byte pool keeps idle expiry, drop-run expiry, and the -per-group frame tail cap, and carries byte eviction only if a JS consumer sets -a capacity. Code that no production path reaches leaves the bundle. - -## Plan - -Found 2026-11-14 by the [feature review](/quest/m3/release/feature-review.md). -The pool's capacity defaults to infinity (`js/net/src/cache.ts`), every pool -is built without one (`broadcast.ts`, `track.ts`), and `resize` has no -non-test caller, so `Pool.accrue` always returns `undefined` and `#payDebt`, -reader debt, group `#shed`, and the debt calls in `track.ts` run only in -tests. [moq-dev/moq#8177](https://github.com/moq-dev/moq/pull/8177) said so -("byte eviction runs only for a pool given one. Today only tests do that"), -yet [moq-dev/moq#8294](https://github.com/moq-dev/moq/pull/8294), -[moq-dev/moq#8440](https://github.com/moq-dev/moq/pull/8440), and -[moq-dev/moq#8673](https://github.com/moq-dev/moq/pull/8673) kept building -on it. Rust differs: the relay sets a capacity. Expiry alone already bounds -the drop set (#8294). - -This revisits the pool quest's decision to port it "with no new setting" -(deleted by #8177), with the evidence that two weeks on nothing reaches the -path. - -Delete byte debt, shed, reader debt, and run billing from JS, -keeping the expiries; restore them from git with a consumer. - -Decided 2026-11-14 (maintainer, pick on [moq-dev/moq#8764](https://github.com/moq-dev/moq/pull/8764)): ✅ (a) delete the unreachable eviction (rejected: (b) expose `capacity`; (c) keep it for parity). - -Public API: none. Wire: none.