Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion drafts/draft-lcurley-moq-lite.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ This is what lets a subscriber move a Track to a different publisher partway thr
A Group can therefore be assembled from more than one publisher: each contributes a disjoint run of frames, and the subscriber concatenates them in index order.

A partial Group is only ever delivered to a subscriber that asked for one, with FETCH or a Position Start.
A publisher that holds only the later frames of a Group a subscription covers names it in a [SUBSCRIBE_DROP](#subscribe-drop) rather than deliver the part it holds.
A relay that holds only the later frames of a Group a subscription covers fetches the earlier frames from upstream and delivers the Group whole; a publisher that can't obtain them names the Group in a [SUBSCRIBE_DROP](#subscribe-drop) rather than deliver the part it holds.
One that holds its earlier frames, such as a relay whose upstream closed the Group, delivers them and closes the Group Stream without a GROUP_END.
A Group is the unit of decodability: its earlier Frames may carry a keyframe or compression state the later Frames need.

Expand Down
64 changes: 64 additions & 0 deletions js/net/src/lite/copy_fill.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,3 +550,67 @@ test.each([Lite.ALPN_05, Lite.ALPN_06, Lite.ALPN_07_WIP])(
}
},
);

// A publisher whose cache holds group 2 from frame 3 only fetches the head from its track's
// handler and serves the group whole to a subscriber reading whole groups, as the Rust
// `position_group` does: a cache only makes things faster. A refused head drops the group.
test.each([Lite.ALPN_06, Lite.ALPN_07_WIP])("%s: a publisher fetches a headless group's head", async (alpn) => {
for (const refuse of [false, true]) {
const pair = createMockTransportPair(alpn);
const origin = new OriginProducer();
const [client, server] = await Promise.all([
connectSession({ url, transport: pair.client }),
acceptSession({ transport: pair.server, url, publish: origin.consume() }),
]);
const broadcast = origin.route(Path.from("room")).broadcast;
const track = defined(broadcast.createTrack("video", { timescale: Timescale.MILLI, maxAge: null }));
const tail = hooks.receiveGroup(track, { start: 2n, offset: 3 });
for (const frame of [3, 4])
tail.writeFrame({ payload: enc.encode(`c${frame}`), timestamp: Timestamp.fromMillis(2000) });
tail.finish();
write(track.createGroup(3n), 3n, "live");
const requests = track.requests();
void (async () => {
for (let fill = await requests.requestedFill(); fill; fill = await requests.requestedFill()) {
for (let key = fill.wanted(); key !== undefined; key = fill.wanted()) {
if (key !== 2n || refuse) {
fill.refuseGroups(key, key + 1n, new StreamError(StreamCode.NotFound));
continue;
}
const group = defined(fill.accept(key));
for (let frame = 0; frame < 5; frame++)
group.writeFrame({ payload: enc.encode(`c${frame}`), timestamp: Timestamp.fromMillis(2000) });
group.finish();
}
fill.close();
}
})();
const remote = wireOf(client).consume(Path.from("room"));
try {
const reader = remote.track("video").subscribe({
ranges: [{ start: { time: 2n } }],
delay: { max: null },
order: "ascending",
});
const ordered = reader.ordered();
const got: [bigint, string[]][] = [];
for (let group = await ordered.nextGroup(); group; group = await ordered.nextGroup()) {
const frames: string[] = [];
for (let frame = await group.readFrame(); frame; frame = await group.readFrame())
frames.push(dec.decode(frame.payload));
got.push([group.start, frames]);
if (group.start === 3n) break;
}
const head: [bigint, string[]][] = refuse ? [] : [[2n, ["c0", "c1", "c2", "c3", "c4"]]];
expect(got).toEqual([...head, [3n, ["live 3"]]]);
reader.close();
} finally {
remote.close();
requests.close();
broadcast.close();
client.abort();
server.abort();
origin.close();
}
}
});
46 changes: 42 additions & 4 deletions js/net/src/lite/publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1325,6 +1325,7 @@ export class Publisher {
},
});
const finished = await this.#runTrack(track, stream.writer, controls, {
source: consumer,
sub: msg.id,
broadcast: msg.broadcast,
stamp,
Expand Down Expand Up @@ -1507,6 +1508,8 @@ export class Publisher {
stream: Writer,
controls: SubscriptionControls,
serving: {
/** The track `track` reads, which fetches the frames a group's copy lacks. */
source: track.Consumer;
sub: bigint;
broadcast: Path.Valid;
stamp: Stamp | undefined;
Expand All @@ -1523,7 +1526,7 @@ export class Publisher {
initial: bigint;
},
): Promise<boolean> {
const { sub, broadcast, stamp, publisher, compress, named, bounds, initial } = serving;
const { source, sub, broadcast, stamp, publisher, compress, named, bounds, initial } = serving;
// What the wire was told of each key: served, or given up, and how. Kept in the
// reader's own record, which its drops consult too. Undefined before lite-05.
const record = named ? hooks.wire(track) : undefined;
Expand Down Expand Up @@ -2052,10 +2055,12 @@ export class Publisher {
// The FIN waits for every stream to finish or reset, including one that has not opened yet.
// A stream reset to make room fails the group like any other reset, so it is dropped.
const task = (async () => {
const writer = await this.#openGroup(options);
let failed: Error | undefined;
let failed = await this.#fetchHead(source, track, options);
try {
failed = writer instanceof Error ? writer : await this.#serveGroup(writer, options);
if (!failed) {
const writer = await this.#openGroup(options);
failed = writer instanceof Error ? writer : await this.#serveGroup(writer, options);
}
} finally {
options.givenUp.close();
}
Expand Down Expand Up @@ -2562,6 +2567,39 @@ export class Publisher {
}
}

/**
* Swap a group whose copy lacks frames the subscription asked for for a copy fetched from
* that frame on, as the Rust `position_group` does: a cache only makes things faster, so
* a partial hit asks upstream for the rest.
*
* @returns The fetch's error, for SUBSCRIBE_DROP to name, when nothing upstream supplies them
*
* @internal
*/
async #fetchHead(source: track.Consumer, reader: track.Subscriber, options: RunGroup): Promise<Error | undefined> {
const held = options.group;
if (hooks.groupIndex(held) <= options.start) return undefined;
const asking = reader.subscription.peek();
const left = new AbortController();
void options.unsubscribed.then(() => left.abort(new Error("unsubscribed while fetching a group's head")));
const fetch: track.FetchGroupOptions & broadcast.FetchFrom = {
priority: asking?.priority,
maxDelay: asking?.delay?.max ?? undefined,
signal: left.signal,
frameStart: options.start,
};
try {
options.group = await source.fetchGroup(held.start, fetch);
} catch (err: unknown) {
return error(err);
} finally {
options.givenUp.close();
held.close();
}
options.givenUp = new GivenUp(options.group);
return undefined;
}

/**
* Serves one group on the stream {@link #openGroup} opened for it.
*
Expand Down
39 changes: 0 additions & 39 deletions quest/m1/missing-head-asks-upstream.md

This file was deleted.

6 changes: 2 additions & 4 deletions quest/m2/time-addressing/range-table.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,11 @@ Recommended:
- The relayed run uses the real copy path in each language, so a JS copy under a Rust origin
is covered the way `just test relay-fill` already covers fills.
- Drive time with the manual clocks both test suites already use.
- A copy holding only a group's later frames is a case too: the relay fetches the head and
delivers the group whole, or names it in SUBSCRIBE_DROP when upstream can't supply it.

Public API: none. Wire: none; a divergence this finds is fixed under lite-07-wip before the cut.

## Required

- [No relative anchors](/quest/m2/time-addressing/no-relative-anchors.md) - the range shapes this table pins

## Related

- [A relay fetches a group's missing head](/quest/m1/missing-head-asks-upstream.md) - a partial group a copy holds is another case for the table
Loading