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
27 changes: 19 additions & 8 deletions js/net/bench/resume.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Sweep readers and route swaps for groups read through a logical front, against the same
* groups read straight from the track.
* Sweep readers and route swaps for direct and forked readers through a logical front,
* against the same groups read straight from the track.
*
* A front reads its serving route's copy directly, so with one route it should cost about what a
* direct read does, and each swap should leave nothing behind once its groups are read.
Expand Down Expand Up @@ -38,7 +38,7 @@ function read(subscribers: Subscriber[]) {
}

/** Microseconds per group per reader, reading through a front after `swaps` route swaps, or directly. */
async function measure(readers: number, swaps: number | undefined): Promise<number> {
async function measure(readers: number, swaps: number | undefined, fork = false): Promise<number> {
let sequence = 0;
const sources: { broadcast: BroadcastProducer; write: () => void }[] = [];
const source = () => {
Expand Down Expand Up @@ -68,7 +68,14 @@ async function measure(readers: number, swaps: number | undefined): Promise<numb
front = new Front();
front.serve(source().consume());
const consumer = front.consumer;
subscribers = Array.from({ length: readers }, () => consumer.track("seq").subscribe());
const track = consumer.track("seq");
if (fork) {
const parent = track.subscribe();
subscribers = Array.from({ length: readers }, () => parent.fork());
parent.close();
} else {
subscribers = Array.from({ length: readers }, () => track.subscribe());
}
for (let swap = 0; swap < swaps; swap++) {
publish();
read(subscribers);
Expand Down Expand Up @@ -96,28 +103,32 @@ async function measure(readers: number, swaps: number | undefined): Promise<numb

// Warm the JIT so the first row isn't the slowest.
await measure(4, 1);
await measure(4, 1, true);
await measure(4, undefined);

console.log("readers,swaps,direct_us,front_us");
console.log("readers,swaps,direct_us,front_us,fork_us");
let baseline: number | undefined;
for (const swaps of swapCounts) {
for (const readers of readerCounts) {
const direct: number[] = [];
const front: number[] = [];
const fork: number[] = [];
// Interleaved, so a noisy stretch of the machine lands on both columns alike.
for (let rep = 0; rep < reps; rep++) {
direct.push(await measure(readers, undefined));
front.push(await measure(readers, swaps));
fork.push(await measure(readers, swaps, true));
}
// The fastest run is the one least disturbed by the rest of the machine.
const directUs = Math.min(...direct);
const frontUs = Math.min(...front);
console.log(`${readers},${swaps},${directUs.toFixed(2)},${frontUs.toFixed(2)}`);
const forkUs = Math.min(...fork);
console.log(`${readers},${swaps},${directUs.toFixed(2)},${frontUs.toFixed(2)},${forkUs.toFixed(2)}`);

baseline ??= frontUs;
if (frontUs > baseline * maxSlope) {
if (Math.max(frontUs, forkUs) > baseline * maxSlope) {
throw new Error(
`${readers} readers after ${swaps} swaps cost ${frontUs.toFixed(2)} us/group, over ${maxSlope}x ${baseline.toFixed(2)}`,
`${readers} readers after ${swaps} swaps cost ${Math.max(frontUs, forkUs).toFixed(2)} us/group, over ${maxSlope}x ${baseline.toFixed(2)}`,
);
}
}
Expand Down
121 changes: 121 additions & 0 deletions js/net/src/resume.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect, test } from "bun:test";
import { Milli } from "@moq/clock";
import { Producer as BroadcastProducer } from "./broadcast.ts";
import * as Epoch from "./epoch.ts";
import { NotFound, StreamCode, StreamError } from "./error.ts";
Expand All @@ -9,6 +10,7 @@ import { Producer } from "./origin.ts";
import * as Path from "./path.ts";
import { Front } from "./resume.ts";
import type * as track from "./track.ts";
import { unwanted } from "./util/demand.ts";
import { overrideBroadcastWire, wireOf } from "./wire.ts";

const EPOCH = Epoch.parse("01900000-0000-7000-8000-000000000001");
Expand Down Expand Up @@ -99,6 +101,125 @@ async function setup() {
};
}

test("a fork continues its open group across a route failure after its parent closes", async () => {
const a = new BroadcastProducer();
const b = new BroadcastProducer();
const ta = copy(a);
const tb = copy(b);
const front = new Front();
front.serve(a.consume());
const parent = front.consumer.track("seq").subscribe();
const fork = parent.fork();
try {
const stalled = ta.write(0, 1, false);
const group = await fork.recvGroup();
if (!group) throw new Error("expected group 0");
expect(dec.decode((await group.readFrame())?.payload)).toBe("0.0");
parent.close();

const next = group.readFrame();
tb.write(0, 3);
front.serve(b.consume());
stalled.close(new StreamError(StreamCode.SessionClosed));
expect(dec.decode((await next)?.payload)).toBe("0.1");
expect(dec.decode((await group.readFrame())?.payload)).toBe("0.2");
expect(await group.readFrame()).toBeUndefined();
} finally {
parent.close();
fork.close();
front.consumer.close();
a.close();
b.close();
}
});

test.each(["conclude", "park"] as const)("a fork made after the front's %s replays its parent's copy", async (end) => {
const a = new BroadcastProducer();
const ta = copy(a);
const front = new Front();
front.serve(a.consume());
const parent = front.consumer.track("seq").subscribe();
ta.write(0);
ta.write(1);
expect(drain(parent)).toEqual([1]);
front[end]();
const fork = parent.fork({ maxDelay: Milli(10_000) });
try {
expect(drain(fork)).toEqual([0, 1]);
} finally {
parent.close();
fork.close();
front.consumer.close();
a.close();
}
});

test("a resumed fork has its own preferences and replay cursor", async () => {
const a = new BroadcastProducer();
const b = new BroadcastProducer();
const ta = copy(a);
const tb = copy(b);
const front = new Front();
front.serve(a.consume());
const parent = front.consumer.track("seq").subscribe({
priority: 2,
groups: { start: { included: 1 } },
});
ta.write(0);
ta.write(1);
expect(drain(parent)).toEqual([1]);
const fork = parent.fork({ priority: 7, maxDelay: Milli(10_000) });
try {
expect(drain(fork)).toEqual([0, 1]);
fork.update({ priority: 9 });
expect(parent.subscription.peek()?.priority).toBe(2);
expect(fork.subscription.peek()?.priority).toBe(9);
const subscribed = tb.producer.subscription.changed();
front.serve(b.consume());
expect((await subscribed)?.priority).toBe(9);
const narrowed = tb.producer.subscription.changed();
fork.close();
expect((await narrowed)?.priority).toBe(2);
tb.write(2);
expect(drain(parent)).toEqual([2]);
parent.close();
await unwanted(tb.producer.demand());
} finally {
parent.close();
fork.close();
front.consumer.close();
a.close();
b.close();
}
});

test("a resumed fork shares an on-demand track request", async () => {
const a = new BroadcastProducer();
const requested = wireOf(a).requested();
const front = new Front();
front.serve(a.consume());
const parent = front.consumer.track("seq").subscribe();
const request = await requested;
if (!request) throw new Error("expected on-demand track request");
const producer = request.accept();
const extra = wireOf(a).requested();
const fork = parent.fork();
try {
producer.writeFrame({ payload: enc.encode("shared") });
for (const subscriber of [parent, fork]) {
const group = await subscriber.recvGroup();
expect(dec.decode((await group?.readFrame())?.payload)).toBe("shared");
group?.close();
}
} finally {
parent.close();
fork.close();
front.consumer.close();
a.close();
}
expect(await extra).toBeUndefined();
});

test("a subscription reads on across a same-epoch route swap, each group once", async () => {
const { a, b, active, request, swap, close } = await setup();
const ta = copy(a);
Expand Down
34 changes: 28 additions & 6 deletions js/net/src/resume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,8 @@ class Reader implements ResumeSource {
/** Records the properties a copy committed, and which source it came from. */
#record: (info: track.Info, source: Source) => void;

constructor(route: Route, shell: ResumeShell, record: (info: track.Info, source: Source) => void) {
/** `parent` is the newest copy of the reader this one forks, if any. */
constructor(route: Route, shell: ResumeShell, record: (info: track.Info, source: Source) => void, parent?: Copy) {
this.route = route;
this.#shell = shell;
this.#record = record;
Expand All @@ -278,6 +279,19 @@ class Reader implements ResumeSource {
for (const copy of this.#copies) this.#bound(copy);
}),
);
// A fork starts on its parent's newest copy, at that copy's generation: the copy keeps its
// retained groups even where no route serves the track, between routes or once the front
// concluded.
if (parent) {
this.#generation = parent.generation;
this.#add({
generation: parent.generation,
source: parent.source,
sub: parent.sub.fork(this.#wire({})),
replaced: false,
lease: 0,
});
}
// Subscribed to the serving copy at once, so groups it has now are the reader's even if
// the route changes before the first read, and to each copy after.
this.sync();
Expand All @@ -295,7 +309,7 @@ class Reader implements ResumeSource {
// group they get meanwhile may be one the new route never has. See #observe.
for (const copy of this.#copies) copy.replaced = true;
const floor = this.#newest;
const copy: Copy = {
this.#add({
generation: route.generation,
source,
// The wire request carries the floor from the first message: some sessions read the
Expand All @@ -305,7 +319,12 @@ class Reader implements ResumeSource {
datagramsSeen: this.#datagram,
replaced: false,
lease: 0,
};
});
}

/** Read `copy` as the newest one, recording the properties it commits. */
#add(copy: Copy): void {
const { source } = copy;
source.retain();
this.#bound(copy);
this.#copies.push(copy);
Expand Down Expand Up @@ -677,9 +696,12 @@ class Reader implements ResumeSource {

fork(options?: track.Subscription): track.Subscriber {
this.sync();
const serving = this.#serving();
if (!serving) throw new Error("track has no serving route to fork from");
return serving.sub.fork(options);
const newest = this.#copies.at(-1);
return hooks.resumeSubscriber(
this.route.name,
options,
(shell) => new Reader(this.route, shell, this.#record, newest),
);
}

close(): void {
Expand Down
1 change: 1 addition & 0 deletions js/net/src/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2015,6 +2015,7 @@ export class Subscriber {
* publisher read the cache alongside the cursor it is serving from. Resolving the track
* again through the broadcast would not do: a dynamic serve is one request per peer
* subscription, so that mints a second producer the application has to answer separately.
* A routed track's fork follows the same logical track across route swaps.
*
* @internal Wire layers only.
*/
Expand Down
1 change: 0 additions & 1 deletion quest/m1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ lands before the cut.
- [Rust resumed reads](/quest/m1/resume-ordered.md) - a Rust reader takes the lowest group across route copies in both orders, as JS does
- [Resumed close](/quest/m1/resume-closed.md) - a resumed JS track keeps its declared end across copies, and its `closed` and `largest()` settle on a clean end
- [Resumed in-flight groups](/quest/m1/resume-in-flight.md) - a replaced route copy stays while a group below its bound is in flight or it backs a handed-out group late, and no longer than the reader's budget
- [Resumed forks](/quest/m1/resume-fork.md) - a fork of a resumed JS subscriber follows route swaps like its parent
- [Recovered group readiness](/quest/m1/resume-readable.md) - a recovered JS group's `readable()` ignores frames its replacement replays
- [Resumed datagrams](/quest/m1/resume-datagrams.md) - a resumed track skips only datagrams it already delivered, not every lower sequence
- [Received timestamps are media time](/quest/m1/lite-time-provenance.md) - a received timed track's timestamps are media time, so the rewind check can run across hops
Expand Down
33 changes: 0 additions & 33 deletions quest/m1/resume-fork.md

This file was deleted.