diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index 546458adae5..4eb5bbfd8e5 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -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() }} diff --git a/doc/concept/moq-lite.md b/doc/concept/moq-lite.md index f1fc3ff0218..c2947245ed4 100644 --- a/doc/concept/moq-lite.md +++ b/doc/concept/moq-lite.md @@ -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>, route, kind }` and TypeScript `Announce.Update { prefix, captures, route, kind }`, where the kind is diff --git a/js/net/bench/forward.ts b/js/net/bench/forward.ts new file mode 100644 index 00000000000..c87f7bb6c96 --- /dev/null +++ b/js/net/bench/forward.ts @@ -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((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; + 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"); diff --git a/js/net/src/ietf/publisher.test.ts b/js/net/src/ietf/publisher.test.ts index 72b489e5370..76c0c59d360 100644 --- a/js/net/src/ietf/publisher.test.ts +++ b/js/net/src/ietf/publisher.test.ts @@ -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 diff --git a/js/net/src/ietf/publisher.ts b/js/net/src/ietf/publisher.ts index de3479b97b2..ee12f521184 100644 --- a/js/net/src/ietf/publisher.ts +++ b/js/net/src/ietf/publisher.ts @@ -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"; @@ -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 | undefined>; + #advertised: Getter; #publish?: OriginConsumer; // What every advertisement carries on a session that negotiated the MoQ Cluster @@ -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 | undefined>((resolve) => { + const changed = new Promise((resolve) => { dispose = this.#advertised.changed(resolve); }); @@ -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 | undefined>((resolve) => { + const changed = new Promise((resolve) => { dispose = this.#advertised.changed(resolve); }); @@ -915,10 +915,10 @@ export class Publisher { } const updated = new Map(); - 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 diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index fdda3370578..0f00c90848e 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -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 } { @@ -54,24 +54,25 @@ export function hiddenBelow(prefix: Path.Valid, path: Path.Valid): boolean { */ export function presented( prefix: Path.Valid, - table: ReadonlyMap, + table: Advertisements, carries: (covered: Path.Valid) => boolean, ): Map { const out = new Map(); 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; } diff --git a/js/net/src/lite/publisher.ts b/js/net/src/lite/publisher.ts index 2327855f707..ed2a854dd84 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -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"; @@ -340,7 +340,7 @@ export class Publisher { #datagramWriter?: WritableStreamDefaultWriter; // Originated advertisements this session forwards. - #advertised: Getter | undefined>; + #advertised: Getter; #publish?: OriginConsumer; @@ -450,7 +450,7 @@ export class Publisher { // unrelated moved. // TODO Make a better helper within Signals. let dispose!: Dispose; - let changed = new Promise | undefined>((resolve) => { + let changed = new Promise((resolve) => { dispose = this.#advertised.changed(resolve); }); @@ -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 | undefined>((resolve) => { + changed = new Promise((resolve) => { dispose = this.#advertised.changed(resolve); }); diff --git a/js/net/src/origin.test.ts b/js/net/src/origin.test.ts index 7ad40d0fa9c..5ed445a3144 100644 --- a/js/net/src/origin.test.ts +++ b/js/net/src/origin.test.ts @@ -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(); @@ -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(); @@ -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(); +}); diff --git a/js/net/src/origin.ts b/js/net/src/origin.ts index e69cd1d907d..f5b94382389 100644 --- a/js/net/src/origin.ts +++ b/js/net/src/origin.ts @@ -17,7 +17,7 @@ import { StreamCode, StreamError } from "./error.ts"; import { isAnonymous, Route, routesEqual } from "./hop.ts"; import { hiddenBelow, hooks, scopeCaptures, scopeHead, scopeOverlaps } from "./internal.ts"; import * as Path from "./path.ts"; -import { type Advertised, registerWire, wireOf } from "./wire.ts"; +import { type Advertised, type Advertisements, registerWire, wireOf } from "./wire.ts"; export type { Cost, Hop, Route } from "./hop.ts"; export { isAnonymous } from "./hop.ts"; @@ -84,19 +84,39 @@ class Scope { return out; } - /** The advertised prefixes that may serve this scope, relative to its root. */ - projectRoutes( - values: ReadonlyMap | undefined, - ): ReadonlyMap | undefined { + /** + * The advertisements that may serve this scope, relative to its root. Every prefix at or + * above the root presents as the empty path, most specific first, since that is the order + * a request beneath the root resolves in. + */ + projectRoutes(values: Advertisements | undefined): Advertisements | undefined { if (!values || this === Scope.all) return values; - const out = new Map(); - const covering = new CoveringRoot(this.root); - for (const [path, value] of values) { - if (this.allowed && ![...this.allowed].some((pattern) => advertOverlaps(value, path, pattern))) continue; - const relative = covering.relative(path); - if (relative === undefined) continue; - // The claim moves with the key, so it compares against root-relative requests. - out.set(relative, value.claim ? { ...value, claim: value.claim.rebase(this.root) } : value); + const out = new Map(); + const covering: [Path.Valid, Advertised[]][] = []; + const allowed = this.allowed && [...this.allowed]; + for (const [path, candidates] of values) { + const relative = Path.stripPrefix(this.root, path); + const above = relative === null || relative === Path.empty(); + if (above && !Path.hasPrefix(path, this.root)) continue; + const visible = candidates + .filter((value) => !allowed || allowed.some((pattern) => advertOverlaps(value, path, pattern))) + // The claim moves with the key, so it compares against root-relative requests. + .map((value) => (value.claim ? { ...value, claim: value.claim.rebase(this.root) } : value)); + if (visible.length === 0) continue; + if (!above) { + out.set(relative, visible); + continue; + } + // Hold the empty path's place in the order until every covering prefix is known. + if (covering.length === 0) out.set(Path.empty(), []); + covering.push([path, visible]); + } + if (covering.length > 0) { + covering.sort(([a], [b]) => b.length - a.length); + out.set( + Path.empty(), + covering.flatMap(([, visible]) => visible), + ); } return out; } @@ -107,11 +127,6 @@ function advertOverlaps(advert: Advertised, prefix: Path.Valid, pattern: Path.Pa return advert.claim ? advert.claim.overlaps(pattern) : scopeOverlaps(pattern, prefix); } -/** The paths `entry` may serve beneath `prefix`, when its producer is scoped. */ -function claimOf(entry: RouteEntry, prefix: Path.Valid): Path.Patterns | undefined { - return entry.scope.allowed?.intersect(new Path.Patterns([Path.Pattern.subtree(prefix)])); -} - /** * Presents advertised prefixes relative to a root. Every prefix at or above the root * collapses to the empty path, where the most specific one wins, since that is the route @@ -180,11 +195,27 @@ export interface RequestSlot { export interface RouteEntry { readonly identity: object; readonly scope: Scope; + /** The paths the entry may serve beneath its prefix, when its producer is scoped. */ + readonly claim?: Path.Patterns; readonly route: Signal; readonly originated: boolean; readonly server?: ServeState; } +/** One advertisement at a prefix. `exact` marks an announced local broadcast, which is only its own path. */ +interface Candidate extends Advertised { + readonly exact: boolean; +} + +/** Orders advertisements at one prefix: the better route, then a local broadcast on a tie, then fewer hops. */ +function compareCandidates(a: Candidate, b: Candidate): number { + return ( + compareRoutes(a.route, b.route) || + Number(b.exact) - Number(a.exact) || + a.route.hops.length - b.route.hops.length + ); +} + /** Orders two routes by preference: identified before anonymous, then lower warm cost, then lower cold cost. */ function compareRoutes(a: Route, b: Route): number { const anonymous = Number(isAnonymous(a)) - Number(isAnonymous(b)); @@ -343,12 +374,11 @@ class OriginState { routes = new VersionedSignal | undefined>(new Map()); #snapshotVersion = ""; - #snapshot = { - remote: new Map(), - local: new Map(), - routes: new Map(), - visible: new Map(), - }; + #snapshot: { + candidates: ReadonlyMap; + routes: ReadonlyMap; + visible: ReadonlyMap; + } = { candidates: new Map(), routes: new Map(), visible: new Map() }; /** The full route table is built once per mutation, regardless of observer count. */ available = new Derived([this.local, this.advertisedLocal, this.routes], () => this.snapshot().routes); @@ -356,43 +386,57 @@ class OriginState { visible = new Derived([this.local, this.advertisedLocal, this.routes], () => this.snapshot().visible); snapshot(): { - remote: ReadonlyMap; - local: ReadonlyMap; + candidates: ReadonlyMap; routes: ReadonlyMap; visible: ReadonlyMap; } { const version = `${this.local.version}/${this.advertisedLocal.version}/${this.routes.version}`; if (version === this.#snapshotVersion) return this.#snapshot; - const remote = new Map(); - const local = new Map(); + const candidates = this.candidates(); const available = new Map(); - for (const [path, routes] of this.routes.peek() ?? []) { - const entry = preferredEntry(routes); - if (!entry) continue; - const value = { identity: entry.identity, route: entry.route.peek(), claim: claimOf(entry, path) }; - remote.set(path, value); - available.set(path, value.route); - } - for (const [path, front] of this.local.peek() ?? []) { - const routes = this.routes.peek()?.get(path); - if (!this.localWins(path, routes && preferredEntry(routes))) continue; - const value = { identity: front, route: this.advertisedLocal.peek()?.get(path) ?? Route.default }; - local.set(path, value); - available.set(path, value.route); - } const visible = new Map(); - for (const [path, route] of available) { - if (!hiddenBelow(Path.empty(), path)) visible.set(path, route); + for (const [path, [best]] of candidates) { + available.set(path, best.route); + if (!hiddenBelow(Path.empty(), path)) visible.set(path, best.route); } - this.#snapshot = { remote, local, routes: available, visible }; + this.#snapshot = { candidates, routes: available, visible }; this.#snapshotVersion = version; return this.#snapshot; } + /** + * Every advertisement per prefix, most preferred first, without the `skip`ped entries. + * Readers select after filtering by their scope, so a cheaper route they cannot see + * never hides one they can. + */ + candidates(skip?: (entry: RouteEntry) => boolean): Map { + const out = new Map(); + for (const [path, entries] of this.routes.peek() ?? []) { + const list: Candidate[] = []; + for (const entry of entries) { + if (skip?.(entry)) continue; + list.push({ identity: entry.identity, route: entry.route.peek(), claim: entry.claim, exact: false }); + } + if (list.length > 0) out.set(path, list); + } + const advertised = this.advertisedLocal.peek(); + for (const [path, front] of this.local.peek() ?? []) { + const local = { identity: front, route: advertised?.get(path) ?? Route.default, exact: true }; + const list = out.get(path); + if (list) list.push(local); + else out.set(path, [local]); + } + for (const list of out.values()) { + // Stable, so equal routes keep the table's newest-first order. + if (list.length > 1) list.sort(compareCandidates); + } + return out; + } + // Originated advertisements sessions should forward: exact-path announces plus // originated dynamics. Identity is the local front or the route entry, so a // republish diffs as retract-then-announce and a re-price as another active. - originated = new Signal | undefined>(new Map()); + originated = new Signal(new Map()); // Broadcasts materialized from a served route, keyed by exact path. Shared by every // request for the path so repeats reuse one accept; dropped (and closed) when the @@ -471,28 +515,12 @@ class OriginState { /** Rebuild the publisher-facing originated table after an advertisement write. */ rebuildOriginated(): void { - const local = this.local.peek(); - const advertised = this.advertisedLocal.peek(); - const routes = this.routes.peek(); - if (!local && !advertised && !routes) { + if (!this.local.peek() && !this.advertisedLocal.peek() && !this.routes.peek()) { this.originated.set(undefined); return; } - const next = new Map(); - for (const [prefix, entries] of routes ?? []) { - const mine = preferredEntry(entries, received); - if (mine) - next.set(prefix, { identity: mine.identity, route: mine.route.peek(), claim: claimOf(mine, prefix) }); - } // A local broadcast and an originated dynamic at one path compete on cost, as they do for requests. - for (const [path, route] of advertised ?? []) { - const front = local?.get(path); - const entries = routes?.get(path); - if (front && this.localWins(path, entries && preferredEntry(entries, received))) { - next.set(path, { identity: front, route }); - } - } - this.originated.set(next); + this.originated.set(this.candidates(received)); } /** @@ -792,6 +820,7 @@ export class Producer implements Table { const entry: RouteEntry = { identity: {}, scope: this.#scope, + claim: this.#scope.allowed?.intersect(new Path.Patterns([Path.Pattern.subtree(prefix)])), route: new Signal(route), originated, server, @@ -1339,29 +1368,32 @@ export class Consumer { #listed(patterns: Path.Patterns, hidden: boolean): Map { const next = new Map(); const covering = new CoveringRoot(this.#scope.root); - const { remote, local } = this.#state.snapshot(); const scopes = [...patterns] .sort((a, b) => Path.compareSpecificity(b.specificity(), a.specificity())) .map((pattern) => ({ pattern, head: scopeHead(pattern) })); - for (const [table, exact] of [ - [remote, false], - [local, true], - ] as const) { - for (const [path, entry] of table) { - const scope = scopes.find( + for (const [path, candidates] of this.#state.snapshot().candidates) { + // The first candidate this reader can see wins, since the preferred one overall may not be. + let entry: Candidate | undefined; + let scope: (typeof scopes)[number] | undefined; + for (const candidate of candidates) { + scope = scopes.find( ({ pattern, head }) => - (exact ? pattern.matches(path) : advertOverlaps(entry, path, pattern)) && + (candidate.exact ? pattern.matches(path) : advertOverlaps(candidate, path, pattern)) && (hidden || !hiddenBelow(head, path)), ); - if (!scope) continue; - const relative = covering.relative(path); - if (relative === undefined) continue; - next.set(relative, { - identity: entry.identity, - route: entry.route, - captures: scopeCaptures(scope.pattern, path), - }); + if (scope) { + entry = candidate; + break; + } } + if (!entry || !scope) continue; + const relative = covering.relative(path); + if (relative === undefined) continue; + next.set(relative, { + identity: entry.identity, + route: entry.route, + captures: scopeCaptures(scope.pattern, path), + }); } return next; } diff --git a/js/net/src/wire.ts b/js/net/src/wire.ts index 3af39f89ffe..9065dc3561d 100644 --- a/js/net/src/wire.ts +++ b/js/net/src/wire.ts @@ -45,7 +45,7 @@ export interface OriginProducer { export interface OriginConsumer { routes(path: Path.Valid): boolean; readonly broadcasts: Getter | undefined>; - readonly advertised: Getter | undefined>; + readonly advertised: Getter; /** The announced local broadcast at `path`, when it is the route peers are offered there. */ local(path: Path.Valid): broadcast.Consumer | undefined; demand(path: Path.Valid): Promise; @@ -59,6 +59,12 @@ export interface Advertised { readonly claim?: Path.Patterns; } +/** + * Every originated advertisement per prefix, most preferred first. A reader takes the first + * one its scope admits, so a cheaper route it cannot use never hides one it can. + */ +export type Advertisements = ReadonlyMap; + /** The protocol-facing operation behind an established session. */ export interface Established { consume(path: Path.Valid): broadcast.Consumer; diff --git a/quest/m1/README.md b/quest/m1/README.md index 8b0d88c9056..65d24564f5d 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -45,7 +45,6 @@ transport, benchmark tooling); worktrees isolate commits, not semantics. - [Splice edge cases](/quest/m1/splice-edges.md) - an unstamped successor, a pruned segment's boundary group, and a warm head during a takeover are each handled correctly - [Track tail hardening](/quest/m1/track-tail-hardening.md) - Rust and JS wait out a track's tail by the same rules, with the known hang, count, truncation, grace, and memory holes closed - [Session death parity](/quest/m1/session-death.md) - a local close ends tracks cleanly in both languages, and JS group readers see the session's error on session death -- [JS scoped routes](/quest/m1/js-scoped-routes.md) - a scoped JS origin reader announces the preferred route among those in its scope, like Rust, with the fan-out benchmarked - [moqsrc stop](/quest/m1/moqsrc-stop.md) - moqsrc's stop blocks until its session ends, without deadlocking on a blocked pad push - [More tests under load](/quest/m1/test-flakes-2.md) - the second round of load-only failures, fixed at the cause - [Interop contention](/quest/m1/interop-contention.md) - two `just test interop --all` matrices pass side by side, and `just test harness` runs from a clean checkout diff --git a/quest/m1/js-scoped-routes.md b/quest/m1/js-scoped-routes.md deleted file mode 100644 index 27f91eb06cf..00000000000 --- a/quest/m1/js-scoped-routes.md +++ /dev/null @@ -1,37 +0,0 @@ -# [S] JS scoped readers pick from their own routes - -## Goal - -A scoped `@moq/net` origin reader, and a connection publishing that scoped -view, announces a prefix whenever some route there overlaps its scope, and -presents the preferred route among those, as moq-net does. For example, with -a cheap `*/chat` route and a costlier `*/video` route both at `""`, a -`room/video` reader sees the video route instead of nothing. - -The cost of `forwardAnnounced` fanning one advertisement out across interest -streams is measured, and fixed only if the sweep shows it matters. - -## Plan - -Correctness first. `OriginState.snapshot()` in `js/net/src/origin.ts` keeps -one `preferredEntry` per path before `#listed` applies the reader's scope, -and `rebuildOriginated()` makes the same early choice, so a reader whose -scope excludes the globally preferred entry drops the path even though -`bestEntry` would route a request through another one. moq-net filters each -entry by the cursor's scope (`RouteEntry::overlaps` in -`rs/moq-net/src/model/origin.rs`) before choosing. Keep the candidates until -the scope applies. The shared unscoped snapshot is the fast path the -`js/net/bench/broadcasts.ts` sweep guards; keep it, and run that sweep before -and after. Raised by Codex on https://github.com/moq-dev/moq/pull/4234. - -Then the fan-out. `forwardAnnounced` in `js/net/src/connection/forward.ts` -opens one announce stream per interest head and keeps a `Dynamic` plus a -`drive` task per stream, so a peer advertising a prefix above several heads -(`""` for `a/**` and `b/**`) is stored and repriced once per head, and closing -one stream can drop the entry a request was using while an identical one -remains. Extend the nightly JS benchmark with a sweep over heads and routes -(the repo's fan-out rule), then decide: if cost grows with heads times routes -at realistic sizes, share or reference-count received prefixes across -streams; if not, record the numbers in the PR and leave it. - -No public API or wire change is expected. diff --git a/rs/moq-net/src/model/origin.rs b/rs/moq-net/src/model/origin.rs index bfb20e19b58..a93764e92ea 100644 --- a/rs/moq-net/src/model/origin.rs +++ b/rs/moq-net/src/model/origin.rs @@ -5670,6 +5670,25 @@ mod tests { assert_eq!(request.path().as_str(), "room/chat"); } + #[tokio::test] + async fn scoped_cursor_selects_among_the_routes_it_can_see() { + let producer = origin(1).produce(); + let scoped = |pattern: &str| { + producer + .scope("", &Patterns::from(pattern.parse::().unwrap())) + .unwrap() + }; + let _chat = scoped("*/chat").dynamic("", Route::default().with_cost(1)).unwrap(); + let _video = scoped("*/video").dynamic("", Route::default().with_cost(5)).unwrap(); + + let mut video = producer + .consume() + .scope("", &scopes(&["room/video"])) + .unwrap() + .announced(); + assert_eq!(video.assert_next_active("").cost, Cost::new(5)); + } + #[tokio::test] async fn dynamic_accepts_a_max_depth_prefix() { let producer = origin(1).produce();