diff --git a/js/net/src/lite/publisher.test.ts b/js/net/src/lite/publisher.test.ts index e15417981c..7afcc28f2c 100644 --- a/js/net/src/lite/publisher.test.ts +++ b/js/net/src/lite/publisher.test.ts @@ -1680,6 +1680,29 @@ for (const version of [Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07]) { } } +// A relay's copy already asks from the oldest group, so a downstream asking from time 2 adds no +// range upstream and gets no start of its own. The oldest range starting at 6 still places it: +// the source holds nothing at or before 2. +test("lite draft-07: a time start a copy's oldest range covers starts where that range did", async () => { + const sub = await servedSubscription({ version: Version.DRAFT_07, startGroup: 2n }); + // The relay's own reader, asking from the oldest group, which the copy's demand then starts at. + const local = sub.track.subscribe({ delay: { max: Duration.millis(60_000) } }); + try { + hooks.receiveTrack(sub.track); + await flush(); + hooks.timeStarts(sub.track, [[0n, 6n]]); + hooks.startTrack(sub.track, { start: 6n }); + sub.serve(6n); + expect(await sub.servedSequence()).toBe(6n); + const response = await decodeSubscribeResponse(sub.client.reader, Version.DRAFT_07); + if (!("start" in response)) throw new Error("expected SUBSCRIBE_START"); + expect(response.start.starts[0]).toBe(6n); + } finally { + local.close(); + await sub.close(); + } +}); + // A relay's group stream can beat the upstream START on its own stream. Resolving from that // group would give up the backlog the START still promises, so the first group waits for it. for (const version of [Version.DRAFT_06, Version.DRAFT_07]) { diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index 4ce0b398d0..20f498e71c 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -2875,8 +2875,8 @@ export class Subscriber { // say where one starts. Mirrors the Rust `SubStream::declare`. #declare(entry: SubscribeEntry): void { // Where the publisher started each live range asked from a group's start, which a reader's - // time start there resolves to when the copy holds no group at it. Mirrors the Rust - // `report_time_starts`. + // time start there resolves to when the copy holds no group at it, and one asked from the + // oldest group as asked from 0. Mirrors the Rust `SubStream::report_starts`. const times: [bigint, bigint][] = []; // And each one counting back, which the copy, holding only part of the track, can't count, // with the latest group it counted back from. @@ -2889,6 +2889,7 @@ export class Subscriber { if (at === undefined) continue; if (time !== undefined) times.push([time, at]); else if (back !== undefined) relative.push([back, at, entry.edges.get(id)]); + else if (range !== undefined && range.start === undefined) times.push([0n, at]); } hooks.timeStarts(entry.track, times); hooks.relativeStarts(entry.track, relative); diff --git a/js/net/src/resume.test.ts b/js/net/src/resume.test.ts index 31d937c8e3..85c11e2537 100644 --- a/js/net/src/resume.test.ts +++ b/js/net/src/resume.test.ts @@ -4665,6 +4665,31 @@ test("a group an inner front hands out already failed still continues on the out b.close(); }); +// Mirrors `a_routed_reader_places_a_range_past_one_still_waiting` in rs/moq-net/src/model/resume.rs: +// a range the copy placed is placed on the reader too while an earlier one still waits on a time +// start, so a publisher names its start. +test("a routed reader places a range past one still waiting", async () => { + const a = new BroadcastProducer(); + const ta = copy(a); + hooks.receiveTrack(ta.producer); + const front = new Front(); + front.serve(a.consume()); + ta.write(64n); + const sub = front.consumer + .track("seq") + .subscribe({ delay: { max: null }, ranges: [{ start: { time: 66n } }, { start: { time: 64n } }] }); + try { + await settle(); + const resolved = hooks.servingResolved(sub); + expect(resolved[0]).toBeUndefined(); + expect(resolved[1]?.start).toBe(64n); + } finally { + sub.close(); + front.consumer.close(); + a.close(); + } +}); + // Mirrors `an_ended_copy_leaves_a_time_start_to_the_serving_copy` in rs/moq-net/src/model/resume.rs: // a copy that failed holds nothing past its last group, so it reaches a time start past that group // back to its oldest. The copy continuing the track places it where the track holds it. diff --git a/js/net/src/resume.ts b/js/net/src/resume.ts index 5d145e2f68..d2310a9c78 100644 --- a/js/net/src/resume.ts +++ b/js/net/src/resume.ts @@ -666,10 +666,10 @@ class Reader implements ResumeSource { } /** - * The reader's ranges resolved, a relative or time anchor where the first copy to place them - * all placed it, whatever its failover cap: that bounds what the copy delivers, not where the - * ranges resolve. Kept once placed, past that copy being let go. Pending, naming every group, - * while an anchor waits on a copy to place it. A copy resolves a time start against the groups + * The reader's ranges resolved, a relative or time anchor where the first copy to place it + * placed it, whatever its failover cap: that bounds what the copy delivers, not where the ranges + * resolve. Kept once placed, past that copy being let go. A range stays as first held, unplaced, + * while an anchor of its waits on a copy to place it. A copy resolves a time start against the groups * it holds, which may place it below where the reader's window did: the window decides what is * handed out. Mirrors the Rust `Reader::window`. */ @@ -704,42 +704,41 @@ class Reader implements ResumeSource { } /** - * Resolve unsettled {@link #window} anchors with a copy's resolution of the reader's ranges, - * unless an anchor still waits there too. Mirrors the Rust `Reader::resolve`. + * Place each unsettled {@link #window} range where a copy resolved it, on its own: one whose + * anchors still wait leaves the rest placed, so a publisher names their starts. Mirrors the Rust + * `Reader::resolve`. */ #resolve(resolved: readonly (Resolved | undefined)[]): void { const resolution = this.#resolution; if (!resolution) return; - // Once placed, relative and time starts stay pinned even while a time end settles. - const placed = resolution.window.pending ? undefined : resolution.window.resolved(); - const anchored: track.Range[] = []; - for (const [index, range] of resolution.from.entries()) { - const at = resolved[index]; + const placed: [bigint, track.Range][] = []; + for (const key of resolution.window.unsettled()) { + const range = resolution.from[Number(key)]; + if (range === undefined) continue; + const at = resolved[Number(key)]; if (!at) { // A range the copy left unresolved keeps its anchors, which only a copy can place. const unplaced = range.start?.relative !== undefined || (range.start?.time ?? 0n) > 0n || range.end?.relative !== undefined; - if (unplaced && !isEmpty(range)) return; - anchored.push(range); + // Placed as it is: nothing of it waits on a copy. + if (!unplaced || isEmpty(range)) placed.push([key, range]); continue; } - const previous = placed?.[index]; - const start = previous ?? at; - const end = previous?.end ?? at.end; - if (range.end !== undefined && end === 0n) { - anchored.push(EMPTY); + if (range.end !== undefined && at.end === 0n) { + placed.push([key, EMPTY]); continue; } const out: track.Range = {}; // One that reached back to the oldest group still does, as on a plain reader. - if (range.start !== undefined && start.floored) out.start = { time: start.start }; - if (range.end !== undefined) out.end = end === undefined ? range.end : { time: end - 1n }; - anchored.push(out); + if (range.start !== undefined && at.floored) out.start = { time: at.start }; + if (range.end !== undefined) out.end = at.end === undefined ? range.end : { time: at.end - 1n }; + // A relative end still open would leave it waiting on a first group it never gets. + if (out.end?.relative !== undefined && !isEmpty(out)) continue; + placed.push([key, out]); } - const window = new Window(anchored, undefined, AT_KEYS); - if (!window.pending) resolution.window = window; + if (placed.length > 0) resolution.window.edit([], placed, undefined, AT_KEYS); } /** The reader's ranges, as its preferences name them. */ diff --git a/js/net/src/track.test.ts b/js/net/src/track.test.ts index d925383297..904877b625 100644 --- a/js/net/src/track.test.ts +++ b/js/net/src/track.test.ts @@ -5634,7 +5634,10 @@ test("finishing returns handles that write nothing new but fill a late group", a finished.close(); }); -test("track: a reader's unplaced times are the times its holdings can't place", () => { +// Mirrors moq-net's `unplaced_holds_only_what_containing_cant_place`: a run holds only times +// `containing` can't place, but those a reported start places directly, and leaves out only +// times below the newest start reported past an earlier range's time. +test("track: a reader's unplaced times are only times its holdings can't place", () => { let seed = 5; const random = (below: number) => { seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0; @@ -5645,6 +5648,14 @@ test("track: a reader's unplaced times are the times its holdings can't place", for (let round = 0; round < 400; round++) { const received = random(2) === 0; const producer = received ? receivedTrack() : new TrackProducer("test").accept({ maxAge: null }); + const starts: [bigint, bigint][] = []; + if (received) { + for (let n = random(3); n > 0; n--) starts.push([BigInt(random(48)), BigInt(random(48))]); + hooks.timeStarts(producer, starts); + } + const reported = new Set(starts.map(([time]) => time)); + let past = 0n; + for (const [asked, start] of new Map(starts)) if (asked < start && start > past) past = start; const reader = producer.subscribe(); for (let step = random(16); step >= 0; step--) { const holdings = hooks.subscriberHoldings(reader); @@ -5652,7 +5663,10 @@ test("track: a reader's unplaced times are the times its holdings can't place", expect(unplaced).toBeDefined(); for (let at = 0n; at < 64n; at++) { const open = unplaced?.runs.some((run) => run.start <= at && at < run.end) ?? false; - expect([received, at, holdings.containing(at) === "unknown"]).toEqual([received, at, open]); + const unknown = holdings.containing(at) === "unknown"; + if (reported.has(at)) expect([at, unknown]).toEqual([at, false]); + else if (open) expect([at, unknown]).toEqual([at, true]); + else if (unknown) expect([at, received && at < past]).toEqual([at, true]); } const key = BigInt(random(48)); if (random(2) === 0) { diff --git a/js/net/src/track.ts b/js/net/src/track.ts index b274a2893a..a3af08b498 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -1008,6 +1008,8 @@ let keysOf: (producer: Producer) => readonly bigint[]; let timeAnchors: (producer: Producer) => { atKeys: boolean; starts: ReadonlyMap; + /** The newest of `starts` past the time its range asked from, 0 when none is. */ + past: bigint; relative: ReadonlyMap; /** Bumped whenever the others, or whether the producer is a received copy, change. */ placing: number; @@ -1277,6 +1279,9 @@ export class Producer { // Where the upstream publisher started each of a lite-07 copy's live wire ranges, by the time // it asked from. Mirrors the Rust `TrackState::time_starts`. #timeStarts = new Map(); + // The newest start in `#timeStarts` past the time its range asked from, kept as it is + // replaced: a reader's `unplaced` reads it on every settle. Mirrors the Rust `starts_past`. + #startsPast = 0n; // On a lite-07 copy, where the publisher started each live range counting back, by its count. #relativeStarts = new Map(); // Counts changes to how a time start is placed other than by groups arriving or drops: the wire, @@ -1513,6 +1518,7 @@ export class Producer { timeAnchors = (producer) => ({ atKeys: producer.#startsAtKeys, starts: producer.#timeStarts, + past: producer.#startsPast, relative: producer.#relativeStarts, placing: producer.#placing, }); @@ -1522,6 +1528,10 @@ export class Producer { }; hooks.timeStarts = (producer, starts) => { producer.#timeStarts = new Map(starts); + producer.#startsPast = 0n; + for (const [asked, start] of producer.#timeStarts) { + if (asked < start && start > producer.#startsPast) producer.#startsPast = start; + } producer.#placing++; }; hooks.relativeStarts = (producer, starts) => { @@ -3634,7 +3644,16 @@ export class Subscriber { if (below !== undefined && (drops.covers(below + 1n, time + 1n) || index < keys.length)) { return { group: below }; } - if (below === undefined && drops.covers(0n, time + 1n)) return "before"; + if (below === undefined) { + if (drops.covers(0n, time + 1n)) return "before"; + // A range asked from an earlier time, or from the oldest group (as 0), that the + // publisher started past it says the same, while the copy holds no group there. + if (anchors.past > time) { + for (const [asked, start] of anchors.starts) { + if (asked < time && start > time) return "before"; + } + } + } return "unknown"; } if (below !== undefined) return { group: below }; @@ -3649,14 +3668,20 @@ export class Subscriber { const runs: Run[] = []; const first = keys[0]; const last = keys.at(-1); + // `containing` places a time below every held group from a start reported past an + // earlier range's time, which an eviction brings about without moving the mark, so a + // run there starts past the newest such start. Mirrors the Rust `TrackState::unplaced`. + const past = wire ? anchors.past : 0n; + const unheld = (run: Run | undefined): Run | undefined => + run && run.start < past ? { start: past, end: run.end } : run; if (anchors.atKeys || state.fetch) { // Every time starts a group. } else if (first === undefined || last === undefined) { - const open = uncovered(0n); + const open = unheld(uncovered(0n)); if (open) runs.push(open); } else if (wire) { // Below every group, up to the first, and above the last, past the drops after it. - const below = uncovered(0n); + const below = unheld(uncovered(0n)); if (below && below.start < first) runs.push({ start: below.start, end: first }); const above = uncovered(last + 1n); if (above) runs.push(above); diff --git a/js/net/src/window.ts b/js/net/src/window.ts index a8b90bb0fe..f99e24ac20 100644 --- a/js/net/src/window.ts +++ b/js/net/src/window.ts @@ -560,6 +560,17 @@ export class Window { return this.#pending.size > 0; } + /** + * The keys of the ranges still waiting on a relative anchor, a live join's reach back, or a + * time start. Mirrors the Rust `Window::unsettled`. + */ + unsettled(): Set { + const keys = new Set([...this.#pending, ...this.#reaching]); + for (const entry of this.#waiting.all()) for (const key of entry.keys) keys.add(key); + for (const entry of this.#looking.all()) for (const key of entry.keys) keys.add(key); + return keys; + } + /** Whether nothing is left to resolve as groups arrive. */ get settled(): boolean { return this.#pending.size === 0 && !this.#unplaced() && this.#looking.size === 0; diff --git a/quest/m2/multicast/README.md b/quest/m2/multicast/README.md index d0c4d749c2..b9c1ea92d7 100644 --- a/quest/m2/multicast/README.md +++ b/quest/m2/multicast/README.md @@ -40,9 +40,8 @@ resumes on a detour learned after its session closed. The lite-07 cut still waits on this epic's children, the other flaw, [a route moving behind one next hop resending a group](/quest/m2/multicast/resend.md), whose fix changes the wire, -and two flaws the mesh replay found, -[feeding an old relay's subscription mid-roll](/quest/m2/multicast/roll-echo-stall.md) -and [a lite-07 front that stops delivering after it switches copies](/quest/m2/multicast/splice-stall.md); +and a flaw the mesh replay found, +[a lite-07 front that stops delivering after it switches copies](/quest/m2/multicast/splice-stall.md); and on [route layer acceptance](/quest/m2/cluster-routes/acceptance.md)'s scoped subscriber whose grant is too wide to ask for. It does not wait on diff --git a/quest/m2/multicast/roll-echo-stall.md b/quest/m2/multicast/roll-echo-stall.md deleted file mode 100644 index 7200a79c1d..0000000000 --- a/quest/m2/multicast/roll-echo-stall.md +++ /dev/null @@ -1,59 +0,0 @@ -# [M] A subscription from an old relay mid-roll is fed - -## Goal - -Mid-roll, a new relay feeds every SUBSCRIBE an old (lite-06) neighbour sends -it, including one that arrives as that neighbour withdraws its own echo of the -broadcast. Today the new relay accepts the subscription and forwards no group -until the broadcast ends, so the old relay's viewers stay subscribed, see the -broadcast live, and read nothing. - -No reader starves in -`just test mesh-replay --mesh test/mesh-replay/ring6.toml --script test/mesh-replay/half-roll.toml --broadcasts 32` -or in the nightly ring meshes' rolls, once `[[known]]` no longer names this -quest in `test/mesh-replay/script.toml`. - -## Plan - -Found by [the mesh replay](/test/mesh-replay/README.md). In the ring of six, -rolling `b` onto the new build starved old relay `c`'s readers of `a`'s -broadcasts in 3 of 4 nightly cells: 9 of 24 paths at 4 broadcasts per relay, -40 and 41 of 192 at 32. Rolls further on starved others the same way. The half -roll alone (`a` and `b`) starved them in 1 of 3 runs at 4 broadcasts. - -What the logs of a chain of three show (`a` dials `b` dials `c`, roll `a` then -`b`), with `b` the new relay and `c` the old one: - -1. `b` restarts on the new build and dials `c`. Its predecessor's session to `c` - is still draining, so the old relay `c` announces `a`'s broadcasts to the new - `b` over lite-06 with three hops, learned through that predecessor: `b` - hears its own stale state echoed back before its link to `a` is up. -2. `b`'s link to `a` comes up a second later. `b` pulls some of `a`'s broadcasts - from `a` for its own reader and for `c`'s first subscribes, and those flow. -3. The predecessor's session to `c` times out (the 10s idle timeout). In one - millisecond, `c` unannounces the echoed broadcasts to `b` and resubscribes - the ones it read through the predecessor, now on the new `b`. -4. `b` logs `subscribed started` for those subscribes and serves nothing until - the publisher stops 50s later, when it serves one group from step 3 and - then the final group. Its own upstream subscription to `a` for the same - broadcasts is live the whole time, feeding `b`'s reader. - -So a lite-06 subscription that resolves while its broadcast's echoed route is -being withdrawn attaches to something that never delivers, and nothing moves -it to the live route. Likely in how the publisher resolves a request against -the origin while a route is withdrawn: the wait for a claim's route -([moq-dev/moq#9052](https://github.com/moq-dev/moq/pull/9052)), or the lite-06 -path's announce handling (hop counts are not checked against loops), in -`rs/moq-net/src/lite/publisher.rs` and `rs/moq-net/src/model/`. Rerun after -[moq-dev/moq#9112](https://github.com/moq-dev/moq/pull/9112), which changes how -a let-go claim's copies end but keeps lite-06 routes as they are. Run with -`HARNESS_KEEP=1 MESH_REPLAY_LOG=debug` to keep every relay's debug log. - -Decided 2026-11-19 (coordinator, to which the maintainer delegated the call -while away): ✅ the lite-07 cut waits on it (rejected: cutting without it). -It needs an old relay, so it lasts only as long as a roll does, but every roll -passes through it, once per relay, and a starved viewer sees a live broadcast -that never plays; letting a deployment roll lite-07 out safely is the reason -for the cut. - -Not a wire change. diff --git a/quest/m2/multicast/splice-stall.md b/quest/m2/multicast/splice-stall.md index 252f67f24c..f41d505120 100644 --- a/quest/m2/multicast/splice-stall.md +++ b/quest/m2/multicast/splice-stall.md @@ -58,5 +58,5 @@ Not a wire change. - [reopen-starve](https://github.com/moq-dev/moq/pull/9179) - a request refused before the neighbour's claims returned, and never asked again -- [roll-echo-stall](/quest/m2/multicast/roll-echo-stall.md) - a time start a +- [roll-echo-stall](https://github.com/moq-dev/moq/pull/9174) - a time start a shared copy never places diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 9786d3c259..48d39a2c8e 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -5894,13 +5894,39 @@ impl TrackRun { } // SUBSCRIBE_START answers the SUBSCRIBE's ranges, by Range ID: none for one an update // retired already, and SUBSCRIBE_STARTED answers each one an update added. - self.track - .resolved_keys(0..self.initial) - .into_iter() - .map(|resolved| { - let resolved = resolved?; - let from = resolved.start.max(start); - resolved.end.is_none_or(|end| end > from).then_some(from) + let mut resolved = self.track.resolved_keys(0..self.initial); + let unresolved: Vec = (0..) + .zip(&resolved) + .filter(|(_, resolved)| resolved.is_none()) + .map(|(id, _)| id) + .collect(); + let waiting = self.track.waiting_keys(unresolved.iter().copied()); + // Each read syncs the cursor on its own, so an upstream answer on another thread can + // place a range between them. Placing is one-way: a range no longer waiting is resolved + // now, unless an update retired it, so read those again rather than name no start. + let placed: Vec = (unresolved.iter().zip(&waiting)) + .filter(|(_, waiting)| waiting.is_none()) + .map(|(&id, _)| id) + .collect(); + if !placed.is_empty() { + for (id, late) in placed.iter().zip(self.track.resolved_keys(placed.iter().copied())) { + resolved[*id as usize] = late; + } + } + let mut waiting = unresolved.iter().copied().zip(waiting).peekable(); + (0..) + .zip(resolved) + .map(|(id, resolved)| { + let waits = waiting.next_if(|(key, _)| *key == id).and_then(|(_, waits)| waits); + let (from, end) = match (resolved, waits) { + (Some(resolved), _) => (resolved.start.max(start), resolved.end), + // A time start still waiting at or below the floor, as an update before the + // start leaves one: whichever group contains it is out of range, so the range + // starts where delivery does, the answer placing it would give. + (None, Some((time, end))) if time <= self.floor => (start, end), + (None, _) => return None, + }; + end.is_none_or(|end| end > from).then_some(from) }) .collect() } @@ -8093,6 +8119,23 @@ mod serve_group_test { (TrackRun::new(ctx, track, bounds, 0, steer), writer, log) } + /// [`lite07_run`] with the SUBSCRIBE asking for `ranges` on `track`. + fn lite07_run_ranges( + track: &track::Producer, + ranges: &[lite::Range], + ) -> (TrackRun, Writer, Log) { + let bounds = Bounds::new(ranges, false); + let subscriber = track.subscribe( + track::Subscription::default() + .with_delay(track::Delay::default().with_max(Duration::from_secs(3600))) + .with_ranges(bounds.ranges.iter().copied()), + ); + let (mut run, writer, log) = lite07_run(SinkSession::new(Log::default()), subscriber); + run.initial = bounds.ranges.len() as u64; + run.requested = Requested::new((0..).zip(bounds.ranges.iter().copied())); + (run, writer, log) + } + /// [`lite_run`] with the SUBSCRIBE naming group `start`. fn lite_run_from( version: Version, @@ -8756,6 +8799,98 @@ mod serve_group_test { ); } + /// An update before SUBSCRIBE_START that leaves only a time start a lite-07 copy can't + /// place yet floors the cursor at that time, so the group containing it is out of range + /// whichever it is: the range starts where delivery does, reported rather than left + /// without a start. + #[kio::test] + async fn lite07_a_floored_time_start_reports_where_delivery_starts() { + // Open, and through the latest group, which already holds group 1000. + for end in [None, Some(lite::Anchor::Relative(0))] { + let mut track = track::Producer::new( + Arc::new(broadcast::Info::default()), + "test", + crate::track::Info::timed(), + ); + track.received(); + track.anchored(); + write_group(&mut track, 1000, 1000); + let kept = lite::Range { + end, + ..lite::Range::span(Some(500), 0, None, None) + }; + let (mut run, mut writer, log) = + lite07_run_ranges(&track, &[lite::Range::span(Some(100), 0, None, None), kept]); + assert!(run.poll_step(&mut writer, &kio::Waiter::noop()).is_ready()); + assert!(run.start.resolved().is_none(), "time 100 waits on the copy to place it"); + + run.update(lite07_add(&run, &[(1, kept)], 2, &[])); + while run.poll_step(&mut writer, &kio::Waiter::noop()).is_ready() {} + let starts: Vec<_> = responses(&mut writer, &log) + .await + .into_iter() + .filter_map(|msg| match msg { + lite::SubscribeResponse::Start(start) => Some(start.starts), + _ => None, + }) + .collect(); + assert_eq!(starts, [vec![None, Some(1000)]], "ending at {end:?}"); + } + } + + /// As [`lite07_a_floored_time_start_reports_where_delivery_starts`], on a relay's front + /// whose copy already resolved a later range: that range names the group it resolved to, + /// whatever the floored one still waits on. + #[kio::test] + async fn lite07_a_routed_range_past_a_floored_time_start_reports_its_start() { + let mut routed = track::Routed::new("test", crate::track::Info::timed()); + routed.copy.received(); + routed.copy.anchored(); + write_group(&mut routed.copy, 1000, 1000); + let initial = [ + lite::Range::span(Some(100), 0, None, None), + lite::Range::span(Some(500), 0, None, None), + lite::Range::span(Some(1000), 0, None, None), + ]; + let bounds = Bounds::new(&initial, false); + let subscriber = routed + .logical + .consume() + .subscribe( + track::Subscription::default() + .with_delay(track::Delay::default().with_max(Duration::from_secs(3600))) + .with_ranges(bounds.ranges.iter().copied()), + ) + .now_or_never() + .expect("an accepted logical track answers at once") + .unwrap(); + let (mut run, mut writer, log) = lite07_run(SinkSession::new(Log::default()), subscriber); + run.initial = bounds.ranges.len() as u64; + run.requested = Requested::new((0..).zip(bounds.ranges.iter().copied())); + assert!(run.poll_step(&mut writer, &kio::Waiter::noop()).is_ready()); + assert!(run.start.resolved().is_none(), "time 100 waits on the copy to place it"); + + let live = [(1, initial[1]), (2, initial[2])]; + run.update(lite07_add(&run, &live, 3, &[])); + while run.poll_step(&mut writer, &kio::Waiter::noop()).is_ready() {} + // A range an update adds afterwards is told where it starts too. + run.update(lite07_add(&run, &live, 3, &[initial[2]])); + while run.poll_step(&mut writer, &kio::Waiter::noop()).is_ready() {} + let answers: Vec<_> = responses(&mut writer, &log) + .await + .into_iter() + .filter_map(|msg| match msg { + lite::SubscribeResponse::Start(start) => Some((None, start.starts)), + lite::SubscribeResponse::Started(started) => Some((Some(started.id), vec![started.start])), + _ => None, + }) + .collect(); + assert_eq!( + answers, + [(None, vec![None, Some(1000), Some(1000)]), (Some(3), vec![Some(1000)])] + ); + } + /// A gap the publisher never declared might have existed, so the end names it with a /// DROP rather than a SKIP: here the reader passed it, so it is stale. #[kio::test] diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index d3545cd896..0d980c5ebe 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -10144,7 +10144,9 @@ impl SubStream { /// Tell the copy where the publisher started each live range: one asked from a group's /// start, which a reader's time start there resolves to when the copy holds no group at /// it, and one counting back, which the copy, holding only part of the track, can't count. - /// Lite-07 only, where each range's start is reported. + /// One asked from the oldest group counts as asked from time 0, so its start places any + /// earlier time start a reader joining it asks. Lite-07 only, where each range's start is + /// reported. fn report_starts(&self, serving: &track::Producer) { if !self.live.reports_starts() { return; @@ -10157,6 +10159,7 @@ impl SubStream { }; match range.start { Some(lite::Anchor::Absolute(time)) if time.frame == 0 => times.push((time.group, *start)), + None => times.push((0, *start)), Some(lite::Anchor::Relative(back)) => { if let Some(Some(latest)) = self.edges.get(&id) { relative.push((back, *start, *latest)); diff --git a/rs/moq-net/src/model/resume.rs b/rs/moq-net/src/model/resume.rs index 4c9bd10f6f..6b4c5260ae 100644 --- a/rs/moq-net/src/model/resume.rs +++ b/rs/moq-net/src/model/resume.rs @@ -1171,6 +1171,32 @@ impl Subscriber { keys.into_iter().map(|key| window.resolved_key(key)).collect() } + /// The reader's ranges keyed in `keys` while their time starts wait; see + /// [`track::Subscriber::waiting_keys`]. + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + pub(super) fn waiting_keys(&self, keys: impl IntoIterator) -> Vec)>> { + let mut reader = self.reader(); + reader.window(); + let reader = &mut *reader; + // Only the serving copy places one: the reader's own window holds no groups. Its + // subscription keys each range as the reader does. + let indices: Vec> = keys + .into_iter() + .map(|key| match reader.window.resolved_key(key) { + Some(_) => None, + None => reader.mirrored.ranges.contains_key(&key).then_some(key), + }) + .collect(); + let mut found = match reader.copies.last_mut().map(|copy| &mut copy.sub) { + Some(Sub::Ready(sub)) => sub.waiting_keys(indices.iter().flatten().copied()).into_iter(), + _ => Vec::new().into_iter(), + }; + indices + .into_iter() + .map(|index| index.and_then(|_| found.next().flatten())) + .collect() + } + /// The lowest group at or above `from` the reader's ranges cover; see /// [`track::Subscriber::next_in_range`]. pub(super) fn next_in_range(&self, from: u64) -> Option { @@ -1721,8 +1747,8 @@ impl Reader { &self.window } - /// Place the unsettled [`Self::window`] ranges where a copy resolved them, unless one it - /// leaves unresolved, or a relative anchor, still waits there too. + /// Place each unsettled [`Self::window`] range where a copy resolved it, on its own: one + /// whose anchors still wait leaves the rest placed, so a publisher names their starts. fn resolve(&mut self, resolved: &BTreeMap) { let mut placed = Vec::new(); for key in self.window.unsettled() { @@ -1735,7 +1761,7 @@ impl Reader { && (matches!(range.start, Anchor::Relative(_) | Anchor::Time(1..)) || matches!(range.end, Some(Anchor::Relative(_)))) { - return; + continue; } // Placed as it is: nothing of it waits on a copy. placed.push((key, range)); @@ -1757,13 +1783,15 @@ impl Reader { Anchor::Relative(_) | Anchor::Time(_) => Anchor::Oldest, start => start, }; - placed.push((key, track::Range { start, end })); + let range = track::Range { start, end }; + // A relative end still open would leave it waiting on a first group it never gets. + if !range.is_empty() && matches!(range.end, Some(Anchor::Relative(_))) { + continue; + } + placed.push((key, range)); } - // A relative end still open leaves the window waiting on a first group it never gets. - if placed.iter().any(|(_, range)| { - !range.is_empty() - && (matches!(range.start, Anchor::Relative(_)) || matches!(range.end, Some(Anchor::Relative(_)))) - }) { + // An edit restarts the window's coverage check, so one that places nothing is skipped. + if placed.is_empty() { return; } self.window @@ -4086,6 +4114,91 @@ mod test { drop(held); } + /// A routed reader's time start that its serving copy can't place yet says what it waits + /// on and where it ends, as a plain reader's does, so a publisher whose floor already keeps + /// out the group containing it can say where the range starts. + #[kio::test] + async fn a_routed_reader_tells_a_waiting_time_start() { + let routes = Producer::new(); + let logical = logical(&routes); + let serving = received_copy(); + serving.anchored(); + routes.serve(serving.consume()); + write(&serving, 1000); + let ranges = Subscription::default() + .with_delay(Delay::default().with_max(None)) + .with_ranges([track::Range { + start: Anchor::Time(500), + end: None, + }]); + let mut open = logical + .consume() + .subscribe(ranges.clone()) + .now_or_never() + .unwrap() + .unwrap(); + assert!(open.poll_start(&kio::Waiter::noop()).is_pending()); + assert_eq!(open.resolved_range(0), None); + assert_eq!(open.waiting_keys([0]), [Some((500, None))]); + + // A floor at the time keeps out whichever group contains it: the start waits on nothing. + let mut floored = logical + .consume() + .subscribe(ranges) + .now_or_never() + .unwrap() + .unwrap() + .with_range(500..); + assert!(floored.poll_start(&kio::Waiter::noop()).is_ready()); + assert_eq!(floored.resolved_range(0), None); + assert_eq!(floored.waiting_keys([0]), [Some((500, None))]); + } + + /// A routed reader places each range its copy resolved, even while an earlier one waits on + /// a time start: a floor at that time lets the start go out, and the later range names its + /// group rather than none. + #[kio::test] + async fn a_routed_reader_places_a_range_past_one_still_waiting() { + let routes = Producer::new(); + let logical = logical(&routes); + let serving = received_copy(); + serving.anchored(); + routes.serve(serving.consume()); + write(&serving, 1000); + let ranges = Subscription::default() + .with_delay(Delay::default().with_max(None)) + .with_ranges([ + track::Range { + start: Anchor::Time(500), + end: None, + }, + track::Range { + start: Anchor::Time(1000), + end: None, + }, + ]); + let mut floored = logical + .consume() + .subscribe(ranges) + .now_or_never() + .unwrap() + .unwrap() + .with_range(500..); + assert!(floored.poll_start(&kio::Waiter::noop()).is_ready()); + assert_eq!(floored.waiting_keys([0, 1]), [Some((500, None)), None]); + assert_eq!( + floored.resolved_keys([0, 1]), + [ + None, + Some(Resolved { + start: 1000, + end: None, + floored: true, + }) + ] + ); + } + /// As [`an_ended_copy_leaves_a_time_start_to_the_serving_copy`], for a reader that replaces /// its ranges while its copy has failed: the replacement places them. #[kio::test] diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index 47c1a5ce3e..444aa35415 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -332,9 +332,12 @@ impl Window { !self.reaching.is_empty() } - /// Whether a time start still waits on the groups held to place it. - pub fn is_waiting(&self) -> bool { - !self.waiting.is_empty() + /// Whether a time start still waits on the groups held to place it, where the group + /// containing it may start above `floor`. One at or below the floor needs no placing: the + /// floor already keeps out every group but one starting there, and serves the first group + /// from it either way. + pub fn is_waiting_above(&self, floor: u64) -> bool { + self.waiting.last().is_some_and(|&(time, _)| time > floor) } /// Resolve what the groups now held settle: relative anchors once `latest` exists, and @@ -471,6 +474,14 @@ impl Window { }) } + /// The range keyed `key` while its time start waits on the groups held to place it: that + /// time, and the exclusive end it resolved to, `None` while it stays open. + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + pub fn waiting_key(&self, key: u64) -> Option<(u64, Option)> { + let span = self.spans.get(&key)?; + span.waits_on().map(|time| (time, span.end)) + } + /// Each range resolved, in key order, as [`Self::resolved_key`] reads it. pub fn resolved(&self) -> Vec> { self.ranges.keys().map(|key| self.resolved_key(*key)).collect() @@ -1037,7 +1048,12 @@ mod tests { window.runs(0).collect::>(), window.missable(0).collect::>(), (window.floor(), window.start(), window.named(), window.end()), - (window.resolved(), lookups, window.is_settled(), window.is_waiting()), + ( + window.resolved(), + lookups, + window.is_settled(), + !window.waiting.is_empty(), + ), ) } diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index e4413de5d3..f705d99915 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -754,6 +754,9 @@ pub(crate) struct TrackState { // a time it holds no group at, but its publisher reports where it resolved one. Replaced // whole as the ranges change, so it holds at most one entry per live range. time_starts: BTreeMap, + // The newest start in `time_starts` past the time its range asked from, kept as it is + // replaced: [`Holdings::unplaced`] reads it on every settle of every reader. + starts_past: Option, // Where the upstream publisher started each of a lite-07 copy's live wire ranges counting // back, by how many groups each counted, with the latest group it counted from: the copy // holds only part of the track, so it can't count back over its own groups. Replaced whole @@ -1130,9 +1133,20 @@ impl Holdings for TrackState { } } - /// Exactly the times [`Self::containing`] can't place, but those `time_starts` places. + /// Only times [`Self::containing`] can't place, but those `time_starts` places directly. + /// It leaves out the times below the newest start the publisher reported past an earlier + /// range's time: `containing_received` places those once no held group precedes them, + /// which an eviction brings about without changing the mark. A consistent publisher + /// reports its oldest group for each such range, so the run then still starts where its + /// unknown times do. fn unplaced(&self) -> Option { let none = Unplaced::NONE; + let past = self.starts_past.filter(|_| self.anchors == Anchors::Wire); + // The times below every held group that no inference from an earlier range places. + let unheld = |run: ops::RangeInclusive| match past { + Some(past) => (*run.start()).max(past)..=*run.end(), + None => run, + }; // The times at or after `from` whose keys from `from` through them drops do not all // cover. As in `dropped_cover`, the last time checks the keys below it only, so a run // from the last key covers nothing and leaves nothing unplaced. @@ -1147,7 +1161,7 @@ impl Holdings for TrackState { } else { match (self.lookup.first_key_value(), self.lookup.last_key_value()) { // Before any group, only a time whose keys down to 0 were all dropped is placed. - (None, _) | (_, None) => [uncovered(0), none], + (None, _) | (_, None) => [unheld(uncovered(0)), none], (Some(_), Some(_)) if self.anchors != Anchors::Wire => [none.clone(), none], // A copy places a time below its groups only once the keys down to 0 were // dropped, and one above them only once the keys down to the newest were. @@ -1156,7 +1170,7 @@ impl Holdings for TrackState { let below = *below.start()..=(*below.end()).min(first.saturating_sub(1)); let below = match first { 0 => none.clone(), - _ => below, + _ => unheld(below), }; let above = match last.checked_add(1) { Some(next) => uncovered(next), @@ -1272,6 +1286,14 @@ impl TrackState { /// or where the publisher reports starting the range it asked from `time`. A group past /// it says the feed went by: one between still in flight, a stream overtaken, would /// only add that group to the reader's range. + /// + /// With no group held at or before `time`, a range asked from an earlier time, or from + /// the oldest group (reported as time 0), that the publisher started past `time` says + /// the same: no group starts at or before it. A reader joining a copy that another + /// reader already asks from further back adds no range of its own, so that is the only + /// report its time start gets. A group the copy still holds there is a group the + /// publisher has since expired, so it wins: the time still waits to learn whether it + /// contains it. fn containing_received(&self, time: u64) -> Containing { if self.lookup.contains_key(&time) { return Containing::Group(time); @@ -1290,7 +1312,12 @@ impl TrackState { { Containing::Group(key) } - None if self.dropped_cover(0..time.saturating_add(1)) => Containing::Before, + None if self.dropped_cover(0..time.saturating_add(1)) + || (self.starts_past.is_some_and(|past| past > time) + && self.time_starts.range(..time).any(|(_, &start)| start > time)) => + { + Containing::Before + } _ => Containing::Unknown, } } @@ -4788,6 +4815,11 @@ impl Producer { if let Ok(mut state) = self.modify() && state.time_starts != starts { + state.starts_past = starts + .iter() + .filter(|&(asked, start)| asked < start) + .map(|(_, &start)| start) + .max(); state.time_starts = starts; state.placing += 1; } @@ -10867,7 +10899,8 @@ impl Subscriber { /// none: groups arrive newest first, so the first to arrive is not the oldest served. /// /// Pending too while a time start waits on the groups held to place it, since where its - /// range starts is unknown until then. + /// range starts is unknown until then, unless the floor sits at or past that time, as a + /// pre-lite-07 wire's start does: the group containing it is out of range then. #[cfg_attr(feature = "async", kio::wrapper)] pub fn poll_start(&mut self, waiter: &kio::Waiter) -> Poll> { let cursor = match &mut self.inner { @@ -10876,12 +10909,12 @@ impl Subscriber { }; let start = ready!(Consumer::poll_state_start(&cursor.state, waiter)); cursor.sync(); - if cursor.filter.window.is_waiting() { + if cursor.filter.window.is_waiting_above(cursor.min_key) { // Registered before syncing again, so a change placing it after this wakes it. A // closed track places it at once, since nothing more can arrive. let closed = cursor.state.poll(waiter, |_| Poll::<()>::Pending).is_ready(); cursor.sync(); - if !closed && cursor.filter.window.is_waiting() { + if !closed && cursor.filter.window.is_waiting_above(cursor.min_key) { return Poll::Pending; } } @@ -10929,6 +10962,22 @@ impl Subscriber { } } + /// This subscriber's ranges keyed in `keys` while their time starts wait on the groups + /// held to place them, or on its route copies for a reader following them: each one's + /// time, and the exclusive end it resolved to. `None` for one that resolved. + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + pub(crate) fn waiting_keys(&mut self, keys: impl IntoIterator) -> Vec)>> { + match &mut self.inner { + Inner::Plain(cursor) => { + cursor.sync(); + keys.into_iter() + .map(|key| cursor.filter.window.waiting_key(key)) + .collect() + } + Inner::Resume(resume, _) => resume.waiting_keys(keys), + } + } + /// This subscriber's range keyed `key` as resolved here; see [`Self::resolved_keys`]. #[cfg(test)] #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] @@ -12409,12 +12458,14 @@ mod test { } proptest::proptest! { - /// The times a track reports unplaced are exactly those its `containing` can't place, - /// but for the ones a copy's reported time starts place, on its own groups or on a - /// lite-07 copy's, through groups arriving and every way keys are given up. + /// The times a track reports unplaced are only those its `containing` can't place, but + /// for the ones a copy's reported time starts place, after each step: through groups + /// arriving and leaving, and every way keys are given up. A time it leaves out lies below + /// the newest start reported past an earlier range's time, which may place it once no held + /// group precedes it. #[test] - fn unplaced_matches_containing( - ops in proptest::collection::vec((0u8..5, 0u64..40, 1u64..8), 0..24), + fn unplaced_holds_only_what_containing_cant_place( + ops in proptest::collection::vec((0u8..6, 0u64..40, 1u64..8), 0..24), wire: bool, starts in proptest::collection::vec((0u64..48, 0u64..48), 0..3), ) { @@ -12423,6 +12474,25 @@ mod test { producer.anchored(); producer.time_starts(starts.clone()); } + let reported: BTreeSet = starts.iter().map(|(time, _)| *time).collect(); + let past = starts.iter().filter(|(asked, start)| asked < start).map(|(_, start)| *start).max(); + let check = |producer: &Producer| -> std::result::Result<(), proptest::test_runner::TestCaseError> { + let state = producer.state.read(); + let unplaced = state.unplaced().expect("a track always tells"); + for time in (0..64).chain([u64::MAX - 1, u64::MAX]) { + let inside = unplaced.runs.iter().any(|run| run.contains(&time)); + let unknown = state.containing(time) == Containing::Unknown; + if wire && reported.contains(&time) { + proptest::prop_assert!(!unknown, "a reported start places {}", time); + } else if inside { + proptest::prop_assert!(unknown, "{} is unplaced but placed", time); + } else if unknown { + proptest::prop_assert!(wire && past.is_some_and(|past| time < past), "{} is left out", time); + } + } + Ok(()) + }; + check(&producer)?; for (op, start, len) in ops { let groups = start..start + len; match op { @@ -12430,20 +12500,17 @@ mod test { 1 => producer.skip_groups(groups), 2 => drop(producer.create_group(group::Info { start })), 3 => producer.modify().unwrap().forget_drops(groups), + // Evict the first group held from `start`, as expiry does. + 4 => { + let held = producer.state.read().lookup.range(start..).next().map(|(&key, _)| key); + if let Some(key) = held { + producer.modify().unwrap().evict_group(key, Error::Evicted); + } + } // A group at the last key that can start one, past every time but the last. _ => drop(producer.create_group(group::Info { start: u64::MAX - 1 })), } - } - let state = producer.state.read(); - let unplaced = state.unplaced().expect("a track always tells"); - let reported: BTreeSet = starts.iter().map(|(time, _)| *time).collect(); - for time in (0..64).chain([u64::MAX - 1, u64::MAX]) { - let inside = unplaced.runs.iter().any(|run| run.contains(&time)); - let unknown = state.containing(time) == Containing::Unknown; - match wire && reported.contains(&time) { - true => proptest::prop_assert!(!unknown, "a reported start places {}", time), - false => proptest::prop_assert_eq!(inside, unknown, "at {}", time), - } + check(&producer)?; } } @@ -15765,6 +15832,137 @@ mod test { assert_eq!(drain(&mut past), vec![1000, 3000]); } + /// A reader joining a lite-07 copy that another reader already asks from further back adds + /// no range of its own, so its publisher reports no start for the reader's time. The start + /// of a range asked from earlier, or from the oldest group, places it: past the time, no + /// group starts at or before it, and a publisher serving the reader learns where delivery + /// starts. At or before the time, a group between may still arrive. + #[test] + #[cfg(feature = "async")] + fn a_received_copy_places_a_time_start_by_an_earlier_range() { + let from = |time| { + Subscription::default() + .with_ranges([Range { + start: Anchor::Time(time), + end: None, + }]) + .with_delay(Delay::default().with_max(None)) + }; + let placed = |subscriber: &mut Subscriber| { + subscriber.resolved_ranges([0])[0].map(|resolved| (resolved.start, resolved.floored)) + }; + let producer = track_producer("test", Info::timed()); + producer.received(); + producer.anchored(); + write_group(&producer, 1000); + + let mut before = producer.subscribe(from(500)); + let mut after = producer.subscribe(from(1500)); + assert_eq!(placed(&mut before), None); + assert!(before.poll_start(&kio::Waiter::noop()).is_pending()); + + // Only a range asked from a later time: says nothing of 500. + producer.time_starts([(700, 1000)]); + assert_eq!(placed(&mut before), None); + + // The range asked from the oldest group started at 1000. + producer.time_starts([(0, 1000)]); + assert_eq!(placed(&mut before), Some((0, false))); + assert!(before.poll_start(&kio::Waiter::noop()).is_ready()); + assert_eq!(drain(&mut before), vec![1000]); + assert_eq!(placed(&mut after), None, "a group past 1000 may still arrive"); + } + + /// A group the copy still holds at or before a time start outranks a start reported for an + /// earlier range: the publisher expiring it says nothing of whether it contains the time. + #[test] + #[cfg(feature = "async")] + fn a_received_copy_keeps_a_held_group_over_an_earlier_range() { + let from = |time| { + Subscription::default() + .with_ranges([Range { + start: Anchor::Time(time), + end: None, + }]) + .with_delay(Delay::default().with_max(None)) + }; + let placed = |subscriber: &mut Subscriber| { + subscriber.resolved_ranges([0])[0].map(|resolved| (resolved.start, resolved.floored)) + }; + let producer = track_producer("test", Info::timed()); + producer.received(); + producer.anchored(); + write_group(&producer, 2000); + + let mut reader = producer.subscribe(from(2500)); + producer.time_starts([(0, 3000)]); + assert_eq!(placed(&mut reader), None, "the held 2000 may contain 2500"); + + write_group(&producer, 3000); + assert_eq!(placed(&mut reader), Some((2000, true))); + assert_eq!(drain(&mut reader), vec![2000, 3000]); + } + + /// Once the copy evicts the group it held before a time start, the start of a range asked + /// from earlier places the time: no reported start changed, but the waiting reader still + /// starts. + #[test] + #[cfg(feature = "async")] + fn a_received_copy_places_a_time_start_once_the_group_before_it_is_evicted() { + let from = |time| { + Subscription::default() + .with_ranges([Range { + start: Anchor::Time(time), + end: None, + }]) + .with_delay(Delay::default().with_max(None)) + }; + let placed = |subscriber: &mut Subscriber| { + subscriber.resolved_ranges([0])[0].map(|resolved| (resolved.start, resolved.floored)) + }; + let producer = track_producer("test", Info::timed()); + producer.received(); + producer.anchored(); + write_group(&producer, 2000); + + let mut reader = producer.subscribe(from(2500)); + producer.time_starts([(0, 3000)]); + assert_eq!(placed(&mut reader), None, "the held 2000 may contain 2500"); + + producer.evict(2000); + write_group(&producer, 3000); + assert_eq!(placed(&mut reader), Some((0, false))); + assert_eq!(drain(&mut reader), vec![3000]); + } + + /// A floor at or past a time start, as a pre-lite-07 wire's start sets, keeps out the group + /// containing that time unless it starts there, so where the range starts waits on nothing + /// a lite-07 copy has yet to place. Without the floor, it waits. + #[test] + #[cfg(feature = "async")] + fn a_floor_at_a_time_start_waits_on_no_placing() { + let from = |time| { + Subscription::default() + .with_ranges([Range { + start: Anchor::Time(time), + end: None, + }]) + .with_delay(Delay::default().with_max(None)) + }; + let producer = track_producer("test", Info::timed()); + producer.received(); + producer.anchored(); + write_group(&producer, 1000); + + let mut open = producer.subscribe(from(500)); + assert!(open.poll_start(&kio::Waiter::noop()).is_pending()); + let mut floored = producer.subscribe(from(500)).with_range(500..); + assert!(floored.poll_start(&kio::Waiter::noop()).is_ready()); + let mut below = producer.subscribe(from(500)).with_range(499..); + assert!(below.poll_start(&kio::Waiter::noop()).is_pending()); + assert_eq!(drain(&mut floored), vec![1000]); + } + /// A lite-07 copy holds only part of the track, so a relative start counts back as its /// publisher reported, not over the groups the copy holds, until a group past the latest /// the publisher counted from moves the live edge. diff --git a/rs/moq-net/tests/integration/main.rs b/rs/moq-net/tests/integration/main.rs index 48b3485359..67fccffef8 100644 --- a/rs/moq-net/tests/integration/main.rs +++ b/rs/moq-net/tests/integration/main.rs @@ -83,6 +83,7 @@ mod session_close; mod session_panic; mod settled_end_survives_abort; mod setup_negotiation; +mod shared_copy_start; mod slow_joiner; mod source_errors; mod stream_credit; diff --git a/rs/moq-net/tests/integration/shared_copy_start.rs b/rs/moq-net/tests/integration/shared_copy_start.rs new file mode 100644 index 0000000000..40443ffc56 --- /dev/null +++ b/rs/moq-net/tests/integration/shared_copy_start.rs @@ -0,0 +1,114 @@ +#![cfg(feature = "async")] + +//! A relay's subscriber asking from a time before the copy it joins is fed. +//! +//! The relay pulls the track over lite-07 for a reader of its own, from the oldest group +//! its budget keeps, so its copy starts at the live edge. A downstream peer then subscribes +//! from an earlier group, as an older relay resuming onto this one does, after the publisher +//! expired everything there. Its range adds nothing to what the copy already asks, so the +//! publisher never reports where a range from that time starts, and the group containing +//! it never arrives: the subscription must still start, at the oldest group the copy holds. + +use std::time::Duration; + +use moq_net::track::{Anchor, Delay, Info, Range, Subscription}; +use moq_net::{Timescale, Timestamp, Version, origin}; + +use crate::route_change::{link, produce_origin, settle}; + +const TIMEOUT: Duration = Duration::from_secs(10); + +/// One group every 100ms, keyed by its start in milliseconds. +const STEP: u64 = 100; + +fn write(track: &moq_net::track::Producer, key: u64) { + let mut group = track.create_group(moq_net::group::Info { start: key }).unwrap(); + group + .write_frame(Timestamp::from_millis(key).unwrap(), &b"x"[..]) + .unwrap(); + group.finish().unwrap(); +} + +/// Write the groups `keys` in real time. +async fn play(track: &moq_net::track::Producer, keys: std::ops::Range) { + for key in keys.step_by(STEP as usize) { + write(track, key); + kio::clock::sleep(Duration::from_millis(STEP)).await; + } +} + +/// The groups a downstream subscriber over `version` reads when it asks from 500, long +/// expired at the publisher, while the relay's own reader already holds the copy. +async fn read_from_expired(version: &str) -> Vec { + let publisher = produce_origin(1); + let relay = produce_origin(2); + let downstream = produce_origin(3); + + let broadcast = publisher.route("live", origin::RouteOptions::hidden()).unwrap(); + let track = broadcast + .broadcast() + .create_track( + "video", + Info::default() + .with_timescale(Timescale::MILLI) + .with_max_age(Some(Duration::from_millis(500))), + ) + .unwrap(); + broadcast.announce().unwrap(); + play(&track, 0..1000).await; + + // The relay's own reader pulls the copy from the live edge. + let _up = link("moq-lite-07-wip".parse().unwrap(), &publisher, &relay).await; + let local = kio::clock::timeout(TIMEOUT, relay.consume().follow("live", None).unwrap().available()) + .await + .unwrap() + .unwrap() + .unwrap(); + let _local = local + .track("video") + .unwrap() + .subscribe(Subscription::default()) + .await + .unwrap(); + play(&track, 1000..2000).await; + + let _down = link(version.parse::().unwrap(), &relay, &downstream).await; + let remote = kio::clock::timeout(TIMEOUT, downstream.consume().follow("live", None).unwrap().available()) + .await + .unwrap() + .unwrap() + .unwrap(); + let subscription = Subscription::default() + .with_delay(Delay::default().with_max(Duration::from_secs(60))) + .with_ranges([Range { + start: Anchor::Time(500), + end: None, + }]); + let mut sub = remote.track("video").unwrap().subscribe(subscription).await.unwrap(); + play(&track, 2000..3000).await; + settle().await; + + let mut read = Vec::new(); + while let Ok(Ok(Some(group))) = kio::clock::timeout(Duration::from_millis(1), sub.recv_group()).await { + read.push(group.start); + } + read.sort_unstable(); + read +} + +#[kio::test] +async fn an_older_start_on_a_shared_copy_is_fed() { + for version in ["moq-lite-06", "moq-lite-07-wip"] { + let read = read_from_expired(version).await; + // Everything from the oldest group the relay still holds, through the live edge. + assert_eq!( + read.last(), + Some(&2900), + "{version}: fed to the live edge, read {read:?}" + ); + assert!( + read.windows(2).all(|pair| pair[1] - pair[0] == STEP), + "{version}: no gap once started, read {read:?}" + ); + } +} diff --git a/test/mesh-replay/script.toml b/test/mesh-replay/script.toml index 701e274ae8..90f2a9c9a2 100644 --- a/test/mesh-replay/script.toml +++ b/test/mesh-replay/script.toml @@ -45,19 +45,23 @@ kind = "stuck" event = "roll" quest = "/quest/m3/release/cut.md" -# Mid-roll, a new relay accepts an old neighbour's subscribe that arrives as the neighbour -# withdraws its echo of the broadcast, then forwards nothing. `half-roll.toml` reproduces it -# and lists nothing known. +# On lite-07 alone, a front that switched copies after a reopened link or a crashed relay can +# deliver a group only every 15-25s on an unchanged route. Not on a roll, so a roll's starved +# reader still fails. `reopen.toml` reproduces it and lists nothing known. [[known]] check = "unfed" kind = "starved" -event = "roll" -quest = "/quest/m2/multicast/roll-echo-stall.md" +event = "reopen-link" +quest = "/quest/m2/multicast/splice-stall.md" + +[[known]] +check = "unfed" +kind = "starved" +event = "reopen-relay" +quest = "/quest/m2/multicast/splice-stall.md" -# On lite-07 alone, a front that switched copies after a reopened link or a crashed relay can -# deliver a group only every 15-25s on an unchanged route. After the roll entry, so a roll's -# starved reader stays with roll-echo-stall. `reopen.toml` reproduces it and lists nothing known. [[known]] check = "unfed" kind = "starved" +event = "kill" quest = "/quest/m2/multicast/splice-stall.md"