diff --git a/drafts/draft-lcurley-moq-lite.md b/drafts/draft-lcurley-moq-lite.md index 027ac9ac54c..029f98648a6 100644 --- a/drafts/draft-lcurley-moq-lite.md +++ b/drafts/draft-lcurley-moq-lite.md @@ -501,7 +501,7 @@ An advertiser signals capacity through its route alone, withdrawing or re-pricin A subscriber opens Subscribe Streams to request a Track. The subscriber MUST start a Subscribe Stream with a SUBSCRIBE message followed by any number of SUBSCRIBE_UPDATE messages. -The publisher replies with a SUBSCRIBE_OK message once each Range's start is resolved, and a SUBSCRIBE_FINISH message once the Track's final group is known. +The publisher replies with a SUBSCRIBE_OK message once each Range's start is resolved, a SUBSCRIBE_STARTED message for each Range an update adds once its start is resolved, and a SUBSCRIBE_FINISH message once the Track's final group is known. For a live track the publisher MAY withhold SUBSCRIBE_OK until the first matching group resolves the start, and withholds the SUBSCRIBE_DROP messages for groups it gives up meanwhile until it sends SUBSCRIBE_OK. It MUST NOT withhold it past the point where no group can resolve it: once every Group in a subscription whose every Range has an `End` other than None has been given up, or once the Track has finished, the publisher sends SUBSCRIBE_OK and then the SUBSCRIBE_DROP messages it withheld. A Track that finished before producing any Group is the one case with nothing to resolve: SUBSCRIBE_FINISH with a Group of 0 needs no SUBSCRIBE_OK. @@ -515,10 +515,14 @@ The subscriber needs the track's TRACK_INFO (notably its timescale) to interpret The publisher sends SUBSCRIBE_OK once the start is resolved, and SUBSCRIBE_FINISH as soon as the exclusive final group is known (see [SUBSCRIBE_OK](#subscribe-ok) and [SUBSCRIBE_FINISH](#subscribe-finish)). Neither closes the stream: the publisher keeps reading SUBSCRIBE_UPDATE messages, so an update can still widen the Ranges, including into an ended Track's history, and the publisher serves or drops what it newly covers. -The subscriber judges a Range complete from what it observed: the Groups it received, the SUBSCRIBE_DROP messages, the Range Starts in SUBSCRIBE_OK, and SUBSCRIBE_FINISH. +The subscriber judges a Range complete from what it observed: the Groups it received, the SUBSCRIBE_DROP messages, the Range Starts in SUBSCRIBE_OK and SUBSCRIBE_STARTED, and SUBSCRIBE_FINISH. Drops and Groups name absolute sequences, so the subscriber judges them against its latest Ranges, and an update still in flight changes nothing it has counted. The publisher never closes its side first: a subscription whose every Range has an `End` other than None and whose every Group has been delivered or dropped stays open for an update to widen it. +A subscriber fills Groups it misses by asking for them as Ranges, adding them to an open subscription with SUBSCRIBE_UPDATE or opening a bounded SUBSCRIBE when none is open, rather than with a FETCH of each Group; FETCH remains for one Group, or a slice of one. +A publisher never sends a Group twice on one subscription, so a Group the subscription already delivered or named in SUBSCRIBE_DROP is asked for with a SUBSCRIBE of its own. +The subscriber asks that way, once, for a Group a subscription dropped with OLD that it still wants, with a `Subscriber Max Age` its age does not reach, and never for a Group that may still arrive on the existing subscription. + The subscriber closes its side (FIN) once it wants nothing more from the subscription, and the publisher then closes its side (FIN), cancelling any Group Stream still open. After SUBSCRIBE_FINISH, the subscriber SHOULD close its side once its tail accounting has settled: every received Group Stream has been read to its FIN, reset, or dropped, and every sequence in range has arrived, been named by SUBSCRIBE_DROP, or exhausted the subscriber's grace period. A publisher closing its session gracefully MUST wait for the subscriber's FIN or reset on every Subscribe Stream before closing the connection; a transport acknowledgement alone does not confirm the subscriber read the tail. @@ -1308,6 +1312,7 @@ A value of 0 is a PROTOCOL_VIOLATION, since a subscription that asks for nothing **Range**: A span of the Track to deliver, from its `Start` up to its `End`. The subscription is the union of its Ranges: the publisher delivers each Group inside any of them once, in `Group Order` across all of them, and accounts with SUBSCRIBE_DROP for every Group it will not deliver. +Each Range implicitly takes the next Range ID on the stream, starting at 0, which a [SUBSCRIBE_UPDATE](#subscribe-update) uses to retire it. **Anchor Type**: Which fields follow, and what the Anchor names: @@ -1319,6 +1324,7 @@ Which fields follow, and what the Anchor names: Any other value is a PROTOCOL_VIOLATION. The publisher resolves a Relative or Time Anchor against the Track as it holds it. +A Range resolves once: an update that leaves it live does not resolve it again. A `Start` is a floor, not a request; `Subscriber Max Age` is the only thing that asks for data. The publisher SHOULD start at the oldest group at or above the floor that [Expiration](#expiration) has not expired, and MUST NOT deliver an older one. @@ -1331,10 +1337,11 @@ An Absolute `Start` past frame 0 delivers its group from that frame; a start res 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 +## SUBSCRIBE_UPDATE {#subscribe-update} A subscriber can modify a subscription with a SUBSCRIBE_UPDATE message. A subscriber MAY send multiple SUBSCRIBE_UPDATE messages to update the subscription. -Its Ranges replace the previous ones, growing or shrinking the subscription. +Every field but the Ranges restates the subscription, and the latest one applies. +The Ranges are an edit, growing or shrinking the subscription: the update retires the live Ranges it names by Range ID, then adds its own, each taking the next Range ID. Lowering a `Start` covers the groups below the old one that the publisher still holds, not only those that arrive later; a group already sent is not sent again. ~~~ @@ -1344,6 +1351,8 @@ SUBSCRIBE_UPDATE Message { Group Order (8) Subscriber Max Age (i) Subscriber Min Age (i) + Remove Count (i) + Range ID (i) ... Range Count (i) Range (..) ... Wanted Group (i) @@ -1353,6 +1362,14 @@ SUBSCRIBE_UPDATE Message { See [SUBSCRIBE](#subscribe) for information about each field. Moving a `Start` forward within a group that is already being delivered does not retract frames the publisher has already sent; it only bounds what the publisher sends from here on. +**Remove Count** and **Range ID**: +The live Ranges to retire. +A Range ID that was never assigned, or is already retired, including earlier in the same list, is a PROTOCOL_VIOLATION. + +**Range Count** and **Range**: +The Ranges to add, which may be none. +The live Ranges after the edit MUST NOT be empty, and MUST fit in one SUBSCRIBE: once encoded they take at most 65,535 bytes. +A publisher closes the session with a PROTOCOL_VIOLATION otherwise. **Wanted Group**: The newest group the subscriber's live-edge joins want, encoded as the sequence plus one; `0` means none. @@ -1572,11 +1589,12 @@ The largest position the publisher knows of for the track when it answers: the l A subscriber takes it as where the live feed is: a relay holding groups from an earlier subscription serves them from cache only once this says they are current, which a publisher answering promptly lets it do without waiting for a frame. **Range Start**: -One per Range the publisher is serving, in order: the Group where the Range's delivery starts, plus one, so a subscriber learns where a Relative or Time Anchor landed. -0 means the Range has no Group to deliver. +One per Range of the SUBSCRIBE, in Range ID order: the Group where the Range's delivery starts, plus one, so a subscriber learns where a Relative or Time Anchor landed. +0 means the Range has no Group to deliver, including one an update retired before the answer. The Range's Groups from this one are owed as described in [SUBSCRIBE_DROP](#subscribe-drop); an earlier one is not, though it MAY still arrive. A Range whose `Range Start` is its requested start Group begins at the requested frame, and otherwise at frame 0 (see [Positions](#positions)). -The Ranges are those of the SUBSCRIBE unless the publisher applied a SUBSCRIBE_UPDATE before answering; a subscriber that sent none MUST treat a count other than its number of Ranges as a PROTOCOL_VIOLATION. +A subscriber MUST treat a `Range Start Count` other than the SUBSCRIBE's `Range Count` as a PROTOCOL_VIOLATION. +A Range an update adds has its start reported by [SUBSCRIBE_STARTED](#subscribe-started) instead, even one added before SUBSCRIBE_OK. ## SUBSCRIBE_FINISH {#subscribe-finish} A SUBSCRIBE_FINISH message is sent by the publisher to signal that no group at or after a given sequence will be produced. @@ -1605,6 +1623,32 @@ Groups below the end stay deliverable either way; a subscriber reaching the end The publisher SHOULD send SUBSCRIBE_FINISH as soon as the exclusive final group is known, even while lower groups are still in flight. +## SUBSCRIBE_STARTED {#subscribe-started} +A SUBSCRIBE_STARTED message resolves where a Range that a [SUBSCRIBE_UPDATE](#subscribe-update) added starts. + +~~~ +SUBSCRIBE_STARTED Message { + Type (i) = 0x4 + Message Length (i) + Range ID (i) + Range Start (i) +} +~~~ + +**Type**: +Set to 0x4 to indicate a SUBSCRIBE_STARTED message. + +**Range ID**: +The Range it resolves. +A Range ID the subscription never assigned, one of the SUBSCRIBE's Ranges, or one already resolved is a PROTOCOL_VIOLATION. +A Range the subscriber has since retired is ignored. + +**Range Start**: +As in [SUBSCRIBE_OK](#subscribe-ok), and the Range's Groups from it are owed the same way. + +The publisher sends it after SUBSCRIBE_OK, once the Range's start is resolved, and MUST NOT withhold it past the point where no group can resolve it, as for SUBSCRIBE_OK. +A Range retired before its start resolved gets none. + ## SUBSCRIBE_DROP {#subscribe-drop} A publisher names groups it will not deliver with SUBSCRIBE_DROP on the Subscribe Stream. @@ -1947,6 +1991,9 @@ The `Message Length` describes the payload size on the wire. - SUBSCRIBE may start at any frame, including when the subscriber no longer holds the prefix. - A SUBSCRIBE_UPDATE that lowers a `Start` covers the groups below the old start that the publisher still holds, not only those that arrive later. A group already sent is not sent again. - Replaced SUBSCRIBE_OK `Group` with a `Range Start` per Range: the Group where that Range's delivery starts, including at a Relative or Time Anchor, from which its Groups are owed. +- Each Range takes an implicit Range ID, and SUBSCRIBE_UPDATE edits the live Ranges instead of restating them: `Remove Count` Range IDs to retire, then the Ranges to add. The live Ranges must stay non-empty and fit in one SUBSCRIBE. A Range resolves its Anchors once, however many updates leave it live. +- SUBSCRIBE_OK's Range Starts answer the SUBSCRIBE's Ranges, and added SUBSCRIBE_STARTED (type 0x4) resolving where each Range an update adds starts. +- A subscriber fills missing Groups through Ranges on a subscription instead of a FETCH per Group, and asks for a Group a subscription already delivered or dropped, including one dropped with OLD, with a SUBSCRIBE of its own. - Added `Largest Group` and `Largest Frame` to SUBSCRIBE_OK: the publisher's largest position when it answers, which a subscriber takes as where the live feed is. A publisher MUST answer at once when the requested start is past it. - Renamed SUBSCRIBE_END to SUBSCRIBE_FINISH, matching TRACKS_FINISH: it ends the Track, not the subscription. - Added `Error Code` to SUBSCRIBE_FINISH: a track can end with an error while keeping the groups below its end. diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index 2afb389a738..cd477c7a208 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -30,7 +30,7 @@ import type { } from "./track.ts"; import { max, min } from "./util/sequence.ts"; import * as Varint from "./varint.ts"; -import type { Window } from "./window.ts"; +import type { Resolved, Window } from "./window.ts"; import type { Advertised, Advertisements } from "./wire.ts"; /** What a null {@link Delay.max} goes out as: the largest value every peer can read, as Rust sends for `None`. */ @@ -375,6 +375,8 @@ export interface ResumeSource { end(): Position | undefined; /** Where each range starts on the serving copy once delivery starts at `from`, undefined without one. */ starts(from: bigint): (bigint | undefined)[] | undefined; + /** Each range as the serving copy resolved it, undefined without one. */ + resolved(): (Resolved | undefined)[] | undefined; /** The runs of `[start, end)` the serving copy reads, undefined without one. */ runs(start: bigint, end: bigint): Run[] | undefined; /** @@ -586,6 +588,11 @@ export const hooks: { * with no group there: what lite-07 SUBSCRIBE_START answers. Assigned by `track.ts`. */ servingStarts: (subscriber: Subscriber, from: bigint) => (bigint | undefined)[]; + /** + * Each of a subscriber's ranges as resolved where it is served, undefined for one whose start + * still waits: what lite-07 SUBSCRIBE_STARTED answers. Assigned by `track.ts`. + */ + servingResolved: (subscriber: Subscriber) => (Resolved | undefined)[]; /** The lowest buffered candidate, which an ordered take may wait below; `floor` passes sequences taken on other copies. */ peekGroup: (subscriber: Subscriber, ordered: boolean, floor?: bigint) => bigint | undefined; /** Take the next group in sequence order without waiting, as {@link tryRecvGroup} does in arrival order. */ @@ -795,6 +802,9 @@ export const hooks: { servingStarts: () => { throw new Error("track.ts not loaded"); }, + servingResolved: () => { + throw new Error("track.ts not loaded"); + }, makeRequest: () => { throw new Error("track.ts not loaded"); }, diff --git a/js/net/src/lite/connection.test.ts b/js/net/src/lite/connection.test.ts index 7af5f8d8db9..f3bf2b621d3 100644 --- a/js/net/src/lite/connection.test.ts +++ b/js/net/src/lite/connection.test.ts @@ -20,7 +20,7 @@ import { decodeSubscribeResponse, encodeSubscribeResponse, Subscribe, - SubscribeEnd, + SubscribeFinish, SubscribeStart, } from "./subscribe.ts"; import { TrackInfo, Track as TrackMessage } from "./track.ts"; @@ -539,9 +539,9 @@ async function servedRawSubscription(alpn: string = ALPN_07_WIP, version: Versio expect("start" in (await decodeSubscribeResponse(subscriber.reader, version))).toBe(true); producer.finish(); producer.close(); - // SUBSCRIBE_END, then before lite-07 the publisher's FIN; lite-07 keeps the stream open. + // SUBSCRIBE_FINISH, then before lite-07 the publisher's FIN; lite-07 keeps the stream open. if (keepsSubscribeOpen(version)) { - while (!("end" in (await decodeSubscribeResponse(subscriber.reader, version)))) {} + while (!("finish" in (await decodeSubscribeResponse(subscriber.reader, version)))) {} } else await subscriber.reader.readAll(); let closed = false; @@ -561,7 +561,7 @@ async function servedRawSubscription(alpn: string = ALPN_07_WIP, version: Versio } // The publisher's FIN says nothing about whether the subscriber read the tail, and a receiver -// may drop unread bytes on close. From lite-05, whose SUBSCRIBE_END lets the subscriber settle +// may drop unread bytes on close. From lite-05, whose SUBSCRIBE_FINISH lets the subscriber settle // its tail at once, the subscriber's FIN says so, and close waits for it. for (const [alpn, version] of [ [ALPN_05, Version.DRAFT_05], @@ -718,8 +718,8 @@ test("close waits for a request served while withdrawals are in flight", async ( expect("start" in (await decodeSubscribeResponse(subscriber.reader, version))).toBe(true); producer.finish(); producer.close(); - // SUBSCRIBE_END; lite-07 keeps the stream open past it. - while (!("end" in (await decodeSubscribeResponse(subscriber.reader, version)))) {} + // SUBSCRIBE_FINISH; lite-07 keeps the stream open past it. + while (!("finish" in (await decodeSubscribeResponse(subscriber.reader, version)))) {} // The withdrawal completes, but the subscription still waits for the subscriber's FIN. fin.release(); @@ -902,7 +902,7 @@ for (const violation of ["a partial group", "a group past the declared end"] as // A received tail uses its wire position even without a locally retained prefix. group = new GroupMessage({ subscribe: 0n, sequence: 0n, frameStart: 5 }); } else { - await encodeSubscribeResponse(sub.writer, { end: new SubscribeEnd(2n) }, version); + await encodeSubscribeResponse(sub.writer, { finish: new SubscribeFinish(2n) }, version); group = new GroupMessage({ subscribe: 0n, sequence: 2n }); } const writer = await Writer.open(pair.server, { version }); diff --git a/js/net/src/lite/publisher.test.ts b/js/net/src/lite/publisher.test.ts index 91bd928c19a..f2f229045e7 100644 --- a/js/net/src/lite/publisher.test.ts +++ b/js/net/src/lite/publisher.test.ts @@ -26,11 +26,11 @@ import { decodeSubscribeResponse, decodeSubscribeResponseMaybe, Subscribe, - type SubscribeEnd, + type SubscribeFinish, SubscribeUpdate, } from "./subscribe.ts"; import { Track as TrackMessage } from "./track.ts"; -import { ALPN_05, ALPN_06, ALPN_07_WIP, keepsSubscribeOpen, Version } from "./version.ts"; +import { ALPN_05, ALPN_06, ALPN_07_WIP, hasRanges, keepsSubscribeOpen, Version } from "./version.ts"; function publish(origin: OriginProducer, path: Path.Valid) { const broadcastPublication = origin.createBroadcast(path); @@ -50,6 +50,16 @@ function replayUpdate(props: ConstructorParameters[0]) { return new SubscribeUpdate({ ...props, maxDelay: TEST_MAX_DELAY_MS }); } +// A lite-07 update that keeps its ranges, which edits none of them. +function keep(version: Version) { + return hasRanges(version) ? { ranges: [] } : {}; +} + +// A lite-07 update restating its span retires the range it replaces, the one with Range ID `id`. +function restate(version: Version, id = 0n) { + return hasRanges(version) ? { remove: [id] } : {}; +} + test.each([Version.DRAFT_01, Version.DRAFT_03, Version.DRAFT_06])( "an initial announcement write failure disposes its listener in version %s", async (version) => { @@ -191,12 +201,12 @@ test.each([Version.DRAFT_06, Version.DRAFT_07])( ); // Delivers `sequences` in the given order, finishes the track (with `error`, if given), and -// returns the SUBSCRIBE_END the publisher put on the wire. +// returns the SUBSCRIBE_FINISH the publisher put on the wire. async function subscribeEnd( sequences: number[], version: Version = Version.DRAFT_05, error?: Error, -): Promise { +): Promise { const pair = createMockTransportPair(version === Version.DRAFT_07 ? ALPN_07_WIP : ALPN_05); const origin = new OriginProducer(); const publisher = new Publisher(pair.server, version, randomHop(), origin.consume()); @@ -230,7 +240,7 @@ async function subscribeEnd( try { for (;;) { const resp = await decodeSubscribeResponse(client.reader, version); - if ("end" in resp) return resp.end; + if ("finish" in resp) return resp.finish; } } finally { publisher.close(); @@ -296,7 +306,7 @@ async function groupSendOrders(options: { try { for (const [index, sequence] of sequences.entries()) { if (index === 1 && update !== undefined) { - await replayUpdate({ priority: update }).encode(client.writer, version); + await replayUpdate({ priority: update, ...keep(version) }).encode(client.writer, version); // Wait for the publisher to apply it, rather than assuming it beat the next group. while (track.subscription.peek()?.priority !== update) await track.subscription.changed(); } @@ -896,6 +906,138 @@ test("lite-07 SUBSCRIBE_START names each resolved range start", async () => { } }); +// A range an update adds learns where it starts from SUBSCRIBE_STARTED, and keeps that start however +// many later updates leave it live, rather than counting back from a newer latest group. +test("lite-07 SUBSCRIBE_STARTED reports an added range's start, which later updates keep", async () => { + const version = Version.DRAFT_07; + const sub = await servedSubscription({ + version, + prefill: [0n, 1n, 2n, 3n, 4n], + maxAge: Milli(3_600_000), + ranges: [{ start: { absolute: 0n }, end: { absolute: 1n } }], + }); + const next = async () => { + for (;;) { + const resp = await decodeSubscribeResponse(sub.client.reader, version); + if ("start" in resp || "started" in resp) return resp; + } + }; + try { + const start = await next(); + if (!("start" in start)) throw new Error("expected SUBSCRIBE_OK"); + expect(start.start.starts).toEqual([0n]); + + // Two groups back from the latest, group 2, which takes Range ID 1. + await replayUpdate({ priority: 0, ranges: [{ start: { relative: 2n } }] }).encode(sub.client.writer, version); + const started = await next(); + if (!("started" in started)) throw new Error("expected SUBSCRIBE_STARTED"); + expect([started.started.id, started.started.start]).toEqual([1n, 2n]); + + // Newer groups arrive, and another update leaves the range live: it still starts at 2. + for (const sequence of [5n, 6n, 7n]) sub.serve(sequence); + await replayUpdate({ priority: 0, ranges: [{ start: { absolute: 9n } }] }).encode(sub.client.writer, version); + const pinned = await next(); + if (!("started" in pinned)) throw new Error("expected SUBSCRIBE_STARTED"); + expect([pinned.started.id, pinned.started.start]).toEqual([2n, 9n]); + const ranges = sub.track.subscription.peek()?.ranges ?? []; + expect(ranges.some((range) => range.start?.absolute === 2n)).toBe(true); + expect(ranges.some((range) => range.start?.relative !== undefined)).toBe(false); + } finally { + await sub.close(); + } +}); + +// A time start resolves once too: a newer group starting before its time doesn't move it once +// an update leaves the range live. Mirrors the Rust `lite07_an_added_time_start_keeps_its_start`. +test("lite-07 keeps where an added time start resolved", async () => { + const version = Version.DRAFT_07; + const sub = await servedSubscription({ + version, + prefill: [0n, 1n, 2n, 3n, 4n], + maxAge: Milli(3_600_000), + ranges: [{ start: { absolute: 0n }, end: { absolute: 1n } }], + }); + const next = async () => { + for (;;) { + const resp = await decodeSubscribeResponse(sub.client.reader, version); + if ("start" in resp || "started" in resp) return resp; + } + }; + try { + const start = await next(); + if (!("start" in start)) throw new Error("expected SUBSCRIBE_OK"); + + // 10 ms falls in group 4, the newest one starting at or before it. + await replayUpdate({ priority: 0, ranges: [{ start: { time: 10 } }] }).encode(sub.client.writer, version); + const started = await next(); + if (!("started" in started)) throw new Error("expected SUBSCRIBE_STARTED"); + expect([started.started.id, started.started.start]).toEqual([1n, 4n]); + + // Group 5 starts at 5 ms, before 10 ms, and another update leaves the range live: it + // still starts at 4. + sub.serve(5n); + await replayUpdate({ priority: 0, ranges: [{ start: { absolute: 9n } }] }).encode(sub.client.writer, version); + const pinned = await next(); + if (!("started" in pinned)) throw new Error("expected SUBSCRIBE_STARTED"); + expect(pinned.started.id).toBe(2n); + const ranges = sub.track.subscription.peek()?.ranges ?? []; + expect(ranges.some((range) => range.start?.absolute === 4n)).toBe(true); + expect(ranges.some((range) => range.start?.time !== undefined)).toBe(false); + } finally { + await sub.close(); + } +}); + +// Group 0 included: a time start that resolved to group 0 names it, so a newer group starting +// before its time doesn't move it either. +test("lite-07 keeps an added time start that resolved to group 0", async () => { + const version = Version.DRAFT_07; + const sub = await servedSubscription({ + version, + prefill: [0n], + maxAge: Milli(3_600_000), + ranges: [{ start: { absolute: 0n }, end: { absolute: 1n } }], + }); + const next = async () => { + for (;;) { + const resp = await decodeSubscribeResponse(sub.client.reader, version); + if ("start" in resp || "started" in resp) return resp; + } + }; + try { + if (!("start" in (await next()))) throw new Error("expected SUBSCRIBE_OK"); + + // 10 ms falls in group 0, the only group. + await replayUpdate({ priority: 0, ranges: [{ start: { time: 10 } }] }).encode(sub.client.writer, version); + const started = await next(); + if (!("started" in started)) throw new Error("expected SUBSCRIBE_STARTED"); + expect([started.started.id, started.started.start]).toEqual([1n, 0n]); + + // Group 1 starts at 1 ms, before 10 ms; the range still starts at group 0. + sub.serve(1n); + await replayUpdate({ priority: 0, ranges: [{ start: { absolute: 9n } }] }).encode(sub.client.writer, version); + if (!("started" in (await next()))) throw new Error("expected SUBSCRIBE_STARTED"); + const ranges = sub.track.subscription.peek()?.ranges ?? []; + expect(ranges.some((range) => range.start?.time !== undefined)).toBe(false); + expect(ranges.some((range) => range.start?.absolute === 0n && range.end === undefined)).toBe(true); + } finally { + await sub.close(); + } +}); + +// An edit naming a Range ID the subscription never assigned violates the protocol. +test("lite-07 refuses an update retiring an unknown Range ID", async () => { + const version = Version.DRAFT_07; + const sub = await servedSubscription({ version, prefill: [0n], ranges: [{ start: { absolute: 0n } }] }); + try { + await replayUpdate({ priority: 0, ranges: [], remove: [1n] }).encode(sub.client.writer, version); + await sub.serving; + await expect(sub.client.reader.closed).rejects.toThrow(); + } finally { + await sub.close(); + } +}); + // A bounded window that resolves empty has no group to answer it, so the publisher answers it // at once. The stream stays open for an update until the subscriber FINs, and the publisher // FINs in reply. @@ -1039,7 +1181,7 @@ for (const version of [Version.DRAFT_06, Version.DRAFT_07]) { try { sub.serve(5n); expect(await sub.servedSequence()).toBe(5n); - await replayUpdate({ priority: 0, startGroup: 2n }).encode(sub.client.writer, version); + await replayUpdate({ priority: 0, startGroup: 2n, ...restate(version) }).encode(sub.client.writer, version); await flush(); sub.track.finish(); sub.track.close(); @@ -1048,8 +1190,8 @@ for (const version of [Version.DRAFT_06, Version.DRAFT_07]) { const resp = await decodeSubscribeResponseMaybe(sub.client.reader, version); if (!resp) break; if ("drop" in resp) codes.push(resp.drop.error); - // Lite-07 keeps the stream open past SUBSCRIBE_END, after the sweep's drops. - if ("end" in resp && keepsSubscribeOpen(version)) break; + // Lite-07 keeps the stream open past SUBSCRIBE_FINISH, after the sweep's drops. + if ("finish" in resp && keepsSubscribeOpen(version)) break; } expect(codes).toEqual([version === Version.DRAFT_07 ? StreamCode.NotFound : 0]); } finally { @@ -1110,17 +1252,25 @@ test.each([Version.DRAFT_06, Version.DRAFT_07])("lite %s: datagrams honor group send([0, 2, 1]); expect(await recv()).toBe(1n); // Widen both boundaries, then narrow to one datagram. - await replayUpdate({ priority: 0, startGroup: 0n, startFrame: 0, endGroup: 4n, endFrame: 0 }).encode( - sub.client.writer, - version, - ); + await replayUpdate({ + priority: 0, + startGroup: 0n, + startFrame: 0, + endGroup: 4n, + endFrame: 0, + ...restate(version), + }).encode(sub.client.writer, version); await applied(5n); send([2, 3, 5, 4]); expect([await recv(), await recv(), await recv()]).toEqual([2n, 3n, 4n]); - await replayUpdate({ priority: 0, startGroup: 6n, startFrame: 1, endGroup: 7n, endFrame: 0 }).encode( - sub.client.writer, - version, - ); + await replayUpdate({ + priority: 0, + startGroup: 6n, + startFrame: 1, + endGroup: 7n, + endFrame: 0, + ...restate(version, 1n), + }).encode(sub.client.writer, version); await applied(8n); send([6, 8, 7]); expect(await recv()).toBe(7n); @@ -1874,7 +2024,7 @@ test("lite draft-07: subscribe end precedes groups below a declared finish", asy expect("start" in start).toBe(true); const pending = decodeSubscribeResponse(client.reader, Version.DRAFT_07); const early = await pending; - expect("end" in early).toBe(true); + expect("finish" in early).toBe(true); const second = track.createGroup(1n); second.writeFrame(textFrame("hello")); @@ -1883,15 +2033,15 @@ test("lite draft-07: subscribe end precedes groups below a declared finish", asy track.close(); const resp = await pending; - if (!("end" in resp)) throw new Error("expected SUBSCRIBE_END"); - expect(resp.end.group).toBe(2n); + if (!("finish" in resp)) throw new Error("expected SUBSCRIBE_FINISH"); + expect(resp.finish.group).toBe(2n); } finally { publisher.close(); client.close(); } }); -// Before lite-07 an errored end goes out only as the reset at that end: a clean SUBSCRIBE_END +// Before lite-07 an errored end goes out only as the reset at that end: a clean SUBSCRIBE_FINISH // sent early could let the subscriber settle before the reset arrives. test("lite draft-06: an errored end is not declared early", async () => { const pair = createMockTransportPair(ALPN_06); @@ -1944,8 +2094,8 @@ test("lite draft-06: a subscription capped short of an errored end gets its boun sub.track.finishAt({ group: 4n, frame: 0 }, new StreamError(StreamCode(71))); for (;;) { const resp = await decodeSubscribeResponse(sub.client.reader, Version.DRAFT_06); - if (!("end" in resp)) continue; - expect(resp.end.group).toBe(4n); + if (!("finish" in resp)) continue; + expect(resp.finish.group).toBe(4n); break; } } finally { @@ -1954,7 +2104,7 @@ test("lite draft-06: a subscription capped short of an errored end gets its boun }); // Serves one group with its stream open held until `open(ok)`, and returns the pending -// SUBSCRIBE_END plus the call that lets the open succeed or fail. +// SUBSCRIBE_FINISH plus the call that lets the open succeed or fail. async function heldOpenEnd() { const pair = createMockTransportPair(ALPN_07_WIP); const origin = new OriginProducer(); @@ -2006,12 +2156,12 @@ test("lite draft-07: subscribe end precedes a blocked stream open", async () => const held = await heldOpenEnd(); try { const early = await held.end; - expect("end" in early).toBe(true); + expect("finish" in early).toBe(true); held.open(true); const resp = await held.end; - if (!("end" in resp)) throw new Error("expected SUBSCRIBE_END"); - expect(resp.end.group).toBe(1n); + if (!("finish" in resp)) throw new Error("expected SUBSCRIBE_FINISH"); + expect(resp.finish.group).toBe(1n); } finally { held.close(); } @@ -2023,8 +2173,8 @@ test("lite draft-07: subscribe end precedes a failed stream open", async () => { try { held.open(false); const resp = await held.end; - if (!("end" in resp)) throw new Error("expected SUBSCRIBE_END"); - expect(resp.end.group).toBe(1n); + if (!("finish" in resp)) throw new Error("expected SUBSCRIBE_FINISH"); + expect(resp.finish.group).toBe(1n); const dropped = await held.next(); if (!("drop" in dropped)) throw new Error("expected SUBSCRIBE_DROP"); expect([dropped.drop.start, dropped.drop.end, dropped.drop.error]).toEqual([0n, 0n, 0]); @@ -2071,7 +2221,7 @@ test("lite draft-07: a group whose close fails is dropped", async () => { const start = await decodeSubscribeResponse(client.reader, Version.DRAFT_07); expect("start" in start).toBe(true); const end = await decodeSubscribeResponse(client.reader, Version.DRAFT_07); - if (!("end" in end)) throw new Error("expected SUBSCRIBE_END"); + if (!("finish" in end)) throw new Error("expected SUBSCRIBE_FINISH"); const dropped = await decodeSubscribeResponse(client.reader, Version.DRAFT_07); if (!("drop" in dropped)) throw new Error("expected SUBSCRIBE_DROP"); expect([dropped.drop.start, dropped.drop.end, dropped.drop.error]).toEqual([0n, 0n, 0]); @@ -2133,7 +2283,7 @@ test("blocked DROP responses stop opening later groups", async () => { track.finish(); track.close(); let dropped = 0; - // Lite-07 keeps the stream open past SUBSCRIBE_END, so read until every group is named. + // Lite-07 keeps the stream open past SUBSCRIBE_FINISH, so read until every group is named. while (dropped < 72) { const response = await decodeSubscribeResponseMaybe(client.reader, Version.DRAFT_07); if (!response) break; @@ -2159,7 +2309,7 @@ type Served = { sequence: bigint; 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. A group in `received` arrived as a tail + * SUBSCRIBE_START / SUBSCRIBE_FINISH range. A group in `received` arrived as a tail * starting at that frame. */ async function serve( @@ -2237,8 +2387,8 @@ async function serve( for (;;) { const resp = await decodeSubscribeResponse(client.reader, Version.DRAFT_06); if ("start" in resp) start = resp.start.starts[0]; - if ("end" in resp) { - end = resp.end.group; + if ("finish" in resp) { + end = resp.finish.group; break; } } @@ -2316,7 +2466,7 @@ test("lite draft-06: groups past the end group are not served", async () => { expect(served.map((s) => s.sequence)).toEqual([1n, 2n]); }); -// SUBSCRIBE_END names the track's exclusive final boundary, not the capped delivered +// SUBSCRIBE_FINISH names the track's exclusive final boundary, not the capped delivered // range: a Rust peer feeds it into finish_at, so a truncated value would silently drop // the held-back groups across languages. test("lite draft-06: a capped subscription still reports the track's final boundary", async () => { @@ -2497,7 +2647,7 @@ test("lite draft-05: a group waiting for a stream slot is dropped when the subsc }); // The publisher FINs the subscribe stream itself once a track ends, which must not be -// mistaken for the subscriber leaving: SUBSCRIBE_END counts those queued groups as +// mistaken for the subscriber leaving: SUBSCRIBE_FINISH counts those queued groups as // delivered, so dropping them here would strand the tail of every finite track. The FIN // tells the subscriber every group is accounted for, so it waits for the queued group. test("lite draft-05: a group waiting for a stream slot survives the track finishing", async () => { @@ -2506,10 +2656,10 @@ test("lite draft-05: a group waiting for a stream slot survives the track finish track.finish(); track.close(); - // SUBSCRIBE_END goes out while the group is still waiting for its slot. + // SUBSCRIBE_FINISH goes out while the group is still waiting for its slot. for (;;) { const resp = await decodeSubscribeResponse(client.reader, Version.DRAFT_05); - if ("end" in resp) break; + if ("finish" in resp) break; } // The FIN holds until the queued group is on the wire. diff --git a/js/net/src/lite/publisher.ts b/js/net/src/lite/publisher.ts index 89ed83f74fd..f384269fe29 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -33,6 +33,7 @@ import { untilAborted } from "../util/abort.ts"; import { nextChange } from "../util/change.ts"; import { GivenUp } from "../util/given_up.ts"; import { max, min } from "../util/sequence.ts"; +import type { Resolved } from "../window.ts"; import { type Advertised, type Advertisements, sameInstance, wireOf } from "../wire.ts"; import { AnnounceInit, AnnounceOk, type AnnounceRequest, encodeAnnounceBroadcast } from "./announce.ts"; import { Datagram as DatagramMessage } from "./datagram.ts"; @@ -42,14 +43,19 @@ import { Group as GroupMessage } from "./group.ts"; import { Probe } from "./probe.ts"; import { encodeSubscribeResponse, + entriesOf, hasWireTime, + LiveRanges, + type RangeEntry, requestedRanges, type Subscribe, SubscribeDrop, - SubscribeEnd, + SubscribeFinish, SubscribeOk, SubscribeStart, + SubscribeStarted, SubscribeUpdate, + spanOf, } from "./subscribe.ts"; import { TrackInfo as TrackInfoMessage, type Track as TrackMessage } from "./track.ts"; import { encodeTracksEvent, type Tracks as TracksMessage } from "./tracks.ts"; @@ -240,8 +246,11 @@ type ServeFetch = Omit; /** What the serving loop takes from the subscribe stream instead of serving a group. */ type Control = - /** The peer re-stated the subscription: priority, ordering, latency, and both ranges. */ - | { kind: "update"; update: SubscribeUpdate } + /** + * The peer re-stated the subscription: priority, ordering, latency, and its ranges, with the + * Range ID of each on lite-07. + */ + | { kind: "update"; update: SubscribeUpdate; ids: bigint[] } /** The peer FIN'd, or our own half went away. Either way there is nobody left to serve. */ | { kind: "done" } /** The stream failed; the subscription goes down with it. */ @@ -252,7 +261,10 @@ type SubscriptionControlOptions = { reader: Reader; writer: Writer; version: Version; - apply: (update: SubscribeUpdate) => void; + /** The SUBSCRIBE's ranges, which each lite-07 update edits. */ + live: LiveRanges; + /** Apply an update, with every live range by Range ID on lite-07. */ + apply: (update: SubscribeUpdate, live: { id: bigint; entry: RangeEntry }[]) => void; }; /** @@ -270,7 +282,8 @@ type SubscriptionControlOptions = { */ class SubscriptionControls { #writer: Writer; - #update?: SubscribeUpdate; + #live: LiveRanges; + #update?: { update: SubscribeUpdate; ids: bigint[] }; // Sticky, first one wins: null once the stream is over, an Error once it failed. #end?: Error | null; #ended: Promise; @@ -281,8 +294,9 @@ class SubscriptionControls { readonly decoding: Promise; constructor(options: SubscriptionControlOptions) { - const { writer } = options; + const { writer, live } = options; this.#writer = writer; + this.#live = live; this.#ended = new Promise((resolve) => { this.#resolveEnd = resolve; }); @@ -300,11 +314,20 @@ class SubscriptionControls { const update = this.#update; this.#update = undefined; // An update decoded before the stream ended still applies before the sticky end. - if (update) return { kind: "update", update }; + if (update) return { kind: "update", ...update }; if (this.#end === undefined) return undefined; return this.#end === null ? { kind: "done" } : { kind: "error", error: this.#end }; } + /** + * Where each live Range ID sits among the track model's ranges. The decoder applies each + * update to the model as it decodes it, ahead of {@link take}, so this rather than the + * last control taken is what indexes the model. + */ + positions(): Map { + return new Map(this.#live.ids().map((id, index) => [id, index])); + } + /** Whether {@link take} has a control to answer with now. */ pending(): boolean { return this.#update !== undefined || this.#end !== undefined; @@ -349,15 +372,18 @@ class SubscriptionControls { this.#changed.update((value) => value + 1); } - async #decode({ reader, version, apply, quic }: SubscriptionControlOptions) { + async #decode({ reader, version, live, apply, quic }: SubscriptionControlOptions) { try { // Runs until the peer's half ends, even past our own FIN: a lite-05+ publisher waits // for that end, and every other teardown stops the reader. for (;;) { const update = await SubscribeUpdate.decodeMaybe(reader, version); if (!update) break; - apply(update); - this.#update = update; + // Each lite-07 update edits the live ranges, which only the stream's history names. + const ranges = live.apply(update); + apply(update, ranges); + // Resolved in full, so the latest one replaces any the loop has yet to take. + this.#update = { update, ids: ranges.map(({ id }) => id) }; this.#changed.update((value) => value + 1); } } catch (err: unknown) { @@ -369,10 +395,25 @@ class SubscriptionControls { } } -// Where each range of `track` starts once delivery starts at `from`, undefined for one with no -// group there. Before lite-07 there is one range, which starts there. -function rangeStarts(version: Version, track: track.Subscriber, from: bigint): (bigint | undefined)[] { - return hasRanges(version) ? hooks.servingStarts(track, from) : [from]; +// `entry`, a range this subscription already held, resolved as it was: a range resolves once, so +// an update that leaves it live never counts its relative anchors back from a newer latest group, +// nor looks a time start up again in newer holdings. A start reaching back to the oldest group +// held stays as it was, since an absolute start would turn it into a floor the subscriber never +// named. +function pin(entry: RangeEntry, resolved: Resolved | undefined): RangeEntry { + if (!resolved) return entry; + const range = { ...entry.range }; + let { from, to } = entry; + if (resolved.floored && (range.start?.relative !== undefined || range.start?.time !== undefined)) { + range.start = { absolute: resolved.start }; + from = [resolved.start, 0]; + } + // An end resolves once it settles; until then it is reported open. + if ((range.end?.relative !== undefined || range.end?.time !== undefined) && resolved.end !== undefined) { + range.end = { absolute: resolved.end }; + to = [resolved.end, 0]; + } + return { range, from, to }; } // The later of two locations, either of which may be unknown. @@ -1004,7 +1045,7 @@ export class Publisher { if (hasTrackStream(this.version)) { // Lite-05+ accepts implicitly: no SUBSCRIBE_OK (the immutable // properties live in TRACK_INFO), and the resolved range arrives as - // SUBSCRIBE_START / SUBSCRIBE_END emitted from #runTrack. + // SUBSCRIBE_START / SUBSCRIBE_FINISH emitted from #runTrack. // // The timescale is an immutable property, so serving MUST use exactly // what TRACK_INFO advertised. It comes from the producer's accept(), so @@ -1030,12 +1071,26 @@ export class Publisher { datagrams = this.#runDatagrams(track, { sub: msg.id, stamp, served, bounds: datagramBounds }); } + // The SUBSCRIBE's ranges, and the Range ID of each range the model holds, in its order. + const asked = entriesOf(msg); + // Where each Range ID sits among the model's ranges, as of the last update applied. + let positions = new Map(asked.map((_, index) => [BigInt(index), index])); controls = new SubscriptionControls({ quic: this.#quic, reader: stream.reader, writer: stream.writer, version: this.version, - apply: (update) => { + live: new LiveRanges(this.version, asked), + apply: (update, live) => { + if (hasRanges(this.version)) { + const resolved = hooks.servingResolved(track); + const entries = live.map(({ id, entry }) => { + const index = positions.get(id); + return index === undefined ? entry : pin(entry, resolved[index]); + }); + Object.assign(update, spanOf(this.version, entries)); + positions = new Map(live.map(({ id }, index) => [id, index])); + } Object.assign(datagramBounds, { startGroup: update.startGroup, startFrame: update.startFrame, @@ -1067,6 +1122,7 @@ export class Publisher { served, floor, bounds, + initial: BigInt(asked.length), }); console.debug(`publish done: broadcast=${msg.broadcast} track=${track.name}`); @@ -1234,9 +1290,11 @@ export class Publisher { // The read cursor's floor; see {@link cursorFloor}. floor: bigint; bounds: FrameBounds; + // How many ranges the SUBSCRIBE carried, whose starts SUBSCRIBE_START reports. + initial: bigint; }, ): Promise { - const { sub, broadcast, stamp, publisher, compress, served, bounds } = serving; + const { sub, broadcast, stamp, publisher, compress, served, bounds, initial } = serving; // The read cursor's floor; the subscription's ranges cap it. let floor = serving.floor; const moveCursor = () => track.withRange({ start: { included: floor } }); @@ -1247,7 +1305,7 @@ export class Publisher { moveCursor(); }; // Lite-05+ resolves the range on the subscribe stream: SUBSCRIBE_START once the - // first group is known, SUBSCRIBE_END when the track finishes. + // first group is known, SUBSCRIBE_FINISH when the track finishes. const emitRange = hasTrackStream(this.version); let startSent = false; // Dispositions before START refine its boundary; later ones must be named as drops. @@ -1258,6 +1316,55 @@ export class Publisher { // resolves where its feed starts. let first: { group: group.Consumer; largest: track.Position | undefined } | undefined; let endSent = false; + // The Range IDs of the last update taken, and those an update added whose start no + // SUBSCRIBE_STARTED reported yet. Lite-07 only. + let ids = Array.from({ length: Number(initial) }, (_, index) => BigInt(index)); + let unstarted: bigint[] = []; + + // Where each of the SUBSCRIBE's ranges starts once delivery starts at `from`, by Range ID, + // undefined for one with no group there or an update retired: SUBSCRIBE_STARTED answers + // each one an update added. Before lite-07 there is one range, which starts there. + const rangeStarts = (from: bigint): (bigint | undefined)[] => { + if (!hasRanges(this.version)) return [from]; + const starts = hooks.servingStarts(track, from); + const positions = controls.positions(); + return Array.from({ length: Number(initial) }, (_, id) => { + const index = positions.get(BigInt(id)); + return index === undefined ? undefined : starts[index]; + }); + }; + + // Report where each range an update added starts, once it resolves, or with no start once + // nothing can resolve it (`finished`). SUBSCRIBE_START goes first. + const reportStarted = async (finished: boolean): Promise => { + if (startSequence === undefined || unstarted.length === 0) return true; + const resolved = hooks.servingResolved(track); + const positions = controls.positions(); + const waiting: bigint[] = []; + for (const id of unstarted) { + const index = positions.get(id); + // An update not taken yet retired it, and taking that update forgets it. + if (index === undefined) { + waiting.push(id); + continue; + } + const range = resolved[index]; + if (!range && !finished) { + waiting.push(id); + continue; + } + let start: bigint | undefined; + if (range) { + start = max(range.start, startSequence); + if (range.end !== undefined && range.end <= start) start = undefined; + } + const started = new SubscribeStarted(id, start); + if (!(await controls.response(encodeSubscribeResponse(stream, { started }, this.version)))) + return false; + } + unstarted = waiting; + return true; + }; // Drop whatever part of `[start, end)` this subscription has not been served or dropped // yet, given up with `err`. @@ -1272,7 +1379,7 @@ export class Publisher { return true; }; - // The track's exclusive final boundary. A Rust subscriber feeds SUBSCRIBE_END + // The track's exclusive final boundary. A Rust subscriber feeds SUBSCRIBE_FINISH // straight into finish_at, so it must name the track's boundary (which counts // datagram sequences too), not the delivered range: a subscription cap can hold // produced groups back. The latest() fallback covers a subscription torn down @@ -1292,7 +1399,7 @@ export class Publisher { const endError = hooks.subscriberEndError(track); const code = endError && hasEndError(this.version) ? toStreamCode(endError) : undefined; return controls.response( - encodeSubscribeResponse(stream, { end: new SubscribeEnd(boundary(), code) }, this.version), + encodeSubscribeResponse(stream, { finish: new SubscribeFinish(boundary(), code) }, this.version), ); }; @@ -1305,7 +1412,7 @@ export class Publisher { if (end instanceof Error) throw end; return end === done; }; - // Before lite-07 an errored end is only the reset at that end: a clean SUBSCRIBE_END sent + // Before lite-07 an errored end is only the reset at that end: a clean SUBSCRIBE_FINISH sent // early could let the subscriber settle before the reset arrives. A subscription capped // short of the end is owed no error, so its boundary goes out. const declareEarly = () => @@ -1348,6 +1455,7 @@ export class Publisher { return false; } } + if (!(await reportStarted(false))) return false; // Control before data, matching the Rust publisher: every control decoded while // the loop was parked applies to the next pop, never to one already made. This // drain is synchronous, so nothing the decoder holds can land between the pop @@ -1367,6 +1475,14 @@ export class Publisher { // An update may reach the finished track's history, so it is served again. finished = false; const update = control.update; + if (hasRanges(this.version)) { + // A range retired before its start resolved is owed no SUBSCRIBE_STARTED. + const live = new Set(control.ids); + unstarted = unstarted.filter((id) => live.has(id)); + const held = new Set(ids); + for (const id of control.ids) if (id >= initial && !held.has(id)) unstarted.push(id); + ids = control.ids; + } console.debug(`subscribe update: broadcast=${broadcast} track=${track.name}`); // `cursorFloor` pins an unfloored pre-06 SUBSCRIBE to the latest // group, so an update that drops a named floor re-pins there too. On @@ -1442,10 +1558,7 @@ export class Publisher { startSent = true; startSequence = bounds.startGroup; raiseFloor(bounds.startGroup); - const start = new SubscribeStart( - rangeStarts(this.version, track, bounds.startGroup), - largest, - ); + const start = new SubscribeStart(rangeStarts(bounds.startGroup), largest); if ( !(await controls.response(encodeSubscribeResponse(stream, { start }, this.version))) ) @@ -1460,7 +1573,7 @@ export class Publisher { if (end !== undefined && hooks.servingRuns(track, 0n, end).length === 0) { startSent = true; startSequence = end; - const start = new SubscribeStart(rangeStarts(this.version, track, end), largest); + const start = new SubscribeStart(rangeStarts(end), largest); if ( !(await controls.response(encodeSubscribeResponse(stream, { start }, this.version))) ) @@ -1487,10 +1600,7 @@ export class Publisher { ) { startSent = true; startSequence = runs[0].start; - const start = new SubscribeStart( - rangeStarts(this.version, track, startSequence), - largest, - ); + const start = new SubscribeStart(rangeStarts(startSequence), largest); if ( !(await controls.response(encodeSubscribeResponse(stream, { start }, this.version))) ) @@ -1523,11 +1633,13 @@ export class Publisher { if (keepsSubscribeOpen(this.version) && !startSent && boundary() > 0n) { startSent = true; startSequence = hooks.servingRuns(track, 0n, boundary())[0]?.start ?? boundary(); - const start = new SubscribeStart(rangeStarts(this.version, track, startSequence), largest); + const start = new SubscribeStart(rangeStarts(startSequence), largest); if (!(await controls.response(encodeSubscribeResponse(stream, { start }, this.version)))) return false; continue; } + // Nothing arrives to resolve a range still waiting on the live feed. + if (!(await reportStarted(true))) return false; // Name every sequence from the start to the end this subscription never // got. A cap below the end bounds what the subscriber is owed, as on its side. if (startSequence !== undefined) { @@ -1597,7 +1709,7 @@ export class Publisher { !(await controls.response( encodeSubscribeResponse( stream, - { start: new SubscribeStart(rangeStarts(this.version, track, startSequence), largest) }, + { start: new SubscribeStart(rangeStarts(startSequence), largest) }, this.version, ), )) diff --git a/js/net/src/lite/subscribe.test.ts b/js/net/src/lite/subscribe.test.ts index 8a74a4ba376..a50d7874673 100644 --- a/js/net/src/lite/subscribe.test.ts +++ b/js/net/src/lite/subscribe.test.ts @@ -7,15 +7,19 @@ import { decodeSubscribeResponse, encodeSubscribeResponse, inclusiveGroupEnd, + LiveRanges, requestedRanges, Subscribe, SubscribeDrop, - SubscribeEnd, + SubscribeFinish, SubscribeOk, type SubscribeResponse, SubscribeStart, + SubscribeStarted, SubscribeUpdate, + spanOf, toWireRanges, + type WireRange, } from "./subscribe.ts"; import { Version } from "./version.ts"; @@ -195,14 +199,15 @@ test("lite-07 carries the span as its only range, exclusive at the end", async ( test("a lite-07 list decodes as the span covering it", async () => { // Priority, order, max delay, and an unstated min delay as SubscribeUpdate writes them, - // then the ranges and an absent wanted group. - const head = (await encodeMessage(Version.DRAFT_07, new SubscribeUpdate({ priority: 0 }))).slice(1, 5); - const update = (ranges: number[]) => { - const body = [...head, ...ranges, 0]; - return SubscribeUpdate.decode( + // then no Range ID to retire, the ranges it adds, and an absent wanted group. + const head = (await encodeMessage(Version.DRAFT_07, new SubscribeUpdate({ priority: 0, ranges: [] }))).slice(1, 5); + const update = async (ranges: number[]) => { + const body = [...head, 0, ...ranges, 0]; + const got = await SubscribeUpdate.decode( new Reader(undefined, new Uint8Array([body.length, ...body]), Version.DRAFT_07), Version.DRAFT_07, ); + return { ...got, ...spanOf(Version.DRAFT_07, got.entries ?? []) }; }; // Groups 2 through 4 and 7 through frame 1 of group 9. @@ -234,7 +239,8 @@ test("a lite-07 list decodes as the span covering it", async () => { const oldest = await update([2, 0, 1, 3, 0, 1, 5, 2, 1, 5, 2]); expect(oldest.ranges).toEqual([{ end: { absolute: 3n } }, { start: { absolute: 5n }, end: { absolute: 5n } }]); - await expect(update([0])).rejects.toThrow(ProtocolViolation); + // An update may add no range, retiring some or none. + expect((await update([0])).ranges).toEqual([]); await expect(update([1, 4, 0, 0])).rejects.toThrow(ProtocolViolation); await expect(update([1, 0, 1, 0, 0])).rejects.toThrow(ProtocolViolation); }); @@ -304,24 +310,26 @@ test("SubscribeStart carries each range's start in place of one group on draft-0 await expect(encode(Version.DRAFT_06, { start })).rejects.toThrow(); }); -test("SubscribeEnd round-trips on draft-05", async () => { +test("SubscribeFinish round-trips on draft-05", async () => { // Type, length, exclusive final group. - expect(await encode(Version.DRAFT_05, { end: new SubscribeEnd(7n) })).toEqual(new Uint8Array([1, 1, 7])); - const got = await responseRoundtrip(Version.DRAFT_05, { end: new SubscribeEnd(7n) }); - expect("end" in got).toBe(true); - if (!("end" in got)) throw new Error("expected end"); - expect(got.end.group).toBe(7n); + expect(await encode(Version.DRAFT_05, { finish: new SubscribeFinish(7n) })).toEqual(new Uint8Array([1, 1, 7])); + const got = await responseRoundtrip(Version.DRAFT_05, { finish: new SubscribeFinish(7n) }); + expect("finish" in got).toBe(true); + if (!("finish" in got)) throw new Error("expected end"); + expect(got.finish.group).toBe(7n); }); -test("SubscribeEnd carries its error on draft-07", async () => { +test("SubscribeFinish carries its error on draft-07", async () => { // Type, length, exclusive final group, then the error code + 1, 0 for a clean end. - expect(await encode(Version.DRAFT_07, { end: new SubscribeEnd(7n) })).toEqual(new Uint8Array([1, 2, 7, 0])); - expect(await encode(Version.DRAFT_07, { end: new SubscribeEnd(7n, 71) })).toEqual(new Uint8Array([1, 2, 7, 72])); - const got = await responseRoundtrip(Version.DRAFT_07, { end: new SubscribeEnd(7n, 71) }); - if (!("end" in got)) throw new Error("expected end"); - expect([got.end.group, got.end.error]).toEqual([7n, 71]); + expect(await encode(Version.DRAFT_07, { finish: new SubscribeFinish(7n) })).toEqual(new Uint8Array([1, 2, 7, 0])); + expect(await encode(Version.DRAFT_07, { finish: new SubscribeFinish(7n, 71) })).toEqual( + new Uint8Array([1, 2, 7, 72]), + ); + const got = await responseRoundtrip(Version.DRAFT_07, { finish: new SubscribeFinish(7n, 71) }); + if (!("finish" in got)) throw new Error("expected end"); + expect([got.finish.group, got.finish.error]).toEqual([7n, 71]); // An older version has nowhere to carry it. - await expect(encode(Version.DRAFT_06, { end: new SubscribeEnd(7n, 71) })).rejects.toThrow(); + await expect(encode(Version.DRAFT_06, { finish: new SubscribeFinish(7n, 71) })).rejects.toThrow(); }); test("SubscribeDrop is restored on draft-07", async () => { @@ -385,7 +393,7 @@ test.each(["ascending", "descending"] as const)("lite-07 round-trips a %s group ); expect(got.order).toBe(order); - const update = await encodeMessage(version, new SubscribeUpdate({ priority: 3, order, maxDelay: 250 })); + const update = await encodeMessage(version, new SubscribeUpdate({ priority: 3, order, maxDelay: 250, ranges: [] })); // The length prefix, the priority byte, then the order. expect(update[1]).toBe(3 + 128); expect(update[2]).toBe(order === "ascending" ? 1 : 0); @@ -427,7 +435,7 @@ test("min delay survives lite-07 and decodes as not stated on legacy wires", asy const got = await Subscribe.decode(new Reader(undefined, subscribe, version), version); expect(got.minDelay).toBe(expected); - const update = await encodeMessage(version, new SubscribeUpdate({ priority: 3, minDelay })); + const update = await encodeMessage(version, new SubscribeUpdate({ priority: 3, minDelay, ranges: [] })); const decoded = await SubscribeUpdate.decode(new Reader(undefined, update, version), version); expect(decoded.minDelay).toBe(expected); } @@ -451,7 +459,7 @@ test("optional subscriber milliseconds reject unsafe budgets", async () => { test("wanted group survives lite-07 and disappears on legacy wires", async () => { for (const version of [Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07]) { for (const wantedGroup of [undefined, 0n, 3n, BigInt(Number.MAX_SAFE_INTEGER)]) { - const update = new SubscribeUpdate({ priority: 7, wantedGroup }); + const update = new SubscribeUpdate({ priority: 7, wantedGroup, ranges: [] }); const encoded = await encodeMessage(version, update); const decoded = await SubscribeUpdate.decode(new Reader(undefined, encoded, version), version); expect(decoded.wantedGroup).toBe(version === Version.DRAFT_07 ? wantedGroup : undefined); @@ -483,3 +491,56 @@ for (const version of [Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07]) { } }); } + +// An update sends only the ranges that changed, and the publisher resolves the same list, with the +// same Range IDs, as the subscriber holds. Mirrors the Rust `lite07_updates_edit_only_the_changed_ranges`. +test("lite-07 updates edit only the changed ranges", async () => { + const version = Version.DRAFT_07; + const a = { start: { absolute: 1n }, end: { absolute: 5n } }; + const b = { start: { relative: 3n } }; + const c = { start: { time: 9_000 } }; + const sent = new LiveRanges(version, [a, b]); + const live = new LiveRanges(version, [a, b]); + const step = async (ranges: WireRange[], remove: bigint[], add: WireRange[]) => { + const edit = sent.edit(ranges); + expect(edit).toEqual({ remove, add }); + const update = new SubscribeUpdate({ priority: 0, remove: edit.remove, ranges: edit.add }); + const got = await SubscribeUpdate.decode( + new Reader(undefined, await encodeMessage(version, update), version), + version, + ); + const resolved = live.apply(got).map(({ id, entry }) => [id, entry.range]); + expect(resolved).toEqual(sent.entries()); + }; + await step([b, a], [], []); + await step([a, c], [1n], [c]); + await step([c, c, a], [], [c]); + await step([b], [0n, 2n, 3n], [b]); + expect(live.entries()).toEqual([[4n, b]]); + + // An id never assigned or already retired, or nothing left live, is refused. + const refused = (remove: bigint[], ranges: WireRange[]) => + new LiveRanges(version, [a]).apply(new SubscribeUpdate({ priority: 0, remove, ranges })); + expect(() => refused([1n], [a])).toThrow(ProtocolViolation); + expect(() => refused([0n, 0n], [a])).toThrow(ProtocolViolation); + expect(() => refused([0n], [])).toThrow(ProtocolViolation); +}); + +test("SUBSCRIBE_STARTED is type 4 on lite-07 and unknown before it", async () => { + for (const start of [undefined, 0n, 7n]) { + const started = new SubscribeStarted(4n, start); + expect(await encode(Version.DRAFT_07, { started })).toEqual( + new Uint8Array([4, 2, 4, start === undefined ? 0 : Number(start) + 1]), + ); + const got = await responseRoundtrip(Version.DRAFT_07, { started }); + if (!("started" in got)) throw new Error("expected SUBSCRIBE_STARTED"); + expect([got.started.id, got.started.start]).toEqual([4n, start]); + } + await expect(encode(Version.DRAFT_06, { started: new SubscribeStarted(0n) })).rejects.toThrow(); + await expect( + decodeSubscribeResponse( + new Reader(undefined, new Uint8Array([4, 2, 0, 0]), Version.DRAFT_06), + Version.DRAFT_06, + ), + ).rejects.toThrow(ProtocolViolation); +}); diff --git a/js/net/src/lite/subscribe.ts b/js/net/src/lite/subscribe.ts index f9c975b3e82..53e47deb676 100644 --- a/js/net/src/lite/subscribe.ts +++ b/js/net/src/lite/subscribe.ts @@ -11,6 +11,7 @@ import * as Path from "../path.ts"; import type { Reader, Writer } from "../stream.ts"; import { floorAt, type Timescale, Timestamp } from "../time.ts"; import type { Anchor, Order, Position, Range } from "../track.ts"; +import { lengthLeadingOnes, parts, split } from "../util/varint.ts"; import * as Duration from "./duration.ts"; import { decodePin, encodePin } from "./instance.ts"; import * as Message from "./message.ts"; @@ -260,7 +261,7 @@ export function requestedRanges(msg: Span & { ranges?: WireRange[] }, timescale: } /** A position as a comparable pair, group then frame. */ -type Point = [group: bigint, frame: number]; +export type Point = [group: bigint, frame: number]; function before(a: Point, b: Point): boolean { return a[0] < b[0] || (a[0] === b[0] && a[1] < b[1]); @@ -289,39 +290,50 @@ async function decodeAnchor(r: Reader, end: boolean): Promise<{ point?: Point; a } /** - * The inverse of {@link encodeSpan}. A lite-07 list decodes as its ranges and as the one span - * covering every range in it, which the subscriber narrows again. + * A decoded lite-07 range, with the positions its absolute anchors name, which a + * {@link WireRange} rounds to whole groups. */ -async function decodeSpan(r: Reader, version: Version): Promise { - if (!hasRanges(version)) { - const startGroup = await decodeStartGroup(r, version); - const endGroup = (await r.u62()) || undefined; - const end = endGroup !== undefined ? endGroup - 1n : undefined; - const frames = await decodeFrameBounds(r, version, startGroup, end); - return { startGroup: canonicalStartGroup(version, startGroup, frames.startFrame), endGroup: end, ...frames }; - } +export interface RangeEntry { + range: WireRange; + from?: Point; + to?: Point; +} + +/** Decode one lite-07 range: its start, then its end. */ +async function decodeEntry(r: Reader): Promise { + const from = await decodeAnchor(r, false); + const to = await decodeAnchor(r, true); + const range: WireRange = {}; + if (from.anchor) range.start = from.anchor; + if (to.anchor) range.end = to.anchor; + // Empty partway through a group, which whole groups can't say: keep its place, + // covering no group, as the Rust publisher does. + if (from.point && to.point && !before(from.point, to.point)) range.end = { absolute: from.point[0] }; + return { range, from: from.point, to: to.point }; +} + +/** Decode a lite-07 `Range Count` and its ranges, which may be none. */ +async function decodeEntries(r: Reader): Promise { + // Not preallocated: each range costs at least two bytes of the bounded message. + const entries: RangeEntry[] = []; + for (let count = await r.u53(); count > 0; count--) entries.push(await decodeEntry(r)); + return entries; +} - const count = await r.u53(); - if (count === 0) throw new ProtocolViolation("a subscription asks for no range"); +/** + * Lite-07 `entries` as their ranges and the one span covering every one of them, which the + * subscriber narrows again. + */ +export function spanOf(version: Version, entries: readonly RangeEntry[]): Span & { ranges: WireRange[] } { let start: Point | undefined; let end: Point | undefined; let floor = true; let live = false; - const ranges: WireRange[] = []; - for (let i = 0; i < count; i++) { - const from = await decodeAnchor(r, false); - const to = await decodeAnchor(r, true); - const range: WireRange = {}; - if (from.anchor) range.start = from.anchor; - if (to.anchor) range.end = to.anchor; - // Empty partway through a group, which whole groups can't say: keep its place, - // covering no group, as the Rust publisher does. - if (from.point && to.point && !before(from.point, to.point)) range.end = { absolute: from.point[0] }; - ranges.push(range); - if (from.point === undefined) floor = false; - else if (start === undefined || before(from.point, start)) start = from.point; - if (to.point === undefined) live = true; - else if (end === undefined || before(end, to.point)) end = to.point; + for (const { from, to } of entries) { + if (from === undefined) floor = false; + else if (start === undefined || before(from, start)) start = from; + if (to === undefined) live = true; + else if (end === undefined || before(end, to)) end = to; } if (!floor) start = undefined; if (live) end = undefined; @@ -330,7 +342,7 @@ async function decodeSpan(r: Reader, version: Version): Promise entry.range), startGroup: canonicalStartGroup(version, start?.[0], startFrame), startFrame, endGroup: end === undefined ? undefined : end[1] === 0 ? end[0] - 1n : end[0], @@ -338,6 +350,208 @@ async function decodeSpan(r: Reader, version: Version): Promise { + if (!hasRanges(version)) { + const startGroup = await decodeStartGroup(r, version); + const endGroup = (await r.u62()) || undefined; + const end = endGroup !== undefined ? endGroup - 1n : undefined; + const frames = await decodeFrameBounds(r, version, startGroup, end); + return { startGroup: canonicalStartGroup(version, startGroup, frames.startFrame), endGroup: end, ...frames }; + } + + const entries = await decodeEntries(r); + if (entries.length === 0) throw new ProtocolViolation("a subscription asks for no range"); + return { ...spanOf(version, entries), entries }; +} + +/** + * The ranges `msg` carries as lite-07 entries: its decoded list, or else the one span it + * encodes as its only range. + */ +export function entriesOf(msg: Span & { ranges?: WireRange[]; entries?: RangeEntry[] }): RangeEntry[] { + if (msg.entries) return msg.entries; + if (msg.ranges) return msg.ranges.map((range) => ({ range })); + const range: WireRange = {}; + const entry: RangeEntry = { range }; + if (msg.startGroup !== undefined) { + range.start = { absolute: msg.startGroup }; + entry.from = [msg.startGroup, msg.startFrame]; + } + if (msg.endGroup !== undefined) { + entry.to = msg.endFrame === undefined ? [msg.endGroup + 1n, 0] : [msg.endGroup, msg.endFrame + 1]; + range.end = { absolute: msg.endGroup + 1n }; + } + return [entry]; +} + +/** The bytes a lite-07 range takes on the wire, whose varints count leading ones. */ +function entrySize({ range, from, to }: RangeEntry): number { + const size = (value: number | bigint) => { + const lo = split(value); + return lengthLeadingOnes(parts.hi, lo); + }; + const anchor = (anchor: WireAnchor | undefined, point: Point | undefined): number => { + if (point) return size(1) + size(point[0]) + size(point[1]); + if (anchor?.absolute !== undefined) return size(1) + size(anchor.absolute) + size(0); + if (anchor?.relative !== undefined) return size(2) + size(anchor.relative); + if (anchor?.time !== undefined) return size(3) + size(anchor.time); + return size(0); + }; + return anchor(range.start, from) + anchor(range.end, to); +} + +/** A range's anchors as a key, so equal ranges compare equal. */ +function rangeKey(range: WireRange): string { + const anchor = (anchor: WireAnchor | undefined) => { + if (anchor?.absolute !== undefined) return `a${anchor.absolute}`; + if (anchor?.relative !== undefined) return `r${anchor.relative}`; + if (anchor?.time !== undefined) return `t${anchor.time}`; + return "-"; + }; + return `${anchor(range.start)}:${anchor(range.end)}`; +} + +/** + * A subscription's live ranges by Range ID, which both ends of a Subscribe Stream keep: the + * subscriber to write each update as an edit, the publisher to resolve one. Mirrors the Rust + * `LiveRanges`. + * + * The SUBSCRIBE's ranges take Range IDs from 0 in order, and each range an update adds takes the + * next. Before lite-07 an update replaces the list, numbered afresh. + */ +export class LiveRanges { + readonly #version: Version; + #next = 0n; + // In Range ID order, since each id is the next. + readonly #live = new Map(); + // The bytes the live ranges take on the wire, held to what one SUBSCRIBE carries. + #size = 0; + /** How many ranges the SUBSCRIBE carried, whose starts SUBSCRIBE_START reports. */ + readonly initial: bigint; + + /** The ranges a SUBSCRIBE carried. */ + constructor(version: Version, ranges: readonly (RangeEntry | WireRange)[]) { + this.#version = version; + this.initial = BigInt(ranges.length); + this.#add(ranges); + } + + #add(ranges: readonly (RangeEntry | WireRange)[]) { + for (const range of ranges) { + const entry = "range" in range ? (range as RangeEntry) : { range: range as WireRange }; + // Only lite-07 numbers ranges on the wire, and only it is held to one SUBSCRIBE. + if (hasRanges(this.#version)) this.#size += entrySize(entry); + this.#live.set(this.#next++, entry); + } + } + + #remove(id: bigint): boolean { + const entry = this.#live.get(id); + if (!entry) return false; + if (hasRanges(this.#version)) this.#size -= entrySize(entry); + this.#live.delete(id); + return true; + } + + /** The live range with Range ID `id`. */ + get(id: bigint): WireRange | undefined { + return this.#live.get(id)?.range; + } + + /** Whether `id` was ever assigned. */ + assigned(id: bigint): boolean { + return id < this.#next; + } + + /** Every live Range ID, in order. */ + ids(): bigint[] { + return [...this.#live.keys()]; + } + + /** Every live range, in Range ID order. */ + ranges(): WireRange[] { + return [...this.#live.values()].map((entry) => entry.range); + } + + /** Every live range with its Range ID, in order. */ + entries(): [bigint, WireRange][] { + return [...this.#live].map(([id, entry]) => [id, entry.range]); + } + + /** Whether the publisher reports where each range starts, which lite-07 does by Range ID. */ + get reportsStarts(): boolean { + return hasRanges(this.#version); + } + + /** + * Apply a received lite-07 `update`'s edit, returning every live range by Range ID. Throws + * {@link ProtocolViolation} for an edit it cannot apply. Before lite-07 an update restates + * its ranges, so nothing is numbered. + */ + apply(update: SubscribeUpdate): { id: bigint; entry: RangeEntry }[] { + if (!hasRanges(this.#version)) return []; + for (const id of update.remove ?? []) { + // An id never assigned, or already retired (including earlier in this list). + if (!this.#remove(id)) throw new ProtocolViolation(`unknown range id: ${id}`); + } + this.#add(update.entries ?? update.ranges ?? []); + // Asking for nothing is refused like a SUBSCRIBE that does, and a set no SUBSCRIBE + // could carry like one too large to send. + if (this.#live.size === 0) throw new ProtocolViolation("a subscription asks for no range"); + if (this.#size > Message.MAX_MESSAGE_SIZE) throw new ProtocolViolation("too many ranges"); + return [...this.#live].map(([id, entry]) => ({ id, entry })); + } + + /** + * The edit that turns the live ranges into `ranges`, which become the live ones: the Range IDs + * to retire and the ranges to add. A range already live keeps its Range ID, so an update that + * changes one range sends only that one, and the publisher keeps where the rest resolved. + * Before lite-07 replaced ranges take fresh Range IDs. + */ + edit(ranges: readonly WireRange[]): { remove: bigint[]; add: WireRange[] } { + if (ranges.length === 0) throw new Error(EMPTY_RANGE); + if (!hasRanges(this.#version)) { + const same = + ranges.length === this.#live.size && + this.ranges().every((range, index) => rangeKey(range) === rangeKey(ranges[index])); + if (!same) { + this.#live.clear(); + this.#add(ranges); + } + return { remove: [], add: [...ranges] }; + } + // Each live range keeps at most one wanted copy, so duplicates edit like any other. + const wanted = new Map(); + for (const range of ranges) wanted.set(rangeKey(range), (wanted.get(rangeKey(range)) ?? 0) + 1); + const remove: bigint[] = []; + for (const [id, entry] of this.#live) { + const key = rangeKey(entry.range); + const count = wanted.get(key) ?? 0; + if (count > 0) wanted.set(key, count - 1); + else remove.push(id); + } + // In the order asked for, so the publisher numbers them the way this side does. + const add = ranges.filter((range) => { + const key = rangeKey(range); + const count = wanted.get(key) ?? 0; + if (count === 0) return false; + wanted.set(key, count - 1); + return true; + }); + for (const id of remove) this.#remove(id); + this.#add(add); + if (this.#size > Message.MAX_MESSAGE_SIZE) throw new Error("too many ranges for one SUBSCRIBE"); + return { remove, add }; + } +} + /** Step over the retired `Ordered` byte on a version whose layout still has it. */ async function skipGroupOrder(r: Reader, version: Version) { if (hasGroupOrder(version)) await r.bool(); @@ -427,8 +641,15 @@ export class SubscribeUpdate { startFrame: number; /** See {@link Subscribe.endFrame}. */ endFrame?: number; - /** See {@link Subscribe.ranges}. */ + /** + * See {@link Subscribe.ranges}. On lite-07 the wire carries the ranges to add, after retiring + * {@link remove}; {@link LiveRanges.apply} resolves a received update to every live range. + */ ranges?: WireRange[]; + /** Lite-07: the Range IDs of the live ranges to retire before adding {@link ranges}. */ + remove?: bigint[]; + /** Lite-07: the ranges a received update adds, with the positions they name; see {@link RangeEntry}. */ + entries?: RangeEntry[]; constructor(props: { priority: number; @@ -441,6 +662,8 @@ export class SubscribeUpdate { startFrame?: number; endFrame?: number; ranges?: WireRange[]; + remove?: bigint[]; + entries?: RangeEntry[]; }) { if (props.wantedGroup !== undefined && props.wantedGroup < 0n) throw new RangeError("wanted group must be non-negative"); @@ -454,6 +677,8 @@ export class SubscribeUpdate { this.startFrame = props.startFrame ?? 0; this.endFrame = props.endFrame; this.ranges = props.ranges; + this.remove = props.remove; + this.entries = props.entries; } async #encode(w: Writer, version: Version) { @@ -467,7 +692,26 @@ export class SubscribeUpdate { await encodeOrder(w, version, this.order); await w.u53(this.maxDelay); await encodeMinDelay(w, version, this.minDelay); - await encodeSpan(w, version, this, this.ranges); + if (hasRanges(version)) { + // An edit of the live ranges, which may add none: `ranges`, or else the span as + // the one range added once `remove` retires the rest. + if (this.ranges === undefined && this.remove === undefined) + throw new Error("a lite-07 update edits its ranges"); + const remove = this.remove ?? []; + await w.u53(remove.length); + for (const id of remove) await w.u62(id); + if (this.ranges === undefined) { + await encodeSpan(w, version, this); + } else { + await w.u53(this.ranges.length); + for (const range of this.ranges) { + await encodeAnchor(w, range.start); + await encodeAnchor(w, range.end); + } + } + } else { + await encodeSpan(w, version, this, this.ranges); + } if (hasRefresh(version)) await w.u62(this.wantedGroup === undefined ? 0n : this.wantedGroup + 1n); break; } @@ -483,7 +727,16 @@ export class SubscribeUpdate { const order = await decodeOrder(r, version); const maxDelay = await r.u53(); const minDelay = await decodeMinDelay(r, version); - const span = await decodeSpan(r, version); + let span: Span & { ranges?: WireRange[]; remove?: bigint[]; entries?: RangeEntry[] }; + if (hasRanges(version)) { + // Not preallocated, like the ranges: each id costs a byte of the bounded message. + const remove: bigint[] = []; + for (let count = await r.u53(); count > 0; count--) remove.push(await r.u62()); + const entries = await decodeEntries(r); + span = { startFrame: 0, remove, entries, ranges: entries.map((entry) => entry.range) }; + } else { + span = await decodeSpan(r, version); + } const wanted = hasRefresh(version) ? await r.u62() : 0n; return new SubscribeUpdate({ wantedGroup: wanted === 0n ? undefined : wanted - 1n, @@ -553,6 +806,8 @@ export class Subscribe { * the span alone; decoding a lite-07 list fills them alongside the span covering them all. */ ranges?: WireRange[]; + /** Lite-07: the decoded ranges with the positions they name; see {@link RangeEntry}. */ + entries?: RangeEntry[]; constructor(props: { id: bigint; @@ -568,6 +823,7 @@ export class Subscribe { startFrame?: number; endFrame?: number; ranges?: WireRange[]; + entries?: RangeEntry[]; }) { this.id = props.id; this.broadcast = props.broadcast; @@ -582,6 +838,7 @@ export class Subscribe { this.startFrame = props.startFrame ?? 0; this.endFrame = props.endFrame; this.ranges = props.ranges; + this.entries = props.entries; } async #encode(w: Writer, version: Version) { @@ -648,7 +905,7 @@ export class Subscribe { * Publisher's acknowledgement on the Subscribe Stream for drafts 01-04. * * Draft-05+ replaced this with implicit acceptance plus {@link SubscribeStart} / - * {@link SubscribeEnd}; the immutable codec/timescale/cache moved to TRACK_INFO. + * {@link SubscribeFinish}; the immutable codec/timescale/cache moved to TRACK_INFO. */ export class SubscribeOk { priority: number; @@ -797,10 +1054,11 @@ export class SubscribeStart { } /** - * Signals that no group at or after `group` (exclusive upper bound) will be produced - * on a Draft-05+ subscription. `0` means the track ended before producing any groups. + * SUBSCRIBE_FINISH (SUBSCRIBE_END before lite-07): no group at or after `group` (exclusive + * upper bound) will be produced on a Draft-05+ subscription. `0` means the track ended before + * producing any groups. */ -export class SubscribeEnd { +export class SubscribeFinish { /** The exclusive final group sequence: the first sequence that will never be produced. */ group: bigint; /** The stream error code the track ended with, if it did not end cleanly. Draft-07+ only. */ @@ -828,12 +1086,40 @@ export class SubscribeEnd { }); } - static async decode(r: Reader, version: Version): Promise { + static async decode(r: Reader, version: Version): Promise { return Message.decode(r, async (r) => { const group = await r.u62(); - if (!hasEndError(version)) return new SubscribeEnd(group); + if (!hasEndError(version)) return new SubscribeFinish(group); const code = await r.u53(); - return new SubscribeEnd(group, code === 0 ? undefined : code - 1); + return new SubscribeFinish(group, code === 0 ? undefined : code - 1); + }); + } +} + +/** SUBSCRIBE_STARTED: where a range a SUBSCRIBE_UPDATE added starts. Lite-07+. */ +export class SubscribeStarted { + /** The range's Range ID. */ + id: bigint; + /** The group its delivery starts at, `undefined` for a range with no group there. */ + start?: bigint; + + constructor(id: bigint, start?: bigint) { + this.id = id; + this.start = start; + } + + async encode(w: Writer): Promise { + return Message.encode(w, async (w) => { + await w.u62(this.id); + await w.u62(this.start === undefined ? 0n : this.start + 1n); + }); + } + + static async decode(r: Reader): Promise { + return Message.decode(r, async (r) => { + const id = await r.u62(); + const start = await r.u62(); + return new SubscribeStarted(id, start === 0n ? undefined : start - 1n); }); } } @@ -899,14 +1185,17 @@ export class SubscribeDrop { * * The discriminator is version-dependent: * - Draft-03/04: `0x0` SUBSCRIBE_OK, `0x1` SUBSCRIBE_DROP. - * - Draft-05+: `0x0` SUBSCRIBE_START, `0x1` SUBSCRIBE_END, `0x2` SUBSCRIBE_DROP - * (SUBSCRIBE_OK was removed; acceptance is implicit). + * - Draft-05+: `0x0` SUBSCRIBE_START (SUBSCRIBE_OK on lite-07), `0x1` SUBSCRIBE_FINISH + * (SUBSCRIBE_END before lite-07), `0x2` SUBSCRIBE_DROP (SUBSCRIBE_OK was removed; + * acceptance is implicit). + * - Lite-07+: also `0x4` SUBSCRIBE_STARTED. */ export type SubscribeResponse = | { ok: SubscribeOk } | { start: SubscribeStart } - | { end: SubscribeEnd } - | { drop: SubscribeDrop }; + | { finish: SubscribeFinish } + | { drop: SubscribeDrop } + | { started: SubscribeStarted }; export async function encodeSubscribeResponse(w: Writer, resp: SubscribeResponse, version: Version): Promise { switch (version) { @@ -927,7 +1216,7 @@ export async function encodeSubscribeResponse(w: Writer, resp: SubscribeResponse await w.u53(0x1); await resp.drop.encode(w); } else { - throw new Error("SUBSCRIBE_START/END not supported for this version"); + throw new Error("SUBSCRIBE_START/FINISH not supported for this version"); } break; default: @@ -935,12 +1224,15 @@ export async function encodeSubscribeResponse(w: Writer, resp: SubscribeResponse if ("start" in resp) { await w.u53(0x0); await resp.start.encode(w, version); - } else if ("end" in resp) { + } else if ("finish" in resp) { await w.u53(0x1); - await resp.end.encode(w, version); + await resp.finish.encode(w, version); } else if ("drop" in resp) { await w.u53(0x2); await resp.drop.encode(w); + } else if ("started" in resp && hasRanges(version)) { + await w.u53(0x4); + await resp.started.encode(w); } else { throw new Error("subscribe response not supported for this version"); } @@ -971,9 +1263,12 @@ export async function decodeSubscribeResponse(r: Reader, version: Version): Prom case 0x0: return { start: await SubscribeStart.decode(r, version) }; case 0x1: - return { end: await SubscribeEnd.decode(r, version) }; + return { finish: await SubscribeFinish.decode(r, version) }; case 0x2: return { drop: await SubscribeDrop.decode(r) }; + case 0x4: + if (!hasRanges(version)) throw new ProtocolViolation(`unknown subscribe response type: ${typ}`); + return { started: await SubscribeStarted.decode(r) }; default: throw new ProtocolViolation(`unknown subscribe response type: ${typ}`); } diff --git a/js/net/src/lite/subscriber.test.ts b/js/net/src/lite/subscriber.test.ts index 5724b4c2a58..29612ff9646 100644 --- a/js/net/src/lite/subscriber.test.ts +++ b/js/net/src/lite/subscriber.test.ts @@ -24,7 +24,7 @@ import { GRACE } from "../ledger.ts"; import * as Path from "../path.ts"; import { Reader, Writer } from "../stream.ts"; import { Timescale } from "../time.ts"; -import type { Producer, Subscriber as TrackSubscriber } from "../track.ts"; +import type { Producer } from "../track.ts"; import { SEGMENT_BYTES } from "../util/limits.ts"; import { Budget } from "../util/yield.ts"; import * as Varint from "../varint.ts"; @@ -37,8 +37,7 @@ import { StreamId } from "./stream.ts"; import { encodeSubscribeResponse, Subscribe, - SubscribeDrop, - SubscribeEnd, + SubscribeFinish, SubscribeOk, type SubscribeResponse, SubscribeStart, @@ -1893,7 +1892,7 @@ test.each([ await respond( subscribe, version, - [{ start: new SubscribeStart([0n]) }, { end: new SubscribeEnd(0n) }], + [{ start: new SubscribeStart([0n]) }, { finish: new SubscribeFinish(0n) }], true, ); expect(await track.closed).toBeNull(); @@ -2220,7 +2219,7 @@ function drainReactions(): Promise { }); } -test("a local unsubscribe owes no SUBSCRIBE_END", async () => { +test("a local unsubscribe owes no SUBSCRIBE_FINISH", async () => { const { quic, streams, opened } = fakeSession(); const subscriber = new Subscriber(quic, Version.DRAFT_07, HopSchema.parse(1n)); const responseTask = Promise.withResolvers(); @@ -2469,7 +2468,7 @@ test("a lite-07 rewind is refused", async () => { } }); -test("a publisher FIN without SUBSCRIBE_END still violates an open subscription", async () => { +test("a publisher FIN without SUBSCRIBE_FINISH still violates an open subscription", async () => { const { quic, streams, opened, closes } = fakeSession(); const subscriber = new Subscriber(quic, Version.DRAFT_07, HopSchema.parse(1n)); const broadcast = subscriber.consume(Path.from("room")); @@ -2480,7 +2479,7 @@ test("a publisher FIN without SUBSCRIBE_END still violates an open subscription" await (await opened(1)).reading; streams[1].inbound.close(); expect(await track.closed).toBeInstanceOf(ProtocolViolation); - expect((track.closed.peek() as Error).message).toBe("subscribe stream ended without SUBSCRIBE_END"); + expect((track.closed.peek() as Error).message).toBe("subscribe stream ended without SUBSCRIBE_FINISH"); await drainReactions(); expect(closes.map((info) => info?.closeCode)).toEqual([SessionCode.ProtocolViolation]); } finally { @@ -2536,98 +2535,6 @@ test("an empty START remains resolved when its range widens", async () => { } }); -// A lite-07 publisher FINs a bounded subscription without SUBSCRIBE_END once it covered the range, -// which is no protocol violation. Covering a range is not ending the track, so the copy stays for -// its readers, as the Rust copy does: they park, and widening the range asks again on the same -// track. Decided 2026-10-31 (maintainer). -test("a lite-07 publisher FIN keeps a covered bounded subscription's copy", async () => { - const version = Version.DRAFT_07; - const { quic, streams, opened, closes } = fakeSession(); - const subscriber = new Subscriber(quic, version, HopSchema.parse(1n)); - const broadcast = subscriber.consume(Path.from("room")); - const track = broadcast.track("video").subscribe({ ranges: [{ start: { absolute: 0n }, end: { absolute: 2n } }] }); - try { - await opened(0); - await answerTrackInfo(streams[0], version); - const stream = await opened(1); - await stream.reading; - const chunks: Uint8Array[] = []; - const writer = new Writer( - new WritableStream({ write: (chunk) => void chunks.push(new Uint8Array(chunk)) }), - version, - ); - await encodeSubscribeResponse(writer, { start: new SubscribeStart([0n]) }, version); - await encodeSubscribeResponse( - writer, - { drop: new SubscribeDrop({ start: 0n, end: 1n, error: StreamCode.NotFound }) }, - version, - ); - for (const chunk of chunks) stream.inbound.enqueue(chunk); - stream.inbound.close(); - await drainReactions(); - expect(track.closed.peek()).toBeUndefined(); - expect(streams).toHaveLength(2); - - track.update({ ranges: [{ start: { absolute: 0n }, end: { absolute: 3n } }] }); - const reopened = await opened(2); - await reopened.reading; - const reader = new Reader(undefined, new Uint8Array(Buffer.concat(reopened.written)), version); - expect(await reader.u53()).toBe(StreamId.Subscribe); - const msg = await Subscribe.decode(reader, version); - expect(msg.ranges).toEqual([{ start: { absolute: 0n }, end: { absolute: 3n } }]); - expect(track.closed.peek()).toBeUndefined(); - expect(closes).toEqual([]); - } finally { - track.close(); - broadcast.close(); - subscriber.close(); - } -}); - -// A widening reopens a covered copy, and its readers leaving while the publisher never takes the -// new SUBSCRIBE gives the setup up: its stream is reset and the copy released, not held forever. -test("a reopened lite-07 subscription ends once its readers leave", async () => { - const version = Version.DRAFT_07; - const { quic, streams, opened } = fakeSession([], [2]); - const subscriber = new Subscriber(quic, version, HopSchema.parse(1n)); - const broadcast = subscriber.consume(Path.from("room")); - const track = broadcast.track("video").subscribe({ ranges: [{ start: { absolute: 0n }, end: { absolute: 2n } }] }); - let next: TrackSubscriber | undefined; - try { - await opened(0); - await answerTrackInfo(streams[0], version); - const stream = await opened(1); - await stream.reading; - const chunks: Uint8Array[] = []; - const writer = new Writer( - new WritableStream({ write: (chunk) => void chunks.push(new Uint8Array(chunk)) }), - version, - ); - await encodeSubscribeResponse(writer, { start: new SubscribeStart([0n]) }, version); - await encodeSubscribeResponse( - writer, - { drop: new SubscribeDrop({ start: 0n, end: 1n, error: StreamCode.NotFound }) }, - version, - ); - for (const chunk of chunks) stream.inbound.enqueue(chunk); - stream.inbound.close(); - await drainReactions(); - - track.update({ ranges: [{ start: { absolute: 0n }, end: { absolute: 3n } }] }); - await (await opened(2)).writing; - track.close(); - await drainReactions(); - // A reader arriving now asks afresh, rather than join a copy held by the stalled setup. - next = broadcast.track("video").subscribe(); - await opened(3); - } finally { - next?.close(); - track.close(); - broadcast.close(); - subscriber.close(); - } -}); - // Before lite-05 the publisher FINs the subscribe stream only once the track is over, so that FIN // finishes the track once the tail settles, rather than releasing it for another route. test.each( @@ -2666,7 +2573,7 @@ test.each( // A session that dies once the publisher ended the track, but before the tail settled, cuts it // off: the track ends with the session's error unless the declared end settled first. That end -// is SUBSCRIBE_END on lite-05+, and the FIN before it. +// is SUBSCRIBE_FINISH on lite-05+, and the FIN before it. test.each([ ["lite-03 ends clean with its last group finished", Version.DRAFT_03, true], ["lite-03 aborts with its last group still open", Version.DRAFT_03, false], @@ -2689,7 +2596,7 @@ test.each([ await subscribe.reading; const responses: SubscribeResponse[] = legacy ? [{ ok: new SubscribeOk({}) }] - : [{ start: new SubscribeStart([0n]) }, { end: new SubscribeEnd(1n) }]; + : [{ start: new SubscribeStart([0n]) }, { finish: new SubscribeFinish(1n) }]; await respond(subscribe, version, responses, true); if (settled) { const group = new ReadableStream({ @@ -2719,74 +2626,6 @@ test.each([ } }); -// The FIN may answer the bounded range before the publisher read an update that widened it, so -// the copy asks again for the demand rather than end clean short of it or fail its readers. -test.each([ - ["an open widening after SUBSCRIBE_START", { start: { absolute: 0n } }, true, false], - ["a bounded widening without SUBSCRIBE_START", { start: { absolute: 0n }, end: { absolute: 5n } }, false, false], - ["a widening the publisher stopped reading", { start: { absolute: 0n }, end: { absolute: 5n } }, false, true], -] as const)("a lite-07 FIN short of %s subscribes again on the same track", async (_, range, start, stopped) => { - const clock = new Manual(); - const installed = clock.install(); - const version = Version.DRAFT_07; - const { quic, streams, opened, closes } = fakeSession(); - const subscriber = new Subscriber(quic, version, HopSchema.parse(1n)); - const broadcast = subscriber.consume(Path.from("room")); - const track = broadcast - .track("video") - .subscribe({ delay: { max: Milli.zero }, ranges: [{ start: { absolute: 0n }, end: { absolute: 1n } }] }); - try { - await opened(0); - await answerTrackInfo(streams[0], version); - const stream = await opened(1); - await stream.reading; - const refused = stopped - ? stream.stop( - Object.assign(new Error("The stream was aborted by the remote server"), { - source: "stream", - streamErrorCode: StreamCode.Cancel, - }), - ) - : undefined; - track.update({ delay: { max: Milli.zero }, ranges: [range] }); - await refused; - await drainReactions(); - - const chunks: Uint8Array[] = []; - const writer = new Writer( - new WritableStream({ write: (chunk) => void chunks.push(new Uint8Array(chunk)) }), - version, - ); - if (start) { - await encodeSubscribeResponse(writer, { start: new SubscribeStart([0n]) }, version); - await encodeSubscribeResponse( - writer, - { drop: new SubscribeDrop({ start: 0n, end: 0n, error: StreamCode.NotFound }) }, - version, - ); - } - for (const chunk of chunks) stream.inbound.enqueue(chunk); - stream.inbound.close(); - await drainReactions(); - - await clock.advance(Nano.fromMilli(GRACE)); - const reopened = await opened(2); - await reopened.reading; - const reader = new Reader(undefined, new Uint8Array(Buffer.concat(reopened.written)), version); - expect(await reader.u53()).toBe(StreamId.Subscribe); - const msg = await Subscribe.decode(reader, version); - expect(msg.id).toBe(1n); - expect(msg.ranges).toEqual([range]); - expect(track.closed.peek()).toBeUndefined(); - expect(closes).toEqual([]); - } finally { - track.close(); - broadcast.close(); - subscriber.close(); - installed.close(); - } -}); - // A publisher that finishes the track stops reading its subscribe stream, so a SUBSCRIBE_UPDATE // written then fails. The track still ends with the publisher's finish; a reset still fails it. test.each([ @@ -2821,7 +2660,7 @@ test.each([ new WritableStream({ write: (chunk) => void chunks.push(new Uint8Array(chunk)) }), version, ); - await encodeSubscribeResponse(writer, { end: new SubscribeEnd(0n) }, version); + await encodeSubscribeResponse(writer, { finish: new SubscribeFinish(0n) }, version); for (const chunk of chunks) stream.inbound.enqueue(chunk); stream.inbound.close(); expect(await track.closed).toBeNull(); @@ -2870,7 +2709,7 @@ test("an unknown subscribe response type closes the session", async () => { await answerTrackInfo(streams[0], Version.DRAFT_07); await (await opened(1)).reading; // An unassigned response type is a session protocol violation. - streams[1].inbound.enqueue(new Uint8Array([3, 0])); + streams[1].inbound.enqueue(new Uint8Array([5, 0])); expect(await track.closed).toBeInstanceOf(ProtocolViolation); await drainReactions(); expect(closes.map((info) => info?.closeCode)).toEqual([SessionCode.ProtocolViolation]); diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index 8ed50deec9a..ccd153d61b5 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -37,7 +37,7 @@ import { abandoned, isAbandoned, unwanted } from "../util/demand.ts"; import { LIMITS, type Limits, SlotMap, Slots } from "../util/limits.ts"; import { min, UNBOUNDED } from "../util/sequence.ts"; import { Budget } from "../util/yield.ts"; -import { bounded, hasTime, rangesStart, sameRanges, Window, wireRanges, wireSpan } from "../window.ts"; +import { hasTime, rangesStart, sameRanges, Window, wireRanges, wireSpan } from "../window.ts"; import { overrideBroadcastWire, wireOf } from "../wire.ts"; import { AnnounceHistory, @@ -58,9 +58,12 @@ import { decodeSubscribeResponseMaybe, EMPTY_RANGE, inclusiveGroupEnd, + LiveRanges, + requestedRanges, Subscribe, SubscribeUpdate, toWireRanges, + type WireRange, } from "./subscribe.ts"; import { TrackInfo, Track as TrackMessage } from "./track.ts"; import { decodeTracksEventMaybe, Tracks as TracksMessage } from "./tracks.ts"; @@ -140,19 +143,21 @@ interface SubscribeEntry { // The first group the SUBSCRIBE asked for, if any. requested?: bigint; // The first group the publisher serves (SUBSCRIBE_START) and the track's exclusive end - // (SUBSCRIBE_END), once it declares them. + // (SUBSCRIBE_FINISH), once it declares them. start?: bigint; end?: bigint; - // Where SUBSCRIBE_START says each of `asked` starts on lite-07, undefined for one with - // nothing to deliver. It answers `asked` unless `updated`. - starts?: (bigint | undefined)[]; + // The ranges on the wire by Range ID (lite-07), which each update edits. + live: LiveRanges; + // Where SUBSCRIBE_START or SUBSCRIBE_STARTED says each live range starts on lite-07, by Range + // ID, undefined for one with nothing to deliver. + starts: Map; // Whether a SUBSCRIBE_UPDATE went out, which the publisher may have read before answering. updated?: boolean; // Whether SUBSCRIBE_START arrived, which `start` alone cannot say when no range starts. started?: boolean; // Whether our side is FINned, which lite-07 does once the tail settles past SUBSCRIBE_FINISH. fin?: boolean; - // How many SUBSCRIBE_UPDATEs went out, so a tail settling past SUBSCRIBE_END waits a fresh + // How many SUBSCRIBE_UPDATEs went out, so a tail settling past SUBSCRIBE_FINISH waits a fresh // grace for what one sent meanwhile asks for. sent?: number; // Set by the subscribe stream's FIN before lite-05, the track's only end there. @@ -160,11 +165,7 @@ interface SubscribeEntry { // Settles once the subscribe stream's responses are read to its FIN or reset, which // Subscriber.drain waits on. responses?: Promise; - // Whether the stream ever asked for a bounded range, which a lite-07 publisher FINs without - // SUBSCRIBE_END once it covers it. - bounded: boolean; - // The ranges the SUBSCRIBE asked for, as `wireRanges` gives them, which that FIN covers - // unless the demand moved since in an update the publisher may not have read. + // The ranges the SUBSCRIBE asked for, as `wireRanges` gives them. asked: track.Range[]; // The TRACK stream that answered (lite-05+), held open as interest in the track until // the SUBSCRIBE has its first response, so the publisher's demand never lapses between @@ -1052,77 +1053,11 @@ export class Subscriber { } valid?.removeEventListener("abort", retire); - let { stream, entry } = opened; + const { stream, entry } = opened; // Lite-05+ FETCHes what the copy's readers miss for as long as the copy lives, across // every subscription that feeds it. if (hasTrackStream(this.version)) void this.#runFills(broadcast, consumed, entry); - let current = { id, msg }; - // A lite-07 publisher that FINs a covered bounded subscription leaves the copy to its - // readers. If the demand moved past what it covered, ask again on the same track, as the - // Rust copy does, rather than fail the readers still waiting on it. - while (await this.#serveStream(current.id, broadcast, consumed, state, stream, entry, current.msg)) { - const producer = entry.track; - const subscription = producer.subscription.peek(); - const ranges = wireRanges(subscription?.ranges ?? [{}]); - if (!subscription || ranges.length === 0) { - hooks.releaseTrack(producer); - return; - } - const id = this.#subscribeNext++; - const msg = this.#subscribeMessage(id, broadcast, consumed, producer.name, subscription, ranges); - console.debug(`subscribe reopen: id=${id} broadcast=${broadcast} track=${producer.name}`); - // Bounded like the first setup, and given up once nobody wants the track, so a peer - // that stops reading holds neither the stream nor its entry. - // Watched only while the setup runs, so a reopen leaves no watcher behind. - const left = new AbortController(); - const demand = producer.demand(); - let unwatch: Dispose | undefined; - const watch = () => { - if (isAbandoned(demand)) left.abort(new StreamError(StreamCode.Cancel, { message: "cancel" })); - else unwatch = Signal.race(demand.wanted, hooks.demandReleased(demand)).changed(watch); - }; - watch(); - let sent: Promise | undefined; - valid?.addEventListener("abort", retire); - try { - if (valid?.aborted) retire(); - state.cancel.signal.throwIfAborted(); - const framing = { compress: entry.compress, timescale: entry.timescale, scale: entry.scale }; - entry = this.#register( - id, - this.#withRanges(msg, ranges, entry.scale), - ranges, - producer, - framing, - state, - ); - const deadline = new Deadline(); - sent = this.#sendSubscribe(state, msg, deadline); - stream = await deadline.wait(untilAborted(untilAborted(sent, valid), left.signal)); - current = { id, msg }; - } catch (err) { - const e = - state.revoked ?? (left.signal.aborted ? left.signal.reason : await sessionCause(this.#quic, err)); - state.cancel.abort(e); - this.#subscribes.delete(id); - // A stream that opens late is reset too, as the first setup's are. - const close = () => state.stream?.abort(e); - close(); - sent?.then(close, close); - if (left.signal.aborted && !state.revoked) { - hooks.releaseTrack(producer); - return; - } - producer.abort(e); - console.warn( - `subscribe error: id=${id} broadcast=${broadcast} track=${producer.name} error=${reason(e)}`, - ); - return; - } finally { - unwatch?.(); - valid?.removeEventListener("abort", retire); - } - } + await this.#serveStream(id, broadcast, consumed, state, stream, entry, msg); } // The SUBSCRIBE for `subscription`, asking for `ranges`, its wire ranges in the model. Its own @@ -1159,9 +1094,7 @@ export class Subscriber { }; } - // Serve one SUBSCRIBE stream into its track until it ends. Resolves true when a lite-07 - // publisher covered the bounded ranges it read but an update since moved the demand past - // them, leaving the track open for the caller to ask again. + // Serve one SUBSCRIBE stream into its track until it ends. async #serveStream( id: bigint, broadcast: Path.Valid, @@ -1170,7 +1103,7 @@ export class Subscriber { stream: Stream, entry: SubscribeEntry, msg: Subscribe, - ): Promise { + ): Promise { const { answering } = consumed; const producer = entry.track; // Sent but unanswered (lite-05+; older drafts already waited for SUBSCRIBE_OK), only a @@ -1192,10 +1125,8 @@ export class Subscriber { const responses = this.runResponses(stream, entry); entry.responses = responses.catch(() => {}); let tailSettled = false; - // Set when the publisher ended the subscription short of the demand. - let short = false; const closed = responses.then(async () => { - short = !(await this.#settleTail(entry)); + await this.#settleTail(entry); tailSettled = true; }); // A reset that lands after the race below settled is moot; the race observes one before. @@ -1238,26 +1169,15 @@ export class Subscriber { else if (!entry.fin) stream.close(); console.debug(`subscribe close: id=${id} broadcast=${broadcast} track=${producer.name}`); - // A publisher that covered the range before reading an update widening it sent fewer - // groups than the demand asks for, so the caller asks again for the rest. A lite-07 - // FIN without SUBSCRIBE_END that covered the demand leaves the copy to its readers - // until the demand moves past it. Otherwise only a received SUBSCRIBE_END, or before - // lite-05 a FIN, declares the track's end: a subscription nobody wants anymore is a - // released copy, not finished content. - const live = reason_ !== idle && hooks.trackReleased(producer).peek() === undefined; - const covered = live && tailSettled && entry.end === undefined && hasRanges(this.version); - const reopen = live && (short || (covered && (await this.#parkCovered(entry)))); - if (!reopen) { - if (tailSettled && entry.finished) producer.finish(); - hooks.releaseTrack(producer); - } - return reopen; + // Only a received SUBSCRIBE_FINISH, or before lite-05 a FIN, declares the track's end: a + // subscription nobody wants anymore is a released copy, not finished content. + if (tailSettled && entry.finished) producer.finish(); + hooks.releaseTrack(producer); } catch (err) { const e = await sessionCause(this.#quic, err); producer.abort(e); console.warn(`subscribe error: id=${id} broadcast=${broadcast} track=${producer.name} error=${reason(e)}`); stream.abort(e); - return false; } finally { answering?.removeEventListener("abort", replaced); entry.held?.close(); @@ -1265,21 +1185,6 @@ export class Subscriber { } } - // Keep a copy whose lite-07 publisher covered its bounded ranges, as the Rust copy does: its - // readers park rather than end, the cache answers the same ranges, and an update can still - // widen them. Resolves true once the demand asks for groups it lacks, to ask again on the - // same track, and false once nobody wants the track or it closed. - async #parkCovered(entry: SubscribeEntry): Promise { - const producer = entry.track; - const demand = producer.demand(); - const released = hooks.trackReleased(producer); - for (;;) { - if (released.peek() !== undefined || isAbandoned(demand)) return false; - if (!this.#demandCovered(entry)) return true; - await race([Signal.race(producer.subscription, demand.wanted, hooks.demandReleased(demand)), released]); - } - } - // Open the subscribe stream and accept the request (so the application's track.Subscriber // resolves and incoming groups have a producer to write into) with the track's immutable // properties. `state.stream` is populated as soon as it opens, and `state.track` once @@ -1413,8 +1318,9 @@ export class Subscriber { grace: () => hooks.tailGrace(producer), }), requested: msg.startGroup, - bounded: hasRanges(this.version) && bounded(ranges), asked: [...ranges], + live: new LiveRanges(this.version, msg.ranges ?? []), + starts: new Map(), }; this.#subscribes.set(id, entry); state.producer = producer; @@ -1876,9 +1782,9 @@ export class Subscriber { // accounted against: SUBSCRIBE_START/END/DROP on lite-05+, and any SUBSCRIBE_DROP after the // OK before then, where the FIN itself ends the track. Reading to the FIN, rather than // awaiting it, matters: a pull-driven transport stream closes only on a read. - // SUBSCRIBE_END declares the track's end right away, so a consumer learns it before the + // SUBSCRIBE_FINISH declares the track's end right away, so a consumer learns it before the // last groups arrive. The publisher must declare the end before FIN unless we cancelled - // locally, or on lite-07 it covered a bounded range; resets and malformed responses reject. + // locally; resets and malformed responses reject. private async runResponses(stream: Stream, entry: SubscribeEntry): Promise { for (;;) { const resp = await decodeSubscribeResponseMaybe(stream.reader, this.version); @@ -1893,22 +1799,25 @@ export class Subscriber { entry.finished = true; return; } - // A bounded range may have widened since, in an update the publisher never read. - const covered = hasRanges(this.version) && entry.bounded; - if (entry.end === undefined && !covered && hooks.trackReleased(entry.track).peek() === undefined) - throw new ProtocolViolation("subscribe stream ended without SUBSCRIBE_END"); + if (entry.end === undefined && hooks.trackReleased(entry.track).peek() === undefined) + throw new ProtocolViolation("subscribe stream ended without SUBSCRIBE_FINISH"); return; } if ("ok" in resp) throw new ProtocolViolation("duplicate SUBSCRIBE_OK"); if ("start" in resp) { - // One start per range the SUBSCRIBE carried, unless an update the publisher read - // first replaced them. - if (hasRanges(this.version) && !entry.updated && resp.start.starts.length !== entry.asked.length) - throw new ProtocolViolation( - `SUBSCRIBE_START resolves ${resp.start.starts.length} ranges, not ${entry.asked.length}`, - ); - if (hasRanges(this.version)) entry.starts = resp.start.starts; + if (hasRanges(this.version)) { + if (entry.started) throw new ProtocolViolation("duplicate SUBSCRIBE_OK"); + // One start per range the SUBSCRIBE carried, by Range ID. + if (BigInt(resp.start.starts.length) !== entry.live.initial) + throw new ProtocolViolation( + `SUBSCRIBE_OK resolves ${resp.start.starts.length} ranges, not ${entry.live.initial}`, + ); + // An update may have retired some already, whose starts say nothing now. + resp.start.starts.forEach((start, index) => { + if (entry.live.get(BigInt(index))) entry.starts.set(BigInt(index), start); + }); + } // The lowest range start is the first group served; none leaves nothing owed. entry.start = resp.start.starts.reduce( (lowest, start) => @@ -1924,27 +1833,38 @@ export class Subscriber { // demand asks later. One that still arrives is delivered. if (entry.requested !== undefined && entry.start !== undefined) entry.tail.account(entry.requested, entry.start); - } else if ("end" in resp) { - if (entry.end !== undefined) throw new ProtocolViolation("duplicate SUBSCRIBE_END"); - entry.end = resp.end.group; + } else if ("finish" in resp) { + if (entry.end !== undefined) throw new ProtocolViolation("duplicate SUBSCRIBE_FINISH"); + entry.end = resp.finish.group; // A local close can win the race with the response; there is nothing left to end. if (hooks.trackReleased(entry.track).peek() !== undefined) continue; try { // Lower groups may still be on the wire behind a higher one, so readers hold // at a hole below the end until the tail settles. - hooks.endTrack(entry.track, { group: entry.end, frame: 0 }, resp.end.cause()); + hooks.endTrack(entry.track, { group: entry.end, frame: 0 }, resp.finish.cause()); } catch (err) { // lite-05 specified an inclusive end, and @moq/net 0.1.3 to 0.1.9 sent one, so // there an end below a received group only costs the early boundary: the FIN // still finishes the track. Later drafts made it exclusive. if (this.version !== Version.DRAFT_05) { - throw new ProtocolViolation(`invalid SUBSCRIBE_END: ${reason(error(err))}`); + throw new ProtocolViolation(`invalid SUBSCRIBE_FINISH: ${reason(error(err))}`); } - console.warn(`invalid SUBSCRIBE_END: ${reason(error(err))}`); + console.warn(`invalid SUBSCRIBE_FINISH: ${reason(error(err))}`); } // Lite-07 keeps the stream open past SUBSCRIBE_FINISH: FIN once the tail settles, // and the publisher's FIN in reply ends this loop. if (keepsSubscribeOpen(this.version)) void this.#finishTail(stream, entry).catch(() => {}); + } else if ("started" in resp) { + // Where a range an update added starts, from which its groups are owed. + const { id, start } = resp.started; + if (!entry.started || !entry.live.assigned(id) || id < entry.live.initial || entry.starts.has(id)) + throw new ProtocolViolation(`unexpected SUBSCRIBE_STARTED: ${id}`); + // One retired since is owed nothing. + if (entry.live.get(id)) { + entry.starts.set(id, start); + // The tail judges what is owed again. + entry.tail.touch(); + } } else if ("drop" in resp) { // The publisher will never send these groups, so they are accounted for without a // stream, and a reader of one still arriving ends with the drop's error now. @@ -1954,7 +1874,7 @@ export class Subscriber { } } - // Settle a lite-07 tail past SUBSCRIBE_END, then FIN. An update sent meanwhile asks for more, + // Settle a lite-07 tail past SUBSCRIBE_FINISH, then FIN. An update sent meanwhile asks for more, // which waits a fresh grace rather than one that may have passed while a stream was read. async #finishTail(stream: Stream, entry: SubscribeEntry): Promise { for (;;) { @@ -1977,71 +1897,43 @@ export class Subscriber { // Received headers and SUBSCRIBE_DROP account for the range. An unaccounted // sequence, such as a lost datagram, still waits out the grace. Streams whose // headers arrived keep reading until their own FIN or reset. - // - // Resolves false when a lite-07 publisher's FIN without SUBSCRIBE_END may have left the - // demand uncovered: it covered the bounded range it read, and the demand has moved since. - async #settleTail(entry: SubscribeEntry): Promise { + async #settleTail(entry: SubscribeEntry): Promise { const { tail, track } = entry; - const complete = () => this.#covered(entry); - const released = hooks.trackReleased(track); - await tail.settle(complete, released); - if (entry.end !== undefined || !hasRanges(this.version) || released.peek() !== undefined) return true; - return this.#demandCovered(entry); - } - - // Whether a lite-07 FIN without SUBSCRIBE_END covered the demand: the ranges it asked for, or - // the groups that arrived account for the demand since. - #demandCovered(entry: SubscribeEntry): boolean { - const demand = wireRanges(entry.track.subscription.peek()?.ranges ?? [{}]); - if (sameRanges(demand, entry.asked)) return true; - // Without SUBSCRIBE_START nothing says which ranges the publisher read, so only the - // groups that arrived prove the demand covered. - return entry.start !== undefined && this.#covered(entry); + await tail.settle(() => this.#covered(entry), hooks.trackReleased(track)); } // Whether the groups received and dropped account for every group the demand is owed once // the publisher ended the subscription. #covered(entry: SubscribeEntry): boolean { + // Without SUBSCRIBE_FINISH (older drafts) nothing says which groups are owed. + if (entry.end === undefined) return false; const ranges = wireRanges(entry.track.subscription.peek()?.ranges ?? [{}]); - const span = wireSpan(ranges); - // Without SUBSCRIBE_END (older drafts) nothing says which groups are owed, unless a - // lite-07 publisher covered the bounded ranges. - const end = - entry.end === undefined - ? hasRanges(this.version) - ? span.end - : undefined - : min(entry.end, span.end ?? entry.end); - if (end === undefined) return false; - // Without SUBSCRIBE_START the publisher served no group at all, though lite-07 still - // answers with one once the track ends, unless nothing was asked below the end. One - // that started no range owes nothing either, unless an update since asked from a floor - // below the end, which lite-07's open stream still serves. + const end = min(entry.end, wireSpan(ranges).end ?? entry.end); const floor = rangesStart(ranges); - let first = entry.start; - if (first === undefined) { - if (!keepsSubscribeOpen(this.version)) return true; + // Lite-07 answers with SUBSCRIBE_OK once the track ends, unless nothing was asked below the + // end. Each range the publisher holds is then owed from the start SUBSCRIBE_OK or + // SUBSCRIBE_STARTED named, which accounts for a skipped prefix and resolves a relative or + // time start; one whose SUBSCRIBE_STARTED is still on its way owes what nothing says yet. + if (hasRanges(this.version)) { if (!entry.started) return !(end > (floor ?? 0n)); - if (!entry.updated || floor === undefined || floor >= end) return true; - first = floor; + const owed: track.Range[] = []; + for (const [id, range] of entry.live.entries()) { + if (!entry.starts.has(id)) return false; + const at = entry.starts.get(id); + if (at === undefined) continue; + const [model] = requestedRanges({ startFrame: 0, ranges: [range] }, entry.scale); + owed.push({ ...model, start: { absolute: at } }); + } + const window = new Window(owed, entry.track.largest()?.group); + return window.runs(0n, end).every((run) => entry.tail.covers(run.start, run.end)); } - // Owed from the floor the demand last asked for, which an update can move either - // way, or where SUBSCRIBE_START resolved a live-edge one. The groups the SUBSCRIBE - // asked for below its SUBSCRIBE_START were accounted for when it arrived. Only the - // groups inside the ranges are owed: the publisher accounts for nothing between them. - // While SUBSCRIBE_START answers these ranges, each is owed from the start it named, which - // accounts for a skipped prefix and resolves a relative or time start. Otherwise a time - // start resolved at the publisher, at the lowest start it named. - const starts = !entry.updated && sameRanges(ranges, entry.asked) && entry.starts; - const start = starts ? first : (floor ?? first); - const owed = starts - ? ranges.flatMap((range, index) => { - const at = starts[index]; - return at === undefined ? [] : [{ ...range, start: { absolute: at } }]; - }) - : ranges.map((range) => (range.start?.time ? { ...range, start: { absolute: start } } : range)); - const window = new Window(owed, entry.track.largest()?.group); - return window.runs(start, end).every((run) => entry.tail.covers(run.start, run.end)); + // Without SUBSCRIBE_START the publisher served no group at all. Otherwise each group is owed + // from the floor the demand last asked for, which an update can move either way, or where + // SUBSCRIBE_START resolved a live-edge one. The groups the SUBSCRIBE asked for below its + // SUBSCRIBE_START were accounted for when it arrived. + if (entry.start === undefined) return true; + const window = new Window(ranges, entry.track.largest()?.group); + return window.runs(floor ?? entry.start, end).every((run) => entry.tail.covers(run.start, run.end)); } /** @@ -2113,6 +2005,13 @@ export class Subscriber { if (!sameRanges(ranges, wireRanges(lastSent.ranges ?? [{}]))) entry.tail.demand(start ?? entry.start ?? 0n, UNBOUNDED); + // Lite-07 edits the live ranges: a range already live keeps its Range ID, and where it + // started. The retired ones owe nothing more. + let edit: { remove: bigint[]; add: WireRange[] } | undefined; + if (hasRanges(this.version)) { + edit = entry.live.edit(toWireRanges(ranges, entry.scale)); + for (const id of entry.starts.keys()) if (!entry.live.get(id)) entry.starts.delete(id); + } // Round-trip the other Subscribe parameters so the publisher doesn't // interpret SUBSCRIBE_UPDATE as a reset of ordered/delay/etc. const update = new SubscribeUpdate({ @@ -2123,7 +2022,8 @@ export class Subscriber { minDelay: minDelayOf(current), startGroup: span.start, endGroup: inclusiveGroupEnd(span.end), - ranges: hasRanges(this.version) ? toWireRanges(ranges, entry.scale) : undefined, + ranges: edit?.add, + remove: edit?.remove, }); // Encoded apart from the write, so a bound the wire can't carry still fails the track. const encoded = await Writer.collect(this.version, (w) => update.encode(w, this.version)); @@ -2135,10 +2035,9 @@ export class Subscriber { return; } if (refresh !== undefined) sentWanted = refresh; - entry.bounded ||= bounded(ranges); entry.updated = true; entry.sent = (entry.sent ?? 0) + 1; - // A tail settling past SUBSCRIBE_END re-judges what the update now asks for. + // A tail settling past SUBSCRIBE_FINISH re-judges what the update now asks for. entry.tail.touch(); lastSent = { ...current }; console.debug(`subscribe update: id=${id} broadcast=${broadcast} track=${track.name}`); diff --git a/js/net/src/lite/tail.test.ts b/js/net/src/lite/tail.test.ts index c13c5d0655a..c7a6f530e3b 100644 --- a/js/net/src/lite/tail.test.ts +++ b/js/net/src/lite/tail.test.ts @@ -15,13 +15,15 @@ import { encodeSubscribeResponse, Subscribe, SubscribeDrop, - SubscribeEnd, + SubscribeFinish, type SubscribeResponse, SubscribeStart, + SubscribeStarted, + SubscribeUpdate, } from "./subscribe.ts"; import { Subscriber } from "./subscriber.ts"; import { TrackInfo, Track as TrackMessage } from "./track.ts"; -import { ALPN_05, resolvesStart, Version } from "./version.ts"; +import { ALPN_05, hasRanges, resolvesStart, Version } from "./version.ts"; // The subscription's max delay, which is also how long it waits for a group that never arrives. const GRACE = Milli(100); @@ -78,6 +80,7 @@ async function subscribed(version: Version, maxDelay = GRACE, range?: Range) { subscriber, reader, respond: (resp: SubscribeResponse) => encodeSubscribeResponse(sub.writer, resp, version), + update: () => SubscribeUpdate.decode(sub.reader, version), fin: () => sub.writer.close(), subscriberFin: () => sub.reader.done(), reset: (error: Error) => sub.writer.reset(error), @@ -125,7 +128,7 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver const first = groupStream(subscriber, 0n); first.write("0.0"); first.finish(); - await respond({ end: new SubscribeEnd(2n) }); + await respond({ finish: new SubscribeFinish(2n) }); await fin(); // The end is known before the last group arrives. @@ -149,7 +152,7 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver const group = groupStream(subscriber, 0n); group.write("0.0"); - await respond({ end: new SubscribeEnd(1n) }); + await respond({ finish: new SubscribeFinish(1n) }); await fin(); const received = await reader.recvGroup(); expect(await received?.readString()).toBe("0.0"); @@ -167,7 +170,7 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver await respond({ start: new SubscribeStart([0n]) }); const group = groupStream(subscriber, 0n); group.write("0.0"); - await respond({ end: new SubscribeEnd(1n) }); + await respond({ finish: new SubscribeFinish(1n) }); await fin(); const received = await reader.recvGroup(); @@ -196,7 +199,7 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver group.write("1.0"); group.finish(); await group.handled; - await respond({ end: new SubscribeEnd(2n) }); + await respond({ finish: new SubscribeFinish(2n) }); await fin(); // Group 0 has no header, so accounting still waits, and its stream may yet @@ -228,14 +231,14 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver }); // QUIC does not order streams, so a lower group's stream can land after a higher one and - // after SUBSCRIBE_END. The reader receives it before its end, which then comes at once. - test("a lower group that lands after SUBSCRIBE_END reaches the reader", async () => { + // after SUBSCRIBE_FINISH. The reader receives it before its end, which then comes at once. + test("a lower group that lands after SUBSCRIBE_FINISH reaches the reader", async () => { const { subscriber, reader, respond, fin } = await subscribed(version, Milli(60_000)); await respond({ start: new SubscribeStart([0n]) }); const high = groupStream(subscriber, 1n); high.finish(); await high.handled; - await respond({ end: new SubscribeEnd(2n) }); + await respond({ finish: new SubscribeFinish(2n) }); expect(await reader.finished()).toEqual({ group: 2n, frame: 0 }); expect((await reader.recvGroup())?.sequence).toBe(1n); @@ -264,7 +267,7 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver const group = groupStream(subscriber, 1n); group.write("1.0"); group.finish(); - await respond({ end: new SubscribeEnd(2n) }); + await respond({ finish: new SubscribeFinish(2n) }); await fin(); expect((await reader.recvGroup())?.sequence).toBe(1n); @@ -300,9 +303,9 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver subscriber.close(); }); - test("a subscription that served nothing ends at SUBSCRIBE_END", async () => { + test("a subscription that served nothing ends at SUBSCRIBE_FINISH", async () => { const { reader, respond, fin } = await subscribed(version, Milli(60_000)); - await respond({ end: new SubscribeEnd(0n) }); + await respond({ finish: new SubscribeFinish(0n) }); await fin(); expect(await settlesWithin(reader.recvGroup(), 1000)).toBe(true); @@ -311,10 +314,10 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver }); // lite-05 specified an inclusive end, so there the group costs only its own stream. - test(`a group at or past SUBSCRIBE_END ${version === Version.DRAFT_05 ? "is dropped" : "aborts the track"}`, async () => { + test(`a group at or past SUBSCRIBE_FINISH ${version === Version.DRAFT_05 ? "is dropped" : "aborts the track"}`, async () => { const { subscriber, reader, respond } = await subscribed(version); await respond({ start: new SubscribeStart([0n]) }); - await respond({ end: new SubscribeEnd(2n) }); + await respond({ finish: new SubscribeFinish(2n) }); expect(await reader.finished()).toEqual({ group: 2n, frame: 0 }); const handled = groupStream(subscriber, 2n).handled; @@ -341,7 +344,7 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver const group = groupStream(subscriber, 3n); group.finish(); await group.handled; - await respond({ end: new SubscribeEnd(4n) }); + await respond({ finish: new SubscribeFinish(4n) }); await fin(); expect((await reader.recvGroup())?.sequence).toBe(3n); @@ -351,7 +354,9 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver test("a lowered floor owes the groups it newly asked for", async () => { const maxDelay = Milli(60_000); - const { subscriber, respond, fin } = await subscribed(version, maxDelay, { start: { absolute: 3n } }); + const { subscriber, respond, update, fin } = await subscribed(version, maxDelay, { + start: { absolute: 3n }, + }); await respond({ start: new SubscribeStart([3n]) }); const group = groupStream(subscriber, 3n); group.write("3.0"); @@ -363,7 +368,13 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver .consume(Path.from("room")) .track("video") .subscribe({ delay: { max: maxDelay }, ranges: [{ start: { absolute: 1n } }] }); - await respond({ end: new SubscribeEnd(4n) }); + // Lite-07 retires the range and adds the lowered one, whose start SUBSCRIBE_STARTED reports. + const lowered = await update(); + if (hasRanges(version)) { + expect([lowered.remove, lowered.ranges]).toEqual([[0n], [{ start: { absolute: 1n } }]]); + await respond({ started: new SubscribeStarted(1n, 1n) }); + } + await respond({ finish: new SubscribeFinish(4n) }); await fin(); // Long enough for a subscription that owed nothing more to have ended. await sleep(Nano.fromMilli(Milli(50))); @@ -389,13 +400,13 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver // lite-05 specified an inclusive end, and @moq/net 0.1.3 to 0.1.9 sent one, so there an end // below a received group only costs the early boundary. - test(`a SUBSCRIBE_END below a group already received ${version === Version.DRAFT_05 ? "finishes clean" : "aborts the track"}`, async () => { + test(`a SUBSCRIBE_FINISH below a group already received ${version === Version.DRAFT_05 ? "finishes clean" : "aborts the track"}`, async () => { const { subscriber, reader, respond, fin } = await subscribed(version); await respond({ start: new SubscribeStart([0n]) }); const group = groupStream(subscriber, 3n); group.finish(); await group.handled; - await respond({ end: new SubscribeEnd(2n) }); + await respond({ finish: new SubscribeFinish(2n) }); if (version === Version.DRAFT_05) { await fin(); @@ -415,7 +426,7 @@ test("lite-07 settles a skipped group with SUBSCRIBE_DROP", async () => { group.finish(); await group.handled; } - await respond({ end: new SubscribeEnd(3n) }); + await respond({ finish: new SubscribeFinish(3n) }); await respond({ drop: new SubscribeDrop({ start: 1n, end: 1n, error: 0 }) }); await fin(); @@ -428,7 +439,7 @@ test("lite-07 settles a skipped group with SUBSCRIBE_DROP", async () => { test("lite-07 settles a dropped range without grace", async () => { const { reader, respond, fin } = await subscribed(Version.DRAFT_07, Milli(60_000)); await respond({ start: new SubscribeStart([0n]) }); - await respond({ end: new SubscribeEnd(3n) }); + await respond({ finish: new SubscribeFinish(3n) }); await respond({ drop: new SubscribeDrop({ start: 0n, end: 2n, error: 0 }) }); await fin(); @@ -464,14 +475,14 @@ test("lite-07 FIN waits for the skipped and reset final range to settle", async await first.handled; const last = groupStream(subscriber, 2n); last.write("tail"); - await respond({ end: new SubscribeEnd(3n) }); + await respond({ finish: new SubscribeFinish(3n) }); await respond({ drop: new SubscribeDrop({ start: 1n, end: 1n, error: 0 }) }); await fin(); let finished = false; const ack = subscriberFin().then(() => { finished = true; }); - // Wait for SUBSCRIBE_END to be decoded without advancing any timers. + // Wait for SUBSCRIBE_FINISH to be decoded without advancing any timers. while (hooks.subscriberEnd(reader)?.group !== 3n) await Promise.resolve(); expect(finished).toBe(false); last.reset(); diff --git a/js/net/src/lite/version.ts b/js/net/src/lite/version.ts index fe92afe5538..88e849ba25f 100644 --- a/js/net/src/lite/version.ts +++ b/js/net/src/lite/version.ts @@ -397,7 +397,7 @@ export function hasHidden(version: Version): boolean { /** * Whether a served SUBSCRIBE completes only once the subscriber FINs or resets its half of the * Subscribe Stream. A subscriber FINs once its tail accounting settles, since a transport ACK - * does not say the application read the tail. SUBSCRIBE_END says which groups are owed, so that + * does not say the application read the tail. SUBSCRIBE_FINISH says which groups are owed, so that * is prompt from lite-05; before it, only the subscriber's grace settles the tail. */ export function waitsForSubscriberFin(version: Version): boolean { @@ -601,7 +601,7 @@ export function hasDropCode(version: Version): boolean { } /** - * Whether SUBSCRIBE_END carries the error the track ended with, so an errored end keeps its + * Whether SUBSCRIBE_FINISH carries the error the track ended with, so an errored end keeps its * history across a hop. Added in lite-07; earlier versions can only reset. */ export function hasEndError(version: Version): boolean { diff --git a/js/net/src/resume.ts b/js/net/src/resume.ts index 65a031977af..99959fb0022 100644 --- a/js/net/src/resume.ts +++ b/js/net/src/resume.ts @@ -73,7 +73,7 @@ import { grace } from "./ledger.ts"; import type { Lister, Tracks, TracksEvent } from "./listing.ts"; import * as track from "./track.ts"; import { max, min, UNBOUNDED } from "./util/sequence.ts"; -import { clamped, type Window } from "./window.ts"; +import { clamped, type Resolved, type Window } from "./window.ts"; import { type Broadcast as BroadcastWire, registerWire, wireOf } from "./wire.ts"; // Stream codes that report the route failing to deliver rather than judging what it carried: @@ -1207,6 +1207,13 @@ class Reader implements ResumeSource { return serving && hooks.servingStarts(serving.sub, from); } + // Each copy resolves the reader's ranges in turn, clamped to its floor and replacement. + resolved(): (Resolved | undefined)[] | undefined { + this.sync(); + const serving = this.#serving(); + return serving && hooks.servingResolved(serving.sub); + } + // The reader's ranges resolve on the copies, which hold the groups their anchors name. runs(start: bigint, end: bigint): Run[] | undefined { this.sync(); diff --git a/js/net/src/tail.test.ts b/js/net/src/tail.test.ts index b7dde35ea7a..40a81d606c1 100644 --- a/js/net/src/tail.test.ts +++ b/js/net/src/tail.test.ts @@ -57,7 +57,7 @@ async function readAll(reader: Ordered): Promise { } } -// moq-lite carries the end in SUBSCRIBE_END as soon as it is declared, so a subscriber +// moq-lite carries the end in SUBSCRIBE_FINISH as soon as it is declared, so a subscriber // learns it while the last group is still to come. test.each([Lite.ALPN_05, Lite.ALPN_06])( "%s: an end declared ahead of the live edge reaches the subscriber", diff --git a/js/net/src/track.test.ts b/js/net/src/track.test.ts index d5823fbec83..591856855c8 100644 --- a/js/net/src/track.test.ts +++ b/js/net/src/track.test.ts @@ -2806,7 +2806,7 @@ test("a closed track still delivers a group parked above the cap once the cap is }); // The final boundary counts datagrams: they share the group sequence namespace, and a -// Rust peer feeds SUBSCRIBE_END into finish_at, so a boundary that ignored a trailing +// Rust peer feeds SUBSCRIBE_FINISH into finish_at, so a boundary that ignored a trailing // datagram would finalize the track before it arrives. test("end reports the head after the highest produced sequence, datagrams included", async () => { const producer = new TrackProducer("test"); diff --git a/js/net/src/track.ts b/js/net/src/track.ts index 4b5cc73d146..2803e35e689 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -2905,6 +2905,12 @@ export class Subscriber { subscriber.#settle(); return subscriber.#window.starts(from); }; + hooks.servingResolved = (subscriber) => { + const resumed = subscriber.#resume?.resolved(); + if (resumed) return resumed; + subscriber.#settle(); + return subscriber.#window.resolved(); + }; hooks.tryNextGroup = (subscriber, floor) => { subscriber.#raiseNext(floor); return subscriber.#tryNextGroup(); diff --git a/js/net/src/window.test.ts b/js/net/src/window.test.ts index 790dc100a72..245c41d5937 100644 --- a/js/net/src/window.test.ts +++ b/js/net/src/window.test.ts @@ -60,7 +60,11 @@ test("a window reads only its ranges, counting relative anchors back from the la { start: 1n, end: 4n }, { start: 9n, end: 12n }, ]); - expect(window.resolved()).toEqual([{ start: 2n, end: 4n }, { start: 9n }, { start: 0n, end: 3n }]); + expect(window.resolved()).toEqual([ + { start: 2n, end: 4n, floored: true }, + { start: 9n, floored: true }, + { start: 0n, end: 3n, floored: true }, + ]); }); test("a relative anchor clamps to group 0 and waits for a first group", () => { @@ -100,7 +104,7 @@ test("a wire without ranges carries one span covering them all", () => { test("clamping raises starts to the floor and caps ends without widening", () => { const ranges: Range[] = [abs(1n, 9n), {}, { start: { relative: 2n }, end: { relative: 0n } }]; - expect(clamped(ranges, 4n, 6n, [undefined, undefined, { start: 8n, end: 11n }])).toEqual([ + expect(clamped(ranges, 4n, 6n, [undefined, undefined, { start: 8n, end: 11n, floored: true }])).toEqual([ abs(4n, 6n), abs(4n, 6n), abs(8n, 6n), @@ -179,13 +183,13 @@ test("a time start is the group containing it, and an end stops past it once a l let window = new Window([{ start: time(3500), end: time(4500) }], 6n, groups); expect(covered(window, 8)).toEqual([3n, 4n]); expect(window.settled).toBe(true); - expect(window.resolved()).toEqual([{ start: 3n, end: 5n }]); + expect(window.resolved()).toEqual([{ start: 3n, end: 5n, floored: true }]); // Group 6 may yet start at 5000 too: read on speculation, the end left open. window = new Window([{ start: time(3500), end: time(5000) }], 6n, groups); expect(window.settled).toBe(false); expect(covered(window, 9)).toEqual([3n, 4n, 5n, 6n]); - expect(window.resolved()).toEqual([{ start: 3n, end: undefined }]); + expect(window.resolved()).toEqual([{ start: 3n, end: undefined, floored: true }]); expect(window.end()).toBeUndefined(); // Before every start held: reach back to the oldest. @@ -219,7 +223,7 @@ test("a waiting time start keeps the relative end it was asked with", () => { groups.held.set(2n, started(2000)); expect(window.settle(10n, groups)).toBe(true); - expect(window.resolved()).toEqual([{ start: 2n, end: 10n }]); + expect(window.resolved()).toEqual([{ start: 2n, end: 10n, floored: true }]); } }); @@ -328,7 +332,7 @@ test("a datagram is judged by its own timestamp, one range at a time", () => { ]); groups.gones.push({ start: 1n, end: 2n }); const settled = new Window([{ start: time(0), end: time(5000) }], undefined, groups); - expect(settled.resolved()).toEqual([{ start: 0n, end: 2n }]); + expect(settled.resolved()).toEqual([{ start: 0n, end: 2n, floored: true }]); expect(settled.admitsDatagram(1n, Timestamp.fromMillis(4000))).toBe(true); expect(settled.admitsDatagram(1n, Timestamp.fromMillis(7000))).toBe(false); @@ -356,7 +360,7 @@ test("a time anchor needs moq-lite-07, is resolved by the copy, and clamps to it { start: time(5) }, ]); // Resolved anchors become their groups; an unresolved time end stays so the cap applies apart. - expect(clamped([{ start: time(1), end: time(5) }], 2n, 9n, [{ start: 3n, end: 7n }])).toEqual([ + expect(clamped([{ start: time(1), end: time(5) }], 2n, 9n, [{ start: 3n, end: 7n, floored: true }])).toEqual([ { start: { absolute: 3n }, end: { absolute: 7n } }, ]); expect(clamped([{ start: time(1), end: time(5) }], 2n, 9n, [])).toEqual([{ start: time(1), end: time(5) }]); diff --git a/js/net/src/window.ts b/js/net/src/window.ts index 1f7119ceab4..7e435bf0898 100644 --- a/js/net/src/window.ts +++ b/js/net/src/window.ts @@ -16,8 +16,11 @@ import { max, min, saturatingSub, UNBOUNDED } from "./util/sequence.ts"; /** A run of group sequences `[start, end)`; an open run ends at {@link UNBOUNDED}. */ export type Run = { start: bigint; end: bigint }; -/** One range resolved to groups: from `start` up to the exclusive `end`, undefined while open. */ -export type Resolved = { start: bigint; end?: bigint }; +/** + * One range resolved to groups: from `start` up to the exclusive `end`, undefined while open. + * `floored` says whether the start names a group, rather than reaching back to the oldest one. + */ +export type Resolved = { start: bigint; end?: bigint; floored: boolean }; /** Everything from the latest group onward. */ export const LIVE: Range = Object.freeze({ start: Object.freeze({ relative: 0n }) }); @@ -677,15 +680,21 @@ export class Window { } /** - * Each range resolved, in the order given: undefined for one that is empty or whose start - * still waits. An unsettled time end is reported open, never at its cap. + * Each range resolved, in the order given: undefined for one whose start still waits. One + * empty as given, or that only resolves empty, ends at or below its start. An unsettled time + * end is reported open, never at its cap. Mirrors the Rust `Window::resolved`. */ resolved(): (Resolved | undefined)[] { - const resolved: (Resolved | undefined)[] = this.ranges.map(() => undefined); + const resolved: (Resolved | undefined)[] = this.ranges.map((range) => { + if (!isEmpty(range)) return undefined; + const end = absolute(range.end) ?? 0n; + return { start: (range.start && absolute(range.start)) ?? end, end, floored: true }; + }); for (const span of this.#spans) { resolved[span.index] = { start: span.start, end: span.end !== "open" && "before" in span.end ? span.end.before : undefined, + floored: span.floored, }; } return resolved; diff --git a/quest/m1/group-error-cache.md b/quest/m1/group-error-cache.md index b51a73d9077..08b7284d10c 100644 --- a/quest/m1/group-error-cache.md +++ b/quest/m1/group-error-cache.md @@ -80,7 +80,6 @@ mirror, if a caller outside moq-net needs it; otherwise none. Wire: none. ## Required - [Drop set](/quest/m1/drop-set.md) - its per-cursor runs and their reasons are what this bounds; its PR, [moq-dev/moq#7670](https://github.com/moq-dev/moq/pull/7670), rewrites the same recording paths -- [Lite-07 ranges](/quest/m1/subscribe-ranges/lite.md) - its PR, [moq-dev/moq#7872](https://github.com/moq-dev/moq/pull/7872), makes the start skip and recovery non-permanent, the rule this generalizes ## Related diff --git a/quest/m1/lite07-finalize.md b/quest/m1/lite07-finalize.md index a57e3f2dfbe..961d41858c9 100644 --- a/quest/m1/lite07-finalize.md +++ b/quest/m1/lite07-finalize.md @@ -85,7 +85,7 @@ The `Time(pts)` range end rides in the subscribe-ranges children already Require Decided 2026-10-27 (maintainer): of [Subscribe ranges](/quest/m1/subscribe-ranges/README.md), only the children that change the lite-07 wire gate the cut: -[Lite-07 ranges](/quest/m1/subscribe-ranges/lite.md), +Lite-07 ranges ([moq-dev/moq#7872](https://github.com/moq-dev/moq/pull/7872)), JS ranges ([moq-dev/moq#7696](https://github.com/moq-dev/moq/pull/7696)), and [Lite-07 FETCH is single-group only](/quest/m1/subscribe-ranges/lite07-fetch.md). The fill path, fetch as a range, and the start bookkeeping change no wire. @@ -112,7 +112,6 @@ timestamp bound in [#6168](https://github.com/moq-dev/moq/pull/6168). - [Routes and announces](/quest/m1/cluster-routing/routes/README.md) - lite-07 carries the route layer: ROUTE per origin node and path-less ANNOUNCE, with the hop list gone - [Drop set](/quest/m1/drop-set.md) - lite-07's SUBSCRIBE_DROP carries `Max Age`, so a widening re-offers groups reported dropped -- [Lite-07 ranges](/quest/m1/subscribe-ranges/lite.md) - a relay fills a lite-07 upstream through the subscription - [Lite-07 FETCH is single-group only](/quest/m1/subscribe-ranges/lite07-fetch.md) - lite-07 FETCH reads one group or a slice of one, and SUBSCRIBE ranges of whole groups - [Lite-07 by start](/quest/m1/time-addressing/lite07.md) - lite-07 carries group starts, previous-start links, GROUP_END, and time bounds instead of sequence numbers - [@moq/net by start](/quest/m1/time-addressing/js.md) - JS speaks the same lite-07 wire diff --git a/quest/m1/subscribe-ranges/README.md b/quest/m1/subscribe-ranges/README.md index b36200ca701..b287c92ceeb 100644 --- a/quest/m1/subscribe-ranges/README.md +++ b/quest/m1/subscribe-ranges/README.md @@ -98,9 +98,8 @@ every sequence delivered or dropped. ## Required -- [Lite-07 ranges](/quest/m1/subscribe-ranges/lite.md) - the lite-07 wire, Rust publisher and subscriber, and the draft - [JS head fills and time lookups](/quest/m1/subscribe-ranges/js-fills.md) - JS readers fill a cached group's missing head before handing it out, and look a time start up through `Fill.time` -- [Lite-07 FETCH is single-group only](/quest/m1/subscribe-ranges/lite07-fetch.md) - lite-07 FETCH reads one group or a slice of one, and SUBSCRIBE ranges of whole groups, in Rust, JS, and the draft +- [Lite-07 FETCH is single-group only](/quest/m1/subscribe-ranges/lite07-fetch.md) - lite-07 FETCH reads one group or a slice of one, and SUBSCRIBE ranges of whole groups, in Rust, JS, and the draft, with JS multi-group fills moving onto the subscription - [Delete the old start bookkeeping](/quest/m1/subscribe-ranges/start-bookkeeping.md) - what each lite cursor is owed and dropped lives in the model, and `TrackRun` only translates it per version ## Related diff --git a/quest/m1/subscribe-ranges/lite.md b/quest/m1/subscribe-ranges/lite.md deleted file mode 100644 index 5ed3658d37a..00000000000 --- a/quest/m1/subscribe-ranges/lite.md +++ /dev/null @@ -1,238 +0,0 @@ -# [L] Lite-07 ranges - -## Goal - -Lite-07 ranges carry implicit Range IDs, so a range a SUBSCRIBE_UPDATE adds -learns where it started. A relay fills a lite-07 upstream by subscribing to the -missing ranges, and recovers a dropped group with a one-group SUBSCRIBE. Lite-07 -FETCH stays, for one group or a slice within one group -([Lite-07 FETCH is single-group only](/quest/m1/subscribe-ranges/lite07-fetch.md)). - -## Plan - -Decided 2026-10-25 (maintainer, on -[moq-dev/moq#6807](https://github.com/moq-dev/moq/pull/6807)): FETCH removal -moves out of this quest (to its own quest since 2026-10-27), since JS still requests and serves wire FETCH on lite-07 -(`track.Consumer.fetchGroup`), so a Rust-only removal would drop a current -cross-language capability. Since 2026-11-03 FETCH stays on lite-07 for one -group or a slice within one group, and this quest fills multi-group ranges -through the subscription. - -Decided 2026-11-01 (maintainer, on -[moq-dev/moq#7487](https://github.com/moq-dev/moq/pull/7487)): this quest -waited for Fetch as a range's miss judging (a miss with no feed frontier, and -holes below where a live feed started) and its cached backfill rule. Without -them a range fill is a functional regression from FETCH, which answers a miss -at once. Both landed in -[moq-dev/moq#7545](https://github.com/moq-dev/moq/pull/7545) on 2026-11-02. -Fetch as a range finished in -[moq-dev/moq#7625](https://github.com/moq-dev/moq/pull/7625), which kept -`track::Consumer::fetch_group` public as the single-group fetch and moved its -remaining lite-07 work here (maintainer, 2026-11-02): the explicit end, the -back-to-back hang, and the one-group recovery below. - -The explicit end landed in -[moq-dev/moq#7675](https://github.com/moq-dev/moq/pull/7675): SUBSCRIBE_END is -renamed SUBSCRIBE_FINISH, and on lite-07 neither it nor a covered bounded -subscription closes the Subscribe Stream. The publisher keeps reading updates -and FINs only in reply to the subscriber's FIN. Once no group can resolve a -held start (every group of a bounded subscription given up, or the track -finished), it sends SUBSCRIBE_OK and then the drops it held. The Rust copy -starts its tail at SUBSCRIBE_FINISH, waits for SUBSCRIBE_OK before judging it, -and FINs once it settles. That fixed back-to-back one-range reads -(`range_read::one_range_reads_back_to_back_each_complete`), and the Rust -copy's covered-FIN path (`SubStream::covered`, `ServeLoop::covered`) is gone. -Datagrams take no ranges, since they become their own track kind (maintainer, -2026-11-01; the datagram-tracks plan, -[moq-dev/moq#7575](https://github.com/moq-dev/moq/pull/7575)). The same PR -brought the JS lite-07 publisher and subscriber to the explicit end, so the two -implementations share the wire; JS still names it SUBSCRIBE_END in code. - -Decided 2026-11-03 (maintainer, on -[moq-dev/moq#7675](https://github.com/moq-dev/moq/pull/7675)), answering the -first attempt's open questions: - -1. **Land part 1:** ✅ land it now. Options: ✅ land now; keep a draft. -2. **Fill budget:** ✅ the live demand's budget. Groups that budget drops - count as officially dropped, so the one-group SUBSCRIBE recovery (no max - delay) brings them back, and the live cap never loosens. Options: ✅ live - demand's budget; loosest budget; per-range wire budget. -3. **Partial-group fills:** "we can keep FETCH then but for a single group (or - slice within a single group). SUBSCRIBE is for multiple groups and does not - support slicing within a group." A fill starting past frame 0, such as route - recovery's (`resume::Recover`), keeps its FETCH. See - [Lite-07 FETCH is single-group only](/quest/m1/subscribe-ranges/lite07-fetch.md). -4. **Datagram-only groups:** ✅ wait for datagram tracks - ([Datagram tracks](/quest/m1/datagram-tracks/README.md), planned in - [moq-dev/moq#7575](https://github.com/moq-dev/moq/pull/7575)); after them - ranges never carry datagrams. Options: ✅ wait for datagram tracks; - NOT_FETCHABLE drops now; keep FETCH for these. -5. **Range IDs:** ✅ implicit range IDs now, using - [moq-dev/moq#7207](https://github.com/moq-dev/moq/pull/7207)'s encoding, so - a range added by SUBSCRIBE_UPDATE learns where it started. Options: - absolute-only fills; ✅ implicit range IDs now. - -Range IDs are this quest's next step. They did not fit #7675 as a checkpoint -addition: the encoding changes SUBSCRIBE_UPDATE in the Rust and JS codecs, -publishers, and subscribers together (#7207 was 1,420 lines across both and -now conflicts with main), and it needs a start report #7207 never defined. -What remains is Range IDs, then the range fill and the one-group recovery. - -### Range IDs - -[moq-dev/moq#7207](https://github.com/moq-dev/moq/pull/7207) is the reference: -each Range implicitly takes the next Range ID on the stream, from 0, and -SUBSCRIBE_UPDATE edits the live Ranges rather than restating them, with -`Remove Count` Range IDs to retire and then the Ranges to add. A Range ID never -assigned or already retired is a PROTOCOL_VIOLATION, and the live Ranges must -stay non-empty and fit in one SUBSCRIBE. SUBSCRIBE_OK's Range Starts answer the -Ranges live when it is sent, in Range ID order. A Range an update adds after -SUBSCRIBE_OK has its start reported by SUBSCRIBE_STARTED, below. Rust and JS -change together, with draft text and codec, publisher, and subscriber tests. - -Decided 2026-11-03 (maintainer, on -[moq-dev/moq#7675](https://github.com/moq-dev/moq/pull/7675)): ✅ a new -**SUBSCRIBE_STARTED** message (type 0x3) carrying `Range ID` and -`Range Start`, sent when an added Range's start resolves. Options: ✅ new -SUBSCRIBE_STARTED; repeat SUBSCRIBE_OK. `Range Start` is encoded as in -SUBSCRIBE_OK (the Group plus one, 0 for none), and the subscriber owes the -added Range's groups from it as it does a SUBSCRIBE_OK Range Start. - -### Range fill through the subscription - -Lite-07 SUBSCRIBE and SUBSCRIBE_UPDATE carry the range list (`lite::Range`, -typed anchors), and the publisher serves it from the model. Older wires map at -the codec, and a merged request goes upstream as one span, from group 0 when -its starts mix (`wire_span`). A covered bounded subscription stays open, so a -fill joins the live subscription's ranges with a SUBSCRIBE_UPDATE. - -The first attempt -([moq-dev/moq#7487](https://github.com/moq-dev/moq/pull/7487), WIP commit -[b7f573d](https://github.com/moq-dev/moq/commit/b7f573d755ae53e6f2bdb94cc480ae47fd6ba254)) built this in -`rs/moq-net/src/lite/subscriber.rs` and was moved back out until the -requirement landed. Reuse its design: - -- A `RangeFills` set holds each fill and asks for its next owed run - (`Fill::next_run`), added as an absolute bounded range after the demand's - ranges on the upstream SUBSCRIBE or SUBSCRIBE_UPDATE. With no demand open, - a bounded SUBSCRIBE asks for the fills alone. A fill answers itself from the - cache as its groups arrive. -- A fill is judged only against the subscription that asked for it, not the - track's arrivals ledger, which still holds a group an earlier subscription - cut. A SUBSCRIBE_DROP over its groups, or a SUBSCRIBE_OK Range Start past - its range's start, refuses it, so a cut group is filled whole. The WIP's - covered-FIN refusal has no counterpart now: the publisher drops what it - gives up instead. -- A run stays on the wire while a fill owes part of it, then until each of its - streams still being read finishes, so narrowing never resets a group in - flight. -- Only the demand's ranges set the floor and the live state. A subscription - carrying only fills sets neither, and canceling it does not idle the copy. - If the demand leaves while fills are out, the subscription keeps the - demand's ranges until they are answered. -- A NotFound drop names the fill's groups dropped - (`Fill::drop_groups`), as a FETCH miss does; Old rejects them, and any - other error refuses only that request. - -Its integration tests (`range_fill.rs`: a miss subscribes and sends no FETCH, -a miss rides the live subscription then retires its range, a group the -publisher never had becomes a miss after the grace, a cut group is filled -whole) passed. These failed, and must pass on #7545's miss judging: - -- On-demand `requests()` tracks with no live feed, such as moq-archive's, never - ask a fill and park (`untimed::a_fetched_untimed_frame_arrives_untimed`, - `fetch_cancel::a_refetch_within_the_linger_stays_on_the_relay_lite07`, - `subscription_end_integrity::a_session_close_cancels_a_pending_fetch_from_either_side`). - #7545's miss with no feed frontier covers them. -- A missing or expired group fetched with no budget parks where FETCH answered - NotFound at once (`rejoin::{rejoined,taken_over}_quiet_catalog_survives_cache_expiry`). - #7545's holes below where a live feed started cover them. -- A relay re-asks upstream for groups it cached, since its covered memory - leaves with the downstream demand - (`fetch_fanout::sequential_viewers_share_the_relays_fetched_groups`, two - upstream SUBSCRIBEs where FETCH needed one). #7545's cached - backfill rule covers it: a relay leaves cached groups out of a bounded - upstream range. On lite-07 the test counts SUBSCRIBE streams, not FETCH. - -Open questions the attempt raised, now decided (2026-11-03, above), kept for -their detail: - -- **Datagram sequences.** Settled: datagrams are their own track kind, which - takes no ranges, and a stream track never holds one (maintainer, 2026-11-01; - NOT_FETCHABLE is gone). -- **Fills from partway through a group.** Route recovery (`resume::Recover`) - fills from frame N, which a whole-group range cannot say. Recommended: keep - FETCH on lite-07 for a fill whose first group starts past frame 0, as - `resume::Recover` does today (`group::Fetch::frame_start`). The alternative asks - for the whole group and drops the frames the reader already has. -- **One budget caps every range.** Merging the fills into the upstream - Subscriber Max Age takes the most tolerant budget, which loosens the live - feed's cap while a fill is out - (`route_change::resume_delivers_the_outage_tail_within_the_budget` delivers - a stale group 5). Recommended: settle the partial-group question first, - since the failing fill is a recovery fill, and rerun the test. The - alternatives fill under the demand's budget, refusing older groups as Old, - or add a per-range budget to the wire. Since the one-group recovery below - was decided (2026-11-02), filling under the demand's budget composes with - it: a group the live budget drops as OLD is officially dropped, so a - one-group SUBSCRIBE with no max delay recovers it, and the live cap never - loosens. Decided that way on 2026-11-03. -- **A range added after SUBSCRIBE_OK has no reported start.** SUBSCRIBE_OK's - Range Starts are positional and sent once, yet the subscriber judges a - bounded range complete from them, on a stream that stays open for later - updates. So a Relative or Time range a SUBSCRIBE_UPDATE adds never learns - where it started. Raised by the closed Range ID prototype - ([moq-dev/moq#7207](https://github.com/moq-dev/moq/pull/7207)), whose - recommendation was to decide here whether ranges get implicit IDs so a later - message can report a start. SUBSCRIBE_UPDATE keeps restating the whole list - either way: each range is a few bytes, and restating stays stateless. IDs - were adopted on 2026-11-03, with #7207's encoding, which edits rather than - restates. -- `auth::lite_07_narrowing_resets_a_fetch_in_flight` read `Stream(Cancel)` - instead of `Unauthorized`/`Unroutable` when the grant narrowed mid-group; - the refusal must carry the grant's error. - -The Rust copy sends a relative or time end as open, since nothing names a -resolved end and it resolves the end itself (`wire_ranges`), so only absolute -bounded ranges complete upstream. A hole on a live track counts as dropped -once the feed gives up on it, past the arrival grace (#7545's bounded wait, -`TrackState::miss_grace`). - -### Recovering a dropped group - -Decided 2026-11-02 (maintainer, on -[moq-dev/moq#7625](https://github.com/moq-dev/moq/pull/7625)): "lite-07 can -issue a one group SUBSCRIBE as a FETCH replacement (no max-delay) if an -existing SUBSCRIBE had it dropped", "and only when the group is officially -marked DROP so we don't receive two copies of it". A group a reader still -wants, once an existing subscription names it in SUBSCRIBE_DROP, is recovered -by a new SUBSCRIBE covering only that group with no max delay, never on a -guess, a timeout, or while the group may still arrive. This is lite-07's -replacement for a FETCH of a dropped group wider than one group's slice, and -with the fill budget decided 2026-11-03 it also recovers what the live -budget drops. Tests: a -group still in flight on the existing subscription sends no second SUBSCRIBE, -and a dropped group is recovered exactly once. - -### JavaScript names the explicit end - -Moved here from JS ranges on 2026-11-03 (maintainer, on -[moq-dev/moq#7696](https://github.com/moq-dev/moq/pull/7696)), which finished -that quest: the SUBSCRIBE_FINISH JS work belongs with -[moq-dev/moq#7675](https://github.com/moq-dev/moq/pull/7675), which brought the -JS lite-07 publisher and subscriber to the explicit end. What it left: rename -SUBSCRIBE_END to SUBSCRIBE_FINISH in `js/net`, as Rust and the draft did, and -delete the JS subscriber's covered-FIN path (`#parkCovered`, `#demandCovered`, -the `short` reopen), which no lite-07 publisher triggers any more. - -### Draft and checks - -Update `drafts/draft-lcurley-moq-lite.md` with each wire change here, -including that a subscriber fills missing groups through its ranges, and -recovers a dropped group with a one-group SUBSCRIBE. Run -`just drafts check` and `just test interop --all`, and the `moq-net-tests` -`restart::retired_fetch_lite07` and `moq-relay` suites. - -## Related - -- [Lite-07 FETCH is single-group only](/quest/m1/subscribe-ranges/lite07-fetch.md) - lite-07 FETCH reads one group or a slice of one, once this and JS ranges land diff --git a/quest/m1/subscribe-ranges/lite07-fetch.md b/quest/m1/subscribe-ranges/lite07-fetch.md index 3c45c1fc960..75738354eb1 100644 --- a/quest/m1/subscribe-ranges/lite07-fetch.md +++ b/quest/m1/subscribe-ranges/lite07-fetch.md @@ -1,4 +1,4 @@ -# [M] Lite-07 FETCH is single-group only +# [L] Lite-07 FETCH is single-group only ## Goal @@ -29,15 +29,26 @@ on lite-07 (`track.Consumer.fetchGroup` in JS). SUBSCRIBE may start at any frame. A read starting partway through a group goes through FETCH instead. - Rust and JS send FETCH on lite-07 only for one group or a slice of one - group; a fill of more than one group goes through - [Lite-07 ranges](/quest/m1/subscribe-ranges/lite.md)' subscription fill. + group; a fill of more than one group goes through the subscription, as the + Rust copy does since + [moq-dev/moq#7872](https://github.com/moq-dev/moq/pull/7872). +- Decided 2026-11-04 (maintainer, on + [moq-dev/moq#7872](https://github.com/moq-dev/moq/pull/7872)): the JS port + of those subscription fills is this quest's, not #7872's. JS copies started + filling only in [moq-dev/moq#7845](https://github.com/moq-dev/moq/pull/7845), + after #7872 was scoped as the Rust copy's fill, and they FETCH each missing + group one at a time, which breaks no wire rule. Port all of #7872's Rust fill + rules to the JS subscriber (`Subscriber.#runFills`), with tests: + - Subscription ranges: a lite-07 miss of more than one whole group joins the + subscription's ranges beside the demand's, told apart by Range ID, or rides + a fills-only SUBSCRIBE when no demand is open. + - Recovery: a group the subscription drops as stale, or delivered before and + since evicted, is asked once on a SUBSCRIBE of its own with no max delay. + - Skips: a range start past what a fill asked refuses only the fills owing + those groups now, while that range is live, and records nothing. - `drafts/draft-lcurley-moq-lite.md` states the split in its Fetch and Subscribe sections. Run `just drafts check`. - Run `just test interop --all`. Public API: none expected. Wire: lite-07 (unpublished) SUBSCRIBE anchors lose their frame. - -## Required - -- [Lite-07 ranges](/quest/m1/subscribe-ranges/lite.md) - Rust fills multi-group ranges through the subscription diff --git a/quest/m1/subscribe-ranges/start-bookkeeping.md b/quest/m1/subscribe-ranges/start-bookkeeping.md index fb9dbc8792c..599ecb645ef 100644 --- a/quest/m1/subscribe-ranges/start-bookkeeping.md +++ b/quest/m1/subscribe-ranges/start-bookkeeping.md @@ -38,7 +38,3 @@ generations of "start". Public API: none expected. Wire: lite-07 (unpublished) may send a START per update; published versions are unchanged. - -## Required - -- [Lite-07 ranges](/quest/m1/subscribe-ranges/lite.md) - the lite-07 range fill this bookkeeping is rebuilt around diff --git a/quest/m1/time-addressing/model.md b/quest/m1/time-addressing/model.md index cfec36cb232..0987f1e420c 100644 --- a/quest/m1/time-addressing/model.md +++ b/quest/m1/time-addressing/model.md @@ -54,7 +54,7 @@ a producer would need per-track memory of its last number. - **What it waits on** (decided 2026-10-27, maintainer; Fetch as a range finished in [moq-dev/moq#7625](https://github.com/moq-dev/moq/pull/7625)): only the subscribe-ranges children that put sequence ranges into the model and the - wire, [Lite-07 ranges](/quest/m1/subscribe-ranges/lite.md) and + wire, Lite-07 ranges ([moq-dev/moq#7872](https://github.com/moq-dev/moq/pull/7872)) and [Delete the old start bookkeeping](/quest/m1/subscribe-ranges/start-bookkeeping.md), not the whole epic. [Drop set](/quest/m1/drop-set.md) lands first too: its runs keyed by group sequence convert mechanically to starts. @@ -164,6 +164,5 @@ Wire: no format change; legacy and lite-07-wip group IDs become start ticks. ## Required -- [Lite-07 ranges](/quest/m1/subscribe-ranges/lite.md) - the range ends and drops this rework moves onto times - [Delete the old start bookkeeping](/quest/m1/subscribe-ranges/start-bookkeeping.md) - the lite cursor state lives in the model before the cut rekeys it - [Drop set](/quest/m1/drop-set.md) - its dropped and expired runs convert mechanically from sequences to starts diff --git a/rs/moq-net/src/fuzz.rs b/rs/moq-net/src/fuzz.rs index 07c43d80fa1..4734539e11e 100644 --- a/rs/moq-net/src/fuzz.rs +++ b/rs/moq-net/src/fuzz.rs @@ -838,10 +838,11 @@ impl Default for Messages { priority: 4, max_delay: Duration::from_millis(500), min_delay: None, - ranges: vec![lite::Range { + // Lite-06, the bench's version, restates the ranges. + ranges: lite::RangeEdit::Replace(vec![lite::Range { start: Some(lite::Anchor::Absolute(crate::track::Position::group(1_000))), end: Some(lite::Anchor::Absolute(crate::track::Position::group(2_001))), - }], + }]), }, lite_start: lite::SubscribeResponse::Start(lite::SubscribeStart { largest: None, diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 15aae6cf6d1..21ff0b295d8 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -1663,8 +1663,8 @@ trait Request: Sized { const HOLDS: bool; /// The message that opens the stream, owned so it outlives the bytes it came from. type Message: std::fmt::Debug; - /// What the requester may send after the request; [`NoUpdate`] for nothing. - type Update: for<'a> Decode<'a, Version> + std::fmt::Debug; + /// What the requester may send after the request, as it travels; [`NoUpdate`] for nothing. + type Update: Resolve; /// Decode the message that opens the stream. fn decode(r: &mut Decoder<'_>, version: Version) -> Result; @@ -1689,7 +1689,7 @@ trait Request: Sized { ) -> Result; /// Apply an update from the requester. - fn update(&mut self, update: Self::Update); + fn update(&mut self, update: Resolved); /// Write the reply: `Continue` after each unit of work, so the requester is checked /// between them even while work stays ready, and `Break` once all of it is buffered. @@ -1711,6 +1711,61 @@ impl<'a> Decode<'a, Version> for NoUpdate { } } +/// An update as it travels, resolved against what the stream carried before it. +trait Resolve: for<'a> Decode<'a, Version> + std::fmt::Debug { + /// What the stream carried so far, seeded by the request. + type State; + /// The update [`Request::update`] applies. + type Resolved: std::fmt::Debug; + + fn state(msg: &M, version: Version) -> Self::State; + fn resolve(self, state: &mut Self::State) -> Result; +} + +/// The update a request applies. +type Resolved = <::Update as Resolve<::Message>>::Resolved; + +impl Resolve for NoUpdate { + type State = (); + type Resolved = Self; + + fn state(_: &M, _: Version) {} + + fn resolve(self, _: &mut ()) -> Result { + match self {} + } +} + +impl Resolve> for lite::DatagramsUpdate { + type State = (); + type Resolved = Self; + + fn state(_: &lite::Datagrams<'static>, _: Version) {} + + fn resolve(self, _: &mut ()) -> Result { + Ok(self) + } +} + +/// Each lite-07 update edits the live ranges, which only the stream's history names. +impl Resolve> for lite::SubscribeUpdate { + type State = lite::LiveRanges; + type Resolved = LiveUpdate; + + fn state(msg: &lite::Subscribe<'static>, version: Version) -> lite::LiveRanges { + lite::LiveRanges::new(version, &msg.ranges) + } + + fn resolve(mut self, live: &mut lite::LiveRanges) -> Result { + let edit = std::mem::replace(&mut self.ranges, lite::RangeEdit::Replace(Vec::new())); + let ranges = live.apply(edit)?; + Ok(self.with_ranges(ranges)) + } +} + +/// A SUBSCRIBE_UPDATE resolved to every live range, by Range ID. +type LiveUpdate = lite::SubscribeUpdate>; + /// Serves one request stream: decode the request, resolve its broadcast, drive the /// kind's reply, then FIN. /// @@ -1729,8 +1784,11 @@ struct RequestServe { _subscription: Option, // Holds the session busy while a subscription or fetch is open. _working: Option, - // The latest update to arrive before the reply started. Each one replaces the last. - update: Option, + // What resolves each update, seeded once the request decodes. + history: Option<>::State>, + // The latest update to arrive before the reply started. Each one, resolved, replaces the + // last. + update: Option>, /// Ends the request, even mid-reply, once the session stops allowing its broadcast. gate: Option, // Log context, filled in after the decode. Relative to the origin's root, so a path @@ -1778,6 +1836,7 @@ impl RequestServe { owing: true, _subscription: None, _working, + history: None, update: None, gate: None, broadcast: Default::default(), @@ -1864,6 +1923,7 @@ impl RequestServe { RequestState::Decode => { let version = self.shared.version; let msg = ready!(stream.reader.poll_decode_with(waiter, |r| R::decode(r, version)))?; + self.history = Some(R::Update::state(&msg, version)); let (broadcast, track) = R::target(&msg); self.broadcast = broadcast.clone(); if R::SUBSCRIPTION { @@ -1974,9 +2034,13 @@ impl RequestServe { /// Read the requester's next message while we answer: an update, or `None` once it FINs /// its send direction. - fn poll_requester(&mut self, waiter: &kio::Waiter) -> Poll, Error>> { + fn poll_requester(&mut self, waiter: &kio::Waiter) -> Poll>, Error>> { let stream = self.stream.as_mut().expect("stream present"); - let res = ready!(stream.reader.poll_decode_maybe::(waiter)); + let res = ready!(stream.reader.poll_decode_maybe::(waiter)).and_then(|update| { + let Some(update) = update else { return Ok(None) }; + let history = self.history.as_mut().expect("seeded by the request"); + Ok(Some(update.resolve(history)?)) + }); if let Err(err) = &res { self.malformed = matches!(err, Error::Decode(_)); } @@ -1994,7 +2058,7 @@ impl RequestServe { } /// Each update reaches the reply, or waits for it to start. - fn apply(&mut self, update: R::Update) { + fn apply(&mut self, update: Resolved) { match &mut self.state { RequestState::Serve(request) => request.update(update), _ => self.update = Some(update), @@ -2196,7 +2260,7 @@ enum SubscribeServe { querying: track::Resolving, /// The newest group when the SUBSCRIBE arrived; see [`cursor_floor`]. latest: Option, - update: Option, + update: Option, legacy: Option>, }, /// Waiting for the model subscription to be confirmed. @@ -2207,7 +2271,7 @@ enum SubscribeServe { subscribing: track::Subscribing, /// The newest group when the SUBSCRIBE arrived; see [`cursor_floor`]. latest: Option, - update: Option, + update: Option, legacy: Option>, }, /// Streaming groups. Boxed: by far the largest state, and the enum is moved on every @@ -2287,7 +2351,7 @@ impl Request for SubscribeServe { Ok(Self::subscribe(shared, msg, bounds, &track, latest, None, legacy)) } - fn update(&mut self, update: lite::SubscribeUpdate) { + fn update(&mut self, update: LiveUpdate) { match self { Self::Query { update: pending, .. } | Self::Confirm { update: pending, .. } => *pending = Some(update), Self::Run(run) => run.update(update), @@ -2866,7 +2930,7 @@ impl SubscribeServe { bounds: Bounds, track: &track::Consumer, latest: Option, - update: Option, + update: Option, legacy: Option>, ) -> Self { let subscription = crate::track::Subscription { @@ -2890,11 +2954,7 @@ impl SubscribeServe { } /// Serve `msg` from the name's datagram track, at the priority of its latest `update`. - fn datagrams( - mut consumer: datagram::Consumer, - msg: &lite::Subscribe<'static>, - update: Option, - ) -> Self { + fn datagrams(mut consumer: datagram::Consumer, msg: &lite::Subscribe<'static>, update: Option) -> Self { let priority = update.map_or(msg.priority, |update| update.priority); consumer.set_priority(priority); Self::Datagrams { @@ -4609,6 +4669,13 @@ struct TrackRun { idle: Option>, /// Lite-05/06: the name's datagram track, which the subscription carries too. datagrams: Option, + /// The Range ID of each requested range, in the order the model holds them. + ids: Vec, + /// How many ranges the SUBSCRIBE carried, whose starts SUBSCRIBE_START reports. + initial: u64, + /// Lite-07: the ranges an update added whose start no SUBSCRIBE_STARTED reported yet, by + /// Range ID. + unstarted: Vec, } impl TrackRun { @@ -4630,8 +4697,12 @@ impl TrackRun { // Lite03 and Lite04 declare no end, so neither side could settle on a drop. let served = emit_range.then(RangeSet::new); let bounded = bounds.ends(ctx.version); + let initial = bounds.ranges.len() as u64; Self { + ids: (0..initial).collect(), + initial, + unstarted: Vec::new(), bounded, frames: bounds.frames, named: groups.start(), @@ -4667,7 +4738,7 @@ impl TrackRun { /// unsets it), then served in order. Only a peer FIN ends the subscription. This is /// what lets relays pause an upstream subscription across consumer churn without /// tearing it down. Lite-07 keeps every subscription open until the subscriber FINs. - fn update(&mut self, upd: lite::SubscribeUpdate) { + fn update(&mut self, upd: LiveUpdate) { if let Some(datagrams) = &mut self.datagrams { datagrams.steer(upd.priority); } @@ -4701,7 +4772,9 @@ impl TrackRun { // Read first: `update` replaces these preferences. let requested = self.named; let floored = requested.is_some_and(|group| group > 0); - let bounds = match Bounds::new(&upd.ranges, self.ctx.stamp.map(|stamp| stamp.timescale)) { + let (ids, ranges): (Vec, Vec) = upd.ranges.iter().copied().unzip(); + let ranges = self.pin(&ids, ranges); + let bounds = match Bounds::new(&ranges, self.ctx.stamp.map(|stamp| stamp.timescale)) { Ok(bounds) => bounds, Err(err) => { self.refused = Some(err); @@ -4765,6 +4838,98 @@ impl TrackRun { } self.bounded = bounds.ends(self.ctx.version); self.frames = bounds.frames; + if self.ctx.version.has_ranges() { + // A range retired before its start resolved is owed no SUBSCRIBE_STARTED. + // Both list Range IDs in ascending order, since each one assigned is the next. + self.unstarted.retain(|id| ids.binary_search(id).is_ok()); + let added = ids + .iter() + .filter(|id| **id >= self.initial && self.ids.binary_search(id).is_err()); + self.unstarted.extend(added); + } + self.ids = ids; + } + + /// Where each live Range ID sits among the model's ranges. + fn positions(&self) -> HashMap { + self.ids.iter().enumerate().map(|(index, id)| (*id, index)).collect() + } + + /// `ranges`, numbered by `ids`, with each range this subscription already held resolved + /// as it was: a range resolves once, so an update that leaves it live never counts its + /// relative anchors back from a newer latest group, nor looks a time start up again in + /// newer groups. A start reaching back to the oldest group held stays as it was, since an + /// absolute start would turn it into a floor the subscriber never named. Lite-07 only: an + /// older update replaces every range. + fn pin(&mut self, ids: &[u64], mut ranges: Vec) -> Vec { + if !self.ctx.version.has_ranges() { + return ranges; + } + let resolved = self.track.resolved(); + let positions = self.positions(); + for (id, range) in ids.iter().zip(&mut ranges) { + let Some(resolved) = positions + .get(id) + .and_then(|index| resolved.get(*index).copied().flatten()) + else { + continue; + }; + if resolved.floored && matches!(range.start, Some(lite::Anchor::Relative(_) | lite::Anchor::Time(_))) { + range.start = Some(lite::Anchor::Absolute(track::Position::group(resolved.start))); + } + // An end resolves once it settles; until then it is reported open. + if let (Some(lite::Anchor::Relative(_) | lite::Anchor::Time(_)), Some(end)) = (range.end, resolved.end) { + range.end = Some(lite::Anchor::Absolute(track::Position::group(end))); + } + } + ranges + } + + /// Report where each range an update added starts, once it resolves, or with no start + /// once nothing can resolve it (`finished`). Returns whether any SUBSCRIBE_STARTED was + /// buffered. Waits for SUBSCRIBE_START, which goes first. + fn report_started( + &mut self, + finished: bool, + writer: &mut Writer, + ) -> Result { + let Some(from) = self.start else { + return Ok(false); + }; + if self.unstarted.is_empty() { + return Ok(false); + } + let resolved = self.track.resolved(); + let positions = self.positions(); + let mut waiting = Vec::new(); + let mut reported = false; + for id in std::mem::take(&mut self.unstarted) { + let index = *positions.get(&id).expect("unstarted ranges are live"); + let start = match resolved.get(index).copied().flatten() { + Some(resolved) => { + let start = resolved.start.max(from); + resolved.end.is_none_or(|end| end > start).then_some(start) + } + None if finished => None, + None => { + waiting.push(id); + continue; + } + }; + writer.buffer(&lite::SubscribeResponse::Started(lite::SubscribeStarted { id, start }))?; + reported = true; + } + self.unstarted = waiting; + Ok(reported) + } + + /// Apply `update`, replacing every range with fresh Range IDs, as an update before + /// lite-07 does. + #[cfg(test)] + fn replace(&mut self, update: lite::SubscribeUpdate>) { + let next = self.ids.iter().max().map_or(0, |last| last + 1).max(self.initial); + let ranges = (next..).zip(update.ranges.iter().copied()).collect(); + self.update(update.with_ranges(ranges)); } /// Apply [`Self::floor`] and [`Self::cap`] to the read cursor. @@ -4857,7 +5022,7 @@ impl TrackRun { writer: &mut Writer, waiter: &kio::Waiter, ) -> Poll, Error>> { - if self.poll_forward_drop(writer, waiter)? { + if self.poll_forward_drop(writer, waiter)? || self.report_started(false, writer)? { return Poll::Ready(Ok(ControlFlow::Continue(()))); } @@ -4962,7 +5127,9 @@ impl TrackRun { self.resolve_held(final_sequence(fin), writer)?; answered = true; } - let dropped = self.drop_unserved(writer)? || answered; + // Nothing arrives to resolve a range still waiting on the live feed. + let started = self.report_started(true, writer)?; + let dropped = self.drop_unserved(writer)? || answered || started; if !self.ctx.version.keeps_subscribe_open() { return Poll::Ready(Ok(ControlFlow::Break(()))); } @@ -5093,10 +5260,16 @@ impl TrackRun { /// Where each requested range starts once delivery starts at `start`. Before lite-07 /// there is one range, which starts there. fn starts(&mut self, start: u64) -> Vec> { - match self.ctx.version.has_ranges() { - true => self.track.starts(start), - false => vec![Some(start)], + if !self.ctx.version.has_ranges() { + return vec![Some(start)]; } + // SUBSCRIBE_START answers the SUBSCRIBE's ranges, by Range ID: none for one an update + // retired already, and SUBSCRIBE_STARTED answers each one an update added. + let starts = self.track.starts(start); + let positions = self.positions(); + (0..self.initial) + .map(|id| starts.get(*positions.get(&id)?).copied().flatten()) + .collect() } /// Send SUBSCRIBE_START for the first servable group, then serve it. @@ -6649,16 +6822,16 @@ mod serve_group_test { ranges: vec![lite::Range::span(None, 0, None, None)], order: Order::Descending, }; - run.update(update(Some(0))); + run.replace(update(Some(0))); assert_eq!(track.demand().wanted(), Some(0), "stale request is not a join"); - run.update(update(Some(1))); + run.replace(update(Some(1))); assert_eq!(track.demand().wanted(), Some(1)); let mut group = track.append_group().unwrap(); group.write_frame(None, &b"key"[..]).unwrap(); group.finish().unwrap(); - run.update(update(Some(1))); + run.replace(update(Some(1))); assert_eq!(track.demand().wanted(), Some(1), "answered request stays answered"); - run.update(update(Some(2))); + run.replace(update(Some(2))); assert_eq!(track.demand().wanted(), Some(1), "new hop judges its own ratio"); } @@ -6777,7 +6950,7 @@ mod serve_group_test { assert_eq!(log.orders(), [(0, 1), (1, 3)], "newer groups first"); assert!(log.priorities().is_empty(), "the session queue ranks no stream"); - run.update(lite::SubscribeUpdate { + run.replace(lite::SubscribeUpdate { wanted_group: None, order: Order::Ascending, priority: 7, @@ -6898,6 +7071,9 @@ mod serve_group_test { ]), ); let (mut run, mut writer, log) = lite07_run(SinkSession::new(Log::default()), subscriber); + // The SUBSCRIBE carried the subscriber's four ranges. + run.initial = 4; + run.ids = (0..4).collect(); let mut run = std::pin::pin!(kio::wait(move |waiter| run.poll(&mut writer, waiter))); assert!(futures::poll!(run.as_mut()).is_pending()); @@ -6911,6 +7087,188 @@ mod serve_group_test { } } + /// Every response a run loop's subscribe stream carried, once flushed. + async fn responses( + writer: &mut Writer, + log: &Log, + ) -> Vec { + kio::wait(|waiter| writer.poll_flush(waiter)).await.unwrap(); + let writes = log.writes.lock().unwrap().clone(); + let mut buf = writes.as_slice(); + let mut responses = Vec::new(); + while !buf.is_empty() { + let (msg, size) = lite::SubscribeResponse::decode_slice(buf, Version::Lite07).unwrap(); + buf = &buf[size..]; + responses.push(msg); + } + responses + } + + /// A lite-07 update that keeps every live range and adds `added`, each taking the next + /// Range ID after `next`. + fn lite07_add(live: &[(u64, lite::Range)], next: u64, added: &[lite::Range]) -> LiveUpdate { + let mut ranges = live.to_vec(); + ranges.extend((next..).zip(added.iter().copied())); + lite::SubscribeUpdate { + wanted_group: None, + priority: 0, + max_delay: Duration::from_secs(3600), + min_delay: None, + ranges, + order: Order::Descending, + } + } + + /// A range an update adds learns where it starts from SUBSCRIBE_STARTED, after + /// SUBSCRIBE_START, and keeps that start however many later updates leave it live. + #[kio::test] + async fn lite07_an_added_range_reports_its_start_and_keeps_it() { + let mut track = track::Producer::new( + Arc::new(broadcast::Info::default()), + "test", + crate::track::Info::timed(), + ); + for sequence in 0..5 { + write_group(&mut track, sequence, sequence * 1000); + } + // Bounded below the groups the added range reaches, so its start is its own. + let first = lite::Range::span(Some(0), 0, Some(0), None); + let subscriber = track.subscribe( + track::Subscription::default() + .with_delay(track::Delay::default().with_max(Duration::from_secs(3600))) + .with_range(0..1), + ); + let (mut run, mut writer, log) = lite07_run(SinkSession::new(Log::default()), subscriber); + while run.start.is_none() { + assert!(run.poll_step(&mut writer, &kio::Waiter::noop()).is_ready()); + } + + // Two groups back from the latest, group 2. + let back = lite::Range { + start: Some(lite::Anchor::Relative(2)), + end: None, + }; + let live = [(0, first)]; + run.update(lite07_add(&live, 1, &[back])); + while run.poll_step(&mut writer, &kio::Waiter::noop()).is_ready() {} + let started: Vec<_> = responses(&mut writer, &log) + .await + .into_iter() + .filter_map(|msg| match msg { + lite::SubscribeResponse::Started(started) => Some(started), + _ => None, + }) + .collect(); + assert_eq!(started, [lite::SubscribeStarted { id: 1, start: Some(2) }]); + + // Newer groups arrive, and another update leaves the range live: it still starts at 2. + for sequence in 5..8 { + write_group(&mut track, sequence, sequence * 1000); + } + let live = [(0, first), (1, back)]; + run.update(lite07_add(&live, 2, &[lite::Range::span(Some(9), 0, None, None)])); + let resolved = run.track.resolved()[1].map(|r| (r.start, r.end)); + assert_eq!(resolved, Some((2, None)), "the update kept where it started"); + } + + /// Where an added time start `time` resolves among `groups` (sequence and start, in + /// milliseconds), before and after `newer` arrives and another update leaves it live, with + /// whether it names that group. The run serves group `first` first. + async fn time_start_across_an_update( + groups: &[(u64, u64)], + first: u64, + time: u64, + newer: (u64, u64), + ) -> [(u64, bool); 2] { + let mut track = track::Producer::new( + Arc::new(broadcast::Info::default()), + "test", + crate::track::Info::timed(), + ); + for (sequence, millis) in groups { + write_group(&mut track, *sequence, *millis); + } + let served = lite::Range::span(Some(first), 0, Some(first), None); + let subscriber = track.subscribe( + track::Subscription::default() + .with_delay(track::Delay::default().with_max(Duration::from_secs(3600))) + .with_range(first..first + 1), + ); + let (mut run, mut writer, _log) = lite07_run(SinkSession::new(Log::default()), subscriber); + while run.start.is_none() { + assert!(run.poll_step(&mut writer, &kio::Waiter::noop()).is_ready()); + } + let time = lite::Range { + start: Some(lite::Anchor::Time(time)), + end: None, + }; + run.update(lite07_add(&[(0, served)], 1, &[time])); + while run.poll_step(&mut writer, &kio::Waiter::noop()).is_ready() {} + let before = run.track.resolved()[1].expect("resolved"); + + write_group(&mut track, newer.0, newer.1); + let live = [(0, served), (1, time)]; + run.update(lite07_add(&live, 2, &[lite::Range::span(Some(99), 0, None, None)])); + let after = run.track.resolved()[1].expect("resolved"); + [(before.start, before.floored), (after.start, after.floored)] + } + + /// A time start resolves once too: a newer group starting before its time doesn't move it + /// once an update leaves the range live, group 0 included. One older than every group held + /// still reaches back to the oldest, rather than becoming a floor at group 0. + #[kio::test] + async fn lite07_an_added_time_start_keeps_its_start() { + let five: Vec<_> = (0..5).map(|sequence| (sequence, sequence * 1000)).collect(); + // 4.5 s falls in group 4, the newest starting at or before it, until group 5 starts at + // 4.2 s. + assert_eq!( + time_start_across_an_update(&five, 0, 4500, (5, 4200)).await, + [(4, true), (4, true)] + ); + // 1 s falls in group 0 until group 1 starts at 0.5 s. + assert_eq!( + time_start_across_an_update(&[(0, 0)], 0, 1000, (1, 500)).await, + [(0, true), (0, true)] + ); + // 0.1 s is older than group 5, the oldest held. + assert_eq!( + time_start_across_an_update(&[(5, 5000)], 5, 100, (6, 6000)).await, + [(0, false), (0, false)] + ); + } + + /// A range retired before SUBSCRIBE_STARTED reported it gets none, and one still waiting + /// once the track ends is reported with no start. + #[kio::test] + async fn lite07_a_retired_range_reports_no_start() { + let mut track = track::Producer::new( + Arc::new(broadcast::Info::default()), + "test", + crate::track::Info::timed(), + ); + write_group(&mut track, 0, 0); + let first = lite::Range::span(Some(0), 0, None, None); + let subscriber = track.subscribe( + track::Subscription::default() + .with_delay(track::Delay::default().with_max(Duration::from_secs(3600))) + .with_range(0..), + ); + let (mut run, mut writer, log) = lite07_run(SinkSession::new(Log::default()), subscriber); + let added = lite::Range::span(Some(4), 0, None, None); + // Added and retired before the run reported it. + run.update(lite07_add(&[(0, first)], 1, &[added])); + run.update(lite07_add(&[(0, first)], 2, &[])); + while run.poll_step(&mut writer, &kio::Waiter::noop()).is_ready() {} + assert!(run.start.is_some()); + assert!( + !responses(&mut writer, &log) + .await + .iter() + .any(|msg| matches!(msg, lite::SubscribeResponse::Started(_))), + "a retired range was reported" + ); + } + /// A skipped sequence is accounted for by DROP rather than a stream count. #[kio::test] async fn lite07_end_drops_skipped_sequences() { @@ -7014,7 +7372,7 @@ mod serve_group_test { assert_eq!(run.ctx.outcomes.opened.load(Ordering::Relaxed), 3, "{version:?}"); assert_eq!(drops(&mut writer, &log, version).await, [(1, 2)], "{version:?}"); - run.update(lite::SubscribeUpdate { + run.replace(lite::SubscribeUpdate { wanted_group: None, priority: 0, max_delay: Duration::from_secs(10), @@ -7169,7 +7527,7 @@ mod serve_group_test { while run.poll_step(&mut writer, &kio::Waiter::noop()).is_ready() {} assert_eq!(drops(&mut writer, &log, version).await, []); - run.update(lite::SubscribeUpdate { + run.replace(lite::SubscribeUpdate { wanted_group: None, order: crate::track::Order::Descending, priority: 0, @@ -7576,7 +7934,7 @@ mod serve_group_test { } assert_eq!(run.start, Some(5)); - run.update(lite::SubscribeUpdate { + run.replace(lite::SubscribeUpdate { wanted_group: None, order: crate::track::Order::Descending, priority: 0, @@ -7617,7 +7975,7 @@ mod serve_group_test { } kio::clock::sim::advance(Duration::from_secs(2)).await; - run.update(lite::SubscribeUpdate { + run.replace(lite::SubscribeUpdate { wanted_group: None, order: crate::track::Order::Descending, priority: 0, @@ -7671,7 +8029,7 @@ mod serve_group_test { min_delay: None, ranges: vec![lite::Range::span(Some(start_group), 0, None, None)], }; - run.update(update(5)); + run.replace(update(5)); { let mut run = std::pin::pin!(kio::wait(|waiter| run.poll(&mut writer, waiter))); write_group(&mut track, 5, 0); @@ -7679,7 +8037,7 @@ mod serve_group_test { } kio::clock::sim::advance(Duration::from_millis(900)).await; - run.update(update(2)); + run.replace(update(2)); kio::clock::sim::advance(Duration::from_millis(200)).await; track.finish_at(track::Position::group(6), None).unwrap(); drop(track); @@ -7717,11 +8075,11 @@ mod serve_group_test { min_delay: None, ranges: vec![lite::Range::span(Some(start_group), 0, None, None)], }; - run.update(update(5)); + run.replace(update(5)); while run.poll_step(&mut writer, &kio::Waiter::noop()).is_ready() {} assert_eq!(drops(&mut writer, &log, version).await, []); - run.update(update(0)); + run.replace(update(0)); for sequence in 1..5 { write_group(&mut track, sequence, 0); } @@ -7755,7 +8113,7 @@ mod serve_group_test { write_group(&mut track, 7, 7); assert_eq!(settle(&mut run), 1); - run.update(lite::SubscribeUpdate { + run.replace(lite::SubscribeUpdate { wanted_group: None, order: crate::track::Order::Descending, priority: 0, @@ -7785,7 +8143,7 @@ mod serve_group_test { .with_delay(track::Delay::default().with_max(Duration::from_secs(60))), ); let (mut run, mut writer, _log) = lite_run(Version::Lite05, SinkSession::new(Log::default()), subscriber); - run.update(lite::SubscribeUpdate { + run.replace(lite::SubscribeUpdate { wanted_group: None, order: crate::track::Order::Descending, priority: 0, @@ -7922,7 +8280,7 @@ mod serve_group_test { relay.settle(); assert_eq!(relay.opened(), 0, "the resume is past the only group"); - relay.run.update(lite::SubscribeUpdate { + relay.run.replace(lite::SubscribeUpdate { wanted_group: None, order: crate::track::Order::Descending, priority: 0, @@ -7952,7 +8310,7 @@ mod serve_group_test { relay.settle(); assert_eq!(relay.opened(), 2, "groups 1 and 2 are at or above the floor"); - relay.run.update(lite::SubscribeUpdate { + relay.run.replace(lite::SubscribeUpdate { wanted_group: None, order: crate::track::Order::Descending, priority: 0, @@ -7987,7 +8345,7 @@ mod serve_group_test { min_delay: None, ranges: vec![lite::Range::span(Some(7), 0, None, None)], }; - relay.run.update(update); + relay.run.replace(update); relay.settle(); track.start_at(0).unwrap(); @@ -9151,13 +9509,17 @@ mod tests { let mut serve = RequestServe::::new(publisher.shared.clone(), stream); assert!(!drive(&mut serve).await); + // The ranges stay as the SUBSCRIBE asked, so each update edits none of them. let update = |wanted_group, priority| lite::SubscribeUpdate { wanted_group, order: crate::track::Order::Descending, priority, max_delay: Duration::ZERO, min_delay: None, - ranges: vec![lite::Range::span(Some(1), 0, None, None)], + ranges: lite::RangeEdit::Edit { + remove: Vec::new(), + add: Vec::new(), + }, }; session.push(&update(Some(2), 3).encode_bytes(version).unwrap()); session.push(&update(Some(2), 7).encode_bytes(version).unwrap()); @@ -9216,7 +9578,10 @@ mod tests { priority: 0, max_delay: Duration::from_secs(3600), min_delay: None, - ranges: vec![lite::Range::span(None, 0, None, None)], + ranges: lite::RangeEdit::Edit { + remove: Vec::new(), + add: Vec::new(), + }, } } diff --git a/rs/moq-net/src/lite/subscribe.rs b/rs/moq-net/src/lite/subscribe.rs index 2872f075f44..7967fe06ef2 100644 --- a/rs/moq-net/src/lite/subscribe.rs +++ b/rs/moq-net/src/lite/subscribe.rs @@ -2,7 +2,7 @@ use std::borrow::Cow; use crate::{ Path, StreamError, - coding::{Decode, DecodeError, Decoder, Encode, EncodeError, Encoder, StreamCodes}, + coding::{Decode, DecodeError, Decoder, Encode, EncodeError, Encoder, Form, StreamCodes, varint}, }; use super::{Message, Version}; @@ -50,7 +50,7 @@ impl Subscribe<'_> { } /// One end of a [`Range`], as the wire carries it. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub enum Anchor { /// A position: a start delivers from it, and an end stops just before it. Absolute(crate::track::Position), @@ -69,7 +69,7 @@ pub enum Anchor { /// `Group Start` of 0 with a `Frame Start` of 0, the same absence of a floor there. No start /// and a [`Anchor::Relative`] start of 0 both encode as an absent `Group Start`, which a /// pre-06 publisher reads as the latest group and a lite-06 one as no floor. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub struct Range { /// Where delivery starts, or `None` for the oldest group the max age allows. pub start: Option, @@ -208,19 +208,11 @@ fn encode_min_delay( /// `Group End`, and their frame bounds; see [`Range`]. fn decode_ranges(r: &mut Decoder<'_>, version: Version) -> Result, DecodeError> { if version.has_ranges() { - let count = r.varint()?; + let ranges = decode_range_list(r)?; // A subscription that asks for nothing has no use: refuse it rather than park it. - if count == 0 { + if ranges.is_empty() { return Err(DecodeError::InvalidSubscribeLocation); } - // Not preallocated: each range costs at least two bytes of the bounded message, so a - // count the bytes can't back runs out before it costs anything. - let mut ranges = Vec::new(); - for _ in 0..count { - let start = decode_anchor(r)?; - let end = decode_anchor(r)?; - ranges.push(Range { start, end }); - } return Ok(ranges); } @@ -257,12 +249,7 @@ fn encode_ranges(w: &mut Encoder<'_>, version: Version, ranges: &[Range]) -> Res if ranges.is_empty() { return Err(EncodeError::InvalidState); } - w.varint(ranges.len() as u64)?; - for range in ranges { - encode_anchor(w, range.start)?; - encode_anchor(w, range.end)?; - } - return Ok(()); + return encode_range_list(w, ranges); } let [range] = ranges else { @@ -288,6 +275,42 @@ fn encode_ranges(w: &mut Encoder<'_>, version: Version, ranges: &[Range]) -> Res encode_frame_bounds(w, version, start_group, start_frame, end_group, end_frame) } +/// Decode a lite-07 `Range Count` and its ranges, which may be none. +fn decode_range_list(r: &mut Decoder<'_>) -> Result, DecodeError> { + let count = r.varint()?; + // Not preallocated: each range costs at least two bytes of the bounded message, so a + // count the bytes can't back runs out before it costs anything. + let mut ranges = Vec::new(); + for _ in 0..count { + let start = decode_anchor(r)?; + let end = decode_anchor(r)?; + ranges.push(Range { start, end }); + } + Ok(ranges) +} + +/// Encode the inverse of [`decode_range_list`]. +fn encode_range_list(w: &mut Encoder<'_>, ranges: &[Range]) -> Result<(), EncodeError> { + w.varint(ranges.len() as u64)?; + for range in ranges { + encode_anchor(w, range.start)?; + encode_anchor(w, range.end)?; + } + Ok(()) +} + +/// The bytes a lite-07 range takes on the wire, or `None` past the varint's reach. +fn range_size(range: &Range, form: Form) -> Option { + let size = |value| varint::size(value, form).ok(); + let anchor = |anchor: Option| match anchor { + None => size(0), + Some(Anchor::Absolute(position)) => Some(size(1)? + size(position.group)? + size(position.frame)?), + Some(Anchor::Relative(back)) => Some(size(2)? + size(back)?), + Some(Anchor::Time(pts)) => Some(size(3)? + size(pts)?), + }; + Some(anchor(range.start)? + anchor(range.end)?) +} + /// Decode one lite-07 range end: its type, then the fields that type carries. Type 0 is no /// anchor, the oldest group as a start and the live edge as an end. fn decode_anchor(r: &mut Decoder<'_>) -> Result, DecodeError> { @@ -639,21 +662,38 @@ impl<'a> Message<'a> for SubscribeFinish { /// Sent by the subscriber to update subscription parameters. /// +/// `R` is how the ranges travel: a [`RangeEdit`] on the wire, and every live range with its +/// Range ID once [`LiveRanges`] resolves the edit, which is what a publisher applies. +/// /// Lite03+ only. #[derive(Clone, Debug)] -pub struct SubscribeUpdate { +pub struct SubscribeUpdate { /// A join requesting this group or later, judged against the local refresh ratio. pub wanted_group: Option, pub priority: i8, pub max_delay: std::time::Duration, /// See [`Subscribe::min_delay`]. pub min_delay: Option, - /// The replacement for every range; see [`Subscribe::ranges`]. - pub ranges: Vec, + /// The change to the ranges; see [`Subscribe::ranges`]. + pub ranges: R, /// See [`Subscribe::order`]. pub order: Order, } +impl SubscribeUpdate { + /// The same update with its ranges as `ranges`. + pub(crate) fn with_ranges(self, ranges: T) -> SubscribeUpdate { + SubscribeUpdate { + wanted_group: self.wanted_group, + priority: self.priority, + max_delay: self.max_delay, + min_delay: self.min_delay, + ranges, + order: self.order, + } + } +} + impl<'a> Message<'a> for SubscribeUpdate { const CONTROL: Option = Some(crate::stats::Message::Subscribe); fn decode_msg(r: &mut Decoder<'a>, version: Version) -> Result { @@ -668,7 +708,20 @@ impl<'a> Message<'a> for SubscribeUpdate { let order = decode_order(r, version)?; let max_delay = std::time::Duration::from_millis(r.varint()?); let min_delay = decode_min_delay(r, version)?; - let ranges = decode_ranges(r, version)?; + let ranges = match version.has_ranges() { + true => { + // Not preallocated, like the ranges: each id costs a byte of the bounded message. + let mut remove = Vec::new(); + for _ in 0..r.varint()? { + remove.push(r.varint()?); + } + RangeEdit::Edit { + remove, + add: decode_range_list(r)?, + } + } + false => RangeEdit::Replace(decode_ranges(r, version)?), + }; let wanted_group = match version.has_refresh() { true => r.varint()?.checked_sub(1), @@ -697,7 +750,17 @@ impl<'a> Message<'a> for SubscribeUpdate { encode_order(w, version, self.order)?; w.varint(u64::try_from(self.max_delay.as_millis()).map_err(|_| EncodeError::BoundsExceeded)?)?; encode_min_delay(w, version, self.min_delay)?; - encode_ranges(w, version, &self.ranges)?; + match (&self.ranges, version.has_ranges()) { + (RangeEdit::Edit { remove, add }, true) => { + w.varint(remove.len() as u64)?; + for id in remove { + w.varint(*id)?; + } + encode_range_list(w, add)?; + } + (RangeEdit::Replace(ranges), false) => encode_ranges(w, version, ranges)?, + _ => return Err(EncodeError::Version), + } if version.has_refresh() { w.varint_opt(self.wanted_group)?; @@ -706,6 +769,235 @@ impl<'a> Message<'a> for SubscribeUpdate { } } +/// The ranges a SUBSCRIBE_UPDATE carries. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum RangeEdit { + /// Before lite-07: the whole list, replacing the live one. + Replace(Vec), + /// Lite-07: retire the ranges with these Range IDs, then add these, each taking the next + /// Range ID. + Edit { remove: Vec, add: Vec }, +} + +/// A subscription's live ranges by Range ID, which both ends of a Subscribe Stream keep: the +/// subscriber to write each update as an edit, the publisher to resolve one. +/// +/// The SUBSCRIBE's ranges take Range IDs from 0 in order, and each range an update adds +/// takes the next. Before lite-07 an update replaces the list, numbered afresh. +#[derive(Clone, Debug)] +pub(crate) struct LiveRanges { + version: Version, + next: u64, + live: std::collections::BTreeMap, + /// The bytes the live ranges take on the wire, held to what one SUBSCRIBE carries. + size: usize, + /// How many ranges the SUBSCRIBE carried, whose starts SUBSCRIBE_OK reports. + initial: u64, +} + +impl LiveRanges { + /// The ranges a SUBSCRIBE carried. + pub fn new(version: Version, ranges: &[Range]) -> Self { + let mut live = Self { + version, + next: 0, + live: Default::default(), + size: 0, + initial: ranges.len() as u64, + }; + live.add(ranges.iter().copied()); + live + } + + fn add(&mut self, ranges: impl IntoIterator) { + let form: Form = self.version.into(); + for range in ranges { + // One past the varint's reach can't come off the wire, and is refused below. + self.size = self.size.saturating_add(range_size(&range, form).unwrap_or(usize::MAX)); + self.live.insert(self.next, range); + self.next += 1; + } + } + + fn remove(&mut self, id: u64) -> Option { + let range = self.live.remove(&id)?; + self.size = self + .size + .saturating_sub(range_size(&range, self.version.into()).unwrap_or(usize::MAX)); + Some(range) + } + + /// Every live range with its Range ID, in Range ID order. + pub fn numbered(&self) -> Vec<(u64, Range)> { + self.iter().map(|(id, range)| (id, *range)).collect() + } + + /// Every live range with its Range ID, in Range ID order. + pub fn iter(&self) -> impl Iterator { + self.live.iter().map(|(id, range)| (*id, range)) + } + + /// Whether the publisher reports where each range starts, which lite-07 does by Range + /// ID. Before it, a start describes the SUBSCRIBE's one range alone. + pub fn reports_starts(&self) -> bool { + self.version.has_ranges() + } + + /// The live range with Range ID `id`. + pub fn get(&self, id: u64) -> Option<&Range> { + self.live.get(&id) + } + + /// Whether `id` was ever assigned. + pub fn assigned(&self, id: u64) -> bool { + id < self.next + } + + /// Whether SUBSCRIBE_OK reports the start of the range with Range ID `id`, which the + /// SUBSCRIBE carried; SUBSCRIBE_STARTED reports each one an update adds. + pub fn initial(&self, id: u64) -> bool { + id < self.initial + } + + /// How many ranges the SUBSCRIBE carried. + pub fn initial_count(&self) -> u64 { + self.initial + } + + /// Resolve a received edit, returning every live range with its Range ID. + pub fn apply(&mut self, edit: RangeEdit) -> Result, DecodeError> { + match edit { + RangeEdit::Replace(ranges) if !self.version.has_ranges() => { + *self = Self::new(self.version, &ranges); + } + RangeEdit::Edit { remove, add } if self.version.has_ranges() => { + for id in remove { + // An id never assigned, or already retired (including earlier in this list). + self.remove(id).ok_or(DecodeError::InvalidValue)?; + } + self.add(add); + } + _ => return Err(DecodeError::Version), + } + // Asking for nothing is refused like a SUBSCRIBE that does, and a set no SUBSCRIBE + // could carry like one too large to send. + if self.live.is_empty() { + return Err(DecodeError::InvalidSubscribeLocation); + } + if self.size > super::message::MAX_MESSAGE_SIZE { + return Err(DecodeError::TooMany); + } + Ok(self.numbered()) + } + + /// The edit that keeps the live ranges as they are. + pub fn unchanged(&self) -> RangeEdit { + match self.version.has_ranges() { + true => RangeEdit::Edit { + remove: Vec::new(), + add: Vec::new(), + }, + false => RangeEdit::Replace(self.live.values().copied().collect()), + } + } + + /// The edit that turns the live ranges into `ranges`, which become the live ones. + /// + /// A range already live keeps its Range ID, so an update that changes one range sends + /// only that one, and the publisher keeps where the rest resolved. + #[cfg(test)] + pub fn edit(&mut self, ranges: &[Range]) -> Result { + let keyed: Vec<_> = ranges.iter().map(|range| ((), *range)).collect(); + self.edit_keyed(&keyed, |_| ()).map(|(edit, _)| edit) + } + + /// [`Self::edit`] for ranges asked for by different owners: a live range keeps its Range + /// ID only for a wanted range of the same key, `key_of` giving each live one's. Returns + /// the edit and the Range ID each range it adds takes, with that range's key. + pub fn edit_keyed( + &mut self, + ranges: &[(K, Range)], + key_of: impl Fn(u64) -> K, + ) -> Result<(RangeEdit, Vec<(u64, K)>), EncodeError> { + if ranges.is_empty() { + return Err(EncodeError::InvalidState); + } + if !self.version.has_ranges() { + // Replaced ranges take fresh Range IDs, so nothing said of the old ones applies. + let ranges: Vec = ranges.iter().map(|(_, range)| *range).collect(); + if !self.live.values().eq(&ranges) { + self.live.clear(); + self.size = 0; + self.add(ranges.iter().copied()); + } + return Ok((RangeEdit::Replace(ranges), Vec::new())); + } + // Each live range keeps at most one wanted copy, so duplicates edit like any other. + let mut wanted = std::collections::HashMap::<(K, Range), usize>::new(); + for wanted_range in ranges { + *wanted.entry(*wanted_range).or_default() += 1; + } + let mut remove = Vec::new(); + for (&id, range) in &self.live { + match wanted.get_mut(&(key_of(id), *range)) { + Some(count) if *count > 0 => *count -= 1, + _ => remove.push(id), + } + } + // In the order asked for, so the publisher numbers them the way this side does. + let added: Vec<(K, Range)> = ranges + .iter() + .filter(|wanted_range| { + let count = wanted.get_mut(*wanted_range).expect("counted above"); + let keep = *count > 0; + *count = count.saturating_sub(1); + keep + }) + .copied() + .collect(); + for &id in &remove { + self.remove(id); + } + let ids = (self.next..).zip(added.iter().map(|(key, _)| *key)).collect(); + let add: Vec = added.into_iter().map(|(_, range)| range).collect(); + self.add(add.iter().copied()); + if self.size > super::message::MAX_MESSAGE_SIZE { + return Err(EncodeError::TooLarge); + } + Ok((RangeEdit::Edit { remove, add }, ids)) + } +} + +/// SUBSCRIBE_STARTED: where a range a SUBSCRIBE_UPDATE added starts. Lite-07 and later. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct SubscribeStarted { + /// The range's Range ID. + pub id: u64, + /// The group its delivery starts at, `None` for a range with no group there. + pub start: Option, +} + +impl<'a> Message<'a> for SubscribeStarted { + const CONTROL: Option = Some(crate::stats::Message::Subscribe); + fn decode_msg(r: &mut Decoder<'a>, version: Version) -> Result { + if !version.has_ranges() { + return Err(DecodeError::Version); + } + Ok(Self { + id: r.varint()?, + start: r.varint_opt()?, + }) + } + + fn encode_msg(&self, w: &mut Encoder<'_>, version: Version) -> Result<(), EncodeError> { + if !version.has_ranges() { + return Err(EncodeError::Version); + } + w.varint(self.id)?; + w.varint_opt(self.start) + } +} + /// Indicates that one or more groups have been dropped. /// /// The range `[start, end]` is inclusive on both ends. For example, @@ -802,12 +1094,14 @@ impl<'a> Message<'a> for SubscribeDrop { /// - Lite05+: `0x0` SUBSCRIBE_START (SUBSCRIBE_OK on lite-07), `0x1` SUBSCRIBE_FINISH /// (SUBSCRIBE_END before lite-07), `0x2` SUBSCRIBE_DROP /// (SUBSCRIBE_OK was removed; acceptance is implicit). +/// - Lite07+: also `0x4` SUBSCRIBE_STARTED. #[derive(Clone, Debug)] pub enum SubscribeResponse { Ok(SubscribeOk), Start(SubscribeStart), Finish(SubscribeFinish), Drop(SubscribeDrop), + Started(SubscribeStarted), } /// Write a `type` varint followed by the size-prefixed message body. @@ -838,6 +1132,7 @@ impl Encode for SubscribeResponse { Self::Start(start) => encode_typed(w, 0, start, version)?, Self::Finish(end) => encode_typed(w, 1, end, version)?, Self::Drop(drop) => encode_typed(w, 2, drop, version)?, + Self::Started(started) => encode_typed(w, 4, started, version)?, Self::Ok(_) => return Err(EncodeError::Version), }, } @@ -864,6 +1159,7 @@ impl<'a> Decode<'a, Version> for SubscribeResponse { 0 => Ok(Self::Start(SubscribeStart::decode(buf, version)?)), 1 => Ok(Self::Finish(SubscribeFinish::decode(buf, version)?)), 2 => Ok(Self::Drop(SubscribeDrop::decode(buf, version)?)), + 4 if version.has_ranges() => Ok(Self::Started(SubscribeStarted::decode(buf, version)?)), _ => Err(DecodeError::InvalidMessage(typ)), } } @@ -1257,18 +1553,148 @@ mod test { ]; assert_eq!(roundtrip(&msg, Version::Lite07).ranges, msg.ranges); + // An update from the oldest group to the same ranges adds every one of them. + let mut sent = LiveRanges::new(Version::Lite07, &[Range::OLDEST]); let update = SubscribeUpdate { wanted_group: None, priority: 3, max_delay: std::time::Duration::from_millis(250), min_delay: None, - ranges: msg.ranges.clone(), + ranges: sent.edit(&msg.ranges).unwrap(), order: crate::track::Order::Ascending, }; let buf = update.encode_bytes(Version::Lite07).unwrap(); let got = crate::coding::decode_buf(&mut buf.as_ref(), Version::Lite07, |r, v| SubscribeUpdate::decode(r, v)) .unwrap(); - assert_eq!(got.ranges, msg.ranges); + let mut live = LiveRanges::new(Version::Lite07, &[Range::OLDEST]); + let resolved: Vec<_> = live + .apply(got.ranges) + .unwrap() + .into_iter() + .map(|(_, range)| range) + .collect(); + assert_eq!(resolved, msg.ranges); + } + + /// An update sends only the ranges that changed, and the publisher resolves the same list, + /// with the same Range IDs, as the subscriber holds. + #[test] + fn lite07_updates_edit_only_the_changed_ranges() { + let version = Version::Lite07; + let (a, b, c) = ( + Range { + start: absolute(1, 0), + end: absolute(5, 0), + }, + Range { + start: Some(Anchor::Relative(3)), + end: None, + }, + Range { + start: Some(Anchor::Time(9_000)), + end: None, + }, + ); + let mut sent = LiveRanges::new(version, &[a, b]); + let mut live = LiveRanges::new(version, &[a, b]); + let mut step = |ranges: &[Range], expected: RangeEdit| { + let edit = sent.edit(ranges).unwrap(); + assert_eq!(edit, expected); + let update = SubscribeUpdate { + wanted_group: None, + priority: 0, + max_delay: std::time::Duration::ZERO, + min_delay: None, + ranges: edit, + order: crate::track::Order::Descending, + }; + let buf = update.encode_bytes(version).unwrap(); + let got = + crate::coding::decode_buf(&mut buf.as_ref(), version, |r, v| SubscribeUpdate::decode(r, v)).unwrap(); + assert_eq!(live.apply(got.ranges).unwrap(), sent.numbered()); + }; + + let edit = |remove: &[u64], add: &[Range]| RangeEdit::Edit { + remove: remove.to_vec(), + add: add.to_vec(), + }; + step(&[b, a], edit(&[], &[])); + step(&[a, c], edit(&[1], &[c])); + step(&[c, c, a], edit(&[], &[c])); + step(&[b], edit(&[0, 2, 3], &[b])); + assert_eq!(live.numbered(), [(4, b)]); + assert!( + live.initial(1) && !live.initial(2), + "SUBSCRIBE_START reports only the SUBSCRIBE's" + ); + } + + /// An edit naming a Range ID never assigned or already retired, one leaving nothing + /// live, or one adding more than a SUBSCRIBE carries, is refused. + #[test] + fn lite07_refuses_edits_it_cannot_apply() { + let version = Version::Lite07; + let edit = |remove: Vec, add: Vec| RangeEdit::Edit { remove, add }; + let fresh = || LiveRanges::new(version, &[Range::OLDEST]); + assert!( + fresh().apply(edit(vec![1], vec![Range::OLDEST])).is_err(), + "never assigned" + ); + assert!( + fresh().apply(edit(vec![0, 0], vec![Range::OLDEST])).is_err(), + "retired twice" + ); + assert!(fresh().apply(edit(vec![0], vec![])).is_err(), "nothing left"); + assert!( + fresh().apply(RangeEdit::Replace(vec![Range::OLDEST])).is_err(), + "not on lite-07" + ); + + let mut live = fresh(); + let one = Range { + start: absolute(1 << 40, 1 << 40), + end: absolute(1 << 41, 1 << 40), + }; + // Each update fits in a message, but together they would not fit in a SUBSCRIBE. + let mut refused = false; + for _ in 0..(super::super::message::MAX_MESSAGE_SIZE / 16) { + if live.apply(edit(vec![], vec![one; 8])).is_err() { + refused = true; + break; + } + } + assert!(refused, "the live ranges are held to one SUBSCRIBE"); + } + + /// SUBSCRIBE_STARTED is type 4 on lite-07 and unknown before it. + #[test] + fn subscribe_started_is_type_4_on_lite07() { + for start in [None, Some(0), Some(7)] { + let started = SubscribeStarted { id: 4, start }; + let buf = SubscribeResponse::Started(started.clone()) + .encode_bytes(Version::Lite07) + .unwrap(); + assert_eq!(buf[0], 4); + let got = crate::coding::decode_buf(&mut buf.as_ref(), Version::Lite07, |r, v| { + SubscribeResponse::decode(r, v) + }) + .unwrap(); + assert!(matches!(got, SubscribeResponse::Started(got) if got == started)); + } + let started = SubscribeResponse::Started(SubscribeStarted { id: 0, start: None }); + assert!(started.encode_bytes(Version::Lite06).is_err()); + let mut buf = Vec::new(); + let mut w = Encoder::new(&mut buf, Version::Lite06.into()); + w.varint(4).unwrap(); + SubscribeStarted { id: 0, start: None } + .encode(&mut w, Version::Lite07) + .unwrap(); + assert!( + crate::coding::decode_buf(&mut buf.as_slice(), Version::Lite06, |r, v| SubscribeResponse::decode( + r, v + )) + .is_err() + ); } /// A subscription asking for nothing is refused both ways, and an unknown anchor type @@ -1357,7 +1783,7 @@ mod test { priority: 0, max_delay: std::time::Duration::ZERO, min_delay: None, - ranges: vec![Range::OLDEST], + ranges: LiveRanges::new(version, &[Range::OLDEST]).unchanged(), order: crate::track::Order::Descending, }; let buf = update.encode_bytes(version).unwrap(); @@ -1390,7 +1816,7 @@ mod test { priority: 0, max_delay: std::time::Duration::ZERO, min_delay, - ranges: vec![Range::OLDEST], + ranges: LiveRanges::new(version, &[Range::OLDEST]).unchanged(), order: crate::track::Order::Descending, }; let buf = update.encode_bytes(version).unwrap(); @@ -1418,7 +1844,7 @@ mod test { priority: 3, max_delay: std::time::Duration::from_millis(250), min_delay: None, - ranges: vec![Range::OLDEST], + ranges: LiveRanges::new(version, &[Range::OLDEST]).unchanged(), order, }; let mut buf = Vec::new(); diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index ccbcb4c7345..c813200e213 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -20,6 +20,9 @@ use crate::tail::{self, Reading, Settle, Tail}; use kio::Lock; +mod fills; +use fills::{FillAsk, RangeFills}; + pub(super) struct SubscriberConfig { pub runtime: crate::time::Deadlines, pub session: Transport, @@ -2907,15 +2910,17 @@ mod tests { start: None, priority: 0, requested: None, - ranges: vec![lite::Range::OLDEST], + live: lite::LiveRanges::new(Version::Lite04, &[lite::Range::OLDEST]), + demand: Some(Subscription::default()), + fill_ids: Default::default(), + fill_groups: Default::default(), tail: tail.clone(), served: None, started: false, - starts: Vec::new(), + starts: Default::default(), end: None, wanted: None, unanswered: None, - updated: false, _working: crate::session::Work::default().start(), }) } @@ -2961,21 +2966,24 @@ mod tests { unreachable!("feed_sub is active") }; let largest = Some(Position::group(10)); - active.ranges = vec![lite::Range::span(Some(0), 0, Some(10), None)]; + let live = |ranges: &[lite::Range]| lite::LiveRanges::new(Version::Lite07, ranges); + active.live = live(&[lite::Range::span(Some(0), 0, Some(10), None)]); active.served = Some(0); - active.starts = vec![Some(0)]; + active.starts = [(0, Some(0))].into(); assert_eq!(active.never_serves(largest), None, "group 10 is in 0..11"); - active.ranges = vec![lite::Range::span(Some(0), 0, Some(3), None)]; + active.live = live(&[lite::Range::span(Some(0), 0, Some(3), None)]); assert_eq!(active.never_serves(largest), largest, "0..4 never reaches group 10"); - active.ranges.push(lite::Range::span(Some(10), 0, Some(12), None)); - active.starts = vec![Some(0), Some(10)]; + let later = lite::Range::span(Some(10), 0, Some(12), None); + active.live = live(&[lite::Range::span(Some(0), 0, Some(3), None), later]); + active.starts = [(0, Some(0)), (1, Some(10))].into(); assert_eq!(active.never_serves(largest), None, "a later range serves it"); - active.starts[1] = None; + active.starts.insert(1, None); assert_eq!(active.never_serves(largest), largest, "that range served nothing"); - active.ranges[1].end = None; + let open = lite::Range { end: None, ..later }; + active.live = live(&[lite::Range::span(Some(0), 0, Some(3), None), open]); assert_eq!(active.never_serves(largest), None, "an open feed reaches it"); assert_eq!(active.never_serves(None), None, "nothing to go live at"); } @@ -3056,15 +3064,17 @@ mod tests { start: Some(floor), priority: 0, requested: Some(floor), - ranges: vec![lite::Range::span(Some(floor), 0, None, None)], + live: lite::LiveRanges::new(Version::Lite04, &[lite::Range::span(Some(floor), 0, None, None)]), + demand: Some(Subscription::default()), + fill_ids: Default::default(), + fill_groups: Default::default(), tail: Default::default(), served: None, started: false, - starts: Vec::new(), + starts: Default::default(), end: None, wanted: None, unanswered: None, - updated: false, _working: crate::session::Work::default().start(), }); if update { @@ -3073,7 +3083,9 @@ mod tests { .await .unwrap(); } else { - let begin = h.serve.begin_subscription(&mut track, &mut Sub::None, lowered, true); + let begin = h + .serve + .begin_subscription(&mut track, &mut Sub::None, lowered, &FillAsk::default(), true); assert!(matches!(begin, Ok(Begin::Establish(_)))); } @@ -3517,15 +3529,17 @@ mod tests { start: Some(2), priority: 0, requested: Some(2), - ranges: vec![lite::Range::span(Some(2), 0, None, None)], + live: lite::LiveRanges::new(VERSION, &[lite::Range::span(Some(2), 0, None, None)]), + demand: Some(Subscription::default()), + fill_ids: Default::default(), + fill_groups: Default::default(), tail: Default::default(), served: None, started: false, - starts: Vec::new(), + starts: Default::default(), end: Some(lite::SubscribeFinish { group: 10, error: None }), wanted: None, unanswered: None, - updated: false, _working: crate::session::Work::default().start(), }; assert_eq!( @@ -3534,12 +3548,10 @@ mod tests { "without SUBSCRIBE_START nothing was served" ); - // An update moves the floor, the ranges, and `updated` together, as - // `begin_subscription` does. + // An update moves the floor and the ranges together, as `begin_subscription` does. let update = |sub: &mut SubStream, start: Option, ranges: Vec| { sub.start = start; - sub.ranges = ranges; - sub.updated = true; + sub.edit(&ranges, 0).unwrap(); }; sub.started = true; update(&mut sub, Some(1), vec![lite::Range::span(Some(1), 0, None, None)]); @@ -3550,10 +3562,14 @@ mod tests { ); sub.start = Some(2); - sub.ranges = vec![ - lite::Range::span(Some(2), 0, Some(3), None), - lite::Range::span(Some(6), 0, None, None), - ]; + sub.edit( + &[ + lite::Range::span(Some(2), 0, Some(3), None), + lite::Range::span(Some(6), 0, None, None), + ], + 0, + ) + .unwrap(); assert_eq!( sub.owed(None), Some(vec![2..4, 6..10]), @@ -3561,9 +3577,10 @@ mod tests { ); update(&mut sub, Some(2), vec![lite::Range::span(Some(2), 0, None, None)]); - sub.updated = false; + // A START for the range now live. sub.served = Some(4); - sub.starts = vec![Some(4)]; + let (id, _) = sub.live.iter().next().unwrap(); + sub.starts = [(id, Some(4))].into(); assert_eq!( sub.owed(None), Some(vec![4..10]), @@ -3602,6 +3619,133 @@ mod tests { ); } + /// A fill asking for exactly what a demand range does is still a fill, judged by its own + /// Range ID, and keeps that ID while it stays held. + #[kio::test] + async fn a_lite07_fill_is_known_by_its_range_id() { + let session = crate::lite::test_transport::ScriptedSession::new(Vec::new()); + let stream = Stream::open(&mut crate::driver::Transport::new(session.clone()), Version::Lite07) + .await + .unwrap(); + // As a fill asks on the wire. + let groups = |range: std::ops::Range| lite::Range { + start: Some(lite::Anchor::Absolute(Position::group(range.start))), + end: Some(lite::Anchor::Absolute(Position::group(range.end))), + }; + let demand = groups(4..8); + let mut sub = SubStream { + order: crate::track::Order::Descending, + stream, + id: 0, + max_delay: Duration::ZERO, + min_delay: None, + start: Some(4), + priority: 0, + requested: Some(4), + live: lite::LiveRanges::new(Version::Lite07, &[demand]), + demand: Some(Subscription::default()), + fill_ids: Default::default(), + fill_groups: Default::default(), + tail: Default::default(), + served: Some(4), + started: true, + starts: [(0, Some(4))].into(), + end: None, + wanted: None, + unanswered: None, + _working: crate::session::Work::default().start(), + }; + assert_eq!(sub.fill_range(0), None, "the demand's range is no fill"); + + // The same bounds again, as a fill. + sub.edit(&[demand, demand], 1).unwrap(); + assert_eq!(sub.fill_range(0), None, "the demand keeps its range"); + assert_eq!(sub.fill_range(1), Some(4..8), "the copy the edit added is the fill"); + + // Another fill joins, and the first keeps its Range ID. + let other = groups(10..12); + sub.edit(&[demand, demand, other], 2).unwrap(); + assert_eq!(sub.fill_ids.iter().copied().collect::>(), [1, 2]); + + // The demand moves away while the fill stays: the fill keeps Range ID 1. + let moved = lite::Range::span(Some(20), 0, None, None); + sub.edit(&[moved, demand, other], 2).unwrap(); + assert_eq!(sub.fill_ids.iter().copied().collect::>(), [1, 2]); + assert_eq!(sub.fill_range(1), Some(4..8)); + assert_eq!(sub.fill_range(3), None, "the demand's new range is no fill"); + + // Before SUBSCRIBE_START, a fill an update retires keeps its Range ID, since the start + // still names it, and it must not count as the demand's. + sub.started = false; + sub.edit(&[moved, demand], 1).unwrap(); + assert_eq!(sub.fill_range(2), None, "retired"); + assert!(sub.fill_ids.contains(&2), "still a fill's"); + sub.started = true; + sub.edit(&[moved, demand], 1).unwrap(); + assert!(!sub.fill_ids.contains(&2), "forgotten once started"); + } + + /// A lite-07 range an update adds owes nothing known until SUBSCRIBE_STARTED says where it + /// starts, and then owes from there; one retired since owes nothing. + #[kio::test] + #[expect( + clippy::single_range_in_vec_init, + reason = "the owed groups are a list of runs, often one" + )] + async fn a_lite07_added_range_is_owed_from_its_reported_start() { + let session = crate::lite::test_transport::ScriptedSession::new(Vec::new()); + let stream = Stream::open(&mut crate::driver::Transport::new(session.clone()), Version::Lite07) + .await + .unwrap(); + let first = lite::Range::span(Some(4), 0, None, None); + let mut sub = SubStream { + order: crate::track::Order::Descending, + stream, + id: 0, + max_delay: Duration::ZERO, + min_delay: None, + start: Some(4), + priority: 0, + requested: Some(4), + live: lite::LiveRanges::new(Version::Lite07, &[first]), + demand: Some(Subscription::default()), + fill_ids: Default::default(), + fill_groups: Default::default(), + tail: Default::default(), + served: Some(4), + started: true, + starts: [(0, Some(4))].into(), + end: Some(lite::SubscribeFinish { group: 10, error: None }), + wanted: None, + unanswered: None, + _working: crate::session::Work::default().start(), + }; + assert_eq!(sub.owed(None), Some(vec![4..10])); + + // Two groups back from the latest, which only the publisher can resolve. + let back = lite::Range { + start: Some(lite::Anchor::Relative(2)), + end: None, + }; + let (edit, changed) = sub.edit(&[first, back], 0).unwrap(); + assert!(changed); + assert_eq!( + edit, + lite::RangeEdit::Edit { + remove: vec![], + add: vec![back] + } + ); + assert_eq!(sub.owed(None), None, "nothing says yet where it starts"); + sub.starts.insert(1, Some(1)); + assert_eq!(sub.owed(None), Some(vec![4..10, 1..10])); + + // Retiring the first range forgets its start. + sub.edit(&[back], 0).unwrap(); + assert_eq!(sub.starts.keys().copied().collect::>(), [1]); + assert_eq!(sub.owed(Some(8)), Some(vec![1..8])); + } + async fn receive_padding(version: Version, bytes: Vec) { let session = crate::lite::test_transport::ScriptedSession::new(bytes); let stream = Stream::open(&mut crate::driver::Transport::new(session.clone()), version) @@ -5939,8 +6083,13 @@ mod tests { let broadcast = crate::broadcast::Info::new().produce(); let request = broadcast.reserve_track("audio").unwrap(); let mut serving = ServeLoop::new(&serve, request, Default::default()); - let establish = - serve.prepare_establish(&mut serving.serving, Subscription::default(), vec![lite::Range::OLDEST]); + let wire = Wire { + subscription: Subscription::default(), + demand: Some(Subscription::default()), + demand_ranges: vec![lite::Range::OLDEST], + ranges: vec![lite::Range::OLDEST], + }; + let establish = serve.prepare_establish(&mut serving.serving, wire, &Default::default()); serving.mode = ServeMode::Establish(establish); let gate_run = serve.gate(); let mut running = TrackServeRun { @@ -7906,11 +8055,6 @@ fn wire_ranges(subscription: &Subscription, timescale: Option) -> Res Ok((!ranges.is_empty()).then_some(ranges)) } -/// Whether every range has an end. -fn bounded(ranges: &[lite::Range]) -> bool { - ranges.iter().all(|range| range.end.is_some()) -} - /// `subscription` narrowed to the one span a wire without ranges carries, which the /// subscribers' own reads narrow again. /// @@ -7945,8 +8089,17 @@ struct SubStream { /// The lowest absolute start among the ranges, or `None` when one counts back from /// the latest group: the floor below which nothing is asked for. start: Option, - /// The ranges on the wire, never empty. - ranges: Vec, + /// The ranges on the wire by Range ID, never empty: the demand's, then the fills'. + live: lite::LiveRanges, + /// The demand the ranges ask for, `None` when they ask only for fills. + demand: Option, + /// The Range IDs of the fills' ranges; the rest are the demand's, which alone set where + /// the live feed is. Held by ID, since a fill may ask for exactly what a demand range does, + /// and until SUBSCRIBE_START even once retired, since it answers every range the + /// SUBSCRIBE carried. + fill_ids: std::collections::BTreeSet, + /// The groups the ranges ask for fills. + fill_groups: rangemap::RangeSet, priority: i8, order: crate::track::Order, /// The start the SUBSCRIBE itself carried, fixed for the stream's life. A @@ -7961,9 +8114,9 @@ struct SubStream { served: Option, /// Whether SUBSCRIBE_START arrived, which `served` alone cannot say when no range starts. started: bool, - /// Where SUBSCRIBE_START says each range's delivery starts, `None` for one with nothing - /// to deliver. It matches `ranges` until an update changes them. - starts: Vec>, + /// Where SUBSCRIBE_START or SUBSCRIBE_STARTED says each live range's delivery starts, by + /// Range ID, `None` for one with nothing to deliver. + starts: std::collections::BTreeMap>, /// The track's exclusive end (SUBSCRIBE_FINISH), once declared. end: Option, /// The refresh join's wanted group, restated in every SUBSCRIBE_UPDATE. @@ -7972,9 +8125,6 @@ struct SubStream { /// without an instance resolves whichever one wins the path when it arrives, so one /// still unanswered once another instance takes the path may be answered by it. unanswered: Option, - /// Whether an update changed the ranges since the SUBSCRIBE, which the publisher may - /// have read before its SUBSCRIBE_START, so the starts need not match the SUBSCRIBE. - updated: bool, /// Holds the session busy for as long as the subscription is open. _working: crate::session::Working, } @@ -7992,36 +8142,45 @@ impl SubStream { /// serves Largest waits for it, so an old cache is judged against that edge. fn never_serves(&self, largest: Option) -> Option { largest.filter(|largest| { - bounded(&self.ranges) + self.live.iter().all(|(_, range)| range.end.is_some()) && self .spans() .is_none_or(|spans| !spans.iter().any(|span| span.contains(&largest.group))) }) } - /// Where each bounded range delivers, as SUBSCRIBE_START resolved it: from its absolute - /// start, or else from where SUBSCRIBE_START resolved delivery, up to its end. `None` when - /// a range is open, or the ranges changed with nothing served, so nothing says where. + /// Where the live range `id` delivers from, `None` while nothing says yet, and `Some(None)` + /// for a range with no group to deliver. + /// + /// Lite-07 reports every range's start: SUBSCRIBE_START the SUBSCRIBE's, and + /// SUBSCRIBE_STARTED each one an update added. Before it, the one start describes the + /// SUBSCRIBE's range, so a range an update replaced it with delivers from its absolute + /// start, or else from where delivery started. + fn range_start(&self, id: u64, range: &lite::Range) -> Option> { + if let Some(start) = self.starts.get(&id) { + return Some(*start); + } + if self.live.reports_starts() { + return None; + } + match range.start { + Some(lite::Anchor::Absolute(start)) => Some(Some(start.group)), + _ => self.served.or(self.start).map(Some), + } + } + + /// Where each bounded range delivers, as its reported start resolved it, up to its end. + /// `None` when a range is open, or nothing says yet where one starts. fn spans(&self) -> Option>> { - let Some(served) = self.served else { - return (!self.updated).then(Vec::new); - }; - self.ranges + self.live .iter() - .enumerate() - .map(|(index, range)| { + .map(|(id, range)| { // Our absolute ends name a group's first frame (see `wire_ranges`). let Some(lite::Anchor::Absolute(end)) = range.end else { return None; }; - let start = match self.starts.get(index) { - // Each range's own start accounts for its skipped prefix. - Some(start) if !self.updated => start.unwrap_or(end.group), - _ => match range.start { - Some(lite::Anchor::Absolute(start)) => start.group, - _ => served, - }, - }; + // Each range's own start accounts for its skipped prefix. + let start = self.range_start(id, range)?.unwrap_or(end.group); Some(start..end.group) }) .collect() @@ -8030,30 +8189,22 @@ impl SubStream { /// The groups the publisher still owes once it has ended the subscription, one run per /// range: the publisher accounts for nothing between disjoint ranges. /// - /// `None` when nothing says which: drafts before SUBSCRIBE_FINISH only have the FIN. - /// Without a SUBSCRIBE_START the publisher served no group at all, unless an update since - /// moved the floor the demand asks from. Each range is owed from where SUBSCRIBE_START - /// said it starts while the ranges are still the SUBSCRIBE's, which accounts for a skipped - /// prefix and resolves a relative or time start; after an update, from its absolute start, - /// or else from the first group served. The groups the SUBSCRIBE asked for below its - /// SUBSCRIBE_START are accounted for as unavailable when it arrives. + /// `None` when nothing says which: drafts before SUBSCRIBE_FINISH only have the FIN, and a + /// lite-07 range an update added owes nothing known until SUBSCRIBE_STARTED says where it + /// starts. Without a SUBSCRIBE_START the publisher served no group at all. Each range is + /// owed from its reported start (see [`Self::range_start`]), which accounts for a skipped + /// prefix and resolves a relative or time start. The groups the SUBSCRIBE asked for below + /// its SUBSCRIBE_START are accounted for as unavailable when it arrives. fn owed(&self, requested_end: Option) -> Option>> { let end = self.end.as_ref()?.group; let end = requested_end.map_or(end, |requested| requested.min(end)); - // A SUBSCRIBE_START that started no range owes nothing, unless an update since asked - // for more, which the open stream still serves. - if self.served.is_none() && !(self.started && self.updated) { + if !self.started { return Some(Vec::new()); } - let fallback = self.served.or(self.start); - let owed = self.ranges.iter().enumerate().filter_map(|(index, range)| { - let start = match (self.updated, self.starts.get(index)) { - (false, Some(start)) => (*start)?, - _ => match range.start { - Some(lite::Anchor::Absolute(start)) => start.group, - // A start counted back from the latest group resolves where delivery did. - _ => fallback?, - }, + let mut owed = Vec::new(); + for (id, range) in self.live.iter() { + let Some(start) = self.range_start(id, range)? else { + continue; }; // Our absolute ends name a group's first frame (see `wire_ranges`), and the rest // go upstream open. @@ -8061,9 +8212,48 @@ impl SubStream { Some(lite::Anchor::Absolute(stop)) => stop.group.min(end), _ => end, }; - (start < stop).then_some(start..stop) - }); - Some(owed.collect()) + if start < stop { + owed.push(start..stop); + } + } + Some(owed) + } + + /// The groups the live range `id` asks for a fill, `None` for one of the demand's. + fn fill_range(&self, id: u64) -> Option> { + if !self.fill_ids.contains(&id) { + return None; + } + let range = self.live.get(id)?; + match (range.start, range.end) { + (Some(lite::Anchor::Absolute(start)), Some(lite::Anchor::Absolute(end))) => Some(start.group..end.group), + _ => None, + } + } + + /// Edit the live ranges into `ranges`, the last `fills` of which are the held fills', + /// forgetting the starts of those retired. A fill asking for what a demand range does + /// still takes a range of its own, so each Range ID stays the demand's or a fill's. + /// Returns the edit, and whether it changed anything. + fn edit(&mut self, ranges: &[lite::Range], fills: usize) -> Result<(lite::RangeEdit, bool), Error> { + let before: Vec<_> = self.live.iter().map(|(id, _)| id).collect(); + let demand = ranges.len().saturating_sub(fills); + let keyed: Vec<_> = ranges + .iter() + .enumerate() + .map(|(index, range)| (index >= demand, *range)) + .collect(); + let fill_ids = &self.fill_ids; + let (edit, added) = self.live.edit_keyed(&keyed, |id| fill_ids.contains(&id))?; + let changed = !self.live.iter().map(|(id, _)| id).eq(before); + let live = &self.live; + self.starts.retain(|id, _| live.get(*id).is_some()); + if self.started { + self.fill_ids.retain(|id| live.get(*id).is_some()); + } + self.fill_ids + .extend(added.into_iter().filter(|(_, fill)| *fill).map(|(id, _)| id)); + Ok((edit, changed)) } } @@ -8461,20 +8651,60 @@ impl TrackServe { producer: &mut track::Producer, sub: &mut Sub, pref: Option, + fills: &FillAsk, supports_update: bool, ) -> Result { let opening = matches!(sub, Sub::None); + // Lite-07 asks the held fills' groups through the same subscription. Demand leaving + // while they are out keeps what it asked until they are answered, rather than retire + // the live feed's ranges under a subscription that still runs. + let none = FillAsk::default(); + let fills = match self.subscriber.version.has_ranges() { + true => fills, + false => &none, + }; + let pref = match (pref, &*sub) { + (None, Sub::Active(active)) if !fills.is_empty() => active.demand.clone(), + (pref, _) => pref, + }; let request = self.wire_request(pref, opening, producer.idle_newest(), producer.info().timescale)?; + // What goes on the wire: the demand's ranges, then the fills', under the demand's + // parameters. The demand alone sets the floor. + let request = match request { + Some((demand, demand_ranges)) => { + let mut ranges = demand_ranges.clone(); + ranges.extend(fills.wire()); + Some(Wire { + subscription: fills.merge(demand.clone()), + demand: Some(demand), + demand_ranges, + ranges, + }) + } + None if fills.is_empty() => None, + None => Some(Wire { + subscription: fills.subscription(), + demand: None, + demand_ranges: Vec::new(), + ranges: fills.wire().collect(), + }), + }; match request { - Some((subscription, ranges)) => { + Some(wire) => { match sub { // Open an upstream SUBSCRIBE for the first subscriber. - Sub::None => Ok(Begin::Establish(self.prepare_establish(producer, subscription, ranges))), + Sub::None => Ok(Begin::Establish(self.prepare_establish(producer, wire, &fills.groups))), Sub::Active(active) => { + let Wire { + subscription, + demand, + demand_ranges, + ranges, + } = wire; // Downstream preferences changed: forward them upstream as a // SUBSCRIBE_UPDATE (Lite03+ only; older peers can't carry one). - let start_moved = active.start != subscription.start(); + let start_moved = demand.is_some() && active.start != subscription.start(); active.priority = subscription.priority; active.order = subscription.order; active.max_delay = wire_max_delay(subscription.delay.max); @@ -8489,7 +8719,8 @@ impl TrackServe { // So do changed ranges that keep the floor, such as a widening that joins // two: each gap they reach waits a fresh grace, which only delays giving up // on one still owed. - let widened = (active.ranges != ranges) + let (edit, changed) = active.edit(&ranges, ranges.len() - demand_ranges.len())?; + let widened = (changed && demand.is_some()) .then(|| subscription.start().or(active.served).unwrap_or(0)..u64::MAX); if let Ok(mut tail) = active.tail.write() { tail.set_grace(producer.tail_grace(subscription.budget())); @@ -8497,10 +8728,17 @@ impl TrackServe { for groups in lowered.into_iter().chain(widened) { tail.demand(groups, now); } + // So do the groups a fill newly asks for. + for groups in fills.groups.iter() { + let asked = active.fill_groups.gaps(groups).collect::>(); + for groups in asked { + tail.demand(groups, now); + } + } } + active.fill_groups = fills.groups.clone(); + active.demand = demand; active.start = subscription.start(); - active.updated |= active.ranges != ranges; - active.ranges = ranges; if supports_update { // The floor follows the requested start, in both directions: // moving below a declared SUBSCRIBE_START reopens those groups @@ -8513,7 +8751,7 @@ impl TrackServe { if start_moved { let _ = producer.start_at(active.start); } - buffer_update(active)?; + buffer_update(active, edit)?; } Ok(Begin::None) } @@ -8528,9 +8766,12 @@ impl TrackServe { self.subscriber.remove_subscribe(active.id); let _ = active.stream.writer.finish(); tracing::debug!(track = %self.name, "subscribe canceled (idle)"); + // The copy is still held: what it cached goes stale from here, unless the + // subscription only filled it and never fed it live. + if active.demand.is_some() { + producer.set_idle(); + } *sub = Sub::None; - // The copy is still held: what it cached goes stale from here. - producer.set_idle(); } Ok(Begin::None) } @@ -8606,19 +8847,32 @@ impl TrackServe { fn prepare_establish( &self, producer: &mut track::Producer, - subscription: Subscription, - ranges: Vec, + wire: Wire, + fills: &rangemap::RangeSet, ) -> Establish { - self.request_floor(producer, subscription.start()); + let Wire { + subscription, + demand, + demand_ranges, + ranges, + } = wire; + // One asking only for fills is no live feed, so it leaves the floor alone. + if demand.is_some() { + self.request_floor(producer, subscription.start()); + } let framing = lite::Framing::new(producer.info(), self.subscriber.version); - self.register( + let mut establish = self.register( subscription, ranges, Some(Accepted { producer: producer.clone(), framing, }), - ) + ); + establish.demand = demand; + establish.demand_ranges = demand_ranges; + establish.fill_groups = fills.clone(); + establish } /// The floor tracks the requested start until this subscription's own @@ -8682,6 +8936,9 @@ impl TrackServe { closed: session.clone(), session, id, + demand: Some(subscription.clone()), + demand_ranges: ranges.clone(), + fill_groups: Default::default(), subscription, ranges, tail, @@ -8702,7 +8959,13 @@ impl TrackServe { false => wire_span(subscription), }; let ranges = wire_ranges(&subscription, producer.info().timescale)?.expect("a non-empty subscription"); - let mut est = Box::new(self.prepare_establish(producer, subscription, ranges)); + let wire = Wire { + subscription: subscription.clone(), + demand: Some(subscription), + demand_ranges: ranges.clone(), + ranges, + }; + let mut est = Box::new(self.prepare_establish(producer, wire, &Default::default())); let id = est.id; match kio::wait(move |waiter| est.poll(waiter)).await { Ok(active) => { @@ -8726,7 +8989,7 @@ impl TrackServe { pref: Option, supports_update: bool, ) -> Result<(), Error> { - match self.begin_subscription(producer, sub, pref, supports_update)? { + match self.begin_subscription(producer, sub, pref, &FillAsk::default(), supports_update)? { Begin::Establish(est) => { let mut est = Box::new(est); let id = est.id; @@ -8767,18 +9030,28 @@ fn wire_max_delay(max_delay: Option) -> std::time::Duration max_delay.unwrap_or(std::time::Duration::from_millis(super::MAX_SAFE_DELAY_MS)) } -/// Buffer the current SUBSCRIBE parameters. The caller flushes. -fn buffer_update(active: &mut SubStream) -> Result<(), Error> { +/// Buffer the current SUBSCRIBE parameters, with `ranges` the change to the live ranges. +/// The caller flushes. +fn buffer_update(active: &mut SubStream, ranges: lite::RangeEdit) -> Result<(), Error> { active.stream.writer.buffer(&lite::SubscribeUpdate { wanted_group: active.wanted, priority: active.priority, max_delay: active.max_delay, min_delay: active.min_delay, - ranges: active.ranges.clone(), + ranges, order: active.order, }) } +/// What a subscription puts on the wire: its parameters, the demand it asks for, and the +/// ranges, the demand's first, then the held fills'. +struct Wire { + subscription: Subscription, + demand: Option, + demand_ranges: Vec, + ranges: Vec, +} + /// Opens the upstream SUBSCRIBE control stream: send the request, then (pre /// lite-05) wait for the SUBSCRIBE_OK. Resolves with the live [`SubStream`]; /// the caller deregisters the id on failure. @@ -8789,8 +9062,15 @@ struct Establish { // A dedicated close-watch handle for the SUBSCRIBE_OK wait. closed: Transport, id: u64, + /// The parameters the SUBSCRIBE carries. subscription: Subscription, - /// The ranges the SUBSCRIBE carries for `subscription`. + /// The demand it asks for, `None` when it asks only for fills. + demand: Option, + /// The demand's ranges, which come first on the wire. + demand_ranges: Vec, + /// The groups it asks for fills. + fill_groups: rangemap::RangeSet, + /// The ranges the SUBSCRIBE carries: the demand's, then the fills'. ranges: Vec, tail: kio::Producer, /// Holds the session busy while the SUBSCRIBE opens. @@ -8862,17 +9142,20 @@ impl Establish { max_delay: wire_max_delay(self.subscription.delay.max), min_delay: self.subscription.delay.min, start: self.subscription.start(), - ranges: self.ranges.clone(), + live: lite::LiveRanges::new(self.serve.subscriber.version, &self.ranges), + demand: self.demand.clone(), + // The ranges take Range IDs in order, the demand's first. + fill_ids: (self.demand_ranges.len() as u64..self.ranges.len() as u64).collect(), + fill_groups: self.fill_groups.clone(), priority: self.subscription.priority, order: self.subscription.order, requested: self.subscription.start(), tail: self.tail.clone(), served: None, started: false, - starts: Vec::new(), + starts: Default::default(), end: None, wanted: None, - updated: false, _working: self.session.work().start(), } } @@ -9522,6 +9805,8 @@ struct ServeLoop { fetches: kio::Tasks, /// Fetches a pre-lite-05 upstream has no FETCH for, held for the live feed. feed_fetches: FeedFetches, + /// Fills a lite-07 upstream serves through the subscription. + range_fills: RangeFills, // A dedicated close-watch handle for the session-died arm. closed: Transport, // SUBSCRIBE_UPDATE only exists on Lite03+, so older peers can't carry a @@ -9812,6 +10097,7 @@ impl ServeLoop { sub: Sub::None, fetches, feed_fetches: FeedFetches::new(&serve.subscriber.runtime), + range_fills: RangeFills::default(), closed: serve.subscriber.session.clone(), supports_update: !matches!(serve.subscriber.version, Version::Lite01 | Version::Lite02), supports_fetch: serve.subscriber.version.has_track_stream(), @@ -9856,8 +10142,18 @@ impl ServeLoop { /// Apply a demand change: open, update, or cancel the upstream subscription. fn begin(&mut self, serve: &TrackServe, pref: Option) -> Result<(), Error> { - match serve.begin_subscription(&mut self.serving, &mut self.sub, pref, self.supports_update)? { - Begin::Establish(est) => self.mode = ServeMode::Establish(est), + match serve.begin_subscription( + &mut self.serving, + &mut self.sub, + pref, + &self.range_fills.ask, + self.supports_update, + )? { + Begin::Establish(est) => { + // A fresh subscription may deliver what the last one would not. + self.range_fills.forget_refused(); + self.mode = ServeMode::Establish(est); + } Begin::None => {} } // A finished track's tail waits a fresh grace for what an update newly asked for, @@ -9921,19 +10217,53 @@ impl ServeLoop { // so a reader waiting on a skipped group fails over // instead of stalling on a live route. lite::SubscribeResponse::Start(start) => { - // One resolved start per range the SUBSCRIBE carried, unless an - // update the publisher read first replaced them. - if serve.subscriber.version.has_ranges() && !active.updated && start.starts.len() != active.ranges.len() - { - tracing::warn!(track = %serve.name, starts = start.starts.len(), ranges = active.ranges.len(), "subscribe start resolves other ranges"); + if active.started && serve.subscriber.version.has_ranges() { + tracing::warn!(track = %serve.name, "duplicate subscribe start"); return Err(Error::ProtocolViolation); } - // No range starting anywhere leaves nothing owed. - let served = start.starts.iter().flatten().min().copied(); + // One resolved start per range the SUBSCRIBE carried, by Range ID. + if start.starts.len() as u64 != active.live.initial_count() { + tracing::warn!(track = %serve.name, starts = start.starts.len(), ranges = active.live.initial_count(), "subscribe start resolves other ranges"); + return Err(Error::ProtocolViolation); + } + // An update may have retired some already, whose starts say nothing now. A + // fill's range starting past where it asked skips groups it will not deliver. + let now = serve.subscriber.runtime.now(); + let mut served: Option = None; + for (id, start) in (0..).zip(&start.starts) { + // One an update retired since owes nothing, though before lite-07 its start + // still says where the SUBSCRIBE's demand was served from. + if active.live.get(id).is_some() { + active.starts.insert(id, *start); + } + match active.fill_range(id) { + Some(groups) => { + let skipped = groups.start..start.unwrap_or(groups.end).clamp(groups.start, groups.end); + if let Ok(mut tail) = active.tail.write() { + tail.account(skipped.clone(), now); + } + self.range_fills.skip(groups, skipped); + } + // A fill an update retired since says nothing of the live feed either. + None if active.fill_ids.contains(&id) => {} + // Only the demand's ranges say where the live feed is served from. No range + // starting anywhere leaves nothing owed. + None => { + served = match (served, *start) { + (Some(served), Some(start)) => Some(served.min(start)), + (served, start) => served.or(start), + } + } + } + } active.served = served; - active.starts = start.starts.clone(); - // Where the live feed is, on versions whose answer says. - if serve.subscriber.version.has_largest() { + // Only a live fill's Range ID matters from here. + let live = &active.live; + active.fill_ids.retain(|id| live.get(*id).is_some()); + // One asking only for fills feeds nothing live. + if active.demand.is_none() { + active.started = true; + } else if serve.subscriber.version.has_largest() { // A bounded feed that never delivers Largest goes live at the // answer alone; one that does waits for it to show. match active.never_serves(start.largest) { @@ -9952,7 +10282,7 @@ impl ServeLoop { // that moved the start makes it stale, and one that // moved back restores it); elsewhere the // request-tracked floor stands rather than a guess. - if active.start == active.requested { + if active.start == active.requested && active.demand.is_some() { let _ = match (serve.subscriber.version.resolves_start(), served) { (true, Some(served)) => self.serving.start_resolved(served), _ => self.serving.start_at(served), @@ -9969,20 +10299,57 @@ impl ServeLoop { tail.account(requested..served, serve.subscriber.runtime.now()); } } + // Where a range an update added starts, from which its groups are owed. + lite::SubscribeResponse::Started(started) => { + let id = started.id; + if !active.started + || !active.live.assigned(id) + || active.live.initial(id) + || active.starts.contains_key(&id) + { + tracing::warn!(track = %serve.name, id, "unexpected subscribe started"); + return Err(Error::ProtocolViolation); + } + // One retired since is owed nothing. + if active.live.get(id).is_some() { + active.starts.insert(id, started.start); + let now = serve.subscriber.runtime.now(); + // A fill's range starting past where it asked skips groups it will not deliver. + if let Some(groups) = active.fill_range(id) { + let skipped = groups.start..started.start.unwrap_or(groups.end).clamp(groups.start, groups.end); + if let Ok(mut tail) = active.tail.write() { + tail.account(skipped.clone(), now); + } + self.range_fills.skip(groups, skipped); + } + // The tail judges what is owed again. + if let Ok(mut tail) = active.tail.write() { + tail.expire(now); + } + } + } // The publisher will never send these groups, so they // are accounted for without a stream, and a reader of // one still arriving ends with the drop's error now. lite::SubscribeResponse::Drop(dropped) => { + let groups = dropped.start..dropped.end.saturating_add(1); + let err = dropped.error(serve.subscriber.version); if let Ok(mut tail) = active.tail.write() { - tail.account( - dropped.start..dropped.end.saturating_add(1), - serve.subscriber.runtime.now(), - ); + tail.account(groups.clone(), serve.subscriber.runtime.now()); } - self.serving.drop_groups( - dropped.start..dropped.end.saturating_add(1), - dropped.error(serve.subscriber.version), - ); + // A group a fill asked for and the live budget dropped as stale is recovered on + // its own, so the copy does not record it given up meanwhile. + let mut given = rangemap::RangeSet::new(); + given.insert(groups.clone()); + if err == crate::StreamError::Old { + for asked in active.fill_groups.overlapping(&groups) { + given.remove(asked.clone()); + } + } + for given in given.iter() { + self.serving.drop_groups(given.clone(), err.clone()); + } + self.range_fills.refuse(groups, err); } // OK just resolves the range (the producer already orders // groups). @@ -10075,7 +10442,10 @@ impl ServeLoop { if !drained { // The groups are drained only for a reader: once nobody holds the // copy, a stream the publisher never finishes must not hold it. - if self.fetches.is_empty() && crate::util::poll_unwanted(&mut self.demand, waiter).is_ready() { + if self.fetches.is_empty() + && self.range_fills.is_empty() + && crate::util::poll_unwanted(&mut self.demand, waiter).is_ready() + { return Poll::Ready(ServeEnd::Idle); } return Poll::Pending; @@ -10106,7 +10476,10 @@ impl ServeLoop { { return Poll::Ready(ServeEnd::Finished); } - if self.fetches.is_empty() && crate::util::poll_unwanted(&mut self.demand, waiter).is_ready() { + if self.fetches.is_empty() + && self.range_fills.is_empty() + && crate::util::poll_unwanted(&mut self.demand, waiter).is_ready() + { return Poll::Ready(ServeEnd::Idle); } // The tail is still unsettled, so a session that dies now cuts the track @@ -10162,7 +10535,16 @@ impl ServeLoop { // subscription churn. match self.dynamic.poll_requested_fill(waiter) { Poll::Ready(Ok(fill)) => { - if self.supports_fetch { + // Lite-07 asks for more than one group through the subscription. One + // group, or a slice of one, is what FETCH carries, and a time lookup + // names no group yet. + let last = fill.end().saturating_sub(1); + let groups = fill.start().frame == 0 + && fill.time().is_none() && fill.start().group < last + && fill.group(last).frame_end().is_none(); + if serve.subscriber.version.has_ranges() && groups { + self.range_fills.push(fill); + } else if self.supports_fetch { self.fetches.push(FillServeRun::new( serve.clone(), fill, @@ -10206,7 +10588,8 @@ impl ServeLoop { && active.wanted != Some(wanted) { active.wanted = Some(wanted); - if let Err(err) = buffer_update(active) { + let ranges = active.live.unchanged(); + if let Err(err) = buffer_update(active, ranges) { return Poll::Ready(ServeEnd::GiveBack(err)); } continue; @@ -10218,9 +10601,30 @@ impl ServeLoop { let _ = self.fetches.poll(waiter); self.feed_fetches .poll(&self.sub, serve.subscriber.runtime.now(), waiter); + // A fill asking for other groups changes the subscription. + let accounted = match &self.sub { + Sub::Active(active) => Some(active.tail.clone()), + Sub::None => None, + }; + // A stream still arriving on a recovery is kept only for someone reading it. + let read = crate::util::poll_unwanted(&mut self.demand, waiter).is_pending(); + match self + .range_fills + .poll(serve, &self.serving, accounted.as_ref(), read, waiter) + { + Ok(true) => { + if let Err(err) = self.begin(serve, self.serving.subscription()) { + return Poll::Ready(ServeEnd::GiveBack(err)); + } + continue; + } + Ok(false) => {} + Err(err) => return Poll::Ready(ServeEnd::GiveBack(err)), + } - // A lite-07 track the publisher finished ends once its tail settles. - if self.poll_finishing(waiter).is_ready() { + // A lite-07 track the publisher finished ends once its tail settles, and once + // every recovery delivered what the tail no longer waits for: ending drops it. + if self.poll_finishing(waiter).is_ready() && !self.range_fills.recovering() { return Poll::Ready(ServeEnd::Finished); } @@ -10228,7 +10632,10 @@ impl ServeLoop { // went with the last subscriber, so it lingers, cache and all, for a // reader or fetch that asks again soon, then is dropped. In-flight // fetches keep it alive: work already accepted still gets finished. - if self.fetches.is_empty() && crate::util::poll_unwanted(&mut self.demand, waiter).is_ready() { + if self.fetches.is_empty() + && self.range_fills.is_empty() + && crate::util::poll_unwanted(&mut self.demand, waiter).is_ready() + { self.held = None; if self.linger.deadline().is_none() { let now = serve.subscriber.runtime.now(); diff --git a/rs/moq-net/src/lite/subscriber/fills.rs b/rs/moq-net/src/lite/subscriber/fills.rs new file mode 100644 index 00000000000..b02b06a6724 --- /dev/null +++ b/rs/moq-net/src/lite/subscriber/fills.rs @@ -0,0 +1,762 @@ +//! Fills a lite-07 copy asks of its upstream through a subscription rather than FETCH. + +use std::{ + collections::BTreeMap, + ops::Range, + task::{Poll, ready}, + time::Duration, +}; + +use rangemap::{RangeMap, RangeSet}; + +use super::{Establish, SubStream, Tail, TrackServe}; +use crate::{ + Error, StreamError, group, lite, + track::{self, Delay, Order, Position, Subscription}, +}; + +/// What a lite-07 copy's held fills ask of its upstream subscription, beside the demand. +#[derive(Clone, Debug, PartialEq)] +pub(super) struct FillAsk { + /// The groups to ask for, empty when no fill is held. + pub groups: RangeSet, + /// The most urgent priority a held fill asks with. + pub priority: i8, + /// The most tolerant budget a held fill asks with, `None` for the publisher's max age. + pub max_delay: Option, + pub order: Order, +} + +impl Default for FillAsk { + fn default() -> Self { + Self { + groups: Default::default(), + priority: i8::MIN, + max_delay: Some(Duration::ZERO), + order: Order::Ascending, + } + } +} + +impl FillAsk { + pub fn is_empty(&self) -> bool { + self.groups.is_empty() + } + + /// Count `fill`'s preferences in. + fn add(&mut self, fill: &track::Fill) { + self.priority = self.priority.max(fill.priority()); + self.max_delay = match (self.max_delay, fill.max_delay()) { + (Some(ours), Some(theirs)) => Some(ours.max(theirs)), + _ => None, + }; + if fill.order() == Order::Descending { + self.order = Order::Descending; + } + } + + /// The bounded ranges the groups take on the wire, each ending before a group's first + /// frame, as `wire_ranges` ends an absolute one. + pub fn wire(&self) -> impl Iterator + '_ { + self.groups.iter().map(|groups| lite::Range { + start: Some(lite::Anchor::Absolute(Position::group(groups.start))), + end: Some(lite::Anchor::Absolute(Position::group(groups.end))), + }) + } + + /// `demand` asked with the fills' priority too. The demand's budget caps every range, so + /// the live feed never loosens for a fill: a group it drops as stale is recovered on its + /// own (see [`Recovery`]). + pub fn merge(&self, demand: Subscription) -> Subscription { + let priority = demand.priority.max(self.priority); + demand.with_priority(priority) + } + + /// A subscription asking for the fills alone: no range of its own, so no floor. + pub fn subscription(&self) -> Subscription { + Subscription::default() + .with_ranges([]) + .with_priority(self.priority) + .with_delay(Delay::default().with_max(self.max_delay)) + .with_order(self.order) + } +} + +/// Fills a lite-07 upstream serves through the subscription rather than FETCH: each held +/// fill's next owed run joins the upstream ranges until its groups arrive or the publisher +/// names them given up, and the fill answers itself from the cache. +/// +/// A fill is judged against the subscription that asked for it alone. The track's arrivals +/// still account for a group an earlier subscription cut, which this one may still deliver, +/// so only what this subscription says it will not deliver (a SUBSCRIBE_DROP, or a range +/// start past the range) refuses a fill. A range start speaks only for its range: once the +/// range retires, a later one asking for the same groups may start lower. A group it drops as stale is officially dropped, +/// and one it already accounted for (delivered before and since evicted here) it never +/// sends again, so a [`Recovery`] asks for either on its own subscription with no max delay, +/// once. +/// +/// A group stays asked until its stream finishes, since narrowing the subscription under a +/// stream still arriving would reset it. +#[derive(Default)] +pub(super) struct RangeFills { + held: Vec, + /// What the upstream subscription asks for the fills now. + pub ask: FillAsk, + /// Asked groups no fill owes any more whose stream is still being read. + reading: BTreeMap, + /// Asked groups no fill owes any more, already searched for streams still being read. + draining: RangeSet, + /// Groups the subscription said it will not deliver, and why. + refused: RangeMap, + /// Groups below where a fill's range starts, by the asked run that range carries, which + /// stand only while that range is live. + skipped: RangeMap>, + /// Groups dropped as stale, which a [`Recovery`] has yet to ask for. + recover: RangeSet, + /// Groups a [`Recovery`] asks for now, which the subscription leaves out meanwhile. + recovering: RangeSet, + /// Groups already recovered once, which a second stale drop refuses. + recovered: RangeSet, + /// The subscriptions recovering them. + recoveries: Vec, +} + +impl RangeFills { + /// Whether no fill is held, asked for, or recovering. + pub fn is_empty(&self) -> bool { + self.held.is_empty() && self.ask.is_empty() && self.recoveries.is_empty() + } + + pub fn push(&mut self, fill: track::Fill) { + self.held.push(fill); + } + + /// The subscription will not deliver `groups`: a fill owing one is refused with `err`, + /// or recovered once when it was dropped as stale. + pub fn refuse(&mut self, groups: Range, err: StreamError) { + // Only what a fill asked for: a fill asks for nothing the subscription dropped + // before it. + let asked: Vec<_> = self + .ask + .groups + .overlapping(&groups) + .map(|run| run.start.max(groups.start)..run.end.min(groups.end)) + .collect(); + for groups in asked { + if err == StreamError::Old { + for fresh in self.recovered.gaps(&groups).collect::>() { + self.recover.insert(fresh); + } + // Recovered once already: this drop stands for the fills owing it now. + for again in self.recovered.overlapping(&groups).cloned().collect::>() { + let again = again.start.max(groups.start)..again.end.min(groups.end); + self.refused.insert(again, Refusal::dropped(err.clone())); + } + continue; + } + self.refused.insert(groups, Refusal::dropped(err.clone())); + } + } + + /// The subscription's range for the asked run `run` starts past `groups`, which it will not + /// deliver while that range is live. + pub fn skip(&mut self, run: Range, groups: Range) { + if !groups.is_empty() { + self.skipped.insert(groups, run); + } + } + + /// A fresh subscription may deliver what the last one refused. + pub fn forget_refused(&mut self) { + self.refused = Default::default(); + self.skipped = Default::default(); + } + + /// Whether a [`Recovery`] is still out, whose groups the subscription's tail does not wait + /// for. + pub fn recovering(&self) -> bool { + !self.recoveries.is_empty() + } + + /// Why the subscription will not deliver the run of groups from `first`, if it said so. + fn refusal(&self, first: u64) -> Option<(Range, Refusal)> { + if let Some((groups, refusal)) = self.refused.get_key_value(&first) { + return Some((groups.clone(), refusal.clone())); + } + // The subscriber's own range starting past them says nothing of the source. + let (groups, _) = self.skipped.get_key_value(&first)?; + Some((groups.clone(), Refusal::passing(StreamError::NotFound))) + } + + /// Settle each held fill against what the subscription refused, open a [`Recovery`] for + /// each run it will not deliver, and recompute what to ask. `accounted` is the tail of + /// the subscription the fills ride, if one is open, and `read` whether anyone still reads + /// the track. Returns whether the ask changed, or the protocol violation a recovery's + /// publisher committed. + pub fn poll( + &mut self, + serve: &TrackServe, + serving: &track::Producer, + accounted: Option<&kio::Producer>, + read: bool, + waiter: &kio::Waiter, + ) -> Result { + // The groups the subscription already accounted for, which it never sends again, are + // recovered on their own, once. + if let Some(accounted) = accounted { + let tail = accounted.read(); + let mut asked = RangeSet::new(); + for fill in &self.held { + if let Some(run) = fill.next_run() { + asked.insert(run); + } + } + for run in asked.iter() { + for gone in tail.accounted(run.clone()) { + for gap in self.recovering.gaps(&gone).collect::>() { + for again in self.recovered.overlapping(&gap).cloned().collect::>() { + let again = again.start.max(gap.start)..again.end.min(gap.end); + if self.refusal(again.start).is_none() { + // Recovered once for these fills; a later request may recover it again. + self.refused.insert(again, Refusal::passing(StreamError::NotFound)); + } + } + for fresh in self.recovered.gaps(&gap).collect::>() { + if self.refusal(fresh.start).is_none() { + self.recover.insert(fresh); + } + } + } + } + } + } + // Each stale run goes out on its own subscription with no max delay, once. + for run in std::mem::take(&mut self.recover).iter() { + self.recovered.insert(run.clone()); + self.recovering.insert(run.clone()); + self.recoveries + .push(Recovery::new(serve, serving, run.clone(), &self.ask)); + } + // What a finished recovery did not deliver stands refused. + let mut done = Vec::new(); + let mut violation = None; + self.recoveries.retain_mut(|recovery| match recovery.poll(waiter) { + Poll::Ready(Ok(refused)) => { + done.push((recovery.groups.clone(), refused)); + false + } + Poll::Ready(Err(err)) => { + violation.get_or_insert(err); + true + } + Poll::Pending => true, + }); + if let Some(err) = violation { + return Err(err); + } + for (groups, refused) in done { + self.recovering.remove(groups); + for (groups, refusal) in refused.iter() { + self.refused.insert(groups.clone(), refusal.clone()); + } + } + + let mut ask = FillAsk::default(); + // Every run a held fill still owes, recovered or not. + let mut owed = RangeSet::new(); + let mut held = std::mem::take(&mut self.held); + let this = &*self; + held.retain_mut(|fill| { + if fill.poll_withdrawn(waiter).is_ready() { + return false; + } + // Arms every input of the owed groups: readers, answers, and the cache. + let _ = fill.poll_wanted_changed(waiter); + loop { + let Some(run) = fill.next_run() else { + return false; + }; + // `next_run` extends from the group owed first, which the subscription may have + // refused already. Refusing the run answers it, so the loop moves on. + let first = match fill.order() { + Order::Ascending => run.start, + Order::Descending => run.end - 1, + }; + if let Some((groups, refusal)) = this.refusal(first) { + let groups = run.start.max(groups.start)..run.end.min(groups.end); + refusal.answer(fill, groups); + continue; + } + owed.insert(run.clone()); + // A run being recovered waits on its own subscription. + for gap in this.recovering.gaps(&run) { + ask.groups.insert(gap); + } + ask.add(fill); + return true; + } + }); + self.held = held; + + // A recovery no held fill owes any group of is cancelled, once none of its streams is + // still being read or nobody reads the track, and a group is recovered once for the + // fills owing it now: a later request for it may recover it again. + self.recoveries + .retain(|recovery| owed.overlaps(&recovery.groups) || (read && recovery.reading())); + let still: RangeSet = self.recoveries.iter().map(|recovery| recovery.groups.clone()).collect(); + self.recovering = still; + let recovered: Vec<_> = self.recovered.iter().cloned().collect(); + self.recovered = RangeSet::new(); + for run in recovered { + for kept in owed.overlapping(&run) { + self.recovered.insert(kept.start.max(run.start)..kept.end.min(run.end)); + } + } + + // A run stays asked whole while a fill still owes any of it, so the subscription + // changes once as a run is asked and once as it is done, and the streams of its + // answered groups keep arriving. One no fill owes stays while any of its streams is + // still being read, each group searched for once. + self.reading.retain(|_, group| group.poll_finished(waiter).is_pending()); + let edge = serving.consume(); + let mut groups = ask.groups.clone(); + let mut draining = RangeSet::new(); + for run in self.ask.groups.iter() { + if ask.groups.overlaps(run) { + groups.insert(run.clone()); + continue; + } + // Only the groups the cache holds, so a wide run costs what it holds. + for gap in self.draining.gaps(run) { + for sequence in edge.cached_groups(gap) { + if let Some(mut group) = edge.peek_group(sequence) + && group.poll_finished(waiter).is_pending() + { + self.reading.insert(sequence, group); + } + } + } + if self.reading.range(run.clone()).next().is_some() { + groups.insert(run.clone()); + draining.insert(run.clone()); + } + } + self.reading.retain(|sequence, _| draining.contains(sequence)); + self.draining = draining; + // What finishes a stream still asks with what its fills asked. + if self.held.is_empty() && !groups.is_empty() { + ask.priority = self.ask.priority; + ask.max_delay = self.ask.max_delay; + ask.order = self.ask.order; + } + ask.groups = groups; + if ask == self.ask { + return Ok(false); + } + self.retire_skipped(&ask.groups); + self.ask = ask; + Ok(true) + } + + /// Asking for `asked` from now on retires the range of each run not asked whole any more, + /// and what its start skipped with it. + fn retire_skipped(&mut self, asked: &RangeSet) { + let retired: Vec<_> = self + .skipped + .iter() + .filter(|(_, run)| asked.get(&run.start) != Some(*run)) + .map(|(groups, _)| groups.clone()) + .collect(); + for groups in retired { + self.skipped.remove(groups); + } + } +} + +/// Why the subscription that asked for groups will not deliver them. +#[derive(Clone, Debug, PartialEq, Eq)] +struct Refusal { + err: StreamError, + /// Whether the source no longer has them, which the track records so no reader asks + /// again: only a genuine miss, which its publisher named. Anything else answers only the + /// fills owing them now, since another request may still get them: a stale drop judges + /// a budget, and a range starting past them is the subscriber's own choice. + lost: bool, +} + +impl Refusal { + /// The publisher gave the groups up with `err` (a SUBSCRIBE_DROP, or an end before them): + /// lost only when it never had them. + fn dropped(err: StreamError) -> Self { + let lost = err == StreamError::NotFound; + Self { err, lost } + } + + /// Not delivered on this subscription, which says nothing of the source. + fn passing(err: StreamError) -> Self { + Self { err, lost: false } + } + + /// Answer `groups` of `fill`: a lost group is named dropped, as a FETCH's miss is, and + /// anything else is refused for now, recorded nowhere. + fn answer(&self, fill: &track::Fill, groups: Range) { + match &self.err { + StreamError::NotFound if self.lost => fill.drop_groups(groups), + StreamError::NotFound => fill.refuse_groups(groups, Error::NotFound), + StreamError::Old => fill.refuse_groups(groups, Error::Old), + err => fill.refuse_groups(groups, Error::Stream(err.clone())), + } + } +} + +/// A subscription asking again, with no max delay, for groups another one dropped as stale, +/// which a fill still owes. It lives until each group arrives and its stream is read, or the +/// publisher gives it up, then FINs and waits for the publisher's FIN in reply. +struct Recovery { + serve: TrackServe, + id: u64, + groups: Range, + state: RecoveryState, + /// The groups this subscription said it will not deliver, and why. + refused: RangeMap, + /// Whether SUBSCRIBE_START arrived. + started: bool, + /// Whether SUBSCRIBE_FINISH arrived. + ended: bool, + /// Whether our FIN went out. + finished: bool, +} + +enum RecoveryState { + Establish(Box), + Active(Box), + /// The subscription failed to open or was reset: everything left is refused. + Done, +} + +impl Recovery { + fn new(serve: &TrackServe, serving: &track::Producer, groups: Range, ask: &FillAsk) -> Self { + let subscription = Subscription::default() + .with_ranges([track::Range::groups(groups.clone())]) + .with_priority(ask.priority) + .with_delay(Delay::default().with_max(None)) + .with_order(Order::Ascending); + let ranges = vec![lite::Range { + start: Some(lite::Anchor::Absolute(Position::group(groups.start))), + end: Some(lite::Anchor::Absolute(Position::group(groups.end))), + }]; + let copy = super::Accepted { + producer: serving.clone(), + framing: lite::Framing::new(serving.info(), serve.subscriber.version), + }; + tracing::debug!(track = %serve.name, ?groups, "recovering stale groups"); + let establish = serve.register(subscription, ranges, Some(copy)); + Self { + serve: serve.clone(), + id: establish.id, + groups, + state: RecoveryState::Establish(Box::new(establish)), + refused: Default::default(), + started: false, + ended: false, + finished: false, + } + } + + /// Whether a stream of the subscription is still being read, which cancelling would cut. + fn reading(&self) -> bool { + match &self.state { + RecoveryState::Active(active) => active.tail.read().is_reading(), + _ => false, + } + } + + /// End the subscription, refusing what is left of the groups with `err`, as nothing more + /// will arrive on it. + fn end(&mut self, err: StreamError) { + let state = std::mem::replace(&mut self.state, RecoveryState::Done); + let mut owed = RangeSet::new(); + owed.insert(self.groups.clone()); + if let RecoveryState::Active(active) = &state { + for arrived in active.tail.read().accounted(self.groups.clone()) { + owed.remove(arrived); + } + } + for (refused, _) in self.refused.iter() { + owed.remove(refused.clone()); + } + for gap in owed.iter() { + self.refused.insert(gap.clone(), Refusal::passing(err.clone())); + } + self.serve.subscriber.remove_subscribe(self.id); + match state { + RecoveryState::Establish(est) => est.abort(&Error::Cancel), + RecoveryState::Active(active) if !self.finished => active.stream.writer.abort(&Error::Cancel), + _ => {} + } + } + + /// Ready once the recovery is over, with the groups it refused, or with the protocol + /// violation its publisher committed, which the subscription's would be too. + fn poll(&mut self, waiter: &kio::Waiter) -> Poll, Error>> { + loop { + match &mut self.state { + RecoveryState::Establish(est) => match ready!(est.poll(waiter)) { + Ok(active) => self.state = RecoveryState::Active(Box::new(active)), + Err(err) => { + tracing::debug!(%err, "recovery subscribe failed"); + self.end(StreamError::from(&err)); + } + }, + RecoveryState::Active(active) => { + // FIN once every group is accounted for and read, as a subscriber does once + // its tail settles, and the publisher FINs in reply. + if !self.finished { + let groups = self.groups.clone(); + let settled = active + .tail + .consume() + .poll(waiter, |tail| match tail.covers(groups.clone()) && !tail.is_reading() { + true => Poll::Ready(()), + false => Poll::Pending, + }) + .is_ready(); + if settled { + let _ = active.stream.writer.finish(); + self.finished = true; + } + } + if let Poll::Ready(Err(err)) = active.stream.writer.poll_flush(waiter) { + tracing::debug!(%err, "recovery subscribe write failed"); + } + let res = ready!( + active + .stream + .reader + .poll_decode_maybe::(waiter) + ); + let now = self.serve.subscriber.runtime.now(); + let version = self.serve.subscriber.version; + let groups = self.groups.clone(); + // What the publisher gives up of the groups, accounted for in the tail. + let mut given_up = |given: Range, refusal: Refusal| { + if let Ok(mut tail) = active.tail.write() { + tail.account(given.clone(), now); + } + let given = given.start.max(groups.start)..given.end.min(groups.end); + if !given.is_empty() { + self.refused.insert(given, refusal); + } + }; + match res { + // One start, for its one range. + Ok(Some(lite::SubscribeResponse::Start(start))) if !self.started && start.starts.len() == 1 => { + self.started = true; + // Below its start the publisher delivers nothing. + let from = start.starts[0].unwrap_or(groups.end); + given_up( + groups.start..from.clamp(groups.start, groups.end), + Refusal::passing(StreamError::NotFound), + ); + } + Ok(Some(lite::SubscribeResponse::Drop(dropped))) => { + given_up( + dropped.start..dropped.end.saturating_add(1), + Refusal::dropped(dropped.error(version)), + ); + } + // Nothing at or past the track's end will be produced. + Ok(Some(lite::SubscribeResponse::Finish(finish))) if !self.ended => { + self.ended = true; + given_up( + finish.group.max(groups.start)..groups.end, + Refusal::dropped(StreamError::NotFound), + ); + } + Ok(Some(lite::SubscribeResponse::Ok(_))) => {} + // A second START or FINISH, or a STARTED for a range no update added. + Ok(Some(msg)) => { + tracing::warn!(?msg, "unexpected recovery subscribe response"); + return Poll::Ready(Err(Error::ProtocolViolation)); + } + // The publisher FINs in reply to ours, or once it finished the track. + Ok(None) if self.finished || self.ended => self.end(StreamError::NotFound), + Ok(None) => { + tracing::warn!("recovery subscribe ended without SUBSCRIBE_FINISH"); + return Poll::Ready(Err(Error::ProtocolViolation)); + } + Err(err) => { + // Malformed bytes are a violation, as on the subscription itself; a + // reset gives up what is left with its reason. + let err = match err { + Error::Decode(crate::DecodeError::Short) => err, + err => crate::auth::protocol_error(err), + }; + if matches!(err, Error::ProtocolViolation) { + return Poll::Ready(Err(err)); + } + tracing::debug!(%err, "recovery subscribe ended"); + self.end(StreamError::from(&err)); + } + } + } + RecoveryState::Done => return Poll::Ready(Ok(std::mem::take(&mut self.refused))), + } + } + } +} + +impl Drop for Recovery { + fn drop(&mut self) { + if !matches!(self.state, RecoveryState::Done) { + self.end(StreamError::Cancel); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// A stale drop of groups a fill asked for is recovered once; a second one stands, and a + /// drop of groups no fill asked for refuses nothing. + #[test] + #[expect(clippy::single_range_in_vec_init, reason = "the recovered runs are a list, here one")] + fn a_stale_drop_is_recovered_once() { + let mut fills = RangeFills::default(); + fills.ask.groups.insert(2..6); + fills.refuse(3..5, StreamError::Old); + assert_eq!(fills.recover.iter().cloned().collect::>(), [3..5]); + assert!(fills.refused.is_empty(), "recovered, not refused"); + + // Once recovered, the same groups dropped stale again stay dropped. + fills.recovered.insert(3..5); + fills.recover = Default::default(); + fills.refuse(2..5, StreamError::Old); + assert_eq!(fills.recover.iter().cloned().collect::>(), [2..3]); + assert_eq!(fills.refused.get(&3), Some(&Refusal::dropped(StreamError::Old))); + assert!(!fills.refused.get(&3).unwrap().lost, "a stale drop is never a loss"); + + fills.refuse(7..9, StreamError::NotFound); + assert_eq!(fills.refused.get(&7), None, "no fill asked for it"); + fills.refuse(5..6, StreamError::NotFound); + assert_eq!(fills.refused.get(&5), Some(&Refusal::dropped(StreamError::NotFound))); + assert!(fills.refused.get(&5).unwrap().lost, "the publisher never had it"); + } + + /// A track whose cache evicted groups 1..4, with a reader of them whose fill is the next + /// request. + fn missed() -> (track::Producer, track::Requests, track::Ordered, track::Fill) { + let producer = track::Producer::new( + std::sync::Arc::new(crate::broadcast::Info::default()), + "test", + track::Info::default() + .with_timescale(crate::Timescale::MILLI) + .with_max_age(None), + ); + let requests = producer.requests(); + for sequence in 0..6 { + let mut group = producer.create_group(group::Info { sequence }).unwrap(); + group + .write_frame(crate::Timestamp::from_millis(sequence * 1000).unwrap(), b"x".as_slice()) + .unwrap(); + group.finish().unwrap(); + } + for sequence in 1..4 { + producer.evict(sequence); + } + let (reader, fill) = ask(&producer, &requests); + (producer, requests, reader, fill) + } + + /// A reader of groups 1..4, and the fill it asks for. + fn ask(producer: &track::Producer, requests: &track::Requests) -> (track::Ordered, track::Fill) { + let waiter = kio::Waiter::noop(); + let mut reader = producer + .subscribe( + Subscription::default() + .with_delay(Delay::default().with_max(None)) + .with_range(1..4), + ) + .ordered(); + assert!(reader.poll_next_group(&waiter).is_pending(), "groups 1..4 are owed"); + let Poll::Ready(Ok(fill)) = requests.poll_requested_fill(&waiter) else { + panic!("groups 1..4 are a miss"); + }; + (reader, fill) + } + + /// A range starting past what a fill asked is the subscriber's own choice: the fill's + /// readers stop waiting, but nothing is recorded, so once a lowered start reaches the + /// groups a later reader asks again and gets them. A genuine miss stays dropped. + #[test] + fn a_skipped_start_is_never_a_loss() { + let waiter = kio::Waiter::noop(); + let (producer, requests, mut reader, fill) = missed(); + Refusal::passing(StreamError::NotFound).answer(&fill, 1..4); + assert!( + reader.poll_next_group(&waiter).is_ready(), + "the reader stops waiting for the skipped groups" + ); + drop((reader, fill)); + + // The lowered start asks for them again, and they arrive. + let (mut reader, fill) = ask(&producer, &requests); + for sequence in 1..4 { + let mut group = fill.accept(sequence, None).unwrap(); + group + .write_frame(crate::Timestamp::from_millis(sequence * 1000).unwrap(), b"x".as_slice()) + .unwrap(); + group.finish().unwrap(); + } + for sequence in 1..4 { + let Poll::Ready(Ok(Some(group))) = reader.poll_next_group(&waiter) else { + panic!("group {sequence} was delivered"); + }; + assert_eq!(group.sequence, sequence); + } + + // The publisher naming them not found is a loss: nobody asks again. + let (producer, requests, reader, fill) = missed(); + Refusal::dropped(StreamError::NotFound).answer(&fill, 1..4); + drop((reader, fill)); + let mut reader = producer + .subscribe( + Subscription::default() + .with_delay(Delay::default().with_max(None)) + .with_range(1..4), + ) + .ordered(); + let _ = reader.poll_next_group(&waiter); + assert!( + requests.poll_requested_fill(&waiter).is_pending(), + "a lost group is not asked again" + ); + } + + /// A range starting past what it asked refuses those groups only while that range is live: + /// a later range asking for them again may start lower. + #[test] + fn a_skipped_start_stands_only_while_its_range_does() { + let mut fills = RangeFills::default(); + fills.ask.groups.insert(2..6); + fills.ask.groups.insert(8..10); + fills.skip(2..6, 2..4); + fills.skip(8..10, 8..9); + assert_eq!(fills.refusal(3), Some((2..4, Refusal::passing(StreamError::NotFound)))); + + // Asking 2..6 whole keeps its range; asking 8..12 retires the range for 8..10. + let mut asked = RangeSet::new(); + asked.insert(2..6); + asked.insert(8..12); + fills.retire_skipped(&asked); + assert_eq!(fills.refusal(3), Some((2..4, Refusal::passing(StreamError::NotFound)))); + assert_eq!(fills.refusal(8), None, "the new range says where it starts"); + + // A drop stands whatever is asked. + fills.refuse(4..5, StreamError::NotFound); + fills.retire_skipped(&RangeSet::new()); + assert_eq!(fills.refusal(3), None); + assert_eq!(fills.refusal(4), Some((4..5, Refusal::dropped(StreamError::NotFound)))); + } +} diff --git a/rs/moq-net/src/model/resume.rs b/rs/moq-net/src/model/resume.rs index 49c9b35ca6e..0baf8b7f886 100644 --- a/rs/moq-net/src/model/resume.rs +++ b/rs/moq-net/src/model/resume.rs @@ -871,6 +871,11 @@ impl Subscriber { self.reader().window().starts(from) } + /// Each of the reader's ranges resolved; see [`track::Subscriber::resolved`]. + pub(super) fn resolved(&self) -> Vec> { + self.reader().window().resolved() + } + /// The lowest group at or above `from` the reader's ranges cover; see /// [`track::Subscriber::next_in_range`]. pub(super) fn next_in_range(&self, from: u64) -> Option { diff --git a/rs/moq-net/src/model/subscription.rs b/rs/moq-net/src/model/subscription.rs index c0d61d143af..a2e52d9ecf7 100644 --- a/rs/moq-net/src/model/subscription.rs +++ b/rs/moq-net/src/model/subscription.rs @@ -9,7 +9,8 @@ mod runs; mod window; pub(super) use runs::{Layered, Runs}; pub(crate) use window::Accounted; -pub(super) use window::{Containing, Held, Holdings, Resolved, Seen, Unheld, Window, later}; +pub(crate) use window::Resolved; +pub(super) use window::{Containing, Held, Holdings, Seen, Unheld, Window, later}; /// Subscriber-side preferences for receiving a track. /// diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index 1620d8e9858..9c9d45bed1c 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -16,9 +16,12 @@ use crate::{Error, Result, Timestamp}; /// One range resolved to groups: from `start` up to the exclusive `end`, `None` while it /// stays open. #[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub(in crate::model) struct Resolved { +pub(crate) struct Resolved { pub start: u64, pub end: Option, + /// Whether the start names a group, rather than reaching back to the oldest one (a + /// time start older than every group held, or the oldest start). + pub floored: bool, } /// The groups a publisher accounted for, which [`Window::covered_by`] checks against: a @@ -855,12 +858,22 @@ impl Window { } } - /// Each range resolved, in the order given: `None` for one empty as given, whose start - /// still waits, or still looking up its start. One that only resolves empty keeps its - /// span, ending at or below its start. An unsettled time end is reported open, never at - /// its cap. + /// Each range resolved, in the order given: `None` for one whose start still waits, or + /// still looking up its start. One empty as given, or that only resolves empty, ends at + /// or below its start. An unsettled time end is reported open, never at its cap. pub fn resolved(&self) -> Vec> { - let mut resolved = vec![None; self.ranges.len()]; + let mut resolved: Vec<_> = self + .ranges + .iter() + .map(|range| match (range.start, range.end) { + (Anchor::Absolute(start), Some(Anchor::Absolute(end))) if range.is_empty() => Some(Resolved { + start, + end: Some(end), + floored: true, + }), + _ => None, + }) + .collect(); for span in self.spans.iter().filter(|span| span.looking.is_none() && !span.waiting) { resolved[span.index] = Some(Resolved { start: span.start, @@ -868,6 +881,7 @@ impl Window { End::Before(end) => Some(end), End::Open | End::Time(_) => None, }, + floored: span.floored, }); } resolved @@ -1450,8 +1464,16 @@ mod tests { assert_eq!( window.resolved(), vec![ - Some(Resolved { start: 2, end: Some(4) }), - Some(Resolved { start: 7, end: None }), + Some(Resolved { + start: 2, + end: Some(4), + floored: true + }), + Some(Resolved { + start: 7, + end: None, + floored: true + }), ] ); // Delivery starting inside a range raises its start; past its end leaves it none. @@ -1510,13 +1532,27 @@ mod tests { let window = Window::new(&[range(time(3500), time(4500))], Some(6), &groups).unwrap(); assert_eq!(covered(&window, 8), vec![3, 4]); assert!(window.is_settled()); - assert_eq!(window.resolved(), vec![Some(Resolved { start: 3, end: Some(5) })]); + assert_eq!( + window.resolved(), + vec![Some(Resolved { + start: 3, + end: Some(5), + floored: true + })] + ); // Group 6 may yet start at 5000 too: read on speculation, the end left open. let window = Window::new(&[range(time(3500), time(5000))], Some(6), &groups).unwrap(); assert!(!window.is_settled()); assert_eq!(covered(&window, 9), vec![3, 4, 5, 6]); - assert_eq!(window.resolved(), vec![Some(Resolved { start: 3, end: None })]); + assert_eq!( + window.resolved(), + vec![Some(Resolved { + start: 3, + end: None, + floored: true + })] + ); assert_eq!(window.end(), None, "the cap is not the end"); assert_eq!(window.speculative().collect::>(), vec![6]); @@ -1657,13 +1693,15 @@ mod tests { assert!(resolved[..split].iter().all(|r| *r == Some(Resolved { start: 0, - end: Some(count) + end: Some(count), + floored: true, + }))); + assert!(resolved[split..].iter().all(|r| *r + == Some(Resolved { + start: 0, + end: None, + floored: true }))); - assert!( - resolved[split..] - .iter() - .all(|r| *r == Some(Resolved { start: 0, end: None })) - ); // Once a group starts past every end, they all settle. groups.held.insert(count + 1, Held::Started(at(2_000_000))); @@ -1847,7 +1885,14 @@ mod tests { asked: Cell::new(false), }; let window = Window::new(&[range(Anchor::Absolute(0), time(5000))], None, &racing).unwrap(); - assert_eq!(window.resolved(), vec![Some(Resolved { start: 0, end: Some(1) })]); + assert_eq!( + window.resolved(), + vec![Some(Resolved { + start: 0, + end: Some(1), + floored: true + })] + ); } #[test] @@ -1864,7 +1909,14 @@ mod tests { assert_eq!(covered(&window, 5), vec![0, 1], "2 waits on 1 starting"); groups.held.insert(1, Held::Started(at(3600))); window.settle(Some(3), &groups); - assert_eq!(window.resolved(), vec![Some(Resolved { start: 0, end: Some(1) })]); + assert_eq!( + window.resolved(), + vec![Some(Resolved { + start: 0, + end: Some(1), + floored: true + })] + ); } #[test] @@ -1875,7 +1927,8 @@ mod tests { window.resolved(), vec![Some(Resolved { start: u64::MAX, - end: None + end: None, + floored: true, })] ); assert!(window.contains(u64::MAX)); @@ -1903,7 +1956,8 @@ mod tests { None, Some(Resolved { start: 20, - end: Some(21) + end: Some(21), + floored: true, }) ] ); @@ -1914,12 +1968,23 @@ mod tests { assert!(window.settle(Some(9), &groups)); assert_eq!( window.resolved()[0], - Some(Resolved { start: 0, end: None }), + Some(Resolved { + start: 0, + end: None, + floored: false + }), "3 starts past it" ); groups.held.insert(2, Held::Started(at(2000))); let window = Window::new(&ranges, Some(9), &groups).unwrap(); - assert_eq!(window.resolved()[0], Some(Resolved { start: 2, end: None })); + assert_eq!( + window.resolved()[0], + Some(Resolved { + start: 2, + end: None, + floored: true + }) + ); assert!(window.is_settled()); let groups = Groups { @@ -1927,7 +1992,14 @@ mod tests { ..Groups::new(pending) }; let window = Window::new(&ranges, Some(9), &groups).unwrap(); - assert_eq!(window.resolved()[0], Some(Resolved { start: 0, end: None })); + assert_eq!( + window.resolved()[0], + Some(Resolved { + start: 0, + end: None, + floored: false + }) + ); } /// A relative end counts back from the latest group it was asked against, however many @@ -1951,7 +2023,8 @@ mod tests { window.resolved()[0], Some(Resolved { start: 2, - end: Some(10) + end: Some(10), + floored: true, }) ); } @@ -2032,7 +2105,14 @@ mod tests { assert!(!window.is_settled() && !window.contains(9) && window.resolved() == [None]); groups.held.insert(8, Held::Started(at(8000))); assert!(window.found(at(8500), Some(8), &groups)); - assert_eq!(window.resolved(), [Some(Resolved { start: 8, end: None })]); + assert_eq!( + window.resolved(), + [Some(Resolved { + start: 8, + end: None, + floored: true + })] + ); assert!(window.is_settled()); let window = Window::new(&[range(time(8500), None)], Some(9), &groups).unwrap(); @@ -2041,7 +2121,14 @@ mod tests { let mut window = Window::new(&[range(time(500), None)], Some(9), &groups).unwrap(); assert_eq!(window.lookups().collect::>(), [(at(500), 4)]); assert!(window.found(at(500), None, &groups)); - assert_eq!(window.resolved(), [Some(Resolved { start: 0, end: None })]); + assert_eq!( + window.resolved(), + [Some(Resolved { + start: 0, + end: None, + floored: false + })] + ); assert!(window.contains(4) && !window.missable(0).any(|run| *run.start() < 4)); } @@ -2055,7 +2142,14 @@ mod tests { assert_eq!(window.lookups().collect::>(), [(at(500), 4)]); assert!(window.found(at(500), None, &groups)); assert!(window.is_settled()); - assert_eq!(window.resolved(), [Some(Resolved { start: 0, end: Some(4) })]); + assert_eq!( + window.resolved(), + [Some(Resolved { + start: 0, + end: Some(4), + floored: false + })] + ); } /// Without a handler serving the history, the groups held decide alone. @@ -2067,8 +2161,16 @@ mod tests { assert_eq!( window.resolved(), [ - Some(Resolved { start: 4, end: None }), - Some(Resolved { start: 0, end: None }), + Some(Resolved { + start: 4, + end: None, + floored: true + }), + Some(Resolved { + start: 0, + end: None, + floored: false + }), ] ); } diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 431ea0aa4d6..c7689bf2035 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -22,7 +22,8 @@ use super::idle; use super::skipped::Skipped; use crate::ledger::{self, Ledger}; -use super::subscription::{Accounted, Containing, Held, Holdings, Layered, Resolved, Runs, Seen, Window, later}; +pub(crate) use super::subscription::Resolved; +use super::subscription::{Accounted, Containing, Held, Holdings, Layered, Runs, Seen, Window, later}; pub use super::subscription::{Anchor, Delay, Order, Position, Range, Subscription}; use rangemap::{RangeInclusiveSet, RangeMap, RangeSet}; @@ -3706,6 +3707,12 @@ impl Producer { self.state.read().start_sequence } + /// Evict group `sequence` from the cache, as expiry does. + #[cfg(test)] + pub(crate) fn evict(&self, sequence: u64) { + self.modify().unwrap().evict_group(sequence, Error::Evicted); + } + /// The exclusive end, once [`Self::finish`] or [`Self::finish_at`] declared one. /// /// `None` while the track is still open ended. Both methods reject a second end, so @@ -8503,6 +8510,21 @@ impl Subscriber { } } + /// Each of this subscriber's ranges as resolved here, by index: its first group and + /// exclusive end, `None` while open, or `None` for one whose start still waits on the + /// live feed or a lookup. One empty as given, or that only resolves empty, ends at or + /// below its start. + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + pub(crate) fn resolved(&mut self) -> Vec> { + match &mut self.inner { + Inner::Plain(cursor) => { + cursor.sync(); + cursor.filter.window.resolved() + } + Inner::Resume(resume, _) => resume.resolved(), + } + } + /// The lowest group at or above `from` this subscriber's ranges and /// [`Self::with_range`] floor cover, `None` when none does: the groups between are /// never read, so a reader waiting on each group in sequence need not wait for them. @@ -22121,7 +22143,11 @@ mod test { let mut subscriber = producer.subscribe(replay().with_ranges(ranges)); assert_eq!( subscriber.resolved_ranges(), - vec![Some(Resolved { start: 0, end: None })], + vec![Some(Resolved { + start: 0, + end: None, + floored: true + })], "group 1 may still start before the end" ); if dropped { @@ -22129,7 +22155,11 @@ mod test { assert_eq!(drain(&mut subscriber), vec![0]); assert_eq!( subscriber.resolved_ranges(), - vec![Some(Resolved { start: 0, end: Some(2) })], + vec![Some(Resolved { + start: 0, + end: Some(2), + floored: true + })], "the drop settles the end" ); continue; @@ -22169,7 +22199,11 @@ mod test { assert_eq!(reader.drain(), Vec::::new(), "ordered: {ordered}"); assert_eq!( reader.resolved(), - vec![Some(Resolved { start: 0, end: Some(1) })], + vec![Some(Resolved { + start: 0, + end: Some(1), + floored: true + })], "1 starting past the end settles it" ); } @@ -22219,7 +22253,11 @@ mod test { .unwrap(); assert_eq!( subscriber.resolved_ranges(), - vec![Some(Resolved { start: 0, end: Some(3) })] + vec![Some(Resolved { + start: 0, + end: Some(3), + floored: true + })] ); } diff --git a/rs/moq-net/src/tail.rs b/rs/moq-net/src/tail.rs index 4eeb6532792..05f2f1600cc 100644 --- a/rs/moq-net/src/tail.rs +++ b/rs/moq-net/src/tail.rs @@ -86,6 +86,11 @@ impl Tail { self.ledger.covers(groups) } + /// See [`Ledger::accounted`]. + pub fn accounted(&self, groups: Range) -> impl Iterator> + '_ { + self.ledger.accounted(groups) + } + /// See [`Ledger::folds_at`]. #[cfg(test)] pub fn folds_at(&self, group: u64) -> Option { diff --git a/rs/moq-net/tests/integration/main.rs b/rs/moq-net/tests/integration/main.rs index d521ca74ed8..fa361127848 100644 --- a/rs/moq-net/tests/integration/main.rs +++ b/rs/moq-net/tests/integration/main.rs @@ -46,6 +46,7 @@ mod multi_cdn; mod partial_group; mod quiet_catalog; mod range_anchors; +mod range_fill; mod range_read; mod received_rewind; mod rejoin; diff --git a/rs/moq-net/tests/integration/range_fill.rs b/rs/moq-net/tests/integration/range_fill.rs new file mode 100644 index 00000000000..b31ba7bf89c --- /dev/null +++ b/rs/moq-net/tests/integration/range_fill.rs @@ -0,0 +1,196 @@ +#![cfg(feature = "async")] + +//! A copy fills a miss of more than one group over moq-lite-07 through a subscription rather +//! than FETCH, and recovers on a SUBSCRIBE of its own what its live subscription will never +//! send again. + +use crate::support; + +use std::collections::BTreeSet; +use std::time::Duration; + +use moq_net::track::{Delay, Info, Range, Subscription}; +use moq_net::{Hop, Timescale, Timestamp, Version, cache, kio}; +use support::harness::{MockConnectOptions, MockPair, connect_mock}; + +const TIMEOUT: Duration = Duration::from_secs(10); + +/// SUBSCRIBE's and FETCH's lite stream types, each one varint byte. +const SUBSCRIBE: u8 = 2; +const FETCH: u8 = 3; + +fn origin(hop: u64, pool: Option) -> moq_net::origin::Producer { + let mut config = moq_net::origin::Config::new(Hop::new(hop).unwrap()); + if let Some(pool) = pool { + config.pool = pool; + } + let (producer, driver) = moq_net::origin::Producer::new(config); + support::harness::spawn(driver); + producer +} + +/// The publisher's groups, one per second, with no publisher max age. +struct Fixture { + track: moq_net::track::Producer, + remote: moq_net::broadcast::Consumer, + pair: MockPair, + _broadcast: moq_net::origin::Publication, + _origins: (moq_net::origin::Producer, moq_net::origin::Producer), +} + +impl Fixture { + /// A lite-07 publisher holding `groups` for good, and a copy whose cache expires them. + async fn new(groups: u64) -> Self { + let publisher = origin(1, Some(cache::Pool::unbounded())); + let broadcast = publisher.create_broadcast("bcast").unwrap(); + let track = broadcast + .create_track( + "video", + Info::default().with_timescale(Timescale::MILLI).with_max_age(None), + ) + .unwrap(); + broadcast.announce(Default::default()).unwrap(); + for sequence in 0..groups { + let mut group = track.create_group(moq_net::group::Info { sequence }).unwrap(); + group + .write_frame(Timestamp::from_millis(sequence * 1000).unwrap(), b"x".as_slice()) + .unwrap(); + group.finish().unwrap(); + } + + let copy = origin(2, None); + let mut options = MockConnectOptions::new("moq-lite-07-wip".parse::().unwrap()); + options.server_publish = Some(publisher.consume()); + options.client_subscribe = Some(copy.clone()); + let pair = connect_mock(options).await; + let remote = copy.consume().follow("bcast", None).unwrap().available().await.unwrap(); + Self { + track, + remote, + pair, + _broadcast: broadcast, + _origins: (publisher, copy), + } + } + + /// How many streams of `kind` the copy opened upstream. + fn opened(&self, kind: u8) -> usize { + self.pair + .client_transport + .bidi_types() + .into_iter() + .filter(|opened| *opened == kind) + .count() + } + + /// A reader with no budget, of `ranges`. + async fn subscribe(&self, ranges: impl IntoIterator) -> moq_net::track::Subscriber { + self.remote + .track("video") + .unwrap() + .subscribe( + Subscription::default() + .with_delay(Delay::default().with_max(None)) + .with_ranges(ranges), + ) + .await + .unwrap() + } +} + +/// The sequences of the next `count` groups `reader` receives. +async fn receive(reader: &mut moq_net::track::Subscriber, count: usize) -> BTreeSet { + let mut got = BTreeSet::new(); + while got.len() < count { + let group = kio::clock::timeout(TIMEOUT, reader.recv_group()) + .await + .expect("group timeout") + .unwrap() + .expect("a group"); + got.insert(group.sequence); + } + got +} + +/// History the live subscription already delivered, and the copy's cache has since evicted, +/// is never sent again on that subscription. A reader reaching back for it gets it from one +/// SUBSCRIBE of its own, not a FETCH per group. +#[kio::test] +async fn evicted_history_is_recovered_once_on_its_own_subscription() { + kio::clock::timeout(TIMEOUT + cache::DEFAULT_EXPIRY * 6, async { + let fixture = Fixture::new(6).await; + let mut first = fixture.subscribe([Range::oldest()]).await; + assert_eq!(receive(&mut first, 6).await, (0..6).collect()); + + // Superseded, the copy expires them, while the reader keeps the subscription live. + kio::clock::sleep(cache::DEFAULT_EXPIRY * 2 + Duration::from_secs(2)).await; + let subscribes = fixture.opened(SUBSCRIBE); + let mut second = fixture.subscribe([Range::groups(1..4)]).await; + assert_eq!(receive(&mut second, 3).await, (1..4).collect()); + assert_eq!(fixture.opened(SUBSCRIBE), subscribes + 1, "recovered on one SUBSCRIBE"); + assert_eq!(fixture.opened(FETCH), 0, "nothing fetched"); + + // The live subscription goes on. + let mut group = fixture + .track + .create_group(moq_net::group::Info { sequence: 6 }) + .unwrap(); + group + .write_frame(Timestamp::from_millis(6000).unwrap(), b"x".as_slice()) + .unwrap(); + group.finish().unwrap(); + assert_eq!(receive(&mut first, 1).await, [6].into()); + assert_eq!(fixture.opened(SUBSCRIBE), subscribes + 1, "recovered once"); + + // Expired again, the history is recovered again for a later reader. + drop(second); + kio::clock::sleep(cache::DEFAULT_EXPIRY * 2 + Duration::from_secs(2)).await; + let mut third = fixture.subscribe([Range::groups(1..4)]).await; + assert_eq!(receive(&mut third, 3).await, (1..4).collect()); + assert_eq!( + fixture.opened(SUBSCRIBE), + subscribes + 2, + "recovered for the later reader" + ); + }) + .await + .expect("timed out"); +} + +/// The track finishing while a recovery is still out doesn't cut it: the copy ends only once +/// the recovered groups arrived, which the live subscription's tail never waits for. +#[kio::test] +async fn a_recovery_outlives_the_track_finishing() { + kio::clock::timeout(TIMEOUT + cache::DEFAULT_EXPIRY * 3, async { + let fixture = Fixture::new(6).await; + let mut first = fixture.subscribe([Range::oldest()]).await; + assert_eq!(receive(&mut first, 6).await, (0..6).collect()); + + // Superseded, the copy expires them, while the reader keeps the subscription live. + kio::clock::sleep(cache::DEFAULT_EXPIRY * 2 + Duration::from_secs(2)).await; + let mut second = fixture.subscribe([Range::groups(1..4)]).await; + fixture.track.finish().unwrap(); + assert_eq!(receive(&mut second, 3).await, (1..4).collect()); + }) + .await + .expect("timed out"); +} + +/// One group, or a slice of one, is what lite-07 FETCH carries, so a miss of one group still +/// fetches it. +#[kio::test] +async fn a_one_group_miss_still_fetches() { + kio::clock::timeout(TIMEOUT, async { + let fixture = Fixture::new(3).await; + let mut group = kio::clock::timeout(TIMEOUT, fixture.remote.track("video").unwrap().fetch_group(1, None)) + .await + .expect("fetch timeout") + .expect("fetched"); + let frame = group.read_frame().await.unwrap().expect("a frame"); + assert_eq!(frame.payload.as_ref(), b"x"); + assert_eq!(fixture.opened(FETCH), 1); + assert_eq!(fixture.opened(SUBSCRIBE), 0, "no subscription for one group"); + }) + .await + .expect("timed out"); +} diff --git a/rs/moq-net/tests/integration/widened_max_age.rs b/rs/moq-net/tests/integration/widened_max_age.rs index def074c3d6e..b21e8b9273e 100644 --- a/rs/moq-net/tests/integration/widened_max_age.rs +++ b/rs/moq-net/tests/integration/widened_max_age.rs @@ -31,8 +31,8 @@ const WIDE: Duration = Duration::from_millis(6500); /// start at the live edge, below which a reader fills only the floor its ranges name. const VERSIONS: &[&str] = &["moq-lite-06", "moq-lite-07-wip"]; -/// The wire for the tests on one version: only lite-07 FETCHes below the start, so only it -/// can pull a group twice. +/// The wire for the tests on one version: only lite-07 fills below the start, so only it can +/// pull a group twice. const VERSION: &str = "moq-lite-07-wip"; fn origin(hop: u64) -> origin::Producer { @@ -159,8 +159,9 @@ async fn a_fresh_reader_fills_nothing_below_its_start() { } /// Tighten while no start is selectable, then ask for a budget between the old and -/// selected ones. Hold the publisher's driver so only an actual fill request, rather -/// than its live re-offer, can prove the copy left the history below its start fillable. +/// selected ones: the copy leaves the history below its start fillable rather than giving +/// it up. Lite-07 asks for it as a range on the subscription the publisher's re-offer +/// serves too, so only the groups the reader gets tell the two apart from a give-up. #[kio::test] async fn a_tightened_start_leaves_history_fillable_by_an_intermediate_reader() { for relay in [false, true] { @@ -180,7 +181,6 @@ async fn a_tightened_start_leaves_history_fillable_by_an_intermediate_reader() { assert_eq!(received(&mut first).await, (6..GROUPS).collect()); *chain.source_gate.write().ok().unwrap() = false; - let before = chain.fetches(); let mut intermediate = chain.subscribe(Duration::from_millis(4500)).await; // Drive the reader's miss walk too, with the upstream deliberately parked. let reading = kio::clock::sim::spawn(async move { @@ -190,13 +190,12 @@ async fn a_tightened_start_leaves_history_fillable_by_an_intermediate_reader() { } got }); - let upstream = &chain.links[0].client_transport; - kio::clock::timeout(TIMEOUT, upstream.bidi_count(3, before + 1)) + *chain.source_gate.write().ok().unwrap() = true; + let got = kio::clock::timeout(TIMEOUT, reading) .await .expect("the history below the start is fillable"); - *chain.source_gate.write().ok().unwrap() = true; - // This reader can also receive the feed's re-offer once the source resumes. - assert_eq!(reading.await.unwrap(), (4..GROUPS).collect(), "relay={relay}"); + assert_eq!(got.unwrap(), (4..GROUPS).collect(), "relay={relay}"); + assert_eq!(chain.fetches(), 0, "relay={relay}: the history rides the subscription"); } } diff --git a/test/interop/bare-fin.ts b/test/interop/bare-fin.ts index 3550e6f371c..d207829e699 100644 --- a/test/interop/bare-fin.ts +++ b/test/interop/bare-fin.ts @@ -8,7 +8,12 @@ import { Subscribe as IetfSubscribe, SubscribeOk } from "../../js/net/src/ietf/s import { Subscriber as IetfSubscriber } from "../../js/net/src/ietf/subscriber.ts"; import { Version as IetfVersion } from "../../js/net/src/ietf/version.ts"; import { StreamId } from "../../js/net/src/lite/stream.ts"; -import { encodeSubscribeResponse, Subscribe, SubscribeEnd, SubscribeStart } from "../../js/net/src/lite/subscribe.ts"; +import { + encodeSubscribeResponse, + Subscribe, + SubscribeFinish, + SubscribeStart, +} from "../../js/net/src/lite/subscribe.ts"; import { Subscriber as LiteSubscriber } from "../../js/net/src/lite/subscriber.ts"; import { Track, TrackInfo } from "../../js/net/src/lite/track.ts"; import { Version as LiteVersion } from "../../js/net/src/lite/version.ts"; @@ -60,7 +65,7 @@ if (version !== undefined) { if (input.started || input.owed) await encodeSubscribeResponse(output, { start: new SubscribeStart([0n]) }, version); if (input.clean || input.owed) { - await encodeSubscribeResponse(output, { end: new SubscribeEnd(input.owed ? 1n : 0n) }, version); + await encodeSubscribeResponse(output, { finish: new SubscribeFinish(input.owed ? 1n : 0n) }, version); } } else { assert.equal(input.version, "moqt-18");