From 77e3b49601aa2819b81a26aea9747cfb1532f096 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 27 Oct 2026 02:40:20 -0700 Subject: [PATCH 01/16] chore(quest): claim quest/m1/transport-frame-offsets.md Quest-Claim: 30e8bc628b086d43 From 74130d940156f62ad1c3d1cf48c0b7007a415084 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 27 Oct 2026 03:20:10 -0700 Subject: [PATCH 02/16] fix(net): preserve transport offsets and bounded fetch provenance Co-Authored-By: GPT-6 --- drafts/draft-lcurley-moq-lite.md | 13 +- js/net/src/group.test.ts | 30 +++ js/net/src/group.ts | 24 ++- js/net/src/internal.ts | 24 ++- js/net/src/lite/connection.test.ts | 110 ++++++----- js/net/src/lite/publisher.test.ts | 42 ++++ js/net/src/lite/publisher.ts | 9 +- js/net/src/lite/subscriber.test.ts | 34 ++++ js/net/src/lite/subscriber.ts | 8 +- js/net/src/resume.ts | 15 +- js/net/src/track.test.ts | 21 ++ js/net/src/track.ts | 25 ++- quest/m1/transport-frame-offsets.md | 108 +++++++++++ rs/moq-net/src/lite/publisher.rs | 60 +++++- rs/moq-net/src/lite/subscriber.rs | 38 ++-- rs/moq-net/src/model/group.rs | 50 ++++- rs/moq-net/src/model/resume.rs | 15 +- rs/moq-net/src/model/track.rs | 284 ++++++++++++++++++++++++---- rs/moq-net/src/model/track/fill.rs | 55 +++++- rs/moq-net/src/model/track/miss.rs | 15 +- 20 files changed, 829 insertions(+), 151 deletions(-) diff --git a/drafts/draft-lcurley-moq-lite.md b/drafts/draft-lcurley-moq-lite.md index 358b3bcfc89..431b0c5e6c1 100644 --- a/drafts/draft-lcurley-moq-lite.md +++ b/drafts/draft-lcurley-moq-lite.md @@ -212,11 +212,10 @@ A partial Group is only ever delivered to a subscriber that asked for one. A publisher that cannot serve a Group from the frame the subscription names MUST skip that Group entirely and resolve the subscription to a later one, rather than deliver the part it holds. The resolved start is therefore always either exactly the requested Position or the beginning of a later Group, never some third Position the subscriber did not choose. -The asymmetry with Groups is deliberate. -A Group is the unit of decodability: dropping leading *Groups* leaves a stream the application can still decode, whereas dropping leading *Frames* often does not, whether because the Group opens with a keyframe the rest depends on or because its compression state lives in the frames that went missing. -A subscription therefore starts at a Group's first frame, and a subscriber asks for a partial Group only to finish one whose earlier frames it already holds, from this publisher or another. - -Frame indices are only meaningful relative to a Group the subscriber has already begun receiving, so a subscriber MUST start a Range at frame 0 unless it already holds every frame of that group below its start. +A Group is the unit of decodability: its earlier Frames may carry a keyframe or compression state the later Frames need. +Applications request whole Groups, while a transport may request a partial Group for route continuation, hole fills, or front-eviction refills. +A subscriber MAY start a Range at any frame index, whether or not it retains the earlier Frames. +A relay that holds none of a requested Group SHOULD request it upstream from frame 0 and apply the downstream bounds locally, retaining a copy usable by whole-Group readers and compression. # Flow This section outlines the flow of messages within a moq-lite session. @@ -1306,7 +1305,7 @@ A floor above the latest group simply waits there: that is a resumed subscriptio Reaching back is best-effort, not a guarantee that the groups still exist; see `Publisher Max Age` in [TRACK_INFO](#track-info). An Absolute `Start` past frame 0 delivers its group from that frame; a start resolved at a later group is always delivered from frame 0. -A subscriber MUST start at frame 0 unless it already holds every frame below it, from this publisher or another, since a Group is the unit of decodability (see [Positions](#positions)). +A subscriber MAY name any start frame, as described in [Positions](#positions). An Absolute `End` past frame 0 delivers its group up to that frame. ## SUBSCRIBE_UPDATE @@ -1839,7 +1838,7 @@ The `Message Length` describes the payload size on the wire. - Added TRACK_INFO `Refresh` and SUBSCRIBE_UPDATE `Wanted Group`, carrying ratio-based refresh demand through relays. - Added `Compression` to TRACK_INFO: none (0x0) or DEFLATE (0x1), whose Groups are DEFLATE streams per draft-lcurley-moq-flate. FRAME and datagrams carry an `Inflated Size` when it is not none. Earlier versions receive the Track inflated. -- A subscription starts at a Group's first frame: SUBSCRIBE starts past frame 0 only when the subscriber holds every frame below it, compressed Track or not. +- SUBSCRIBE may start at any frame, including when the subscriber no longer holds the prefix. Relays widen a cold Group request upstream to frame 0 and cut locally. - Replaced ANNOUNCE_OK Active Count with an ordered, empty SYNCED event (0x4), marking the initial set without retaining routes that end before delivery. diff --git a/js/net/src/group.test.ts b/js/net/src/group.test.ts index 62faf87a33b..d983ece316a 100644 --- a/js/net/src/group.test.ts +++ b/js/net/src/group.test.ts @@ -9,6 +9,36 @@ import { Timestamp } from "./time.ts"; const dec = new TextDecoder(); +test("a transport offset costs no frame storage and proves no group start", async () => { + const offset = MAX_GROUP_FRAMES + 7; + const producer = new Producer(0); + hooks.startGroup(producer, offset); + const early = producer.mirror(); + expect(hooks.groupStarted(early)).toBe(false); + expect(hooks.groupTimestamp(early)).toBeUndefined(); + for (let i = 0; i < MAX_GROUP_FRAMES; i++) + producer.writeFrame({ payload: new Uint8Array([i % 256]), timestamp: Timestamp.fromMillis(i) }); + const late = producer.mirror(); + for (const reader of [early, late]) { + expect(hooks.groupStarted(reader)).toBe(true); + expect(hooks.groupTimestamp(reader)).toBeUndefined(); + for (let i = 0; i < MAX_GROUP_FRAMES; i++) { + const frame = await reader.readFrameSequence(); + expect(frame?.sequence).toBe(offset + i); + expect(frame?.payload[0]).toBe(i % 256); + } + reader.close(); + } + expect(() => producer.writeFrame(textFrame("overflow"))).toThrow(GroupTooLarge); +}); + +test("a compressed group cannot start without its dictionary prefix", () => { + const producer = new Producer(0); + hooks.startGroup(producer, 5); + expect(() => hooks.bindGroupCompression(producer, true)).toThrow("compressed group lacks its prefix"); + producer.finish(); +}); + test("reordered frames keep the highest group PTS in live and later mirrors", () => { const installed = new Manual().install(); try { diff --git a/js/net/src/group.ts b/js/net/src/group.ts index 332ed668f01..11b69d5feba 100644 --- a/js/net/src/group.ts +++ b/js/net/src/group.ts @@ -68,6 +68,8 @@ class GroupState { // Absolute sequence of the frame at the front of `frames`, advanced by a read: the // sequence the next read returns. start = 0; + // The wire offset this copy began at; unlike `start`, reads never advance it. + offset = 0; cacheBytes = 0; compressed?: boolean; @@ -217,6 +219,16 @@ export class Producer { } if (state.closed.peek() !== undefined) flate.finish(); }; + hooks.groupOffset = (group) => group.#state.offset; + hooks.startGroup = (group, start) => { + if (!Number.isSafeInteger(start) || start < 0) throw new RangeError("invalid group frame start"); + const state = group.#state; + if (state.total.peek() !== 0 || state.closed.peek() !== undefined || group.#mirrors?.size) + throw new Error("group already started"); + if (start !== 0 && state.compressed) throw new ProtocolViolation("compressed group lacks its prefix"); + state.start = state.offset = start; + state.total.set(start); + }; hooks.writeGroupFrame = (group, frame, inflated) => group.#writeFrame(frame, inflated); hooks.groupCacheBytes = (group) => group.#state.flate?.bytes ?? group.#state.cacheBytes; hooks.groupProgress = (group, fn) => { @@ -293,6 +305,7 @@ export class Producer { dst.compressed = this.#state.compressed; for (const frame of this.#state.frames.peek()) appendFrame(dst, frame); dst.start = this.#state.start; + dst.offset = this.#state.offset; // The replay only covers what is still buffered, so the count has to come from the // source: it is what names a frame's absolute sequence, and what a publisher reads // to resolve the live edge. @@ -345,6 +358,8 @@ export class Producer { throw new FrameTooLarge(); if (inflated !== undefined && (!this.#state.flate || !Number.isSafeInteger(inflated) || inflated < 0)) throw new ProtocolViolation("unexpected inflated frame size"); + if (this.#state.total.peek() === Number.MAX_SAFE_INTEGER) + throw new ProtocolViolation("group frame index exceeds the safe integer range"); checkTimed(this.#state.timed, frame); if (this.#limit !== undefined && this.#state.total.peek() >= this.#limit) { throw new Error(`frame ${this.#state.total.peek()} is past the track's end`); @@ -357,7 +372,7 @@ export class Producer { commits = this.#admits.map((admit) => admit(start)); } if ( - this.#state.total.peek() >= MAX_GROUP_FRAMES || + this.#state.total.peek() - this.#state.offset >= MAX_GROUP_FRAMES || ((!this.#state.flate || inflated !== undefined) && this.#state.cacheBytes + frame.payload.byteLength > MAX_GROUP_CACHE_BYTES) || (this.#state.flate?.declared ?? this.#state.cacheBytes) + (inflated ?? frame.payload.length) > @@ -690,13 +705,16 @@ export class Consumer { static { makeConsumer = (state, owned) => new Consumer(state, owned); hooks.releaseGroupReader = (group, abort) => group.#release(abort); - hooks.groupTimestamp = (group) => (group.#state.unverified ? undefined : group.#state.timestamp); + hooks.groupTimestamp = (group) => + group.#state.unverified || group.#state.offset !== 0 ? undefined : group.#state.timestamp; hooks.groupLatest = (group) => (group.#state.unverified ? undefined : group.#state.latest); hooks.unverifyGroup = (group) => { group.#state.unverified = true; }; hooks.groupArrived = (group) => group.#state.arrived; - hooks.groupStarted = (group) => group.#state.total.peek() > 0 || group.#state.closed.peek() !== undefined; + hooks.groupIndex = (group) => group.#index(); + hooks.groupStarted = (group) => + group.#state.total.peek() > group.#state.offset || group.#state.closed.peek() !== undefined; hooks.groupFate = (group) => { const state = group.#state; return () => { diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index fb76722f169..fa69835055a 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -319,7 +319,7 @@ export interface ResumeSource { /** An independent subscriber to the serving copy. */ fork(options?: Subscription): Subscriber; /** A fresh reader for a group still cached by one of this reader's copies. */ - cachedGroup(sequence: number): GroupConsumer | undefined; + cachedGroup(sequence: number, from?: number): GroupConsumer | undefined; /** Let go of every copy. */ close(abort?: Error): void; } @@ -386,6 +386,8 @@ export const hooks: { * the drop. Mirrors the Rust `drop_groups`; assigned by `track.ts`. */ dropTrackGroups: (producer: Producer, start: number, end: number, error: Error) => void; + /** Create a received group at its wire offset before publishing it to readers. Assigned by `track.ts`. */ + receiveGroup: (producer: Producer, group: { sequence: number; start: number }) => GroupProducer; /** The producer's largest reported location and its subsequent changes. */ producerLargest: (producer: Producer) => Getter; @@ -481,7 +483,7 @@ export const hooks: { /** Whether this subscriber's budget gives up on `sequence`, judged by its successor; see {@link expireGroup}. */ staleAt: (subscriber: Subscriber, sequence: number) => Expiry; /** A group the subscriber's track still caches, as a fresh read handle. */ - cachedGroup: (subscriber: Subscriber, sequence: number) => GroupConsumer | undefined; + cachedGroup: (subscriber: Subscriber, sequence: number, from?: number) => GroupConsumer | undefined; /** Whether {@link cachedGroup} would find the group, without making a read handle. */ holdsGroup: (subscriber: Subscriber, sequence: number) => boolean; /** The error the subscriber's track gave `sequence` up with, if it did, and what changes it. */ @@ -512,6 +514,10 @@ export const hooks: { /** Bind the group to its immutable track codec. */ bindGroupCompression: (group: GroupProducer, compressed: boolean) => void; /** Admit a received stored slice without recompressing it. */ + /** Set a received group's first absolute frame before publishing it. Assigned by `group.ts`. */ + startGroup: (group: GroupProducer, start: number) => void; + /** The first absolute frame this source copy began at. Assigned by `group.ts`. */ + groupOffset: (group: GroupProducer) => number; writeGroupFrame: (group: GroupProducer, frame: Frame, inflated?: number) => void; /** Payload footprint including a shared inflated copy, for measurements. */ groupCacheBytes: (group: GroupProducer) => number; @@ -541,6 +547,8 @@ export const hooks: { /** Return when a group joined its track at this hop, on the installed clock. */ groupArrived: (group: GroupConsumer) => Instant; /** Whether a group counts as content: its first frame landed, or it ended. */ + /** The next absolute frame this reader can serve. Assigned by `group.ts`. */ + groupIndex: (group: GroupConsumer) => number; groupStarted: (group: GroupConsumer) => boolean; /** Watch the error a group's copy closes with, ignoring readers' verdicts, without holding the consumer. */ groupFate: (group: GroupConsumer) => () => Error | undefined; @@ -712,6 +720,15 @@ export const hooks: { bindGroupCompression: () => { throw new Error("group.ts not loaded"); }, + receiveGroup: () => { + throw new Error("track.ts not loaded"); + }, + groupOffset: () => { + throw new Error("group.ts not loaded"); + }, + startGroup: () => { + throw new Error("group.ts not loaded"); + }, writeGroupFrame: () => { throw new Error("group.ts not loaded"); }, @@ -748,6 +765,9 @@ export const hooks: { groupArrived: () => { throw new Error("group.ts not loaded"); }, + groupIndex: () => { + throw new Error("group.ts not loaded"); + }, groupStarted: () => { throw new Error("group.ts not loaded"); }, diff --git a/js/net/src/lite/connection.test.ts b/js/net/src/lite/connection.test.ts index 863fdd23c8b..3d6027d3a8f 100644 --- a/js/net/src/lite/connection.test.ts +++ b/js/net/src/lite/connection.test.ts @@ -618,50 +618,74 @@ for (const [alpn, version] of [ }); } -// A group stream that breaks the protocol ends the session, so a hostile peer cannot repeat -// it on every stream. +// A transport tail keeps its absolute frame numbers and the session open. A group past +// the declared end still ends the session, so a hostile peer cannot repeat that violation. for (const violation of ["a partial group", "a group past the declared end"] as const) { - test(`${violation} closes the session with PROTOCOL_VIOLATION`, async () => { - const version = Version.DRAFT_07; - const pair = createMockTransportPair(ALPN_07_WIP); - const connection = new Connection({ url: new URL("https://relay.example/"), quic: pair.client, version }); - try { - const track = wireOf(connection).consume(Path.from("room")).track("video").subscribe(); - - // The publisher, played by hand: answer TRACK_INFO, start the subscription, and - // leave any other stream the session opens (such as PROBE) open. - let sub: Stream | undefined; - while (!sub) { - const stream = await Stream.accept(pair.server, version); - if (!stream) throw new Error("the subscriber never subscribed"); - const id = await stream.reader.u53(); - if (id === StreamId.Track) { - await TrackMessage.decode(stream.reader, version); - await new TrackInfo({ maxAge: 60_000, timescale: Timescale.MILLI }).encode(stream.writer, version); - stream.close(); - } else if (id === StreamId.Subscribe) { - await Subscribe.decode(stream.reader, version); - sub = stream; + test( + violation === "a partial group" + ? "a partial group keeps its indices and the session open" + : "a group past the declared end closes the session with PROTOCOL_VIOLATION", + async () => { + const version = Version.DRAFT_07; + const pair = createMockTransportPair(ALPN_07_WIP); + const connection = new Connection({ url: new URL("https://relay.example/"), quic: pair.client, version }); + try { + const track = wireOf(connection).consume(Path.from("room")).track("video").subscribe(); + + // The publisher, played by hand: answer TRACK_INFO, start the subscription, and + // leave any other stream the session opens (such as PROBE) open. + let sub: Stream | undefined; + while (!sub) { + const stream = await Stream.accept(pair.server, version); + if (!stream) throw new Error("the subscriber never subscribed"); + const id = await stream.reader.u53(); + if (id === StreamId.Track) { + await TrackMessage.decode(stream.reader, version); + await new TrackInfo({ maxAge: 60_000, timescale: Timescale.MILLI }).encode( + stream.writer, + version, + ); + stream.close(); + } else if (id === StreamId.Subscribe) { + await Subscribe.decode(stream.reader, version); + sub = stream; + } } + await encodeSubscribeResponse(sub.writer, { start: new SubscribeStart(0) }, version); + + let group: GroupMessage; + if (violation === "a partial group") { + // A received tail uses its wire position even without a locally retained prefix. + group = new GroupMessage({ subscribe: 0n, sequence: 0, frameStart: 5 }); + } else { + await encodeSubscribeResponse(sub.writer, { end: new SubscribeEnd(2) }, version); + group = new GroupMessage({ subscribe: 0n, sequence: 2 }); + } + const writer = await Writer.open(pair.server, { version }); + await writer.u53(DataType.Group); + await group.encode(writer, version); + + if (violation === "a partial group") { + await writer.u62(2000n); // timestamp 1000, zigzag delta from 0 + await writer.u53(1); + await writer.write(new Uint8Array([7])); + writer.close(); + const received = await track.recvGroup(); + expect((await received?.readFrameSequence())?.sequence).toBe(5); + expect(await received?.readFrameSequence()).toBeUndefined(); + received?.close(); + const goaway = await Stream.open(pair.server, { version }); + await goaway.writer.u53(StreamId.Goaway); + await new Goaway("").encode(goaway.writer, version); + goaway.writer.close(); + expect((await wireOf(connection).goaway).uri).toBe(""); + } else { + expect((await pair.client.closed).closeCode).toBe(SessionCode.ProtocolViolation); + } + track.close(); + } finally { + connection.abort(); } - await encodeSubscribeResponse(sub.writer, { start: new SubscribeStart(0) }, version); - - let group: GroupMessage; - if (violation === "a partial group") { - // The subscriber never asked to start mid-group. - group = new GroupMessage({ subscribe: 0n, sequence: 0, frameStart: 5 }); - } else { - await encodeSubscribeResponse(sub.writer, { end: new SubscribeEnd(2) }, version); - group = new GroupMessage({ subscribe: 0n, sequence: 2 }); - } - const writer = await Writer.open(pair.server, { version }); - await writer.u53(DataType.Group); - await group.encode(writer, version); - - expect((await pair.client.closed).closeCode).toBe(SessionCode.ProtocolViolation); - track.close(); - } finally { - connection.abort(); - } - }); + }, + ); } diff --git a/js/net/src/lite/publisher.test.ts b/js/net/src/lite/publisher.test.ts index a4f98abb92a..0a3b72e4cc3 100644 --- a/js/net/src/lite/publisher.test.ts +++ b/js/net/src/lite/publisher.test.ts @@ -552,6 +552,48 @@ test("lite draft-05: the fetch response ranks the publisher's own writes", async } }); +test.each([0, 5, 6, 7])("a fetched cached tail keeps absolute indices from %s", async (startFrame) => { + const version = Version.DRAFT_07; + const pair = createMockTransportPair(ALPN_07_WIP); + const origin = new OriginProducer(); + const publisher = new Publisher(pair.server, version, randomHop(), origin.consume()); + const broadcast = publish(origin, Path.from("test")); + const track = broadcast.createTrack("video", { timescale: Timescale.MILLI }); + const group = hooks.receiveGroup(track, { sequence: 0, start: 5 }); + for (const value of ["a", "b"]) group.writeFrame(textFrame(value)); + group.finish(); + const client = await Stream.open(pair.client, { version }); + const server = await Stream.accept(pair.server, version); + if (!server) throw new Error("publisher never saw the fetch stream"); + try { + await publisher.runFetch( + new Fetch({ + broadcast: Path.from("test"), + track: "video", + priority: 0, + group: 0, + startFrame, + }), + server, + ); + if (startFrame === 0) { + await expect(client.reader.done()).rejects.toThrow(); + } else { + const payloads: string[] = []; + while (!(await client.reader.done())) { + await client.reader.u62(); + payloads.push(new TextDecoder().decode(await client.reader.read(await client.reader.u53()))); + } + expect(payloads).toEqual(["a", "b"].slice(startFrame - 5)); + } + } finally { + publisher.close(); + client.close(); + broadcast.close(); + origin.close(); + } +}); + // An unchecked track goes out untimed, so lite-06 carries its send time, never the frame's own // timestamp, which may be a moq-transport arrival time. test("lite draft-06: an unchecked track's fetched frames carry their send time", async () => { diff --git a/js/net/src/lite/publisher.ts b/js/net/src/lite/publisher.ts index f7f03b36f04..50d3142676f 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -1675,10 +1675,11 @@ export class Publisher { // The response carries no header, so the receiver numbers the first frame it gets // as `startFrame`. Skipping the head here is the only thing keeping those numbers // honest; a group that ends before we reach it can't be served at all. - for (let i = 0; i < startFrame; i++) { + if (hooks.groupIndex(group) > startFrame) throw new Error(`group lacks frame ${startFrame}`); + while (hooks.groupIndex(group) < startFrame) { const skipped = await race([hooks.readGroupFrame(group, compress), stream.closed]); if (!skipped) { - throw new Error(`fetch group ended at frame ${i}, before the requested start ${startFrame}`); + throw new Error(`fetch group ended before the requested start ${startFrame}`); } } @@ -1754,8 +1755,7 @@ export class Publisher { */ async #serveGroup(stream: Writer, options: RunGroup) { const { sub, group, stamp, compress, priority, start: startFrame, end: endFrame } = options; - // This model holds whole groups, so frame `startFrame` is always reachable unless - // the group ends first. Declaring it up front keeps the stream self-describing. + // The header names the requested position, so a copy missing it must be refused. const msg = new GroupMessage({ subscribe: sub, sequence: group.sequence, frameStart: startFrame }); // The subscription's budget, or an eviction of what is left to read, may give up on the // group mid-write, which abandons the stream. A group already given up never starts the write. @@ -1766,6 +1766,7 @@ export class Publisher { // Everything past this point runs inside the cleanup scope, so a failure never leaves // a finished group's stream being ranked. try { + if (hooks.groupIndex(group) > startFrame) throw new Error(`group lacks frame ${startFrame}`); // A SUBSCRIBE_UPDATE re-ranks the subscription, so a group already on the wire // follows it too rather than keeping a stale rank until it finishes. priority.add(stream, group.sequence, startFrame); diff --git a/js/net/src/lite/subscriber.test.ts b/js/net/src/lite/subscriber.test.ts index 3dc71585c9e..c2cc5d4d247 100644 --- a/js/net/src/lite/subscriber.test.ts +++ b/js/net/src/lite/subscriber.test.ts @@ -2022,6 +2022,40 @@ test.each( } }); +test.each([0, 5, 9000])("a received group numbers its frames from transport offset %s", async (frameStart) => { + const version = Version.DRAFT_07; + const { quic, streams, opened } = fakeSession(); + const subscriber = new Subscriber(quic, version, HopSchema.parse(1n)); + const broadcast = subscriber.consume(Path.from("room")); + const consumer = broadcast.track("video"); + const early = consumer.subscribe(); + try { + await opened(0); + await answerTrackInfo(streams[0], version, 1000); + await (await opened(1)).reading; + await subscriber.runGroup( + new Group({ subscribe: 0n, sequence: 0, frameStart }), + await groupStream(1000, version), + ); + const late = consumer.subscribe(); + try { + for (const reader of [early, late]) { + const group = await reader.recvGroup(); + if (!group) throw new Error("no received group"); + expect((await group.readFrameSequence())?.sequence).toBe(frameStart); + expect(await group.readFrameSequence()).toBeUndefined(); + group.close(); + } + } finally { + late.close(); + } + } finally { + early.close(); + broadcast.close(); + subscriber.close(); + } +}); + // A group stream carrying one frame at `timestamp` (zigzag delta from 0) in the track's scale. async function groupStream(timestamp: number, version: Version = Version.DRAFT_07): Promise { const chunks: Uint8Array[] = []; diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index ad2eadb3a70..39363618669 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -1667,12 +1667,6 @@ export class Subscriber { const read = tail.open(group.sequence); try { - // This side never asks for a group from a later frame, so a stream that skips the - // first ones is a peer bug. Read as a whole group, its frames would be misnumbered, - // and resumption and deduplication rely on those positions. - if (group.frameStart !== 0) { - throw new ProtocolViolation(`group ${group.sequence} starts at frame ${group.frameStart}`); - } // The publisher contradicted its own end, which no later group can repair. lite-05 // specified an inclusive end, so its last group lands on it: the write below drops // only that group there. @@ -1683,7 +1677,7 @@ export class Subscriber { track.abort(violation); throw violation; } - producer = track.createGroup(group.sequence); + producer = hooks.receiveGroup(track, { sequence: group.sequence, start: group.frameStart }); // Block until the timescale is known; the group's stream can arrive before // TRACK_INFO (or implicit defaults) resolves it on the subscribe stream. diff --git a/js/net/src/resume.ts b/js/net/src/resume.ts index 9bb4abf29a7..7ffef6a7e75 100644 --- a/js/net/src/resume.ts +++ b/js/net/src/resume.ts @@ -551,10 +551,10 @@ class Reader implements ResumeSource { return holding; } - cachedGroup(sequence: number): GroupConsumer | undefined { + cachedGroup(sequence: number, from = 0): GroupConsumer | undefined { for (let index = this.#copies.length - 1; index >= 0; index--) { const copy = this.#copies[index]; - const group = hooks.cachedGroup(copy.sub, sequence); + const group = hooks.cachedGroup(copy.sub, sequence, from); if (group) return this.#handOut(copy, group); } return undefined; @@ -1327,11 +1327,11 @@ class Recover implements GroupRecover { return routeFailed(failed) || copy.sub.closed.peek() instanceof Error; } - async replace(_index: number, failed: Error | undefined, signal: AbortSignal): Promise { + async replace(index: number, failed: Error | undefined, signal: AbortSignal): Promise { let fetch: Fetch | undefined; let kept: GroupConsumer | undefined; try { - const replacement = await this.#replace(failed, signal, (next) => { + const replacement = await this.#replace({ index, failed, signal }, (next) => { fetch?.cancel(); fetch = next; }); @@ -1345,8 +1345,7 @@ class Recover implements GroupRecover { } async #replace( - failed: Error | undefined, - signal: AbortSignal, + { index, failed, signal }: { index: number; failed?: Error; signal: AbortSignal }, started: (fetch: Fetch) => void, ): Promise { const reader = this.#reader; @@ -1369,7 +1368,7 @@ class Recover implements GroupRecover { // the group back and forth. if (failed) { const held = reader.holding(this.sequence, current)[0]; - const cached = held && hooks.cachedGroup(held.sub, this.sequence); + const cached = held && hooks.cachedGroup(held.sub, this.sequence, index); if (cached) return this.#replacement(cached, held); // Once the track ended, no route continues it: only a copy holding it could. if (reader.ended) throw failed; @@ -1390,7 +1389,7 @@ class Recover implements GroupRecover { waits.push(given.changed); // A newer copy's track caches it: continue from there. if (!failed && generation !== current.generation) { - const cached = hooks.cachedGroup(serving.sub, this.sequence); + const cached = hooks.cachedGroup(serving.sub, this.sequence, index); if (cached) return this.#replacement(cached, serving); } // Otherwise ask for it, if the route's version can fetch. The subscription may still diff --git a/js/net/src/track.test.ts b/js/net/src/track.test.ts index 85db29036dd..761120a0d75 100644 --- a/js/net/src/track.test.ts +++ b/js/net/src/track.test.ts @@ -18,6 +18,27 @@ const dec = new TextDecoder(); // Matches the private idle cache window and Rust cache::DEFAULT_EXPIRY. const CACHE_WINDOW_MS = 30_000; +test("a cached transport tail covers only positions it actually holds", async () => { + const producer = new TrackProducer("test"); + const subscriber = producer.subscribe(); + const tail = hooks.receiveGroup(producer, { sequence: 0, start: 5 }); + tail.writeFrame({ payload: new Uint8Array([7]) }); + tail.finish(); + expect(hooks.cachedGroup(subscriber, 0)).toBeUndefined(); + const covered = hooks.cachedGroup(subscriber, 0, 5); + expect((await covered?.readFrameSequence())?.sequence).toBe(5); + covered?.close(); + // Distinct hostile starts cannot create one cache copy per request. + for (let start = 6; start < 106; start++) + expect(() => hooks.receiveGroup(producer, { sequence: 0, start })).toThrow("duplicate group"); + const retained = hooks.cachedGroup(subscriber, 0, 5); + expect((await retained?.readFrameSequence())?.payload).toEqual(new Uint8Array([7])); + retained?.close(); + subscriber.close(); + producer.finish(); + producer.close(); +}); + /** Let every pending microtask and timer callback run, so a parked read gets a turn. */ function settle(): Promise { return new Promise((resolve) => setTimeout(resolve, 0)); diff --git a/js/net/src/track.ts b/js/net/src/track.ts index ab9b5af33c3..bfe84451be9 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -592,7 +592,7 @@ type CachedGroup = { type Start = { sequence: number; start: Timestamp }; // A fresh read handle on a group the producer still caches; assigned in its static block. -let cachedMirror: (producer: Producer, sequence: number) => GroupConsumer | undefined; +let cachedMirror: (producer: Producer, sequence: number, from: number) => GroupConsumer | undefined; // Whether the producer still caches a group that has not failed; assigned in its static block. let cachedHolds: (producer: Producer, sequence: number) => boolean; // Mirror the cached groups in `[start, end)` that `offer` accepts into a sink again, cut ones @@ -774,6 +774,7 @@ export class Producer { } static { + hooks.receiveGroup = (producer, { sequence, start }) => producer.#createGroup(sequence, start); hooks.writeDatagram = (producer, datagram, inflated) => producer.#insertDatagram(datagram, inflated); sourceStart = (producer) => producer.#served && producer.#feedStart(); hooks.producerLargest = (producer) => producer.#state.largest; @@ -797,10 +798,14 @@ export class Producer { const entry = producer.#cached.get(sequence); return entry !== undefined && !(entry.group.closed.peek() instanceof Error); }; - cachedMirror = (producer, sequence) => { + cachedMirror = (producer, sequence, from) => { const entry = producer.#cached.get(sequence); if (!entry || entry.group.closed.peek() instanceof Error) return undefined; const mirror = entry.group.mirror(); + if (hooks.groupIndex(mirror) > from) { + mirror.close(); + return undefined; + } hooks.arriveGroup(mirror, entry.arrived); return mirror; }; @@ -1195,7 +1200,7 @@ export class Producer { let largest: Position | undefined; for (const { group } of this.#cache) { const count = group.consume().frameCount; - if (count > 0 && (largest === undefined || group.sequence > largest.group)) { + if (count > hooks.groupOffset(group) && (largest === undefined || group.sequence > largest.group)) { largest = { group: group.sequence, frame: count - 1 }; } } @@ -1324,7 +1329,7 @@ export class Producer { for (const sink of this.#sinks) this.#mirror(entry, sink); // Follow the group's frames as they land, past the head `#advance` already named. const stop = hooks.groupProgress(group, (total) => { - if (total > 0) this.#advanceObject({ group: group.sequence, frame: total - 1 }); + if (total > hooks.groupOffset(group)) this.#advanceObject({ group: group.sequence, frame: total - 1 }); }); void group.closed.then((closed) => { stop(); @@ -1419,6 +1424,10 @@ export class Producer { * checked like {@link appendGroup}'s. */ createGroup(sequence: number): GroupProducer { + return this.#createGroup(sequence, 0); + } + + #createGroup(sequence: number, start: number): GroupProducer { this.#writable(sequence); const existing = this.#cached.get(sequence); if (existing) { @@ -1431,6 +1440,8 @@ export class Producer { } const group = new GroupProducer(sequence); + // Mirrors copy the absolute indices, so set them before publishing to any sink. + hooks.startGroup(group, start); this.#bind(group); this.#cap(group); this.#publish(group); @@ -2183,10 +2194,10 @@ export class Subscriber { changed: subscriber.#expiryChanged(), }; }; - hooks.cachedGroup = (subscriber, sequence) => { - if (subscriber.#resume) return subscriber.#resume.cachedGroup(sequence); + hooks.cachedGroup = (subscriber, sequence, from = 0) => { + if (subscriber.#resume) return subscriber.#resume.cachedGroup(sequence, from); const producer = subscriber.#state.producer; - return producer && cachedMirror(producer, sequence); + return producer && cachedMirror(producer, sequence, from); }; hooks.holdsGroup = (subscriber, sequence) => { const producer = subscriber.#state.producer; diff --git a/quest/m1/transport-frame-offsets.md b/quest/m1/transport-frame-offsets.md index e586f19b7e7..7f6f9564b70 100644 --- a/quest/m1/transport-frame-offsets.md +++ b/quest/m1/transport-frame-offsets.md @@ -43,3 +43,111 @@ Public API: none. Wire: lite-07-wip draft text only; the fields exist. ## Related - [Front eviction](/quest/m1/unbounded-groups/front-eviction.md) - refills a front hole with a frame-precise range + +### Implementation checkpoint + +[moq-dev/moq#6922](https://github.com/moq-dev/moq/pull/6922) implements private +JS offset initialization before publication, exact forwarding/cache coverage, +and private Rust frame-end provenance. Public APIs and existing wire fields +remain unchanged. The full Goal is unfinished: retaining one cached head does +not bound storage held by earlier readers. The selected one-copy bound still +applies; no weaker ownership or resource policy has been selected. + +Before fixing, the timed JS GROUP control passed at frame 0 and refused frame +5 and 9000 with PROTOCOL_VIOLATION. The actual Rust FetchServe control queued +start 2 for a cold request with start 2/end 3, rather than widening to 0. +Those controls now pass. Focused JS checks cover early/late mirrors, retained +frame limits, missing compression prefix, exact tail forwarding, and the +unchanged declared-end violation. Fifteen focused Rust controls pass, including +bounded-end cache misses, queued/taken range widening, and live-copy preservation. +These are focused checks, not final-head CI or full interop evidence. + +A small deterministic held-reader control demonstrates the remaining cost in +both the inherited unbounded-end path and the bounded-end path. Eight tails +start at frames 8 through 1, with identical bytes for every matching frame +index, beside a live copy starting at 32. Each reader retains its tail unread. +The unbounded copies all end at 33; the bounded copies all end at 17. + +| End | Distinct tail stores + live | Slot handles | Tail payload retained | Unique tail payload | Pool charge before/after identity-probe drop | After reader drop | +| --- | --- | --- | --- | --- | --- | --- | +| Unbounded | 8 + 1 | 2 | 233472 B | 32768 B | 266376 B / 266376 B | 40864 B | +| Exclusive 17 | 8 + 1 | 2 | 102400 B | 16384 B | 124456 B / 124456 B | 22144 B | + +The live baseline charge is 2216 B. Distinct state channels, overlapping payloads, +and unchanged charge after identity probes are dropped establish reader-held +multiplication; Slot ownership alone does not prove the Goal. Existing +`Head::adopt_head` shares payload references but only joins a head that reaches +the target offset, refusing sparse gaps. The bounded copies ending at 17 do not +reach the live copy at 32. Coalescing must preserve existing public reader +outcomes, completion/error ownership, compression, and cache accounting. + +The diagnostic below was inserted into `model::track::test`, run once through +the normal runner, then removed from executable source. It remains here so the +proof survives local scratch cleanup. The source and raw output are retained +under this worktree's `.tmp/held-tail-cost.rs` and +`.tmp/held-tail-overlap-cost.log`. It measures correctness and ownership, not +host performance or a new acceptance budget. SHA256 source: +`ea08934f00d65cb19443c9f0c90eec824ff6e2abc0fa347c587aef2564e33a76`; +raw output: `3c9efaf8a72a0a23e2f01c536e61a3ab0e5e4e52a35dd028eb6d73c7081c9eb9`. + +```sh +nix develop --command just rs test -p moq-net --lib \ + -E 'test(=model::track::test::diagnostic_held_tail_storage)' \ + --success-output immediate +``` + +```rust + #[moq_clock::test] + #[cfg(feature = "async")] + async fn diagnostic_held_tail_storage() { + for bounded in [false, true] { + let producer = track_producer("test", Info::timed()); + let pool = producer.broadcast.pool.clone(); + let dynamic = producer.dynamic(); + let consumer = producer.consume(); + let mut live = producer.create_group(group::Info { sequence: 0 }).unwrap(); + live.start_at(32).unwrap(); + live.write_frame(Timestamp::ZERO, vec![32; 1024]).unwrap(); + let baseline = pool.used(); + let mut held = Vec::new(); + let mut identities = Vec::new(); + for start in (1..=8).rev() { + let options = group::Fetch { frame_start: start, ..Default::default() }; + let fetch = if bounded { + consumer.fetch_range(0, FetchOptions { options, end: Some(17) }) + } else { + consumer.fetch_group(0, options) + }; + let request = dynamic.requested_fill_group().await.unwrap(); + let mut tail = request.accept(Info::timed()).unwrap(); + for frame in start..if bounded { 17 } else { 33 } { + tail.write_frame(Timestamp::ZERO, vec![frame as u8; 1024]).unwrap(); + } + tail.finish().unwrap(); + identities.push(tail.handle()); + held.push(fetch.await.unwrap()); + } + for (i, left) in identities.iter().enumerate() { + for right in &identities[i + 1..] { assert!(!left.is_clone(right)); } + assert!(!left.is_clone(&live.handle())); + } + let before_identity_drop = pool.used(); + drop(identities); + let only_readers_and_slot = pool.used(); + let cached_handles = producer.state.read().lookup[&0].copies().count(); + eprintln!("HELD_TAIL_COST bounded={bounded} requests=8 unique_tail_storages=8 live_storages=1 slot_handles={cached_handles} unread_payload={} unique_payload={} baseline_charge={baseline} charge_with_identities={before_identity_drop} charge_after_identity_drop={only_readers_and_slot}", if bounded { 100*1024 } else { 228*1024 }, if bounded { 16*1024 } else { 32*1024 }); + for (i, reader) in held.iter_mut().enumerate() { + let frame = reader.read_frame().await.unwrap().unwrap(); + assert_eq!(frame.payload.len(), 1024); + assert!(frame.payload.iter().all(|byte| *byte == (8-i) as u8)); + for expected in (9-i)..if bounded { 17 } else { 33 } { + let frame = reader.read_frame().await.unwrap().unwrap(); + assert!(frame.payload.iter().all(|byte| *byte == expected as u8)); + } + } + drop(held); + eprintln!("HELD_TAIL_COST bounded={bounded} charge_after_reader_drop={}", pool.used()); + live.abort(Error::Cancel).unwrap(); + } + } +``` diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 9c074712e60..07334800e46 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -2206,12 +2206,15 @@ impl Request for FetchServe { fn start(_: &Shared, msg: Self::Message, broadcast: crate::broadcast::Consumer) -> Result { let fetching = broadcast .track(&msg.track)? - .fetch_group( + .fetch_range( msg.group, - group::Fetch { - priority: msg.priority, - max_delay: msg.max_delay, - frame_start: msg.start_frame, + track::FetchOptions { + options: group::Fetch { + priority: msg.priority, + max_delay: msg.max_delay, + frame_start: msg.start_frame, + }, + end: msg.end_frame.and_then(|end| end.checked_add(1)), }, ) .into_inner(); @@ -2233,7 +2236,7 @@ impl Request for FetchServe { Self::Fetch { msg, fetching } => { // Before the group: a fetched group polled while this waited would be lost. let ietf = ready!(shared.peer_setup.poll_ietf(waiter)); - let group = ready!(kio::Task::poll(fetching, waiter))?; + let group = ready!(kio::Task::poll(fetching, waiter))?.with_range(msg.start_frame..); // The response carries no header, so a short run is indistinguishable // from one that started elsewhere: only serve a range we can cover @@ -8094,6 +8097,51 @@ mod tests { ); } + /// A cold relay fills a bounded request from the group's head, so its cache does not + /// store an undecodable tail merely because that was the downstream reader's floor. + #[moq_clock::test] + async fn a_cold_bounded_fetch_requests_the_prefix() { + let origin = crate::origin::Config::new(Hop::new(1).unwrap()).produce(); + let broadcast = origin.create_broadcast("room").unwrap(); + let track = broadcast.create_track("video", track::Info::timed()).unwrap(); + let dynamic = track.dynamic(); + let peer_setup = crate::lite::PeerSetup::new(false, crate::transport::Address::Setup); + peer_setup.set(crate::lite::Setup::default()); + let (_, goaway) = crate::goaway::Handle::new(true); + let publisher = Publisher::new(PublisherConfig { + runtime: crate::time::sim(), + session: crate::driver::Transport::new(ScriptedSession::new(Vec::new())), + origin: origin.consume(), + version: Version::Lite07, + peer_setup, + goaway, + peer_hop: None, + auth: crate::auth::Link::new(false), + peer_grant: Default::default(), + client: false, + subscriptions: Default::default(), + estimate: kio::Producer::default().weak(), + }); + let run = FetchServe::start( + &publisher.shared, + lite::Fetch { + epoch: None, + broadcast: crate::Path::new("room"), + track: "video".into(), + priority: 0, + max_delay: None, + group: 0, + start_frame: 2, + end_frame: Some(3), + }, + broadcast.consume(), + ) + .unwrap(); + let request = dynamic.requested_fill_group().await.unwrap(); + assert_eq!(request.frame_start(), 0, "a cold relay keeps the prefix"); + drop(run); + } + /// A FETCH whose own budget gives up on its group partway resets as too old: a FIN /// would tell the requester the prefix it got is the whole group. #[moq_clock::test] diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 005f3b57bb7..c185f192e24 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -8093,12 +8093,14 @@ enum FetchRunState { request: Option, stream: Stream, frame_start: u64, + frame_end: Option, }, /// Flushed; waiting for the publisher to answer before accepting. Answer { request: Option, stream: Stream, frame_start: u64, + frame_end: Option, }, Ingest { stream: Stream, @@ -8244,10 +8246,17 @@ impl kio::Task for FetchServeRun { // been. Asking for the offset anyway would fail to encode and reject a // fetch we can serve. let frame_start = match self.serve.subscriber.version.has_frame_bounds() { - true => request.frame_start(), + true if !self.info.compress => request.frame_start(), + true => 0, false => 0, }; + let frame_end = if self.serve.subscriber.version.has_frame_bounds() && !self.info.compress { + request.frame_end() + } else { + None + }; + let msg = lite::Fetch { epoch: self.serve.epoch.clone(), broadcast: self.serve.path.as_path(), @@ -8259,10 +8268,9 @@ impl kio::Task for FetchServeRun { .filter(|_| self.serve.subscriber.version.has_fetch_max_delay()), group: self.group, start_frame: frame_start, - // Always through the end of the group: a fetch that stopped short - // would cache a group indistinguishable from a complete one. A - // downstream cap is applied when serving, not when fetching. - end_frame: None, + // A bounded plain copy keeps this provenance in the cache; older wires + // and compressed groups widen to a complete representation. + end_frame: frame_end.and_then(|end| end.checked_sub(1)), }; stream = stream.with_stats(self.serve.subscriber.origin.stats()); let buffered = stream @@ -8279,6 +8287,7 @@ impl kio::Task for FetchServeRun { request: Some(request), stream, frame_start, + frame_end, }; } FetchRunState::Send { stream, .. } => { @@ -8296,6 +8305,7 @@ impl kio::Task for FetchServeRun { request, stream, frame_start, + frame_end, } = std::mem::replace(&mut self.state, FetchRunState::Done) else { unreachable!() @@ -8304,6 +8314,7 @@ impl kio::Task for FetchServeRun { request, stream, frame_start, + frame_end, }; } FetchRunState::Answer { stream, .. } => { @@ -8317,6 +8328,7 @@ impl kio::Task for FetchServeRun { request, stream, frame_start, + frame_end, } = std::mem::replace(&mut self.state, FetchRunState::Done) else { unreachable!() @@ -8337,7 +8349,13 @@ impl kio::Task for FetchServeRun { // The group's readers pick it up from the cache only when next polled, // so their demand outlives the request until then. let readers = request.readers(); - let mut producer = match request.accept(group_info) { + let producer = match request.accept_from( + group_info, + group::Bounds { + start: frame_start, + end: frame_end, + }, + ) { Ok(producer) => producer, Err(err) => { // Already served (a concurrent fetch) or the track closed. @@ -8347,14 +8365,6 @@ impl kio::Task for FetchServeRun { } }; - // The response starts at the frame we asked for, so number it from - // there rather than restarting the group at 0. - if let Err(err) = producer.start_at(frame_start) { - stream.writer.abort(&err); - let _ = producer.abort(err); - return Poll::Ready(()); - } - self.state = FetchRunState::Ingest { stream, producer: Box::new(crate::recv::Group::new(producer)), diff --git a/rs/moq-net/src/model/group.rs b/rs/moq-net/src/model/group.rs index a7e465c9233..2e11bfbdf8c 100644 --- a/rs/moq-net/src/model/group.rs +++ b/rs/moq-net/src/model/group.rs @@ -167,6 +167,13 @@ impl Entry { } } +/// The frames carried by one transport copy; `end` is exclusive, not the group's end. +#[derive(Clone, Copy, Debug, Default)] +pub(crate) struct Bounds { + pub start: u64, + pub end: Option, +} + /// Shared group state. Model-visible so [`frame`] handles can observe the abort flag /// while streaming a partial frame. #[derive(Default)] @@ -198,6 +205,8 @@ pub(super) struct GroupState { // Exclusive cap on `next_index`, set when the track ends inside this group: a write // past the track's end is refused rather than served as content that does not exist. limit: Option, + // A transport stopped this copy here; FIN does not declare the original group complete. + range_end: Option, // The total size (in bytes) of all cached frames plus what the in-flight frame has // written so far. @@ -1197,7 +1206,7 @@ impl Producer { /// there. /// /// A group can be short at its front or its back, never in the middle: this trims - /// the front, and simply stopping (then [`finish`](Self::finish)ing) trims the back. + /// the front. Transport cuts at the back also retain their incomplete provenance. /// The frames below `index` are not a gap this handle will ever fill, so a reader /// positioned below it gets [`Error::Lagged`]. They belong to whoever produced the /// head of the group, typically another route serving the same track (see @@ -1225,6 +1234,21 @@ impl Producer { Ok(()) } + /// Apply transport bounds before this copy is published to the cache. + pub(crate) fn bound(&mut self, frames: Bounds) -> Result<()> { + self.start_at(frames.start)?; + if let Some(end) = frames.end { + let end = usize::try_from(end).map_err(|_| Error::BoundsExceeded(crate::coding::BoundsExceeded))?; + let mut state = modify(&self.handle.state)?; + if end < state.offset { + return Err(Error::InvalidRange); + } + state.range_end = Some(end); + state.limit = Some(state.limit.map_or(end, |limit| limit.min(end))); + } + Ok(()) + } + /// A helper method to write a frame from a single byte buffer. /// /// If you want to write multiple chunks, use [Self::create_frame] to get a frame producer. @@ -1934,9 +1958,29 @@ impl Handle { Fate(self.alive.clone()) } + /// Whether a transport-bounded copy may answer these requested frames. + pub(super) fn covers(&self, frames: Bounds) -> bool { + let state = self.state.read(); + state.abort.is_none() + && state.offset as u64 <= frames.start + && match (state.range_end, frames.end) { + (None, _) => true, + (Some(cap), Some(end)) => end <= cap as u64, + (Some(_), None) => false, + } + } + + /// Whether this copy was cut short at the back by its transport request. + pub(super) fn back_cut(&self) -> bool { + self.state.read().range_end.is_some() + } + /// Whether the group was finished: it holds every frame it will ever have. pub(crate) fn is_finished(&self) -> bool { - self.state.read().fin.is_some() + { + let state = self.state.read(); + state.fin.is_some() && state.range_end.is_none() + } } /// The index of the first frame this group still holds, or `None` once it has been @@ -1966,7 +2010,7 @@ impl Handle { /// tail, and the count outlives the released cache. pub(super) fn resume_frame(&self) -> Option { let state = self.state.read(); - if state.fin.is_some() || state.sparse() { + if (state.fin.is_some() && state.range_end.is_none()) || state.sparse() { return None; } (state.committed() > state.offset).then_some(state.committed()) diff --git a/rs/moq-net/src/model/resume.rs b/rs/moq-net/src/model/resume.rs index 94628830631..d5efe7ba19f 100644 --- a/rs/moq-net/src/model/resume.rs +++ b/rs/moq-net/src/model/resume.rs @@ -315,7 +315,7 @@ impl Consumer { pub(super) fn fetch_group( &self, sequence: u64, - options: group::Fetch, + options: track::FetchOptions, logical: kio::Consumer, ) -> Fetching { Fetching { @@ -1753,7 +1753,7 @@ pub(super) struct Recover { } enum Fetch { - Pending(kio::Pending), + Pending(Box), /// The serving route will not fetch it: wait for its subscription instead. Refused(Error), } @@ -1962,7 +1962,10 @@ impl Recover { frame_start: index, ..Default::default() }; - self.fetch = Some((generation, Fetch::Pending(serving.fetch_group(self.sequence, options)))); + self.fetch = Some(( + generation, + Fetch::Pending(Box::new(serving.fetch_group(self.sequence, options).into_inner())), + )); } let Some((_, fetch)) = &mut self.fetch else { unreachable!("asked above") @@ -2060,7 +2063,7 @@ pub(super) struct Replacement { pub(super) struct Fetching { route: Consumer, sequence: u64, - options: group::Fetch, + options: track::FetchOptions, logical: kio::Consumer, /// The fetch in flight, and the route generation it went to. pending: Option<(u64, kio::Pending)>, @@ -2089,7 +2092,7 @@ impl kio::Task for Fetching { continue; } if self.pending.as_ref().is_none_or(|(asked, _)| *asked != generation) { - self.pending = Some((generation, serving.fetch_group(self.sequence, self.options.clone()))); + self.pending = Some((generation, serving.fetch_range(self.sequence, self.options.clone()))); } let (_, pending) = self.pending.as_mut().expect("asked above"); let result = match kio::Task::poll(&mut **pending, waiter) { @@ -2107,7 +2110,7 @@ impl kio::Task for Fetching { Poll::Ready(Ok(group.with_recover(Recover { sequence: self.sequence, reader: None, - max_delay: self.options.max_delay, + max_delay: self.options.options.max_delay, route: self.route.clone(), generation, copy: serving.clone(), diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index a399e1923d9..942ad139c9b 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -558,10 +558,16 @@ impl Slot { self.is_live() && self.group.is_started() } + /// Whether a group-only reader may treat this slot as reaching the original end. + fn serves_end(&self) -> bool { + let group = self.serving(); + !group.is_aborted() && !group.back_cut() + } + /// Whether the arrival walk hands this slot out: the live feed's, and still serving, a /// fetched head counting for an aborted live group. fn is_walked(&self) -> bool { - self.visible && !self.is_aborted() + self.visible && self.serves_end() } /// Why the publisher gave up this live-feed group readers were shown, once nothing @@ -581,7 +587,7 @@ impl Slot { /// The copy to hand readers: the fetched head while it lasts, else the live group. fn serving(&self) -> &group::Handle { match &self.head { - Some(head) if !head.is_aborted() => head, + Some(head) if !head.is_aborted() && !head.back_cut() => head, _ => &self.group, } } @@ -593,7 +599,7 @@ impl Slot { /// Whether every copy is gone, so the slot has nothing left to serve. fn is_aborted(&self) -> bool { - self.serving().is_aborted() + self.copies().all(group::Handle::is_aborted) } /// The mean access over copies still held, weighed the way each one samples the @@ -1143,11 +1149,17 @@ impl TrackState { /// it to someone who asked for the whole group would silently hand back a tail. It /// is a miss instead, so the fetch goes upstream for the frames that are missing. fn covering_group(&self, sequence: u64, frame_start: u64) -> Option<&group::Handle> { - self.lookup.get(&sequence)?.copies().find(|group| { - group - .live_first_frame() - .is_some_and(|first| first as u64 <= frame_start) - }) + self.covering_range( + sequence, + group::Bounds { + start: frame_start, + end: None, + }, + ) + } + + fn covering_range(&self, sequence: u64, frames: group::Bounds) -> Option<&group::Handle> { + self.lookup.get(&sequence)?.copies().find(|group| group.covers(frames)) } /// The local retention bound, or `None` when unknown or unlimited. @@ -1477,10 +1489,10 @@ impl TrackState { /// once it can never be served. A missing group is a failure ([`Error::NotFound`]), not an /// end-of-stream. The handler side (a refusal, or no [`Dynamic`] at all) lives /// in the track's fills; [`Fetching`] polls both. - fn poll_fetch_cached(&self, sequence: u64, frame_start: u64) -> Poll> { + fn poll_fetch_cached(&self, sequence: u64, frames: group::Bounds) -> Poll> { // A group aborted between the lookup and the consume (an abandoned fetch cut // short) is a miss, not a hit that fails on its first read. - if let Some(group) = self.covering_group(sequence, frame_start) + if let Some(group) = self.covering_range(sequence, frames) && let Some(consumer) = group.try_consume() { // A cache hit refreshes the group: it resets both its age (expiry keys @@ -2584,7 +2596,7 @@ impl TrackState { pub(super) fn insert_group_request( &mut self, sequence: u64, - frame_start: u64, + frames: group::Bounds, info: Option, ) -> Result { if let Some(err) = &self.abort { @@ -2609,18 +2621,21 @@ impl TrackState { // head instead. Anything else that can't answer from `frame_start` (evicted, // aborted, or narrower backfill) is replaced; a group that can is a duplicate. if self.lookup.get(&sequence).is_some_and(Slot::is_live) { - if self.covering_group(sequence, frame_start).is_some() { + if self.covering_range(sequence, frames).is_some() { return Err(Error::Duplicate); } } else { - self.claim_sequence(sequence, frame_start)?; + if self.covering_range(sequence, frames).is_some() { + return Err(Error::Duplicate); + } + self.uncache(sequence); } let mut group = group::Producer::new(group::Info { sequence }, info, self.cache.clone()); // Start where the request did before the group is visible: a fetch looking it up // in between would otherwise see it begin at 0 and get a reader that later skips // the head it asked for. - group.start_at(frame_start)?; + group.bound(frames)?; self.cap(&group.handle())?; // A backfill exists because someone is fetching it right now: stamp that // access so the eviction walk can't kill it before the fetch resolves. @@ -4618,7 +4633,43 @@ impl Consumer { /// or the track's abort error if it's already closed. A miss queues a one-group [`Fill`], /// which concurrent fetches of the same sequence join. pub fn fetch_group(&self, sequence: u64, options: impl Into>) -> kio::Pending { - let options = options.into().unwrap_or_default(); + self.fetching( + sequence, + FetchOptions { + options: options.into().unwrap_or_default(), + end: None, + }, + ) + } + + /// Fill a wire request, widening a cold or compressed group to a reusable prefix. + pub(crate) fn fetch_range(&self, sequence: u64, mut request: FetchOptions) -> kio::Pending { + let state = self.resolve(None).0; + { + let state = state.read(); + if state.lookup.get(&sequence).is_none_or(Slot::is_aborted) + || state.info.as_ref().is_none_or(|info| info.compress) + { + request.options.frame_start = 0; + } + // A compressed stream needs the entire dictionary; an unknown track is conservative + // until TRACK_INFO has told the relay which representation it will receive. + if state.info.as_ref().is_none_or(|info| info.compress) { + request.end = None; + } + } + self.fetching(sequence, request) + } + + fn fetching(&self, sequence: u64, request: FetchOptions) -> kio::Pending { + let FetchOptions { + options, + end: frame_end, + } = request; + let frames = group::Bounds { + start: options.frame_start, + end: frame_end, + }; // One fetch per calling context, counted here (coalesced upstream work is // still one request served). Independent of `subscriptions` and the viewer @@ -4634,6 +4685,7 @@ impl Consumer { fills, sequence, frame_start: options.frame_start, + frame_end, priority: options.priority, max_delay: options.max_delay, result: None, @@ -4654,12 +4706,20 @@ impl Consumer { fills: state.read().fills.clone(), sequence, frame_start: options.frame_start, + frame_end, priority: options.priority, max_delay: options.max_delay, result: None, hit: None, stats: self.stats.clone(), - resume: Some(Box::new(resume.fetch_group(sequence, options, state.clone()))), + resume: Some(Box::new(resume.fetch_group( + sequence, + FetchOptions { + options, + end: frame_end, + }, + state.clone(), + ))), }); } @@ -4668,10 +4728,7 @@ impl Consumer { // a queue entry). let (fills, cached) = { let state = state.read(); - ( - state.fills.clone(), - state.poll_fetch_cached(sequence, options.frame_start), - ) + (state.fills.clone(), state.poll_fetch_cached(sequence, frames)) }; let unresolved = cached.is_pending(); // Hold a hit until the caller polls: the held consumer keeps the group wanted, @@ -4682,7 +4739,14 @@ impl Consumer { }; if unresolved { - result = queue(&fills, sequence, &options); + result = queue( + &fills, + sequence, + &FetchOptions { + options: options.clone(), + end: frame_end, + }, + ); } kio::Pending::new(Fetching { @@ -4690,6 +4754,7 @@ impl Consumer { fills, sequence, frame_start: options.frame_start, + frame_end, priority: options.priority, max_delay: options.max_delay, result, @@ -4723,7 +4788,7 @@ impl Consumer { let cached = state .lookup .range(span.clone()) - .filter(|(_, slot)| !slot.is_aborted()) + .filter(|(_, slot)| slot.serves_end()) .map(|(sequence, _)| *sequence) .collect(); let holes: Vec> = miss::holes(&state, span) @@ -4759,7 +4824,7 @@ impl Consumer { state .lookup .range(span) - .filter(|(_, slot)| !slot.is_aborted()) + .filter(|(_, slot)| slot.serves_end()) .map(|(sequence, _)| *sequence) .collect() }) @@ -4995,7 +5060,8 @@ impl kio::Task for Resolving { /// Join the fill covering `sequence`, or queue a one-group fill; `None` when no handler /// exists to take one, or for the last sequence, which a fill's exclusive end can't name. -fn queue(fills: &kio::Shared, sequence: u64, options: &group::Fetch) -> Option> { +fn queue(fills: &kio::Shared, sequence: u64, request: &FetchOptions) -> Option> { + let options = &request.options; let end = sequence.checked_add(1)?; let ask = fill::Ask { start: Position { @@ -5003,6 +5069,7 @@ fn queue(fills: &kio::Shared, sequence: u64, options: &group::Fetch) frame: options.frame_start, }, end, + frame_end: request.end, asking: fill::Asking { priority: options.priority, max_delay: options.max_delay, @@ -5161,6 +5228,7 @@ fn ask_span(fills: &kio::Shared, span: ops::Range, priority: i8) let ask = fill::Ask { start: Position::group(span.start), end: span.end, + frame_end: None, asking: fill::Asking { priority, // A fetch names its groups outright, so only the publisher's max age applies. @@ -5171,6 +5239,13 @@ fn ask_span(fills: &kio::Shared, span: ops::Range, priority: i8) fills.lock().join(fills, &ask).0 } +/// One transport request, whose end may cut the stored copy short. +#[derive(Clone)] +pub(crate) struct FetchOptions { + pub options: group::Fetch, + pub end: Option, +} + /// The pollable state of a [`Consumer::fetch_group`]. /// /// Awaited via the [`kio::Pending`] wrapper; resolves to the @@ -5183,6 +5258,7 @@ pub struct Fetching { // This caller's own start, so a cached group that begins above it is a miss // rather than a short answer. frame_start: u64, + frame_end: Option, // This caller's priority, for a fetch that moves to a front's routes. priority: i8, // This caller's own age cap. None means only the publisher's max age. @@ -5214,7 +5290,14 @@ impl kio::Task for Fetching { max_delay: self.max_delay, frame_start: self.frame_start, }; - self.resume = Some(Box::new(routes.fetch_group(self.sequence, options, self.state.clone()))); + self.resume = Some(Box::new(routes.fetch_group( + self.sequence, + FetchOptions { + options, + end: self.frame_end, + }, + self.state.clone(), + ))); } if let Some(resume) = &mut self.resume { let group = ready!(kio::Task::poll(&mut **resume, waiter))?; @@ -5247,6 +5330,7 @@ impl kio::Task for Fetching { let resolve = |group: group::Consumer| { group .with_range(frame_start..) + .with_end(self.frame_end) .with_meter(self.stats.meter()) .with_expiry(expiry.clone()) }; @@ -5256,12 +5340,19 @@ impl kio::Task for Fetching { // Track side: the cached group, the abort error, or past-final. The outer // error is the channel closing without any of those. - let cached = - |waiter: &kio::Waiter| match state.poll(waiter, |state| state.poll_fetch_cached(sequence, frame_start)) { - Poll::Ready(Ok(res)) => Poll::Ready(res.map(resolve)), - Poll::Ready(Err(closed)) => Poll::Ready(Err(closed.abort.clone().unwrap_or(Error::Dropped))), - Poll::Pending => Poll::Pending, - }; + let cached = |waiter: &kio::Waiter| match state.poll(waiter, |state| { + state.poll_fetch_cached( + sequence, + group::Bounds { + start: frame_start, + end: self.frame_end, + }, + ) + }) { + Poll::Ready(Ok(res)) => Poll::Ready(res.map(resolve)), + Poll::Ready(Err(closed)) => Poll::Ready(Err(closed.abort.clone().unwrap_or(Error::Dropped))), + Poll::Pending => Poll::Pending, + }; if let Poll::Ready(res) = cached(waiter) { return Poll::Ready(res); } @@ -5320,7 +5411,14 @@ impl kio::Task for Fetching { max_delay: self.max_delay, frame_start: self.frame_start, }; - self.result = queue(&self.fills, self.sequence, &options); + self.result = queue( + &self.fills, + self.sequence, + &FetchOptions { + options, + end: self.frame_end, + }, + ); return self.poll(waiter); } Poll::Ready(Err(err)) @@ -15078,6 +15176,126 @@ mod test { assert_eq!(&fetched.read_frame().await.unwrap().unwrap().payload[..], b"head"); } + /// A bounded FIN is local to its request, never proof that the original group ended. + #[moq_clock::test] + #[cfg(feature = "async")] + async fn bounded_transport_copies_never_answer_whole_fetches() { + let producer = track_producer("test", Info::timed()); + let dynamic = producer.dynamic(); + let consumer = producer.consume(); + let request = |start, end| FetchOptions { + options: group::Fetch { + frame_start: start, + ..Default::default() + }, + end, + }; + let bounded = consumer.fetch_range(0, request(2, Some(4))); + let fill = dynamic.requested_fill_group().await.unwrap(); + assert_eq!((fill.frame_start(), fill.frame_end()), (0, Some(4))); + let mut prefix = fill.accept(Info::timed()).unwrap(); + for _ in 0..4 { + prefix.write_frame(Timestamp::ZERO, b"prefix".as_slice()).unwrap(); + } + prefix.finish().unwrap(); + let mut bounded = bounded.await.unwrap().with_range(2..).with_end(Some(4)); + for expected in 2..4 { + assert_eq!(bounded.index(), expected); + assert!(bounded.read_frame().await.unwrap().is_some()); + } + assert!(bounded.read_frame().await.unwrap().is_none()); + assert!( + consumer.cached_groups(0..1).is_empty(), + "a bounded prefix is no whole cached group" + ); + let all = consumer.fill_groups(0..1, 0); + // A narrower bound reuses the same stored prefix. + assert!( + consumer + .fetch_range(0, request(1, Some(3))) + .now_or_never() + .unwrap() + .is_ok() + ); + let mut whole = consumer.fetch_group(0, None); + assert!(kio::Task::poll(&mut *whole, &kio::Waiter::noop()).is_pending()); + let fill = dynamic.requested_fill_group().await.unwrap(); + assert_eq!((fill.frame_start(), fill.frame_end()), (0, None)); + let mut complete = fill.accept(Info::timed()).unwrap(); + for _ in 0..6 { + complete.write_frame(Timestamp::ZERO, b"whole".as_slice()).unwrap(); + } + complete.finish().unwrap(); + let mut whole = whole.await.unwrap(); + for _ in 0..6 { + assert!(whole.read_frame().await.unwrap().is_some()); + } + assert!(whole.read_frame().await.unwrap().is_none()); + assert_eq!(all.await.held(0..1), BTreeSet::from([0])); + assert_eq!(producer.state.read().lookup[&0].copies().count(), 1); + } + + /// Tail starts replace one backfill slot rather than retaining a copy per peer range. + #[moq_clock::test] + #[cfg(feature = "async")] + async fn bounded_tail_refills_coalesce_behind_a_live_copy() { + let producer = track_producer("test", Info::timed()); + let dynamic = producer.dynamic(); + let consumer = producer.consume(); + let mut live = producer.create_group(group::Info { sequence: 0 }).unwrap(); + live.start_at(200).unwrap(); + live.write_frame(Timestamp::ZERO, b"live".as_slice()).unwrap(); + for start in (1..100).rev() { + let fetched = consumer.fetch_range( + 0, + FetchOptions { + options: group::Fetch { + frame_start: start, + ..Default::default() + }, + end: Some(start + 1), + }, + ); + let fill = dynamic.requested_fill_group().await.unwrap(); + assert_eq!((fill.frame_start(), fill.frame_end()), (start, Some(start + 1))); + let mut tail = fill.accept(Info::timed()).unwrap(); + tail.write_frame(Timestamp::ZERO, b"tail".as_slice()).unwrap(); + tail.finish().unwrap(); + assert_eq!(fetched.await.unwrap().index(), start); + assert_eq!(producer.state.read().lookup[&0].copies().count(), 2); + } + let mut sub = producer.subscribe(None); + let group = sub.recv_group().await.unwrap().unwrap(); + assert_eq!( + group.with_range(0..).index(), + 200, + "a bounded head does not replace the live feed for whole-group readers" + ); + live.abort(Error::Cancel).unwrap(); + // The backfill remains cache-owned after the live copy failed. + assert!(!producer.state.read().lookup[&0].is_aborted()); + } + + /// Queued ranges widen before dispatch; a taken cap does not promise an unseen suffix. + #[moq_clock::test] + #[cfg(feature = "async")] + async fn bounded_fetches_widen_before_dispatch_and_split_after() { + let producer = track_producer("test", Info::timed()); + let dynamic = producer.dynamic(); + let consumer = producer.consume(); + let request = |end| FetchOptions { + options: group::Fetch::default(), + end: Some(end), + }; + let _a = consumer.fetch_range(0, request(3)); + let _b = consumer.fetch_range(0, request(5)); + let first = dynamic.requested_fill_group().await.unwrap(); + assert_eq!(first.frame_end(), Some(5)); + let _c = consumer.fetch_range(0, request(8)); + let second = dynamic.requested_fill_group().await.unwrap(); + assert_eq!((first.frame_end(), second.frame_end()), (Some(5), Some(8))); + } + /// An accepted group starts where its request did before anyone can see it, so a /// wider fetch arriving before the handler writes misses instead of being handed a /// reader that would skip the head it asked for. @@ -17318,7 +17536,7 @@ mod test { .write() .ok() .unwrap() - .insert_group_request(3, 0, None) + .insert_group_request(3, group::Bounds::default(), None) .unwrap(); assert!(subscriber.poll_recv_group(&waiter).is_pending()); assert!(fill.is_wanted(3), "the answer has not reached the reader yet"); diff --git a/rs/moq-net/src/model/track/fill.rs b/rs/moq-net/src/model/track/fill.rs index 2b1bc7dea15..fdd4431c675 100644 --- a/rs/moq-net/src/model/track/fill.rs +++ b/rs/moq-net/src/model/track/fill.rs @@ -42,6 +42,8 @@ pub(in crate::model) struct FillState { struct Pending { start: Position, end: u64, + // Exclusive frame cap in the last group; None asks through the original end. + frame_end: Option, // What its joins ask with now, shared with them and its handler. asks: kio::Shared, queued: bool, @@ -282,6 +284,7 @@ impl Eq for Refusal {} pub(super) struct Ask { pub start: Position, pub end: u64, + pub frame_end: Option, pub asking: Asking, } @@ -456,6 +459,18 @@ impl FillState { } } } + if !groups.is_empty() && groups.end == pending.end { + let end = if groups.end == ask.end { ask.frame_end } else { None }; + if pending.frame_end.is_some_and(|cap| end.is_none_or(|end| end > cap)) { + if pending.queued { + pending.frame_end = end; + } else { + // The source already saw the cap: the missing suffix asks for its own copy. + gaps.push(groups.end - 1..groups.end); + groups.end -= 1; + } + } + } if groups.is_empty() { continue; } @@ -509,6 +524,7 @@ impl FillState { Pending { start, end: groups.end, + frame_end: if groups.end == ask.end { ask.frame_end } else { None }, asks, queued: true, wants: RangeMap::from_iter([(groups.clone(), 1)]), @@ -626,7 +642,7 @@ pub(super) fn poll_requested( }) { let id = guard.pop().expect("predicate guaranteed a fill"); let pending = guard.pending.get(&id).expect("popped fill is pending"); - let (start, end) = (pending.start, pending.end); + let (start, end, frame_end) = (pending.start, pending.end, pending.frame_end); let (asks, result) = (pending.asks.clone(), pending.result.clone()); drop(guard); // Built with the fills unlocked, so no other lock nests under theirs. @@ -636,6 +652,7 @@ pub(super) fn poll_requested( id, start, end, + frame_end, asks, result, }; @@ -676,6 +693,8 @@ struct Inner { id: u64, start: Position, end: u64, + // Exclusive frame cap in the last group; None asks through the original end. + frame_end: Option, asks: kio::Shared, result: kio::Producer, } @@ -802,7 +821,7 @@ impl Fill { .inner .state .read() - .covering_group(sequence, self.inner.frame_start(sequence)) + .covering_range(sequence, self.inner.bounds(sequence)) .is_some(); if !cached { return Some(sequence); @@ -922,6 +941,7 @@ impl Fill { inner: self.inner.clone(), sequence, frame_start: self.inner.frame_start(sequence), + frame_end: self.inner.bounds(sequence).end, priority: self.priority(), max_delay: self.max_delay(), } @@ -970,7 +990,22 @@ impl Inner { } } + fn bounds(&self, sequence: u64) -> group::Bounds { + group::Bounds { + start: self.frame_start(sequence), + end: if sequence.checked_add(1) == Some(self.end) { + self.frame_end + } else { + None + }, + } + } + fn accept(&self, sequence: u64, info: Option) -> Result { + self.accept_from(sequence, info, self.bounds(sequence)) + } + + fn accept_from(&self, sequence: u64, info: Option, frames: group::Bounds) -> Result { if !self.contains(sequence) { return Err(Error::InvalidRange); } @@ -981,8 +1016,8 @@ impl Inner { return Err(Error::Duplicate); } // Cache the group before answering: readers resolve an accept through the cache. - let res = TrackState::modify(&self.state) - .and_then(|mut state| state.insert_group_request(sequence, self.frame_start(sequence), info)); + let res = + TrackState::modify(&self.state).and_then(|mut state| state.insert_group_request(sequence, frames, info)); if let Ok(mut outcome) = self.result.write() { outcome.accept(sequence..end); } @@ -1190,6 +1225,7 @@ pub(crate) struct GroupFill { inner: Arc, sequence: u64, frame_start: u64, + frame_end: Option, priority: i8, max_delay: Option, } @@ -1217,6 +1253,16 @@ impl GroupFill { self.frame_start } + /// The requested exclusive last frame, if this copy is bounded at the back. + pub(crate) fn frame_end(&self) -> Option { + self.frame_end + } + + /// Accept the actual wire range, which an older or compressed upstream may have widened. + pub(crate) fn accept_from(&self, info: Info, frames: group::Bounds) -> Result { + self.inner.accept_from(self.sequence, Some(info), frames) + } + /// Poll until no reader waits on this group, as [`Fill::poll_unwanted`]. pub(crate) fn poll_unused(&self, waiter: &kio::Waiter) -> Poll<()> { self.inner.poll_unwanted(self.sequence, waiter) @@ -1240,6 +1286,7 @@ impl GroupFill { } /// Insert the group into the cache, as [`Fill::accept`]. + #[cfg(test)] pub(crate) fn accept(self, info: impl Into>) -> Result { self.inner.accept(self.sequence, info.into()) } diff --git a/rs/moq-net/src/model/track/miss.rs b/rs/moq-net/src/model/track/miss.rs index ebc7083c32e..0deabd5baf9 100644 --- a/rs/moq-net/src/model/track/miss.rs +++ b/rs/moq-net/src/model/track/miss.rs @@ -99,7 +99,7 @@ impl Misses { Order::Descending => self.filled.last(), } .copied()?; - if let Some(slot) = state.lookup.get(&sequence).filter(|slot| !slot.is_aborted()) { + if let Some(slot) = state.lookup.get(&sequence).filter(|slot| slot.serves_end()) { return Some(slot.serving().clone()); } self.filled.remove(&sequence); @@ -226,7 +226,7 @@ impl Misses { Ok(()) => { self.settle(&state, groups.clone(), given_up); for (&sequence, slot) in state.lookup.range(groups) { - if !slot.is_aborted() { + if slot.serves_end() { self.filled.insert(sequence); taken.remove(sequence..sequence + 1); } @@ -315,7 +315,13 @@ impl Misses { let arrived: Vec = self .refused .iter() - .flat_map(|run| state.lookup.range(run.clone()).map(|(sequence, _)| *sequence)) + .flat_map(|run| { + state + .lookup + .range(run.clone()) + .filter(|(_, slot)| slot.serves_end()) + .map(|(sequence, _)| *sequence) + }) .collect(); for sequence in arrived { self.refused.remove(sequence..sequence + 1); @@ -449,6 +455,7 @@ impl Misses { let ask = Ask { start: Position::group(run.start), end: run.end, + frame_end: None, asking: *asking, }; let (joins, unfillable) = fills.lock().join(fills, &ask); @@ -558,7 +565,7 @@ pub(super) fn holes(state: &TrackState, run: ops::Range) -> Vec Date: Tue, 27 Oct 2026 04:55:41 -0700 Subject: [PATCH 03/16] Share canonical group content across transport deliveries Activate shared indexed storage only when range deliveries coexist, retaining each delivery progress, bounds, terminal and arrival records. Preserve ordinary single-copy writes and record ownership/cost controls; typed and compressed integration validation remains pending. Co-Authored-By: GPT-6 --- quest/m1/transport-frame-offsets.md | 88 ++++ rs/moq-net/src/model/cache.rs | 15 + rs/moq-net/src/model/frame.rs | 610 +++++++++++++++++++++++--- rs/moq-net/src/model/group.rs | 332 +++++++++++--- rs/moq-net/src/model/group/content.rs | 266 +++++++++++ rs/moq-net/src/model/track.rs | 309 ++++++++++++- 6 files changed, 1471 insertions(+), 149 deletions(-) create mode 100644 rs/moq-net/src/model/group/content.rs diff --git a/quest/m1/transport-frame-offsets.md b/quest/m1/transport-frame-offsets.md index 7f6f9564b70..d250f00b3b6 100644 --- a/quest/m1/transport-frame-offsets.md +++ b/quest/m1/transport-frame-offsets.md @@ -151,3 +151,91 @@ nix develop --command just rs test -p moq-net --lib \ } } ``` + +### Canonical-owner prototype checkpoint + +The private Rust prototype activates an indexed content owner when a second +transport delivery would coexist with the first. Ordinary single-copy whole +and batch writes retain their existing representation. Activation imports the +existing payloads without copying and attaches the exact in-flight `FrameBuf`, +so an already-streaming reader and writer keep their progress. New deliveries +fork their own received counter while sharing immutable content, publication +and the origin's admission. Contradictory bytes use `ProtocolViolation`. + +A `TrackState`-scoped weak index lets a refill discover an owner still retained +by older tails after its Slot was removed. Owners remove their weak entries; +the index is owned by the live owners, so dead history does not accumulate. +Explicit live-group recreation replaces the sequence's indexed incarnation. +A different track instance does not inherit it. Delivery logs, bounds, FIN, +errors, timestamps and public arrival records remain separate. IETF Object IDs +have their own key space instead of being mistaken for dense frame ordinals. + +Intrinsic `FrameBuf` charges follow the shared storage and each progress view, +including streamed readers that outlive the delivery's partial slot. A +payload-only `Charge::transfer` moves old bytes without recording them as new +writes. Fresh attachment and already-canonical transfer have controls. Deferred +log entries hold weak row handles; a non-polling cursor cannot keep a canonical +owner alive through retired log storage. Handoff heads retain the content owner. + +The first eager prototype exposed real ownership and accounting integration +failures: 466/491 existing group/track tests passed initially, then 482/491 +after ownership corrections. Its sources and raw results remain in +`.tmp/eager-content-prototype/`, `.tmp/content-model-tests.log` and +`.tmp/content-model-tests-2.log`. It was replaced by lazy activation rather than +relaxing cache-pressure thresholds. Two refill fixtures had different bytes for +the same fixed incarnation; only those payloads were made consistent, preserving +the bounds, lifetime and end-refusal assertions. + +The lazy implementation passes 508/508 strict frame, group and track controls, +including the following new outer controls: + +- Eight distinct tail starts with all earlier readers held, at 0, 1 and 1024 B + per frame, both bounded and unbounded. Matching nonempty frame payloads have + the same physical pointer. Dropping identity probes changes no charge. +- Actual Slot removal with an older reader-held tail, followed by a refill + sharing its owner; explicit live recreation accepts different bytes and uses + a separate owner. +- Activation while the original frame and streaming reader are already open. + Overlap credits no progress to that delivery. One abort leaves the other + producer valid and admitted. Two completions retain distinct arrival times + 10 ms apart under the deterministic clock while sharing the same payload. + +The nine delivery handles remain distinct. Their logs and arrival metadata are +charged separately; this proves one canonical content store, not a constant +bound on all delivery bookkeeping or arbitrary caller-retained public Frames. +The bounded case also retains the live frame at 32 beside tail frames 1..16. + +| Payload/frame | End | Canonical rows | Unique payload | Shared charge | Delivery charge | Discovery charge | Total | +| --- | --- | --- | --- | --- | --- | --- | --- | +| 0 B | Unbounded | 32 | 0 B | 10368 B | 34272 B | 136 B | 44776 B | +| 0 B | Exclusive 17 | 17 | 0 B | 5688 B | 22272 B | 136 B | 28096 B | +| 1 B | Unbounded | 32 | 32 B | 10400 B | 34272 B | 136 B | 44808 B | +| 1 B | Exclusive 17 | 17 | 17 B | 5705 B | 22272 B | 136 B | 28113 B | +| 1024 B | Unbounded | 32 | 32768 B | 43136 B | 34272 B | 136 B | 77544 B | +| 1024 B | Exclusive 17 | 17 | 17408 B | 23096 B | 22272 B | 136 B | 45504 B | + +On this x86_64 build the shared frame content allocation is 184 B, a progress +view is 56 B, and admission bookkeeping is 32 B. The empty-payload cases expose +the metadata independently of payload savings. These are ownership and charge +measurements, not quiet-host timing or allocator-overhead claims. + +```sh +nix develop --command just rs test -p moq-net --lib \ + -E 'test(model::frame::tests) | test(model::group::test) | test(model::track::test)' \ + --no-fail-fast --success-output immediate +``` + +Raw output: `.tmp/lazy-content-model-tests-2.log`, SHA256 +`76e5c37663d9529589e0119fa817cf3c064cfd4cc436a099f56bf6055bdccbda`. +`just fix` also passed. These focused results precede this checkpoint's final +integration gates; the full CI and interop pass on 5796 remain separate evidence. + +The Goal remains unfinished pending typed sparse/relay and compressed raw/plain +outer preservation controls, actual-main prerequisite integration, final clean +committed-head gates and real surface proof. Actual compressed transport ingress +must share stored raw slices as received, with one lazy inflated copy. Local +plaintext-to-DEFLATE producer scratch is a distinct representation; it is not +claimed to share raw wire storage. Compressed transport fetches must continue +widening to start 0/end None, and a plain reader without the prefix must still +refuse. No public API, wire, resource-budget or maintainer-policy choice has been +selected by this checkpoint. diff --git a/rs/moq-net/src/model/cache.rs b/rs/moq-net/src/model/cache.rs index 17c54841b8f..6de5651f370 100644 --- a/rs/moq-net/src/model/cache.rs +++ b/rs/moq-net/src/model/cache.rs @@ -1159,6 +1159,21 @@ impl Charge { self.bytes } + /// Move already-accounted payload to a shared content owner without recording a write. + pub(super) fn transfer(&mut self, bytes: u64) -> Extra { + if self.track.is_none() { + return Extra::default(); + } + self.bytes = self + .bytes + .checked_sub(bytes) + .expect("transferred payload must already be charged"); + Extra { + track: self.track.clone(), + bytes: AtomicU64::new(bytes), + } + } + /// The shared handle to this group's last-access stamp, so the group can read it /// without taking the state lock this charge lives behind. pub(super) fn access(&self) -> Arc { diff --git a/rs/moq-net/src/model/frame.rs b/rs/moq-net/src/model/frame.rs index d14efe8e8ca..728ac55db7a 100644 --- a/rs/moq-net/src/model/frame.rs +++ b/rs/moq-net/src/model/frame.rs @@ -6,14 +6,15 @@ //! the borrow checker enforces that only one frame is open at a time. A [`Consumer`] //! reads one frame, sharing the group's channel rather than a per-frame one. use std::ops::Range; -use std::sync::Arc; use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; +use std::sync::{Arc, Weak}; use std::sync::{Mutex, OnceLock, PoisonError}; use std::task::{Poll, ready}; use arrayvec::ArrayVec; use bytes::{Buf, Bytes, BytesMut}; +use crate::cache; use crate::group::{self, GroupState}; use crate::time::Instant; use crate::{Error, IntoBytes, Result, Timestamp, stats}; @@ -435,27 +436,48 @@ impl Drop for Reservation { } } -/// Payload storage for the single in-flight frame, shared between the writing -/// [`Producer`] and any streaming [`Consumer`]s. +/// One delivery's progress over a frame's canonical payload storage. /// /// A whole-frame [`Bytes`] write is stored directly. Chunked writes are copied into a /// heap [`Segment`], or a chain of them for a frame larger than [`SEGMENT`], each either /// allocated at its full length by its first byte or grown as bytes arrive (see -/// [`Budget`]). The producer writes through the raw pointer (sole writer, guaranteed by -/// the frame owning the group's only writer); `written` provides happens-before for -/// cross-thread reads. +/// [`Budget`]). Clones share delivery progress; [`Self::fork`] starts another delivery +/// over the same content. Publication serializes their writes, never rewriting bytes +/// already visible to readers, while each delivery advances only as its bytes arrive. #[derive(Clone)] pub(super) struct FrameBuf(Arc); struct FrameBufInner { + content: Arc, + received: AtomicUsize, + bill: OnceLock, +} + +struct FrameContent { size: usize, - grow: bool, + grow: AtomicBool, written: AtomicUsize, storage: OnceLock, + frozen: OnceLock, + publication: Mutex, + bill: OnceLock, +} + +#[derive(Default)] +struct Publication { + admission: Weak, + admitted: bool, +} + +/// One canonical frame's reservation while any of its producers remains active. +/// Even a failed reservation is shared, so overlapping deliveries use the same +/// growth strategy without each attempting another admission. +pub(super) struct Admission { + _reserved: Option, } enum FrameStorage { - Shared(Bytes), + Shared(Payload), Fixed(Arc), // Replaced by a larger copy whenever a write outgrows it. A reader clones the current // segment under the lock, so a slice it already handed out keeps the old one alive. @@ -504,8 +526,8 @@ struct Segment { capacity: usize, } -// Safety: `data` is owned (Box-allocated, freed in Drop). The producer is the sole -// writer and consumers only read bytes `< written`. +// Safety: `data` is owned (Box-allocated, freed in Drop). Publication serializes +// writers, which never rewrite published bytes; consumers only read `< written`. unsafe impl Send for Segment {} unsafe impl Sync for Segment {} @@ -574,6 +596,10 @@ impl AsRef<[u8]> for Filled { } impl FrameBuf { + pub(super) const CONTENT_HEAP: usize = 2 * size_of::() + size_of::(); + pub(super) const VIEW_HEAP: usize = 2 * size_of::() + size_of::(); + pub(super) const ADMISSION_HEAP: usize = 2 * size_of::() + size_of::(); + /// A buffer for a frame of `size` bytes, allocated at that size on the first write. /// /// The oversized-frame guard lives in [`group::Producer`], which rejects a declared @@ -583,65 +609,216 @@ impl FrameBuf { } /// A buffer for a frame of `size` bytes that allocates only as bytes arrive. + #[cfg(test)] pub(super) fn growing(size: usize) -> Self { Self::with_growth(size, true) } fn with_growth(size: usize, grow: bool) -> Self { Self(Arc::new(FrameBufInner { - size, - grow, - written: AtomicUsize::new(0), - storage: OnceLock::new(), + content: Arc::new(FrameContent { + size, + grow: AtomicBool::new(grow), + written: AtomicUsize::new(0), + storage: OnceLock::new(), + frozen: OnceLock::new(), + publication: Mutex::default(), + bill: OnceLock::new(), + }), + received: AtomicUsize::new(0), + bill: OnceLock::new(), })) } + /// Keep an already-complete payload without copying its buffers or segments. + pub(super) fn from_payload(payload: Payload) -> Self { + let size = payload.len(); + Self(Arc::new(FrameBufInner { + content: Arc::new(FrameContent { + size, + grow: AtomicBool::new(false), + written: AtomicUsize::new(size), + storage: OnceLock::from(FrameStorage::Shared(payload.clone())), + frozen: OnceLock::from(payload), + publication: Mutex::default(), + bill: OnceLock::new(), + }), + received: AtomicUsize::new(size), + bill: OnceLock::new(), + })) + } + + /// Another delivery of the same immutable frame, starting before its first byte. + pub(super) fn fork(&self) -> Self { + Self(Arc::new(FrameBufInner { + content: self.0.content.clone(), + received: AtomicUsize::new(0), + bill: OnceLock::new(), + })) + } + + /// Bill physical content once, including bytes already received before attachment. + /// The charge follows its buffers even if a cached row lets go before a stream does. + pub(super) fn bill_content(&self, bill: cache::Extra) { + let content = &self.0.content; + let publication = content.publication.lock().unwrap_or_else(PoisonError::into_inner); + content.bill.get_or_init(|| { + let admission = if publication.admitted { Self::ADMISSION_HEAP } else { 0 }; + let written = self.shared_written(Ordering::Relaxed) as u64; + // A lazy owner may take already-billed payload from the original delivery. + let fresh = written + .checked_sub(bill.size()) + .expect("transferred payload cannot exceed published bytes"); + bill.add(fresh + (Self::CONTENT_HEAP + admission) as u64); + bill + }); + } + + /// One delivery's metadata, retained by its streaming readers and shared by clones. + pub(super) fn bill_view(&self, bill: cache::Extra) { + self.0.bill.get_or_init(|| { + bill.add(Self::VIEW_HEAP as u64); + bill + }); + } + + /// The canonical allocation's charge, counted once by its cache owner. + pub(super) fn content_size(&self) -> u64 { + self.0.content.bill.get().map_or(0, cache::Extra::size) + } + + /// This delivery's charge; clones retain the same view allocation. + pub(super) fn view_size(&self) -> u64 { + self.0.bill.get().map_or(0, cache::Extra::size) + } + + /// Share the existing per-frame admission until its last active producer ends. + /// Complete canonical data is replayed without reserving memory again. + pub(super) fn admit(&self, budget: &Budget) -> Option> { + let content = &self.0.content; + let mut publication = content.publication.lock().unwrap_or_else(PoisonError::into_inner); + if self.shared_written(Ordering::Acquire) == self.size() { + return None; + } + if let Some(admission) = publication.admission.upgrade() { + return Some(admission); + } + let reserved = budget.reserve(self.size().min(SEGMENT)); + // Existing bytes keep their allocations. A later admission may only make + // future growth more conservative once storage already exists. + if reserved.is_none() || content.storage.get().is_none() { + content.grow.store(reserved.is_none(), Ordering::Relaxed); + } + let admission = Arc::new(Admission { _reserved: reserved }); + publication.admission = Arc::downgrade(&admission); + if !publication.admitted { + publication.admitted = true; + if let Some(bill) = content.bill.get() { + bill.add(Self::ADMISSION_HEAP as u64); + } + } + Some(admission) + } + /// The frame's declared size. pub(super) fn size(&self) -> usize { - self.0.size + self.0.content.size } + /// Bytes this delivery received, independently of other copies' progress. pub(super) fn written(&self, ord: Ordering) -> usize { - self.0.written.load(ord) + self.0.received.load(ord) + } + + /// Unique canonical bytes published so far, for shared-content accounting. + pub(super) fn shared_written(&self, ord: Ordering) -> usize { + self.0.content.written.load(ord) } fn try_set_bytes(&self, bytes: Bytes) -> std::result::Result<(), Bytes> { - if bytes.len() != self.size() || self.written(Ordering::Acquire) != 0 { + if bytes.len() != self.size() || self.shared_written(Ordering::Acquire) != 0 { return Err(bytes); } self.0 + .content .storage - .set(FrameStorage::Shared(bytes)) + .set(FrameStorage::Shared(Payload::Whole(bytes))) .map_err(|storage| match storage { - FrameStorage::Shared(bytes) => bytes, + FrameStorage::Shared(Payload::Whole(bytes)) => bytes, _ => unreachable!("try_set_bytes only installs shared storage"), }) } - /// Safety: caller must be the sole producer and `new_written` must be `<= size`. + /// Safety: caller holds the publication mutex and `new_written` is `<= size`. unsafe fn store_written(&self, new_written: usize) { + let content = &self.0.content; + let before = content.written.load(Ordering::Relaxed); + if let Some(bill) = content.bill.get() { + bill.add((new_written - before) as u64); + } // Release pairs with consumers' Acquire load to publish prior writes. - self.0.written.store(new_written, Ordering::Release); + content.written.store(new_written, Ordering::Release); } - /// Append `src` at the current write offset and publish it. - /// - /// Safety relies on the single-producer invariant: only one [`Producer`] exists for - /// a frame (it owns the group's only writer), so this is the sole writer even though - /// it takes `&self`. - fn append(&self, src: &[u8]) { + /// Validate this delivery's overlap, then publish only its unseen suffix. + pub(super) fn append(&self, src: &[u8]) -> Result<()> { + self.write_chunk(src) + } + + fn write_chunk(&self, chunk: B) -> Result<()> { + let content = &self.0.content; + let _publication = content.publication.lock().unwrap_or_else(PoisonError::into_inner); + let start = self.written(Ordering::Relaxed); + let end = start + .checked_add(chunk.as_ref().len()) + .filter(|end| *end <= self.size()) + .ok_or(Error::WrongSize)?; + let published = self.shared_written(Ordering::Acquire); + if start > published { + return Err(Error::ProtocolViolation); + } + let overlap = end.min(published); + let mut at = start; + while at < overlap { + let prior = self.slice(at, overlap); + if prior.as_ref() != &chunk.as_ref()[at - start..at - start + prior.len()] { + return Err(Error::ProtocolViolation); + } + at += prior.len(); + } + if end > published { + if published == 0 && end == self.size() { + match self.try_set_bytes(chunk.into_bytes()) { + Ok(()) => { + // Safety: the mutex excludes other publishers; the full size was checked. + unsafe { self.store_written(end) }; + } + Err(chunk) => self.append_suffix(&chunk), + } + } else { + self.append_suffix(&chunk.as_ref()[published - start..]); + } + } + self.0.received.store(end, Ordering::Release); + Ok(()) + } + + /// Append after the shared prefix while holding the publication mutex. + fn append_suffix(&self, src: &[u8]) { if src.is_empty() { return; } - let prev = self.written(Ordering::Relaxed); + let prev = self.shared_written(Ordering::Relaxed); let size = self.size(); - let storage = self.0.storage.get_or_init(|| match self.0.grow { + let content = &self.0.content; + let grow = content.grow.load(Ordering::Relaxed); + let storage = content.storage.get_or_init(|| match grow { _ if size > SEGMENT => FrameStorage::Chain(Mutex::default()), true => FrameStorage::Growing(Mutex::new(Arc::new(Segment::new(0)))), false => FrameStorage::Fixed(Arc::new(Segment::new(size))), }); - // Safety (every write below): sole writer; the caller bounds-checked `src` against - // the remaining size, and consumers only read `[..written]`. + // Safety (every write below): the publication mutex makes this the sole writer; + // the caller checked the remaining size, and consumers only read `[..written]`. match storage { // Only reachable if the frame is already complete via shared storage, which // `Producer::write` rejects for a non-empty chunk. Nothing to copy. @@ -654,39 +831,50 @@ impl FrameBuf { } FrameStorage::Chain(segments) => { let mut segments = segments.lock().unwrap_or_else(PoisonError::into_inner); - segments.append(prev, src, size, self.0.grow); + segments.append(prev, src, size, grow); } } - // Safety: sole writer, and the caller bounds-checked `src` against the size. + // Safety: the publication mutex excludes other writers, and the caller checked the size. unsafe { self.store_written(prev + src.len()) }; } - /// Freeze the written frame into the completed payload, without a copy. - fn freeze(&self) -> Payload { - match self.0.storage.get() { - Some(FrameStorage::Chain(segments)) => { - let segments = segments.lock().unwrap_or_else(PoisonError::into_inner); - debug_assert!(segments.open.is_none(), "froze a frame mid-segment"); - Payload::Chain(Chain { - segments: segments.sealed.as_slice().into(), - start: 0, - }) - } - _ => Payload::Whole(self.slice(0, self.size())), - } + /// The completed payload, shared by every delivery without copying its segments. + /// Called only once the canonical payload is complete. Delivery completion is + /// checked separately by [`Producer::finish`]. + pub(super) fn freeze(&self) -> Payload { + debug_assert_eq!(self.shared_written(Ordering::Acquire), self.size()); + self.0 + .content + .frozen + .get_or_init(|| match self.0.content.storage.get() { + Some(FrameStorage::Shared(payload)) => payload.clone(), + Some(FrameStorage::Chain(segments)) => { + let segments = segments.lock().unwrap_or_else(PoisonError::into_inner); + debug_assert!(segments.open.is_none(), "froze a frame mid-segment"); + Payload::Chain(Chain { + segments: segments.sealed.as_slice().into(), + start: 0, + }) + } + _ => Payload::Whole(self.slice(0, self.size())), + }) + .clone() } /// A zero-copy slice of the written region from `start` to `end`, or to the end of /// `start`'s segment if that comes first. /// - /// `end` must not exceed a `written` value already loaded with `Acquire`, which is - /// also what makes the segment read here at least as new as those bytes. + /// `end` must not exceed a shared published value already loaded with `Acquire`. + /// A delivery's own received count is always within that prefix too. fn slice(&self, start: usize, end: usize) -> Bytes { if start >= end { return Bytes::new(); } - let (segment, base) = match self.0.storage.get() { - Some(FrameStorage::Shared(bytes)) => return bytes.slice(start..end), + let (segment, base) = match self.0.content.storage.get() { + Some(FrameStorage::Shared(payload)) => { + let chunk = payload.chunk_at(start); + return chunk.slice(..chunk.len().min(end - start)); + } Some(FrameStorage::Fixed(segment)) => (segment.clone(), 0), Some(FrameStorage::Growing(current)) => (current.lock().unwrap_or_else(PoisonError::into_inner).clone(), 0), Some(FrameStorage::Chain(segments)) => { @@ -710,8 +898,8 @@ impl FrameBuf { /// Heap bytes the buffer currently holds for the payload. #[cfg(test)] pub(super) fn allocated(&self) -> usize { - match self.0.storage.get() { - Some(FrameStorage::Shared(bytes)) => bytes.len(), + match self.0.content.storage.get() { + Some(FrameStorage::Shared(payload)) => payload.len(), Some(FrameStorage::Fixed(segment)) => segment.capacity, Some(FrameStorage::Growing(current)) => current.lock().unwrap_or_else(PoisonError::into_inner).capacity, Some(FrameStorage::Chain(segments)) => { @@ -752,7 +940,7 @@ pub struct Producer { // chunk's bytes. Empty (no-op) for an untagged group. stats: stats::Meter, // The frame's share of the origin's [`Budget`], if it got one, held until it ends. - _reserved: Option, + _reserved: Option>, } impl std::ops::Deref for Producer { @@ -764,7 +952,7 @@ impl std::ops::Deref for Producer { } impl Producer { - pub(super) fn new(group: group::Producer, buf: FrameBuf, info: Info, reserved: Option) -> Self { + pub(super) fn new(group: group::Producer, buf: FrameBuf, info: Info, reserved: Option>) -> Self { Self { group: Some(group), buf, @@ -834,21 +1022,11 @@ impl Producer { } // Ingress payload: count the chunk's bytes as they're written. self.stats.bytes(len as u64); - // Fast path: a single whole-frame write keeps the caller's allocation. if len == self.buf.size() && self.buf.written(Ordering::Acquire) == 0 { - match self.buf.try_set_bytes(chunk.into_bytes()) { - Ok(()) => { - let size = self.buf.size(); - // Safety: `try_set_bytes` checked the buffer exactly matches the declared - // size, so publishing all bytes is in bounds. - unsafe { self.buf.store_written(size) }; - } - Err(chunk) => self.buf.append(&chunk), - } + self.buf.write_chunk(chunk) } else { - self.buf.append(chunk.as_ref()); + self.buf.append(chunk.as_ref()) } - Ok(()) } /// Publish what has been written so far, waking consumers parked on the group. @@ -1272,18 +1450,308 @@ where mod tests { use super::*; + #[test] + fn activating_shared_content_transfers_payload_without_recording_it_twice() { + let pool = cache::Pool::unbounded(); + let account = cache::Track::new(pool.clone(), Default::default(), kio::Weak::new()); + let mut delivery = account.charge(); + delivery.add(8); + let before = pool.used(); + account.take_written(); + let content = FrameBuf::from_payload(Bytes::from_static(b"abcdefgh").into()); + content.bill_content(delivery.transfer(8)); + assert_eq!(content.content_size(), 8 + FrameBuf::CONTENT_HEAP as u64); + assert_eq!(pool.used(), before + FrameBuf::CONTENT_HEAP as u64); + assert_eq!( + account.take_written(), + FrameBuf::CONTENT_HEAP as u64, + "old payload is not a new write" + ); + delivery.add(8); + account.take_written(); + content.bill_content(delivery.transfer(8)); + assert_eq!( + account.take_written(), + 0, + "an already-canonical owner records no second write" + ); + assert_eq!(pool.used(), before + FrameBuf::CONTENT_HEAP as u64); + delivery.clear(); + assert_eq!(pool.used(), content.content_size()); + drop(content); + assert_eq!(pool.used(), 0); + } + + #[test] + fn canonical_content_keeps_each_delivery_progress_independent() { + let a = FrameBuf::growing(8); + let b = a.fork(); + let b_reader = b.clone(); + a.append(b"abcd").unwrap(); + let early = a.slice(0, 4); + b.append(b"ab").unwrap(); + b.append(b"cdef").unwrap(); + assert_eq!((a.written(Ordering::Acquire), b.written(Ordering::Acquire)), (4, 6)); + assert_eq!( + b_reader.written(Ordering::Acquire), + 6, + "clones follow their own delivery" + ); + a.append(b"efgh").unwrap(); + assert_eq!( + b.written(Ordering::Acquire), + 6, + "another producer's completion credits no bytes" + ); + assert_eq!(b.shared_written(Ordering::Acquire), 8); + assert_eq!(b.allocated(), 8, "the overlapping payload is stored once"); + b.append(b"gh").unwrap(); + assert_eq!(early, b"abcd".as_slice()); + let (left, right) = (a.freeze().contiguous(0), b.freeze().contiguous(0)); + assert_eq!(left, b"abcdefgh".as_slice()); + assert_eq!(left.as_ptr(), right.as_ptr()); + eprintln!( + "canonical frame metadata: content={}B, view={}B, active admission={}B", + FrameBuf::CONTENT_HEAP, + FrameBuf::VIEW_HEAP, + FrameBuf::ADMISSION_HEAP + ); + } + + /// A disagreement late in the overlap cannot publish an otherwise new suffix. + #[test] + fn a_conflicting_delivery_leaves_shared_content_and_progress_unchanged() { + let size = SEGMENT + 8; + let payload: Vec<_> = (0..size).map(|i| (i % 251) as u8).collect(); + let a = FrameBuf::growing(size); + a.append(&payload[..SEGMENT + 4]).unwrap(); + let b = a.fork(); + b.append(&payload[..SEGMENT - 2]).unwrap(); + let allocated = a.allocated(); + let mut conflict = payload[SEGMENT - 2..].to_vec(); + conflict[3] ^= 1; + assert!(matches!(b.append(&conflict), Err(Error::ProtocolViolation))); + assert_eq!(b.written(Ordering::Acquire), SEGMENT - 2); + assert_eq!(a.shared_written(Ordering::Acquire), SEGMENT + 4); + assert_eq!(a.allocated(), allocated); + a.append(&payload[SEGMENT + 4..]).unwrap(); + assert_eq!(a.freeze().contiguous(0), payload, "the valid delivery remains usable"); + assert!(matches!(b.append(&vec![0; size]), Err(Error::WrongSize))); + assert_eq!(b.written(Ordering::Acquire), SEGMENT - 2); + } + + #[test] + fn a_complete_chain_replays_without_copying_or_readmitting_it() { + let payload = Payload::Chain(Chain { + segments: Arc::from([Bytes::from(vec![1; SEGMENT]), Bytes::from_static(b"tail")]), + start: 0, + }); + let canonical = FrameBuf::from_payload(payload.clone()); + let copy = canonical.fork(); + assert_eq!(copy.written(Ordering::Acquire), 0); + assert!(copy.admit(&Budget::new(0)).is_none()); + let mut at = 0; + while at < payload.len() { + let chunk = payload.chunk_at(at); + copy.append(&chunk).unwrap(); + at += chunk.len(); + } + let (Payload::Chain(original), Payload::Chain(replayed)) = (&payload, copy.freeze()) else { + panic!("the chain stays segmented"); + }; + assert!(Arc::ptr_eq(&original.segments, &replayed.segments)); + assert_eq!(copy.allocated(), payload.len()); + } + + #[test] + fn simultaneous_deliveries_publish_one_segmented_payload() { + const SIZE: usize = 2 * SEGMENT + 37; + const ROUNDS: usize = 12; + let payload: Vec<_> = (0..SIZE).map(|i| (i % 251) as u8).collect(); + for grow in [false, true] { + let a = FrameBuf::with_growth(SIZE, grow); + let b = a.fork(); + let barrier = std::sync::Barrier::new(2); + std::thread::scope(|scope| { + for (buf, chunk) in [(&a, SIZE.div_ceil(7)), (&b, SIZE.div_ceil(11))] { + let (payload, barrier) = (&payload, &barrier); + scope.spawn(move || { + let mut at = 0; + for round in 0..ROUNDS { + barrier.wait(); + let end = ((round + 1) * chunk).min(SIZE); + buf.append(&payload[at..end]).unwrap(); + assert_eq!(buf.written(Ordering::Acquire), end); + at = end; + barrier.wait(); + } + }); + } + }); + let (Payload::Chain(left), Payload::Chain(right)) = (a.freeze(), b.freeze()) else { + panic!("chunked content stays segmented"); + }; + assert!( + Arc::ptr_eq(&left.segments, &right.segments), + "even the segment list is shared" + ); + assert_eq!(a.allocated(), SIZE); + assert_eq!(Payload::Chain(left).contiguous(0), payload); + } + } + + #[test] + fn admission_ends_with_active_producers_not_cached_views() { + let budget = Budget::new(SEGMENT); + let a = FrameBuf::new(SEGMENT + 1); + let b = a.fork(); + let a_active = a.admit(&budget).unwrap(); + let b_active = b.admit(&budget).unwrap(); + assert!(Arc::ptr_eq(&a_active, &b_active)); + assert!(budget.reserve(1).is_none()); + a.append(&vec![7; SEGMENT + 1]).unwrap(); + let replay = a.fork(); + assert!( + replay.admit(&budget).is_none(), + "complete content reserves nothing on replay" + ); + drop(a_active); + assert!( + budget.reserve(1).is_none(), + "another active delivery owns the same reservation" + ); + drop(b_active); + let returned = budget.reserve(SEGMENT).expect("cached views keep no admission alive"); + assert_eq!(b.written(Ordering::Acquire), 0); + assert_eq!(replay.written(Ordering::Acquire), 0); + drop(returned); + } + + #[test] + fn unadmitted_deliveries_share_the_same_growing_frame() { + let budget = Budget::new(0); + let a = FrameBuf::new(SEGMENT); + let b = a.fork(); + let left = a.admit(&budget).unwrap(); + let right = b.admit(&budget).unwrap(); + assert!(Arc::ptr_eq(&left, &right)); + assert!(left._reserved.is_none()); + a.append(b"a").unwrap(); + assert_eq!(a.allocated(), 1, "failed admission grows only with received bytes"); + b.append(b"ab").unwrap(); + assert_eq!(a.allocated(), 2); + assert_eq!((a.written(Ordering::Acquire), b.written(Ordering::Acquire)), (1, 2)); + } + + #[test] + fn canonical_and_delivery_charges_follow_their_last_owners() { + let pool = cache::Pool::unbounded(); + let account = cache::Track::new(pool.clone(), Default::default(), kio::Weak::new()); + let charge = account.charge(); + let baseline = pool.used(); + let a = FrameBuf::growing(8); + let active = a.admit(&Budget::new(0)).unwrap(); + a.append(b"abcd").unwrap(); + assert_eq!(pool.used(), baseline, "the content has not attached its account yet"); + a.bill_content(charge.extra()); + a.bill_view(charge.extra()); + let b = a.fork(); + b.bill_content(charge.extra()); + b.bill_view(charge.extra()); + let reader = b.clone(); + reader.bill_view(charge.extra()); + let metadata = (FrameBuf::CONTENT_HEAP + FrameBuf::ADMISSION_HEAP + 2 * FrameBuf::VIEW_HEAP) as u64; + assert_eq!( + pool.used(), + baseline + metadata + 4, + "late attachment and clones charge once" + ); + b.append(b"abcdef").unwrap(); + assert_eq!( + pool.used(), + baseline + metadata + 6, + "overlap contributes only two new bytes" + ); + a.append(b"efgh").unwrap(); + assert_eq!(pool.used(), baseline + metadata + 8); + drop(active); + drop(a); + assert_eq!(pool.used(), baseline + metadata + 8 - FrameBuf::VIEW_HEAP as u64); + drop(b); + assert_eq!( + pool.used(), + baseline + metadata + 8 - FrameBuf::VIEW_HEAP as u64, + "the streaming clone owns its view and content" + ); + drop(reader); + assert_eq!( + pool.used(), + baseline, + "the weak admission and all intrinsic charges release with content" + ); + } + + #[test] + fn preattached_content_bills_admission_once_and_completed_payload_needs_none() { + let pool = cache::Pool::unbounded(); + let account = cache::Track::new(pool.clone(), Default::default(), kio::Weak::new()); + let charge = account.charge(); + let baseline = pool.used(); + let a = FrameBuf::new(8); + a.bill_content(charge.extra()); + a.bill_view(charge.extra()); + let metadata = (FrameBuf::CONTENT_HEAP + FrameBuf::VIEW_HEAP) as u64; + assert_eq!(pool.used(), baseline + metadata); + let budget = Budget::new(0); + let first = a.admit(&budget).unwrap(); + let copy = a.fork(); + let second = copy.admit(&budget).unwrap(); + assert!(Arc::ptr_eq(&first, &second)); + assert_eq!(pool.used(), baseline + metadata + FrameBuf::ADMISSION_HEAP as u64); + drop(first); + drop(second); + let resumed = a.admit(&budget).unwrap(); + assert_eq!( + pool.used(), + baseline + metadata + FrameBuf::ADMISSION_HEAP as u64, + "replacing an expired admission keeps one weak allocation" + ); + a.append(b"abcdefgh").unwrap(); + assert_eq!( + a.content_size(), + (FrameBuf::CONTENT_HEAP + FrameBuf::ADMISSION_HEAP + 8) as u64 + ); + assert_eq!(a.view_size(), FrameBuf::VIEW_HEAP as u64); + assert_eq!(copy.view_size(), 0, "a fork starts without a delivery charge"); + drop(resumed); + drop(a); + drop(copy); + assert_eq!(pool.used(), baseline); + + let payload = Bytes::from_static(b"complete"); + let complete = FrameBuf::from_payload(payload.clone().into()); + complete.bill_content(charge.extra()); + complete.bill_view(charge.extra()); + assert!(complete.admit(&budget).is_none()); + assert_eq!(complete.content_size(), (FrameBuf::CONTENT_HEAP + payload.len()) as u64); + assert_eq!(pool.used(), baseline + metadata + payload.len() as u64); + assert_eq!(complete.freeze().contiguous(0).as_ptr(), payload.as_ptr()); + drop(complete); + assert_eq!(pool.used(), baseline); + } + /// Each reallocation leaves the slices already handed out intact, and the last /// segment is exactly the frame, so the payload freezes without another copy. #[test] fn growing_buffer_keeps_handed_out_slices() { let buf = FrameBuf::growing(10); - buf.append(b"abc"); + buf.append(b"abc").unwrap(); assert_eq!(buf.allocated(), 3); let early = buf.slice(0, 3); - buf.append(b"defg"); + buf.append(b"defg").unwrap(); assert_eq!(buf.allocated(), 7); - buf.append(b"hij"); + buf.append(b"hij").unwrap(); assert_eq!(buf.allocated(), 10, "doubling stops at the declared size"); assert_eq!(early, &b"abc"[..]); @@ -1315,7 +1783,7 @@ mod tests { let payload: Vec = (0..SIZE).map(|i| (i % 251) as u8).collect(); for chunk in payload.chunks(1000) { - buf.append(chunk); + buf.append(chunk).unwrap(); } reader.join().unwrap(); assert_eq!(buf.freeze().contiguous(0), payload); @@ -1335,7 +1803,7 @@ mod tests { let buf = FrameBuf::with_growth(SIZE, grow); let mut read = 0; for chunk in payload.chunks(CHUNK) { - buf.append(chunk); + buf.append(chunk).unwrap(); let written = buf.written(Ordering::Acquire); let ahead = written.next_multiple_of(SEGMENT).min(SIZE); assert!( diff --git a/rs/moq-net/src/model/group.rs b/rs/moq-net/src/model/group.rs index 2e11bfbdf8c..24a866abcf7 100644 --- a/rs/moq-net/src/model/group.rs +++ b/rs/moq-net/src/model/group.rs @@ -15,10 +15,13 @@ //! //! The stream is closed with [Error] when all writers or readers are dropped. use super::flate; +mod content; use crate::cache; use crate::frame::{self, Cached, FrameBuf, Payload}; use crate::{Timescale, stats, track}; use bytes::Bytes; +pub(super) use content::Contents; +use content::{Content, Input, Row, Spec}; use std::collections::VecDeque; use std::mem::MaybeUninit; use std::ops::RangeBounds; @@ -119,6 +122,7 @@ impl From for Info { /// The in-flight (tail) frame being written. At most one exists at a time, since a /// group is a single ordered stream. pub(super) struct Partial { + owner: Option>, timestamp: Option, fields: Option>, buf: FrameBuf, @@ -134,6 +138,7 @@ pub(super) struct Partial { /// A completed frame as a group stores it, with what egress needs beside it. #[derive(Clone)] pub(super) struct Entry { + owner: Option>, frame: Cached, // The IETF object fields it arrived with, for a relay to forward. fields: Option>, @@ -145,6 +150,7 @@ impl Entry { /// A frame without object fields. fn plain(frame: Cached, inflated: u64) -> Self { Self { + owner: None, frame, fields: None, inflated, @@ -178,6 +184,7 @@ pub(crate) struct Bounds { /// while streaming a partial frame. #[derive(Default)] pub(super) struct GroupState { + content: Option>, // Completed frames, each a contiguous payload, from where this copy started (see // [`Producer::start_at`]). Its end is one past the last frame fully written. Cursors // read it without this state's lock. A compressed group stores DEFLATE slices here. @@ -244,6 +251,7 @@ pub(super) struct GroupState { /// The frames a copy of a group holds, from where it starts, which a successor copy of /// the same group on another route adopts below where it starts. pub(super) struct Head { + content: Option>, start: usize, // Whether `start` is the group's first object. first_object: bool, @@ -304,7 +312,7 @@ impl Head { .filter_map(|entry| entry.fields.as_ref()) .map(|fields| fields.heap()) .sum(); - payload + fields + (self.entries.len() * size_of::()) as u64 + self.content.as_ref().map_or(payload, |_| 0) + fields + (self.entries.len() * size_of::()) as u64 } } @@ -332,15 +340,11 @@ struct Flate { } impl Flate { - fn new(bill: cache::Extra) -> Self { + fn new(plain: Arc) -> Self { Self { inflated: 0, encoder: None, - plain: Arc::new(Plain { - len: AtomicUsize::new(0), - bill, - state: Mutex::new(PlainState::default()), - }), + plain, } } @@ -356,10 +360,13 @@ impl Flate { /// Drop the codec state once no frame follows at `fin`: the encoder, and the decoder if /// its readers already inflated every frame. - fn finish(&mut self, fin: usize) { + fn finish(&mut self, fin: Option) { if self.encoder.take().is_some() { self.plain.bill.sub(flate::ENCODER_HEAP); } + let Some(fin) = fin else { + return; + }; let mut plain = self.plain.lock(); plain.fin = Some(fin); if plain.frames.len() == fin { @@ -368,9 +375,16 @@ impl Flate { } /// Drop everything but how the group is read, failing a late inflater with `err`. - fn release(&mut self, err: Error) { - self.encoder = None; - self.plain.release(err); + fn release(&mut self, err: Error, shared: bool) { + if !shared { + self.encoder = None; + self.plain.release(err); + return; + } + if self.encoder.take().is_some() { + self.plain.bill.sub(flate::ENCODER_HEAP); + } + self.plain = Arc::new(Plain::released(err)); } } @@ -405,6 +419,20 @@ struct PlainState { } impl Plain { + fn new(bill: cache::Extra) -> Self { + Self { + len: AtomicUsize::new(0), + bill, + state: Mutex::default(), + } + } + + fn released(err: Error) -> Self { + let plain = Self::new(cache::Extra::default()); + plain.release(err); + plain + } + fn lock(&self) -> MutexGuard<'_, PlainState> { self.state.lock().unwrap_or_else(PoisonError::into_inner) } @@ -845,6 +873,30 @@ impl GroupState { self.relay.as_mut()?.wire.as_mut() } + fn canonicalize(&self, index: usize, entry: &mut Entry) -> Result<()> { + let Some(content) = &self.content else { + return Ok(()); + }; + let key = Content::key(index, entry.fields.as_deref(), self.wire().is_some()); + let input = match entry.owner.as_ref().and_then(Weak::upgrade) { + Some(row) => Input::Adopt(row), + None => Input::Whole(entry.frame.payload.clone()), + }; + let row = content.row( + Spec { + key, + size: entry.frame.payload.len() as u64, + inflated: self.flate.as_ref().map(|_| entry.inflated), + }, + input, + &self.charge, + None, + )?; + entry.frame.payload = row.buf.freeze(); + entry.owner = Some(Arc::downgrade(&row)); + Ok(()) + } + /// Whether Object IDs left the frame ordinals, so ordinal recovery cannot stitch. pub(super) fn sparse(&self) -> bool { self.wire().is_some_and(|wire| wire.sparse) @@ -877,7 +929,8 @@ impl GroupState { Some(_) => self.reserve_frame(), None => 0, }; - self.charge.add(written + slots) + self.charge + .add(slots + if self.content.is_none() { written } else { 0 }) } /// Whether the frames held start at the group's first object. @@ -892,6 +945,7 @@ impl GroupState { return None; } Some(Head { + content: self.content.clone(), start: self.offset, first_object: self.first_object(), entries: self.entries(self.offset, self.committed()).cloned().collect(), @@ -915,10 +969,14 @@ impl GroupState { self.front = VecDeque::new(); self.log.evict(self.log.end()); self.partial = None; + let shared = self.content.is_some(); + if let Some(content) = self.content.take() { + content.detach(self.abort.clone().unwrap_or(Error::Dropped)); + } self.cache = 0; self.charge.clear(); if let Some(flate) = &mut self.flate { - flate.release(self.abort.clone().unwrap_or(Error::Dropped)); + flate.release(self.abort.clone().unwrap_or(Error::Dropped), shared); } // The header state and terminal object outlive the frames. if let Some(relay) = &mut self.relay { @@ -941,6 +999,14 @@ impl GroupState { } } +impl Drop for GroupState { + fn drop(&mut self) { + if let Some(content) = self.content.take() { + content.detach(Error::Dropped); + } + } +} + /// `timestamp` at the track's `timescale`. /// /// A track is all timed or all untimed, so a frame must match it: a timed frame on an @@ -1144,7 +1210,7 @@ impl Producer { let log = { let mut state = state.write().ok().expect("a new group is open"); if track.compress { - state.flate = Some(Box::new(Flate::new(charge.extra()))); + state.flate = Some(Box::new(Flate::new(Arc::new(Plain::new(charge.extra()))))); } state.charge = charge; state.log.reader() @@ -1289,21 +1355,21 @@ impl Producer { if state.would_overflow(1, size) || state.inflated_overflow(inflated) { return Err(self.abort_too_large(state)); } - state.cache += size; - let inflated = match &mut state.flate { - Some(flate) => { - flate.inflated += inflated; - inflated - } - None => 0, - }; + let inflated = if state.flate.is_some() { inflated } else { 0 }; let frame = Cached { timestamp, payload: payload.into(), arrived: super::clock::now(), }; - let slots = state.push(Entry::plain(frame, inflated)); - let now = state.charge.add(size + slots); + let mut entry = Entry::plain(frame, inflated); + state.canonicalize(state.next_index, &mut entry)?; + state.cache += size; + if let Some(flate) = &mut state.flate { + flate.inflated += inflated; + } + let slots = state.push(entry); + let charged = slots + if state.content.is_none() { size } else { 0 }; + let now = state.charge.add(charged); state.next_index = next_index; state.stamp(timestamp); let bound = state.settle(origin); @@ -1389,27 +1455,41 @@ impl Producer { let mut now = None; let mut latest = None; let mut deflated = deflated.into_iter(); - // The whole batch lands at once. let arrived = super::clock::now(); - for mut frame in frames.drain() { - frame.arrived = arrived; - frame.timestamp = + let mut prepared = arrayvec::ArrayVec::::new(); + for (index, frame) in frames.filled().iter().enumerate() { + let timestamp = on_track(frame.timestamp, self.handle.track.timescale).expect("timestamp scale checked above"); - let inflated = match &mut state.flate { - Some(flate) => { - let inflated = frame.payload.len() as u64; - flate.inflated += inflated; - frame.payload = deflated.next().expect("one slice per frame"); - inflated - } - None => 0, + let (payload, inflated) = match state.flate.is_some() { + true => ( + deflated.next().expect("one slice per frame"), + frame.payload.len() as u64, + ), + false => (frame.payload.clone(), 0), }; - let size = frame.payload.len() as u64; + let mut entry = Entry::plain( + Cached { + timestamp, + payload: payload.into(), + arrived, + }, + inflated, + ); + state.canonicalize(state.next_index + index, &mut entry)?; + prepared.push(entry); + } + frames.drain().for_each(drop); + for entry in prepared { + let size = entry.frame.payload.len() as u64; + if let Some(flate) = &mut state.flate { + flate.inflated += entry.inflated; + } state.cache += size; - state.stamp(frame.timestamp); - latest = latest.max(frame.timestamp); - let slots = state.push(Entry::plain(frame.into(), inflated)); - now = state.charge.add(size + slots); + state.stamp(entry.frame.timestamp); + latest = latest.max(entry.frame.timestamp); + let slots = state.push(entry); + let charged = slots + if state.content.is_none() { size } else { 0 }; + now = state.charge.add(charged); } state.next_index = next_index; let bound = state.settle(origin); @@ -1459,20 +1539,14 @@ impl Producer { #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] pub(crate) fn create_object(mut self, frame: frame::Header) -> Result { let meter = self.chunk_meter(frame.inflated); - let size = frame.info.size as usize; - // Only the segment being written is ever allocated ahead of its bytes. - let reserved = self.handle.cache.frames().reserve(size.min(frame::SEGMENT)); - let buf = match reserved { - Some(_) => FrameBuf::new(size), - None => FrameBuf::growing(size), - }; - let info = match self.open_frame(frame, &buf) { - Ok(info) => info, + let (info, buf) = match self.open_frame(frame) { + Ok(opened) => opened, Err(err) => { let _ = self.handle.abort(err.clone()); return Err(err); } }; + let reserved = buf.admit(self.handle.cache.frames()); Ok(frame::Producer::new(self, buf, info, reserved).with_meter(meter)) } @@ -1490,7 +1564,7 @@ impl Producer { /// /// Its bytes are charged to the cache as they are written, not here: the declared /// size is only a promise until they arrive. - fn open_frame(&mut self, header: frame::Header, buf: &FrameBuf) -> Result { + fn open_frame(&mut self, header: frame::Header) -> Result<(frame::Info, FrameBuf)> { let frame = header.info; let timestamp = on_track(frame.timestamp, self.handle.track.timescale)?; if frame.size > MAX_CACHE_BYTES || header.inflated.is_some_and(|inflated| inflated > MAX_CACHE_BYTES) { @@ -1529,8 +1603,28 @@ impl Producer { if let (Some(flate), Some(inflated)) = (&mut state.flate, header.inflated) { flate.inflated += inflated; } + let (owner, buf) = match state.content.as_ref().filter(|_| !scratch) { + Some(content) => { + let key = Content::key(state.next_index, header.fields.as_deref(), state.wire().is_some()); + let row = content.row( + Spec { + key, + size: frame.size, + inflated: header.inflated, + }, + Input::Stream(None), + &state.charge, + None, + )?; + let buf = row.buf.fork(); + buf.bill_view(state.charge.extra()); + (Some(row), buf) + } + None => (None, FrameBuf::new(frame.size as usize)), + }; let now = state.charge.record_write(); state.partial = Some(Partial { + owner, timestamp, fields: header.fields, buf: buf.clone(), @@ -1577,10 +1671,13 @@ impl Producer { // producer writes them. self.stats.frames(1); - Ok(frame::Info { - size: frame.size, - timestamp, - }) + Ok(( + frame::Info { + size: frame.size, + timestamp, + }, + buf, + )) } /// Wake consumers parked on the group channel (called after a partial write). @@ -1625,7 +1722,8 @@ impl Producer { return Err(self.abort_too_large(state)); } state.cache += size; - now = state.charge.add(size); + let charged = if state.content.is_none() { size } else { 0 }; + now = state.charge.add(charged); self.stats.bytes(size); } else if let Some(size) = partial.as_ref().and_then(|partial| partial.inflated) { // Admitted against the budget when it opened. @@ -1633,11 +1731,16 @@ impl Producer { } // Stamped once readable, so a slow DEFLATE above counts against its lateness. frame.arrived = super::clock::now(); - let entry = Entry { + let (owner, fields) = partial.map_or((None, None), |partial| (partial.owner, partial.fields)); + let mut entry = Entry { + owner: owner.as_ref().map(Arc::downgrade), frame, - fields: partial.and_then(|partial| partial.fields), + fields, inflated, }; + if entry.owner.is_none() { + state.canonicalize(state.next_index - 1, &mut entry)?; + } // The slot was reserved and charged by `charge_partial` above. state.push(entry); debug_assert_eq!( @@ -1678,8 +1781,9 @@ impl Producer { let fin = state.next_index; state.fin = Some(fin); // No frame follows to compress. + let original_fin = state.range_end.is_none().then_some(fin); if let Some(flate) = &mut state.flate { - flate.finish(fin); + flate.finish(original_fin); } drop(state); // An empty group counts as content once it ends. @@ -1775,6 +1879,91 @@ impl Handle { } } + pub(super) fn content(&self) -> Option> { + self.state.read().content.clone() + } + + pub(super) fn activate_content(&self) -> Result>> { + let Ok(mut state) = self.state.write() else { + return Ok(None); + }; + if state.abort.is_some() { + return Ok(None); + } + if let Some(content) = &state.content { + return Ok(Some(content.clone())); + } + let content = Content::new(&state.charge, state.flate.as_ref().map(|flate| flate.plain.clone())); + let entries: Vec<_> = state.entries(state.offset, state.committed()).cloned().collect(); + for (index, entry) in (state.offset..).zip(entries) { + let key = Content::key(index, entry.fields.as_deref(), state.wire().is_some()); + let transferred = state.charge.transfer(entry.frame.payload.len() as u64); + content.row( + Spec { + key, + size: entry.frame.payload.len() as u64, + inflated: state.flate.as_ref().map(|_| entry.inflated), + }, + Input::Whole(entry.frame.payload), + &state.charge, + Some(transferred), + )?; + } + if state.partial.as_ref().is_some_and(|partial| !partial.scratch) { + let partial = state.partial.as_ref().expect("checked above"); + let key = Content::key(state.next_index - 1, partial.fields.as_deref(), state.wire().is_some()); + let (buf, inflated, charged) = (partial.buf.clone(), partial.inflated, partial.charged); + let transferred = state.charge.transfer(charged); + let row = content.row( + Spec { + key, + size: buf.size() as u64, + inflated, + }, + Input::Stream(Some(buf)), + &state.charge, + Some(transferred), + )?; + state.partial.as_mut().expect("still open").owner = Some(row); + } + content.attach(&state.charge); + state.content = Some(content.clone()); + Ok(Some(content)) + } + + pub(super) fn register_content(&self, contents: &Contents) { + let state = self.state.read(); + if let Some(content) = &state.content { + contents.insert(self.sequence, content, &state.charge); + } + } + + pub(super) fn share_content(&self, content: Arc) { + let Ok(mut state) = self.state.write() else { + return; + }; + debug_assert!(state.partial.is_none() && state.next_index == state.offset); + let plain = content.attach(&state.charge); + if let Some(old) = state.content.replace(content) { + old.detach(Error::Dropped); + } + if let (Some(flate), Some(plain)) = (&mut state.flate, plain) { + flate.plain = plain; + } + } + + pub(super) fn same_content(&self, other: &Self) -> bool { + match (self.content(), other.content()) { + (Some(a), Some(b)) => Arc::ptr_eq(&a, &b), + _ => false, + } + } + + pub(super) fn delivery_size(&self) -> u64 { + let state = self.state.read(); + state.charge.size() + state.partial.as_ref().map_or(0, |partial| partial.buf.view_size()) + } + /// Adopt the frames of `head` below where this copy starts, so it holds the group /// from `head`'s start, as if this route had delivered them. /// @@ -1814,8 +2003,16 @@ impl Handle { let before = state.front.capacity(); state.front.reserve(count); let growth = ((state.front.capacity() - before) * size_of::()) as u64; - for entry in entries.iter().rev() { - state.front.push_front(entry.clone()); + let mut adopted = Vec::with_capacity(count); + for (index, entry) in (head.start..).zip(entries) { + let mut entry = entry.clone(); + if state.canonicalize(index, &mut entry).is_err() { + return; + } + adopted.push(entry); + } + for entry in adopted.into_iter().rev() { + state.front.push_front(entry); } if metadata > 0 { state.relay().bytes += metadata; @@ -1842,7 +2039,8 @@ impl Handle { wire.first_id = head.start as u64; wire.last = wire.last.or(Some(offset as u64 - 1)); } - let now = state.charge.add(payload + metadata + growth); + let extra = metadata + growth + if state.content.is_none() { payload } else { 0 }; + let now = state.charge.add(extra); drop(state); self.start(None); self.cache.settle(now); @@ -2102,7 +2300,11 @@ impl Handle { /// as an eviction victim. pub(super) fn cache_size(&self) -> u64 { let state = self.state.read(); - state.charge.size() + state.flate.as_ref().map_or(0, |flate| flate.plain.bill.size()) + state.charge.size() + + state.content.as_ref().map_or_else( + || state.flate.as_ref().map_or(0, |flate| flate.plain.bill.size()), + |content| content.size(), + ) + state.partial.as_ref().map_or(0, |partial| partial.buf.view_size()) } /// Whether the payload cached after the first frame exceeds `ratio` times the first @@ -3494,6 +3696,7 @@ mod test { #[test] fn adopted_heads_keep_the_highest_pts_beside_the_tail() { let head = Head { + content: None, start: 0, first_object: true, entries: [0, 80, 40] @@ -4074,6 +4277,7 @@ mod test { }) .unwrap(); let head = Head { + content: None, start: 0, first_object: true, entries: vec![Entry::plain(Frame::new(None, Bytes::from_static(b"ab")).into(), 0)], @@ -4099,11 +4303,13 @@ mod test { })) }; let head = Head { + content: None, start: 0, first_object: true, entries: vec![ Entry::plain(Frame::new(None, Bytes::from_static(b"a")).into(), 0), Entry { + owner: None, frame: Frame::new(None, Bytes::from_static(b"b")).into(), fields: fields(1), inflated: 0, diff --git a/rs/moq-net/src/model/group/content.rs b/rs/moq-net/src/model/group/content.rs new file mode 100644 index 00000000000..a938ea56a38 --- /dev/null +++ b/rs/moq-net/src/model/group/content.rs @@ -0,0 +1,266 @@ +//! One immutable content owner, independent of each transport delivery's progress. +use super::*; + +/// Wire Object IDs are not frame ordinals on a sparse IETF stream. +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +pub(super) enum Key { + Frame(u64), + Object(u64), +} + +pub(super) enum Input { + Whole(Payload), + Stream(Option), + Adopt(Arc), +} + +pub(super) struct Spec { + pub key: Key, + pub size: u64, + pub inflated: Option, +} + +pub(super) struct Row { + key: Key, + inflated: Option, + pub(super) buf: FrameBuf, + bill: cache::Extra, +} + +pub(in crate::model) struct Content { + state: Mutex, + bill: cache::Extra, + plain: Mutex>>, + views: AtomicUsize, + index: Mutex, u64)>>, +} + +#[derive(Default)] +struct Stored { + rows: Vec>, + declared: u64, + inflated: u64, +} + +impl Content { + pub(super) fn new(charge: &cache::Charge, plain: Option>) -> Arc { + let bill = charge.extra(); + bill.add((2 * size_of::() + size_of::()) as u64); + Arc::new(Self { + state: Mutex::default(), + bill, + plain: Mutex::new(plain), + views: AtomicUsize::new(0), + index: Mutex::default(), + }) + } + + pub(super) fn key(index: usize, fields: Option<&super::super::object::Fields>, wire: bool) -> Key { + if wire { + Key::Object(fields.map_or(index as u64, |fields| fields.id)) + } else { + Key::Frame(index as u64) + } + } + + pub(super) fn row( + &self, + spec: Spec, + input: Input, + charge: &cache::Charge, + transferred: Option, + ) -> Result> { + let Spec { key, size, inflated } = spec; + let mut state = self.state.lock().unwrap_or_else(PoisonError::into_inner); + let at = match state.rows.binary_search_by_key(&key, |row| row.key) { + Ok(at) => { + let row = &state.rows[at]; + if row.buf.size() as u64 != size || row.inflated != inflated { + return Err(Error::ProtocolViolation); + } + let payload = match input { + Input::Whole(payload) => Some(payload), + Input::Adopt(row) => Some(row.buf.freeze()), + Input::Stream(_) => None, + }; + if let Some(payload) = payload { + let delivery = row.buf.fork(); + let mut at = 0; + while at < payload.len() { + let chunk = payload.chunk_at(at); + delivery.append(&chunk)?; + at += chunk.len(); + } + } + return Ok(row.clone()); + } + Err(at) => at, + }; + if size > MAX_CACHE_BYTES || inflated.is_some_and(|size| size > MAX_CACHE_BYTES) { + return Err(Error::FrameTooLarge); + } + if state.rows.len() == MAX_GROUP_FRAMES + || state.declared.saturating_add(size) > MAX_CACHE_BYTES + || state.inflated.saturating_add(inflated.unwrap_or(0)) > MAX_CACHE_BYTES + { + return Err(Error::GroupTooLarge); + } + let row = match input { + Input::Adopt(row) if row.key == key && row.inflated == inflated => row, + input => { + let buf = match input { + Input::Whole(payload) => FrameBuf::from_payload(payload), + Input::Stream(Some(buf)) => buf, + Input::Stream(None) => FrameBuf::new(size as usize), + Input::Adopt(row) => FrameBuf::from_payload(row.buf.freeze()), + }; + buf.bill_content(transferred.unwrap_or_else(|| charge.extra())); + buf.bill_view(charge.extra()); + let bill = charge.extra(); + bill.add((2 * size_of::() + size_of::()) as u64); + Arc::new(Row { + key, + inflated, + buf, + bill, + }) + } + }; + let before = state.rows.capacity(); + state.rows.insert(at, row.clone()); + self.bill + .add(((state.rows.capacity() - before) * size_of::>()) as u64); + state.declared += size; + state.inflated += inflated.unwrap_or(0); + Ok(row) + } + + pub(super) fn attach(&self, charge: &cache::Charge) -> Option> { + let mut plain = self.plain.lock().unwrap_or_else(PoisonError::into_inner); + self.views.fetch_add(1, Ordering::Relaxed); + if plain.as_ref().is_some_and(|plain| plain.lock().released.is_some()) { + *plain = Some(Arc::new(Plain::new(charge.extra()))); + } + plain.clone() + } + + pub(super) fn detach(&self, err: Error) { + let plain = self.plain.lock().unwrap_or_else(PoisonError::into_inner); + if self.views.fetch_sub(1, Ordering::AcqRel) == 1 + && let Some(plain) = plain.as_ref() + { + plain.release(err); + } + } + + #[cfg(test)] + pub(in crate::model) fn cost(&self) -> (usize, usize, u64) { + let state = self.state.lock().unwrap_or_else(PoisonError::into_inner); + let rows = state.rows.len(); + let bytes = state + .rows + .iter() + .map(|row| row.buf.shared_written(Ordering::Acquire)) + .sum(); + drop(state); + (rows, bytes, self.size()) + } + + pub(super) fn size(&self) -> u64 { + self.bill.size() + + self + .plain + .lock() + .unwrap_or_else(PoisonError::into_inner) + .as_ref() + .map_or(0, |plain| plain.bill.size()) + + self + .state + .lock() + .unwrap_or_else(PoisonError::into_inner) + .rows + .iter() + .map(|row| row.bill.size() + row.buf.content_size() + row.buf.view_size()) + .sum::() + } +} + +impl Drop for Content { + fn drop(&mut self) { + if let Some(plain) = self.plain.get_mut().unwrap_or_else(PoisonError::into_inner) + && plain.lock().released.is_none() + { + plain.release(Error::Dropped); + } + if let Some((index, sequence)) = self.index.get_mut().unwrap_or_else(PoisonError::into_inner).take() { + let mut state = index.state.lock().unwrap_or_else(PoisonError::into_inner); + if let Ok(at) = state.binary_search_by_key(&sequence, |(sequence, _)| *sequence) + && std::ptr::eq(state[at].1.as_ptr(), self) + { + state.remove(at); + } + } + } +} + +/// Only live content owners occupy the index; a dead owner removes its weak entry. +/// A new publisher incarnation replaces the entry instead of inheriting old bytes. +#[derive(Default)] +pub(in crate::model) struct Contents(Mutex>); + +#[derive(Default)] +struct Index { + state: Mutex)>>, + bill: OnceLock, +} + +impl Contents { + pub(in crate::model) fn get(&self, sequence: u64) -> Option> { + let index = self.0.lock().unwrap_or_else(PoisonError::into_inner).upgrade()?; + let state = index.state.lock().unwrap_or_else(PoisonError::into_inner); + state + .binary_search_by_key(&sequence, |(sequence, _)| *sequence) + .ok() + .and_then(|at| state[at].1.upgrade()) + } + + pub(in crate::model) fn remove(&self, sequence: u64) { + let Some(index) = self.0.lock().unwrap_or_else(PoisonError::into_inner).upgrade() else { + return; + }; + let mut state = index.state.lock().unwrap_or_else(PoisonError::into_inner); + if let Ok(at) = state.binary_search_by_key(&sequence, |(sequence, _)| *sequence) { + state.remove(at); + } + } + + pub(in crate::model) fn insert(&self, sequence: u64, content: &Arc, charge: &cache::Charge) { + let index = { + let mut current = self.0.lock().unwrap_or_else(PoisonError::into_inner); + match current.upgrade() { + Some(index) => index, + None => { + let index = Arc::new(Index::default()); + *current = Arc::downgrade(&index); + index + } + } + }; + let mut state = index.state.lock().unwrap_or_else(PoisonError::into_inner); + let bill = index.bill.get_or_init(|| { + let bill = charge.extra(); + bill.add((2 * size_of::() + size_of::()) as u64); + bill + }); + match state.binary_search_by_key(&sequence, |(sequence, _)| *sequence) { + Ok(at) => state[at].1 = Arc::downgrade(content), + Err(at) => { + let before = state.capacity(); + state.insert(at, (sequence, Arc::downgrade(content))); + bill.add(((state.capacity() - before) * size_of::<(u64, Weak)>()) as u64); + } + } + drop(state); + *content.index.lock().unwrap_or_else(PoisonError::into_inner) = Some((index, sequence)); + } +} diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 942ad139c9b..36fb3a8f4c4 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -325,6 +325,7 @@ pub(crate) struct TrackState { // Ordered so an in-range subscriber can seek to its cursor instead of scanning // every cached group for each delivery. lookup: BTreeMap, + contents: group::Contents, // Publisher-produced groups in arrival order as (sequence, stamp), walked by // subscriptions; an entry only resolves while its stamp matches the slot's. @@ -617,7 +618,15 @@ impl Slot { /// The bytes every copy holds, all freed together. fn cache_size(&self) -> u64 { - self.copies().map(group::Handle::cache_size).sum() + let mut size = self.group.cache_size(); + if let Some(head) = &self.head { + size += if head.same_content(&self.group) { + head.delivery_size() + } else { + head.cache_size() + }; + } + size } /// Whether every copy still held sat idle past the scan's expiry window. Asks @@ -1885,6 +1894,10 @@ impl TrackState { fn insert_group(&mut self, group: &group::Handle, visible: bool) { self.latch_settled(); let sequence = group.sequence; + if visible { + self.contents.remove(sequence); + } + group.register_content(&self.contents); self.next_stamp = self.next_stamp.wrapping_add(1); let stamp = self.next_stamp; @@ -2616,22 +2629,29 @@ impl TrackState { } let info = self.info.clone().unwrap(); - // A live group the feed started past `frame_start` keeps its slot: subscribers - // walk it in arrival order and the feed keeps writing it. The fetch fills its - // head instead. Anything else that can't answer from `frame_start` (evicted, - // aborted, or narrower backfill) is replaced; a group that can is a duplicate. - if self.lookup.get(&sequence).is_some_and(Slot::is_live) { - if self.covering_range(sequence, frames).is_some() { - return Err(Error::Duplicate); - } - } else { - if self.covering_range(sequence, frames).is_some() { - return Err(Error::Duplicate); + if self.covering_range(sequence, frames).is_some() { + return Err(Error::Duplicate); + } + let mut content = self.contents.get(sequence); + if content.is_none() + && let Some(slot) = self.lookup.get(&sequence) + { + for copy in slot.copies() { + if let Some(owner) = copy.activate_content()? { + content = Some(owner); + break; + } } + } + if !self.lookup.get(&sequence).is_some_and(Slot::is_live) { self.uncache(sequence); } let mut group = group::Producer::new(group::Info { sequence }, info, self.cache.clone()); + if let Some(content) = content { + group.handle().share_content(content); + } + group.handle().register_content(&self.contents); // Start where the request did before the group is visible: a fetch looking it up // in between would otherwise see it begin at 0 and get a reader that later skips // the head it asked for. @@ -15223,7 +15243,7 @@ mod test { assert_eq!((fill.frame_start(), fill.frame_end()), (0, None)); let mut complete = fill.accept(Info::timed()).unwrap(); for _ in 0..6 { - complete.write_frame(Timestamp::ZERO, b"whole".as_slice()).unwrap(); + complete.write_frame(Timestamp::ZERO, b"prefix".as_slice()).unwrap(); } complete.finish().unwrap(); let mut whole = whole.await.unwrap(); @@ -15512,8 +15532,13 @@ mod test { .unwrap() .accept(Info::timed()) .unwrap(); - for _ in 0..3 { - head.write_frame(Timestamp::ZERO, b"x".as_slice()).unwrap(); + for index in 0..3 { + let payload = if index == 0 { + b"x".as_slice() + } else { + b"delta".as_slice() + }; + head.write_frame(Timestamp::ZERO, payload).unwrap(); } fetch.await.unwrap(); @@ -19679,4 +19704,258 @@ mod test { .unwrap(); assert_eq!(drain(&mut subscriber), Vec::::new()); } + /// Distinct transport starts retain delivery outcomes, but share the same indexed + /// bytes even while every earlier delivery is held by an unread consumer. + #[moq_clock::test] + #[cfg(feature = "async")] + async fn distinct_tail_readers_share_canonical_content() { + for size in [0, 1, 1024] { + for bounded in [false, true] { + let producer = track_producer("test", Info::timed()); + let pool = producer.broadcast.pool.clone(); + let dynamic = producer.dynamic(); + let consumer = producer.consume(); + let mut live = producer.create_group(group::Info { sequence: 0 }).unwrap(); + live.start_at(32).unwrap(); + live.write_frame(Timestamp::ZERO, vec![32; size]).unwrap(); + assert!( + live.handle().content().is_none(), + "a single copy keeps its fast representation" + ); + let mut held = Vec::new(); + let mut deliveries = vec![live.handle()]; + let end = if bounded { 17 } else { 33 }; + for start in (1..=8).rev() { + let pending = consumer.fetch_range( + 0, + FetchOptions { + options: group::Fetch { + frame_start: start, + ..Default::default() + }, + end: bounded.then_some(end), + }, + ); + let fill = dynamic.requested_fill_group().await.unwrap(); + let mut tail = fill.accept(Info::timed()).unwrap(); + for frame in start..end { + tail.write_frame(Timestamp::ZERO, vec![frame as u8; size]).unwrap(); + } + tail.finish().unwrap(); + deliveries.push(tail.handle()); + held.push(pending.await.unwrap()); + } + let owner = deliveries[0].content().unwrap(); + for delivery in &deliveries { + assert!(Arc::ptr_eq(&owner, &delivery.content().unwrap())); + } + let (rows, bytes, shared_charge) = owner.cost(); + assert_eq!(rows, if bounded { 17 } else { 32 }); + assert_eq!(bytes, rows * size); + let total = pool.used(); + let delivery_charge: u64 = deliveries.iter().map(group::Handle::delivery_size).sum(); + eprintln!( + "CANONICAL_TAIL_COST size={size} bounded={bounded} delivery_handles={} canonical_rows={rows} unique_payload={bytes} shared_charge={shared_charge} delivery_charge={delivery_charge} discovery_charge={} total={total}", + deliveries.len(), + total - shared_charge - delivery_charge + ); + let mut pointers = BTreeMap::new(); + for (copy, reader) in held.iter_mut().enumerate() { + for index in (8 - copy) as u64..end { + assert_eq!(reader.index(), index); + let frame = reader.read_frame().await.unwrap().unwrap(); + assert_eq!(frame.payload.as_ref(), vec![index as u8; size]); + if size != 0 + && let Some(prior) = pointers.insert(index, frame.payload.as_ptr() as usize) + { + assert_eq!( + prior, + frame.payload.as_ptr() as usize, + "one physical payload for every matching frame" + ); + } + } + if bounded { + assert!(reader.read_frame().await.unwrap().is_none()); + } + } + drop((pointers, owner, deliveries)); + assert_eq!( + pool.used(), + total, + "identity probes neither own nor conceal payload copies" + ); + drop(held); + live.abort(Error::Cancel).unwrap(); + } + } + } + + /// Evicting a Slot does not hide a canonical owner from still-held older tails; + /// explicitly recreating the live group, however, starts a new incarnation. + #[moq_clock::test] + #[cfg(feature = "async")] + async fn tail_owner_survives_slot_removal_but_not_live_recreation() { + let producer = track_producer("test", Info::timed()); + let mut live = producer.create_group(group::Info { sequence: 0 }).unwrap(); + live.start_at(8).unwrap(); + live.write_frame(Timestamp::ZERO, b"eight".as_slice()).unwrap(); + let mut tails = Vec::new(); + let mut held = Vec::new(); + for start in [4, 2] { + let mut tail = producer + .state + .write() + .ok() + .unwrap() + .insert_group_request( + 0, + group::Bounds { + start, + end: Some(start + 1), + }, + None, + ) + .unwrap(); + tail.write_frame(Timestamp::ZERO, vec![start as u8; 4]).unwrap(); + tail.finish().unwrap(); + held.push(tail.consume().with_range(start..)); + tails.push(tail); + } + let owner = tails[0].handle().content().unwrap(); + producer + .state + .write() + .ok() + .unwrap() + .uncache(0) + .unwrap() + .abort(Error::Evicted); + assert!( + !tails[0].handle().is_aborted(), + "the replaced delivery is held outside the Slot" + ); + let mut refill = producer + .state + .write() + .ok() + .unwrap() + .insert_group_request(0, group::Bounds { start: 0, end: Some(1) }, None) + .unwrap(); + assert!(Arc::ptr_eq(&owner, &refill.handle().content().unwrap())); + refill.write_frame(Timestamp::ZERO, b"zero".as_slice()).unwrap(); + refill.finish().unwrap(); + refill.abort(Error::Cancel).unwrap(); + let mut recreated = producer.create_group(group::Info { sequence: 0 }).unwrap(); + recreated.start_at(8).unwrap(); + recreated.write_frame(Timestamp::ZERO, b"new eight".as_slice()).unwrap(); + assert!( + recreated.handle().content().is_none(), + "an explicit new live incarnation starts independently" + ); + let mut new = producer + .state + .write() + .ok() + .unwrap() + .insert_group_request(0, group::Bounds { start: 0, end: Some(1) }, None) + .unwrap(); + new.write_frame(Timestamp::ZERO, b"new zero".as_slice()).unwrap(); + assert!(!Arc::ptr_eq(&owner, &new.handle().content().unwrap())); + assert_eq!(held[0].read_frame().await.unwrap().unwrap().payload.as_ref(), &[4; 4]); + } + + /// Activation preserves an already-streaming reader and producer, and every + /// delivery's progress, arrival and abort while sharing the origin's admission. + #[moq_clock::test] + #[cfg(feature = "async")] + async fn canonical_activation_preserves_inflight_delivery_ownership() { + for abort_first in [false, true] { + let budget = frame::Budget::new(1024); + let broadcast = Arc::new(broadcast::Info { + frames: budget.clone(), + ..Default::default() + }); + let producer = Producer::new(broadcast, "test", Info::timed()); + let mut first = producer.create_group(group::Info { sequence: 0 }).unwrap(); + first.start_at(4).unwrap(); + let mut first_read = first.consume().with_range(4..); + let mut first = first + .create_frame(frame::Info { + size: 1024, + timestamp: Some(Timestamp::ZERO), + }) + .unwrap(); + first.write(vec![4; 512]).unwrap(); + let mut streaming = first_read.next_frame().await.unwrap().unwrap(); + let early = streaming.read_chunk().await.unwrap().unwrap(); + assert_eq!(early.as_ref(), &[4; 512]); + let mut second = producer + .state + .write() + .ok() + .unwrap() + .insert_group_request(0, group::Bounds { start: 3, end: Some(5) }, None) + .unwrap(); + second.write_frame(Timestamp::ZERO, b"three".as_slice()).unwrap(); + let mut second_read = second.consume().with_range(4..); + let mut second = second + .create_frame(frame::Info { + size: 1024, + timestamp: Some(Timestamp::ZERO), + }) + .unwrap(); + assert!( + budget.reserve(1).is_none(), + "overlap shares the first producer's reservation" + ); + assert_eq!(first.remaining(), 512); + second.write(vec![4; 256]).unwrap(); + assert_eq!(first.remaining(), 512, "another delivery credits no progress"); + assert!(streaming.read_chunk().now_or_never().is_none()); + moq_clock::sim::advance(Duration::from_millis(20)).await; + if abort_first { + first.abort(Error::Cancel).unwrap(); + assert!(budget.reserve(1).is_none(), "the survivor still holds admission"); + second.write(vec![4; 768]).unwrap(); + let mut second = second.finish().unwrap(); + second.finish().unwrap(); + assert!(matches!(streaming.read_chunk().await, Err(Error::Cancel))); + let frame = second_read.read_frame().await.unwrap().unwrap(); + assert_eq!(frame.payload.as_ref(), &[4; 1024]); + assert_eq!( + early.as_ptr(), + frame.payload.as_ptr(), + "the survivor reuses the published storage" + ); + } else { + second.write(vec![4; 768]).unwrap(); + let mut second = second.finish().unwrap(); + second.finish().unwrap(); + let later_copy = second_read.read_frame().await.unwrap().unwrap(); + assert!( + budget.reserve(1).is_none(), + "the slower first delivery still owns admission" + ); + assert_eq!(first.remaining(), 512); + assert!(streaming.read_chunk().now_or_never().is_none()); + moq_clock::sim::advance(Duration::from_millis(10)).await; + first.write(vec![4; 512]).unwrap(); + let mut first = first.finish().unwrap(); + first.finish().unwrap(); + let original_copy = first.consume().with_range(4..).read_frame().await.unwrap().unwrap(); + assert_eq!( + original_copy.arrived().duration_since(later_copy.arrived()), + Duration::from_millis(10) + ); + assert_eq!(original_copy.payload.as_ptr(), later_copy.payload.as_ptr()); + assert_eq!(early.as_ptr(), later_copy.payload.as_ptr()); + assert_eq!(streaming.read_chunk().await.unwrap().unwrap().as_ref(), &[4; 512]); + } + assert!( + budget.reserve(1024).is_some(), + "all completed producers return admission" + ); + } + } } From 1bef49679ecb5c920fe130908922d89b9cf3c438 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 27 Oct 2026 05:48:12 -0700 Subject: [PATCH 04/16] Preserve canonical storage and inflater delivery lifetimes Keep indexed identity alive through retained frame storage, preserve existing per-delivery limits, and gate shared inflation with each delivery release. Record deterministic negative and positive controls and remaining direct Lite ingress work. Co-Authored-By: GPT-6 --- quest/m1/transport-frame-offsets.md | 75 ++++++ rs/moq-net/src/lite/publisher.rs | 4 +- rs/moq-net/src/model/frame.rs | 219 +++++++++++----- rs/moq-net/src/model/group.rs | 222 ++++++++++------ rs/moq-net/src/model/group/content.rs | 171 +++++++------ rs/moq-net/src/model/track.rs | 356 +++++++++++++++++++++++++- 6 files changed, 816 insertions(+), 231 deletions(-) diff --git a/quest/m1/transport-frame-offsets.md b/quest/m1/transport-frame-offsets.md index d250f00b3b6..6cbef73db34 100644 --- a/quest/m1/transport-frame-offsets.md +++ b/quest/m1/transport-frame-offsets.md @@ -239,3 +239,78 @@ claimed to share raw wire storage. Compressed transport fetches must continue widening to start 0/end None, and a plain reader without the prefix must still refuse. No public API, wire, resource-budget or maintainer-policy choice has been selected by this checkpoint. + +### Storage-lifetime preservation checkpoint + +The later implementation supersedes the 2420 prototype's ownership details. +Canonical frame storage now owns its group identity, while the group's index +holds weak frame records. Completed delivery entries retain shared frame owners; +their owner cell can be populated during activation without replacing payloads +that lock-free readers already see. A streamed reader keeps the same identity +through its existing buffer. Last storage release removes its weak record, so +neither retired entries nor frame readers can hide retained bytes from a refill. +The index retains no dead weak history and introduces no new union-wide limits. +Existing per-delivery limits remain enforced. + +A deterministic evicted-streaming-reader control first failed because its weak +group identity vanished while its frame buffer still existed. It now passes, +reusing the same physical payload on refill while the old reader still returns +`Evicted`. A separate control accepts an existing valid 8192-frame tail and a +disjoint valid one-frame prefix, preserving their existing per-delivery limits. + +An independent review found that a captured inflater job could outlive delivery +A's release when delivery B kept the shared Plain alive. The existing +`inflation_is_shared_and_stops_at_release` control was extended and reproduced +that failure. A compressed delivery now has its own release guard. Each inflation +frame and get synchronizes with that guard; releasing A waits for its current +frame and preserves A's error while B can still read and inflate. The guard's +metadata is charged to the active delivery. This changes neither the shared +inflation representation nor the existing caller ownership of completed bytes. + +Typed object controls preserve IDs across different delivery ordinals, sealed +timestamps and delivery-local terminal metadata. The compressed ingress control +uses independent received buffers, verifies raw payload pointer reuse without +recompression, preserves the missing-prefix refusal and different arrival times, +inflates plain bytes once, and keeps start-0/end-None widening for repeated tail +requests. Local plaintext compression scratch remains a separate producer path. + +The full strict frame/group/track cohort passes 512/512. `just fix` passes. +Source includes the actually landed request and Path prerequisites through a +normal merge of main `c764acebb90b5c102a8ea90207dd125ae4fd326b`; no rebase. +The subsequent IETF version-removal integration and final full gates remain. + +The updated x86_64 metadata sizes are content 216 B, progress view 56 B and +admission bookkeeping 32 B. Ownership charges from the retained-reader control: + +| Payload/frame | End | Canonical rows | Unique payload | Shared charge | Delivery charge | Discovery charge | Total | +| --- | --- | --- | --- | --- | --- | --- | --- | +| 0 B | Unbounded | 32 | 0 B | 8304 B | 36448 B | 136 B | 44888 B | +| 0 B | Exclusive 17 | 17 | 0 B | 5064 B | 23296 B | 136 B | 28496 B | +| 1 B | Unbounded | 32 | 32 B | 8336 B | 36448 B | 136 B | 44920 B | +| 1 B | Exclusive 17 | 17 | 17 B | 5081 B | 23296 B | 136 B | 28513 B | +| 1024 B | Unbounded | 32 | 32768 B | 41072 B | 36448 B | 136 B | 77656 B | +| 1024 B | Exclusive 17 | 17 | 17408 B | 22472 B | 23296 B | 136 B | 45904 B | + +Raw outputs and SHA256: + +- Full focused model cohort: `.tmp/weak-owner-model-tests.log`, + `65b5eba4bd4a0c6d95536806469c8ede169cb5f6112ae11a2d638e094590fbf4`. +- Frame-reader negative: `.tmp/frame-reader-owner-before-2.log`, + `41b552539fe590464c5040fbc93e8fc0597214cb73bfaa630fce1124b06c7005`. +- Frame-reader/union positive: `.tmp/weak-frame-owner-controls.log`, + `13639d690f523102d340f5f4106f2b8ed9eb9a9e76184452bb470006cf716f4c`. +- Shared late-inflater negative: `.tmp/shared-late-inflate-before.log`, + `ad2524de1e81d2a5e01ee02707585401ae15d428653f21a40392e7d010452c99`. +- Late-inflater/typed/compressed positives: + `.tmp/canonical-representation-controls.log`, + `44a2868fc82a01871af38327cc13697450988df42c9c379bbc946796bd80bec1`. + +The Goal is still unfinished. Read-only review found a direct live Lite GROUP +path that creates a public whole group before applying the received frame start, +then resets the owner when replacing an earlier positive-start copy. The next +control must exercise actual GroupRecv with held readers and distinct tail +starts, followed by a private transport claim that knows the offset before +publication. Public explicit group recreation must retain its separate owner. +Ordinary pre-existing cross-route duplication is not itself evidence of this +specific tail-amplification bug. Final readiness still needs this direct ingress +proof, current-main integration, clean committed-head gates and the real surface. diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 2a539470d45..10aef8881f9 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -8171,7 +8171,7 @@ mod tests { let origin = crate::origin::Config::new(Hop::new(1).unwrap()).produce(); let broadcast = origin.create_broadcast("room").unwrap(); let track = broadcast.create_track("video", track::Info::timed()).unwrap(); - let dynamic = track.dynamic(); + let dynamic = track.requests(); let peer_setup = crate::lite::PeerSetup::new(false, crate::transport::Address::Setup); peer_setup.set(crate::lite::Setup::default()); let (_, goaway) = crate::goaway::Handle::new(true); @@ -8193,7 +8193,7 @@ mod tests { &publisher.shared, lite::Fetch { epoch: None, - broadcast: crate::Path::new("room"), + broadcast: crate::Path::new("room").unwrap(), track: "video".into(), priority: 0, max_delay: None, diff --git a/rs/moq-net/src/model/frame.rs b/rs/moq-net/src/model/frame.rs index 728ac55db7a..42b3efe9895 100644 --- a/rs/moq-net/src/model/frame.rs +++ b/rs/moq-net/src/model/frame.rs @@ -461,6 +461,148 @@ struct FrameContent { frozen: OnceLock, publication: Mutex, bill: OnceLock, + identity: OnceLock, +} + +struct Identity { + owner: Arc, + key: group::ContentKey, +} + +impl Drop for FrameContent { + fn drop(&mut self) { + if let Some(identity) = self.identity.get() { + identity.owner.forget_frame(identity.key, self as *const Self as usize); + } + } +} + +#[derive(Clone)] +pub(super) struct Shared(Arc); + +#[derive(Clone)] +pub(super) struct SharedWeak(Weak); + +impl SharedWeak { + pub(super) fn upgrade(&self) -> Option { + self.0.upgrade().map(Shared) + } + pub(super) fn address(&self) -> usize { + self.0.as_ptr() as usize + } +} + +impl Shared { + pub(super) fn from_payload(payload: Payload) -> Self { + let size = payload.len(); + Self(Arc::new(FrameContent { + size, + grow: AtomicBool::new(false), + written: AtomicUsize::new(size), + storage: OnceLock::from(FrameStorage::Shared(payload.clone())), + frozen: OnceLock::from(payload), + publication: Mutex::default(), + bill: OnceLock::new(), + identity: OnceLock::new(), + })) + } + pub(super) fn downgrade(&self) -> SharedWeak { + SharedWeak(Arc::downgrade(&self.0)) + } + pub(super) fn fork(&self) -> FrameBuf { + FrameBuf(Arc::new(FrameBufInner { + content: self.0.clone(), + received: AtomicUsize::new(0), + bill: OnceLock::new(), + })) + } + pub(super) fn freeze(&self) -> Payload { + self.0.freeze() + } + pub(super) fn size(&self) -> usize { + self.0.size + } + #[cfg(test)] + pub(super) fn written(&self) -> usize { + self.0.written.load(Ordering::Acquire) + } + pub(super) fn bill_content(&self, bill: cache::Extra) { + self.0.bill_content(bill); + } + pub(super) fn content_size(&self) -> u64 { + self.0.bill.get().map_or(0, cache::Extra::size) + } + pub(super) fn identify(&self, owner: Arc, key: group::ContentKey) -> bool { + self.0.identity.set(Identity { owner, key }).is_ok() + } +} + +impl FrameContent { + fn bill_content(&self, bill: cache::Extra) { + let content = self; + let publication = content.publication.lock().unwrap_or_else(PoisonError::into_inner); + content.bill.get_or_init(|| { + let admission = if publication.admitted { + FrameBuf::ADMISSION_HEAP + } else { + 0 + }; + let written = self.written.load(Ordering::Relaxed) as u64; + // A lazy owner may take already-billed payload from the original delivery. + let fresh = written + .checked_sub(bill.size()) + .expect("transferred payload cannot exceed published bytes"); + bill.add(fresh + (FrameBuf::CONTENT_HEAP + admission) as u64); + bill + }); + } + + fn freeze(&self) -> Payload { + debug_assert_eq!(self.written.load(Ordering::Acquire), self.size); + self.frozen + .get_or_init(|| match self.storage.get() { + Some(FrameStorage::Shared(payload)) => payload.clone(), + Some(FrameStorage::Chain(segments)) => { + let segments = segments.lock().unwrap_or_else(PoisonError::into_inner); + debug_assert!(segments.open.is_none(), "froze a frame mid-segment"); + Payload::Chain(Chain { + segments: segments.sealed.as_slice().into(), + start: 0, + }) + } + _ => Payload::Whole(self.slice(0, self.size)), + }) + .clone() + } + + fn slice(&self, start: usize, end: usize) -> Bytes { + if start >= end { + return Bytes::new(); + } + let (segment, base) = match self.storage.get() { + Some(FrameStorage::Shared(payload)) => { + let chunk = payload.chunk_at(start); + return chunk.slice(..chunk.len().min(end - start)); + } + Some(FrameStorage::Fixed(segment)) => (segment.clone(), 0), + Some(FrameStorage::Growing(current)) => (current.lock().unwrap_or_else(PoisonError::into_inner).clone(), 0), + Some(FrameStorage::Chain(segments)) => { + let segments = segments.lock().unwrap_or_else(PoisonError::into_inner); + let index = start / SEGMENT; + let base = index * SEGMENT; + if let Some(sealed) = segments.sealed.get(index) { + return sealed.slice(start - base..end.min(base + SEGMENT) - base); + } + let open = segments.open.clone().expect("written bytes are sealed or open"); + (open, base) + } + // Nothing written, so the range is empty. + None => return Bytes::new(), + }; + let len = end.min(base + SEGMENT) - base; + debug_assert!(len <= segment.capacity); + Bytes::from_owner(Filled { segment, len }).slice(start - base..) + } } #[derive(Default)] @@ -624,6 +766,7 @@ impl FrameBuf { frozen: OnceLock::new(), publication: Mutex::default(), bill: OnceLock::new(), + identity: OnceLock::new(), }), received: AtomicUsize::new(0), bill: OnceLock::new(), @@ -631,24 +774,22 @@ impl FrameBuf { } /// Keep an already-complete payload without copying its buffers or segments. + #[cfg(test)] pub(super) fn from_payload(payload: Payload) -> Self { let size = payload.len(); Self(Arc::new(FrameBufInner { - content: Arc::new(FrameContent { - size, - grow: AtomicBool::new(false), - written: AtomicUsize::new(size), - storage: OnceLock::from(FrameStorage::Shared(payload.clone())), - frozen: OnceLock::from(payload), - publication: Mutex::default(), - bill: OnceLock::new(), - }), + content: Shared::from_payload(payload).0, received: AtomicUsize::new(size), bill: OnceLock::new(), })) } + pub(super) fn shared(&self) -> Shared { + Shared(self.0.content.clone()) + } + /// Another delivery of the same immutable frame, starting before its first byte. + #[cfg(test)] pub(super) fn fork(&self) -> Self { Self(Arc::new(FrameBufInner { content: self.0.content.clone(), @@ -659,19 +800,9 @@ impl FrameBuf { /// Bill physical content once, including bytes already received before attachment. /// The charge follows its buffers even if a cached row lets go before a stream does. + #[cfg(test)] pub(super) fn bill_content(&self, bill: cache::Extra) { - let content = &self.0.content; - let publication = content.publication.lock().unwrap_or_else(PoisonError::into_inner); - content.bill.get_or_init(|| { - let admission = if publication.admitted { Self::ADMISSION_HEAP } else { 0 }; - let written = self.shared_written(Ordering::Relaxed) as u64; - // A lazy owner may take already-billed payload from the original delivery. - let fresh = written - .checked_sub(bill.size()) - .expect("transferred payload cannot exceed published bytes"); - bill.add(fresh + (Self::CONTENT_HEAP + admission) as u64); - bill - }); + self.0.content.bill_content(bill); } /// One delivery's metadata, retained by its streaming readers and shared by clones. @@ -683,6 +814,7 @@ impl FrameBuf { } /// The canonical allocation's charge, counted once by its cache owner. + #[cfg(test)] pub(super) fn content_size(&self) -> u64 { self.0.content.bill.get().map_or(0, cache::Extra::size) } @@ -842,23 +974,7 @@ impl FrameBuf { /// Called only once the canonical payload is complete. Delivery completion is /// checked separately by [`Producer::finish`]. pub(super) fn freeze(&self) -> Payload { - debug_assert_eq!(self.shared_written(Ordering::Acquire), self.size()); - self.0 - .content - .frozen - .get_or_init(|| match self.0.content.storage.get() { - Some(FrameStorage::Shared(payload)) => payload.clone(), - Some(FrameStorage::Chain(segments)) => { - let segments = segments.lock().unwrap_or_else(PoisonError::into_inner); - debug_assert!(segments.open.is_none(), "froze a frame mid-segment"); - Payload::Chain(Chain { - segments: segments.sealed.as_slice().into(), - start: 0, - }) - } - _ => Payload::Whole(self.slice(0, self.size())), - }) - .clone() + self.0.content.freeze() } /// A zero-copy slice of the written region from `start` to `end`, or to the end of @@ -867,32 +983,7 @@ impl FrameBuf { /// `end` must not exceed a shared published value already loaded with `Acquire`. /// A delivery's own received count is always within that prefix too. fn slice(&self, start: usize, end: usize) -> Bytes { - if start >= end { - return Bytes::new(); - } - let (segment, base) = match self.0.content.storage.get() { - Some(FrameStorage::Shared(payload)) => { - let chunk = payload.chunk_at(start); - return chunk.slice(..chunk.len().min(end - start)); - } - Some(FrameStorage::Fixed(segment)) => (segment.clone(), 0), - Some(FrameStorage::Growing(current)) => (current.lock().unwrap_or_else(PoisonError::into_inner).clone(), 0), - Some(FrameStorage::Chain(segments)) => { - let segments = segments.lock().unwrap_or_else(PoisonError::into_inner); - let index = start / SEGMENT; - let base = index * SEGMENT; - if let Some(sealed) = segments.sealed.get(index) { - return sealed.slice(start - base..end.min(base + SEGMENT) - base); - } - let open = segments.open.clone().expect("written bytes are sealed or open"); - (open, base) - } - // Nothing written, so the range is empty. - None => return Bytes::new(), - }; - let len = end.min(base + SEGMENT) - base; - debug_assert!(len <= segment.capacity); - Bytes::from_owner(Filled { segment, len }).slice(start - base..) + self.0.content.slice(start, end) } /// Heap bytes the buffer currently holds for the payload. diff --git a/rs/moq-net/src/model/group.rs b/rs/moq-net/src/model/group.rs index 8f16dd71853..9165f32b909 100644 --- a/rs/moq-net/src/model/group.rs +++ b/rs/moq-net/src/model/group.rs @@ -20,8 +20,8 @@ use crate::cache; use crate::frame::{self, Cached, FrameBuf, Payload}; use crate::{Timescale, stats, track}; use bytes::Bytes; -pub(super) use content::Contents; -use content::{Content, Input, Row, Spec}; +pub(super) use content::{Content, Contents, Key as ContentKey}; +use content::{Input, Spec}; use std::collections::VecDeque; use std::mem::MaybeUninit; use std::ops::RangeBounds; @@ -31,16 +31,16 @@ use std::task::{Poll, ready}; use crate::{Error, IntoBytes, Result, StreamError, Timestamp}; -/// Maximum total size of frames in a group. +/// Maximum total size of frames held by one cached group copy. /// /// A write that would exceed this aborts the group with [`Error::GroupTooLarge`]. /// Doubles as the per-frame size cap: a larger declared size is [`Error::FrameTooLarge`] /// before allocating, so one maximum-size frame can fill a group. pub const MAX_CACHE_BYTES: u64 = 32 * 1024 * 1024; // 32 MB -/// Maximum number of frames in a group. +/// Maximum number of frames held by one cached group copy. /// -/// 8192 is the largest legal group; the 8193rd write returns [`Error::GroupTooLarge`] +/// 8192 is the largest legal cached copy; the 8193rd write returns [`Error::GroupTooLarge`] /// and aborts the group. pub const MAX_GROUP_FRAMES: usize = 8192; @@ -122,7 +122,6 @@ impl From for Info { /// The in-flight (tail) frame being written. At most one exists at a time, since a /// group is a single ordered stream. pub(super) struct Partial { - owner: Option>, timestamp: Option, fields: Option>, buf: FrameBuf, @@ -136,9 +135,8 @@ pub(super) struct Partial { } /// A completed frame as a group stores it, with what egress needs beside it. -#[derive(Clone)] pub(super) struct Entry { - owner: Option>, + owner: Mutex>, frame: Cached, // The IETF object fields it arrived with, for a relay to forward. fields: Option>, @@ -146,11 +144,22 @@ pub(super) struct Entry { inflated: u64, } +impl Clone for Entry { + fn clone(&self) -> Self { + Self { + owner: Mutex::new(self.owner.lock().unwrap_or_else(PoisonError::into_inner).clone()), + frame: self.frame.clone(), + fields: self.fields.clone(), + inflated: self.inflated, + } + } +} + impl Entry { /// A frame without object fields. fn plain(frame: Cached, inflated: u64) -> Self { Self { - owner: None, + owner: Mutex::new(None), frame, fields: None, inflated, @@ -329,6 +338,8 @@ struct Relay { /// What a compressed group keeps beside its stored DEFLATE slices. struct Flate { + // A pending inflater belongs to this delivery even when its Plain is shared. + release: Arc>>, // The inflated size of every frame admitted so far, held to the budget a plain // group's bytes are, so a declaration is refused before anything inflates. inflated: u64, @@ -340,8 +351,11 @@ struct Flate { } impl Flate { + const RELEASE_HEAP: u64 = (2 * size_of::() + size_of::>>()) as u64; + fn new(plain: Arc) -> Self { Self { + release: Arc::default(), inflated: 0, encoder: None, plain, @@ -376,6 +390,8 @@ impl Flate { /// Drop everything but how the group is read, failing a late inflater with `err`. fn release(&mut self, err: Error, shared: bool) { + // Wait for at most the frame currently inflating, just as Plain::release did. + *self.release.lock().unwrap_or_else(PoisonError::into_inner) = Some(err.clone()); if !shared { self.encoder = None; self.plain.release(err); @@ -384,7 +400,6 @@ impl Flate { if self.encoder.take().is_some() { self.plain.bill.sub(flate::ENCODER_HEAP); } - self.plain = Arc::new(Plain::released(err)); } } @@ -427,12 +442,6 @@ impl Plain { } } - fn released(err: Error) -> Self { - let plain = Self::new(cache::Extra::default()); - plain.release(err); - plain - } - fn lock(&self) -> MutexGuard<'_, PlainState> { self.state.lock().unwrap_or_else(PoisonError::into_inner) } @@ -476,6 +485,7 @@ impl Plain { /// The stored slices a plain read of a compressed group inflates once the group's guard /// is released. struct Inflate { + release: Arc>>, plain: Arc, // Local index of the first slice: the copy's inflated length when the read took them. from: usize, @@ -491,6 +501,10 @@ impl Inflate { /// lock held, since settling the debt takes the track's. fn run(&self, meter: &stats::Meter) { for (index, (slice, size)) in (self.from..).zip(&self.slices) { + let released = self.release.lock().unwrap_or_else(PoisonError::into_inner); + if released.is_some() { + break; + } let mut state = self.plain.lock(); if state.released.is_some() || state.err.is_some() { break; @@ -532,6 +546,10 @@ impl Inflate { /// Inflated local frame `local`, or why it is not. fn get(&self, local: usize) -> Result { + let released = self.release.lock().unwrap_or_else(PoisonError::into_inner); + if let Some(err) = released.as_ref() { + return Err(err.clone()); + } self.plain.get(local) } } @@ -719,6 +737,7 @@ impl GroupState { } let from = flate.plain.len.load(Ordering::Acquire).min(last + 1); Ok(Inflate { + release: flate.release.clone(), plain: flate.plain.clone(), from, slices: self @@ -878,7 +897,7 @@ impl GroupState { return Ok(()); }; let key = Content::key(index, entry.fields.as_deref(), self.wire().is_some()); - let input = match entry.owner.as_ref().and_then(Weak::upgrade) { + let input = match entry.owner.lock().unwrap_or_else(PoisonError::into_inner).clone() { Some(row) => Input::Adopt(row), None => Input::Whole(entry.frame.payload.clone()), }; @@ -892,8 +911,8 @@ impl GroupState { &self.charge, None, )?; - entry.frame.payload = row.buf.freeze(); - entry.owner = Some(Arc::downgrade(&row)); + entry.frame.payload = row.freeze(); + *entry.owner.lock().unwrap_or_else(PoisonError::into_inner) = Some(row); Ok(()) } @@ -966,18 +985,18 @@ impl GroupState { false => None, }; let head = handoff.as_ref().and_then(|_| self.head()); + let shared = self.content.is_some(); + if let Some(flate) = &mut self.flate { + flate.release(self.abort.clone().unwrap_or(Error::Dropped), shared); + } self.front = VecDeque::new(); self.log.evict(self.log.end()); self.partial = None; - let shared = self.content.is_some(); if let Some(content) = self.content.take() { content.detach(self.abort.clone().unwrap_or(Error::Dropped)); } self.cache = 0; self.charge.clear(); - if let Some(flate) = &mut self.flate { - flate.release(self.abort.clone().unwrap_or(Error::Dropped), shared); - } // The header state and terminal object outlive the frames. if let Some(relay) = &mut self.relay { relay.bytes = size_of::() as u64 + relay.end_object.as_ref().map_or(0, |fields| fields.heap()); @@ -1205,11 +1224,12 @@ impl Producer { /// track evicts toward under memory pressure. pub(super) fn new(info: Info, track: track::Info, cache: Arc) -> Self { let state = kio::Producer::::default(); - let charge = cache.charge(); + let mut charge = cache.charge(); let access = charge.access(); let log = { let mut state = state.write().ok().expect("a new group is open"); if track.compress { + charge.add(Flate::RELEASE_HEAP); state.flate = Some(Box::new(Flate::new(Arc::new(Plain::new(charge.extra()))))); } state.charge = charge; @@ -1603,7 +1623,7 @@ impl Producer { if let (Some(flate), Some(inflated)) = (&mut state.flate, header.inflated) { flate.inflated += inflated; } - let (owner, buf) = match state.content.as_ref().filter(|_| !scratch) { + let buf = match state.content.as_ref().filter(|_| !scratch) { Some(content) => { let key = Content::key(state.next_index, header.fields.as_deref(), state.wire().is_some()); let row = content.row( @@ -1616,15 +1636,14 @@ impl Producer { &state.charge, None, )?; - let buf = row.buf.fork(); + let buf = row.fork(); buf.bill_view(state.charge.extra()); - (Some(row), buf) + buf } - None => (None, FrameBuf::new(frame.size as usize)), + None => FrameBuf::new(frame.size as usize), }; let now = state.charge.record_write(); state.partial = Some(Partial { - owner, timestamp, fields: header.fields, buf: buf.clone(), @@ -1731,14 +1750,18 @@ impl Producer { } // Stamped once readable, so a slow DEFLATE above counts against its lateness. frame.arrived = super::clock::now(); - let (owner, fields) = partial.map_or((None, None), |partial| (partial.owner, partial.fields)); + let owner = partial + .as_ref() + .filter(|partial| !partial.scratch && state.content.is_some()) + .map(|partial| partial.buf.shared()); + let fields = partial.and_then(|partial| partial.fields); let mut entry = Entry { - owner: owner.as_ref().map(Arc::downgrade), + owner: Mutex::new(owner), frame, fields, inflated, }; - if entry.owner.is_none() { + if entry.owner.lock().unwrap_or_else(PoisonError::into_inner).is_none() { state.canonicalize(state.next_index - 1, &mut entry)?; } // The slot was reserved and charged by `charge_partial` above. @@ -1894,40 +1917,53 @@ impl Handle { return Ok(Some(content.clone())); } let content = Content::new(&state.charge, state.flate.as_ref().map(|flate| flate.plain.clone())); - let entries: Vec<_> = state.entries(state.offset, state.committed()).cloned().collect(); - for (index, entry) in (state.offset..).zip(entries) { - let key = Content::key(index, entry.fields.as_deref(), state.wire().is_some()); - let transferred = state.charge.transfer(entry.frame.payload.len() as u64); - content.row( - Spec { - key, - size: entry.frame.payload.len() as u64, - inflated: state.flate.as_ref().map(|_| entry.inflated), - }, - Input::Whole(entry.frame.payload), - &state.charge, - Some(transferred), - )?; - } - if state.partial.as_ref().is_some_and(|partial| !partial.scratch) { - let partial = state.partial.as_ref().expect("checked above"); - let key = Content::key(state.next_index - 1, partial.fields.as_deref(), state.wire().is_some()); - let (buf, inflated, charged) = (partial.buf.clone(), partial.inflated, partial.charged); - let transferred = state.charge.transfer(charged); - let row = content.row( - Spec { - key, - size: buf.size() as u64, - inflated, - }, - Input::Stream(Some(buf)), - &state.charge, - Some(transferred), - )?; - state.partial.as_mut().expect("still open").owner = Some(row); - } content.attach(&state.charge); state.content = Some(content.clone()); + let loaded = (|| -> Result<()> { + let entries: Vec<_> = state.entries(state.offset, state.committed()).cloned().collect(); + for (index, entry) in (state.offset..).zip(entries) { + let key = Content::key(index, entry.fields.as_deref(), state.wire().is_some()); + let transferred = state.charge.transfer(entry.frame.payload.len() as u64); + let shared = content.row( + Spec { + key, + size: entry.frame.payload.len() as u64, + inflated: state.flate.as_ref().map(|_| entry.inflated), + }, + Input::Whole(entry.frame.payload), + &state.charge, + Some(transferred), + )?; + *state + .entry(index) + .expect("entry remains under the group guard") + .owner + .lock() + .unwrap_or_else(PoisonError::into_inner) = Some(shared); + } + if state.partial.as_ref().is_some_and(|partial| !partial.scratch) { + let partial = state.partial.as_ref().expect("checked above"); + let key = Content::key(state.next_index - 1, partial.fields.as_deref(), state.wire().is_some()); + let (buf, inflated, charged) = (partial.buf.clone(), partial.inflated, partial.charged); + let transferred = state.charge.transfer(charged); + buf.bill_view(state.charge.extra()); + content.row( + Spec { + key, + size: buf.size() as u64, + inflated, + }, + Input::Stream(Some(buf)), + &state.charge, + Some(transferred), + )?; + } + Ok(()) + })(); + if let Err(err) = loaded { + self.commit_abort(state, err.clone()); + return Err(err); + } Ok(Some(content)) } @@ -1995,14 +2031,6 @@ impl Handle { if state.would_overflow(count, payload + metadata + state.unwritten()) || state.inflated_overflow(inflated) { return; } - if let Some(flate) = &mut state.flate { - flate.inflated += inflated; - } - - // Below the log's first frame, so these never enter it: readers find them here. - let before = state.front.capacity(); - state.front.reserve(count); - let growth = ((state.front.capacity() - before) * size_of::()) as u64; let mut adopted = Vec::with_capacity(count); for (index, entry) in (head.start..).zip(entries) { let mut entry = entry.clone(); @@ -2011,6 +2039,15 @@ impl Handle { } adopted.push(entry); } + if let Some(flate) = &mut state.flate { + flate.inflated += inflated; + } + + // Below the log's first frame, so these never enter it: readers find them here. + let before = state.front.capacity(); + state.front.reserve(count); + let growth = ((state.front.capacity() - before) * size_of::()) as u64; + for entry in adopted.into_iter().rev() { state.front.push_front(entry); } @@ -3632,9 +3669,8 @@ pub struct Fetch { /// delivered. An application always fetches from a group's first frame, since a group is /// the unit of decodability, and skips frames locally with [`Consumer::with_range`]. /// - /// There is no matching end: stopping the *fetch* short would put a group in the cache - /// that is indistinguishable from a complete one, so a later fetch of the whole group - /// would resolve from it and come up short. + /// There is no public matching end. Private transport caps retain incomplete cache + /// provenance, so a bounded reply never answers a later whole-group fetch. pub(crate) frame_start: u64, } @@ -4309,7 +4345,7 @@ mod test { entries: vec![ Entry::plain(Frame::new(None, Bytes::from_static(b"a")).into(), 0), Entry { - owner: None, + owner: Mutex::new(None), frame: Frame::new(None, Bytes::from_static(b"b")).into(), fields: fields(1), inflated: 0, @@ -5529,6 +5565,40 @@ mod test { assert!(matches!(late.get(0), Err(Error::Evicted))); assert_eq!(pool.used(), used, "a late inflater bills nothing past the release"); assert_eq!(inflated(), plain); + // A job belongs to its delivery even when another copy keeps the canonical + // inflated cache alive. Releasing one must not make its pending read succeed. + let cache = cache::Track::new(pool.clone(), Default::default(), kio::Weak::new()); + let info = track::Info::default().with_compress(true); + let mut released = Producer::new(Info::from(0u64), info.clone(), cache.clone()); + released.bound(Bounds { start: 0, end: Some(2) }).unwrap(); + for index in 0..2 { + released.write_frame(None, verbose(index)).unwrap(); + } + released.finish().unwrap(); + let content = released.handle.activate_content().unwrap().unwrap(); + let mut survivor = Producer::new(Info::from(0u64), info, cache); + survivor.handle.share_content(content); + for index in 0..3 { + survivor.write_frame(None, verbose(index)).unwrap(); + } + survivor.finish().unwrap(); + let late = take_slices(&released, 1); + released.handle().abort(Error::Evicted).unwrap(); + let used = pool.used(); + late.run(&stats::Meter::default()); + assert!( + matches!(late.get(0), Err(Error::Evicted)), + "a released delivery cannot read through another copy's inflation" + ); + assert_eq!(pool.used(), used, "a released job creates no inflation charge"); + let mut reader = survivor.consume(); + for index in 0..3 { + assert_eq!(read_plain(&mut reader).unwrap().unwrap(), verbose(index)); + } + assert!( + matches!(late.get(0), Err(Error::Evicted)), + "the other copy's completion cannot revive this read" + ); } /// A compressed group meters `bytes` as stored, whether a frame is written whole, in a diff --git a/rs/moq-net/src/model/group/content.rs b/rs/moq-net/src/model/group/content.rs index a938ea56a38..0e081e3fe30 100644 --- a/rs/moq-net/src/model/group/content.rs +++ b/rs/moq-net/src/model/group/content.rs @@ -1,9 +1,9 @@ -//! One immutable content owner, independent of each transport delivery's progress. +//! Weak indexed identity; delivery records and streaming readers own the bytes. use super::*; -/// Wire Object IDs are not frame ordinals on a sparse IETF stream. +/// Object IDs are not frame ordinals on a sparse IETF stream. #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] -pub(super) enum Key { +pub(in crate::model) enum Key { Frame(u64), Object(u64), } @@ -11,7 +11,7 @@ pub(super) enum Key { pub(super) enum Input { Whole(Payload), Stream(Option), - Adopt(Arc), + Adopt(frame::Shared), } pub(super) struct Spec { @@ -20,28 +20,20 @@ pub(super) struct Spec { pub inflated: Option, } -pub(super) struct Row { +struct Row { key: Key, inflated: Option, - pub(super) buf: FrameBuf, - bill: cache::Extra, + content: frame::SharedWeak, } pub(in crate::model) struct Content { - state: Mutex, + state: Mutex>, bill: cache::Extra, plain: Mutex>>, views: AtomicUsize, index: Mutex, u64)>>, } -#[derive(Default)] -struct Stored { - rows: Vec>, - declared: u64, - inflated: u64, -} - impl Content { pub(super) fn new(charge: &cache::Charge, plain: Option>) -> Arc { let bill = charge.extra(); @@ -56,7 +48,7 @@ impl Content { } pub(super) fn key(index: usize, fields: Option<&super::super::object::Fields>, wire: bool) -> Key { - if wire { + if wire || fields.is_some() { Key::Object(fields.map_or(index as u64, |fields| fields.id)) } else { Key::Frame(index as u64) @@ -64,75 +56,87 @@ impl Content { } pub(super) fn row( - &self, + self: &Arc, spec: Spec, input: Input, charge: &cache::Charge, transferred: Option, - ) -> Result> { + ) -> Result { let Spec { key, size, inflated } = spec; let mut state = self.state.lock().unwrap_or_else(PoisonError::into_inner); - let at = match state.rows.binary_search_by_key(&key, |row| row.key) { + let at = match state.binary_search_by_key(&key, |row| row.key) { Ok(at) => { - let row = &state.rows[at]; - if row.buf.size() as u64 != size || row.inflated != inflated { - return Err(Error::ProtocolViolation); - } - let payload = match input { - Input::Whole(payload) => Some(payload), - Input::Adopt(row) => Some(row.buf.freeze()), - Input::Stream(_) => None, - }; - if let Some(payload) = payload { - let delivery = row.buf.fork(); - let mut at = 0; - while at < payload.len() { - let chunk = payload.chunk_at(at); - delivery.append(&chunk)?; - at += chunk.len(); + if let Some(shared) = state[at].content.upgrade() { + let matches = shared.size() as u64 == size && state[at].inflated == inflated; + // A temporary strong owner must never drop under this mutex: its last + // release removes its weak record through `forget_frame`. + drop(state); + if !matches { + return Err(Error::ProtocolViolation); + } + let payload = match input { + Input::Whole(payload) => Some(payload), + Input::Adopt(shared) => Some(shared.freeze()), + Input::Stream(_) => None, + }; + if let Some(payload) = payload { + let delivery = shared.fork(); + let mut at = 0; + while at < payload.len() { + let chunk = payload.chunk_at(at); + delivery.append(&chunk)?; + at += chunk.len(); + } } + return Ok(shared); } - return Ok(row.clone()); + state.remove(at); + at } Err(at) => at, }; + // Retained bytes/frame limits remain per delivery. Combining valid disjoint + // ranges must not introduce a stricter aggregate admission policy. if size > MAX_CACHE_BYTES || inflated.is_some_and(|size| size > MAX_CACHE_BYTES) { return Err(Error::FrameTooLarge); } - if state.rows.len() == MAX_GROUP_FRAMES - || state.declared.saturating_add(size) > MAX_CACHE_BYTES - || state.inflated.saturating_add(inflated.unwrap_or(0)) > MAX_CACHE_BYTES + let shared = match input { + Input::Whole(payload) => frame::Shared::from_payload(payload), + Input::Stream(Some(buf)) => buf.shared(), + Input::Stream(None) => FrameBuf::new(size as usize).shared(), + Input::Adopt(shared) => frame::Shared::from_payload(shared.freeze()), + }; + if !shared.identify(self.clone(), key) { + drop(state); + return Err(Error::ProtocolViolation); + } + shared.bill_content(transferred.unwrap_or_else(|| charge.extra())); + let before = state.capacity(); + state.insert( + at, + Row { + key, + inflated, + content: shared.downgrade(), + }, + ); + self.bill.add(((state.capacity() - before) * size_of::()) as u64); + drop(state); + Ok(shared) + } + + pub(in crate::model) fn forget_frame(&self, key: Key, address: usize) { + let mut state = self.state.lock().unwrap_or_else(PoisonError::into_inner); + if let Ok(at) = state.binary_search_by_key(&key, |row| row.key) + && state[at].content.address() == address { - return Err(Error::GroupTooLarge); + state.remove(at); + } + if state.is_empty() { + let bytes = state.capacity() * size_of::(); + *state = Vec::new(); + self.bill.sub(bytes as u64); } - let row = match input { - Input::Adopt(row) if row.key == key && row.inflated == inflated => row, - input => { - let buf = match input { - Input::Whole(payload) => FrameBuf::from_payload(payload), - Input::Stream(Some(buf)) => buf, - Input::Stream(None) => FrameBuf::new(size as usize), - Input::Adopt(row) => FrameBuf::from_payload(row.buf.freeze()), - }; - buf.bill_content(transferred.unwrap_or_else(|| charge.extra())); - buf.bill_view(charge.extra()); - let bill = charge.extra(); - bill.add((2 * size_of::() + size_of::()) as u64); - Arc::new(Row { - key, - inflated, - buf, - bill, - }) - } - }; - let before = state.rows.capacity(); - state.rows.insert(at, row.clone()); - self.bill - .add(((state.rows.capacity() - before) * size_of::>()) as u64); - state.declared += size; - state.inflated += inflated.unwrap_or(0); - Ok(row) } pub(super) fn attach(&self, charge: &cache::Charge) -> Option> { @@ -153,20 +157,24 @@ impl Content { } } + fn frames(&self) -> Vec { + self.state + .lock() + .unwrap_or_else(PoisonError::into_inner) + .iter() + .filter_map(|row| row.content.upgrade()) + .collect() + } + #[cfg(test)] pub(in crate::model) fn cost(&self) -> (usize, usize, u64) { - let state = self.state.lock().unwrap_or_else(PoisonError::into_inner); - let rows = state.rows.len(); - let bytes = state - .rows - .iter() - .map(|row| row.buf.shared_written(Ordering::Acquire)) - .sum(); - drop(state); - (rows, bytes, self.size()) + let frames = self.frames(); + let bytes = frames.iter().map(frame::Shared::written).sum(); + (frames.len(), bytes, self.size()) } pub(super) fn size(&self) -> u64 { + let frames = self.frames(); self.bill.size() + self .plain @@ -174,14 +182,7 @@ impl Content { .unwrap_or_else(PoisonError::into_inner) .as_ref() .map_or(0, |plain| plain.bill.size()) - + self - .state - .lock() - .unwrap_or_else(PoisonError::into_inner) - .rows - .iter() - .map(|row| row.bill.size() + row.buf.content_size() + row.buf.view_size()) - .sum::() + + frames.iter().map(frame::Shared::content_size).sum::() } } diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index fcb4acedfd9..6fe53d6c031 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -15201,7 +15201,7 @@ mod test { #[cfg(feature = "async")] async fn bounded_transport_copies_never_answer_whole_fetches() { let producer = track_producer("test", Info::timed()); - let dynamic = producer.dynamic(); + let dynamic = producer.requests(); let consumer = producer.consume(); let request = |start, end| FetchOptions { options: group::Fetch { @@ -15260,7 +15260,7 @@ mod test { #[cfg(feature = "async")] async fn bounded_tail_refills_coalesce_behind_a_live_copy() { let producer = track_producer("test", Info::timed()); - let dynamic = producer.dynamic(); + let dynamic = producer.requests(); let consumer = producer.consume(); let mut live = producer.create_group(group::Info { sequence: 0 }).unwrap(); live.start_at(200).unwrap(); @@ -15301,7 +15301,7 @@ mod test { #[cfg(feature = "async")] async fn bounded_fetches_widen_before_dispatch_and_split_after() { let producer = track_producer("test", Info::timed()); - let dynamic = producer.dynamic(); + let dynamic = producer.requests(); let consumer = producer.consume(); let request = |end| FetchOptions { options: group::Fetch::default(), @@ -19713,7 +19713,7 @@ mod test { for bounded in [false, true] { let producer = track_producer("test", Info::timed()); let pool = producer.broadcast.pool.clone(); - let dynamic = producer.dynamic(); + let dynamic = producer.requests(); let consumer = producer.consume(); let mut live = producer.create_group(group::Info { sequence: 0 }).unwrap(); live.start_at(32).unwrap(); @@ -19958,4 +19958,352 @@ mod test { ); } } + /// IETF Object IDs can occupy different delivery ordinals. Sharing bytes must + /// preserve their typed fields and each delivery's independent terminal record. + #[moq_clock::test] + #[cfg(feature = "async")] + async fn canonical_content_keys_typed_objects_by_id_not_ordinal() { + fn wire(first: u64) -> group::Wire { + group::Wire { + sparse: true, + first_object: false, + first_id: first, + extensions: true, + has_end: true, + fetchable: true, + last: None, + } + } + fn write(group: group::Producer, id: u64, payload: &'static [u8]) -> group::Producer { + let mut frame = group + .create_object(frame::Header { + info: frame::Info { + size: payload.len() as u64, + timestamp: Some(Timestamp::ZERO), + }, + fields: Some(Arc::new(super::super::object::Fields { + id, + properties: super::super::object::Properties { + sealed_time: Some(Timestamp::ZERO), + ..Default::default() + }, + })), + inflated: None, + }) + .unwrap(); + frame.write(payload).unwrap(); + frame.finish().unwrap() + } + let producer = track_producer("test", Info::timed()); + let mut live = producer.create_group(group::Info { sequence: 0 }).unwrap(); + live.start_at(2).unwrap(); + live.set_wire(wire(100)).unwrap(); + let live = write(live, 100, b"a"); + let head = producer + .state + .write() + .ok() + .unwrap() + .insert_group_request(0, group::Bounds { start: 1, end: Some(3) }, None) + .unwrap(); + head.set_wire(wire(90)).unwrap(); + let head = write(head, 90, b"b"); + let mut head = write(head, 100, b"a"); + head.set_end_object(super::super::object::Fields { + id: 101, + properties: Default::default(), + }) + .unwrap(); + head.finish().unwrap(); + let earlier = producer + .state + .write() + .ok() + .unwrap() + .insert_group_request(0, group::Bounds { start: 0, end: Some(2) }, None) + .unwrap(); + earlier.set_wire(wire(90)).unwrap(); + let earlier = write(earlier, 90, b"b"); + let mut earlier = write(earlier, 95, b"c"); + earlier.finish().unwrap(); + assert!(head.consume().end_object().is_some_and(|fields| fields.id == 101)); + assert!( + earlier.consume().end_object().is_none(), + "content sharing copies no terminal metadata" + ); + let mut prior = head.consume().with_range(1..).compressed(); + let mut later = earlier.consume().compressed(); + let mut a = arrayvec::ArrayVec::::new(); + let mut b = arrayvec::ArrayVec::::new(); + assert!(matches!( + prior.poll_read_objects(&kio::Waiter::noop(), &mut a), + Poll::Ready(Ok(2)) + )); + assert!(matches!( + later.poll_read_objects(&kio::Waiter::noop(), &mut b), + Poll::Ready(Ok(2)) + )); + assert_eq!( + a.iter() + .map(|object| object.fields.as_ref().unwrap().id) + .collect::>(), + [90, 100] + ); + assert_eq!( + b.iter() + .map(|object| object.fields.as_ref().unwrap().id) + .collect::>(), + [90, 95] + ); + assert_eq!( + a[0].payload.chunk_at(0).as_ptr(), + b[0].payload.chunk_at(0).as_ptr(), + "one Object ID shares bytes across different ordinals" + ); + assert_eq!( + b[1].payload.contiguous(0), + b"c".as_slice(), + "the same ordinal cannot alias a different Object ID" + ); + assert!( + a.iter() + .chain(&b) + .all(|object| object.fields.as_ref().unwrap().properties.sealed_time == Some(Timestamp::ZERO)) + ); + assert_eq!(live.handle().content().unwrap().cost().0, 3); + } + + /// Compressed transport refills keep the received slices, share one inflation, + /// and continue widening hostile tail requests rather than minting local encoders. + #[moq_clock::test] + #[cfg(feature = "async")] + async fn canonical_compressed_ingress_preserves_raw_plain_and_widening() { + fn ingest(group: group::Producer, object: &frame::Object) -> group::Producer { + assert!( + object.inflated.is_some(), + "compressed ingress carries the stored representation" + ); + let mut frame = group + .create_object(frame::Header { + info: frame::Info { + size: object.payload.len() as u64, + timestamp: object.timestamp, + }, + fields: object.fields.clone(), + inflated: object.inflated, + }) + .unwrap(); + let mut at = 0; + while at < object.payload.len() { + let chunk = object.payload.chunk_at(at); + at += chunk.len(); + frame.write(bytes::Bytes::copy_from_slice(&chunk)).unwrap(); + } + frame.finish().unwrap() + } + let info = Info::timed().with_compress(true); + let reference = track_producer("reference", info.clone()); + let mut source = reference.create_group(group::Info { sequence: 0 }).unwrap(); + let expected: Vec<_> = (0..9) + .map(|index| bytes::Bytes::from(format!("shared dictionary frame {index}; ").repeat(64))) + .collect(); + for payload in &expected { + source.write_frame(Timestamp::ZERO, payload.clone()).unwrap(); + } + source.finish().unwrap(); + let mut stored = arrayvec::ArrayVec::::new(); + assert!(matches!( + source + .consume() + .compressed() + .poll_read_objects(&kio::Waiter::noop(), &mut stored), + Poll::Ready(Ok(9)) + )); + let producer = track_producer("test", info.clone()); + let requests = producer.requests(); + let consumer = producer.consume(); + let mut live = producer.create_group(group::Info { sequence: 0 }).unwrap(); + live.start_at(8).unwrap(); + let live = ingest(live, &stored[8]); + let mut raw_live = live.consume().with_range(8..).compressed(); + let raw_live = raw_live.read_frame().await.unwrap().unwrap(); + assert!( + matches!(live.consume().with_range(8..).read_frame().await, Err(Error::Lagged)), + "canonical storage does not grant a missing compression prefix" + ); + let pending = consumer.fetch_range( + 0, + FetchOptions { + options: group::Fetch { + frame_start: 2, + ..Default::default() + }, + end: Some(4), + }, + ); + let fill = requests.requested_fill_group().await.unwrap(); + assert_eq!((fill.frame_start(), fill.frame_end()), (0, None)); + let mut head = fill.accept(info).unwrap(); + for object in &stored[..8] { + head = ingest(head, object); + } + let registry = stats::Registry::new(stats::Config::new()); + let meter = registry.publisher_meter(crate::Path::new("demo").unwrap(), &stats::Tier::default()); + let mut first = head.consume().with_meter(meter.clone()); + assert_eq!(first.read_frame().await.unwrap().unwrap().payload, expected[0]); + moq_clock::sim::advance(Duration::from_millis(20)).await; + head = ingest(head, &stored[8]); + head.finish().unwrap(); + pending.await.unwrap(); + let mut raw_head = head.consume().with_range(8..).compressed(); + let raw_head = raw_head.read_frame().await.unwrap().unwrap(); + assert_eq!( + raw_live.payload.as_ptr(), + raw_head.payload.as_ptr(), + "stored compressed bytes are retained once" + ); + assert_eq!( + raw_head.payload, + stored[8].payload.contiguous(0), + "the relay does not recompress" + ); + assert_eq!( + raw_head.arrived().duration_since(raw_live.arrived()), + Duration::from_millis(20) + ); + let mut second = head.consume().with_meter(meter); + for (index, payload) in expected.iter().enumerate() { + if index != 0 { + assert_eq!(first.read_frame().await.unwrap().unwrap().payload, *payload); + } + assert_eq!(second.read_frame().await.unwrap().unwrap().payload, *payload); + } + let inflated: u64 = registry + .snapshot() + .traffic() + .into_iter() + .map(|(_, _, traffic)| traffic.inflated) + .sum(); + assert_eq!( + inflated, + expected.iter().map(|payload| payload.len() as u64).sum::() + ); + for start in 1..9 { + consumer + .fetch_range( + 0, + FetchOptions { + options: group::Fetch { + frame_start: start, + ..Default::default() + }, + end: Some(start + 1), + }, + ) + .await + .unwrap(); + } + assert!( + requests.poll_requested_fill_group(&kio::Waiter::noop()).is_pending(), + "compressed tail starts coalesce through the established widening policy" + ); + assert_eq!(head.handle().content().unwrap().cost().0, 9); + } + + /// An evicted streaming reader still owns in-flight storage until it is dropped. + /// A refill must discover those same bytes without changing that reader's abort. + #[moq_clock::test] + #[cfg(feature = "async")] + async fn canonical_owner_follows_an_evicted_streaming_frame_reader() { + let producer = track_producer("test", Info::timed()); + let mut live = producer.create_group(group::Info { sequence: 0 }).unwrap(); + live.start_at(4).unwrap(); + let observing = live.handle(); + let mut reader = live.consume().with_range(4..); + let mut live = live + .create_frame(frame::Info { + size: 1024, + timestamp: Some(Timestamp::ZERO), + }) + .unwrap(); + live.write(vec![4; 512]).unwrap(); + let mut streaming = reader.next_frame().await.unwrap().unwrap(); + let first = streaming.read_chunk().await.unwrap().unwrap(); + let original = first.as_ptr() as usize; + drop(first); + let mut head = producer + .state + .write() + .ok() + .unwrap() + .insert_group_request(0, group::Bounds { start: 3, end: Some(4) }, None) + .unwrap(); + head.write_frame(Timestamp::ZERO, b"three".as_slice()).unwrap(); + head.finish().unwrap(); + let identity = Arc::downgrade(&observing.content().unwrap()); + producer + .state + .write() + .ok() + .unwrap() + .uncache(0) + .unwrap() + .abort(Error::Evicted); + drop(live); + assert!( + identity.upgrade().is_some(), + "the streaming frame still owns canonical storage" + ); + let refill = producer + .state + .write() + .ok() + .unwrap() + .insert_group_request(0, group::Bounds { start: 4, end: Some(5) }, None) + .unwrap(); + assert!(Arc::ptr_eq( + &identity.upgrade().unwrap(), + &refill.handle().content().unwrap() + )); + let mut refill = refill + .create_frame(frame::Info { + size: 1024, + timestamp: Some(Timestamp::ZERO), + }) + .unwrap(); + refill.write(vec![4; 1024]).unwrap(); + let mut refill = refill.finish().unwrap(); + refill.finish().unwrap(); + let complete = refill.consume().with_range(4..).read_frame().await.unwrap().unwrap(); + assert_eq!( + complete.payload.as_ptr() as usize, + original, + "refill reuses the reader-held content" + ); + assert_eq!(complete.payload.as_ref(), &[4; 1024]); + assert!(matches!(streaming.read_chunk().await, Err(Error::Evicted))); + } + + /// Existing limits apply to each delivery, not the union of disjoint valid ranges. + #[test] + fn canonical_content_preserves_individually_valid_disjoint_ranges() { + let producer = track_producer("test", Info::timed()); + let mut live = producer.create_group(group::Info { sequence: 0 }).unwrap(); + live.start_at(group::MAX_GROUP_FRAMES as u64).unwrap(); + for _ in 0..group::MAX_GROUP_FRAMES { + live.write_frame(Timestamp::ZERO, bytes::Bytes::new()).unwrap(); + } + let mut head = producer + .state + .write() + .ok() + .unwrap() + .insert_group_request(0, group::Bounds { start: 0, end: Some(1) }, None) + .unwrap(); + head.write_frame(Timestamp::ZERO, bytes::Bytes::new()).unwrap(); + head.finish().unwrap(); + assert_eq!(live.handle().content().unwrap().cost().0, group::MAX_GROUP_FRAMES + 1); + assert_eq!(live.frame_count(), 2 * group::MAX_GROUP_FRAMES); + assert_eq!(head.frame_count(), 1); + } } From 368a2f424c90eb5d0abafd4934fd880fa081c6ec Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 27 Oct 2026 06:42:42 -0700 Subject: [PATCH 05/16] Bound canonical ownership discovery and final cleanup Co-Authored-By: GPT-6 --- Cargo.lock | 1 + Cargo.toml | 1 + quest/m1/transport-frame-offsets.md | 113 ++++++++++++ rs/moq-net/Cargo.toml | 9 +- rs/moq-net/benches/canonical.rs | 27 +++ rs/moq-net/src/fuzz.rs | 12 ++ rs/moq-net/src/lite/subscriber.rs | 86 ++++++++- rs/moq-net/src/model/frame.rs | 44 ++++- rs/moq-net/src/model/group.rs | 90 ++++++++-- rs/moq-net/src/model/group/content.rs | 250 +++++++++++++++++++------- rs/moq-net/src/model/track.rs | 62 +++++-- 11 files changed, 576 insertions(+), 119 deletions(-) create mode 100644 rs/moq-net/benches/canonical.rs diff --git a/Cargo.lock b/Cargo.lock index fb6e1cde155..6c1b4e1e62d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5491,6 +5491,7 @@ dependencies = [ "flate2", "futures", "getrandom 0.4.3", + "hashbrown 0.17.1", "hashlink", "kio 0.6.2", "libc", diff --git a/Cargo.toml b/Cargo.toml index 4e6a36d37d5..c96c253118d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -113,6 +113,7 @@ futures = "0.3" getrandom = { version = "0.4", features = ["wasm_js"] } h2 = "0.4" hang = { version = "0.21.11", path = "rs/hang" } +hashbrown = { version = "0.17.1", default-features = false } hashlink = "0.12.2" hex = "0.4" # HMAC-SHA256 for the mDNS membership proofs (moq-tokio's `mdns` feature). diff --git a/quest/m1/transport-frame-offsets.md b/quest/m1/transport-frame-offsets.md index 6cbef73db34..1e9a704940f 100644 --- a/quest/m1/transport-frame-offsets.md +++ b/quest/m1/transport-frame-offsets.md @@ -314,3 +314,116 @@ publication. Public explicit group recreation must retain its separate owner. Ordinary pre-existing cross-route duplication is not itself evidence of this specific tail-amplification bug. Final readiness still needs this direct ingress proof, current-main integration, clean committed-head gates and the real surface. + + +### Live GROUP and intrinsic-ownership checkpoint + +The direct Lite ingress failure was reproduced through actual `GroupRecv` and +one subscriber entry: eight independent GROUP streams for the same sequence, +with starts 8 down to 1 and all earlier consumers held. Each stream received +fresh 1 KiB buffers. Before the correction, matching frame 8 had different +physical payload pointers. The private transport claim now receives the known +start before group publication, discovers retained canonical content before +replacing a Slot, and preserves public explicit recreation as a separate owner. +The same wire control now passes, including each consumer's bounds, payload and +FIN. No new route identity or Epoch-less cross-route sharing was introduced. + +The new inflater release guard also had a distinct accounting regression: a +captured job retained the guard after group release, but its charge was cleared +with the group. A deterministic negative control observed 0 B rather than the +live guard's 64 B. The guard now carries its own intrinsic `Extra`; releasing +the delivery preserves its error, and the final retained job/clone returns that +metadata charge. The earlier shared-Plain preservation control still passes. + +Both weak discovery indexes now use randomized `hashbrown` maps instead of +sorted vectors. The existing locked 0.17.1 release is a direct dependency with +default features disabled and `std::RandomState`; `allocation_size()` measures +the actual table allocation. Final owners remove their records immediately, +empty tables release their buckets, and replacing a sequence cannot let the old +owner remove the new incarnation. Frame-owned accounting events update a checked +aggregate, so group sizing neither allocates nor walks all retained rows. The +underlying intrinsic charges still end with the final physical owner. + +The pre-change benchmark used the actual canonical frame owners, 64 B frames, +1/64/1024/8192 frames and 1/8 held owner sets. Sizing runs on a constant fixture; +cleanup times actual final-owner destruction with setup outside the timed body. +It used 20 samples, 500 ms warm-up and 2 s measurement, on this 24-CPU x86_64 host +at 13:13:39–13:14:35 UTC; load averages were 0.64/2.05/3.47 before and +1.13/1.97/3.36 after. At 8192 frames, sizing took 69.856/69.493 µs and cleanup +12.734/13.046 ms for 1/8 owner sets. The largest cleanup case warned that the +fixed 20 samples needed more than the 2 s estimate; no sample count or case was +changed. Repeating the same cases after the source correction remains due. + +The strict frame/group/track/Lite cohort passes 606/606, including unordered +empty-frame final releases, replacement owner discovery, actual live GROUP, +sparse typed properties, raw/plain compression, independent errors/arrivals, +frame-reader lifetime and original versus bounded ends. This includes actual +main's IETF 14–17 removal through a normal merge of +`22808bbb2f4aab9fdaf9a60507f77efbaef4bd0d`. Full CI and interop on 5796 are +historical evidence only; this newer source still needs its final gates. + +Updated held-reader charges, with distinct per-delivery bookkeeping retained: + +| Payload/frame | End | Canonical rows | Unique payload | Shared charge | Delivery charge | Discovery charge | Total | +| --- | --- | --- | --- | --- | --- | --- | --- | +| 0 B | Unbounded | 32 | 0 B | 9696 B | 36448 B | 180 B | 46324 B | +| 0 B | Exclusive 17 | 17 | 0 B | 5144 B | 23296 B | 180 B | 28620 B | +| 1 B | Unbounded | 32 | 32 B | 9728 B | 36448 B | 180 B | 46356 B | +| 1 B | Exclusive 17 | 17 | 17 B | 5161 B | 23296 B | 180 B | 28637 B | +| 1024 B | Unbounded | 32 | 32768 B | 42464 B | 36448 B | 180 B | 79092 B | +| 1024 B | Exclusive 17 | 17 | 17408 B | 22552 B | 23296 B | 180 B | 46028 B | + +Raw evidence and SHA256: + +- Live GROUP negative: `.tmp/live-group-tail-before-2.log`, + `7490dc59ae51a8d719aa2f4dfc42cee989e4e874f9d3fe8f0ae7d49a6853d476`. +- Retained guard charge negative: `.tmp/inflater-guard-charge-before.log`, + `d7d891702abefc0bf34c7860a202c7b8ab406afa131d1e31a3c635abb331db9e`. +- Both focused positives: `.tmp/live-claim-guard-controls.log`, + `7e78fecf9113922de9e8d5162e030129b73c3d0e02ef053d42ee907dab0a5cf6`. +- Strict 606-control cohort: `.tmp/canonical-map-focused.log`, + `a46a4bfaa8928eef9082f55e175bde24a610aad4de233f9ca90a94ccb735dc05`. +- Before benchmark: `.tmp/canonical-bench.log`, + `0fe6d7d6dc4bbcf6a6d05a659cb8e72b0f366c929944a92465a732c0eb3c84f5`. + +No public API, wire field, resource budget, arrival policy or compressed +representation was changed. The Goal remains unfinished until the after +measurement, current-source committed-head gates and real surface proof finish. + + +### Measured weak-index correction + +The identical single after-run completed 13:37:58–13:38:45 UTC on the same +24-CPU x86_64 host, after the other worker's CI became terminal. No heavy job +ran concurrently. Load averages were 3.58/5.89/5.86 before and +2.51/5.24/5.64 after, higher than the earlier baseline. This is a bounded local +comparison, not a general throughput or quiet-host performance guarantee. +The fixed cases and measurement settings were unchanged; no threshold was tuned. + +| Frames × held owner sets | Sizing before | Sizing after | Cleanup before | Cleanup after | +| --- | --- | --- | --- | --- | +| 1 × 1 | 25.912 ns | 8.9837 ns | 230.16 ns | 252.76 ns | +| 1 × 8 | 25.855 ns | 9.0134 ns | 263.43 ns | 285.32 ns | +| 64 × 1 | 599.47 ns | 9.065 ns | 5.6539 µs | 6.4272 µs | +| 64 × 8 | 606.83 ns | 8.9804 ns | 7.3737 µs | 8.1054 µs | +| 1024 × 1 | 8.7272 µs | 9.0952 ns | 165.32 µs | 108.94 µs | +| 1024 × 8 | 8.6809 µs | 8.9223 ns | 191.46 µs | 134.89 µs | +| 8192 × 1 | 69.856 µs | 8.9981 ns | 12.734 ms | 887.63 µs | +| 8192 × 8 | 69.493 µs | 8.9692 ns | 13.046 ms | 1.1143 ms | + +Small-group cleanup is modestly slower, while sizing is constant work and final +cleanup no longer repeatedly shifts retained rows. Exact hash-table capacity is +still charged, so a table may retain spare buckets until its last row drops. +Empty tables free those buckets immediately. The same map and accounting helper +serve the group discovery index; neither retains dead weak history. + +```sh +nix develop --command cargo bench --locked -p moq-net --features fuzz \ + --bench canonical -- --noplot +``` + +After output: `.tmp/canonical-map-bench.log`, SHA256 +`19ca1cca45bb37756cd9143e051e00b061796edd549a3579bbb20534d546e660`. +Conditions: `.tmp/canonical-map-bench-conditions.json`, SHA256 +`2a91de696ca6eaf5d594742e15ee4045e97fe2add5533a67078beb2545d861fd`. +The stable full CI and real surface gates remain due on the final committed head. diff --git a/rs/moq-net/Cargo.toml b/rs/moq-net/Cargo.toml index 1dfc7b41fc1..869f2527551 100644 --- a/rs/moq-net/Cargo.toml +++ b/rs/moq-net/Cargo.toml @@ -20,7 +20,7 @@ default = ["async"] # Runtime-backed helpers and Lite/IETF sessions; explicit-time model drivers stay available. async = ["dep:futures", "moq-clock/runtime"] -# Exposes the wire codecs to the `fuzz/` harness and the announce, codec, path, varint, and frame benches, +# Exposes the wire codecs to the `fuzz/` harness and the announce, codec, path, varint, frame, and canonical storage benches, # and session views of an origin to the viewers bench, through the hidden `fuzz` module. Off by # default and hidden from the docs. fuzz = ["async"] @@ -32,6 +32,7 @@ bytes = { workspace = true } # space left but the flush unfinished, silently truncating the frame. flate2 = { workspace = true, features = ["zlib-rs", "runtime_detection"] } futures = { workspace = true, optional = true } +hashbrown = { workspace = true } hashlink = { workspace = true } kio = { workspace = true } moq-clock = { version = "0.11.0", path = "../moq-clock", default-features = false } @@ -204,5 +205,11 @@ name = "path" harness = false required-features = ["fuzz"] +# Uses private weak-index ownership rather than timing deferred log retirement. +[[bench]] +name = "canonical" +harness = false +required-features = ["fuzz"] + [lints] workspace = true diff --git a/rs/moq-net/benches/canonical.rs b/rs/moq-net/benches/canonical.rs new file mode 100644 index 00000000000..f922610594c --- /dev/null +++ b/rs/moq-net/benches/canonical.rs @@ -0,0 +1,27 @@ +//! Cost of canonical cache sizing and final frame-storage cleanup. +//! Each case retains real frame owners; cleanup includes their weak-record removals, +//! without reporting deferred log retirement as completed physical storage cleanup. +use criterion::{BatchSize, BenchmarkId, Criterion, criterion_group, criterion_main}; +use moq_net::fuzz::CanonicalStorage; +use std::{hint::black_box, time::Duration}; + +fn canonical(c: &mut Criterion) { + let mut group = c.benchmark_group("canonical_storage"); + group.sample_size(20); + group.warm_up_time(Duration::from_millis(500)); + group.measurement_time(Duration::from_secs(2)); + for frames in [1, 64, 1024, 8192] { + for views in [1, 8] { + let id = format!("{frames}x{views}"); + let stored = CanonicalStorage::new(frames, views); + group.bench_function(BenchmarkId::new("size", &id), |b| b.iter(|| black_box(stored.size()))); + drop(stored); + group.bench_function(BenchmarkId::new("cleanup", &id), |b| { + b.iter_batched(|| CanonicalStorage::new(frames, views), drop, BatchSize::PerIteration) + }); + } + } + group.finish(); +} +criterion_group!(benches, canonical); +criterion_main!(benches); diff --git a/rs/moq-net/src/fuzz.rs b/rs/moq-net/src/fuzz.rs index 9d61e7b21ca..a1c227de94d 100644 --- a/rs/moq-net/src/fuzz.rs +++ b/rs/moq-net/src/fuzz.rs @@ -73,6 +73,18 @@ pub fn object_group(frames: usize, properties: bool) -> (broadcast::Producer, tr (broadcast, track, group) } +/// Private canonical-owner fixture for size and final-storage cleanup benchmarks. +pub struct CanonicalStorage(crate::model::group::StorageBench); + +impl CanonicalStorage { + pub fn new(frames: usize, views: usize) -> Self { + Self(crate::model::group::StorageBench::new(frames, views)) + } + pub fn size(&self) -> u64 { + self.0.size() + } +} + /// Every target, keyed by the name of its `fuzz_targets/.rs` shim. pub const TARGETS: &[(&str, Target)] = &[ ("lite", lite_wire), diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 7f34257a139..bb790d734d8 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -987,7 +987,7 @@ impl GroupRecv { // Stats (groups/frames/bytes) are counted in the model as the group // is written, through the tagged `track::Producer`. The track counts // it as content once its first frame lands. - let received = entry.producer.create_group(group_info); + let received = entry.producer.create_group_at(group_info, hdr.frame_start); // The subscription's end waits until this stream is read. Counted // whatever the cache said, since the publisher counts every stream it // opens (a resubscribe resends a cached group), and only after the @@ -1008,7 +1008,7 @@ impl GroupRecv { None, ); } - let mut group = match received { + let group = match received { Ok(group) => group, // The group is at or past the end the publisher declared, which no // later stream can repair. Before lite-05 only a local finish sets @@ -1028,10 +1028,6 @@ impl GroupRecv { } Err(err) => return Poll::Ready(Err(err)), }; - // The stream may carry only the tail of the group; number the frames - // from where the publisher said they start so a front continuing the - // group across routes lines them up. - group.start_at(hdr.frame_start)?; (group, entry.producer.clone(), entry.framing, reading) }; let (priority, order) = track @@ -6100,6 +6096,84 @@ mod tests { let _a = slots.acquire().expect("the failed stream kept a slot"); let _b = slots.acquire().expect("the failed stream kept a slot"); } + /// Distinct live GROUP starts are copies of the same group, including while + /// readers retain every earlier tail. Exercise the real header/ingest path. + #[moq_clock::test] + async fn live_group_tails_share_content_held_by_earlier_readers() { + use crate::transport::poll::Session as _; + let version = Version::Lite07; + let session = crate::lite::test_transport::ScriptedSession::eof(Vec::new()); + let subscriber = Subscriber::new(SubscriberConfig { + runtime: crate::time::sim(), + session: crate::driver::Transport::new(session), + origin: origin::Config::new(crate::Hop::new(1).unwrap()).produce(), + recv_bandwidth: None, + version, + peer_setup: crate::lite::PeerSetup::new(false, crate::transport::Address::Setup), + peer_hop: None, + cost: None, + going_away: Default::default(), + auth: crate::auth::Link::new(false), + }); + let track = track::Producer::new( + std::sync::Arc::new(crate::broadcast::Info::default()), + "video", + crate::track::Info::timed(), + ); + subscriber.subscribes.lock().insert( + 7, + TrackEntry { + producer: track.clone(), + framing: lite::Framing { + timescale: Some(Timescale::MILLI), + ietf: None, + compressed: false, + }, + tail: Default::default(), + }, + ); + let mut held = Vec::new(); + for start in (1..=8).rev() { + let mut script = Vec::new(); + let mut encoder = crate::coding::Encoder::new(&mut script, version.into()); + lite::Group { + subscribe: 7, + sequence: 0, + frame_start: start, + } + .encode(&mut encoder, version) + .unwrap(); + for index in start..9 { + encoder.varint(crate::coding::varint::zigzag(0)).unwrap(); + encoder.bytes(&vec![index as u8; 1024]).unwrap(); + } + let mut incoming = crate::lite::test_transport::ScriptedSession::eof(script); + let (_, recv) = incoming.open_bi(Default::default()).await.unwrap(); + let mut group = GroupRecv::new( + subscriber.clone(), + Reader::new(crate::transport::boxed::RecvStream::new(recv), version), + ); + kio::wait(|waiter| group.poll_serve(waiter)).await.unwrap(); + held.push((start, track.consume().peek_group(0).unwrap().with_range(start..))); + } + let mut pointers = std::collections::BTreeMap::new(); + for (start, reader) in &mut held { + for index in *start..9 { + assert_eq!(reader.index(), index); + let frame = reader.read_frame().await.unwrap().unwrap(); + assert_eq!(frame.payload.as_ref(), vec![index as u8; 1024]); + if let Some(prior) = pointers.insert(index, frame.payload.as_ptr() as usize) { + assert_eq!( + prior, + frame.payload.as_ptr() as usize, + "live GROUP frame {index} has one physical payload across held tails" + ); + } + } + assert!(reader.read_frame().await.unwrap().is_none()); + } + assert!(track.closed().now_or_never().is_none()); + } } /// The ranges a SUBSCRIBE carries for `subscription`, without the empty ones, with time diff --git a/rs/moq-net/src/model/frame.rs b/rs/moq-net/src/model/frame.rs index 42b3efe9895..210ca0e755b 100644 --- a/rs/moq-net/src/model/frame.rs +++ b/rs/moq-net/src/model/frame.rs @@ -472,7 +472,11 @@ struct Identity { impl Drop for FrameContent { fn drop(&mut self) { if let Some(identity) = self.identity.get() { - identity.owner.forget_frame(identity.key, self as *const Self as usize); + identity.owner.forget_frame( + identity.key, + self as *const Self as usize, + self.bill.get().map_or(0, cache::Extra::size), + ); } } } @@ -529,11 +533,25 @@ impl Shared { pub(super) fn bill_content(&self, bill: cache::Extra) { self.0.bill_content(bill); } + #[cfg(test)] pub(super) fn content_size(&self) -> u64 { self.0.bill.get().map_or(0, cache::Extra::size) } pub(super) fn identify(&self, owner: Arc, key: group::ContentKey) -> bool { - self.0.identity.set(Identity { owner, key }).is_ok() + let _publication = self.0.publication.lock().unwrap_or_else(PoisonError::into_inner); + if self + .0 + .identity + .set(Identity { + owner: owner.clone(), + key, + }) + .is_err() + { + return false; + } + owner.charge_frame(self.0.bill.get().map_or(0, cache::Extra::size)); + true } } @@ -553,10 +571,24 @@ impl FrameContent { .checked_sub(bill.size()) .expect("transferred payload cannot exceed published bytes"); bill.add(fresh + (FrameBuf::CONTENT_HEAP + admission) as u64); + if let Some(identity) = self.identity.get() { + identity.owner.charge_frame(bill.size()); + } bill }); } + /// Caller holds publication so attachment and growth update one aggregate. + fn add_charge(&self, bytes: u64) { + if let Some(bill) = self.bill.get() { + let before = bill.size(); + bill.add(bytes); + if let Some(identity) = self.identity.get() { + identity.owner.charge_frame(bill.size() - before); + } + } + } + fn freeze(&self) -> Payload { debug_assert_eq!(self.written.load(Ordering::Acquire), self.size); self.frozen @@ -845,9 +877,7 @@ impl FrameBuf { publication.admission = Arc::downgrade(&admission); if !publication.admitted { publication.admitted = true; - if let Some(bill) = content.bill.get() { - bill.add(Self::ADMISSION_HEAP as u64); - } + content.add_charge(Self::ADMISSION_HEAP as u64); } Some(admission) } @@ -885,9 +915,7 @@ impl FrameBuf { unsafe fn store_written(&self, new_written: usize) { let content = &self.0.content; let before = content.written.load(Ordering::Relaxed); - if let Some(bill) = content.bill.get() { - bill.add((new_written - before) as u64); - } + content.add_charge((new_written - before) as u64); // Release pairs with consumers' Acquire load to publish prior writes. content.written.store(new_written, Ordering::Release); } diff --git a/rs/moq-net/src/model/group.rs b/rs/moq-net/src/model/group.rs index 9165f32b909..b4248046368 100644 --- a/rs/moq-net/src/model/group.rs +++ b/rs/moq-net/src/model/group.rs @@ -20,6 +20,8 @@ use crate::cache; use crate::frame::{self, Cached, FrameBuf, Payload}; use crate::{Timescale, stats, track}; use bytes::Bytes; +#[cfg(any(test, feature = "fuzz"))] +pub(crate) use content::StorageBench; pub(super) use content::{Content, Contents, Key as ContentKey}; use content::{Input, Spec}; use std::collections::VecDeque; @@ -339,7 +341,7 @@ struct Relay { /// What a compressed group keeps beside its stored DEFLATE slices. struct Flate { // A pending inflater belongs to this delivery even when its Plain is shared. - release: Arc>>, + release: Option>, // The inflated size of every frame admitted so far, held to the budget a plain // group's bytes are, so a declaration is refused before anything inflates. inflated: u64, @@ -350,12 +352,27 @@ struct Flate { plain: Arc, } +struct Release { + error: Mutex>, + bill: cache::Extra, +} + +impl Release { + fn new(bill: cache::Extra) -> Arc { + bill.add(Flate::RELEASE_HEAP); + Arc::new(Self { + error: Mutex::default(), + bill, + }) + } +} + impl Flate { - const RELEASE_HEAP: u64 = (2 * size_of::() + size_of::>>()) as u64; + const RELEASE_HEAP: u64 = (2 * size_of::() + size_of::()) as u64; - fn new(plain: Arc) -> Self { + fn new(plain: Arc, bill: cache::Extra) -> Self { Self { - release: Arc::default(), + release: Some(Release::new(bill)), inflated: 0, encoder: None, plain, @@ -391,7 +408,9 @@ impl Flate { /// Drop everything but how the group is read, failing a late inflater with `err`. fn release(&mut self, err: Error, shared: bool) { // Wait for at most the frame currently inflating, just as Plain::release did. - *self.release.lock().unwrap_or_else(PoisonError::into_inner) = Some(err.clone()); + if let Some(release) = self.release.take() { + *release.error.lock().unwrap_or_else(PoisonError::into_inner) = Some(err.clone()); + } if !shared { self.encoder = None; self.plain.release(err); @@ -485,7 +504,7 @@ impl Plain { /// The stored slices a plain read of a compressed group inflates once the group's guard /// is released. struct Inflate { - release: Arc>>, + release: Arc, plain: Arc, // Local index of the first slice: the copy's inflated length when the read took them. from: usize, @@ -501,7 +520,7 @@ impl Inflate { /// lock held, since settling the debt takes the track's. fn run(&self, meter: &stats::Meter) { for (index, (slice, size)) in (self.from..).zip(&self.slices) { - let released = self.release.lock().unwrap_or_else(PoisonError::into_inner); + let released = self.release.error.lock().unwrap_or_else(PoisonError::into_inner); if released.is_some() { break; } @@ -546,7 +565,7 @@ impl Inflate { /// Inflated local frame `local`, or why it is not. fn get(&self, local: usize) -> Result { - let released = self.release.lock().unwrap_or_else(PoisonError::into_inner); + let released = self.release.error.lock().unwrap_or_else(PoisonError::into_inner); if let Some(err) = released.as_ref() { return Err(err.clone()); } @@ -737,7 +756,10 @@ impl GroupState { } let from = flate.plain.len.load(Ordering::Acquire).min(last + 1); Ok(Inflate { - release: flate.release.clone(), + release: flate + .release + .clone() + .ok_or_else(|| self.abort.clone().unwrap_or(Error::Dropped))?, plain: flate.plain.clone(), from, slices: self @@ -1224,13 +1246,15 @@ impl Producer { /// track evicts toward under memory pressure. pub(super) fn new(info: Info, track: track::Info, cache: Arc) -> Self { let state = kio::Producer::::default(); - let mut charge = cache.charge(); + let charge = cache.charge(); let access = charge.access(); let log = { let mut state = state.write().ok().expect("a new group is open"); if track.compress { - charge.add(Flate::RELEASE_HEAP); - state.flate = Some(Box::new(Flate::new(Arc::new(Plain::new(charge.extra()))))); + state.flate = Some(Box::new(Flate::new( + Arc::new(Plain::new(charge.extra())), + charge.extra(), + ))); } state.charge = charge; state.log.reader() @@ -1997,7 +2021,13 @@ impl Handle { pub(super) fn delivery_size(&self) -> u64 { let state = self.state.read(); - state.charge.size() + state.partial.as_ref().map_or(0, |partial| partial.buf.view_size()) + state.charge.size() + + state + .flate + .as_ref() + .and_then(|flate| flate.release.as_ref()) + .map_or(0, |release| release.bill.size()) + + state.partial.as_ref().map_or(0, |partial| partial.buf.view_size()) } /// Adopt the frames of `head` below where this copy starts, so it holds the group @@ -2338,6 +2368,11 @@ impl Handle { pub(super) fn cache_size(&self) -> u64 { let state = self.state.read(); state.charge.size() + + state + .flate + .as_ref() + .and_then(|flate| flate.release.as_ref()) + .map_or(0, |release| release.bill.size()) + state.content.as_ref().map_or_else( || state.flate.as_ref().map_or(0, |flate| flate.plain.bill.size()), |content| content.size(), @@ -5648,7 +5683,7 @@ mod test { let pool = cache::Pool::unbounded(); let mut group = compressed_group(&pool); group.write_frame(None, verbose(0)).unwrap(); - let stored = group.handle.state.read().charge.size(); + let stored = group.handle.state.read().charge.size() + Flate::RELEASE_HEAP; assert_eq!(group.handle.cache_size(), stored + flate::ENCODER_HEAP); group.write_frame(None, verbose(1)).unwrap(); @@ -5656,7 +5691,7 @@ mod test { let mut reader = group.consume(); assert_eq!(read_plain(&mut reader).unwrap().unwrap(), verbose(0)); let inflated = (verbose(0).len() + verbose(1).len()) as u64; - let stored = group.handle.state.read().charge.size(); + let stored = group.handle.state.read().charge.size() + Flate::RELEASE_HEAP; let held = stored + flate::ENCODER_HEAP + flate::DECODER_HEAP + inflated; assert_eq!(group.handle.cache_size(), held, "an open group keeps both"); @@ -5671,7 +5706,7 @@ mod test { assert_eq!(read_plain(&mut reader).unwrap().unwrap(), verbose(0)); group.write_frame(None, verbose(1)).unwrap(); group.finish().unwrap(); - let stored = group.handle.state.read().charge.size(); + let stored = group.handle.state.read().charge.size() + Flate::RELEASE_HEAP; assert_eq!(read_plain(&mut reader).unwrap().unwrap(), verbose(1)); assert_eq!(group.handle.cache_size(), stored + inflated); @@ -5682,7 +5717,7 @@ mod test { let job = take_slices(&group, 1); group.finish().unwrap(); job.run(&stats::Meter::default()); - let stored = group.handle.state.read().charge.size(); + let stored = group.handle.state.read().charge.size() + Flate::RELEASE_HEAP; assert_eq!(group.handle.cache_size(), stored + inflated, "the decoder is dropped"); } @@ -5826,4 +5861,25 @@ mod test { expired.store(false, Ordering::Relaxed); assert!(group.poll_expired(&waiter), "and the verdict stands"); } + /// A captured job owns its release guard independently of the cached delivery. + #[test] + fn inflater_release_guard_charge_follows_its_last_job() { + let pool = cache::Pool::unbounded(); + let mut group = compressed_group(&pool); + group.write_frame(None, verbose(0)).unwrap(); + group.finish().unwrap(); + let job = take_slices(&group, 0); + let retained = job.release.clone(); + group.abort(Error::Evicted).unwrap(); + assert_eq!( + pool.used(), + Flate::RELEASE_HEAP, + "the job still owns the new guard allocation" + ); + assert!(matches!(job.get(0), Err(Error::Evicted))); + drop(job); + assert_eq!(pool.used(), Flate::RELEASE_HEAP, "clones share one intrinsic charge"); + drop(retained); + assert_eq!(pool.used(), 0, "the final guard releases its own charge"); + } } diff --git a/rs/moq-net/src/model/group/content.rs b/rs/moq-net/src/model/group/content.rs index 0e081e3fe30..af49b29c76e 100644 --- a/rs/moq-net/src/model/group/content.rs +++ b/rs/moq-net/src/model/group/content.rs @@ -1,8 +1,28 @@ //! Weak indexed identity; delivery records and streaming readers own the bytes. use super::*; +use std::collections::hash_map::RandomState; +use std::hash::Hash; +use std::sync::atomic::AtomicU64; + +type Map = hashbrown::HashMap; + +fn insert(map: &mut Map, key: K, value: V, bill: &cache::Extra) { + let before = map.allocation_size(); + map.insert(key, value); + bill.add((map.allocation_size() - before) as u64); +} + +fn remove(map: &mut Map, key: &K, bill: &cache::Extra) { + map.remove(key); + if map.is_empty() { + let bytes = map.allocation_size(); + *map = Map::default(); + bill.sub(bytes as u64); + } +} /// Object IDs are not frame ordinals on a sparse IETF stream. -#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +#[derive(Clone, Copy, PartialEq, Eq, Hash)] pub(in crate::model) enum Key { Frame(u64), Object(u64), @@ -21,13 +41,13 @@ pub(super) struct Spec { } struct Row { - key: Key, inflated: Option, content: frame::SharedWeak, } pub(in crate::model) struct Content { - state: Mutex>, + state: Mutex>, + frame_charge: AtomicU64, bill: cache::Extra, plain: Mutex>>, views: AtomicUsize, @@ -40,6 +60,7 @@ impl Content { bill.add((2 * size_of::() + size_of::()) as u64); Arc::new(Self { state: Mutex::default(), + frame_charge: AtomicU64::new(0), bill, plain: Mutex::new(plain), views: AtomicUsize::new(0), @@ -64,37 +85,33 @@ impl Content { ) -> Result { let Spec { key, size, inflated } = spec; let mut state = self.state.lock().unwrap_or_else(PoisonError::into_inner); - let at = match state.binary_search_by_key(&key, |row| row.key) { - Ok(at) => { - if let Some(shared) = state[at].content.upgrade() { - let matches = shared.size() as u64 == size && state[at].inflated == inflated; - // A temporary strong owner must never drop under this mutex: its last - // release removes its weak record through `forget_frame`. - drop(state); - if !matches { - return Err(Error::ProtocolViolation); - } - let payload = match input { - Input::Whole(payload) => Some(payload), - Input::Adopt(shared) => Some(shared.freeze()), - Input::Stream(_) => None, - }; - if let Some(payload) = payload { - let delivery = shared.fork(); - let mut at = 0; - while at < payload.len() { - let chunk = payload.chunk_at(at); - delivery.append(&chunk)?; - at += chunk.len(); - } + if let Some(row) = state.get(&key) { + if let Some(shared) = row.content.upgrade() { + let matches = shared.size() as u64 == size && row.inflated == inflated; + // A temporary strong owner must never drop under this mutex: its last + // release removes its weak record through `forget_frame`. + drop(state); + if !matches { + return Err(Error::ProtocolViolation); + } + let payload = match input { + Input::Whole(payload) => Some(payload), + Input::Adopt(shared) => Some(shared.freeze()), + Input::Stream(_) => None, + }; + if let Some(payload) = payload { + let delivery = shared.fork(); + let mut at = 0; + while at < payload.len() { + let chunk = payload.chunk_at(at); + delivery.append(&chunk)?; + at += chunk.len(); } - return Ok(shared); } - state.remove(at); - at + return Ok(shared); } - Err(at) => at, - }; + state.remove(&key); + } // Retained bytes/frame limits remain per delivery. Combining valid disjoint // ranges must not introduce a stricter aggregate admission policy. if size > MAX_CACHE_BYTES || inflated.is_some_and(|size| size > MAX_CACHE_BYTES) { @@ -111,31 +128,30 @@ impl Content { return Err(Error::ProtocolViolation); } shared.bill_content(transferred.unwrap_or_else(|| charge.extra())); - let before = state.capacity(); - state.insert( - at, + insert( + &mut state, + key, Row { - key, inflated, content: shared.downgrade(), }, + &self.bill, ); - self.bill.add(((state.capacity() - before) * size_of::()) as u64); drop(state); Ok(shared) } - pub(in crate::model) fn forget_frame(&self, key: Key, address: usize) { + pub(in crate::model) fn charge_frame(&self, bytes: u64) { + self.frame_charge.fetch_add(bytes, Ordering::Relaxed); + } + + pub(in crate::model) fn forget_frame(&self, key: Key, address: usize, charge: u64) { + self.frame_charge + .fetch_update(Ordering::Relaxed, Ordering::Relaxed, |total| total.checked_sub(charge)) + .expect("canonical frame charge underflow"); let mut state = self.state.lock().unwrap_or_else(PoisonError::into_inner); - if let Ok(at) = state.binary_search_by_key(&key, |row| row.key) - && state[at].content.address() == address - { - state.remove(at); - } - if state.is_empty() { - let bytes = state.capacity() * size_of::(); - *state = Vec::new(); - self.bill.sub(bytes as u64); + if state.get(&key).is_some_and(|row| row.content.address() == address) { + remove(&mut state, &key, &self.bill); } } @@ -157,11 +173,12 @@ impl Content { } } + #[cfg(test)] fn frames(&self) -> Vec { self.state .lock() .unwrap_or_else(PoisonError::into_inner) - .iter() + .values() .filter_map(|row| row.content.upgrade()) .collect() } @@ -170,11 +187,14 @@ impl Content { pub(in crate::model) fn cost(&self) -> (usize, usize, u64) { let frames = self.frames(); let bytes = frames.iter().map(frame::Shared::written).sum(); + assert_eq!( + frames.iter().map(frame::Shared::content_size).sum::(), + self.frame_charge.load(Ordering::Relaxed) + ); (frames.len(), bytes, self.size()) } pub(super) fn size(&self) -> u64 { - let frames = self.frames(); self.bill.size() + self .plain @@ -182,7 +202,7 @@ impl Content { .unwrap_or_else(PoisonError::into_inner) .as_ref() .map_or(0, |plain| plain.bill.size()) - + frames.iter().map(frame::Shared::content_size).sum::() + + self.frame_charge.load(Ordering::Relaxed) } } @@ -195,10 +215,15 @@ impl Drop for Content { } if let Some((index, sequence)) = self.index.get_mut().unwrap_or_else(PoisonError::into_inner).take() { let mut state = index.state.lock().unwrap_or_else(PoisonError::into_inner); - if let Ok(at) = state.binary_search_by_key(&sequence, |(sequence, _)| *sequence) - && std::ptr::eq(state[at].1.as_ptr(), self) + if state + .get(&sequence) + .is_some_and(|owner| std::ptr::eq(owner.as_ptr(), self)) { - state.remove(at); + remove( + &mut state, + &sequence, + index.bill.get().expect("registered index is billed"), + ); } } } @@ -211,7 +236,7 @@ pub(in crate::model) struct Contents(Mutex>); #[derive(Default)] struct Index { - state: Mutex)>>, + state: Mutex>>, bill: OnceLock, } @@ -219,10 +244,7 @@ impl Contents { pub(in crate::model) fn get(&self, sequence: u64) -> Option> { let index = self.0.lock().unwrap_or_else(PoisonError::into_inner).upgrade()?; let state = index.state.lock().unwrap_or_else(PoisonError::into_inner); - state - .binary_search_by_key(&sequence, |(sequence, _)| *sequence) - .ok() - .and_then(|at| state[at].1.upgrade()) + state.get(&sequence).and_then(Weak::upgrade) } pub(in crate::model) fn remove(&self, sequence: u64) { @@ -230,9 +252,11 @@ impl Contents { return; }; let mut state = index.state.lock().unwrap_or_else(PoisonError::into_inner); - if let Ok(at) = state.binary_search_by_key(&sequence, |(sequence, _)| *sequence) { - state.remove(at); - } + remove( + &mut state, + &sequence, + index.bill.get().expect("registered index is billed"), + ); } pub(in crate::model) fn insert(&self, sequence: u64, content: &Arc, charge: &cache::Charge) { @@ -253,15 +277,105 @@ impl Contents { bill.add((2 * size_of::() + size_of::()) as u64); bill }); - match state.binary_search_by_key(&sequence, |(sequence, _)| *sequence) { - Ok(at) => state[at].1 = Arc::downgrade(content), - Err(at) => { - let before = state.capacity(); - state.insert(at, (sequence, Arc::downgrade(content))); - bill.add(((state.capacity() - before) * size_of::<(u64, Weak)>()) as u64); - } - } + insert(&mut state, sequence, Arc::downgrade(content), bill); drop(state); *content.index.lock().unwrap_or_else(PoisonError::into_inner) = Some((index, sequence)); } } + +/// The actual weak-index/strong-frame ownership used by canonical cache accounting. +#[cfg(any(test, feature = "fuzz"))] +pub(crate) struct StorageBench { + content: Arc, + _owners: Vec>, + _charge: cache::Charge, +} + +#[cfg(any(test, feature = "fuzz"))] +impl StorageBench { + pub(crate) fn new(frames: usize, views: usize) -> Self { + let account = cache::Track::new(cache::Pool::unbounded(), Default::default(), kio::Weak::new()); + let charge = account.charge(); + let content = Content::new(&charge, None); + let mut owners = Vec::with_capacity(frames); + for index in 0..frames { + owners.push( + content + .row( + Spec { + key: Key::Frame(index as u64), + size: 64, + inflated: None, + }, + Input::Whole(Bytes::from(vec![0; 64]).into()), + &charge, + None, + ) + .unwrap(), + ); + } + let held = (0..views).map(|_| owners.clone()).collect(); + Self { + content, + _owners: held, + _charge: charge, + } + } + + pub(crate) fn size(&self) -> u64 { + self.content.size() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn weak_indexes_remove_final_owners_without_losing_replacements() { + let pool = cache::Pool::unbounded(); + let account = cache::Track::new(pool.clone(), Default::default(), kio::Weak::new()); + let charge = account.charge(); + let baseline = pool.used(); + let contents = Contents::default(); + let old = Content::new(&charge, None); + contents.insert(7, &old, &charge); + let current = Content::new(&charge, None); + contents.insert(7, ¤t, &charge); + drop(old); + assert!(Arc::ptr_eq(&contents.get(7).unwrap(), ¤t)); + let mut frames = Vec::new(); + for index in 0..1024 { + frames.push( + current + .row( + Spec { + key: Key::Frame(index), + size: 0, + inflated: None, + }, + Input::Whole(Bytes::new().into()), + &charge, + None, + ) + .unwrap(), + ); + } + assert_eq!(current.cost().0, 1024); + // Unordered final releases exercise removal while other weak rows remain. + for index in (0..512).rev() { + frames.swap_remove(index); + } + assert_eq!(current.cost().0, 512); + drop(frames); + assert_eq!(current.cost().0, 0); + assert_eq!(current.state.lock().unwrap().allocation_size(), 0); + drop(current); + assert!(contents.get(7).is_none()); + assert_eq!( + pool.used(), + baseline, + "frame and group weak indexes own no dead history" + ); + } +} diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 0477b64e61c..619e063b018 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -2600,6 +2600,20 @@ impl TrackState { Ok(()) } + fn content_for_copy(&self, sequence: u64) -> Result>> { + if let Some(content) = self.contents.get(sequence) { + return Ok(Some(content)); + } + if let Some(slot) = self.lookup.get(&sequence) { + for copy in slot.copies() { + if let Some(content) = copy.activate_content()? { + return Ok(Some(content)); + } + } + } + Ok(None) + } + /// Insert a group a [`Fill`] answered, setting the track's [`Info`] /// if it isn't accepted yet. The group's timescale comes from that info, so a /// fetch can serve an as-yet-unaccepted track (e.g. a relay with no live @@ -2631,17 +2645,8 @@ impl TrackState { if self.covering_range(sequence, frames).is_some() { return Err(Error::Duplicate); } - let mut content = self.contents.get(sequence); - if content.is_none() - && let Some(slot) = self.lookup.get(&sequence) - { - for copy in slot.copies() { - if let Some(owner) = copy.activate_content()? { - content = Some(owner); - break; - } - } - } + let content = self.content_for_copy(sequence)?; + if !self.lookup.get(&sequence).is_some_and(Slot::is_live) { self.uncache(sequence); } @@ -2867,20 +2872,39 @@ impl Producer { /// live edge, or the successor that ages the group before it) only once its first frame /// opens or it finishes. pub fn create_group(&self, group: group::Info) -> Result { + self.create_group_inner(group, None) + } + + /// A received copy starts at its wire offset before any reader can see it. + pub(crate) fn create_group_at(&self, group: group::Info, start: u64) -> Result { + self.create_group_inner(group, Some(start)) + } + + fn create_group_inner(&self, info: group::Info, received: Option) -> Result { let mut state = self.modify()?; - // A sealed track takes no group, not even one below its end. - if state.sealed || state.final_sequence().is_some_and(|fin| group.sequence >= fin) { + if state.sealed || state.final_sequence().is_some_and(|fin| info.sequence >= fin) { return Err(Error::Closed); } + let start = received.unwrap_or(0); + let content = if received.is_some() { + if state.covering_group(info.sequence, start).is_some() { + return Err(Error::Duplicate); + } + state.content_for_copy(info.sequence)? + } else { + None + }; let track = state.info.clone().unwrap(); - - // An evicted sequence can be re-created; a live one is a duplicate. - state.claim_sequence(group.sequence, 0)?; - - let group = group::Producer::new(group, track, state.cache.clone()).with_meter(self.stats.meter()); + state.claim_sequence(info.sequence, start)?; + let mut group = group::Producer::new(info, track, state.cache.clone()).with_meter(self.stats.meter()); + if start != 0 { + group.bound(group::Bounds { start, end: None })?; + } + if let Some(content) = content { + group.handle().share_content(content); + } state.cap(&group.handle())?; state.commit_group(&group.handle()); - Ok(group) } From d16f5977fd4fbd1df598b2ae99174e6b718e0966 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 27 Oct 2026 06:46:36 -0700 Subject: [PATCH 06/16] Complete transport frame offset quest Co-Authored-By: GPT-6 --- quest/m1/README.md | 1 - quest/m1/transport-frame-offsets.md | 429 -------------------- quest/m1/unbounded-groups/front-eviction.md | 4 - 3 files changed, 434 deletions(-) delete mode 100644 quest/m1/transport-frame-offsets.md diff --git a/quest/m1/README.md b/quest/m1/README.md index eba86c17ded..81cde26d1de 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -124,7 +124,6 @@ surface and zero-copy texture import, land before the cut. - [Lite-05/06 timestamps pass through](/quest/m1/lite-timestamps-through.md) - a timed track received over lite-05/06 stays timed and is forwarded with its timestamps over every version - [Exact timestamps in every binding](/quest/m1/binding-timestamps.md) - bindings carry `{value, scale}` timestamps instead of lossy microseconds - [Announcement takeover](/quest/m1/announce-takeover.md) - a new independent route explicitly continues instances served through its predecessor, with shared Rust/JS semantics and lite-07 wire -- [The transport may start a group mid-way](/quest/m1/transport-frame-offsets.md) - a hop may request and deliver a group from any frame it needs, while applications still ask for whole groups - [Subscribe ranges](/quest/m1/subscribe-ranges/README.md) - a lite-07 SUBSCRIBE asks for past and live ranges with absolute or relative ends, in either order, and replaces FETCH; merged ranges never starve a subscriber; relays fill misses by range, including over moq-transport - [Drop set](/quest/m1/drop-set.md) - each cursor judges missing groups against its own budget, so a narrow reader drops promptly and a widening re-offers groups already reported dropped on lite-07 - [Broadcast archives](/quest/m1/archive/README.md) - ANNOUNCE, TRACKS, and a SUBSCRIBE per track record every track under a prefix to any object_store, a host serves a stored broadcast back, and the catalog drops its archive pointer for an opt-in `hls` field diff --git a/quest/m1/transport-frame-offsets.md b/quest/m1/transport-frame-offsets.md deleted file mode 100644 index 1e9a704940f..00000000000 --- a/quest/m1/transport-frame-offsets.md +++ /dev/null @@ -1,429 +0,0 @@ -# [M] The transport may start a group mid-way - -## Goal - -A moq-lite hop may request and deliver a group starting at any frame k the -requester needs, whether or not it still holds the frames below k (route -continuation, hole fills, and front-eviction refills after the front was -evicted, with a frame-precise end), so a relay never refetches frames it does -not need. Applications still only ask for whole groups: no public API -starts at a frame. Rust and `@moq/net` both accept and cache such a tail. - -## Plan - -Decided 2026-10-25 (maintainer): remove frame-N starts from the API, as -[moq-dev/moq#6465](https://github.com/moq-dev/moq/pull/6465) did, but allow -them in the transport as an optimization. - -- Draft: amend lite-07-wip so a subscriber may send any nonzero frame start, - replacing "MUST send 0 unless it already holds every frame below this - index". A front-eviction refill needs frames k and up after the frames - below k were evicted, so holding the prefix cannot be the condition. Keep - the `Frame End` rule. -- Widening is relay policy, not a wire rule: a relay asked for frame k of a - group it holds none of widens the upstream request to frame 0 and cuts - locally, so the cache stays usable by - whole-group readers and a compressed group can inflate. -- A partial copy is the same group, never a new identity: `covering_group` - already serves only copies that cover the start. A copy cut short at the back - is marked incomplete so it never answers a whole-group fetch. -- JS accepts a GROUP with `frameStart > 0` (numbering from it, as Rust's - `group::Producer::start_at` does) instead of refusing it as a protocol - violation. -- A moq-transport stream that starts mid-group with no head is still dropped - (2026-10-22 decision, reaffirmed 2026-10-25). -- The rule in `rs/moq-net/AGENTS.md` that banned partial groups was deleted - 2026-10-25. - -Bound the cost a hostile peer can create: many distinct tail starts for one -group must not multiply its copies; coalesce to one copy per group. - -Public API: none. Wire: lite-07-wip draft text only; the fields exist. - -## Related - -- [Front eviction](/quest/m1/unbounded-groups/front-eviction.md) - refills a front hole with a frame-precise range - -### Implementation checkpoint - -[moq-dev/moq#6922](https://github.com/moq-dev/moq/pull/6922) implements private -JS offset initialization before publication, exact forwarding/cache coverage, -and private Rust frame-end provenance. Public APIs and existing wire fields -remain unchanged. The full Goal is unfinished: retaining one cached head does -not bound storage held by earlier readers. The selected one-copy bound still -applies; no weaker ownership or resource policy has been selected. - -Before fixing, the timed JS GROUP control passed at frame 0 and refused frame -5 and 9000 with PROTOCOL_VIOLATION. The actual Rust FetchServe control queued -start 2 for a cold request with start 2/end 3, rather than widening to 0. -Those controls now pass. Focused JS checks cover early/late mirrors, retained -frame limits, missing compression prefix, exact tail forwarding, and the -unchanged declared-end violation. Fifteen focused Rust controls pass, including -bounded-end cache misses, queued/taken range widening, and live-copy preservation. -These are focused checks, not final-head CI or full interop evidence. - -A small deterministic held-reader control demonstrates the remaining cost in -both the inherited unbounded-end path and the bounded-end path. Eight tails -start at frames 8 through 1, with identical bytes for every matching frame -index, beside a live copy starting at 32. Each reader retains its tail unread. -The unbounded copies all end at 33; the bounded copies all end at 17. - -| End | Distinct tail stores + live | Slot handles | Tail payload retained | Unique tail payload | Pool charge before/after identity-probe drop | After reader drop | -| --- | --- | --- | --- | --- | --- | --- | -| Unbounded | 8 + 1 | 2 | 233472 B | 32768 B | 266376 B / 266376 B | 40864 B | -| Exclusive 17 | 8 + 1 | 2 | 102400 B | 16384 B | 124456 B / 124456 B | 22144 B | - -The live baseline charge is 2216 B. Distinct state channels, overlapping payloads, -and unchanged charge after identity probes are dropped establish reader-held -multiplication; Slot ownership alone does not prove the Goal. Existing -`Head::adopt_head` shares payload references but only joins a head that reaches -the target offset, refusing sparse gaps. The bounded copies ending at 17 do not -reach the live copy at 32. Coalescing must preserve existing public reader -outcomes, completion/error ownership, compression, and cache accounting. - -The diagnostic below was inserted into `model::track::test`, run once through -the normal runner, then removed from executable source. It remains here so the -proof survives local scratch cleanup. The source and raw output are retained -under this worktree's `.tmp/held-tail-cost.rs` and -`.tmp/held-tail-overlap-cost.log`. It measures correctness and ownership, not -host performance or a new acceptance budget. SHA256 source: -`ea08934f00d65cb19443c9f0c90eec824ff6e2abc0fa347c587aef2564e33a76`; -raw output: `3c9efaf8a72a0a23e2f01c536e61a3ab0e5e4e52a35dd028eb6d73c7081c9eb9`. - -```sh -nix develop --command just rs test -p moq-net --lib \ - -E 'test(=model::track::test::diagnostic_held_tail_storage)' \ - --success-output immediate -``` - -```rust - #[moq_clock::test] - #[cfg(feature = "async")] - async fn diagnostic_held_tail_storage() { - for bounded in [false, true] { - let producer = track_producer("test", Info::timed()); - let pool = producer.broadcast.pool.clone(); - let dynamic = producer.dynamic(); - let consumer = producer.consume(); - let mut live = producer.create_group(group::Info { sequence: 0 }).unwrap(); - live.start_at(32).unwrap(); - live.write_frame(Timestamp::ZERO, vec![32; 1024]).unwrap(); - let baseline = pool.used(); - let mut held = Vec::new(); - let mut identities = Vec::new(); - for start in (1..=8).rev() { - let options = group::Fetch { frame_start: start, ..Default::default() }; - let fetch = if bounded { - consumer.fetch_range(0, FetchOptions { options, end: Some(17) }) - } else { - consumer.fetch_group(0, options) - }; - let request = dynamic.requested_fill_group().await.unwrap(); - let mut tail = request.accept(Info::timed()).unwrap(); - for frame in start..if bounded { 17 } else { 33 } { - tail.write_frame(Timestamp::ZERO, vec![frame as u8; 1024]).unwrap(); - } - tail.finish().unwrap(); - identities.push(tail.handle()); - held.push(fetch.await.unwrap()); - } - for (i, left) in identities.iter().enumerate() { - for right in &identities[i + 1..] { assert!(!left.is_clone(right)); } - assert!(!left.is_clone(&live.handle())); - } - let before_identity_drop = pool.used(); - drop(identities); - let only_readers_and_slot = pool.used(); - let cached_handles = producer.state.read().lookup[&0].copies().count(); - eprintln!("HELD_TAIL_COST bounded={bounded} requests=8 unique_tail_storages=8 live_storages=1 slot_handles={cached_handles} unread_payload={} unique_payload={} baseline_charge={baseline} charge_with_identities={before_identity_drop} charge_after_identity_drop={only_readers_and_slot}", if bounded { 100*1024 } else { 228*1024 }, if bounded { 16*1024 } else { 32*1024 }); - for (i, reader) in held.iter_mut().enumerate() { - let frame = reader.read_frame().await.unwrap().unwrap(); - assert_eq!(frame.payload.len(), 1024); - assert!(frame.payload.iter().all(|byte| *byte == (8-i) as u8)); - for expected in (9-i)..if bounded { 17 } else { 33 } { - let frame = reader.read_frame().await.unwrap().unwrap(); - assert!(frame.payload.iter().all(|byte| *byte == expected as u8)); - } - } - drop(held); - eprintln!("HELD_TAIL_COST bounded={bounded} charge_after_reader_drop={}", pool.used()); - live.abort(Error::Cancel).unwrap(); - } - } -``` - -### Canonical-owner prototype checkpoint - -The private Rust prototype activates an indexed content owner when a second -transport delivery would coexist with the first. Ordinary single-copy whole -and batch writes retain their existing representation. Activation imports the -existing payloads without copying and attaches the exact in-flight `FrameBuf`, -so an already-streaming reader and writer keep their progress. New deliveries -fork their own received counter while sharing immutable content, publication -and the origin's admission. Contradictory bytes use `ProtocolViolation`. - -A `TrackState`-scoped weak index lets a refill discover an owner still retained -by older tails after its Slot was removed. Owners remove their weak entries; -the index is owned by the live owners, so dead history does not accumulate. -Explicit live-group recreation replaces the sequence's indexed incarnation. -A different track instance does not inherit it. Delivery logs, bounds, FIN, -errors, timestamps and public arrival records remain separate. IETF Object IDs -have their own key space instead of being mistaken for dense frame ordinals. - -Intrinsic `FrameBuf` charges follow the shared storage and each progress view, -including streamed readers that outlive the delivery's partial slot. A -payload-only `Charge::transfer` moves old bytes without recording them as new -writes. Fresh attachment and already-canonical transfer have controls. Deferred -log entries hold weak row handles; a non-polling cursor cannot keep a canonical -owner alive through retired log storage. Handoff heads retain the content owner. - -The first eager prototype exposed real ownership and accounting integration -failures: 466/491 existing group/track tests passed initially, then 482/491 -after ownership corrections. Its sources and raw results remain in -`.tmp/eager-content-prototype/`, `.tmp/content-model-tests.log` and -`.tmp/content-model-tests-2.log`. It was replaced by lazy activation rather than -relaxing cache-pressure thresholds. Two refill fixtures had different bytes for -the same fixed incarnation; only those payloads were made consistent, preserving -the bounds, lifetime and end-refusal assertions. - -The lazy implementation passes 508/508 strict frame, group and track controls, -including the following new outer controls: - -- Eight distinct tail starts with all earlier readers held, at 0, 1 and 1024 B - per frame, both bounded and unbounded. Matching nonempty frame payloads have - the same physical pointer. Dropping identity probes changes no charge. -- Actual Slot removal with an older reader-held tail, followed by a refill - sharing its owner; explicit live recreation accepts different bytes and uses - a separate owner. -- Activation while the original frame and streaming reader are already open. - Overlap credits no progress to that delivery. One abort leaves the other - producer valid and admitted. Two completions retain distinct arrival times - 10 ms apart under the deterministic clock while sharing the same payload. - -The nine delivery handles remain distinct. Their logs and arrival metadata are -charged separately; this proves one canonical content store, not a constant -bound on all delivery bookkeeping or arbitrary caller-retained public Frames. -The bounded case also retains the live frame at 32 beside tail frames 1..16. - -| Payload/frame | End | Canonical rows | Unique payload | Shared charge | Delivery charge | Discovery charge | Total | -| --- | --- | --- | --- | --- | --- | --- | --- | -| 0 B | Unbounded | 32 | 0 B | 10368 B | 34272 B | 136 B | 44776 B | -| 0 B | Exclusive 17 | 17 | 0 B | 5688 B | 22272 B | 136 B | 28096 B | -| 1 B | Unbounded | 32 | 32 B | 10400 B | 34272 B | 136 B | 44808 B | -| 1 B | Exclusive 17 | 17 | 17 B | 5705 B | 22272 B | 136 B | 28113 B | -| 1024 B | Unbounded | 32 | 32768 B | 43136 B | 34272 B | 136 B | 77544 B | -| 1024 B | Exclusive 17 | 17 | 17408 B | 23096 B | 22272 B | 136 B | 45504 B | - -On this x86_64 build the shared frame content allocation is 184 B, a progress -view is 56 B, and admission bookkeeping is 32 B. The empty-payload cases expose -the metadata independently of payload savings. These are ownership and charge -measurements, not quiet-host timing or allocator-overhead claims. - -```sh -nix develop --command just rs test -p moq-net --lib \ - -E 'test(model::frame::tests) | test(model::group::test) | test(model::track::test)' \ - --no-fail-fast --success-output immediate -``` - -Raw output: `.tmp/lazy-content-model-tests-2.log`, SHA256 -`76e5c37663d9529589e0119fa817cf3c064cfd4cc436a099f56bf6055bdccbda`. -`just fix` also passed. These focused results precede this checkpoint's final -integration gates; the full CI and interop pass on 5796 remain separate evidence. - -The Goal remains unfinished pending typed sparse/relay and compressed raw/plain -outer preservation controls, actual-main prerequisite integration, final clean -committed-head gates and real surface proof. Actual compressed transport ingress -must share stored raw slices as received, with one lazy inflated copy. Local -plaintext-to-DEFLATE producer scratch is a distinct representation; it is not -claimed to share raw wire storage. Compressed transport fetches must continue -widening to start 0/end None, and a plain reader without the prefix must still -refuse. No public API, wire, resource-budget or maintainer-policy choice has been -selected by this checkpoint. - -### Storage-lifetime preservation checkpoint - -The later implementation supersedes the 2420 prototype's ownership details. -Canonical frame storage now owns its group identity, while the group's index -holds weak frame records. Completed delivery entries retain shared frame owners; -their owner cell can be populated during activation without replacing payloads -that lock-free readers already see. A streamed reader keeps the same identity -through its existing buffer. Last storage release removes its weak record, so -neither retired entries nor frame readers can hide retained bytes from a refill. -The index retains no dead weak history and introduces no new union-wide limits. -Existing per-delivery limits remain enforced. - -A deterministic evicted-streaming-reader control first failed because its weak -group identity vanished while its frame buffer still existed. It now passes, -reusing the same physical payload on refill while the old reader still returns -`Evicted`. A separate control accepts an existing valid 8192-frame tail and a -disjoint valid one-frame prefix, preserving their existing per-delivery limits. - -An independent review found that a captured inflater job could outlive delivery -A's release when delivery B kept the shared Plain alive. The existing -`inflation_is_shared_and_stops_at_release` control was extended and reproduced -that failure. A compressed delivery now has its own release guard. Each inflation -frame and get synchronizes with that guard; releasing A waits for its current -frame and preserves A's error while B can still read and inflate. The guard's -metadata is charged to the active delivery. This changes neither the shared -inflation representation nor the existing caller ownership of completed bytes. - -Typed object controls preserve IDs across different delivery ordinals, sealed -timestamps and delivery-local terminal metadata. The compressed ingress control -uses independent received buffers, verifies raw payload pointer reuse without -recompression, preserves the missing-prefix refusal and different arrival times, -inflates plain bytes once, and keeps start-0/end-None widening for repeated tail -requests. Local plaintext compression scratch remains a separate producer path. - -The full strict frame/group/track cohort passes 512/512. `just fix` passes. -Source includes the actually landed request and Path prerequisites through a -normal merge of main `c764acebb90b5c102a8ea90207dd125ae4fd326b`; no rebase. -The subsequent IETF version-removal integration and final full gates remain. - -The updated x86_64 metadata sizes are content 216 B, progress view 56 B and -admission bookkeeping 32 B. Ownership charges from the retained-reader control: - -| Payload/frame | End | Canonical rows | Unique payload | Shared charge | Delivery charge | Discovery charge | Total | -| --- | --- | --- | --- | --- | --- | --- | --- | -| 0 B | Unbounded | 32 | 0 B | 8304 B | 36448 B | 136 B | 44888 B | -| 0 B | Exclusive 17 | 17 | 0 B | 5064 B | 23296 B | 136 B | 28496 B | -| 1 B | Unbounded | 32 | 32 B | 8336 B | 36448 B | 136 B | 44920 B | -| 1 B | Exclusive 17 | 17 | 17 B | 5081 B | 23296 B | 136 B | 28513 B | -| 1024 B | Unbounded | 32 | 32768 B | 41072 B | 36448 B | 136 B | 77656 B | -| 1024 B | Exclusive 17 | 17 | 17408 B | 22472 B | 23296 B | 136 B | 45904 B | - -Raw outputs and SHA256: - -- Full focused model cohort: `.tmp/weak-owner-model-tests.log`, - `65b5eba4bd4a0c6d95536806469c8ede169cb5f6112ae11a2d638e094590fbf4`. -- Frame-reader negative: `.tmp/frame-reader-owner-before-2.log`, - `41b552539fe590464c5040fbc93e8fc0597214cb73bfaa630fce1124b06c7005`. -- Frame-reader/union positive: `.tmp/weak-frame-owner-controls.log`, - `13639d690f523102d340f5f4106f2b8ed9eb9a9e76184452bb470006cf716f4c`. -- Shared late-inflater negative: `.tmp/shared-late-inflate-before.log`, - `ad2524de1e81d2a5e01ee02707585401ae15d428653f21a40392e7d010452c99`. -- Late-inflater/typed/compressed positives: - `.tmp/canonical-representation-controls.log`, - `44a2868fc82a01871af38327cc13697450988df42c9c379bbc946796bd80bec1`. - -The Goal is still unfinished. Read-only review found a direct live Lite GROUP -path that creates a public whole group before applying the received frame start, -then resets the owner when replacing an earlier positive-start copy. The next -control must exercise actual GroupRecv with held readers and distinct tail -starts, followed by a private transport claim that knows the offset before -publication. Public explicit group recreation must retain its separate owner. -Ordinary pre-existing cross-route duplication is not itself evidence of this -specific tail-amplification bug. Final readiness still needs this direct ingress -proof, current-main integration, clean committed-head gates and the real surface. - - -### Live GROUP and intrinsic-ownership checkpoint - -The direct Lite ingress failure was reproduced through actual `GroupRecv` and -one subscriber entry: eight independent GROUP streams for the same sequence, -with starts 8 down to 1 and all earlier consumers held. Each stream received -fresh 1 KiB buffers. Before the correction, matching frame 8 had different -physical payload pointers. The private transport claim now receives the known -start before group publication, discovers retained canonical content before -replacing a Slot, and preserves public explicit recreation as a separate owner. -The same wire control now passes, including each consumer's bounds, payload and -FIN. No new route identity or Epoch-less cross-route sharing was introduced. - -The new inflater release guard also had a distinct accounting regression: a -captured job retained the guard after group release, but its charge was cleared -with the group. A deterministic negative control observed 0 B rather than the -live guard's 64 B. The guard now carries its own intrinsic `Extra`; releasing -the delivery preserves its error, and the final retained job/clone returns that -metadata charge. The earlier shared-Plain preservation control still passes. - -Both weak discovery indexes now use randomized `hashbrown` maps instead of -sorted vectors. The existing locked 0.17.1 release is a direct dependency with -default features disabled and `std::RandomState`; `allocation_size()` measures -the actual table allocation. Final owners remove their records immediately, -empty tables release their buckets, and replacing a sequence cannot let the old -owner remove the new incarnation. Frame-owned accounting events update a checked -aggregate, so group sizing neither allocates nor walks all retained rows. The -underlying intrinsic charges still end with the final physical owner. - -The pre-change benchmark used the actual canonical frame owners, 64 B frames, -1/64/1024/8192 frames and 1/8 held owner sets. Sizing runs on a constant fixture; -cleanup times actual final-owner destruction with setup outside the timed body. -It used 20 samples, 500 ms warm-up and 2 s measurement, on this 24-CPU x86_64 host -at 13:13:39–13:14:35 UTC; load averages were 0.64/2.05/3.47 before and -1.13/1.97/3.36 after. At 8192 frames, sizing took 69.856/69.493 µs and cleanup -12.734/13.046 ms for 1/8 owner sets. The largest cleanup case warned that the -fixed 20 samples needed more than the 2 s estimate; no sample count or case was -changed. Repeating the same cases after the source correction remains due. - -The strict frame/group/track/Lite cohort passes 606/606, including unordered -empty-frame final releases, replacement owner discovery, actual live GROUP, -sparse typed properties, raw/plain compression, independent errors/arrivals, -frame-reader lifetime and original versus bounded ends. This includes actual -main's IETF 14–17 removal through a normal merge of -`22808bbb2f4aab9fdaf9a60507f77efbaef4bd0d`. Full CI and interop on 5796 are -historical evidence only; this newer source still needs its final gates. - -Updated held-reader charges, with distinct per-delivery bookkeeping retained: - -| Payload/frame | End | Canonical rows | Unique payload | Shared charge | Delivery charge | Discovery charge | Total | -| --- | --- | --- | --- | --- | --- | --- | --- | -| 0 B | Unbounded | 32 | 0 B | 9696 B | 36448 B | 180 B | 46324 B | -| 0 B | Exclusive 17 | 17 | 0 B | 5144 B | 23296 B | 180 B | 28620 B | -| 1 B | Unbounded | 32 | 32 B | 9728 B | 36448 B | 180 B | 46356 B | -| 1 B | Exclusive 17 | 17 | 17 B | 5161 B | 23296 B | 180 B | 28637 B | -| 1024 B | Unbounded | 32 | 32768 B | 42464 B | 36448 B | 180 B | 79092 B | -| 1024 B | Exclusive 17 | 17 | 17408 B | 22552 B | 23296 B | 180 B | 46028 B | - -Raw evidence and SHA256: - -- Live GROUP negative: `.tmp/live-group-tail-before-2.log`, - `7490dc59ae51a8d719aa2f4dfc42cee989e4e874f9d3fe8f0ae7d49a6853d476`. -- Retained guard charge negative: `.tmp/inflater-guard-charge-before.log`, - `d7d891702abefc0bf34c7860a202c7b8ab406afa131d1e31a3c635abb331db9e`. -- Both focused positives: `.tmp/live-claim-guard-controls.log`, - `7e78fecf9113922de9e8d5162e030129b73c3d0e02ef053d42ee907dab0a5cf6`. -- Strict 606-control cohort: `.tmp/canonical-map-focused.log`, - `a46a4bfaa8928eef9082f55e175bde24a610aad4de233f9ca90a94ccb735dc05`. -- Before benchmark: `.tmp/canonical-bench.log`, - `0fe6d7d6dc4bbcf6a6d05a659cb8e72b0f366c929944a92465a732c0eb3c84f5`. - -No public API, wire field, resource budget, arrival policy or compressed -representation was changed. The Goal remains unfinished until the after -measurement, current-source committed-head gates and real surface proof finish. - - -### Measured weak-index correction - -The identical single after-run completed 13:37:58–13:38:45 UTC on the same -24-CPU x86_64 host, after the other worker's CI became terminal. No heavy job -ran concurrently. Load averages were 3.58/5.89/5.86 before and -2.51/5.24/5.64 after, higher than the earlier baseline. This is a bounded local -comparison, not a general throughput or quiet-host performance guarantee. -The fixed cases and measurement settings were unchanged; no threshold was tuned. - -| Frames × held owner sets | Sizing before | Sizing after | Cleanup before | Cleanup after | -| --- | --- | --- | --- | --- | -| 1 × 1 | 25.912 ns | 8.9837 ns | 230.16 ns | 252.76 ns | -| 1 × 8 | 25.855 ns | 9.0134 ns | 263.43 ns | 285.32 ns | -| 64 × 1 | 599.47 ns | 9.065 ns | 5.6539 µs | 6.4272 µs | -| 64 × 8 | 606.83 ns | 8.9804 ns | 7.3737 µs | 8.1054 µs | -| 1024 × 1 | 8.7272 µs | 9.0952 ns | 165.32 µs | 108.94 µs | -| 1024 × 8 | 8.6809 µs | 8.9223 ns | 191.46 µs | 134.89 µs | -| 8192 × 1 | 69.856 µs | 8.9981 ns | 12.734 ms | 887.63 µs | -| 8192 × 8 | 69.493 µs | 8.9692 ns | 13.046 ms | 1.1143 ms | - -Small-group cleanup is modestly slower, while sizing is constant work and final -cleanup no longer repeatedly shifts retained rows. Exact hash-table capacity is -still charged, so a table may retain spare buckets until its last row drops. -Empty tables free those buckets immediately. The same map and accounting helper -serve the group discovery index; neither retains dead weak history. - -```sh -nix develop --command cargo bench --locked -p moq-net --features fuzz \ - --bench canonical -- --noplot -``` - -After output: `.tmp/canonical-map-bench.log`, SHA256 -`19ca1cca45bb37756cd9143e051e00b061796edd549a3579bbb20534d546e660`. -Conditions: `.tmp/canonical-map-bench-conditions.json`, SHA256 -`2a91de696ca6eaf5d594742e15ee4045e97fe2add5533a67078beb2545d861fd`. -The stable full CI and real surface gates remain due on the final committed head. diff --git a/quest/m1/unbounded-groups/front-eviction.md b/quest/m1/unbounded-groups/front-eviction.md index 7a7122d79cb..bf5a908f031 100644 --- a/quest/m1/unbounded-groups/front-eviction.md +++ b/quest/m1/unbounded-groups/front-eviction.md @@ -108,7 +108,3 @@ See the [epic](/quest/m1/unbounded-groups/README.md) for the decisions. Public API: removes `MAX_GROUP_FRAMES`, and `MAX_CACHE_BYTES` stops capping groups. A reader that used to see `GroupTooLarge` now refills or sees `Evicted`. Wire: none beyond the amended frame offset rule. - -## Required - -- [Transport frame offsets](/quest/m1/transport-frame-offsets.md) - the lite draft lets a refill start at the missing frame without holding the frames below it From 21a64960f91a7edbec0e821927b567aa89f2c03a Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 27 Oct 2026 06:47:01 -0700 Subject: [PATCH 07/16] Align dependent plans with transport frame ranges Co-Authored-By: GPT-6 --- quest/m1/time-addressing/lite07.md | 3 +-- quest/m1/unbounded-groups/README.md | 5 ++--- quest/m1/unbounded-groups/front-eviction.md | 14 +++++--------- 3 files changed, 8 insertions(+), 14 deletions(-) diff --git a/quest/m1/time-addressing/lite07.md b/quest/m1/time-addressing/lite07.md index 22e11e9ba54..c2721ca950a 100644 --- a/quest/m1/time-addressing/lite07.md +++ b/quest/m1/time-addressing/lite07.md @@ -23,8 +23,7 @@ Decided 2026-10-25 (maintainer), from the spike: SUBSCRIBE_END, SUBSCRIBE_DROP, and datagrams address by start. Drops name time spans with no group to deliver, not sequence ranges. `Relative(n)` stays a group count. -- Positions stay (start, frame index), so - [frame offsets](/quest/m1/transport-frame-offsets.md) carry over. +- Positions stay (start, frame index), preserving transport frame offsets. Update `drafts/draft-lcurley-moq-lite.md` (fields, the group-end rules, and the lite-07 changelog) and `doc/concept/moq-lite.md`, and run `just drafts check` diff --git a/quest/m1/unbounded-groups/README.md b/quest/m1/unbounded-groups/README.md index 946e2155e85..525c2dbcb93 100644 --- a/quest/m1/unbounded-groups/README.md +++ b/quest/m1/unbounded-groups/README.md @@ -90,9 +90,8 @@ Decided 2026-10-16 (maintainer interview): - `GroupTooLarge` and `FrameTooLarge` stay, raised only when an allocation can't be satisfied. - The byte offset goes on the next unpublished lite version after lite-07, - not lite-07. Frame-boundary holes refill over lite-07's frame offsets once - [transport frame offsets](/quest/m1/transport-frame-offsets.md) lets a - refill start at a frame the relay no longer holds below (decided + not lite-07. Frame-boundary holes refill over lite-07's transport frame + offsets, which allow a refill to start without the evicted prefix (decided 2026-10-25, maintainer: the application API stays whole-group). - JS is a separate quest. Docs change inline: the lite draft, `doc/concept`, the relay cache docs, the JSON and moq-net compression docs, and the e2ee diff --git a/quest/m1/unbounded-groups/front-eviction.md b/quest/m1/unbounded-groups/front-eviction.md index bf5a908f031..c4a23db686e 100644 --- a/quest/m1/unbounded-groups/front-eviction.md +++ b/quest/m1/unbounded-groups/front-eviction.md @@ -39,18 +39,14 @@ See the [epic](/quest/m1/unbounded-groups/README.md) for the decisions. single request, as `FillState` does. Fold today's `Slot.head` tail copy into this. - A refill asks for exactly the hole: from the reader's missing frame to the - first frame still cached, over a frame-precise range end. Today's fills - always run to the group's end (`end_frame: None`), which on a live group - would duplicate the live edge. If more is evicted while the refill runs, + first frame still cached, using the transport fill's private frame-precise + range end instead of duplicating the live edge. If more is evicted while the refill runs, the reader hits the next hole and refills again, one RTT per hole. - Decided 2026-10-25 (maintainer): the application API stays whole-group, but the transport may start a group at a frame offset as an optimization, - and a front-eviction refill is one. The lite draft's rule that a - subscriber "MUST send 0 unless it already holds every frame below" forbids - this refill, since the relay evicted the frames below the hole, so - [transport frame offsets](/quest/m1/transport-frame-offsets.md) amends it - first. A relay asked for frame k of a group it holds nothing of widens to - frame 0 upstream. + and a front-eviction refill is one. The lite draft allows that start + without holding the prefix. A relay asked for frame k of a group it holds + nothing of widens to frame 0 upstream. - Refills are unpaced (decided 2026-10-24, maintainer). Pacing a refill on its fastest reader, and shared "trains" of readers that merge into the run ahead, were rejected as not worth their complexity. `FillState` From 761c1981f2cad13373082cb751f3c3eceeafd2a3 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 27 Oct 2026 07:01:37 -0700 Subject: [PATCH 08/16] Preserve transport storage controls across feature shapes Co-Authored-By: GPT-6 --- quest/m1/README.md | 1 + quest/m1/transport-frame-offsets.md | 468 ++++++++++++++++++++ quest/m1/unbounded-groups/front-eviction.md | 4 + rs/moq-net/src/model/group.rs | 3 +- rs/moq-net/src/model/group/content.rs | 4 +- rs/moq-net/src/model/track.rs | 1 + 6 files changed, 477 insertions(+), 4 deletions(-) create mode 100644 quest/m1/transport-frame-offsets.md diff --git a/quest/m1/README.md b/quest/m1/README.md index 81cde26d1de..eba86c17ded 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -124,6 +124,7 @@ surface and zero-copy texture import, land before the cut. - [Lite-05/06 timestamps pass through](/quest/m1/lite-timestamps-through.md) - a timed track received over lite-05/06 stays timed and is forwarded with its timestamps over every version - [Exact timestamps in every binding](/quest/m1/binding-timestamps.md) - bindings carry `{value, scale}` timestamps instead of lossy microseconds - [Announcement takeover](/quest/m1/announce-takeover.md) - a new independent route explicitly continues instances served through its predecessor, with shared Rust/JS semantics and lite-07 wire +- [The transport may start a group mid-way](/quest/m1/transport-frame-offsets.md) - a hop may request and deliver a group from any frame it needs, while applications still ask for whole groups - [Subscribe ranges](/quest/m1/subscribe-ranges/README.md) - a lite-07 SUBSCRIBE asks for past and live ranges with absolute or relative ends, in either order, and replaces FETCH; merged ranges never starve a subscriber; relays fill misses by range, including over moq-transport - [Drop set](/quest/m1/drop-set.md) - each cursor judges missing groups against its own budget, so a narrow reader drops promptly and a widening re-offers groups already reported dropped on lite-07 - [Broadcast archives](/quest/m1/archive/README.md) - ANNOUNCE, TRACKS, and a SUBSCRIBE per track record every track under a prefix to any object_store, a host serves a stored broadcast back, and the catalog drops its archive pointer for an opt-in `hls` field diff --git a/quest/m1/transport-frame-offsets.md b/quest/m1/transport-frame-offsets.md new file mode 100644 index 00000000000..d509595467c --- /dev/null +++ b/quest/m1/transport-frame-offsets.md @@ -0,0 +1,468 @@ +# [M] The transport may start a group mid-way + +## Goal + +A moq-lite hop may request and deliver a group starting at any frame k the +requester needs, whether or not it still holds the frames below k (route +continuation, hole fills, and front-eviction refills after the front was +evicted, with a frame-precise end), so a relay never refetches frames it does +not need. Applications still only ask for whole groups: no public API +starts at a frame. Rust and `@moq/net` both accept and cache such a tail. + +## Plan + +Decided 2026-10-25 (maintainer): remove frame-N starts from the API, as +[moq-dev/moq#6465](https://github.com/moq-dev/moq/pull/6465) did, but allow +them in the transport as an optimization. + +- Draft: amend lite-07-wip so a subscriber may send any nonzero frame start, + replacing "MUST send 0 unless it already holds every frame below this + index". A front-eviction refill needs frames k and up after the frames + below k were evicted, so holding the prefix cannot be the condition. Keep + the `Frame End` rule. +- Widening is relay policy, not a wire rule: a relay asked for frame k of a + group it holds none of widens the upstream request to frame 0 and cuts + locally, so the cache stays usable by + whole-group readers and a compressed group can inflate. +- A partial copy is the same group, never a new identity: `covering_group` + already serves only copies that cover the start. A copy cut short at the back + is marked incomplete so it never answers a whole-group fetch. +- JS accepts a GROUP with `frameStart > 0` (numbering from it, as Rust's + `group::Producer::start_at` does) instead of refusing it as a protocol + violation. +- A moq-transport stream that starts mid-group with no head is still dropped + (2026-10-22 decision, reaffirmed 2026-10-25). +- The rule in `rs/moq-net/AGENTS.md` that banned partial groups was deleted + 2026-10-25. + +Bound the cost a hostile peer can create: many distinct tail starts for one +group must not multiply its copies; coalesce to one copy per group. + +Public API: none. Wire: lite-07-wip draft text only; the fields exist. + +## Related + +- [Front eviction](/quest/m1/unbounded-groups/front-eviction.md) - refills a front hole with a frame-precise range + +## Verification remaining + +The private implementation, current-main integration and surface controls are +retained in [moq-dev/moq#6922](https://github.com/moq-dev/moq/pull/6922). The full +interop matrix and native Chromium offsets 0/5/9000 passed on clean source head +`21a64960f91a7edbec0e821927b567aa89f2c03a`. That head's strict CI failed on three +real no-default-feature boundaries and on the runner-image host restriction. +The feature boundaries are corrected without warnings allowances or coverage +removal: benchmark support follows its async fuzz/test consumer, transport-only +claiming follows async, and the synchronous inflater helper also runs without +default features. Strict no-default retained-job/index controls pass 2/2. +Enabled default production code is unchanged from the passed full matrix. + +The remaining prerequisite is a complete strict `just ci origin/main` pass on +a builder that can create the runner image's user namespace. On this host, +`systemd-repart` fails with `unshare: write failed /proc/self/uid_map: Operation +not permitted`. Both CI and one isolated build of the exact failed derivation +were already run with approved escalated execution; this is not the tool's +ordinary workspace sandbox. No flags, profiles, Nix sandbox configuration or +host policy were changed. Open PR searches for uid_map, userns and runner fixes +found none. Actual base `38bc872f1dec5ece74e07f43b508443d8a300378` evaluates the +same runner-image check derivation as this source: +`/nix/store/0gii4wssls74069axylcnmv5dp377pp2-vm-test-run-github-runner-vm-image.drv`. +The failing nested image derivation is +`/nix/store/bbsr6sl5q70a1kxkz4s412dhjic9h46v-github-runner.drv`. + +Do not waive the required check, rerun the unchanged isolated build to seek +green, patch unrelated runner infrastructure here, or mark this draft ready +while the prerequisite remains. Finish by running the unchanged required gate +on a capable approved builder, addressing any actual findings, then deleting +this quest and its links and completing normal review. No public API, wire, +resource-budget or arrival-policy decision remains. + +Portable implementation/cost evidence is retained below. Local raw outputs: +`.tmp/final-ci.log`, `.tmp/final-interop.log`, `.tmp/final-offset-browser.log`, +`.tmp/no-default-cfg-controls.log`, `.tmp/uid-map-isolated-lane.log` and +`.tmp/base-runner-image-drv.log`. The latest full CI result in the PR body +supersedes the historical checkpoints below. + +### Historical implementation checkpoint + +[moq-dev/moq#6922](https://github.com/moq-dev/moq/pull/6922) implements private +JS offset initialization before publication, exact forwarding/cache coverage, +and private Rust frame-end provenance. Public APIs and existing wire fields +remain unchanged. The full Goal is unfinished: retaining one cached head does +not bound storage held by earlier readers. The selected one-copy bound still +applies; no weaker ownership or resource policy has been selected. + +Before fixing, the timed JS GROUP control passed at frame 0 and refused frame +5 and 9000 with PROTOCOL_VIOLATION. The actual Rust FetchServe control queued +start 2 for a cold request with start 2/end 3, rather than widening to 0. +Those controls now pass. Focused JS checks cover early/late mirrors, retained +frame limits, missing compression prefix, exact tail forwarding, and the +unchanged declared-end violation. Fifteen focused Rust controls pass, including +bounded-end cache misses, queued/taken range widening, and live-copy preservation. +These are focused checks, not final-head CI or full interop evidence. + +A small deterministic held-reader control demonstrates the remaining cost in +both the inherited unbounded-end path and the bounded-end path. Eight tails +start at frames 8 through 1, with identical bytes for every matching frame +index, beside a live copy starting at 32. Each reader retains its tail unread. +The unbounded copies all end at 33; the bounded copies all end at 17. + +| End | Distinct tail stores + live | Slot handles | Tail payload retained | Unique tail payload | Pool charge before/after identity-probe drop | After reader drop | +| --- | --- | --- | --- | --- | --- | --- | +| Unbounded | 8 + 1 | 2 | 233472 B | 32768 B | 266376 B / 266376 B | 40864 B | +| Exclusive 17 | 8 + 1 | 2 | 102400 B | 16384 B | 124456 B / 124456 B | 22144 B | + +The live baseline charge is 2216 B. Distinct state channels, overlapping payloads, +and unchanged charge after identity probes are dropped establish reader-held +multiplication; Slot ownership alone does not prove the Goal. Existing +`Head::adopt_head` shares payload references but only joins a head that reaches +the target offset, refusing sparse gaps. The bounded copies ending at 17 do not +reach the live copy at 32. Coalescing must preserve existing public reader +outcomes, completion/error ownership, compression, and cache accounting. + +The diagnostic below was inserted into `model::track::test`, run once through +the normal runner, then removed from executable source. It remains here so the +proof survives local scratch cleanup. The source and raw output are retained +under this worktree's `.tmp/held-tail-cost.rs` and +`.tmp/held-tail-overlap-cost.log`. It measures correctness and ownership, not +host performance or a new acceptance budget. SHA256 source: +`ea08934f00d65cb19443c9f0c90eec824ff6e2abc0fa347c587aef2564e33a76`; +raw output: `3c9efaf8a72a0a23e2f01c536e61a3ab0e5e4e52a35dd028eb6d73c7081c9eb9`. + +```sh +nix develop --command just rs test -p moq-net --lib \ + -E 'test(=model::track::test::diagnostic_held_tail_storage)' \ + --success-output immediate +``` + +```rust + #[moq_clock::test] + #[cfg(feature = "async")] + async fn diagnostic_held_tail_storage() { + for bounded in [false, true] { + let producer = track_producer("test", Info::timed()); + let pool = producer.broadcast.pool.clone(); + let dynamic = producer.dynamic(); + let consumer = producer.consume(); + let mut live = producer.create_group(group::Info { sequence: 0 }).unwrap(); + live.start_at(32).unwrap(); + live.write_frame(Timestamp::ZERO, vec![32; 1024]).unwrap(); + let baseline = pool.used(); + let mut held = Vec::new(); + let mut identities = Vec::new(); + for start in (1..=8).rev() { + let options = group::Fetch { frame_start: start, ..Default::default() }; + let fetch = if bounded { + consumer.fetch_range(0, FetchOptions { options, end: Some(17) }) + } else { + consumer.fetch_group(0, options) + }; + let request = dynamic.requested_fill_group().await.unwrap(); + let mut tail = request.accept(Info::timed()).unwrap(); + for frame in start..if bounded { 17 } else { 33 } { + tail.write_frame(Timestamp::ZERO, vec![frame as u8; 1024]).unwrap(); + } + tail.finish().unwrap(); + identities.push(tail.handle()); + held.push(fetch.await.unwrap()); + } + for (i, left) in identities.iter().enumerate() { + for right in &identities[i + 1..] { assert!(!left.is_clone(right)); } + assert!(!left.is_clone(&live.handle())); + } + let before_identity_drop = pool.used(); + drop(identities); + let only_readers_and_slot = pool.used(); + let cached_handles = producer.state.read().lookup[&0].copies().count(); + eprintln!("HELD_TAIL_COST bounded={bounded} requests=8 unique_tail_storages=8 live_storages=1 slot_handles={cached_handles} unread_payload={} unique_payload={} baseline_charge={baseline} charge_with_identities={before_identity_drop} charge_after_identity_drop={only_readers_and_slot}", if bounded { 100*1024 } else { 228*1024 }, if bounded { 16*1024 } else { 32*1024 }); + for (i, reader) in held.iter_mut().enumerate() { + let frame = reader.read_frame().await.unwrap().unwrap(); + assert_eq!(frame.payload.len(), 1024); + assert!(frame.payload.iter().all(|byte| *byte == (8-i) as u8)); + for expected in (9-i)..if bounded { 17 } else { 33 } { + let frame = reader.read_frame().await.unwrap().unwrap(); + assert!(frame.payload.iter().all(|byte| *byte == expected as u8)); + } + } + drop(held); + eprintln!("HELD_TAIL_COST bounded={bounded} charge_after_reader_drop={}", pool.used()); + live.abort(Error::Cancel).unwrap(); + } + } +``` + +### Canonical-owner prototype checkpoint + +The private Rust prototype activates an indexed content owner when a second +transport delivery would coexist with the first. Ordinary single-copy whole +and batch writes retain their existing representation. Activation imports the +existing payloads without copying and attaches the exact in-flight `FrameBuf`, +so an already-streaming reader and writer keep their progress. New deliveries +fork their own received counter while sharing immutable content, publication +and the origin's admission. Contradictory bytes use `ProtocolViolation`. + +A `TrackState`-scoped weak index lets a refill discover an owner still retained +by older tails after its Slot was removed. Owners remove their weak entries; +the index is owned by the live owners, so dead history does not accumulate. +Explicit live-group recreation replaces the sequence's indexed incarnation. +A different track instance does not inherit it. Delivery logs, bounds, FIN, +errors, timestamps and public arrival records remain separate. IETF Object IDs +have their own key space instead of being mistaken for dense frame ordinals. + +Intrinsic `FrameBuf` charges follow the shared storage and each progress view, +including streamed readers that outlive the delivery's partial slot. A +payload-only `Charge::transfer` moves old bytes without recording them as new +writes. Fresh attachment and already-canonical transfer have controls. Deferred +log entries hold weak row handles; a non-polling cursor cannot keep a canonical +owner alive through retired log storage. Handoff heads retain the content owner. + +The first eager prototype exposed real ownership and accounting integration +failures: 466/491 existing group/track tests passed initially, then 482/491 +after ownership corrections. Its sources and raw results remain in +`.tmp/eager-content-prototype/`, `.tmp/content-model-tests.log` and +`.tmp/content-model-tests-2.log`. It was replaced by lazy activation rather than +relaxing cache-pressure thresholds. Two refill fixtures had different bytes for +the same fixed incarnation; only those payloads were made consistent, preserving +the bounds, lifetime and end-refusal assertions. + +The lazy implementation passes 508/508 strict frame, group and track controls, +including the following new outer controls: + +- Eight distinct tail starts with all earlier readers held, at 0, 1 and 1024 B + per frame, both bounded and unbounded. Matching nonempty frame payloads have + the same physical pointer. Dropping identity probes changes no charge. +- Actual Slot removal with an older reader-held tail, followed by a refill + sharing its owner; explicit live recreation accepts different bytes and uses + a separate owner. +- Activation while the original frame and streaming reader are already open. + Overlap credits no progress to that delivery. One abort leaves the other + producer valid and admitted. Two completions retain distinct arrival times + 10 ms apart under the deterministic clock while sharing the same payload. + +The nine delivery handles remain distinct. Their logs and arrival metadata are +charged separately; this proves one canonical content store, not a constant +bound on all delivery bookkeeping or arbitrary caller-retained public Frames. +The bounded case also retains the live frame at 32 beside tail frames 1..16. + +| Payload/frame | End | Canonical rows | Unique payload | Shared charge | Delivery charge | Discovery charge | Total | +| --- | --- | --- | --- | --- | --- | --- | --- | +| 0 B | Unbounded | 32 | 0 B | 10368 B | 34272 B | 136 B | 44776 B | +| 0 B | Exclusive 17 | 17 | 0 B | 5688 B | 22272 B | 136 B | 28096 B | +| 1 B | Unbounded | 32 | 32 B | 10400 B | 34272 B | 136 B | 44808 B | +| 1 B | Exclusive 17 | 17 | 17 B | 5705 B | 22272 B | 136 B | 28113 B | +| 1024 B | Unbounded | 32 | 32768 B | 43136 B | 34272 B | 136 B | 77544 B | +| 1024 B | Exclusive 17 | 17 | 17408 B | 23096 B | 22272 B | 136 B | 45504 B | + +On this x86_64 build the shared frame content allocation is 184 B, a progress +view is 56 B, and admission bookkeeping is 32 B. The empty-payload cases expose +the metadata independently of payload savings. These are ownership and charge +measurements, not quiet-host timing or allocator-overhead claims. + +```sh +nix develop --command just rs test -p moq-net --lib \ + -E 'test(model::frame::tests) | test(model::group::test) | test(model::track::test)' \ + --no-fail-fast --success-output immediate +``` + +Raw output: `.tmp/lazy-content-model-tests-2.log`, SHA256 +`76e5c37663d9529589e0119fa817cf3c064cfd4cc436a099f56bf6055bdccbda`. +`just fix` also passed. These focused results precede this checkpoint's final +integration gates; the full CI and interop pass on 5796 remain separate evidence. + +The Goal remains unfinished pending typed sparse/relay and compressed raw/plain +outer preservation controls, actual-main prerequisite integration, final clean +committed-head gates and real surface proof. Actual compressed transport ingress +must share stored raw slices as received, with one lazy inflated copy. Local +plaintext-to-DEFLATE producer scratch is a distinct representation; it is not +claimed to share raw wire storage. Compressed transport fetches must continue +widening to start 0/end None, and a plain reader without the prefix must still +refuse. No public API, wire, resource-budget or maintainer-policy choice has been +selected by this checkpoint. + +### Storage-lifetime preservation checkpoint + +The later implementation supersedes the 2420 prototype's ownership details. +Canonical frame storage now owns its group identity, while the group's index +holds weak frame records. Completed delivery entries retain shared frame owners; +their owner cell can be populated during activation without replacing payloads +that lock-free readers already see. A streamed reader keeps the same identity +through its existing buffer. Last storage release removes its weak record, so +neither retired entries nor frame readers can hide retained bytes from a refill. +The index retains no dead weak history and introduces no new union-wide limits. +Existing per-delivery limits remain enforced. + +A deterministic evicted-streaming-reader control first failed because its weak +group identity vanished while its frame buffer still existed. It now passes, +reusing the same physical payload on refill while the old reader still returns +`Evicted`. A separate control accepts an existing valid 8192-frame tail and a +disjoint valid one-frame prefix, preserving their existing per-delivery limits. + +An independent review found that a captured inflater job could outlive delivery +A's release when delivery B kept the shared Plain alive. The existing +`inflation_is_shared_and_stops_at_release` control was extended and reproduced +that failure. A compressed delivery now has its own release guard. Each inflation +frame and get synchronizes with that guard; releasing A waits for its current +frame and preserves A's error while B can still read and inflate. The guard's +metadata is charged to the active delivery. This changes neither the shared +inflation representation nor the existing caller ownership of completed bytes. + +Typed object controls preserve IDs across different delivery ordinals, sealed +timestamps and delivery-local terminal metadata. The compressed ingress control +uses independent received buffers, verifies raw payload pointer reuse without +recompression, preserves the missing-prefix refusal and different arrival times, +inflates plain bytes once, and keeps start-0/end-None widening for repeated tail +requests. Local plaintext compression scratch remains a separate producer path. + +The full strict frame/group/track cohort passes 512/512. `just fix` passes. +Source includes the actually landed request and Path prerequisites through a +normal merge of main `c764acebb90b5c102a8ea90207dd125ae4fd326b`; no rebase. +The subsequent IETF version-removal integration and final full gates remain. + +The updated x86_64 metadata sizes are content 216 B, progress view 56 B and +admission bookkeeping 32 B. Ownership charges from the retained-reader control: + +| Payload/frame | End | Canonical rows | Unique payload | Shared charge | Delivery charge | Discovery charge | Total | +| --- | --- | --- | --- | --- | --- | --- | --- | +| 0 B | Unbounded | 32 | 0 B | 8304 B | 36448 B | 136 B | 44888 B | +| 0 B | Exclusive 17 | 17 | 0 B | 5064 B | 23296 B | 136 B | 28496 B | +| 1 B | Unbounded | 32 | 32 B | 8336 B | 36448 B | 136 B | 44920 B | +| 1 B | Exclusive 17 | 17 | 17 B | 5081 B | 23296 B | 136 B | 28513 B | +| 1024 B | Unbounded | 32 | 32768 B | 41072 B | 36448 B | 136 B | 77656 B | +| 1024 B | Exclusive 17 | 17 | 17408 B | 22472 B | 23296 B | 136 B | 45904 B | + +Raw outputs and SHA256: + +- Full focused model cohort: `.tmp/weak-owner-model-tests.log`, + `65b5eba4bd4a0c6d95536806469c8ede169cb5f6112ae11a2d638e094590fbf4`. +- Frame-reader negative: `.tmp/frame-reader-owner-before-2.log`, + `41b552539fe590464c5040fbc93e8fc0597214cb73bfaa630fce1124b06c7005`. +- Frame-reader/union positive: `.tmp/weak-frame-owner-controls.log`, + `13639d690f523102d340f5f4106f2b8ed9eb9a9e76184452bb470006cf716f4c`. +- Shared late-inflater negative: `.tmp/shared-late-inflate-before.log`, + `ad2524de1e81d2a5e01ee02707585401ae15d428653f21a40392e7d010452c99`. +- Late-inflater/typed/compressed positives: + `.tmp/canonical-representation-controls.log`, + `44a2868fc82a01871af38327cc13697450988df42c9c379bbc946796bd80bec1`. + +The Goal is still unfinished. Read-only review found a direct live Lite GROUP +path that creates a public whole group before applying the received frame start, +then resets the owner when replacing an earlier positive-start copy. The next +control must exercise actual GroupRecv with held readers and distinct tail +starts, followed by a private transport claim that knows the offset before +publication. Public explicit group recreation must retain its separate owner. +Ordinary pre-existing cross-route duplication is not itself evidence of this +specific tail-amplification bug. Final readiness still needs this direct ingress +proof, current-main integration, clean committed-head gates and the real surface. + + +### Live GROUP and intrinsic-ownership checkpoint + +The direct Lite ingress failure was reproduced through actual `GroupRecv` and +one subscriber entry: eight independent GROUP streams for the same sequence, +with starts 8 down to 1 and all earlier consumers held. Each stream received +fresh 1 KiB buffers. Before the correction, matching frame 8 had different +physical payload pointers. The private transport claim now receives the known +start before group publication, discovers retained canonical content before +replacing a Slot, and preserves public explicit recreation as a separate owner. +The same wire control now passes, including each consumer's bounds, payload and +FIN. No new route identity or Epoch-less cross-route sharing was introduced. + +The new inflater release guard also had a distinct accounting regression: a +captured job retained the guard after group release, but its charge was cleared +with the group. A deterministic negative control observed 0 B rather than the +live guard's 64 B. The guard now carries its own intrinsic `Extra`; releasing +the delivery preserves its error, and the final retained job/clone returns that +metadata charge. The earlier shared-Plain preservation control still passes. + +Both weak discovery indexes now use randomized `hashbrown` maps instead of +sorted vectors. The existing locked 0.17.1 release is a direct dependency with +default features disabled and `std::RandomState`; `allocation_size()` measures +the actual table allocation. Final owners remove their records immediately, +empty tables release their buckets, and replacing a sequence cannot let the old +owner remove the new incarnation. Frame-owned accounting events update a checked +aggregate, so group sizing neither allocates nor walks all retained rows. The +underlying intrinsic charges still end with the final physical owner. + +The pre-change benchmark used the actual canonical frame owners, 64 B frames, +1/64/1024/8192 frames and 1/8 held owner sets. Sizing runs on a constant fixture; +cleanup times actual final-owner destruction with setup outside the timed body. +It used 20 samples, 500 ms warm-up and 2 s measurement, on this 24-CPU x86_64 host +at 13:13:39–13:14:35 UTC; load averages were 0.64/2.05/3.47 before and +1.13/1.97/3.36 after. At 8192 frames, sizing took 69.856/69.493 µs and cleanup +12.734/13.046 ms for 1/8 owner sets. The largest cleanup case warned that the +fixed 20 samples needed more than the 2 s estimate; no sample count or case was +changed. Repeating the same cases after the source correction remains due. + +The strict frame/group/track/Lite cohort passes 606/606, including unordered +empty-frame final releases, replacement owner discovery, actual live GROUP, +sparse typed properties, raw/plain compression, independent errors/arrivals, +frame-reader lifetime and original versus bounded ends. This includes actual +main's IETF 14–17 removal through a normal merge of +`22808bbb2f4aab9fdaf9a60507f77efbaef4bd0d`. Full CI and interop on 5796 are +historical evidence only; this newer source still needs its final gates. + +Updated held-reader charges, with distinct per-delivery bookkeeping retained: + +| Payload/frame | End | Canonical rows | Unique payload | Shared charge | Delivery charge | Discovery charge | Total | +| --- | --- | --- | --- | --- | --- | --- | --- | +| 0 B | Unbounded | 32 | 0 B | 9696 B | 36448 B | 180 B | 46324 B | +| 0 B | Exclusive 17 | 17 | 0 B | 5144 B | 23296 B | 180 B | 28620 B | +| 1 B | Unbounded | 32 | 32 B | 9728 B | 36448 B | 180 B | 46356 B | +| 1 B | Exclusive 17 | 17 | 17 B | 5161 B | 23296 B | 180 B | 28637 B | +| 1024 B | Unbounded | 32 | 32768 B | 42464 B | 36448 B | 180 B | 79092 B | +| 1024 B | Exclusive 17 | 17 | 17408 B | 22552 B | 23296 B | 180 B | 46028 B | + +Raw evidence and SHA256: + +- Live GROUP negative: `.tmp/live-group-tail-before-2.log`, + `7490dc59ae51a8d719aa2f4dfc42cee989e4e874f9d3fe8f0ae7d49a6853d476`. +- Retained guard charge negative: `.tmp/inflater-guard-charge-before.log`, + `d7d891702abefc0bf34c7860a202c7b8ab406afa131d1e31a3c635abb331db9e`. +- Both focused positives: `.tmp/live-claim-guard-controls.log`, + `7e78fecf9113922de9e8d5162e030129b73c3d0e02ef053d42ee907dab0a5cf6`. +- Strict 606-control cohort: `.tmp/canonical-map-focused.log`, + `a46a4bfaa8928eef9082f55e175bde24a610aad4de233f9ca90a94ccb735dc05`. +- Before benchmark: `.tmp/canonical-bench.log`, + `0fe6d7d6dc4bbcf6a6d05a659cb8e72b0f366c929944a92465a732c0eb3c84f5`. + +No public API, wire field, resource budget, arrival policy or compressed +representation was changed. The Goal remains unfinished until the after +measurement, current-source committed-head gates and real surface proof finish. + + +### Measured weak-index correction + +The identical single after-run completed 13:37:58–13:38:45 UTC on the same +24-CPU x86_64 host, after the other worker's CI became terminal. No heavy job +ran concurrently. Load averages were 3.58/5.89/5.86 before and +2.51/5.24/5.64 after, higher than the earlier baseline. This is a bounded local +comparison, not a general throughput or quiet-host performance guarantee. +The fixed cases and measurement settings were unchanged; no threshold was tuned. + +| Frames × held owner sets | Sizing before | Sizing after | Cleanup before | Cleanup after | +| --- | --- | --- | --- | --- | +| 1 × 1 | 25.912 ns | 8.9837 ns | 230.16 ns | 252.76 ns | +| 1 × 8 | 25.855 ns | 9.0134 ns | 263.43 ns | 285.32 ns | +| 64 × 1 | 599.47 ns | 9.065 ns | 5.6539 µs | 6.4272 µs | +| 64 × 8 | 606.83 ns | 8.9804 ns | 7.3737 µs | 8.1054 µs | +| 1024 × 1 | 8.7272 µs | 9.0952 ns | 165.32 µs | 108.94 µs | +| 1024 × 8 | 8.6809 µs | 8.9223 ns | 191.46 µs | 134.89 µs | +| 8192 × 1 | 69.856 µs | 8.9981 ns | 12.734 ms | 887.63 µs | +| 8192 × 8 | 69.493 µs | 8.9692 ns | 13.046 ms | 1.1143 ms | + +Small-group cleanup is modestly slower, while sizing is constant work and final +cleanup no longer repeatedly shifts retained rows. Exact hash-table capacity is +still charged, so a table may retain spare buckets until its last row drops. +Empty tables free those buckets immediately. The same map and accounting helper +serve the group discovery index; neither retains dead weak history. + +```sh +nix develop --command cargo bench --locked -p moq-net --features fuzz \ + --bench canonical -- --noplot +``` + +After output: `.tmp/canonical-map-bench.log`, SHA256 +`19ca1cca45bb37756cd9143e051e00b061796edd549a3579bbb20534d546e660`. +Conditions: `.tmp/canonical-map-bench-conditions.json`, SHA256 +`2a91de696ca6eaf5d594742e15ee4045e97fe2add5533a67078beb2545d861fd`. +The stable full CI and real surface gates remain due on the final committed head. diff --git a/quest/m1/unbounded-groups/front-eviction.md b/quest/m1/unbounded-groups/front-eviction.md index c4a23db686e..6059d84c53a 100644 --- a/quest/m1/unbounded-groups/front-eviction.md +++ b/quest/m1/unbounded-groups/front-eviction.md @@ -104,3 +104,7 @@ See the [epic](/quest/m1/unbounded-groups/README.md) for the decisions. Public API: removes `MAX_GROUP_FRAMES`, and `MAX_CACHE_BYTES` stops capping groups. A reader that used to see `GroupTooLarge` now refills or sees `Evicted`. Wire: none beyond the amended frame offset rule. + +## Required + +- [Transport frame offsets](/quest/m1/transport-frame-offsets.md) - the lite draft lets a refill start at the missing frame without holding the frames below it diff --git a/rs/moq-net/src/model/group.rs b/rs/moq-net/src/model/group.rs index b4248046368..fe42aa6ba01 100644 --- a/rs/moq-net/src/model/group.rs +++ b/rs/moq-net/src/model/group.rs @@ -20,7 +20,7 @@ use crate::cache; use crate::frame::{self, Cached, FrameBuf, Payload}; use crate::{Timescale, stats, track}; use bytes::Bytes; -#[cfg(any(test, feature = "fuzz"))] +#[cfg(all(feature = "async", any(test, feature = "fuzz")))] pub(crate) use content::StorageBench; pub(super) use content::{Content, Contents, Key as ContentKey}; use content::{Input, Spec}; @@ -5539,7 +5539,6 @@ mod test { } /// The slices a plain read up to local frame `last` takes under the group's guard. - #[cfg(feature = "async")] fn take_slices(group: &Producer, last: usize) -> Inflate { let state = group.handle.state.read(); state.inflate(state.flate.as_ref().unwrap(), last).unwrap() diff --git a/rs/moq-net/src/model/group/content.rs b/rs/moq-net/src/model/group/content.rs index af49b29c76e..4e0353744a6 100644 --- a/rs/moq-net/src/model/group/content.rs +++ b/rs/moq-net/src/model/group/content.rs @@ -284,14 +284,14 @@ impl Contents { } /// The actual weak-index/strong-frame ownership used by canonical cache accounting. -#[cfg(any(test, feature = "fuzz"))] +#[cfg(all(feature = "async", any(test, feature = "fuzz")))] pub(crate) struct StorageBench { content: Arc, _owners: Vec>, _charge: cache::Charge, } -#[cfg(any(test, feature = "fuzz"))] +#[cfg(all(feature = "async", any(test, feature = "fuzz")))] impl StorageBench { pub(crate) fn new(frames: usize, views: usize) -> Self { let account = cache::Track::new(cache::Pool::unbounded(), Default::default(), kio::Weak::new()); diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 619e063b018..136f8afabce 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -2876,6 +2876,7 @@ impl Producer { } /// A received copy starts at its wire offset before any reader can see it. + #[cfg(feature = "async")] pub(crate) fn create_group_at(&self, group: group::Info, start: u64) -> Result { self.create_group_inner(group, Some(start)) } From c1dea63ef4fbcc454b228c7177465c632556ca44 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 27 Oct 2026 07:08:34 -0700 Subject: [PATCH 09/16] Keep validation checkpoint within quest plan Co-Authored-By: GPT-6 --- quest/m1/transport-frame-offsets.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/quest/m1/transport-frame-offsets.md b/quest/m1/transport-frame-offsets.md index d509595467c..ee41c3281ae 100644 --- a/quest/m1/transport-frame-offsets.md +++ b/quest/m1/transport-frame-offsets.md @@ -40,11 +40,7 @@ group must not multiply its copies; coalesce to one copy per group. Public API: none. Wire: lite-07-wip draft text only; the fields exist. -## Related - -- [Front eviction](/quest/m1/unbounded-groups/front-eviction.md) - refills a front hole with a frame-precise range - -## Verification remaining +### Verification remaining The private implementation, current-main integration and surface controls are retained in [moq-dev/moq#6922](https://github.com/moq-dev/moq/pull/6922). The full @@ -466,3 +462,7 @@ After output: `.tmp/canonical-map-bench.log`, SHA256 Conditions: `.tmp/canonical-map-bench-conditions.json`, SHA256 `2a91de696ca6eaf5d594742e15ee4045e97fe2add5533a67078beb2545d861fd`. The stable full CI and real surface gates remain due on the final committed head. + +## Related + +- [Front eviction](/quest/m1/unbounded-groups/front-eviction.md) - refills a front hole with a frame-precise range From 340c911425b47db6756372ed406c7d6afca2712f Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 27 Oct 2026 13:55:18 -0700 Subject: [PATCH 10/16] quest: finish transport frame offsets The maintainer accepted the merge-queue gate in place of the local runner-image lane. Co-Authored-By: Claude Opus 5.5 --- quest/m1/README.md | 1 - quest/m1/transport-frame-offsets.md | 468 -------------------- quest/m1/unbounded-groups/front-eviction.md | 4 - 3 files changed, 473 deletions(-) delete mode 100644 quest/m1/transport-frame-offsets.md diff --git a/quest/m1/README.md b/quest/m1/README.md index 042df11f5c6..4cc08ef8b16 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -123,7 +123,6 @@ surface and zero-copy texture import, land before the cut. - [Lite-05/06 timestamps pass through](/quest/m1/lite-timestamps-through.md) - a timed track received over lite-05/06 stays timed and is forwarded with its timestamps over every version - [Exact timestamps in every binding](/quest/m1/binding-timestamps.md) - bindings carry `{value, scale}` timestamps instead of lossy microseconds - [Announcement takeover](/quest/m1/announce-takeover.md) - a new independent route explicitly continues instances served through its predecessor, with shared Rust/JS semantics and lite-07 wire -- [The transport may start a group mid-way](/quest/m1/transport-frame-offsets.md) - a hop may request and deliver a group from any frame it needs, while applications still ask for whole groups - [Subscribe ranges](/quest/m1/subscribe-ranges/README.md) - a lite-07 SUBSCRIBE asks for past and live ranges with absolute or relative ends, in either order, and replaces FETCH; merged ranges never starve a subscriber; relays fill misses by range, including over moq-transport - [Drop set](/quest/m1/drop-set.md) - each cursor judges missing groups against its own budget, so a narrow reader drops promptly and a widening re-offers groups already reported dropped on lite-07 - [Broadcast archives](/quest/m1/archive/README.md) - ANNOUNCE, TRACKS, and a SUBSCRIBE per track record every track under a prefix to any object_store, a host serves a stored broadcast back, and the catalog drops its archive pointer for an opt-in `hls` field diff --git a/quest/m1/transport-frame-offsets.md b/quest/m1/transport-frame-offsets.md deleted file mode 100644 index ee41c3281ae..00000000000 --- a/quest/m1/transport-frame-offsets.md +++ /dev/null @@ -1,468 +0,0 @@ -# [M] The transport may start a group mid-way - -## Goal - -A moq-lite hop may request and deliver a group starting at any frame k the -requester needs, whether or not it still holds the frames below k (route -continuation, hole fills, and front-eviction refills after the front was -evicted, with a frame-precise end), so a relay never refetches frames it does -not need. Applications still only ask for whole groups: no public API -starts at a frame. Rust and `@moq/net` both accept and cache such a tail. - -## Plan - -Decided 2026-10-25 (maintainer): remove frame-N starts from the API, as -[moq-dev/moq#6465](https://github.com/moq-dev/moq/pull/6465) did, but allow -them in the transport as an optimization. - -- Draft: amend lite-07-wip so a subscriber may send any nonzero frame start, - replacing "MUST send 0 unless it already holds every frame below this - index". A front-eviction refill needs frames k and up after the frames - below k were evicted, so holding the prefix cannot be the condition. Keep - the `Frame End` rule. -- Widening is relay policy, not a wire rule: a relay asked for frame k of a - group it holds none of widens the upstream request to frame 0 and cuts - locally, so the cache stays usable by - whole-group readers and a compressed group can inflate. -- A partial copy is the same group, never a new identity: `covering_group` - already serves only copies that cover the start. A copy cut short at the back - is marked incomplete so it never answers a whole-group fetch. -- JS accepts a GROUP with `frameStart > 0` (numbering from it, as Rust's - `group::Producer::start_at` does) instead of refusing it as a protocol - violation. -- A moq-transport stream that starts mid-group with no head is still dropped - (2026-10-22 decision, reaffirmed 2026-10-25). -- The rule in `rs/moq-net/AGENTS.md` that banned partial groups was deleted - 2026-10-25. - -Bound the cost a hostile peer can create: many distinct tail starts for one -group must not multiply its copies; coalesce to one copy per group. - -Public API: none. Wire: lite-07-wip draft text only; the fields exist. - -### Verification remaining - -The private implementation, current-main integration and surface controls are -retained in [moq-dev/moq#6922](https://github.com/moq-dev/moq/pull/6922). The full -interop matrix and native Chromium offsets 0/5/9000 passed on clean source head -`21a64960f91a7edbec0e821927b567aa89f2c03a`. That head's strict CI failed on three -real no-default-feature boundaries and on the runner-image host restriction. -The feature boundaries are corrected without warnings allowances or coverage -removal: benchmark support follows its async fuzz/test consumer, transport-only -claiming follows async, and the synchronous inflater helper also runs without -default features. Strict no-default retained-job/index controls pass 2/2. -Enabled default production code is unchanged from the passed full matrix. - -The remaining prerequisite is a complete strict `just ci origin/main` pass on -a builder that can create the runner image's user namespace. On this host, -`systemd-repart` fails with `unshare: write failed /proc/self/uid_map: Operation -not permitted`. Both CI and one isolated build of the exact failed derivation -were already run with approved escalated execution; this is not the tool's -ordinary workspace sandbox. No flags, profiles, Nix sandbox configuration or -host policy were changed. Open PR searches for uid_map, userns and runner fixes -found none. Actual base `38bc872f1dec5ece74e07f43b508443d8a300378` evaluates the -same runner-image check derivation as this source: -`/nix/store/0gii4wssls74069axylcnmv5dp377pp2-vm-test-run-github-runner-vm-image.drv`. -The failing nested image derivation is -`/nix/store/bbsr6sl5q70a1kxkz4s412dhjic9h46v-github-runner.drv`. - -Do not waive the required check, rerun the unchanged isolated build to seek -green, patch unrelated runner infrastructure here, or mark this draft ready -while the prerequisite remains. Finish by running the unchanged required gate -on a capable approved builder, addressing any actual findings, then deleting -this quest and its links and completing normal review. No public API, wire, -resource-budget or arrival-policy decision remains. - -Portable implementation/cost evidence is retained below. Local raw outputs: -`.tmp/final-ci.log`, `.tmp/final-interop.log`, `.tmp/final-offset-browser.log`, -`.tmp/no-default-cfg-controls.log`, `.tmp/uid-map-isolated-lane.log` and -`.tmp/base-runner-image-drv.log`. The latest full CI result in the PR body -supersedes the historical checkpoints below. - -### Historical implementation checkpoint - -[moq-dev/moq#6922](https://github.com/moq-dev/moq/pull/6922) implements private -JS offset initialization before publication, exact forwarding/cache coverage, -and private Rust frame-end provenance. Public APIs and existing wire fields -remain unchanged. The full Goal is unfinished: retaining one cached head does -not bound storage held by earlier readers. The selected one-copy bound still -applies; no weaker ownership or resource policy has been selected. - -Before fixing, the timed JS GROUP control passed at frame 0 and refused frame -5 and 9000 with PROTOCOL_VIOLATION. The actual Rust FetchServe control queued -start 2 for a cold request with start 2/end 3, rather than widening to 0. -Those controls now pass. Focused JS checks cover early/late mirrors, retained -frame limits, missing compression prefix, exact tail forwarding, and the -unchanged declared-end violation. Fifteen focused Rust controls pass, including -bounded-end cache misses, queued/taken range widening, and live-copy preservation. -These are focused checks, not final-head CI or full interop evidence. - -A small deterministic held-reader control demonstrates the remaining cost in -both the inherited unbounded-end path and the bounded-end path. Eight tails -start at frames 8 through 1, with identical bytes for every matching frame -index, beside a live copy starting at 32. Each reader retains its tail unread. -The unbounded copies all end at 33; the bounded copies all end at 17. - -| End | Distinct tail stores + live | Slot handles | Tail payload retained | Unique tail payload | Pool charge before/after identity-probe drop | After reader drop | -| --- | --- | --- | --- | --- | --- | --- | -| Unbounded | 8 + 1 | 2 | 233472 B | 32768 B | 266376 B / 266376 B | 40864 B | -| Exclusive 17 | 8 + 1 | 2 | 102400 B | 16384 B | 124456 B / 124456 B | 22144 B | - -The live baseline charge is 2216 B. Distinct state channels, overlapping payloads, -and unchanged charge after identity probes are dropped establish reader-held -multiplication; Slot ownership alone does not prove the Goal. Existing -`Head::adopt_head` shares payload references but only joins a head that reaches -the target offset, refusing sparse gaps. The bounded copies ending at 17 do not -reach the live copy at 32. Coalescing must preserve existing public reader -outcomes, completion/error ownership, compression, and cache accounting. - -The diagnostic below was inserted into `model::track::test`, run once through -the normal runner, then removed from executable source. It remains here so the -proof survives local scratch cleanup. The source and raw output are retained -under this worktree's `.tmp/held-tail-cost.rs` and -`.tmp/held-tail-overlap-cost.log`. It measures correctness and ownership, not -host performance or a new acceptance budget. SHA256 source: -`ea08934f00d65cb19443c9f0c90eec824ff6e2abc0fa347c587aef2564e33a76`; -raw output: `3c9efaf8a72a0a23e2f01c536e61a3ab0e5e4e52a35dd028eb6d73c7081c9eb9`. - -```sh -nix develop --command just rs test -p moq-net --lib \ - -E 'test(=model::track::test::diagnostic_held_tail_storage)' \ - --success-output immediate -``` - -```rust - #[moq_clock::test] - #[cfg(feature = "async")] - async fn diagnostic_held_tail_storage() { - for bounded in [false, true] { - let producer = track_producer("test", Info::timed()); - let pool = producer.broadcast.pool.clone(); - let dynamic = producer.dynamic(); - let consumer = producer.consume(); - let mut live = producer.create_group(group::Info { sequence: 0 }).unwrap(); - live.start_at(32).unwrap(); - live.write_frame(Timestamp::ZERO, vec![32; 1024]).unwrap(); - let baseline = pool.used(); - let mut held = Vec::new(); - let mut identities = Vec::new(); - for start in (1..=8).rev() { - let options = group::Fetch { frame_start: start, ..Default::default() }; - let fetch = if bounded { - consumer.fetch_range(0, FetchOptions { options, end: Some(17) }) - } else { - consumer.fetch_group(0, options) - }; - let request = dynamic.requested_fill_group().await.unwrap(); - let mut tail = request.accept(Info::timed()).unwrap(); - for frame in start..if bounded { 17 } else { 33 } { - tail.write_frame(Timestamp::ZERO, vec![frame as u8; 1024]).unwrap(); - } - tail.finish().unwrap(); - identities.push(tail.handle()); - held.push(fetch.await.unwrap()); - } - for (i, left) in identities.iter().enumerate() { - for right in &identities[i + 1..] { assert!(!left.is_clone(right)); } - assert!(!left.is_clone(&live.handle())); - } - let before_identity_drop = pool.used(); - drop(identities); - let only_readers_and_slot = pool.used(); - let cached_handles = producer.state.read().lookup[&0].copies().count(); - eprintln!("HELD_TAIL_COST bounded={bounded} requests=8 unique_tail_storages=8 live_storages=1 slot_handles={cached_handles} unread_payload={} unique_payload={} baseline_charge={baseline} charge_with_identities={before_identity_drop} charge_after_identity_drop={only_readers_and_slot}", if bounded { 100*1024 } else { 228*1024 }, if bounded { 16*1024 } else { 32*1024 }); - for (i, reader) in held.iter_mut().enumerate() { - let frame = reader.read_frame().await.unwrap().unwrap(); - assert_eq!(frame.payload.len(), 1024); - assert!(frame.payload.iter().all(|byte| *byte == (8-i) as u8)); - for expected in (9-i)..if bounded { 17 } else { 33 } { - let frame = reader.read_frame().await.unwrap().unwrap(); - assert!(frame.payload.iter().all(|byte| *byte == expected as u8)); - } - } - drop(held); - eprintln!("HELD_TAIL_COST bounded={bounded} charge_after_reader_drop={}", pool.used()); - live.abort(Error::Cancel).unwrap(); - } - } -``` - -### Canonical-owner prototype checkpoint - -The private Rust prototype activates an indexed content owner when a second -transport delivery would coexist with the first. Ordinary single-copy whole -and batch writes retain their existing representation. Activation imports the -existing payloads without copying and attaches the exact in-flight `FrameBuf`, -so an already-streaming reader and writer keep their progress. New deliveries -fork their own received counter while sharing immutable content, publication -and the origin's admission. Contradictory bytes use `ProtocolViolation`. - -A `TrackState`-scoped weak index lets a refill discover an owner still retained -by older tails after its Slot was removed. Owners remove their weak entries; -the index is owned by the live owners, so dead history does not accumulate. -Explicit live-group recreation replaces the sequence's indexed incarnation. -A different track instance does not inherit it. Delivery logs, bounds, FIN, -errors, timestamps and public arrival records remain separate. IETF Object IDs -have their own key space instead of being mistaken for dense frame ordinals. - -Intrinsic `FrameBuf` charges follow the shared storage and each progress view, -including streamed readers that outlive the delivery's partial slot. A -payload-only `Charge::transfer` moves old bytes without recording them as new -writes. Fresh attachment and already-canonical transfer have controls. Deferred -log entries hold weak row handles; a non-polling cursor cannot keep a canonical -owner alive through retired log storage. Handoff heads retain the content owner. - -The first eager prototype exposed real ownership and accounting integration -failures: 466/491 existing group/track tests passed initially, then 482/491 -after ownership corrections. Its sources and raw results remain in -`.tmp/eager-content-prototype/`, `.tmp/content-model-tests.log` and -`.tmp/content-model-tests-2.log`. It was replaced by lazy activation rather than -relaxing cache-pressure thresholds. Two refill fixtures had different bytes for -the same fixed incarnation; only those payloads were made consistent, preserving -the bounds, lifetime and end-refusal assertions. - -The lazy implementation passes 508/508 strict frame, group and track controls, -including the following new outer controls: - -- Eight distinct tail starts with all earlier readers held, at 0, 1 and 1024 B - per frame, both bounded and unbounded. Matching nonempty frame payloads have - the same physical pointer. Dropping identity probes changes no charge. -- Actual Slot removal with an older reader-held tail, followed by a refill - sharing its owner; explicit live recreation accepts different bytes and uses - a separate owner. -- Activation while the original frame and streaming reader are already open. - Overlap credits no progress to that delivery. One abort leaves the other - producer valid and admitted. Two completions retain distinct arrival times - 10 ms apart under the deterministic clock while sharing the same payload. - -The nine delivery handles remain distinct. Their logs and arrival metadata are -charged separately; this proves one canonical content store, not a constant -bound on all delivery bookkeeping or arbitrary caller-retained public Frames. -The bounded case also retains the live frame at 32 beside tail frames 1..16. - -| Payload/frame | End | Canonical rows | Unique payload | Shared charge | Delivery charge | Discovery charge | Total | -| --- | --- | --- | --- | --- | --- | --- | --- | -| 0 B | Unbounded | 32 | 0 B | 10368 B | 34272 B | 136 B | 44776 B | -| 0 B | Exclusive 17 | 17 | 0 B | 5688 B | 22272 B | 136 B | 28096 B | -| 1 B | Unbounded | 32 | 32 B | 10400 B | 34272 B | 136 B | 44808 B | -| 1 B | Exclusive 17 | 17 | 17 B | 5705 B | 22272 B | 136 B | 28113 B | -| 1024 B | Unbounded | 32 | 32768 B | 43136 B | 34272 B | 136 B | 77544 B | -| 1024 B | Exclusive 17 | 17 | 17408 B | 23096 B | 22272 B | 136 B | 45504 B | - -On this x86_64 build the shared frame content allocation is 184 B, a progress -view is 56 B, and admission bookkeeping is 32 B. The empty-payload cases expose -the metadata independently of payload savings. These are ownership and charge -measurements, not quiet-host timing or allocator-overhead claims. - -```sh -nix develop --command just rs test -p moq-net --lib \ - -E 'test(model::frame::tests) | test(model::group::test) | test(model::track::test)' \ - --no-fail-fast --success-output immediate -``` - -Raw output: `.tmp/lazy-content-model-tests-2.log`, SHA256 -`76e5c37663d9529589e0119fa817cf3c064cfd4cc436a099f56bf6055bdccbda`. -`just fix` also passed. These focused results precede this checkpoint's final -integration gates; the full CI and interop pass on 5796 remain separate evidence. - -The Goal remains unfinished pending typed sparse/relay and compressed raw/plain -outer preservation controls, actual-main prerequisite integration, final clean -committed-head gates and real surface proof. Actual compressed transport ingress -must share stored raw slices as received, with one lazy inflated copy. Local -plaintext-to-DEFLATE producer scratch is a distinct representation; it is not -claimed to share raw wire storage. Compressed transport fetches must continue -widening to start 0/end None, and a plain reader without the prefix must still -refuse. No public API, wire, resource-budget or maintainer-policy choice has been -selected by this checkpoint. - -### Storage-lifetime preservation checkpoint - -The later implementation supersedes the 2420 prototype's ownership details. -Canonical frame storage now owns its group identity, while the group's index -holds weak frame records. Completed delivery entries retain shared frame owners; -their owner cell can be populated during activation without replacing payloads -that lock-free readers already see. A streamed reader keeps the same identity -through its existing buffer. Last storage release removes its weak record, so -neither retired entries nor frame readers can hide retained bytes from a refill. -The index retains no dead weak history and introduces no new union-wide limits. -Existing per-delivery limits remain enforced. - -A deterministic evicted-streaming-reader control first failed because its weak -group identity vanished while its frame buffer still existed. It now passes, -reusing the same physical payload on refill while the old reader still returns -`Evicted`. A separate control accepts an existing valid 8192-frame tail and a -disjoint valid one-frame prefix, preserving their existing per-delivery limits. - -An independent review found that a captured inflater job could outlive delivery -A's release when delivery B kept the shared Plain alive. The existing -`inflation_is_shared_and_stops_at_release` control was extended and reproduced -that failure. A compressed delivery now has its own release guard. Each inflation -frame and get synchronizes with that guard; releasing A waits for its current -frame and preserves A's error while B can still read and inflate. The guard's -metadata is charged to the active delivery. This changes neither the shared -inflation representation nor the existing caller ownership of completed bytes. - -Typed object controls preserve IDs across different delivery ordinals, sealed -timestamps and delivery-local terminal metadata. The compressed ingress control -uses independent received buffers, verifies raw payload pointer reuse without -recompression, preserves the missing-prefix refusal and different arrival times, -inflates plain bytes once, and keeps start-0/end-None widening for repeated tail -requests. Local plaintext compression scratch remains a separate producer path. - -The full strict frame/group/track cohort passes 512/512. `just fix` passes. -Source includes the actually landed request and Path prerequisites through a -normal merge of main `c764acebb90b5c102a8ea90207dd125ae4fd326b`; no rebase. -The subsequent IETF version-removal integration and final full gates remain. - -The updated x86_64 metadata sizes are content 216 B, progress view 56 B and -admission bookkeeping 32 B. Ownership charges from the retained-reader control: - -| Payload/frame | End | Canonical rows | Unique payload | Shared charge | Delivery charge | Discovery charge | Total | -| --- | --- | --- | --- | --- | --- | --- | --- | -| 0 B | Unbounded | 32 | 0 B | 8304 B | 36448 B | 136 B | 44888 B | -| 0 B | Exclusive 17 | 17 | 0 B | 5064 B | 23296 B | 136 B | 28496 B | -| 1 B | Unbounded | 32 | 32 B | 8336 B | 36448 B | 136 B | 44920 B | -| 1 B | Exclusive 17 | 17 | 17 B | 5081 B | 23296 B | 136 B | 28513 B | -| 1024 B | Unbounded | 32 | 32768 B | 41072 B | 36448 B | 136 B | 77656 B | -| 1024 B | Exclusive 17 | 17 | 17408 B | 22472 B | 23296 B | 136 B | 45904 B | - -Raw outputs and SHA256: - -- Full focused model cohort: `.tmp/weak-owner-model-tests.log`, - `65b5eba4bd4a0c6d95536806469c8ede169cb5f6112ae11a2d638e094590fbf4`. -- Frame-reader negative: `.tmp/frame-reader-owner-before-2.log`, - `41b552539fe590464c5040fbc93e8fc0597214cb73bfaa630fce1124b06c7005`. -- Frame-reader/union positive: `.tmp/weak-frame-owner-controls.log`, - `13639d690f523102d340f5f4106f2b8ed9eb9a9e76184452bb470006cf716f4c`. -- Shared late-inflater negative: `.tmp/shared-late-inflate-before.log`, - `ad2524de1e81d2a5e01ee02707585401ae15d428653f21a40392e7d010452c99`. -- Late-inflater/typed/compressed positives: - `.tmp/canonical-representation-controls.log`, - `44a2868fc82a01871af38327cc13697450988df42c9c379bbc946796bd80bec1`. - -The Goal is still unfinished. Read-only review found a direct live Lite GROUP -path that creates a public whole group before applying the received frame start, -then resets the owner when replacing an earlier positive-start copy. The next -control must exercise actual GroupRecv with held readers and distinct tail -starts, followed by a private transport claim that knows the offset before -publication. Public explicit group recreation must retain its separate owner. -Ordinary pre-existing cross-route duplication is not itself evidence of this -specific tail-amplification bug. Final readiness still needs this direct ingress -proof, current-main integration, clean committed-head gates and the real surface. - - -### Live GROUP and intrinsic-ownership checkpoint - -The direct Lite ingress failure was reproduced through actual `GroupRecv` and -one subscriber entry: eight independent GROUP streams for the same sequence, -with starts 8 down to 1 and all earlier consumers held. Each stream received -fresh 1 KiB buffers. Before the correction, matching frame 8 had different -physical payload pointers. The private transport claim now receives the known -start before group publication, discovers retained canonical content before -replacing a Slot, and preserves public explicit recreation as a separate owner. -The same wire control now passes, including each consumer's bounds, payload and -FIN. No new route identity or Epoch-less cross-route sharing was introduced. - -The new inflater release guard also had a distinct accounting regression: a -captured job retained the guard after group release, but its charge was cleared -with the group. A deterministic negative control observed 0 B rather than the -live guard's 64 B. The guard now carries its own intrinsic `Extra`; releasing -the delivery preserves its error, and the final retained job/clone returns that -metadata charge. The earlier shared-Plain preservation control still passes. - -Both weak discovery indexes now use randomized `hashbrown` maps instead of -sorted vectors. The existing locked 0.17.1 release is a direct dependency with -default features disabled and `std::RandomState`; `allocation_size()` measures -the actual table allocation. Final owners remove their records immediately, -empty tables release their buckets, and replacing a sequence cannot let the old -owner remove the new incarnation. Frame-owned accounting events update a checked -aggregate, so group sizing neither allocates nor walks all retained rows. The -underlying intrinsic charges still end with the final physical owner. - -The pre-change benchmark used the actual canonical frame owners, 64 B frames, -1/64/1024/8192 frames and 1/8 held owner sets. Sizing runs on a constant fixture; -cleanup times actual final-owner destruction with setup outside the timed body. -It used 20 samples, 500 ms warm-up and 2 s measurement, on this 24-CPU x86_64 host -at 13:13:39–13:14:35 UTC; load averages were 0.64/2.05/3.47 before and -1.13/1.97/3.36 after. At 8192 frames, sizing took 69.856/69.493 µs and cleanup -12.734/13.046 ms for 1/8 owner sets. The largest cleanup case warned that the -fixed 20 samples needed more than the 2 s estimate; no sample count or case was -changed. Repeating the same cases after the source correction remains due. - -The strict frame/group/track/Lite cohort passes 606/606, including unordered -empty-frame final releases, replacement owner discovery, actual live GROUP, -sparse typed properties, raw/plain compression, independent errors/arrivals, -frame-reader lifetime and original versus bounded ends. This includes actual -main's IETF 14–17 removal through a normal merge of -`22808bbb2f4aab9fdaf9a60507f77efbaef4bd0d`. Full CI and interop on 5796 are -historical evidence only; this newer source still needs its final gates. - -Updated held-reader charges, with distinct per-delivery bookkeeping retained: - -| Payload/frame | End | Canonical rows | Unique payload | Shared charge | Delivery charge | Discovery charge | Total | -| --- | --- | --- | --- | --- | --- | --- | --- | -| 0 B | Unbounded | 32 | 0 B | 9696 B | 36448 B | 180 B | 46324 B | -| 0 B | Exclusive 17 | 17 | 0 B | 5144 B | 23296 B | 180 B | 28620 B | -| 1 B | Unbounded | 32 | 32 B | 9728 B | 36448 B | 180 B | 46356 B | -| 1 B | Exclusive 17 | 17 | 17 B | 5161 B | 23296 B | 180 B | 28637 B | -| 1024 B | Unbounded | 32 | 32768 B | 42464 B | 36448 B | 180 B | 79092 B | -| 1024 B | Exclusive 17 | 17 | 17408 B | 22552 B | 23296 B | 180 B | 46028 B | - -Raw evidence and SHA256: - -- Live GROUP negative: `.tmp/live-group-tail-before-2.log`, - `7490dc59ae51a8d719aa2f4dfc42cee989e4e874f9d3fe8f0ae7d49a6853d476`. -- Retained guard charge negative: `.tmp/inflater-guard-charge-before.log`, - `d7d891702abefc0bf34c7860a202c7b8ab406afa131d1e31a3c635abb331db9e`. -- Both focused positives: `.tmp/live-claim-guard-controls.log`, - `7e78fecf9113922de9e8d5162e030129b73c3d0e02ef053d42ee907dab0a5cf6`. -- Strict 606-control cohort: `.tmp/canonical-map-focused.log`, - `a46a4bfaa8928eef9082f55e175bde24a610aad4de233f9ca90a94ccb735dc05`. -- Before benchmark: `.tmp/canonical-bench.log`, - `0fe6d7d6dc4bbcf6a6d05a659cb8e72b0f366c929944a92465a732c0eb3c84f5`. - -No public API, wire field, resource budget, arrival policy or compressed -representation was changed. The Goal remains unfinished until the after -measurement, current-source committed-head gates and real surface proof finish. - - -### Measured weak-index correction - -The identical single after-run completed 13:37:58–13:38:45 UTC on the same -24-CPU x86_64 host, after the other worker's CI became terminal. No heavy job -ran concurrently. Load averages were 3.58/5.89/5.86 before and -2.51/5.24/5.64 after, higher than the earlier baseline. This is a bounded local -comparison, not a general throughput or quiet-host performance guarantee. -The fixed cases and measurement settings were unchanged; no threshold was tuned. - -| Frames × held owner sets | Sizing before | Sizing after | Cleanup before | Cleanup after | -| --- | --- | --- | --- | --- | -| 1 × 1 | 25.912 ns | 8.9837 ns | 230.16 ns | 252.76 ns | -| 1 × 8 | 25.855 ns | 9.0134 ns | 263.43 ns | 285.32 ns | -| 64 × 1 | 599.47 ns | 9.065 ns | 5.6539 µs | 6.4272 µs | -| 64 × 8 | 606.83 ns | 8.9804 ns | 7.3737 µs | 8.1054 µs | -| 1024 × 1 | 8.7272 µs | 9.0952 ns | 165.32 µs | 108.94 µs | -| 1024 × 8 | 8.6809 µs | 8.9223 ns | 191.46 µs | 134.89 µs | -| 8192 × 1 | 69.856 µs | 8.9981 ns | 12.734 ms | 887.63 µs | -| 8192 × 8 | 69.493 µs | 8.9692 ns | 13.046 ms | 1.1143 ms | - -Small-group cleanup is modestly slower, while sizing is constant work and final -cleanup no longer repeatedly shifts retained rows. Exact hash-table capacity is -still charged, so a table may retain spare buckets until its last row drops. -Empty tables free those buckets immediately. The same map and accounting helper -serve the group discovery index; neither retains dead weak history. - -```sh -nix develop --command cargo bench --locked -p moq-net --features fuzz \ - --bench canonical -- --noplot -``` - -After output: `.tmp/canonical-map-bench.log`, SHA256 -`19ca1cca45bb37756cd9143e051e00b061796edd549a3579bbb20534d546e660`. -Conditions: `.tmp/canonical-map-bench-conditions.json`, SHA256 -`2a91de696ca6eaf5d594742e15ee4045e97fe2add5533a67078beb2545d861fd`. -The stable full CI and real surface gates remain due on the final committed head. - -## Related - -- [Front eviction](/quest/m1/unbounded-groups/front-eviction.md) - refills a front hole with a frame-precise range diff --git a/quest/m1/unbounded-groups/front-eviction.md b/quest/m1/unbounded-groups/front-eviction.md index 6059d84c53a..c4a23db686e 100644 --- a/quest/m1/unbounded-groups/front-eviction.md +++ b/quest/m1/unbounded-groups/front-eviction.md @@ -104,7 +104,3 @@ See the [epic](/quest/m1/unbounded-groups/README.md) for the decisions. Public API: removes `MAX_GROUP_FRAMES`, and `MAX_CACHE_BYTES` stops capping groups. A reader that used to see `GroupTooLarge` now refills or sees `Evicted`. Wire: none beyond the amended frame offset rule. - -## Required - -- [Transport frame offsets](/quest/m1/transport-frame-offsets.md) - the lite draft lets a refill start at the missing frame without holding the frames below it From ba6160b6a06c4d46a1ec6be1e4fecd4f3c751286 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 27 Oct 2026 14:14:59 -0700 Subject: [PATCH 11/16] Restore the caller's frame range on a routed fetch A cold or compressed serving copy widens its fill to the group's head, so the logical track re-applies the requested start and end. Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/model/resume.rs | 36 ++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/rs/moq-net/src/model/resume.rs b/rs/moq-net/src/model/resume.rs index b591028213a..bbea996ab1d 100644 --- a/rs/moq-net/src/model/resume.rs +++ b/rs/moq-net/src/model/resume.rs @@ -2106,6 +2106,10 @@ impl kio::Task for Fetching { }; return match result { Ok(group) => { + // The copy may have widened its fill; the caller still reads only its range. + let group = group + .with_range(self.options.options.frame_start..) + .with_end(self.options.end); let handoff = group.handoff(); Poll::Ready(Ok(group.with_recover(Recover { sequence: self.sequence, @@ -5009,6 +5013,38 @@ mod test { assert!(matches!(kio::Task::poll(&mut *fetching, &waiter), Poll::Ready(Ok(_)))); } + /// A cold route widens its fill to the group's head, but the caller still reads from + /// the frame it asked for. + #[test] + #[cfg(feature = "async")] + fn a_widened_route_fetch_keeps_the_callers_start() { + let routes = Producer::new(); + let a = copy(); + let dynamic = a.requests(); + routes.serve(a.consume()); + let logical = logical(&routes); + let options = group::Fetch { + frame_start: 2, + ..Default::default() + }; + let mut fetching = logical.consume().fetch_group(0, options); + let waiter = kio::Waiter::noop(); + assert!(kio::Task::poll(&mut *fetching, &waiter).is_pending()); + let request = dynamic.requested_fill_group().now_or_never().unwrap().unwrap(); + assert_eq!(request.frame_start(), 0, "the cold copy fills from the head"); + let mut group = request.accept(None).unwrap(); + for frame in 0..4u8 { + group.write_frame(ts(0), vec![frame]).unwrap(); + } + group.finish().unwrap(); + let Poll::Ready(Ok(mut group)) = kio::Task::poll(&mut *fetching, &waiter) else { + panic!("the fill did not resolve the fetch"); + }; + assert_eq!(group.index(), 2); + let frame = group.read_frame().now_or_never().unwrap().unwrap().unwrap(); + assert_eq!(&frame.payload[..], &[2]); + } + /// A span fill made before the front serves a copy waits for one and asks it for the /// whole span, rather than resolving with nothing filled. #[test] From 088d7fed4b985a7ad84345ad8255909c1dfeb0a9 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 27 Oct 2026 14:21:04 -0700 Subject: [PATCH 12/16] FIN an empty received tail served from its start A subscription starting exactly where a received tail begins already reached its start, so an empty tail is a valid empty range, not a gap. Co-Authored-By: Claude Opus 5.5 --- js/net/src/lite/publisher.test.ts | 16 ++++++++++++++-- js/net/src/lite/publisher.ts | 4 ++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/js/net/src/lite/publisher.test.ts b/js/net/src/lite/publisher.test.ts index 1a451359fdc..604cbaa3dcc 100644 --- a/js/net/src/lite/publisher.test.ts +++ b/js/net/src/lite/publisher.test.ts @@ -1932,11 +1932,13 @@ type Served = { sequence: number; frameStart: number; payloads: string[] }; /** * Serves `groups` (frame payloads per group, keyed by sequence) under `bounds`, and * reports every group stream that reached the wire plus the resolved - * SUBSCRIBE_START / SUBSCRIBE_END range. + * SUBSCRIBE_START / SUBSCRIBE_END range. A group in `received` arrived as a tail + * starting at that frame. */ async function serve( groups: Record, bounds: { startGroup?: number; startFrame?: number; endGroup?: number; endFrame?: number }, + received: Record = {}, ): Promise<{ start?: number; end?: number; served: Served[] }> { const pair = createMockTransportPair(ALPN_06); const origin = new OriginProducer(); @@ -1962,7 +1964,11 @@ async function serve( void publisher.runSubscribe(msg, server); for (const [sequence, frames] of Object.entries(groups)) { - const group = track.createGroup(Number(sequence)); + const start = received[Number(sequence)]; + const group = + start === undefined + ? track.createGroup(Number(sequence)) + : hooks.receiveGroup(track, { sequence: Number(sequence), start }); for (const frame of frames) group.writeFrame(textFrame(frame)); group.finish(); @@ -2035,6 +2041,12 @@ test("lite draft-06: a subscription starting at the end of a group serves it emp expect(served).toEqual([{ sequence: 0, frameStart: 2, payloads: [] }]); }); +// A received tail that ends where it starts is the same valid, empty range. +test("lite draft-06: an empty received tail is served empty at its start", async () => { + const { served } = await serve({ 0: [] }, { startGroup: 0, startFrame: 5 }, { 0: 5 }); + expect(served).toEqual([{ sequence: 0, frameStart: 5, payloads: [] }]); +}); + // The end bound is inclusive. test("lite draft-06: a subscription capped mid-group stops at the end frame", async () => { const { served } = await serve( diff --git a/js/net/src/lite/publisher.ts b/js/net/src/lite/publisher.ts index 50d3142676f..e79414d0bdc 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -1778,8 +1778,8 @@ export class Publisher { let prevTs = 0n; // Whether the cursor ever reached the requested start, which decides how the - // end of the group is read below. - let reached = startFrame === 0; + // end of the group is read below. A received tail may already begin there. + let reached = hooks.groupIndex(group) === startFrame; for (;;) { const read = await race([hooks.readGroupFrame(group, compress), stream.closed]); From 7dda6cf61cdb57d0c690977a3ed379c3f0252d44 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 27 Oct 2026 14:59:06 -0700 Subject: [PATCH 13/16] fix(net): keep bounded copies off whole-group readers A fetch whose accepted bounded copy a later, disjoint copy displaced from the cache now resolves through the fill's outcome instead of NotFound. A copy cut short at the back no longer displaces a fetched one that reaches the group's end. Group-read gates (arrival, ordered, unread, landing, successor, peek) now require a copy that serves the group's end. A live group whose only survivor is a back-cut copy reads as the cut it was, or a hole for a cancel, never as that copy. A whole frame another copy holds differently now fails the group in write_frame/write_frames, since a compressed group already took it into its history. Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/model/group.rs | 48 ++++++- rs/moq-net/src/model/track.rs | 212 +++++++++++++++++++++++------ rs/moq-net/src/model/track/fill.rs | 13 ++ 3 files changed, 231 insertions(+), 42 deletions(-) diff --git a/rs/moq-net/src/model/group.rs b/rs/moq-net/src/model/group.rs index fe42aa6ba01..1f1546012f8 100644 --- a/rs/moq-net/src/model/group.rs +++ b/rs/moq-net/src/model/group.rs @@ -1406,7 +1406,9 @@ impl Producer { arrived: super::clock::now(), }; let mut entry = Entry::plain(frame, inflated); - state.canonicalize(state.next_index, &mut entry)?; + if let Err(err) = state.canonicalize(state.next_index, &mut entry) { + return Err(self.abort_conflict(state, err)); + } state.cache += size; if let Some(flate) = &mut state.flate { flate.inflated += inflated; @@ -1448,7 +1450,8 @@ impl Producer { /// the batch with [`frame::Buffer::push`]. /// /// The batch is validated before anything is written, so a rejected frame leaves - /// both the group and the buffer exactly as they were, ready to retry or redirect. + /// both the group and the buffer exactly as they were, ready to retry or redirect, + /// unless it fails the group: too large, or held differently by another copy. pub fn write_frames(&mut self, frames: &mut frame::Buffer) -> Result<()> { for frame in frames.filled() { on_track(frame.timestamp, self.handle.track.timescale)?; @@ -1519,7 +1522,9 @@ impl Producer { }, inflated, ); - state.canonicalize(state.next_index + index, &mut entry)?; + if let Err(err) = state.canonicalize(state.next_index + index, &mut entry) { + return Err(self.abort_conflict(state, err)); + } prepared.push(entry); } frames.drain().for_each(drop); @@ -1859,6 +1864,13 @@ impl Producer { Error::GroupTooLarge } + /// Fail the group with a whole frame that another copy of it holds differently. A + /// compressed group already took the frame into its history, which no reader gets. + fn abort_conflict(&self, state: kio::Mut<'_, GroupState>, err: Error) -> Error { + self.handle.commit_abort(state, err.clone()); + err + } + /// Block until the group's outcome is final: `Ok` once finished, or the abort error, /// including eviction. #[cfg(feature = "async")] @@ -5735,6 +5747,36 @@ mod test { assert!(matches!(read_plain(&mut consumer), Err(Error::Lagged))); } + /// A frame another copy of the group holds differently is refused after compressing it, + /// so the refusal fails the group: its later frames would decompress against history no + /// reader gets. Written whole or in a batch. + #[test] + fn a_refused_compressed_write_fails_the_group() { + let cache = cache::Track::new(cache::Pool::unbounded(), Default::default(), kio::Weak::new()); + let info = track::Info::default().with_compress(true); + let mut first = Producer::new(Info::from(0u64), info.clone(), cache.clone()); + first.write_frame(None, verbose(0)).unwrap(); + let content = first.handle.activate_content().unwrap().unwrap(); + let copy = || { + let copy = Producer::new(Info::from(0u64), info.clone(), cache.clone()); + copy.handle.share_content(content.clone()); + copy + }; + + let mut whole = copy(); + assert!(matches!( + whole.write_frame(None, verbose(10)), + Err(Error::ProtocolViolation) + )); + assert!(whole.handle.is_aborted()); + + let mut batch = copy(); + let mut frames = frame::Buffer::<1>::new(); + frames.push(Frame::new(None, verbose(10))).unwrap(); + assert!(matches!(batch.write_frames(&mut frames), Err(Error::ProtocolViolation))); + assert!(batch.handle.is_aborted()); + } + /// A received slice that inflates to other than its declared size fails the read. #[test] fn a_received_size_mismatch_fails_the_read() { diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 90aaa82e763..87dd5cca365 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -535,8 +535,8 @@ struct Slot { arrived: crate::time::Instant, // A fetched copy of a live `group` that the feed started past the frames a fetch - // asked for. It runs to the end of the group too, so it is served in the live - // group's place while it lasts; `group` stays the slot, still written by the feed, + // asked for. One that runs to the end of the group is served in the live group's + // place while it lasts; `group` stays the slot, still written by the feed, // so an abandoned fetch leaves the group servable rather than gone. Boxed: rare. head: Option>, @@ -572,9 +572,9 @@ impl Slot { } /// Why the publisher gave up this live-feed group readers were shown, once nothing - /// serves it: a reader is handed the group to see that error, never a missing group. + /// serves its end: a reader is handed the group to see that error, never a missing group. fn cut(&self) -> Option<&Error> { - match self.visible && self.is_aborted() && !self.retired.load(Ordering::Relaxed) { + match self.visible && !self.serves_end() && !self.retired.load(Ordering::Relaxed) { true => self.group.cut_error(), false => None, } @@ -637,13 +637,10 @@ impl Slot { .cache_accessed_tick(scan.gc.then_some(scan.now)) .is_some_and(|tick| scan.now.saturating_sub(tick) > scan.max_ticks) }; - // No copy of a cut serves, so it ages as the live group it was: a reader behind - // still gets it for as long as it would have got that group. - if self.cut().is_some() { - return idle(&self.group); - } + // No copy of a cut serves its end, so it ages as the live group it was: a reader + // behind still gets it for as long as it would have got that group. + let mut expired = self.cut().is_none() || idle(&self.group); // Not `all`, which stops at the first live copy and leaves the rest undated. - let mut expired = true; for group in self.copies().filter(|group| !group.is_aborted()) { expired &= idle(group); } @@ -1017,7 +1014,7 @@ impl TrackState { if *sequence >= min_sequence && let Some(slot) = self.lookup.get(sequence) && slot.stamp == *stamp - && (!slot.is_aborted() || slot.cut().is_some()) + && slot.is_read() { return Poll::Ready(Ok(Some((slot.serving().clone(), self.offset + i)))); } @@ -1145,7 +1142,7 @@ impl TrackState { .range(run) .map(|(_, slot)| slot) .take_while(|slot| super::subscription::before_end(slot.group.sequence, end_sequence)) - .find(|slot| !slot.is_aborted() || slot.cut().is_some()) + .find(|slot| slot.serves_end() || slot.cut().is_some()) .map(Slot::serving) }); @@ -1303,7 +1300,7 @@ impl TrackState { fn holds(&self, sequence: u64, stamp: u32) -> bool { self.lookup .get(&sequence) - .is_some_and(|slot| slot.stamp == stamp && !slot.is_aborted()) + .is_some_and(|slot| slot.stamp == stamp && slot.serves_end()) } /// The furthest presentation time the group at `sequence` could still reach: where @@ -2171,7 +2168,7 @@ impl TrackState { /// and pending while it still may land. fn landing(&self, sequence: u64) -> Poll> { if let Some(slot) = self.lookup.get(&sequence) - && !slot.is_aborted() + && slot.serves_end() { return Poll::Ready(Some(slot.serving().consume())); } @@ -2665,7 +2662,17 @@ impl TrackState { } let content = self.content_for_copy(sequence)?; - if !self.lookup.get(&sequence).is_some_and(Slot::is_live) { + // A copy cut short at the back never displaces a fetched one that reaches the end, + // which whole-group readers still take. The fill hands it to its own fetch instead. + let kept = frames.end.is_some() + && self.lookup.get(&sequence).is_some_and(|slot| match slot.is_live() { + true => slot + .head + .as_deref() + .is_some_and(|head| !head.is_aborted() && !head.back_cut()), + false => slot.serves_end(), + }); + if !kept && !self.lookup.get(&sequence).is_some_and(Slot::is_live) { self.uncache(sequence); } @@ -2687,6 +2694,7 @@ impl TrackState { self.charge_debt(); let protected = self.protects(sequence); match self.lookup.get_mut(&sequence).filter(|slot| slot.is_live()) { + _ if kept => {} Some(slot) => { // Joins the slot's standing: demoted already unless it is the live edge. if !protected { @@ -4464,16 +4472,15 @@ impl Consumer { .lookup .range(..sequence) .rev() - .map(|(_, slot)| slot.serving()) - .find(|group| !group.is_aborted()) - .map(|group| group.consume()) + .find(|(_, slot)| slot.serves_end()) + .map(|(_, slot)| slot.serving().consume()) }) } /// A cached group by sequence, under the same terms as [`Self::peek_latest`]. Unlike a /// fetch, a peek does not refresh the group's cache standing, so it never keeps a - /// group alive over one a subscriber actually read; an aborted (evicted) group is a - /// miss. + /// group alive over one a subscriber actually read; an aborted (evicted) group, or a + /// copy cut short at the back, is a miss. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] pub(crate) fn peek_group(&self, sequence: u64) -> Option { if let Some(serving) = self.serving() { @@ -4481,11 +4488,8 @@ impl Consumer { } self.with_state(|state| { let state = state?.read(); - let group = state.lookup.get(&sequence)?.serving(); - if group.is_aborted() { - return None; - } - Some(group.consume()) + let slot = state.lookup.get(&sequence).filter(|slot| slot.serves_end())?; + Some(slot.serving().consume()) }) } @@ -5402,15 +5406,12 @@ impl kio::Task for Fetching { // Track side: the cached group, the abort error, or past-final. The outer // error is the channel closing without any of those. - let cached = |waiter: &kio::Waiter| match state.poll(waiter, |state| { - state.poll_fetch_cached( - sequence, - group::Bounds { - start: frame_start, - end: self.frame_end, - }, - ) - }) { + let frames = group::Bounds { + start: frame_start, + end: self.frame_end, + }; + let cached = |waiter: &kio::Waiter| match state.poll(waiter, |state| state.poll_fetch_cached(sequence, frames)) + { Poll::Ready(Ok(res)) => Poll::Ready(res.map(resolve)), Poll::Ready(Err(closed)) => Poll::Ready(Err(closed.abort.clone().unwrap_or(Error::Dropped))), Poll::Pending => Poll::Pending, @@ -5443,15 +5444,17 @@ impl kio::Task for Fetching { // An answered fill fails a joined fetch the cache cannot cover. The channel // closing without an answer means its handlers went away. - let (err, asked) = match result + let (err, asked, partial) = match result .outcome() .poll(waiter, |outcome| match outcome.refused(sequence) { - Some((err, asked)) => Poll::Ready((err.clone(), asked)), - None if outcome.is_accepted(sequence) => Poll::Ready((Error::NotFound, None)), + Some((err, asked)) => Poll::Ready((err.clone(), asked, None)), + None if outcome.is_accepted(sequence) => { + Poll::Ready((Error::NotFound, None, outcome.partial(sequence, frames))) + } None => Poll::Pending, }) { Poll::Ready(Ok(res)) => res, - Poll::Ready(Err(_closed)) => (Error::NotFound, None), + Poll::Ready(Err(_closed)) => (Error::NotFound, None, None), Poll::Pending => return Poll::Pending, }; // An accept caches the group before it answers the channel, which may land @@ -5459,6 +5462,10 @@ impl kio::Task for Fetching { if let Poll::Ready(res) = cached(waiter) { return Poll::Ready(res); } + // A later copy displaced the one accepted for this fetch from the cache. + if let Some(group) = partial.as_ref().and_then(group::Handle::try_consume) { + return Poll::Ready(Ok(resolve(group))); + } // Joined after a tighter budget went on the wire, which judged the group too old // for that budget, not ours: ask again with ours. Terminates, since the new // fill asks at least our budget. @@ -7283,7 +7290,7 @@ impl Subscriber { && state .lookup .get(sequence) - .is_some_and(|slot| slot.stamp == *stamp && (!slot.is_aborted() || slot.cut().is_some())) + .is_some_and(|slot| slot.stamp == *stamp && slot.is_read()) }) || owed()) } @@ -15436,6 +15443,133 @@ mod test { assert!(!producer.state.read().lookup[&0].is_aborted()); } + /// A bounded fill that a later, disjoint one displaces from the cache still answers the + /// fetch that asked for it. + #[moq_clock::test] + #[cfg(feature = "async")] + async fn a_displaced_bounded_fill_still_answers_its_fetch() { + let producer = track_producer("test", Info::timed()); + let dynamic = producer.requests(); + let consumer = producer.consume(); + let mut live = producer.create_group(group::Info { sequence: 0 }).unwrap(); + live.start_at(200).unwrap(); + live.write_frame(Timestamp::ZERO, b"live".as_slice()).unwrap(); + let range = |start, end| FetchOptions { + options: group::Fetch { + frame_start: start, + ..Default::default() + }, + end: Some(end), + }; + let fill = async |frames: std::ops::Range| { + let fill = dynamic.requested_fill_group().await.unwrap(); + assert_eq!((fill.frame_start(), fill.frame_end()), (frames.start, Some(frames.end))); + let mut group = fill.accept(Info::timed()).unwrap(); + for _ in frames { + group.write_frame(Timestamp::ZERO, b"tail".as_slice()).unwrap(); + } + group.finish().unwrap(); + }; + let later = consumer.fetch_range(0, range(4, 10)); + fill(4..10).await; + let earlier = consumer.fetch_range(0, range(0, 3)); + fill(0..3).await; + assert_eq!(earlier.await.unwrap().index(), 0); + assert_eq!(later.await.unwrap().index(), 4); + } + + /// A copy cut short at the back never displaces a fetched one that reaches the group's + /// end, so whole-group readers keep it, and it still answers its own fetch. + #[moq_clock::test] + #[cfg(feature = "async")] + async fn a_bounded_fill_never_displaces_a_whole_tail() { + let producer = track_producer("test", Info::timed()); + let dynamic = producer.requests(); + let consumer = producer.consume(); + let mut live = producer.create_group(group::Info { sequence: 0 }).unwrap(); + live.start_at(200).unwrap(); + live.write_frame(Timestamp::ZERO, b"live".as_slice()).unwrap(); + let fill = async |start: u64, end: Option| { + let fill = dynamic.requested_fill_group().await.unwrap(); + assert_eq!((fill.frame_start(), fill.frame_end()), (start, end)); + let mut group = fill.accept(Info::timed()).unwrap(); + group.write_frame(Timestamp::ZERO, b"tail".as_slice()).unwrap(); + group + }; + let tail = consumer.fetch_group( + 0, + group::Fetch { + frame_start: 5, + ..Default::default() + }, + ); + let _tail = fill(5, None).await; + let head = consumer.fetch_range( + 0, + FetchOptions { + options: group::Fetch::default(), + end: Some(1), + }, + ); + fill(0, Some(1)).await.finish().unwrap(); + assert_eq!(head.await.unwrap().index(), 0); + assert_eq!(tail.await.unwrap().index(), 5); + let mut sub = producer.subscribe(None); + assert_eq!(sub.assert_group().with_range(0..).index(), 5); + } + + /// A copy cut short at the back never answers a whole-group reader. A live group the + /// publisher cut is still handed out as that cut, and one it cancelled stays a hole, + /// whatever bounded copy of it remains. + #[moq_clock::test] + #[cfg(feature = "async")] + async fn a_back_cut_copy_never_answers_a_whole_group_reader() { + let producer = track_producer("test", Info::timed()); + let dynamic = producer.requests(); + let consumer = producer.consume(); + let bounded = async |sequence: u64| { + let fetch = consumer.fetch_range( + sequence, + FetchOptions { + options: group::Fetch::default(), + end: Some(1), + }, + ); + let mut group = dynamic + .requested_fill_group() + .await + .unwrap() + .accept(Info::timed()) + .unwrap(); + group.write_frame(Timestamp::ZERO, b"head".as_slice()).unwrap(); + group.finish().unwrap(); + fetch.await.unwrap(); + }; + let mut arrival = producer.subscribe(replay()); + let mut ordered = producer.subscribe(replay()).ordered(); + for (sequence, err) in [(0, Error::App(5)), (1, Error::Cancel)] { + let mut live = producer.create_group(group::Info { sequence }).unwrap(); + live.start_at(200).unwrap(); + live.write_frame(Timestamp::ZERO, b"live".as_slice()).unwrap(); + bounded(sequence).await; + live.abort(err).unwrap(); + } + // Fetched only, so it reaches the ordered reader alone. + bounded(2).await; + + let cut = arrival.assert_group(); + assert_eq!(cut.sequence, 0); + assert!(matches!(cut.with_range(200..).read_frame().await, Err(Error::App(5)))); + arrival.assert_no_group(); + let cut = ordered.next_group().now_or_never().unwrap().unwrap().unwrap(); + assert_eq!(cut.sequence, 0); + assert!(matches!(cut.with_range(200..).read_frame().await, Err(Error::App(5)))); + assert!( + ordered.next_group().now_or_never().is_none(), + "neither the cancelled group nor a copy ending short of its group is read" + ); + } + /// Queued ranges widen before dispatch; a taken cap does not promise an unseen suffix. #[moq_clock::test] #[cfg(feature = "async")] diff --git a/rs/moq-net/src/model/track/fill.rs b/rs/moq-net/src/model/track/fill.rs index 2591857c9fb..49231da69fd 100644 --- a/rs/moq-net/src/model/track/fill.rs +++ b/rs/moq-net/src/model/track/fill.rs @@ -183,6 +183,9 @@ pub(crate) struct Outcome { // Accepted or refused, kept as answers arrive so a fill answered group by group costs // the runs each answer touches, not every answer before it. resolved: RangeSet, + // Each accepted copy short of the whole group, which a later copy may displace from the + // cache before a reader takes it. A whole copy covers every other, so none displaces it. + partial: BTreeMap, } impl Outcome { @@ -195,6 +198,11 @@ impl Outcome { self.accepted.contains(&sequence) } + /// The partial copy accepted for `sequence`, if it still holds `frames`. + pub(super) fn partial(&self, sequence: u64, frames: group::Bounds) -> Option { + self.partial.get(&sequence).filter(|copy| copy.covers(frames)).cloned() + } + /// The sequences of `span` accepted, lowest first. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] pub(super) fn accepted_in(&self, span: &ops::Range) -> impl Iterator { @@ -1020,6 +1028,11 @@ impl Inner { TrackState::modify(&self.state).and_then(|mut state| state.insert_group_request(sequence, frames, info)); if let Ok(mut outcome) = self.result.write() { outcome.accept(sequence..end); + if let Ok(group) = &res + && (frames.start > 0 || frames.end.is_some()) + { + outcome.partial.insert(sequence, group.handle()); + } } uncover(&self.fills, self.id, sequence..end); res From 86912564da68731d6f5d214128f9424c46e47745 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 27 Oct 2026 16:03:11 -0700 Subject: [PATCH 14/16] fix(moq-net): end uncached copies on track abort A bounded copy kept beside a whole tail, a displaced fetched head, and a displaced slot left the cache while a fetch or reader still held them, so a track abort missed them and their readers stayed pending. The track now tracks them weakly and ends those still being written on teardown. Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/model/group.rs | 38 ++++++++++++++ rs/moq-net/src/model/track.rs | 98 ++++++++++++++++++++++++++++++++--- 2 files changed, 129 insertions(+), 7 deletions(-) diff --git a/rs/moq-net/src/model/group.rs b/rs/moq-net/src/model/group.rs index 1f1546012f8..fd695c4d7cb 100644 --- a/rs/moq-net/src/model/group.rs +++ b/rs/moq-net/src/model/group.rs @@ -1097,6 +1097,33 @@ pub(crate) struct Handle { alive: Arc, } +/// A group the track no longer caches, held without keeping it open or allocated, so a +/// track abort can still end it while its writer adds to it. +pub(super) struct Detached { + state: kio::Weak, + info: Info, + track: track::Info, + cache: Arc, + alive: Arc, +} + +impl Detached { + /// The group, while it is neither finished nor aborted. + pub(super) fn writing(&self) -> Option { + let state = self.state.upgrade()?; + if state.read().fin.is_some() { + return None; + } + Some(Handle { + state, + info: self.info, + track: self.track.clone(), + cache: self.cache.clone(), + alive: self.alive.clone(), + }) + } +} + /// Whether a group failed, without holding it open or pinning its frames. pub(super) struct Fate(Arc); @@ -2230,6 +2257,17 @@ impl Handle { self.alive.cut.get() } + /// This group as [`Detached`], for the track to end once it stops caching it. + pub(super) fn detach(&self) -> Detached { + Detached { + state: self.state.downgrade(), + info: self.info, + track: self.track.clone(), + cache: self.cache.clone(), + alive: self.alive.clone(), + } + } + /// This group's [`Fate`], readable after the group and its frames are gone. pub(super) fn fate(&self) -> Fate { Fate(self.alive.clone()) diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 87dd5cca365..c366b25152e 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -325,6 +325,11 @@ pub(crate) struct TrackState { // every cached group for each delivery. lookup: BTreeMap, contents: group::Contents, + // Copies that left `lookup`, or never entered it, while a fetch or reader may still + // hold them, so a teardown ends those still being written. Pruned once it doubles + // past `detached_kept`, the count its last prune left. + detached: Vec, + detached_kept: usize, // Publisher-produced groups in arrival order as (sequence, stamp), walked by // subscriptions; an entry only resolves while its stamp matches the slot's. @@ -892,6 +897,25 @@ impl TrackState { Some(slot) } + /// [`Self::uncache`] a group that another copy displaces rather than one that ends, so a + /// teardown still ends the copies its readers hold. + fn displace(&mut self, sequence: u64) { + if let Some(slot) = self.uncache(sequence) { + for group in slot.copies() { + self.detach(group); + } + } + } + + /// Keep `group` for a teardown to end, though the cache does not hold it. + fn detach(&mut self, group: &group::Handle) { + self.detached.push(group.detach()); + if self.detached.len() > 2 * self.detached_kept { + self.detached.retain(|copy| copy.writing().is_some()); + self.detached_kept = self.detached.len(); + } + } + /// Record sequences the upstream publisher gave up, with `err`. fn drop_groups(&mut self, groups: ops::Range, err: StreamError) { if groups.is_empty() { @@ -1758,8 +1782,8 @@ impl TrackState { /// End the track abruptly with `err`: drop the open groups nobody will finish, keep the /// finished ones for readers still draining, and stop protecting the live edge. /// - /// Returns every unfinished copy, to abort once the track lock is released, so a reader - /// that already pulled one ends with `err` too and its writer fails. + /// Returns every unfinished copy, cached or not, to abort once the track lock is released, + /// so a reader that already pulled one ends with `err` too and its writer fails. fn tear_down(&mut self, err: Error) -> OpenGroups { // Snapshot the frame boundary before the open group it may sit in goes away: an // abrupt end is exactly when a replacement route asks where to resume. @@ -1770,6 +1794,7 @@ impl TrackState { .flat_map(Slot::copies) .filter(|group| !group.is_finished()) .cloned() + .chain(self.detached.drain(..).filter_map(|copy| copy.writing())) .collect(); let dropped = StreamError::from(&err); let open: Vec<_> = self @@ -1895,7 +1920,7 @@ impl TrackState { if self.covering_group(sequence, frame_start).is_some() { return Err(Error::Duplicate); } - self.uncache(sequence); + self.displace(sequence); Ok(()) } @@ -2473,7 +2498,7 @@ impl TrackState { .map(|(&sequence, _)| sequence) .collect(); for sequence in open { - self.uncache(sequence); + self.displace(sequence); } } @@ -2673,7 +2698,7 @@ impl TrackState { false => slot.serves_end(), }); if !kept && !self.lookup.get(&sequence).is_some_and(Slot::is_live) { - self.uncache(sequence); + self.displace(sequence); } let mut group = group::Producer::new(group::Info { sequence }, info, self.cache.clone()); @@ -2694,13 +2719,15 @@ impl TrackState { self.charge_debt(); let protected = self.protects(sequence); match self.lookup.get_mut(&sequence).filter(|slot| slot.is_live()) { - _ if kept => {} + _ if kept => self.detach(&group.handle()), Some(slot) => { // Joins the slot's standing: demoted already unless it is the live edge. if !protected { group.handle().cache_demote(); } - slot.head = Some(Box::new(group.handle())); + if let Some(head) = slot.head.replace(Box::new(group.handle())) { + self.detach(&head); + } } // Invisible to arrival-order subscribers: fetched on demand, not produced // live by the publisher. @@ -15518,6 +15545,63 @@ mod test { assert_eq!(sub.assert_group().with_range(0..).index(), 5); } + /// A track abort ends every copy still being written, including one the cache never held + /// or has since displaced: its reader gets the abort and its writer fails. + #[moq_clock::test] + #[cfg(feature = "async")] + async fn a_track_abort_ends_uncached_copies() { + let producer = track_producer("test", Info::timed()); + let dynamic = producer.requests(); + let consumer = producer.consume(); + let range = |start, end| FetchOptions { + options: group::Fetch { + frame_start: start, + ..Default::default() + }, + end, + }; + let fill = async |frames: FetchOptions| { + let fill = dynamic.requested_fill_group().await.unwrap(); + assert_eq!( + (fill.frame_start(), fill.frame_end()), + (frames.options.frame_start, frames.end) + ); + let mut group = fill.accept(Info::timed()).unwrap(); + group.write_frame(Timestamp::ZERO, b"copy".as_slice()).unwrap(); + group + }; + let fetch = async |sequence, frames: FetchOptions| { + let pending = consumer.fetch_range(sequence, frames.clone()); + let held = fill(frames).await; + (held, pending.await.unwrap()) + }; + let mut lives = Vec::new(); + for sequence in 0..2 { + let mut live = producer.create_group(group::Info { sequence }).unwrap(); + live.start_at(200).unwrap(); + live.write_frame(Timestamp::ZERO, b"live".as_slice()).unwrap(); + lives.push(live); + } + // A bounded copy beside a cached whole tail never enters the cache. + let _tail = fetch(0, range(5, None)).await; + let kept = fetch(0, range(0, Some(3))).await; + // A later copy displaces a live group's bounded head, and a fetched-only bounded copy. + let head = fetch(1, range(4, Some(10))).await; + let _wider = fetch(1, range(0, Some(3))).await; + let fetched = fetch(2, range(0, Some(3))).await; + let _wider = fetch(2, range(0, None)).await; + let mut detached = [kept, head, fetched]; + for (_, fetched) in &mut detached { + fetched.read_frame().await.unwrap().unwrap(); + } + + producer.abort(Error::Cancel).unwrap(); + for (mut held, mut fetched) in detached { + assert!(matches!(fetched.read_frame().now_or_never(), Some(Err(Error::Cancel)))); + assert!(held.write_frame(Timestamp::ZERO, b"late".as_slice()).is_err()); + } + } + /// A copy cut short at the back never answers a whole-group reader. A live group the /// publisher cut is still handed out as that cut, and one it cancelled stays a hole, /// whatever bounded copy of it remains. From 980c59c8b5cf5fa0e5477389e57a18322b649875 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 27 Oct 2026 16:41:49 -0700 Subject: [PATCH 15/16] fix(moq-net): keep finished bounded copies on track abort Teardown aborted every cached copy short of a whole finished group, so a bounded copy whose writer had finished failed a reader still draining it. It now aborts only copies still being written, as it already did for uncached ones. Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/model/group.rs | 17 ++++++++++------- rs/moq-net/src/model/track.rs | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/rs/moq-net/src/model/group.rs b/rs/moq-net/src/model/group.rs index fd695c4d7cb..aaaa927dec5 100644 --- a/rs/moq-net/src/model/group.rs +++ b/rs/moq-net/src/model/group.rs @@ -1110,17 +1110,14 @@ pub(super) struct Detached { impl Detached { /// The group, while it is neither finished nor aborted. pub(super) fn writing(&self) -> Option { - let state = self.state.upgrade()?; - if state.read().fin.is_some() { - return None; - } - Some(Handle { - state, + let handle = Handle { + state: self.state.upgrade()?, info: self.info, track: self.track.clone(), cache: self.cache.clone(), alive: self.alive.clone(), - }) + }; + (!handle.is_settled()).then_some(handle) } } @@ -2290,6 +2287,12 @@ impl Handle { self.state.read().range_end.is_some() } + /// Whether the writer finished this copy, which a transport bound may end short of the + /// group's end. + pub(super) fn is_settled(&self) -> bool { + self.state.read().fin.is_some() + } + /// Whether the group was finished: it holds every frame it will ever have. pub(crate) fn is_finished(&self) -> bool { { diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index c366b25152e..38e04265b77 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -1792,7 +1792,7 @@ impl TrackState { .lookup .values() .flat_map(Slot::copies) - .filter(|group| !group.is_finished()) + .filter(|group| !group.is_settled()) .cloned() .chain(self.detached.drain(..).filter_map(|copy| copy.writing())) .collect(); @@ -15602,6 +15602,39 @@ mod test { } } + /// A track abort keeps a finished bounded copy for the reader still draining it, as it + /// does a finished whole group. + #[moq_clock::test] + #[cfg(feature = "async")] + async fn a_track_abort_keeps_a_finished_bounded_copy() { + let producer = track_producer("test", Info::timed()); + let dynamic = producer.requests(); + let consumer = producer.consume(); + let fetch = consumer.fetch_range( + 0, + FetchOptions { + options: group::Fetch::default(), + end: Some(2), + }, + ); + let mut group = dynamic + .requested_fill_group() + .await + .unwrap() + .accept(Info::timed()) + .unwrap(); + for _ in 0..2 { + group.write_frame(Timestamp::ZERO, b"copy".as_slice()).unwrap(); + } + group.finish().unwrap(); + let mut fetched = fetch.await.unwrap(); + + producer.abort(Error::Cancel).unwrap(); + assert!(fetched.read_frame().await.unwrap().is_some()); + assert!(fetched.read_frame().await.unwrap().is_some()); + assert!(fetched.read_frame().await.unwrap().is_none()); + } + /// A copy cut short at the back never answers a whole-group reader. A live group the /// publisher cut is still handed out as that cut, and one it cancelled stays a hole, /// whatever bounded copy of it remains. From 65eaf83901e924cb7e65ac62b7919d3d67a64441 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 27 Oct 2026 17:24:56 -0700 Subject: [PATCH 16/16] fix(moq-net): let a finished bounded copy settle the track end A finished bounded fetch below a reached end still read as unfinished, so a later abort turned the deliberately finished track into an error. The settlement check now asks whether each cached copy's writer finished. Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/model/track.rs | 42 +++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 38e04265b77..1fa3ee98068 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -2427,13 +2427,14 @@ impl TrackState { } /// Whether the declared end is reached, the pending tail owes nothing below it, and - /// every cached group below it finished, so nothing the end promised is still in flight. + /// every cached copy below it finished, even one a transport bound cut short, so + /// nothing the end promised is still in flight. fn is_settled(&self) -> bool { let Some(fin) = self.final_sequence() else { return false; }; let reached = self.sealed || self.reaches(fin); - reached && !self.owes(fin) && self.lookup.range(..fin).all(|(_, slot)| slot.group.is_finished()) + reached && !self.owes(fin) && self.lookup.range(..fin).all(|(_, slot)| slot.group.is_settled()) } /// Record that the end settled before a write that could reopen it: an abort, a late @@ -15635,6 +15636,43 @@ mod test { assert!(fetched.read_frame().await.unwrap().is_none()); } + /// A finished bounded copy below the declared end is nothing in flight, so an abort after + /// the end is reached still ends the track cleanly. + #[moq_clock::test] + #[cfg(feature = "async")] + async fn a_finished_bounded_copy_lets_the_end_settle() { + let mut producer = track_producer("test", Info::timed()); + let dynamic = producer.requests(); + let consumer = producer.consume(); + let fetch = consumer.fetch_range( + 0, + FetchOptions { + options: group::Fetch::default(), + end: Some(1), + }, + ); + let fill = dynamic.requested_fill_group().await.unwrap(); + assert_eq!(fill.frame_end(), Some(1)); + let mut group = fill.accept(Info::timed()).unwrap(); + group.write_frame(Timestamp::ZERO, b"copy".as_slice()).unwrap(); + // The end is reached while the copy is still being written, so only the abort + // can find it settled. + producer + .create_group(group::Info { sequence: 1 }) + .unwrap() + .finish() + .unwrap(); + producer.finish_at(Position::group(2)).unwrap(); + group.finish().unwrap(); + fetch.await.unwrap(); + + producer.abort(Error::Timeout).unwrap(); + assert!(matches!( + consumer.poll_complete(&kio::Waiter::noop()), + Poll::Ready(Ok(())) + )); + } + /// A copy cut short at the back never answers a whole-group reader. A live group the /// publisher cut is still handed out as that cut, and one it cancelled stays a hole, /// whatever bounded copy of it remains.