Skip to content
Merged
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
24 changes: 20 additions & 4 deletions js/net/src/ietf/subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@ import {
} from "../error.ts";
import * as netGroup from "../group.ts";
import { Cost, type Route, routesEqual, UNKNOWN_HOP } from "../hop.ts";
import { finalSequence, hiddenBelow, hooks, scopeCaptures, scopeHead, scopeOverlaps } from "../internal.ts";
import {
finalSequence,
groupBounds,
hiddenBelow,
hooks,
scopeCaptures,
scopeHead,
scopeOverlaps,
} from "../internal.ts";
import * as Path from "../path.ts";
import { type Cursor, Reader, type Stream } from "../stream.ts";
import { Tail } from "../tail.ts";
Expand Down Expand Up @@ -1007,10 +1015,18 @@ export class Subscriber {
priority: fromWire(ok.properties.priority ?? 128),
maxAge: maxCacheDuration === undefined ? undefined : Milli(Number(maxCacheDuration)),
});
if (ok.largest && ok.largest.groupId > BigInt(Number.MAX_SAFE_INTEGER)) {
throw new RangeError("largest group exceeds safe integers");
}
// The live join starts at the publisher's edge, whatever floor the demand asked for.
// With no Largest it skips nothing, so every group is owed: a lower one's stream may
// still arrive after a higher one's.
const groups = request.subscription.groups;
hooks.startTrack(subscription.track, {
start: ok.largest ? Number(ok.largest.groupId) : 0,
requested: groups?.start === undefined ? undefined : groupBounds(groups).start,
});
if (ok.largest) {
if (ok.largest.groupId > BigInt(Number.MAX_SAFE_INTEGER)) {
throw new RangeError("largest group exceeds safe integers");
}
// Object IDs can start past zero or skip, while readers see frame ordinals, so only
// the group carries over: its first frame is the edge a reader can count on.
hooks.reportedLargest(subscription.track, { group: Number(ok.largest.groupId), frame: 0 });
Expand Down
42 changes: 37 additions & 5 deletions js/net/src/ietf/tail.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { expect, test } from "bun:test";
import { Milli } from "@moq/clock";
import { ProtocolViolation, StreamCode, StreamError } from "../error.ts";
import type { Consumer as GroupConsumer } from "../group.ts";
import { GRACE } from "../ledger.ts";
import { createMockTransportPair } from "../mock.ts";
import * as Path from "../path.ts";
import { Reader, Stream } from "../stream.ts";
import { TAIL_GRACE_MS } from "../tail.ts";
import { NativeSession } from "./adapter.ts";
import { type GroupFlags, Group as GroupMessage } from "./object.ts";
import { PublishDone } from "./publish.ts";
Expand Down Expand Up @@ -52,7 +52,7 @@ function groupStream(subscriber: Subscriber, groupId: number, flags = FLAGS) {
}

/** A subscriber with one track subscribed and answered; the test plays the publisher. */
async function subscribed() {
async function subscribed(largest?: number) {
const pair = createMockTransportPair(ALPN.DRAFT_19);
const session = new NativeSession(pair.server, VERSION, true);
const subscriber = new Subscriber({ session });
Expand All @@ -66,7 +66,11 @@ async function subscribed() {
expect(await peer.reader.u53()).toBe(Subscribe.id);
const request = await Subscribe.decode(peer.reader, VERSION);
await peer.writer.u53(SubscribeOk.id);
await new SubscribeOk({ requestId: request.requestId, trackAlias: ALIAS }).encode(peer.writer, VERSION);
await new SubscribeOk({
requestId: request.requestId,
trackAlias: ALIAS,
largest: largest === undefined ? undefined : { groupId: BigInt(largest), objectId: 0n },
}).encode(peer.writer, VERSION);

return {
subscriber,
Expand Down Expand Up @@ -109,7 +113,7 @@ test("a group stream that arrives after PUBLISH_DONE is delivered", async () =>
expect(await reader.recvGroup()).toBeUndefined();
expect(await reader.closed).toBeNull();
// The Stream Count was met, so nothing waited out the grace.
expect(performance.now() - started).toBeLessThan(TAIL_GRACE_MS);
expect(performance.now() - started).toBeLessThan(GRACE);
});

test("a group read across PUBLISH_DONE is delivered whole", async () => {
Expand Down Expand Up @@ -139,7 +143,7 @@ test("a Stream Count of 0 is a hint, so a late stream within the grace is still
expect(await readAll(await reader.recvGroup())).toEqual(["0.0"]);
expect(await reader.recvGroup()).toBeUndefined();
expect(await reader.closed).toBeNull();
expect(performance.now() - started).toBeGreaterThanOrEqual(TAIL_GRACE_MS - 5);
expect(performance.now() - started).toBeGreaterThanOrEqual(GRACE - 5);
});

test("a PUBLISH_DONE with an error status aborts the track", async () => {
Expand Down Expand Up @@ -221,6 +225,34 @@ test("a compact stream starting at object 5 is subgroup 5, refused without a gro
expect((await reader.recvGroup())?.sequence).toBe(6);
});

// A live join starts at the publisher's Largest, and QUIC does not order streams, so the
// groups from there up to END_OF_TRACK are owed even when a higher one lands first.
test("a reader holds for groups from SUBSCRIBE_OK's Largest that land after a higher one", async () => {
const { subscriber, reader, done } = await subscribed(3);
const high = groupStream(subscriber, 5);
high.write(object("5.0"));
high.finish();
await high.handled;
const end = groupStream(subscriber, 6);
end.write(END_OF_TRACK);
end.finish();
expect(await reader.finished()).toEqual({ group: 6, frame: 0 });
expect(await readAll(await reader.recvGroup())).toEqual(["5.0"]);

// Asked before the lower groups land: a reader that ended early returns nothing.
const held = reader.recvGroup();
for (const sequence of [3, 4]) {
const low = groupStream(subscriber, sequence);
low.write(object(`${sequence}.0`));
low.finish();
}
expect(await readAll(await held)).toEqual(["3.0"]);
expect(await readAll(await reader.recvGroup())).toEqual(["4.0"]);
expect(await reader.recvGroup()).toBeUndefined();
await done(TRACK_ENDED, 4n);
expect(await reader.closed).toBeNull();
});

test("a group at or past the END_OF_TRACK aborts the track", async () => {
const { subscriber, reader } = await subscribed();
const end = groupStream(subscriber, 2);
Expand Down
15 changes: 14 additions & 1 deletion js/net/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,19 @@ export const hooks: {
/**
* Declare the end a wire subscription received: repeating it is a no-op, and a mid-group
* end may refine the group boundary it falls just before. Throws `ProtocolViolation` on
* any other second end. Mirrors the Rust `finish_at_pending`; assigned by `track.ts`.
* any other second end. Readers then hold at a hole below it until the hole fills, folds
* past the grace, or the track closes, since QUIC may deliver a lower group's stream after
* a higher one. Mirrors the Rust `finish_at_pending`; assigned by `track.ts`.
*/
endTrack: (producer: Producer, end: Location) => void;
// pending: /quest/m1/public-model/deferred.md
/**
* Declare the first group a wire subscription's live feed serves (lite SUBSCRIBE_START, or
* IETF SUBSCRIBE_OK's Largest) for the floor its SUBSCRIBE requested: while the demand keeps
* that floor, a pending tail owes nothing below it. Mirrors the Rust `start_at`; assigned by
* `track.ts`.
*/
startTrack: (producer: Producer, served: { start: number; requested?: number }) => void;
// pending: /quest/m1/public-model/ingest.md
/**
* Mark a track as a session's copy of a remote one: its groups are never checked for a
Expand Down Expand Up @@ -353,6 +363,9 @@ export const hooks: {
endTrack: () => {
throw new Error("track.ts not loaded");
},
startTrack: () => {
throw new Error("track.ts not loaded");
},
makeRequest: () => {
throw new Error("track.ts not loaded");
},
Expand Down
115 changes: 115 additions & 0 deletions js/net/src/ledger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { Instant, Milli, Nano } from "@moq/clock";

/**
* How long a subscriber waits for a group stream it cannot account for once the publisher
* has ended the subscription.
*
* A group reset before its header arrived leaves no trace, and QUIC does not order streams,
* so a stream opened before the end can still be in flight after it. This bounds the wait on
* IETF, and on moq-lite when the subscription has no max delay to bound it with.
*/
export const GRACE = Milli(1000);

// private: plumbing
/** The grace for a subscription with `maxDelay`: how long it was willing to wait for a late group anyway, or {@link GRACE} without one. */
export function grace(maxDelay: Milli): Milli {
return maxDelay > 0 ? maxDelay : GRACE;
}

// A run of accounted sequences `[start, end)`, and when the gap below it opened, which is
// when its missing groups became late.
type Run = { start: number; end: number; since: Instant };

/**
* The sequences a feed has accounted for, and how long each gap between them is waited for.
*
* A subscription's `Tail` and a track's record of live arrivals both keep one, so they
* give up on a missing group by the same rule. Mirrors the Rust `Ledger`.
*
* @internal
*/
export class Ledger {
// Disjoint, sorted, non-adjacent runs of accounted sequences. A gap older than the grace
// can no longer be waited for, so it folds into the runs around it, which bounds this by
// the gaps opened within the grace rather than every gap ever seen. A fold is final: a
// later, longer grace or a lowered floor cannot reopen it.
#runs: Run[] = [];

/** Record sequences `[start, end)` as accounted for. Gaps fold only on {@link expire}. */
account(start: number, end: number, at: Instant): void {
if (start >= end) return;

// An in-order feed extends the last run in place, the usual case for every arrival.
const last = this.#runs.at(-1);
if (last && last.start <= start && start <= last.end) {
last.end = Math.max(last.end, end);
return;
}

const runs: Run[] = [];
let merged: Run | undefined;
let placed = false;
for (const run of this.#runs) {
if (run.end < start) {
runs.push(run);
} else if (end < run.start) {
// Splitting a gap leaves both halves as late as it was.
if (!placed) runs.push(merged ?? { start, end, since: run.since });
placed = true;
runs.push(run);
} else {
merged = {
start: Math.min(merged?.start ?? start, run.start),
end: Math.max(merged?.end ?? end, run.end),
since: merged?.since ?? run.since,
};
}
}
// A run past every other one opens a new gap.
if (!placed) runs.push(merged ?? { start, end, since: at });
this.#runs = runs;
}

/** Fold every gap at least `grace` old into the runs around it. */
expire(at: Instant, grace: Milli): void {
const limit = Nano.fromMilli(grace);
const runs: Run[] = [];
for (const run of this.#runs) {
const below = runs.at(-1);
if (below && Instant.durationSince(at, run.since) >= limit) {
runs[runs.length - 1] = { ...below, end: run.end };
} else {
runs.push(run);
}
}
this.#runs = runs;
}

/** Whether a gap separates two runs, so {@link expire} has anything to fold. */
hasGaps(): boolean {
return this.#runs.length > 1;
}

/** The lowest sequence accounted for. */
first(): number | undefined {
return this.#runs[0]?.start;
}

/** Whether every sequence in `[start, end)` is accounted for. */
covers(start: number, end: number): boolean {
if (start >= end) return true;
// Runs merge on insert, so one run covers the span or none does.
return this.#runs.some((run) => run.start <= start && end <= run.end);
}

/** Restart the age of every gap reaching into `[start, end)`, which the demand newly asks for. */
demand(start: number, end: number, at: Instant): void {
if (start >= end) return;
let below = 0;
this.#runs = this.#runs.map((run) => {
const reached = below < end && start < run.start;
below = run.end;
return reached ? { ...run, since: at } : run;
});
}
}
13 changes: 7 additions & 6 deletions js/net/src/lite/subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import { error, ProtocolViolation, reason, StreamCode, StreamError, sessionCause
import * as netGroup from "../group.ts";
import { Cost, type Hop, MAX_HOPS, type Route, routesEqual, UNKNOWN_HOP } from "../hop.ts";
import { groupBounds, hiddenBelow, hooks, scopeCaptures, scopeHead, scopeOverlaps } from "../internal.ts";
import { grace } from "../ledger.ts";
import * as Path from "../path.ts";
import { sendOrder } from "../priority.ts";
import { type OpenOptions, type Reader, Stream, Writer } from "../stream.ts";
import { TAIL_GRACE_MS, Tail } from "../tail.ts";
import { Tail } from "../tail.ts";
import * as Time from "../time.ts";
import type * as track from "../track.ts";
import { untilAborted } from "../util/abort.ts";
Expand Down Expand Up @@ -859,10 +860,7 @@ export class Subscriber {
// presentation-time drift), but it is how long the subscriber was willing to wait
// for a late group anyway. Already the smaller of the subscriber's and the track's.
tail: new Tail({
grace: () => {
const maxDelay = producer.subscription.peek()?.maxDelay ?? Milli.zero;
return maxDelay > 0 ? maxDelay : TAIL_GRACE_MS;
},
grace: () => grace(producer.subscription.peek()?.maxDelay ?? Milli.zero),
}),
requested: msg.startGroup,
held: state.track,
Expand Down Expand Up @@ -1205,6 +1203,7 @@ export class Subscriber {

if ("start" in resp) {
entry.start = resp.start.group;
hooks.startTrack(entry.track, { start: entry.start, requested: entry.requested });
hooks.reportedLargest(entry.track, resp.start.largest);
// The groups the SUBSCRIBE asked for below it are not waited for, whatever the
// demand asks later. One that still arrives is delivered.
Expand All @@ -1216,7 +1215,9 @@ export class Subscriber {
// A local close can win the race with the response; there is nothing left to end.
if (entry.track.closed.peek() !== undefined) continue;
try {
entry.track.finishAt({ group: entry.end, frame: 0 });
// Lower groups may still be on the wire behind a higher one, so readers hold
// at a hole below the end until the tail settles.
hooks.endTrack(entry.track, { group: entry.end, frame: 0 });
} catch (err) {
// lite-05 specified an inclusive end, and @moq/net 0.1.3 to 0.1.9 sent one, so
// there an end below a received group only costs the early boundary: the FIN
Expand Down
46 changes: 42 additions & 4 deletions js/net/src/lite/tail.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ async function readAll(group: GroupConsumer | undefined): Promise<string[]> {
}
}

/** Let every pending microtask run, so a parked read gets a turn without a timer. */
const settle = () => new Promise<void>((resolve) => setImmediate(resolve));

/** Whether `promise` settles within `ms`. */
async function settlesWithin(promise: Promise<unknown>, ms: number): Promise<boolean> {
let timer: ReturnType<typeof setTimeout> | undefined;
Expand Down Expand Up @@ -166,7 +169,7 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver
expect(await reader.closed).toBeNull();
});

test("readers end before a missing group's grace settles the subscription", async () => {
test("readers hold at a missing group until the grace settles the subscription", async () => {
const realTimeout = globalThis.setTimeout;
const realNow = performance.now.bind(performance);
let now = 0;
Expand Down Expand Up @@ -197,17 +200,23 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver
await fin();
await graceArmed;

// Group 0 has no header, so accounting still waits. Both reader cursors
// skip that hole and see the end as soon as the newest group reaches it.
// Group 0 has no header, so accounting still waits, and its stream may yet
// land behind group 1's. The arrival reader holds at the hole; the sequence
// reader is already past it and ends at the boundary.
expect((await reader.recvGroup())?.sequence).toBe(1);
expect(await reader.recvGroup()).toBeUndefined();
let ended = false;
const held = reader.recvGroup().finally(() => (ended = true));
expect(await ordered.readString()).toBe("1.0");
expect(await ordered.readString()).toBeUndefined();
await settle();
expect(ended).toBe(false);
expect(reader.closed.peek()).toBeUndefined();
expect(reader.end()).toEqual({ group: 2, frame: 0 });

// The grace gives up on group 0, and the settled subscription ends the reader.
now = GRACE;
expire();
expect(await held).toBeUndefined();
expect(await reader.closed).toBeNull();
ordered.close();
} finally {
Expand All @@ -217,6 +226,35 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver
}
});

// QUIC does not order streams, so a lower group's stream can land after a higher one and
// after SUBSCRIBE_END. The reader receives it before its end, which then comes at once.
test("a lower group that lands after SUBSCRIBE_END reaches the reader", async () => {
const { subscriber, reader, respond, fin } = await subscribed(version, Milli(60_000));
await respond({ start: new SubscribeStart(0) });
const high = groupStream(subscriber, 1);
high.finish();
await high.handled;
await respond({ end: new SubscribeEnd(2, 2) });
expect(await reader.finished()).toEqual({ group: 2, frame: 0 });

expect((await reader.recvGroup())?.sequence).toBe(1);
// Asked before group 0 lands: a reader that ended early returns nothing.
const held = reader.recvGroup();

const low = groupStream(subscriber, 0);
low.write("0.0");
low.finish();
await low.handled;
const late = await held;
expect(late?.sequence).toBe(0);
expect(await late?.readString()).toBe("0.0");
expect(await reader.recvGroup()).toBeUndefined();

await fin();
expect(await reader.closed).toBeNull();
subscriber.close();
});

test.skipIf(version === Version.DRAFT_07)(
"a subscription ends without waiting once every group is accounted for",
async () => {
Expand Down
Loading
Loading