diff --git a/Cargo.lock b/Cargo.lock index 99c8f23fd37..979b2a5e148 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3177,6 +3177,15 @@ dependencies = [ "foldhash", ] +[[package]] +name = "hashlink" +version = "0.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a596f1b20ed2cc5ecac41a164aaebc7258057060f06c0cf7a2ba3991ee7990fb" +dependencies = [ + "hashbrown 0.17.1", +] + [[package]] name = "heck" version = "0.5.0" @@ -4957,6 +4966,7 @@ dependencies = [ "criterion", "futures", "getrandom 0.4.3", + "hashlink", "kio 0.6.2", "libc", "loom", diff --git a/Cargo.toml b/Cargo.toml index 6a4f1c1cd39..fa5890cb037 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -131,6 +131,7 @@ flate2 = { version = "1.1", default-features = false } futures = "0.3" getrandom = { version = "0.4", features = ["wasm_js"] } hang = { version = "0.21.11", path = "rs/hang" } +hashlink = "0.12.2" hex = "0.4" # HMAC-SHA256 for the mDNS membership proofs (moq-tokio's `mdns` feature). hmac = "0.13" diff --git a/drafts/draft-lcurley-moq-lite.md b/drafts/draft-lcurley-moq-lite.md index 20aca4079cb..c3382646bb5 100644 --- a/drafts/draft-lcurley-moq-lite.md +++ b/drafts/draft-lcurley-moq-lite.md @@ -408,8 +408,15 @@ The publisher replies with a single ANNOUNCE_OK message followed by announcement - ANNOUNCE_END: a previously started route is no longer advertised. - ANNOUNCE_UPDATE: a previously started advertisement was atomically updated (new hops or cost). - ANNOUNCE_RESTART: another publisher instance now serves a previously started advertisement. +- SYNCED: the initial set is complete. -ANNOUNCE_OK carries metadata that applies to every announcement on the stream: the publisher's own `Hop ID` (the implicit trailing entry of every announcement's path) and the number of initial announcements, which lets the subscriber deliver the initial set as a batch (see [ANNOUNCE_OK](#announce-ok)). +ANNOUNCE_OK carries metadata that applies to every announcement on the stream: the publisher's own `Hop ID` (the implicit trailing entry of every announcement's path) (see [ANNOUNCE_OK](#announce-ok)). +The publisher MUST send [SYNCED](#synced) exactly once, after the initial set. +The subscriber MAY batch announcements until SYNCED; subsequent announcements are live updates. +The initial set is relative to this hop: a relay answers with what it holds without waiting for upstreams. +Each reader's pending queue MUST collapse updates by route in insertion order, rewriting existing entries in place and omitting an unseen route that ends. +The publisher MUST send SYNCED at the first drain of that queue or after sending N events, whichever comes first, where N is the queue size when the reader subscribed. +A retraction of a route already sent MAY precede SYNCED. Each ANNOUNCE_START implicitly assigns the next Announce ID on the stream: a counter starting at 0 that increments by 1 per advertisement. The id never appears on the wire; both endpoints derive it from the message order on the (reliable, ordered) stream. @@ -949,7 +956,6 @@ It carries metadata that is constant for the lifetime of the stream and applies ANNOUNCE_OK Message { Message Length (i) Hop ID (i) - Active Count (i) } ~~~ @@ -960,11 +966,18 @@ The value 0 is reserved to mean "unknown": either no Hop ID was assigned (e.g. w A publisher that assigns a Hop ID MUST choose a non-zero value, and SHOULD assign itself one (a fresh random value per session suffices), so downstream receivers can detect loops through it. Receivers reconstruct the full path as `Hop IDs ++ [ANNOUNCE_OK.Hop ID]`. -**Active Count**: -The number of ANNOUNCE_START messages that the publisher will send immediately as the initial set. -The subscriber MAY block reporting any announcement to the application until all `Active Count` initial announcements have arrived, then deliver the initial set as a batch. -Any announcements beyond `Active Count` are live updates and SHOULD be reported as they arrive. -A value of `0` is valid and means the publisher is offering no initial available broadcasts; all subsequent announcements (if any) are live updates. +## SYNCED {#synced} +The initial set on a discovery stream has been delivered. + +~~~ +SYNCED Message { + Type (i) = 0x4 + Message Length (i) = 0 +} +~~~ + +SYNCED has no body and assigns no Announce ID. +A second SYNCED on the same stream, or a stream ending before SYNCED, is a PROTOCOL_VIOLATION. ## ANNOUNCE_START {#announce-start} @@ -1609,6 +1622,8 @@ The `Message Length` describes the payload size on the wire. ## moq-lite-07 +- Replaced ANNOUNCE_OK Active Count with an ordered, empty SYNCED event (0x4), marking the initial set without retaining routes that end before delivery. + - A publisher must not start a timed Group's frame 0 earlier than a lower-sequence Group's. Receivers do not check it, since a received timestamp may be a send time. - Added the reliable ordered RTCDataChannel binding, with one Qmux Record per binary message and no datagrams. diff --git a/js/net/bench/broadcasts.ts b/js/net/bench/broadcasts.ts index b82ded1a111..39d1b154c0c 100644 --- a/js/net/bench/broadcasts.ts +++ b/js/net/bench/broadcasts.ts @@ -6,12 +6,15 @@ import { Producer as BroadcastProducer } from "../src/broadcast.ts"; import { BroadcastCache } from "../src/consume.ts"; import * as Epoch from "../src/epoch.ts"; import { HopSchema, Route } from "../src/hop.ts"; +import { AnnounceRequest } from "../src/lite/announce.ts"; +import { Publisher } from "../src/lite/publisher.ts"; import { Subscriber } from "../src/lite/subscriber.ts"; import { TrackInfo } from "../src/lite/track.ts"; -import { Version } from "../src/lite/version.ts"; +import { ALPN_07_WIP, Version } from "../src/lite/version.ts"; +import { createMockTransportPair } from "../src/mock.ts"; import { Producer } from "../src/origin.ts"; import * as Path from "../src/path.ts"; -import { Writer } from "../src/stream.ts"; +import { Stream, Writer } from "../src/stream.ts"; const routeCounts = [8, 32, 128]; const observerCounts = [1, 8, 32]; @@ -369,3 +372,59 @@ for (const routeCount of routeCounts) { console.log(`retirement,${routeCount},${consumerCount},${((elapsed * 1000) / 256).toFixed(2)},${refused}`); } } + +// Exercise each sender's keyed queue while its first wire write is stalled. +console.log("scope,routes,observers,update_us"); +for (const routeCount of routeCounts) { + for (const observerCount of observerCounts) { + const origin = new Producer(); + const routes = Array.from({ length: routeCount }, (_, index) => { + const broadcast = origin.createBroadcast(Path.from(`z/${index}`)); + broadcast.announce(); + return broadcast; + }); + const senders = Array.from({ length: observerCount }, () => { + const pair = createMockTransportPair(ALPN_07_WIP); + const publisher = new Publisher(pair.server, Version.DRAFT_07, HopSchema.parse(1n), origin.consume()); + const entered = Promise.withResolvers(); + const release = Promise.withResolvers(); + const stream = new Stream({ + version: Version.DRAFT_07, + readable: new ReadableStream(), + writable: new WritableStream({ + async write() { + entered.resolve(); + await release.promise; + }, + }), + }); + const running = publisher.runAnnounce(new AnnounceRequest(Path.empty()), stream); + return { pair, publisher, entered, release, running }; + }); + await Promise.all(senders.map((sender) => sender.entered.promise)); + const start = performance.now(); + for (let index = 0; index < updates; index++) { + const broadcast = origin.createBroadcast(Path.from(`a/${index}`)); + broadcast.announce(); + await new Promise((resolve) => setImmediate(resolve)); + broadcast.close(); + await new Promise((resolve) => setImmediate(resolve)); + } + console.log( + `stalled-announce,${routeCount},${observerCount},${(((performance.now() - start) * 1000) / updates).toFixed(1)}`, + ); + await Promise.all( + senders.map(async ({ pair, publisher, release, running }) => { + const drained = publisher.drain(); + release.resolve(); + await drained; + await running; + publisher.close(); + pair.client.close(); + pair.server.close(); + }), + ); + for (const route of routes) route.close(); + origin.close(); + } +} diff --git a/js/net/src/lite/announce.test.ts b/js/net/src/lite/announce.test.ts index d03c8a24838..b7d392dc790 100644 --- a/js/net/src/lite/announce.test.ts +++ b/js/net/src/lite/announce.test.ts @@ -469,3 +469,18 @@ test("lite06 ignores a legacy cold price and writes its saturation ceiling", asy expect(await r.u62()).toBe(12n); expect(await r.u62()).toBe(2n ** 62n - 1n); }); + +test("SYNCED is empty and belongs only to lite-07", async () => { + expect( + await bytes((w) => encodeAnnounceBroadcast(w, { status: "synced" }, Version.DRAFT_07), Version.DRAFT_07), + ).toEqual(new Uint8Array([4, 0])); + expect(await roundTrip({ status: "synced" }, Version.DRAFT_07)).toEqual({ status: "synced" }); + await expect(roundTrip({ status: "synced" }, Version.DRAFT_06)).rejects.toThrow(); +}); + +test("ANNOUNCE_OK omits Active Count only on lite-07", async () => { + for (const version of [Version.DRAFT_06, Version.DRAFT_07]) { + const wire = await bytes((w) => new AnnounceOk(HopSchema.parse(1n), 7).encode(w, version), version); + expect(wire).toEqual(new Uint8Array(version === Version.DRAFT_06 ? [2, 1, 7] : [1, 1])); + } +}); diff --git a/js/net/src/lite/announce.ts b/js/net/src/lite/announce.ts index 30d56eead15..cb911f035a4 100644 --- a/js/net/src/lite/announce.ts +++ b/js/net/src/lite/announce.ts @@ -13,6 +13,7 @@ import { hasExcludeHop, hasHidden, hasRouteCost, + hasSynced, Version, } from "./version.ts"; @@ -30,6 +31,7 @@ const ANNOUNCE_END = 1; const ANNOUNCE_UPDATE = 2; // lite-07: another publisher instance replaces the advertisement. const ANNOUNCE_RESTART = 3; +const SYNCED = 4; /** * Lite-07: copy `keep` path segments (from the head) or hops (from the tail) of the @@ -76,7 +78,9 @@ export type AnnounceBroadcast = * instance. The id stays live. */ | { status: "restart"; id: bigint; epoch?: Epoch.Valid; hops: Hop[]; cost?: Cost; hopBase?: Base } /** An unknown lite-06+ announce type, skipped by length. Does not assign an id. */ - | { status: "skipped" }; + | { status: "skipped" } + /** Lite07+: the initial set has been delivered. */ + | { status: "synced" }; // Both wire rules on a hop chain, applied to what we send and to what we receive: a // chain that revisits a hop looped, so neither forwarding it nor subscribing through it @@ -234,6 +238,8 @@ async function encodeAnnounce06Body(w: Writer, msg: AnnounceBroadcast, version: await encodeHopsBlock(w, version, msg.hops, msg.hopBase); await encodeRouteCost(w, version, msg.cost); break; + case "synced": + break; case "ended": // The pre-lite-06 path-form retraction has no place on lite-06. throw new Error("ended-by-path not supported for this version"); @@ -255,6 +261,9 @@ function announce06Type(msg: AnnounceBroadcast, version: Version): number { // Older versions send an end and a start instead. if (!hasAnnounceRestart(version)) throw new Error("announce restart not supported for this version"); return ANNOUNCE_RESTART; + case "synced": + if (!hasSynced(version)) throw new Error("synced not supported for this version"); + return SYNCED; case "ended": throw new Error("ended-by-path not supported for this version"); case "skipped": @@ -270,6 +279,9 @@ async function decodeAnnounce06Body(r: Reader, typ: number, version: Version): P const hops = await decodeHopsBlock(r, version); return { status: "active", ...path, epoch, ...hops, cost: await decodeRouteCost(r, version) }; } + case SYNCED: + if (!hasSynced(version)) break; + return { status: "synced" }; case ANNOUNCE_END: return { status: "endedId", id: await r.u62() }; case ANNOUNCE_UPDATE: { @@ -306,6 +318,7 @@ async function encodeLegacyBody(w: Writer, msg: AnnounceBroadcast, version: Vers await w.string(Path.encode(msg.suffix)); await encodeHops(w, version, []); break; + case "synced": case "endedId": case "update": case "restart": @@ -526,8 +539,8 @@ export class AnnounceInit { /// /// `origin` is the responder's Hop ID, which the subscriber stamps onto each /// announce's hop chain (the publisher no longer stamps itself), or the reserved -/// {@link UNKNOWN_HOP} when the responder has no identity to give. `active` is -/// the number of initial Announce messages that follow immediately. +/// {@link UNKNOWN_HOP} when the responder has no identity to give. Lite05/06 +/// carry `active`, the initial start count; lite07 uses SYNCED instead. export class AnnounceOk { hop: Hop; active: number; @@ -543,27 +556,27 @@ export class AnnounceOk { } } - async #encode(w: Writer) { + async #encode(w: Writer, version: Version) { await w.u62(this.hop); - await w.u53(this.active); + if (!hasSynced(version)) await w.u53(this.active); } - static async #decode(r: Reader): Promise { + static async #decode(r: Reader, version: Version): Promise { // The draft reserves 0 for "unknown": the responder was never assigned an id, or // withholds it to obscure its routing. It names nobody, so callers must not stamp // it onto a hop chain, but it is a legal message and not grounds to drop the stream. const origin = HopSchema.parse(await r.u62()); - const active = await r.u53(); + const active = hasSynced(version) ? 0 : await r.u53(); return new AnnounceOk(origin, active); } async encode(w: Writer, version: Version): Promise { AnnounceOk.#guard(version); - return Message.encode(w, this.#encode.bind(this)); + return Message.encode(w, (w) => this.#encode(w, version)); } static async decode(r: Reader, version: Version): Promise { AnnounceOk.#guard(version); - return Message.decode(r, AnnounceOk.#decode); + return Message.decode(r, (r) => AnnounceOk.#decode(r, version)); } } diff --git a/js/net/src/lite/publisher.test.ts b/js/net/src/lite/publisher.test.ts index 0e1ac7edad0..c4f7d66c1c2 100644 --- a/js/net/src/lite/publisher.test.ts +++ b/js/net/src/lite/publisher.test.ts @@ -13,7 +13,7 @@ import * as Path from "../path.ts"; import { sendOrder } from "../priority.ts"; import { Reader, Stream, Writer } from "../stream.ts"; import { Timescale, Timestamp } from "../time.ts"; -import { type AnnounceBroadcast, AnnounceRequest, decodeAnnounceBroadcast } from "./announce.ts"; +import { type AnnounceBroadcast, AnnounceOk, AnnounceRequest, decodeAnnounceBroadcast } from "./announce.ts"; import { Fetch } from "./fetch.ts"; import { Group as GroupMessage } from "./group.ts"; import { Probe as ProbeMessage } from "./probe.ts"; @@ -2553,3 +2553,70 @@ for (const version of [Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07]) { } }); } + +test("lite-07 SYNCED survives an initial removal and continuous earlier-prefix churn", async () => { + const version = Version.DRAFT_07; + const pair = createMockTransportPair(ALPN_07_WIP); + const origin = new OriginProducer(); + const publisher = new Publisher(pair.server, version, randomHop(), origin.consume()); + const removed = publish(origin, Path.from("z/removed")); + const retained = publish(origin, Path.from("z/retained")); + const entered = Promise.withResolvers(); + const release = Promise.withResolvers(); + let output!: ReadableStreamDefaultController; + const reader = new Reader( + new ReadableStream({ + start(controller) { + output = controller; + }, + }), + undefined, + version, + ); + let writes = 0; + let churnEnabled = true; + const churn: BroadcastProducer[] = []; + const stream = new Stream({ + version, + readable: new ReadableStream(), + writable: new WritableStream({ + async write(chunk) { + if (writes++ === 0) { + entered.resolve(); + await release.promise; + } + output.enqueue(new Uint8Array(chunk)); + if (churnEnabled) { + churn.push(publish(origin, Path.from(`a/${writes}`))); + if (churn.length > 2) churn.shift()?.close(); + } + }, + }), + }); + const running = publisher.runAnnounce(new AnnounceRequest(Path.empty()), stream); + try { + await entered.promise; + removed.close(); + release.resolve(); + expect((await AnnounceOk.decode(reader, version)).active).toBe(0); + const events: AnnounceBroadcast[] = []; + for (let i = 0; i < 3; i++) { + const event = await decodeAnnounceBroadcast(reader, version); + events.push(event); + if (event.status === "synced") break; + } + expect(events[0]).toMatchObject({ status: "active", suffix: "z/retained" }); + expect(events.at(-1)).toEqual({ status: "synced" }); + expect(events).not.toContainEqual(expect.objectContaining({ suffix: "z/removed" })); + } finally { + churnEnabled = false; + await publisher.drain(); + await running; + publisher.close(); + retained.close(); + for (const broadcast of churn) broadcast.close(); + origin.close(); + pair.client.close(); + pair.server.close(); + } +}); diff --git a/js/net/src/lite/publisher.ts b/js/net/src/lite/publisher.ts index 684d5246f01..3c5f9a9f990 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -46,6 +46,7 @@ import { hasLargest, hasProbeRtt, hasRouteCost, + hasSynced, hasTrackStream, resolvesStart, Version, @@ -595,6 +596,94 @@ export class Publisher { return; } + if (hasSynced(this.version)) { + dispose(); + const pending = new Map(); + const closed = new Once(); + void stream.reader.closed.then( + () => closed.set(null), + (err: unknown) => closed.set(error(err)), + ); + let wake: (() => void) | undefined; + let ended = false; + const queue = () => { + const table = this.#advertised.peek(); + ended = table === undefined; + const latest = table ? visible(table) : new Map(); + for (const suffix of pending.keys()) { + if (!active.has(suffix) && !latest.has(suffix)) pending.delete(suffix); + } + for (const suffix of active.keys()) { + if (!latest.has(suffix)) pending.set(suffix, { end: true }); + } + for (const [suffix, snap] of latest) { + const prev = active.get(suffix); + const queued = pending.get(suffix); + if ( + queued?.end || + !prev || + !sameInstance(prev, snap) || + !routesEqual(onWire(prev.route), onWire(snap.route)) + ) { + pending.set(suffix, { end: queued?.end ?? false, next: snap }); + } else { + pending.delete(suffix); + } + } + wake?.(); + }; + const stopTable = this.#advertised.subscribe(queue); + const stopGrant = this.#grant?.subscribe(queue); + dispose = () => { + stopTable(); + stopGrant?.(); + }; + queue(); + let remaining: number | undefined = pending.size; + await new AnnounceOk(this.hop, 0).encode(stream.writer, this.version); + for (;;) { + if (remaining !== undefined && (remaining === 0 || pending.size === 0)) { + remaining = undefined; + await encodeAnnounceBroadcast(stream.writer, { status: "synced" }, this.version); + } + const departed = closed.peek(); + if (departed instanceof Error) throw departed; + if (this.#withdrawal.closing.peek() || departed === null || (ended && pending.size === 0)) break; + const first = pending.entries().next(); + if (first.done) { + const changed = new Promise<"changed">((resolve) => { + wake = () => resolve("changed"); + }); + if ((await race([changed, stream.reader.closed, this.#withdrawal.closing])) !== "changed") + break; + continue; + } + const [suffix, entry] = first.value; + if (entry.end) { + if (entry.next) pending.set(suffix, { end: false, next: entry.next }); + else pending.delete(suffix); + active.delete(suffix); + await retract(suffix); + } else if (entry.next) { + pending.delete(suffix); + const prev = active.get(suffix); + active.set(suffix, entry.next); + if (!prev) await announce(suffix, entry.next.route); + else if (!sameInstance(prev, entry.next)) await restart(suffix, entry.next.route); + else await update(suffix, entry.next.route); + } + if (remaining !== undefined) remaining -= 1; + } + if (this.#withdrawal.closing.peek()) { + if (remaining !== undefined) + await encodeAnnounceBroadcast(stream.writer, { status: "synced" }, this.version); + for (const suffix of active.keys()) await retract(suffix); + stream.close(); + await stream.writer.closed; + } + return; + } + const initial = this.#advertised.peek(); if (!initial) return; // closed diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index cd17c52b025..a28793953b2 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -57,6 +57,7 @@ import { hasFetchMaxDelay, hasOrder, hasProbeRtt, + hasSynced, hasTrackStream, resolvesStart, updateSupported, @@ -301,6 +302,7 @@ export class Subscriber { // The ANNOUNCEs still owed of the peer's initial set (lite-05+). Older versions // mark no boundary, so they sync once the request is out, as a best effort. let initial = 0; + let waitingSynced = hasSynced(this.version); if (hasAnnounceOk(this.version)) { const ok = await race([AnnounceOk.decode(stream.reader, this.version), announced.closed]); // A consumer that leaves before the peer answers ends the request now, not on the answer. @@ -376,18 +378,23 @@ export class Subscriber { // Receive announce updates (for Draft03+, this includes initial state) for (;;) { - if (initial === 0) announced.synced(); + if (initial === 0 && !waitingSynced) announced.synced(); const announce = await race([ decodeAnnounceBroadcastMaybe(stream.reader, this.version), announced.closed, ]); // undefined: the stream ended. null: the consumer closed cleanly. - if (announce === undefined && initial > 0) { + if (announce === undefined && (initial > 0 || waitingSynced)) { throw new ProtocolViolation("announce stream ended inside the initial set"); } if (!announce) break; if (announce instanceof Error) throw announce; + if (announce.status === "synced") { + if (!waitingSynced) throw new ProtocolViolation("duplicate SYNCED"); + waitingSynced = false; + continue; + } // The counted initial set is ANNOUNCEs only, so anything else cuts it short. An // unknown message counts for nothing either way. diff --git a/js/net/src/lite/version.ts b/js/net/src/lite/version.ts index eae93fcd7ad..287bcd49327 100644 --- a/js/net/src/lite/version.ts +++ b/js/net/src/lite/version.ts @@ -460,3 +460,18 @@ export function wireTimescale(version: Version, timescale: Timescale | null | un return timescale ?? undefined; } } + +/** Whether discovery streams mark their initial set with SYNCED rather than a count. */ +export function hasSynced(version: Version): boolean { + switch (version) { + case Version.DRAFT_01: + case Version.DRAFT_02: + case Version.DRAFT_03: + case Version.DRAFT_04: + case Version.DRAFT_05: + case Version.DRAFT_06: + return false; + default: + return true; + } +} diff --git a/quest/m1/README.md b/quest/m1/README.md index b644de46934..fa6f2d1b1b1 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -65,7 +65,6 @@ lands before the cut. - [Claim answer epochs](/quest/m1/claim-answer-epochs.md) - a claim answers each output by publishing it at its exact path under its own epoch, so a restarted output never reaches a returning reader as the old one's cache - [Readers see SUBSCRIBE_DROP](/quest/m1/subscribe-drop-errors.md) - a dropped or cut group ends its reader with an error, never a clean end, across cache reclaim, cap changes, and recovery - [Subscribe ranges](/quest/m1/subscribe-ranges/README.md) - a lite-07 SUBSCRIBE asks for past and live ranges with absolute or relative ends, in either order, and replaces FETCH; merged ranges never starve a subscriber; relays fill misses by range, including over moq-transport -- [Announce SYNCED](/quest/m1/announce-synced.md) - lite-07's Announce stream marks its initial set with the same ordered SYNCED as TRACKS, replacing `Active Count` - [More tests under load](/quest/m1/test-flakes-2/README.md) - the second round of load-only failures, one quest per flake, fixed at the cause - [A public model](/quest/m1/public-model/README.md) - every model item a built-in session reaches is public, unnecessary, or marked deliberately private, so a custom publisher, subscriber, or relay builds from the public model alone - [Lazy discovery](/quest/m1/lazy-discovery/README.md) - a `consume` session requests announcements only for prefixes the origin is watching, in JS then Rust; `follow`, `broadcasts`, and the `discovery` option go away diff --git a/quest/m1/announce-synced.md b/quest/m1/announce-synced.md deleted file mode 100644 index 0e3ac04857e..00000000000 --- a/quest/m1/announce-synced.md +++ /dev/null @@ -1,68 +0,0 @@ -# [S] Announce SYNCED - -## Goal - -On lite-07, the Announce stream marks the end of its initial set with the -same ordered SYNCED event [TRACKS](/quest/m1/archive/tracks.md) uses, -replacing ANNOUNCE_OK's `Active Count`. A subscriber knows it holds every -route this hop held that is still live; one that ends before a slow reader -reaches its ANNOUNCE_START is omitted, as in TRACKS. Published versions keep -`Active Count` unchanged. - -## Plan - -Decided by the maintainer on 2026-10-19 with TRACKS ("We don't need the -count at all"), and named SYNCED the same day. A count sent first commits to -that many ANNOUNCE_STARTs, so an initial route that ends before a slow reader -reaches its start leaves the count unmet, unless the hop retains every -initial announcement per reader. SYNCED is sent once, in order, at the -first drain of the reader's queue (its `poll_next` returns Pending) or once -the reader has been sent N events, N being the snapshot size it recorded when -it subscribed, so live churn cannot postpone it (maintainer, 2026-10-19). An initial -route that comes and goes before then is omitted, collapsing in the reader's -pending queue like any other, and a retraction of a route the reader saw may -land before SYNCED. Keep the event identical in shape to TRACKS' SYNCED, so -one encoding and one consumer rule serve both streams. - -- N holds only if the queue delivers in insertion order, so the snapshot - stays ahead of live entries. The origin consumer's queue - (`OriginConsumerState::pending`) is a `BTreeMap` delivered in - lexicographic order, where a live prefix sorting first can precede a - snapshot one indefinitely, and its `owed` set waits on that order. Keep - the keyed collapse but deliver in insertion order. A count of N then - replaces the `owed` set. -- Synced means synced with this hop: a relay answers at once with what it - holds and never waits on its upstream (relays never block on peers). -- The in-process consumers already expose this as synced - (`AnnounceConsumer::poll_synced` in `rs/moq-net/src/model/origin.rs`, - `synced` in `js/net/src/announced.ts`); the wire event feeds the same - signal. -- lite-07's ANNOUNCE_OK loses `Active Count`; no count field remains. Update - `drafts/draft-lcurley-moq-lite.md` (ANNOUNCE_OK, the Announce stream text, - and the lite-07 changelog), then run `just drafts check` and - `just test interop --all`. -- Rust and JS consumers that batch the initial set (such as `moq ls`) wait - for SYNCED on lite-07 and keep reading the count on older versions. -- Coordinate with [Routes and announces](/quest/m1/cluster-routing/routes.md), - which reshapes the lite-07 Announce stream and adds ANNOUNCE_FLOOR to - certify per-route completeness. SYNCED is per stream and orthogonal; - whichever lands second adapts its wire to the other. -- The moq-transport Active Count extension - (`drafts/draft-lcurley-moq-active-count.md`) is out of scope; it is a - separate published draft. - -Test on mock time, directly and through a relay: SYNCED after the initial -routes, sent the first time the reader's queue drains; an initial route -ending before a stalled reader reaches it, with SYNCED still arriving; a -retraction of a seen route landing before SYNCED; a cold relay answering -SYNCED at once; a stalled reader under continuous churn of new prefixes, -including ones sorting before the snapshot, getting SYNCED once it has been -sent N events; and a lite-06 peer still getting `Active Count`. - -Public API: none, if consumers already expose "initial set done"; otherwise -the same signal as TRACKS, mirrored in Rust and `@moq/net`. Wire: lite-07-wip -ANNOUNCE_OK drops `Active Count` and the Announce stream gains SYNCED. - -## Related - -- [TRACKS](/quest/m1/archive/tracks.md) - the same event on the track listing diff --git a/quest/m1/archive/tracks.md b/quest/m1/archive/tracks.md index df0264ee119..56de3a9c8d9 100644 --- a/quest/m1/archive/tracks.md +++ b/quest/m1/archive/tracks.md @@ -102,8 +102,8 @@ when a track no longer exists"): count anywhere, decided by the maintainer on 2026-10-19 ("We don't need the count at all"): a count sent first commits to that many adds, which a track removed before a slow reader reaches its add would break. The event itself - is the whole wire cost. The announce stream gets the same event in lite-07 - ([Announce SYNCED](/quest/m1/announce-synced.md)), identical in shape. + is the whole wire cost. The announce stream already carries the same + event in lite-07, identical in shape. Synced means synced with this hop, decided by the maintainer on 2026-10-19: a relay answers at once with what it currently holds and never waits on its upstream, since relays never block on peers. A cold relay may diff --git a/quest/m1/lite07-finalize.md b/quest/m1/lite07-finalize.md index 9def8517eeb..0b304f49302 100644 --- a/quest/m1/lite07-finalize.md +++ b/quest/m1/lite07-finalize.md @@ -91,7 +91,6 @@ is published; older versions are unchanged. - [Compressed tracks in Rust](/quest/m1/compress/core.md) - TRACK_INFO carries the compress flag and the frame header each compressed frame's inflated size - [Compressed tracks in js/net](/quest/m1/compress/js.md) - JS reads and writes compressed lite-07 tracks instead of refusing them - [TRACKS](/quest/m1/archive/tracks.md) - lite-07 carries the request listing a broadcast's tracks as they appear -- [Announce SYNCED](/quest/m1/announce-synced.md) - lite-07's Announce stream marks its initial set with SYNCED instead of `Active Count` ## Related diff --git a/rs/moq-net/Cargo.toml b/rs/moq-net/Cargo.toml index 7844e1f899f..fd3f53810a2 100644 --- a/rs/moq-net/Cargo.toml +++ b/rs/moq-net/Cargo.toml @@ -29,6 +29,7 @@ fuzz = ["async"] arrayvec = "0.7" bytes = { workspace = true } futures = { workspace = true, optional = true } +hashlink = { workspace = true } kio = { workspace = true } moq-clock = { version = "0.11.0", path = "../moq-clock", default-features = false } moq-pattern = { workspace = true, features = ["serde"] } diff --git a/rs/moq-net/benches/origin.rs b/rs/moq-net/benches/origin.rs index 461dc9185ab..3e379fab963 100644 --- a/rs/moq-net/benches/origin.rs +++ b/rs/moq-net/benches/origin.rs @@ -110,6 +110,35 @@ fn bench_announce(c: &mut Criterion) { group.finish(); } +/// Cancel unseen additions while every cursor stalls on its initial replay. +fn bench_pending_churn(c: &mut Criterion) { + let mut group = c.benchmark_group("origin/pending_churn"); + for routes in [10, 1_000] { + for readers in [1, 100] { + group.bench_function(format!("{routes}routes_{readers}readers"), |b| { + let (producer, _driver) = origin::Producer::new(origin::Config::default()); + let _routes: Vec<_> = (0..routes) + .map(|i| producer.publish(format!("z/{i}"), origin::Route::default()).unwrap()) + .collect(); + let mut cursors: Vec<_> = (0..readers).map(|_| producer.consume().announced()).collect(); + let mut sequence = 0u64; + b.iter(|| { + sequence += 1; + drop( + producer + .publish(format!("a/{sequence}"), origin::Route::default()) + .unwrap(), + ); + }); + for cursor in &mut cursors { + assert_eq!(std::iter::from_fn(|| cursor.try_next()).count(), routes); + } + }); + } + } + group.finish(); +} + /// A peer carrying `publishers` broadcasts withdraws them all before the /// driver's clock moves again, then `subscribers` cursors drain the retractions. /// Each retraction quarantines its prefix until a fresh clock reading, so a cost @@ -1404,6 +1433,7 @@ fn bench_resume_overlap(c: &mut Criterion) { criterion_group!( benches, bench_announce, + bench_pending_churn, bench_withdraw, bench_announce_mounted, bench_announce_fleet, diff --git a/rs/moq-net/src/lite/announce.rs b/rs/moq-net/src/lite/announce.rs index adb489dcd43..ff927379911 100644 --- a/rs/moq-net/src/lite/announce.rs +++ b/rs/moq-net/src/lite/announce.rs @@ -15,6 +15,7 @@ const ANNOUNCE_END: u64 = 1; const ANNOUNCE_UPDATE: u64 = 2; // lite-07: another publisher instance replaces the advertisement. const ANNOUNCE_RESTART: u64 = 3; +const SYNCED: u64 = 4; /// Whether the negotiated version can update a live advertisement's metadata. On lite-05 an /// update travels as a duplicate ANNOUNCE (a second `active` for an already-announced path); on @@ -75,6 +76,8 @@ pub enum AnnounceBroadcast<'a> { /// An unknown lite-06+ announce type. The length-prefixed body was skipped so /// the stream stays up; it does not assign an announce id. Skipped, + /// The initial set has been delivered (lite-07+). + Synced, } /// A path suffix as it travels: the first `keep` segments of a base announcement's @@ -204,6 +207,7 @@ impl AnnounceBroadcast<'_> { Self::Update { id, hops, cost } => AnnounceBroadcast::Update { id, hops, cost }, Self::Restart { id, epoch, hops, cost } => AnnounceBroadcast::Restart { id, epoch, hops, cost }, Self::Skipped => AnnounceBroadcast::Skipped, + Self::Synced => AnnounceBroadcast::Synced, } } } @@ -245,6 +249,8 @@ impl Encode for AnnounceBroadcast<'_> { // subscribe stream). The body varies by type. let typ = match self { Self::Active { .. } => ANNOUNCE_START, + Self::Synced if version.has_synced() => SYNCED, + Self::Synced => return Err(EncodeError::Version), Self::EndedId { .. } => ANNOUNCE_END, Self::Update { .. } => ANNOUNCE_UPDATE, Self::Restart { .. } if version.has_announce_restart() => ANNOUNCE_RESTART, @@ -282,6 +288,7 @@ impl Encode for AnnounceBroadcast<'_> { hops.encode(w, version)?; cost.encode(w, version)?; } + Self::Synced => {} Self::Ended { .. } | Self::Skipped => unreachable!("refused above"), } if w.since(&prefix) > MAX_MESSAGE_SIZE { @@ -311,7 +318,7 @@ impl Encode for AnnounceBroadcast<'_> { encode_hops(w, version, hops)?; } // The id-referencing forms only exist on lite-06+. - Self::EndedId { .. } | Self::Update { .. } | Self::Restart { .. } | Self::Skipped => { + Self::EndedId { .. } | Self::Update { .. } | Self::Restart { .. } | Self::Skipped | Self::Synced => { return Err(EncodeError::Version); } } @@ -337,6 +344,7 @@ impl Decode for AnnounceBroadcast<'_> { hops: HopsRef::decode(&mut body, version)?, cost: Cost::decode(&mut body, version)?, }, + SYNCED if version.has_synced() => Self::Synced, ANNOUNCE_END => Self::EndedId { id: body.varint()? }, ANNOUNCE_UPDATE => Self::Update { id: body.varint()?, @@ -558,9 +566,8 @@ impl Message for AnnounceInit<'_> { /// /// `origin` is the responder's session origin id. In Lite05 the publisher no /// longer stamps it onto each Announce's hop chain; the subscriber appends it on -/// receipt instead. `active` is the number of currently-active broadcasts the -/// publisher sends as the initial set immediately after this message, letting the -/// receiver block until the initial set has arrived. +/// receipt instead. Lite05/06 carry `active`, the number of starts immediately +/// following; lite07 marks the initial set with SYNCED instead. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct AnnounceOk { pub origin: Hop, @@ -574,7 +581,7 @@ impl Message for AnnounceOk { } let origin = Hop::decode(r, version)?; - let active = r.varint()?; + let active = if version.has_synced() { 0 } else { r.varint()? }; Ok(Self { origin, active }) } @@ -584,7 +591,10 @@ impl Message for AnnounceOk { } self.origin.encode(w, version)?; - w.varint(self.active) + if !version.has_synced() { + w.varint(self.active)?; + } + Ok(()) } } @@ -593,6 +603,39 @@ mod tests { use super::*; use bytes::Buf; + #[test] + fn synced_replaces_only_lite07s_count() { + for version in [Version::Lite06, Version::Lite07] { + let mut bytes = Vec::new(); + AnnounceOk { + origin: Hop::new(1).unwrap(), + active: 7, + } + .encode(&mut Encoder::new(&mut bytes, version.into()), version) + .unwrap(); + assert_eq!( + bytes, + if version == Version::Lite06 { + vec![2, 1, 7] + } else { + vec![1, 1] + } + ); + } + let mut bytes = Vec::new(); + AnnounceBroadcast::Synced + .encode(&mut Encoder::new(&mut bytes, Version::Lite07.into()), Version::Lite07) + .unwrap(); + assert_eq!(bytes, vec![4, 0]); + assert!(matches!( + AnnounceBroadcast::Synced.encode( + &mut Encoder::new(&mut Vec::new(), Version::Lite06.into()), + Version::Lite06 + ), + Err(EncodeError::Version) + )); + } + // Forge an ANNOUNCE_BROADCAST with the draft's explicit `restart` status (2) for the given version. fn encode_forged_restart(version: Version) -> bytes::Bytes { // Encode a normal Active, then flip its status byte (1 -> 2). diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 34a497e5927..ce8a6b85f0c 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -978,6 +978,8 @@ struct AnnounceRun { // prefix. live: HashMap, phase: AnnouncePhase, + initial: Option, + replay: bool, // Our grant and the ceiling on the peer: only what both let us publish is // announced, and a shrink withdraws what they no longer cover. auth: Option, @@ -1014,12 +1016,25 @@ impl AnnounceRun { encoder: lite::AnnounceEncoder::new(version), live: HashMap::new(), phase: AnnouncePhase::Init, + initial: None, + replay: false, auth: None, epoch: 0, permit: Default::default(), } } + fn sent(&mut self, stream: &mut Stream) -> Result<(), Error> { + if let Some(remaining) = &mut self.initial { + *remaining = remaining.saturating_sub(1); + if *remaining == 0 { + stream.writer.buffer(&lite::AnnounceBroadcast::Synced)?; + self.initial = None; + } + } + Ok(()) + } + /// Whether we may announce `suffix` (relative to the requested prefix). fn permitted(&self, suffix: &crate::Path) -> bool { self.permit.matches(self.prefix.join(suffix).as_str()) @@ -1035,6 +1050,10 @@ impl AnnounceRun { ) -> Result<(), Error> { let grew = !self.permit.covers(&permit); self.permit = permit; + if self.version.has_synced() { + self.replay = true; + return Ok(()); + } let revoked: Vec<_> = self .live @@ -1115,6 +1134,7 @@ impl AnnounceRun { cost, })?, } + self.sent(stream)?; } // Pre-restart versions have no way to update a live // advertisement; the peer keeps the original chain. @@ -1174,6 +1194,7 @@ impl AnnounceRun { hops: chain, cost, })?; + self.sent(stream)?; Ok(()) } @@ -1204,6 +1225,7 @@ impl AnnounceRun { stream .writer .buffer(&lite::AnnounceBroadcast::Restart { id, epoch, hops, cost })?; + self.sent(stream)?; } _ => { self.retract(stream, suffix.clone(), absolute)?; @@ -1236,6 +1258,7 @@ impl AnnounceRun { hops: Hops::new(), })?, } + self.sent(stream)?; Ok(()) } @@ -1249,6 +1272,14 @@ impl AnnounceRun { if !matches!(self.version, Version::Lite01 | Version::Lite02) && !self.version.has_announce_ok() { return Ok(()); } + if self.version.has_synced() { + stream.writer.buffer(&lite::AnnounceOk { + origin: self.self_origin, + active: 0, + })?; + self.initial = Some(announced.pending_len()); + return Ok(()); + } let initial: Vec<_> = super::replay::initial(std::iter::from_fn(|| announced.try_next())) .into_iter() .filter_map(|update| { @@ -1300,6 +1331,9 @@ impl AnnounceRun { for suffix in self.live.keys().cloned().collect::>() { self.retract(stream, suffix.clone(), &origin.absolute(&suffix))?; } + if self.initial.take().is_some() { + stream.writer.buffer(&lite::AnnounceBroadcast::Synced)?; + } self.phase = AnnouncePhase::Withdrawing; Ok(()) } @@ -1341,6 +1375,17 @@ impl AnnounceRun { return stream.writer.poll_closed(&mut cx); } + if self.replay { + let revoked = self + .live + .keys() + .filter(|suffix| !self.permitted(suffix)) + .cloned() + .collect(); + announced.replay(self.live.keys(), &revoked); + self.replay = false; + } + // A grant change applies before the next update, so a route it no longer // covers is withdrawn rather than re-sent. if let Some(auth) = &self.auth @@ -1354,7 +1399,16 @@ impl AnnounceRun { if let Poll::Ready(res) = stream.reader.poll_closed(&mut cx) { return Poll::Ready(res); } + if self.initial == Some(0) { + stream.writer.buffer(&lite::AnnounceBroadcast::Synced)?; + self.initial = None; + continue; + } let Poll::Ready(next) = announced.poll_next(waiter) else { + if self.initial.take().is_some() { + stream.writer.buffer(&lite::AnnounceBroadcast::Synced)?; + continue; + } return Poll::Pending; }; @@ -1363,6 +1417,10 @@ impl AnnounceRun { Some(announce::Event::Restart(update)) => (update, true, true), Some(announce::Event::End(update)) => (update, false, false), None => { + if self.initial.take().is_some() { + stream.writer.buffer(&lite::AnnounceBroadcast::Synced)?; + continue; + } // The buffer is empty (flushed at the loop top), so FIN now and // wait for the acknowledgement. stream.writer.finish()?; @@ -2505,6 +2563,201 @@ mod announce_test { ); } + #[test] + fn synced_survives_a_removed_initial_route_and_live_churn() { + let origin = Hop::new(1).unwrap().produce(); + let removed = origin.announce("z/removed", origin::Route::default()).unwrap(); + let _initial = origin.announce("z/retained", origin::Route::default()).unwrap(); + let consumer = origin.consume(); + let mut announced = consumer.announced(); + let budget = kio::Producer::new(0usize); + let log = Log::default(); + let mut wire = Wire { + writes: log.writes.clone(), + cursor: 0, + version: Version::Lite07, + }; + let mut stream = Stream:: { + writer: Writer::new(SinkSend::stepped(log, budget.consume()), Version::Lite07), + reader: Reader::new(PendingRecv, Version::Lite07), + }; + let mut run = AnnounceRun::new(crate::Path::new("").to_owned(), consumer.hop(), Version::Lite07); + let waiter = kio::Waiter::noop(); + assert!(run.poll(&mut stream, &consumer, &mut announced, &waiter).is_pending()); + drop(removed); + let mut churn = Vec::new(); + for limit in 1..=4 { + churn.push(origin.announce(format!("a/{limit}"), origin::Route::default()).unwrap()); + *budget.write().ok().unwrap() = limit; + assert!(run.poll(&mut stream, &consumer, &mut announced, &waiter).is_pending()); + } + assert_eq!(wire.take_ok().active, 0); + let messages = wire.take_announces(); + assert!( + matches!(&messages[0], lite::AnnounceBroadcast::Active { suffix, .. } if suffix.rest.as_str() == "z/retained") + ); + assert_eq!(messages[2], lite::AnnounceBroadcast::Synced); + assert!(!messages.iter().any( + |msg| matches!(msg, lite::AnnounceBroadcast::Active { suffix, .. } if suffix.rest.as_str() == "z/removed") + )); + } + + #[test] + fn a_partial_regrant_cannot_overtake_an_initial_route() { + let origin = Hop::new(1).unwrap().produce(); + let _a = origin.announce("a", origin::Route::default()).unwrap(); + let _b = origin.announce("b", origin::Route::default()).unwrap(); + let consumer = origin.consume(); + let mut announced = consumer.announced(); + let budget = kio::Producer::new(1usize); + let log = Log::default(); + let mut wire = Wire { + writes: log.writes.clone(), + cursor: 0, + version: Version::Lite07, + }; + let mut stream = Stream:: { + writer: Writer::new(SinkSend::stepped(log, budget.consume()), Version::Lite07), + reader: Reader::new(PendingRecv, Version::Lite07), + }; + let mut run = AnnounceRun::new(crate::Path::new("").to_owned(), consumer.hop(), Version::Lite07); + let waiter = kio::Waiter::noop(); + assert!(run.poll(&mut stream, &consumer, &mut announced, &waiter).is_pending()); + let auth = crate::auth::Handle::new(false); + auth.authorize(&crate::auth::Grant { + subscribe: crate::Pattern::literal("b").unwrap().into(), + ..Default::default() + }); + let Poll::Ready(restricted) = auth.poll_permit(crate::auth::Direction::Publish, &mut 0, &waiter) else { + panic!("the changed grant is ready"); + }; + run.regrant(&mut stream, &consumer, restricted).unwrap(); + *budget.write().ok().unwrap() = 8; + assert!(run.poll(&mut stream, &consumer, &mut announced, &waiter).is_pending()); + wire.take_ok(); + let messages = wire.take_announces(); + assert!(matches!(&messages[1], lite::AnnounceBroadcast::Active { suffix, .. } if suffix.rest.as_str() == "b")); + assert_eq!(messages[2], lite::AnnounceBroadcast::Synced); + assert_eq!(messages[3], lite::AnnounceBroadcast::EndedId { id: 0 }); + } + + #[test] + fn synced_cuts_a_regrant_burst_at_the_initial_bound() { + let origin = Hop::new(1).unwrap().produce(); + let _a = origin.announce("a", origin::Route::default()).unwrap(); + let _b = origin.announce("b", origin::Route::default()).unwrap(); + let consumer = origin.consume(); + let mut announced = consumer.announced(); + let budget = kio::Producer::new(0usize); + let log = Log::default(); + let mut wire = Wire { + writes: log.writes.clone(), + cursor: 0, + version: Version::Lite07, + }; + let mut stream = Stream:: { + writer: Writer::new(SinkSend::stepped(log, budget.consume()), Version::Lite07), + reader: Reader::new(PendingRecv, Version::Lite07), + }; + let mut run = AnnounceRun::new(crate::Path::new("").to_owned(), consumer.hop(), Version::Lite07); + let waiter = kio::Waiter::noop(); + assert!(run.poll(&mut stream, &consumer, &mut announced, &waiter).is_pending()); + let auth = crate::auth::Handle::new(false); + auth.authorize(&crate::auth::Grant::default()); + let Poll::Ready(restricted) = auth.poll_permit(crate::auth::Direction::Publish, &mut 0, &waiter) else { + panic!("the changed grant is ready"); + }; + run.regrant(&mut stream, &consumer, restricted).unwrap(); + let _live = origin.announce("c", origin::Route::default()).unwrap(); + run.regrant(&mut stream, &consumer, crate::auth::Permit::default()) + .unwrap(); + *budget.write().ok().unwrap() = 4; + assert!(run.poll(&mut stream, &consumer, &mut announced, &waiter).is_pending()); + wire.take_ok(); + let messages = wire.take_announces(); + assert_eq!(messages.len(), 4); + let initial: std::collections::BTreeSet<_> = messages[..2] + .iter() + .map(|message| match message { + lite::AnnounceBroadcast::Active { suffix, .. } => suffix.rest.as_str(), + other => panic!("expected an initial start, got {other:?}"), + }) + .collect(); + assert_eq!(initial, std::collections::BTreeSet::from(["a", "b"])); + assert_eq!(messages[2], lite::AnnounceBroadcast::Synced); + assert_eq!( + messages + .iter() + .filter(|msg| matches!(msg, lite::AnnounceBroadcast::Synced)) + .count(), + 1 + ); + } + + #[test] + fn a_seen_retraction_can_precede_synced() { + let origin = Hop::new(1).unwrap().produce(); + let first = origin.announce("a", origin::Route::default()).unwrap(); + let unseen = origin.announce("b", origin::Route::default()).unwrap(); + let consumer = origin.consume(); + let mut announced = consumer.announced(); + let budget = kio::Producer::new(1usize); + let log = Log::default(); + let mut wire = Wire { + writes: log.writes.clone(), + cursor: 0, + version: Version::Lite07, + }; + let mut stream = Stream:: { + writer: Writer::new(SinkSend::stepped(log, budget.consume()), Version::Lite07), + reader: Reader::new(PendingRecv, Version::Lite07), + }; + let mut run = AnnounceRun::new(crate::Path::new("").to_owned(), consumer.hop(), Version::Lite07); + let waiter = kio::Waiter::noop(); + assert!(run.poll(&mut stream, &consumer, &mut announced, &waiter).is_pending()); + drop(unseen); + drop(first); + *budget.write().ok().unwrap() = 4; + assert!(run.poll(&mut stream, &consumer, &mut announced, &waiter).is_pending()); + wire.take_ok(); + assert!(matches!( + wire.take_announces().as_slice(), + [ + lite::AnnounceBroadcast::Active { .. }, + lite::AnnounceBroadcast::EndedId { id: 0 }, + lite::AnnounceBroadcast::Synced + ] + )); + assert!(run.poll(&mut stream, &consumer, &mut announced, &waiter).is_pending()); + wire.assert_quiet(); + } + + #[test] + fn an_empty_cold_relay_syncs_without_its_upstream() { + let origin = Hop::new(1).unwrap().produce(); + let _upstream = origin.snapshot(""); + let consumer = origin.consume(); + let mut announced = consumer.announced(); + assert!(!announced.is_synced()); + let log = Log::default(); + let mut wire = Wire { + writes: log.writes.clone(), + cursor: 0, + version: Version::Lite07, + }; + let mut stream = Stream:: { + writer: Writer::new(SinkSend::new(log), Version::Lite07), + reader: Reader::new(PendingRecv, Version::Lite07), + }; + let mut run = AnnounceRun::new(crate::Path::new("").to_owned(), consumer.hop(), Version::Lite07); + assert!( + run.poll(&mut stream, &consumer, &mut announced, &kio::Waiter::noop()) + .is_pending() + ); + wire.take_ok(); + assert_eq!(wire.take_announces(), vec![lite::AnnounceBroadcast::Synced]); + } + /// The hops stamped on every harness route. fn pub_hops() -> Hops { Hops::try_from(vec![Hop::new(9).unwrap()]).unwrap() @@ -2652,8 +2905,16 @@ mod announce_test { cursor: 0, version, }; - assert_eq!(wire.take_ok().active, 1, "expected one initial announce"); - match wire.take_announces().as_slice() { + assert_eq!( + wire.take_ok().active, + if version.has_synced() { 0 } else { 1 }, + "initial count on old versions" + ); + let mut initial = wire.take_announces(); + if version.has_synced() { + assert_eq!(initial.pop(), Some(lite::AnnounceBroadcast::Synced)); + } + match initial.as_slice() { [lite::AnnounceBroadcast::Active { suffix, hops, cost, .. }] => { assert_eq!(suffix.rest.as_str(), "cam"); assert_eq!(hops, &lite::HopsRef::literal(pub_hops())); diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index c67271c38a0..8758fe03f7a 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -260,7 +260,7 @@ impl Subscriber { &mut run.announced, )?; } - lite::AnnounceBroadcast::Skipped => {} + lite::AnnounceBroadcast::Skipped | lite::AnnounceBroadcast::Synced => {} } Ok(()) } @@ -1386,7 +1386,7 @@ impl AnnouncePrefix { }; let version = self.subscriber.version; // Lite03/04 mark no initial set, so the request going out is the best - // boundary. Lite01/02 mark it with ANNOUNCE_INIT, newer ones with a count. + // boundary. Lite01/02 use ANNOUNCE_INIT, lite05/06 a count, lite07 SYNCED. if !version.has_announce_ok() && !matches!(version, Version::Lite01 | Version::Lite02) { self.snapshot = None; } @@ -1402,9 +1402,9 @@ impl AnnouncePrefix { PrefixState::ReadOk { stream } => { // Lite05+: the publisher reports its own origin id, which we stamp onto // every received Announce's hop chain since it no longer does so itself. - // Its `active` count marks where the initial set ends. + // Lite05/06 mark the initial set by count; lite07 waits for SYNCED. let ok = ready!(stream.reader.poll_decode::(&mut cx))?; - if ok.active == 0 { + if ok.active == 0 && !self.subscriber.version.has_synced() { self.snapshot = None; } // A peer may legally report id 0 (no identity). Keep it: the assigned @@ -1492,6 +1492,12 @@ impl AnnouncePrefix { loop { match stream.reader.poll_decode_maybe::(&mut cx) { Poll::Ready(Ok(Some(announce))) => { + if matches!(announce, lite::AnnounceBroadcast::Synced) { + if self.snapshot.take().is_none() { + return Poll::Ready(Err(Error::ProtocolViolation)); + } + continue; + } if run.initial > 0 { match announce { lite::AnnounceBroadcast::Active { .. } => run.initial -= 1, @@ -1502,11 +1508,11 @@ impl AnnouncePrefix { } self.subscriber.handle_announce(&self.prefix, announce, run)?; // Released only once the last counted route is in the origin. - if run.initial == 0 { + if run.initial == 0 && !self.subscriber.version.has_synced() { self.snapshot = None; } } - Poll::Ready(Ok(None)) if run.initial > 0 => { + Poll::Ready(Ok(None)) if run.initial > 0 || self.snapshot.is_some() => { return Poll::Ready(Err(Error::ProtocolViolation)); } Poll::Ready(Ok(None)) => { @@ -4511,11 +4517,12 @@ mod tests { assert!(!cursor.is_synced()); } - /// A count of zero is synced on the ANNOUNCE_OK. + /// An empty lite-07 set waits for the ordered SYNCED event. #[moq_clock::test] async fn an_empty_initial_set_catches_up_at_once() { const VERSION: Version = Version::Lite07; - let script = announce_script(VERSION, &[&announce_ok(VERSION, 0)]); + let synced = |w: &mut crate::coding::Encoder<'_>| lite::AnnounceBroadcast::Synced.encode(w, VERSION).unwrap(); + let script = announce_script(VERSION, &[&announce_ok(VERSION, 0), &synced]); let (mut prefix, consumer) = root_prefix(VERSION, crate::lite::test_transport::ScriptedSession::new(script)); let cursor = consumer.announced(); let mut run = std::pin::pin!(kio::wait(|waiter| prefix.poll(waiter))); @@ -4523,6 +4530,16 @@ mod tests { assert!(cursor.is_synced()); } + #[moq_clock::test] + async fn a_duplicate_synced_is_refused() { + const VERSION: Version = Version::Lite07; + let synced = |w: &mut crate::coding::Encoder<'_>| lite::AnnounceBroadcast::Synced.encode(w, VERSION).unwrap(); + let script = announce_script(VERSION, &[&announce_ok(VERSION, 0), &synced, &synced]); + let (mut prefix, _consumer) = root_prefix(VERSION, crate::lite::test_transport::ScriptedSession::new(script)); + let err = kio::wait(|waiter| prefix.poll_run(waiter)).await.unwrap_err(); + assert!(matches!(err, Error::ProtocolViolation)); + } + /// Anything but a start inside the counted set cuts it short: a protocol violation. #[moq_clock::test] async fn an_end_inside_the_initial_set_is_refused() { diff --git a/rs/moq-net/src/lite/test_transport.rs b/rs/moq-net/src/lite/test_transport.rs index 577f25bdc78..e36de468f1d 100644 --- a/rs/moq-net/src/lite/test_transport.rs +++ b/rs/moq-net/src/lite/test_transport.rs @@ -183,6 +183,8 @@ pub struct SinkSend { /// Writes park until this flips to true; `None` writes immediately. See /// [`SinkSession::gated_bi`]. gate: Option>, + budget: Option>, + writes: usize, /// Bridges the gate's kio wakeups onto the caller's `Context`. park: kio::Park, /// Set by [`finish`](poll::SendStream::finish). A finished stream is what @@ -236,6 +238,8 @@ impl SinkSend { id: log.streams.fetch_add(1, Ordering::Relaxed), log, gate: None, + budget: None, + writes: 0, park: kio::Park::default(), finished: false, unacked_fin: false, @@ -243,6 +247,14 @@ impl SinkSend { } } + /// A send stream that accepts only the permitted number of writes. + pub fn stepped(log: Log, budget: kio::Consumer) -> Self { + Self { + budget: Some(budget), + ..Self::new(log) + } + } + /// A send stream whose writes park until `gate` flips to true. pub fn gated(log: Log, gate: kio::Consumer) -> Self { Self { @@ -250,6 +262,8 @@ impl SinkSend { id: log.streams.fetch_add(1, Ordering::Relaxed), log, gate: Some(gate), + budget: None, + writes: 0, park: kio::Park::default(), finished: false, unacked_fin: false, @@ -274,6 +288,20 @@ impl poll::SendStream for SinkSend { Poll::Ready(Err(_)) | Poll::Pending => return Poll::Pending, } } + if let Some(budget) = &self.budget { + let waiter = self.park.hold(cx); + match budget.poll(waiter, |limit| { + if self.writes < **limit { + Poll::Ready(()) + } else { + Poll::Pending + } + }) { + Poll::Ready(Ok(_)) => {} + _ => return Poll::Pending, + } + } + self.writes += 1; let buf = if self.prefix > 0 { let take = buf.len().min(self.prefix); self.prefix -= take; diff --git a/rs/moq-net/src/lite/version.rs b/rs/moq-net/src/lite/version.rs index 1aab80781be..59c749088a9 100644 --- a/rs/moq-net/src/lite/version.rs +++ b/rs/moq-net/src/lite/version.rs @@ -285,6 +285,15 @@ impl Version { } } + /// Whether discovery streams end their initial set with SYNCED rather than a count. + #[allow(clippy::match_like_matches_macro)] + pub fn has_synced(self) -> bool { + match self { + Self::Lite01 | Self::Lite02 | Self::Lite03 | Self::Lite04 | Self::Lite05 | Self::Lite06 => false, + _ => true, + } + } + /// Whether announcements carry the route cost: the marginal cost of pulling /// the broadcast via this route, accumulated per link. Added in lite-06. /// Older versions carry nothing, so a received route stays at zero and ranks diff --git a/rs/moq-net/src/model/origin.rs b/rs/moq-net/src/model/origin.rs index 421c7760efe..607e23345ab 100644 --- a/rs/moq-net/src/model/origin.rs +++ b/rs/moq-net/src/model/origin.rs @@ -1,8 +1,9 @@ use crate::{broadcast, cache, stats, track}; +use hashlink::LinkedHashMap; use kio::Task; use std::{ cmp::Reverse, - collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque}, + collections::{BTreeSet, HashMap, HashSet, VecDeque}, fmt, sync::Arc, sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering}, @@ -806,13 +807,18 @@ enum PendingUpdate { }, } -/// Pending updates keyed by prefix. `BTreeMap` keeps memory strictly bounded by -/// the number of distinct prefixes with outstanding work (collapsed pairs are -/// fully erased) and gives a deterministic lexicographic delivery order so -/// tests can predict it. +// An update is taken only while collapsing it under the cursor state lock. +struct Pending { + update: Option, + // A later live event must not consume an earlier snapshot entry's debt. + owed: bool, +} + +/// Pending updates keep their insertion order when metadata changes. Fully removing +/// cancelled entries bounds memory by prefixes with outstanding work. #[derive(Default)] struct OriginConsumerState { - pending: BTreeMap, + pending: LinkedHashMap, /// Prefixes whose most recently delivered update was an announce. A pending /// `Announce` is ambiguous on its own: it is an unseen initial announce (a /// retraction cancels it entirely) or a metadata update on a route the @@ -828,39 +834,51 @@ struct OriginConsumerState { /// holds back (nothing waits on losing sync), except the last landing, which /// takes the write guard to owe the queue too. Only the table lock changes it. waiting: AtomicUsize, - /// The pending prefixes the consumer still owes reading from before the last - /// snapshot landed. Synced once empty and nothing is `waiting`. - owed: BTreeSet, + /// Pending entries from before the last snapshot landed. Synced at zero + /// when no source is `waiting`; held metadata changes are not counted. + owed: usize, } impl OriginConsumerState { /// Whether the consumer has read everything its sources announced before they /// synced. A torn-down origin has no sources left to wait on. fn synced(&self) -> bool { - (self.waiting.load(Ordering::Relaxed) == 0 || self.ended) && self.owed.is_empty() + (self.waiting.load(Ordering::Relaxed) == 0 || self.ended) && self.owed == 0 } /// Owe every pending update that changes which prefixes are announced: a start /// the consumer has not seen, or an end. A re-price or restart of a delivered /// route leaves the set as it is, and may be held, so it is not owed. fn owe(&mut self) { - self.owed = self - .pending - .iter() - .filter(|(prefix, update)| { - !self.delivered.contains(*prefix) - || matches!( - update, - PendingUpdate::Unannounce(_) | PendingUpdate::UnannounceAnnounce { .. } - ) - }) - .map(|(prefix, _)| prefix.clone()) - .collect(); + self.owed = 0; + for (prefix, pending) in &mut self.pending { + pending.owed = !self.delivered.contains(prefix) + || matches!( + pending.update, + Some(PendingUpdate::Unannounce(_) | PendingUpdate::UnannounceAnnounce { .. }) + ); + self.owed += usize::from(pending.owed); + } + } + + fn put(&mut self, prefix: PathOwned, update: PendingUpdate) { + match self.pending.get_mut(&prefix) { + Some(pending) => pending.update = Some(update), + None => { + self.pending.insert( + prefix, + Pending { + update: Some(update), + owed: false, + }, + ); + } + } } fn apply_announce(&mut self, prefix: PathOwned, meta: RouteMeta, captures: Option>) { let meta = (meta, captures); - let new = match self.pending.remove(&prefix) { + let new = match self.pending.get_mut(&prefix).and_then(|pending| pending.update.take()) { // First announce, a stale announce being replaced, or a metadata update. None | Some(PendingUpdate::Announce(_)) => PendingUpdate::Announce(meta), // The consumer still owes the restart; this only moves its metadata. @@ -870,7 +888,7 @@ impl OriginConsumerState { PendingUpdate::UnannounceAnnounce { old, new: meta } } }; - self.pending.insert(prefix, new); + self.put(prefix, new); } /// Another instance replaces the route at `prefix`, or announces it afresh when @@ -884,7 +902,7 @@ impl OriginConsumerState { release: Option, ) { let meta = (meta, captures); - let new = match self.pending.remove(&prefix) { + let new = match self.pending.get_mut(&prefix).and_then(|pending| pending.update.take()) { Some(PendingUpdate::Unannounce(old) | PendingUpdate::UnannounceAnnounce { old, .. }) => { PendingUpdate::UnannounceAnnounce { old, new: meta } } @@ -895,26 +913,27 @@ impl OriginConsumerState { // The consumer never saw the old instance, so this is its first announce. _ => PendingUpdate::Announce(meta), }; - self.pending.insert(prefix, new); + self.put(prefix, new); } fn apply_unannounce(&mut self, prefix: PathOwned, last: RouteMeta, captures: Option>) { let last = (last, captures); - match self.pending.remove(&prefix) { + match self.pending.get_mut(&prefix).and_then(|pending| pending.update.take()) { // The pending announce was never delivered and neither was any earlier // one, so the pair cancels entirely, and nothing is owed for it. Some(PendingUpdate::Announce(_)) if !self.delivered.contains(&prefix) => { - self.owed.remove(&prefix); + let pending = self.pending.remove(&prefix).unwrap(); + self.owed -= usize::from(pending.owed); } // Either nothing is pending or the pending announce was a metadata // update on a delivered route; the consumer still owes a retraction. None | Some(PendingUpdate::Announce(_) | PendingUpdate::Restart(..) | PendingUpdate::Unannounce(_)) => { - self.pending.insert(prefix, PendingUpdate::Unannounce(last)); + self.put(prefix, PendingUpdate::Unannounce(last)); } // The embedded announce cancels with this retraction; the consumer still // needs the leading one. Some(PendingUpdate::UnannounceAnnounce { old, .. }) => { - self.pending.insert(prefix, PendingUpdate::Unannounce(old)); + self.put(prefix, PendingUpdate::Unannounce(old)); } } } @@ -940,6 +959,7 @@ impl OriginConsumerState { ) -> Result> { let mut wake = None; for (prefix, update) in &self.pending { + let update = update.update.as_ref().unwrap(); let Some(now) = now.filter(|_| !hold.is_zero() && !self.ended) else { return Ok(prefix.clone()); }; @@ -986,20 +1006,22 @@ impl OriginConsumerState { /// Take the pending update at `prefix` for delivery. fn take_prefix(&mut self, prefix: PathOwned) -> AnnounceEvent { - let event = self.take_update(prefix); + let (event, owed) = self.take_update(prefix); let (AnnounceEvent::Start(announce) | AnnounceEvent::Update(announce) | AnnounceEvent::Restart(announce) | AnnounceEvent::End(announce)) = &event; // A trailing announce left pending is still owed. - if !self.pending.contains_key(&announce.prefix) { - self.owed.remove(&announce.prefix); + if owed && !self.pending.contains_key(&announce.prefix) { + self.owed -= 1; } event } - fn take_update(&mut self, prefix: PathOwned) -> AnnounceEvent { - let ((meta, captures), kind): (_, fn(Announce) -> AnnounceEvent) = match self.pending.remove(&prefix).unwrap() { + fn take_update(&mut self, prefix: PathOwned) -> (AnnounceEvent, bool) { + let pending = self.pending.remove(&prefix).unwrap(); + let owed = pending.owed; + let ((meta, captures), kind): (_, fn(Announce) -> AnnounceEvent) = match pending.update.unwrap() { PendingUpdate::Announce(meta) => { // The consumer has seen this prefix before, so it is a metadata update. let kind = match self.delivered.insert(prefix.clone()) { @@ -1020,22 +1042,31 @@ impl OriginConsumerState { // Deliver the retraction now; leave the trailing announce pending so // the next take returns it for the same prefix. self.delivered.remove(&prefix); - self.pending.insert(prefix.clone(), PendingUpdate::Announce(new)); + self.pending.insert( + prefix.clone(), + Pending { + update: Some(PendingUpdate::Announce(new)), + owed, + }, + ); (old, AnnounceEvent::End) } }; - kind(Announce { - prefix, - captures, - route: Route { - hops: meta.0, - cost: meta.1, - via: Hop::UNKNOWN, - source: meta.2, - epoch: meta.3, - auth: meta.4, - }, - }) + ( + kind(Announce { + prefix, + captures, + route: Route { + hops: meta.0, + cost: meta.1, + via: Hop::UNKNOWN, + source: meta.2, + epoch: meta.3, + auth: meta.4, + }, + }), + owed, + ) } } @@ -6061,6 +6092,56 @@ impl AnnounceConsumer { kio::wait(|waiter| self.poll_synced(waiter)).await } + // The wire sender uses this only to bound its initial replay. + // private: tuning + #[cfg(feature = "async")] + pub(crate) fn pending_len(&self) -> usize { + self.state.read().pending.len() + } + + // A wider wire grant must reuse pending slots, rather than bypass the initial queue. + // private: tuning + #[cfg(feature = "async")] + pub(crate) fn replay<'a>( + &mut self, + advertised: impl IntoIterator, + revoked: &HashSet, + ) { + let advertised: HashSet<_> = advertised.into_iter().collect(); + let table = self.shared.lock(); + let Ok(mut state) = self.state.write() else { + return; + }; + for id in &self.ids { + let Some(cursor) = table.cursors.get(id) else { + continue; + }; + for (presented, (_, meta, _, captures, _)) in &cursor.current { + let prefix = cursor.under.join(presented); + if revoked.contains(&prefix) { + state.apply_unannounce(prefix, meta.clone(), captures.clone()); + continue; + } + if advertised.contains(&prefix) { + if state.pending.get(&prefix).is_some_and(|pending| { + matches!( + pending.update, + Some(PendingUpdate::Unannounce(_) | PendingUpdate::UnannounceAnnounce { .. }) + ) + }) { + state.apply_announce(prefix, meta.clone(), captures.clone()); + } + continue; + } + // The wire never sent this route, so it is a fresh start without a hold. + state.delivered.remove(&prefix); + self.held.remove(&prefix); + self.retracted.remove(&prefix); + state.put(prefix, PendingUpdate::Announce((meta.clone(), captures.clone()))); + } + } + } + /// Returns the root that is automatically stripped from emitted prefixes. pub fn root(&self) -> &Path<'_> { &self.root @@ -6396,6 +6477,32 @@ mod tests { assert!(announced.is_synced()); } + #[test] + fn initial_routes_precede_live_prefixes() { + let producer = origin(1).produce(); + let _initial = producer.announce("z/initial", Route::default()).unwrap(); + let mut announced = producer.consume().announced(); + let _live = producer.announce("a/live", Route::default()).unwrap(); + announced.assert_next_active("z/initial"); + assert!(announced.is_synced()); + } + + #[test] + fn stalled_initial_replay_collapses_churn_without_a_backlog() { + let producer = origin(1).produce(); + let initial = producer.announce("z/initial", Route::default()).unwrap(); + let mut announced = producer.consume().announced(); + for i in 0..1_000 { + let transient = producer.announce(format!("a/{i}"), Route::default()).unwrap(); + initial.update(Route::default().with_cost(i)).unwrap(); + drop(transient); + assert_eq!(announced.state.read().pending.len(), 1); + } + announced.assert_next_active("z/initial"); + assert!(announced.is_synced()); + announced.assert_next_wait(); + } + /// A start and end that cancel before being read are owed nothing. #[test] fn a_cancelled_start_still_catches_up() { @@ -6664,9 +6771,9 @@ mod tests { announced.assert_next_wait(); let mut announced = project.clone().with_hidden(true).announced(); + announced.expect_next_active("cam").await; announced.expect_next_active(".svc").await; announced.expect_next_active(".svc/foo").await; - announced.expect_next_active("cam").await; announced.assert_next_wait(); // Rooted inside the mount, the claim covers the root itself. @@ -6994,7 +7101,7 @@ mod tests { let _b = producer.announce("room/bob", Route::default()).unwrap(); let mut announced = producer.consume().announced(); - // BTreeMap order: lexicographic by prefix. + // Replay follows the table traversal order. announced.expect_next_active("room/alice").await; announced.expect_next_active("room/bob").await; announced.assert_next_wait(); @@ -8932,11 +9039,11 @@ mod tests { .unwrap(); assert_eq!(announced.expect_next_active("client").await.source(), Source::Local); + assert_eq!(announced.expect_next_active("gateway").await.source(), Source::Local); assert_eq!( announced.expect_next_active("forwarded").await.source(), Source::Peer(origin(7)) ); - assert_eq!(announced.expect_next_active("gateway").await.source(), Source::Local); // The mark survives narrowing the handle. let scoped = peer.scope("room", &Patterns::from(Pattern::all())).unwrap(); diff --git a/rs/moq-net/tests/integration/synced.rs b/rs/moq-net/tests/integration/synced.rs index 47a05873b41..cbb741d5c8d 100644 --- a/rs/moq-net/tests/integration/synced.rs +++ b/rs/moq-net/tests/integration/synced.rs @@ -120,3 +120,29 @@ async fn a_second_session_catches_up() { read_until_synced(&mut announced).await; drop(second); } + +/// A cold relay's empty set is complete before an upstream joins it. +#[moq_clock::test] +async fn a_cold_relay_syncs_then_forwards_upstream_routes_as_live() { + let relay = produce_origin(2); + let subscriber = produce_origin(3); + let version: Version = "moq-lite-07-wip".parse().unwrap(); + let mut downstream = MockConnectOptions::new(version); + downstream.server_publish = Some(relay.consume()); + downstream.client_subscribe = Some(subscriber.clone()); + let _downstream = connect_mock(downstream).await; + let mut announced = subscriber.consume().announced(); + moq_net::kio::wait(|waiter| announced.poll_synced(waiter)).await; + assert!(announced.try_next().is_none()); + + let upstream = produce_origin(1); + let broadcast = upstream.create_broadcast("room/cam").unwrap(); + broadcast.announce(Default::default()).unwrap(); + let mut options = MockConnectOptions::new(version); + options.server_publish = Some(upstream.consume()); + options.client_subscribe = Some(relay.clone()); + let _upstream = connect_mock(options).await; + let event = announced.next().await.unwrap(); + assert!(matches!(event, announce::Event::Start(route) if route.prefix.as_str() == "room/cam")); + assert!(announced.is_synced()); +}