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
6 changes: 6 additions & 0 deletions .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ jobs:
nix develop --command bun install --frozen-lockfile
nix develop --command bun js/net/bench/broadcasts.ts

# A route above several interest heads lands once per head, so its re-price
# should expose any slope over heads times received routes.
- name: JS origin announce forwarding benchmark
if: ${{ !cancelled() }}
run: nix develop --command bun js/net/bench/forward.ts

# Fails if reading a fragmented frame costs more per byte as the frame grows.
- name: JS stream reader benchmark
if: ${{ !cancelled() }}
Expand Down
4 changes: 3 additions & 1 deletion doc/concept/moq-lite.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ prefixes it is told about.

A subscriber watching under a root sees advertisements named relative to that
root. The pattern scope filters which prefixes are visible without changing a
route's prefix. Announce events carry the covered path, captures, and what
route's prefix. When several routes advertise one prefix, each reader sees the
best route its scope can use, so a cheaper route scoped elsewhere never hides
it. Announce events carry the covered path, captures, and what
happened to it: Rust
`announce::Update { prefix, captures: Option<Vec<Pattern>>, route, kind }` and
TypeScript `Announce.Update { prefix, captures, route, kind }`, where the kind is
Expand Down
91 changes: 91 additions & 0 deletions js/net/bench/forward.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/** Sweep interest heads and received routes for one route re-price forwarded from a session. */
import * as Announce from "../src/announced.ts";
import { Producer as BroadcastProducer } from "../src/broadcast.ts";
import type { Established } from "../src/connection/established.ts";
import { forwardAnnounced } from "../src/connection/forward.ts";
import { Route } from "../src/hop.ts";
import { Producer } from "../src/origin.ts";
import * as Path from "../src/path.ts";
import { registerWire } from "../src/wire.ts";

const headCounts = [1, 4, 16];
const routeCounts = [8, 32, 128];
const updates = 32;
let checksum = 0;

// Let every interest stream's forwarding loop drain its queue.
const flush = () => new Promise<void>((resolve) => setImmediate(resolve));

/** A session standing in for the wire: one announce stream per interest head, driven by the bench. */
class FakeSession {
readonly discovery = true;
readonly streams: Announce.Producer[] = [];
readonly closed: Promise<Error | null>;
die!: () => void;

constructor() {
registerWire(this, { consume: () => new BroadcastProducer().consume() });
this.closed = new Promise((resolve) => {
this.die = () => resolve(null);
});
}

announced(): Announce.Consumer {
const stream = new Announce.Producer();
this.streams.push(stream);
return stream.consume();
}
}

const announce = (stream: Announce.Producer, prefix: Path.Valid, cost: bigint, kind: Announce.Kind) =>
stream.append({ prefix, captures: undefined, kind, route: Route.normalize({ cost }) });

console.log("touched,heads,routes,update_us");
for (const touched of ["covering", "single"] as const) {
for (const headCount of headCounts) {
for (const routeCount of routeCounts) {
const origin = new Producer();
const heads = Array.from({ length: headCount }, (_, index) => Path.from(`h${index}`));
const scoped = origin.scope(
Path.empty(),
new Path.Patterns(heads.map((head) => Path.Pattern.subtree(head))),
);
const session = new FakeSession();
forwardAnnounced(session as unknown as Established, scoped);
if (session.streams.length !== headCount) throw new Error("expected one announce stream per head");

// Routes beneath the heads, spread across them, plus one above every head. The wire
// presents that covering route on each head's stream, so it lands once per head.
for (let index = 0; index < routeCount; index++) {
const head = index % headCount;
announce(session.streams[head], Path.join(heads[head], Path.from(`r${index}`)), 1n, "announced");
}
for (const stream of session.streams) announce(stream, Path.empty(), 1n, "announced");
await flush();

const path = touched === "covering" ? Path.empty() : Path.join(heads[0], Path.from("r0"));
const table = origin.broadcasts();
if (table.peek().size !== routeCount + 1) throw new Error(`expected ${routeCount + 1} routes`);

const start = performance.now();
for (let index = 0; index < updates; index++) {
const cost = BigInt(index + 2);
if (touched === "covering") {
for (const stream of session.streams) announce(stream, path, cost, "updated");
} else {
announce(session.streams[0], path, cost, "updated");
}
await flush();
if (table.peek().get(path)?.cost.warm !== cost) throw new Error("re-price did not land");
checksum += table.peek().size;
}
const elapsed = performance.now() - start;
console.log(`${touched},${headCount},${routeCount},${((elapsed * 1000) / updates).toFixed(1)}`);

session.die();
await flush();
origin.close();
}
}
}
if (checksum === 0) throw new Error("benchmark did no work");
72 changes: 72 additions & 0 deletions js/net/src/ietf/publisher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,78 @@ test("a subscription outside a covering route's claim does not hear it", async (
origin.close();
});

