diff --git a/doc/concept/moq-lite.md b/doc/concept/moq-lite.md index 38a72e4ff0..b60734faf6 100644 --- a/doc/concept/moq-lite.md +++ b/doc/concept/moq-lite.md @@ -329,11 +329,15 @@ A group is addressed by its start: where it starts in the track's timescale, or a bare number that grows with each group on a track without one. A time start is the group containing that time, the newest starting at or before it, and a time end includes the group containing it, so neither waits on a frame. -A relative anchor counts the groups the hop holds back from the latest one. It -resolves on the hop that publishes the track, and over moq-lite-07, which -carries it upstream and reports the live edge, so a copy resolves the ends -itself. A Rust moq-transport copy resolves a relative anchor from the Largest -Object its live join reports. Older moq-lite versions refuse relative anchors +A relative anchor counts the groups back from the latest one. It resolves on +the hop that publishes the track, and over moq-lite-07, which carries it +upstream and reports the live edge. A moq-lite-07 copy never counts a relative +start over the groups it happens to hold: it takes the start its publisher +reports for the same range, asking upstream for one when none has, even once the +track ended, so a reader reads the same groups from a warm relay as from a cold +one. A Rust +moq-transport copy counts a relative anchor back by group ID from the Largest +Object its live join reports, as a joining FETCH does. Older moq-lite versions refuse relative anchors other than the live and default starts. Older moq-lite versions and moq-transport carry a group ID equal to the start, so a time anchor goes upstream as that ID, where a start selects the first group at or after it diff --git a/drafts/draft-lcurley-moq-lite.md b/drafts/draft-lcurley-moq-lite.md index fd344f0455..bed7001f69 100644 --- a/drafts/draft-lcurley-moq-lite.md +++ b/drafts/draft-lcurley-moq-lite.md @@ -1538,7 +1538,8 @@ Which fields follow, and what the Anchor names: Any other value is a PROTOCOL_VIOLATION. Both ends include the Group they name, so a Range from Time `a` to Time `b` delivers every Group from the one containing `a` through the one containing `b`. -The publisher resolves a Relative or Time Anchor against the Track as it holds it. +The publisher resolves a Relative or Time Anchor against the Track as it holds it, except a Relative `Start` of one or more `Groups Back` on a relay, which never depends on the part of the Track the relay holds. +A relay resolves one to the `Range Start` its upstream reported for a Range with the same `Start`, counted from the same latest Group or a newer one: when none has, it asks its upstream for such a Range in its SUBSCRIBE or a SUBSCRIBE_UPDATE, or in a SUBSCRIBE of its own once its subscription is over, as when the Track ended, and resolves the Range only once that answer arrives. 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` and `Subscriber Min Age` are the only things that ask for data. diff --git a/js/net/src/integration.test.ts b/js/net/src/integration.test.ts index bd47736ffd..05618ed458 100644 --- a/js/net/src/integration.test.ts +++ b/js/net/src/integration.test.ts @@ -29,7 +29,7 @@ import { Producer as OriginProducer } from "./origin.ts"; import * as Path from "./path.ts"; import { Stream } from "./stream.ts"; import { Timescale, Timestamp } from "./time.ts"; -import type { Position, Producer as TrackProducer } from "./track.ts"; +import type { Position, Producer as TrackProducer, Range as TrackRange } from "./track.ts"; import { unwanted } from "./util/demand.ts"; import { withTimeout } from "./util/timeout.ts"; import { wireOf } from "./wire.ts"; @@ -484,6 +484,248 @@ test("integration: lite-07 serves sparse and relative ranges", async () => { } }); +// A later reader's relative start on a lite-07 copy already holding part of the track counts back as +// the publisher did, not over the groups the copy happens to hold: over a copy holding groups 1 and +// 2 of 0 to 9, it reads only the groups its range names. Mirrors the Rust +// `a_later_relative_start_on_a_partial_copy_reads_only_its_groups`. +test("integration: lite-07 starts a later reader's relative range where the publisher did", async () => { + const pair = createMockTransportPair(Lite.ALPN_07_WIP); + const origin = new OriginProducer(); + const [client, server] = await Promise.all([ + connect(url, { transport: pair.client }), + accept(pair.server, url, { publish: origin.consume() }), + ]); + + const broadcast = publish(origin, Path.from("test")); + const producer = defined(broadcast.createTrack("video", { timescale: Timescale.MILLI, maxAge: null })); + for (let key = 0; key < 10; key++) hooks.appendNext(producer).finish(); + + const remote = wireOf(client).consume(Path.from("test")); + const subscribe = (ranges: TrackRange[]) => + remote + .track("video") + .subscribe({ delay: { max: null }, order: "ascending", ranges }) + .ordered(); + const read = async (reader: ReturnType) => { + const got: (bigint | undefined)[] = []; + for (;;) { + const group = await reader.nextGroup(); + got.push(group?.start); + if (!group) return got; + } + }; + try { + const first = subscribe([{ start: { time: 1n }, end: { time: 2n } }]); + expect(await read(first)).toEqual([1n, 2n, undefined]); + + // The publisher counts back past the end, so it has no group there to deliver. + const empty = subscribe([{ start: { relative: 2n }, end: { time: 6n } }]); + expect(await read(empty)).toEqual([undefined]); + empty.close(); + + const second = subscribe([{ start: { relative: 2n }, end: { time: 8n } }]); + expect(await read(second)).toEqual([7n, 8n, undefined]); + first.close(); + second.close(); + } finally { + remote.close(); + broadcast.close(); + client.abort(); + server.abort(); + } +}); + +// A copy reading a track from its oldest group learns where the publisher's groups start, so a later +// reader counting back past them starts there, as the publisher does, rather than wait on a group +// below that never arrives. Mirrors the Rust +// `a_later_relative_start_counts_back_to_where_the_oldest_range_started`. +test("integration: lite-07 counts a later relative start back to where the oldest range started", async () => { + const pair = createMockTransportPair(Lite.ALPN_07_WIP); + const origin = new OriginProducer(); + const [client, server] = await Promise.all([ + connect(url, { transport: pair.client }), + accept(pair.server, url, { publish: origin.consume() }), + ]); + + const broadcast = publish(origin, Path.from("test")); + const producer = defined(broadcast.createTrack("video", { timescale: Timescale.MILLI, maxAge: null })); + for (let key = 1n; key < 4n; key++) { + const group = producer.createGroup(key); + group.writeFrame({ payload: new Uint8Array([1]), timestamp: Timestamp.fromMillis(Number(key)) }); + group.finish(); + } + + const remote = wireOf(client).consume(Path.from("test")); + const subscribe = (ranges: TrackRange[]) => + remote + .track("video") + .subscribe({ delay: { max: null }, order: "ascending", ranges }) + .ordered(); + const read = async (reader: ReturnType, count: number) => { + const got: (bigint | undefined)[] = []; + for (let i = 0; i < count; i++) + got.push((await withTimeout(reader.nextGroup(), Duration.millis(2_000), "group timed out"))?.start); + return got; + }; + try { + const oldest = subscribe([{}]); + expect(await read(oldest, 3)).toEqual([1n, 2n, 3n]); + const later = subscribe([{ start: { relative: 5n } }]); + expect(await read(later, 3)).toEqual([1n, 2n, 3n]); + oldest.close(); + later.close(); + } finally { + remote.close(); + broadcast.close(); + client.abort(); + server.abort(); + } +}); + +// A relative start on a lite-07 copy never resolves against the groups the copy holds: over a copy +// holding groups 9 and 10, a reader counting 5 back starts where the publisher counts to, not at 9. +// While the subscription that delivered them is live, the copy asks it for the reader's range, and +// the publisher counts back from its latest group, 10, to 5. Once it is gone, the copy subscribes +// again and the reader waits for that answer, counted back from the Largest it reports: 11, written +// while the copy was idle, so 6. Mirrors the Rust +// `a_relative_start_never_resolves_against_the_groups_a_copy_holds`. +for (const live of [true, false]) { + test(`integration: lite-07 never resolves a relative start against the groups a copy holds (live: ${live})`, async () => { + const pair = createMockTransportPair(Lite.ALPN_07_WIP); + const origin = new OriginProducer(); + const [client, server] = await Promise.all([ + connect(url, { transport: pair.client }), + accept(pair.server, url, { publish: origin.consume() }), + ]); + + const broadcast = publish(origin, Path.from("test")); + const producer = defined(broadcast.createTrack("video", { timescale: Timescale.MILLI, maxAge: null })); + const write = (key: bigint) => { + const group = producer.createGroup(key); + group.writeFrame({ payload: new Uint8Array([1]), timestamp: Timestamp.fromMillis(Number(key)) }); + group.finish(); + }; + for (let key = 0n; key <= 10n; key++) write(key); + + const remote = wireOf(client).consume(Path.from("test")); + const subscribe = (ranges: TrackRange[]) => + remote.track("video").subscribe({ delay: { max: null }, order: "ascending", ranges }); + const read = async (reader: ReturnType) => { + const got: bigint[] = []; + for (;;) { + const group = await withTimeout(reader.recvGroup(), Duration.millis(2_000), "group timed out"); + if (!group) return got.sort((a, b) => Number(a - b)); + got.push(group.start); + } + }; + let later: ReturnType | undefined; + try { + // The copy holds groups 9 and 10. + const first = subscribe([{ start: { time: 9n }, end: { time: 10n } }]); + expect(await read(first)).toEqual([9n, 10n]); + let end = 10n; + if (!live) { + // Its upstream subscription leaves with its last reader, and the track moves on. + first.close(); + await withTimeout( + (async () => { + while (producer.subscription.peek() !== undefined) await yieldNow(); + })(), + Duration.millis(2_000), + "the upstream subscription never left", + ); + write(11n); + end = 11n; + } + later = subscribe([{ start: { relative: 5n }, end: { time: end } }]); + expect(await read(later)).toEqual(live ? [5n, 6n, 7n, 8n, 9n, 10n] : [6n, 7n, 8n, 9n, 10n, 11n]); + first.close(); + } finally { + later?.close(); + remote.close(); + broadcast.close(); + client.abort(); + server.abort(); + } + }); +} + +// A finished copy has no upstream subscription left to carry a later reader's relative start, so +// the reader still reads what the publisher counts back to, not nothing, whether the copy holds +// every group it counts back to or only some, and whether it is still open or closed, kept for its +// cache. Mirrors the Rust `a_relative_start_on_a_finished_copy_reads_the_publishers_groups`. +for (const [from, closed] of [ + [0n, false], + [9n, false], + [9n, true], +] as const) { + test(`integration: lite-07 starts a relative start on a finished copy where the publisher does (from ${from}, closed: ${closed})`, async () => { + const pair = createMockTransportPair(Lite.ALPN_07_WIP); + const origin = new OriginProducer(); + const [client, server] = await Promise.all([ + connect(url, { transport: pair.client }), + accept(pair.server, url, { publish: origin.consume() }), + ]); + + const broadcast = publish(origin, Path.from("test")); + const producer = defined(broadcast.createTrack("video", { timescale: Timescale.MILLI, maxAge: null })); + for (let key = 0n; key <= 10n; key++) { + const group = producer.createGroup(key); + group.writeFrame({ payload: new Uint8Array([1]), timestamp: Timestamp.fromMillis(Number(key)) }); + group.finish(); + } + producer.finish(); + + const remote = wireOf(client).consume(Path.from("test")); + const subscribe = (ranges: TrackRange[]) => + remote.track("video").subscribe({ delay: { max: null }, order: "ascending", ranges }); + const read = async (reader: ReturnType) => { + const got: bigint[] = []; + for (;;) { + const group = await withTimeout(reader.recvGroup(), Duration.millis(2_000), "group timed out"); + if (!group) return got.sort((a, b) => Number(a - b)); + got.push(group.start); + } + }; + // The first reader holds the finished copy. + const first = subscribe([{ start: { time: from } }]); + let later: ReturnType | undefined; + let short: ReturnType | undefined; + let next: ReturnType | undefined; + try { + const held: bigint[] = []; + for (let key = from; key <= 10n; key++) held.push(key); + expect(await read(first)).toEqual(held); + if (closed) { + // The copy closes once its subscription is over. + await withTimeout( + (async () => { + while (producer.subscription.peek() !== undefined) await yieldNow(); + })(), + Duration.millis(2_000), + "the upstream subscription never left", + ); + } + later = subscribe([{ start: { relative: 5n } }]); + expect(await read(later)).toEqual([5n, 6n, 7n, 8n, 9n, 10n]); + // An ask ending before the track does is over at its own end, so the next one still goes out. + short = subscribe([{ start: { relative: 4n }, end: { time: 6n } }]); + expect(await read(short)).toEqual([6n]); + next = subscribe([{ start: { relative: 3n } }]); + expect(await read(next)).toEqual([7n, 8n, 9n, 10n]); + } finally { + next?.close(); + short?.close(); + later?.close(); + first.close(); + remote.close(); + broadcast.close(); + client.abort(); + server.abort(); + } + }); +} + // A publisher counts a relative start back from its newest group, which may hold no frame yet and // so counts neither in the largest it reports nor where the copy resolves the start. The copy keeps // the publisher's start, so a reader waiting on each group in sequence starts there rather than diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index 18ada0adec..f4abc105bc 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -27,6 +27,7 @@ import type { InfoInit, Position, Producer, + Range, Request, Subscriber, Subscription, @@ -583,6 +584,13 @@ export interface StagedTrack { commit(): Producer; } +/** + * Where a publisher reported starting a range counting back: at a group, or at or past an exclusive + * end where the range held no group, which says nothing of a range ending later. Mirrors the Rust + * `track::CountedStart`. + */ +export type CountedStart = { at: bigint; past?: never } | { past: bigint; at?: never }; + /** * How a received copy's start bounds the fills its readers ask below it. Mirrors the Rust * `Start::Resolved` and `Producer::fills_from_start`. @@ -758,10 +766,36 @@ export const hooks: { /** * Tell a lite-07 copy where its publisher started each live range counting back, as * `[back, start, latest]` triples, replacing the last set: the copy holds only part of the - * track, so a reader's relative start counts back as the publisher did, until a group past - * the `latest` it counted from moves the live edge. Assigned by `track.ts`. + * track, so a reader's relative start counting back from `latest`, or an older group, starts + * where the publisher did. Assigned by `track.ts`. + */ + relativeStarts: (producer: Producer, starts: Iterable) => void; + /** + * Mark a received copy whose publisher reports where each range starts (lite-07), so a reader's + * relative start waits for {@link relativeStarts} rather than count over the groups held, and + * asks for it (see {@link asks}). Mirrors the Rust `Producer::reported`; assigned by `track.ts`. + */ + reportsStarts: (producer: Producer) => void; + /** + * The relative ranges a lite-07 copy's readers wait on its publisher to start, which its + * subscription asks upstream for, each as a range of its own, so the publisher reports where it + * starts. Mirrors the Rust `Producer::asks`; assigned by `track.ts`. + */ + asks: (producer: Producer) => readonly Range[]; + /** + * Bumped whenever a reader starts or stops waiting on one of {@link asks}, even one another + * reader already waits on: that reader may have joined after the last answer. Mirrors the Rust + * `Producer::poll_asks_changed`; assigned by `track.ts`. + */ + asksChanged: (producer: Producer) => Getter; + /** + * Count an ask a finished lite-07 copy's session sends upstream as out while `asking`, and done + * once not: the groups it delivers land below the end, so a reader waits for them rather than end + * at the boundary. Mirrors the Rust `Producer::asking`; assigned by `track.ts`. */ - relativeStarts: (producer: Producer, starts: Iterable) => void; + asking: (producer: Producer, asking: boolean) => void; + /** Evict group `key` from the cache, as the pool reclaiming it does. Tests only. */ + evict: (producer: Producer, key: bigint) => void; /** Mint a track {@link Request}; assigned by `track.ts`. */ makeRequest: (options: TrackRequestOptions) => Request; /** @@ -1186,6 +1220,21 @@ export const hooks: { timeStarts: () => { throw new Error("track.ts not loaded"); }, + reportsStarts: () => { + throw new Error("track.ts not loaded"); + }, + asks: () => { + throw new Error("track.ts not loaded"); + }, + asksChanged: () => { + throw new Error("track.ts not loaded"); + }, + asking: () => { + throw new Error("track.ts not loaded"); + }, + evict: () => { + throw new Error("track.ts not loaded"); + }, relativeStarts: () => { throw new Error("track.ts not loaded"); }, diff --git a/js/net/src/lite/fit.ts b/js/net/src/lite/fit.ts index 902357d58b..a853b7e4e9 100644 --- a/js/net/src/lite/fit.ts +++ b/js/net/src/lite/fit.ts @@ -43,6 +43,25 @@ export function budget(fields: number, next: bigint, count: number): Budget { }; } +/** + * The longest prefix of `ranges` fitting half of what `budget` has left, which it then takes: the + * other half stays for the demand and fills, which {@link fit} can only widen so far. Mirrors the + * Rust `Budget::take`. + */ +export function take(budget: Budget, ranges: readonly WireRange[]): WireRange[] { + const room = Math.floor(budget.bytes / 2); + let used = 0; + const taken: WireRange[] = []; + for (const range of ranges) { + const bytes = cost(budget, range); + if (bytes === undefined || used + bytes > room) break; + used += bytes; + taken.push(range); + } + budget.bytes -= used; + return taken; +} + /** What `range` costs, or undefined when it can't go out. */ function cost(budget: Budget, range: WireRange): number | undefined { // An end at group 0 asks for nothing, which encoding refuses. diff --git a/js/net/src/lite/subscribe.test.ts b/js/net/src/lite/subscribe.test.ts index 72b95844fa..efe5332460 100644 --- a/js/net/src/lite/subscribe.test.ts +++ b/js/net/src/lite/subscribe.test.ts @@ -693,7 +693,7 @@ test("lite-07 updates edit only the changed ranges", async () => { 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, fillIds: [] }); + expect(edit).toEqual({ remove, add, askIds: [], fillIds: [] }); 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), @@ -749,7 +749,7 @@ test("lite-07 a refused edit leaves the ranges as sent", () => { expect(() => live.edit(wide)).toThrow("too many ranges"); expect(live.entries()).toEqual([[0n, {}]]); expect(live.next()).toBe(1n); - expect(live.edit(wide.slice(0, 2))).toEqual({ remove: [0n], add: wide.slice(0, 2), fillIds: [] }); + expect(live.edit(wide.slice(0, 2))).toEqual({ remove: [0n], add: wide.slice(0, 2), askIds: [], fillIds: [] }); }); test("SUBSCRIBE_STARTED is type 4 on lite-07 and unknown before it", async () => { diff --git a/js/net/src/lite/subscribe.ts b/js/net/src/lite/subscribe.ts index e7f08dc034..925537ba58 100644 --- a/js/net/src/lite/subscribe.ts +++ b/js/net/src/lite/subscribe.ts @@ -446,7 +446,7 @@ export function rangeSize(range: WireRange): number { } /** A range's anchors as a key, so equal ranges compare equal. */ -function rangeKey(range: WireRange): string { +export function wireRangeKey(range: WireRange): string { const anchor = (anchor: WireAnchor | undefined) => { if (anchor?.absolute !== undefined) return `a${anchor.absolute}.${anchor.frame ?? 0}`; if (anchor?.relative !== undefined) return `r${anchor.relative}`; @@ -462,6 +462,13 @@ export interface Applied { added: [bigint, WireRange][]; } +/** + * Who a live range on a lite-07 subscription asks for: the demand, a reader waiting on the publisher + * to start a relative range, or a fill. A `"stale"` ask was answered before the copy's latest group, + * so no wanted range keeps it. Mirrors the Rust `Owner`. + */ +export type RangeOwner = "demand" | "ask" | "stale" | "fill"; + /** * 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 @@ -562,39 +569,42 @@ export class LiveRanges { * changes one range sends only that one, and the publisher keeps where the rest resolved. * Before lite-07 replaced ranges take fresh Range IDs. * - * The last `fills` of `ranges` ask for fills, and `isFill` names the live ones that do: 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. `fillIds` are the Range IDs the added fill ranges took. + * The demand's ranges come first, then `asks` asks', then `fills` fills', and `ownerOf` names who + * asked for each live one: an ask or fill asking for what a demand range does still takes a range + * of its own, so each Range ID stays one owner's, and a live range `ownerOf` calls stale is kept + * for no one. `askIds` and `fillIds` are the Range IDs the added asks' and fills' ranges took. */ edit( ranges: readonly WireRange[], - fills = 0, - isFill: (id: bigint) => boolean = () => false, - ): { remove: bigint[]; add: WireRange[]; fillIds: bigint[] } { + { asks = 0, fills = 0 }: { asks?: number; fills?: number } = {}, + ownerOf: (id: bigint) => RangeOwner = () => "demand", + ): { remove: bigint[]; add: WireRange[]; askIds: bigint[]; fillIds: bigint[] } { 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])); + this.ranges().every((range, index) => wireRangeKey(range) === wireRangeKey(ranges[index])); if (!same) { this.#live.clear(); this.#add(ranges); } - return { remove: [], add: [...ranges], fillIds: [] }; + return { remove: [], add: [...ranges], askIds: [], fillIds: [] }; } - const demand = ranges.length - fills; - const key = (range: WireRange, fill: boolean) => `${fill ? "f" : "d"}${rangeKey(range)}`; + const demand = ranges.length - asks - fills; + const owner = (index: number): RangeOwner => + index < demand ? "demand" : index < demand + asks ? "ask" : "fill"; + const key = (range: WireRange, owner: RangeOwner) => `${owner}:${wireRangeKey(range)}`; // Each live range keeps at most one wanted copy, so duplicates edit like any other. const wanted = new Map(); ranges.forEach((range, index) => { - const k = key(range, index >= demand); + const k = key(range, owner(index)); wanted.set(k, (wanted.get(k) ?? 0) + 1); }); const remove: bigint[] = []; // The bytes the live ranges take once the edit applies. let size = this.#size; for (const [id, range] of this.#live) { - const k = key(range, isFill(id)); + const k = key(range, ownerOf(id)); const count = wanted.get(k) ?? 0; if (count > 0) { wanted.set(k, count - 1); @@ -605,22 +615,25 @@ export class LiveRanges { } // In the order asked for, so the publisher numbers them the way this side does. const add: WireRange[] = []; + const askIds: bigint[] = []; const fillIds: bigint[] = []; const first = this.#next; ranges.forEach((range, index) => { - const k = key(range, index >= demand); + const k = key(range, owner(index)); const count = wanted.get(k) ?? 0; if (count === 0) return; wanted.set(k, count - 1); add.push(range); - if (index >= demand) fillIds.push(first + BigInt(add.length - 1)); + const id = first + BigInt(add.length - 1); + if (owner(index) === "ask") askIds.push(id); + if (owner(index) === "fill") fillIds.push(id); }); // Checked before anything changes, so a refused edit leaves the ranges as last sent. for (const range of add) size += rangeSize(range); if (size > Message.MAX_MESSAGE_SIZE) throw new Error("too many ranges for one SUBSCRIBE"); for (const id of remove) this.#remove(id); this.#add(add); - return { remove, add, fillIds }; + return { remove, add, askIds, fillIds }; } } diff --git a/js/net/src/lite/subscriber.test.ts b/js/net/src/lite/subscriber.test.ts index f8b086a45f..91d08d7b3e 100644 --- a/js/net/src/lite/subscriber.test.ts +++ b/js/net/src/lite/subscriber.test.ts @@ -28,7 +28,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 } from "../track.ts"; +import type { Producer, Range as TrackRange, Subscriber as TrackSubscriber } from "../track.ts"; import { SEGMENT_BYTES } from "../util/limits.ts"; import { Budget } from "../util/yield.ts"; import * as Varint from "../varint.ts"; @@ -2972,6 +2972,78 @@ test("a widened range's SUBSCRIBE_STARTED resolves the feed's start", async () = } }); +// A reader of a lite-07 copy whose relative start no report answers asks the copy's upstream for +// its range, in an update of its own, and once SUBSCRIBE_STARTED answers it the copy retires the +// ask: one update each way, however many groups arrive in between. The demand's own range, added +// fresh, answers the first reader with no ask. Mirrors the Rust +// `a_lite07_ask_the_demand_adds_takes_no_range_of_its_own`. +test("a lite-07 copy asks upstream for a relative start once, and retires the ask once answered", async () => { + const version = Version.DRAFT_07; + const { quic, streams, opened } = fakeSession(); + const subscriber = new Subscriber(quic, version, toHop(1n)); + const broadcast = subscriber.consume(Path.from("room")); + const subscribe = (ranges: TrackRange[]) => broadcast.track("video").subscribe({ delay: { max: null }, ranges }); + const live = subscribe([{ start: { time: 0n } }]); + const readers: TrackSubscriber[] = []; + const group = async (key: bigint) => { + await subscriber.runGroup(new Group({ subscribe: 0n, key }), await groupStream(Number(key), version)); + (await live.recvGroup())?.close(); + await drainReactions(); + }; + try { + await opened(0); + await answerTrackInfo(streams[0], version, 1000); + const request = await opened(1); + const sent = new Reader(request.outbound, undefined, version); + expect(await sent.u53()).toBe(StreamId.Subscribe); + await Subscribe.decode(sent, version); + await request.reading; + await respond(request, version, [{ start: new SubscribeStart([0n]) }]); + for (const key of [0n, 1n, 2n]) await group(key); + + // The demand adds the first reader's range, which its answer starts. + const first = subscribe([{ start: { relative: 1n } }]); + readers.push(first); + const demanded = await SubscribeUpdate.decode(sent, version); + expect([demanded.remove, demanded.ranges]).toEqual([[], [{ start: { relative: 1n } }]]); + await respond(request, version, [{ started: new SubscribeStarted(1n, 1n) }]); + await drainReactions(); + expect(first.floor()).toBe(1n); + + // A later reader counting from group 3 can't use that answer, counted from 2, so it asks. + await group(3n); + const later = subscribe([{ start: { relative: 1n } }]); + readers.push(later); + // Waiting on a group, as a reader does, so the answer settles it. + const waiting = later.recvGroup(); + const asked = await SubscribeUpdate.decode(sent, version); + expect([asked.remove, asked.ranges]).toEqual([[], [{ start: { relative: 1n } }]]); + // A group arriving while the ask is out asks nothing again. + await group(4n); + await respond(request, version, [{ started: new SubscribeStarted(2n, 3n) }]); + const retired = await SubscribeUpdate.decode(sent, version); + expect([retired.remove, retired.ranges]).toEqual([[2n], []]); + (await waiting)?.close(); + expect(later.floor()).toBe(3n); + + // Nothing more to say. + await drainReactions(); + const more = await Promise.race([ + SubscribeUpdate.decode(sent, version).then(() => "update"), + (async () => { + for (let i = 0; i < 5; i++) await drainReactions(); + return "quiet"; + })(), + ]); + expect(more).toBe("quiet"); + } finally { + for (const reader of readers) reader.close(); + live.close(); + broadcast.close(); + subscriber.close(); + } +}); + // A lite-07 stale (OLD) drop accounts for its groups for good, whichever of it and a group's stream // lands first: an update owes none of them again, and a group still arriving, such as one another // subscription sharing the copy delivers, is never aborted by the drop. diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index 9300af24b9..f6914d5ab9 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -28,6 +28,7 @@ import { Cost, type Hop, MAX_HOPS, routesEqual, UNKNOWN_HOP, type WireRoute } fr import * as Instance from "../instance.ts"; import { type Arriving, + type CountedStart, hiddenBelow, hooks, maxDelayOf, @@ -85,11 +86,13 @@ import { EMPTY_RANGE, inclusiveGroupEnd, LiveRanges, + type RangeOwner, requestedRange, Subscribe, SubscribeUpdate, toWireRanges, type WireRange, + wireRangeKey, } from "./subscribe.ts"; import { TrackInfo, Track as TrackMessage } from "./track.ts"; import { decodeTracksEventMaybe, Tracks as TracksMessage } from "./tracks.ts"; @@ -236,6 +239,9 @@ interface SubscribeEntry { // The Range IDs of the fills' live ranges; the rest are the demand's, which alone say where the // live feed starts. fillIds: Set; + // The Range IDs of the asks' live ranges: a relative range a reader of the copy waits on the + // publisher to start (see `hooks.asks`), which says nothing of the live feed either. + askIds: Set; // The groups the fills' ranges ask for, as last sent: widened with the demand's ranges until // they fit, so a run may cover more than the fills ask. fillGroups: readonly Run[]; @@ -1506,6 +1512,18 @@ export class Subscriber { // 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. + // A reader still waiting on its publisher to start a relative range asks on a subscription of + // its own before the finished copy closes, one at a time: an answer moving the readers changes + // the asks again. Once closed, a later reader asks with a copy of its own. + if (tailSettled && entry.end !== undefined && hasRanges(this.version)) { + const asksChanged = hooks.asksChanged(producer); + for (let asked: number | undefined; hooks.asks(producer).length > 0; ) { + // The same asks after an answer are ones it could not settle. + if (asked === asksChanged.peek()) break; + asked = asksChanged.peek(); + await this.#askFinished(broadcast, consumed, entry); + } + } if (tailSettled && entry.stream.kind === "finished") producer.finish(); hooks.releaseTrack(producer); } catch (err) { @@ -1683,7 +1701,9 @@ export class Subscriber { }); // A new upstream subscription: stale runs an earlier one reported were judged under other terms. hooks.supersedeStale(producer); - if (!hasRanges(this.version)) { + // Lite-07 reports where each range starts. + if (hasRanges(this.version)) hooks.reportsStarts(producer); + else { hooks.dropAnchors(producer); // A wire without ranges starts a time anchor at the first group at or after it, so its // readers do too, rather than at an earlier group the copy happens to hold. @@ -1709,6 +1729,7 @@ export class Subscriber { sent: 0, stream: { kind: "setup" }, fillIds: new Set(), + askIds: new Set(), fillGroups: [], fillsChanged: new Signal(0), }; @@ -2187,6 +2208,7 @@ export class Subscriber { stream: { kind: "setup" }, filling: true, fillIds: new Set(), + askIds: new Set(), fillGroups: [], fillsChanged: new Signal(0), }; @@ -2747,6 +2769,10 @@ export class Subscriber { // What the fills asked with the last update, and the priority it carried. let sentFills: Run[] = []; let sentPriority = msg.priority; + // The revision of the asks the last update carried, as many of them as fit: answers moving the + // readers change it again, which asks for the rest. Mirrors the Rust `asks_seen`. + let sentAsks: number | undefined; + const asksChanged = hooks.asksChanged(track); const asked = hooks.demandAsked(track.demand()); const edge = hooks.producerLargest(track); for (;;) { @@ -2766,16 +2792,22 @@ export class Subscriber { const fills = entry.fills?.ask; const fillGroups = fills?.groups ?? []; const priority = Math.max(current?.priority ?? 0, fills?.priority ?? -128); + // The relative ranges readers wait on the publisher to start, each asked as a range of its own + // (see `RangeOwner`), alongside a demand that covers their groups. + const asksRevision = asksChanged.peek(); + const asks = hasRanges(this.version) ? this.#asks(entry) : []; + const askedAll = sentAsks === asksRevision || (asks.length === 0 && entry.askIds.size === 0); if ( current === undefined || (refresh === undefined && priority === sentPriority && sameRuns(fillGroups, sentFills) && + askedAll && this.#sameSubscription(current, lastSent)) ) { // Each wake releases the quiet signals too, including when termination wins. const next = await race([ - Signal.race(hooks.requested(track), asked, edge, entry.fillsChanged), + Signal.race(hooks.requested(track), asked, edge, entry.fillsChanged, asksChanged), stopped, ]); if (next === null) return; @@ -2808,7 +2840,9 @@ export class Subscriber { maxDelay: wireMaxDelay(current), minDelay: minDelayOf(current), }); - const budget = Fit.budget(fields, entry.live.next(), demanded.length + fillGroups.length); + const budget = Fit.budget(fields, entry.live.next(), demanded.length + asks.length + fillGroups.length); + // The asks that don't fit wait for those sent to be answered, which retires them. + const askRanges = Fit.take(budget, asks); const fitted = Fit.fit(demanded, fillGroups, entry.fills?.avoid() ?? [], budget); // A fill's range starting past its run speaks only while that run is asked whole. entry.fills?.retireSkipped(fitted.fills); @@ -2816,9 +2850,20 @@ export class Subscriber { start: { absolute: run.start }, end: { absolute: run.end }, })); - // The demand's ranges, then the fills'. - const edited = entry.live.edit([...fitted.ranges, ...fillRanges], fillRanges.length, (id) => - entry.fillIds.has(id), + // An ask for a range the demand adds here takes no range of its own: the publisher answers + // the demand's from its latest group too. Mirrors the Rust `SubStream::edit`. + const fed = new Set(); + for (const id of entry.live.ids()) { + const range = entry.live.get(id); + if (range && !entry.fillIds.has(id) && !entry.askIds.has(id)) fed.add(wireRangeKey(range)); + } + const added = new Set(fitted.ranges.map(wireRangeKey).filter((key) => !fed.has(key))); + const ownAsks = askRanges.filter((range) => !added.has(wireRangeKey(range))); + // The demand's ranges, the asks', then the fills'. + const edited = entry.live.edit( + [...fitted.ranges, ...ownAsks, ...fillRanges], + { asks: ownAsks.length, fills: fillRanges.length }, + (id) => this.#ownerOf(entry, id), ); edit = edited; for (const id of entry.starts.keys()) @@ -2828,6 +2873,8 @@ export class Subscriber { } for (const id of entry.fillIds) if (!entry.live.get(id)) entry.fillIds.delete(id); for (const id of edited.fillIds) entry.fillIds.add(id); + for (const id of entry.askIds) if (!entry.live.get(id)) entry.askIds.delete(id); + for (const id of edited.askIds) entry.askIds.add(id); // The groups a fill newly asks for wait a fresh grace too. for (const run of fitted.fills) for (const gap of runGaps(entry.fillGroups, run.start, run.end)) @@ -2867,10 +2914,179 @@ export class Subscriber { lastSent = { ...current }; sentFills = fillGroups; sentPriority = priority; + sentAsks = asksRevision; console.debug(`subscribe update: id=${id} broadcast=${broadcast} track=${track.name}`); } } + // The relative ranges the copy's readers wait on the publisher to start, as wire ranges, once each. + #asks(entry: SubscribeEntry): WireRange[] { + const asks = new Map(); + // A relative end goes upstream open, so two asks may carry the same range. + for (const range of toWireRanges(wireRanges(hooks.asks(entry.track)))) asks.set(wireRangeKey(range), range); + return [...asks.values()]; + } + + // Who asked for the live range `id`. An ask its publisher answered counting from before the copy's + // latest group is stale when the asks change: a reader waiting on it joined since. Mirrors the + // Rust `SubStream::edit`. + #ownerOf(entry: SubscribeEntry, id: bigint): RangeOwner { + if (entry.fillIds.has(id)) return "fill"; + if (!entry.askIds.has(id)) return "demand"; + const largest = hooks.producerLargest(entry.track).peek()?.group; + const edge = entry.edges.get(id); + const stale = entry.starts.has(id) && largest !== undefined && (edge === undefined || edge < largest); + return stale ? "stale" : "ask"; + } + + // Ask where the relative starts `copy`'s readers wait on begin, on a lite-07 SUBSCRIBE of its own: + // the subscription that fed the finished copy is over, so nothing else can carry them, and the copy + // never counts them over the groups it holds. Its SUBSCRIBE_START answers the readers, and the + // groups it delivers land in the copy, which its readers wait for while it is out (see + // `hooks.asking`). It FINs once every group a range owes below the publisher's end is accounted for + // and read. + // Rejects when it fails, leaving its readers no answer. Mirrors the Rust `ask::Ask`. + async #askFinished(broadcast: Path.Valid, consumed: Consumed, copy: SubscribeEntry): Promise { + const producer = copy.track; + const id = this.#subscribeNext++; + const ranges = wireRanges(hooks.asks(producer)); + const terms = { ...hooks.requested(producer).peek(), order: "ascending" as const }; + const msg = this.#withRanges( + this.#subscribeMessage(id, broadcast, consumed, producer.name, terms, ranges), + ranges, + ); + // The asks that don't fit wait for those sent to be answered, which retires them. + if (msg.ranges) { + const fields = await this.#subscribeFields(msg, msg); + msg.ranges = Fit.take(Fit.budget(fields, 0n, msg.ranges.length), msg.ranges); + } + const entry: SubscribeEntry = { + track: producer, + staleBudget: msg.maxDelay ?? NO_MAX_DELAY, + compress: copy.compress, + timescale: copy.timescale, + tail: new Tail({ grace: () => hooks.tailGrace(producer) }), + requested: msg.startGroup, + floor: msg.startGroup, + asked: ranges, + live: new LiveRanges(this.version, msg.ranges ?? []), + starts: new Map(), + edges: new Map(), + sent: 0, + // Its responses are read here, which owns this stream's end. + stream: { kind: "setup" }, + fillIds: new Set(), + askIds: new Set(), + fillGroups: [], + fillsChanged: new Signal(0), + }; + this.#subscribes.set(id, entry); + hooks.asking(producer, true); + let stream: Stream | undefined; + // Whether our FIN went out: the answer arrived and every group it owes is in, so a reset after + // it, such as the publisher cutting a response short, costs nothing. + let fin = false; + try { + if (this.#denied(broadcast)) throw unauthorized(broadcast); + const opened = await this.#exchange( + { sendOrder: sendOrder({ priority: msg.priority }), version: this.version }, + async (stream) => { + await stream.writer.u53(StreamId.Subscribe); + await msg.encode(stream.writer, this.version); + return stream; + }, + consumed.valid, + new Deadline(), + ); + stream = opened; + // Every group a range owes below the publisher's end, from where it starts up to its own end. + const covered = (end: bigint) => + entry.start !== undefined && + entry.live.ids().every((id) => { + const start = entry.starts.get(id); + const range = entry.live.get(id); + if (start === undefined || range === undefined) return true; + const until = range.end?.absolute ?? (range.end?.time !== undefined ? range.end.time + 1n : end); + return entry.tail.covers(start, max(start, min(until, end))); + }); + // FIN once they are in. + const finish = async (end: bigint) => { + await entry.tail.settle(() => covered(end), hooks.trackReleased(producer)); + fin = true; + opened.writer.close(); + }; + for (;;) { + const resp = await decodeSubscribeResponseMaybe(opened.reader, this.version); + if (!resp) { + // The publisher FINs in reply to ours, or once it finished the track. + if (fin || entry.end !== undefined) break; + throw new ProtocolViolation("ask subscribe ended without SUBSCRIBE_FINISH"); + } + if ("start" in resp) { + if (entry.start !== undefined || BigInt(resp.start.starts.length) !== entry.live.initial) + throw new ProtocolViolation("unexpected ask SUBSCRIBE_OK"); + const edge = resp.start.largest?.position.group; + resp.start.starts.forEach((start, index) => { + entry.starts.set(BigInt(index), start); + entry.edges.set(BigInt(index), edge); + }); + entry.start = resp.start.starts.reduce( + (lowest, start) => + start === undefined || (lowest !== null && lowest <= start) ? lowest : start, + null, + ); + entry.tail.touch(); + // Read as they arrive: the readers wait for the rest while the ask is out. + hooks.relativeStarts(producer, this.#counted(entry)); + } else if ("finish" in resp) { + if (entry.end !== undefined) throw new ProtocolViolation("duplicate SUBSCRIBE_FINISH"); + entry.end = resp.finish.group; + entry.tail.touch(); + void finish(entry.end).catch(() => {}); + } else if ("drop" in resp) { + entry.tail.account(resp.drop.start, resp.drop.end); + } else if ("skip" in resp) { + this.#skipped(entry, resp.skip.start, resp.skip.end, "SUBSCRIBE_SKIP"); + } else { + throw new ProtocolViolation("unexpected ask subscribe response"); + } + } + opened.close(); + } catch (err) { + closeViolation(this.#quic, err); + stream?.abort(error(err)); + if (!fin) throw err; + } finally { + this.#subscribes.delete(id); + entry.tail.close(); + // Its groups are in, or never coming: the readers end at the boundary again. + hooks.asking(producer, false); + } + } + + // Where the publisher started each of `entry`'s live ranges counting back, which the copy, holding + // only part of the track, can't count, with the latest group it counted back from. + #counted(entry: SubscribeEntry): [bigint, CountedStart, bigint | undefined][] { + const relative: [bigint, CountedStart, bigint | undefined][] = []; + for (const id of entry.live.ids()) { + const range = entry.live.get(id); + const back = range?.start?.relative; + if (back === undefined || !entry.starts.has(id)) continue; + // Undefined for a range with no group to deliver. + const at = entry.starts.get(id); + const latest = entry.edges.get(id); + // With no group to deliver, the start lies at or past the range's exclusive end: an + // absolute end names it, a time end includes its group, and an open one the latest. + const end = range?.end; + const past = + end?.absolute ?? + (end?.time !== undefined ? end.time + 1n : latest === undefined ? undefined : latest + 1n); + const start = at !== undefined ? { at } : past !== undefined ? { past } : undefined; + if (start) relative.push([back, start, latest]); + } + return relative; + } + // Declare where a lite-07 feed starts: the lowest start the live ranges report, or the // requested floor, pending, while SUBSCRIBE_START or a range's SUBSCRIBE_STARTED has yet to // say where one starts. Mirrors the Rust `SubStream::declare`. @@ -2879,28 +3095,24 @@ export class Subscriber { // time start there resolves to when the copy holds no group at it, and one asked from the // oldest group as asked from 0. Mirrors the Rust `SubStream::report_starts`. const times: [bigint, bigint][] = []; - // And each one counting back, which the copy, holding only part of the track, can't count, - // with the latest group it counted back from. - const relative: [bigint, bigint, bigint | undefined][] = []; for (const id of entry.live.ids()) { const range = entry.live.get(id); - const time = range?.start?.time ?? range?.start?.absolute; - const back = range?.start?.relative; + if (range === undefined || !entry.starts.has(id)) continue; + // One from the oldest group is asked from group 0. + const time = range.start === undefined ? 0n : (range.start.time ?? range.start.absolute); + // Undefined for a range with no group to deliver. const at = entry.starts.get(id); - if (at === undefined) continue; - if (time !== undefined) times.push([time, at]); - else if (back !== undefined) relative.push([back, at, entry.edges.get(id)]); - else if (range !== undefined && range.start === undefined) times.push([0n, at]); + if (time !== undefined && at !== undefined) times.push([time, at]); } hooks.timeStarts(entry.track, times); - hooks.relativeStarts(entry.track, relative); + hooks.relativeStarts(entry.track, this.#counted(entry)); let start: bigint | null = null; let pending = entry.start === undefined; for (const id of entry.live.ids()) { // Only the demand's ranges say where the live feed starts. - if (entry.fillIds.has(id)) continue; + if (entry.fillIds.has(id) || entry.askIds.has(id)) continue; if (!entry.starts.has(id)) { pending = true; break; diff --git a/js/net/src/track.test.ts b/js/net/src/track.test.ts index 904877b625..3acb577838 100644 --- a/js/net/src/track.test.ts +++ b/js/net/src/track.test.ts @@ -5802,3 +5802,226 @@ test("the counted aggregates match a full fold of every subscriber", async () => producer.close(); } }); + +// A lite-07 copy holds only part of the track, so a relative start counts back only as its publisher +// reported, from the same latest group or a newer one, and reads nothing until it does, whatever +// groups the copy holds or receives: its reader asks for that report. Mirrors the Rust +// `a_received_copy_counts_a_relative_start_back_as_its_publisher_did`. +test("a received copy counts a relative start back as its publisher did", async () => { + const producer = receivedTrack({ timescale: Timescale.MILLI }); + hooks.reportsStarts(producer); + for (const key of [1n, 2n, 9n]) putGroup(producer, key, Number(key)); + hooks.relativeStarts(producer, [[2n, { at: 7n }, 9n]]); + const back = (back: bigint, end?: bigint) => + producer.subscribe({ + delay: { max: null }, + ranges: [{ start: { relative: back }, end: end === undefined ? undefined : { time: end } }], + }); + + const reported = back(2n); + expect(drain(reported)).toEqual([9n]); + expect(reported.floor()).toBe(7n); + const latest = back(0n); + expect(drain(latest)).toEqual([9n]); + expect(latest.floor()).toBe(9n); + // Group 8 may sit below 9, so it reads nothing meanwhile, and asks for a report. + const unreported = back(1n); + expect(drain(unreported)).toEqual([]); + expect(unreported.floor()).toBeUndefined(); + expect(hooks.asks(producer)).toEqual([{ start: { relative: 1n } }]); + + // Holding every group in between counts nothing either. + putGroup(producer, 8n, 8); + await settle(); + expect(drain(unreported)).toEqual([]); + + // A report counted from a newer latest group starts it, as the publisher placed it then. + putGroup(producer, 10n, 10); + hooks.relativeStarts(producer, [ + [2n, { at: 7n }, 9n], + [1n, { at: 9n }, 10n], + ]); + await settle(); + expect(unreported.floor()).toBe(9n); + expect(drain(unreported).sort((a, b) => Number(a - b))).toEqual([9n, 10n]); + expect(hooks.asks(producer)).toEqual([]); + // But one from an older latest group says nothing of a reader counting from a newer one. + const later = back(2n); + expect(drain(later)).toEqual([]); + expect(reported.floor()).toBe(7n); + + // A publisher with no group to deliver in a range ending at 10 has none in one ending sooner, but + // says nothing of one ending later. + hooks.relativeStarts(producer, [[5n, { past: 10n }, 10n]]); + const empty = back(5n, 8n); + expect(drain(empty)).toEqual([]); + expect(await empty.recvGroup()).toBeUndefined(); + const longer = back(5n, 10n); + expect(drain(longer)).toEqual([]); + expect(longer.floor()).toBeUndefined(); + + // Nothing counts over the groups held once the copy ends either, and its reader waits for the + // report rather than end with nothing: a finished copy asks on a subscription of its own. + hooks.endTrack(producer, { group: 11n, frame: 0 }); + await settle(); + expect(drain(later)).toEqual([]); + expect(later.floor()).toBeUndefined(); + let ended = false; + void later.recvGroup().then((group) => { + ended = group === undefined; + }); + await settle(); + expect(ended).toBe(false); + + // While the ask is out, a reader waits for the groups it delivers rather than end at the + // boundary, reading each as it lands. + hooks.asking(producer, true); + const joined = producer.subscribe({ delay: { max: null }, ranges: [{ start: { time: 3n } }] }); + expect(drain(joined).sort((a, b) => Number(a - b))).toEqual([2n, 8n, 9n, 10n]); + putGroup(producer, 5n, 5); + await settle(); + expect(drain(joined)).toEqual([5n]); + // The rest were never produced, as the ask's GROUP Previous Starts say. + for (const [start, end] of [ + [0n, 1n], + [3n, 5n], + [6n, 8n], + ]) + hooks.skipTrackGroups(producer, start, end); + let done = false; + void joined.recvGroup().then((group) => { + done = group === undefined; + }); + await settle(); + expect(done).toBe(false); + hooks.asking(producer, false); + await settle(); + expect(done).toBe(true); +}); + +// While a finished copy's ask is out, a reader in key order waits at a gap below where the copy's +// feed started, which the ask may still fill, rather than pass it. Mirrors the Rust +// `an_ordered_read_waits_on_the_gaps_an_ask_may_fill`. +test("an ordered read waits on the gaps an ask may fill", async () => { + const producer = receivedTrack({ timescale: Timescale.MILLI }); + hooks.reportsStarts(producer); + hooks.startTrack(producer, { start: 9n }); + for (const key of [9n, 10n]) putGroup(producer, key, Number(key)); + hooks.endTrack(producer, { group: 11n, frame: 0 }); + hooks.asking(producer, true); + hooks.relativeStarts(producer, [[5n, { at: 5n }, 10n]]); + const ordered = producer.subscribe({ delay: { max: null }, ranges: [{ start: { relative: 5n } }] }).ordered(); + const got: (bigint | undefined)[] = []; + const reading = (async () => { + for (;;) { + const group = await ordered.nextGroup(); + got.push(group?.start); + if (!group) return; + } + })(); + await settle(); + expect(got).toEqual([]); + putGroup(producer, 5n, 5); + await settle(); + expect(got).toEqual([5n]); + // The rest were never produced, as the ask's next GROUP says. + hooks.skipTrackGroups(producer, 6n, 9n); + await settle(); + expect(got).toEqual([5n, 9n, 10n]); + hooks.asking(producer, false); + await reading; + expect(got).toEqual([5n, 9n, 10n, undefined]); + producer.close(); +}); + +// While a finished copy's ask is out, a reader in key order waits for a group the copy held and +// evicted, which the ask delivers again, rather than pass it. Mirrors the Rust +// `an_ordered_read_waits_on_an_evicted_group_an_ask_redelivers`. +test("an ordered read waits on an evicted group an ask redelivers", async () => { + const producer = receivedTrack({ timescale: Timescale.MILLI }); + hooks.reportsStarts(producer); + hooks.startTrack(producer, { start: 0n }); + for (let key = 0n; key <= 10n; key++) putGroup(producer, key, Number(key)); + hooks.endTrack(producer, { group: 11n, frame: 0 }); + for (let key = 0n; key < 9n; key++) hooks.evict(producer, key); + hooks.asking(producer, true); + hooks.relativeStarts(producer, [[5n, { at: 5n }, 10n]]); + const ordered = producer.subscribe({ delay: { max: null }, ranges: [{ start: { relative: 5n } }] }).ordered(); + const got: (bigint | undefined)[] = []; + const reading = (async () => { + for (;;) { + const group = await ordered.nextGroup(); + got.push(group?.start); + if (!group) return; + } + })(); + await settle(); + expect(got).toEqual([]); + for (let key = 5n; key < 9n; key++) { + putGroup(producer, key, Number(key)); + await settle(); + } + expect(got).toEqual([5n, 6n, 7n, 8n, 9n, 10n]); + hooks.asking(producer, false); + await reading; + expect(got).toEqual([5n, 6n, 7n, 8n, 9n, 10n, undefined]); + producer.close(); +}); + +// A relay serving a reader whose relative start still waits on its publisher's report can't say +// where that range starts, so the feed's start stays pending rather than answer the range as empty, +// until the report arrives or the copy closes. Mirrors the Rust `is_counting` in `poll_start`. +test("a received copy's feed start waits on a relative start still counting", async () => { + const report = (close: boolean) => { + const producer = receivedTrack({ timescale: Timescale.MILLI }); + hooks.reportsStarts(producer); + for (let key = 0n; key < 10n; key++) putGroup(producer, key, Number(key)); + const reader = producer.subscribe({ + delay: { max: null }, + ranges: [ + { start: { time: 1n }, end: { time: 2n } }, + { start: { relative: 2n }, end: { time: 8n } }, + ], + }); + expect(hooks.sourceStart(reader)).toBe("pending"); + if (close) producer.close(); + else hooks.relativeStarts(producer, [[2n, { at: 7n }, 9n]]); + return hooks.sourceStart(reader); + }; + expect(report(false)).not.toBe("pending"); + expect(report(true)).not.toBe("pending"); +}); + +// Whatever groups a lite-07 copy holds, a reader's relative start lands only where its publisher +// reported it, or waits: the cache never moves it. Mirrors the Rust +// `a_copys_cache_never_moves_a_relative_start`. +test("a copy's cache never moves a relative start", async () => { + let seed = 0x9e3779b9; + const next = (n: number) => { + seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0; + return (seed >>> 16) % n; + }; + for (let run = 0; run < 64; run++) { + const held = new Set(); + for (let i = next(12); i > 0; i--) held.add(BigInt(next(16))); + const back = BigInt(next(6)); + // Unreported half the time. + const report = next(2) === 0 ? undefined : BigInt(next(17)); + const producer = receivedTrack({ timescale: Timescale.MILLI }); + hooks.reportsStarts(producer); + for (const key of [...held].sort((a, b) => Number(a - b))) putGroup(producer, key, Number(key)); + putGroup(producer, 16n, 16); + if (report !== undefined) hooks.relativeStarts(producer, [[back, { at: report }, 16n]]); + const reader = producer.subscribe({ delay: { max: null }, ranges: [{ start: { relative: back } }] }); + await settle(); + const expected = back === 0n ? 16n : report; + const at_ = { held: [...held], back, report }; + expect({ ...at_, floor: reader.floor() }).toEqual({ ...at_, floor: expected }); + expect({ ...at_, asked: hooks.asks(producer).length }).toEqual({ + ...at_, + asked: expected === undefined ? 1 : 0, + }); + reader.close(); + producer.close(); + } +}); diff --git a/js/net/src/track.ts b/js/net/src/track.ts index 5394b907c9..d98fca21f3 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -25,6 +25,7 @@ import { type Frame, type Consumer as GroupConsumer, Producer as GroupProducer } import type { IetfVersion } from "./ietf/version.ts"; import { type Ask, + type CountedStart, cutRun, type DropRun, dropAt, @@ -57,7 +58,7 @@ import { rescale } from "./rescale.ts"; import { floorAt, Timescale, type Timestamp } from "./time.ts"; import { checkEnd, checkKey, max, min, UNBOUNDED } from "./util/key.ts"; import { checkName } from "./util/utf8.ts"; -import { bounded, type Holdings, joinsLive, rangesOf, UNHELD, Window, wireSpan } from "./window.ts"; +import { bounded, type Holdings, joinsLive, rangeKey, rangesOf, UNHELD, Window, wireSpan } from "./window.ts"; import { type Broadcast as BroadcastWire, registerTrackConsumer } from "./wire.ts"; export { type Fill, FillGroups, FillLookup, Requests } from "./fill.ts"; @@ -1009,6 +1010,10 @@ let fillFloor: (producer: Producer, named: bigint | undefined) => bigint | undef // The keys of the groups the producer caches, sorted: what relative and time anchors resolve // against, as the Rust `TrackState::lookup` keys; assigned in its static block. let keysOf: (producer: Producer) => readonly bigint[]; +// Count `ranges`, in a stable order, as the relative ranges the reader reading through `sink` waits +// on the publisher to start, in place of those counted before; see `hooks.asks`. Assigned in the +// producer's static block. +let askStarts: (producer: Producer, sink: TrackState, ranges: readonly Range[]) => void; // How a received copy resolves its readers' time anchors, as its wire tells it; assigned in the // producer's static block. See `hooks.startsAtKeys` and `hooks.timeStarts`. let timeAnchors: (producer: Producer) => { @@ -1016,7 +1021,9 @@ let timeAnchors: (producer: Producer) => { starts: ReadonlyMap; /** The newest of `starts` past the time its range asked from, 0 when none is. */ past: bigint; - relative: ReadonlyMap; + relative: ReadonlyMap; + /** Whether its publisher reports where each range starts (lite-07), which `relative` holds. */ + reported: boolean; /** Bumped whenever the others, or whether the producer is a received copy, change. */ placing: number; }; @@ -1290,7 +1297,17 @@ export class Producer { // replaced: a reader's `unplaced` reads it on every settle. Mirrors the Rust `starts_past`. #startsPast = 0n; // On a lite-07 copy, where the publisher started each live range counting back, by its count. - #relativeStarts = new Map(); + #relativeStarts = new Map(); + // Whether a copy's publisher reports where each range starts, so a relative start waits for that + // rather than count over the groups held. Mirrors the Rust `Anchors::Reported`. + #reportsStarts = false; + // The asks a finished copy's session has out (see `hooks.asking`). + #asking = 0; + // The relative ranges each reader waits on the publisher to start, which the copy's subscription + // asks upstream for. Mirrors the Rust `Subscriptions::asks`. + #readerAsks = new Map(); + // Bumped whenever a reader's entry in `#readerAsks` changes. Mirrors the Rust `asks_revision`. + #asksChanged = new Signal(0); // Counts changes to how a time start is placed other than by groups arriving or drops: the wire, // `#timeStarts`, and `#startsAtKeys`. Mirrors the Rust `TrackState::placing`. #placing = 0; @@ -1532,6 +1549,7 @@ export class Producer { starts: producer.#timeStarts, past: producer.#startsPast, relative: producer.#relativeStarts, + reported: producer.#reportsStarts, placing: producer.#placing, }); hooks.startsAtKeys = (producer) => { @@ -1546,8 +1564,68 @@ export class Producer { } producer.#placing++; }; + hooks.asks = (producer) => { + const asks = new Map(); + for (const ranges of producer.#readerAsks.values()) + for (const range of ranges) asks.set(rangeKey(range), range); + return [...asks.values()]; + }; + hooks.asksChanged = (producer) => producer.#asksChanged; + hooks.evict = (producer, key) => { + const entry = producer.#cached.get(key); + if (!entry) return; + producer.#evict(entry); + producer.#uncache(entry, true); + }; + hooks.asking = (producer, asking) => { + producer.#asking = Math.max(0, producer.#asking + (asking ? 1 : -1)); + const end = producer.#state.end.peek(); + const owed = producer.#asking > 0 || (end?.tail !== undefined && producer.#owes(finalKey(end.at))); + for (const state of [producer.#state, ...producer.#sinks]) state.owed.set(owed); + }; + askStarts = (producer, sink, ranges) => { + const last = producer.#readerAsks.get(sink) ?? []; + const same = + last.length === ranges.length && + last.every((range, index) => rangeKey(range) === rangeKey(ranges[index])); + if (same) return; + if (ranges.length === 0) producer.#readerAsks.delete(sink); + else producer.#readerAsks.set(sink, ranges); + producer.#asksChanged.update((revision) => revision + 1); + }; + hooks.reportsStarts = (producer) => { + producer.#reportsStarts = true; + }; hooks.relativeStarts = (producer, starts) => { - producer.#relativeStarts = new Map([...starts].map(([back, start, latest]) => [back, { start, latest }])); + // Ranges counting back the same number of groups report together: the one from the newer + // latest group, and from the same one, the lower start, nearer the group both counted to, + // over any range holding none, since a start holds for every range ending past it, or else + // the furthest of those. Mirrors the Rust `RelativeStart::merge`. + const next = new Map(); + for (const [back, start, latest] of starts) { + const last = next.get(back); + if (last === undefined || (latest ?? -1n) > (last.latest ?? -1n)) { + next.set(back, { start, latest }); + } else if (latest === last.latest) { + const lower = start.at !== undefined && (last.start.at === undefined || start.at < last.start.at); + const further = + last.start.at === undefined && start.past !== undefined && start.past > (last.start.past ?? 0n); + if (lower || further) next.set(back, { start, latest }); + } + } + const last = producer.#relativeStarts; + const same = + next.size === last.size && + [...next].every(([back, at]) => { + const was = last.get(back); + return ( + was?.start.at === at.start.at && was?.start.past === at.start.past && was?.latest === at.latest + ); + }); + if (same) return; + producer.#relativeStarts = next; + // A reader whose relative start waits on the report reads again. + for (const state of [producer.#state, ...producer.#sinks]) state.startChanged.update((rev) => rev + 1); }; hooks.producerLargest = (producer) => producer.#state.largest; hooks.askProducer = (producer, ask) => producer.#ask(ask); @@ -1646,6 +1724,9 @@ export class Producer { }; owedRuns = (producer, start, end) => { if (!producer.#wire) return []; + // An ask out may still deliver any gap the arrivals leave, whatever the feed's floor or grace + // (see `hooks.asking`). Mirrors the Rust `TrackState::owed`. + if (producer.#asking > 0) return [...producer.#askable(start, end)]; const floor = feedFloor(producer); return producer.#landing({ start: max(start, floor ?? 0n), @@ -1669,6 +1750,22 @@ export class Producer { return undefined; } + // The groups an ask out may still deliver in `[start, end)`: every one the copy doesn't hold and + // nothing gave up, up to the declared end, including one it held and evicted, which the ask + // delivers again. Mirrors the Rust `TrackState::owed`. + *#askable(start: bigint, end: bigint): Generator { + if (this.#state.closed.peek() !== undefined) return; + const declared = this.#state.end.peek()?.at; + if (declared !== undefined) end = min(end, finalKey(declared)); + const drops = this.#state.drops; + let next = start; + for (let index = keyIndex(this.#keys, start); next < end; index++) { + const key = min(this.#keys[index] ?? end, end); + if (key > next) yield* runGaps(drops.overlapping(next, key), next, key); + next = key + 1n; + } + } + *#landing(land: Land): Generator { if (this.#state.closed.peek() !== undefined) return; let start = land.start; @@ -2066,6 +2163,7 @@ export class Producer { // Take `sink` out of the aggregates. #leave(sink: TrackState): void { sink.update.watch = undefined; + askStarts(this, sink, []); this.#moved(sink, { before: sink.update.contribution(), gone: [...sink.update.ranges()], came: [] }); this.#counted.delete(sink); this.#subscribed(); @@ -2671,7 +2769,7 @@ export class Producer { #owe(): void { const end = this.#state.end.peek(); if (!end?.tail) return; - const owed = this.#owes(finalKey(end.at)); + const owed = this.#asking > 0 || this.#owes(finalKey(end.at)); for (const state of [this.#state, ...this.#sinks]) state.owed.set(owed); this.#endLargest(); } @@ -3629,10 +3727,23 @@ export class Subscriber { this.#moved(); } - // The window moved: narrow the bounds to it. + // The window moved: narrow the bounds to it, and ask for the relative starts it waits on. #moved(): void { this.#revision++; this.#bound(); + this.#ask(); + } + + // Count the relative ranges the window waits on the publisher to start, which a received copy's + // subscription asks upstream for. Mirrors the Rust `Asked::set`. + #ask(): void { + const producer = this.#state.producer; + if (!producer || this.#left) return; + const counting = this.#window.counting().sort((a, b) => { + const [x, y] = [rangeKey(a), rangeKey(b)]; + return x < y ? -1 : x > y ? 1 : 0; + }); + askStarts(producer, this.#state, counting); } // The groups this reader's track holds, by start, for resolving its relative and time anchors: @@ -3717,13 +3828,26 @@ export class Subscriber { return { runs, mark }; }, nextStart: (from) => keys[keyIndex(keys, from)], - // A lite-07 copy holds only part of the track, so it starts where its publisher did, - // counting from the same latest group: a newer one moved the live edge. - reportedStart: (latest, back) => { - const reported = receivedCopy(producer) ? anchors.relative.get(back) : undefined; - return reported !== undefined && reported.latest === latest ? reported.start : undefined; + // A lite-07 copy holds only part of the track, so it starts only where its publisher + // reported starting, counting from the same latest group or a newer one, and waits for that + // report otherwise, whatever it holds: its reader asks upstream for one (see `hooks.asks`). + // The latest group itself needs no count. Mirrors the Rust `TrackState::counted_start`. + countedStart: (latest, back, end) => { + if (!anchors.reported || !receivedCopy(producer)) return undefined; + if (back === 0n) return { group: latest }; + const reported = anchors.relative.get(back); + const { at, past } = reported?.start ?? {}; + if (at !== undefined && reported?.latest !== undefined && reported.latest >= latest) + return { group: at }; + // Past a range ending no earlier than this one, so this one holds no group either. Counting + // from another latest group starts no earlier, so it holds there too. + if (past !== undefined && end <= past) return { group: end }; + return "unknown"; }, back: (latest, back) => { + // Keys that are group IDs count back by ID, as a moq-transport joining FETCH does, so the + // count never depends on what this copy holds. + if (anchors.atKeys) return latest > back ? latest - back : 0n; if (back === 0n) return latest; // Bounded by the cache: a huge count reaches back to the oldest. const index = BigInt(keyIndex(keys, latest)) - back; @@ -4318,6 +4442,10 @@ export class Subscriber { // A relative anchor still pending reads every group, so a fill asked below would claim // the history it never names. Mirrors the Rust `cursor.sync()` in `poll_start`. subscriber.#settle(); + // A relative start still waiting on its publisher's report names no group yet, so where it + // starts can't be answered; a closed track answers at once, since nothing more can arrive. + // Mirrors the Rust `is_counting` in `poll_start`. + if (subscriber.#window.isCounting() && subscriber.#state.closed.peek() === undefined) return "pending"; const producer = subscriber.#state.producer; const start = producer && sourceStart(producer); if (start === "pending") return start; @@ -4969,6 +5097,9 @@ export class Subscriber { if (this.#askFills(start, end ?? UNBOUNDED, drift) !== undefined) return { kind: "idle" }; // A time start still looked up may yet take in a group held already. if (this.#window.lookups().length > 0) return { kind: "idle" }; + // So may a relative start its publisher has yet to report, which a finished copy still asks + // for. Mirrors the Rust `Cursor::awaits_report`. + if (this.#window.isCounting()) return { kind: "idle" }; // A bounded window ends once nothing in it is left to deliver, as in Rust. if (this.#exhausted()) return { kind: "done" }; if (!this.#complete() || this.#starting(groups)) return { kind: "idle" }; @@ -5613,6 +5744,8 @@ export class Subscriber { const end = this.#state.end.peek()?.at; // A time start still looked up may yet take in a group held already. if (this.#window.lookups().length > 0) return { kind: "idle" }; + // So may a relative start its publisher has yet to report, while a producer remains to record it. + if (this.#window.isCounting() && this.#state.closed.peek() === undefined) return { kind: "idle" }; if (this.#exhausted()) { this.#passEnd(); return { kind: "done" }; diff --git a/js/net/src/window.test.ts b/js/net/src/window.test.ts index 131e5207e6..8672173b56 100644 --- a/js/net/src/window.test.ts +++ b/js/net/src/window.test.ts @@ -180,7 +180,7 @@ describe("Window", () => { containing: (at) => held.containing(at), nextStart: (from) => held.nextStart(from), back: (latest, back) => held.back(latest, back), - reportedStart: (latest, back) => (latest === 9n && back === 2n ? 8n : undefined), + countedStart: (latest, back) => (latest === 9n && back === 2n ? { group: 8n } : undefined), complete: () => false, }; expect(new Window([range(relative(2n))], 9n, reported).start()).toBe(8n); @@ -188,6 +188,44 @@ describe("Window", () => { expect(covered(ended, held.held)).toEqual(keys(0n, 8n)); }); + test("a relative start reads nothing until counted", () => { + const held = new Keys([1n, 2n, 9n]); + let counted: Containing = "unknown"; + let countedFrom: bigint | undefined; + const counting: Holdings = { + resolves: () => true, + containing: (at) => held.containing(at), + nextStart: (from) => held.nextStart(from), + back: (latest, back) => held.back(latest, back), + countedStart: (latest) => { + countedFrom = latest; + return counted; + }, + complete: () => false, + }; + const window = new Window([range(relative(2n), time(8n)), range(time(1n), time(1n))], 9n, counting); + expect(window.pending).toBe(false); + expect(window.settled).toBe(false); + expect(covered(window, keys(0n, 10n))).toEqual([1n]); + expect([window.floor(), window.start(), window.end()]).toEqual([undefined, undefined, undefined]); + expect(window.resolved()[0]).toBeUndefined(); + expect(window.counting()).toEqual([range(relative(2n), time(8n))]); + + // A report counted from the latest group it first counted from, or a newer one, starts it. + counted = { group: 7n }; + expect(window.settle(12n, counting)).toBe(true); + expect(countedFrom).toBe(9n); + expect(window.settled).toBe(true); + expect(window.counting()).toEqual([]); + expect(covered(window, keys(0n, 10n))).toEqual([1n, 7n, 8n]); + expect([window.start(), window.end()]).toEqual([1n, 9n]); + + // Counting back past every group the track holds reaches back to the oldest. + counted = "before"; + const oldest = new Window([range(relative(5n))], 9n, counting); + expect([oldest.floor(), oldest.start()]).toEqual([0n, undefined]); + }); + test("a relative start past every group asks from the oldest group held when placed", () => { const held = new Keys([33n, 66n, 99n]); const window = new Window([range(relative(10n))], 99n, held); diff --git a/js/net/src/window.ts b/js/net/src/window.ts index f99e24ac20..1712f93d58 100644 --- a/js/net/src/window.ts +++ b/js/net/src/window.ts @@ -307,13 +307,13 @@ export function clamped( }); } -/** Where a time falls among the groups a hop holds. Mirrors the Rust `Containing`. */ +/** Where a time, or a count back from the latest group, falls among the groups a hop holds. Mirrors the Rust `Containing`. */ export type Containing = - /** The newest group starting at or before it. */ + /** The newest group starting at or before it, or the group counted back to. */ | { group: bigint } /** Every group held starts after it. */ | "before" - /** No group is held. */ + /** The groups held can't place it yet. */ | "unknown"; /** @@ -336,12 +336,14 @@ export interface Holdings { */ back(latest: bigint, back: bigint): bigint | undefined; /** - * Where the publisher reported starting a range counting `back` groups before `latest`, which a - * relative start, but never an end, takes over {@link back}: a delivery start may sit past the - * group counted, such as above groups too stale for the budget. Absent for none. Mirrors the - * Rust `Holdings::reported_start`. + * Where a range counting `back` groups before `latest`, up to the exclusive `end` ({@link UNBOUNDED} + * for none), starts, which a relative start, but never an end, takes over {@link back}: on a + * received copy, where its publisher reported starting one counted from `latest` or a newer + * group, since the copy holds only part of the track and a delivery start may sit past the group + * counted, or `"unknown"` until it reports one. Undefined, or absent, counts it with + * {@link back}. Mirrors the Rust `Holdings::counted_start`. */ - reportedStart?(latest: bigint, back: bigint): bigint | undefined; + countedStart?(latest: bigint, back: bigint, end: bigint): Containing | undefined; /** Whether nothing more can arrive here, so a time start nothing contains reaches back to the oldest group rather than wait. */ complete(): boolean; /** @@ -470,6 +472,12 @@ export class Window { * live edge, so they read nothing yet: a group held is no reason to start at it. */ #reaching = new Set(); + /** + * The ranges, by key, whose relative start waits on its publisher to say where it counted back to + * (see {@link Holdings.countedStart}), each with the latest group it counted from then, so they + * read nothing yet: a group held may sit below where the publisher started. + */ + #counting = new Map(); // How many spans name each start, and how many name none: see {@link start}. #named = new Counts(); #unnamed = 0; @@ -551,6 +559,7 @@ export class Window { #retire(key: bigint): void { this.#pending.delete(key); this.#reaching.delete(key); + this.#counting.delete(key); const span = this.#spans.get(key); if (span) this.#uncount(key, span); } @@ -560,12 +569,25 @@ export class Window { return this.#pending.size > 0; } + /** Whether a relative start still waits on its publisher to say where it starts. Mirrors the Rust `is_counting`. */ + isCounting(): boolean { + return this.#counting.size > 0; + } + + /** + * The ranges whose relative start waits on its publisher to say where it starts, which a received + * copy asks its upstream for. Mirrors the Rust `Window::counting`. + */ + counting(): Range[] { + return [...this.#counting.keys()].flatMap((key) => this.#ranges.get(key) ?? []); + } + /** - * The keys of the ranges still waiting on a relative anchor, a live join's reach back, or a - * time start. Mirrors the Rust `Window::unsettled`. + * The keys of the ranges still waiting on a relative anchor, its publisher's report, a live + * join's reach back, or a time start. Mirrors the Rust `Window::unsettled`. */ unsettled(): Set { - const keys = new Set([...this.#pending, ...this.#reaching]); + const keys = new Set([...this.#pending, ...this.#reaching, ...this.#counting.keys()]); for (const entry of this.#waiting.all()) for (const key of entry.keys) keys.add(key); for (const entry of this.#looking.all()) for (const key of entry.keys) keys.add(key); return keys; @@ -595,6 +617,15 @@ export class Window { for (const key of reaching) this.#placeRange(key, latest, holdings); moved = this.#spans.size > placed; } + // Each from the latest group it first counted from: a report counted from that one or a newer + // one starts it. + if (this.#counting.size > 0) { + const placed = this.#spans.size; + const counting = [...this.#counting]; + this.#counting.clear(); + for (const [key, since] of counting) this.#placeRange(key, since, holdings); + if (this.#spans.size > placed) moved = true; + } if (this.#waiting.size > 0 && this.#startWaiting(holdings)) moved = true; return moved; } @@ -669,7 +700,7 @@ export class Window { * anchor waits. */ start(): bigint | undefined { - if (this.pending || this.#reaching.size > 0 || this.#unnamed > 0) return undefined; + if (this.pending || this.#reaching.size > 0 || this.#counting.size > 0 || this.#unnamed > 0) return undefined; return this.#named.first(); } @@ -789,9 +820,10 @@ export class Window { return this.#named.first(); } - // Whether a time start or a join's reach back waits on the groups held to place it. + // Whether a time start or a join's reach back waits on the groups held to place it, or a + // relative start on its publisher. #unplaced(): boolean { - return this.#waiting.size > 0 || this.#reaching.size > 0; + return this.#waiting.size > 0 || this.#reaching.size > 0 || this.#counting.size > 0; } // Resolve every range again from scratch. @@ -801,6 +833,7 @@ export class Window { this.#waiting = new ByTime(); this.#looking = new ByTime(); this.#reaching.clear(); + this.#counting.clear(); this.#groups = new Cover(); this.#missable = new Cover(); this.#named = new Counts(); @@ -834,11 +867,16 @@ export class Window { } anchor = reached.anchor; } - const start = startOf(anchor, latest, holdings); + const start = startOf(anchor, latest, until, holdings); if (start === "pending") { this.#pending.add(key); return; } + if (start === "counting") { + // Counting needs a latest group to count from. + if (latest !== undefined) this.#counting.set(key, latest); + return; + } if (start === "waiting") { // Reads only the groups from its time until the groups held place it; see #startWaiting. this.#count(key, { anchor, start: 0n, end: until, floored: false, waiting: true }); @@ -1017,20 +1055,26 @@ function joins(range: Range, min: Duration | undefined, holdings: Holdings): Dur /** * The first group a range starts at, and whether it names that group rather than reaching back to - * the oldest one. `"pending"` while it waits on a latest group, reading everything meanwhile, and - * `"waiting"` for a time start the groups held can't place yet. Mirrors the Rust `start`. + * the oldest one. `"pending"` while it waits on a latest group, reading everything meanwhile, + * `"waiting"` for a time start the groups held can't place yet, and `"counting"` for a relative + * start its publisher has yet to say where it starts, reading nothing meanwhile. Mirrors the Rust + * `start`. */ function startOf( anchor: Anchor | undefined, latest: bigint | undefined, + until: bigint, holdings: Holdings, -): { group: bigint; floored: boolean } | "waiting" | "pending" { +): { group: bigint; floored: boolean } | "waiting" | "pending" | "counting" { // Nothing sits below group 0, so the oldest start needs no latest group. if (anchor === undefined) return { group: 0n, floored: false }; if (anchor.relative !== undefined) { if (latest === undefined) return "pending"; - const group = holdings.reportedStart?.(latest, anchor.relative) ?? holdings.back(latest, anchor.relative); + const counted = holdings.countedStart?.(latest, anchor.relative, until); + if (counted === "unknown") return "counting"; // Fewer groups are held than it counts back, so reach back to the oldest. + if (counted === "before") return { group: 0n, floored: false }; + const group = counted?.group ?? holdings.back(latest, anchor.relative); return group === undefined ? { group: 0n, floored: false } : { group, floored: true }; } // No group starts before 0, so time 0 names group 0 whatever is held. diff --git a/quest/m2/time-addressing/late-relative-end.md b/quest/m2/time-addressing/late-relative-end.md new file mode 100644 index 0000000000..910fa94763 --- /dev/null +++ b/quest/m2/time-addressing/late-relative-end.md @@ -0,0 +1,41 @@ +# [M] A relative end on a partial lite-07 copy ends where its publisher's does + +## Goal + +A reader whose range ends at a relative anchor, joining a lite-07 received +copy that already holds part of the track, reads through the group its +publisher counts back to and no further, in Rust and `@moq/net`. Today the +copy counts the end back over the groups it happens to hold: over a copy +holding groups 1 and 2 of 0..9, a later reader of `[{time:0..relative:1}]` +counts one back from 9 to group 2, so it reads 1, 2, 0 and ends, where it +should read 0 through 8. + +## Plan + +Found by [moq-dev/moq#9198](https://github.com/moq-dev/moq/pull/9198), which +fixed the same bug for a relative start. Under the maintainer's rule there, a +subscription never changes behavior based on cache state: a start resolves only +from its publisher's report (SUBSCRIBE_OK or SUBSCRIBE_STARTED, kept in +`TrackState::relative_starts` in `rs/moq-net/src/model/track.rs`), and a reader +no report answers asks the copy's upstream for its range and waits (the asks in +`model/track/demand.rs`). An end has no such report: the publisher reports +where each range starts, never where it ends. So #9198 left ends counting over +the cache (`end` in `model/subscription/window.rs`), which breaks the same rule. + +- Reproduce first, as `range_anchors.rs` and `js/net/src/integration.test.ts` + tests beside `a_later_relative_start_on_a_partial_copy_reads_only_its_groups`. +- Recommendation: carry each range's resolved end beside its start in + SUBSCRIBE_OK and SUBSCRIBE_STARTED. Lite-07 is still `-wip`, so the wire may + change freely. The copy then resolves a relative end from that report, as + `relative_starts` does for starts, and asks for it and waits the way a start + does. +- Alternative: the copy treats a bounded range as ended once the subscription + accounts for every group it delivered. That needs no wire change, but the + copy can't tell a range that ended from one still owed a late group. + +## Waiting + +- Maintainer: whether lite-07 SUBSCRIBE_OK and SUBSCRIBE_STARTED gain a + resolved Range End (recommended, since the draft is unpublished), or the + copy infers the end from accounting with no wire change. An agent can't + decide a wire change on its own. diff --git a/quest/m2/time-addressing/late-relative-join.md b/quest/m2/time-addressing/late-relative-join.md deleted file mode 100644 index 629536f579..0000000000 --- a/quest/m2/time-addressing/late-relative-join.md +++ /dev/null @@ -1,34 +0,0 @@ -# [M] A later reader's relative start on a partial lite-07 copy reads only its groups - -## Goal - -A reader that joins a lite-07 received copy already holding part of the -track, with a relative start, reads the groups its publisher counts back to -and nothing below them, in Rust and `@moq/net`. Today it resolves the start -against the groups the copy holds before SUBSCRIBE_STARTED can report the -publisher's: over a copy holding groups 1 and 2 of 0..9, a second reader of -`[{relative:2..time:8}]` reads 1, 2, 7, 8, then 3, 4, 5 and on, where it -should read 7 and 8 and end. - -## Plan - -Found by [moq-dev/moq#9132](https://github.com/moq-dev/moq/pull/9132), which -reports each live range's relative start on the copy -(`TrackState::relative_starts` in `rs/moq-net/src/model/track.rs`) but only -helps a reader whose start resolves after the report lands: the first reader, -whose relative start waits on SUBSCRIBE_START. A later reader's window -resolves at once (`Window::new` in `model/subscription/window.rs`), and -`Holdings::back` counts the copy's cache. - -- Reproduce first: the second-reader case above, as a `range_anchors.rs` - integration test, and the same in `js/net/src/integration.test.ts`. -- Decided 2026-11-19 (coordinator, maintainer away, merge granted), taking - the recommendation: on a received copy, count back over the cache only where - the copy holds every group from the counted one to its latest (received - contiguously since it went live), and otherwise leave the relative start - waiting, as a time start waits (`Start::Waiting`), until the range's - SUBSCRIBE_STARTED reports it. Rejected: always waiting on the report, - since an update that coalesces the reader's range into another one - (`fit` in `lite/subscriber/fit.rs`) gets no SUBSCRIBE_STARTED of its own and would hang. -- The waiting reader must still settle when no report can come: the copy - ends, or its feed goes idle. diff --git a/rs/moq-net/proptest-regressions/model/track.txt b/rs/moq-net/proptest-regressions/model/track.txt new file mode 100644 index 0000000000..8e470d0497 --- /dev/null +++ b/rs/moq-net/proptest-regressions/model/track.txt @@ -0,0 +1,7 @@ +# Seeds for failure cases proptest has generated in the past. It is +# automatically read and these particular cases re-run before any +# novel cases are generated. +# +# It is recommended to check this file in to source control so that +# everyone who runs the test benefits from these saved cases. +cc dc24e7ccea720fe2e18cea7e4bf6f9ce0876cb5bf500446c96abd65758cb2083 # shrinks to held = {}, back = 1, reported = None diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 360f882bd3..7737d15713 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -21,6 +21,7 @@ use crate::tail::{self, Reading, Settle, Tail}; use kio::Lock; +mod ask; mod fills; use fills::{FillAsk, RangeFills}; mod fit; @@ -3219,6 +3220,7 @@ mod tests { live: lite::LiveRanges::new(Version::Lite04, &[lite::Range::OLDEST]), demand: Some(Subscription::default()), fill_ids: Default::default(), + ask_ids: Default::default(), fill_groups: Default::default(), tail: tail.clone(), started: None, @@ -3381,6 +3383,7 @@ mod tests { live: lite::LiveRanges::new(Version::Lite04, &[lite::Range::span(Some(floor), 0, None, None)]), demand: Some(Subscription::default()), fill_ids: Default::default(), + ask_ids: Default::default(), fill_groups: Default::default(), tail: Default::default(), started: None, @@ -3860,6 +3863,7 @@ mod tests { live: lite::LiveRanges::new(VERSION, &[lite::Range::span(Some(2), 0, None, None)]), demand: Some(Subscription::default()), fill_ids: Default::default(), + ask_ids: Default::default(), fill_groups: Default::default(), tail: Default::default(), started: None, @@ -3882,7 +3886,7 @@ mod tests { // 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.edit(&ranges, 0).unwrap(); + sub.edit(&ranges, 0, 0, None).unwrap(); }; sub.started = Some(None); update(&mut sub, Some(1), vec![lite::Range::span(Some(1), 0, None, None)]); @@ -3899,6 +3903,8 @@ mod tests { lite::Range::span(Some(6), 0, None, None), ], 0, + 0, + None, ) .unwrap(); assert_eq!( @@ -3977,6 +3983,7 @@ mod tests { live: lite::LiveRanges::new(Version::Lite07, &[demand]), demand: Some(Subscription::default()), fill_ids: Default::default(), + ask_ids: Default::default(), fill_groups: Default::default(), tail: Default::default(), started: Some(Some(4)), @@ -3993,18 +4000,18 @@ mod tests { 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(); + sub.edit(&[demand, demand], 0, 1, None).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(); + sub.edit(&[demand, demand, other], 0, 2, None).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(); + sub.edit(&[moved, demand, other], 0, 2, None).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"); @@ -4012,14 +4019,82 @@ mod tests { // 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 = None; - sub.edit(&[moved, demand], 1).unwrap(); + sub.edit(&[moved, demand], 0, 1, None).unwrap(); assert_eq!(sub.fill_range(2), None, "retired"); assert!(sub.fill_ids.contains(&2), "still a fill's"); sub.started = Some(Some(4)); - sub.edit(&[moved, demand], 1).unwrap(); + sub.edit(&[moved, demand], 0, 1, None).unwrap(); assert!(!sub.fill_ids.contains(&2), "forgotten once started"); } + /// An ask for a relative range the demand adds in the same edit takes no range of its own, + /// since the publisher answers the demand's from its latest group too; one for a range the + /// demand already has live does, since that one's answer may predate the reader. + #[kio::test] + async fn a_lite07_ask_the_demand_adds_takes_no_range_of_its_own() { + let session = crate::lite::test_transport::ScriptedSession::new(Vec::new()); + let stream = Stream::open(&crate::driver::Transport::new(session.clone()), Version::Lite07) + .await + .unwrap(); + let live = lite::Range { + start: Some(lite::Anchor::Time(0)), + end: None, + }; + let back = lite::Range { + start: Some(lite::Anchor::Relative(1)), + end: None, + }; + let mut sub = SubStream { + order: crate::track::Order::Descending, + stream, + id: 0, + max_delay: None, + stale_budget: Duration::ZERO, + min_delay: None, + start: Some(0), + priority: 0, + requested: Some(0), + live: lite::LiveRanges::new(Version::Lite07, &[live]), + demand: Some(Subscription::default()), + fill_ids: Default::default(), + ask_ids: Default::default(), + fill_groups: Default::default(), + tail: Default::default(), + started: Some(Some(0)), + finishing: None, + parked: false, + starts: [(0, Some(0))].into(), + edges: Default::default(), + declared: None, + end: None, + wanted: None, + unanswered: None, + _working: crate::session::Work::default().start(), + }; + + let (edit, _) = sub.edit(&[live, back, back], 1, 0, Some(3)).unwrap(); + assert_eq!( + edit, + lite::RangeEdit::Edit { + remove: vec![], + add: vec![back] + }, + "the demand's range answers the ask" + ); + assert!(sub.ask_ids.is_empty()); + + // The demand's range is live now, so another reader's ask takes one of its own. + let (edit, _) = sub.edit(&[live, back, back], 1, 0, Some(5)).unwrap(); + assert_eq!( + edit, + lite::RangeEdit::Edit { + remove: vec![], + add: vec![back] + } + ); + assert_eq!(sub.ask_ids.iter().copied().collect::>(), [2]); + } + /// 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] @@ -4046,6 +4121,7 @@ mod tests { live: lite::LiveRanges::new(Version::Lite07, &[first]), demand: Some(Subscription::default()), fill_ids: Default::default(), + ask_ids: Default::default(), fill_groups: Default::default(), tail: Default::default(), started: Some(Some(4)), @@ -4066,7 +4142,7 @@ mod tests { start: Some(lite::Anchor::Relative(2)), end: None, }; - let (edit, changed) = sub.edit(&[first, back], 0).unwrap(); + let (edit, changed) = sub.edit(&[first, back], 0, 0, None).unwrap(); assert!(changed); assert_eq!( edit, @@ -4080,7 +4156,7 @@ mod tests { assert_eq!(sub.owed(None), Some(vec![4..10, 1..10])); // Retiring the first range forgets its start. - sub.edit(&[back], 0).unwrap(); + sub.edit(&[back], 0, 0, None).unwrap(); assert_eq!(sub.starts.keys().copied().collect::>(), [1]); assert_eq!(sub.owed(Some(8)), Some(vec![1..8])); } @@ -5568,6 +5644,7 @@ mod tests { live: lite::LiveRanges::new(version, &[lite::Range::OLDEST]), demand: Some(Subscription::default()), fill_ids: Default::default(), + ask_ids: Default::default(), fill_groups: Default::default(), tail: tail.clone(), started: None, @@ -5988,13 +6065,14 @@ mod tests { crate::track::Info::timed(), ); let track = serve_loop.serving.clone(); - let finished = FinishedServe { - serving: Some(serve_loop.serving), - demand: serve_loop.demand, - dynamic: serve_loop.dynamic.fetches_only(), - fetches: serve_loop.fetches, - closed: serve_loop.closed, - }; + let finished = FinishedServe::new( + &serve, + serve_loop.serving, + serve_loop.demand, + serve_loop.dynamic.fetches_only(), + serve_loop.fetches, + serve_loop.closed, + ); // A reader holds the finished copy. let _reader = broadcast .consume() @@ -7434,6 +7512,7 @@ mod tests { subscription: Subscription::default(), demand: Some(Subscription::default()), demand_ranges: vec![lite::Range::OLDEST], + asks: 0, ranges: vec![lite::Range::OLDEST], }; let establish = serve.prepare_establish(&mut serving.serving, wire, &Default::default()); @@ -7600,6 +7679,7 @@ mod tests { subscription: Subscription::default(), demand: Some(Subscription::default()), demand_ranges: vec![lite::Range::OLDEST], + asks: 0, ranges: vec![lite::Range::OLDEST], }; let reader = serving.serving.subscribe(None); @@ -7665,6 +7745,7 @@ mod tests { subscription: Subscription::default(), demand: Some(Subscription::default()), demand_ranges: vec![lite::Range::OLDEST], + asks: 0, ranges: vec![lite::Range::OLDEST], }; let establish = serve.prepare_establish(&mut serving.serving, wire, &Default::default()); @@ -10158,6 +10239,10 @@ struct SubStream { /// and until SUBSCRIBE_START even once retired, since it answers every range the /// SUBSCRIBE carried. fill_ids: std::collections::BTreeSet, + /// The Range IDs of the asks' ranges: a relative range a reader of the copy waits on the + /// publisher to start (see [`track::Producer::asks`]), which says nothing of the live feed + /// either. Kept like [`Self::fill_ids`]. + ask_ids: std::collections::BTreeSet, /// The groups the ranges ask for fills. fill_groups: rangemap::RangeSet, priority: i8, @@ -10218,7 +10303,7 @@ impl SubStream { return None; } let mut start: Option = None; - for (id, _) in self.live.iter().filter(|(id, _)| !self.fill_ids.contains(id)) { + for (id, _) in self.live.iter().filter(|(id, _)| self.feeds(*id)) { match self.starts.get(&id) { // A range with no group there serves nothing to start from. Some(Some(at)) => start = Some(start.map_or(*at, |start| start.min(*at))), @@ -10242,15 +10327,24 @@ impl SubStream { let mut times = Vec::new(); let mut relative = Vec::new(); for (id, range) in self.live.iter() { - let Some(Some(start)) = self.starts.get(&id) else { + // `None` for a range with no group to deliver. + let Some(&start) = self.starts.get(&id) else { continue; }; - match range.start { - Some(lite::Anchor::Absolute(time)) => times.push((time.group, *start)), - None => times.push((0, *start)), - Some(lite::Anchor::Relative(back)) => { + match (range.start, start) { + (Some(lite::Anchor::Absolute(time)), Some(start)) => times.push((time.group, start)), + // From the oldest group, as one asked from group 0. + (None, Some(start)) => times.push((0, start)), + (Some(lite::Anchor::Relative(back)), start) => { if let Some(Some(latest)) = self.edges.get(&id) { - relative.push((back, *start, *latest)); + // With no group to deliver, the start lies at or past the range's end: + // an absolute end names its exclusive group, and an open one the latest. + let start = match (start, range.end) { + (Some(start), _) => track::CountedStart::At(start), + (None, Some(lite::Anchor::Absolute(end))) => track::CountedStart::Past(end.group), + (None, _) => track::CountedStart::Past(latest.saturating_add(1)), + }; + relative.push((back, start, *latest)); } } _ => {} @@ -10371,33 +10465,91 @@ impl SubStream { } } - /// 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. + /// Whether the live range `id` is the demand's, which alone says where the live feed is. + fn feeds(&self, id: u64) -> bool { + !self.fill_ids.contains(&id) && !self.ask_ids.contains(&id) + } + + /// Edit the live ranges into `ranges`: the demand's, then `asks` asks', then `fills` held + /// fills', forgetting the starts of those retired. A fill or ask asking for what a demand + /// range does still takes a range of its own, so each Range ID stays one owner's, except an + /// ask for a range the demand adds here: the publisher answers that one from its latest + /// group too. An ask its publisher answered counting from before `latest`, the copy's + /// latest group, is asked again: a reader waiting on it joined since. /// Returns the edit, and whether it changed anything. - fn edit(&mut self, ranges: &[lite::Range], fills: usize) -> Result<(lite::RangeEdit, bool), Error> { + fn edit( + &mut self, + ranges: &[lite::Range], + asks: usize, + fills: usize, + latest: Option, + ) -> Result<(lite::RangeEdit, bool), Error> { let before: Vec<_> = self.live.iter().map(|(id, _)| id).collect(); - let demand = ranges.len().saturating_sub(fills); + let demand = ranges.len().saturating_sub(asks + fills); + let fed: std::collections::HashSet<_> = self + .live + .iter() + .filter(|(id, _)| self.feeds(*id)) + .map(|(_, range)| *range) + .collect(); + let added: std::collections::HashSet<_> = + ranges[..demand].iter().filter(|range| !fed.contains(range)).collect(); let keyed: Vec<_> = ranges .iter() .enumerate() - .map(|(index, range)| (index >= demand, *range)) + .filter(|(index, range)| !(demand..demand + asks).contains(index) || !added.contains(range)) + .map(|(index, range)| { + let owner = match index { + index if index < demand => Owner::Demand, + index if index < demand + asks => Owner::Ask, + _ => Owner::Fill, + }; + (owner, *range) + }) .collect(); - let fill_ids = &self.fill_ids; - let (edit, added) = self.live.edit_keyed(&keyed, |id| fill_ids.contains(&id))?; + let (fill_ids, ask_ids, starts, edges) = (&self.fill_ids, &self.ask_ids, &self.starts, &self.edges); + let owner = |id| match () { + _ if fill_ids.contains(&id) => Owner::Fill, + _ if ask_ids.contains(&id) + && starts.contains_key(&id) + && edges.get(&id).is_some_and(|edge| *edge < latest) => + { + Owner::Stale + } + _ if ask_ids.contains(&id) => Owner::Ask, + _ => Owner::Demand, + }; + let (edit, added) = self.live.edit_keyed(&keyed, owner)?; let changed = !self.live.iter().map(|(id, _)| id).eq(before); let live = &self.live; self.starts.retain(|id, _| live.get(*id).is_some()); self.edges.retain(|id, _| live.get(*id).is_some()); if self.started.is_some() { self.fill_ids.retain(|id| live.get(*id).is_some()); + self.ask_ids.retain(|id| live.get(*id).is_some()); + } + for (id, owner) in added { + match owner { + Owner::Fill => self.fill_ids.insert(id), + Owner::Ask => self.ask_ids.insert(id), + Owner::Demand | Owner::Stale => false, + }; } - self.fill_ids - .extend(added.into_iter().filter(|(_, fill)| *fill).map(|(id, _)| id)); Ok((edit, changed)) } } +/// Who a live range on a lite-07 subscription asks for. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +enum Owner { + Demand, + /// A reader waiting on the publisher to start a relative range. + Ask, + /// An ask answered before the copy's latest group, which no wanted range keeps. + Stale, + Fill, +} + #[expect( clippy::large_enum_variant, reason = "one per track copy, which holds a live stream while anyone reads it, so boxing it would add an allocation per upstream subscription without saving memory" @@ -10802,6 +10954,15 @@ impl TrackServe { (pref, _) => pref, }; let request = self.wire_request(pref, producer.idle_newest().filter(|_| opening)); + // The relative ranges readers wait on the publisher to start, each asked as a range of + // its own (see `Owner::Ask`), alongside a demand that covers their groups. + let mut asks = match (ranged, &request) { + (true, Some(_)) => wire_ranges(&Subscription::default().with_ranges(producer.asks())).unwrap_or_default(), + _ => Vec::new(), + }; + // A relative end goes upstream open, so two asks may carry the same range. + let mut seen = std::collections::HashSet::new(); + asks.retain(|range| seen.insert(*range)); // The demand's ranges and the fills' groups, widened together until they fit. let (request, groups) = match ranged { true => { @@ -10813,9 +10974,11 @@ impl TrackServe { Some((demand, ranges)) => (Some(demand), ranges), None => (None, Vec::new()), }; - let count = demand_ranges.len() + fills.groups.iter().count(); + let count = demand_ranges.len() + asks.len() + fills.groups.iter().count(); let fields = self.subscribe_fields(demand.as_ref().unwrap_or(&fills.subscription())); - let budget = fit::Budget::new(self.subscriber.version, fields, next, count); + let mut budget = fit::Budget::new(self.subscriber.version, fields, next, count); + // The asks that don't fit wait for those sent to be answered, which retires them. + asks = budget.take(asks); let (demand_ranges, groups) = fit::fit(demand_ranges, &fills.groups, &range_fills.avoid(), budget)?; // A fill's range starting past its run speaks only while that run is asked whole. range_fills.retire_skipped(&groups); @@ -10827,16 +10990,18 @@ impl TrackServe { true => &range_fills.ask, false => &none, }; - // What goes on the wire: the demand's ranges, then the fills', under the demand's - // parameters. The demand alone sets the floor. + // What goes on the wire: the demand's ranges, the asks', 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(asks.iter().copied()); ranges.extend(FillAsk::wire(&groups)); Some(Wire { subscription: fills.merge(demand.clone()), demand: Some(demand), demand_ranges, + asks: asks.len(), ranges, }) } @@ -10845,6 +11010,7 @@ impl TrackServe { subscription: fills.subscription(), demand: None, demand_ranges: Vec::new(), + asks: 0, ranges: FillAsk::wire(&groups).collect(), }), }; @@ -10863,6 +11029,7 @@ impl TrackServe { subscription, demand, demand_ranges, + asks, ranges, } = wire; // Downstream preferences changed: forward them upstream as a @@ -10882,7 +11049,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 (edit, changed) = active.edit(&ranges, ranges.len() - demand_ranges.len())?; + let fills = ranges.len() - demand_ranges.len() - asks; + let (edit, changed) = active.edit(&ranges, asks, fills, producer.latest_live())?; let widened = (changed && demand.is_some()) .then(|| subscription.start().or(active.started.flatten()).unwrap_or(0)..u64::MAX); if let Ok(mut tail) = active.tail.write() { @@ -11046,6 +11214,7 @@ impl TrackServe { subscription, demand, demand_ranges, + asks, ranges, } = wire; // One asking only for fills is no live feed, so it leaves the floor alone. @@ -11063,6 +11232,7 @@ impl TrackServe { ); establish.demand = demand; establish.demand_ranges = demand_ranges; + establish.asks = asks; establish.fill_groups = fills.clone(); establish } @@ -11132,6 +11302,7 @@ impl TrackServe { id, demand: Some(subscription.clone()), demand_ranges: ranges.clone(), + asks: 0, fill_groups: Default::default(), fills_changed: false, subscription, @@ -11159,6 +11330,7 @@ impl TrackServe { subscription: subscription.clone(), demand: Some(subscription), demand_ranges: ranges.clone(), + asks: 0, ranges, }; let mut est = Box::new(self.prepare_establish(producer, wire, &Default::default())); @@ -11241,6 +11413,8 @@ struct Wire { subscription: Subscription, demand: Option, demand_ranges: Vec, + /// How many asks follow the demand's ranges in `ranges`, before the fills'. + asks: usize, ranges: Vec, } @@ -11262,11 +11436,13 @@ struct Establish { demand: Option, /// The demand's ranges, which come first on the wire. demand_ranges: Vec, + /// How many asks follow them, before the fills' ranges. + asks: usize, /// Fill preferences changed while opening and need an update once active. fills_changed: bool, /// The groups it asks for fills. fill_groups: rangemap::RangeSet, - /// The ranges the SUBSCRIBE carries: the demand's, then the fills'. + /// The ranges the SUBSCRIBE carries: the demand's, the asks', then the fills'. ranges: Vec, tail: kio::Producer, /// Its group streams stay parked once it is active; see [`SubStream::parked`]. @@ -11345,8 +11521,9 @@ impl Establish { start: self.subscription.start(), 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(), + // The ranges take Range IDs in order: the demand's, the asks', then the fills'. + ask_ids: (self.demand_ranges.len() as u64..(self.demand_ranges.len() + self.asks) as u64).collect(), + fill_ids: ((self.demand_ranges.len() + self.asks) as u64..self.ranges.len() as u64).collect(), fill_groups: self.fill_groups.clone(), priority: self.subscription.priority, order: self.subscription.order, @@ -11433,13 +11610,37 @@ struct FinishedServe { dynamic: track::Requests, fetches: kio::Tasks, closed: Transport, + /// Lite-07: the subscription asking where the readers' relative starts begin, which no + /// other subscription is left to carry. + ask: Option, + /// Whether the readers' asks changed since the last [`ask::Ask`] went out, or none has. + ask_due: bool, } impl FinishedServe { + fn new( + serve: &TrackServe, + serving: track::Producer, + demand: track::Demand, + dynamic: track::Requests, + fetches: kio::Tasks, + closed: Transport, + ) -> Self { + Self { + serving: Some(serving), + demand, + dynamic, + fetches, + closed, + ask: None, + ask_due: serve.subscriber.version.has_ranges(), + } + } + /// Ready once nobody reads the copy and no fetch is in flight, or the session closed. fn poll(&mut self, serve: &TrackServe, waiter: &kio::Waiter) -> Poll<()> { loop { - let Some(serving) = &self.serving else { + let Some(serving) = &mut self.serving else { return Poll::Ready(()); }; match self.dynamic.poll_requested_groups(waiter) { @@ -11456,6 +11657,29 @@ impl FinishedServe { Poll::Pending => {} } let _ = self.fetches.poll(waiter); + // A reader waiting on the publisher to start a relative range asks on a subscription + // of its own, one at a time: an answer moving the readers changes the asks again. + if serve.subscriber.version.has_ranges() && serving.poll_asks_changed(waiter).is_ready() { + self.ask_due = true; + } + if let Some(ask) = &mut self.ask + && let Poll::Ready(res) = ask.poll(waiter) + { + self.ask = None; + // Its readers have no answer coming, as when a live copy's subscription fails. + if let Err(err) = res { + if let Some(serving) = self.serving.take() { + let _ = serving.abort(err); + } + return Poll::Ready(()); + } + } + if self.ask.is_none() && std::mem::take(&mut self.ask_due) { + self.ask = ask::Ask::new(serve, serving); + if self.ask.is_some() { + continue; + } + } // The groups it already holds stay readable, its clean end too once settled. if let Poll::Ready(err) = self.closed.poll_closed(waiter) { if let Some(serving) = self.serving.take() { @@ -11888,13 +12112,14 @@ impl TrackServeRun { // A reader's miss in flight is settled by the tail instead. let mut fetches = serve_loop.fetches; fetches.retain(|run| run.fill.is_fetch()); - self.finished = Some(FinishedServe { - serving: Some(serve_loop.serving), - demand: serve_loop.demand, - dynamic: serve_loop.dynamic.fetches_only(), + self.finished = Some(FinishedServe::new( + &self.serve, + serve_loop.serving, + serve_loop.demand, + serve_loop.dynamic.fetches_only(), fetches, - closed: serve_loop.closed, - }); + serve_loop.closed, + )); } if let Some(datagrams) = &mut self.datagrams { datagrams.end(Ok(())); @@ -12434,8 +12659,9 @@ impl ServeLoop { // Marked received before any reader sees it, so none resolves an anchor this wire dropped. let serving = request.stage(info); serving.received(); + // Every version that resolves them also reports where each range starts. if serve.subscriber.version.resolves_anchors() { - serving.anchored(); + serving.reported(); } // A wire without ranges starts a time anchor at the first group at or after it, so // its readers do too, rather than at an earlier group the copy happens to hold. @@ -12622,8 +12848,8 @@ impl ServeLoop { } 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) => {} + // Nor does an ask, or a fill an update retired since. + None if !active.feeds(id) => {} // Only the demand's ranges say where the live feed is served from. No range // starting anywhere leaves nothing owed. None => { @@ -12644,9 +12870,10 @@ impl ServeLoop { { self.serving.report_edge(largest.position.group); } - // Only a live fill's Range ID matters from here. + // Only a live fill's or ask's Range ID matters from here. let live = &active.live; active.fill_ids.retain(|id| live.get(*id).is_some()); + active.ask_ids.retain(|id| live.get(*id).is_some()); // One asking only for fills feeds nothing live. if active.demand.is_some() && serve.subscriber.version.has_largest() { // A bounded feed that never delivers Largest goes live at the @@ -13033,6 +13260,13 @@ impl ServeLoop { Poll::Ready(Err(err)) => return Poll::Ready(ServeEnd::GiveBack(err)), Poll::Pending => {} } + // A reader waiting on the publisher to start a relative range asks upstream. + if serve.subscriber.version.has_ranges() && self.serving.poll_asks_changed(waiter).is_ready() { + if let Err(err) = self.begin(serve, self.serving.subscription()) { + return Poll::Ready(ServeEnd::GiveBack(err)); + } + continue; + } // The wanted group is subscription state: an update announces each new one // past our largest, and every later update restates it. Joins asking for diff --git a/rs/moq-net/src/lite/subscriber/ask.rs b/rs/moq-net/src/lite/subscriber/ask.rs new file mode 100644 index 0000000000..eecbf24a7c --- /dev/null +++ b/rs/moq-net/src/lite/subscriber/ask.rs @@ -0,0 +1,216 @@ +//! A finished lite-07 copy asking its upstream where its readers' relative starts begin. + +use std::task::{Poll, ready}; + +use super::{Accepted, Establish, SubStream, TrackServe, wire_ranges}; +use crate::{ + Error, + coding::Recv, + lite, + track::{self, Order}, +}; + +/// A SUBSCRIBE of its own for the relative ranges a finished copy's readers wait on their +/// publisher to start (see [`track::Producer::asks`]): the subscription that fed the copy is +/// over, so nothing else can carry them, and the copy never counts them over the groups it +/// holds. Its SUBSCRIBE_START answers the readers, and the groups it delivers land in the copy, +/// which its readers wait for while it is out (see [`track::Producer::asking`]). It FINs once +/// every group a range owes below the publisher's end is accounted for and read, then waits for +/// the publisher's FIN in reply. +pub(super) struct Ask { + serve: TrackServe, + serving: track::Producer, + id: u64, + state: AskState, +} + +enum AskState { + Establish(Box), + Active { + sub: Box, + /// Whether our FIN went out. + finished: bool, + }, + Done, +} + +impl Ask { + /// Ask for what `serving`'s readers wait on, `None` when they wait on nothing. + pub fn new(serve: &TrackServe, serving: &track::Producer) -> Option { + let asks = serving.asks(); + if asks.is_empty() { + return None; + } + // Under the readers' own preferences, in key order, as a cold copy's reader asks. + let subscription = serving + .subscription() + .unwrap_or_default() + .with_ranges(asks) + .with_order(Order::Ascending); + let mut ranges = wire_ranges(&subscription)?; + // A relative end goes upstream open, so two asks may carry the same range. + let mut seen = std::collections::HashSet::new(); + ranges.retain(|range| seen.insert(*range)); + // The asks that don't fit wait for those sent to be answered, which changes the asks. + let fields = serve.subscribe_fields(&subscription); + let ranges = super::fit::Budget::new(serve.subscriber.version, fields, 0, ranges.len()).take(ranges); + if ranges.is_empty() { + return None; + } + let copy = Accepted { + producer: serving.clone(), + framing: lite::Framing::new(serving.info(), serve.subscriber.version), + }; + tracing::debug!(track = %serve.name, ?ranges, "asking where relative starts begin"); + let establish = serve.register(subscription, ranges, Some(copy)); + serving.asking(true); + Some(Self { + serve: serve.clone(), + serving: serving.clone(), + id: establish.id, + state: AskState::Establish(Box::new(establish)), + }) + } + + /// Ready once the ask is over, or with why it failed: its readers then have no answer + /// coming. + pub fn poll(&mut self, waiter: &kio::Waiter) -> Poll> { + loop { + match &mut self.state { + AskState::Establish(est) => match ready!(est.poll(waiter)) { + Ok(active) => { + self.state = AskState::Active { + sub: Box::new(active), + finished: false, + } + } + Err(err) => { + self.end(); + return Poll::Ready(Err(err)); + } + }, + AskState::Active { sub, finished } => { + if !*finished + && sub.started.is_some() + && let Some(end) = &sub.end + { + let owed: Vec<_> = sub + .live + .iter() + .filter_map(|(id, range)| Some(owed(range, (*sub.starts.get(&id)?)?, end.group))) + .collect(); + let settled = sub + .tail + .consume() + .poll(waiter, |tail| { + match owed.iter().all(|owed| tail.covers(owed.clone())) && !tail.is_reading() { + true => Poll::Ready(()), + false => Poll::Pending, + } + }) + .is_ready(); + if settled { + let _ = sub.stream.writer.finish(); + *finished = true; + } + } + if let Poll::Ready(Err(err)) = sub.stream.writer.poll_flush(waiter) { + tracing::debug!(%err, "ask subscribe write failed"); + } + let res = ready!(sub.stream.reader.poll_recv::(waiter)); + let now = self.serve.subscriber.runtime.now(); + match res { + Recv::Message(lite::SubscribeResponse::Start(start)) + if sub.started.is_none() && start.starts.len() as u64 == sub.live.initial_count() => + { + let edge = start.largest.map(|largest| largest.position.group); + for (id, start) in (0..).zip(&start.starts) { + sub.starts.insert(id, *start); + sub.edges.insert(id, edge); + } + sub.started = Some(start.starts.iter().flatten().min().copied()); + // Read as they arrive: the readers wait for the rest while the ask is out. + sub.report_starts(&self.serving); + } + Recv::Message(lite::SubscribeResponse::Drop(dropped)) => { + if let Ok(mut tail) = sub.tail.write() { + tail.account(dropped.groups, now); + } + } + // Never produced: the copy records the skip, as the subscription itself does. + Recv::Message(lite::SubscribeResponse::Skip(skipped)) => { + if !skipped.groups.is_empty() { + self.serving.skip_received(skipped.groups.clone()); + if let Ok(mut tail) = sub.tail.write() { + tail.account(skipped.groups, now); + } + } + } + Recv::Message(lite::SubscribeResponse::Finish(finish)) if sub.end.is_none() => { + sub.end = Some(finish); + } + Recv::Message(lite::SubscribeResponse::Ok(_)) => {} + // A second START or FINISH, or a STARTED for a range no update added. + Recv::Message(msg) => { + tracing::warn!(?msg, "unexpected ask subscribe response"); + self.end(); + return Poll::Ready(Err(Error::ProtocolViolation)); + } + // The publisher FINs in reply to ours, or once it finished the track. + Recv::Ended if *finished || sub.end.is_some() => { + self.end(); + return Poll::Ready(Ok(())); + } + Recv::Ended | Recv::Malformed(_) => { + tracing::warn!("ask subscribe ended without SUBSCRIBE_FINISH"); + self.end(); + return Poll::Ready(Err(Error::ProtocolViolation)); + } + // After our FIN everything it owes is in, so a reset, such as the publisher + // cutting a response short, costs nothing. + Recv::Reset(err) => { + tracing::debug!(%err, "ask subscribe ended"); + let finished = *finished; + self.end(); + return Poll::Ready(if finished { Ok(()) } else { Err(err) }); + } + } + } + AskState::Done => return Poll::Ready(Ok(())), + } + } + } + + /// Stop routing the subscription's streams, resetting it unless our FIN already went out. + fn end(&mut self) { + let state = std::mem::replace(&mut self.state, AskState::Done); + // Its groups are in, or never coming: the readers end at the boundary again. + self.serving.asking(false); + self.serve.subscriber.remove_subscribe(self.id); + match state { + AskState::Establish(est) => est.abort(&Error::Cancel), + AskState::Active { sub, finished: false } => sub.stream.writer.abort(&Error::Cancel), + _ => {} + } + } +} + +impl Drop for Ask { + fn drop(&mut self) { + if !matches!(self.state, AskState::Done) { + self.end(); + } + } +} + +/// The groups `range`, delivered from `start`, owes below the track's exclusive `end`. +fn owed(range: &lite::Range, start: u64, end: u64) -> std::ops::Range { + let until = match range.end { + // Stops just before it, taking the group it falls in when partway through one. + Some(lite::Anchor::Absolute(at)) => at.group.saturating_add(u64::from(at.frame > 0)), + // Includes the group containing it. + Some(lite::Anchor::Time(time)) => time.saturating_add(1), + _ => end, + }; + start..until.min(end).max(start) +} diff --git a/rs/moq-net/src/lite/subscriber/fit.rs b/rs/moq-net/src/lite/subscriber/fit.rs index 9561d91302..b04fa0e615 100644 --- a/rs/moq-net/src/lite/subscriber/fit.rs +++ b/rs/moq-net/src/lite/subscriber/fit.rs @@ -49,6 +49,23 @@ impl Budget { } } + /// The longest prefix of `ranges` fitting half of what is left, which it then takes: the + /// other half stays for the demand and fills, which [`fit`] can only widen so far. + pub fn take(&mut self, ranges: Vec) -> Vec { + let room = self.bytes / 2; + let mut used = 0; + let mut taken = Vec::new(); + for range in ranges { + match self.cost(&range).map(|cost| used + cost) { + Some(next) if next <= room => used = next, + _ => break, + } + taken.push(range); + } + self.bytes -= used; + taken + } + /// What `range` costs, or `None` when it can't go out. fn cost(&self, range: &Range) -> Option { Some(lite::range_size(range, self.form)? + self.id) diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index 444aa35415..4ad6d3058d 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -82,11 +82,14 @@ pub(in crate::model) trait Holdings { /// held: `None` once fewer are held than that. fn back(&self, latest: u64, back: u64) -> Option; - /// Where the publisher reported starting a range counting `back` groups before `latest`, - /// which a relative start, but never an end, takes over [`Self::back`]: a delivery start - /// may sit past the group counted, such as above groups too stale for the budget. - fn reported_start(&self, latest: u64, back: u64) -> Option { - let _ = (latest, back); + /// Where a range counting `back` groups before `latest`, up to the exclusive `end`, starts, + /// which a relative start, but never an end, takes over [`Self::back`]: on a received copy, + /// where its publisher reported starting one counted from `latest` or a newer group, since + /// the copy holds only part of the track and a delivery start may sit past the group + /// counted, or [`Containing::Unknown`] until it reports one. `None` counts it with + /// [`Self::back`]. + fn counted_start(&self, latest: u64, back: u64, end: Option) -> Option { + let _ = (latest, back, end); None } @@ -111,14 +114,14 @@ pub(in crate::model) trait Holdings { } } -/// Where a time falls among the groups a hop holds. +/// Where a time, or a count back from the latest group, falls among the groups a hop holds. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(in crate::model) enum Containing { - /// The newest group starting at or before it. + /// The newest group starting at or before it, or the group counted back to. Group(u64), /// Every group held starts after it. Before, - /// No group is held. + /// The groups held can't place it yet. Unknown, } @@ -190,6 +193,10 @@ pub(in crate::model) struct Window { /// The ranges of a live join whose reach back waits on the groups held to measure the /// live edge, which read nothing meanwhile: a group held is no reason to start at it. reaching: BTreeSet, + /// The ranges whose relative start waits on its publisher to say where it counted back to + /// (see [`Holdings::counted_start`]), each with the latest group it counted from then, + /// which read nothing meanwhile: a group held may sit below where the publisher started. + counting: BTreeMap, /// The groups the spans cover, shared with what reads them. groups: Cover, /// The part of `groups` a miss may ask for: see [`Self::missable`]. @@ -261,6 +268,7 @@ impl Window { waiting: BTreeSet::new(), waited: None, reaching: BTreeSet::new(), + counting: BTreeMap::new(), groups: Cover::default(), missable: Cover::default(), covered: None, @@ -305,6 +313,7 @@ impl Window { } self.pending.remove(&key); self.reaching.remove(&key); + self.counting.remove(&key); if let Some(span) = self.spans.remove(&key) { self.uncount(key, &span); } @@ -340,6 +349,17 @@ impl Window { self.waiting.last().is_some_and(|&(time, _)| time > floor) } + /// Whether a relative start still waits on its publisher to say where it starts. + pub fn is_counting(&self) -> bool { + !self.counting.is_empty() + } + + /// The ranges whose relative start waits on its publisher to say where it starts, which a + /// received copy asks its upstream for. + pub fn counting(&self) -> impl Iterator + '_ { + self.counting.keys().map(|key| self.ranges[key]) + } + /// Resolve what the groups now held settle: relative anchors once `latest` exists, and /// time starts once a group arrives. Returns whether the groups in range moved. pub fn settle(&mut self, latest: Option, holdings: &impl Holdings) -> bool { @@ -361,6 +381,15 @@ impl Window { } moved = self.spans.len() > placed; } + // Each from the latest group it first counted from: a report counted from that one or + // a newer one starts it. + if !self.counting.is_empty() { + let placed = self.spans.len(); + for (key, since) in std::mem::take(&mut self.counting) { + self.place_range(key, Some(since), holdings); + } + moved |= self.spans.len() > placed; + } moved |= !self.waiting.is_empty() && self.start_waiting(holdings); if moved { self.covered = None; @@ -431,7 +460,7 @@ impl Window { /// The lowest group the ranges name, `None` while one reaches back to the oldest group /// or an anchor waits. pub fn start(&self) -> Option { - match self.is_pending() || !self.reaching.is_empty() || self.unnamed > 0 { + match self.is_pending() || !self.reaching.is_empty() || !self.counting.is_empty() || self.unnamed > 0 { true => None, false => self.lowest_named(), } @@ -496,10 +525,12 @@ impl Window { } /// The keys of the ranges not yet settled, which keep [`Self::is_settled`] false: waiting - /// on a first group, on the groups held to place a start, or on a lookup. Costs those. + /// on a first group, on the groups held to place a start, on a publisher to say where a + /// relative one starts, or on a lookup. Costs those. pub fn unsettled(&self) -> BTreeSet { let mut keys = self.pending.clone(); keys.extend(self.reaching.iter().copied()); + keys.extend(self.counting.keys().copied()); keys.extend(self.waiting.iter().map(|(_, key)| *key)); keys.extend(self.looking.iter().map(|(_, key)| *key)); keys @@ -637,6 +668,7 @@ impl Window { self.looking.clear(); self.waiting.clear(); self.reaching.clear(); + self.counting.clear(); self.groups = Cover::default(); self.missable = Cover::default(); self.named.clear(); @@ -678,12 +710,16 @@ impl Window { } }, }; - let (start, floored) = match start(anchor, latest, holdings) { + let (start, floored) = match start(anchor, latest, end, holdings) { Start::At(start, floored) => (start, floored), Start::Pending => { self.pending.insert(key); return; } + Start::Counting(since) => { + self.counting.insert(key, since); + return; + } // Reads only the groups from its time until the groups held place it; see // [`Self::start_waiting`]. Start::Waiting => { @@ -703,9 +739,10 @@ impl Window { self.count(key, Span::new(anchor, start, end, floored, looking), holdings); } - /// Whether a time start or a join's reach back waits on the groups held to place it. + /// Whether a time start or a join's reach back waits on the groups held to place it, or a + /// relative start on its publisher. fn unplaced(&self) -> bool { - !self.waiting.is_empty() || !self.reaching.is_empty() + !self.waiting.is_empty() || !self.reaching.is_empty() || !self.counting.is_empty() } /// Hold `span` under `key`, counting the groups it reads and where it starts. @@ -864,21 +901,27 @@ enum Start { Waiting, /// Waiting on a latest group, reading everything meanwhile. Pending, + /// A relative start counted from this latest group that its publisher has yet to say + /// where it starts, reading nothing meanwhile. + Counting(u64), } -fn start(anchor: Anchor, latest: Option, holdings: &impl Holdings) -> Start { +/// Where `anchor` starts a range ending at the exclusive `end`. +fn start(anchor: Anchor, latest: Option, end: Option, holdings: &impl Holdings) -> Start { match anchor { // Nothing sits below group 0, so the oldest start needs no latest group. Anchor::Oldest => Start::At(0, false), Anchor::Relative(back) => match latest { None => Start::Pending, - Some(latest) => match holdings - .reported_start(latest, back) - .or_else(|| holdings.back(latest, back)) - { - Some(group) => Start::At(group, true), + Some(latest) => match holdings.counted_start(latest, back, end).unwrap_or_else(|| { + holdings + .back(latest, back) + .map_or(Containing::Before, Containing::Group) + }) { + Containing::Group(group) => Start::At(group, true), // Fewer groups are held than it counts back, so reach back to the oldest. - None => Start::At(0, false), + Containing::Before => Start::At(0, false), + Containing::Unknown => Start::Counting(latest), }, }, // No group starts before 0, so time 0 names group 0 whatever is held. @@ -952,6 +995,10 @@ mod tests { /// Every key below this was dropped, so a time below it is placed before every /// group even while none is held. dropped: u64, + /// What a relative start counts back to, when not over the groups held. + counted: Option, + /// The latest group the last relative start was counted from. + counted_from: std::cell::Cell>, } impl Keys { @@ -998,6 +1045,11 @@ mod tests { self.held.range(..latest).rev().nth(back as usize).copied() } + fn counted_start(&self, latest: u64, _: u64, _: Option) -> Option { + self.counted_from.set(Some(latest)); + self.counted + } + fn complete(&self) -> bool { self.complete } @@ -1239,6 +1291,52 @@ mod tests { } } + /// A relative start its publisher has yet to say where it starts reads nothing, rather than + /// every group as one waiting on a latest group does, and starts once it says, counted + /// from the latest group it first counted from, however far the live edge moved since. + /// An end still counts over the groups held. + #[test] + fn a_relative_start_reads_nothing_until_counted() { + let mut keys = Keys::new([1, 2, 9]); + keys.counted = Some(Containing::Unknown); + let ranges = [range(Anchor::Relative(2), time(8)), range(time(1), time(1))]; + let mut window = Window::new(&ranges, None, Some(9), &keys).unwrap(); + assert!(window.is_counting() && !window.is_pending() && !window.is_settled()); + assert_eq!(covered(&window, 0..10), vec![1]); + assert_eq!( + (window.floor(), window.start(), window.named(), window.end()), + (None, None, None, None) + ); + assert_eq!(window.resolved_key(0), None); + + keys.counted = Some(Containing::Group(7)); + assert!(window.settle(Some(12), &keys)); + assert_eq!(keys.counted_from.get(), Some(9), "from where it first counted"); + assert!(window.is_settled() && !window.is_counting()); + assert_eq!(covered(&window, 0..10), vec![1, 7, 8]); + assert_eq!(window.start(), Some(1)); + assert_eq!(window.end(), Some(9)); + + // Retiring a range still counting takes it out of the count. + keys.counted = Some(Containing::Unknown); + window + .edit([], &[(2, range(Anchor::Relative(3), None))], Some(9), &keys) + .unwrap(); + assert!(window.is_counting()); + assert_eq!( + window.counting().collect::>(), + [range(Anchor::Relative(3), None)] + ); + window.edit([2], &[], Some(9), &keys).unwrap(); + assert_eq!(window.counting().count(), 0); + assert!(window.is_settled()); + + // Counting back past every group the track holds reaches back to the oldest. + keys.counted = Some(Containing::Before); + let window = Window::new(&[range(Anchor::Relative(5), None)], None, Some(9), &keys).unwrap(); + assert_eq!((window.floor(), window.start()), (Some(0), None)); + } + /// No hop can place the oldest group as an end, so it is refused. #[test] fn an_oldest_end_is_refused() { diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index bb544a5229..3b484b5108 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -30,7 +30,7 @@ use rangemap::{RangeInclusiveSet, RangeMap, RangeSet}; mod demand; pub(crate) use demand::Edit; pub(super) use demand::Preferences; -use demand::Subscriptions; +use demand::{Asked, Subscriptions}; mod fill; mod hole; @@ -599,22 +599,59 @@ enum Anchors { /// A received copy whose wire tells it what relative anchors count back from, and carries /// time anchors as group starts, so they resolve here as at the publisher. Wire, + /// A [`Self::Wire`] copy whose publisher also reports where each range starts (lite-07), + /// so a relative start waits for that report rather than count over the groups held. + Reported, } impl Anchors { fn received(self) -> bool { self != Self::Local } + + /// Whether the wire carries relative and time anchors, resolving them as the publisher. + fn on_wire(self) -> bool { + matches!(self, Self::Wire | Self::Reported) + } } /// Where a received copy's publisher started a range counting back, and the latest group it /// counted from. #[derive(Clone, Copy, Debug, PartialEq, Eq)] struct RelativeStart { - start: u64, + start: CountedStart, latest: u64, } +impl RelativeStart { + /// What two ranges counting back the same number of groups report together: the one from + /// the newer latest group, and from the same one, the lower start, nearer the group both + /// counted to, over any range holding none, since a start holds for every range ending past + /// it, or else the furthest of those. + fn merge(self, other: Self) -> Self { + if self.latest != other.latest { + return if self.latest > other.latest { self } else { other }; + } + let start = match (self.start, other.start) { + (CountedStart::At(a), CountedStart::At(b)) => CountedStart::At(a.min(b)), + (CountedStart::At(start), _) | (_, CountedStart::At(start)) => CountedStart::At(start), + (CountedStart::Past(a), CountedStart::Past(b)) => CountedStart::Past(a.max(b)), + }; + Self { start, ..self } + } +} + +/// Where a publisher reported starting a range counting back. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum CountedStart { + /// At this group. + At(u64), + /// At or past this exclusive end, holding no group of the range there: it says nothing of + /// a range ending later. + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + Past(u64), +} + #[derive(Default)] pub(crate) struct TrackState { // Ingress stats scope, inherited from a tagged [`broadcast::Producer`] or [`Request`]. @@ -770,6 +807,9 @@ pub(crate) struct TrackState { // holds only part of the track, so it can't count back over its own groups. Replaced whole // as the ranges change, like `time_starts`. relative_starts: BTreeMap, + // The asks a finished copy's session has out (see [`Producer::asking`]): their groups may + // still land below the end, so no reader ends at it meanwhile. + asking: usize, // Counts changes to how a time start is placed other than by groups arriving or drops: // `time_starts`, `anchors`, and `starts_at_keys`. See [`Unplaced::mark`]. placing: u64, @@ -1129,7 +1169,7 @@ impl Holdings for TrackState { if self.starts_at_keys { return Containing::Group(time); } - if self.anchors == Anchors::Wire { + if self.anchors.on_wire() { return self.containing_received(time); } match self.lookup.range(..=time).next_back() { @@ -1149,7 +1189,7 @@ impl Holdings for TrackState { /// unknown times do. fn unplaced(&self) -> Option { let none = Unplaced::NONE; - let past = self.starts_past.filter(|_| self.anchors == Anchors::Wire); + let past = self.starts_past.filter(|_| self.anchors.on_wire()); // The times below every held group that no inference from an earlier range places. let unheld = |run: ops::RangeInclusive| match past { Some(past) => (*run.start()).max(past)..=*run.end(), @@ -1170,7 +1210,7 @@ impl Holdings for TrackState { match (self.lookup.first_key_value(), self.lookup.last_key_value()) { // Before any group, only a time whose keys down to 0 were all dropped is placed. (None, _) | (_, None) => [unheld(uncovered(0)), none], - (Some(_), Some(_)) if self.anchors != Anchors::Wire => [none.clone(), none], + (Some(_), Some(_)) if !self.anchors.on_wire() => [none.clone(), none], // A copy places a time below its groups only once the keys down to 0 were // dropped, and one above them only once the keys down to the newest were. (Some((&first, _)), Some((&last, _))) => { @@ -1199,6 +1239,11 @@ impl Holdings for TrackState { } fn back(&self, latest: u64, back: u64) -> Option { + // Keys that are group IDs count back by ID, as a moq-transport joining FETCH does, so + // the count never depends on what this copy holds. + if self.starts_at_keys { + return Some(latest.saturating_sub(back)); + } let Some(back) = back.checked_sub(1) else { return Some(latest); }; @@ -1207,13 +1252,27 @@ impl Holdings for TrackState { self.lookup.range(..latest).rev().nth(back).map(|(&key, _)| key) } - /// A received copy starts where its publisher did, counting from the same latest group: a - /// newer one moved the live edge, which the copy then counts from itself. - fn reported_start(&self, latest: u64, back: u64) -> Option { - self.relative_starts + /// A lite-07 copy holds only part of the track, so it starts only where its publisher + /// reported starting, counting from the same latest group or a newer one, and waits for + /// that report otherwise, whatever it holds: a reader asks its upstream for one (see + /// [`Producer::poll_asks_changed`]). The latest group itself needs no count. + fn counted_start(&self, latest: u64, back: u64, end: Option) -> Option { + if self.anchors != Anchors::Reported { + return None; + } + if back == 0 { + return Some(Containing::Group(latest)); + } + let reported = self + .relative_starts .get(&back) - .filter(|reported| reported.latest == latest) - .map(|reported| reported.start) + .and_then(|reported| match reported.start { + CountedStart::At(start) => (reported.latest >= latest).then_some(start), + // Past a range ending no earlier than this one, so this one holds no group either. + // Counting from another latest group starts no earlier, so it holds there too. + CountedStart::Past(past) => end.filter(|end| *end <= past), + }); + Some(reported.map_or(Containing::Unknown, Containing::Group)) } fn complete(&self) -> bool { @@ -3441,6 +3500,23 @@ impl TrackState { if !self.anchors.received() || self.outcome.is_released() || !self.readable() { return None; } + // An ask out may still deliver any group the copy doesn't hold, whatever the feed's floor + // or grace, including one it held and evicted, which the ask delivers again (see + // [`Producer::asking`]). + if self.asking > 0 { + let end = groups.end.min(self.final_key().unwrap_or(u64::MAX)); + let mut next = groups.start; + let held = self.visible.range(groups.start..end).copied().chain([end]); + for key in held { + if key > next + && let Some(owed) = self.drops.gaps(&(next..key)).find_map(|open| window.clip(open)) + { + return Some(owed.start); + } + next = key.saturating_add(1); + } + return None; + } let asked = self.start.key().is_some(); self.find_landable(groups, asked, self.arrival_grace(), |open, _| { window.clip(open).map(|run| run.start) @@ -4501,6 +4577,8 @@ pub struct Producer { broadcast: Arc, state: kio::Producer, prev_subscription: Option, + // The revision of the asks [`Self::poll_asks_changed`] last reported. + asks_seen: u64, // Shared with every clone and every `Requests`: its `Drop` is the teardown. alive: Arc, } @@ -4537,6 +4615,7 @@ impl Producer { state, broadcast, prev_subscription: None, + asks_seen: 0, alive, } } @@ -4910,8 +4989,19 @@ impl Producer { /// reports the live edge and media time and asks for every group the budget allows. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] pub(crate) fn anchored(&self) { + self.set_anchors(Anchors::Wire); + } + + /// [`Self::anchored`], for a wire whose publisher also reports where each range starts: + /// a relative start waits for that report, which its reader asks for. + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + pub(crate) fn reported(&self) { + self.set_anchors(Anchors::Reported); + } + + fn set_anchors(&self, anchors: Anchors) { if let Ok(mut state) = self.modify() { - state.anchors = Anchors::Wire; + state.anchors = anchors; state.placing += 1; } } @@ -4946,13 +5036,20 @@ impl Producer { /// Record where the upstream publisher started each of this lite-07 copy's live wire /// ranges counting back, as `(back, start, latest)` triples, replacing the last set. A - /// reader's relative anchor counting back `back` groups from `latest` resolves to `start`. + /// reader's relative start counting back `back` groups from `latest`, or from an older + /// group, resolves to `start`. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] - pub(crate) fn relative_starts(&self, starts: impl IntoIterator) { - let starts: BTreeMap = starts - .into_iter() - .map(|(back, start, latest)| (back, RelativeStart { start, latest })) - .collect(); + pub(crate) fn relative_starts(&self, starts: impl IntoIterator) { + let mut merged = BTreeMap::::new(); + for (back, start, latest) in starts { + let next = RelativeStart { start, latest }; + let start = match merged.get(&back) { + Some(last) => last.merge(next), + None => next, + }; + merged.insert(back, start); + } + let starts = merged; if let Ok(mut state) = self.modify() && state.relative_starts != starts { @@ -4960,6 +5057,19 @@ impl Producer { } } + /// Count an ask this finished copy's session sends upstream as out while `asking`, and done + /// once not: the groups it delivers land below the end, so a reader waits for them rather + /// than end at the boundary, while still reading those already in. + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + pub(crate) fn asking(&self, asking: bool) { + if let Ok(mut state) = self.modify() { + state.asking = match asking { + true => state.asking + 1, + false => state.asking.saturating_sub(1), + }; + } + } + /// Where this copy's readers count a relative start back from now (see /// [`TrackState::latest_live`]). #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] @@ -5588,6 +5698,23 @@ impl Producer { poll_combined_changed(&subs, bound, &mut self.prev_subscription, waiter).map(Ok) } + /// Ready once a reader of this lite-07 copy starts or stops waiting on its publisher to + /// start a relative range (see [`Self::asks`]): its session asks upstream for each as a + /// range of its own, so the publisher reports where it starts. + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + pub(crate) fn poll_asks_changed(&mut self, waiter: &kio::Waiter) -> Poll<()> { + let subs = self.state.read().subscriptions.clone(); + subs.lock().poll_asks(&mut self.asks_seen, waiter) + } + + /// The relative ranges this copy's readers wait on its publisher to start; see + /// [`Self::poll_asks_changed`]. + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + pub(crate) fn asks(&self) -> Vec { + let subs = self.state.read().subscriptions.clone(); + subs.lock().asks() + } + /// Count `seen` as the aggregate already acted on, so /// [`Self::poll_subscription_changed`] reports only a change from it: the demand a /// subscriber session asked upstream for before accepting the track, or `None` @@ -8654,6 +8781,9 @@ struct Cursor { cache: Arc, subscription: Preferences, + /// The relative ranges its window waits on the publisher to start, which a received + /// copy's session asks upstream for. + asked: Asked, /// Arrival-order cursor used by `recv_group`. index: usize, /// Minimum key to return from any `recv` method. Set by `set_range`. @@ -9064,10 +9194,13 @@ impl Cursor { }; let live = Arc::::default(); let published = Published::new(Servable::of(&filter.window, &live), filter.resolved, None); + let mut asked = subscription.asked(); + asked.set(filter.window.counting().collect()); Self { state, cache, subscription, + asked, min_key: 0, filter: Box::new(filter), index: 0, @@ -9268,6 +9401,7 @@ impl Cursor { self.filter.revision += 1; self.update_servable(); } + self.asked.set(self.filter.window.counting().collect()); moved } @@ -9926,13 +10060,30 @@ impl Cursor { (misses.lowest_wanted(), misses.has_filled(), misses.is_waiting()) } - /// Whether a fill still owes this cursor a group, so the end of the feed is not its end. + /// Whether a fill still owes this cursor a group, or a relative start waits on its + /// publisher's report while a producer remains to record it, as a finished copy asks for + /// one: either way the end of the feed is not its end. fn awaits_fill(&self) -> bool { - self.misses.as_ref().is_some_and(|misses| misses.is_waiting()) + self.awaits_report() + || self.misses.as_ref().is_some_and(|misses| misses.is_waiting()) || self.filter.window.lookups().next().is_some() || self.heads.as_ref().is_some_and(|heads| !heads.fetching.is_empty()) } + /// Whether a relative start waits on its publisher's report, or an ask's groups may still + /// arrive (see [`Producer::asking`]), while a producer remains to record them. + fn awaits_report(&self) -> bool { + let state = self.state.read(); + (self.filter.window.is_counting() || state.asking > 0) && !state.outcome.is_released() + } + + /// Wake on the track's next change while [`Self::awaits_report`], which a report is. + fn park_on_report(&self, waiter: &kio::Waiter) { + if self.awaits_report() { + let _ = self.state.poll(waiter, |_| Poll::<()>::Pending); + } + } + /// Move past the group `peeked` found and hand it out: the exact group, even if its /// producer evicted it since, so eviction cannot advance the reader past a gap. fn take(&mut self, peeked: Peeked) -> group::Consumer { @@ -9998,7 +10149,9 @@ impl Cursor { continue; } let Some((producer, found_index)) = ready!(polled)? else { - // The feed ended, but a fill still owes a group. Its answer wakes us. + // The feed ended, but a fill still owes a group. Its answer wakes us, as the + // report a relative start waits on does. + self.park_on_report(waiter); if self.awaits_fill() { return Poll::Pending; } @@ -10300,7 +10453,9 @@ impl Cursor { continue; } let Some(producer) = ready!(polled)? else { - // The feed ended, but a fill still owes a group. Its answer wakes us. + // The feed ended, but a fill still owes a group. Its answer wakes us, as the report + // a relative start waits on does. + self.park_on_report(waiter); if self.awaits_fill() { return Poll::Pending; } @@ -11101,12 +11256,14 @@ impl Subscriber { }; let start = ready!(Consumer::poll_state_start(&cursor.state, waiter)); cursor.sync(); - if cursor.filter.window.is_waiting_above(cursor.min_key) { + let unplaced = |window: &Window, floor| window.is_waiting_above(floor) || window.is_counting(); + if unplaced(&cursor.filter.window, cursor.min_key) { // Registered before syncing again, so a change placing it after this wakes it. A - // closed track places it at once, since nothing more can arrive. + // closed track places it at once, since nothing more can arrive, except a relative + // start a finished copy still asks its publisher for. let closed = cursor.state.poll(waiter, |_| Poll::<()>::Pending).is_ready(); cursor.sync(); - if !closed && cursor.filter.window.is_waiting_above(cursor.min_key) { + if (!closed || cursor.awaits_report()) && unplaced(&cursor.filter.window, cursor.min_key) { return Poll::Pending; } } @@ -12535,6 +12692,7 @@ impl Request { broadcast: self.broadcast, state: self.state, prev_subscription: None, + asks_seen: 0, alive: self.alive, }), _dynamic: self._dynamic, @@ -16280,9 +16438,9 @@ mod test { assert_eq!(drain(&mut floored), vec![1000]); } - /// A lite-07 copy holds only part of the track, so a relative start counts back as its - /// publisher reported, not over the groups the copy holds, until a group past the latest - /// the publisher counted from moves the live edge. + /// A lite-07 copy holds only part of the track, so a relative start counts back only as its + /// publisher reported, from the same latest group or a newer one, and reads nothing until + /// it does, whatever groups the copy holds or receives: its reader asks for that report. #[test] #[cfg(feature = "async")] fn a_received_copy_counts_a_relative_start_back_as_its_publisher_did() { @@ -16295,21 +16453,30 @@ mod test { .with_delay(Delay::default().with_max(None)) }; let placed = |subscriber: &mut Subscriber| subscriber.resolved_range(0).map(|resolved| resolved.start); + let read = |subscriber: &mut Subscriber| { + let mut read = drain(subscriber); + read.sort_unstable(); + read + }; let producer = track_producer("test", Info::timed()); producer.received(); - producer.anchored(); + producer.reported(); for key in [1, 2, 9] { write_group(&producer, key); } producer.report_edge(9); - producer.relative_starts([(2, 7, 9)]); + producer.relative_starts([(2, CountedStart::At(7), 9)]); let mut reported = producer.subscribe(back(2)); assert_eq!(placed(&mut reported), Some(7), "the publisher's start, not group 1"); + let mut latest = producer.subscribe(back(0)); + assert_eq!(placed(&mut latest), Some(9), "the latest group needs no count"); let mut unreported = producer.subscribe(back(1)); - assert_eq!(placed(&mut unreported), Some(2), "counted over the groups held"); - // A reported start is where delivery began, which may sit past the group counted, so a - // relative end counts over the groups held. + assert_eq!(placed(&mut unreported), None, "group 8 may sit below 9"); + assert_eq!(read(&mut unreported), Vec::::new(), "reads nothing meanwhile"); + assert_eq!(producer.asks(), back(1).ranges, "and asks for a report"); + // A reported start is where delivery began, which may sit past the group counted, and + // nothing reports an end, so a relative end counts over the groups held. let mut ended = producer.subscribe( Subscription::default() .with_ranges([Range { @@ -16320,15 +16487,231 @@ mod test { ); assert_eq!(ended.resolved_range(0).and_then(|resolved| resolved.end), Some(2)); - // A newer group moved the live edge, so a later reader counts back from it. + // Holding every group in between counts nothing either. + write_group(&producer, 8); + assert_eq!(placed(&mut unreported), None, "holding group 8 says nothing"); + + // A report counted from a newer latest group starts it, as the publisher placed it then. write_group(&producer, 10); + producer.relative_starts([(2, CountedStart::At(7), 9), (1, CountedStart::At(9), 10)]); + assert_eq!(placed(&mut unreported), Some(9)); + assert_eq!(read(&mut unreported), vec![9, 10]); + assert!(producer.asks().is_empty(), "answered, so asked no more"); + // But one from an older latest group says nothing of a reader counting from a newer one. let mut later = producer.subscribe(back(2)); - assert_eq!(placed(&mut later), Some(2)); + assert_eq!(placed(&mut later), None); assert_eq!( placed(&mut reported), Some(7), "an earlier reader stays where it started" ); + + // A publisher with no group to deliver in a range ending at 10 has none in one ending + // sooner, but says nothing of one ending later. + producer.relative_starts([(5, CountedStart::Past(10), 10)]); + let until = |end| { + Subscription::default() + .with_ranges([Range { + start: Anchor::Relative(5), + end: Some(Anchor::Time(end)), + }]) + .with_delay(Delay::default().with_max(None)) + }; + let mut empty = producer.subscribe(until(8)); + assert_eq!( + empty.resolved_range(0).map(|resolved| (resolved.start, resolved.end)), + Some((9, Some(9))) + ); + let mut longer = producer.subscribe(until(10)); + assert_eq!(placed(&mut longer), None); + + // Counting from a newer latest group only starts later, so the empty report still holds. + write_group(&producer, 11); + let mut newer = producer.subscribe(until(8)); + assert_eq!( + newer.resolved_range(0).map(|resolved| (resolved.start, resolved.end)), + Some((9, Some(9))) + ); + + // Nothing counts over the groups held once the copy ends either, and its reader waits for + // the report rather than end with nothing: a finished copy asks on a subscription of its + // own. + producer.finish_shared().unwrap(); + assert_eq!(placed(&mut later), None); + assert!(later.recv_group().now_or_never().is_none(), "waits for the report"); + assert!(later.poll_start(&kio::Waiter::noop()).is_pending(), "so does its start"); + assert!(producer.asks().contains(&back(2).ranges[0]), "and asks for it"); + producer.relative_starts([(2, CountedStart::At(9), 11)]); + assert_eq!(read(&mut later), vec![9, 10, 11]); + assert!(matches!(later.recv_group().now_or_never(), Some(Ok(None))), "then ends"); + + // While an ask is out, a reader waits for the groups it delivers rather than end at the + // boundary, reading each as it lands. + producer.asking(true); + let mut joined = producer.subscribe( + Subscription::default() + .with_ranges([Range { + start: Anchor::Time(3), + end: None, + }]) + .with_delay(Delay::default().with_max(None)), + ); + assert_eq!(read(&mut joined), vec![2, 8, 9, 10, 11]); + assert!(joined.recv_group().now_or_never().is_none(), "waits on the ask"); + write_group(&producer, 5); + assert_eq!(read(&mut joined), vec![5]); + producer.asking(false); + assert!( + matches!(joined.recv_group().now_or_never(), Some(Ok(None))), + "then ends" + ); + } + + /// While a finished copy's ask is out, a reader in key order waits at a gap below where the + /// copy's feed started, which the ask may still fill, rather than pass it. + #[test] + #[cfg(feature = "async")] + fn an_ordered_read_waits_on_the_gaps_an_ask_may_fill() { + let mut producer = track_producer("test", Info::timed()); + producer.received(); + producer.reported(); + producer.start_resolved(9).unwrap(); + for key in [9, 10] { + write_group(&producer, key); + } + producer.report_edge(10); + producer.finish_shared().unwrap(); + producer.asking(true); + producer.relative_starts([(5, CountedStart::At(5), 10)]); + let mut ordered = producer + .subscribe( + Subscription::default() + .with_ranges([Range { + start: Anchor::Relative(5), + end: None, + }]) + .with_delay(Delay::default().with_max(None)), + ) + .ordered(); + let mut next = || match ordered.poll_next_group(&kio::Waiter::noop()) { + Poll::Ready(Ok(Some(group))) => Some(group.start), + Poll::Ready(Ok(None)) => Some(u64::MAX), + _ => None, + }; + assert_eq!(next(), None, "group 5 may still land"); + write_group(&producer, 5); + assert_eq!(next(), Some(5)); + // The rest were never produced, as the ask's next GROUP says. + producer.skip_received(6..9); + assert_eq!(next(), Some(9)); + assert_eq!(next(), Some(10)); + producer.asking(false); + assert_eq!(next(), Some(u64::MAX), "then ends"); + } + + /// While a finished copy's ask is out, a reader in key order waits for a group the copy held + /// and evicted, which the ask redelivers, rather than pass it. + #[test] + #[cfg(feature = "async")] + fn an_ordered_read_waits_on_an_evicted_group_an_ask_redelivers() { + let mut producer = track_producer("test", Info::timed()); + producer.received(); + producer.reported(); + producer.start_resolved(0).unwrap(); + for key in 0..=10 { + write_group(&producer, key); + } + producer.report_edge(10); + producer.finish_shared().unwrap(); + for key in 0..9 { + producer.evict(key); + } + producer.asking(true); + producer.relative_starts([(5, CountedStart::At(5), 10)]); + let mut ordered = producer + .subscribe( + Subscription::default() + .with_ranges([Range { + start: Anchor::Relative(5), + end: None, + }]) + .with_delay(Delay::default().with_max(None)), + ) + .ordered(); + let mut next = || match ordered.poll_next_group(&kio::Waiter::noop()) { + Poll::Ready(Ok(Some(group))) => Some(group.start), + Poll::Ready(Ok(None)) => Some(u64::MAX), + _ => None, + }; + assert_eq!(next(), None, "group 5 is coming again"); + for key in 5..9 { + write_group(&producer, key); + assert_eq!(next(), Some(key)); + } + assert_eq!(next(), Some(9)); + assert_eq!(next(), Some(10)); + producer.asking(false); + assert_eq!(next(), Some(u64::MAX), "then ends"); + } + + proptest::proptest! { + /// Whatever groups a lite-07 copy holds, a reader's relative start lands only where its + /// publisher reported it, or waits: the cache never moves it. + #[test] + fn a_copys_cache_never_moves_a_relative_start( + held in proptest::collection::btree_set(0u64..16, 0..12), + back in 0u64..6, + reported in proptest::option::of(0u64..17), + ) { + let producer = track_producer("test", Info::timed()); + producer.received(); + producer.reported(); + for key in held.iter().copied().chain([16]) { + write_group(&producer, key); + } + producer.report_edge(16); + if let Some(start) = reported { + producer.relative_starts([(back, CountedStart::At(start), 16)]); + } + let mut reader = producer.subscribe( + Subscription::default() + .with_ranges([Range { start: Anchor::Relative(back), end: None }]) + .with_delay(Delay::default().with_max(None)), + ); + let expected = match back { + 0 => Some(16), + _ => reported, + }; + proptest::prop_assert_eq!(reader.resolved_range(0).map(|resolved| resolved.start), expected); + proptest::prop_assert_eq!(producer.asks().is_empty(), expected.is_some()); + } + } + + /// Ranges counting back the same number of groups report together: a start holds for every + /// range ending past it, so it outlasts a range reporting none, whatever order they come in. + #[test] + fn relative_starts_merge_by_count() { + let at = |start, latest| RelativeStart { + start: CountedStart::At(start), + latest, + }; + let past = |past, latest| RelativeStart { + start: CountedStart::Past(past), + latest, + }; + assert_eq!(at(9, 9).merge(past(7, 9)), at(9, 9)); + assert_eq!(past(7, 9).merge(at(9, 9)), at(9, 9)); + assert_eq!(past(7, 9).merge(past(8, 9)), past(8, 9)); + assert_eq!( + at(9, 9).merge(at(7, 9)), + at(7, 9), + "a wider budget delivered from lower" + ); + assert_eq!( + at(9, 9).merge(past(7, 10)), + past(7, 10), + "a newer latest moved the count" + ); } #[test] diff --git a/rs/moq-net/src/model/track/demand.rs b/rs/moq-net/src/model/track/demand.rs index 4bc3690818..950f50fe91 100644 --- a/rs/moq-net/src/model/track/demand.rs +++ b/rs/moq-net/src/model/track/demand.rs @@ -20,6 +20,12 @@ pub(super) struct Subscriptions { // Only stated minimums count, so an unstated one never reads as zero. min_delay: BTreeMap, ranges: Ranges, + /// The relative ranges readers of a received copy wait on its publisher to say where they + /// start, counted: its session asks each upstream as a range of its own. + asks: BTreeMap, + /// Bumped whenever a reader starts or stops waiting on one of `asks`, even one another + /// reader already waits on: that reader may have joined after the last answer. + asks_revision: u64, descending: usize, /// Subscribers that need the live feed; see [`Subscription::feed`]. feed: usize, @@ -320,6 +326,33 @@ impl Subscriptions { pub(super) fn len(&self) -> usize { self.count } + + /// Count `came` and uncount `gone` among [`Self::asks`]. + fn ask(&mut self, gone: &[Range], came: &[Range]) -> kio::WaiterList { + for range in came { + add(&mut self.asks, *range); + } + for range in gone { + remove(&mut self.asks, *range); + } + self.asks_revision += 1; + self.waiters.take() + } + + /// Ready once a reader started or stopped waiting on one of [`Self::asks`] since `seen`. + pub(super) fn poll_asks(&mut self, seen: &mut u64, waiter: &kio::Waiter) -> Poll<()> { + if *seen == self.asks_revision { + waiter.register(&mut self.waiters); + return Poll::Pending; + } + *seen = self.asks_revision; + Poll::Ready(()) + } + + /// The ranges readers wait on their publisher to start. + pub(super) fn asks(&self) -> Vec { + self.asks.keys().copied().collect() + } } /// The ranges `old` holds more of than `new`, then those `new` holds more of than `old`. @@ -657,6 +690,14 @@ impl Preferences { Ok(()) } + /// What this subscriber's reader counts among the track's asks, none yet. + pub(in crate::model) fn asked(&self) -> Asked { + Asked { + subscriptions: self.0.subscriptions.clone(), + ranges: Vec::new(), + } + } + pub(in crate::model) fn same_channel(&self, other: &Self) -> bool { self.0.value.same_channel(&other.0.value) } @@ -674,6 +715,35 @@ impl Preferences { } } +/// The relative ranges one reader waits on its publisher to start, counted among the track's +/// asks while held. +pub(in crate::model) struct Asked { + subscriptions: Option>, + /// Sorted, so the same ranges compare equal however the reader keys them. + ranges: Vec, +} + +impl Asked { + /// Count `ranges` in place of those counted before. + pub(in crate::model) fn set(&mut self, mut ranges: Vec) { + ranges.sort_unstable(); + if ranges == self.ranges { + return; + } + let ranges = std::mem::replace(&mut self.ranges, ranges); + if let Some(subs) = &self.subscriptions { + let mut wakes = subs.lock().ask(&ranges, &self.ranges); + wakes.wake(); + } + } +} + +impl Drop for Asked { + fn drop(&mut self) { + self.set(Vec::new()); + } +} + impl Drop for Preference { fn drop(&mut self) { if let Some(subs) = &self.subscriptions { diff --git a/rs/moq-net/tests/integration/range_anchors.rs b/rs/moq-net/tests/integration/range_anchors.rs index 43d8eded0b..4740a6c1cb 100644 --- a/rs/moq-net/tests/integration/range_anchors.rs +++ b/rs/moq-net/tests/integration/range_anchors.rs @@ -4,8 +4,9 @@ //! the track is held, and on a copy received over a wire that tells it enough to resolve //! the anchor the same way. Every wire carries a time anchor, as the group ID with that key, //! but only lite-07 resolves a time start to the group containing it: an older wire's -//! publisher starts at the first group at or after it. A moq-transport copy resolves a -//! relative anchor from the Largest Object its live join reports. A copy received over any +//! publisher starts at the first group at or after it. A moq-transport copy counts a +//! relative anchor back by group ID from the Largest Object its live join reports. A copy +//! received over any //! other wire refuses a relative anchor other than the live start instead of resolving it //! against the part of the track it happens to hold. @@ -201,6 +202,17 @@ async fn each_wire_refuses_only_the_anchors_it_drops() { if let (false, Anchor::Time(start)) = (resolves, range.start) { held.retain(|&key| key >= start); } + // A moq-transport copy counts a relative anchor back by group ID, as a joining FETCH + // does, never over the groups it holds: these groups' IDs are their starts, a second + // apart, so two back from 4000 is 3998. + if transport { + if let Anchor::Relative(back) = range.start { + held.retain(|&key| key >= 4000 - back); + } + if let Some(Anchor::Relative(back)) = range.end { + held.retain(|&key| key <= 4000 - back); + } + } let mut result = read(version, range).await; // Any other wire carries time anchors and the live start, and drops the rest. let wire = matches!(range.start, Anchor::Time(_) | Anchor::Oldest | Anchor::Relative(0)) @@ -357,6 +369,333 @@ async fn a_sparse_request_counts_its_relative_start_back_as_the_publisher_did() let _track = track.finish().unwrap(); } +/// A later reader's relative start on a lite-07 copy already holding part of the track counts +/// back as the publisher did, not over the groups the copy happens to hold: over a copy +/// holding groups 1 and 2 of 0 to 9, it reads only the groups its range names. Mirrors the +/// `@moq/net` `integration: lite-07 starts a later reader's relative range where the +/// publisher did`. +#[kio::test] +async fn a_later_relative_start_on_a_partial_copy_reads_only_its_groups() { + let publisher = produce_origin(1); + let broadcast = publisher + .route("room", moq_net::origin::RouteOptions::hidden()) + .unwrap(); + let track = broadcast + .broadcast() + .create_track( + "data", + Info::default().with_timescale(Timescale::MILLI).with_max_age(FOREVER), + ) + .unwrap(); + broadcast.announce().unwrap(); + for key in 0..10 { + let mut group = track.create_group(moq_net::group::Info { start: key }).unwrap(); + group.write_frame(millis(key), &b"x"[..]).unwrap(); + group.finish().unwrap(); + } + + let subscriber = produce_origin(2); + let _pair = peer("moq-lite-07-wip".parse::().unwrap(), &publisher, &subscriber).await; + let remote = kio::clock::timeout(TIMEOUT, subscriber.consume().follow("room", None).unwrap().available()) + .await + .expect("resolve timeout") + .expect("broadcast resolves") + .unwrap(); + let subscribe = |ranges: Vec| { + let subscription = Subscription::default() + .with_delay(Delay::default().with_max(FOREVER)) + .with_order(Order::Ascending) + .with_ranges(ranges); + remote.track("data").unwrap().subscribe(subscription) + }; + let read = async |reader: &mut moq_net::track::Subscriber, count: usize| { + let mut read = Vec::new(); + for _ in 0..count { + let group = kio::clock::timeout(TIMEOUT, reader.recv_group()) + .await + .expect("group timeout") + .unwrap(); + read.push(group.map(|group| group.start)); + } + read + }; + + let mut first = kio::clock::timeout(TIMEOUT, subscribe(vec![range(Anchor::Time(1), Some(Anchor::Time(2)))])) + .await + .expect("subscribe timeout") + .unwrap(); + assert_eq!(read(&mut first, 3).await, [Some(1), Some(2), None]); + + // The publisher counts back past the end, so it has no group there to deliver. + let mut empty = kio::clock::timeout( + TIMEOUT, + subscribe(vec![range(Anchor::Relative(2), Some(Anchor::Time(6)))]), + ) + .await + .expect("subscribe timeout") + .unwrap(); + assert_eq!(read(&mut empty, 1).await, [None], "nothing to deliver"); + drop(empty); + + let mut second = kio::clock::timeout( + TIMEOUT, + subscribe(vec![range(Anchor::Relative(2), Some(Anchor::Time(8)))]), + ) + .await + .expect("subscribe timeout") + .unwrap(); + assert_eq!( + read(&mut second, 3).await, + [Some(7), Some(8), None], + "only the groups the publisher counts back to" + ); + drop(first); + let _track = track.finish().unwrap(); +} + +/// A later reader's relative start on a copy reading a track from its oldest group, which +/// already delivers every group the reader asks for, still asks the publisher where it starts, +/// and starts there: counting back past group 1, at group 1. Mirrors the `@moq/net` +/// `integration: lite-07 counts a later relative start back to where the oldest range +/// started`. +#[kio::test] +async fn a_later_relative_start_counts_back_to_where_the_oldest_range_started() { + let publisher = produce_origin(1); + let broadcast = publisher + .route("room", moq_net::origin::RouteOptions::hidden()) + .unwrap(); + let track = broadcast + .broadcast() + .create_track( + "data", + Info::default().with_timescale(Timescale::MILLI).with_max_age(FOREVER), + ) + .unwrap(); + broadcast.announce().unwrap(); + for key in 1..4 { + let mut group = track.create_group(moq_net::group::Info { start: key }).unwrap(); + group.write_frame(millis(key), &b"x"[..]).unwrap(); + group.finish().unwrap(); + } + + let subscriber = produce_origin(2); + let _pair = peer("moq-lite-07-wip".parse::().unwrap(), &publisher, &subscriber).await; + let remote = kio::clock::timeout(TIMEOUT, subscriber.consume().follow("room", None).unwrap().available()) + .await + .expect("resolve timeout") + .expect("broadcast resolves") + .unwrap(); + let subscribe = |ranges: Vec| { + let subscription = Subscription::default() + .with_delay(Delay::default().with_max(FOREVER)) + .with_order(Order::Ascending) + .with_ranges(ranges); + remote.track("data").unwrap().subscribe(subscription) + }; + let read = async |reader: &mut moq_net::track::Subscriber, count: usize| { + let mut read = Vec::new(); + for _ in 0..count { + let group = kio::clock::timeout(TIMEOUT, reader.recv_group()) + .await + .expect("group timeout") + .unwrap(); + read.push(group.map(|group| group.start)); + } + read + }; + + let mut oldest = kio::clock::timeout(TIMEOUT, subscribe(vec![Range::oldest()])) + .await + .expect("subscribe timeout") + .unwrap(); + assert_eq!(read(&mut oldest, 3).await, [Some(1), Some(2), Some(3)]); + + let mut later = kio::clock::timeout(TIMEOUT, subscribe(vec![range(Anchor::Relative(5), None)])) + .await + .expect("subscribe timeout") + .unwrap(); + assert_eq!( + read(&mut later, 3).await, + [Some(1), Some(2), Some(3)], + "counted back past group 1, as the publisher does" + ); + drop(oldest); + let _track = track.finish().unwrap(); +} + +/// A relative start on a lite-07 copy never resolves against the groups the copy holds: over a +/// copy holding groups 9 and 10, a reader counting 5 back starts where the publisher counts +/// to, not at 9. While the subscription that delivered them is live, the copy asks it for the +/// reader's range, and the publisher counts back from its latest group, 10, to 5. Once it is +/// gone, the copy subscribes again and the reader waits for that answer, counted back from the +/// Largest it reports: 11, written while the copy was idle, so 6. Mirrors the `@moq/net` +/// `integration: lite-07 never resolves a relative start against the groups a copy holds`. +#[kio::test] +async fn a_relative_start_never_resolves_against_the_groups_a_copy_holds() { + for live in [true, false] { + let publisher = produce_origin(1); + let broadcast = publisher + .route("room", moq_net::origin::RouteOptions::hidden()) + .unwrap(); + let track = broadcast + .broadcast() + .create_track( + "data", + Info::default().with_timescale(Timescale::MILLI).with_max_age(FOREVER), + ) + .unwrap(); + broadcast.announce().unwrap(); + let write = |key: u64| { + let mut group = track.create_group(moq_net::group::Info { start: key }).unwrap(); + group.write_frame(millis(key), &b"x"[..]).unwrap(); + group.finish().unwrap(); + }; + for key in 0..=10 { + write(key); + } + + let subscriber = produce_origin(2); + let _pair = peer("moq-lite-07-wip".parse::().unwrap(), &publisher, &subscriber).await; + let remote = kio::clock::timeout(TIMEOUT, subscriber.consume().follow("room", None).unwrap().available()) + .await + .expect("resolve timeout") + .expect("broadcast resolves") + .unwrap(); + let subscribe = |ranges: Vec| { + let subscription = Subscription::default() + .with_delay(Delay::default().with_max(FOREVER)) + .with_order(Order::Ascending) + .with_ranges(ranges); + remote.track("data").unwrap().subscribe(subscription) + }; + let read = async |reader: &mut moq_net::track::Subscriber| { + let mut read = Vec::new(); + loop { + let group = kio::clock::timeout(TIMEOUT, reader.recv_group()) + .await + .expect("group timeout") + .unwrap(); + match group { + Some(group) => read.push(group.start), + None => break, + } + } + read.sort_unstable(); + read + }; + + // The copy holds groups 9 and 10. + let mut first = kio::clock::timeout(TIMEOUT, subscribe(vec![range(Anchor::Time(9), Some(Anchor::Time(10)))])) + .await + .expect("subscribe timeout") + .unwrap(); + assert_eq!(read(&mut first).await, [9, 10]); + let (expected, end) = match live { + true => (vec![5, 6, 7, 8, 9, 10], 10), + false => { + // Its upstream subscription leaves with its last reader, and the track moves on. + drop(first); + kio::clock::sleep(Duration::from_secs(1)).await; + write(11); + (vec![6, 7, 8, 9, 10, 11], 11) + } + }; + let mut later = kio::clock::timeout( + TIMEOUT, + subscribe(vec![range(Anchor::Relative(5), Some(Anchor::Time(end)))]), + ) + .await + .expect("subscribe timeout") + .unwrap(); + assert_eq!(read(&mut later).await, expected, "live: {live}"); + let _track = track.finish().unwrap(); + } +} + +/// A finished copy has no upstream subscription left to carry a later reader's relative start, +/// so the copy asks again with a subscription of its own, and the reader still reads what the +/// publisher counts back to rather than nothing: the same groups a cold copy reads. Mirrors the +/// `@moq/net` `integration: lite-07 starts a relative start on a finished copy where the +/// publisher does`. +#[kio::test] +async fn a_relative_start_on_a_finished_copy_reads_the_publishers_groups() { + // Whether the copy holds every group the reader counts back to or only some. + for from in [0, 9] { + let publisher = produce_origin(1); + let broadcast = publisher + .route("room", moq_net::origin::RouteOptions::hidden()) + .unwrap(); + let track = broadcast + .broadcast() + .create_track( + "data", + Info::default().with_timescale(Timescale::MILLI).with_max_age(FOREVER), + ) + .unwrap(); + broadcast.announce().unwrap(); + for key in 0..=10 { + let mut group = track.create_group(moq_net::group::Info { start: key }).unwrap(); + group.write_frame(millis(key), &b"x"[..]).unwrap(); + group.finish().unwrap(); + } + let _track = track.finish().unwrap(); + + let subscriber = produce_origin(2); + let _pair = peer("moq-lite-07-wip".parse::().unwrap(), &publisher, &subscriber).await; + let remote = kio::clock::timeout(TIMEOUT, subscriber.consume().follow("room", None).unwrap().available()) + .await + .expect("resolve timeout") + .expect("broadcast resolves") + .unwrap(); + let subscribe = |ranges: Vec| { + let subscription = Subscription::default() + .with_delay(Delay::default().with_max(FOREVER)) + .with_order(Order::Ascending) + .with_ranges(ranges); + remote.track("data").unwrap().subscribe(subscription) + }; + let read = async |reader: &mut moq_net::track::Subscriber| { + let mut read = Vec::new(); + while let Some(group) = kio::clock::timeout(TIMEOUT, reader.recv_group()) + .await + .expect("group timeout") + .unwrap() + { + read.push(group.start); + } + read.sort_unstable(); + read + }; + + // The first reader holds the finished copy. + let mut first = kio::clock::timeout(TIMEOUT, subscribe(vec![range(Anchor::Time(from), None)])) + .await + .expect("subscribe timeout") + .unwrap(); + assert_eq!(read(&mut first).await, (from..=10).collect::>()); + // Its subscription is over by now. + kio::clock::sleep(Duration::from_secs(1)).await; + let mut later = kio::clock::timeout(TIMEOUT, subscribe(vec![range(Anchor::Relative(5), None)])) + .await + .expect("subscribe timeout") + .unwrap(); + assert_eq!(read(&mut later).await, [5, 6, 7, 8, 9, 10], "holding from {from}"); + // An ask ending before the track does is over at its own end, so the next one still goes + // out. + for (ranges, expected) in [ + (range(Anchor::Relative(4), Some(Anchor::Time(6))), vec![6]), + (range(Anchor::Relative(3), None), vec![7, 8, 9, 10]), + ] { + let mut reader = kio::clock::timeout(TIMEOUT, subscribe(vec![ranges])) + .await + .expect("subscribe timeout") + .unwrap(); + assert_eq!(read(&mut reader).await, expected, "holding from {from}"); + } + drop(first); + } +} + /// A time end between two groups ends an ordered read once the publisher created a group past /// it: handing that group out would have passed the starts between. Through the publisher's own /// origin that is its own state. A lite-07 copy ends there only on what its wire proves, the