/**
* A cheaper route claiming `room/chat` does not hide a costlier `room/video` route at the
* same prefix from a subscription at `room/video`.
*/
test("a subscription hears the best covering route its claim allows", async () => {
const pair = createMockTransportPair(ALPN.DRAFT_19);
const { pub, origin } = publisher(pair.server, { requiresSolicitation: true });
const scoped = (path: string) =>
origin.scope(Path.empty(), new Path.Patterns([Path.Pattern.subtree(Path.from(path))]));
const chat = scoped("room/chat").dynamic(Path.from("room"), { cost: 1n });
const video = scoped("room/video").dynamic(Path.from("room"), { cost: 5n });

const subscription = await Stream.open(pair.client, { version: VERSION });
const accepted = await Stream.accept(pair.server, VERSION);
if (!accepted) throw new Error("the subscription stream was never accepted");
void pub.runSubscribeNamespace(
new SubscribeNamespace({ requestId: 0n, namespace: Path.from("room/video") }),
accepted,
);

expect(await subscription.reader.u53()).toBe(RequestOk.id);
await RequestOk.decode(subscription.reader, VERSION);

// The first entry is the video route as the empty suffix, not a later broadcast beneath it.
publish(origin, Path.from("room/video/cam"));
expect(await subscription.reader.u53()).toBe(SubscribeNamespaceEntry.id);
expect((await SubscribeNamespaceEntry.decode(subscription.reader, VERSION)).suffix).toBe(Path.empty());

video.close();
chat.close();
subscription.close();
origin.close();
});

/**
* A served root collapses every covering route to the empty suffix. A narrow one claiming
* only `tenant/chat` must not hide a broader one from a subscription at `video`.
*/
test("a subscription hears a broader covering route when the narrowest cannot serve it", async () => {
const pair = createMockTransportPair(ALPN.DRAFT_19);
const origin = new OriginProducer();
const tenant = origin.scope(Path.from("tenant"), new Path.Patterns([Path.Pattern.all()]));
const pub = new Publisher({
quic: pair.server,
session: new NativeSession(pair.server, VERSION, true),
publish: tenant.consume(),
requiresSolicitation: true,
});
const chat = origin
.scope(Path.empty(), new Path.Patterns([Path.Pattern.subtree(Path.from("tenant/chat"))]))
.dynamic(Path.from("tenant"));
const broad = origin.dynamic(Path.empty(), { cost: 5n });

const subscription = await Stream.open(pair.client, { version: VERSION });
const accepted = await Stream.accept(pair.server, VERSION);
if (!accepted) throw new Error("the subscription stream was never accepted");
void pub.runSubscribeNamespace(new SubscribeNamespace({ requestId: 0n, namespace: Path.from("video") }), accepted);

expect(await subscription.reader.u53()).toBe(RequestOk.id);
await RequestOk.decode(subscription.reader, VERSION);

// The first entry is the broad route as the empty suffix, not a later broadcast beneath it.
publish(origin, Path.from("tenant/video/cam"));
expect(await subscription.reader.u53()).toBe(SubscribeNamespaceEntry.id);
expect((await SubscribeNamespaceEntry.decode(subscription.reader, VERSION)).suffix).toBe(Path.empty());

broad.close();
chat.close();
subscription.close();
origin.close();
});

/**
* The claim is presented relative to the served origin's root, like the route's key: a
* publisher serving `tenant` offers `room` (claiming `tenant/room/chat`) to a
Expand Down
14 changes: 7 additions & 7 deletions js/net/src/ietf/publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Milli, Timescale } from "../time.ts";
import type { Subscriber as TrackSubscriber } from "../track.ts";
import { TimeoutError, withTimeout } from "../util/timeout.ts";
import * as Varint from "../varint.ts";
import { type Advertised, wireOf } from "../wire.ts";
import { type Advertised, type Advertisements, wireOf } from "../wire.ts";
import type { Session } from "./adapter.ts";
import * as Cluster from "./cluster.ts";
import { requestReason, toRequestCode } from "./error.ts";
Expand Down Expand Up @@ -161,7 +161,7 @@ export class Publisher {
// session leaves its broadcasts alone. The namespaces are advertised with an unsolicited
// PUBLISH_NAMESPACE (see {@link runPublishNamespaces}), or on request if the peer asked
// for that (see {@link runSubscribeNamespace}).
#advertised: Getter<ReadonlyMap<Path.Valid, Advertised> | undefined>;
#advertised: Getter<Advertisements | undefined>;
#publish?: OriginConsumer;

// What every advertisement carries on a session that negotiated the MoQ Cluster
Expand Down Expand Up @@ -779,7 +779,7 @@ export class Publisher {
// waits for its reply only notifies listeners already registered.
// TODO Make a better helper within Signals.
let dispose!: Dispose;
const changed = new Promise<ReadonlyMap<Path.Valid, Advertised> | undefined>((resolve) => {
const changed = new Promise<Advertisements | undefined>((resolve) => {
dispose = this.#advertised.changed(resolve);
});

Expand Down Expand Up @@ -904,7 +904,7 @@ export class Publisher {
// through it and leave the namespace unadvertised until something unrelated
// changed.
// TODO Make a better helper within Signals.
const changed = new Promise<ReadonlyMap<Path.Valid, Advertised> | undefined>((resolve) => {
const changed = new Promise<Advertisements | undefined>((resolve) => {
dispose = this.#advertised.changed(resolve);
});

Expand All @@ -915,10 +915,10 @@ export class Publisher {
}

const updated = new Map<Path.Valid, Advertised>();
for (const [covered, snap] of advertised) {
for (const [covered, candidates] of advertised) {
// Unasked, a hidden namespace stays off the wire (MoQ Hidden).
if (hiddenBelow(Path.empty(), covered)) continue;
updated.set(covered, snap);
if (hiddenBelow(Path.empty(), covered) || candidates.length === 0) continue;
updated.set(covered, candidates[0]);
}

// A namespace that is gone, or that a republish replaced, takes its refusal with
Expand Down
13 changes: 7 additions & 6 deletions js/net/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type { Route } from "./hop.ts";
import * as Path from "./path.ts";
import type { Timestamp } from "./time.ts";
import type { Groups, Producer, Request, Subscriber } from "./track.ts";
import type { Advertised } from "./wire.ts";
import type { Advertised, Advertisements } from "./wire.ts";

/** Normalize public group bounds into an inclusive start and exclusive end. */
export function groupBounds(groups: Groups = {}): { start: number; end?: number } {
Expand Down Expand Up @@ -54,24 +54,25 @@ export function hiddenBelow(prefix: Path.Valid, path: Path.Valid): boolean {
*/
export function presented(
prefix: Path.Valid,
table: ReadonlyMap<Path.Valid, Advertised>,
table: Advertisements,
carries: (covered: Path.Valid) => boolean,
): Map<Path.Valid, Advertised> {
const out = new Map<Path.Valid, Advertised>();
let rootLen = -1;
const requested = Path.Pattern.subtree(prefix);
for (const [covered, snap] of table) {
for (const [covered, candidates] of table) {
if (!carries(covered)) continue;
if (Path.hasPrefix(covered, prefix)) {
// A scoped route covers only what it claims, so it cannot serve a prefix outside that.
if (snap.claim && !snap.claim.overlaps(requested)) continue;
if (covered.length < rootLen) continue;
// A scoped route covers only what it claims, so the best one that can serve the prefix wins.
const snap = candidates.find((candidate) => !candidate.claim || candidate.claim.overlaps(requested));
if (!snap) continue;
rootLen = covered.length;
out.set(Path.empty(), snap);
continue;
}
const suffix = Path.stripPrefix(prefix, covered);
if (suffix !== null) out.set(suffix, snap);
if (suffix !== null && candidates.length > 0) out.set(suffix, candidates[0]);
}
return out;
}
Expand Down
8 changes: 4 additions & 4 deletions js/net/src/lite/publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type * as Path from "../path.ts";
import { type Reader, type Stream, Writer } from "../stream.ts";
import { Milli, Timescale } from "../time.ts";
import type * as track from "../track.ts";
import { type Advertised, wireOf } from "../wire.ts";
import { type Advertised, type Advertisements, wireOf } from "../wire.ts";
import { AnnounceInit, AnnounceOk, type AnnounceRequest, encodeAnnounceBroadcast } from "./announce.ts";
import { Datagram as DatagramMessage } from "./datagram.ts";
import * as DatagramStream from "./datagram_stream.ts";
Expand Down Expand Up @@ -340,7 +340,7 @@ export class Publisher {
#datagramWriter?: WritableStreamDefaultWriter<Uint8Array>;

// Originated advertisements this session forwards.
#advertised: Getter<ReadonlyMap<Path.Valid, Advertised> | undefined>;
#advertised: Getter<Advertisements | undefined>;

#publish?: OriginConsumer;

Expand Down Expand Up @@ -450,7 +450,7 @@ export class Publisher {
// unrelated moved.
// TODO Make a better helper within Signals.
let dispose!: Dispose;
let changed = new Promise<ReadonlyMap<Path.Valid, Advertised> | undefined>((resolve) => {
let changed = new Promise<Advertisements | undefined>((resolve) => {
dispose = this.#advertised.changed(resolve);
});

Expand Down Expand Up @@ -498,7 +498,7 @@ export class Publisher {
if (!advertised) break;

// Re-arm before reading, so an advertise that lands while we write is not lost.
changed = new Promise<ReadonlyMap<Path.Valid, Advertised> | undefined>((resolve) => {
changed = new Promise<Advertisements | undefined>((resolve) => {
dispose = this.#advertised.changed(resolve);
});

Expand Down
48 changes: 44 additions & 4 deletions js/net/src/origin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1181,7 +1181,7 @@ test("a peer is offered and served the cheapest originated route", async () => {
const cheap = origin.dynamic(prefix, { cost: 1n });
const pricey = origin.dynamic(prefix, { cost: 10n });

expect(wireOf(consumer).advertised.peek()?.get(prefix)?.route).toEqual(Route.normalize({ cost: 1n }));
expect(wireOf(consumer).advertised.peek()?.get(prefix)?.[0]?.route).toEqual(Route.normalize({ cost: 1n }));
const pending = wireOf(consumer).demand(Path.from("live/cam"));
const { value: req } = await cheap.requested().next();
const upstream = new BroadcastProducer();
Expand All @@ -1191,9 +1191,9 @@ test("a peer is offered and served the cheapest originated route", async () => {
// An exact-path local broadcast competes with them on cost too.
const local = origin.createBroadcast(prefix);
local.announce({ cost: 5n });
expect(wireOf(consumer).advertised.peek()?.get(prefix)?.route).toEqual(Route.normalize({ cost: 1n }));
expect(wireOf(consumer).advertised.peek()?.get(prefix)?.[0]?.route).toEqual(Route.normalize({ cost: 1n }));
local.announce({ cost: 0n });
expect(wireOf(consumer).advertised.peek()?.get(prefix)?.route).toEqual(Route.normalize({ cost: 0n }));
expect(wireOf(consumer).advertised.peek()?.get(prefix)?.[0]?.route).toEqual(Route.normalize({ cost: 0n }));

local.close();
upstream.close();
Expand Down Expand Up @@ -1513,8 +1513,48 @@ test("a rooted reader presents the most specific covering route", () => {
const broad = origin.dynamic(Path.from("room"), { cost: 1n });
const rooted = origin.scope(Path.from("room/alice"), new Path.Patterns([Path.Pattern.all()]));
expect(rooted.broadcasts().peek().get(Path.empty())?.cost.warm).toBe(9n);
expect(wireOf(rooted.consume()).advertised.peek()?.get(Path.empty())?.route.cost.warm).toBe(9n);
expect(wireOf(rooted.consume()).advertised.peek()?.get(Path.empty())?.[0]?.route.cost.warm).toBe(9n);
broad.close();
narrow.close();
origin.close();
});

test("a scoped reader picks the best route its scope can see at a prefix", async () => {
const origin = new Producer();
const scoped = (pattern: string) => origin.scope(Path.empty(), new Path.Patterns([Path.Pattern.parse(pattern)]));
const chat = scoped("*/chat").dynamic(Path.empty(), { cost: 1n });
const video = scoped("*/video").dynamic(Path.empty(), { cost: 5n });
const reader = scoped("room/video");

expect(reader.broadcasts().peek().get(Path.empty())?.cost.warm).toBe(5n);
expect(origin.broadcasts(Path.Pattern.parse("room/video")).peek().get(Path.empty())?.cost.warm).toBe(5n);
const announced = reader.announced();
expect((await announced.next())?.route.cost.warm).toBe(5n);
announced.close();

// A session publishing the scoped view offers the video route too.
const offered = [...(wireOf(reader.consume()).advertised.peek()?.get(Path.empty()) ?? [])];
expect(offered.map((advert) => advert.route.cost.warm)).toEqual([5n]);

video.close();
chat.close();
origin.close();
});

test("a scoped reader sees a local broadcast that only loses to a route outside its scope", () => {
const origin = new Producer();
const chat = origin
.scope(Path.empty(), new Path.Patterns([Path.Pattern.parse("room/*/chat")]))
.dynamic(Path.from("room/alice"), { cost: 0n });
const local = origin.createBroadcast(Path.from("room/alice"));
local.announce({ cost: 5n });
const reader = origin.scope(Path.empty(), new Path.Patterns([Path.Pattern.parse("room/*")]));

expect(reader.broadcasts().peek().get(Path.from("room/alice"))?.cost.warm).toBe(5n);
// Unscoped, the cheaper route still wins the prefix.
expect(origin.broadcasts().peek().get(Path.from("room/alice"))?.cost.warm).toBe(0n);

local.close();
chat.close();
origin.close();
});
Loading
Loading