From 53b06aadd1c75691adc2d71f57ba007e2161dc00 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 27 Oct 2026 16:48:57 -0700 Subject: [PATCH 01/30] chore(quest): claim quest/m1/subscribe-ranges/lite.md Quest-Claim: 63fd64bf1a64cb17 From 66c7317cfdf28e4272e51889ddaf369a21c71d9e Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 27 Oct 2026 17:02:20 -0700 Subject: [PATCH 02/30] feat(net): lite-07 SUBSCRIBE_OK names each range's resolved start Co-Authored-By: Claude Opus 5.5 --- drafts/draft-lcurley-moq-lite.md | 12 +++++- js/net/src/lite/publisher.ts | 10 ++++- js/net/src/lite/subscribe.test.ts | 15 ++++++- js/net/src/lite/subscribe.ts | 47 ++++++++++++++++++++-- rs/moq-net/src/coding/writer.rs | 1 + rs/moq-net/src/fuzz.rs | 1 + rs/moq-net/src/lite/publisher.rs | 65 ++++++++++++++++++++++++++++--- rs/moq-net/src/lite/subscribe.rs | 49 ++++++++++++++++++++++- rs/moq-net/src/lite/subscriber.rs | 13 +++++++ rs/moq-net/src/model/resume.rs | 6 +++ rs/moq-net/src/model/track.rs | 16 ++++++++ 11 files changed, 219 insertions(+), 16 deletions(-) diff --git a/drafts/draft-lcurley-moq-lite.md b/drafts/draft-lcurley-moq-lite.md index 49a2714db34..871885c9501 100644 --- a/drafts/draft-lcurley-moq-lite.md +++ b/drafts/draft-lcurley-moq-lite.md @@ -1493,7 +1493,7 @@ The name a TRACKS_ADD on the stream listed. A SUBSCRIBE_OK message confirms a subscription and resolves its absolute start position. It is the first message the publisher sends on the Subscribe Stream, once the start position is known. -This is the trimmed-down counterpart of MoqTransport's SUBSCRIBE_OK: it retains the name and the role of the publisher's positive response, but carries only the resolved start position and the publisher's largest position (all other per-track properties live in [TRACK_INFO](#track-info)). +This is the trimmed-down counterpart of MoqTransport's SUBSCRIBE_OK: it retains the name and the role of the publisher's positive response, but carries only the resolved start position, the publisher's largest position, and where each Range starts (all other per-track properties live in [TRACK_INFO](#track-info)). ~~~ SUBSCRIBE_OK Message { @@ -1502,6 +1502,8 @@ SUBSCRIBE_OK Message { Group (i) Largest Group (i) [Largest Frame (i)] + Range Start Count (i) + Range Start (i) ... } ~~~ @@ -1531,6 +1533,13 @@ The largest position the publisher knows of for the track when it answers: the l `Largest Group` is the group sequence plus one, and 0 means the track has nothing yet, in which case `Largest Frame` is absent. A subscriber takes it as where the live feed is: a relay holding groups from an earlier subscription serves them from cache only once this says they are current, which a publisher answering promptly lets it do without waiting for a frame. +**Range Start**: +One per Range the publisher is serving, in order: the Group its `Start` resolved to, plus one, so a subscriber learns where a Relative or Time Anchor landed. +0 means the Range is empty or its `Start` has not resolved. +A `Start` that reaches back to the oldest Group resolves to Group 0. +Unlike `Group`, it is the Range's floor: a Group between it and `Group` that arrives later is in range. +The Ranges are those of the SUBSCRIBE unless the publisher applied a SUBSCRIBE_UPDATE before answering; a subscriber that sent none MUST treat a count other than its number of Ranges as a PROTOCOL_VIOLATION. + ## SUBSCRIBE_END {#subscribe-end} A SUBSCRIBE_END message is sent by the publisher to signal that no group at or after a given sequence will be produced. @@ -1859,6 +1868,7 @@ The `Message Length` describes the payload size on the wire. - Listed the shared INVALID_REQUEST_ID (0x4) session code used by moq-transport drafts 14 through 16. - Listed the shared EXPIRED_AUTH_TOKEN (0x18) session code. A request that expires on its own resets with the UNAUTHORIZED stream code. +- Added `Range Start` to SUBSCRIBE_OK: the Group each Range's `Start` resolved to, including a Relative or Time Anchor. - SUBSCRIBE_OK `Group` names where delivery starts when the subscription resolves, which need not be the first group sent. It is not a new floor. A subscriber does not wait for a group between the requested floor and it, but a publisher still delivers one that arrives later within Subscriber Max Age. - Replaced `Group Start`, `Group End`, `Frame Start`, and `Frame End` in SUBSCRIBE and SUBSCRIBE_UPDATE with a non-empty list of Ranges. Each end is an Anchor: none, an absolute Position (exclusive as an end), a count of Groups back from the latest, or a presentation time. - A subscription's range bounds datagrams like groups, and FETCH never returns a datagram. diff --git a/js/net/src/lite/publisher.ts b/js/net/src/lite/publisher.ts index f7f03b36f04..28f866b40c8 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -963,6 +963,7 @@ export class Publisher { served, floor, bounds, + rangeStarts: msg.rangeStarts ?? [], }); console.debug(`publish done: broadcast=${msg.broadcast} track=${track.name}`); @@ -1127,11 +1128,15 @@ export class Publisher { // The read cursor's floor; see {@link cursorFloor}. floor: number; bounds: FrameBounds; + // Each lite-07 range's start, which SUBSCRIBE_START answers for. This publisher + // resolves no relative or time anchor, so only an absolute start is known. + rangeStarts: (number | undefined)[]; }, ): Promise { const { sub, broadcast, stamp, publisher, compress, served, bounds } = serving; // The read cursor's floor, assigned with the cap in {@link bounds} on every move. let floor = serving.floor; + let rangeStarts = serving.rangeStarts; const moveCursor = () => track.withRange({ start: { included: floor }, @@ -1267,6 +1272,7 @@ export class Publisher { bounds.startFrame = update.startFrame; bounds.endGroup = update.endGroup; bounds.endFrame = update.endFrame; + rangeStarts = update.rangeStarts ?? []; moveCursor(); // A microtask is too short: decoding one framed update crosses several // awaits, each of which can requeue behind the serving continuation. A @@ -1314,7 +1320,7 @@ export class Publisher { startSent = true; startSequence = bounds.startGroup; raiseFloor(bounds.startGroup); - const start = new SubscribeStart(bounds.startGroup, largest); + const start = new SubscribeStart(bounds.startGroup, largest, rangeStarts); if ( !(await controls.response(encodeSubscribeResponse(stream, { start }, this.version))) ) @@ -1397,7 +1403,7 @@ export class Publisher { !(await controls.response( encodeSubscribeResponse( stream, - { start: new SubscribeStart(startSequence, largest) }, + { start: new SubscribeStart(startSequence, largest, rangeStarts) }, this.version, ), )) diff --git a/js/net/src/lite/subscribe.test.ts b/js/net/src/lite/subscribe.test.ts index 08f1e66ae99..87bb7d84c0f 100644 --- a/js/net/src/lite/subscribe.test.ts +++ b/js/net/src/lite/subscribe.test.ts @@ -225,9 +225,9 @@ test("SubscribeStart round-trips on draft-05", async () => { }); test("SubscribeStart carries the largest position on draft-07", async () => { - // Type, length, group, largest group + 1, largest frame. + // Type, length, group, largest group + 1, largest frame, no range starts. expect(await encode(Version.DRAFT_07, { start: new SubscribeStart(4, { group: 3, frame: 2 }) })).toEqual( - new Uint8Array([0, 3, 4, 4, 2]), + new Uint8Array([0, 4, 4, 4, 2, 0]), ); for (const largest of [undefined, { group: 3, frame: 2 }]) { const got = await responseRoundtrip(Version.DRAFT_07, { start: new SubscribeStart(4, largest) }); @@ -240,6 +240,17 @@ test("SubscribeStart carries the largest position on draft-07", async () => { ); }); +test("SubscribeStart carries each range's resolved start on draft-07", async () => { + const start = new SubscribeStart(4, undefined, [2, undefined, 0]); + // Type, length, group, no largest, three starts each plus one with 0 for none. + expect(await encode(Version.DRAFT_07, { start })).toEqual(new Uint8Array([0, 6, 4, 0, 3, 3, 0, 1])); + const got = await responseRoundtrip(Version.DRAFT_07, { start }); + if (!("start" in got)) throw new Error("expected start"); + expect(got.start.starts).toEqual([2, undefined, 0]); + // Draft-06 has no range starts on the wire. + expect(await encode(Version.DRAFT_06, { start })).toEqual(new Uint8Array([0, 1, 4])); +}); + test("SubscribeEnd round-trips on draft-05", async () => { // Type, length, exclusive final group. expect(await encode(Version.DRAFT_05, { end: new SubscribeEnd(7) })).toEqual(new Uint8Array([1, 1, 7])); diff --git a/js/net/src/lite/subscribe.ts b/js/net/src/lite/subscribe.ts index 79599498a23..b04bb6c040f 100644 --- a/js/net/src/lite/subscribe.ts +++ b/js/net/src/lite/subscribe.ts @@ -118,6 +118,11 @@ interface Span { startFrame: number; endGroup?: number; endFrame?: number; + /** + * Lite-07's start group of each range in turn, `undefined` for one that does not name a + * group. Decoded only, so SUBSCRIBE_START can answer for every range. + */ + rangeStarts?: (number | undefined)[]; } /** @@ -202,9 +207,12 @@ async function decodeSpan(r: Reader, version: Version): Promise { let end: Point | undefined; let floor = true; let live = false; + // Not preallocated: each range costs at least two bytes of the bounded message. + const rangeStarts: (number | undefined)[] = []; for (let i = 0; i < count; i++) { const from = await decodeAnchor(r); const to = await decodeAnchor(r); + rangeStarts.push(from?.[0]); if (from === undefined) floor = false; else if (start === undefined || before(from, start)) start = from; if (to === undefined) live = true; @@ -221,6 +229,7 @@ async function decodeSpan(r: Reader, version: Version): Promise { startFrame, endGroup: end === undefined ? undefined : end[1] === 0 ? end[0] - 1 : end[0], endFrame: end === undefined || end[1] === 0 ? undefined : end[1] - 1, + rangeStarts, }; } @@ -318,6 +327,9 @@ export class SubscribeUpdate { /** See {@link Subscribe.endFrame}. */ endFrame?: number; + /** See {@link Subscribe.rangeStarts}. */ + rangeStarts?: (number | undefined)[]; + constructor(props: { priority: number; wantedGroup?: number; @@ -327,6 +339,7 @@ export class SubscribeUpdate { endGroup?: number; startFrame?: number; endFrame?: number; + rangeStarts?: (number | undefined)[]; }) { if (props.wantedGroup !== undefined && (!Number.isSafeInteger(props.wantedGroup) || props.wantedGroup < 0)) throw new RangeError("wanted group must be a safe non-negative integer"); @@ -338,6 +351,7 @@ export class SubscribeUpdate { this.endGroup = props.endGroup; this.startFrame = props.startFrame ?? 0; this.endFrame = props.endFrame; + this.rangeStarts = props.rangeStarts; } async #encode(w: Writer, version: Version) { @@ -427,6 +441,12 @@ export class Subscribe { */ endFrame?: number; + /** + * The start group of each lite-07 range in turn, `undefined` for one that names none. + * Decoded only, so SUBSCRIBE_START can answer for every range. + */ + rangeStarts?: (number | undefined)[]; + constructor(props: { id: bigint; broadcast: Path.Valid; @@ -439,6 +459,7 @@ export class Subscribe { endGroup?: number; startFrame?: number; endFrame?: number; + rangeStarts?: (number | undefined)[]; }) { this.id = props.id; this.broadcast = props.broadcast; @@ -451,6 +472,7 @@ export class Subscribe { this.endGroup = props.endGroup; this.startFrame = props.startFrame ?? 0; this.endFrame = props.endFrame; + this.rangeStarts = props.rangeStarts; } async #encode(w: Writer, version: Version) { @@ -617,9 +639,16 @@ export class SubscribeStart { */ largest?: Position; - constructor(group: number, largest?: Position) { + /** + * The group each range's start resolved to, in turn, `undefined` for one that is empty + * or unresolved. Draft-07+ only; empty before. + */ + starts: (number | undefined)[]; + + constructor(group: number, largest?: Position, starts: (number | undefined)[] = []) { this.group = group; this.largest = largest; + this.starts = starts; } async encode(w: Writer, version: Version): Promise { @@ -633,6 +662,9 @@ export class SubscribeStart { await w.u53(this.largest.group + 1); await w.u53(this.largest.frame); } + if (!hasRanges(version)) return; + await w.u53(this.starts.length); + for (const start of this.starts) await w.u53(start === undefined ? 0 : start + 1); }); } @@ -640,9 +672,16 @@ export class SubscribeStart { return Message.decode(r, async (r) => { const group = await r.u53(); if (!hasLargest(version)) return new SubscribeStart(group); - const largest = await r.u53(); - if (largest === 0) return new SubscribeStart(group); - return new SubscribeStart(group, { group: largest - 1, frame: await r.u53() }); + const wire = await r.u53(); + const largest = wire === 0 ? undefined : { group: wire - 1, frame: await r.u53() }; + if (!hasRanges(version)) return new SubscribeStart(group, largest); + // Not preallocated: each start costs a byte of the bounded message. + const starts: (number | undefined)[] = []; + for (let count = await r.u53(); count > 0; count--) { + const start = await r.u53(); + starts.push(start === 0 ? undefined : start - 1); + } + return new SubscribeStart(group, largest, starts); }); } } diff --git a/rs/moq-net/src/coding/writer.rs b/rs/moq-net/src/coding/writer.rs index ee05770cead..71cf50c1092 100644 --- a/rs/moq-net/src/coding/writer.rs +++ b/rs/moq-net/src/coding/writer.rs @@ -466,6 +466,7 @@ mod tests { .buffer(&lite::SubscribeStart { group: 3, largest: None, + starts: Vec::new(), }) .unwrap(); writer.buffer(&lite::AnnounceBroadcast::Synced).unwrap(); diff --git a/rs/moq-net/src/fuzz.rs b/rs/moq-net/src/fuzz.rs index 34560d57f46..93cccc28316 100644 --- a/rs/moq-net/src/fuzz.rs +++ b/rs/moq-net/src/fuzz.rs @@ -814,6 +814,7 @@ impl Default for Messages { lite_start: lite::SubscribeResponse::Start(lite::SubscribeStart { group: 1_234, largest: None, + starts: vec![Some(1_000)], }), lite_info: lite::TrackInfo { refresh: None, diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 37541e01408..9bf32ec44e1 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -4082,6 +4082,7 @@ impl TrackRun { writer.buffer(&lite::SubscribeResponse::Start(lite::SubscribeStart { group: start, largest: Some(largest), + starts: self.starts(), }))?; self.raise_floor(start); return Poll::Ready(Ok(ControlFlow::Continue(()))); @@ -4301,6 +4302,14 @@ impl TrackRun { } impl TrackRun { + /// Where each requested range starts, on versions whose SUBSCRIBE_START says. + fn starts(&mut self) -> Vec> { + match self.ctx.version.has_ranges() { + true => self.track.starts(), + false => Vec::new(), + } + } + /// Send SUBSCRIBE_START for the first servable group, then serve it. /// /// A relay caches groups in upstream arrival order, and a newer group's stream can @@ -4337,6 +4346,7 @@ impl TrackRun { writer.buffer(&lite::SubscribeResponse::Start(lite::SubscribeStart { group: start, largest, + starts: self.starts(), }))?; // The subscriber now owes itself everything from the start, so a range given up // above it is named as it would have been after the start. @@ -6084,6 +6094,48 @@ mod serve_group_test { .await; } + /// SUBSCRIBE_START tells the subscriber where each range's start landed, which a relative + /// or time start leaves it no way to work out from the groups it receives. + #[moq_clock::test] + async fn lite07_start_names_each_resolved_range_start() { + use crate::track::{Anchor, Range}; + + let mut track = track::Producer::new( + Arc::new(broadcast::Info::default()), + "test", + crate::track::Info::timed(), + ); + for sequence in 0..5 { + write_group(&mut track, sequence, sequence * 1000); + } + let at = |millis| Anchor::Time(Timestamp::from_millis(millis).unwrap()); + let subscriber = track.subscribe( + track::Subscription::default() + .with_max_delay(Duration::from_secs(3600)) + .with_ranges([ + Range { + start: Anchor::Relative(1), + end: None, + }, + Range { + start: at(1500), + end: Some(Anchor::Absolute(2)), + }, + // Empty, so it starts nowhere. + Range::groups(3..3), + ]), + ); + let (mut run, mut writer, log) = lite07_run(SinkSession::new(Log::default()), subscriber); + let mut run = std::pin::pin!(kio::wait(move |waiter| run.poll(&mut writer, waiter))); + assert!(futures::poll!(run.as_mut()).is_pending()); + + let writes = log.writes.lock().unwrap().clone(); + match lite::SubscribeResponse::decode_slice(&writes, Version::Lite07).unwrap().0 { + lite::SubscribeResponse::Start(start) => assert_eq!(start.starts, [Some(3), Some(1), None]), + other => panic!("expected SUBSCRIBE_START, got {other:?}"), + } + } + /// A skipped sequence is accounted for by DROP rather than a stream count. #[moq_clock::test] async fn lite07_end_drops_skipped_sequences() { @@ -6102,9 +6154,12 @@ mod serve_group_test { track.finish().unwrap(); run.await.unwrap(); - // SUBSCRIBE_START at 0, a DROP for the skipped group 1 with NOT_FOUND (0x33), which - // the end gives up on at once, and SUBSCRIBE_END at 3. - assert_eq!(*log.writes.lock().unwrap(), [0, 3, 0, 1, 0, 2, 3, 1, 1, 0x33, 1, 1, 3]); + // SUBSCRIBE_START at 0 with the range resolved there, a DROP for the skipped group 1 + // with NOT_FOUND (0x33), which the end gives up on at once, and SUBSCRIBE_END at 3. + assert_eq!( + *log.writes.lock().unwrap(), + [0, 5, 0, 1, 0, 1, 1, 2, 3, 1, 1, 0x33, 1, 1, 3] + ); } /// A relay that holds only the first group of a backlog answers with the largest its @@ -7240,7 +7295,7 @@ mod serve_group_test { assert_eq!( *log.writes.lock().unwrap(), // The DROP carries OLD (0x34). - [0, 3, 0, 1, 0, 2, 3, 0, 0, 0x34, 1, 1, 2], + [0, 5, 0, 1, 0, 1, 1, 2, 3, 0, 0, 0x34, 1, 1, 2], "the expired group is dropped and the end precedes stream credit" ); @@ -7250,7 +7305,7 @@ mod serve_group_test { *open = true; drop(open); run.await.unwrap(); - assert_eq!(*log.writes.lock().unwrap(), [0, 3, 0, 1, 0, 2, 3, 0, 0, 0x34, 1, 1, 2]); + assert_eq!(*log.writes.lock().unwrap(), [0, 5, 0, 1, 0, 1, 1, 2, 3, 0, 0, 0x34, 1, 1, 2]); } } diff --git a/rs/moq-net/src/lite/subscribe.rs b/rs/moq-net/src/lite/subscribe.rs index 51e5cc53069..4cdfcaec8cf 100644 --- a/rs/moq-net/src/lite/subscribe.rs +++ b/rs/moq-net/src/lite/subscribe.rs @@ -492,6 +492,9 @@ pub struct SubscribeStart { /// The publisher's largest (group, frame) when it answered, `None` for a track with /// nothing yet. Lite07+ only; not on the wire before, where it decodes as `None`. pub largest: Option, + /// The group each range's start resolved to, by index, `None` for one that is empty or + /// unresolved. Lite07+ only; empty before. + pub starts: Vec>, } impl Message for SubscribeStart { @@ -512,7 +515,14 @@ impl Message for SubscribeStart { }, false => None, }; - Ok(Self { group, largest }) + // Not preallocated: each start costs a byte of the bounded message. + let mut starts = Vec::new(); + if version.has_ranges() { + for _ in 0..r.varint()? { + starts.push(r.varint_opt()?); + } + } + Ok(Self { group, largest, starts }) } fn encode_msg(&self, w: &mut Encoder<'_>, version: Version) -> Result<(), EncodeError> { @@ -526,6 +536,12 @@ impl Message for SubscribeStart { w.varint(largest.frame)?; } } + if version.has_ranges() { + w.varint(self.starts.len() as u64)?; + for start in &self.starts { + w.varint_opt(*start)?; + } + } Ok(()) } } @@ -782,6 +798,7 @@ mod test { let resp = SubscribeResponse::Start(SubscribeStart { group: 42, largest: None, + starts: Vec::new(), }); let mut buf = Vec::new(); resp.encode(&mut Encoder::new(&mut buf, Version::Lite05.into()), Version::Lite05) @@ -798,7 +815,11 @@ mod test { #[test] fn subscribe_start_carries_the_largest_position_on_lite07() { for largest in [None, Some(crate::track::Position { group: 3, frame: 2 })] { - let resp = SubscribeResponse::Start(SubscribeStart { group: 4, largest }); + let resp = SubscribeResponse::Start(SubscribeStart { + group: 4, + largest, + starts: Vec::new(), + }); let mut buf = Vec::new(); resp.encode( &mut crate::coding::Encoder::new(&mut buf, Version::Lite07.into()), @@ -813,6 +834,7 @@ mod test { let resp = SubscribeResponse::Start(SubscribeStart { group: 4, largest: Some(crate::track::Position { group: 3, frame: 2 }), + starts: Vec::new(), }); let mut buf = Vec::new(); resp.encode( @@ -823,6 +845,29 @@ mod test { assert_eq!(buf, [0, 1, 4], "lite-06 has no largest position"); } + /// Lite-07 says where each range's start resolved, `None` for one that didn't; earlier + /// versions leave it off the wire. + #[test] + fn subscribe_start_carries_the_range_starts_on_lite07() { + let start = SubscribeStart { + group: 4, + largest: None, + starts: vec![Some(2), None, Some(0)], + }; + let resp = SubscribeResponse::Start(start.clone()); + let buf = resp.encode_bytes(Version::Lite07).unwrap(); + assert_eq!(buf.as_ref(), [0, 6, 4, 0, 3, 3, 0, 1]); + match SubscribeResponse::decode_slice(&buf, Version::Lite07).unwrap().0 { + SubscribeResponse::Start(decoded) => assert_eq!(decoded.starts, start.starts), + other => panic!("expected Start, got {other:?}"), + } + let buf = resp.encode_bytes(Version::Lite06).unwrap(); + match SubscribeResponse::decode_slice(&buf, Version::Lite06).unwrap().0 { + SubscribeResponse::Start(decoded) => assert!(decoded.starts.is_empty()), + other => panic!("expected Start, got {other:?}"), + } + } + #[test] fn subscribe_end_roundtrips_on_lite05() { let resp = SubscribeResponse::End(SubscribeEnd { group: 7 }); diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 835d46e7102..bc86ab0bd09 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -3424,6 +3424,7 @@ mod tests { lite::SubscribeResponse::Start(lite::SubscribeStart { group: 0, largest: None, + starts: vec![Some(0)], }) .encode( &mut crate::coding::Encoder::new(&mut responses, version.into()), @@ -3542,6 +3543,7 @@ mod tests { lite::SubscribeResponse::Start(lite::SubscribeStart { group: 0, largest: None, + starts: vec![Some(0)], }) .encode(&mut encoder, version) .unwrap(); @@ -3596,6 +3598,7 @@ mod tests { lite::SubscribeResponse::Start(lite::SubscribeStart { group: 4, largest: Some(largest), + starts: vec![Some(4)], }) .encode( &mut crate::coding::Encoder::new(&mut responses, version.into()), @@ -3658,6 +3661,7 @@ mod tests { lite::SubscribeResponse::Start(lite::SubscribeStart { group: 0, largest: None, + starts: vec![Some(0)], }), lite::SubscribeResponse::Drop(lite::SubscribeDrop { start: 2, @@ -3798,6 +3802,7 @@ mod tests { lite::SubscribeResponse::Start(lite::SubscribeStart { group: 0, largest: None, + starts: vec![Some(0)], }) .encode( &mut crate::coding::Encoder::new(&mut responses, version.into()), @@ -7895,6 +7900,14 @@ impl ServeLoop { // signal, so a reader waiting on a skipped group // fails over instead of stalling on a live route. lite::SubscribeResponse::Start(start) => { + // One resolved start per range the SUBSCRIBE carried, unless an + // update the publisher read first replaced them. + if serve.subscriber.version.has_ranges() + && !active.updated && start.starts.len() != active.ranges.len() + { + tracing::warn!(track = %serve.name, starts = start.starts.len(), ranges = active.ranges.len(), "subscribe start resolves other ranges"); + return Poll::Ready(ServeEnd::GiveBack(Error::ProtocolViolation)); + } // Where the live feed is, on versions whose answer says. if serve.subscriber.version.has_largest() { self.serving.set_live(start.largest, Some(start.group)); diff --git a/rs/moq-net/src/model/resume.rs b/rs/moq-net/src/model/resume.rs index cbb6b6db76d..8586ee6587d 100644 --- a/rs/moq-net/src/model/resume.rs +++ b/rs/moq-net/src/model/resume.rs @@ -693,6 +693,12 @@ impl Subscriber { self.reader().window().start() } + /// Where each of the reader's ranges starts; see [`track::Subscriber::starts`]. + pub(super) fn starts(&self) -> Vec> { + let mut reader = self.reader(); + reader.window().resolved().iter().map(|r| r.map(|r| r.start)).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 { diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index e83e895427c..12a3ebf2d77 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -7297,6 +7297,22 @@ impl Subscriber { Some(start?.max(self.bounds().start)) } + /// Where each of this subscriber's ranges starts, by index, as resolved here: `None` for + /// one that is empty or still waits on the first group from the live feed. + /// + /// A publisher tells its peer where a relative or time start landed, which the peer + /// can't work out from the groups it receives. + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + pub(crate) fn starts(&mut self) -> Vec> { + match &mut self.inner { + Inner::Plain(cursor) => { + cursor.sync(); + cursor.filter.window.resolved().iter().map(|r| r.map(|r| r.start)).collect() + } + Inner::Resume(resume, _) => resume.starts(), + } + } + /// The lowest group at or above `from` this subscriber's ranges and /// [`Self::with_range`] floor cover, `None` when none does: the groups between are /// never read, so a reader waiting on each group in sequence need not wait for them. From e87c577740e47d4f233c03456f90e98fc5cf11ae Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 27 Oct 2026 17:03:14 -0700 Subject: [PATCH 03/30] quest: the resolved start landed; lite-07 ranges keeps range fill and cold IETF anchors Co-Authored-By: Claude Opus 5.5 --- quest/m1/read-backfill.md | 17 ++++++++--------- quest/m1/subscribe-ranges/js.md | 2 ++ quest/m1/subscribe-ranges/lite.md | 18 +++++++----------- 3 files changed, 17 insertions(+), 20 deletions(-) diff --git a/quest/m1/read-backfill.md b/quest/m1/read-backfill.md index 62a90a542df..546538966f5 100644 --- a/quest/m1/read-backfill.md +++ b/quest/m1/read-backfill.md @@ -36,10 +36,11 @@ delivers a group that arrives below the cursor as a late backfill; Rust `largest()`, sparse moq-transport groups break the arithmetic, and a later coalesced largest isn't this request's resolution. SUBSCRIBE_OK's `Group` isn't it either, since the lite draft lets a group between the requested - start and it arrive later. Lite-07 SUBSCRIBE_OK carries the resolved start - for a relative range start (decided 2026-10-22 in - [Lite-07 ranges](/quest/m1/subscribe-ranges/lite.md)); settle the moq-transport equivalent or keep today's first-arrival cursor - there. + start and it arrive later. Lite-07 SUBSCRIBE_OK's `Range Start` carries + where each range's start resolved (`lite::SubscribeStart::starts`, 0 on the + wire for one unresolved); the Rust copy only checks its count today, so + carry it to the reader. Settle the moq-transport equivalent or keep today's + first-arrival cursor there. - The cursor is range-aware, not just floored: with disjoint ranges it skips straight across an unrequested gap (group 1, then 10 onward) instead of waiting for the latency budget, since no SUBSCRIBE_DROP is owed there. @@ -53,15 +54,13 @@ delivers a group that arrives below the cursor as a late backfill; Rust `Sync`) and adjust them; one that wants every group reads an arrival-order API instead. -Decided 2026-10-27 (maintainer): this waits only on Lite-07 ranges (the -resolved start in SUBSCRIBE_OK) and JS ranges, not the whole +Decided 2026-10-27 (maintainer): this waits only on the resolved start in +SUBSCRIBE_OK, now landed, and JS ranges, not the whole [Subscribe ranges](/quest/m1/subscribe-ranges/README.md) epic. Public API: no type change; the ordered read changes in Rust and JS (the -floor comes from reported bounds, and JS drops backfill). Wire: the resolved -start on lite-07 SUBSCRIBE_OK, through Lite-07 ranges. +floor comes from reported bounds, and JS drops backfill). Wire: none. ## Required -- [Lite-07 ranges](/quest/m1/subscribe-ranges/lite.md) - lite-07 SUBSCRIBE_OK carries the resolved start the Rust floor comes from - [JS ranges](/quest/m1/subscribe-ranges/js.md) - the JS reader's requested ranges set its floor diff --git a/quest/m1/subscribe-ranges/js.md b/quest/m1/subscribe-ranges/js.md index c8ea0c9db4f..98a0e9d2b44 100644 --- a/quest/m1/subscribe-ranges/js.md +++ b/quest/m1/subscribe-ranges/js.md @@ -24,6 +24,8 @@ update moved the range and the groups that arrived leave it uncovered. Reopen that demand on the same track instead, as the Rust copy does. Mirror the Rust names, including `Time(pts)` ends and their refusal on an untimed track. +The publisher's SUBSCRIBE_OK `Range Start` names only absolute starts today, +since it resolves no relative or time anchor; report where those resolve too. Also mirror Rust's optional `Subscription::max_delay` ([moq-dev/moq#6171](https://github.com/moq-dev/moq/pull/6171)): a `null` `maxDelay` asks for the publisher's max age alone, dominates when subscriptions diff --git a/quest/m1/subscribe-ranges/lite.md b/quest/m1/subscribe-ranges/lite.md index ccfa8186641..6542adca8b3 100644 --- a/quest/m1/subscribe-ranges/lite.md +++ b/quest/m1/subscribe-ranges/lite.md @@ -2,9 +2,8 @@ ## Goal -A relay fills a lite-07 upstream by subscribing to the missing ranges, and -SUBSCRIBE_OK carries the resolved start. Older wires and moq-transport carry -the anchors they can, and refuse only the rest. FETCH stays on lite-07 until +A relay fills a lite-07 upstream by subscribing to the missing ranges. Older +wires and moq-transport carry the anchors they can, and refuse only the rest. FETCH stays on lite-07 until [Remove FETCH from lite-07](/quest/m1/subscribe-ranges/lite07-fetch.md) removes it from both implementations. @@ -17,12 +16,6 @@ moves out of this quest (to its own quest since 2026-10-27), since JS still requ cross-language capability. This quest keeps FETCH on lite-07 and makes the range fill work without it. -Decided 2026-10-22 (maintainer): SUBSCRIBE_OK carries the resolved start for -a relative range start, so a reader's ordered cursor takes its floor from it -([Container reads drop backfill](/quest/m1/read-backfill.md)). Decided -2026-10-27 (maintainer): it is part of the Goal, since read-backfill now -requires this quest for it. - ### Range fill through the subscription Lite-07 SUBSCRIBE and SUBSCRIBE_UPDATE carry the range list (`lite::Range`, @@ -68,7 +61,11 @@ relative joining FETCH. Reason: both count back by Group ID arithmetic, which breaks once [time addressing](/quest/m1/time-addressing/model.md) makes legacy Group IDs sparse start ticks. A range with a bounded end resolves both bounds against the Largest Object and goes upstream as an -absolute range. Live keeps today's join (`ietf::subscriber::subscribe_join`). +absolute range. Live keeps today's join (`ietf::subscriber::subscribe_join`). A copy already +resolves what its cache settles (`ResolvedRanges`, from +[moq-dev/moq#6815](https://github.com/moq-dev/moq/pull/6815)) and refuses the +rest (`anchored_cached`); what remains is a cold copy resolving `Relative(n)` +from the Largest Object of a live join instead of refusing it. Each mapping that makes a wire carry an anchor lifts that wire's `Unsupported` refusal for it, and extends the per-wire table in `rs/moq-net/tests/integration/range_anchors.rs`. @@ -78,5 +75,4 @@ Update `drafts/draft-lcurley-moq-lite.md` with each wire change here. Run ## Related -- [Container reads drop backfill](/quest/m1/read-backfill.md) - floors its ordered cursor on the resolved start - [Remove FETCH from lite-07](/quest/m1/subscribe-ranges/lite07-fetch.md) - removes FETCH from lite-07 in both implementations once this and JS ranges land From 638e56ec93403d7d0486ea7e410d5b3ad86fba18 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 27 Oct 2026 17:03:21 -0700 Subject: [PATCH 04/30] quest: lite07-finalize names what lite-07 ranges still owes Co-Authored-By: Claude Opus 5.5 --- quest/m1/lite07-finalize.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quest/m1/lite07-finalize.md b/quest/m1/lite07-finalize.md index 97ca3bf8361..0ce44d7c2ef 100644 --- a/quest/m1/lite07-finalize.md +++ b/quest/m1/lite07-finalize.md @@ -103,7 +103,7 @@ timestamp bound in [#6168](https://github.com/moq-dev/moq/pull/6168). - [Routes and announces](/quest/m1/cluster-routing/routes.md) - lite-07 carries the route layer: ROUTE per origin node and path-less ANNOUNCE, with the hop list gone - [Drop set](/quest/m1/drop-set.md) - lite-07's SUBSCRIBE_DROP carries `Max Age`, so a widening re-offers groups reported dropped -- [Lite-07 ranges](/quest/m1/subscribe-ranges/lite.md) - lite-07 SUBSCRIBE carries ranges and an order, and SUBSCRIBE_OK the resolved start +- [Lite-07 ranges](/quest/m1/subscribe-ranges/lite.md) - a relay fills a lite-07 upstream through the subscription, and moq-transport carries the anchors it can - [JS ranges](/quest/m1/subscribe-ranges/js.md) - JS speaks lite-07 ranges - [Remove FETCH from lite-07](/quest/m1/subscribe-ranges/lite07-fetch.md) - lite FETCH is gone from lite-07, in Rust and JS - [Lite-07 by start](/quest/m1/time-addressing/lite07.md) - lite-07 carries group starts, previous-start links, GROUP_END, and time bounds instead of sequence numbers From 7ccf42bf5e7fbe834dec9ea678f6c33240778408 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 27 Oct 2026 17:14:49 -0700 Subject: [PATCH 05/30] style: rustfmt Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/lite/publisher.rs | 10 ++++++++-- rs/moq-net/src/model/track.rs | 8 +++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 9bf32ec44e1..57687a412a8 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -6130,7 +6130,10 @@ mod serve_group_test { assert!(futures::poll!(run.as_mut()).is_pending()); let writes = log.writes.lock().unwrap().clone(); - match lite::SubscribeResponse::decode_slice(&writes, Version::Lite07).unwrap().0 { + match lite::SubscribeResponse::decode_slice(&writes, Version::Lite07) + .unwrap() + .0 + { lite::SubscribeResponse::Start(start) => assert_eq!(start.starts, [Some(3), Some(1), None]), other => panic!("expected SUBSCRIBE_START, got {other:?}"), } @@ -7305,7 +7308,10 @@ mod serve_group_test { *open = true; drop(open); run.await.unwrap(); - assert_eq!(*log.writes.lock().unwrap(), [0, 5, 0, 1, 0, 1, 1, 2, 3, 0, 0, 0x34, 1, 1, 2]); + assert_eq!( + *log.writes.lock().unwrap(), + [0, 5, 0, 1, 0, 1, 1, 2, 3, 0, 0, 0x34, 1, 1, 2] + ); } } diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 12a3ebf2d77..fe996cfbcca 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -7307,7 +7307,13 @@ impl Subscriber { match &mut self.inner { Inner::Plain(cursor) => { cursor.sync(); - cursor.filter.window.resolved().iter().map(|r| r.map(|r| r.start)).collect() + cursor + .filter + .window + .resolved() + .iter() + .map(|r| r.map(|r| r.start)) + .collect() } Inner::Resume(resume, _) => resume.starts(), } From a3e71c65de02a7c2196b61a03fe523ae5449123d Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 27 Oct 2026 18:20:07 -0700 Subject: [PATCH 06/30] fix(net): report a lite-07 range with no start as group 0 A None start reaches back to the oldest group, which the draft resolves to group 0, so the JS publisher now reports it instead of leaving it unresolved. An empty range reports no start, matching the Rust publisher. Co-Authored-By: Claude Opus 5.5 --- js/net/src/lite/subscribe.test.ts | 6 ++++++ js/net/src/lite/subscribe.ts | 21 ++++++++++++--------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/js/net/src/lite/subscribe.test.ts b/js/net/src/lite/subscribe.test.ts index 87bb7d84c0f..42ec20328f1 100644 --- a/js/net/src/lite/subscribe.test.ts +++ b/js/net/src/lite/subscribe.test.ts @@ -207,10 +207,16 @@ test("a lite-07 list decodes as the span covering it", async () => { // Groups 2 through 4 and 7 through frame 1 of group 9. const sparse = await update([2, 1, 2, 0, 1, 5, 0, 1, 7, 0, 1, 9, 2]); expect([sparse.startGroup, sparse.startFrame, sparse.endGroup, sparse.endFrame]).toEqual([2, 0, 9, 1]); + expect(sparse.rangeStarts).toEqual([2, 7]); // A relative start resolves upstream of nothing here, so it widens to no floor. const relative = await update([2, 2, 3, 1, 5, 0, 1, 7, 0, 1, 9, 0]); expect([relative.startGroup, relative.endGroup, relative.endFrame]).toEqual([undefined, 8, undefined]); + expect(relative.rangeStarts).toEqual([undefined, 7]); + + // No start resolves to group 0; an empty range resolves nothing. + const oldest = await update([2, 0, 1, 3, 0, 1, 5, 2, 1, 5, 2]); + expect(oldest.rangeStarts).toEqual([0, undefined]); await expect(update([0])).rejects.toThrow(ProtocolViolation); await expect(update([1, 4, 0, 0])).rejects.toThrow(ProtocolViolation); diff --git a/js/net/src/lite/subscribe.ts b/js/net/src/lite/subscribe.ts index b04bb6c040f..7bfd2c7d509 100644 --- a/js/net/src/lite/subscribe.ts +++ b/js/net/src/lite/subscribe.ts @@ -119,8 +119,9 @@ interface Span { endGroup?: number; endFrame?: number; /** - * Lite-07's start group of each range in turn, `undefined` for one that does not name a - * group. Decoded only, so SUBSCRIBE_START can answer for every range. + * Lite-07's start group of each range in turn, 0 for no start, and `undefined` for an + * empty range or a relative or time start. Decoded only, so SUBSCRIBE_START can answer + * for every range. */ rangeStarts?: (number | undefined)[]; } @@ -168,15 +169,15 @@ function before(a: Point, b: Point): boolean { } /** - * Decode one lite-07 range end, `undefined` for none. A relative or time anchor is also - * `undefined`: this publisher resolves neither, so it serves from the oldest group its + * Decode one lite-07 range end: its position, `null` for none, or `undefined` for a relative + * or time anchor. This publisher resolves neither, so it serves from the oldest group its * budget allows, or through the live edge, and the subscriber keeps what it asked for. */ -async function decodeAnchor(r: Reader): Promise { +async function decodeAnchor(r: Reader): Promise { const kind = await r.u53(); switch (kind) { case 0: - return undefined; + return null; case 1: return [await r.u53(), await r.u53()]; case 2: @@ -212,10 +213,12 @@ async function decodeSpan(r: Reader, version: Version): Promise { for (let i = 0; i < count; i++) { const from = await decodeAnchor(r); const to = await decodeAnchor(r); - rangeStarts.push(from?.[0]); - if (from === undefined) floor = false; + // No start reaches back to the oldest group, which is group 0. + const resolved = from === null ? ([0, 0] as Point) : from; + rangeStarts.push(resolved && (!to || before(resolved, to)) ? resolved[0] : undefined); + if (!from) floor = false; else if (start === undefined || before(from, start)) start = from; - if (to === undefined) live = true; + if (!to) live = true; else if (end === undefined || before(end, to)) end = to; } if (!floor) start = undefined; From d6e693e5147b2b12927ac3ac0670bd4383a219c5 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 27 Oct 2026 18:49:07 -0700 Subject: [PATCH 07/30] fix(net): report no start for a lite-07 range that resolves empty A range like Relative(0) to Absolute(2) is empty only once its start resolves past its end. Window::resolved keeps such a span, so SUBSCRIBE_OK reported a start for it. Window::starts now drops it for both plain and route-following readers. Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/lite/publisher.rs | 7 ++++++- rs/moq-net/src/model/resume.rs | 3 +-- rs/moq-net/src/model/subscription/window.rs | 14 ++++++++++++-- rs/moq-net/src/model/track.rs | 8 +------- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 57687a412a8..b279cd5a202 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -6123,6 +6123,11 @@ mod serve_group_test { }, // Empty, so it starts nowhere. Range::groups(3..3), + // Empty only once its start resolves to group 4. + Range { + start: Anchor::Relative(0), + end: Some(Anchor::Absolute(2)), + }, ]), ); let (mut run, mut writer, log) = lite07_run(SinkSession::new(Log::default()), subscriber); @@ -6134,7 +6139,7 @@ mod serve_group_test { .unwrap() .0 { - lite::SubscribeResponse::Start(start) => assert_eq!(start.starts, [Some(3), Some(1), None]), + lite::SubscribeResponse::Start(start) => assert_eq!(start.starts, [Some(3), Some(1), None, None]), other => panic!("expected SUBSCRIBE_START, got {other:?}"), } } diff --git a/rs/moq-net/src/model/resume.rs b/rs/moq-net/src/model/resume.rs index 8586ee6587d..b9a71ac81a7 100644 --- a/rs/moq-net/src/model/resume.rs +++ b/rs/moq-net/src/model/resume.rs @@ -695,8 +695,7 @@ impl Subscriber { /// Where each of the reader's ranges starts; see [`track::Subscriber::starts`]. pub(super) fn starts(&self) -> Vec> { - let mut reader = self.reader(); - reader.window().resolved().iter().map(|r| r.map(|r| r.start)).collect() + self.reader().window().starts() } /// The lowest group at or above `from` the reader's ranges cover; see diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index fc4b32df39e..3e4ded40c8b 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -450,8 +450,9 @@ impl Window { self.spans.iter().map(|span| span.start).min() } - /// Each range resolved, in the order given: `None` for one that is empty or still - /// waits on a first group. An unsettled time end is reported open, never at its cap. + /// Each range resolved, in the order given: `None` for one empty as given or still + /// waiting on a first group. One that only resolves empty keeps its span, ending at or + /// below its start. An unsettled time end is reported open, never at its cap. pub fn resolved(&self) -> Vec> { let mut resolved = vec![None; self.ranges.len()]; for span in &self.spans { @@ -466,6 +467,15 @@ impl Window { resolved } + /// Where each range starts, in the order given: `None` for one that is empty, even only + /// once resolved, or still waits on a first group. + pub fn starts(&self) -> Vec> { + self.resolved() + .into_iter() + .map(|r| r.filter(|r| r.end.is_none_or(|end| end > r.start)).map(|r| r.start)) + .collect() + } + /// The exclusive end past every group in range, `None` when one stays open. An /// unsettled time end counts as open: its cap only holds reads back meanwhile. pub fn end(&self) -> Option { diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index fe996cfbcca..aeac277d717 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -7307,13 +7307,7 @@ impl Subscriber { match &mut self.inner { Inner::Plain(cursor) => { cursor.sync(); - cursor - .filter - .window - .resolved() - .iter() - .map(|r| r.map(|r| r.start)) - .collect() + cursor.filter.window.starts() } Inner::Resume(resume, _) => resume.starts(), } From 06d49b240af030aa328773e8ae96cb38b43ae2de Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 27 Oct 2026 20:12:50 -0700 Subject: [PATCH 08/30] fix(net): keep a lite-07 range empty partway through a group Bounds::new widened a range like (3,5) to (3,5) to all of group 3, so SUBSCRIBE_OK reported a start for it and its empty frame part narrowed group 3 for any other range covering it. It now maps to an empty model range. Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/lite/publisher.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index b279cd5a202..101bae22930 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -3543,6 +3543,14 @@ impl Bounds { frames: Frames::default(), }; for range in ranges { + // Empty partway through a group, which whole groups can't say: keep its place, + // covering no group and narrowing no frame of one. + if let (Some(lite::Anchor::Absolute(start)), Some(lite::Anchor::Absolute(end))) = (range.start, range.end) + && end <= start + { + bounds.ranges.push(Range::groups(start.group..start.group)); + continue; + } let start = match range.start { None => Range::oldest().start, Some(start) => anchor(start)?, @@ -4891,6 +4899,11 @@ mod serve_group_test { assert_eq!((started.groups().start(), started.groups().end()), (Some(5), None)); assert_eq!(started.frames.start(5), 3); + // Empty partway through group 5 covers no group, rather than all of it. + let empty = bounds(group(5, 3), group(5, 3)); + assert_eq!(empty.ranges, [crate::track::Range::groups(5..5)]); + assert_eq!(empty.frames, Frames::default()); + // A time anchor names no group on a track the peer was told is untimed. let time = [lite::Range { start: Some(lite::Anchor::Time(1_000)), From 9fd2893e4a07fa854a7b37452d054585481d2879 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 28 Oct 2026 02:03:30 -0700 Subject: [PATCH 09/30] feat(net): drop Group from lite-07 SUBSCRIBE_OK Each Range Start now names where that range's delivery starts, raised past the groups the publisher skipped or expired, so the lowest is the old Group and the field is redundant. Earlier versions keep Group, carried as the one range's start. Co-Authored-By: Claude Opus 5.5 --- drafts/draft-lcurley-moq-lite.md | 42 ++------ js/net/src/lite/connection.test.ts | 2 +- js/net/src/lite/publisher.test.ts | 16 +-- js/net/src/lite/publisher.ts | 27 +++-- js/net/src/lite/subscribe.test.ts | 42 ++++---- js/net/src/lite/subscribe.ts | 91 ++++++++-------- js/net/src/lite/subscriber.test.ts | 10 +- js/net/src/lite/subscriber.ts | 10 +- js/net/src/lite/tail.test.ts | 34 +++--- js/net/src/lite/track_stream.test.ts | 2 +- quest/m1/read-backfill.md | 13 ++- quest/m1/subscribe-ranges/js.md | 4 +- rs/moq-net/src/coding/writer.rs | 3 +- rs/moq-net/src/fuzz.rs | 3 +- rs/moq-net/src/lite/publisher.rs | 38 +++---- rs/moq-net/src/lite/subscribe.rs | 114 +++++++++----------- rs/moq-net/src/lite/subscriber.rs | 28 +++-- rs/moq-net/src/model/resume.rs | 4 +- rs/moq-net/src/model/subscription/window.rs | 15 ++- rs/moq-net/src/model/track.rs | 11 +- 20 files changed, 251 insertions(+), 258 deletions(-) diff --git a/drafts/draft-lcurley-moq-lite.md b/drafts/draft-lcurley-moq-lite.md index b687d656bed..652c195210c 100644 --- a/drafts/draft-lcurley-moq-lite.md +++ b/drafts/draft-lcurley-moq-lite.md @@ -526,16 +526,16 @@ A receiver SHOULD NOT cache refusals; rate limiting is the advertiser's concern. A subscriber opens Subscribe Streams to request a Track. The subscriber MUST start a Subscribe Stream with a SUBSCRIBE message followed by any number of SUBSCRIBE_UPDATE messages. -The publisher replies with a SUBSCRIBE_OK message once the start group is resolved, followed by a SUBSCRIBE_END message once the subscription ends. +The publisher replies with a SUBSCRIBE_OK message once each Range's start is resolved, followed by a SUBSCRIBE_END message once the subscription ends. For a live track the publisher MAY withhold SUBSCRIBE_OK until the first matching group resolves the start; if the track has already ended with no matching groups, it sends SUBSCRIBE_END with no preceding SUBSCRIBE_OK. -A requested start past the publisher's largest position is the exception: the publisher MUST send SUBSCRIBE_OK without waiting, with `Group` set to the requested start group, so a subscriber resuming just past what it holds learns where the live feed is from the answer alone (a quiet track may not reach the start for a long time). +A requested start past the publisher's largest position is the exception: the publisher MUST send SUBSCRIBE_OK without waiting, so a subscriber resuming just past what it holds learns where the live feed is from the answer alone (a quiet track may not reach the start for a long time). A rejection is a stream reset: a publisher that cannot serve the subscription (no such track, an ended broadcast, or any other refusal) MUST promptly reset the stream rather than leave it pending, so a subscriber distinguishes "pending" from "refused" by the reset, not by a timeout. A route claims capability rather than inventory, so a subscription for a covered path that names nothing is refused this way too. The track's immutable publisher properties are not carried here; they are fetched once via a [Track Stream](#track-stream). The subscriber needs the track's TRACK_INFO (notably its timescale) to interpret FRAME messages, and MAY open the Track and Subscribe streams concurrently, buffering frames until it arrives. -The publisher sends SUBSCRIBE_OK once the absolute start position is resolved, and SUBSCRIBE_END as soon as the exclusive final group is known (see [SUBSCRIBE_OK](#subscribe-ok) and [SUBSCRIBE_END](#subscribe-end)). +The publisher sends SUBSCRIBE_OK once the start is resolved, and SUBSCRIBE_END as soon as the exclusive final group is known (see [SUBSCRIBE_OK](#subscribe-ok) and [SUBSCRIBE_END](#subscribe-end)). The publisher closes its side of the stream (FIN) after SUBSCRIBE_END, once every Group Stream has finished or been reset. A subscription whose every Range has an `End` other than None is covered once every Group in its Ranges has been sent as a Group Stream or a datagram, or named by SUBSCRIBE_DROP. The publisher MAY then close its side (FIN) without SUBSCRIBE_END, once every Group Stream has finished or been reset, rather than wait for a SUBSCRIBE_UPDATE to widen it. @@ -1489,16 +1489,15 @@ TRACKS_FINISH Message { The name a TRACKS_ADD on the stream listed. ## SUBSCRIBE_OK {#subscribe-ok} -A SUBSCRIBE_OK message confirms a subscription and resolves its absolute start position. -It is the first message the publisher sends on the Subscribe Stream, once the start position is known. +A SUBSCRIBE_OK message confirms a subscription and resolves where each Range starts. +It is the first message the publisher sends on the Subscribe Stream, once those starts are known. -This is the trimmed-down counterpart of MoqTransport's SUBSCRIBE_OK: it retains the name and the role of the publisher's positive response, but carries only the resolved start position, the publisher's largest position, and where each Range starts (all other per-track properties live in [TRACK_INFO](#track-info)). +This is the trimmed-down counterpart of MoqTransport's SUBSCRIBE_OK: it retains the name and the role of the publisher's positive response, but carries only the publisher's largest position and where each Range starts (all other per-track properties live in [TRACK_INFO](#track-info)). ~~~ SUBSCRIBE_OK Message { Type (i) = 0x0 Message Length (i) - Group (i) Largest Group (i) [Largest Frame (i)] Range Start Count (i) @@ -1509,34 +1508,16 @@ SUBSCRIBE_OK Message { **Type**: Set to 0x0 to indicate a SUBSCRIBE_OK message. -**Group**: -The absolute sequence number where delivery starts when the subscription resolves. -Group streams can arrive out of order, so it need not be the first group sent. -It MUST be greater than or equal to the lowest requested `Start`, as resolved for a Relative or Time Anchor. -This group is not a new floor. -A subscriber SHOULD NOT wait for a group between the requested floor and this group. -The publisher still delivers such a group if it is created or becomes available later, while it is within `Subscriber Max Age`. -A subscriber whose `Subscriber Max Age` resolves the start to the latest group learns that sequence here. - -There is no matching frame field, because the start frame is never in doubt: a partial group is only delivered when it was asked for, so the subscription starts either exactly where it asked or at the beginning of a later group (see [Positions](#positions)). -The subscriber derives the start frame from `Group` and its own request: - -- `Group` equals the requested start group: delivery begins at the requested start frame. -- `Group` is greater: delivery begins at frame 0. - -The second case is easy to get wrong, so to be explicit: a subscriber that requested group 5 frame 15 and receives `Group` = 6 starts at **frame 0** of group 6, not frame 15. -The frame offset belonged to group 5 and is gone along with the rest of it; it does not carry forward to whichever group the publisher resolved to. - **Largest Group** and **Largest Frame**: The largest position the publisher knows of for the track when it answers: the last frame that exists, like moq-transport's Largest Location, including one a relay's source reported that has yet to arrive. `Largest Group` is the group sequence plus one, and 0 means the track has nothing yet, in which case `Largest Frame` is absent. A subscriber takes it as where the live feed is: a relay holding groups from an earlier subscription serves them from cache only once this says they are current, which a publisher answering promptly lets it do without waiting for a frame. **Range Start**: -One per Range the publisher is serving, in order: the Group its `Start` resolved to, plus one, so a subscriber learns where a Relative or Time Anchor landed. -0 means the Range is empty or its `Start` has not resolved. -A `Start` that reaches back to the oldest Group resolves to Group 0. -Unlike `Group`, it is the Range's floor: a Group between it and `Group` that arrives later is in range. +One per Range the publisher is serving, in order: the Group where the Range's delivery starts, plus one, so a subscriber learns where a Relative or Time Anchor landed. +0 means the Range has no Group to deliver. +The Range's Groups from this one are owed as described in [SUBSCRIBE_DROP](#subscribe-drop); an earlier one is not, though it MAY still arrive. +A Range whose `Range Start` is its requested start Group begins at the requested frame, and otherwise at frame 0 (see [Positions](#positions)). The Ranges are those of the SUBSCRIBE unless the publisher applied a SUBSCRIBE_UPDATE before answering; a subscriber that sent none MUST treat a count other than its number of Ranges as a PROTOCOL_VIOLATION. ## SUBSCRIBE_END {#subscribe-end} @@ -1867,8 +1848,7 @@ The `Message Length` describes the payload size on the wire. - Listed the shared INVALID_REQUEST_ID (0x4) session code used by moq-transport drafts 14 through 16. - Listed the shared EXPIRED_AUTH_TOKEN (0x18) session code. A request that expires on its own resets with the UNAUTHORIZED stream code. -- Added `Range Start` to SUBSCRIBE_OK: the Group each Range's `Start` resolved to, including a Relative or Time Anchor. -- SUBSCRIBE_OK `Group` names where delivery starts when the subscription resolves, which need not be the first group sent. It is not a new floor. A subscriber does not wait for a group between the requested floor and it, but a publisher still delivers one that arrives later within Subscriber Max Age. +- Replaced SUBSCRIBE_OK `Group` with a `Range Start` per Range: the Group where that Range's delivery starts, including at a Relative or Time Anchor, from which its Groups are owed. - Replaced `Group Start`, `Group End`, `Frame Start`, and `Frame End` in SUBSCRIBE and SUBSCRIBE_UPDATE with a non-empty list of Ranges. Each end is an Anchor: none, an absolute Position (exclusive as an end), a count of Groups back from the latest, or a presentation time. - A subscription's range bounds datagrams like groups, and FETCH never returns a datagram. - A SUBSCRIBE_UPDATE that lowers a `Start` covers the groups below the old start that the publisher still holds, not only those that arrive later. A group already sent is not sent again. diff --git a/js/net/src/lite/connection.test.ts b/js/net/src/lite/connection.test.ts index 3d6027d3a8f..d3a9d032509 100644 --- a/js/net/src/lite/connection.test.ts +++ b/js/net/src/lite/connection.test.ts @@ -651,7 +651,7 @@ for (const violation of ["a partial group", "a group past the declared end"] as sub = stream; } } - await encodeSubscribeResponse(sub.writer, { start: new SubscribeStart(0) }, version); + await encodeSubscribeResponse(sub.writer, { start: new SubscribeStart([0]) }, version); let group: GroupMessage; if (violation === "a partial group") { diff --git a/js/net/src/lite/publisher.test.ts b/js/net/src/lite/publisher.test.ts index 2527f300ca9..02e35668e99 100644 --- a/js/net/src/lite/publisher.test.ts +++ b/js/net/src/lite/publisher.test.ts @@ -1163,7 +1163,7 @@ test("lite draft-05: a straggler below the announced start group is not served", // The publisher announced group 2 as the resolved start. const resp = await decodeSubscribeResponse(sub.client.reader, Version.DRAFT_05); if (!("start" in resp)) throw new Error("expected SUBSCRIBE_START"); - expect(resp.start.group).toBe(2); + expect(resp.start.starts[0]).toBe(2); // A straggler below the announced start never reaches the wire: the next // stream the publisher opens is group 3's. @@ -1185,7 +1185,7 @@ test("lite draft-05: an explicit floor still serves a group below the first one" const resp = await decodeSubscribeResponse(sub.client.reader, Version.DRAFT_05); if (!("start" in resp)) throw new Error("expected SUBSCRIBE_START"); - expect(resp.start.group).toBe(2); + expect(resp.start.starts[0]).toBe(2); sub.serve(0); expect(await sub.servedSequence()).toBe(0); @@ -1210,7 +1210,7 @@ for (const version of [Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07]) { expect(await sub.servedSequence()).toBe(6); const response = await decodeSubscribeResponse(sub.client.reader, version); if (!("start" in response)) throw new Error("expected SUBSCRIBE_START"); - expect(response.start.group).toBe(expected); + expect(response.start.starts[0]).toBe(expected); if (expected < 6) { sub.serve(expected); expect(await sub.servedSequence()).toBe(expected); @@ -1234,7 +1234,7 @@ for (const version of [Version.DRAFT_06, Version.DRAFT_07]) { hooks.startTrack(sub.track, { start: 3, requested: 2 }); const response = await decodeSubscribeResponse(sub.client.reader, version); if (!("start" in response)) throw new Error("expected SUBSCRIBE_START"); - expect(response.start.group).toBe(3); + expect(response.start.starts[0]).toBe(3); expect(await sub.servedSequence()).toBe(6); } finally { await sub.close(); @@ -1276,7 +1276,7 @@ for (const skipped of ["failed", "stale"] as const) { sub.serve(6); const response = await decodeSubscribeResponse(sub.client.reader, Version.DRAFT_06); if (!("start" in response)) throw new Error("expected SUBSCRIBE_START"); - expect(response.start.group).toBe(6); + expect(response.start.starts[0]).toBe(6); if (skipped === "failed") { const dropped = await decodeSubscribeResponse(sub.client.reader, Version.DRAFT_06); if (!("drop" in dropped)) throw new Error("expected SUBSCRIBE_DROP"); @@ -2039,7 +2039,7 @@ async function serve( let end: number | undefined; for (;;) { const resp = await decodeSubscribeResponse(client.reader, Version.DRAFT_06); - if ("start" in resp) start = resp.start.group; + if ("start" in resp) start = resp.start.starts[0]; if ("end" in resp) { end = resp.end.group; break; @@ -2855,7 +2855,7 @@ test.each([0, 1])("lite draft-07 reports the cached largest position when starti try { const response = await decodeSubscribeResponse(client.reader, version); if (!("start" in response)) throw new Error("expected SUBSCRIBE_OK"); - expect(response.start.group).toBe(startGroup); + expect(response.start.starts[0]).toBe(startGroup); expect(response.start.largest).toEqual({ group: 0, frame: 0 }); } finally { client.close(); @@ -2893,7 +2893,7 @@ test("lite draft-07 reports the source's largest position past what a relay hold try { const response = await decodeSubscribeResponse(client.reader, version); if (!("start" in response)) throw new Error("expected SUBSCRIBE_START"); - expect([response.start.group, response.start.largest]).toEqual([0, { group: 4, frame: 0 }]); + expect([response.start.starts[0], response.start.largest]).toEqual([0, { group: 4, frame: 0 }]); } finally { client.close(); publisher.close(); diff --git a/js/net/src/lite/publisher.ts b/js/net/src/lite/publisher.ts index d0b2e959a31..4c7b432b9d1 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -32,6 +32,7 @@ import { Probe } from "./probe.ts"; import { encodeSubscribeResponse, exclusiveGroupEnd, + type RangeGroups, type Subscribe, SubscribeDrop, SubscribeEnd, @@ -335,6 +336,17 @@ class SubscriptionControls { } } +// Where each range starts once delivery starts at `from`, `undefined` for one with no group +// there. Before lite-07 there is one range, which starts there. +function rangeStarts(ranges: (RangeGroups | undefined)[] | undefined, from: number): (number | undefined)[] { + if (!ranges) return [from]; + return ranges.map((range) => { + if (!range) return undefined; + const start = Math.max(range.start, from); + return range.end === undefined || range.end > start ? start : undefined; + }); +} + // The later of two locations, either of which may be unknown. function newer(a: track.Position | undefined, b: track.Position | undefined): track.Position | undefined { if (!a || !b) return a ?? b; @@ -963,7 +975,7 @@ export class Publisher { served, floor, bounds, - rangeStarts: msg.rangeStarts ?? [], + ranges: msg.ranges, }); console.debug(`publish done: broadcast=${msg.broadcast} track=${track.name}`); @@ -1128,15 +1140,14 @@ export class Publisher { // The read cursor's floor; see {@link cursorFloor}. floor: number; bounds: FrameBounds; - // Each lite-07 range's start, which SUBSCRIBE_START answers for. This publisher - // resolves no relative or time anchor, so only an absolute start is known. - rangeStarts: (number | undefined)[]; + // Each lite-07 range, which SUBSCRIBE_START answers for. + ranges: (RangeGroups | undefined)[] | undefined; }, ): Promise { const { sub, broadcast, stamp, publisher, compress, served, bounds } = serving; // The read cursor's floor, assigned with the cap in {@link bounds} on every move. let floor = serving.floor; - let rangeStarts = serving.rangeStarts; + let ranges = serving.ranges; const moveCursor = () => track.withRange({ start: { included: floor }, @@ -1272,7 +1283,7 @@ export class Publisher { bounds.startFrame = update.startFrame; bounds.endGroup = update.endGroup; bounds.endFrame = update.endFrame; - rangeStarts = update.rangeStarts ?? []; + ranges = update.ranges; moveCursor(); // A microtask is too short: decoding one framed update crosses several // awaits, each of which can requeue behind the serving continuation. A @@ -1320,7 +1331,7 @@ export class Publisher { startSent = true; startSequence = bounds.startGroup; raiseFloor(bounds.startGroup); - const start = new SubscribeStart(bounds.startGroup, largest, rangeStarts); + const start = new SubscribeStart(rangeStarts(ranges, bounds.startGroup), largest); if ( !(await controls.response(encodeSubscribeResponse(stream, { start }, this.version))) ) @@ -1403,7 +1414,7 @@ export class Publisher { !(await controls.response( encodeSubscribeResponse( stream, - { start: new SubscribeStart(startSequence, largest, rangeStarts) }, + { start: new SubscribeStart(rangeStarts(ranges, startSequence), largest) }, this.version, ), )) diff --git a/js/net/src/lite/subscribe.test.ts b/js/net/src/lite/subscribe.test.ts index 42ec20328f1..30be9349ae2 100644 --- a/js/net/src/lite/subscribe.test.ts +++ b/js/net/src/lite/subscribe.test.ts @@ -207,16 +207,22 @@ test("a lite-07 list decodes as the span covering it", async () => { // Groups 2 through 4 and 7 through frame 1 of group 9. const sparse = await update([2, 1, 2, 0, 1, 5, 0, 1, 7, 0, 1, 9, 2]); expect([sparse.startGroup, sparse.startFrame, sparse.endGroup, sparse.endFrame]).toEqual([2, 0, 9, 1]); - expect(sparse.rangeStarts).toEqual([2, 7]); + expect(sparse.ranges).toEqual([ + { start: 2, end: 5 }, + { start: 7, end: 10 }, + ]); // A relative start resolves upstream of nothing here, so it widens to no floor. const relative = await update([2, 2, 3, 1, 5, 0, 1, 7, 0, 1, 9, 0]); expect([relative.startGroup, relative.endGroup, relative.endFrame]).toEqual([undefined, 8, undefined]); - expect(relative.rangeStarts).toEqual([undefined, 7]); + expect(relative.ranges).toEqual([ + { start: 0, end: 5 }, + { start: 7, end: 9 }, + ]); - // No start resolves to group 0; an empty range resolves nothing. + // No start reaches back to group 0; an empty range has no groups. const oldest = await update([2, 0, 1, 3, 0, 1, 5, 2, 1, 5, 2]); - expect(oldest.rangeStarts).toEqual([0, undefined]); + expect(oldest.ranges).toEqual([{ start: 0, end: 3 }, undefined]); await expect(update([0])).rejects.toThrow(ProtocolViolation); await expect(update([1, 4, 0, 0])).rejects.toThrow(ProtocolViolation); @@ -224,37 +230,37 @@ test("a lite-07 list decodes as the span covering it", async () => { }); test("SubscribeStart round-trips on draft-05", async () => { - const got = await responseRoundtrip(Version.DRAFT_05, { start: new SubscribeStart(42) }); + const got = await responseRoundtrip(Version.DRAFT_05, { start: new SubscribeStart([42]) }); expect("start" in got).toBe(true); if (!("start" in got)) throw new Error("expected start"); - expect(got.start.group).toBe(42); + expect(got.start.starts).toEqual([42]); }); test("SubscribeStart carries the largest position on draft-07", async () => { - // Type, length, group, largest group + 1, largest frame, no range starts. - expect(await encode(Version.DRAFT_07, { start: new SubscribeStart(4, { group: 3, frame: 2 }) })).toEqual( - new Uint8Array([0, 4, 4, 4, 2, 0]), + // Type, length, largest group + 1, largest frame, one range start plus one. + expect(await encode(Version.DRAFT_07, { start: new SubscribeStart([4], { group: 3, frame: 2 }) })).toEqual( + new Uint8Array([0, 4, 4, 2, 1, 5]), ); for (const largest of [undefined, { group: 3, frame: 2 }]) { - const got = await responseRoundtrip(Version.DRAFT_07, { start: new SubscribeStart(4, largest) }); + const got = await responseRoundtrip(Version.DRAFT_07, { start: new SubscribeStart([4], largest) }); if (!("start" in got)) throw new Error("expected start"); - expect([got.start.group, got.start.largest]).toEqual([4, largest]); + expect(got.start.largest).toEqual(largest); } // Draft-06 has no largest position on the wire. - expect(await encode(Version.DRAFT_06, { start: new SubscribeStart(4, { group: 3, frame: 2 }) })).toEqual( + expect(await encode(Version.DRAFT_06, { start: new SubscribeStart([4], { group: 3, frame: 2 }) })).toEqual( new Uint8Array([0, 1, 4]), ); }); -test("SubscribeStart carries each range's resolved start on draft-07", async () => { - const start = new SubscribeStart(4, undefined, [2, undefined, 0]); - // Type, length, group, no largest, three starts each plus one with 0 for none. - expect(await encode(Version.DRAFT_07, { start })).toEqual(new Uint8Array([0, 6, 4, 0, 3, 3, 0, 1])); +test("SubscribeStart carries each range's start in place of one group on draft-07", async () => { + const start = new SubscribeStart([2, undefined, 0]); + // Type, length, no largest, three starts each plus one with 0 for none. + expect(await encode(Version.DRAFT_07, { start })).toEqual(new Uint8Array([0, 5, 0, 3, 3, 0, 1])); const got = await responseRoundtrip(Version.DRAFT_07, { start }); if (!("start" in got)) throw new Error("expected start"); expect(got.start.starts).toEqual([2, undefined, 0]); - // Draft-06 has no range starts on the wire. - expect(await encode(Version.DRAFT_06, { start })).toEqual(new Uint8Array([0, 1, 4])); + // Draft-06's one `Group` names a single range. + await expect(encode(Version.DRAFT_06, { start })).rejects.toThrow(); }); test("SubscribeEnd round-trips on draft-05", async () => { diff --git a/js/net/src/lite/subscribe.ts b/js/net/src/lite/subscribe.ts index 7bfd2c7d509..5255b1142ec 100644 --- a/js/net/src/lite/subscribe.ts +++ b/js/net/src/lite/subscribe.ts @@ -10,7 +10,6 @@ import { hasDropCode, hasFrameBounds, hasGroupOrder, - hasLargest, hasOrder, hasRanges, hasRefresh, @@ -118,12 +117,18 @@ interface Span { startFrame: number; endGroup?: number; endFrame?: number; - /** - * Lite-07's start group of each range in turn, 0 for no start, and `undefined` for an - * empty range or a relative or time start. Decoded only, so SUBSCRIBE_START can answer - * for every range. - */ - rangeStarts?: (number | undefined)[]; + /** See {@link Subscribe.ranges}. */ + ranges?: (RangeGroups | undefined)[]; +} + +/** + * The groups of one lite-07 range: from `start` up to the exclusive `end`, or through the + * live edge without one. A relative or time anchor counts as no bound, since this + * publisher resolves neither. + */ +export interface RangeGroups { + start: number; + end?: number; } /** @@ -209,13 +214,13 @@ async function decodeSpan(r: Reader, version: Version): Promise { let floor = true; let live = false; // Not preallocated: each range costs at least two bytes of the bounded message. - const rangeStarts: (number | undefined)[] = []; + const ranges: (RangeGroups | undefined)[] = []; for (let i = 0; i < count; i++) { const from = await decodeAnchor(r); const to = await decodeAnchor(r); - // No start reaches back to the oldest group, which is group 0. - const resolved = from === null ? ([0, 0] as Point) : from; - rangeStarts.push(resolved && (!to || before(resolved, to)) ? resolved[0] : undefined); + // An end partway through a group still covers it. + const empty = from && to && !before(from, to); + ranges.push(empty ? undefined : { start: from?.[0] ?? 0, end: to ? to[0] + (to[1] > 0 ? 1 : 0) : undefined }); if (!from) floor = false; else if (start === undefined || before(from, start)) start = from; if (!to) live = true; @@ -232,7 +237,7 @@ async function decodeSpan(r: Reader, version: Version): Promise { startFrame, endGroup: end === undefined ? undefined : end[1] === 0 ? end[0] - 1 : end[0], endFrame: end === undefined || end[1] === 0 ? undefined : end[1] - 1, - rangeStarts, + ranges, }; } @@ -330,8 +335,8 @@ export class SubscribeUpdate { /** See {@link Subscribe.endFrame}. */ endFrame?: number; - /** See {@link Subscribe.rangeStarts}. */ - rangeStarts?: (number | undefined)[]; + /** See {@link Subscribe.ranges}. */ + ranges?: (RangeGroups | undefined)[]; constructor(props: { priority: number; @@ -342,7 +347,7 @@ export class SubscribeUpdate { endGroup?: number; startFrame?: number; endFrame?: number; - rangeStarts?: (number | undefined)[]; + ranges?: (RangeGroups | undefined)[]; }) { if (props.wantedGroup !== undefined && (!Number.isSafeInteger(props.wantedGroup) || props.wantedGroup < 0)) throw new RangeError("wanted group must be a safe non-negative integer"); @@ -354,7 +359,7 @@ export class SubscribeUpdate { this.endGroup = props.endGroup; this.startFrame = props.startFrame ?? 0; this.endFrame = props.endFrame; - this.rangeStarts = props.rangeStarts; + this.ranges = props.ranges; } async #encode(w: Writer, version: Version) { @@ -445,10 +450,10 @@ export class Subscribe { endFrame?: number; /** - * The start group of each lite-07 range in turn, `undefined` for one that names none. - * Decoded only, so SUBSCRIBE_START can answer for every range. + * The groups of each lite-07 range in turn, `undefined` for an empty one. Decoded only, + * so SUBSCRIBE_START can answer for every range. */ - rangeStarts?: (number | undefined)[]; + ranges?: (RangeGroups | undefined)[]; constructor(props: { id: bigint; @@ -462,7 +467,7 @@ export class Subscribe { endGroup?: number; startFrame?: number; endFrame?: number; - rangeStarts?: (number | undefined)[]; + ranges?: (RangeGroups | undefined)[]; }) { this.id = props.id; this.broadcast = props.broadcast; @@ -475,7 +480,7 @@ export class Subscribe { this.endGroup = props.endGroup; this.startFrame = props.startFrame ?? 0; this.endFrame = props.endFrame; - this.rangeStarts = props.rangeStarts; + this.ranges = props.ranges; } async #encode(w: Writer, version: Version) { @@ -625,16 +630,20 @@ export class SubscribeOk { } /** - * Resolves the absolute start group of a Draft-05+ subscription. The first message - * the publisher sends, once the start group is known. A value greater than the - * requested start implicitly drops the leading range. + * Resolves where a Draft-05+ subscription starts. The first message the publisher sends, + * once the start is known. A start above the requested one implicitly drops the groups + * below it. * - * There is no start frame: a partial group is only served to a subscriber that asked - * for one, so delivery begins either at the requested `startFrame` (when this is the - * requested group) or at frame 0 (when the publisher resolved to a later one). + * There is no start frame: a partial group is only served to a subscriber that asked for + * one, so a range begins at its requested frame when it starts at its requested group, + * and at frame 0 of a later one otherwise. */ export class SubscribeStart { - group: number; + /** + * The group each range's delivery starts at, in turn, `undefined` for a range with no + * group there. Before draft-07 the wire carries the one range's start as `Group`. + */ + starts: (number | undefined)[]; /** * The publisher's largest (group, frame) when it answered, or `undefined` for a track @@ -642,22 +651,19 @@ export class SubscribeStart { */ largest?: Position; - /** - * The group each range's start resolved to, in turn, `undefined` for one that is empty - * or unresolved. Draft-07+ only; empty before. - */ - starts: (number | undefined)[]; - - constructor(group: number, largest?: Position, starts: (number | undefined)[] = []) { - this.group = group; - this.largest = largest; + constructor(starts: (number | undefined)[], largest?: Position) { this.starts = starts; + this.largest = largest; } async encode(w: Writer, version: Version): Promise { return Message.encode(w, async (w) => { - await w.u53(this.group); - if (!hasLargest(version)) return; + // Draft-07 added the largest position along with the ranges. + if (!hasRanges(version)) { + const [group, ...rest] = this.starts; + if (group === undefined || rest.length > 0) throw new Error("one start group before draft-07"); + return w.u53(group); + } // Group + 1, so 0 is a track with nothing yet; the frame follows only otherwise. if (this.largest === undefined) { await w.u53(0); @@ -665,7 +671,6 @@ export class SubscribeStart { await w.u53(this.largest.group + 1); await w.u53(this.largest.frame); } - if (!hasRanges(version)) return; await w.u53(this.starts.length); for (const start of this.starts) await w.u53(start === undefined ? 0 : start + 1); }); @@ -673,18 +678,16 @@ export class SubscribeStart { static async decode(r: Reader, version: Version): Promise { return Message.decode(r, async (r) => { - const group = await r.u53(); - if (!hasLargest(version)) return new SubscribeStart(group); + if (!hasRanges(version)) return new SubscribeStart([await r.u53()]); const wire = await r.u53(); const largest = wire === 0 ? undefined : { group: wire - 1, frame: await r.u53() }; - if (!hasRanges(version)) return new SubscribeStart(group, largest); // Not preallocated: each start costs a byte of the bounded message. const starts: (number | undefined)[] = []; for (let count = await r.u53(); count > 0; count--) { const start = await r.u53(); starts.push(start === 0 ? undefined : start - 1); } - return new SubscribeStart(group, largest, starts); + return new SubscribeStart(starts, largest); }); } } diff --git a/js/net/src/lite/subscriber.test.ts b/js/net/src/lite/subscriber.test.ts index e61a68f46f3..799093ce00d 100644 --- a/js/net/src/lite/subscriber.test.ts +++ b/js/net/src/lite/subscriber.test.ts @@ -1018,7 +1018,7 @@ test.each([Version.DRAFT_06, Version.DRAFT_07])( await answerTrackInfo(await opened(0), version, 1000); const request = await opened(1); await request.reading; - await respond(request, version, [{ start: new SubscribeStart(0) }]); + await respond(request, version, [{ start: new SubscribeStart([0]) }]); const received = track.recvDatagram(); await writer.write(new DatagramMessage(0n, 0, 0, new Uint8Array(1198)).encode(version)); await writer.write(new DatagramMessage(0n, 1, 1, new Uint8Array(1197)).encode(version)); @@ -1653,7 +1653,7 @@ test.each([ await subscribe.reading; if (outcome === "answered") { - await respond(subscribe, version, [{ start: new SubscribeStart(0) }]); + await respond(subscribe, version, [{ start: new SubscribeStart([0]) }]); await drainReactions(); await pool.replace(); await drainReactions(); @@ -1662,7 +1662,7 @@ test.each([ await pool.withdraw(); await drainReactions(); expect(track.closed.peek()).toBeUndefined(); - await respond(subscribe, version, [{ start: new SubscribeStart(0) }, { end: new SubscribeEnd(0) }], true); + await respond(subscribe, version, [{ start: new SubscribeStart([0]) }, { end: new SubscribeEnd(0) }], true); expect(await track.closed).toBeNull(); } else { await pool.replace(); @@ -2236,7 +2236,7 @@ test("a lite-07 publisher FIN ends a covered bounded subscription", async () => new WritableStream({ write: (chunk) => void chunks.push(new Uint8Array(chunk)) }), version, ); - await encodeSubscribeResponse(writer, { start: new SubscribeStart(0) }, version); + await encodeSubscribeResponse(writer, { start: new SubscribeStart([0]) }, version); await encodeSubscribeResponse( writer, { drop: new SubscribeDrop({ start: 0, end: 1, error: StreamCode.NotFound }) }, @@ -2292,7 +2292,7 @@ test.each([ version, ); if (start) { - await encodeSubscribeResponse(writer, { start: new SubscribeStart(0) }, version); + await encodeSubscribeResponse(writer, { start: new SubscribeStart([0]) }, version); await encodeSubscribeResponse( writer, { drop: new SubscribeDrop({ start: 0, end: 0, error: StreamCode.NotFound }) }, diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index 7b3debc7861..19b9625c514 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -1457,12 +1457,18 @@ export class Subscriber { } if ("start" in resp) { - entry.start = resp.start.group; + // The lowest range start is the first group served; none leaves nothing owed. + entry.start = resp.start.starts.reduce( + (lowest, start) => + start === undefined || (lowest !== undefined && lowest <= start) ? lowest : start, + undefined, + ); hooks.startTrack(entry.track, { start: entry.start, requested: entry.requested }); hooks.reportedLargest(entry.track, resp.start.largest); // The groups the SUBSCRIBE asked for below it are not waited for, whatever the // demand asks later. One that still arrives is delivered. - if (entry.requested !== undefined) entry.tail.account(entry.requested, entry.start); + if (entry.requested !== undefined && entry.start !== undefined) + entry.tail.account(entry.requested, entry.start); } else if ("end" in resp) { if (entry.end !== undefined) throw new ProtocolViolation("duplicate SUBSCRIBE_END"); entry.end = resp.end.group; diff --git a/js/net/src/lite/tail.test.ts b/js/net/src/lite/tail.test.ts index 1fdcbea68e4..5cfa8fecac4 100644 --- a/js/net/src/lite/tail.test.ts +++ b/js/net/src/lite/tail.test.ts @@ -112,7 +112,7 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver test("the source start waits for SUBSCRIBE_START where it resolves the start", async () => { const { reader, respond } = await subscribed(version, GRACE, { start: { included: 2 } }); expect(hooks.sourceStart(reader)).toBe(resolvesStart(version) ? "pending" : undefined); - await respond({ start: new SubscribeStart(3) }); + await respond({ start: new SubscribeStart([3]) }); await settle(); expect(hooks.sourceStart(reader)).toBe(3); reader.close(); @@ -120,7 +120,7 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver test("a group stream that arrives after the subscribe stream's FIN is delivered", async () => { const { subscriber, reader, respond, fin } = await subscribed(version); - await respond({ start: new SubscribeStart(0) }); + await respond({ start: new SubscribeStart([0]) }); const first = groupStream(subscriber, 0); first.write("0.0"); first.finish(); @@ -144,7 +144,7 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver test("a group read across the subscribe stream's FIN is delivered whole", async () => { const { subscriber, reader, respond, fin } = await subscribed(version); - await respond({ start: new SubscribeStart(0) }); + await respond({ start: new SubscribeStart([0]) }); const group = groupStream(subscriber, 0); group.write("0.0"); @@ -163,7 +163,7 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver test("a group reset after the subscribe stream's FIN is not presented as complete", async () => { const { subscriber, reader, respond, fin } = await subscribed(version); - await respond({ start: new SubscribeStart(0) }); + await respond({ start: new SubscribeStart([0]) }); const group = groupStream(subscriber, 0); group.write("0.0"); await respond({ end: new SubscribeEnd(1) }); @@ -201,7 +201,7 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver subscriber = sub.subscriber; const { reader, respond, fin } = sub; const ordered = reader.fork({ maxDelay: GRACE }).ordered(); - await respond({ start: new SubscribeStart(0) }); + await respond({ start: new SubscribeStart([0]) }); const group = groupStream(subscriber, 1); group.write("1.0"); group.finish(); @@ -240,7 +240,7 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver // after SUBSCRIBE_END. The reader receives it before its end, which then comes at once. test("a lower group that lands after SUBSCRIBE_END reaches the reader", async () => { const { subscriber, reader, respond, fin } = await subscribed(version, Milli(60_000)); - await respond({ start: new SubscribeStart(0) }); + await respond({ start: new SubscribeStart([0]) }); const high = groupStream(subscriber, 1); high.finish(); await high.handled; @@ -268,7 +268,7 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver test("a subscription ends without waiting once every group is accounted for", async () => { // A max delay far past the test's patience: only the accounting may end it. const { subscriber, reader, respond, fin } = await subscribed(version, Milli(60_000)); - await respond({ start: new SubscribeStart(0) }); + await respond({ start: new SubscribeStart([0]) }); await respond({ drop: new SubscribeDrop({ start: 0, end: 0, error: 0 }) }); const group = groupStream(subscriber, 1); group.write("1.0"); @@ -285,7 +285,7 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver // bytes read as OLD there. test("a SUBSCRIBE_DROP ends a group still arriving, before the FIN", async () => { const { subscriber, reader, respond } = await subscribed(version, Milli(60_000)); - await respond({ start: new SubscribeStart(0) }); + await respond({ start: new SubscribeStart([0]) }); const arriving = groupStream(subscriber, 0); arriving.write("0.0"); const group = await reader.recvGroup(); @@ -322,7 +322,7 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver // lite-05 specified an inclusive end, so there the group costs only its own stream. test(`a group at or past SUBSCRIBE_END ${version === Version.DRAFT_05 ? "is dropped" : "aborts the track"}`, async () => { const { subscriber, reader, respond } = await subscribed(version); - await respond({ start: new SubscribeStart(0) }); + await respond({ start: new SubscribeStart([0]) }); await respond({ end: new SubscribeEnd(2) }); expect(await reader.finished()).toEqual({ group: 2, frame: 0 }); @@ -346,7 +346,7 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver const { subscriber, reader, respond, fin } = await subscribed(version, Milli(60_000), { start: { included: 1 }, }); - await respond({ start: new SubscribeStart(3) }); + await respond({ start: new SubscribeStart([3]) }); const group = groupStream(subscriber, 3); group.finish(); await group.handled; @@ -361,7 +361,7 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver test("a lowered floor owes the groups it newly asked for", async () => { const maxDelay = Milli(60_000); const { subscriber, respond, fin } = await subscribed(version, maxDelay, { start: { included: 3 } }); - await respond({ start: new SubscribeStart(3) }); + await respond({ start: new SubscribeStart([3]) }); const group = groupStream(subscriber, 3); group.write("3.0"); group.finish(); @@ -400,7 +400,7 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver // below a received group only costs the early boundary. test(`a SUBSCRIBE_END below a group already received ${version === Version.DRAFT_05 ? "finishes clean" : "aborts the track"}`, async () => { const { subscriber, reader, respond, fin } = await subscribed(version); - await respond({ start: new SubscribeStart(0) }); + await respond({ start: new SubscribeStart([0]) }); const group = groupStream(subscriber, 3); group.finish(); await group.handled; @@ -417,7 +417,7 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver test("lite-07 settles a skipped group with SUBSCRIBE_DROP", async () => { const { subscriber, reader, respond, fin } = await subscribed(Version.DRAFT_07, Milli(60_000)); - await respond({ start: new SubscribeStart(0) }); + await respond({ start: new SubscribeStart([0]) }); for (const sequence of [0, 2]) { const group = groupStream(subscriber, sequence); group.write(`${sequence}.0`); @@ -436,7 +436,7 @@ test("lite-07 settles a skipped group with SUBSCRIBE_DROP", async () => { test("lite-07 settles a dropped range without grace", async () => { const { reader, respond, fin } = await subscribed(Version.DRAFT_07, Milli(60_000)); - await respond({ start: new SubscribeStart(0) }); + await respond({ start: new SubscribeStart([0]) }); await respond({ end: new SubscribeEnd(3) }); await respond({ drop: new SubscribeDrop({ start: 0, end: 2, error: 0 }) }); await fin(); @@ -449,7 +449,7 @@ test("lite-07 settles a dropped range without grace", async () => { for (const started of [false, true]) { test(`a bare FIN ${started ? "after SUBSCRIBE_START" : "without responses"} aborts the track`, async () => { const { reader, respond, fin } = await subscribed(Version.DRAFT_05); - if (started) await respond({ start: new SubscribeStart(0) }); + if (started) await respond({ start: new SubscribeStart([0]) }); await fin(); expect(await reader.closed).toBeInstanceOf(ProtocolViolation); await expect(reader.recvGroup()).rejects.toThrow(ProtocolViolation); @@ -467,7 +467,7 @@ test("a subscribe stream reset preserves the publisher's failure", async () => { test("lite-07 FIN waits for the skipped and reset final range to settle", async () => { const { subscriber, reader, respond, fin, subscriberFin } = await subscribed(Version.DRAFT_07, Milli(60_000)); - await respond({ start: new SubscribeStart(0) }); + await respond({ start: new SubscribeStart([0]) }); const first = groupStream(subscriber, 0); first.finish(); await first.handled; @@ -492,7 +492,7 @@ test("lite-07 FIN waits for the skipped and reset final range to settle", async test("draft-07's SUBSCRIBE_START says the largest before any group arrives", async () => { const { reader, respond } = await subscribed(Version.DRAFT_07); - await respond({ start: new SubscribeStart(4, { group: 4, frame: 2 }) }); + await respond({ start: new SubscribeStart([4], { group: 4, frame: 2 }) }); expect(await reader.largest()).toEqual({ group: 4, frame: 2 }); reader.close(); }); diff --git a/js/net/src/lite/track_stream.test.ts b/js/net/src/lite/track_stream.test.ts index 97bdb20ac3f..319d3e752ae 100644 --- a/js/net/src/lite/track_stream.test.ts +++ b/js/net/src/lite/track_stream.test.ts @@ -61,7 +61,7 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver const fin = track.reader.done(); expect(await settlesWithin(fin, 50)).toBe(false); - await encodeSubscribeResponse(sub.writer, { start: new SubscribeStart(0) }, version); + await encodeSubscribeResponse(sub.writer, { start: new SubscribeStart([0]) }, version); expect(await fin).toBe(true); reader.close(); diff --git a/quest/m1/read-backfill.md b/quest/m1/read-backfill.md index 546538966f5..4611998630d 100644 --- a/quest/m1/read-backfill.md +++ b/quest/m1/read-backfill.md @@ -34,13 +34,12 @@ delivers a group that arrives below the cursor as a late backfill; Rust - Don't infer a relative start from the reported largest location: a frameless newest group counts when a relative end resolves but not in `largest()`, sparse moq-transport groups break the arithmetic, and a later - coalesced largest isn't this request's resolution. SUBSCRIBE_OK's `Group` - isn't it either, since the lite draft lets a group between the requested - start and it arrive later. Lite-07 SUBSCRIBE_OK's `Range Start` carries - where each range's start resolved (`lite::SubscribeStart::starts`, 0 on the - wire for one unresolved); the Rust copy only checks its count today, so - carry it to the reader. Settle the moq-transport equivalent or keep today's - first-arrival cursor there. + coalesced largest isn't this request's resolution. Lite-07 SUBSCRIBE_OK's + `Range Start` carries where each range's delivery starts, from which its + groups are owed (`lite::SubscribeStart::starts`, 0 on the wire for a range + with none); the Rust copy only takes the lowest today, so carry each to the + reader. Settle the moq-transport equivalent or keep today's first-arrival + cursor there. - The cursor is range-aware, not just floored: with disjoint ranges it skips straight across an unrequested gap (group 1, then 10 onward) instead of waiting for the latency budget, since no SUBSCRIBE_DROP is owed there. diff --git a/quest/m1/subscribe-ranges/js.md b/quest/m1/subscribe-ranges/js.md index e20fd421ddb..49ad976361d 100644 --- a/quest/m1/subscribe-ranges/js.md +++ b/quest/m1/subscribe-ranges/js.md @@ -24,8 +24,8 @@ update moved the range and the groups that arrived leave it uncovered. Reopen that demand on the same track instead, as the Rust copy does. Mirror the Rust names, including `Time(pts)` ends and their refusal on an untimed track. -The publisher's SUBSCRIBE_OK `Range Start` names only absolute starts today, -since it resolves no relative or time anchor; report where those resolve too. +The publisher resolves no relative or time start, serving it from the oldest +group its budget allows, and SUBSCRIBE_OK `Range Start` says so; resolve them. Also mirror Rust's optional `Subscription::max_delay` ([moq-dev/moq#6171](https://github.com/moq-dev/moq/pull/6171)): a `null` `maxDelay` asks for the publisher's max age alone, dominates when subscriptions diff --git a/rs/moq-net/src/coding/writer.rs b/rs/moq-net/src/coding/writer.rs index 14de4dfb060..e364d987383 100644 --- a/rs/moq-net/src/coding/writer.rs +++ b/rs/moq-net/src/coding/writer.rs @@ -464,9 +464,8 @@ mod tests { writer .buffer(&lite::SubscribeStart { - group: 3, largest: None, - starts: Vec::new(), + starts: vec![Some(3)], }) .unwrap(); writer.buffer(&lite::AnnounceBroadcast::Synced).unwrap(); diff --git a/rs/moq-net/src/fuzz.rs b/rs/moq-net/src/fuzz.rs index ef672482b2f..59a8db095f8 100644 --- a/rs/moq-net/src/fuzz.rs +++ b/rs/moq-net/src/fuzz.rs @@ -829,9 +829,8 @@ impl Default for Messages { }], }, lite_start: lite::SubscribeResponse::Start(lite::SubscribeStart { - group: 1_234, largest: None, - starts: vec![Some(1_000)], + starts: vec![Some(1_234)], }), lite_info: lite::TrackInfo { refresh: None, diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index cfcb00288ea..20b6d6378d7 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -4109,9 +4109,8 @@ impl TrackRun { { self.start = Some(start); writer.buffer(&lite::SubscribeResponse::Start(lite::SubscribeStart { - group: start, largest: Some(largest), - starts: self.starts(), + starts: self.starts(start), }))?; self.raise_floor(start); return Poll::Ready(Ok(ControlFlow::Continue(()))); @@ -4331,11 +4330,12 @@ impl TrackRun { } impl TrackRun { - /// Where each requested range starts, on versions whose SUBSCRIBE_START says. - fn starts(&mut self) -> Vec> { + /// Where each requested range starts once delivery starts at `start`. Before lite-07 + /// there is one range, which starts there. + fn starts(&mut self, start: u64) -> Vec> { match self.ctx.version.has_ranges() { - true => self.track.starts(), - false => Vec::new(), + true => self.track.starts(start), + false => vec![Some(start)], } } @@ -4363,7 +4363,7 @@ impl TrackRun { } let skipped = std::mem::take(&mut self.skipped); self.start = Some(start); - // Only the group: the subscriber derives the start frame from its own request + // Only groups: the subscriber derives the start frame from its own request // (see `lite::SubscribeStart`). The track is live by now, since its first group // was readable. A relay can hold just the first group of a backlog its source // already answered past, so the source's largest says where the live feed is. @@ -4373,9 +4373,8 @@ impl TrackRun { }; let largest = live.max(self.track.peek_largest()); writer.buffer(&lite::SubscribeResponse::Start(lite::SubscribeStart { - group: start, largest, - starts: self.starts(), + starts: self.starts(start), }))?; // The subscriber now owes itself everything from the start, so a range given up // above it is named as it would have been after the start. @@ -6229,11 +6228,11 @@ mod serve_group_test { track.finish().unwrap(); run.await.unwrap(); - // SUBSCRIBE_START at 0 with the range resolved there, a DROP for the skipped group 1 + // SUBSCRIBE_START with the range starting at 0, a DROP for the skipped group 1 // with NOT_FOUND (0x33), which the end gives up on at once, and SUBSCRIBE_END at 3. assert_eq!( *log.writes.lock().unwrap(), - [0, 5, 0, 1, 0, 1, 1, 2, 3, 1, 1, 0x33, 1, 1, 3] + [0, 4, 1, 0, 1, 1, 2, 3, 1, 1, 0x33, 1, 1, 3] ); } @@ -6264,7 +6263,10 @@ mod serve_group_test { let lite::SubscribeResponse::Start(start) = msg else { panic!("expected SUBSCRIBE_START, got {msg:?}"); }; - assert_eq!((start.group, start.largest), (0, Some(track::Position::group(4)))); + assert_eq!( + (start.starts, start.largest), + (vec![Some(0)], Some(track::Position::group(4))) + ); } /// The SUBSCRIBE_DROP ranges a run loop's subscribe stream carried, once flushed. @@ -6753,9 +6755,9 @@ mod serve_group_test { let lite::SubscribeResponse::Start(start) = msg else { panic!("expected SUBSCRIBE_START, got {msg:?}"); }; - starts.push(start.group); + starts.push(start.starts[0]); } - assert_eq!(starts, [2, 2], "{version}: cold and warm relay starts differ"); + assert_eq!(starts, [Some(2); 2], "{version}: cold and warm relay starts differ"); } } @@ -6786,7 +6788,7 @@ mod serve_group_test { let lite::SubscribeResponse::Start(start) = msg else { panic!("expected SUBSCRIBE_START, got {msg:?}"); }; - assert_eq!(start.group, 1_000_000, "{version}: named a dropped group"); + assert_eq!(start.starts, [Some(1_000_000)], "{version}: named a dropped group"); } } @@ -7157,7 +7159,7 @@ mod serve_group_test { let writes = self.session.log.writes.lock().unwrap(); matches!( lite::SubscribeResponse::decode_slice(&writes, Version::Lite07), - Ok((lite::SubscribeResponse::Start(start), _)) if start.group == group + Ok((lite::SubscribeResponse::Start(start), _)) if start.starts == [Some(group)] ) } } @@ -7370,7 +7372,7 @@ mod serve_group_test { assert_eq!( *log.writes.lock().unwrap(), // The DROP carries OLD (0x34). - [0, 5, 0, 1, 0, 1, 1, 2, 3, 0, 0, 0x34, 1, 1, 2], + [0, 4, 1, 0, 1, 1, 2, 3, 0, 0, 0x34, 1, 1, 2], "the expired group is dropped and the end precedes stream credit" ); @@ -7382,7 +7384,7 @@ mod serve_group_test { run.await.unwrap(); assert_eq!( *log.writes.lock().unwrap(), - [0, 5, 0, 1, 0, 1, 1, 2, 3, 0, 0, 0x34, 1, 1, 2] + [0, 4, 1, 0, 1, 1, 2, 3, 0, 0, 0x34, 1, 1, 2] ); } } diff --git a/rs/moq-net/src/lite/subscribe.rs b/rs/moq-net/src/lite/subscribe.rs index 6874846a85e..b4b8f6b4f6b 100644 --- a/rs/moq-net/src/lite/subscribe.rs +++ b/rs/moq-net/src/lite/subscribe.rs @@ -494,22 +494,20 @@ impl<'a> Message<'a> for SubscribeOk { } } -/// Resolves the absolute start group of a Lite05+ subscription. The first message -/// the publisher sends, once the start group is known. A value greater than the -/// requested start implicitly drops the leading range. +/// Resolves where a Lite05+ subscription starts. The first message the publisher sends, +/// once the start is known. A start above the requested one implicitly drops the groups +/// below it. /// /// There is no start *frame*: a partial group is only served to a subscriber that asked -/// for one, so delivery begins either at the requested `Frame Start` (when this is the -/// requested group) or at frame 0 (when the publisher resolved to a later one). A -/// subscriber that asked for group 5 frame 15 and receives group 6 starts at frame 0. +/// for one, so a range begins at its requested frame when it starts at its requested +/// group, and at frame 0 of a later one otherwise. #[derive(Clone, Debug)] pub struct SubscribeStart { - pub group: u64, /// The publisher's largest (group, frame) when it answered, `None` for a track with /// nothing yet. Lite07+ only; not on the wire before, where it decodes as `None`. pub largest: Option, - /// The group each range's start resolved to, by index, `None` for one that is empty or - /// unresolved. Lite07+ only; empty before. + /// The group each range's delivery starts at, by index, `None` for a range with no + /// group there. Before lite-07 the wire carries the one range's start as `Group`. pub starts: Vec>, } @@ -519,44 +517,46 @@ impl<'a> Message<'a> for SubscribeStart { if !version.has_track_stream() { return Err(DecodeError::Version); } - let group = r.varint()?; - let largest = match version.has_largest() { - // Group + 1, so 0 is a track with nothing yet; the frame follows only otherwise. - true => match r.varint_opt()? { - Some(group) => Some(crate::track::Position { - group, - frame: r.varint()?, - }), - None => None, - }, - false => None, + // Lite-07 added the largest position along with the ranges. + if !version.has_ranges() { + return Ok(Self { + largest: None, + starts: vec![Some(r.varint()?)], + }); + } + // Group + 1, so 0 is a track with nothing yet; the frame follows only otherwise. + let largest = match r.varint_opt()? { + Some(group) => Some(crate::track::Position { + group, + frame: r.varint()?, + }), + None => None, }; // Not preallocated: each start costs a byte of the bounded message. let mut starts = Vec::new(); - if version.has_ranges() { - for _ in 0..r.varint()? { - starts.push(r.varint_opt()?); - } + for _ in 0..r.varint()? { + starts.push(r.varint_opt()?); } - Ok(Self { group, largest, starts }) + Ok(Self { largest, starts }) } fn encode_msg(&self, w: &mut Encoder<'_>, version: Version) -> Result<(), EncodeError> { if !version.has_track_stream() { return Err(EncodeError::Version); } - w.varint(self.group)?; - if version.has_largest() { - w.varint_opt(self.largest.map(|largest| largest.group))?; - if let Some(largest) = self.largest { - w.varint(largest.frame)?; - } + if !version.has_ranges() { + let [Some(group)] = self.starts[..] else { + return Err(EncodeError::InvalidState); + }; + return w.varint(group); } - if version.has_ranges() { - w.varint(self.starts.len() as u64)?; - for start in &self.starts { - w.varint_opt(*start)?; - } + w.varint_opt(self.largest.map(|largest| largest.group))?; + if let Some(largest) = self.largest { + w.varint(largest.frame)?; + } + w.varint(self.starts.len() as u64)?; + for start in &self.starts { + w.varint_opt(*start)?; } Ok(()) } @@ -817,16 +817,15 @@ mod test { #[test] fn subscribe_start_roundtrips_on_lite05() { let resp = SubscribeResponse::Start(SubscribeStart { - group: 42, largest: None, - starts: Vec::new(), + starts: vec![Some(42)], }); let mut buf = Vec::new(); resp.encode(&mut Encoder::new(&mut buf, Version::Lite05.into()), Version::Lite05) .unwrap(); let mut slice = buf.as_slice(); match crate::coding::decode_buf(&mut slice, Version::Lite05, |r, v| SubscribeResponse::decode(r, v)).unwrap() { - SubscribeResponse::Start(start) => assert_eq!(start.group, 42), + SubscribeResponse::Start(start) => assert_eq!(start.starts, [Some(42)]), other => panic!("expected Start, got {other:?}"), } } @@ -837,56 +836,39 @@ mod test { fn subscribe_start_carries_the_largest_position_on_lite07() { for largest in [None, Some(crate::track::Position { group: 3, frame: 2 })] { let resp = SubscribeResponse::Start(SubscribeStart { - group: 4, largest, - starts: Vec::new(), + starts: vec![Some(4)], }); - let mut buf = Vec::new(); - resp.encode( - &mut crate::coding::Encoder::new(&mut buf, Version::Lite07.into()), - Version::Lite07, - ) - .unwrap(); + let buf = resp.encode_bytes(Version::Lite07).unwrap(); match SubscribeResponse::decode_slice(&buf, Version::Lite07).unwrap().0 { - SubscribeResponse::Start(start) => assert_eq!((start.group, start.largest), (4, largest)), + SubscribeResponse::Start(start) => assert_eq!(start.largest, largest), other => panic!("expected Start, got {other:?}"), } } let resp = SubscribeResponse::Start(SubscribeStart { - group: 4, largest: Some(crate::track::Position { group: 3, frame: 2 }), - starts: Vec::new(), + starts: vec![Some(4)], }); - let mut buf = Vec::new(); - resp.encode( - &mut crate::coding::Encoder::new(&mut buf, Version::Lite06.into()), - Version::Lite06, - ) - .unwrap(); - assert_eq!(buf, [0, 1, 4], "lite-06 has no largest position"); + let buf = resp.encode_bytes(Version::Lite06).unwrap(); + assert_eq!(buf.as_ref(), [0, 1, 4], "lite-06 has no largest position"); } - /// Lite-07 says where each range's start resolved, `None` for one that didn't; earlier - /// versions leave it off the wire. + /// Lite-07 says where each range starts in place of the one `Group` earlier versions + /// carry, which can name only a single range. #[test] fn subscribe_start_carries_the_range_starts_on_lite07() { let start = SubscribeStart { - group: 4, largest: None, starts: vec![Some(2), None, Some(0)], }; let resp = SubscribeResponse::Start(start.clone()); let buf = resp.encode_bytes(Version::Lite07).unwrap(); - assert_eq!(buf.as_ref(), [0, 6, 4, 0, 3, 3, 0, 1]); + assert_eq!(buf.as_ref(), [0, 5, 0, 3, 3, 0, 1]); match SubscribeResponse::decode_slice(&buf, Version::Lite07).unwrap().0 { SubscribeResponse::Start(decoded) => assert_eq!(decoded.starts, start.starts), other => panic!("expected Start, got {other:?}"), } - let buf = resp.encode_bytes(Version::Lite06).unwrap(); - match SubscribeResponse::decode_slice(&buf, Version::Lite06).unwrap().0 { - SubscribeResponse::Start(decoded) => assert!(decoded.starts.is_empty()), - other => panic!("expected Start, got {other:?}"), - } + assert!(resp.encode_bytes(Version::Lite06).is_err()); } #[test] diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 48bfaa96225..0aa17dfb6f5 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -3444,7 +3444,6 @@ mod tests { for version in [Version::Lite05, Version::Lite06, Version::Lite07] { let mut responses = Vec::new(); lite::SubscribeResponse::Start(lite::SubscribeStart { - group: 0, largest: None, starts: vec![Some(0)], }) @@ -3563,7 +3562,6 @@ mod tests { let mut answer = Vec::new(); let mut encoder = crate::coding::Encoder::new(&mut answer, version.into()); lite::SubscribeResponse::Start(lite::SubscribeStart { - group: 0, largest: None, starts: vec![Some(0)], }) @@ -3618,7 +3616,6 @@ mod tests { let largest = track::Position { group: 4, frame: 2 }; let mut responses = Vec::new(); lite::SubscribeResponse::Start(lite::SubscribeStart { - group: 4, largest: Some(largest), starts: vec![Some(4)], }) @@ -3681,7 +3678,6 @@ mod tests { let code = crate::coding::StreamCodes::encode_stream_code(&Version::Lite07, &crate::StreamError::App(5)); for resp in [ lite::SubscribeResponse::Start(lite::SubscribeStart { - group: 0, largest: None, starts: vec![Some(0)], }), @@ -3822,7 +3818,6 @@ mod tests { let mut responses = Vec::new(); if started { lite::SubscribeResponse::Start(lite::SubscribeStart { - group: 0, largest: None, starts: vec![Some(0)], }) @@ -8008,11 +8003,12 @@ impl ServeLoop { } active.end = Some(end.clone()); } - // SUBSCRIBE_START names the first group this feed serves: - // the publisher skipped everything below it (e.g. it could - // not serve the requested frame). Record it as a drop - // signal, so a reader waiting on a skipped group - // fails over instead of stalling on a live route. + // SUBSCRIBE_START names where each range's delivery starts, + // the lowest being the first group this feed serves: the + // publisher skipped everything below it (e.g. it could not + // serve the requested frame). Record it as a drop signal, + // so a reader waiting on a skipped group fails over + // instead of stalling on a live route. lite::SubscribeResponse::Start(start) => { // One resolved start per range the SUBSCRIBE carried, unless an // update the publisher read first replaced them. @@ -8022,9 +8018,11 @@ impl ServeLoop { tracing::warn!(track = %serve.name, starts = start.starts.len(), ranges = active.ranges.len(), "subscribe start resolves other ranges"); return Poll::Ready(ServeEnd::GiveBack(Error::ProtocolViolation)); } + // No range starting anywhere leaves nothing owed. + let served = start.starts.iter().flatten().min().copied(); // Where the live feed is, on versions whose answer says. if serve.subscriber.version.has_largest() { - self.serving.set_live(start.largest, Some(start.group)); + self.serving.set_live(start.largest, served); if let Some(largest) = start.largest { self.serving.report_largest(largest); } @@ -8036,16 +8034,16 @@ impl ServeLoop { // moved back restores it); elsewhere the // request-tracked floor stands rather than a guess. if active.start == active.requested { - let _ = self.serving.start_at(start.group); + let _ = self.serving.start_at(served); } - active.served = Some(start.group); + active.served = served; // The groups the SUBSCRIBE asked for below it are not // waited for, whatever the demand asks later. One that // still arrives is delivered, so they are not dropped. if let Some(requested) = active.requested - && let Ok(mut tail) = active.tail.write() + && let Some(served) = served && let Ok(mut tail) = active.tail.write() { - tail.account(requested..start.group, serve.subscriber.runtime.now()); + tail.account(requested..served, serve.subscriber.runtime.now()); } } // The publisher will never send these groups, so they diff --git a/rs/moq-net/src/model/resume.rs b/rs/moq-net/src/model/resume.rs index 3b86674eaaf..b8b3abff9aa 100644 --- a/rs/moq-net/src/model/resume.rs +++ b/rs/moq-net/src/model/resume.rs @@ -718,8 +718,8 @@ impl Subscriber { } /// Where each of the reader's ranges starts; see [`track::Subscriber::starts`]. - pub(super) fn starts(&self) -> Vec> { - self.reader().window().starts() + pub(super) fn starts(&self, from: u64) -> Vec> { + self.reader().window().starts(from) } /// The lowest group at or above `from` the reader's ranges cover; see diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index 3e4ded40c8b..9899f2c735d 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -467,12 +467,16 @@ impl Window { resolved } - /// Where each range starts, in the order given: `None` for one that is empty, even only - /// once resolved, or still waits on a first group. - pub fn starts(&self) -> Vec> { + /// Where each range starts once delivery starts at `from`, in the order given: `None` + /// for one with no group there, or still waiting on a first group. + pub fn starts(&self, from: u64) -> Vec> { self.resolved() .into_iter() - .map(|r| r.filter(|r| r.end.is_none_or(|end| end > r.start)).map(|r| r.start)) + .map(|r| { + let r = r?; + let start = r.start.max(from); + r.end.is_none_or(|end| end > start).then_some(start) + }) .collect() } @@ -775,6 +779,9 @@ mod tests { Some(Resolved { start: 7, end: None }), ] ); + // Delivery starting inside a range raises its start; past its end leaves it none. + assert_eq!(window.starts(3), [Some(3), Some(7)]); + assert_eq!(window.starts(5), [None, Some(7)]); // Nothing asked for is nothing in range, floored nowhere. let window = Window::new(&[], None, &Untimed).unwrap(); diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 07c53e2f6d5..0b692559482 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -7475,19 +7475,20 @@ impl Subscriber { Some(start?.max(self.bounds().start)) } - /// Where each of this subscriber's ranges starts, by index, as resolved here: `None` for - /// one that is empty or still waits on the first group from the live feed. + /// Where each of this subscriber's ranges starts, by index, as resolved here and raised + /// to `from`, where delivery starts: `None` for one with no group there or still waiting + /// on the first group from the live feed. /// /// A publisher tells its peer where a relative or time start landed, which the peer /// can't work out from the groups it receives. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] - pub(crate) fn starts(&mut self) -> Vec> { + pub(crate) fn starts(&mut self, from: u64) -> Vec> { match &mut self.inner { Inner::Plain(cursor) => { cursor.sync(); - cursor.filter.window.starts() + cursor.filter.window.starts(from) } - Inner::Resume(resume, _) => resume.starts(), + Inner::Resume(resume, _) => resume.starts(from), } } From 4dcc35caa6655186150f8cf3b3d9b9fcc82bbac7 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 28 Oct 2026 02:29:55 -0700 Subject: [PATCH 10/30] test(interop): pass SUBSCRIBE_OK's starts to the bare-FIN harness Co-Authored-By: Claude Opus 5.5 --- test/interop/bare-fin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/interop/bare-fin.ts b/test/interop/bare-fin.ts index 2448935fe64..1b7ce09e5f8 100644 --- a/test/interop/bare-fin.ts +++ b/test/interop/bare-fin.ts @@ -52,7 +52,7 @@ if (version !== undefined) { assert(peer); assert.equal(await peer.reader.u53(), StreamId.Subscribe); await Subscribe.decode(peer.reader, version); - if (input.started) await encodeSubscribeResponse(output, { start: new SubscribeStart(0) }, version); + if (input.started) await encodeSubscribeResponse(output, { start: new SubscribeStart([0]) }, version); if (input.clean) await encodeSubscribeResponse(output, { end: new SubscribeEnd(0) }, version); } else { assert.equal(input.version, "moqt-18"); From 71b994da426aba03b1f5bb0dca3d7887e544d6c2 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 29 Oct 2026 09:10:48 -0700 Subject: [PATCH 11/30] fix(net): owe each lite-07 range from its own start, and check JS start counts A covered FIN counted a relative or time range from the lowest start of any range, so disjoint ranges owed the gap between them. The JS subscriber now rejects a lite-07 SUBSCRIBE_START whose count isn't the one range it sends. Co-Authored-By: Claude Opus 5.5 --- js/net/src/lite/subscriber.test.ts | 22 ++++++++++++++ js/net/src/lite/subscriber.ts | 3 ++ rs/moq-net/src/lite/subscriber.rs | 46 ++++++++++++++++++++++++++++-- 3 files changed, 69 insertions(+), 2 deletions(-) diff --git a/js/net/src/lite/subscriber.test.ts b/js/net/src/lite/subscriber.test.ts index f9c7eb64218..e86aabee861 100644 --- a/js/net/src/lite/subscriber.test.ts +++ b/js/net/src/lite/subscriber.test.ts @@ -2221,6 +2221,28 @@ test("a publisher FIN without SUBSCRIBE_END still violates an open subscription" } }); +// Our SUBSCRIBE carries one range, so a lite-07 SUBSCRIBE_START with any other count is malformed. +test.each([[[]], [[0, 5]]])("a lite-07 SUBSCRIBE_START resolving %j violates the protocol", async (starts) => { + const { quic, streams, opened, closes } = fakeSession(); + const subscriber = new Subscriber(quic, Version.DRAFT_07, HopSchema.parse(1n)); + const broadcast = subscriber.consume(Path.from("room")); + const track = broadcast.track("video").subscribe(); + try { + await opened(0); + await answerTrackInfo(streams[0], Version.DRAFT_07); + const request = await opened(1); + await request.reading; + await respond(request, Version.DRAFT_07, [{ start: new SubscribeStart(starts) }]); + expect(await track.closed).toBeInstanceOf(ProtocolViolation); + await drainReactions(); + expect(closes.map((info) => info?.closeCode)).toEqual([SessionCode.ProtocolViolation]); + } finally { + track.close(); + broadcast.close(); + subscriber.close(); + } +}); + // A lite-07 publisher FINs a bounded subscription without SUBSCRIBE_END once it covered the range, // which ends the subscription cleanly rather than violating the protocol. Covering a range is // not ending the track, so no end is declared. diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index fa2071e22ae..546052e78b3 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -1474,6 +1474,9 @@ export class Subscriber { if ("ok" in resp) throw new ProtocolViolation("duplicate SUBSCRIBE_OK"); if ("start" in resp) { + // Every SUBSCRIBE and update we send carries one range, so one start answers it. + if (hasRanges(this.version) && resp.start.starts.length !== 1) + throw new ProtocolViolation(`SUBSCRIBE_START resolves ${resp.start.starts.length} ranges, not 1`); // The lowest range start is the first group served; none leaves nothing owed. entry.start = resp.start.starts.reduce( (lowest, start) => diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 0684a6baf54..0332b35dff9 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -2150,6 +2150,7 @@ mod tests { ranges: vec![lite::Range::OLDEST], tail: tail.clone(), served: None, + starts: Vec::new(), end: None, wanted: None, unanswered: None, @@ -2207,6 +2208,34 @@ mod tests { assert_eq!(active.covered(), Some(std::iter::once(0..2).collect())); } + /// A covered FIN owes a relative range only from its own SUBSCRIBE_START, not from the + /// lowest start of a disjoint range below it. + #[moq_clock::test] + async fn a_covered_fin_owes_each_range_from_its_own_start() { + let tail = kio::Producer::new(Tail::default()); + let Sub::Active(mut active) = feed_sub(&tail).await else { + unreachable!("feed_sub is active") + }; + let end = |group| Some(lite::Anchor::Absolute(crate::track::Position { group, frame: 0 })); + active.ranges = vec![ + lite::Range::span(Some(0), 0, Some(0), None), + lite::Range { + start: Some(lite::Anchor::Relative(0)), + end: end(11), + }, + lite::Range { + start: Some(lite::Anchor::Relative(0)), + end: end(20), + }, + ]; + active.served = Some(0); + active.starts = vec![Some(0), Some(10), None]; + assert_eq!(active.covered(), Some(vec![0..1, 10..11, 20..20])); + + active.updated = true; + assert_eq!(active.covered(), Some(vec![0..1, 0..11, 0..20]), "an update leaves only the lowest"); + } + /// QUIC does not order streams, so group 2's header can reach the relay before group 1's. /// A fetch held for group 1 keeps waiting through the grace and is served when it lands. #[moq_clock::test] @@ -2286,6 +2315,7 @@ mod tests { ranges: vec![lite::Range::span(Some(floor), 0, None, None)], tail: Default::default(), served: None, + starts: Vec::new(), end: None, wanted: None, unanswered: None, @@ -2750,6 +2780,7 @@ mod tests { ranges: vec![lite::Range::span(Some(2), 0, None, None)], tail: Default::default(), served: None, + starts: Vec::new(), end: Some(lite::SubscribeEnd { group: 10 }), wanted: None, unanswered: None, @@ -6309,6 +6340,9 @@ struct SubStream { tail: kio::Producer, /// The first group the publisher serves (SUBSCRIBE_START), once declared. served: Option, + /// Where SUBSCRIBE_START says each range's delivery starts, `None` for one with nothing + /// to deliver. It matches `ranges` until an update changes them. + starts: Vec>, /// The track's exclusive end (SUBSCRIBE_END), once declared. end: Option, /// The refresh join's wanted group, restated in every SUBSCRIBE_UPDATE. @@ -6358,14 +6392,20 @@ impl SubStream { }; self.ranges .iter() - .map(|range| { + .enumerate() + .map(|(index, range)| { // Our absolute ends name a group's first frame (see `wire_ranges`). let Some(lite::Anchor::Absolute(end)) = range.end else { return None; }; let start = match range.start { Some(lite::Anchor::Absolute(start)) => start.group, - _ => served, + // The range's own start, so a gap below it isn't owed; after an + // update only the lowest start is known. + _ => match self.starts.get(index) { + Some(start) if !self.updated => start.unwrap_or(end.group), + _ => served, + }, }; Some(start..end.group) }) @@ -7073,6 +7113,7 @@ impl Establish { requested: self.subscription.start(), tail: self.tail.clone(), served: None, + starts: Vec::new(), end: None, wanted: None, bounded: bounded(&self.ranges), @@ -8027,6 +8068,7 @@ impl ServeLoop { }; } active.served = served; + active.starts = start.starts.clone(); // The groups the SUBSCRIBE asked for below it are not // waited for, whatever the demand asks later. One that // still arrives is delivered, so they are not dropped. From 2d5d59c4163d92111e594ba8f0c71c4cbdcef8ec Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 4 Nov 2026 06:26:59 -0800 Subject: [PATCH 12/30] chore(quest): claim quest/m1/subscribe-ranges/lite.md Quest-Claim: 532cdbb2e3c5ba1c From 9f824d3fd373f844df473b3e5d11f8c681f8b430 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 4 Nov 2026 06:43:23 -0800 Subject: [PATCH 13/30] feat(net)!: lite-07 Range IDs and SUBSCRIBE_STARTED in Rust Each lite-07 range takes an implicit Range ID, and SUBSCRIBE_UPDATE edits the live ranges instead of restating them. SUBSCRIBE_OK answers the SUBSCRIBE's ranges, and SUBSCRIBE_STARTED (type 0x3) reports where each range an update adds starts. A range resolves once: an update that keeps it live never counts its relative anchors back from a newer group. Co-Authored-By: Claude Opus 5.5 --- drafts/draft-lcurley-moq-lite.md | 56 ++- rs/moq-net/src/fuzz.rs | 5 +- rs/moq-net/src/lite/publisher.rs | 356 +++++++++++++-- rs/moq-net/src/lite/subscribe.rs | 451 ++++++++++++++++++-- rs/moq-net/src/lite/subscriber.rs | 222 +++++----- rs/moq-net/src/model/resume.rs | 5 + rs/moq-net/src/model/subscription/window.rs | 18 +- rs/moq-net/src/model/track.rs | 16 + 8 files changed, 954 insertions(+), 175 deletions(-) diff --git a/drafts/draft-lcurley-moq-lite.md b/drafts/draft-lcurley-moq-lite.md index 027ac9ac54c..57aa741f0c0 100644 --- a/drafts/draft-lcurley-moq-lite.md +++ b/drafts/draft-lcurley-moq-lite.md @@ -501,7 +501,7 @@ An advertiser signals capacity through its route alone, withdrawing or re-pricin A subscriber opens Subscribe Streams to request a Track. The subscriber MUST start a Subscribe Stream with a SUBSCRIBE message followed by any number of SUBSCRIBE_UPDATE messages. -The publisher replies with a SUBSCRIBE_OK message once each Range's start is resolved, and a SUBSCRIBE_FINISH message once the Track's final group is known. +The publisher replies with a SUBSCRIBE_OK message once each Range's start is resolved, a SUBSCRIBE_STARTED message for each Range an update adds once its start is resolved, and a SUBSCRIBE_FINISH message once the Track's final group is known. For a live track the publisher MAY withhold SUBSCRIBE_OK until the first matching group resolves the start, and withholds the SUBSCRIBE_DROP messages for groups it gives up meanwhile until it sends SUBSCRIBE_OK. It MUST NOT withhold it past the point where no group can resolve it: once every Group in a subscription whose every Range has an `End` other than None has been given up, or once the Track has finished, the publisher sends SUBSCRIBE_OK and then the SUBSCRIBE_DROP messages it withheld. A Track that finished before producing any Group is the one case with nothing to resolve: SUBSCRIBE_FINISH with a Group of 0 needs no SUBSCRIBE_OK. @@ -515,7 +515,7 @@ The subscriber needs the track's TRACK_INFO (notably its timescale) to interpret The publisher sends SUBSCRIBE_OK once the start is resolved, and SUBSCRIBE_FINISH as soon as the exclusive final group is known (see [SUBSCRIBE_OK](#subscribe-ok) and [SUBSCRIBE_FINISH](#subscribe-finish)). Neither closes the stream: the publisher keeps reading SUBSCRIBE_UPDATE messages, so an update can still widen the Ranges, including into an ended Track's history, and the publisher serves or drops what it newly covers. -The subscriber judges a Range complete from what it observed: the Groups it received, the SUBSCRIBE_DROP messages, the Range Starts in SUBSCRIBE_OK, and SUBSCRIBE_FINISH. +The subscriber judges a Range complete from what it observed: the Groups it received, the SUBSCRIBE_DROP messages, the Range Starts in SUBSCRIBE_OK and SUBSCRIBE_STARTED, and SUBSCRIBE_FINISH. Drops and Groups name absolute sequences, so the subscriber judges them against its latest Ranges, and an update still in flight changes nothing it has counted. The publisher never closes its side first: a subscription whose every Range has an `End` other than None and whose every Group has been delivered or dropped stays open for an update to widen it. @@ -1308,6 +1308,7 @@ A value of 0 is a PROTOCOL_VIOLATION, since a subscription that asks for nothing **Range**: A span of the Track to deliver, from its `Start` up to its `End`. The subscription is the union of its Ranges: the publisher delivers each Group inside any of them once, in `Group Order` across all of them, and accounts with SUBSCRIBE_DROP for every Group it will not deliver. +Each Range implicitly takes the next Range ID on the stream, starting at 0, which a [SUBSCRIBE_UPDATE](#subscribe-update) uses to retire it. **Anchor Type**: Which fields follow, and what the Anchor names: @@ -1319,6 +1320,7 @@ Which fields follow, and what the Anchor names: Any other value is a PROTOCOL_VIOLATION. The publisher resolves a Relative or Time Anchor against the Track as it holds it. +A Range resolves once: an update that leaves it live does not resolve it again. A `Start` is a floor, not a request; `Subscriber Max Age` is the only thing that asks for data. The publisher SHOULD start at the oldest group at or above the floor that [Expiration](#expiration) has not expired, and MUST NOT deliver an older one. @@ -1331,10 +1333,11 @@ An Absolute `Start` past frame 0 delivers its group from that frame; a start res A subscriber MAY name any start frame, as described in [Positions](#positions). An Absolute `End` past frame 0 delivers its group up to that frame. -## SUBSCRIBE_UPDATE +## SUBSCRIBE_UPDATE {#subscribe-update} A subscriber can modify a subscription with a SUBSCRIBE_UPDATE message. A subscriber MAY send multiple SUBSCRIBE_UPDATE messages to update the subscription. -Its Ranges replace the previous ones, growing or shrinking the subscription. +Every field but the Ranges restates the subscription, and the latest one applies. +The Ranges are an edit, growing or shrinking the subscription: the update retires the live Ranges it names by Range ID, then adds its own, each taking the next Range ID. Lowering a `Start` covers the groups below the old one that the publisher still holds, not only those that arrive later; a group already sent is not sent again. ~~~ @@ -1344,6 +1347,8 @@ SUBSCRIBE_UPDATE Message { Group Order (8) Subscriber Max Age (i) Subscriber Min Age (i) + Remove Count (i) + Range ID (i) ... Range Count (i) Range (..) ... Wanted Group (i) @@ -1353,6 +1358,14 @@ SUBSCRIBE_UPDATE Message { See [SUBSCRIBE](#subscribe) for information about each field. Moving a `Start` forward within a group that is already being delivered does not retract frames the publisher has already sent; it only bounds what the publisher sends from here on. +**Remove Count** and **Range ID**: +The live Ranges to retire. +A Range ID that was never assigned, or is already retired, including earlier in the same list, is a PROTOCOL_VIOLATION. + +**Range Count** and **Range**: +The Ranges to add, which may be none. +The live Ranges after the edit MUST NOT be empty, and MUST fit in one SUBSCRIBE: once encoded they take at most 65,535 bytes. +A publisher closes the session with a PROTOCOL_VIOLATION otherwise. **Wanted Group**: The newest group the subscriber's live-edge joins want, encoded as the sequence plus one; `0` means none. @@ -1572,11 +1585,12 @@ The largest position the publisher knows of for the track when it answers: the l A subscriber takes it as where the live feed is: a relay holding groups from an earlier subscription serves them from cache only once this says they are current, which a publisher answering promptly lets it do without waiting for a frame. **Range Start**: -One per Range the publisher is serving, in order: the Group where the Range's delivery starts, plus one, so a subscriber learns where a Relative or Time Anchor landed. -0 means the Range has no Group to deliver. +One per Range of the SUBSCRIBE, in Range ID order: the Group where the Range's delivery starts, plus one, so a subscriber learns where a Relative or Time Anchor landed. +0 means the Range has no Group to deliver, including one an update retired before the answer. The Range's Groups from this one are owed as described in [SUBSCRIBE_DROP](#subscribe-drop); an earlier one is not, though it MAY still arrive. A Range whose `Range Start` is its requested start Group begins at the requested frame, and otherwise at frame 0 (see [Positions](#positions)). -The Ranges are those of the SUBSCRIBE unless the publisher applied a SUBSCRIBE_UPDATE before answering; a subscriber that sent none MUST treat a count other than its number of Ranges as a PROTOCOL_VIOLATION. +A subscriber MUST treat a `Range Start Count` other than the SUBSCRIBE's `Range Count` as a PROTOCOL_VIOLATION. +A Range an update adds has its start reported by [SUBSCRIBE_STARTED](#subscribe-started) instead, even one added before SUBSCRIBE_OK. ## SUBSCRIBE_FINISH {#subscribe-finish} A SUBSCRIBE_FINISH message is sent by the publisher to signal that no group at or after a given sequence will be produced. @@ -1605,6 +1619,32 @@ Groups below the end stay deliverable either way; a subscriber reaching the end The publisher SHOULD send SUBSCRIBE_FINISH as soon as the exclusive final group is known, even while lower groups are still in flight. +## SUBSCRIBE_STARTED {#subscribe-started} +A SUBSCRIBE_STARTED message resolves where a Range that a [SUBSCRIBE_UPDATE](#subscribe-update) added starts. + +~~~ +SUBSCRIBE_STARTED Message { + Type (i) = 0x3 + Message Length (i) + Range ID (i) + Range Start (i) +} +~~~ + +**Type**: +Set to 0x3 to indicate a SUBSCRIBE_STARTED message. + +**Range ID**: +The Range it resolves. +A Range ID the subscription never assigned, one of the SUBSCRIBE's Ranges, or one already resolved is a PROTOCOL_VIOLATION. +A Range the subscriber has since retired is ignored. + +**Range Start**: +As in [SUBSCRIBE_OK](#subscribe-ok), and the Range's Groups from it are owed the same way. + +The publisher sends it after SUBSCRIBE_OK, once the Range's start is resolved, and MUST NOT withhold it past the point where no group can resolve it, as for SUBSCRIBE_OK. +A Range retired before its start resolved gets none. + ## SUBSCRIBE_DROP {#subscribe-drop} A publisher names groups it will not deliver with SUBSCRIBE_DROP on the Subscribe Stream. @@ -1947,6 +1987,8 @@ The `Message Length` describes the payload size on the wire. - SUBSCRIBE may start at any frame, including when the subscriber no longer holds the prefix. - A SUBSCRIBE_UPDATE that lowers a `Start` covers the groups below the old start that the publisher still holds, not only those that arrive later. A group already sent is not sent again. - Replaced SUBSCRIBE_OK `Group` with a `Range Start` per Range: the Group where that Range's delivery starts, including at a Relative or Time Anchor, from which its Groups are owed. +- Each Range takes an implicit Range ID, and SUBSCRIBE_UPDATE edits the live Ranges instead of restating them: `Remove Count` Range IDs to retire, then the Ranges to add. The live Ranges must stay non-empty and fit in one SUBSCRIBE. A Range resolves its Anchors once, however many updates leave it live. +- SUBSCRIBE_OK's Range Starts answer the SUBSCRIBE's Ranges, and added SUBSCRIBE_STARTED (type 0x3) resolving where each Range an update adds starts. - Added `Largest Group` and `Largest Frame` to SUBSCRIBE_OK: the publisher's largest position when it answers, which a subscriber takes as where the live feed is. A publisher MUST answer at once when the requested start is past it. - Renamed SUBSCRIBE_END to SUBSCRIBE_FINISH, matching TRACKS_FINISH: it ends the Track, not the subscription. - Added `Error Code` to SUBSCRIBE_FINISH: a track can end with an error while keeping the groups below its end. diff --git a/rs/moq-net/src/fuzz.rs b/rs/moq-net/src/fuzz.rs index 07c43d80fa1..4734539e11e 100644 --- a/rs/moq-net/src/fuzz.rs +++ b/rs/moq-net/src/fuzz.rs @@ -838,10 +838,11 @@ impl Default for Messages { priority: 4, max_delay: Duration::from_millis(500), min_delay: None, - ranges: vec![lite::Range { + // Lite-06, the bench's version, restates the ranges. + ranges: lite::RangeEdit::Replace(vec![lite::Range { start: Some(lite::Anchor::Absolute(crate::track::Position::group(1_000))), end: Some(lite::Anchor::Absolute(crate::track::Position::group(2_001))), - }], + }]), }, lite_start: lite::SubscribeResponse::Start(lite::SubscribeStart { largest: None, diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 6d3d12fb213..4f257e978b2 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -1663,8 +1663,8 @@ trait Request: Sized { const HOLDS: bool; /// The message that opens the stream, owned so it outlives the bytes it came from. type Message: std::fmt::Debug; - /// What the requester may send after the request; [`NoUpdate`] for nothing. - type Update: for<'a> Decode<'a, Version> + std::fmt::Debug; + /// What the requester may send after the request, as it travels; [`NoUpdate`] for nothing. + type Update: Resolve; /// Decode the message that opens the stream. fn decode(r: &mut Decoder<'_>, version: Version) -> Result; @@ -1689,7 +1689,7 @@ trait Request: Sized { ) -> Result; /// Apply an update from the requester. - fn update(&mut self, update: Self::Update); + fn update(&mut self, update: Resolved); /// Write the reply: `Continue` after each unit of work, so the requester is checked /// between them even while work stays ready, and `Break` once all of it is buffered. @@ -1711,6 +1711,61 @@ impl<'a> Decode<'a, Version> for NoUpdate { } } +/// An update as it travels, resolved against what the stream carried before it. +trait Resolve: for<'a> Decode<'a, Version> + std::fmt::Debug { + /// What the stream carried so far, seeded by the request. + type State; + /// The update [`Request::update`] applies. + type Resolved: std::fmt::Debug; + + fn state(msg: &M, version: Version) -> Self::State; + fn resolve(self, state: &mut Self::State) -> Result; +} + +/// The update a request applies. +type Resolved = <::Update as Resolve<::Message>>::Resolved; + +impl Resolve for NoUpdate { + type State = (); + type Resolved = Self; + + fn state(_: &M, _: Version) {} + + fn resolve(self, _: &mut ()) -> Result { + match self {} + } +} + +impl Resolve> for lite::DatagramsUpdate { + type State = (); + type Resolved = Self; + + fn state(_: &lite::Datagrams<'static>, _: Version) {} + + fn resolve(self, _: &mut ()) -> Result { + Ok(self) + } +} + +/// Each lite-07 update edits the live ranges, which only the stream's history names. +impl Resolve> for lite::SubscribeUpdate { + type State = lite::LiveRanges; + type Resolved = LiveUpdate; + + fn state(msg: &lite::Subscribe<'static>, version: Version) -> lite::LiveRanges { + lite::LiveRanges::new(version, &msg.ranges) + } + + fn resolve(mut self, live: &mut lite::LiveRanges) -> Result { + let edit = std::mem::replace(&mut self.ranges, lite::RangeEdit::Replace(Vec::new())); + let ranges = live.apply(edit)?; + Ok(self.with_ranges(ranges)) + } +} + +/// A SUBSCRIBE_UPDATE resolved to every live range, by Range ID. +type LiveUpdate = lite::SubscribeUpdate>; + /// Serves one request stream: decode the request, resolve its broadcast, drive the /// kind's reply, then FIN. /// @@ -1729,8 +1784,11 @@ struct RequestServe { _subscription: Option, // Holds the session busy while a subscription or fetch is open. _working: Option, - // The latest update to arrive before the reply started. Each one replaces the last. - update: Option, + // What resolves each update, seeded once the request decodes. + history: Option<>::State>, + // The latest update to arrive before the reply started. Each one, resolved, replaces the + // last. + update: Option>, /// Ends the request, even mid-reply, once the session stops allowing its broadcast. gate: Option, // Log context, filled in after the decode. Relative to the origin's root, so a path @@ -1778,6 +1836,7 @@ impl RequestServe { owing: true, _subscription: None, _working, + history: None, update: None, gate: None, broadcast: Default::default(), @@ -1864,6 +1923,7 @@ impl RequestServe { RequestState::Decode => { let version = self.shared.version; let msg = ready!(stream.reader.poll_decode_with(waiter, |r| R::decode(r, version)))?; + self.history = Some(R::Update::state(&msg, version)); let (broadcast, track) = R::target(&msg); self.broadcast = broadcast.clone(); if R::SUBSCRIPTION { @@ -1974,9 +2034,13 @@ impl RequestServe { /// Read the requester's next message while we answer: an update, or `None` once it FINs /// its send direction. - fn poll_requester(&mut self, waiter: &kio::Waiter) -> Poll, Error>> { + fn poll_requester(&mut self, waiter: &kio::Waiter) -> Poll>, Error>> { let stream = self.stream.as_mut().expect("stream present"); - let res = ready!(stream.reader.poll_decode_maybe::(waiter)); + let res = ready!(stream.reader.poll_decode_maybe::(waiter)).and_then(|update| { + let Some(update) = update else { return Ok(None) }; + let history = self.history.as_mut().expect("seeded by the request"); + Ok(Some(update.resolve(history)?)) + }); if let Err(err) = &res { self.malformed = matches!(err, Error::Decode(_)); } @@ -1994,7 +2058,7 @@ impl RequestServe { } /// Each update reaches the reply, or waits for it to start. - fn apply(&mut self, update: R::Update) { + fn apply(&mut self, update: Resolved) { match &mut self.state { RequestState::Serve(request) => request.update(update), _ => self.update = Some(update), @@ -2188,7 +2252,7 @@ enum SubscribeServe { querying: track::Resolving, /// The newest group when the SUBSCRIBE arrived; see [`cursor_floor`]. latest: Option, - update: Option, + update: Option, legacy: Option>, }, /// Waiting for the model subscription to be confirmed. @@ -2199,7 +2263,7 @@ enum SubscribeServe { subscribing: track::Subscribing, /// The newest group when the SUBSCRIBE arrived; see [`cursor_floor`]. latest: Option, - update: Option, + update: Option, legacy: Option>, }, /// Streaming groups. Boxed: by far the largest state, and the enum is moved on every @@ -2279,7 +2343,7 @@ impl Request for SubscribeServe { Ok(Self::subscribe(shared, msg, bounds, &track, latest, None, legacy)) } - fn update(&mut self, update: lite::SubscribeUpdate) { + fn update(&mut self, update: LiveUpdate) { match self { Self::Query { update: pending, .. } | Self::Confirm { update: pending, .. } => *pending = Some(update), Self::Run(run) => run.update(update), @@ -2767,7 +2831,7 @@ impl SubscribeServe { bounds: Bounds, track: &track::Consumer, latest: Option, - update: Option, + update: Option, legacy: Option>, ) -> Self { let subscription = crate::track::Subscription { @@ -2794,7 +2858,7 @@ impl SubscribeServe { fn datagrams( mut consumer: datagram::Consumer, msg: &lite::Subscribe<'static>, - update: Option, + update: Option, ) -> Self { let priority = update.map_or(msg.priority, |update| update.priority); consumer.set_priority(priority); @@ -4510,6 +4574,13 @@ struct TrackRun { idle: Option>, /// Lite-05/06: the name's datagram track, which the subscription carries too. datagrams: Option, + /// The Range ID of each requested range, in the order the model holds them. + ids: Vec, + /// How many ranges the SUBSCRIBE carried, whose starts SUBSCRIBE_START reports. + initial: u64, + /// Lite-07: the ranges an update added whose start no SUBSCRIBE_STARTED reported yet, by + /// Range ID. + unstarted: Vec, } impl TrackRun { @@ -4531,8 +4602,12 @@ impl TrackRun { // Lite03 and Lite04 declare no end, so neither side could settle on a drop. let served = emit_range.then(RangeSet::new); let bounded = bounds.ends(ctx.version); + let initial = bounds.ranges.len() as u64; Self { + ids: (0..initial).collect(), + initial, + unstarted: Vec::new(), bounded, frames: bounds.frames, named: groups.start(), @@ -4568,7 +4643,7 @@ impl TrackRun { /// unsets it), then served in order. Only a peer FIN ends the subscription. This is /// what lets relays pause an upstream subscription across consumer churn without /// tearing it down. Lite-07 keeps every subscription open until the subscriber FINs. - fn update(&mut self, upd: lite::SubscribeUpdate) { + fn update(&mut self, upd: LiveUpdate) { if let Some(datagrams) = &mut self.datagrams { datagrams.steer(upd.priority); } @@ -4602,7 +4677,9 @@ impl TrackRun { // Read first: `update` replaces these preferences. let requested = self.named; let floored = requested.is_some_and(|group| group > 0); - let bounds = match Bounds::new(&upd.ranges, self.ctx.stamp.map(|stamp| stamp.timescale)) { + let (ids, ranges): (Vec, Vec) = upd.ranges.iter().copied().unzip(); + let ranges = self.pin(&ids, ranges); + let bounds = match Bounds::new(&ranges, self.ctx.stamp.map(|stamp| stamp.timescale)) { Ok(bounds) => bounds, Err(err) => { self.refused = Some(err); @@ -4666,6 +4743,87 @@ impl TrackRun { } self.bounded = bounds.ends(self.ctx.version); self.frames = bounds.frames; + if self.ctx.version.has_ranges() { + // A range retired before its start resolved is owed no SUBSCRIBE_STARTED. + self.unstarted.retain(|id| ids.contains(id)); + let added = ids.iter().filter(|id| **id >= self.initial && !self.ids.contains(id)); + self.unstarted.extend(added); + } + self.ids = ids; + } + + /// `ranges`, numbered by `ids`, with each range this subscription already held resolved + /// as it was: a range resolves once, so an update that leaves it live never counts its + /// relative anchors back from a newer latest group. Lite-07 only: an older update + /// replaces every range. + fn pin(&mut self, ids: &[u64], mut ranges: Vec) -> Vec { + if !self.ctx.version.has_ranges() { + return ranges; + } + let resolved = self.track.resolved(); + for (id, range) in ids.iter().zip(&mut ranges) { + let Some((start, end)) = self + .ids + .iter() + .position(|held| held == id) + .and_then(|index| resolved.get(index).copied().flatten()) + else { + continue; + }; + if let Some(lite::Anchor::Relative(_)) = range.start { + range.start = Some(lite::Anchor::Absolute(track::Position::group(start))); + } + if let (Some(lite::Anchor::Relative(_)), Some(end)) = (range.end, end) { + range.end = Some(lite::Anchor::Absolute(track::Position::group(end))); + } + } + ranges + } + + /// Report where each range an update added starts, once it resolves, or with no start + /// once nothing can resolve it (`finished`). Returns whether any SUBSCRIBE_STARTED was + /// buffered. Waits for SUBSCRIBE_START, which goes first. + fn report_started( + &mut self, + finished: bool, + writer: &mut Writer, + ) -> Result { + let Some(from) = self.start else { + return Ok(false); + }; + if self.unstarted.is_empty() { + return Ok(false); + } + let resolved = self.track.resolved(); + let mut waiting = Vec::new(); + let mut reported = false; + for id in std::mem::take(&mut self.unstarted) { + let index = self.ids.iter().position(|held| *held == id).expect("unstarted ranges are live"); + let start = match resolved.get(index).copied().flatten() { + Some((start, end)) => { + let start = start.max(from); + end.is_none_or(|end| end > start).then_some(start) + } + None if finished => None, + None => { + waiting.push(id); + continue; + } + }; + writer.buffer(&lite::SubscribeResponse::Started(lite::SubscribeStarted { id, start }))?; + reported = true; + } + self.unstarted = waiting; + Ok(reported) + } + + /// Apply `update`, replacing every range with fresh Range IDs, as an update before + /// lite-07 does. + #[cfg(test)] + fn replace(&mut self, update: lite::SubscribeUpdate>) { + let next = self.ids.iter().max().map_or(0, |last| last + 1).max(self.initial); + let ranges = (next..).zip(update.ranges.iter().copied()).collect(); + self.update(update.with_ranges(ranges)); } /// Apply [`Self::floor`] and [`Self::cap`] to the read cursor. @@ -4758,7 +4916,7 @@ impl TrackRun { writer: &mut Writer, waiter: &kio::Waiter, ) -> Poll, Error>> { - if self.poll_forward_drop(writer, waiter)? { + if self.poll_forward_drop(writer, waiter)? || self.report_started(false, writer)? { return Poll::Ready(Ok(ControlFlow::Continue(()))); } @@ -4863,7 +5021,9 @@ impl TrackRun { self.resolve_held(final_sequence(fin), writer)?; answered = true; } - let dropped = self.drop_unserved(writer)? || answered; + // Nothing arrives to resolve a range still waiting on the live feed. + let started = self.report_started(true, writer)?; + let dropped = self.drop_unserved(writer)? || answered || started; if !self.ctx.version.keeps_subscribe_open() { return Poll::Ready(Ok(ControlFlow::Break(()))); } @@ -6550,16 +6710,16 @@ mod serve_group_test { ranges: vec![lite::Range::span(None, 0, None, None)], order: Order::Descending, }; - run.update(update(Some(0))); + run.replace(update(Some(0))); assert_eq!(track.demand().wanted(), Some(0), "stale request is not a join"); - run.update(update(Some(1))); + run.replace(update(Some(1))); assert_eq!(track.demand().wanted(), Some(1)); let mut group = track.append_group().unwrap(); group.write_frame(None, &b"key"[..]).unwrap(); group.finish().unwrap(); - run.update(update(Some(1))); + run.replace(update(Some(1))); assert_eq!(track.demand().wanted(), Some(1), "answered request stays answered"); - run.update(update(Some(2))); + run.replace(update(Some(2))); assert_eq!(track.demand().wanted(), Some(1), "new hop judges its own ratio"); } @@ -6678,7 +6838,7 @@ mod serve_group_test { assert_eq!(log.orders(), [(0, 1), (1, 3)], "newer groups first"); assert!(log.priorities().is_empty(), "the session queue ranks no stream"); - run.update(lite::SubscribeUpdate { + run.replace(lite::SubscribeUpdate { wanted_group: None, order: Order::Ascending, priority: 7, @@ -6812,6 +6972,121 @@ mod serve_group_test { } } + /// Every response a run loop's subscribe stream carried, once flushed. + async fn responses( + writer: &mut Writer, + log: &Log, + ) -> Vec { + kio::wait(|waiter| writer.poll_flush(waiter)).await.unwrap(); + let writes = log.writes.lock().unwrap().clone(); + let mut buf = writes.as_slice(); + let mut responses = Vec::new(); + while !buf.is_empty() { + let (msg, size) = lite::SubscribeResponse::decode_slice(buf, Version::Lite07).unwrap(); + buf = &buf[size..]; + responses.push(msg); + } + responses + } + + /// A lite-07 update that keeps every live range and adds `added`, each taking the next + /// Range ID after `next`. + fn lite07_add(live: &[(u64, lite::Range)], next: u64, added: &[lite::Range]) -> LiveUpdate { + let mut ranges = live.to_vec(); + ranges.extend((next..).zip(added.iter().copied())); + lite::SubscribeUpdate { + wanted_group: None, + priority: 0, + max_delay: Duration::from_secs(3600), + min_delay: None, + ranges, + order: Order::Descending, + } + } + + /// A range an update adds learns where it starts from SUBSCRIBE_STARTED, after + /// SUBSCRIBE_START, and keeps that start however many later updates leave it live. + #[kio::test] + async fn lite07_an_added_range_reports_its_start_and_keeps_it() { + let mut track = track::Producer::new( + Arc::new(broadcast::Info::default()), + "test", + crate::track::Info::timed(), + ); + for sequence in 0..5 { + write_group(&mut track, sequence, sequence * 1000); + } + // Bounded below the groups the added range reaches, so its start is its own. + let first = lite::Range::span(Some(0), 0, Some(0), None); + let subscriber = track.subscribe( + track::Subscription::default() + .with_delay(track::Delay::default().with_max(Duration::from_secs(3600))) + .with_range(0..1), + ); + let (mut run, mut writer, log) = lite07_run(SinkSession::new(Log::default()), subscriber); + while run.start.is_none() { + assert!(run.poll_step(&mut writer, &kio::Waiter::noop()).is_ready()); + } + + // Two groups back from the latest, group 2. + let back = lite::Range { + start: Some(lite::Anchor::Relative(2)), + end: None, + }; + let live = [(0, first)]; + run.update(lite07_add(&live, 1, &[back])); + while run.poll_step(&mut writer, &kio::Waiter::noop()).is_ready() {} + let started: Vec<_> = responses(&mut writer, &log) + .await + .into_iter() + .filter_map(|msg| match msg { + lite::SubscribeResponse::Started(started) => Some(started), + _ => None, + }) + .collect(); + assert_eq!(started, [lite::SubscribeStarted { id: 1, start: Some(2) }]); + + // Newer groups arrive, and another update leaves the range live: it still starts at 2. + for sequence in 5..8 { + write_group(&mut track, sequence, sequence * 1000); + } + let live = [(0, first), (1, back)]; + run.update(lite07_add(&live, 2, &[lite::Range::span(Some(9), 0, None, None)])); + assert_eq!(run.track.resolved()[1], Some((2, None)), "the update resolved it again"); + } + + /// A range retired before SUBSCRIBE_STARTED reported it gets none, and one still waiting + /// once the track ends is reported with no start. + #[kio::test] + async fn lite07_a_retired_range_reports_no_start() { + let mut track = track::Producer::new( + Arc::new(broadcast::Info::default()), + "test", + crate::track::Info::timed(), + ); + write_group(&mut track, 0, 0); + let first = lite::Range::span(Some(0), 0, None, None); + let subscriber = track.subscribe( + track::Subscription::default() + .with_delay(track::Delay::default().with_max(Duration::from_secs(3600))) + .with_range(0..), + ); + let (mut run, mut writer, log) = lite07_run(SinkSession::new(Log::default()), subscriber); + let added = lite::Range::span(Some(4), 0, None, None); + // Added and retired before the run reported it. + run.update(lite07_add(&[(0, first)], 1, &[added])); + run.update(lite07_add(&[(0, first)], 2, &[])); + while run.poll_step(&mut writer, &kio::Waiter::noop()).is_ready() {} + assert!(run.start.is_some()); + assert!( + !responses(&mut writer, &log) + .await + .iter() + .any(|msg| matches!(msg, lite::SubscribeResponse::Started(_))), + "a retired range was reported" + ); + } + /// A skipped sequence is accounted for by DROP rather than a stream count. #[kio::test] async fn lite07_end_drops_skipped_sequences() { @@ -6915,7 +7190,7 @@ mod serve_group_test { assert_eq!(run.ctx.outcomes.opened.load(Ordering::Relaxed), 3, "{version:?}"); assert_eq!(drops(&mut writer, &log, version).await, [(1, 2)], "{version:?}"); - run.update(lite::SubscribeUpdate { + run.replace(lite::SubscribeUpdate { wanted_group: None, priority: 0, max_delay: Duration::from_secs(10), @@ -7070,7 +7345,7 @@ mod serve_group_test { while run.poll_step(&mut writer, &kio::Waiter::noop()).is_ready() {} assert_eq!(drops(&mut writer, &log, version).await, []); - run.update(lite::SubscribeUpdate { + run.replace(lite::SubscribeUpdate { wanted_group: None, order: crate::track::Order::Descending, priority: 0, @@ -7477,7 +7752,7 @@ mod serve_group_test { } assert_eq!(run.start, Some(5)); - run.update(lite::SubscribeUpdate { + run.replace(lite::SubscribeUpdate { wanted_group: None, order: crate::track::Order::Descending, priority: 0, @@ -7518,7 +7793,7 @@ mod serve_group_test { } kio::clock::sim::advance(Duration::from_secs(2)).await; - run.update(lite::SubscribeUpdate { + run.replace(lite::SubscribeUpdate { wanted_group: None, order: crate::track::Order::Descending, priority: 0, @@ -7572,7 +7847,7 @@ mod serve_group_test { min_delay: None, ranges: vec![lite::Range::span(Some(start_group), 0, None, None)], }; - run.update(update(5)); + run.replace(update(5)); { let mut run = std::pin::pin!(kio::wait(|waiter| run.poll(&mut writer, waiter))); write_group(&mut track, 5, 0); @@ -7580,7 +7855,7 @@ mod serve_group_test { } kio::clock::sim::advance(Duration::from_millis(900)).await; - run.update(update(2)); + run.replace(update(2)); kio::clock::sim::advance(Duration::from_millis(200)).await; track.finish_at(track::Position::group(6), None).unwrap(); drop(track); @@ -7618,11 +7893,11 @@ mod serve_group_test { min_delay: None, ranges: vec![lite::Range::span(Some(start_group), 0, None, None)], }; - run.update(update(5)); + run.replace(update(5)); while run.poll_step(&mut writer, &kio::Waiter::noop()).is_ready() {} assert_eq!(drops(&mut writer, &log, version).await, []); - run.update(update(0)); + run.replace(update(0)); for sequence in 1..5 { write_group(&mut track, sequence, 0); } @@ -7656,7 +7931,7 @@ mod serve_group_test { write_group(&mut track, 7, 7); assert_eq!(settle(&mut run), 1); - run.update(lite::SubscribeUpdate { + run.replace(lite::SubscribeUpdate { wanted_group: None, order: crate::track::Order::Descending, priority: 0, @@ -7686,7 +7961,7 @@ mod serve_group_test { .with_delay(track::Delay::default().with_max(Duration::from_secs(60))), ); let (mut run, mut writer, _log) = lite_run(Version::Lite05, SinkSession::new(Log::default()), subscriber); - run.update(lite::SubscribeUpdate { + run.replace(lite::SubscribeUpdate { wanted_group: None, order: crate::track::Order::Descending, priority: 0, @@ -7823,7 +8098,7 @@ mod serve_group_test { relay.settle(); assert_eq!(relay.opened(), 0, "the resume is past the only group"); - relay.run.update(lite::SubscribeUpdate { + relay.run.replace(lite::SubscribeUpdate { wanted_group: None, order: crate::track::Order::Descending, priority: 0, @@ -7853,7 +8128,7 @@ mod serve_group_test { relay.settle(); assert_eq!(relay.opened(), 2, "groups 1 and 2 are at or above the floor"); - relay.run.update(lite::SubscribeUpdate { + relay.run.replace(lite::SubscribeUpdate { wanted_group: None, order: crate::track::Order::Descending, priority: 0, @@ -7888,7 +8163,7 @@ mod serve_group_test { min_delay: None, ranges: vec![lite::Range::span(Some(7), 0, None, None)], }; - relay.run.update(update); + relay.run.replace(update); relay.settle(); track.start_at(0).unwrap(); @@ -9052,13 +9327,17 @@ mod tests { let mut serve = RequestServe::::new(publisher.shared.clone(), stream); assert!(!drive(&mut serve).await); + // The ranges stay as the SUBSCRIBE asked, so each update edits none of them. let update = |wanted_group, priority| lite::SubscribeUpdate { wanted_group, order: crate::track::Order::Descending, priority, max_delay: Duration::ZERO, min_delay: None, - ranges: vec![lite::Range::span(Some(1), 0, None, None)], + ranges: lite::RangeEdit::Edit { + remove: Vec::new(), + add: Vec::new(), + }, }; session.push(&update(Some(2), 3).encode_bytes(version).unwrap()); session.push(&update(Some(2), 7).encode_bytes(version).unwrap()); @@ -9117,7 +9396,10 @@ mod tests { priority: 0, max_delay: Duration::from_secs(3600), min_delay: None, - ranges: vec![lite::Range::span(None, 0, None, None)], + ranges: lite::RangeEdit::Edit { + remove: Vec::new(), + add: Vec::new(), + }, } } diff --git a/rs/moq-net/src/lite/subscribe.rs b/rs/moq-net/src/lite/subscribe.rs index 2872f075f44..a3589b33d7f 100644 --- a/rs/moq-net/src/lite/subscribe.rs +++ b/rs/moq-net/src/lite/subscribe.rs @@ -2,7 +2,7 @@ use std::borrow::Cow; use crate::{ Path, StreamError, - coding::{Decode, DecodeError, Decoder, Encode, EncodeError, Encoder, StreamCodes}, + coding::{Decode, DecodeError, Decoder, Encode, EncodeError, Encoder, Form, StreamCodes, varint}, }; use super::{Message, Version}; @@ -50,7 +50,7 @@ impl Subscribe<'_> { } /// One end of a [`Range`], as the wire carries it. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub enum Anchor { /// A position: a start delivers from it, and an end stops just before it. Absolute(crate::track::Position), @@ -69,7 +69,7 @@ pub enum Anchor { /// `Group Start` of 0 with a `Frame Start` of 0, the same absence of a floor there. No start /// and a [`Anchor::Relative`] start of 0 both encode as an absent `Group Start`, which a /// pre-06 publisher reads as the latest group and a lite-06 one as no floor. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub struct Range { /// Where delivery starts, or `None` for the oldest group the max age allows. pub start: Option, @@ -208,19 +208,11 @@ fn encode_min_delay( /// `Group End`, and their frame bounds; see [`Range`]. fn decode_ranges(r: &mut Decoder<'_>, version: Version) -> Result, DecodeError> { if version.has_ranges() { - let count = r.varint()?; + let ranges = decode_range_list(r)?; // A subscription that asks for nothing has no use: refuse it rather than park it. - if count == 0 { + if ranges.is_empty() { return Err(DecodeError::InvalidSubscribeLocation); } - // Not preallocated: each range costs at least two bytes of the bounded message, so a - // count the bytes can't back runs out before it costs anything. - let mut ranges = Vec::new(); - for _ in 0..count { - let start = decode_anchor(r)?; - let end = decode_anchor(r)?; - ranges.push(Range { start, end }); - } return Ok(ranges); } @@ -257,12 +249,7 @@ fn encode_ranges(w: &mut Encoder<'_>, version: Version, ranges: &[Range]) -> Res if ranges.is_empty() { return Err(EncodeError::InvalidState); } - w.varint(ranges.len() as u64)?; - for range in ranges { - encode_anchor(w, range.start)?; - encode_anchor(w, range.end)?; - } - return Ok(()); + return encode_range_list(w, ranges); } let [range] = ranges else { @@ -288,6 +275,42 @@ fn encode_ranges(w: &mut Encoder<'_>, version: Version, ranges: &[Range]) -> Res encode_frame_bounds(w, version, start_group, start_frame, end_group, end_frame) } +/// Decode a lite-07 `Range Count` and its ranges, which may be none. +fn decode_range_list(r: &mut Decoder<'_>) -> Result, DecodeError> { + let count = r.varint()?; + // Not preallocated: each range costs at least two bytes of the bounded message, so a + // count the bytes can't back runs out before it costs anything. + let mut ranges = Vec::new(); + for _ in 0..count { + let start = decode_anchor(r)?; + let end = decode_anchor(r)?; + ranges.push(Range { start, end }); + } + Ok(ranges) +} + +/// Encode the inverse of [`decode_range_list`]. +fn encode_range_list(w: &mut Encoder<'_>, ranges: &[Range]) -> Result<(), EncodeError> { + w.varint(ranges.len() as u64)?; + for range in ranges { + encode_anchor(w, range.start)?; + encode_anchor(w, range.end)?; + } + Ok(()) +} + +/// The bytes a lite-07 range takes on the wire, or `None` past the varint's reach. +fn range_size(range: &Range, form: Form) -> Option { + let size = |value| varint::size(value, form).ok(); + let anchor = |anchor: Option| match anchor { + None => size(0), + Some(Anchor::Absolute(position)) => Some(size(1)? + size(position.group)? + size(position.frame)?), + Some(Anchor::Relative(back)) => Some(size(2)? + size(back)?), + Some(Anchor::Time(pts)) => Some(size(3)? + size(pts)?), + }; + Some(anchor(range.start)? + anchor(range.end)?) +} + /// Decode one lite-07 range end: its type, then the fields that type carries. Type 0 is no /// anchor, the oldest group as a start and the live edge as an end. fn decode_anchor(r: &mut Decoder<'_>) -> Result, DecodeError> { @@ -639,21 +662,38 @@ impl<'a> Message<'a> for SubscribeFinish { /// Sent by the subscriber to update subscription parameters. /// +/// `R` is how the ranges travel: a [`RangeEdit`] on the wire, and every live range with its +/// Range ID once [`LiveRanges`] resolves the edit, which is what a publisher applies. +/// /// Lite03+ only. #[derive(Clone, Debug)] -pub struct SubscribeUpdate { +pub struct SubscribeUpdate { /// A join requesting this group or later, judged against the local refresh ratio. pub wanted_group: Option, pub priority: i8, pub max_delay: std::time::Duration, /// See [`Subscribe::min_delay`]. pub min_delay: Option, - /// The replacement for every range; see [`Subscribe::ranges`]. - pub ranges: Vec, + /// The change to the ranges; see [`Subscribe::ranges`]. + pub ranges: R, /// See [`Subscribe::order`]. pub order: Order, } +impl SubscribeUpdate { + /// The same update with its ranges as `ranges`. + pub(crate) fn with_ranges(self, ranges: T) -> SubscribeUpdate { + SubscribeUpdate { + wanted_group: self.wanted_group, + priority: self.priority, + max_delay: self.max_delay, + min_delay: self.min_delay, + ranges, + order: self.order, + } + } +} + impl<'a> Message<'a> for SubscribeUpdate { const CONTROL: Option = Some(crate::stats::Message::Subscribe); fn decode_msg(r: &mut Decoder<'a>, version: Version) -> Result { @@ -668,7 +708,20 @@ impl<'a> Message<'a> for SubscribeUpdate { let order = decode_order(r, version)?; let max_delay = std::time::Duration::from_millis(r.varint()?); let min_delay = decode_min_delay(r, version)?; - let ranges = decode_ranges(r, version)?; + let ranges = match version.has_ranges() { + true => { + // Not preallocated, like the ranges: each id costs a byte of the bounded message. + let mut remove = Vec::new(); + for _ in 0..r.varint()? { + remove.push(r.varint()?); + } + RangeEdit::Edit { + remove, + add: decode_range_list(r)?, + } + } + false => RangeEdit::Replace(decode_ranges(r, version)?), + }; let wanted_group = match version.has_refresh() { true => r.varint()?.checked_sub(1), @@ -697,7 +750,17 @@ impl<'a> Message<'a> for SubscribeUpdate { encode_order(w, version, self.order)?; w.varint(u64::try_from(self.max_delay.as_millis()).map_err(|_| EncodeError::BoundsExceeded)?)?; encode_min_delay(w, version, self.min_delay)?; - encode_ranges(w, version, &self.ranges)?; + match (&self.ranges, version.has_ranges()) { + (RangeEdit::Edit { remove, add }, true) => { + w.varint(remove.len() as u64)?; + for id in remove { + w.varint(*id)?; + } + encode_range_list(w, add)?; + } + (RangeEdit::Replace(ranges), false) => encode_ranges(w, version, ranges)?, + _ => return Err(EncodeError::Version), + } if version.has_refresh() { w.varint_opt(self.wanted_group)?; @@ -706,6 +769,221 @@ impl<'a> Message<'a> for SubscribeUpdate { } } +/// The ranges a SUBSCRIBE_UPDATE carries. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum RangeEdit { + /// Before lite-07: the whole list, replacing the live one. + Replace(Vec), + /// Lite-07: retire the ranges with these Range IDs, then add these, each taking the next + /// Range ID. + Edit { remove: Vec, add: Vec }, +} + +/// A subscription's live ranges by Range ID, which both ends of a Subscribe Stream keep: the +/// subscriber to write each update as an edit, the publisher to resolve one. +/// +/// The SUBSCRIBE's ranges take Range IDs from 0 in order, and each range an update adds +/// takes the next. Before lite-07 an update replaces the list, numbered afresh. +#[derive(Clone, Debug)] +pub(crate) struct LiveRanges { + version: Version, + next: u64, + live: std::collections::BTreeMap, + /// The bytes the live ranges take on the wire, held to what one SUBSCRIBE carries. + size: usize, + /// How many ranges the SUBSCRIBE carried, whose starts SUBSCRIBE_OK reports. + initial: u64, +} + +impl LiveRanges { + /// The ranges a SUBSCRIBE carried. + pub fn new(version: Version, ranges: &[Range]) -> Self { + let mut live = Self { + version, + next: 0, + live: Default::default(), + size: 0, + initial: ranges.len() as u64, + }; + live.add(ranges.iter().copied()); + live + } + + fn add(&mut self, ranges: impl IntoIterator) { + let form: Form = self.version.into(); + for range in ranges { + // One past the varint's reach can't come off the wire, and is refused below. + self.size = self + .size + .saturating_add(range_size(&range, form).unwrap_or(usize::MAX)); + self.live.insert(self.next, range); + self.next += 1; + } + } + + fn remove(&mut self, id: u64) -> Option { + let range = self.live.remove(&id)?; + self.size = self + .size + .saturating_sub(range_size(&range, self.version.into()).unwrap_or(usize::MAX)); + Some(range) + } + + /// Every live range with its Range ID, in Range ID order. + pub fn numbered(&self) -> Vec<(u64, Range)> { + self.iter().map(|(id, range)| (id, *range)).collect() + } + + /// Every live range with its Range ID, in Range ID order. + pub fn iter(&self) -> impl Iterator { + self.live.iter().map(|(id, range)| (*id, range)) + } + + /// Whether the publisher reports where each range starts, which lite-07 does by Range + /// ID. Before it, a start describes the SUBSCRIBE's one range alone. + pub fn reports_starts(&self) -> bool { + self.version.has_ranges() + } + + /// The live range with Range ID `id`. + pub fn get(&self, id: u64) -> Option<&Range> { + self.live.get(&id) + } + + /// Whether `id` was ever assigned. + pub fn assigned(&self, id: u64) -> bool { + id < self.next + } + + /// Whether SUBSCRIBE_OK reports the start of the range with Range ID `id`, which the + /// SUBSCRIBE carried; SUBSCRIBE_STARTED reports each one an update adds. + pub fn initial(&self, id: u64) -> bool { + id < self.initial + } + + /// How many ranges the SUBSCRIBE carried. + pub fn initial_count(&self) -> u64 { + self.initial + } + + /// Resolve a received edit, returning every live range with its Range ID. + pub fn apply(&mut self, edit: RangeEdit) -> Result, DecodeError> { + match edit { + RangeEdit::Replace(ranges) if !self.version.has_ranges() => { + *self = Self::new(self.version, &ranges); + } + RangeEdit::Edit { remove, add } if self.version.has_ranges() => { + for id in remove { + // An id never assigned, or already retired (including earlier in this list). + self.remove(id).ok_or(DecodeError::InvalidValue)?; + } + self.add(add); + } + _ => return Err(DecodeError::Version), + } + // Asking for nothing is refused like a SUBSCRIBE that does, and a set no SUBSCRIBE + // could carry like one too large to send. + if self.live.is_empty() { + return Err(DecodeError::InvalidSubscribeLocation); + } + if self.size > super::message::MAX_MESSAGE_SIZE { + return Err(DecodeError::TooMany); + } + Ok(self.numbered()) + } + + /// The edit that keeps the live ranges as they are. + pub fn unchanged(&self) -> RangeEdit { + match self.version.has_ranges() { + true => RangeEdit::Edit { + remove: Vec::new(), + add: Vec::new(), + }, + false => RangeEdit::Replace(self.live.values().copied().collect()), + } + } + + /// The edit that turns the live ranges into `ranges`, which become the live ones. + /// + /// A range already live keeps its Range ID, so an update that changes one range sends + /// only that one, and the publisher keeps where the rest resolved. + pub fn edit(&mut self, ranges: &[Range]) -> Result { + if ranges.is_empty() { + return Err(EncodeError::InvalidState); + } + if !self.version.has_ranges() { + // Replaced ranges take fresh Range IDs, so nothing said of the old ones applies. + if !self.live.values().eq(ranges) { + self.live.clear(); + self.size = 0; + self.add(ranges.iter().copied()); + } + return Ok(RangeEdit::Replace(ranges.to_vec())); + } + // Each live range keeps at most one wanted copy, so duplicates edit like any other. + let mut wanted = std::collections::HashMap::::new(); + for range in ranges { + *wanted.entry(*range).or_default() += 1; + } + let mut remove = Vec::new(); + for (&id, range) in &self.live { + match wanted.get_mut(range) { + Some(count) if *count > 0 => *count -= 1, + _ => remove.push(id), + } + } + // In the order asked for, so the publisher numbers them the way this side does. + let add: Vec = ranges + .iter() + .filter(|range| { + let count = wanted.get_mut(*range).expect("counted above"); + let keep = *count > 0; + *count = count.saturating_sub(1); + keep + }) + .copied() + .collect(); + for &id in &remove { + self.remove(id); + } + self.add(add.iter().copied()); + if self.size > super::message::MAX_MESSAGE_SIZE { + return Err(EncodeError::TooLarge); + } + Ok(RangeEdit::Edit { remove, add }) + } +} + +/// SUBSCRIBE_STARTED: where a range a SUBSCRIBE_UPDATE added starts. Lite-07 and later. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct SubscribeStarted { + /// The range's Range ID. + pub id: u64, + /// The group its delivery starts at, `None` for a range with no group there. + pub start: Option, +} + +impl<'a> Message<'a> for SubscribeStarted { + const CONTROL: Option = Some(crate::stats::Message::Subscribe); + fn decode_msg(r: &mut Decoder<'a>, version: Version) -> Result { + if !version.has_ranges() { + return Err(DecodeError::Version); + } + Ok(Self { + id: r.varint()?, + start: r.varint_opt()?, + }) + } + + fn encode_msg(&self, w: &mut Encoder<'_>, version: Version) -> Result<(), EncodeError> { + if !version.has_ranges() { + return Err(EncodeError::Version); + } + w.varint(self.id)?; + w.varint_opt(self.start) + } +} + /// Indicates that one or more groups have been dropped. /// /// The range `[start, end]` is inclusive on both ends. For example, @@ -802,12 +1080,14 @@ impl<'a> Message<'a> for SubscribeDrop { /// - Lite05+: `0x0` SUBSCRIBE_START (SUBSCRIBE_OK on lite-07), `0x1` SUBSCRIBE_FINISH /// (SUBSCRIBE_END before lite-07), `0x2` SUBSCRIBE_DROP /// (SUBSCRIBE_OK was removed; acceptance is implicit). +/// - Lite07+: also `0x3` SUBSCRIBE_STARTED. #[derive(Clone, Debug)] pub enum SubscribeResponse { Ok(SubscribeOk), Start(SubscribeStart), Finish(SubscribeFinish), Drop(SubscribeDrop), + Started(SubscribeStarted), } /// Write a `type` varint followed by the size-prefixed message body. @@ -838,6 +1118,7 @@ impl Encode for SubscribeResponse { Self::Start(start) => encode_typed(w, 0, start, version)?, Self::Finish(end) => encode_typed(w, 1, end, version)?, Self::Drop(drop) => encode_typed(w, 2, drop, version)?, + Self::Started(started) => encode_typed(w, 3, started, version)?, Self::Ok(_) => return Err(EncodeError::Version), }, } @@ -864,6 +1145,7 @@ impl<'a> Decode<'a, Version> for SubscribeResponse { 0 => Ok(Self::Start(SubscribeStart::decode(buf, version)?)), 1 => Ok(Self::Finish(SubscribeFinish::decode(buf, version)?)), 2 => Ok(Self::Drop(SubscribeDrop::decode(buf, version)?)), + 3 if version.has_ranges() => Ok(Self::Started(SubscribeStarted::decode(buf, version)?)), _ => Err(DecodeError::InvalidMessage(typ)), } } @@ -1257,18 +1539,129 @@ mod test { ]; assert_eq!(roundtrip(&msg, Version::Lite07).ranges, msg.ranges); + // An update from the oldest group to the same ranges adds every one of them. + let mut sent = LiveRanges::new(Version::Lite07, &[Range::OLDEST]); let update = SubscribeUpdate { wanted_group: None, priority: 3, max_delay: std::time::Duration::from_millis(250), min_delay: None, - ranges: msg.ranges.clone(), + ranges: sent.edit(&msg.ranges).unwrap(), order: crate::track::Order::Ascending, }; let buf = update.encode_bytes(Version::Lite07).unwrap(); let got = crate::coding::decode_buf(&mut buf.as_ref(), Version::Lite07, |r, v| SubscribeUpdate::decode(r, v)) .unwrap(); - assert_eq!(got.ranges, msg.ranges); + let mut live = LiveRanges::new(Version::Lite07, &[Range::OLDEST]); + let resolved: Vec<_> = live.apply(got.ranges).unwrap().into_iter().map(|(_, range)| range).collect(); + assert_eq!(resolved, msg.ranges); + } + + /// An update sends only the ranges that changed, and the publisher resolves the same list, + /// with the same Range IDs, as the subscriber holds. + #[test] + fn lite07_updates_edit_only_the_changed_ranges() { + let version = Version::Lite07; + let (a, b, c) = ( + Range { + start: absolute(1, 0), + end: absolute(5, 0), + }, + Range { + start: Some(Anchor::Relative(3)), + end: None, + }, + Range { + start: Some(Anchor::Time(9_000)), + end: None, + }, + ); + let mut sent = LiveRanges::new(version, &[a, b]); + let mut live = LiveRanges::new(version, &[a, b]); + let mut step = |ranges: &[Range], expected: RangeEdit| { + let edit = sent.edit(ranges).unwrap(); + assert_eq!(edit, expected); + let update = SubscribeUpdate { + wanted_group: None, + priority: 0, + max_delay: std::time::Duration::ZERO, + min_delay: None, + ranges: edit, + order: crate::track::Order::Descending, + }; + let buf = update.encode_bytes(version).unwrap(); + let got = crate::coding::decode_buf(&mut buf.as_ref(), version, |r, v| SubscribeUpdate::decode(r, v)) + .unwrap(); + assert_eq!(live.apply(got.ranges).unwrap(), sent.numbered()); + }; + + let edit = |remove: &[u64], add: &[Range]| RangeEdit::Edit { + remove: remove.to_vec(), + add: add.to_vec(), + }; + step(&[b, a], edit(&[], &[])); + step(&[a, c], edit(&[1], &[c])); + step(&[c, c, a], edit(&[], &[c])); + step(&[b], edit(&[0, 2, 3], &[b])); + assert_eq!(live.numbered(), [(4, b)]); + assert!(live.initial(1) && !live.initial(2), "SUBSCRIBE_START reports only the SUBSCRIBE's"); + } + + /// An edit naming a Range ID never assigned or already retired, one leaving nothing + /// live, or one adding more than a SUBSCRIBE carries, is refused. + #[test] + fn lite07_refuses_edits_it_cannot_apply() { + let version = Version::Lite07; + let edit = |remove: Vec, add: Vec| RangeEdit::Edit { remove, add }; + let fresh = || LiveRanges::new(version, &[Range::OLDEST]); + assert!(fresh().apply(edit(vec![1], vec![Range::OLDEST])).is_err(), "never assigned"); + assert!(fresh().apply(edit(vec![0, 0], vec![Range::OLDEST])).is_err(), "retired twice"); + assert!(fresh().apply(edit(vec![0], vec![])).is_err(), "nothing left"); + assert!(fresh().apply(RangeEdit::Replace(vec![Range::OLDEST])).is_err(), "not on lite-07"); + + let mut live = fresh(); + let one = Range { + start: absolute(1 << 40, 1 << 40), + end: absolute(1 << 41, 1 << 40), + }; + // Each update fits in a message, but together they would not fit in a SUBSCRIBE. + let mut refused = false; + for _ in 0..(super::super::message::MAX_MESSAGE_SIZE / 16) { + if live.apply(edit(vec![], vec![one; 8])).is_err() { + refused = true; + break; + } + } + assert!(refused, "the live ranges are held to one SUBSCRIBE"); + } + + /// SUBSCRIBE_STARTED is type 3 on lite-07 and unknown before it. + #[test] + fn subscribe_started_is_type_3_on_lite07() { + for start in [None, Some(0), Some(7)] { + let started = SubscribeStarted { id: 4, start }; + let buf = SubscribeResponse::Started(started.clone()) + .encode_bytes(Version::Lite07) + .unwrap(); + assert_eq!(buf[0], 3); + let got = crate::coding::decode_buf(&mut buf.as_ref(), Version::Lite07, |r, v| { + SubscribeResponse::decode(r, v) + }) + .unwrap(); + assert!(matches!(got, SubscribeResponse::Started(got) if got == started)); + } + let started = SubscribeResponse::Started(SubscribeStarted { id: 0, start: None }); + assert!(started.encode_bytes(Version::Lite06).is_err()); + let mut buf = Vec::new(); + let mut w = Encoder::new(&mut buf, Version::Lite06.into()); + w.varint(3).unwrap(); + SubscribeStarted { id: 0, start: None } + .encode_msg(&mut w, Version::Lite07) + .unwrap(); + assert!( + crate::coding::decode_buf(&mut buf.as_slice(), Version::Lite06, |r, v| SubscribeResponse::decode(r, v)) + .is_err() + ); } /// A subscription asking for nothing is refused both ways, and an unknown anchor type @@ -1357,7 +1750,7 @@ mod test { priority: 0, max_delay: std::time::Duration::ZERO, min_delay: None, - ranges: vec![Range::OLDEST], + ranges: LiveRanges::new(version, &[Range::OLDEST]).unchanged(), order: crate::track::Order::Descending, }; let buf = update.encode_bytes(version).unwrap(); @@ -1390,7 +1783,7 @@ mod test { priority: 0, max_delay: std::time::Duration::ZERO, min_delay, - ranges: vec![Range::OLDEST], + ranges: LiveRanges::new(version, &[Range::OLDEST]).unchanged(), order: crate::track::Order::Descending, }; let buf = update.encode_bytes(version).unwrap(); @@ -1418,7 +1811,7 @@ mod test { priority: 3, max_delay: std::time::Duration::from_millis(250), min_delay: None, - ranges: vec![Range::OLDEST], + ranges: LiveRanges::new(version, &[Range::OLDEST]).unchanged(), order, }; let mut buf = Vec::new(); diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index ccbcb4c7345..3c6d70abe52 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -2907,15 +2907,14 @@ mod tests { start: None, priority: 0, requested: None, - ranges: vec![lite::Range::OLDEST], + live: lite::LiveRanges::new(Version::Lite04, &[lite::Range::OLDEST]), tail: tail.clone(), served: None, started: false, - starts: Vec::new(), + starts: Default::default(), end: None, wanted: None, unanswered: None, - updated: false, _working: crate::session::Work::default().start(), }) } @@ -2961,21 +2960,24 @@ mod tests { unreachable!("feed_sub is active") }; let largest = Some(Position::group(10)); - active.ranges = vec![lite::Range::span(Some(0), 0, Some(10), None)]; + let live = |ranges: &[lite::Range]| lite::LiveRanges::new(Version::Lite07, ranges); + active.live = live(&[lite::Range::span(Some(0), 0, Some(10), None)]); active.served = Some(0); - active.starts = vec![Some(0)]; + active.starts = [(0, Some(0))].into(); assert_eq!(active.never_serves(largest), None, "group 10 is in 0..11"); - active.ranges = vec![lite::Range::span(Some(0), 0, Some(3), None)]; + active.live = live(&[lite::Range::span(Some(0), 0, Some(3), None)]); assert_eq!(active.never_serves(largest), largest, "0..4 never reaches group 10"); - active.ranges.push(lite::Range::span(Some(10), 0, Some(12), None)); - active.starts = vec![Some(0), Some(10)]; + let later = lite::Range::span(Some(10), 0, Some(12), None); + active.live = live(&[lite::Range::span(Some(0), 0, Some(3), None), later]); + active.starts = [(0, Some(0)), (1, Some(10))].into(); assert_eq!(active.never_serves(largest), None, "a later range serves it"); - active.starts[1] = None; + active.starts.insert(1, None); assert_eq!(active.never_serves(largest), largest, "that range served nothing"); - active.ranges[1].end = None; + let open = lite::Range { end: None, ..later }; + active.live = live(&[lite::Range::span(Some(0), 0, Some(3), None), open]); assert_eq!(active.never_serves(largest), None, "an open feed reaches it"); assert_eq!(active.never_serves(None), None, "nothing to go live at"); } @@ -3056,15 +3058,14 @@ mod tests { start: Some(floor), priority: 0, requested: Some(floor), - ranges: vec![lite::Range::span(Some(floor), 0, None, None)], + live: lite::LiveRanges::new(Version::Lite04, &[lite::Range::span(Some(floor), 0, None, None)]), tail: Default::default(), served: None, started: false, - starts: Vec::new(), + starts: Default::default(), end: None, wanted: None, unanswered: None, - updated: false, _working: crate::session::Work::default().start(), }); if update { @@ -3517,15 +3518,14 @@ mod tests { start: Some(2), priority: 0, requested: Some(2), - ranges: vec![lite::Range::span(Some(2), 0, None, None)], + live: lite::LiveRanges::new(VERSION, &[lite::Range::span(Some(2), 0, None, None)]), tail: Default::default(), served: None, started: false, - starts: Vec::new(), + starts: Default::default(), end: Some(lite::SubscribeFinish { group: 10, error: None }), wanted: None, unanswered: None, - updated: false, _working: crate::session::Work::default().start(), }; assert_eq!( @@ -3534,12 +3534,10 @@ mod tests { "without SUBSCRIBE_START nothing was served" ); - // An update moves the floor, the ranges, and `updated` together, as - // `begin_subscription` does. + // An update moves the floor and the ranges together, as `begin_subscription` does. let update = |sub: &mut SubStream, start: Option, ranges: Vec| { sub.start = start; - sub.ranges = ranges; - sub.updated = true; + sub.edit(&ranges).unwrap(); }; sub.started = true; update(&mut sub, Some(1), vec![lite::Range::span(Some(1), 0, None, None)]); @@ -3550,10 +3548,11 @@ mod tests { ); sub.start = Some(2); - sub.ranges = vec![ + sub.edit(&[ lite::Range::span(Some(2), 0, Some(3), None), lite::Range::span(Some(6), 0, None, None), - ]; + ]) + .unwrap(); assert_eq!( sub.owed(None), Some(vec![2..4, 6..10]), @@ -3561,9 +3560,10 @@ mod tests { ); update(&mut sub, Some(2), vec![lite::Range::span(Some(2), 0, None, None)]); - sub.updated = false; + // A START for the range now live. sub.served = Some(4); - sub.starts = vec![Some(4)]; + let (id, _) = sub.live.iter().next().unwrap(); + sub.starts = [(id, Some(4))].into(); assert_eq!( sub.owed(None), Some(vec![4..10]), @@ -7906,11 +7906,6 @@ fn wire_ranges(subscription: &Subscription, timescale: Option) -> Res Ok((!ranges.is_empty()).then_some(ranges)) } -/// Whether every range has an end. -fn bounded(ranges: &[lite::Range]) -> bool { - ranges.iter().all(|range| range.end.is_some()) -} - /// `subscription` narrowed to the one span a wire without ranges carries, which the /// subscribers' own reads narrow again. /// @@ -7945,8 +7940,8 @@ struct SubStream { /// The lowest absolute start among the ranges, or `None` when one counts back from /// the latest group: the floor below which nothing is asked for. start: Option, - /// The ranges on the wire, never empty. - ranges: Vec, + /// The ranges on the wire by Range ID, never empty. + live: lite::LiveRanges, priority: i8, order: crate::track::Order, /// The start the SUBSCRIBE itself carried, fixed for the stream's life. A @@ -7961,9 +7956,9 @@ struct SubStream { served: Option, /// Whether SUBSCRIBE_START arrived, which `served` alone cannot say when no range starts. started: bool, - /// Where SUBSCRIBE_START says each range's delivery starts, `None` for one with nothing - /// to deliver. It matches `ranges` until an update changes them. - starts: Vec>, + /// Where SUBSCRIBE_START or SUBSCRIBE_STARTED says each live range's delivery starts, by + /// Range ID, `None` for one with nothing to deliver. + starts: std::collections::BTreeMap>, /// The track's exclusive end (SUBSCRIBE_FINISH), once declared. end: Option, /// The refresh join's wanted group, restated in every SUBSCRIBE_UPDATE. @@ -7972,9 +7967,6 @@ struct SubStream { /// without an instance resolves whichever one wins the path when it arrives, so one /// still unanswered once another instance takes the path may be answered by it. unanswered: Option, - /// Whether an update changed the ranges since the SUBSCRIBE, which the publisher may - /// have read before its SUBSCRIBE_START, so the starts need not match the SUBSCRIBE. - updated: bool, /// Holds the session busy for as long as the subscription is open. _working: crate::session::Working, } @@ -7992,36 +7984,45 @@ impl SubStream { /// serves Largest waits for it, so an old cache is judged against that edge. fn never_serves(&self, largest: Option) -> Option { largest.filter(|largest| { - bounded(&self.ranges) + self.live.iter().all(|(_, range)| range.end.is_some()) && self .spans() .is_none_or(|spans| !spans.iter().any(|span| span.contains(&largest.group))) }) } - /// Where each bounded range delivers, as SUBSCRIBE_START resolved it: from its absolute - /// start, or else from where SUBSCRIBE_START resolved delivery, up to its end. `None` when - /// a range is open, or the ranges changed with nothing served, so nothing says where. + /// Where the live range `id` delivers from, `None` while nothing says yet, and `Some(None)` + /// for a range with no group to deliver. + /// + /// Lite-07 reports every range's start: SUBSCRIBE_START the SUBSCRIBE's, and + /// SUBSCRIBE_STARTED each one an update added. Before it, the one start describes the + /// SUBSCRIBE's range, so a range an update replaced it with delivers from its absolute + /// start, or else from where delivery started. + fn range_start(&self, id: u64, range: &lite::Range) -> Option> { + if let Some(start) = self.starts.get(&id) { + return Some(*start); + } + if self.live.reports_starts() { + return None; + } + match range.start { + Some(lite::Anchor::Absolute(start)) => Some(Some(start.group)), + _ => self.served.or(self.start).map(Some), + } + } + + /// Where each bounded range delivers, as its reported start resolved it, up to its end. + /// `None` when a range is open, or nothing says yet where one starts. fn spans(&self) -> Option>> { - let Some(served) = self.served else { - return (!self.updated).then(Vec::new); - }; - self.ranges + self.live .iter() - .enumerate() - .map(|(index, range)| { + .map(|(id, range)| { // Our absolute ends name a group's first frame (see `wire_ranges`). let Some(lite::Anchor::Absolute(end)) = range.end else { return None; }; - let start = match self.starts.get(index) { - // Each range's own start accounts for its skipped prefix. - Some(start) if !self.updated => start.unwrap_or(end.group), - _ => match range.start { - Some(lite::Anchor::Absolute(start)) => start.group, - _ => served, - }, - }; + // Each range's own start accounts for its skipped prefix. + let start = self.range_start(id, range)?.unwrap_or(end.group); Some(start..end.group) }) .collect() @@ -8030,30 +8031,22 @@ impl SubStream { /// The groups the publisher still owes once it has ended the subscription, one run per /// range: the publisher accounts for nothing between disjoint ranges. /// - /// `None` when nothing says which: drafts before SUBSCRIBE_FINISH only have the FIN. - /// Without a SUBSCRIBE_START the publisher served no group at all, unless an update since - /// moved the floor the demand asks from. Each range is owed from where SUBSCRIBE_START - /// said it starts while the ranges are still the SUBSCRIBE's, which accounts for a skipped - /// prefix and resolves a relative or time start; after an update, from its absolute start, - /// or else from the first group served. The groups the SUBSCRIBE asked for below its - /// SUBSCRIBE_START are accounted for as unavailable when it arrives. + /// `None` when nothing says which: drafts before SUBSCRIBE_FINISH only have the FIN, and a + /// lite-07 range an update added owes nothing known until SUBSCRIBE_STARTED says where it + /// starts. Without a SUBSCRIBE_START the publisher served no group at all. Each range is + /// owed from its reported start (see [`Self::range_start`]), which accounts for a skipped + /// prefix and resolves a relative or time start. The groups the SUBSCRIBE asked for below + /// its SUBSCRIBE_START are accounted for as unavailable when it arrives. fn owed(&self, requested_end: Option) -> Option>> { let end = self.end.as_ref()?.group; let end = requested_end.map_or(end, |requested| requested.min(end)); - // A SUBSCRIBE_START that started no range owes nothing, unless an update since asked - // for more, which the open stream still serves. - if self.served.is_none() && !(self.started && self.updated) { + if !self.started { return Some(Vec::new()); } - let fallback = self.served.or(self.start); - let owed = self.ranges.iter().enumerate().filter_map(|(index, range)| { - let start = match (self.updated, self.starts.get(index)) { - (false, Some(start)) => (*start)?, - _ => match range.start { - Some(lite::Anchor::Absolute(start)) => start.group, - // A start counted back from the latest group resolves where delivery did. - _ => fallback?, - }, + let mut owed = Vec::new(); + for (id, range) in self.live.iter() { + let Some(start) = self.range_start(id, range)? else { + continue; }; // Our absolute ends name a group's first frame (see `wire_ranges`), and the rest // go upstream open. @@ -8061,9 +8054,22 @@ impl SubStream { Some(lite::Anchor::Absolute(stop)) => stop.group.min(end), _ => end, }; - (start < stop).then_some(start..stop) - }); - Some(owed.collect()) + if start < stop { + owed.push(start..stop); + } + } + Some(owed) + } + + /// Edit the live ranges into `ranges`, forgetting the starts of those retired. Returns the + /// edit, and whether it changed anything. + fn edit(&mut self, ranges: &[lite::Range]) -> Result<(lite::RangeEdit, bool), Error> { + let before: Vec<_> = self.live.iter().map(|(id, _)| id).collect(); + let edit = self.live.edit(ranges)?; + let changed = !self.live.iter().map(|(id, _)| id).eq(before); + let live = &self.live; + self.starts.retain(|id, _| live.get(*id).is_some()); + Ok((edit, changed)) } } @@ -8489,8 +8495,8 @@ impl TrackServe { // So do changed ranges that keep the floor, such as a widening that joins // two: each gap they reach waits a fresh grace, which only delays giving up // on one still owed. - let widened = (active.ranges != ranges) - .then(|| subscription.start().or(active.served).unwrap_or(0)..u64::MAX); + let (edit, changed) = active.edit(&ranges)?; + let widened = changed.then(|| subscription.start().or(active.served).unwrap_or(0)..u64::MAX); if let Ok(mut tail) = active.tail.write() { tail.set_grace(producer.tail_grace(subscription.budget())); let now = self.subscriber.runtime.now(); @@ -8499,8 +8505,6 @@ impl TrackServe { } } active.start = subscription.start(); - active.updated |= active.ranges != ranges; - active.ranges = ranges; if supports_update { // The floor follows the requested start, in both directions: // moving below a declared SUBSCRIBE_START reopens those groups @@ -8513,7 +8517,7 @@ impl TrackServe { if start_moved { let _ = producer.start_at(active.start); } - buffer_update(active)?; + buffer_update(active, edit)?; } Ok(Begin::None) } @@ -8767,14 +8771,15 @@ fn wire_max_delay(max_delay: Option) -> std::time::Duration max_delay.unwrap_or(std::time::Duration::from_millis(super::MAX_SAFE_DELAY_MS)) } -/// Buffer the current SUBSCRIBE parameters. The caller flushes. -fn buffer_update(active: &mut SubStream) -> Result<(), Error> { +/// Buffer the current SUBSCRIBE parameters, with `ranges` the change to the live ranges. +/// The caller flushes. +fn buffer_update(active: &mut SubStream, ranges: lite::RangeEdit) -> Result<(), Error> { active.stream.writer.buffer(&lite::SubscribeUpdate { wanted_group: active.wanted, priority: active.priority, max_delay: active.max_delay, min_delay: active.min_delay, - ranges: active.ranges.clone(), + ranges, order: active.order, }) } @@ -8862,17 +8867,16 @@ impl Establish { max_delay: wire_max_delay(self.subscription.delay.max), min_delay: self.subscription.delay.min, start: self.subscription.start(), - ranges: self.ranges.clone(), + live: lite::LiveRanges::new(self.serve.subscriber.version, &self.ranges), priority: self.subscription.priority, order: self.subscription.order, requested: self.subscription.start(), tail: self.tail.clone(), served: None, started: false, - starts: Vec::new(), + starts: Default::default(), end: None, wanted: None, - updated: false, _working: self.session.work().start(), } } @@ -9921,17 +9925,24 @@ impl ServeLoop { // so a reader waiting on a skipped group fails over // instead of stalling on a live route. lite::SubscribeResponse::Start(start) => { - // One resolved start per range the SUBSCRIBE carried, unless an - // update the publisher read first replaced them. - if serve.subscriber.version.has_ranges() && !active.updated && start.starts.len() != active.ranges.len() - { - tracing::warn!(track = %serve.name, starts = start.starts.len(), ranges = active.ranges.len(), "subscribe start resolves other ranges"); + if active.started && serve.subscriber.version.has_ranges() { + tracing::warn!(track = %serve.name, "duplicate subscribe start"); + return Err(Error::ProtocolViolation); + } + // One resolved start per range the SUBSCRIBE carried, by Range ID. + if start.starts.len() as u64 != active.live.initial_count() { + tracing::warn!(track = %serve.name, starts = start.starts.len(), ranges = active.live.initial_count(), "subscribe start resolves other ranges"); return Err(Error::ProtocolViolation); } // No range starting anywhere leaves nothing owed. let served = start.starts.iter().flatten().min().copied(); active.served = served; - active.starts = start.starts.clone(); + // An update may have retired some already, whose starts say nothing now. + for (id, start) in (0..).zip(&start.starts) { + if active.live.get(id).is_some() { + active.starts.insert(id, *start); + } + } // Where the live feed is, on versions whose answer says. if serve.subscriber.version.has_largest() { // A bounded feed that never delivers Largest goes live at the @@ -9969,6 +9980,26 @@ impl ServeLoop { tail.account(requested..served, serve.subscriber.runtime.now()); } } + // Where a range an update added starts, from which its groups are owed. + lite::SubscribeResponse::Started(started) => { + let id = started.id; + if !active.started + || !active.live.assigned(id) + || active.live.initial(id) + || active.starts.contains_key(&id) + { + tracing::warn!(track = %serve.name, id, "unexpected subscribe started"); + return Err(Error::ProtocolViolation); + } + // One retired since is owed nothing. + if active.live.get(id).is_some() { + active.starts.insert(id, started.start); + // The tail judges what is owed again. + if let Ok(mut tail) = active.tail.write() { + tail.expire(serve.subscriber.runtime.now()); + } + } + } // The publisher will never send these groups, so they // are accounted for without a stream, and a reader of // one still arriving ends with the drop's error now. @@ -10206,7 +10237,8 @@ impl ServeLoop { && active.wanted != Some(wanted) { active.wanted = Some(wanted); - if let Err(err) = buffer_update(active) { + let ranges = active.live.unchanged(); + if let Err(err) = buffer_update(active, ranges) { return Poll::Ready(ServeEnd::GiveBack(err)); } continue; diff --git a/rs/moq-net/src/model/resume.rs b/rs/moq-net/src/model/resume.rs index 666b212bb27..c15b2db5799 100644 --- a/rs/moq-net/src/model/resume.rs +++ b/rs/moq-net/src/model/resume.rs @@ -860,6 +860,11 @@ impl Subscriber { self.reader().window().starts(from) } + /// Each of the reader's ranges resolved; see [`track::Subscriber::resolved`]. + pub(super) fn resolved(&self) -> Vec> { + self.reader().window().resolved() + } + /// 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 { diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index 1620d8e9858..e1ba7c81892 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -855,12 +855,20 @@ impl Window { } } - /// Each range resolved, in the order given: `None` for one empty as given, whose start - /// still waits, or still looking up its start. One that only resolves empty keeps its - /// span, ending at or below its start. An unsettled time end is reported open, never at - /// its cap. + /// Each range resolved, in the order given: `None` for one whose start still waits, or + /// still looking up its start. One empty as given, or that only resolves empty, ends at + /// or below its start. An unsettled time end is reported open, never at its cap. pub fn resolved(&self) -> Vec> { - let mut resolved = vec![None; self.ranges.len()]; + let mut resolved: Vec<_> = self + .ranges + .iter() + .map(|range| match (range.start, range.end) { + (Anchor::Absolute(start), Some(Anchor::Absolute(end))) if range.is_empty() => { + Some(Resolved { start, end: Some(end) }) + } + _ => None, + }) + .collect(); for span in self.spans.iter().filter(|span| span.looking.is_none() && !span.waiting) { resolved[span.index] = Some(Resolved { start: span.start, diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index e198ac52c3e..87b842da5d8 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -8482,6 +8482,22 @@ impl Subscriber { } } + /// Each of this subscriber's ranges as resolved here, by index: its first group and + /// exclusive end, `None` while open, or `None` for one whose start still waits on the + /// live feed or a lookup. One empty as given, or that only resolves empty, ends at or + /// below its start. + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + pub(crate) fn resolved(&mut self) -> Vec)>> { + let resolved = match &mut self.inner { + Inner::Plain(cursor) => { + cursor.sync(); + cursor.filter.window.resolved() + } + Inner::Resume(resume, _) => resume.resolved(), + }; + resolved.into_iter().map(|r| r.map(|r| (r.start, r.end))).collect() + } + /// The lowest group at or above `from` this subscriber's ranges and /// [`Self::with_range`] floor cover, `None` when none does: the groups between are /// never read, so a reader waiting on each group in sequence need not wait for them. From 587b1fafc397df7489ea28469fb19bc1df707335 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 4 Nov 2026 07:01:12 -0800 Subject: [PATCH 14/30] feat(net)!: lite-07 Range IDs and SUBSCRIBE_STARTED in JS, and SUBSCRIBE_FINISH by name The JS codec, publisher, and subscriber carry lite-07 Range IDs: updates edit the live ranges, SUBSCRIBE_OK answers the SUBSCRIBE's ranges, and SUBSCRIBE_STARTED reports where each range an update adds starts. The JS names SUBSCRIBE_FINISH as Rust and the draft do, and its subscriber drops the covered-FIN reopen, which no lite-07 publisher triggers. Co-Authored-By: Claude Opus 5.5 --- js/net/src/internal.ts | 12 +- js/net/src/lite/connection.test.ts | 14 +- js/net/src/lite/publisher.test.ts | 148 ++++++++--- js/net/src/lite/publisher.ts | 119 +++++++-- js/net/src/lite/subscribe.test.ts | 107 ++++++-- js/net/src/lite/subscribe.ts | 389 +++++++++++++++++++++++++---- js/net/src/lite/subscriber.test.ts | 181 +------------- js/net/src/lite/subscriber.ts | 289 +++++++-------------- js/net/src/lite/tail.test.ts | 57 +++-- js/net/src/lite/version.ts | 4 +- js/net/src/resume.ts | 9 +- js/net/src/tail.test.ts | 2 +- js/net/src/track.test.ts | 2 +- js/net/src/track.ts | 6 + js/net/src/window.ts | 11 +- rs/moq-net/src/lite/subscriber.rs | 58 +++++ 16 files changed, 879 insertions(+), 529 deletions(-) diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index bc994677a7d..ae19e712462 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -30,7 +30,7 @@ import type { } from "./track.ts"; import { max, min } from "./util/sequence.ts"; import * as Varint from "./varint.ts"; -import type { Window } from "./window.ts"; +import type { Resolved, Window } from "./window.ts"; import type { Advertised, Advertisements } from "./wire.ts"; /** What a null {@link Delay.max} goes out as: the largest value every peer can read, as Rust sends for `None`. */ @@ -375,6 +375,8 @@ export interface ResumeSource { end(): Position | undefined; /** Where each range starts on the serving copy once delivery starts at `from`, undefined without one. */ starts(from: bigint): (bigint | undefined)[] | undefined; + /** Each range as the serving copy resolved it, undefined without one. */ + resolved(): (Resolved | undefined)[] | undefined; /** The runs of `[start, end)` the serving copy reads, undefined without one. */ runs(start: bigint, end: bigint): Run[] | undefined; /** @@ -565,6 +567,11 @@ export const hooks: { * with no group there: what lite-07 SUBSCRIBE_START answers. Assigned by `track.ts`. */ servingStarts: (subscriber: Subscriber, from: bigint) => (bigint | undefined)[]; + /** + * Each of a subscriber's ranges as resolved where it is served, undefined for one whose start + * still waits: what lite-07 SUBSCRIBE_STARTED answers. Assigned by `track.ts`. + */ + servingResolved: (subscriber: Subscriber) => (Resolved | undefined)[]; /** The lowest buffered candidate, which an ordered take may wait below; `floor` passes sequences taken on other copies. */ peekGroup: (subscriber: Subscriber, ordered: boolean, floor?: bigint) => bigint | undefined; /** Take the next group in sequence order without waiting, as {@link tryRecvGroup} does in arrival order. */ @@ -774,6 +781,9 @@ export const hooks: { servingStarts: () => { throw new Error("track.ts not loaded"); }, + servingResolved: () => { + throw new Error("track.ts not loaded"); + }, makeRequest: () => { throw new Error("track.ts not loaded"); }, diff --git a/js/net/src/lite/connection.test.ts b/js/net/src/lite/connection.test.ts index 7af5f8d8db9..f3bf2b621d3 100644 --- a/js/net/src/lite/connection.test.ts +++ b/js/net/src/lite/connection.test.ts @@ -20,7 +20,7 @@ import { decodeSubscribeResponse, encodeSubscribeResponse, Subscribe, - SubscribeEnd, + SubscribeFinish, SubscribeStart, } from "./subscribe.ts"; import { TrackInfo, Track as TrackMessage } from "./track.ts"; @@ -539,9 +539,9 @@ async function servedRawSubscription(alpn: string = ALPN_07_WIP, version: Versio expect("start" in (await decodeSubscribeResponse(subscriber.reader, version))).toBe(true); producer.finish(); producer.close(); - // SUBSCRIBE_END, then before lite-07 the publisher's FIN; lite-07 keeps the stream open. + // SUBSCRIBE_FINISH, then before lite-07 the publisher's FIN; lite-07 keeps the stream open. if (keepsSubscribeOpen(version)) { - while (!("end" in (await decodeSubscribeResponse(subscriber.reader, version)))) {} + while (!("finish" in (await decodeSubscribeResponse(subscriber.reader, version)))) {} } else await subscriber.reader.readAll(); let closed = false; @@ -561,7 +561,7 @@ async function servedRawSubscription(alpn: string = ALPN_07_WIP, version: Versio } // The publisher's FIN says nothing about whether the subscriber read the tail, and a receiver -// may drop unread bytes on close. From lite-05, whose SUBSCRIBE_END lets the subscriber settle +// may drop unread bytes on close. From lite-05, whose SUBSCRIBE_FINISH lets the subscriber settle // its tail at once, the subscriber's FIN says so, and close waits for it. for (const [alpn, version] of [ [ALPN_05, Version.DRAFT_05], @@ -718,8 +718,8 @@ test("close waits for a request served while withdrawals are in flight", async ( expect("start" in (await decodeSubscribeResponse(subscriber.reader, version))).toBe(true); producer.finish(); producer.close(); - // SUBSCRIBE_END; lite-07 keeps the stream open past it. - while (!("end" in (await decodeSubscribeResponse(subscriber.reader, version)))) {} + // SUBSCRIBE_FINISH; lite-07 keeps the stream open past it. + while (!("finish" in (await decodeSubscribeResponse(subscriber.reader, version)))) {} // The withdrawal completes, but the subscription still waits for the subscriber's FIN. fin.release(); @@ -902,7 +902,7 @@ for (const violation of ["a partial group", "a group past the declared end"] as // A received tail uses its wire position even without a locally retained prefix. group = new GroupMessage({ subscribe: 0n, sequence: 0n, frameStart: 5 }); } else { - await encodeSubscribeResponse(sub.writer, { end: new SubscribeEnd(2n) }, version); + await encodeSubscribeResponse(sub.writer, { finish: new SubscribeFinish(2n) }, version); group = new GroupMessage({ subscribe: 0n, sequence: 2n }); } const writer = await Writer.open(pair.server, { version }); diff --git a/js/net/src/lite/publisher.test.ts b/js/net/src/lite/publisher.test.ts index 9dd2a350cf1..008987c7792 100644 --- a/js/net/src/lite/publisher.test.ts +++ b/js/net/src/lite/publisher.test.ts @@ -26,11 +26,11 @@ import { decodeSubscribeResponse, decodeSubscribeResponseMaybe, Subscribe, - type SubscribeEnd, + type SubscribeFinish, SubscribeUpdate, } from "./subscribe.ts"; import { Track as TrackMessage } from "./track.ts"; -import { ALPN_05, ALPN_06, ALPN_07_WIP, keepsSubscribeOpen, Version } from "./version.ts"; +import { ALPN_05, ALPN_06, ALPN_07_WIP, hasRanges, keepsSubscribeOpen, Version } from "./version.ts"; function publish(origin: OriginProducer, path: Path.Valid) { const broadcastPublication = origin.createBroadcast(path); @@ -50,6 +50,16 @@ function replayUpdate(props: ConstructorParameters[0]) { return new SubscribeUpdate({ ...props, maxDelay: TEST_MAX_DELAY_MS }); } +// A lite-07 update that keeps its ranges, which edits none of them. +function keep(version: Version) { + return hasRanges(version) ? { ranges: [] } : {}; +} + +// A lite-07 update restating its span retires the range it replaces, the one with Range ID `id`. +function restate(version: Version, id = 0n) { + return hasRanges(version) ? { remove: [id] } : {}; +} + test.each([Version.DRAFT_01, Version.DRAFT_03, Version.DRAFT_06])( "an initial announcement write failure disposes its listener in version %s", async (version) => { @@ -191,12 +201,12 @@ test.each([Version.DRAFT_06, Version.DRAFT_07])( ); // Delivers `sequences` in the given order, finishes the track (with `error`, if given), and -// returns the SUBSCRIBE_END the publisher put on the wire. +// returns the SUBSCRIBE_FINISH the publisher put on the wire. async function subscribeEnd( sequences: number[], version: Version = Version.DRAFT_05, error?: Error, -): Promise { +): Promise { const pair = createMockTransportPair(version === Version.DRAFT_07 ? ALPN_07_WIP : ALPN_05); const origin = new OriginProducer(); const publisher = new Publisher(pair.server, version, randomHop(), origin.consume()); @@ -230,7 +240,7 @@ async function subscribeEnd( try { for (;;) { const resp = await decodeSubscribeResponse(client.reader, version); - if ("end" in resp) return resp.end; + if ("finish" in resp) return resp.finish; } } finally { publisher.close(); @@ -296,7 +306,7 @@ async function groupSendOrders(options: { try { for (const [index, sequence] of sequences.entries()) { if (index === 1 && update !== undefined) { - await replayUpdate({ priority: update }).encode(client.writer, version); + await replayUpdate({ priority: update, ...keep(version) }).encode(client.writer, version); // Wait for the publisher to apply it, rather than assuming it beat the next group. while (track.subscription.peek()?.priority !== update) await track.subscription.changed(); } @@ -896,6 +906,60 @@ test("lite-07 SUBSCRIBE_START names each resolved range start", async () => { } }); +// A range an update adds learns where it starts from SUBSCRIBE_STARTED, and keeps that start however +// many later updates leave it live, rather than counting back from a newer latest group. +test("lite-07 SUBSCRIBE_STARTED reports an added range's start, which later updates keep", async () => { + const version = Version.DRAFT_07; + const sub = await servedSubscription({ + version, + prefill: [0n, 1n, 2n, 3n, 4n], + maxAge: Milli(3_600_000), + ranges: [{ start: { absolute: 0n }, end: { absolute: 1n } }], + }); + const next = async () => { + for (;;) { + const resp = await decodeSubscribeResponse(sub.client.reader, version); + if ("start" in resp || "started" in resp) return resp; + } + }; + try { + const start = await next(); + if (!("start" in start)) throw new Error("expected SUBSCRIBE_OK"); + expect(start.start.starts).toEqual([0n]); + + // Two groups back from the latest, group 2, which takes Range ID 1. + await replayUpdate({ priority: 0, ranges: [{ start: { relative: 2 } }] }).encode(sub.client.writer, version); + const started = await next(); + if (!("started" in started)) throw new Error("expected SUBSCRIBE_STARTED"); + expect([started.started.id, started.started.start]).toEqual([1n, 2n]); + + // Newer groups arrive, and another update leaves the range live: it still starts at 2. + for (const sequence of [5n, 6n, 7n]) sub.serve(sequence); + await replayUpdate({ priority: 0, ranges: [{ start: { absolute: 9n } }] }).encode(sub.client.writer, version); + const pinned = await next(); + if (!("started" in pinned)) throw new Error("expected SUBSCRIBE_STARTED"); + expect([pinned.started.id, pinned.started.start]).toEqual([2n, 9n]); + const ranges = sub.track.subscription.peek()?.ranges ?? []; + expect(ranges.some((range) => range.start?.absolute === 2n)).toBe(true); + expect(ranges.some((range) => range.start?.relative !== undefined)).toBe(false); + } finally { + await sub.close(); + } +}); + +// An edit naming a Range ID the subscription never assigned violates the protocol. +test("lite-07 refuses an update retiring an unknown Range ID", async () => { + const version = Version.DRAFT_07; + const sub = await servedSubscription({ version, prefill: [0n], ranges: [{ start: { absolute: 0n } }] }); + try { + await replayUpdate({ priority: 0, ranges: [], remove: [1n] }).encode(sub.client.writer, version); + await sub.serving; + await expect(sub.client.reader.closed).rejects.toThrow(); + } finally { + await sub.close(); + } +}); + // A bounded window that resolves empty has no group to answer it, so the publisher answers it // at once. The stream stays open for an update until the subscriber FINs, and the publisher // FINs in reply. @@ -1039,7 +1103,7 @@ for (const version of [Version.DRAFT_06, Version.DRAFT_07]) { try { sub.serve(5n); expect(await sub.servedSequence()).toBe(5n); - await replayUpdate({ priority: 0, startGroup: 2n }).encode(sub.client.writer, version); + await replayUpdate({ priority: 0, startGroup: 2n, ...restate(version) }).encode(sub.client.writer, version); await flush(); sub.track.finish(); sub.track.close(); @@ -1048,8 +1112,8 @@ for (const version of [Version.DRAFT_06, Version.DRAFT_07]) { const resp = await decodeSubscribeResponseMaybe(sub.client.reader, version); if (!resp) break; if ("drop" in resp) codes.push(resp.drop.error); - // Lite-07 keeps the stream open past SUBSCRIBE_END, after the sweep's drops. - if ("end" in resp && keepsSubscribeOpen(version)) break; + // Lite-07 keeps the stream open past SUBSCRIBE_FINISH, after the sweep's drops. + if ("finish" in resp && keepsSubscribeOpen(version)) break; } expect(codes).toEqual([version === Version.DRAFT_07 ? StreamCode.NotFound : 0]); } finally { @@ -1110,17 +1174,25 @@ test.each([Version.DRAFT_06, Version.DRAFT_07])("lite %s: datagrams honor group send([0, 2, 1]); expect(await recv()).toBe(1n); // Widen both boundaries, then narrow to one datagram. - await replayUpdate({ priority: 0, startGroup: 0n, startFrame: 0, endGroup: 4n, endFrame: 0 }).encode( - sub.client.writer, - version, - ); + await replayUpdate({ + priority: 0, + startGroup: 0n, + startFrame: 0, + endGroup: 4n, + endFrame: 0, + ...restate(version), + }).encode(sub.client.writer, version); await applied(5n); send([2, 3, 5, 4]); expect([await recv(), await recv(), await recv()]).toEqual([2n, 3n, 4n]); - await replayUpdate({ priority: 0, startGroup: 6n, startFrame: 1, endGroup: 7n, endFrame: 0 }).encode( - sub.client.writer, - version, - ); + await replayUpdate({ + priority: 0, + startGroup: 6n, + startFrame: 1, + endGroup: 7n, + endFrame: 0, + ...restate(version, 1n), + }).encode(sub.client.writer, version); await applied(8n); send([6, 8, 7]); expect(await recv()).toBe(7n); @@ -1874,7 +1946,7 @@ test("lite draft-07: subscribe end precedes groups below a declared finish", asy expect("start" in start).toBe(true); const pending = decodeSubscribeResponse(client.reader, Version.DRAFT_07); const early = await pending; - expect("end" in early).toBe(true); + expect("finish" in early).toBe(true); const second = track.createGroup(1n); second.writeFrame(textFrame("hello")); @@ -1883,15 +1955,15 @@ test("lite draft-07: subscribe end precedes groups below a declared finish", asy track.close(); const resp = await pending; - if (!("end" in resp)) throw new Error("expected SUBSCRIBE_END"); - expect(resp.end.group).toBe(2n); + if (!("finish" in resp)) throw new Error("expected SUBSCRIBE_FINISH"); + expect(resp.finish.group).toBe(2n); } finally { publisher.close(); client.close(); } }); -// Before lite-07 an errored end goes out only as the reset at that end: a clean SUBSCRIBE_END +// Before lite-07 an errored end goes out only as the reset at that end: a clean SUBSCRIBE_FINISH // sent early could let the subscriber settle before the reset arrives. test("lite draft-06: an errored end is not declared early", async () => { const pair = createMockTransportPair(ALPN_06); @@ -1944,8 +2016,8 @@ test("lite draft-06: a subscription capped short of an errored end gets its boun sub.track.finishAt({ group: 4n, frame: 0 }, new StreamError(StreamCode(71))); for (;;) { const resp = await decodeSubscribeResponse(sub.client.reader, Version.DRAFT_06); - if (!("end" in resp)) continue; - expect(resp.end.group).toBe(4n); + if (!("finish" in resp)) continue; + expect(resp.finish.group).toBe(4n); break; } } finally { @@ -1954,7 +2026,7 @@ test("lite draft-06: a subscription capped short of an errored end gets its boun }); // Serves one group with its stream open held until `open(ok)`, and returns the pending -// SUBSCRIBE_END plus the call that lets the open succeed or fail. +// SUBSCRIBE_FINISH plus the call that lets the open succeed or fail. async function heldOpenEnd() { const pair = createMockTransportPair(ALPN_07_WIP); const origin = new OriginProducer(); @@ -2006,12 +2078,12 @@ test("lite draft-07: subscribe end precedes a blocked stream open", async () => const held = await heldOpenEnd(); try { const early = await held.end; - expect("end" in early).toBe(true); + expect("finish" in early).toBe(true); held.open(true); const resp = await held.end; - if (!("end" in resp)) throw new Error("expected SUBSCRIBE_END"); - expect(resp.end.group).toBe(1n); + if (!("finish" in resp)) throw new Error("expected SUBSCRIBE_FINISH"); + expect(resp.finish.group).toBe(1n); } finally { held.close(); } @@ -2023,8 +2095,8 @@ test("lite draft-07: subscribe end precedes a failed stream open", async () => { try { held.open(false); const resp = await held.end; - if (!("end" in resp)) throw new Error("expected SUBSCRIBE_END"); - expect(resp.end.group).toBe(1n); + if (!("finish" in resp)) throw new Error("expected SUBSCRIBE_FINISH"); + expect(resp.finish.group).toBe(1n); const dropped = await held.next(); if (!("drop" in dropped)) throw new Error("expected SUBSCRIBE_DROP"); expect([dropped.drop.start, dropped.drop.end, dropped.drop.error]).toEqual([0n, 0n, 0]); @@ -2071,7 +2143,7 @@ test("lite draft-07: a group whose close fails is dropped", async () => { const start = await decodeSubscribeResponse(client.reader, Version.DRAFT_07); expect("start" in start).toBe(true); const end = await decodeSubscribeResponse(client.reader, Version.DRAFT_07); - if (!("end" in end)) throw new Error("expected SUBSCRIBE_END"); + if (!("finish" in end)) throw new Error("expected SUBSCRIBE_FINISH"); const dropped = await decodeSubscribeResponse(client.reader, Version.DRAFT_07); if (!("drop" in dropped)) throw new Error("expected SUBSCRIBE_DROP"); expect([dropped.drop.start, dropped.drop.end, dropped.drop.error]).toEqual([0n, 0n, 0]); @@ -2133,7 +2205,7 @@ test("blocked DROP responses stop opening later groups", async () => { track.finish(); track.close(); let dropped = 0; - // Lite-07 keeps the stream open past SUBSCRIBE_END, so read until every group is named. + // Lite-07 keeps the stream open past SUBSCRIBE_FINISH, so read until every group is named. while (dropped < 72) { const response = await decodeSubscribeResponseMaybe(client.reader, Version.DRAFT_07); if (!response) break; @@ -2159,7 +2231,7 @@ type Served = { sequence: bigint; frameStart: number; payloads: string[] }; /** * Serves `groups` (frame payloads per group, keyed by sequence) under `bounds`, and * reports every group stream that reached the wire plus the resolved - * SUBSCRIBE_START / SUBSCRIBE_END range. A group in `received` arrived as a tail + * SUBSCRIBE_START / SUBSCRIBE_FINISH range. A group in `received` arrived as a tail * starting at that frame. */ async function serve( @@ -2237,8 +2309,8 @@ async function serve( for (;;) { const resp = await decodeSubscribeResponse(client.reader, Version.DRAFT_06); if ("start" in resp) start = resp.start.starts[0]; - if ("end" in resp) { - end = resp.end.group; + if ("finish" in resp) { + end = resp.finish.group; break; } } @@ -2316,7 +2388,7 @@ test("lite draft-06: groups past the end group are not served", async () => { expect(served.map((s) => s.sequence)).toEqual([1n, 2n]); }); -// SUBSCRIBE_END names the track's exclusive final boundary, not the capped delivered +// SUBSCRIBE_FINISH names the track's exclusive final boundary, not the capped delivered // range: a Rust peer feeds it into finish_at, so a truncated value would silently drop // the held-back groups across languages. test("lite draft-06: a capped subscription still reports the track's final boundary", async () => { @@ -2497,7 +2569,7 @@ test("lite draft-05: a group waiting for a stream slot is dropped when the subsc }); // The publisher FINs the subscribe stream itself once a track ends, which must not be -// mistaken for the subscriber leaving: SUBSCRIBE_END counts those queued groups as +// mistaken for the subscriber leaving: SUBSCRIBE_FINISH counts those queued groups as // delivered, so dropping them here would strand the tail of every finite track. The FIN // tells the subscriber every group is accounted for, so it waits for the queued group. test("lite draft-05: a group waiting for a stream slot survives the track finishing", async () => { @@ -2506,10 +2578,10 @@ test("lite draft-05: a group waiting for a stream slot survives the track finish track.finish(); track.close(); - // SUBSCRIBE_END goes out while the group is still waiting for its slot. + // SUBSCRIBE_FINISH goes out while the group is still waiting for its slot. for (;;) { const resp = await decodeSubscribeResponse(client.reader, Version.DRAFT_05); - if ("end" in resp) break; + if ("finish" in resp) break; } // The FIN holds until the queued group is on the wire. diff --git a/js/net/src/lite/publisher.ts b/js/net/src/lite/publisher.ts index 89ed83f74fd..fc6ca3d3c57 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -33,6 +33,7 @@ import { untilAborted } from "../util/abort.ts"; import { nextChange } from "../util/change.ts"; import { GivenUp } from "../util/given_up.ts"; import { max, min } from "../util/sequence.ts"; +import type { Resolved } from "../window.ts"; import { type Advertised, type Advertisements, sameInstance, wireOf } from "../wire.ts"; import { AnnounceInit, AnnounceOk, type AnnounceRequest, encodeAnnounceBroadcast } from "./announce.ts"; import { Datagram as DatagramMessage } from "./datagram.ts"; @@ -42,14 +43,19 @@ import { Group as GroupMessage } from "./group.ts"; import { Probe } from "./probe.ts"; import { encodeSubscribeResponse, + entriesOf, hasWireTime, + LiveRanges, + type RangeEntry, requestedRanges, type Subscribe, SubscribeDrop, - SubscribeEnd, + SubscribeFinish, SubscribeOk, SubscribeStart, + SubscribeStarted, SubscribeUpdate, + spanOf, } from "./subscribe.ts"; import { TrackInfo as TrackInfoMessage, type Track as TrackMessage } from "./track.ts"; import { encodeTracksEvent, type Tracks as TracksMessage } from "./tracks.ts"; @@ -240,8 +246,11 @@ type ServeFetch = Omit; /** What the serving loop takes from the subscribe stream instead of serving a group. */ type Control = - /** The peer re-stated the subscription: priority, ordering, latency, and both ranges. */ - | { kind: "update"; update: SubscribeUpdate } + /** + * The peer re-stated the subscription: priority, ordering, latency, and its ranges, with the + * Range ID of each on lite-07. + */ + | { kind: "update"; update: SubscribeUpdate; ids: bigint[] } /** The peer FIN'd, or our own half went away. Either way there is nobody left to serve. */ | { kind: "done" } /** The stream failed; the subscription goes down with it. */ @@ -252,7 +261,10 @@ type SubscriptionControlOptions = { reader: Reader; writer: Writer; version: Version; - apply: (update: SubscribeUpdate) => void; + /** The SUBSCRIBE's ranges, which each lite-07 update edits. */ + live: LiveRanges; + /** Apply an update, with every live range by Range ID on lite-07. */ + apply: (update: SubscribeUpdate, live: { id: bigint; entry: RangeEntry }[]) => void; }; /** @@ -270,7 +282,7 @@ type SubscriptionControlOptions = { */ class SubscriptionControls { #writer: Writer; - #update?: SubscribeUpdate; + #update?: { update: SubscribeUpdate; ids: bigint[] }; // Sticky, first one wins: null once the stream is over, an Error once it failed. #end?: Error | null; #ended: Promise; @@ -300,7 +312,7 @@ class SubscriptionControls { const update = this.#update; this.#update = undefined; // An update decoded before the stream ended still applies before the sticky end. - if (update) return { kind: "update", update }; + if (update) return { kind: "update", ...update }; if (this.#end === undefined) return undefined; return this.#end === null ? { kind: "done" } : { kind: "error", error: this.#end }; } @@ -349,15 +361,18 @@ class SubscriptionControls { this.#changed.update((value) => value + 1); } - async #decode({ reader, version, apply, quic }: SubscriptionControlOptions) { + async #decode({ reader, version, live, apply, quic }: SubscriptionControlOptions) { try { // Runs until the peer's half ends, even past our own FIN: a lite-05+ publisher waits // for that end, and every other teardown stops the reader. for (;;) { const update = await SubscribeUpdate.decodeMaybe(reader, version); if (!update) break; - apply(update); - this.#update = update; + // Each lite-07 update edits the live ranges, which only the stream's history names. + const ranges = live.apply(update); + apply(update, ranges); + // Resolved in full, so the latest one replaces any the loop has yet to take. + this.#update = { update, ids: ranges.map(({ id }) => id) }; this.#changed.update((value) => value + 1); } } catch (err: unknown) { @@ -369,6 +384,23 @@ class SubscriptionControls { } } +// `entry`, a range this subscription already held, resolved as it was: a range resolves once, so +// an update that leaves it live never counts its relative anchors back from a newer latest group. +function pin(entry: RangeEntry, resolved: Resolved | undefined): RangeEntry { + if (!resolved) return entry; + const range = { ...entry.range }; + let { from, to } = entry; + if (range.start?.relative !== undefined) { + range.start = { absolute: resolved.start }; + from = [resolved.start, 0]; + } + if (range.end?.relative !== undefined && resolved.end !== undefined) { + range.end = { absolute: resolved.end }; + to = [resolved.end, 0]; + } + return { range, from, to }; +} + // Where each range of `track` starts once delivery starts at `from`, undefined for one with no // group there. Before lite-07 there is one range, which starts there. function rangeStarts(version: Version, track: track.Subscriber, from: bigint): (bigint | undefined)[] { @@ -1004,7 +1036,7 @@ export class Publisher { if (hasTrackStream(this.version)) { // Lite-05+ accepts implicitly: no SUBSCRIBE_OK (the immutable // properties live in TRACK_INFO), and the resolved range arrives as - // SUBSCRIBE_START / SUBSCRIBE_END emitted from #runTrack. + // SUBSCRIBE_START / SUBSCRIBE_FINISH emitted from #runTrack. // // The timescale is an immutable property, so serving MUST use exactly // what TRACK_INFO advertised. It comes from the producer's accept(), so @@ -1030,12 +1062,25 @@ export class Publisher { datagrams = this.#runDatagrams(track, { sub: msg.id, stamp, served, bounds: datagramBounds }); } + // The SUBSCRIBE's ranges, and the Range ID of each range the model holds, in its order. + const asked = entriesOf(msg); + let ids = asked.map((_, index) => BigInt(index)); controls = new SubscriptionControls({ quic: this.#quic, reader: stream.reader, writer: stream.writer, version: this.version, - apply: (update) => { + live: new LiveRanges(this.version, asked), + apply: (update, live) => { + if (hasRanges(this.version)) { + const resolved = hooks.servingResolved(track); + const entries = live.map(({ id, entry }) => { + const index = ids.indexOf(id); + return index < 0 ? entry : pin(entry, resolved[index]); + }); + Object.assign(update, spanOf(this.version, entries)); + ids = live.map(({ id }) => id); + } Object.assign(datagramBounds, { startGroup: update.startGroup, startFrame: update.startFrame, @@ -1067,6 +1112,7 @@ export class Publisher { served, floor, bounds, + initial: BigInt(asked.length), }); console.debug(`publish done: broadcast=${msg.broadcast} track=${track.name}`); @@ -1234,9 +1280,11 @@ export class Publisher { // The read cursor's floor; see {@link cursorFloor}. floor: bigint; bounds: FrameBounds; + // How many ranges the SUBSCRIBE carried, whose starts SUBSCRIBE_START reports. + initial: bigint; }, ): Promise { - const { sub, broadcast, stamp, publisher, compress, served, bounds } = serving; + const { sub, broadcast, stamp, publisher, compress, served, bounds, initial } = serving; // The read cursor's floor; the subscription's ranges cap it. let floor = serving.floor; const moveCursor = () => track.withRange({ start: { included: floor } }); @@ -1247,7 +1295,7 @@ export class Publisher { moveCursor(); }; // Lite-05+ resolves the range on the subscribe stream: SUBSCRIBE_START once the - // first group is known, SUBSCRIBE_END when the track finishes. + // first group is known, SUBSCRIBE_FINISH when the track finishes. const emitRange = hasTrackStream(this.version); let startSent = false; // Dispositions before START refine its boundary; later ones must be named as drops. @@ -1258,6 +1306,35 @@ export class Publisher { // resolves where its feed starts. let first: { group: group.Consumer; largest: track.Position | undefined } | undefined; let endSent = false; + // The Range ID of each requested range, in the order the model holds them, and those an + // update added whose start no SUBSCRIBE_STARTED reported yet. Lite-07 only. + let ids = Array.from({ length: Number(initial) }, (_, index) => BigInt(index)); + let unstarted: bigint[] = []; + + // Report where each range an update added starts, once it resolves, or with no start once + // nothing can resolve it (`finished`). SUBSCRIBE_START goes first. + const reportStarted = async (finished: boolean): Promise => { + if (startSequence === undefined || unstarted.length === 0) return true; + const resolved = hooks.servingResolved(track); + const waiting: bigint[] = []; + for (const id of unstarted) { + const range = resolved[ids.indexOf(id)]; + if (!range && !finished) { + waiting.push(id); + continue; + } + let start: bigint | undefined; + if (range) { + start = max(range.start, startSequence); + if (range.end !== undefined && range.end <= start) start = undefined; + } + const started = new SubscribeStarted(id, start); + if (!(await controls.response(encodeSubscribeResponse(stream, { started }, this.version)))) + return false; + } + unstarted = waiting; + return true; + }; // Drop whatever part of `[start, end)` this subscription has not been served or dropped // yet, given up with `err`. @@ -1272,7 +1349,7 @@ export class Publisher { return true; }; - // The track's exclusive final boundary. A Rust subscriber feeds SUBSCRIBE_END + // The track's exclusive final boundary. A Rust subscriber feeds SUBSCRIBE_FINISH // straight into finish_at, so it must name the track's boundary (which counts // datagram sequences too), not the delivered range: a subscription cap can hold // produced groups back. The latest() fallback covers a subscription torn down @@ -1292,7 +1369,7 @@ export class Publisher { const endError = hooks.subscriberEndError(track); const code = endError && hasEndError(this.version) ? toStreamCode(endError) : undefined; return controls.response( - encodeSubscribeResponse(stream, { end: new SubscribeEnd(boundary(), code) }, this.version), + encodeSubscribeResponse(stream, { finish: new SubscribeFinish(boundary(), code) }, this.version), ); }; @@ -1305,7 +1382,7 @@ export class Publisher { if (end instanceof Error) throw end; return end === done; }; - // Before lite-07 an errored end is only the reset at that end: a clean SUBSCRIBE_END sent + // Before lite-07 an errored end is only the reset at that end: a clean SUBSCRIBE_FINISH sent // early could let the subscriber settle before the reset arrives. A subscription capped // short of the end is owed no error, so its boundary goes out. const declareEarly = () => @@ -1348,6 +1425,7 @@ export class Publisher { return false; } } + if (!(await reportStarted(false))) return false; // Control before data, matching the Rust publisher: every control decoded while // the loop was parked applies to the next pop, never to one already made. This // drain is synchronous, so nothing the decoder holds can land between the pop @@ -1367,6 +1445,13 @@ export class Publisher { // An update may reach the finished track's history, so it is served again. finished = false; const update = control.update; + if (hasRanges(this.version)) { + // A range retired before its start resolved is owed no SUBSCRIBE_STARTED. + unstarted = unstarted.filter((id) => control.ids.includes(id)); + for (const id of control.ids) + if (id >= initial && !ids.includes(id)) unstarted.push(id); + ids = control.ids; + } console.debug(`subscribe update: broadcast=${broadcast} track=${track.name}`); // `cursorFloor` pins an unfloored pre-06 SUBSCRIBE to the latest // group, so an update that drops a named floor re-pins there too. On @@ -1528,6 +1613,8 @@ export class Publisher { return false; continue; } + // Nothing arrives to resolve a range still waiting on the live feed. + if (!(await reportStarted(true))) return false; // Name every sequence from the start to the end this subscription never // got. A cap below the end bounds what the subscriber is owed, as on its side. if (startSequence !== undefined) { diff --git a/js/net/src/lite/subscribe.test.ts b/js/net/src/lite/subscribe.test.ts index 4f6f93ba313..1493a2cc6ac 100644 --- a/js/net/src/lite/subscribe.test.ts +++ b/js/net/src/lite/subscribe.test.ts @@ -7,15 +7,19 @@ import { decodeSubscribeResponse, encodeSubscribeResponse, inclusiveGroupEnd, + LiveRanges, requestedRanges, Subscribe, SubscribeDrop, - SubscribeEnd, + SubscribeFinish, SubscribeOk, type SubscribeResponse, SubscribeStart, + SubscribeStarted, SubscribeUpdate, + spanOf, toWireRanges, + type WireRange, } from "./subscribe.ts"; import { Version } from "./version.ts"; @@ -195,14 +199,15 @@ test("lite-07 carries the span as its only range, exclusive at the end", async ( test("a lite-07 list decodes as the span covering it", async () => { // Priority, order, max delay, and an unstated min delay as SubscribeUpdate writes them, - // then the ranges and an absent wanted group. - const head = (await encodeMessage(Version.DRAFT_07, new SubscribeUpdate({ priority: 0 }))).slice(1, 5); - const update = (ranges: number[]) => { - const body = [...head, ...ranges, 0]; - return SubscribeUpdate.decode( + // then no Range ID to retire, the ranges it adds, and an absent wanted group. + const head = (await encodeMessage(Version.DRAFT_07, new SubscribeUpdate({ priority: 0, ranges: [] }))).slice(1, 5); + const update = async (ranges: number[]) => { + const body = [...head, 0, ...ranges, 0]; + const got = await SubscribeUpdate.decode( new Reader(undefined, new Uint8Array([body.length, ...body]), Version.DRAFT_07), Version.DRAFT_07, ); + return { ...got, ...spanOf(Version.DRAFT_07, got.entries ?? []) }; }; // Groups 2 through 4 and 7 through frame 1 of group 9. @@ -234,7 +239,8 @@ test("a lite-07 list decodes as the span covering it", async () => { const oldest = await update([2, 0, 1, 3, 0, 1, 5, 2, 1, 5, 2]); expect(oldest.ranges).toEqual([{ end: { absolute: 3n } }, { start: { absolute: 5n }, end: { absolute: 5n } }]); - await expect(update([0])).rejects.toThrow(ProtocolViolation); + // An update may add no range, retiring some or none. + expect((await update([0])).ranges).toEqual([]); await expect(update([1, 4, 0, 0])).rejects.toThrow(ProtocolViolation); await expect(update([1, 0, 1, 0, 0])).rejects.toThrow(ProtocolViolation); }); @@ -304,24 +310,26 @@ test("SubscribeStart carries each range's start in place of one group on draft-0 await expect(encode(Version.DRAFT_06, { start })).rejects.toThrow(); }); -test("SubscribeEnd round-trips on draft-05", async () => { +test("SubscribeFinish round-trips on draft-05", async () => { // Type, length, exclusive final group. - expect(await encode(Version.DRAFT_05, { end: new SubscribeEnd(7n) })).toEqual(new Uint8Array([1, 1, 7])); - const got = await responseRoundtrip(Version.DRAFT_05, { end: new SubscribeEnd(7n) }); - expect("end" in got).toBe(true); - if (!("end" in got)) throw new Error("expected end"); - expect(got.end.group).toBe(7n); + expect(await encode(Version.DRAFT_05, { finish: new SubscribeFinish(7n) })).toEqual(new Uint8Array([1, 1, 7])); + const got = await responseRoundtrip(Version.DRAFT_05, { finish: new SubscribeFinish(7n) }); + expect("finish" in got).toBe(true); + if (!("finish" in got)) throw new Error("expected end"); + expect(got.finish.group).toBe(7n); }); -test("SubscribeEnd carries its error on draft-07", async () => { +test("SubscribeFinish carries its error on draft-07", async () => { // Type, length, exclusive final group, then the error code + 1, 0 for a clean end. - expect(await encode(Version.DRAFT_07, { end: new SubscribeEnd(7n) })).toEqual(new Uint8Array([1, 2, 7, 0])); - expect(await encode(Version.DRAFT_07, { end: new SubscribeEnd(7n, 71) })).toEqual(new Uint8Array([1, 2, 7, 72])); - const got = await responseRoundtrip(Version.DRAFT_07, { end: new SubscribeEnd(7n, 71) }); - if (!("end" in got)) throw new Error("expected end"); - expect([got.end.group, got.end.error]).toEqual([7n, 71]); + expect(await encode(Version.DRAFT_07, { finish: new SubscribeFinish(7n) })).toEqual(new Uint8Array([1, 2, 7, 0])); + expect(await encode(Version.DRAFT_07, { finish: new SubscribeFinish(7n, 71) })).toEqual( + new Uint8Array([1, 2, 7, 72]), + ); + const got = await responseRoundtrip(Version.DRAFT_07, { finish: new SubscribeFinish(7n, 71) }); + if (!("finish" in got)) throw new Error("expected end"); + expect([got.finish.group, got.finish.error]).toEqual([7n, 71]); // An older version has nowhere to carry it. - await expect(encode(Version.DRAFT_06, { end: new SubscribeEnd(7n, 71) })).rejects.toThrow(); + await expect(encode(Version.DRAFT_06, { finish: new SubscribeFinish(7n, 71) })).rejects.toThrow(); }); test("SubscribeDrop is restored on draft-07", async () => { @@ -385,7 +393,7 @@ test.each(["ascending", "descending"] as const)("lite-07 round-trips a %s group ); expect(got.order).toBe(order); - const update = await encodeMessage(version, new SubscribeUpdate({ priority: 3, order, maxDelay: 250 })); + const update = await encodeMessage(version, new SubscribeUpdate({ priority: 3, order, maxDelay: 250, ranges: [] })); // The length prefix, the priority byte, then the order. expect(update[1]).toBe(3 + 128); expect(update[2]).toBe(order === "ascending" ? 1 : 0); @@ -427,7 +435,7 @@ test("min delay survives lite-07 and decodes as not stated on legacy wires", asy const got = await Subscribe.decode(new Reader(undefined, subscribe, version), version); expect(got.minDelay).toBe(expected); - const update = await encodeMessage(version, new SubscribeUpdate({ priority: 3, minDelay })); + const update = await encodeMessage(version, new SubscribeUpdate({ priority: 3, minDelay, ranges: [] })); const decoded = await SubscribeUpdate.decode(new Reader(undefined, update, version), version); expect(decoded.minDelay).toBe(expected); } @@ -437,7 +445,7 @@ test("min delay survives lite-07 and decodes as not stated on legacy wires", asy test("wanted group survives lite-07 and disappears on legacy wires", async () => { for (const version of [Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07]) { for (const wantedGroup of [undefined, 0n, 3n, BigInt(Number.MAX_SAFE_INTEGER)]) { - const update = new SubscribeUpdate({ priority: 7, wantedGroup }); + const update = new SubscribeUpdate({ priority: 7, wantedGroup, ranges: [] }); const encoded = await encodeMessage(version, update); const decoded = await SubscribeUpdate.decode(new Reader(undefined, encoded, version), version); expect(decoded.wantedGroup).toBe(version === Version.DRAFT_07 ? wantedGroup : undefined); @@ -469,3 +477,56 @@ for (const version of [Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07]) { } }); } + +// An update sends only the ranges that changed, and the publisher resolves the same list, with the +// same Range IDs, as the subscriber holds. Mirrors the Rust `lite07_updates_edit_only_the_changed_ranges`. +test("lite-07 updates edit only the changed ranges", async () => { + const version = Version.DRAFT_07; + const a = { start: { absolute: 1n }, end: { absolute: 5n } }; + const b = { start: { relative: 3 } }; + const c = { start: { time: 9_000 } }; + const sent = new LiveRanges(version, [a, b]); + const live = new LiveRanges(version, [a, b]); + const step = async (ranges: WireRange[], remove: bigint[], add: WireRange[]) => { + const edit = sent.edit(ranges); + expect(edit).toEqual({ remove, add }); + const update = new SubscribeUpdate({ priority: 0, remove: edit.remove, ranges: edit.add }); + const got = await SubscribeUpdate.decode( + new Reader(undefined, await encodeMessage(version, update), version), + version, + ); + const resolved = live.apply(got).map(({ id, entry }) => [id, entry.range]); + expect(resolved).toEqual(sent.entries()); + }; + await step([b, a], [], []); + await step([a, c], [1n], [c]); + await step([c, c, a], [], [c]); + await step([b], [0n, 2n, 3n], [b]); + expect(live.entries()).toEqual([[4n, b]]); + + // An id never assigned or already retired, or nothing left live, is refused. + const refused = (remove: bigint[], ranges: WireRange[]) => + new LiveRanges(version, [a]).apply(new SubscribeUpdate({ priority: 0, remove, ranges })); + expect(() => refused([1n], [a])).toThrow(ProtocolViolation); + expect(() => refused([0n, 0n], [a])).toThrow(ProtocolViolation); + expect(() => refused([0n], [])).toThrow(ProtocolViolation); +}); + +test("SUBSCRIBE_STARTED is type 3 on lite-07 and unknown before it", async () => { + for (const start of [undefined, 0n, 7n]) { + const started = new SubscribeStarted(4n, start); + expect(await encode(Version.DRAFT_07, { started })).toEqual( + new Uint8Array([3, 2, 4, start === undefined ? 0 : Number(start) + 1]), + ); + const got = await responseRoundtrip(Version.DRAFT_07, { started }); + if (!("started" in got)) throw new Error("expected SUBSCRIBE_STARTED"); + expect([got.started.id, got.started.start]).toEqual([4n, start]); + } + await expect(encode(Version.DRAFT_06, { started: new SubscribeStarted(0n) })).rejects.toThrow(); + await expect( + decodeSubscribeResponse( + new Reader(undefined, new Uint8Array([3, 2, 0, 0]), Version.DRAFT_06), + Version.DRAFT_06, + ), + ).rejects.toThrow(ProtocolViolation); +}); diff --git a/js/net/src/lite/subscribe.ts b/js/net/src/lite/subscribe.ts index 1805ea73863..d9a2d8c7232 100644 --- a/js/net/src/lite/subscribe.ts +++ b/js/net/src/lite/subscribe.ts @@ -11,6 +11,7 @@ import * as Path from "../path.ts"; import type { Reader, Writer } from "../stream.ts"; import { type Timescale, Timestamp } from "../time.ts"; import type { Anchor, Order, Position, Range } from "../track.ts"; +import { lengthLeadingOnes, parts, split } from "../util/varint.ts"; import { decodePin, encodePin } from "./instance.ts"; import * as Message from "./message.ts"; import { fromWire, toWire } from "./priority.ts"; @@ -259,7 +260,7 @@ export function requestedRanges(msg: Span & { ranges?: WireRange[] }, timescale: } /** A position as a comparable pair, group then frame. */ -type Point = [group: bigint, frame: number]; +export type Point = [group: bigint, frame: number]; function before(a: Point, b: Point): boolean { return a[0] < b[0] || (a[0] === b[0] && a[1] < b[1]); @@ -288,39 +289,50 @@ async function decodeAnchor(r: Reader, end: boolean): Promise<{ point?: Point; a } /** - * The inverse of {@link encodeSpan}. A lite-07 list decodes as its ranges and as the one span - * covering every range in it, which the subscriber narrows again. + * A decoded lite-07 range, with the positions its absolute anchors name, which a + * {@link WireRange} rounds to whole groups. */ -async function decodeSpan(r: Reader, version: Version): Promise { - if (!hasRanges(version)) { - const startGroup = await decodeStartGroup(r, version); - const endGroup = (await r.u62()) || undefined; - const end = endGroup !== undefined ? endGroup - 1n : undefined; - const frames = await decodeFrameBounds(r, version, startGroup, end); - return { startGroup: canonicalStartGroup(version, startGroup, frames.startFrame), endGroup: end, ...frames }; - } +export interface RangeEntry { + range: WireRange; + from?: Point; + to?: Point; +} + +/** Decode one lite-07 range: its start, then its end. */ +async function decodeEntry(r: Reader): Promise { + const from = await decodeAnchor(r, false); + const to = await decodeAnchor(r, true); + const range: WireRange = {}; + if (from.anchor) range.start = from.anchor; + if (to.anchor) range.end = to.anchor; + // Empty partway through a group, which whole groups can't say: keep its place, + // covering no group, as the Rust publisher does. + if (from.point && to.point && !before(from.point, to.point)) range.end = { absolute: from.point[0] }; + return { range, from: from.point, to: to.point }; +} + +/** Decode a lite-07 `Range Count` and its ranges, which may be none. */ +async function decodeEntries(r: Reader): Promise { + // Not preallocated: each range costs at least two bytes of the bounded message. + const entries: RangeEntry[] = []; + for (let count = await r.u53(); count > 0; count--) entries.push(await decodeEntry(r)); + return entries; +} - const count = await r.u53(); - if (count === 0) throw new ProtocolViolation("a subscription asks for no range"); +/** + * Lite-07 `entries` as their ranges and the one span covering every one of them, which the + * subscriber narrows again. + */ +export function spanOf(version: Version, entries: readonly RangeEntry[]): Span & { ranges: WireRange[] } { let start: Point | undefined; let end: Point | undefined; let floor = true; let live = false; - const ranges: WireRange[] = []; - for (let i = 0; i < count; i++) { - const from = await decodeAnchor(r, false); - const to = await decodeAnchor(r, true); - const range: WireRange = {}; - if (from.anchor) range.start = from.anchor; - if (to.anchor) range.end = to.anchor; - // Empty partway through a group, which whole groups can't say: keep its place, - // covering no group, as the Rust publisher does. - if (from.point && to.point && !before(from.point, to.point)) range.end = { absolute: from.point[0] }; - ranges.push(range); - if (from.point === undefined) floor = false; - else if (start === undefined || before(from.point, start)) start = from.point; - if (to.point === undefined) live = true; - else if (end === undefined || before(end, to.point)) end = to.point; + for (const { from, to } of entries) { + if (from === undefined) floor = false; + else if (start === undefined || before(from, start)) start = from; + if (to === undefined) live = true; + else if (end === undefined || before(end, to)) end = to; } if (!floor) start = undefined; if (live) end = undefined; @@ -329,7 +341,7 @@ async function decodeSpan(r: Reader, version: Version): Promise entry.range), startGroup: canonicalStartGroup(version, start?.[0], startFrame), startFrame, endGroup: end === undefined ? undefined : end[1] === 0 ? end[0] - 1n : end[0], @@ -337,6 +349,208 @@ async function decodeSpan(r: Reader, version: Version): Promise { + if (!hasRanges(version)) { + const startGroup = await decodeStartGroup(r, version); + const endGroup = (await r.u62()) || undefined; + const end = endGroup !== undefined ? endGroup - 1n : undefined; + const frames = await decodeFrameBounds(r, version, startGroup, end); + return { startGroup: canonicalStartGroup(version, startGroup, frames.startFrame), endGroup: end, ...frames }; + } + + const entries = await decodeEntries(r); + if (entries.length === 0) throw new ProtocolViolation("a subscription asks for no range"); + return { ...spanOf(version, entries), entries }; +} + +/** + * The ranges `msg` carries as lite-07 entries: its decoded list, or else the one span it + * encodes as its only range. + */ +export function entriesOf(msg: Span & { ranges?: WireRange[]; entries?: RangeEntry[] }): RangeEntry[] { + if (msg.entries) return msg.entries; + if (msg.ranges) return msg.ranges.map((range) => ({ range })); + const range: WireRange = {}; + const entry: RangeEntry = { range }; + if (msg.startGroup !== undefined) { + range.start = { absolute: msg.startGroup }; + entry.from = [msg.startGroup, msg.startFrame]; + } + if (msg.endGroup !== undefined) { + entry.to = msg.endFrame === undefined ? [msg.endGroup + 1n, 0] : [msg.endGroup, msg.endFrame + 1]; + range.end = { absolute: msg.endGroup + 1n }; + } + return [entry]; +} + +/** The bytes a lite-07 range takes on the wire, whose varints count leading ones. */ +function entrySize({ range, from, to }: RangeEntry): number { + const size = (value: number | bigint) => { + const lo = split(value); + return lengthLeadingOnes(parts.hi, lo); + }; + const anchor = (anchor: WireAnchor | undefined, point: Point | undefined): number => { + if (point) return size(1) + size(point[0]) + size(point[1]); + if (anchor?.absolute !== undefined) return size(1) + size(anchor.absolute) + size(0); + if (anchor?.relative !== undefined) return size(2) + size(anchor.relative); + if (anchor?.time !== undefined) return size(3) + size(anchor.time); + return size(0); + }; + return anchor(range.start, from) + anchor(range.end, to); +} + +/** A range's anchors as a key, so equal ranges compare equal. */ +function rangeKey(range: WireRange): string { + const anchor = (anchor: WireAnchor | undefined) => { + if (anchor?.absolute !== undefined) return `a${anchor.absolute}`; + if (anchor?.relative !== undefined) return `r${anchor.relative}`; + if (anchor?.time !== undefined) return `t${anchor.time}`; + return "-"; + }; + return `${anchor(range.start)}:${anchor(range.end)}`; +} + +/** + * A subscription's live ranges by Range ID, which both ends of a Subscribe Stream keep: the + * subscriber to write each update as an edit, the publisher to resolve one. Mirrors the Rust + * `LiveRanges`. + * + * The SUBSCRIBE's ranges take Range IDs from 0 in order, and each range an update adds takes the + * next. Before lite-07 an update replaces the list, numbered afresh. + */ +export class LiveRanges { + readonly #version: Version; + #next = 0n; + // In Range ID order, since each id is the next. + readonly #live = new Map(); + // The bytes the live ranges take on the wire, held to what one SUBSCRIBE carries. + #size = 0; + /** How many ranges the SUBSCRIBE carried, whose starts SUBSCRIBE_START reports. */ + readonly initial: bigint; + + /** The ranges a SUBSCRIBE carried. */ + constructor(version: Version, ranges: readonly (RangeEntry | WireRange)[]) { + this.#version = version; + this.initial = BigInt(ranges.length); + this.#add(ranges); + } + + #add(ranges: readonly (RangeEntry | WireRange)[]) { + for (const range of ranges) { + const entry = "range" in range ? (range as RangeEntry) : { range: range as WireRange }; + // Only lite-07 numbers ranges on the wire, and only it is held to one SUBSCRIBE. + if (hasRanges(this.#version)) this.#size += entrySize(entry); + this.#live.set(this.#next++, entry); + } + } + + #remove(id: bigint): boolean { + const entry = this.#live.get(id); + if (!entry) return false; + if (hasRanges(this.#version)) this.#size -= entrySize(entry); + this.#live.delete(id); + return true; + } + + /** The live range with Range ID `id`. */ + get(id: bigint): WireRange | undefined { + return this.#live.get(id)?.range; + } + + /** Whether `id` was ever assigned. */ + assigned(id: bigint): boolean { + return id < this.#next; + } + + /** Every live Range ID, in order. */ + ids(): bigint[] { + return [...this.#live.keys()]; + } + + /** Every live range, in Range ID order. */ + ranges(): WireRange[] { + return [...this.#live.values()].map((entry) => entry.range); + } + + /** Every live range with its Range ID, in order. */ + entries(): [bigint, WireRange][] { + return [...this.#live].map(([id, entry]) => [id, entry.range]); + } + + /** Whether the publisher reports where each range starts, which lite-07 does by Range ID. */ + get reportsStarts(): boolean { + return hasRanges(this.#version); + } + + /** + * Apply a received lite-07 `update`'s edit, returning every live range by Range ID. Throws + * {@link ProtocolViolation} for an edit it cannot apply. Before lite-07 an update restates + * its ranges, so nothing is numbered. + */ + apply(update: SubscribeUpdate): { id: bigint; entry: RangeEntry }[] { + if (!hasRanges(this.#version)) return []; + for (const id of update.remove ?? []) { + // An id never assigned, or already retired (including earlier in this list). + if (!this.#remove(id)) throw new ProtocolViolation(`unknown range id: ${id}`); + } + this.#add(update.entries ?? update.ranges ?? []); + // Asking for nothing is refused like a SUBSCRIBE that does, and a set no SUBSCRIBE + // could carry like one too large to send. + if (this.#live.size === 0) throw new ProtocolViolation("a subscription asks for no range"); + if (this.#size > Message.MAX_MESSAGE_SIZE) throw new ProtocolViolation("too many ranges"); + return [...this.#live].map(([id, entry]) => ({ id, entry })); + } + + /** + * The edit that turns the live ranges into `ranges`, which become the live ones: the Range IDs + * to retire and the ranges to add. A range already live keeps its Range ID, so an update that + * changes one range sends only that one, and the publisher keeps where the rest resolved. + * Before lite-07 replaced ranges take fresh Range IDs. + */ + edit(ranges: readonly WireRange[]): { remove: bigint[]; add: WireRange[] } { + if (ranges.length === 0) throw new Error(EMPTY_RANGE); + if (!hasRanges(this.#version)) { + const same = + ranges.length === this.#live.size && + this.ranges().every((range, index) => rangeKey(range) === rangeKey(ranges[index])); + if (!same) { + this.#live.clear(); + this.#add(ranges); + } + return { remove: [], add: [...ranges] }; + } + // Each live range keeps at most one wanted copy, so duplicates edit like any other. + const wanted = new Map(); + for (const range of ranges) wanted.set(rangeKey(range), (wanted.get(rangeKey(range)) ?? 0) + 1); + const remove: bigint[] = []; + for (const [id, entry] of this.#live) { + const key = rangeKey(entry.range); + const count = wanted.get(key) ?? 0; + if (count > 0) wanted.set(key, count - 1); + else remove.push(id); + } + // In the order asked for, so the publisher numbers them the way this side does. + const add = ranges.filter((range) => { + const key = rangeKey(range); + const count = wanted.get(key) ?? 0; + if (count === 0) return false; + wanted.set(key, count - 1); + return true; + }); + for (const id of remove) this.#remove(id); + this.#add(add); + if (this.#size > Message.MAX_MESSAGE_SIZE) throw new Error("too many ranges for one SUBSCRIBE"); + return { remove, add }; + } +} + /** Step over the retired `Ordered` byte on a version whose layout still has it. */ async function skipGroupOrder(r: Reader, version: Version) { if (hasGroupOrder(version)) await r.bool(); @@ -428,8 +642,15 @@ export class SubscribeUpdate { startFrame: number; /** See {@link Subscribe.endFrame}. */ endFrame?: number; - /** See {@link Subscribe.ranges}. */ + /** + * See {@link Subscribe.ranges}. On lite-07 the wire carries the ranges to add, after retiring + * {@link remove}; {@link LiveRanges.apply} resolves a received update to every live range. + */ ranges?: WireRange[]; + /** Lite-07: the Range IDs of the live ranges to retire before adding {@link ranges}. */ + remove?: bigint[]; + /** Lite-07: the ranges a received update adds, with the positions they name; see {@link RangeEntry}. */ + entries?: RangeEntry[]; constructor(props: { priority: number; @@ -442,6 +663,8 @@ export class SubscribeUpdate { startFrame?: number; endFrame?: number; ranges?: WireRange[]; + remove?: bigint[]; + entries?: RangeEntry[]; }) { if (props.wantedGroup !== undefined && props.wantedGroup < 0n) throw new RangeError("wanted group must be non-negative"); @@ -455,6 +678,8 @@ export class SubscribeUpdate { this.startFrame = props.startFrame ?? 0; this.endFrame = props.endFrame; this.ranges = props.ranges; + this.remove = props.remove; + this.entries = props.entries; } async #encode(w: Writer, version: Version) { @@ -468,7 +693,26 @@ export class SubscribeUpdate { await encodeOrder(w, version, this.order); await w.u53(this.maxDelay); await encodeMinDelay(w, version, this.minDelay); - await encodeSpan(w, version, this, this.ranges); + if (hasRanges(version)) { + // An edit of the live ranges, which may add none: `ranges`, or else the span as + // the one range added once `remove` retires the rest. + if (this.ranges === undefined && this.remove === undefined) + throw new Error("a lite-07 update edits its ranges"); + const remove = this.remove ?? []; + await w.u53(remove.length); + for (const id of remove) await w.u62(id); + if (this.ranges === undefined) { + await encodeSpan(w, version, this); + } else { + await w.u53(this.ranges.length); + for (const range of this.ranges) { + await encodeAnchor(w, range.start); + await encodeAnchor(w, range.end); + } + } + } else { + await encodeSpan(w, version, this, this.ranges); + } if (hasRefresh(version)) await w.u62(this.wantedGroup === undefined ? 0n : this.wantedGroup + 1n); break; } @@ -484,7 +728,16 @@ export class SubscribeUpdate { const order = await decodeOrder(r, version); const maxDelay = await r.u53(); const minDelay = await decodeMinDelay(r, version); - const span = await decodeSpan(r, version); + let span: Span & { ranges?: WireRange[]; remove?: bigint[]; entries?: RangeEntry[] }; + if (hasRanges(version)) { + // Not preallocated, like the ranges: each id costs a byte of the bounded message. + const remove: bigint[] = []; + for (let count = await r.u53(); count > 0; count--) remove.push(await r.u62()); + const entries = await decodeEntries(r); + span = { startFrame: 0, remove, entries, ranges: entries.map((entry) => entry.range) }; + } else { + span = await decodeSpan(r, version); + } const wanted = hasRefresh(version) ? await r.u62() : 0n; return new SubscribeUpdate({ wantedGroup: wanted === 0n ? undefined : wanted - 1n, @@ -554,6 +807,8 @@ export class Subscribe { * the span alone; decoding a lite-07 list fills them alongside the span covering them all. */ ranges?: WireRange[]; + /** Lite-07: the decoded ranges with the positions they name; see {@link RangeEntry}. */ + entries?: RangeEntry[]; constructor(props: { id: bigint; @@ -569,6 +824,7 @@ export class Subscribe { startFrame?: number; endFrame?: number; ranges?: WireRange[]; + entries?: RangeEntry[]; }) { this.id = props.id; this.broadcast = props.broadcast; @@ -583,6 +839,7 @@ export class Subscribe { this.startFrame = props.startFrame ?? 0; this.endFrame = props.endFrame; this.ranges = props.ranges; + this.entries = props.entries; } async #encode(w: Writer, version: Version) { @@ -649,7 +906,7 @@ export class Subscribe { * Publisher's acknowledgement on the Subscribe Stream for drafts 01-04. * * Draft-05+ replaced this with implicit acceptance plus {@link SubscribeStart} / - * {@link SubscribeEnd}; the immutable codec/timescale/cache moved to TRACK_INFO. + * {@link SubscribeFinish}; the immutable codec/timescale/cache moved to TRACK_INFO. */ export class SubscribeOk { priority: number; @@ -798,10 +1055,11 @@ export class SubscribeStart { } /** - * Signals that no group at or after `group` (exclusive upper bound) will be produced - * on a Draft-05+ subscription. `0` means the track ended before producing any groups. + * SUBSCRIBE_FINISH (SUBSCRIBE_END before lite-07): no group at or after `group` (exclusive + * upper bound) will be produced on a Draft-05+ subscription. `0` means the track ended before + * producing any groups. */ -export class SubscribeEnd { +export class SubscribeFinish { /** The exclusive final group sequence: the first sequence that will never be produced. */ group: bigint; /** The stream error code the track ended with, if it did not end cleanly. Draft-07+ only. */ @@ -829,12 +1087,40 @@ export class SubscribeEnd { }); } - static async decode(r: Reader, version: Version): Promise { + static async decode(r: Reader, version: Version): Promise { return Message.decode(r, async (r) => { const group = await r.u62(); - if (!hasEndError(version)) return new SubscribeEnd(group); + if (!hasEndError(version)) return new SubscribeFinish(group); const code = await r.u53(); - return new SubscribeEnd(group, code === 0 ? undefined : code - 1); + return new SubscribeFinish(group, code === 0 ? undefined : code - 1); + }); + } +} + +/** SUBSCRIBE_STARTED: where a range a SUBSCRIBE_UPDATE added starts. Lite-07+. */ +export class SubscribeStarted { + /** The range's Range ID. */ + id: bigint; + /** The group its delivery starts at, `undefined` for a range with no group there. */ + start?: bigint; + + constructor(id: bigint, start?: bigint) { + this.id = id; + this.start = start; + } + + async encode(w: Writer): Promise { + return Message.encode(w, async (w) => { + await w.u62(this.id); + await w.u62(this.start === undefined ? 0n : this.start + 1n); + }); + } + + static async decode(r: Reader): Promise { + return Message.decode(r, async (r) => { + const id = await r.u62(); + const start = await r.u62(); + return new SubscribeStarted(id, start === 0n ? undefined : start - 1n); }); } } @@ -900,14 +1186,17 @@ export class SubscribeDrop { * * The discriminator is version-dependent: * - Draft-03/04: `0x0` SUBSCRIBE_OK, `0x1` SUBSCRIBE_DROP. - * - Draft-05+: `0x0` SUBSCRIBE_START, `0x1` SUBSCRIBE_END, `0x2` SUBSCRIBE_DROP - * (SUBSCRIBE_OK was removed; acceptance is implicit). + * - Draft-05+: `0x0` SUBSCRIBE_START (SUBSCRIBE_OK on lite-07), `0x1` SUBSCRIBE_FINISH + * (SUBSCRIBE_END before lite-07), `0x2` SUBSCRIBE_DROP (SUBSCRIBE_OK was removed; + * acceptance is implicit). + * - Lite-07+: also `0x3` SUBSCRIBE_STARTED. */ export type SubscribeResponse = | { ok: SubscribeOk } | { start: SubscribeStart } - | { end: SubscribeEnd } - | { drop: SubscribeDrop }; + | { finish: SubscribeFinish } + | { drop: SubscribeDrop } + | { started: SubscribeStarted }; export async function encodeSubscribeResponse(w: Writer, resp: SubscribeResponse, version: Version): Promise { switch (version) { @@ -928,7 +1217,7 @@ export async function encodeSubscribeResponse(w: Writer, resp: SubscribeResponse await w.u53(0x1); await resp.drop.encode(w); } else { - throw new Error("SUBSCRIBE_START/END not supported for this version"); + throw new Error("SUBSCRIBE_START/FINISH not supported for this version"); } break; default: @@ -936,12 +1225,15 @@ export async function encodeSubscribeResponse(w: Writer, resp: SubscribeResponse if ("start" in resp) { await w.u53(0x0); await resp.start.encode(w, version); - } else if ("end" in resp) { + } else if ("finish" in resp) { await w.u53(0x1); - await resp.end.encode(w, version); + await resp.finish.encode(w, version); } else if ("drop" in resp) { await w.u53(0x2); await resp.drop.encode(w); + } else if ("started" in resp && hasRanges(version)) { + await w.u53(0x3); + await resp.started.encode(w); } else { throw new Error("subscribe response not supported for this version"); } @@ -972,9 +1264,12 @@ export async function decodeSubscribeResponse(r: Reader, version: Version): Prom case 0x0: return { start: await SubscribeStart.decode(r, version) }; case 0x1: - return { end: await SubscribeEnd.decode(r, version) }; + return { finish: await SubscribeFinish.decode(r, version) }; case 0x2: return { drop: await SubscribeDrop.decode(r) }; + case 0x3: + if (!hasRanges(version)) throw new ProtocolViolation(`unknown subscribe response type: ${typ}`); + return { started: await SubscribeStarted.decode(r) }; default: throw new ProtocolViolation(`unknown subscribe response type: ${typ}`); } diff --git a/js/net/src/lite/subscriber.test.ts b/js/net/src/lite/subscriber.test.ts index 98d57de162d..1f9fbe2d763 100644 --- a/js/net/src/lite/subscriber.test.ts +++ b/js/net/src/lite/subscriber.test.ts @@ -24,7 +24,7 @@ import { GRACE } from "../ledger.ts"; import * as Path from "../path.ts"; import { Reader, Writer } from "../stream.ts"; import { Timescale } from "../time.ts"; -import type { Producer, Subscriber as TrackSubscriber } from "../track.ts"; +import type { Producer } from "../track.ts"; import { SEGMENT_BYTES } from "../util/limits.ts"; import { Budget } from "../util/yield.ts"; import * as Varint from "../varint.ts"; @@ -37,8 +37,7 @@ import { StreamId } from "./stream.ts"; import { encodeSubscribeResponse, Subscribe, - SubscribeDrop, - SubscribeEnd, + SubscribeFinish, SubscribeOk, type SubscribeResponse, SubscribeStart, @@ -1876,7 +1875,7 @@ test.each([ await respond( subscribe, version, - [{ start: new SubscribeStart([0n]) }, { end: new SubscribeEnd(0n) }], + [{ start: new SubscribeStart([0n]) }, { finish: new SubscribeFinish(0n) }], true, ); expect(await track.closed).toBeNull(); @@ -2203,7 +2202,7 @@ function drainReactions(): Promise { }); } -test("a local unsubscribe owes no SUBSCRIBE_END", async () => { +test("a local unsubscribe owes no SUBSCRIBE_FINISH", async () => { const { quic, streams, opened } = fakeSession(); const subscriber = new Subscriber(quic, Version.DRAFT_07, HopSchema.parse(1n)); const responseTask = Promise.withResolvers(); @@ -2452,7 +2451,7 @@ test("a lite-07 rewind is refused", async () => { } }); -test("a publisher FIN without SUBSCRIBE_END still violates an open subscription", async () => { +test("a publisher FIN without SUBSCRIBE_FINISH still violates an open subscription", async () => { const { quic, streams, opened, closes } = fakeSession(); const subscriber = new Subscriber(quic, Version.DRAFT_07, HopSchema.parse(1n)); const broadcast = subscriber.consume(Path.from("room")); @@ -2463,7 +2462,7 @@ test("a publisher FIN without SUBSCRIBE_END still violates an open subscription" await (await opened(1)).reading; streams[1].inbound.close(); expect(await track.closed).toBeInstanceOf(ProtocolViolation); - expect((track.closed.peek() as Error).message).toBe("subscribe stream ended without SUBSCRIBE_END"); + expect((track.closed.peek() as Error).message).toBe("subscribe stream ended without SUBSCRIBE_FINISH"); await drainReactions(); expect(closes.map((info) => info?.closeCode)).toEqual([SessionCode.ProtocolViolation]); } finally { @@ -2519,98 +2518,6 @@ test("an empty START remains resolved when its range widens", async () => { } }); -// A lite-07 publisher FINs a bounded subscription without SUBSCRIBE_END once it covered the range, -// which is no protocol violation. Covering a range is not ending the track, so the copy stays for -// its readers, as the Rust copy does: they park, and widening the range asks again on the same -// track. Decided 2026-10-31 (maintainer). -test("a lite-07 publisher FIN keeps a covered bounded subscription's copy", async () => { - const version = Version.DRAFT_07; - const { quic, streams, opened, closes } = fakeSession(); - const subscriber = new Subscriber(quic, version, HopSchema.parse(1n)); - const broadcast = subscriber.consume(Path.from("room")); - const track = broadcast.track("video").subscribe({ ranges: [{ start: { absolute: 0n }, end: { absolute: 2n } }] }); - try { - await opened(0); - await answerTrackInfo(streams[0], version); - const stream = await opened(1); - await stream.reading; - const chunks: Uint8Array[] = []; - const writer = new Writer( - new WritableStream({ write: (chunk) => void chunks.push(new Uint8Array(chunk)) }), - version, - ); - await encodeSubscribeResponse(writer, { start: new SubscribeStart([0n]) }, version); - await encodeSubscribeResponse( - writer, - { drop: new SubscribeDrop({ start: 0n, end: 1n, error: StreamCode.NotFound }) }, - version, - ); - for (const chunk of chunks) stream.inbound.enqueue(chunk); - stream.inbound.close(); - await drainReactions(); - expect(track.closed.peek()).toBeUndefined(); - expect(streams).toHaveLength(2); - - track.update({ ranges: [{ start: { absolute: 0n }, end: { absolute: 3n } }] }); - const reopened = await opened(2); - await reopened.reading; - const reader = new Reader(undefined, new Uint8Array(Buffer.concat(reopened.written)), version); - expect(await reader.u53()).toBe(StreamId.Subscribe); - const msg = await Subscribe.decode(reader, version); - expect(msg.ranges).toEqual([{ start: { absolute: 0n }, end: { absolute: 3n } }]); - expect(track.closed.peek()).toBeUndefined(); - expect(closes).toEqual([]); - } finally { - track.close(); - broadcast.close(); - subscriber.close(); - } -}); - -// A widening reopens a covered copy, and its readers leaving while the publisher never takes the -// new SUBSCRIBE gives the setup up: its stream is reset and the copy released, not held forever. -test("a reopened lite-07 subscription ends once its readers leave", async () => { - const version = Version.DRAFT_07; - const { quic, streams, opened } = fakeSession([], [2]); - const subscriber = new Subscriber(quic, version, HopSchema.parse(1n)); - const broadcast = subscriber.consume(Path.from("room")); - const track = broadcast.track("video").subscribe({ ranges: [{ start: { absolute: 0n }, end: { absolute: 2n } }] }); - let next: TrackSubscriber | undefined; - try { - await opened(0); - await answerTrackInfo(streams[0], version); - const stream = await opened(1); - await stream.reading; - const chunks: Uint8Array[] = []; - const writer = new Writer( - new WritableStream({ write: (chunk) => void chunks.push(new Uint8Array(chunk)) }), - version, - ); - await encodeSubscribeResponse(writer, { start: new SubscribeStart([0n]) }, version); - await encodeSubscribeResponse( - writer, - { drop: new SubscribeDrop({ start: 0n, end: 1n, error: StreamCode.NotFound }) }, - version, - ); - for (const chunk of chunks) stream.inbound.enqueue(chunk); - stream.inbound.close(); - await drainReactions(); - - track.update({ ranges: [{ start: { absolute: 0n }, end: { absolute: 3n } }] }); - await (await opened(2)).writing; - track.close(); - await drainReactions(); - // A reader arriving now asks afresh, rather than join a copy held by the stalled setup. - next = broadcast.track("video").subscribe(); - await opened(3); - } finally { - next?.close(); - track.close(); - broadcast.close(); - subscriber.close(); - } -}); - // Before lite-05 the publisher FINs the subscribe stream only once the track is over, so that FIN // finishes the track once the tail settles, rather than releasing it for another route. test.each( @@ -2649,7 +2556,7 @@ test.each( // A session that dies once the publisher ended the track, but before the tail settled, cuts it // off: the track ends with the session's error unless the declared end settled first. That end -// is SUBSCRIBE_END on lite-05+, and the FIN before it. +// is SUBSCRIBE_FINISH on lite-05+, and the FIN before it. test.each([ ["lite-03 ends clean with its last group finished", Version.DRAFT_03, true], ["lite-03 aborts with its last group still open", Version.DRAFT_03, false], @@ -2672,7 +2579,7 @@ test.each([ await subscribe.reading; const responses: SubscribeResponse[] = legacy ? [{ ok: new SubscribeOk({}) }] - : [{ start: new SubscribeStart([0n]) }, { end: new SubscribeEnd(1n) }]; + : [{ start: new SubscribeStart([0n]) }, { finish: new SubscribeFinish(1n) }]; await respond(subscribe, version, responses, true); if (settled) { const group = new ReadableStream({ @@ -2702,74 +2609,6 @@ test.each([ } }); -// The FIN may answer the bounded range before the publisher read an update that widened it, so -// the copy asks again for the demand rather than end clean short of it or fail its readers. -test.each([ - ["an open widening after SUBSCRIBE_START", { start: { absolute: 0n } }, true, false], - ["a bounded widening without SUBSCRIBE_START", { start: { absolute: 0n }, end: { absolute: 5n } }, false, false], - ["a widening the publisher stopped reading", { start: { absolute: 0n }, end: { absolute: 5n } }, false, true], -] as const)("a lite-07 FIN short of %s subscribes again on the same track", async (_, range, start, stopped) => { - const clock = new Manual(); - const installed = clock.install(); - const version = Version.DRAFT_07; - const { quic, streams, opened, closes } = fakeSession(); - const subscriber = new Subscriber(quic, version, HopSchema.parse(1n)); - const broadcast = subscriber.consume(Path.from("room")); - const track = broadcast - .track("video") - .subscribe({ delay: { max: Milli.zero }, ranges: [{ start: { absolute: 0n }, end: { absolute: 1n } }] }); - try { - await opened(0); - await answerTrackInfo(streams[0], version); - const stream = await opened(1); - await stream.reading; - const refused = stopped - ? stream.stop( - Object.assign(new Error("The stream was aborted by the remote server"), { - source: "stream", - streamErrorCode: StreamCode.Cancel, - }), - ) - : undefined; - track.update({ delay: { max: Milli.zero }, ranges: [range] }); - await refused; - await drainReactions(); - - const chunks: Uint8Array[] = []; - const writer = new Writer( - new WritableStream({ write: (chunk) => void chunks.push(new Uint8Array(chunk)) }), - version, - ); - if (start) { - await encodeSubscribeResponse(writer, { start: new SubscribeStart([0n]) }, version); - await encodeSubscribeResponse( - writer, - { drop: new SubscribeDrop({ start: 0n, end: 0n, error: StreamCode.NotFound }) }, - version, - ); - } - for (const chunk of chunks) stream.inbound.enqueue(chunk); - stream.inbound.close(); - await drainReactions(); - - await clock.advance(Nano.fromMilli(GRACE)); - const reopened = await opened(2); - await reopened.reading; - const reader = new Reader(undefined, new Uint8Array(Buffer.concat(reopened.written)), version); - expect(await reader.u53()).toBe(StreamId.Subscribe); - const msg = await Subscribe.decode(reader, version); - expect(msg.id).toBe(1n); - expect(msg.ranges).toEqual([range]); - expect(track.closed.peek()).toBeUndefined(); - expect(closes).toEqual([]); - } finally { - track.close(); - broadcast.close(); - subscriber.close(); - installed.close(); - } -}); - // A publisher that finishes the track stops reading its subscribe stream, so a SUBSCRIBE_UPDATE // written then fails. The track still ends with the publisher's finish; a reset still fails it. test.each([ @@ -2804,7 +2643,7 @@ test.each([ new WritableStream({ write: (chunk) => void chunks.push(new Uint8Array(chunk)) }), version, ); - await encodeSubscribeResponse(writer, { end: new SubscribeEnd(0n) }, version); + await encodeSubscribeResponse(writer, { finish: new SubscribeFinish(0n) }, version); for (const chunk of chunks) stream.inbound.enqueue(chunk); stream.inbound.close(); expect(await track.closed).toBeNull(); @@ -2853,7 +2692,7 @@ test("an unknown subscribe response type closes the session", async () => { await answerTrackInfo(streams[0], Version.DRAFT_07); await (await opened(1)).reading; // An unassigned response type is a session protocol violation. - streams[1].inbound.enqueue(new Uint8Array([3, 0])); + streams[1].inbound.enqueue(new Uint8Array([4, 0])); expect(await track.closed).toBeInstanceOf(ProtocolViolation); await drainReactions(); expect(closes.map((info) => info?.closeCode)).toEqual([SessionCode.ProtocolViolation]); diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index 74fabdc0a5c..19a17d556fb 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -37,7 +37,7 @@ import { abandoned, isAbandoned, unwanted } from "../util/demand.ts"; import { LIMITS, type Limits, SlotMap, Slots } from "../util/limits.ts"; import { min, UNBOUNDED } from "../util/sequence.ts"; import { Budget } from "../util/yield.ts"; -import { bounded, hasTime, rangesStart, sameRanges, Window, wireRanges, wireSpan } from "../window.ts"; +import { hasTime, rangesStart, sameRanges, Window, wireRanges, wireSpan } from "../window.ts"; import { overrideBroadcastWire, wireOf } from "../wire.ts"; import { AnnounceHistory, @@ -58,9 +58,12 @@ import { decodeSubscribeResponseMaybe, EMPTY_RANGE, inclusiveGroupEnd, + LiveRanges, + requestedRanges, Subscribe, SubscribeUpdate, toWireRanges, + type WireRange, } from "./subscribe.ts"; import { TrackInfo, Track as TrackMessage } from "./track.ts"; import { decodeTracksEventMaybe, Tracks as TracksMessage } from "./tracks.ts"; @@ -139,19 +142,21 @@ interface SubscribeEntry { // The first group the SUBSCRIBE asked for, if any. requested?: bigint; // The first group the publisher serves (SUBSCRIBE_START) and the track's exclusive end - // (SUBSCRIBE_END), once it declares them. + // (SUBSCRIBE_FINISH), once it declares them. start?: bigint; end?: bigint; - // Where SUBSCRIBE_START says each of `asked` starts on lite-07, undefined for one with - // nothing to deliver. It answers `asked` unless `updated`. - starts?: (bigint | undefined)[]; + // The ranges on the wire by Range ID (lite-07), which each update edits. + live: LiveRanges; + // Where SUBSCRIBE_START or SUBSCRIBE_STARTED says each live range starts on lite-07, by Range + // ID, undefined for one with nothing to deliver. + starts: Map; // Whether a SUBSCRIBE_UPDATE went out, which the publisher may have read before answering. updated?: boolean; // Whether SUBSCRIBE_START arrived, which `start` alone cannot say when no range starts. started?: boolean; // Whether our side is FINned, which lite-07 does once the tail settles past SUBSCRIBE_FINISH. fin?: boolean; - // How many SUBSCRIBE_UPDATEs went out, so a tail settling past SUBSCRIBE_END waits a fresh + // How many SUBSCRIBE_UPDATEs went out, so a tail settling past SUBSCRIBE_FINISH waits a fresh // grace for what one sent meanwhile asks for. sent?: number; // Set by the subscribe stream's FIN before lite-05, the track's only end there. @@ -159,11 +164,7 @@ interface SubscribeEntry { // Settles once the subscribe stream's responses are read to its FIN or reset, which // Subscriber.drain waits on. responses?: Promise; - // Whether the stream ever asked for a bounded range, which a lite-07 publisher FINs without - // SUBSCRIBE_END once it covers it. - bounded: boolean; - // The ranges the SUBSCRIBE asked for, as `wireRanges` gives them, which that FIN covers - // unless the demand moved since in an update the publisher may not have read. + // The ranges the SUBSCRIBE asked for, as `wireRanges` gives them. asked: track.Range[]; // The TRACK stream that answered (lite-05+), held open as interest in the track until // the SUBSCRIBE has its first response, so the publisher's demand never lapses between @@ -1051,74 +1052,8 @@ export class Subscriber { } valid?.removeEventListener("abort", retire); - let { stream, entry } = opened; - let current = { id, msg }; - // A lite-07 publisher that FINs a covered bounded subscription leaves the copy to its - // readers. If the demand moved past what it covered, ask again on the same track, as the - // Rust copy does, rather than fail the readers still waiting on it. - while (await this.#serveStream(current.id, broadcast, consumed, state, stream, entry, current.msg)) { - const producer = entry.track; - const subscription = producer.subscription.peek(); - const ranges = wireRanges(subscription?.ranges ?? [{}]); - if (!subscription || ranges.length === 0) { - hooks.releaseTrack(producer); - return; - } - const id = this.#subscribeNext++; - const msg = this.#subscribeMessage(id, broadcast, consumed, producer.name, subscription, ranges); - console.debug(`subscribe reopen: id=${id} broadcast=${broadcast} track=${producer.name}`); - // Bounded like the first setup, and given up once nobody wants the track, so a peer - // that stops reading holds neither the stream nor its entry. - // Watched only while the setup runs, so a reopen leaves no watcher behind. - const left = new AbortController(); - const demand = producer.demand(); - let unwatch: Dispose | undefined; - const watch = () => { - if (isAbandoned(demand)) left.abort(new StreamError(StreamCode.Cancel, { message: "cancel" })); - else unwatch = Signal.race(demand.wanted, hooks.demandReleased(demand)).changed(watch); - }; - watch(); - let sent: Promise | undefined; - valid?.addEventListener("abort", retire); - try { - if (valid?.aborted) retire(); - state.cancel.signal.throwIfAborted(); - const framing = { compress: entry.compress, timescale: entry.timescale, scale: entry.scale }; - entry = this.#register( - id, - this.#withRanges(msg, ranges, entry.scale), - ranges, - producer, - framing, - state, - ); - const deadline = new Deadline(); - sent = this.#sendSubscribe(state, msg, deadline); - stream = await deadline.wait(untilAborted(untilAborted(sent, valid), left.signal)); - current = { id, msg }; - } catch (err) { - const e = - state.revoked ?? (left.signal.aborted ? left.signal.reason : await sessionCause(this.#quic, err)); - state.cancel.abort(e); - this.#subscribes.delete(id); - // A stream that opens late is reset too, as the first setup's are. - const close = () => state.stream?.abort(e); - close(); - sent?.then(close, close); - if (left.signal.aborted && !state.revoked) { - hooks.releaseTrack(producer); - return; - } - producer.abort(e); - console.warn( - `subscribe error: id=${id} broadcast=${broadcast} track=${producer.name} error=${reason(e)}`, - ); - return; - } finally { - unwatch?.(); - valid?.removeEventListener("abort", retire); - } - } + const { stream, entry } = opened; + await this.#serveStream(id, broadcast, consumed, state, stream, entry, msg); } // The SUBSCRIBE for `subscription`, asking for `ranges`, its wire ranges in the model. Its own @@ -1155,9 +1090,7 @@ export class Subscriber { }; } - // Serve one SUBSCRIBE stream into its track until it ends. Resolves true when a lite-07 - // publisher covered the bounded ranges it read but an update since moved the demand past - // them, leaving the track open for the caller to ask again. + // Serve one SUBSCRIBE stream into its track until it ends. async #serveStream( id: bigint, broadcast: Path.Valid, @@ -1166,7 +1099,7 @@ export class Subscriber { stream: Stream, entry: SubscribeEntry, msg: Subscribe, - ): Promise { + ): Promise { const { answering } = consumed; const producer = entry.track; // Sent but unanswered (lite-05+; older drafts already waited for SUBSCRIBE_OK), only a @@ -1188,10 +1121,8 @@ export class Subscriber { const responses = this.runResponses(stream, entry); entry.responses = responses.catch(() => {}); let tailSettled = false; - // Set when the publisher ended the subscription short of the demand. - let short = false; const closed = responses.then(async () => { - short = !(await this.#settleTail(entry)); + await this.#settleTail(entry); tailSettled = true; }); // A reset that lands after the race below settled is moot; the race observes one before. @@ -1234,26 +1165,15 @@ export class Subscriber { else if (!entry.fin) stream.close(); console.debug(`subscribe close: id=${id} broadcast=${broadcast} track=${producer.name}`); - // A publisher that covered the range before reading an update widening it sent fewer - // groups than the demand asks for, so the caller asks again for the rest. A lite-07 - // FIN without SUBSCRIBE_END that covered the demand leaves the copy to its readers - // until the demand moves past it. Otherwise only a received SUBSCRIBE_END, or before - // lite-05 a FIN, declares the track's end: a subscription nobody wants anymore is a - // released copy, not finished content. - const live = reason_ !== idle && hooks.trackReleased(producer).peek() === undefined; - const covered = live && tailSettled && entry.end === undefined && hasRanges(this.version); - const reopen = live && (short || (covered && (await this.#parkCovered(entry)))); - if (!reopen) { - if (tailSettled && entry.finished) producer.finish(); - hooks.releaseTrack(producer); - } - return reopen; + // Only a received SUBSCRIBE_FINISH, or before lite-05 a FIN, declares the track's end: a + // subscription nobody wants anymore is a released copy, not finished content. + if (tailSettled && entry.finished) producer.finish(); + hooks.releaseTrack(producer); } catch (err) { const e = await sessionCause(this.#quic, err); producer.abort(e); console.warn(`subscribe error: id=${id} broadcast=${broadcast} track=${producer.name} error=${reason(e)}`); stream.abort(e); - return false; } finally { answering?.removeEventListener("abort", replaced); entry.held?.close(); @@ -1261,21 +1181,6 @@ export class Subscriber { } } - // Keep a copy whose lite-07 publisher covered its bounded ranges, as the Rust copy does: its - // readers park rather than end, the cache answers the same ranges, and an update can still - // widen them. Resolves true once the demand asks for groups it lacks, to ask again on the - // same track, and false once nobody wants the track or it closed. - async #parkCovered(entry: SubscribeEntry): Promise { - const producer = entry.track; - const demand = producer.demand(); - const released = hooks.trackReleased(producer); - for (;;) { - if (released.peek() !== undefined || isAbandoned(demand)) return false; - if (!this.#demandCovered(entry)) return true; - await race([Signal.race(producer.subscription, demand.wanted, hooks.demandReleased(demand)), released]); - } - } - // Open the subscribe stream and accept the request (so the application's track.Subscriber // resolves and incoming groups have a producer to write into) with the track's immutable // properties. `state.stream` is populated as soon as it opens, and `state.track` once @@ -1406,8 +1311,9 @@ export class Subscriber { grace: () => hooks.tailGrace(producer), }), requested: msg.startGroup, - bounded: hasRanges(this.version) && bounded(ranges), asked: [...ranges], + live: new LiveRanges(this.version, msg.ranges ?? []), + starts: new Map(), }; this.#subscribes.set(id, entry); state.producer = producer; @@ -1720,9 +1626,9 @@ export class Subscriber { // accounted against: SUBSCRIBE_START/END/DROP on lite-05+, and any SUBSCRIBE_DROP after the // OK before then, where the FIN itself ends the track. Reading to the FIN, rather than // awaiting it, matters: a pull-driven transport stream closes only on a read. - // SUBSCRIBE_END declares the track's end right away, so a consumer learns it before the + // SUBSCRIBE_FINISH declares the track's end right away, so a consumer learns it before the // last groups arrive. The publisher must declare the end before FIN unless we cancelled - // locally, or on lite-07 it covered a bounded range; resets and malformed responses reject. + // locally; resets and malformed responses reject. private async runResponses(stream: Stream, entry: SubscribeEntry): Promise { for (;;) { const resp = await decodeSubscribeResponseMaybe(stream.reader, this.version); @@ -1737,22 +1643,25 @@ export class Subscriber { entry.finished = true; return; } - // A bounded range may have widened since, in an update the publisher never read. - const covered = hasRanges(this.version) && entry.bounded; - if (entry.end === undefined && !covered && hooks.trackReleased(entry.track).peek() === undefined) - throw new ProtocolViolation("subscribe stream ended without SUBSCRIBE_END"); + if (entry.end === undefined && hooks.trackReleased(entry.track).peek() === undefined) + throw new ProtocolViolation("subscribe stream ended without SUBSCRIBE_FINISH"); return; } if ("ok" in resp) throw new ProtocolViolation("duplicate SUBSCRIBE_OK"); if ("start" in resp) { - // One start per range the SUBSCRIBE carried, unless an update the publisher read - // first replaced them. - if (hasRanges(this.version) && !entry.updated && resp.start.starts.length !== entry.asked.length) - throw new ProtocolViolation( - `SUBSCRIBE_START resolves ${resp.start.starts.length} ranges, not ${entry.asked.length}`, - ); - if (hasRanges(this.version)) entry.starts = resp.start.starts; + if (hasRanges(this.version)) { + if (entry.started) throw new ProtocolViolation("duplicate SUBSCRIBE_OK"); + // One start per range the SUBSCRIBE carried, by Range ID. + if (BigInt(resp.start.starts.length) !== entry.live.initial) + throw new ProtocolViolation( + `SUBSCRIBE_OK resolves ${resp.start.starts.length} ranges, not ${entry.live.initial}`, + ); + // An update may have retired some already, whose starts say nothing now. + resp.start.starts.forEach((start, index) => { + if (entry.live.get(BigInt(index))) entry.starts.set(BigInt(index), start); + }); + } // The lowest range start is the first group served; none leaves nothing owed. entry.start = resp.start.starts.reduce( (lowest, start) => @@ -1768,27 +1677,38 @@ export class Subscriber { // demand asks later. One that still arrives is delivered. if (entry.requested !== undefined && entry.start !== undefined) entry.tail.account(entry.requested, entry.start); - } else if ("end" in resp) { - if (entry.end !== undefined) throw new ProtocolViolation("duplicate SUBSCRIBE_END"); - entry.end = resp.end.group; + } else if ("finish" in resp) { + if (entry.end !== undefined) throw new ProtocolViolation("duplicate SUBSCRIBE_FINISH"); + entry.end = resp.finish.group; // A local close can win the race with the response; there is nothing left to end. if (hooks.trackReleased(entry.track).peek() !== undefined) continue; try { // Lower groups may still be on the wire behind a higher one, so readers hold // at a hole below the end until the tail settles. - hooks.endTrack(entry.track, { group: entry.end, frame: 0 }, resp.end.cause()); + hooks.endTrack(entry.track, { group: entry.end, frame: 0 }, resp.finish.cause()); } catch (err) { // lite-05 specified an inclusive end, and @moq/net 0.1.3 to 0.1.9 sent one, so // there an end below a received group only costs the early boundary: the FIN // still finishes the track. Later drafts made it exclusive. if (this.version !== Version.DRAFT_05) { - throw new ProtocolViolation(`invalid SUBSCRIBE_END: ${reason(error(err))}`); + throw new ProtocolViolation(`invalid SUBSCRIBE_FINISH: ${reason(error(err))}`); } - console.warn(`invalid SUBSCRIBE_END: ${reason(error(err))}`); + console.warn(`invalid SUBSCRIBE_FINISH: ${reason(error(err))}`); } // Lite-07 keeps the stream open past SUBSCRIBE_FINISH: FIN once the tail settles, // and the publisher's FIN in reply ends this loop. if (keepsSubscribeOpen(this.version)) void this.#finishTail(stream, entry).catch(() => {}); + } else if ("started" in resp) { + // Where a range an update added starts, from which its groups are owed. + const { id, start } = resp.started; + if (!entry.started || !entry.live.assigned(id) || id < entry.live.initial || entry.starts.has(id)) + throw new ProtocolViolation(`unexpected SUBSCRIBE_STARTED: ${id}`); + // One retired since is owed nothing. + if (entry.live.get(id)) { + entry.starts.set(id, start); + // The tail judges what is owed again. + entry.tail.touch(); + } } else if ("drop" in resp) { // The publisher will never send these groups, so they are accounted for without a // stream, and a reader of one still arriving ends with the drop's error now. @@ -1798,7 +1718,7 @@ export class Subscriber { } } - // Settle a lite-07 tail past SUBSCRIBE_END, then FIN. An update sent meanwhile asks for more, + // Settle a lite-07 tail past SUBSCRIBE_FINISH, then FIN. An update sent meanwhile asks for more, // which waits a fresh grace rather than one that may have passed while a stream was read. async #finishTail(stream: Stream, entry: SubscribeEntry): Promise { for (;;) { @@ -1821,71 +1741,43 @@ export class Subscriber { // Received headers and SUBSCRIBE_DROP account for the range. An unaccounted // sequence, such as a lost datagram, still waits out the grace. Streams whose // headers arrived keep reading until their own FIN or reset. - // - // Resolves false when a lite-07 publisher's FIN without SUBSCRIBE_END may have left the - // demand uncovered: it covered the bounded range it read, and the demand has moved since. - async #settleTail(entry: SubscribeEntry): Promise { + async #settleTail(entry: SubscribeEntry): Promise { const { tail, track } = entry; - const complete = () => this.#covered(entry); - const released = hooks.trackReleased(track); - await tail.settle(complete, released); - if (entry.end !== undefined || !hasRanges(this.version) || released.peek() !== undefined) return true; - return this.#demandCovered(entry); - } - - // Whether a lite-07 FIN without SUBSCRIBE_END covered the demand: the ranges it asked for, or - // the groups that arrived account for the demand since. - #demandCovered(entry: SubscribeEntry): boolean { - const demand = wireRanges(entry.track.subscription.peek()?.ranges ?? [{}]); - if (sameRanges(demand, entry.asked)) return true; - // Without SUBSCRIBE_START nothing says which ranges the publisher read, so only the - // groups that arrived prove the demand covered. - return entry.start !== undefined && this.#covered(entry); + await tail.settle(() => this.#covered(entry), hooks.trackReleased(track)); } // Whether the groups received and dropped account for every group the demand is owed once // the publisher ended the subscription. #covered(entry: SubscribeEntry): boolean { + // Without SUBSCRIBE_FINISH (older drafts) nothing says which groups are owed. + if (entry.end === undefined) return false; const ranges = wireRanges(entry.track.subscription.peek()?.ranges ?? [{}]); - const span = wireSpan(ranges); - // Without SUBSCRIBE_END (older drafts) nothing says which groups are owed, unless a - // lite-07 publisher covered the bounded ranges. - const end = - entry.end === undefined - ? hasRanges(this.version) - ? span.end - : undefined - : min(entry.end, span.end ?? entry.end); - if (end === undefined) return false; - // Without SUBSCRIBE_START the publisher served no group at all, though lite-07 still - // answers with one once the track ends, unless nothing was asked below the end. One - // that started no range owes nothing either, unless an update since asked from a floor - // below the end, which lite-07's open stream still serves. + const end = min(entry.end, wireSpan(ranges).end ?? entry.end); const floor = rangesStart(ranges); - let first = entry.start; - if (first === undefined) { - if (!keepsSubscribeOpen(this.version)) return true; + // Lite-07 answers with SUBSCRIBE_OK once the track ends, unless nothing was asked below the + // end. Each range the publisher holds is then owed from the start SUBSCRIBE_OK or + // SUBSCRIBE_STARTED named, which accounts for a skipped prefix and resolves a relative or + // time start; one whose SUBSCRIBE_STARTED is still on its way owes what nothing says yet. + if (hasRanges(this.version)) { if (!entry.started) return !(end > (floor ?? 0n)); - if (!entry.updated || floor === undefined || floor >= end) return true; - first = floor; + const owed: track.Range[] = []; + for (const [id, range] of entry.live.entries()) { + if (!entry.starts.has(id)) return false; + const at = entry.starts.get(id); + if (at === undefined) continue; + const [model] = requestedRanges({ startFrame: 0, ranges: [range] }, entry.scale); + owed.push({ ...model, start: { absolute: at } }); + } + const window = new Window(owed, entry.track.largest()?.group); + return window.runs(0n, end).every((run) => entry.tail.covers(run.start, run.end)); } - // Owed from the floor the demand last asked for, which an update can move either - // way, or where SUBSCRIBE_START resolved a live-edge one. The groups the SUBSCRIBE - // asked for below its SUBSCRIBE_START were accounted for when it arrived. Only the - // groups inside the ranges are owed: the publisher accounts for nothing between them. - // While SUBSCRIBE_START answers these ranges, each is owed from the start it named, which - // accounts for a skipped prefix and resolves a relative or time start. Otherwise a time - // start resolved at the publisher, at the lowest start it named. - const starts = !entry.updated && sameRanges(ranges, entry.asked) && entry.starts; - const start = starts ? first : (floor ?? first); - const owed = starts - ? ranges.flatMap((range, index) => { - const at = starts[index]; - return at === undefined ? [] : [{ ...range, start: { absolute: at } }]; - }) - : ranges.map((range) => (range.start?.time ? { ...range, start: { absolute: start } } : range)); - const window = new Window(owed, entry.track.largest()?.group); - return window.runs(start, end).every((run) => entry.tail.covers(run.start, run.end)); + // Without SUBSCRIBE_START the publisher served no group at all. Otherwise each group is owed + // from the floor the demand last asked for, which an update can move either way, or where + // SUBSCRIBE_START resolved a live-edge one. The groups the SUBSCRIBE asked for below its + // SUBSCRIBE_START were accounted for when it arrived. + if (entry.start === undefined) return true; + const window = new Window(ranges, entry.track.largest()?.group); + return window.runs(floor ?? entry.start, end).every((run) => entry.tail.covers(run.start, run.end)); } /** @@ -1957,6 +1849,13 @@ export class Subscriber { if (!sameRanges(ranges, wireRanges(lastSent.ranges ?? [{}]))) entry.tail.demand(start ?? entry.start ?? 0n, UNBOUNDED); + // Lite-07 edits the live ranges: a range already live keeps its Range ID, and where it + // started. The retired ones owe nothing more. + let edit: { remove: bigint[]; add: WireRange[] } | undefined; + if (hasRanges(this.version)) { + edit = entry.live.edit(toWireRanges(ranges, entry.scale)); + for (const id of entry.starts.keys()) if (!entry.live.get(id)) entry.starts.delete(id); + } // Round-trip the other Subscribe parameters so the publisher doesn't // interpret SUBSCRIBE_UPDATE as a reset of ordered/delay/etc. const update = new SubscribeUpdate({ @@ -1967,7 +1866,8 @@ export class Subscriber { minDelay: minDelayOf(current), startGroup: span.start, endGroup: inclusiveGroupEnd(span.end), - ranges: hasRanges(this.version) ? toWireRanges(ranges, entry.scale) : undefined, + ranges: edit?.add, + remove: edit?.remove, }); // Encoded apart from the write, so a bound the wire can't carry still fails the track. const encoded = await Writer.collect(this.version, (w) => update.encode(w, this.version)); @@ -1979,10 +1879,9 @@ export class Subscriber { return; } if (refresh !== undefined) sentWanted = refresh; - entry.bounded ||= bounded(ranges); entry.updated = true; entry.sent = (entry.sent ?? 0) + 1; - // A tail settling past SUBSCRIBE_END re-judges what the update now asks for. + // A tail settling past SUBSCRIBE_FINISH re-judges what the update now asks for. entry.tail.touch(); lastSent = { ...current }; console.debug(`subscribe update: id=${id} broadcast=${broadcast} track=${track.name}`); diff --git a/js/net/src/lite/tail.test.ts b/js/net/src/lite/tail.test.ts index c13c5d0655a..c7a6f530e3b 100644 --- a/js/net/src/lite/tail.test.ts +++ b/js/net/src/lite/tail.test.ts @@ -15,13 +15,15 @@ import { encodeSubscribeResponse, Subscribe, SubscribeDrop, - SubscribeEnd, + SubscribeFinish, type SubscribeResponse, SubscribeStart, + SubscribeStarted, + SubscribeUpdate, } from "./subscribe.ts"; import { Subscriber } from "./subscriber.ts"; import { TrackInfo, Track as TrackMessage } from "./track.ts"; -import { ALPN_05, resolvesStart, Version } from "./version.ts"; +import { ALPN_05, hasRanges, resolvesStart, Version } from "./version.ts"; // The subscription's max delay, which is also how long it waits for a group that never arrives. const GRACE = Milli(100); @@ -78,6 +80,7 @@ async function subscribed(version: Version, maxDelay = GRACE, range?: Range) { subscriber, reader, respond: (resp: SubscribeResponse) => encodeSubscribeResponse(sub.writer, resp, version), + update: () => SubscribeUpdate.decode(sub.reader, version), fin: () => sub.writer.close(), subscriberFin: () => sub.reader.done(), reset: (error: Error) => sub.writer.reset(error), @@ -125,7 +128,7 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver const first = groupStream(subscriber, 0n); first.write("0.0"); first.finish(); - await respond({ end: new SubscribeEnd(2n) }); + await respond({ finish: new SubscribeFinish(2n) }); await fin(); // The end is known before the last group arrives. @@ -149,7 +152,7 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver const group = groupStream(subscriber, 0n); group.write("0.0"); - await respond({ end: new SubscribeEnd(1n) }); + await respond({ finish: new SubscribeFinish(1n) }); await fin(); const received = await reader.recvGroup(); expect(await received?.readString()).toBe("0.0"); @@ -167,7 +170,7 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver await respond({ start: new SubscribeStart([0n]) }); const group = groupStream(subscriber, 0n); group.write("0.0"); - await respond({ end: new SubscribeEnd(1n) }); + await respond({ finish: new SubscribeFinish(1n) }); await fin(); const received = await reader.recvGroup(); @@ -196,7 +199,7 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver group.write("1.0"); group.finish(); await group.handled; - await respond({ end: new SubscribeEnd(2n) }); + await respond({ finish: new SubscribeFinish(2n) }); await fin(); // Group 0 has no header, so accounting still waits, and its stream may yet @@ -228,14 +231,14 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver }); // QUIC does not order streams, so a lower group's stream can land after a higher one and - // after SUBSCRIBE_END. The reader receives it before its end, which then comes at once. - test("a lower group that lands after SUBSCRIBE_END reaches the reader", async () => { + // after SUBSCRIBE_FINISH. The reader receives it before its end, which then comes at once. + test("a lower group that lands after SUBSCRIBE_FINISH reaches the reader", async () => { const { subscriber, reader, respond, fin } = await subscribed(version, Milli(60_000)); await respond({ start: new SubscribeStart([0n]) }); const high = groupStream(subscriber, 1n); high.finish(); await high.handled; - await respond({ end: new SubscribeEnd(2n) }); + await respond({ finish: new SubscribeFinish(2n) }); expect(await reader.finished()).toEqual({ group: 2n, frame: 0 }); expect((await reader.recvGroup())?.sequence).toBe(1n); @@ -264,7 +267,7 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver const group = groupStream(subscriber, 1n); group.write("1.0"); group.finish(); - await respond({ end: new SubscribeEnd(2n) }); + await respond({ finish: new SubscribeFinish(2n) }); await fin(); expect((await reader.recvGroup())?.sequence).toBe(1n); @@ -300,9 +303,9 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver subscriber.close(); }); - test("a subscription that served nothing ends at SUBSCRIBE_END", async () => { + test("a subscription that served nothing ends at SUBSCRIBE_FINISH", async () => { const { reader, respond, fin } = await subscribed(version, Milli(60_000)); - await respond({ end: new SubscribeEnd(0n) }); + await respond({ finish: new SubscribeFinish(0n) }); await fin(); expect(await settlesWithin(reader.recvGroup(), 1000)).toBe(true); @@ -311,10 +314,10 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver }); // lite-05 specified an inclusive end, so there the group costs only its own stream. - test(`a group at or past SUBSCRIBE_END ${version === Version.DRAFT_05 ? "is dropped" : "aborts the track"}`, async () => { + test(`a group at or past SUBSCRIBE_FINISH ${version === Version.DRAFT_05 ? "is dropped" : "aborts the track"}`, async () => { const { subscriber, reader, respond } = await subscribed(version); await respond({ start: new SubscribeStart([0n]) }); - await respond({ end: new SubscribeEnd(2n) }); + await respond({ finish: new SubscribeFinish(2n) }); expect(await reader.finished()).toEqual({ group: 2n, frame: 0 }); const handled = groupStream(subscriber, 2n).handled; @@ -341,7 +344,7 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver const group = groupStream(subscriber, 3n); group.finish(); await group.handled; - await respond({ end: new SubscribeEnd(4n) }); + await respond({ finish: new SubscribeFinish(4n) }); await fin(); expect((await reader.recvGroup())?.sequence).toBe(3n); @@ -351,7 +354,9 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver test("a lowered floor owes the groups it newly asked for", async () => { const maxDelay = Milli(60_000); - const { subscriber, respond, fin } = await subscribed(version, maxDelay, { start: { absolute: 3n } }); + const { subscriber, respond, update, fin } = await subscribed(version, maxDelay, { + start: { absolute: 3n }, + }); await respond({ start: new SubscribeStart([3n]) }); const group = groupStream(subscriber, 3n); group.write("3.0"); @@ -363,7 +368,13 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver .consume(Path.from("room")) .track("video") .subscribe({ delay: { max: maxDelay }, ranges: [{ start: { absolute: 1n } }] }); - await respond({ end: new SubscribeEnd(4n) }); + // Lite-07 retires the range and adds the lowered one, whose start SUBSCRIBE_STARTED reports. + const lowered = await update(); + if (hasRanges(version)) { + expect([lowered.remove, lowered.ranges]).toEqual([[0n], [{ start: { absolute: 1n } }]]); + await respond({ started: new SubscribeStarted(1n, 1n) }); + } + await respond({ finish: new SubscribeFinish(4n) }); await fin(); // Long enough for a subscription that owed nothing more to have ended. await sleep(Nano.fromMilli(Milli(50))); @@ -389,13 +400,13 @@ describe.each([Version.DRAFT_05, Version.DRAFT_06, Version.DRAFT_07])("%s", (ver // lite-05 specified an inclusive end, and @moq/net 0.1.3 to 0.1.9 sent one, so there an end // below a received group only costs the early boundary. - test(`a SUBSCRIBE_END below a group already received ${version === Version.DRAFT_05 ? "finishes clean" : "aborts the track"}`, async () => { + test(`a SUBSCRIBE_FINISH below a group already received ${version === Version.DRAFT_05 ? "finishes clean" : "aborts the track"}`, async () => { const { subscriber, reader, respond, fin } = await subscribed(version); await respond({ start: new SubscribeStart([0n]) }); const group = groupStream(subscriber, 3n); group.finish(); await group.handled; - await respond({ end: new SubscribeEnd(2n) }); + await respond({ finish: new SubscribeFinish(2n) }); if (version === Version.DRAFT_05) { await fin(); @@ -415,7 +426,7 @@ test("lite-07 settles a skipped group with SUBSCRIBE_DROP", async () => { group.finish(); await group.handled; } - await respond({ end: new SubscribeEnd(3n) }); + await respond({ finish: new SubscribeFinish(3n) }); await respond({ drop: new SubscribeDrop({ start: 1n, end: 1n, error: 0 }) }); await fin(); @@ -428,7 +439,7 @@ test("lite-07 settles a skipped group with SUBSCRIBE_DROP", async () => { test("lite-07 settles a dropped range without grace", async () => { const { reader, respond, fin } = await subscribed(Version.DRAFT_07, Milli(60_000)); await respond({ start: new SubscribeStart([0n]) }); - await respond({ end: new SubscribeEnd(3n) }); + await respond({ finish: new SubscribeFinish(3n) }); await respond({ drop: new SubscribeDrop({ start: 0n, end: 2n, error: 0 }) }); await fin(); @@ -464,14 +475,14 @@ test("lite-07 FIN waits for the skipped and reset final range to settle", async await first.handled; const last = groupStream(subscriber, 2n); last.write("tail"); - await respond({ end: new SubscribeEnd(3n) }); + await respond({ finish: new SubscribeFinish(3n) }); await respond({ drop: new SubscribeDrop({ start: 1n, end: 1n, error: 0 }) }); await fin(); let finished = false; const ack = subscriberFin().then(() => { finished = true; }); - // Wait for SUBSCRIBE_END to be decoded without advancing any timers. + // Wait for SUBSCRIBE_FINISH to be decoded without advancing any timers. while (hooks.subscriberEnd(reader)?.group !== 3n) await Promise.resolve(); expect(finished).toBe(false); last.reset(); diff --git a/js/net/src/lite/version.ts b/js/net/src/lite/version.ts index f18b3e0c7e1..82c9c29dc0d 100644 --- a/js/net/src/lite/version.ts +++ b/js/net/src/lite/version.ts @@ -387,7 +387,7 @@ export function hasHidden(version: Version): boolean { /** * Whether a served SUBSCRIBE completes only once the subscriber FINs or resets its half of the * Subscribe Stream. A subscriber FINs once its tail accounting settles, since a transport ACK - * does not say the application read the tail. SUBSCRIBE_END says which groups are owed, so that + * does not say the application read the tail. SUBSCRIBE_FINISH says which groups are owed, so that * is prompt from lite-05; before it, only the subscriber's grace settles the tail. */ export function waitsForSubscriberFin(version: Version): boolean { @@ -591,7 +591,7 @@ export function hasDropCode(version: Version): boolean { } /** - * Whether SUBSCRIBE_END carries the error the track ended with, so an errored end keeps its + * Whether SUBSCRIBE_FINISH carries the error the track ended with, so an errored end keeps its * history across a hop. Added in lite-07; earlier versions can only reset. */ export function hasEndError(version: Version): boolean { diff --git a/js/net/src/resume.ts b/js/net/src/resume.ts index 5eeb0f98130..9abfcf4ff7f 100644 --- a/js/net/src/resume.ts +++ b/js/net/src/resume.ts @@ -73,7 +73,7 @@ import { grace } from "./ledger.ts"; import type { Lister, Tracks, TracksEvent } from "./listing.ts"; import * as track from "./track.ts"; import { max, min, UNBOUNDED } from "./util/sequence.ts"; -import { clamped, type Window } from "./window.ts"; +import { clamped, type Resolved, type Window } from "./window.ts"; import { type Broadcast as BroadcastWire, registerWire, wireOf } from "./wire.ts"; // Stream codes that report the route failing to deliver rather than judging what it carried: @@ -1171,6 +1171,13 @@ class Reader implements ResumeSource { return serving && hooks.servingStarts(serving.sub, from); } + // Each copy resolves the reader's ranges in turn, clamped to its floor and replacement. + resolved(): (Resolved | undefined)[] | undefined { + this.sync(); + const serving = this.#serving(); + return serving && hooks.servingResolved(serving.sub); + } + // The reader's ranges resolve on the copies, which hold the groups their anchors name. runs(start: bigint, end: bigint): Run[] | undefined { this.sync(); diff --git a/js/net/src/tail.test.ts b/js/net/src/tail.test.ts index b7dde35ea7a..40a81d606c1 100644 --- a/js/net/src/tail.test.ts +++ b/js/net/src/tail.test.ts @@ -57,7 +57,7 @@ async function readAll(reader: Ordered): Promise { } } -// moq-lite carries the end in SUBSCRIBE_END as soon as it is declared, so a subscriber +// moq-lite carries the end in SUBSCRIBE_FINISH as soon as it is declared, so a subscriber // learns it while the last group is still to come. test.each([Lite.ALPN_05, Lite.ALPN_06])( "%s: an end declared ahead of the live edge reaches the subscriber", diff --git a/js/net/src/track.test.ts b/js/net/src/track.test.ts index 4ee9915c6b7..031b40065f1 100644 --- a/js/net/src/track.test.ts +++ b/js/net/src/track.test.ts @@ -2806,7 +2806,7 @@ test("a closed track still delivers a group parked above the cap once the cap is }); // The final boundary counts datagrams: they share the group sequence namespace, and a -// Rust peer feeds SUBSCRIBE_END into finish_at, so a boundary that ignored a trailing +// Rust peer feeds SUBSCRIBE_FINISH into finish_at, so a boundary that ignored a trailing // datagram would finalize the track before it arrives. test("end reports the head after the highest produced sequence, datagrams included", async () => { const producer = new TrackProducer("test"); diff --git a/js/net/src/track.ts b/js/net/src/track.ts index e2b051eafdc..a4d737c56fc 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -2874,6 +2874,12 @@ export class Subscriber { subscriber.#settle(); return subscriber.#window.starts(from); }; + hooks.servingResolved = (subscriber) => { + const resumed = subscriber.#resume?.resolved(); + if (resumed) return resumed; + subscriber.#settle(); + return subscriber.#window.resolved(); + }; hooks.tryNextGroup = (subscriber, floor) => { subscriber.#raiseNext(floor); return subscriber.#tryNextGroup(); diff --git a/js/net/src/window.ts b/js/net/src/window.ts index 134bab7b8b9..e3b3857fb3c 100644 --- a/js/net/src/window.ts +++ b/js/net/src/window.ts @@ -678,11 +678,16 @@ export class Window { } /** - * Each range resolved, in the order given: undefined for one that is empty or whose start - * still waits. An unsettled time end is reported open, never at its cap. + * Each range resolved, in the order given: undefined for one whose start still waits. One + * empty as given, or that only resolves empty, ends at or below its start. An unsettled time + * end is reported open, never at its cap. Mirrors the Rust `Window::resolved`. */ resolved(): (Resolved | undefined)[] { - const resolved: (Resolved | undefined)[] = this.ranges.map(() => undefined); + const resolved: (Resolved | undefined)[] = this.ranges.map((range) => { + if (!isEmpty(range)) return undefined; + const end = absolute(range.end) ?? 0n; + return { start: (range.start && absolute(range.start)) ?? end, end }; + }); for (const span of this.#spans) { resolved[span.index] = { start: span.start, diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 3c6d70abe52..99d72ae80a0 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -3602,6 +3602,64 @@ mod tests { ); } + /// A lite-07 range an update adds owes nothing known until SUBSCRIBE_STARTED says where it + /// starts, and then owes from there; one retired since owes nothing. + #[kio::test] + #[expect( + clippy::single_range_in_vec_init, + reason = "the owed groups are a list of runs, often one" + )] + async fn a_lite07_added_range_is_owed_from_its_reported_start() { + let session = crate::lite::test_transport::ScriptedSession::new(Vec::new()); + let stream = Stream::open(&mut crate::driver::Transport::new(session.clone()), Version::Lite07) + .await + .unwrap(); + let first = lite::Range::span(Some(4), 0, None, None); + let mut sub = SubStream { + order: crate::track::Order::Descending, + stream, + id: 0, + max_delay: Duration::ZERO, + min_delay: None, + start: Some(4), + priority: 0, + requested: Some(4), + live: lite::LiveRanges::new(Version::Lite07, &[first]), + tail: Default::default(), + served: Some(4), + started: true, + starts: [(0, Some(4))].into(), + end: Some(lite::SubscribeFinish { group: 10, error: None }), + wanted: None, + unanswered: None, + _working: crate::session::Work::default().start(), + }; + assert_eq!(sub.owed(None), Some(vec![4..10])); + + // Two groups back from the latest, which only the publisher can resolve. + let back = lite::Range { + start: Some(lite::Anchor::Relative(2)), + end: None, + }; + let (edit, changed) = sub.edit(&[first, back]).unwrap(); + assert!(changed); + assert_eq!( + edit, + lite::RangeEdit::Edit { + remove: vec![], + add: vec![back] + } + ); + assert_eq!(sub.owed(None), None, "nothing says yet where it starts"); + sub.starts.insert(1, Some(1)); + assert_eq!(sub.owed(None), Some(vec![4..10, 1..10])); + + // Retiring the first range forgets its start. + sub.edit(&[back]).unwrap(); + assert_eq!(sub.starts.keys().copied().collect::>(), [1]); + assert_eq!(sub.owed(Some(8)), Some(vec![1..8])); + } + async fn receive_padding(version: Version, bytes: Vec) { let session = crate::lite::test_transport::ScriptedSession::new(bytes); let stream = Stream::open(&mut crate::driver::Transport::new(session.clone()), version) From 6b2e1d353553677500f348dc4ef8efaa5148ff62 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 4 Nov 2026 07:04:01 -0800 Subject: [PATCH 15/30] fix(net): SUBSCRIBE_STARTED takes type 0x4, leaving 0x3 to SUBSCRIBE_SKIP Co-Authored-By: Claude Opus 5.5 --- drafts/draft-lcurley-moq-lite.md | 6 +++--- js/net/src/lite/subscribe.test.ts | 6 +++--- js/net/src/lite/subscribe.ts | 6 +++--- js/net/src/lite/subscriber.test.ts | 2 +- rs/moq-net/src/lite/subscribe.rs | 16 ++++++++-------- 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/drafts/draft-lcurley-moq-lite.md b/drafts/draft-lcurley-moq-lite.md index 57aa741f0c0..4e6a73a8f17 100644 --- a/drafts/draft-lcurley-moq-lite.md +++ b/drafts/draft-lcurley-moq-lite.md @@ -1624,7 +1624,7 @@ A SUBSCRIBE_STARTED message resolves where a Range that a [SUBSCRIBE_UPDATE](#su ~~~ SUBSCRIBE_STARTED Message { - Type (i) = 0x3 + Type (i) = 0x4 Message Length (i) Range ID (i) Range Start (i) @@ -1632,7 +1632,7 @@ SUBSCRIBE_STARTED Message { ~~~ **Type**: -Set to 0x3 to indicate a SUBSCRIBE_STARTED message. +Set to 0x4 to indicate a SUBSCRIBE_STARTED message. **Range ID**: The Range it resolves. @@ -1988,7 +1988,7 @@ The `Message Length` describes the payload size on the wire. - A SUBSCRIBE_UPDATE that lowers a `Start` covers the groups below the old start that the publisher still holds, not only those that arrive later. A group already sent is not sent again. - Replaced SUBSCRIBE_OK `Group` with a `Range Start` per Range: the Group where that Range's delivery starts, including at a Relative or Time Anchor, from which its Groups are owed. - Each Range takes an implicit Range ID, and SUBSCRIBE_UPDATE edits the live Ranges instead of restating them: `Remove Count` Range IDs to retire, then the Ranges to add. The live Ranges must stay non-empty and fit in one SUBSCRIBE. A Range resolves its Anchors once, however many updates leave it live. -- SUBSCRIBE_OK's Range Starts answer the SUBSCRIBE's Ranges, and added SUBSCRIBE_STARTED (type 0x3) resolving where each Range an update adds starts. +- SUBSCRIBE_OK's Range Starts answer the SUBSCRIBE's Ranges, and added SUBSCRIBE_STARTED (type 0x4) resolving where each Range an update adds starts. - Added `Largest Group` and `Largest Frame` to SUBSCRIBE_OK: the publisher's largest position when it answers, which a subscriber takes as where the live feed is. A publisher MUST answer at once when the requested start is past it. - Renamed SUBSCRIBE_END to SUBSCRIBE_FINISH, matching TRACKS_FINISH: it ends the Track, not the subscription. - Added `Error Code` to SUBSCRIBE_FINISH: a track can end with an error while keeping the groups below its end. diff --git a/js/net/src/lite/subscribe.test.ts b/js/net/src/lite/subscribe.test.ts index 1493a2cc6ac..62257e068b1 100644 --- a/js/net/src/lite/subscribe.test.ts +++ b/js/net/src/lite/subscribe.test.ts @@ -512,11 +512,11 @@ test("lite-07 updates edit only the changed ranges", async () => { expect(() => refused([0n], [])).toThrow(ProtocolViolation); }); -test("SUBSCRIBE_STARTED is type 3 on lite-07 and unknown before it", async () => { +test("SUBSCRIBE_STARTED is type 4 on lite-07 and unknown before it", async () => { for (const start of [undefined, 0n, 7n]) { const started = new SubscribeStarted(4n, start); expect(await encode(Version.DRAFT_07, { started })).toEqual( - new Uint8Array([3, 2, 4, start === undefined ? 0 : Number(start) + 1]), + new Uint8Array([4, 2, 4, start === undefined ? 0 : Number(start) + 1]), ); const got = await responseRoundtrip(Version.DRAFT_07, { started }); if (!("started" in got)) throw new Error("expected SUBSCRIBE_STARTED"); @@ -525,7 +525,7 @@ test("SUBSCRIBE_STARTED is type 3 on lite-07 and unknown before it", async () => await expect(encode(Version.DRAFT_06, { started: new SubscribeStarted(0n) })).rejects.toThrow(); await expect( decodeSubscribeResponse( - new Reader(undefined, new Uint8Array([3, 2, 0, 0]), Version.DRAFT_06), + new Reader(undefined, new Uint8Array([4, 2, 0, 0]), Version.DRAFT_06), Version.DRAFT_06, ), ).rejects.toThrow(ProtocolViolation); diff --git a/js/net/src/lite/subscribe.ts b/js/net/src/lite/subscribe.ts index d9a2d8c7232..6a61f9e0e85 100644 --- a/js/net/src/lite/subscribe.ts +++ b/js/net/src/lite/subscribe.ts @@ -1189,7 +1189,7 @@ export class SubscribeDrop { * - Draft-05+: `0x0` SUBSCRIBE_START (SUBSCRIBE_OK on lite-07), `0x1` SUBSCRIBE_FINISH * (SUBSCRIBE_END before lite-07), `0x2` SUBSCRIBE_DROP (SUBSCRIBE_OK was removed; * acceptance is implicit). - * - Lite-07+: also `0x3` SUBSCRIBE_STARTED. + * - Lite-07+: also `0x4` SUBSCRIBE_STARTED. */ export type SubscribeResponse = | { ok: SubscribeOk } @@ -1232,7 +1232,7 @@ export async function encodeSubscribeResponse(w: Writer, resp: SubscribeResponse await w.u53(0x2); await resp.drop.encode(w); } else if ("started" in resp && hasRanges(version)) { - await w.u53(0x3); + await w.u53(0x4); await resp.started.encode(w); } else { throw new Error("subscribe response not supported for this version"); @@ -1267,7 +1267,7 @@ export async function decodeSubscribeResponse(r: Reader, version: Version): Prom return { finish: await SubscribeFinish.decode(r, version) }; case 0x2: return { drop: await SubscribeDrop.decode(r) }; - case 0x3: + case 0x4: if (!hasRanges(version)) throw new ProtocolViolation(`unknown subscribe response type: ${typ}`); return { started: await SubscribeStarted.decode(r) }; default: diff --git a/js/net/src/lite/subscriber.test.ts b/js/net/src/lite/subscriber.test.ts index 1f9fbe2d763..e61dc6c8e85 100644 --- a/js/net/src/lite/subscriber.test.ts +++ b/js/net/src/lite/subscriber.test.ts @@ -2692,7 +2692,7 @@ test("an unknown subscribe response type closes the session", async () => { await answerTrackInfo(streams[0], Version.DRAFT_07); await (await opened(1)).reading; // An unassigned response type is a session protocol violation. - streams[1].inbound.enqueue(new Uint8Array([4, 0])); + streams[1].inbound.enqueue(new Uint8Array([5, 0])); expect(await track.closed).toBeInstanceOf(ProtocolViolation); await drainReactions(); expect(closes.map((info) => info?.closeCode)).toEqual([SessionCode.ProtocolViolation]); diff --git a/rs/moq-net/src/lite/subscribe.rs b/rs/moq-net/src/lite/subscribe.rs index a3589b33d7f..b1f26f8348b 100644 --- a/rs/moq-net/src/lite/subscribe.rs +++ b/rs/moq-net/src/lite/subscribe.rs @@ -1080,7 +1080,7 @@ impl<'a> Message<'a> for SubscribeDrop { /// - Lite05+: `0x0` SUBSCRIBE_START (SUBSCRIBE_OK on lite-07), `0x1` SUBSCRIBE_FINISH /// (SUBSCRIBE_END before lite-07), `0x2` SUBSCRIBE_DROP /// (SUBSCRIBE_OK was removed; acceptance is implicit). -/// - Lite07+: also `0x3` SUBSCRIBE_STARTED. +/// - Lite07+: also `0x4` SUBSCRIBE_STARTED. #[derive(Clone, Debug)] pub enum SubscribeResponse { Ok(SubscribeOk), @@ -1118,7 +1118,7 @@ impl Encode for SubscribeResponse { Self::Start(start) => encode_typed(w, 0, start, version)?, Self::Finish(end) => encode_typed(w, 1, end, version)?, Self::Drop(drop) => encode_typed(w, 2, drop, version)?, - Self::Started(started) => encode_typed(w, 3, started, version)?, + Self::Started(started) => encode_typed(w, 4, started, version)?, Self::Ok(_) => return Err(EncodeError::Version), }, } @@ -1145,7 +1145,7 @@ impl<'a> Decode<'a, Version> for SubscribeResponse { 0 => Ok(Self::Start(SubscribeStart::decode(buf, version)?)), 1 => Ok(Self::Finish(SubscribeFinish::decode(buf, version)?)), 2 => Ok(Self::Drop(SubscribeDrop::decode(buf, version)?)), - 3 if version.has_ranges() => Ok(Self::Started(SubscribeStarted::decode(buf, version)?)), + 4 if version.has_ranges() => Ok(Self::Started(SubscribeStarted::decode(buf, version)?)), _ => Err(DecodeError::InvalidMessage(typ)), } } @@ -1635,15 +1635,15 @@ mod test { assert!(refused, "the live ranges are held to one SUBSCRIBE"); } - /// SUBSCRIBE_STARTED is type 3 on lite-07 and unknown before it. + /// SUBSCRIBE_STARTED is type 4 on lite-07 and unknown before it. #[test] - fn subscribe_started_is_type_3_on_lite07() { + fn subscribe_started_is_type_4_on_lite07() { for start in [None, Some(0), Some(7)] { let started = SubscribeStarted { id: 4, start }; let buf = SubscribeResponse::Started(started.clone()) .encode_bytes(Version::Lite07) .unwrap(); - assert_eq!(buf[0], 3); + assert_eq!(buf[0], 4); let got = crate::coding::decode_buf(&mut buf.as_ref(), Version::Lite07, |r, v| { SubscribeResponse::decode(r, v) }) @@ -1654,9 +1654,9 @@ mod test { assert!(started.encode_bytes(Version::Lite06).is_err()); let mut buf = Vec::new(); let mut w = Encoder::new(&mut buf, Version::Lite06.into()); - w.varint(3).unwrap(); + w.varint(4).unwrap(); SubscribeStarted { id: 0, start: None } - .encode_msg(&mut w, Version::Lite07) + .encode(&mut w, Version::Lite07) .unwrap(); assert!( crate::coding::decode_buf(&mut buf.as_slice(), Version::Lite06, |r, v| SubscribeResponse::decode(r, v)) From dec06395cd628da3ce4677e7c1ef8d0b67ff4dae Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 4 Nov 2026 07:23:49 -0800 Subject: [PATCH 16/30] feat(net): lite-07 fills more than one group through the subscription A lite-07 copy asks for a fill of more than one group as ranges on its upstream subscription, beside the demand's, or on a fills-only bounded SUBSCRIBE when no demand is open. One group, or a slice of one, keeps FETCH. The demand's budget caps every range, and a group it drops as stale, or one the subscription already delivered and the cache has since evicted, is asked again once on a SUBSCRIBE of its own with no max delay. SUBSCRIBE_OK answers only the SUBSCRIBE's ranges in both publishers. Co-Authored-By: Claude Opus 5.5 --- js/net/src/lite/publisher.ts | 28 +- rs/moq-net/src/lite/publisher.rs | 28 +- rs/moq-net/src/lite/subscribe.rs | 41 +- rs/moq-net/src/lite/subscriber.rs | 299 +++++++++-- rs/moq-net/src/lite/subscriber/fills.rs | 476 ++++++++++++++++++ rs/moq-net/src/tail.rs | 5 + .../tests/integration/widened_max_age.rs | 19 +- 7 files changed, 811 insertions(+), 85 deletions(-) create mode 100644 rs/moq-net/src/lite/subscriber/fills.rs diff --git a/js/net/src/lite/publisher.ts b/js/net/src/lite/publisher.ts index fc6ca3d3c57..1ecbd1a6741 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -401,12 +401,6 @@ function pin(entry: RangeEntry, resolved: Resolved | undefined): RangeEntry { return { range, from, to }; } -// Where each range of `track` starts once delivery starts at `from`, undefined for one with no -// group there. Before lite-07 there is one range, which starts there. -function rangeStarts(version: Version, track: track.Subscriber, from: bigint): (bigint | undefined)[] { - return hasRanges(version) ? hooks.servingStarts(track, from) : [from]; -} - // The later of two locations, either of which may be unknown. function newer(a: track.Position | undefined, b: track.Position | undefined): track.Position | undefined { if (!a || !b) return a ?? b; @@ -1311,6 +1305,18 @@ export class Publisher { let ids = Array.from({ length: Number(initial) }, (_, index) => BigInt(index)); let unstarted: bigint[] = []; + // Where each of the SUBSCRIBE's ranges starts once delivery starts at `from`, by Range ID, + // undefined for one with no group there or an update retired: SUBSCRIBE_STARTED answers + // each one an update added. Before lite-07 there is one range, which starts there. + const rangeStarts = (from: bigint): (bigint | undefined)[] => { + if (!hasRanges(this.version)) return [from]; + const starts = hooks.servingStarts(track, from); + return Array.from({ length: Number(initial) }, (_, id) => { + const index = ids.indexOf(BigInt(id)); + return index < 0 ? undefined : starts[index]; + }); + }; + // Report where each range an update added starts, once it resolves, or with no start once // nothing can resolve it (`finished`). SUBSCRIBE_START goes first. const reportStarted = async (finished: boolean): Promise => { @@ -1528,7 +1534,7 @@ export class Publisher { startSequence = bounds.startGroup; raiseFloor(bounds.startGroup); const start = new SubscribeStart( - rangeStarts(this.version, track, bounds.startGroup), + rangeStarts(bounds.startGroup), largest, ); if ( @@ -1545,7 +1551,7 @@ export class Publisher { if (end !== undefined && hooks.servingRuns(track, 0n, end).length === 0) { startSent = true; startSequence = end; - const start = new SubscribeStart(rangeStarts(this.version, track, end), largest); + const start = new SubscribeStart(rangeStarts(end), largest); if ( !(await controls.response(encodeSubscribeResponse(stream, { start }, this.version))) ) @@ -1573,7 +1579,7 @@ export class Publisher { startSent = true; startSequence = runs[0].start; const start = new SubscribeStart( - rangeStarts(this.version, track, startSequence), + rangeStarts(startSequence), largest, ); if ( @@ -1608,7 +1614,7 @@ export class Publisher { if (keepsSubscribeOpen(this.version) && !startSent && boundary() > 0n) { startSent = true; startSequence = hooks.servingRuns(track, 0n, boundary())[0]?.start ?? boundary(); - const start = new SubscribeStart(rangeStarts(this.version, track, startSequence), largest); + const start = new SubscribeStart(rangeStarts(startSequence), largest); if (!(await controls.response(encodeSubscribeResponse(stream, { start }, this.version)))) return false; continue; @@ -1684,7 +1690,7 @@ export class Publisher { !(await controls.response( encodeSubscribeResponse( stream, - { start: new SubscribeStart(rangeStarts(this.version, track, startSequence), largest) }, + { start: new SubscribeStart(rangeStarts(startSequence), largest) }, this.version, ), )) diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 4f257e978b2..f59e02afbac 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -2855,11 +2855,7 @@ impl SubscribeServe { } /// Serve `msg` from the name's datagram track, at the priority of its latest `update`. - fn datagrams( - mut consumer: datagram::Consumer, - msg: &lite::Subscribe<'static>, - update: Option, - ) -> Self { + fn datagrams(mut consumer: datagram::Consumer, msg: &lite::Subscribe<'static>, update: Option) -> Self { let priority = update.map_or(msg.priority, |update| update.priority); consumer.set_priority(priority); Self::Datagrams { @@ -4798,7 +4794,11 @@ impl TrackRun { let mut waiting = Vec::new(); let mut reported = false; for id in std::mem::take(&mut self.unstarted) { - let index = self.ids.iter().position(|held| *held == id).expect("unstarted ranges are live"); + let index = self + .ids + .iter() + .position(|held| *held == id) + .expect("unstarted ranges are live"); let start = match resolved.get(index).copied().flatten() { Some((start, end)) => { let start = start.max(from); @@ -5154,10 +5154,18 @@ impl TrackRun { /// Where each requested range starts once delivery starts at `start`. Before lite-07 /// there is one range, which starts there. fn starts(&mut self, start: u64) -> Vec> { - match self.ctx.version.has_ranges() { - true => self.track.starts(start), - false => vec![Some(start)], - } + if !self.ctx.version.has_ranges() { + return vec![Some(start)]; + } + // 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. + let starts = self.track.starts(start); + (0..self.initial) + .map(|id| { + let index = self.ids.iter().position(|held| *held == id)?; + starts.get(index).copied().flatten() + }) + .collect() } /// Send SUBSCRIBE_START for the first servable group, then serve it. diff --git a/rs/moq-net/src/lite/subscribe.rs b/rs/moq-net/src/lite/subscribe.rs index b1f26f8348b..142efcab100 100644 --- a/rs/moq-net/src/lite/subscribe.rs +++ b/rs/moq-net/src/lite/subscribe.rs @@ -813,9 +813,7 @@ impl LiveRanges { let form: Form = self.version.into(); for range in ranges { // One past the varint's reach can't come off the wire, and is refused below. - self.size = self - .size - .saturating_add(range_size(&range, form).unwrap_or(usize::MAX)); + self.size = self.size.saturating_add(range_size(&range, form).unwrap_or(usize::MAX)); self.live.insert(self.next, range); self.next += 1; } @@ -1553,7 +1551,12 @@ mod test { let got = crate::coding::decode_buf(&mut buf.as_ref(), Version::Lite07, |r, v| SubscribeUpdate::decode(r, v)) .unwrap(); let mut live = LiveRanges::new(Version::Lite07, &[Range::OLDEST]); - let resolved: Vec<_> = live.apply(got.ranges).unwrap().into_iter().map(|(_, range)| range).collect(); + let resolved: Vec<_> = live + .apply(got.ranges) + .unwrap() + .into_iter() + .map(|(_, range)| range) + .collect(); assert_eq!(resolved, msg.ranges); } @@ -1590,8 +1593,8 @@ mod test { order: crate::track::Order::Descending, }; let buf = update.encode_bytes(version).unwrap(); - let got = crate::coding::decode_buf(&mut buf.as_ref(), version, |r, v| SubscribeUpdate::decode(r, v)) - .unwrap(); + let got = + crate::coding::decode_buf(&mut buf.as_ref(), version, |r, v| SubscribeUpdate::decode(r, v)).unwrap(); assert_eq!(live.apply(got.ranges).unwrap(), sent.numbered()); }; @@ -1604,7 +1607,10 @@ mod test { step(&[c, c, a], edit(&[], &[c])); step(&[b], edit(&[0, 2, 3], &[b])); assert_eq!(live.numbered(), [(4, b)]); - assert!(live.initial(1) && !live.initial(2), "SUBSCRIBE_START reports only the SUBSCRIBE's"); + assert!( + live.initial(1) && !live.initial(2), + "SUBSCRIBE_START reports only the SUBSCRIBE's" + ); } /// An edit naming a Range ID never assigned or already retired, one leaving nothing @@ -1614,10 +1620,19 @@ mod test { let version = Version::Lite07; let edit = |remove: Vec, add: Vec| RangeEdit::Edit { remove, add }; let fresh = || LiveRanges::new(version, &[Range::OLDEST]); - assert!(fresh().apply(edit(vec![1], vec![Range::OLDEST])).is_err(), "never assigned"); - assert!(fresh().apply(edit(vec![0, 0], vec![Range::OLDEST])).is_err(), "retired twice"); + assert!( + fresh().apply(edit(vec![1], vec![Range::OLDEST])).is_err(), + "never assigned" + ); + assert!( + fresh().apply(edit(vec![0, 0], vec![Range::OLDEST])).is_err(), + "retired twice" + ); assert!(fresh().apply(edit(vec![0], vec![])).is_err(), "nothing left"); - assert!(fresh().apply(RangeEdit::Replace(vec![Range::OLDEST])).is_err(), "not on lite-07"); + assert!( + fresh().apply(RangeEdit::Replace(vec![Range::OLDEST])).is_err(), + "not on lite-07" + ); let mut live = fresh(); let one = Range { @@ -1659,8 +1674,10 @@ mod test { .encode(&mut w, Version::Lite07) .unwrap(); assert!( - crate::coding::decode_buf(&mut buf.as_slice(), Version::Lite06, |r, v| SubscribeResponse::decode(r, v)) - .is_err() + crate::coding::decode_buf(&mut buf.as_slice(), Version::Lite06, |r, v| SubscribeResponse::decode( + r, v + )) + .is_err() ); } diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 99d72ae80a0..5ccaa08d11f 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -20,6 +20,9 @@ use crate::tail::{self, Reading, Settle, Tail}; use kio::Lock; +mod fills; +use fills::{FillAsk, RangeFills}; + pub(super) struct SubscriberConfig { pub runtime: crate::time::Deadlines, pub session: Transport, @@ -2908,6 +2911,9 @@ mod tests { priority: 0, requested: None, live: lite::LiveRanges::new(Version::Lite04, &[lite::Range::OLDEST]), + demand: Some(Subscription::default()), + demand_ranges: vec![lite::Range::OLDEST], + fill_groups: Default::default(), tail: tail.clone(), served: None, started: false, @@ -3059,6 +3065,9 @@ mod tests { priority: 0, requested: Some(floor), live: lite::LiveRanges::new(Version::Lite04, &[lite::Range::span(Some(floor), 0, None, None)]), + demand: Some(Subscription::default()), + demand_ranges: vec![lite::Range::span(Some(floor), 0, None, None)], + fill_groups: Default::default(), tail: Default::default(), served: None, started: false, @@ -3074,7 +3083,9 @@ mod tests { .await .unwrap(); } else { - let begin = h.serve.begin_subscription(&mut track, &mut Sub::None, lowered, true); + let begin = h + .serve + .begin_subscription(&mut track, &mut Sub::None, lowered, &FillAsk::default(), true); assert!(matches!(begin, Ok(Begin::Establish(_)))); } @@ -3519,6 +3530,9 @@ mod tests { priority: 0, requested: Some(2), live: lite::LiveRanges::new(VERSION, &[lite::Range::span(Some(2), 0, None, None)]), + demand: Some(Subscription::default()), + demand_ranges: vec![lite::Range::span(Some(2), 0, None, None)], + fill_groups: Default::default(), tail: Default::default(), served: None, started: false, @@ -3625,6 +3639,9 @@ mod tests { priority: 0, requested: Some(4), live: lite::LiveRanges::new(Version::Lite07, &[first]), + demand: Some(Subscription::default()), + demand_ranges: vec![first], + fill_groups: Default::default(), tail: Default::default(), served: Some(4), started: true, @@ -5997,8 +6014,13 @@ mod tests { let broadcast = crate::broadcast::Info::new().produce(); let request = broadcast.reserve_track("audio").unwrap(); let mut serving = ServeLoop::new(&serve, request, Default::default()); - let establish = - serve.prepare_establish(&mut serving.serving, Subscription::default(), vec![lite::Range::OLDEST]); + let wire = Wire { + subscription: Subscription::default(), + demand: Some(Subscription::default()), + demand_ranges: vec![lite::Range::OLDEST], + ranges: vec![lite::Range::OLDEST], + }; + let establish = serve.prepare_establish(&mut serving.serving, wire, &Default::default()); serving.mode = ServeMode::Establish(establish); let gate_run = serve.gate(); let mut running = TrackServeRun { @@ -7998,8 +8020,14 @@ struct SubStream { /// The lowest absolute start among the ranges, or `None` when one counts back from /// the latest group: the floor below which nothing is asked for. start: Option, - /// The ranges on the wire by Range ID, never empty. + /// The ranges on the wire by Range ID, never empty: the demand's, then the fills'. live: lite::LiveRanges, + /// The demand the ranges ask for, `None` when they ask only for fills. + demand: Option, + /// The demand's ranges on the wire, which alone set where the live feed is. + demand_ranges: Vec, + /// The groups the ranges ask for fills. + fill_groups: rangemap::RangeSet, priority: i8, order: crate::track::Order, /// The start the SUBSCRIBE itself carried, fixed for the stream's life. A @@ -8119,6 +8147,18 @@ impl SubStream { Some(owed) } + /// The groups the live range `id` asks for a fill, `None` for one of the demand's. + fn fill_range(&self, id: u64) -> Option> { + let range = self.live.get(id)?; + if self.demand_ranges.contains(range) { + return None; + } + match (range.start, range.end) { + (Some(lite::Anchor::Absolute(start)), Some(lite::Anchor::Absolute(end))) => Some(start.group..end.group), + _ => None, + } + } + /// Edit the live ranges into `ranges`, forgetting the starts of those retired. Returns the /// edit, and whether it changed anything. fn edit(&mut self, ranges: &[lite::Range]) -> Result<(lite::RangeEdit, bool), Error> { @@ -8525,20 +8565,60 @@ impl TrackServe { producer: &mut track::Producer, sub: &mut Sub, pref: Option, + fills: &FillAsk, supports_update: bool, ) -> Result { let opening = matches!(sub, Sub::None); + // Lite-07 asks the held fills' groups through the same subscription. Demand leaving + // while they are out keeps what it asked until they are answered, rather than retire + // the live feed's ranges under a subscription that still runs. + let none = FillAsk::default(); + let fills = match self.subscriber.version.has_ranges() { + true => fills, + false => &none, + }; + let pref = match (pref, &*sub) { + (None, Sub::Active(active)) if !fills.is_empty() => active.demand.clone(), + (pref, _) => pref, + }; let request = self.wire_request(pref, opening, producer.idle_newest(), producer.info().timescale)?; + // What goes on the wire: the demand's ranges, then the fills', under the demand's + // parameters. The demand alone sets the floor. + let request = match request { + Some((demand, demand_ranges)) => { + let mut ranges = demand_ranges.clone(); + ranges.extend(fills.wire()); + Some(Wire { + subscription: fills.merge(demand.clone()), + demand: Some(demand), + demand_ranges, + ranges, + }) + } + None if fills.is_empty() => None, + None => Some(Wire { + subscription: fills.subscription(), + demand: None, + demand_ranges: Vec::new(), + ranges: fills.wire().collect(), + }), + }; match request { - Some((subscription, ranges)) => { + Some(wire) => { match sub { // Open an upstream SUBSCRIBE for the first subscriber. - Sub::None => Ok(Begin::Establish(self.prepare_establish(producer, subscription, ranges))), + Sub::None => Ok(Begin::Establish(self.prepare_establish(producer, wire, &fills.groups))), Sub::Active(active) => { + let Wire { + subscription, + demand, + demand_ranges, + ranges, + } = wire; // Downstream preferences changed: forward them upstream as a // SUBSCRIBE_UPDATE (Lite03+ only; older peers can't carry one). - let start_moved = active.start != subscription.start(); + let start_moved = demand.is_some() && active.start != subscription.start(); active.priority = subscription.priority; active.order = subscription.order; active.max_delay = wire_max_delay(subscription.delay.max); @@ -8554,14 +8634,25 @@ impl TrackServe { // two: each gap they reach waits a fresh grace, which only delays giving up // on one still owed. let (edit, changed) = active.edit(&ranges)?; - let widened = changed.then(|| subscription.start().or(active.served).unwrap_or(0)..u64::MAX); + let widened = (changed && demand.is_some()) + .then(|| subscription.start().or(active.served).unwrap_or(0)..u64::MAX); if let Ok(mut tail) = active.tail.write() { tail.set_grace(producer.tail_grace(subscription.budget())); let now = self.subscriber.runtime.now(); for groups in lowered.into_iter().chain(widened) { tail.demand(groups, now); } + // So do the groups a fill newly asks for. + for groups in fills.groups.iter() { + let asked = active.fill_groups.gaps(groups).collect::>(); + for groups in asked { + tail.demand(groups, now); + } + } } + active.fill_groups = fills.groups.clone(); + active.demand = demand; + active.demand_ranges = demand_ranges; active.start = subscription.start(); if supports_update { // The floor follows the requested start, in both directions: @@ -8590,9 +8681,12 @@ impl TrackServe { self.subscriber.remove_subscribe(active.id); let _ = active.stream.writer.finish(); tracing::debug!(track = %self.name, "subscribe canceled (idle)"); + // The copy is still held: what it cached goes stale from here, unless the + // subscription only filled it and never fed it live. + if active.demand.is_some() { + producer.set_idle(); + } *sub = Sub::None; - // The copy is still held: what it cached goes stale from here. - producer.set_idle(); } Ok(Begin::None) } @@ -8668,19 +8762,32 @@ impl TrackServe { fn prepare_establish( &self, producer: &mut track::Producer, - subscription: Subscription, - ranges: Vec, + wire: Wire, + fills: &rangemap::RangeSet, ) -> Establish { - self.request_floor(producer, subscription.start()); + let Wire { + subscription, + demand, + demand_ranges, + ranges, + } = wire; + // One asking only for fills is no live feed, so it leaves the floor alone. + if demand.is_some() { + self.request_floor(producer, subscription.start()); + } let framing = lite::Framing::new(producer.info(), self.subscriber.version); - self.register( + let mut establish = self.register( subscription, ranges, Some(Accepted { producer: producer.clone(), framing, }), - ) + ); + establish.demand = demand; + establish.demand_ranges = demand_ranges; + establish.fill_groups = fills.clone(); + establish } /// The floor tracks the requested start until this subscription's own @@ -8744,6 +8851,9 @@ impl TrackServe { closed: session.clone(), session, id, + demand: Some(subscription.clone()), + demand_ranges: ranges.clone(), + fill_groups: Default::default(), subscription, ranges, tail, @@ -8764,7 +8874,13 @@ impl TrackServe { false => wire_span(subscription), }; let ranges = wire_ranges(&subscription, producer.info().timescale)?.expect("a non-empty subscription"); - let mut est = Box::new(self.prepare_establish(producer, subscription, ranges)); + let wire = Wire { + subscription: subscription.clone(), + demand: Some(subscription), + demand_ranges: ranges.clone(), + ranges, + }; + let mut est = Box::new(self.prepare_establish(producer, wire, &Default::default())); let id = est.id; match kio::wait(move |waiter| est.poll(waiter)).await { Ok(active) => { @@ -8788,7 +8904,7 @@ impl TrackServe { pref: Option, supports_update: bool, ) -> Result<(), Error> { - match self.begin_subscription(producer, sub, pref, supports_update)? { + match self.begin_subscription(producer, sub, pref, &FillAsk::default(), supports_update)? { Begin::Establish(est) => { let mut est = Box::new(est); let id = est.id; @@ -8842,6 +8958,15 @@ fn buffer_update(active: &mut SubStream, ranges: lite::RangeEdit) -> Result<(), }) } +/// What a subscription puts on the wire: its parameters, the demand it asks for, and the +/// ranges, the demand's first, then the held fills'. +struct Wire { + subscription: Subscription, + demand: Option, + demand_ranges: Vec, + ranges: Vec, +} + /// Opens the upstream SUBSCRIBE control stream: send the request, then (pre /// lite-05) wait for the SUBSCRIBE_OK. Resolves with the live [`SubStream`]; /// the caller deregisters the id on failure. @@ -8852,8 +8977,15 @@ struct Establish { // A dedicated close-watch handle for the SUBSCRIBE_OK wait. closed: Transport, id: u64, + /// The parameters the SUBSCRIBE carries. subscription: Subscription, - /// The ranges the SUBSCRIBE carries for `subscription`. + /// The demand it asks for, `None` when it asks only for fills. + demand: Option, + /// The demand's ranges, which come first on the wire. + demand_ranges: Vec, + /// The groups it asks for fills. + fill_groups: rangemap::RangeSet, + /// The ranges the SUBSCRIBE carries: the demand's, then the fills'. ranges: Vec, tail: kio::Producer, /// Holds the session busy while the SUBSCRIBE opens. @@ -8926,6 +9058,9 @@ impl Establish { min_delay: self.subscription.delay.min, start: self.subscription.start(), live: lite::LiveRanges::new(self.serve.subscriber.version, &self.ranges), + demand: self.demand.clone(), + demand_ranges: self.demand_ranges.clone(), + fill_groups: self.fill_groups.clone(), priority: self.subscription.priority, order: self.subscription.order, requested: self.subscription.start(), @@ -9584,6 +9719,8 @@ struct ServeLoop { fetches: kio::Tasks, /// Fetches a pre-lite-05 upstream has no FETCH for, held for the live feed. feed_fetches: FeedFetches, + /// Fills a lite-07 upstream serves through the subscription. + range_fills: RangeFills, // A dedicated close-watch handle for the session-died arm. closed: Transport, // SUBSCRIBE_UPDATE only exists on Lite03+, so older peers can't carry a @@ -9874,6 +10011,7 @@ impl ServeLoop { sub: Sub::None, fetches, feed_fetches: FeedFetches::new(&serve.subscriber.runtime), + range_fills: RangeFills::default(), closed: serve.subscriber.session.clone(), supports_update: !matches!(serve.subscriber.version, Version::Lite01 | Version::Lite02), supports_fetch: serve.subscriber.version.has_track_stream(), @@ -9918,8 +10056,18 @@ impl ServeLoop { /// Apply a demand change: open, update, or cancel the upstream subscription. fn begin(&mut self, serve: &TrackServe, pref: Option) -> Result<(), Error> { - match serve.begin_subscription(&mut self.serving, &mut self.sub, pref, self.supports_update)? { - Begin::Establish(est) => self.mode = ServeMode::Establish(est), + match serve.begin_subscription( + &mut self.serving, + &mut self.sub, + pref, + &self.range_fills.ask, + self.supports_update, + )? { + Begin::Establish(est) => { + // A fresh subscription may deliver what the last one would not. + self.range_fills.forget_refused(); + self.mode = ServeMode::Establish(est); + } Begin::None => {} } // A finished track's tail waits a fresh grace for what an update newly asked for, @@ -9992,17 +10140,38 @@ impl ServeLoop { tracing::warn!(track = %serve.name, starts = start.starts.len(), ranges = active.live.initial_count(), "subscribe start resolves other ranges"); return Err(Error::ProtocolViolation); } - // No range starting anywhere leaves nothing owed. - let served = start.starts.iter().flatten().min().copied(); - active.served = served; - // An update may have retired some already, whose starts say nothing now. + // An update may have retired some already, whose starts say nothing now. A + // fill's range starting past where it asked skips groups it will not deliver. + let now = serve.subscriber.runtime.now(); + let mut served: Option = None; for (id, start) in (0..).zip(&start.starts) { - if active.live.get(id).is_some() { - active.starts.insert(id, *start); + if active.live.get(id).is_none() { + continue; + } + active.starts.insert(id, *start); + match active.fill_range(id) { + Some(groups) => { + let skipped = groups.start..start.unwrap_or(groups.end).clamp(groups.start, groups.end); + if let Ok(mut tail) = active.tail.write() { + tail.account(skipped.clone(), now); + } + self.range_fills.refuse(skipped, crate::StreamError::NotFound); + } + // Only the demand's ranges say where the live feed is served from. No range + // starting anywhere leaves nothing owed. + None => { + served = match (served, *start) { + (Some(served), Some(start)) => Some(served.min(start)), + (served, start) => served.or(start), + } + } } } - // Where the live feed is, on versions whose answer says. - if serve.subscriber.version.has_largest() { + active.served = served; + // One asking only for fills feeds nothing live. + if active.demand.is_none() { + active.started = true; + } else if serve.subscriber.version.has_largest() { // A bounded feed that never delivers Largest goes live at the // answer alone; one that does waits for it to show. match active.never_serves(start.largest) { @@ -10021,7 +10190,7 @@ impl ServeLoop { // that moved the start makes it stale, and one that // moved back restores it); elsewhere the // request-tracked floor stands rather than a guess. - if active.start == active.requested { + if active.start == active.requested && active.demand.is_some() { let _ = match (serve.subscriber.version.resolves_start(), served) { (true, Some(served)) => self.serving.start_resolved(served), _ => self.serving.start_at(served), @@ -10052,9 +10221,18 @@ impl ServeLoop { // One retired since is owed nothing. if active.live.get(id).is_some() { active.starts.insert(id, started.start); + let now = serve.subscriber.runtime.now(); + // A fill's range starting past where it asked skips groups it will not deliver. + if let Some(groups) = active.fill_range(id) { + let skipped = groups.start..started.start.unwrap_or(groups.end).clamp(groups.start, groups.end); + if let Ok(mut tail) = active.tail.write() { + tail.account(skipped.clone(), now); + } + self.range_fills.refuse(skipped, crate::StreamError::NotFound); + } // The tail judges what is owed again. if let Ok(mut tail) = active.tail.write() { - tail.expire(serve.subscriber.runtime.now()); + tail.expire(now); } } } @@ -10062,16 +10240,24 @@ impl ServeLoop { // are accounted for without a stream, and a reader of // one still arriving ends with the drop's error now. lite::SubscribeResponse::Drop(dropped) => { + let groups = dropped.start..dropped.end.saturating_add(1); + let err = dropped.error(serve.subscriber.version); if let Ok(mut tail) = active.tail.write() { - tail.account( - dropped.start..dropped.end.saturating_add(1), - serve.subscriber.runtime.now(), - ); + tail.account(groups.clone(), serve.subscriber.runtime.now()); } - self.serving.drop_groups( - dropped.start..dropped.end.saturating_add(1), - dropped.error(serve.subscriber.version), - ); + // A group a fill asked for and the live budget dropped as stale is recovered on + // its own, so the copy does not record it given up meanwhile. + let mut given = rangemap::RangeSet::new(); + given.insert(groups.clone()); + if err == crate::StreamError::Old { + for asked in active.fill_groups.overlapping(&groups) { + given.remove(asked.clone()); + } + } + for given in given.iter() { + self.serving.drop_groups(given.clone(), err.clone()); + } + self.range_fills.refuse(groups, err); } // OK just resolves the range (the producer already orders // groups). @@ -10164,7 +10350,10 @@ impl ServeLoop { if !drained { // The groups are drained only for a reader: once nobody holds the // copy, a stream the publisher never finishes must not hold it. - if self.fetches.is_empty() && crate::util::poll_unwanted(&mut self.demand, waiter).is_ready() { + if self.fetches.is_empty() + && self.range_fills.is_empty() + && crate::util::poll_unwanted(&mut self.demand, waiter).is_ready() + { return Poll::Ready(ServeEnd::Idle); } return Poll::Pending; @@ -10195,7 +10384,10 @@ impl ServeLoop { { return Poll::Ready(ServeEnd::Finished); } - if self.fetches.is_empty() && crate::util::poll_unwanted(&mut self.demand, waiter).is_ready() { + if self.fetches.is_empty() + && self.range_fills.is_empty() + && crate::util::poll_unwanted(&mut self.demand, waiter).is_ready() + { return Poll::Ready(ServeEnd::Idle); } // The tail is still unsettled, so a session that dies now cuts the track @@ -10251,7 +10443,16 @@ impl ServeLoop { // subscription churn. match self.dynamic.poll_requested_fill(waiter) { Poll::Ready(Ok(fill)) => { - if self.supports_fetch { + // Lite-07 asks for more than one group through the subscription. One + // group, or a slice of one, is what FETCH carries, and a time lookup + // names no group yet. + let last = fill.end().saturating_sub(1); + let groups = fill.start().frame == 0 + && fill.time().is_none() && fill.start().group < last + && fill.group(last).frame_end().is_none(); + if serve.subscriber.version.has_ranges() && groups { + self.range_fills.push(fill); + } else if self.supports_fetch { self.fetches.push(FillServeRun::new( serve.clone(), fill, @@ -10308,6 +10509,17 @@ impl ServeLoop { let _ = self.fetches.poll(waiter); self.feed_fetches .poll(&self.sub, serve.subscriber.runtime.now(), waiter); + // A fill asking for other groups changes the subscription. + let accounted = match &self.sub { + Sub::Active(active) => Some(active.tail.clone()), + Sub::None => None, + }; + if self.range_fills.poll(serve, &self.serving, accounted.as_ref(), waiter) { + if let Err(err) = self.begin(serve, self.serving.subscription()) { + return Poll::Ready(ServeEnd::GiveBack(err)); + } + continue; + } // A lite-07 track the publisher finished ends once its tail settles. if self.poll_finishing(waiter).is_ready() { @@ -10318,7 +10530,10 @@ impl ServeLoop { // went with the last subscriber, so it lingers, cache and all, for a // reader or fetch that asks again soon, then is dropped. In-flight // fetches keep it alive: work already accepted still gets finished. - if self.fetches.is_empty() && crate::util::poll_unwanted(&mut self.demand, waiter).is_ready() { + if self.fetches.is_empty() + && self.range_fills.is_empty() + && crate::util::poll_unwanted(&mut self.demand, waiter).is_ready() + { self.held = None; if self.linger.deadline().is_none() { let now = serve.subscriber.runtime.now(); diff --git a/rs/moq-net/src/lite/subscriber/fills.rs b/rs/moq-net/src/lite/subscriber/fills.rs new file mode 100644 index 00000000000..10a30c4c2aa --- /dev/null +++ b/rs/moq-net/src/lite/subscriber/fills.rs @@ -0,0 +1,476 @@ +//! Fills a lite-07 copy asks of its upstream through a subscription rather than FETCH. + +use std::{ + collections::BTreeMap, + ops::Range, + task::{Poll, ready}, + time::Duration, +}; + +use rangemap::{RangeMap, RangeSet}; + +use super::{Establish, SubStream, Tail, TrackServe}; +use crate::{ + Error, StreamError, group, lite, + track::{self, Delay, Order, Position, Subscription}, +}; + +/// What a lite-07 copy's held fills ask of its upstream subscription, beside the demand. +#[derive(Clone, Debug, PartialEq)] +pub(super) struct FillAsk { + /// The groups to ask for, empty when no fill is held. + pub groups: RangeSet, + /// The most urgent priority a held fill asks with. + pub priority: i8, + /// The most tolerant budget a held fill asks with, `None` for the publisher's max age. + pub max_delay: Option, + pub order: Order, +} + +impl Default for FillAsk { + fn default() -> Self { + Self { + groups: Default::default(), + priority: i8::MIN, + max_delay: Some(Duration::ZERO), + order: Order::Ascending, + } + } +} + +impl FillAsk { + pub fn is_empty(&self) -> bool { + self.groups.is_empty() + } + + /// Count `fill`'s preferences in. + fn add(&mut self, fill: &track::Fill) { + self.priority = self.priority.max(fill.priority()); + self.max_delay = match (self.max_delay, fill.max_delay()) { + (Some(ours), Some(theirs)) => Some(ours.max(theirs)), + _ => None, + }; + if fill.order() == Order::Descending { + self.order = Order::Descending; + } + } + + /// The bounded ranges the groups take on the wire, each ending before a group's first + /// frame, as `wire_ranges` ends an absolute one. + pub fn wire(&self) -> impl Iterator + '_ { + self.groups.iter().map(|groups| lite::Range { + start: Some(lite::Anchor::Absolute(Position::group(groups.start))), + end: Some(lite::Anchor::Absolute(Position::group(groups.end))), + }) + } + + /// `demand` asked with the fills' priority too. The demand's budget caps every range, so + /// the live feed never loosens for a fill: a group it drops as stale is recovered on its + /// own (see [`Recovery`]). + pub fn merge(&self, demand: Subscription) -> Subscription { + let priority = demand.priority.max(self.priority); + demand.with_priority(priority) + } + + /// A subscription asking for the fills alone: no range of its own, so no floor. + pub fn subscription(&self) -> Subscription { + Subscription::default() + .with_ranges([]) + .with_priority(self.priority) + .with_delay(Delay::default().with_max(self.max_delay)) + .with_order(self.order) + } +} + +/// Fills a lite-07 upstream serves through the subscription rather than FETCH: each held +/// fill's next owed run joins the upstream ranges until its groups arrive or the publisher +/// names them given up, and the fill answers itself from the cache. +/// +/// A fill is judged against the subscription that asked for it alone. The track's arrivals +/// still account for a group an earlier subscription cut, which this one may still deliver, +/// so only what this subscription says it will not deliver (a SUBSCRIBE_DROP, or a range +/// start past the range) refuses a fill. A group it drops as stale is officially dropped, +/// and one it already accounted for (delivered before and since evicted here) it never +/// sends again, so a [`Recovery`] asks for either on its own subscription with no max delay, +/// once. +/// +/// A group stays asked until its stream finishes, since narrowing the subscription under a +/// stream still arriving would reset it. +#[derive(Default)] +pub(super) struct RangeFills { + held: Vec, + /// What the upstream subscription asks for the fills now. + pub ask: FillAsk, + /// Asked groups no fill owes any more whose stream is still being read. + reading: BTreeMap, + /// Asked groups no fill owes any more, already searched for streams still being read. + draining: RangeSet, + /// Groups the subscription said it will not deliver, and why. + refused: RangeMap, + /// Groups dropped as stale, which a [`Recovery`] has yet to ask for. + recover: RangeSet, + /// Groups a [`Recovery`] asks for now, which the subscription leaves out meanwhile. + recovering: RangeSet, + /// Groups already recovered once, which a second stale drop refuses. + recovered: RangeSet, + /// The subscriptions recovering them. + recoveries: Vec, +} + +impl RangeFills { + /// Whether no fill is held, asked for, or recovering. + pub fn is_empty(&self) -> bool { + self.held.is_empty() && self.ask.is_empty() && self.recoveries.is_empty() + } + + pub fn push(&mut self, fill: track::Fill) { + self.held.push(fill); + } + + /// The subscription will not deliver `groups`: a fill owing one is refused with `err`, + /// or recovered once when it was dropped as stale. + pub fn refuse(&mut self, groups: Range, err: StreamError) { + // Only what a fill asked for: a fill asks for nothing the subscription dropped + // before it. + let asked: Vec<_> = self + .ask + .groups + .overlapping(&groups) + .map(|run| run.start.max(groups.start)..run.end.min(groups.end)) + .collect(); + for groups in asked { + if err == StreamError::Old { + for fresh in self.recovered.gaps(&groups).collect::>() { + self.recover.insert(fresh); + } + // Recovered once already: this drop stands. + for again in self.recovered.overlapping(&groups).cloned().collect::>() { + let again = again.start.max(groups.start)..again.end.min(groups.end); + self.refused.insert(again, err.clone()); + } + continue; + } + self.refused.insert(groups, err.clone()); + } + } + + /// A fresh subscription may deliver what the last one refused. + pub fn forget_refused(&mut self) { + self.refused = Default::default(); + } + + /// Settle each held fill against what the subscription refused, open a [`Recovery`] for + /// each run it will not deliver, and recompute what to ask. `accounted` is the tail of + /// the subscription the fills ride, if one is open. Returns whether the ask changed. + pub fn poll( + &mut self, + serve: &TrackServe, + serving: &track::Producer, + accounted: Option<&kio::Producer>, + waiter: &kio::Waiter, + ) -> bool { + // The groups the subscription already accounted for, which it never sends again, are + // recovered on their own, once. + if let Some(accounted) = accounted { + let tail = accounted.read(); + let mut asked = RangeSet::new(); + for fill in &self.held { + if let Some(run) = fill.next_run() { + asked.insert(run); + } + } + for run in asked.iter() { + for gone in tail.accounted(run.clone()) { + for gap in self.recovering.gaps(&gone).collect::>() { + for again in self.recovered.overlapping(&gap).cloned().collect::>() { + let again = again.start.max(gap.start)..again.end.min(gap.end); + if !self.refused.contains_key(&again.start) { + self.refused.insert(again, StreamError::NotFound); + } + } + for fresh in self.recovered.gaps(&gap).collect::>() { + if !self.refused.contains_key(&fresh.start) { + self.recover.insert(fresh); + } + } + } + } + } + } + // Each stale run goes out on its own subscription with no max delay, once. + for run in std::mem::take(&mut self.recover).iter() { + self.recovered.insert(run.clone()); + self.recovering.insert(run.clone()); + self.recoveries + .push(Recovery::new(serve, serving, run.clone(), &self.ask)); + } + // What a finished recovery did not deliver stands refused. + let mut done = Vec::new(); + self.recoveries.retain_mut(|recovery| match recovery.poll(waiter) { + Poll::Ready(refused) => { + done.push((recovery.groups.clone(), refused)); + false + } + Poll::Pending => true, + }); + for (groups, refused) in done { + self.recovering.remove(groups); + for (groups, err) in refused.iter() { + self.refused.insert(groups.clone(), err.clone()); + } + } + + let mut ask = FillAsk::default(); + let refused = &self.refused; + let recovering = &self.recovering; + self.held.retain_mut(|fill| { + if fill.poll_withdrawn(waiter).is_ready() { + return false; + } + // Arms every input of the owed groups: readers, answers, and the cache. + let _ = fill.poll_wanted_changed(waiter); + loop { + let Some(run) = fill.next_run() else { + return false; + }; + // `next_run` extends from the group owed first, which the subscription may have + // refused already. Refusing the run answers it, so the loop moves on. + let first = match fill.order() { + Order::Ascending => run.start, + Order::Descending => run.end - 1, + }; + if let Some((groups, err)) = refused.get_key_value(&first) { + let groups = run.start.max(groups.start)..run.end.min(groups.end); + refuse_fill(fill, groups, err.clone()); + continue; + } + // A run being recovered waits on its own subscription. + for gap in recovering.gaps(&run) { + ask.groups.insert(gap); + } + ask.add(fill); + return true; + } + }); + + // A run stays asked whole while a fill still owes any of it, so the subscription + // changes once as a run is asked and once as it is done, and the streams of its + // answered groups keep arriving. One no fill owes stays while any of its streams is + // still being read, each group searched for once. + self.reading.retain(|_, group| group.poll_finished(waiter).is_pending()); + let edge = serving.consume(); + let mut groups = ask.groups.clone(); + let mut draining = RangeSet::new(); + for run in self.ask.groups.iter() { + if ask.groups.overlaps(run) { + groups.insert(run.clone()); + continue; + } + for gap in self.draining.gaps(run) { + for sequence in gap { + if let Some(mut group) = edge.peek_group(sequence) + && group.poll_finished(waiter).is_pending() + { + self.reading.insert(sequence, group); + } + } + } + if self.reading.range(run.clone()).next().is_some() { + groups.insert(run.clone()); + draining.insert(run.clone()); + } + } + self.reading.retain(|sequence, _| draining.contains(sequence)); + self.draining = draining; + // What finishes a stream still asks with what its fills asked. + if self.held.is_empty() && !groups.is_empty() { + ask.priority = self.ask.priority; + ask.max_delay = self.ask.max_delay; + ask.order = self.ask.order; + } + ask.groups = groups; + if ask == self.ask { + return false; + } + self.ask = ask; + true + } +} + +/// Answer `groups` of `fill` as the subscription that asked for them will not deliver them, +/// with why: a miss names them dropped, as a FETCH's miss does. +fn refuse_fill(fill: &track::Fill, groups: Range, err: StreamError) { + match err { + StreamError::NotFound => fill.drop_groups(groups), + StreamError::Old => fill.reject_groups(groups, Error::Old), + err => fill.refuse_groups(groups, Error::Stream(err)), + } +} + +/// A subscription asking again, with no max delay, for groups another one dropped as stale, +/// which a fill still owes. It lives until each group arrives and its stream is read, or the +/// publisher gives it up, then FINs and waits for the publisher's FIN in reply. +struct Recovery { + serve: TrackServe, + id: u64, + groups: Range, + state: RecoveryState, + /// The groups this subscription said it will not deliver, and why. + refused: RangeMap, + /// Whether our FIN went out. + finished: bool, +} + +enum RecoveryState { + Establish(Box), + Active(Box), + /// The subscription failed to open or was reset: everything left is refused. + Done, +} + +impl Recovery { + fn new(serve: &TrackServe, serving: &track::Producer, groups: Range, ask: &FillAsk) -> Self { + let subscription = Subscription::default() + .with_ranges([track::Range::groups(groups.clone())]) + .with_priority(ask.priority) + .with_delay(Delay::default().with_max(None)) + .with_order(Order::Ascending); + let ranges = vec![lite::Range { + start: Some(lite::Anchor::Absolute(Position::group(groups.start))), + end: Some(lite::Anchor::Absolute(Position::group(groups.end))), + }]; + let copy = super::Accepted { + producer: serving.clone(), + framing: lite::Framing::new(serving.info(), serve.subscriber.version), + }; + tracing::debug!(track = %serve.name, ?groups, "recovering stale groups"); + let establish = serve.register(subscription, ranges, Some(copy)); + Self { + serve: serve.clone(), + id: establish.id, + groups, + state: RecoveryState::Establish(Box::new(establish)), + refused: Default::default(), + finished: false, + } + } + + /// End the subscription, refusing what is left of the groups with `err`, as nothing more + /// will arrive on it. + fn end(&mut self, err: StreamError) { + let state = std::mem::replace(&mut self.state, RecoveryState::Done); + let mut owed = RangeSet::new(); + owed.insert(self.groups.clone()); + if let RecoveryState::Active(active) = &state { + let tail = active.tail.read(); + for sequence in self.groups.clone() { + if tail.covers(sequence..sequence + 1) { + owed.remove(sequence..sequence + 1); + } + } + } + for (refused, _) in self.refused.iter() { + owed.remove(refused.clone()); + } + for gap in owed.iter() { + self.refused.insert(gap.clone(), err.clone()); + } + self.serve.subscriber.remove_subscribe(self.id); + match state { + RecoveryState::Establish(est) => est.abort(&Error::Cancel), + RecoveryState::Active(active) if !self.finished => active.stream.writer.abort(&Error::Cancel), + _ => {} + } + } + + /// Ready once the recovery is over, with the groups it refused. + fn poll(&mut self, waiter: &kio::Waiter) -> Poll> { + loop { + match &mut self.state { + RecoveryState::Establish(est) => match ready!(est.poll(waiter)) { + Ok(active) => self.state = RecoveryState::Active(Box::new(active)), + Err(err) => { + tracing::debug!(%err, "recovery subscribe failed"); + self.end(StreamError::Cancel); + } + }, + RecoveryState::Active(active) => { + // FIN once every group is accounted for and read, as a subscriber does once + // its tail settles, and the publisher FINs in reply. + if !self.finished { + let groups = self.groups.clone(); + let settled = active + .tail + .consume() + .poll(waiter, |tail| match tail.covers(groups.clone()) && !tail.is_reading() { + true => Poll::Ready(()), + false => Poll::Pending, + }) + .is_ready(); + if settled { + let _ = active.stream.writer.finish(); + self.finished = true; + } + } + if let Poll::Ready(Err(err)) = active.stream.writer.poll_flush(waiter) { + tracing::debug!(%err, "recovery subscribe write failed"); + } + let res = ready!( + active + .stream + .reader + .poll_decode_maybe::(waiter) + ); + let now = self.serve.subscriber.runtime.now(); + let version = self.serve.subscriber.version; + let groups = self.groups.clone(); + // What the publisher gives up of the groups, accounted for in the tail. + let mut given_up = |given: Range, err: StreamError| { + if let Ok(mut tail) = active.tail.write() { + tail.account(given.clone(), now); + } + let given = given.start.max(groups.start)..given.end.min(groups.end); + if !given.is_empty() { + self.refused.insert(given, err); + } + }; + match res { + Ok(Some(lite::SubscribeResponse::Start(start))) => { + // Below its start the publisher delivers nothing. + let from = start.starts.first().copied().flatten().unwrap_or(groups.end); + given_up( + groups.start..from.clamp(groups.start, groups.end), + StreamError::NotFound, + ); + } + Ok(Some(lite::SubscribeResponse::Drop(dropped))) => { + given_up(dropped.start..dropped.end.saturating_add(1), dropped.error(version)); + } + // Nothing at or past the track's end will be produced. + Ok(Some(lite::SubscribeResponse::Finish(finish))) => { + given_up(finish.group.max(groups.start)..groups.end, StreamError::NotFound); + } + Ok(Some(msg)) => { + tracing::warn!(?msg, "unexpected recovery subscribe response"); + self.end(StreamError::Cancel); + } + Ok(None) => self.end(StreamError::NotFound), + Err(err) => { + tracing::debug!(%err, "recovery subscribe ended"); + self.end(StreamError::Cancel); + } + } + } + RecoveryState::Done => return Poll::Ready(std::mem::take(&mut self.refused)), + } + } + } +} + +impl Drop for Recovery { + fn drop(&mut self) { + if !matches!(self.state, RecoveryState::Done) { + self.end(StreamError::Cancel); + } + } +} diff --git a/rs/moq-net/src/tail.rs b/rs/moq-net/src/tail.rs index 4eeb6532792..05f2f1600cc 100644 --- a/rs/moq-net/src/tail.rs +++ b/rs/moq-net/src/tail.rs @@ -86,6 +86,11 @@ impl Tail { self.ledger.covers(groups) } + /// See [`Ledger::accounted`]. + pub fn accounted(&self, groups: Range) -> impl Iterator> + '_ { + self.ledger.accounted(groups) + } + /// See [`Ledger::folds_at`]. #[cfg(test)] pub fn folds_at(&self, group: u64) -> Option { diff --git a/rs/moq-net/tests/integration/widened_max_age.rs b/rs/moq-net/tests/integration/widened_max_age.rs index def074c3d6e..b21e8b9273e 100644 --- a/rs/moq-net/tests/integration/widened_max_age.rs +++ b/rs/moq-net/tests/integration/widened_max_age.rs @@ -31,8 +31,8 @@ const WIDE: Duration = Duration::from_millis(6500); /// start at the live edge, below which a reader fills only the floor its ranges name. const VERSIONS: &[&str] = &["moq-lite-06", "moq-lite-07-wip"]; -/// The wire for the tests on one version: only lite-07 FETCHes below the start, so only it -/// can pull a group twice. +/// The wire for the tests on one version: only lite-07 fills below the start, so only it can +/// pull a group twice. const VERSION: &str = "moq-lite-07-wip"; fn origin(hop: u64) -> origin::Producer { @@ -159,8 +159,9 @@ async fn a_fresh_reader_fills_nothing_below_its_start() { } /// Tighten while no start is selectable, then ask for a budget between the old and -/// selected ones. Hold the publisher's driver so only an actual fill request, rather -/// than its live re-offer, can prove the copy left the history below its start fillable. +/// selected ones: the copy leaves the history below its start fillable rather than giving +/// it up. Lite-07 asks for it as a range on the subscription the publisher's re-offer +/// serves too, so only the groups the reader gets tell the two apart from a give-up. #[kio::test] async fn a_tightened_start_leaves_history_fillable_by_an_intermediate_reader() { for relay in [false, true] { @@ -180,7 +181,6 @@ async fn a_tightened_start_leaves_history_fillable_by_an_intermediate_reader() { assert_eq!(received(&mut first).await, (6..GROUPS).collect()); *chain.source_gate.write().ok().unwrap() = false; - let before = chain.fetches(); let mut intermediate = chain.subscribe(Duration::from_millis(4500)).await; // Drive the reader's miss walk too, with the upstream deliberately parked. let reading = kio::clock::sim::spawn(async move { @@ -190,13 +190,12 @@ async fn a_tightened_start_leaves_history_fillable_by_an_intermediate_reader() { } got }); - let upstream = &chain.links[0].client_transport; - kio::clock::timeout(TIMEOUT, upstream.bidi_count(3, before + 1)) + *chain.source_gate.write().ok().unwrap() = true; + let got = kio::clock::timeout(TIMEOUT, reading) .await .expect("the history below the start is fillable"); - *chain.source_gate.write().ok().unwrap() = true; - // This reader can also receive the feed's re-offer once the source resumes. - assert_eq!(reading.await.unwrap(), (4..GROUPS).collect(), "relay={relay}"); + assert_eq!(got.unwrap(), (4..GROUPS).collect(), "relay={relay}"); + assert_eq!(chain.fetches(), 0, "relay={relay}: the history rides the subscription"); } } From 26ccfcae1512e250aaa1e11ac76f8527e5c4660b Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 4 Nov 2026 07:26:31 -0800 Subject: [PATCH 17/30] feat(net): test lite-07 range fills and recovery, describe them in the draft, finish the quest Co-Authored-By: Claude Opus 5.5 --- drafts/draft-lcurley-moq-lite.md | 5 + quest/m1/lite07-finalize.md | 3 +- quest/m1/subscribe-ranges/README.md | 1 - quest/m1/subscribe-ranges/lite.md | 238 ------------------ quest/m1/subscribe-ranges/lite07-fetch.md | 9 +- .../m1/subscribe-ranges/start-bookkeeping.md | 4 - quest/m1/time-addressing/model.md | 3 +- rs/moq-net/src/lite/subscriber/fills.rs | 28 +++ rs/moq-net/tests/integration/main.rs | 1 + rs/moq-net/tests/integration/range_fill.rs | 158 ++++++++++++ 10 files changed, 197 insertions(+), 253 deletions(-) delete mode 100644 quest/m1/subscribe-ranges/lite.md create mode 100644 rs/moq-net/tests/integration/range_fill.rs diff --git a/drafts/draft-lcurley-moq-lite.md b/drafts/draft-lcurley-moq-lite.md index 4e6a73a8f17..029f98648a6 100644 --- a/drafts/draft-lcurley-moq-lite.md +++ b/drafts/draft-lcurley-moq-lite.md @@ -519,6 +519,10 @@ The subscriber judges a Range complete from what it observed: the Groups it rece Drops and Groups name absolute sequences, so the subscriber judges them against its latest Ranges, and an update still in flight changes nothing it has counted. The publisher never closes its side first: a subscription whose every Range has an `End` other than None and whose every Group has been delivered or dropped stays open for an update to widen it. +A subscriber fills Groups it misses by asking for them as Ranges, adding them to an open subscription with SUBSCRIBE_UPDATE or opening a bounded SUBSCRIBE when none is open, rather than with a FETCH of each Group; FETCH remains for one Group, or a slice of one. +A publisher never sends a Group twice on one subscription, so a Group the subscription already delivered or named in SUBSCRIBE_DROP is asked for with a SUBSCRIBE of its own. +The subscriber asks that way, once, for a Group a subscription dropped with OLD that it still wants, with a `Subscriber Max Age` its age does not reach, and never for a Group that may still arrive on the existing subscription. + The subscriber closes its side (FIN) once it wants nothing more from the subscription, and the publisher then closes its side (FIN), cancelling any Group Stream still open. After SUBSCRIBE_FINISH, the subscriber SHOULD close its side once its tail accounting has settled: every received Group Stream has been read to its FIN, reset, or dropped, and every sequence in range has arrived, been named by SUBSCRIBE_DROP, or exhausted the subscriber's grace period. A publisher closing its session gracefully MUST wait for the subscriber's FIN or reset on every Subscribe Stream before closing the connection; a transport acknowledgement alone does not confirm the subscriber read the tail. @@ -1989,6 +1993,7 @@ The `Message Length` describes the payload size on the wire. - Replaced SUBSCRIBE_OK `Group` with a `Range Start` per Range: the Group where that Range's delivery starts, including at a Relative or Time Anchor, from which its Groups are owed. - Each Range takes an implicit Range ID, and SUBSCRIBE_UPDATE edits the live Ranges instead of restating them: `Remove Count` Range IDs to retire, then the Ranges to add. The live Ranges must stay non-empty and fit in one SUBSCRIBE. A Range resolves its Anchors once, however many updates leave it live. - SUBSCRIBE_OK's Range Starts answer the SUBSCRIBE's Ranges, and added SUBSCRIBE_STARTED (type 0x4) resolving where each Range an update adds starts. +- A subscriber fills missing Groups through Ranges on a subscription instead of a FETCH per Group, and asks for a Group a subscription already delivered or dropped, including one dropped with OLD, with a SUBSCRIBE of its own. - Added `Largest Group` and `Largest Frame` to SUBSCRIBE_OK: the publisher's largest position when it answers, which a subscriber takes as where the live feed is. A publisher MUST answer at once when the requested start is past it. - Renamed SUBSCRIBE_END to SUBSCRIBE_FINISH, matching TRACKS_FINISH: it ends the Track, not the subscription. - Added `Error Code` to SUBSCRIBE_FINISH: a track can end with an error while keeping the groups below its end. diff --git a/quest/m1/lite07-finalize.md b/quest/m1/lite07-finalize.md index c3900761b4f..494ccf12de6 100644 --- a/quest/m1/lite07-finalize.md +++ b/quest/m1/lite07-finalize.md @@ -81,7 +81,7 @@ The `Time(pts)` range end rides in the subscribe-ranges children already Require Decided 2026-10-27 (maintainer): of [Subscribe ranges](/quest/m1/subscribe-ranges/README.md), only the children that change the lite-07 wire gate the cut: -[Lite-07 ranges](/quest/m1/subscribe-ranges/lite.md), +Lite-07 ranges ([moq-dev/moq#7872](https://github.com/moq-dev/moq/pull/7872)), JS ranges ([moq-dev/moq#7696](https://github.com/moq-dev/moq/pull/7696)), and [Lite-07 FETCH is single-group only](/quest/m1/subscribe-ranges/lite07-fetch.md). The fill path, fetch as a range, and the start bookkeeping change no wire. @@ -108,7 +108,6 @@ timestamp bound in [#6168](https://github.com/moq-dev/moq/pull/6168). - [Routes and announces](/quest/m1/cluster-routing/routes/README.md) - lite-07 carries the route layer: ROUTE per origin node and path-less ANNOUNCE, with the hop list gone - [Drop set](/quest/m1/drop-set.md) - lite-07's SUBSCRIBE_DROP carries `Max Age`, so a widening re-offers groups reported dropped -- [Lite-07 ranges](/quest/m1/subscribe-ranges/lite.md) - a relay fills a lite-07 upstream through the subscription - [Lite-07 FETCH is single-group only](/quest/m1/subscribe-ranges/lite07-fetch.md) - lite-07 FETCH reads one group or a slice of one, and SUBSCRIBE ranges of whole groups - [Lite-07 by start](/quest/m1/time-addressing/lite07.md) - lite-07 carries group starts, previous-start links, GROUP_END, and time bounds instead of sequence numbers - [@moq/net by start](/quest/m1/time-addressing/js.md) - JS speaks the same lite-07 wire diff --git a/quest/m1/subscribe-ranges/README.md b/quest/m1/subscribe-ranges/README.md index 7469ce1dde9..826e858171b 100644 --- a/quest/m1/subscribe-ranges/README.md +++ b/quest/m1/subscribe-ranges/README.md @@ -98,7 +98,6 @@ every sequence delivered or dropped. ## Required -- [Lite-07 ranges](/quest/m1/subscribe-ranges/lite.md) - the lite-07 wire, Rust publisher and subscriber, and the draft - [JS received copies fill what they missed](/quest/m1/subscribe-ranges/js-fills.md) - a JS subscriber session asks upstream for its copy's misses, with Rust's below-start and head-fill rules - [Lite-07 FETCH is single-group only](/quest/m1/subscribe-ranges/lite07-fetch.md) - lite-07 FETCH reads one group or a slice of one, and SUBSCRIBE ranges of whole groups, in Rust, JS, and the draft - [Delete the old start bookkeeping](/quest/m1/subscribe-ranges/start-bookkeeping.md) - what each lite cursor is owed and dropped lives in the model, and `TrackRun` only translates it per version diff --git a/quest/m1/subscribe-ranges/lite.md b/quest/m1/subscribe-ranges/lite.md deleted file mode 100644 index 5ed3658d37a..00000000000 --- a/quest/m1/subscribe-ranges/lite.md +++ /dev/null @@ -1,238 +0,0 @@ -# [L] Lite-07 ranges - -## Goal - -Lite-07 ranges carry implicit Range IDs, so a range a SUBSCRIBE_UPDATE adds -learns where it started. A relay fills a lite-07 upstream by subscribing to the -missing ranges, and recovers a dropped group with a one-group SUBSCRIBE. Lite-07 -FETCH stays, for one group or a slice within one group -([Lite-07 FETCH is single-group only](/quest/m1/subscribe-ranges/lite07-fetch.md)). - -## Plan - -Decided 2026-10-25 (maintainer, on -[moq-dev/moq#6807](https://github.com/moq-dev/moq/pull/6807)): FETCH removal -moves out of this quest (to its own quest since 2026-10-27), since JS still requests and serves wire FETCH on lite-07 -(`track.Consumer.fetchGroup`), so a Rust-only removal would drop a current -cross-language capability. Since 2026-11-03 FETCH stays on lite-07 for one -group or a slice within one group, and this quest fills multi-group ranges -through the subscription. - -Decided 2026-11-01 (maintainer, on -[moq-dev/moq#7487](https://github.com/moq-dev/moq/pull/7487)): this quest -waited for Fetch as a range's miss judging (a miss with no feed frontier, and -holes below where a live feed started) and its cached backfill rule. Without -them a range fill is a functional regression from FETCH, which answers a miss -at once. Both landed in -[moq-dev/moq#7545](https://github.com/moq-dev/moq/pull/7545) on 2026-11-02. -Fetch as a range finished in -[moq-dev/moq#7625](https://github.com/moq-dev/moq/pull/7625), which kept -`track::Consumer::fetch_group` public as the single-group fetch and moved its -remaining lite-07 work here (maintainer, 2026-11-02): the explicit end, the -back-to-back hang, and the one-group recovery below. - -The explicit end landed in -[moq-dev/moq#7675](https://github.com/moq-dev/moq/pull/7675): SUBSCRIBE_END is -renamed SUBSCRIBE_FINISH, and on lite-07 neither it nor a covered bounded -subscription closes the Subscribe Stream. The publisher keeps reading updates -and FINs only in reply to the subscriber's FIN. Once no group can resolve a -held start (every group of a bounded subscription given up, or the track -finished), it sends SUBSCRIBE_OK and then the drops it held. The Rust copy -starts its tail at SUBSCRIBE_FINISH, waits for SUBSCRIBE_OK before judging it, -and FINs once it settles. That fixed back-to-back one-range reads -(`range_read::one_range_reads_back_to_back_each_complete`), and the Rust -copy's covered-FIN path (`SubStream::covered`, `ServeLoop::covered`) is gone. -Datagrams take no ranges, since they become their own track kind (maintainer, -2026-11-01; the datagram-tracks plan, -[moq-dev/moq#7575](https://github.com/moq-dev/moq/pull/7575)). The same PR -brought the JS lite-07 publisher and subscriber to the explicit end, so the two -implementations share the wire; JS still names it SUBSCRIBE_END in code. - -Decided 2026-11-03 (maintainer, on -[moq-dev/moq#7675](https://github.com/moq-dev/moq/pull/7675)), answering the -first attempt's open questions: - -1. **Land part 1:** ✅ land it now. Options: ✅ land now; keep a draft. -2. **Fill budget:** ✅ the live demand's budget. Groups that budget drops - count as officially dropped, so the one-group SUBSCRIBE recovery (no max - delay) brings them back, and the live cap never loosens. Options: ✅ live - demand's budget; loosest budget; per-range wire budget. -3. **Partial-group fills:** "we can keep FETCH then but for a single group (or - slice within a single group). SUBSCRIBE is for multiple groups and does not - support slicing within a group." A fill starting past frame 0, such as route - recovery's (`resume::Recover`), keeps its FETCH. See - [Lite-07 FETCH is single-group only](/quest/m1/subscribe-ranges/lite07-fetch.md). -4. **Datagram-only groups:** ✅ wait for datagram tracks - ([Datagram tracks](/quest/m1/datagram-tracks/README.md), planned in - [moq-dev/moq#7575](https://github.com/moq-dev/moq/pull/7575)); after them - ranges never carry datagrams. Options: ✅ wait for datagram tracks; - NOT_FETCHABLE drops now; keep FETCH for these. -5. **Range IDs:** ✅ implicit range IDs now, using - [moq-dev/moq#7207](https://github.com/moq-dev/moq/pull/7207)'s encoding, so - a range added by SUBSCRIBE_UPDATE learns where it started. Options: - absolute-only fills; ✅ implicit range IDs now. - -Range IDs are this quest's next step. They did not fit #7675 as a checkpoint -addition: the encoding changes SUBSCRIBE_UPDATE in the Rust and JS codecs, -publishers, and subscribers together (#7207 was 1,420 lines across both and -now conflicts with main), and it needs a start report #7207 never defined. -What remains is Range IDs, then the range fill and the one-group recovery. - -### Range IDs - -[moq-dev/moq#7207](https://github.com/moq-dev/moq/pull/7207) is the reference: -each Range implicitly takes the next Range ID on the stream, from 0, and -SUBSCRIBE_UPDATE edits the live Ranges rather than restating them, with -`Remove Count` Range IDs to retire and then the Ranges to add. A Range ID never -assigned or already retired is a PROTOCOL_VIOLATION, and the live Ranges must -stay non-empty and fit in one SUBSCRIBE. SUBSCRIBE_OK's Range Starts answer the -Ranges live when it is sent, in Range ID order. A Range an update adds after -SUBSCRIBE_OK has its start reported by SUBSCRIBE_STARTED, below. Rust and JS -change together, with draft text and codec, publisher, and subscriber tests. - -Decided 2026-11-03 (maintainer, on -[moq-dev/moq#7675](https://github.com/moq-dev/moq/pull/7675)): ✅ a new -**SUBSCRIBE_STARTED** message (type 0x3) carrying `Range ID` and -`Range Start`, sent when an added Range's start resolves. Options: ✅ new -SUBSCRIBE_STARTED; repeat SUBSCRIBE_OK. `Range Start` is encoded as in -SUBSCRIBE_OK (the Group plus one, 0 for none), and the subscriber owes the -added Range's groups from it as it does a SUBSCRIBE_OK Range Start. - -### Range fill through the subscription - -Lite-07 SUBSCRIBE and SUBSCRIBE_UPDATE carry the range list (`lite::Range`, -typed anchors), and the publisher serves it from the model. Older wires map at -the codec, and a merged request goes upstream as one span, from group 0 when -its starts mix (`wire_span`). A covered bounded subscription stays open, so a -fill joins the live subscription's ranges with a SUBSCRIBE_UPDATE. - -The first attempt -([moq-dev/moq#7487](https://github.com/moq-dev/moq/pull/7487), WIP commit -[b7f573d](https://github.com/moq-dev/moq/commit/b7f573d755ae53e6f2bdb94cc480ae47fd6ba254)) built this in -`rs/moq-net/src/lite/subscriber.rs` and was moved back out until the -requirement landed. Reuse its design: - -- A `RangeFills` set holds each fill and asks for its next owed run - (`Fill::next_run`), added as an absolute bounded range after the demand's - ranges on the upstream SUBSCRIBE or SUBSCRIBE_UPDATE. With no demand open, - a bounded SUBSCRIBE asks for the fills alone. A fill answers itself from the - cache as its groups arrive. -- A fill is judged only against the subscription that asked for it, not the - track's arrivals ledger, which still holds a group an earlier subscription - cut. A SUBSCRIBE_DROP over its groups, or a SUBSCRIBE_OK Range Start past - its range's start, refuses it, so a cut group is filled whole. The WIP's - covered-FIN refusal has no counterpart now: the publisher drops what it - gives up instead. -- A run stays on the wire while a fill owes part of it, then until each of its - streams still being read finishes, so narrowing never resets a group in - flight. -- Only the demand's ranges set the floor and the live state. A subscription - carrying only fills sets neither, and canceling it does not idle the copy. - If the demand leaves while fills are out, the subscription keeps the - demand's ranges until they are answered. -- A NotFound drop names the fill's groups dropped - (`Fill::drop_groups`), as a FETCH miss does; Old rejects them, and any - other error refuses only that request. - -Its integration tests (`range_fill.rs`: a miss subscribes and sends no FETCH, -a miss rides the live subscription then retires its range, a group the -publisher never had becomes a miss after the grace, a cut group is filled -whole) passed. These failed, and must pass on #7545's miss judging: - -- On-demand `requests()` tracks with no live feed, such as moq-archive's, never - ask a fill and park (`untimed::a_fetched_untimed_frame_arrives_untimed`, - `fetch_cancel::a_refetch_within_the_linger_stays_on_the_relay_lite07`, - `subscription_end_integrity::a_session_close_cancels_a_pending_fetch_from_either_side`). - #7545's miss with no feed frontier covers them. -- A missing or expired group fetched with no budget parks where FETCH answered - NotFound at once (`rejoin::{rejoined,taken_over}_quiet_catalog_survives_cache_expiry`). - #7545's holes below where a live feed started cover them. -- A relay re-asks upstream for groups it cached, since its covered memory - leaves with the downstream demand - (`fetch_fanout::sequential_viewers_share_the_relays_fetched_groups`, two - upstream SUBSCRIBEs where FETCH needed one). #7545's cached - backfill rule covers it: a relay leaves cached groups out of a bounded - upstream range. On lite-07 the test counts SUBSCRIBE streams, not FETCH. - -Open questions the attempt raised, now decided (2026-11-03, above), kept for -their detail: - -- **Datagram sequences.** Settled: datagrams are their own track kind, which - takes no ranges, and a stream track never holds one (maintainer, 2026-11-01; - NOT_FETCHABLE is gone). -- **Fills from partway through a group.** Route recovery (`resume::Recover`) - fills from frame N, which a whole-group range cannot say. Recommended: keep - FETCH on lite-07 for a fill whose first group starts past frame 0, as - `resume::Recover` does today (`group::Fetch::frame_start`). The alternative asks - for the whole group and drops the frames the reader already has. -- **One budget caps every range.** Merging the fills into the upstream - Subscriber Max Age takes the most tolerant budget, which loosens the live - feed's cap while a fill is out - (`route_change::resume_delivers_the_outage_tail_within_the_budget` delivers - a stale group 5). Recommended: settle the partial-group question first, - since the failing fill is a recovery fill, and rerun the test. The - alternatives fill under the demand's budget, refusing older groups as Old, - or add a per-range budget to the wire. Since the one-group recovery below - was decided (2026-11-02), filling under the demand's budget composes with - it: a group the live budget drops as OLD is officially dropped, so a - one-group SUBSCRIBE with no max delay recovers it, and the live cap never - loosens. Decided that way on 2026-11-03. -- **A range added after SUBSCRIBE_OK has no reported start.** SUBSCRIBE_OK's - Range Starts are positional and sent once, yet the subscriber judges a - bounded range complete from them, on a stream that stays open for later - updates. So a Relative or Time range a SUBSCRIBE_UPDATE adds never learns - where it started. Raised by the closed Range ID prototype - ([moq-dev/moq#7207](https://github.com/moq-dev/moq/pull/7207)), whose - recommendation was to decide here whether ranges get implicit IDs so a later - message can report a start. SUBSCRIBE_UPDATE keeps restating the whole list - either way: each range is a few bytes, and restating stays stateless. IDs - were adopted on 2026-11-03, with #7207's encoding, which edits rather than - restates. -- `auth::lite_07_narrowing_resets_a_fetch_in_flight` read `Stream(Cancel)` - instead of `Unauthorized`/`Unroutable` when the grant narrowed mid-group; - the refusal must carry the grant's error. - -The Rust copy sends a relative or time end as open, since nothing names a -resolved end and it resolves the end itself (`wire_ranges`), so only absolute -bounded ranges complete upstream. A hole on a live track counts as dropped -once the feed gives up on it, past the arrival grace (#7545's bounded wait, -`TrackState::miss_grace`). - -### Recovering a dropped group - -Decided 2026-11-02 (maintainer, on -[moq-dev/moq#7625](https://github.com/moq-dev/moq/pull/7625)): "lite-07 can -issue a one group SUBSCRIBE as a FETCH replacement (no max-delay) if an -existing SUBSCRIBE had it dropped", "and only when the group is officially -marked DROP so we don't receive two copies of it". A group a reader still -wants, once an existing subscription names it in SUBSCRIBE_DROP, is recovered -by a new SUBSCRIBE covering only that group with no max delay, never on a -guess, a timeout, or while the group may still arrive. This is lite-07's -replacement for a FETCH of a dropped group wider than one group's slice, and -with the fill budget decided 2026-11-03 it also recovers what the live -budget drops. Tests: a -group still in flight on the existing subscription sends no second SUBSCRIBE, -and a dropped group is recovered exactly once. - -### JavaScript names the explicit end - -Moved here from JS ranges on 2026-11-03 (maintainer, on -[moq-dev/moq#7696](https://github.com/moq-dev/moq/pull/7696)), which finished -that quest: the SUBSCRIBE_FINISH JS work belongs with -[moq-dev/moq#7675](https://github.com/moq-dev/moq/pull/7675), which brought the -JS lite-07 publisher and subscriber to the explicit end. What it left: rename -SUBSCRIBE_END to SUBSCRIBE_FINISH in `js/net`, as Rust and the draft did, and -delete the JS subscriber's covered-FIN path (`#parkCovered`, `#demandCovered`, -the `short` reopen), which no lite-07 publisher triggers any more. - -### Draft and checks - -Update `drafts/draft-lcurley-moq-lite.md` with each wire change here, -including that a subscriber fills missing groups through its ranges, and -recovers a dropped group with a one-group SUBSCRIBE. Run -`just drafts check` and `just test interop --all`, and the `moq-net-tests` -`restart::retired_fetch_lite07` and `moq-relay` suites. - -## Related - -- [Lite-07 FETCH is single-group only](/quest/m1/subscribe-ranges/lite07-fetch.md) - lite-07 FETCH reads one group or a slice of one, once this and JS ranges land diff --git a/quest/m1/subscribe-ranges/lite07-fetch.md b/quest/m1/subscribe-ranges/lite07-fetch.md index 3c45c1fc960..152894d2153 100644 --- a/quest/m1/subscribe-ranges/lite07-fetch.md +++ b/quest/m1/subscribe-ranges/lite07-fetch.md @@ -29,15 +29,12 @@ on lite-07 (`track.Consumer.fetchGroup` in JS). SUBSCRIBE may start at any frame. A read starting partway through a group goes through FETCH instead. - Rust and JS send FETCH on lite-07 only for one group or a slice of one - group; a fill of more than one group goes through - [Lite-07 ranges](/quest/m1/subscribe-ranges/lite.md)' subscription fill. + group; a fill of more than one group goes through the subscription, as the + Rust copy does since + [moq-dev/moq#7872](https://github.com/moq-dev/moq/pull/7872). - `drafts/draft-lcurley-moq-lite.md` states the split in its Fetch and Subscribe sections. Run `just drafts check`. - Run `just test interop --all`. Public API: none expected. Wire: lite-07 (unpublished) SUBSCRIBE anchors lose their frame. - -## Required - -- [Lite-07 ranges](/quest/m1/subscribe-ranges/lite.md) - Rust fills multi-group ranges through the subscription diff --git a/quest/m1/subscribe-ranges/start-bookkeeping.md b/quest/m1/subscribe-ranges/start-bookkeeping.md index fb9dbc8792c..599ecb645ef 100644 --- a/quest/m1/subscribe-ranges/start-bookkeeping.md +++ b/quest/m1/subscribe-ranges/start-bookkeeping.md @@ -38,7 +38,3 @@ generations of "start". Public API: none expected. Wire: lite-07 (unpublished) may send a START per update; published versions are unchanged. - -## Required - -- [Lite-07 ranges](/quest/m1/subscribe-ranges/lite.md) - the lite-07 range fill this bookkeeping is rebuilt around diff --git a/quest/m1/time-addressing/model.md b/quest/m1/time-addressing/model.md index cfec36cb232..0987f1e420c 100644 --- a/quest/m1/time-addressing/model.md +++ b/quest/m1/time-addressing/model.md @@ -54,7 +54,7 @@ a producer would need per-track memory of its last number. - **What it waits on** (decided 2026-10-27, maintainer; Fetch as a range finished in [moq-dev/moq#7625](https://github.com/moq-dev/moq/pull/7625)): only the subscribe-ranges children that put sequence ranges into the model and the - wire, [Lite-07 ranges](/quest/m1/subscribe-ranges/lite.md) and + wire, Lite-07 ranges ([moq-dev/moq#7872](https://github.com/moq-dev/moq/pull/7872)) and [Delete the old start bookkeeping](/quest/m1/subscribe-ranges/start-bookkeeping.md), not the whole epic. [Drop set](/quest/m1/drop-set.md) lands first too: its runs keyed by group sequence convert mechanically to starts. @@ -164,6 +164,5 @@ Wire: no format change; legacy and lite-07-wip group IDs become start ticks. ## Required -- [Lite-07 ranges](/quest/m1/subscribe-ranges/lite.md) - the range ends and drops this rework moves onto times - [Delete the old start bookkeeping](/quest/m1/subscribe-ranges/start-bookkeeping.md) - the lite cursor state lives in the model before the cut rekeys it - [Drop set](/quest/m1/drop-set.md) - its dropped and expired runs convert mechanically from sequences to starts diff --git a/rs/moq-net/src/lite/subscriber/fills.rs b/rs/moq-net/src/lite/subscriber/fills.rs index 10a30c4c2aa..52bc7ba8e33 100644 --- a/rs/moq-net/src/lite/subscriber/fills.rs +++ b/rs/moq-net/src/lite/subscriber/fills.rs @@ -474,3 +474,31 @@ impl Drop for Recovery { } } } + +#[cfg(test)] +mod tests { + use super::*; + + /// A stale drop of groups a fill asked for is recovered once; a second one stands, and a + /// drop of groups no fill asked for refuses nothing. + #[test] + fn a_stale_drop_is_recovered_once() { + let mut fills = RangeFills::default(); + fills.ask.groups.insert(2..6); + fills.refuse(3..5, StreamError::Old); + assert_eq!(fills.recover.iter().cloned().collect::>(), [3..5]); + assert!(fills.refused.is_empty(), "recovered, not refused"); + + // Once recovered, the same groups dropped stale again stay dropped. + fills.recovered.insert(3..5); + fills.recover = Default::default(); + fills.refuse(2..5, StreamError::Old); + assert_eq!(fills.recover.iter().cloned().collect::>(), [2..3]); + assert_eq!(fills.refused.get(&3), Some(&StreamError::Old)); + + fills.refuse(7..9, StreamError::NotFound); + assert_eq!(fills.refused.get(&7), None, "no fill asked for it"); + fills.refuse(5..6, StreamError::NotFound); + assert_eq!(fills.refused.get(&5), Some(&StreamError::NotFound)); + } +} diff --git a/rs/moq-net/tests/integration/main.rs b/rs/moq-net/tests/integration/main.rs index 21a710fc538..7a82ea8d007 100644 --- a/rs/moq-net/tests/integration/main.rs +++ b/rs/moq-net/tests/integration/main.rs @@ -46,6 +46,7 @@ mod multi_cdn; mod partial_group; mod quiet_catalog; mod range_anchors; +mod range_fill; mod range_read; mod received_rewind; mod rejoin; diff --git a/rs/moq-net/tests/integration/range_fill.rs b/rs/moq-net/tests/integration/range_fill.rs new file mode 100644 index 00000000000..81f9056ca06 --- /dev/null +++ b/rs/moq-net/tests/integration/range_fill.rs @@ -0,0 +1,158 @@ +#![cfg(feature = "async")] + +//! A copy fills a miss of more than one group over moq-lite-07 through a subscription rather +//! than FETCH, and recovers on a SUBSCRIBE of its own what its live subscription will never +//! send again. + +use crate::support; + +use std::collections::BTreeSet; +use std::time::Duration; + +use moq_net::track::{Delay, Info, Range, Subscription}; +use moq_net::{Hop, Timescale, Timestamp, Version, cache, kio}; +use support::harness::{MockConnectOptions, MockPair, connect_mock}; + +const TIMEOUT: Duration = Duration::from_secs(10); + +/// SUBSCRIBE's and FETCH's lite stream types, each one varint byte. +const SUBSCRIBE: u8 = 2; +const FETCH: u8 = 3; + +fn origin(hop: u64, pool: Option) -> moq_net::origin::Producer { + let mut config = moq_net::origin::Config::new(Hop::new(hop).unwrap()); + if let Some(pool) = pool { + config.pool = pool; + } + let (producer, driver) = moq_net::origin::Producer::new(config); + support::harness::spawn(driver); + producer +} + +/// The publisher's groups, one per second, with no publisher max age. +struct Fixture { + track: moq_net::track::Producer, + remote: moq_net::broadcast::Consumer, + pair: MockPair, + _broadcast: moq_net::origin::Publication, + _origins: (moq_net::origin::Producer, moq_net::origin::Producer), +} + +impl Fixture { + /// A lite-07 publisher holding `groups` for good, and a copy whose cache expires them. + async fn new(groups: u64) -> Self { + let publisher = origin(1, Some(cache::Pool::unbounded())); + let broadcast = publisher.create_broadcast("bcast").unwrap(); + let track = broadcast + .create_track("video", Info::default().with_timescale(Timescale::MILLI).with_max_age(None)) + .unwrap(); + broadcast.announce(Default::default()).unwrap(); + for sequence in 0..groups { + let mut group = track.create_group(moq_net::group::Info { sequence }).unwrap(); + group + .write_frame(Timestamp::from_millis(sequence * 1000).unwrap(), b"x".as_slice()) + .unwrap(); + group.finish().unwrap(); + } + + let copy = origin(2, None); + let mut options = MockConnectOptions::new("moq-lite-07-wip".parse::().unwrap()); + options.server_publish = Some(publisher.consume()); + options.client_subscribe = Some(copy.clone()); + let pair = connect_mock(options).await; + let remote = copy.consume().follow("bcast", None).unwrap().available().await.unwrap(); + Self { + track, + remote, + pair, + _broadcast: broadcast, + _origins: (publisher, copy), + } + } + + /// How many streams of `kind` the copy opened upstream. + fn opened(&self, kind: u8) -> usize { + self.pair + .client_transport + .bidi_types() + .into_iter() + .filter(|opened| *opened == kind) + .count() + } + + /// A reader with no budget, of `ranges`. + async fn subscribe(&self, ranges: impl IntoIterator) -> moq_net::track::Subscriber { + self.remote + .track("video") + .unwrap() + .subscribe( + Subscription::default() + .with_delay(Delay::default().with_max(None)) + .with_ranges(ranges), + ) + .await + .unwrap() + } +} + +/// The sequences of the next `count` groups `reader` receives. +async fn receive(reader: &mut moq_net::track::Subscriber, count: usize) -> BTreeSet { + let mut got = BTreeSet::new(); + while got.len() < count { + let group = kio::clock::timeout(TIMEOUT, reader.recv_group()) + .await + .expect("group timeout") + .unwrap() + .expect("a group"); + got.insert(group.sequence); + } + got +} + +/// History the live subscription already delivered, and the copy's cache has since evicted, +/// is never sent again on that subscription. A reader reaching back for it gets it from one +/// SUBSCRIBE of its own, not a FETCH per group. +#[kio::test] +async fn evicted_history_is_recovered_once_on_its_own_subscription() { + kio::clock::timeout(TIMEOUT + cache::DEFAULT_EXPIRY * 3, async { + let fixture = Fixture::new(6).await; + let mut first = fixture.subscribe([Range::oldest()]).await; + assert_eq!(receive(&mut first, 6).await, (0..6).collect()); + + // Superseded, the copy expires them, while the reader keeps the subscription live. + kio::clock::sleep(cache::DEFAULT_EXPIRY * 2 + Duration::from_secs(2)).await; + let subscribes = fixture.opened(SUBSCRIBE); + let mut second = fixture.subscribe([Range::groups(1..4)]).await; + assert_eq!(receive(&mut second, 3).await, (1..4).collect()); + assert_eq!(fixture.opened(SUBSCRIBE), subscribes + 1, "recovered on one SUBSCRIBE"); + assert_eq!(fixture.opened(FETCH), 0, "nothing fetched"); + + // The live subscription goes on. + let mut group = fixture.track.create_group(moq_net::group::Info { sequence: 6 }).unwrap(); + group.write_frame(Timestamp::from_millis(6000).unwrap(), b"x".as_slice()).unwrap(); + group.finish().unwrap(); + assert_eq!(receive(&mut first, 1).await, [6].into()); + assert_eq!(fixture.opened(SUBSCRIBE), subscribes + 1, "recovered once"); + }) + .await + .expect("timed out"); +} + +/// One group, or a slice of one, is what lite-07 FETCH carries, so a miss of one group still +/// fetches it. +#[kio::test] +async fn a_one_group_miss_still_fetches() { + kio::clock::timeout(TIMEOUT, async { + let fixture = Fixture::new(3).await; + let mut group = kio::clock::timeout(TIMEOUT, fixture.remote.track("video").unwrap().fetch_group(1, None)) + .await + .expect("fetch timeout") + .expect("fetched"); + let frame = group.read_frame().await.unwrap().expect("a frame"); + assert_eq!(frame.payload.as_ref(), b"x"); + assert_eq!(fixture.opened(FETCH), 1); + assert_eq!(fixture.opened(SUBSCRIBE), 0, "no subscription for one group"); + }) + .await + .expect("timed out"); +} From 538cdfe642444b2807cb1cdbf3f8e5594d9cc476 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 4 Nov 2026 07:28:47 -0800 Subject: [PATCH 18/30] fix: rename SubscribeEnd in the interop client, and format Co-Authored-By: Claude Opus 5.5 --- js/net/src/lite/publisher.ts | 10 ++-------- rs/moq-net/tests/integration/range_fill.rs | 14 +++++++++++--- test/interop/bare-fin.ts | 9 +++++++-- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/js/net/src/lite/publisher.ts b/js/net/src/lite/publisher.ts index 1ecbd1a6741..601d8466206 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -1533,10 +1533,7 @@ export class Publisher { startSent = true; startSequence = bounds.startGroup; raiseFloor(bounds.startGroup); - const start = new SubscribeStart( - rangeStarts(bounds.startGroup), - largest, - ); + const start = new SubscribeStart(rangeStarts(bounds.startGroup), largest); if ( !(await controls.response(encodeSubscribeResponse(stream, { start }, this.version))) ) @@ -1578,10 +1575,7 @@ export class Publisher { ) { startSent = true; startSequence = runs[0].start; - const start = new SubscribeStart( - rangeStarts(startSequence), - largest, - ); + const start = new SubscribeStart(rangeStarts(startSequence), largest); if ( !(await controls.response(encodeSubscribeResponse(stream, { start }, this.version))) ) diff --git a/rs/moq-net/tests/integration/range_fill.rs b/rs/moq-net/tests/integration/range_fill.rs index 81f9056ca06..15eb9206b83 100644 --- a/rs/moq-net/tests/integration/range_fill.rs +++ b/rs/moq-net/tests/integration/range_fill.rs @@ -44,7 +44,10 @@ impl Fixture { let publisher = origin(1, Some(cache::Pool::unbounded())); let broadcast = publisher.create_broadcast("bcast").unwrap(); let track = broadcast - .create_track("video", Info::default().with_timescale(Timescale::MILLI).with_max_age(None)) + .create_track( + "video", + Info::default().with_timescale(Timescale::MILLI).with_max_age(None), + ) .unwrap(); broadcast.announce(Default::default()).unwrap(); for sequence in 0..groups { @@ -128,8 +131,13 @@ async fn evicted_history_is_recovered_once_on_its_own_subscription() { assert_eq!(fixture.opened(FETCH), 0, "nothing fetched"); // The live subscription goes on. - let mut group = fixture.track.create_group(moq_net::group::Info { sequence: 6 }).unwrap(); - group.write_frame(Timestamp::from_millis(6000).unwrap(), b"x".as_slice()).unwrap(); + let mut group = fixture + .track + .create_group(moq_net::group::Info { sequence: 6 }) + .unwrap(); + group + .write_frame(Timestamp::from_millis(6000).unwrap(), b"x".as_slice()) + .unwrap(); group.finish().unwrap(); assert_eq!(receive(&mut first, 1).await, [6].into()); assert_eq!(fixture.opened(SUBSCRIBE), subscribes + 1, "recovered once"); diff --git a/test/interop/bare-fin.ts b/test/interop/bare-fin.ts index 3550e6f371c..d207829e699 100644 --- a/test/interop/bare-fin.ts +++ b/test/interop/bare-fin.ts @@ -8,7 +8,12 @@ import { Subscribe as IetfSubscribe, SubscribeOk } from "../../js/net/src/ietf/s import { Subscriber as IetfSubscriber } from "../../js/net/src/ietf/subscriber.ts"; import { Version as IetfVersion } from "../../js/net/src/ietf/version.ts"; import { StreamId } from "../../js/net/src/lite/stream.ts"; -import { encodeSubscribeResponse, Subscribe, SubscribeEnd, SubscribeStart } from "../../js/net/src/lite/subscribe.ts"; +import { + encodeSubscribeResponse, + Subscribe, + SubscribeFinish, + SubscribeStart, +} from "../../js/net/src/lite/subscribe.ts"; import { Subscriber as LiteSubscriber } from "../../js/net/src/lite/subscriber.ts"; import { Track, TrackInfo } from "../../js/net/src/lite/track.ts"; import { Version as LiteVersion } from "../../js/net/src/lite/version.ts"; @@ -60,7 +65,7 @@ if (version !== undefined) { if (input.started || input.owed) await encodeSubscribeResponse(output, { start: new SubscribeStart([0n]) }, version); if (input.clean || input.owed) { - await encodeSubscribeResponse(output, { end: new SubscribeEnd(input.owed ? 1n : 0n) }, version); + await encodeSubscribeResponse(output, { finish: new SubscribeFinish(input.owed ? 1n : 0n) }, version); } } else { assert.equal(input.version, "moqt-18"); From b26931f2725b5888639c7d559d7af8457369556e Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 4 Nov 2026 07:32:56 -0800 Subject: [PATCH 19/30] fix(net): allow the one-run list in the recovery test Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/lite/subscriber/fills.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/rs/moq-net/src/lite/subscriber/fills.rs b/rs/moq-net/src/lite/subscriber/fills.rs index 52bc7ba8e33..9870b1f1eb7 100644 --- a/rs/moq-net/src/lite/subscriber/fills.rs +++ b/rs/moq-net/src/lite/subscriber/fills.rs @@ -482,6 +482,7 @@ mod tests { /// A stale drop of groups a fill asked for is recovered once; a second one stands, and a /// drop of groups no fill asked for refuses nothing. #[test] + #[expect(clippy::single_range_in_vec_init, reason = "the recovered runs are a list, here one")] fn a_stale_drop_is_recovered_once() { let mut fills = RangeFills::default(); fills.ask.groups.insert(2..6); From b2cdb17a130c95028648912cbacaf39fa8d8620a Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 4 Nov 2026 07:37:03 -0800 Subject: [PATCH 20/30] test(net): a run's SUBSCRIBE carries its subscriber's four ranges Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/lite/publisher.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index f59e02afbac..3653b725d0f 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -6967,6 +6967,9 @@ mod serve_group_test { ]), ); let (mut run, mut writer, log) = lite07_run(SinkSession::new(Log::default()), subscriber); + // The SUBSCRIBE carried the subscriber's four ranges. + run.initial = 4; + run.ids = (0..4).collect(); let mut run = std::pin::pin!(kio::wait(move |waiter| run.poll(&mut writer, waiter))); assert!(futures::poll!(run.as_mut()).is_pending()); From 6cca68cde43d9cbbb3e865997d42b739d2af1ed8 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 4 Nov 2026 08:00:44 -0800 Subject: [PATCH 21/30] fix(net): a pre-07 start still says where a replaced demand was served from Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/lite/subscriber.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 5ccaa08d11f..1122353aad2 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -10145,10 +10145,11 @@ impl ServeLoop { let now = serve.subscriber.runtime.now(); let mut served: Option = None; for (id, start) in (0..).zip(&start.starts) { - if active.live.get(id).is_none() { - continue; + // One an update retired since owes nothing, though before lite-07 its start + // still says where the SUBSCRIBE's demand was served from. + if active.live.get(id).is_some() { + active.starts.insert(id, *start); } - active.starts.insert(id, *start); match active.fill_range(id) { Some(groups) => { let skipped = groups.start..start.unwrap_or(groups.end).clamp(groups.start, groups.end); From 81fc76954f85c4735f07f2efa1a4b13b6cd584d5 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 4 Nov 2026 08:21:07 -0800 Subject: [PATCH 22/30] fix(net): bound fill cleanup by what is held, and scope recovery to the fills owing it A draining run looks only at the groups the cache holds, and a recovery's end at the runs its tail accounted for, so neither costs a wide run's width. A group is recovered once for the fills owing it now, so a later request may recover it again, and a recovery no held fill owes is cancelled once none of its streams is still being read, so it never keeps a copy nobody reads. Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/lite/subscriber/fills.rs | 36 ++++++++++++++++++---- rs/moq-net/tests/integration/range_fill.rs | 13 +++++++- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/rs/moq-net/src/lite/subscriber/fills.rs b/rs/moq-net/src/lite/subscriber/fills.rs index 9870b1f1eb7..3663061a8b6 100644 --- a/rs/moq-net/src/lite/subscriber/fills.rs +++ b/rs/moq-net/src/lite/subscriber/fills.rs @@ -221,6 +221,8 @@ impl RangeFills { } let mut ask = FillAsk::default(); + // Every run a held fill still owes, recovered or not. + let mut owed = RangeSet::new(); let refused = &self.refused; let recovering = &self.recovering; self.held.retain_mut(|fill| { @@ -244,6 +246,7 @@ impl RangeFills { refuse_fill(fill, groups, err.clone()); continue; } + owed.insert(run.clone()); // A run being recovered waits on its own subscription. for gap in recovering.gaps(&run) { ask.groups.insert(gap); @@ -253,6 +256,21 @@ impl RangeFills { } }); + // A recovery no held fill owes any group of is cancelled once none of its streams is + // still being read, and a group is recovered once for the fills owing it now: a later + // request for it may recover it again. + self.recoveries + .retain(|recovery| owed.overlaps(&recovery.groups) || recovery.reading()); + let still: RangeSet = self.recoveries.iter().map(|recovery| recovery.groups.clone()).collect(); + self.recovering = still; + let recovered: Vec<_> = self.recovered.iter().cloned().collect(); + self.recovered = RangeSet::new(); + for run in recovered { + for kept in owed.overlapping(&run) { + self.recovered.insert(kept.start.max(run.start)..kept.end.min(run.end)); + } + } + // A run stays asked whole while a fill still owes any of it, so the subscription // changes once as a run is asked and once as it is done, and the streams of its // answered groups keep arriving. One no fill owes stays while any of its streams is @@ -266,8 +284,9 @@ impl RangeFills { groups.insert(run.clone()); continue; } + // Only the groups the cache holds, so a wide run costs what it holds. for gap in self.draining.gaps(run) { - for sequence in gap { + for sequence in edge.cached_groups(gap) { if let Some(mut group) = edge.peek_group(sequence) && group.poll_finished(waiter).is_pending() { @@ -355,6 +374,14 @@ impl Recovery { } } + /// Whether a stream of the subscription is still being read, which cancelling would cut. + fn reading(&self) -> bool { + match &self.state { + RecoveryState::Active(active) => active.tail.read().is_reading(), + _ => false, + } + } + /// End the subscription, refusing what is left of the groups with `err`, as nothing more /// will arrive on it. fn end(&mut self, err: StreamError) { @@ -362,11 +389,8 @@ impl Recovery { let mut owed = RangeSet::new(); owed.insert(self.groups.clone()); if let RecoveryState::Active(active) = &state { - let tail = active.tail.read(); - for sequence in self.groups.clone() { - if tail.covers(sequence..sequence + 1) { - owed.remove(sequence..sequence + 1); - } + for arrived in active.tail.read().accounted(self.groups.clone()) { + owed.remove(arrived); } } for (refused, _) in self.refused.iter() { diff --git a/rs/moq-net/tests/integration/range_fill.rs b/rs/moq-net/tests/integration/range_fill.rs index 15eb9206b83..a6dd18308be 100644 --- a/rs/moq-net/tests/integration/range_fill.rs +++ b/rs/moq-net/tests/integration/range_fill.rs @@ -117,7 +117,7 @@ async fn receive(reader: &mut moq_net::track::Subscriber, count: usize) -> BTree /// SUBSCRIBE of its own, not a FETCH per group. #[kio::test] async fn evicted_history_is_recovered_once_on_its_own_subscription() { - kio::clock::timeout(TIMEOUT + cache::DEFAULT_EXPIRY * 3, async { + kio::clock::timeout(TIMEOUT + cache::DEFAULT_EXPIRY * 6, async { let fixture = Fixture::new(6).await; let mut first = fixture.subscribe([Range::oldest()]).await; assert_eq!(receive(&mut first, 6).await, (0..6).collect()); @@ -141,6 +141,17 @@ async fn evicted_history_is_recovered_once_on_its_own_subscription() { group.finish().unwrap(); assert_eq!(receive(&mut first, 1).await, [6].into()); assert_eq!(fixture.opened(SUBSCRIBE), subscribes + 1, "recovered once"); + + // Expired again, the history is recovered again for a later reader. + drop(second); + kio::clock::sleep(cache::DEFAULT_EXPIRY * 2 + Duration::from_secs(2)).await; + let mut third = fixture.subscribe([Range::groups(1..4)]).await; + assert_eq!(receive(&mut third, 3).await, (1..4).collect()); + assert_eq!( + fixture.opened(SUBSCRIBE), + subscribes + 2, + "recovered for the later reader" + ); }) .await .expect("timed out"); From 9d0504e743d9f8485c9c4a0f4bdae6622e623b65 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 4 Nov 2026 08:27:31 -0800 Subject: [PATCH 23/30] fix(net): an abandoned recovery is released once nobody reads the track Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/lite/subscriber.rs | 7 ++++++- rs/moq-net/src/lite/subscriber/fills.rs | 12 +++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 1122353aad2..4317e0060aa 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -10515,7 +10515,12 @@ impl ServeLoop { Sub::Active(active) => Some(active.tail.clone()), Sub::None => None, }; - if self.range_fills.poll(serve, &self.serving, accounted.as_ref(), waiter) { + // A stream still arriving on a recovery is kept only for someone reading it. + let read = crate::util::poll_unwanted(&mut self.demand, waiter).is_pending(); + if self + .range_fills + .poll(serve, &self.serving, accounted.as_ref(), read, waiter) + { if let Err(err) = self.begin(serve, self.serving.subscription()) { return Poll::Ready(ServeEnd::GiveBack(err)); } diff --git a/rs/moq-net/src/lite/subscriber/fills.rs b/rs/moq-net/src/lite/subscriber/fills.rs index 3663061a8b6..887e4cf78cf 100644 --- a/rs/moq-net/src/lite/subscriber/fills.rs +++ b/rs/moq-net/src/lite/subscriber/fills.rs @@ -161,12 +161,14 @@ impl RangeFills { /// Settle each held fill against what the subscription refused, open a [`Recovery`] for /// each run it will not deliver, and recompute what to ask. `accounted` is the tail of - /// the subscription the fills ride, if one is open. Returns whether the ask changed. + /// the subscription the fills ride, if one is open, and `read` whether anyone still reads + /// the track. Returns whether the ask changed. pub fn poll( &mut self, serve: &TrackServe, serving: &track::Producer, accounted: Option<&kio::Producer>, + read: bool, waiter: &kio::Waiter, ) -> bool { // The groups the subscription already accounted for, which it never sends again, are @@ -256,11 +258,11 @@ impl RangeFills { } }); - // A recovery no held fill owes any group of is cancelled once none of its streams is - // still being read, and a group is recovered once for the fills owing it now: a later - // request for it may recover it again. + // A recovery no held fill owes any group of is cancelled, once none of its streams is + // still being read or nobody reads the track, and a group is recovered once for the + // fills owing it now: a later request for it may recover it again. self.recoveries - .retain(|recovery| owed.overlaps(&recovery.groups) || recovery.reading()); + .retain(|recovery| owed.overlaps(&recovery.groups) || (read && recovery.reading())); let still: RangeSet = self.recoveries.iter().map(|recovery| recovery.groups.clone()).collect(); self.recovering = still; let recovered: Vec<_> = self.recovered.iter().cloned().collect(); From 23f9bf77f69f1cb31d884ff0fd01f7dfd831a50a Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 4 Nov 2026 12:40:23 -0800 Subject: [PATCH 24/30] fix(net): address lite-07 range review findings - JS publisher: index the track model by the decoder's live Range IDs, so SUBSCRIBE_STARTED and SUBSCRIBE_START never read another range's start when an update lands before the serving loop takes it. - Pin a time start that resolved to a group, in both publishers, so a newer group starting before its time does not move it on a later update. - Look Range IDs up through one index in both publishers instead of a scan per range. - Rust subscriber: tell fills from demand ranges by Range ID, so a fill asking for exactly a demand range's bounds is still judged as a fill. - A range start's skip only refuses fills while that range is live; drops still stand. - A recovery keeps the reset's error, treats a malformed, duplicate, or unexpected response as a protocol violation, and a finished track waits for outstanding recoveries before ending. Co-Authored-By: Claude Opus 5.5 --- js/net/src/lite/publisher.test.ts | 41 ++++++ js/net/src/lite/publisher.ts | 54 ++++++-- rs/moq-net/src/lite/publisher.rs | 90 +++++++++--- rs/moq-net/src/lite/subscribe.rs | 36 +++-- rs/moq-net/src/lite/subscriber.rs | 137 ++++++++++++++---- rs/moq-net/src/lite/subscriber/fills.rs | 153 +++++++++++++++++---- rs/moq-net/tests/integration/range_fill.rs | 19 +++ 7 files changed, 433 insertions(+), 97 deletions(-) diff --git a/js/net/src/lite/publisher.test.ts b/js/net/src/lite/publisher.test.ts index 104f2d0eb03..933c43d0a69 100644 --- a/js/net/src/lite/publisher.test.ts +++ b/js/net/src/lite/publisher.test.ts @@ -947,6 +947,47 @@ test("lite-07 SUBSCRIBE_STARTED reports an added range's start, which later upda } }); +// A time start resolves once too: a newer group starting before its time doesn't move it once +// an update leaves the range live. Mirrors the Rust `lite07_an_added_time_start_keeps_its_start`. +test("lite-07 keeps where an added time start resolved", async () => { + const version = Version.DRAFT_07; + const sub = await servedSubscription({ + version, + prefill: [0n, 1n, 2n, 3n, 4n], + maxAge: Milli(3_600_000), + ranges: [{ start: { absolute: 0n }, end: { absolute: 1n } }], + }); + const next = async () => { + for (;;) { + const resp = await decodeSubscribeResponse(sub.client.reader, version); + if ("start" in resp || "started" in resp) return resp; + } + }; + try { + const start = await next(); + if (!("start" in start)) throw new Error("expected SUBSCRIBE_OK"); + + // 10 ms falls in group 4, the newest one starting at or before it. + await replayUpdate({ priority: 0, ranges: [{ start: { time: 10 } }] }).encode(sub.client.writer, version); + const started = await next(); + if (!("started" in started)) throw new Error("expected SUBSCRIBE_STARTED"); + expect([started.started.id, started.started.start]).toEqual([1n, 4n]); + + // Group 5 starts at 5 ms, before 10 ms, and another update leaves the range live: it + // still starts at 4. + sub.serve(5n); + await replayUpdate({ priority: 0, ranges: [{ start: { absolute: 9n } }] }).encode(sub.client.writer, version); + const pinned = await next(); + if (!("started" in pinned)) throw new Error("expected SUBSCRIBE_STARTED"); + expect(pinned.started.id).toBe(2n); + const ranges = sub.track.subscription.peek()?.ranges ?? []; + expect(ranges.some((range) => range.start?.absolute === 4n)).toBe(true); + expect(ranges.some((range) => range.start?.time !== undefined)).toBe(false); + } finally { + await sub.close(); + } +}); + // An edit naming a Range ID the subscription never assigned violates the protocol. test("lite-07 refuses an update retiring an unknown Range ID", async () => { const version = Version.DRAFT_07; diff --git a/js/net/src/lite/publisher.ts b/js/net/src/lite/publisher.ts index 601d8466206..7565319814d 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -282,6 +282,7 @@ type SubscriptionControlOptions = { */ class SubscriptionControls { #writer: Writer; + #live: LiveRanges; #update?: { update: SubscribeUpdate; ids: bigint[] }; // Sticky, first one wins: null once the stream is over, an Error once it failed. #end?: Error | null; @@ -293,8 +294,9 @@ class SubscriptionControls { readonly decoding: Promise; constructor(options: SubscriptionControlOptions) { - const { writer } = options; + const { writer, live } = options; this.#writer = writer; + this.#live = live; this.#ended = new Promise((resolve) => { this.#resolveEnd = resolve; }); @@ -317,6 +319,15 @@ class SubscriptionControls { return this.#end === null ? { kind: "done" } : { kind: "error", error: this.#end }; } + /** + * Where each live Range ID sits among the track model's ranges. The decoder applies each + * update to the model as it decodes it, ahead of {@link take}, so this rather than the + * last control taken is what indexes the model. + */ + positions(): Map { + return new Map(this.#live.ids().map((id, index) => [id, index])); + } + /** Whether {@link take} has a control to answer with now. */ pending(): boolean { return this.#update !== undefined || this.#end !== undefined; @@ -385,12 +396,15 @@ class SubscriptionControls { } // `entry`, a range this subscription already held, resolved as it was: a range resolves once, so -// an update that leaves it live never counts its relative anchors back from a newer latest group. +// an update that leaves it live never counts its relative anchors back from a newer latest group, +// nor looks a time start up again in newer holdings. A time start resolved at group 0 stays: it +// either names group 0, below which nothing sits, or reaches back to the oldest group held, +// which an absolute start would turn into a floor the subscriber never named. function pin(entry: RangeEntry, resolved: Resolved | undefined): RangeEntry { if (!resolved) return entry; const range = { ...entry.range }; let { from, to } = entry; - if (range.start?.relative !== undefined) { + if (range.start?.relative !== undefined || (range.start?.time !== undefined && resolved.start > 0n)) { range.start = { absolute: resolved.start }; from = [resolved.start, 0]; } @@ -1058,7 +1072,8 @@ export class Publisher { // The SUBSCRIBE's ranges, and the Range ID of each range the model holds, in its order. const asked = entriesOf(msg); - let ids = asked.map((_, index) => BigInt(index)); + // Where each Range ID sits among the model's ranges, as of the last update applied. + let positions = new Map(asked.map((_, index) => [BigInt(index), index])); controls = new SubscriptionControls({ quic: this.#quic, reader: stream.reader, @@ -1069,11 +1084,11 @@ export class Publisher { if (hasRanges(this.version)) { const resolved = hooks.servingResolved(track); const entries = live.map(({ id, entry }) => { - const index = ids.indexOf(id); - return index < 0 ? entry : pin(entry, resolved[index]); + const index = positions.get(id); + return index === undefined ? entry : pin(entry, resolved[index]); }); Object.assign(update, spanOf(this.version, entries)); - ids = live.map(({ id }) => id); + positions = new Map(live.map(({ id }, index) => [id, index])); } Object.assign(datagramBounds, { startGroup: update.startGroup, @@ -1300,8 +1315,8 @@ export class Publisher { // resolves where its feed starts. let first: { group: group.Consumer; largest: track.Position | undefined } | undefined; let endSent = false; - // The Range ID of each requested range, in the order the model holds them, and those an - // update added whose start no SUBSCRIBE_STARTED reported yet. Lite-07 only. + // The Range IDs of the last update taken, and those an update added whose start no + // SUBSCRIBE_STARTED reported yet. Lite-07 only. let ids = Array.from({ length: Number(initial) }, (_, index) => BigInt(index)); let unstarted: bigint[] = []; @@ -1311,9 +1326,10 @@ export class Publisher { const rangeStarts = (from: bigint): (bigint | undefined)[] => { if (!hasRanges(this.version)) return [from]; const starts = hooks.servingStarts(track, from); + const positions = controls.positions(); return Array.from({ length: Number(initial) }, (_, id) => { - const index = ids.indexOf(BigInt(id)); - return index < 0 ? undefined : starts[index]; + const index = positions.get(BigInt(id)); + return index === undefined ? undefined : starts[index]; }); }; @@ -1322,9 +1338,16 @@ export class Publisher { const reportStarted = async (finished: boolean): Promise => { if (startSequence === undefined || unstarted.length === 0) return true; const resolved = hooks.servingResolved(track); + const positions = controls.positions(); const waiting: bigint[] = []; for (const id of unstarted) { - const range = resolved[ids.indexOf(id)]; + const index = positions.get(id); + // An update not taken yet retired it, and taking that update forgets it. + if (index === undefined) { + waiting.push(id); + continue; + } + const range = resolved[index]; if (!range && !finished) { waiting.push(id); continue; @@ -1453,9 +1476,10 @@ export class Publisher { const update = control.update; if (hasRanges(this.version)) { // A range retired before its start resolved is owed no SUBSCRIBE_STARTED. - unstarted = unstarted.filter((id) => control.ids.includes(id)); - for (const id of control.ids) - if (id >= initial && !ids.includes(id)) unstarted.push(id); + const live = new Set(control.ids); + unstarted = unstarted.filter((id) => live.has(id)); + const held = new Set(ids); + for (const id of control.ids) if (id >= initial && !held.has(id)) unstarted.push(id); ids = control.ids; } console.debug(`subscribe update: broadcast=${broadcast} track=${track.name}`); diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 3653b725d0f..41e27b1015b 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -4741,32 +4741,47 @@ impl TrackRun { self.frames = bounds.frames; if self.ctx.version.has_ranges() { // A range retired before its start resolved is owed no SUBSCRIBE_STARTED. - self.unstarted.retain(|id| ids.contains(id)); - let added = ids.iter().filter(|id| **id >= self.initial && !self.ids.contains(id)); + // Both list Range IDs in ascending order, since each one assigned is the next. + self.unstarted.retain(|id| ids.binary_search(id).is_ok()); + let added = ids + .iter() + .filter(|id| **id >= self.initial && self.ids.binary_search(id).is_err()); self.unstarted.extend(added); } self.ids = ids; } + /// Where each live Range ID sits among the model's ranges. + fn positions(&self) -> HashMap { + self.ids.iter().enumerate().map(|(index, id)| (*id, index)).collect() + } + /// `ranges`, numbered by `ids`, with each range this subscription already held resolved /// as it was: a range resolves once, so an update that leaves it live never counts its - /// relative anchors back from a newer latest group. Lite-07 only: an older update + /// relative anchors back from a newer latest group, nor looks a time start up again in + /// newer groups. A time start resolved at group 0 stays: it either names group 0, below + /// which nothing sits, or reaches back to the oldest group held, which an absolute start + /// would turn into a floor the subscriber never named. Lite-07 only: an older update /// replaces every range. fn pin(&mut self, ids: &[u64], mut ranges: Vec) -> Vec { if !self.ctx.version.has_ranges() { return ranges; } let resolved = self.track.resolved(); + let positions = self.positions(); for (id, range) in ids.iter().zip(&mut ranges) { - let Some((start, end)) = self - .ids - .iter() - .position(|held| held == id) - .and_then(|index| resolved.get(index).copied().flatten()) + let Some((start, end)) = positions + .get(id) + .and_then(|index| resolved.get(*index).copied().flatten()) else { continue; }; - if let Some(lite::Anchor::Relative(_)) = range.start { + let pinned = match range.start { + Some(lite::Anchor::Relative(_)) => true, + Some(lite::Anchor::Time(_)) => start > 0, + _ => false, + }; + if pinned { range.start = Some(lite::Anchor::Absolute(track::Position::group(start))); } if let (Some(lite::Anchor::Relative(_)), Some(end)) = (range.end, end) { @@ -4791,14 +4806,11 @@ impl TrackRun { return Ok(false); } let resolved = self.track.resolved(); + let positions = self.positions(); let mut waiting = Vec::new(); let mut reported = false; for id in std::mem::take(&mut self.unstarted) { - let index = self - .ids - .iter() - .position(|held| *held == id) - .expect("unstarted ranges are live"); + let index = *positions.get(&id).expect("unstarted ranges are live"); let start = match resolved.get(index).copied().flatten() { Some((start, end)) => { let start = start.max(from); @@ -5160,11 +5172,9 @@ 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. let starts = self.track.starts(start); + let positions = self.positions(); (0..self.initial) - .map(|id| { - let index = self.ids.iter().position(|held| *held == id)?; - starts.get(index).copied().flatten() - }) + .map(|id| starts.get(*positions.get(&id)?).copied().flatten()) .collect() } @@ -7066,6 +7076,50 @@ mod serve_group_test { assert_eq!(run.track.resolved()[1], Some((2, None)), "the update resolved it again"); } + /// A time start resolves once too: a newer group starting before its time doesn't move it + /// once an update leaves the range live. + #[kio::test] + async fn lite07_an_added_time_start_keeps_its_start() { + let mut track = track::Producer::new( + Arc::new(broadcast::Info::default()), + "test", + crate::track::Info::timed(), + ); + for sequence in 0..5 { + write_group(&mut track, sequence, sequence * 1000); + } + let first = lite::Range::span(Some(0), 0, Some(0), None); + let subscriber = track.subscribe( + track::Subscription::default() + .with_delay(track::Delay::default().with_max(Duration::from_secs(3600))) + .with_range(0..1), + ); + let (mut run, mut writer, _log) = lite07_run(SinkSession::new(Log::default()), subscriber); + while run.start.is_none() { + assert!(run.poll_step(&mut writer, &kio::Waiter::noop()).is_ready()); + } + + // 4.5 s falls in group 4, the newest group starting at or before it. + let time = lite::Range { + start: Some(lite::Anchor::Time(4500)), + end: None, + }; + run.update(lite07_add(&[(0, first)], 1, &[time])); + while run.poll_step(&mut writer, &kio::Waiter::noop()).is_ready() {} + assert_eq!(run.track.resolved()[1], Some((4, None))); + + // Group 5 starts before 4.5 s, and another update leaves the range live: it still + // starts at 4. + write_group(&mut track, 5, 4200); + let live = [(0, first), (1, time)]; + run.update(lite07_add(&live, 2, &[lite::Range::span(Some(9), 0, None, None)])); + assert_eq!( + run.track.resolved()[1], + Some((4, None)), + "the update kept where it started" + ); + } + /// A range retired before SUBSCRIBE_STARTED reported it gets none, and one still waiting /// once the track ends is reported with no start. #[kio::test] diff --git a/rs/moq-net/src/lite/subscribe.rs b/rs/moq-net/src/lite/subscribe.rs index 142efcab100..7967fe06ef2 100644 --- a/rs/moq-net/src/lite/subscribe.rs +++ b/rs/moq-net/src/lite/subscribe.rs @@ -905,36 +905,50 @@ impl LiveRanges { /// /// A range already live keeps its Range ID, so an update that changes one range sends /// only that one, and the publisher keeps where the rest resolved. + #[cfg(test)] pub fn edit(&mut self, ranges: &[Range]) -> Result { + let keyed: Vec<_> = ranges.iter().map(|range| ((), *range)).collect(); + self.edit_keyed(&keyed, |_| ()).map(|(edit, _)| edit) + } + + /// [`Self::edit`] for ranges asked for by different owners: a live range keeps its Range + /// ID only for a wanted range of the same key, `key_of` giving each live one's. Returns + /// the edit and the Range ID each range it adds takes, with that range's key. + pub fn edit_keyed( + &mut self, + ranges: &[(K, Range)], + key_of: impl Fn(u64) -> K, + ) -> Result<(RangeEdit, Vec<(u64, K)>), EncodeError> { if ranges.is_empty() { return Err(EncodeError::InvalidState); } if !self.version.has_ranges() { // Replaced ranges take fresh Range IDs, so nothing said of the old ones applies. - if !self.live.values().eq(ranges) { + let ranges: Vec = ranges.iter().map(|(_, range)| *range).collect(); + if !self.live.values().eq(&ranges) { self.live.clear(); self.size = 0; self.add(ranges.iter().copied()); } - return Ok(RangeEdit::Replace(ranges.to_vec())); + return Ok((RangeEdit::Replace(ranges), Vec::new())); } // Each live range keeps at most one wanted copy, so duplicates edit like any other. - let mut wanted = std::collections::HashMap::::new(); - for range in ranges { - *wanted.entry(*range).or_default() += 1; + let mut wanted = std::collections::HashMap::<(K, Range), usize>::new(); + for wanted_range in ranges { + *wanted.entry(*wanted_range).or_default() += 1; } let mut remove = Vec::new(); for (&id, range) in &self.live { - match wanted.get_mut(range) { + match wanted.get_mut(&(key_of(id), *range)) { Some(count) if *count > 0 => *count -= 1, _ => remove.push(id), } } // In the order asked for, so the publisher numbers them the way this side does. - let add: Vec = ranges + let added: Vec<(K, Range)> = ranges .iter() - .filter(|range| { - let count = wanted.get_mut(*range).expect("counted above"); + .filter(|wanted_range| { + let count = wanted.get_mut(*wanted_range).expect("counted above"); let keep = *count > 0; *count = count.saturating_sub(1); keep @@ -944,11 +958,13 @@ impl LiveRanges { for &id in &remove { self.remove(id); } + let ids = (self.next..).zip(added.iter().map(|(key, _)| *key)).collect(); + let add: Vec = added.into_iter().map(|(_, range)| range).collect(); self.add(add.iter().copied()); if self.size > super::message::MAX_MESSAGE_SIZE { return Err(EncodeError::TooLarge); } - Ok(RangeEdit::Edit { remove, add }) + Ok((RangeEdit::Edit { remove, add }, ids)) } } diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 4317e0060aa..4b644c9b258 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -2912,7 +2912,7 @@ mod tests { requested: None, live: lite::LiveRanges::new(Version::Lite04, &[lite::Range::OLDEST]), demand: Some(Subscription::default()), - demand_ranges: vec![lite::Range::OLDEST], + fill_ids: Default::default(), fill_groups: Default::default(), tail: tail.clone(), served: None, @@ -3066,7 +3066,7 @@ mod tests { requested: Some(floor), live: lite::LiveRanges::new(Version::Lite04, &[lite::Range::span(Some(floor), 0, None, None)]), demand: Some(Subscription::default()), - demand_ranges: vec![lite::Range::span(Some(floor), 0, None, None)], + fill_ids: Default::default(), fill_groups: Default::default(), tail: Default::default(), served: None, @@ -3531,7 +3531,7 @@ mod tests { requested: Some(2), live: lite::LiveRanges::new(VERSION, &[lite::Range::span(Some(2), 0, None, None)]), demand: Some(Subscription::default()), - demand_ranges: vec![lite::Range::span(Some(2), 0, None, None)], + fill_ids: Default::default(), fill_groups: Default::default(), tail: Default::default(), served: None, @@ -3551,7 +3551,7 @@ mod tests { // An update moves the floor and the ranges together, as `begin_subscription` does. let update = |sub: &mut SubStream, start: Option, ranges: Vec| { sub.start = start; - sub.edit(&ranges).unwrap(); + sub.edit(&ranges, 0).unwrap(); }; sub.started = true; update(&mut sub, Some(1), vec![lite::Range::span(Some(1), 0, None, None)]); @@ -3562,10 +3562,13 @@ mod tests { ); sub.start = Some(2); - sub.edit(&[ - lite::Range::span(Some(2), 0, Some(3), None), - lite::Range::span(Some(6), 0, None, None), - ]) + sub.edit( + &[ + lite::Range::span(Some(2), 0, Some(3), None), + lite::Range::span(Some(6), 0, None, None), + ], + 0, + ) .unwrap(); assert_eq!( sub.owed(None), @@ -3616,6 +3619,62 @@ mod tests { ); } + /// A fill asking for exactly what a demand range does is still a fill, judged by its own + /// Range ID, and keeps that ID while it stays held. + #[kio::test] + async fn a_lite07_fill_is_known_by_its_range_id() { + let session = crate::lite::test_transport::ScriptedSession::new(Vec::new()); + let stream = Stream::open(&mut crate::driver::Transport::new(session.clone()), Version::Lite07) + .await + .unwrap(); + // As a fill asks on the wire. + let groups = |range: std::ops::Range| lite::Range { + start: Some(lite::Anchor::Absolute(Position::group(range.start))), + end: Some(lite::Anchor::Absolute(Position::group(range.end))), + }; + let demand = groups(4..8); + let mut sub = SubStream { + order: crate::track::Order::Descending, + stream, + id: 0, + max_delay: Duration::ZERO, + min_delay: None, + start: Some(4), + priority: 0, + requested: Some(4), + live: lite::LiveRanges::new(Version::Lite07, &[demand]), + demand: Some(Subscription::default()), + fill_ids: Default::default(), + fill_groups: Default::default(), + tail: Default::default(), + served: Some(4), + started: true, + starts: [(0, Some(4))].into(), + end: None, + wanted: None, + unanswered: None, + _working: crate::session::Work::default().start(), + }; + assert_eq!(sub.fill_range(0), None, "the demand's range is no fill"); + + // The same bounds again, as a fill. + sub.edit(&[demand, demand], 1).unwrap(); + assert_eq!(sub.fill_range(0), None, "the demand keeps its range"); + assert_eq!(sub.fill_range(1), Some(4..8), "the copy the edit added is the fill"); + + // Another fill joins, and the first keeps its Range ID. + let other = groups(10..12); + sub.edit(&[demand, demand, other], 2).unwrap(); + assert_eq!(sub.fill_ids.iter().copied().collect::>(), [1, 2]); + + // The demand moves away while the fill stays: the fill keeps Range ID 1. + let moved = lite::Range::span(Some(20), 0, None, None); + sub.edit(&[moved, demand, other], 2).unwrap(); + assert_eq!(sub.fill_ids.iter().copied().collect::>(), [1, 2]); + assert_eq!(sub.fill_range(1), Some(4..8)); + assert_eq!(sub.fill_range(3), None, "the demand's new range is no fill"); + } + /// A lite-07 range an update adds owes nothing known until SUBSCRIBE_STARTED says where it /// starts, and then owes from there; one retired since owes nothing. #[kio::test] @@ -3640,7 +3699,7 @@ mod tests { requested: Some(4), live: lite::LiveRanges::new(Version::Lite07, &[first]), demand: Some(Subscription::default()), - demand_ranges: vec![first], + fill_ids: Default::default(), fill_groups: Default::default(), tail: Default::default(), served: Some(4), @@ -3658,7 +3717,7 @@ mod tests { start: Some(lite::Anchor::Relative(2)), end: None, }; - let (edit, changed) = sub.edit(&[first, back]).unwrap(); + let (edit, changed) = sub.edit(&[first, back], 0).unwrap(); assert!(changed); assert_eq!( edit, @@ -3672,7 +3731,7 @@ mod tests { assert_eq!(sub.owed(None), Some(vec![4..10, 1..10])); // Retiring the first range forgets its start. - sub.edit(&[back]).unwrap(); + sub.edit(&[back], 0).unwrap(); assert_eq!(sub.starts.keys().copied().collect::>(), [1]); assert_eq!(sub.owed(Some(8)), Some(vec![1..8])); } @@ -8024,8 +8083,9 @@ struct SubStream { live: lite::LiveRanges, /// The demand the ranges ask for, `None` when they ask only for fills. demand: Option, - /// The demand's ranges on the wire, which alone set where the live feed is. - demand_ranges: Vec, + /// The Range IDs of the fills' ranges; the rest are the demand's, which alone set where + /// the live feed is. Held by ID, since a fill may ask for exactly what a demand range does. + fill_ids: std::collections::BTreeSet, /// The groups the ranges ask for fills. fill_groups: rangemap::RangeSet, priority: i8, @@ -8149,24 +8209,36 @@ impl SubStream { /// The groups the live range `id` asks for a fill, `None` for one of the demand's. fn fill_range(&self, id: u64) -> Option> { - let range = self.live.get(id)?; - if self.demand_ranges.contains(range) { + if !self.fill_ids.contains(&id) { return None; } + let range = self.live.get(id)?; match (range.start, range.end) { (Some(lite::Anchor::Absolute(start)), Some(lite::Anchor::Absolute(end))) => Some(start.group..end.group), _ => None, } } - /// Edit the live ranges into `ranges`, forgetting the starts of those retired. Returns the - /// edit, and whether it changed anything. - fn edit(&mut self, ranges: &[lite::Range]) -> Result<(lite::RangeEdit, bool), Error> { + /// Edit the live ranges into `ranges`, the last `fills` of which are the held fills', + /// forgetting the starts of those retired. A fill asking for what a demand range does + /// still takes a range of its own, so each Range ID stays the demand's or a fill's. + /// Returns the edit, and whether it changed anything. + fn edit(&mut self, ranges: &[lite::Range], fills: usize) -> Result<(lite::RangeEdit, bool), Error> { let before: Vec<_> = self.live.iter().map(|(id, _)| id).collect(); - let edit = self.live.edit(ranges)?; + let demand = ranges.len().saturating_sub(fills); + let keyed: Vec<_> = ranges + .iter() + .enumerate() + .map(|(index, range)| (index >= demand, *range)) + .collect(); + let fill_ids = &self.fill_ids; + let (edit, added) = self.live.edit_keyed(&keyed, |id| fill_ids.contains(&id))?; let changed = !self.live.iter().map(|(id, _)| id).eq(before); let live = &self.live; self.starts.retain(|id, _| live.get(*id).is_some()); + self.fill_ids.retain(|id| live.get(*id).is_some()); + self.fill_ids + .extend(added.into_iter().filter(|(_, fill)| *fill).map(|(id, _)| id)); Ok((edit, changed)) } } @@ -8633,7 +8705,7 @@ impl TrackServe { // So do changed ranges that keep the floor, such as a widening that joins // two: each gap they reach waits a fresh grace, which only delays giving up // on one still owed. - let (edit, changed) = active.edit(&ranges)?; + let (edit, changed) = active.edit(&ranges, ranges.len() - demand_ranges.len())?; let widened = (changed && demand.is_some()) .then(|| subscription.start().or(active.served).unwrap_or(0)..u64::MAX); if let Ok(mut tail) = active.tail.write() { @@ -8652,7 +8724,6 @@ impl TrackServe { } active.fill_groups = fills.groups.clone(); active.demand = demand; - active.demand_ranges = demand_ranges; active.start = subscription.start(); if supports_update { // The floor follows the requested start, in both directions: @@ -9059,7 +9130,8 @@ impl Establish { start: self.subscription.start(), live: lite::LiveRanges::new(self.serve.subscriber.version, &self.ranges), demand: self.demand.clone(), - demand_ranges: self.demand_ranges.clone(), + // The ranges take Range IDs in order, the demand's first. + fill_ids: (self.demand_ranges.len() as u64..self.ranges.len() as u64).collect(), fill_groups: self.fill_groups.clone(), priority: self.subscription.priority, order: self.subscription.order, @@ -10156,7 +10228,7 @@ impl ServeLoop { if let Ok(mut tail) = active.tail.write() { tail.account(skipped.clone(), now); } - self.range_fills.refuse(skipped, crate::StreamError::NotFound); + self.range_fills.skip(groups, skipped); } // Only the demand's ranges say where the live feed is served from. No range // starting anywhere leaves nothing owed. @@ -10229,7 +10301,7 @@ impl ServeLoop { if let Ok(mut tail) = active.tail.write() { tail.account(skipped.clone(), now); } - self.range_fills.refuse(skipped, crate::StreamError::NotFound); + self.range_fills.skip(groups, skipped); } // The tail judges what is owed again. if let Ok(mut tail) = active.tail.write() { @@ -10517,18 +10589,23 @@ impl ServeLoop { }; // A stream still arriving on a recovery is kept only for someone reading it. let read = crate::util::poll_unwanted(&mut self.demand, waiter).is_pending(); - if self + match self .range_fills .poll(serve, &self.serving, accounted.as_ref(), read, waiter) { - if let Err(err) = self.begin(serve, self.serving.subscription()) { - return Poll::Ready(ServeEnd::GiveBack(err)); + Ok(true) => { + if let Err(err) = self.begin(serve, self.serving.subscription()) { + return Poll::Ready(ServeEnd::GiveBack(err)); + } + continue; } - continue; + Ok(false) => {} + Err(err) => return Poll::Ready(ServeEnd::GiveBack(err)), } - // A lite-07 track the publisher finished ends once its tail settles. - if self.poll_finishing(waiter).is_ready() { + // A lite-07 track the publisher finished ends once its tail settles, and once + // every recovery delivered what the tail no longer waits for: ending drops it. + if self.poll_finishing(waiter).is_ready() && !self.range_fills.recovering() { return Poll::Ready(ServeEnd::Finished); } diff --git a/rs/moq-net/src/lite/subscriber/fills.rs b/rs/moq-net/src/lite/subscriber/fills.rs index 887e4cf78cf..f5e272a0500 100644 --- a/rs/moq-net/src/lite/subscriber/fills.rs +++ b/rs/moq-net/src/lite/subscriber/fills.rs @@ -89,7 +89,8 @@ impl FillAsk { /// A fill is judged against the subscription that asked for it alone. The track's arrivals /// still account for a group an earlier subscription cut, which this one may still deliver, /// so only what this subscription says it will not deliver (a SUBSCRIBE_DROP, or a range -/// start past the range) refuses a fill. A group it drops as stale is officially dropped, +/// start past the range) refuses a fill. A range start speaks only for its range: once the +/// range retires, a later one asking for the same groups may start lower. A group it drops as stale is officially dropped, /// and one it already accounted for (delivered before and since evicted here) it never /// sends again, so a [`Recovery`] asks for either on its own subscription with no max delay, /// once. @@ -107,6 +108,9 @@ pub(super) struct RangeFills { draining: RangeSet, /// Groups the subscription said it will not deliver, and why. refused: RangeMap, + /// Groups below where a fill's range starts, by the asked run that range carries, which + /// stand only while that range is live. + skipped: RangeMap>, /// Groups dropped as stale, which a [`Recovery`] has yet to ask for. recover: RangeSet, /// Groups a [`Recovery`] asks for now, which the subscription leaves out meanwhile. @@ -154,15 +158,40 @@ impl RangeFills { } } + /// The subscription's range for the asked run `run` starts past `groups`, which it will not + /// deliver while that range is live. + pub fn skip(&mut self, run: Range, groups: Range) { + if !groups.is_empty() { + self.skipped.insert(groups, run); + } + } + /// A fresh subscription may deliver what the last one refused. pub fn forget_refused(&mut self) { self.refused = Default::default(); + self.skipped = Default::default(); + } + + /// Whether a [`Recovery`] is still out, whose groups the subscription's tail does not wait + /// for. + pub fn recovering(&self) -> bool { + !self.recoveries.is_empty() + } + + /// Why the subscription will not deliver the run of groups from `first`, if it said so. + fn refusal(&self, first: u64) -> Option<(Range, StreamError)> { + if let Some((groups, err)) = self.refused.get_key_value(&first) { + return Some((groups.clone(), err.clone())); + } + let (groups, _) = self.skipped.get_key_value(&first)?; + Some((groups.clone(), StreamError::NotFound)) } /// Settle each held fill against what the subscription refused, open a [`Recovery`] for /// each run it will not deliver, and recompute what to ask. `accounted` is the tail of /// the subscription the fills ride, if one is open, and `read` whether anyone still reads - /// the track. Returns whether the ask changed. + /// the track. Returns whether the ask changed, or the protocol violation a recovery's + /// publisher committed. pub fn poll( &mut self, serve: &TrackServe, @@ -170,7 +199,7 @@ impl RangeFills { accounted: Option<&kio::Producer>, read: bool, waiter: &kio::Waiter, - ) -> bool { + ) -> Result { // The groups the subscription already accounted for, which it never sends again, are // recovered on their own, once. if let Some(accounted) = accounted { @@ -186,12 +215,12 @@ impl RangeFills { for gap in self.recovering.gaps(&gone).collect::>() { for again in self.recovered.overlapping(&gap).cloned().collect::>() { let again = again.start.max(gap.start)..again.end.min(gap.end); - if !self.refused.contains_key(&again.start) { + if self.refusal(again.start).is_none() { self.refused.insert(again, StreamError::NotFound); } } for fresh in self.recovered.gaps(&gap).collect::>() { - if !self.refused.contains_key(&fresh.start) { + if self.refusal(fresh.start).is_none() { self.recover.insert(fresh); } } @@ -208,13 +237,21 @@ impl RangeFills { } // What a finished recovery did not deliver stands refused. let mut done = Vec::new(); + let mut violation = None; self.recoveries.retain_mut(|recovery| match recovery.poll(waiter) { - Poll::Ready(refused) => { + Poll::Ready(Ok(refused)) => { done.push((recovery.groups.clone(), refused)); false } + Poll::Ready(Err(err)) => { + violation.get_or_insert(err); + true + } Poll::Pending => true, }); + if let Some(err) = violation { + return Err(err); + } for (groups, refused) in done { self.recovering.remove(groups); for (groups, err) in refused.iter() { @@ -225,9 +262,9 @@ impl RangeFills { let mut ask = FillAsk::default(); // Every run a held fill still owes, recovered or not. let mut owed = RangeSet::new(); - let refused = &self.refused; - let recovering = &self.recovering; - self.held.retain_mut(|fill| { + let mut held = std::mem::take(&mut self.held); + let this = &*self; + held.retain_mut(|fill| { if fill.poll_withdrawn(waiter).is_ready() { return false; } @@ -243,20 +280,21 @@ impl RangeFills { Order::Ascending => run.start, Order::Descending => run.end - 1, }; - if let Some((groups, err)) = refused.get_key_value(&first) { + if let Some((groups, err)) = this.refusal(first) { let groups = run.start.max(groups.start)..run.end.min(groups.end); - refuse_fill(fill, groups, err.clone()); + refuse_fill(fill, groups, err); continue; } owed.insert(run.clone()); // A run being recovered waits on its own subscription. - for gap in recovering.gaps(&run) { + for gap in this.recovering.gaps(&run) { ask.groups.insert(gap); } ask.add(fill); return true; } }); + self.held = held; // A recovery no held fill owes any group of is cancelled, once none of its streams is // still being read or nobody reads the track, and a group is recovered once for the @@ -311,10 +349,25 @@ impl RangeFills { } ask.groups = groups; if ask == self.ask { - return false; + return Ok(false); } + self.retire_skipped(&ask.groups); self.ask = ask; - true + Ok(true) + } + + /// Asking for `asked` from now on retires the range of each run not asked whole any more, + /// and what its start skipped with it. + fn retire_skipped(&mut self, asked: &RangeSet) { + let retired: Vec<_> = self + .skipped + .iter() + .filter(|(_, run)| asked.get(&run.start) != Some(*run)) + .map(|(groups, _)| groups.clone()) + .collect(); + for groups in retired { + self.skipped.remove(groups); + } } } @@ -338,6 +391,10 @@ struct Recovery { state: RecoveryState, /// The groups this subscription said it will not deliver, and why. refused: RangeMap, + /// Whether SUBSCRIBE_START arrived. + started: bool, + /// Whether SUBSCRIBE_FINISH arrived. + ended: bool, /// Whether our FIN went out. finished: bool, } @@ -372,6 +429,8 @@ impl Recovery { groups, state: RecoveryState::Establish(Box::new(establish)), refused: Default::default(), + started: false, + ended: false, finished: false, } } @@ -409,15 +468,16 @@ impl Recovery { } } - /// Ready once the recovery is over, with the groups it refused. - fn poll(&mut self, waiter: &kio::Waiter) -> Poll> { + /// Ready once the recovery is over, with the groups it refused, or with the protocol + /// violation its publisher committed, which the subscription's would be too. + fn poll(&mut self, waiter: &kio::Waiter) -> Poll, Error>> { loop { match &mut self.state { RecoveryState::Establish(est) => match ready!(est.poll(waiter)) { Ok(active) => self.state = RecoveryState::Active(Box::new(active)), Err(err) => { tracing::debug!(%err, "recovery subscribe failed"); - self.end(StreamError::Cancel); + self.end(StreamError::from(&err)); } }, RecoveryState::Active(active) => { @@ -461,9 +521,11 @@ impl Recovery { } }; match res { - Ok(Some(lite::SubscribeResponse::Start(start))) => { + // One start, for its one range. + Ok(Some(lite::SubscribeResponse::Start(start))) if !self.started && start.starts.len() == 1 => { + self.started = true; // Below its start the publisher delivers nothing. - let from = start.starts.first().copied().flatten().unwrap_or(groups.end); + let from = start.starts[0].unwrap_or(groups.end); given_up( groups.start..from.clamp(groups.start, groups.end), StreamError::NotFound, @@ -473,21 +535,38 @@ impl Recovery { given_up(dropped.start..dropped.end.saturating_add(1), dropped.error(version)); } // Nothing at or past the track's end will be produced. - Ok(Some(lite::SubscribeResponse::Finish(finish))) => { + Ok(Some(lite::SubscribeResponse::Finish(finish))) if !self.ended => { + self.ended = true; given_up(finish.group.max(groups.start)..groups.end, StreamError::NotFound); } + Ok(Some(lite::SubscribeResponse::Ok(_))) => {} + // A second START or FINISH, or a STARTED for a range no update added. Ok(Some(msg)) => { tracing::warn!(?msg, "unexpected recovery subscribe response"); - self.end(StreamError::Cancel); + return Poll::Ready(Err(Error::ProtocolViolation)); + } + // The publisher FINs in reply to ours, or once it finished the track. + Ok(None) if self.finished || self.ended => self.end(StreamError::NotFound), + Ok(None) => { + tracing::warn!("recovery subscribe ended without SUBSCRIBE_FINISH"); + return Poll::Ready(Err(Error::ProtocolViolation)); } - Ok(None) => self.end(StreamError::NotFound), Err(err) => { + // Malformed bytes are a violation, as on the subscription itself; a + // reset gives up what is left with its reason. + let err = match err { + Error::Decode(crate::DecodeError::Short) => err, + err => crate::auth::protocol_error(err), + }; + if matches!(err, Error::ProtocolViolation) { + return Poll::Ready(Err(err)); + } tracing::debug!(%err, "recovery subscribe ended"); - self.end(StreamError::Cancel); + self.end(StreamError::from(&err)); } } } - RecoveryState::Done => return Poll::Ready(std::mem::take(&mut self.refused)), + RecoveryState::Done => return Poll::Ready(Ok(std::mem::take(&mut self.refused))), } } } @@ -528,4 +607,30 @@ mod tests { fills.refuse(5..6, StreamError::NotFound); assert_eq!(fills.refused.get(&5), Some(&StreamError::NotFound)); } + + /// A range starting past what it asked refuses those groups only while that range is live: + /// a later range asking for them again may start lower. + #[test] + fn a_skipped_start_stands_only_while_its_range_does() { + let mut fills = RangeFills::default(); + fills.ask.groups.insert(2..6); + fills.ask.groups.insert(8..10); + fills.skip(2..6, 2..4); + fills.skip(8..10, 8..9); + assert_eq!(fills.refusal(3), Some((2..4, StreamError::NotFound))); + + // Asking 2..6 whole keeps its range; asking 8..12 retires the range for 8..10. + let mut asked = RangeSet::new(); + asked.insert(2..6); + asked.insert(8..12); + fills.retire_skipped(&asked); + assert_eq!(fills.refusal(3), Some((2..4, StreamError::NotFound))); + assert_eq!(fills.refusal(8), None, "the new range says where it starts"); + + // A drop stands whatever is asked. + fills.refuse(4..5, StreamError::NotFound); + fills.retire_skipped(&RangeSet::new()); + assert_eq!(fills.refusal(3), None); + assert_eq!(fills.refusal(4), Some((4..5, StreamError::NotFound))); + } } diff --git a/rs/moq-net/tests/integration/range_fill.rs b/rs/moq-net/tests/integration/range_fill.rs index a6dd18308be..b31ba7bf89c 100644 --- a/rs/moq-net/tests/integration/range_fill.rs +++ b/rs/moq-net/tests/integration/range_fill.rs @@ -157,6 +157,25 @@ async fn evicted_history_is_recovered_once_on_its_own_subscription() { .expect("timed out"); } +/// The track finishing while a recovery is still out doesn't cut it: the copy ends only once +/// the recovered groups arrived, which the live subscription's tail never waits for. +#[kio::test] +async fn a_recovery_outlives_the_track_finishing() { + kio::clock::timeout(TIMEOUT + cache::DEFAULT_EXPIRY * 3, async { + let fixture = Fixture::new(6).await; + let mut first = fixture.subscribe([Range::oldest()]).await; + assert_eq!(receive(&mut first, 6).await, (0..6).collect()); + + // Superseded, the copy expires them, while the reader keeps the subscription live. + kio::clock::sleep(cache::DEFAULT_EXPIRY * 2 + Duration::from_secs(2)).await; + let mut second = fixture.subscribe([Range::groups(1..4)]).await; + fixture.track.finish().unwrap(); + assert_eq!(receive(&mut second, 3).await, (1..4).collect()); + }) + .await + .expect("timed out"); +} + /// One group, or a slice of one, is what lite-07 FETCH carries, so a miss of one group still /// fetches it. #[kio::test] From e3b0663bb8ceb4246fb105ddff7bc96e4550ca51 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 4 Nov 2026 12:48:33 -0800 Subject: [PATCH 25/30] fix(net): pin a settled time end too, as a range resolves once Co-Authored-By: Claude Opus 5.5 --- js/net/src/lite/publisher.ts | 3 ++- rs/moq-net/src/lite/publisher.rs | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/js/net/src/lite/publisher.ts b/js/net/src/lite/publisher.ts index 7565319814d..21e548ab007 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -408,7 +408,8 @@ function pin(entry: RangeEntry, resolved: Resolved | undefined): RangeEntry { range.start = { absolute: resolved.start }; from = [resolved.start, 0]; } - if (range.end?.relative !== undefined && resolved.end !== undefined) { + // An end resolves once it settles; until then it is reported open. + if ((range.end?.relative !== undefined || range.end?.time !== undefined) && resolved.end !== undefined) { range.end = { absolute: resolved.end }; to = [resolved.end, 0]; } diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 41e27b1015b..6589b97773d 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -4784,7 +4784,8 @@ impl TrackRun { if pinned { range.start = Some(lite::Anchor::Absolute(track::Position::group(start))); } - if let (Some(lite::Anchor::Relative(_)), Some(end)) = (range.end, end) { + // An end resolves once it settles; until then it is reported open. + if let (Some(lite::Anchor::Relative(_) | lite::Anchor::Time(_)), Some(end)) = (range.end, end) { range.end = Some(lite::Anchor::Absolute(track::Position::group(end))); } } From 483305c550b2c93348a3740617a8ce1f6e84ff28 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 4 Nov 2026 14:08:32 -0800 Subject: [PATCH 26/30] fix(net): a fill refused short of a genuine miss records nothing A range's own start skip, a stale drop, and history recovered once already now refuse only the fills owing them now (Fill::refuse_groups), so a lowered start or a later reader asks again. Only a SUBSCRIBE_DROP naming the groups not found, or groups past SUBSCRIBE_FINISH, are recorded as dropped. Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/lite/subscriber/fills.rs | 192 ++++++++++++++++++++---- rs/moq-net/src/model/track.rs | 6 + 2 files changed, 165 insertions(+), 33 deletions(-) diff --git a/rs/moq-net/src/lite/subscriber/fills.rs b/rs/moq-net/src/lite/subscriber/fills.rs index f5e272a0500..b02b06a6724 100644 --- a/rs/moq-net/src/lite/subscriber/fills.rs +++ b/rs/moq-net/src/lite/subscriber/fills.rs @@ -107,7 +107,7 @@ pub(super) struct RangeFills { /// Asked groups no fill owes any more, already searched for streams still being read. draining: RangeSet, /// Groups the subscription said it will not deliver, and why. - refused: RangeMap, + refused: RangeMap, /// Groups below where a fill's range starts, by the asked run that range carries, which /// stand only while that range is live. skipped: RangeMap>, @@ -147,14 +147,14 @@ impl RangeFills { for fresh in self.recovered.gaps(&groups).collect::>() { self.recover.insert(fresh); } - // Recovered once already: this drop stands. + // Recovered once already: this drop stands for the fills owing it now. for again in self.recovered.overlapping(&groups).cloned().collect::>() { let again = again.start.max(groups.start)..again.end.min(groups.end); - self.refused.insert(again, err.clone()); + self.refused.insert(again, Refusal::dropped(err.clone())); } continue; } - self.refused.insert(groups, err.clone()); + self.refused.insert(groups, Refusal::dropped(err.clone())); } } @@ -179,12 +179,13 @@ impl RangeFills { } /// Why the subscription will not deliver the run of groups from `first`, if it said so. - fn refusal(&self, first: u64) -> Option<(Range, StreamError)> { - if let Some((groups, err)) = self.refused.get_key_value(&first) { - return Some((groups.clone(), err.clone())); + fn refusal(&self, first: u64) -> Option<(Range, Refusal)> { + if let Some((groups, refusal)) = self.refused.get_key_value(&first) { + return Some((groups.clone(), refusal.clone())); } + // The subscriber's own range starting past them says nothing of the source. let (groups, _) = self.skipped.get_key_value(&first)?; - Some((groups.clone(), StreamError::NotFound)) + Some((groups.clone(), Refusal::passing(StreamError::NotFound))) } /// Settle each held fill against what the subscription refused, open a [`Recovery`] for @@ -216,7 +217,8 @@ impl RangeFills { for again in self.recovered.overlapping(&gap).cloned().collect::>() { let again = again.start.max(gap.start)..again.end.min(gap.end); if self.refusal(again.start).is_none() { - self.refused.insert(again, StreamError::NotFound); + // Recovered once for these fills; a later request may recover it again. + self.refused.insert(again, Refusal::passing(StreamError::NotFound)); } } for fresh in self.recovered.gaps(&gap).collect::>() { @@ -254,8 +256,8 @@ impl RangeFills { } for (groups, refused) in done { self.recovering.remove(groups); - for (groups, err) in refused.iter() { - self.refused.insert(groups.clone(), err.clone()); + for (groups, refusal) in refused.iter() { + self.refused.insert(groups.clone(), refusal.clone()); } } @@ -280,9 +282,9 @@ impl RangeFills { Order::Ascending => run.start, Order::Descending => run.end - 1, }; - if let Some((groups, err)) = this.refusal(first) { + if let Some((groups, refusal)) = this.refusal(first) { let groups = run.start.max(groups.start)..run.end.min(groups.end); - refuse_fill(fill, groups, err); + refusal.answer(fill, groups); continue; } owed.insert(run.clone()); @@ -371,13 +373,39 @@ impl RangeFills { } } -/// Answer `groups` of `fill` as the subscription that asked for them will not deliver them, -/// with why: a miss names them dropped, as a FETCH's miss does. -fn refuse_fill(fill: &track::Fill, groups: Range, err: StreamError) { - match err { - StreamError::NotFound => fill.drop_groups(groups), - StreamError::Old => fill.reject_groups(groups, Error::Old), - err => fill.refuse_groups(groups, Error::Stream(err)), +/// Why the subscription that asked for groups will not deliver them. +#[derive(Clone, Debug, PartialEq, Eq)] +struct Refusal { + err: StreamError, + /// Whether the source no longer has them, which the track records so no reader asks + /// again: only a genuine miss, which its publisher named. Anything else answers only the + /// fills owing them now, since another request may still get them: a stale drop judges + /// a budget, and a range starting past them is the subscriber's own choice. + lost: bool, +} + +impl Refusal { + /// The publisher gave the groups up with `err` (a SUBSCRIBE_DROP, or an end before them): + /// lost only when it never had them. + fn dropped(err: StreamError) -> Self { + let lost = err == StreamError::NotFound; + Self { err, lost } + } + + /// Not delivered on this subscription, which says nothing of the source. + fn passing(err: StreamError) -> Self { + Self { err, lost: false } + } + + /// Answer `groups` of `fill`: a lost group is named dropped, as a FETCH's miss is, and + /// anything else is refused for now, recorded nowhere. + fn answer(&self, fill: &track::Fill, groups: Range) { + match &self.err { + StreamError::NotFound if self.lost => fill.drop_groups(groups), + StreamError::NotFound => fill.refuse_groups(groups, Error::NotFound), + StreamError::Old => fill.refuse_groups(groups, Error::Old), + err => fill.refuse_groups(groups, Error::Stream(err.clone())), + } } } @@ -390,7 +418,7 @@ struct Recovery { groups: Range, state: RecoveryState, /// The groups this subscription said it will not deliver, and why. - refused: RangeMap, + refused: RangeMap, /// Whether SUBSCRIBE_START arrived. started: bool, /// Whether SUBSCRIBE_FINISH arrived. @@ -458,7 +486,7 @@ impl Recovery { owed.remove(refused.clone()); } for gap in owed.iter() { - self.refused.insert(gap.clone(), err.clone()); + self.refused.insert(gap.clone(), Refusal::passing(err.clone())); } self.serve.subscriber.remove_subscribe(self.id); match state { @@ -470,7 +498,7 @@ impl Recovery { /// Ready once the recovery is over, with the groups it refused, or with the protocol /// violation its publisher committed, which the subscription's would be too. - fn poll(&mut self, waiter: &kio::Waiter) -> Poll, Error>> { + fn poll(&mut self, waiter: &kio::Waiter) -> Poll, Error>> { loop { match &mut self.state { RecoveryState::Establish(est) => match ready!(est.poll(waiter)) { @@ -511,13 +539,13 @@ impl Recovery { let version = self.serve.subscriber.version; let groups = self.groups.clone(); // What the publisher gives up of the groups, accounted for in the tail. - let mut given_up = |given: Range, err: StreamError| { + let mut given_up = |given: Range, refusal: Refusal| { if let Ok(mut tail) = active.tail.write() { tail.account(given.clone(), now); } let given = given.start.max(groups.start)..given.end.min(groups.end); if !given.is_empty() { - self.refused.insert(given, err); + self.refused.insert(given, refusal); } }; match res { @@ -528,16 +556,22 @@ impl Recovery { let from = start.starts[0].unwrap_or(groups.end); given_up( groups.start..from.clamp(groups.start, groups.end), - StreamError::NotFound, + Refusal::passing(StreamError::NotFound), ); } Ok(Some(lite::SubscribeResponse::Drop(dropped))) => { - given_up(dropped.start..dropped.end.saturating_add(1), dropped.error(version)); + given_up( + dropped.start..dropped.end.saturating_add(1), + Refusal::dropped(dropped.error(version)), + ); } // Nothing at or past the track's end will be produced. Ok(Some(lite::SubscribeResponse::Finish(finish))) if !self.ended => { self.ended = true; - given_up(finish.group.max(groups.start)..groups.end, StreamError::NotFound); + given_up( + finish.group.max(groups.start)..groups.end, + Refusal::dropped(StreamError::NotFound), + ); } Ok(Some(lite::SubscribeResponse::Ok(_))) => {} // A second START or FINISH, or a STARTED for a range no update added. @@ -600,12 +634,104 @@ mod tests { fills.recover = Default::default(); fills.refuse(2..5, StreamError::Old); assert_eq!(fills.recover.iter().cloned().collect::>(), [2..3]); - assert_eq!(fills.refused.get(&3), Some(&StreamError::Old)); + assert_eq!(fills.refused.get(&3), Some(&Refusal::dropped(StreamError::Old))); + assert!(!fills.refused.get(&3).unwrap().lost, "a stale drop is never a loss"); fills.refuse(7..9, StreamError::NotFound); assert_eq!(fills.refused.get(&7), None, "no fill asked for it"); fills.refuse(5..6, StreamError::NotFound); - assert_eq!(fills.refused.get(&5), Some(&StreamError::NotFound)); + assert_eq!(fills.refused.get(&5), Some(&Refusal::dropped(StreamError::NotFound))); + assert!(fills.refused.get(&5).unwrap().lost, "the publisher never had it"); + } + + /// A track whose cache evicted groups 1..4, with a reader of them whose fill is the next + /// request. + fn missed() -> (track::Producer, track::Requests, track::Ordered, track::Fill) { + let producer = track::Producer::new( + std::sync::Arc::new(crate::broadcast::Info::default()), + "test", + track::Info::default() + .with_timescale(crate::Timescale::MILLI) + .with_max_age(None), + ); + let requests = producer.requests(); + for sequence in 0..6 { + let mut group = producer.create_group(group::Info { sequence }).unwrap(); + group + .write_frame(crate::Timestamp::from_millis(sequence * 1000).unwrap(), b"x".as_slice()) + .unwrap(); + group.finish().unwrap(); + } + for sequence in 1..4 { + producer.evict(sequence); + } + let (reader, fill) = ask(&producer, &requests); + (producer, requests, reader, fill) + } + + /// A reader of groups 1..4, and the fill it asks for. + fn ask(producer: &track::Producer, requests: &track::Requests) -> (track::Ordered, track::Fill) { + let waiter = kio::Waiter::noop(); + let mut reader = producer + .subscribe( + Subscription::default() + .with_delay(Delay::default().with_max(None)) + .with_range(1..4), + ) + .ordered(); + assert!(reader.poll_next_group(&waiter).is_pending(), "groups 1..4 are owed"); + let Poll::Ready(Ok(fill)) = requests.poll_requested_fill(&waiter) else { + panic!("groups 1..4 are a miss"); + }; + (reader, fill) + } + + /// A range starting past what a fill asked is the subscriber's own choice: the fill's + /// readers stop waiting, but nothing is recorded, so once a lowered start reaches the + /// groups a later reader asks again and gets them. A genuine miss stays dropped. + #[test] + fn a_skipped_start_is_never_a_loss() { + let waiter = kio::Waiter::noop(); + let (producer, requests, mut reader, fill) = missed(); + Refusal::passing(StreamError::NotFound).answer(&fill, 1..4); + assert!( + reader.poll_next_group(&waiter).is_ready(), + "the reader stops waiting for the skipped groups" + ); + drop((reader, fill)); + + // The lowered start asks for them again, and they arrive. + let (mut reader, fill) = ask(&producer, &requests); + for sequence in 1..4 { + let mut group = fill.accept(sequence, None).unwrap(); + group + .write_frame(crate::Timestamp::from_millis(sequence * 1000).unwrap(), b"x".as_slice()) + .unwrap(); + group.finish().unwrap(); + } + for sequence in 1..4 { + let Poll::Ready(Ok(Some(group))) = reader.poll_next_group(&waiter) else { + panic!("group {sequence} was delivered"); + }; + assert_eq!(group.sequence, sequence); + } + + // The publisher naming them not found is a loss: nobody asks again. + let (producer, requests, reader, fill) = missed(); + Refusal::dropped(StreamError::NotFound).answer(&fill, 1..4); + drop((reader, fill)); + let mut reader = producer + .subscribe( + Subscription::default() + .with_delay(Delay::default().with_max(None)) + .with_range(1..4), + ) + .ordered(); + let _ = reader.poll_next_group(&waiter); + assert!( + requests.poll_requested_fill(&waiter).is_pending(), + "a lost group is not asked again" + ); } /// A range starting past what it asked refuses those groups only while that range is live: @@ -617,20 +743,20 @@ mod tests { fills.ask.groups.insert(8..10); fills.skip(2..6, 2..4); fills.skip(8..10, 8..9); - assert_eq!(fills.refusal(3), Some((2..4, StreamError::NotFound))); + assert_eq!(fills.refusal(3), Some((2..4, Refusal::passing(StreamError::NotFound)))); // Asking 2..6 whole keeps its range; asking 8..12 retires the range for 8..10. let mut asked = RangeSet::new(); asked.insert(2..6); asked.insert(8..12); fills.retire_skipped(&asked); - assert_eq!(fills.refusal(3), Some((2..4, StreamError::NotFound))); + assert_eq!(fills.refusal(3), Some((2..4, Refusal::passing(StreamError::NotFound)))); assert_eq!(fills.refusal(8), None, "the new range says where it starts"); // A drop stands whatever is asked. fills.refuse(4..5, StreamError::NotFound); fills.retire_skipped(&RangeSet::new()); assert_eq!(fills.refusal(3), None); - assert_eq!(fills.refusal(4), Some((4..5, StreamError::NotFound))); + assert_eq!(fills.refusal(4), Some((4..5, Refusal::dropped(StreamError::NotFound)))); } } diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 41814f7c9de..b8eb9943004 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -3706,6 +3706,12 @@ impl Producer { self.state.read().start_sequence } + /// Evict group `sequence` from the cache, as expiry does. + #[cfg(test)] + pub(crate) fn evict(&self, sequence: u64) { + self.modify().unwrap().evict_group(sequence, Error::Evicted); + } + /// The exclusive end, once [`Self::finish`] or [`Self::finish_at`] declared one. /// /// `None` while the track is still open ended. Both methods reject a second end, so From 10611bf3a7a3ded8ad8f023b93f26fcdb22ae127 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 4 Nov 2026 14:18:34 -0800 Subject: [PATCH 27/30] fix(net): pin a time start that names group 0 too A time start resolved to group 0 can still move, once a newer group starts before its time. The window's Resolved now says whether its start names a group, and both publishers pin every start that does; one reaching back to the oldest group held stays as it was. Co-Authored-By: Claude Opus 5.5 --- js/net/src/lite/publisher.test.ts | 37 +++++ js/net/src/lite/publisher.ts | 8 +- js/net/src/window.test.ts | 18 ++- js/net/src/window.ts | 10 +- rs/moq-net/src/lite/publisher.rs | 97 +++++++------ rs/moq-net/src/model/subscription.rs | 3 +- rs/moq-net/src/model/subscription/window.rs | 148 ++++++++++++++++---- rs/moq-net/src/model/track.rs | 34 +++-- 8 files changed, 264 insertions(+), 91 deletions(-) diff --git a/js/net/src/lite/publisher.test.ts b/js/net/src/lite/publisher.test.ts index 933c43d0a69..f2f229045e7 100644 --- a/js/net/src/lite/publisher.test.ts +++ b/js/net/src/lite/publisher.test.ts @@ -988,6 +988,43 @@ test("lite-07 keeps where an added time start resolved", async () => { } }); +// Group 0 included: a time start that resolved to group 0 names it, so a newer group starting +// before its time doesn't move it either. +test("lite-07 keeps an added time start that resolved to group 0", async () => { + const version = Version.DRAFT_07; + const sub = await servedSubscription({ + version, + prefill: [0n], + maxAge: Milli(3_600_000), + ranges: [{ start: { absolute: 0n }, end: { absolute: 1n } }], + }); + const next = async () => { + for (;;) { + const resp = await decodeSubscribeResponse(sub.client.reader, version); + if ("start" in resp || "started" in resp) return resp; + } + }; + try { + if (!("start" in (await next()))) throw new Error("expected SUBSCRIBE_OK"); + + // 10 ms falls in group 0, the only group. + await replayUpdate({ priority: 0, ranges: [{ start: { time: 10 } }] }).encode(sub.client.writer, version); + const started = await next(); + if (!("started" in started)) throw new Error("expected SUBSCRIBE_STARTED"); + expect([started.started.id, started.started.start]).toEqual([1n, 0n]); + + // Group 1 starts at 1 ms, before 10 ms; the range still starts at group 0. + sub.serve(1n); + await replayUpdate({ priority: 0, ranges: [{ start: { absolute: 9n } }] }).encode(sub.client.writer, version); + if (!("started" in (await next()))) throw new Error("expected SUBSCRIBE_STARTED"); + const ranges = sub.track.subscription.peek()?.ranges ?? []; + expect(ranges.some((range) => range.start?.time !== undefined)).toBe(false); + expect(ranges.some((range) => range.start?.absolute === 0n && range.end === undefined)).toBe(true); + } finally { + await sub.close(); + } +}); + // An edit naming a Range ID the subscription never assigned violates the protocol. test("lite-07 refuses an update retiring an unknown Range ID", async () => { const version = Version.DRAFT_07; diff --git a/js/net/src/lite/publisher.ts b/js/net/src/lite/publisher.ts index 21e548ab007..f384269fe29 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -397,14 +397,14 @@ class SubscriptionControls { // `entry`, a range this subscription already held, resolved as it was: a range resolves once, so // an update that leaves it live never counts its relative anchors back from a newer latest group, -// nor looks a time start up again in newer holdings. A time start resolved at group 0 stays: it -// either names group 0, below which nothing sits, or reaches back to the oldest group held, -// which an absolute start would turn into a floor the subscriber never named. +// nor looks a time start up again in newer holdings. A start reaching back to the oldest group +// held stays as it was, since an absolute start would turn it into a floor the subscriber never +// named. function pin(entry: RangeEntry, resolved: Resolved | undefined): RangeEntry { if (!resolved) return entry; const range = { ...entry.range }; let { from, to } = entry; - if (range.start?.relative !== undefined || (range.start?.time !== undefined && resolved.start > 0n)) { + if (resolved.floored && (range.start?.relative !== undefined || range.start?.time !== undefined)) { range.start = { absolute: resolved.start }; from = [resolved.start, 0]; } diff --git a/js/net/src/window.test.ts b/js/net/src/window.test.ts index 790dc100a72..245c41d5937 100644 --- a/js/net/src/window.test.ts +++ b/js/net/src/window.test.ts @@ -60,7 +60,11 @@ test("a window reads only its ranges, counting relative anchors back from the la { start: 1n, end: 4n }, { start: 9n, end: 12n }, ]); - expect(window.resolved()).toEqual([{ start: 2n, end: 4n }, { start: 9n }, { start: 0n, end: 3n }]); + expect(window.resolved()).toEqual([ + { start: 2n, end: 4n, floored: true }, + { start: 9n, floored: true }, + { start: 0n, end: 3n, floored: true }, + ]); }); test("a relative anchor clamps to group 0 and waits for a first group", () => { @@ -100,7 +104,7 @@ test("a wire without ranges carries one span covering them all", () => { test("clamping raises starts to the floor and caps ends without widening", () => { const ranges: Range[] = [abs(1n, 9n), {}, { start: { relative: 2n }, end: { relative: 0n } }]; - expect(clamped(ranges, 4n, 6n, [undefined, undefined, { start: 8n, end: 11n }])).toEqual([ + expect(clamped(ranges, 4n, 6n, [undefined, undefined, { start: 8n, end: 11n, floored: true }])).toEqual([ abs(4n, 6n), abs(4n, 6n), abs(8n, 6n), @@ -179,13 +183,13 @@ test("a time start is the group containing it, and an end stops past it once a l let window = new Window([{ start: time(3500), end: time(4500) }], 6n, groups); expect(covered(window, 8)).toEqual([3n, 4n]); expect(window.settled).toBe(true); - expect(window.resolved()).toEqual([{ start: 3n, end: 5n }]); + expect(window.resolved()).toEqual([{ start: 3n, end: 5n, floored: true }]); // Group 6 may yet start at 5000 too: read on speculation, the end left open. window = new Window([{ start: time(3500), end: time(5000) }], 6n, groups); expect(window.settled).toBe(false); expect(covered(window, 9)).toEqual([3n, 4n, 5n, 6n]); - expect(window.resolved()).toEqual([{ start: 3n, end: undefined }]); + expect(window.resolved()).toEqual([{ start: 3n, end: undefined, floored: true }]); expect(window.end()).toBeUndefined(); // Before every start held: reach back to the oldest. @@ -219,7 +223,7 @@ test("a waiting time start keeps the relative end it was asked with", () => { groups.held.set(2n, started(2000)); expect(window.settle(10n, groups)).toBe(true); - expect(window.resolved()).toEqual([{ start: 2n, end: 10n }]); + expect(window.resolved()).toEqual([{ start: 2n, end: 10n, floored: true }]); } }); @@ -328,7 +332,7 @@ test("a datagram is judged by its own timestamp, one range at a time", () => { ]); groups.gones.push({ start: 1n, end: 2n }); const settled = new Window([{ start: time(0), end: time(5000) }], undefined, groups); - expect(settled.resolved()).toEqual([{ start: 0n, end: 2n }]); + expect(settled.resolved()).toEqual([{ start: 0n, end: 2n, floored: true }]); expect(settled.admitsDatagram(1n, Timestamp.fromMillis(4000))).toBe(true); expect(settled.admitsDatagram(1n, Timestamp.fromMillis(7000))).toBe(false); @@ -356,7 +360,7 @@ test("a time anchor needs moq-lite-07, is resolved by the copy, and clamps to it { start: time(5) }, ]); // Resolved anchors become their groups; an unresolved time end stays so the cap applies apart. - expect(clamped([{ start: time(1), end: time(5) }], 2n, 9n, [{ start: 3n, end: 7n }])).toEqual([ + expect(clamped([{ start: time(1), end: time(5) }], 2n, 9n, [{ start: 3n, end: 7n, floored: true }])).toEqual([ { start: { absolute: 3n }, end: { absolute: 7n } }, ]); expect(clamped([{ start: time(1), end: time(5) }], 2n, 9n, [])).toEqual([{ start: time(1), end: time(5) }]); diff --git a/js/net/src/window.ts b/js/net/src/window.ts index 9f3921eb0f8..7e435bf0898 100644 --- a/js/net/src/window.ts +++ b/js/net/src/window.ts @@ -16,8 +16,11 @@ import { max, min, saturatingSub, UNBOUNDED } from "./util/sequence.ts"; /** A run of group sequences `[start, end)`; an open run ends at {@link UNBOUNDED}. */ export type Run = { start: bigint; end: bigint }; -/** One range resolved to groups: from `start` up to the exclusive `end`, undefined while open. */ -export type Resolved = { start: bigint; end?: bigint }; +/** + * One range resolved to groups: from `start` up to the exclusive `end`, undefined while open. + * `floored` says whether the start names a group, rather than reaching back to the oldest one. + */ +export type Resolved = { start: bigint; end?: bigint; floored: boolean }; /** Everything from the latest group onward. */ export const LIVE: Range = Object.freeze({ start: Object.freeze({ relative: 0n }) }); @@ -685,12 +688,13 @@ export class Window { const resolved: (Resolved | undefined)[] = this.ranges.map((range) => { if (!isEmpty(range)) return undefined; const end = absolute(range.end) ?? 0n; - return { start: (range.start && absolute(range.start)) ?? end, end }; + return { start: (range.start && absolute(range.start)) ?? end, end, floored: true }; }); for (const span of this.#spans) { resolved[span.index] = { start: span.start, end: span.end !== "open" && "before" in span.end ? span.end.before : undefined, + floored: span.floored, }; } return resolved; diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 777855f53d9..21ff0b295d8 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -4858,10 +4858,9 @@ impl TrackRun { /// `ranges`, numbered by `ids`, with each range this subscription already held resolved /// as it was: a range resolves once, so an update that leaves it live never counts its /// relative anchors back from a newer latest group, nor looks a time start up again in - /// newer groups. A time start resolved at group 0 stays: it either names group 0, below - /// which nothing sits, or reaches back to the oldest group held, which an absolute start - /// would turn into a floor the subscriber never named. Lite-07 only: an older update - /// replaces every range. + /// newer groups. A start reaching back to the oldest group held stays as it was, since an + /// absolute start would turn it into a floor the subscriber never named. Lite-07 only: an + /// older update replaces every range. fn pin(&mut self, ids: &[u64], mut ranges: Vec) -> Vec { if !self.ctx.version.has_ranges() { return ranges; @@ -4869,22 +4868,17 @@ impl TrackRun { let resolved = self.track.resolved(); let positions = self.positions(); for (id, range) in ids.iter().zip(&mut ranges) { - let Some((start, end)) = positions + let Some(resolved) = positions .get(id) .and_then(|index| resolved.get(*index).copied().flatten()) else { continue; }; - let pinned = match range.start { - Some(lite::Anchor::Relative(_)) => true, - Some(lite::Anchor::Time(_)) => start > 0, - _ => false, - }; - if pinned { - range.start = Some(lite::Anchor::Absolute(track::Position::group(start))); + if resolved.floored && matches!(range.start, Some(lite::Anchor::Relative(_) | lite::Anchor::Time(_))) { + range.start = Some(lite::Anchor::Absolute(track::Position::group(resolved.start))); } // An end resolves once it settles; until then it is reported open. - if let (Some(lite::Anchor::Relative(_) | lite::Anchor::Time(_)), Some(end)) = (range.end, end) { + if let (Some(lite::Anchor::Relative(_) | lite::Anchor::Time(_)), Some(end)) = (range.end, resolved.end) { range.end = Some(lite::Anchor::Absolute(track::Position::group(end))); } } @@ -4912,9 +4906,9 @@ impl TrackRun { for id in std::mem::take(&mut self.unstarted) { let index = *positions.get(&id).expect("unstarted ranges are live"); let start = match resolved.get(index).copied().flatten() { - Some((start, end)) => { - let start = start.max(from); - end.is_none_or(|end| end > start).then_some(start) + Some(resolved) => { + let start = resolved.start.max(from); + resolved.end.is_none_or(|end| end > start).then_some(start) } None if finished => None, None => { @@ -7173,50 +7167,73 @@ mod serve_group_test { } let live = [(0, first), (1, back)]; run.update(lite07_add(&live, 2, &[lite::Range::span(Some(9), 0, None, None)])); - assert_eq!(run.track.resolved()[1], Some((2, None)), "the update resolved it again"); - } - - /// A time start resolves once too: a newer group starting before its time doesn't move it - /// once an update leaves the range live. - #[kio::test] - async fn lite07_an_added_time_start_keeps_its_start() { + let resolved = run.track.resolved()[1].map(|r| (r.start, r.end)); + assert_eq!(resolved, Some((2, None)), "the update kept where it started"); + } + + /// Where an added time start `time` resolves among `groups` (sequence and start, in + /// milliseconds), before and after `newer` arrives and another update leaves it live, with + /// whether it names that group. The run serves group `first` first. + async fn time_start_across_an_update( + groups: &[(u64, u64)], + first: u64, + time: u64, + newer: (u64, u64), + ) -> [(u64, bool); 2] { let mut track = track::Producer::new( Arc::new(broadcast::Info::default()), "test", crate::track::Info::timed(), ); - for sequence in 0..5 { - write_group(&mut track, sequence, sequence * 1000); + for (sequence, millis) in groups { + write_group(&mut track, *sequence, *millis); } - let first = lite::Range::span(Some(0), 0, Some(0), None); + let served = lite::Range::span(Some(first), 0, Some(first), None); let subscriber = track.subscribe( track::Subscription::default() .with_delay(track::Delay::default().with_max(Duration::from_secs(3600))) - .with_range(0..1), + .with_range(first..first + 1), ); let (mut run, mut writer, _log) = lite07_run(SinkSession::new(Log::default()), subscriber); while run.start.is_none() { assert!(run.poll_step(&mut writer, &kio::Waiter::noop()).is_ready()); } - - // 4.5 s falls in group 4, the newest group starting at or before it. let time = lite::Range { - start: Some(lite::Anchor::Time(4500)), + start: Some(lite::Anchor::Time(time)), end: None, }; - run.update(lite07_add(&[(0, first)], 1, &[time])); + run.update(lite07_add(&[(0, served)], 1, &[time])); while run.poll_step(&mut writer, &kio::Waiter::noop()).is_ready() {} - assert_eq!(run.track.resolved()[1], Some((4, None))); + let before = run.track.resolved()[1].expect("resolved"); - // Group 5 starts before 4.5 s, and another update leaves the range live: it still - // starts at 4. - write_group(&mut track, 5, 4200); - let live = [(0, first), (1, time)]; - run.update(lite07_add(&live, 2, &[lite::Range::span(Some(9), 0, None, None)])); + write_group(&mut track, newer.0, newer.1); + let live = [(0, served), (1, time)]; + run.update(lite07_add(&live, 2, &[lite::Range::span(Some(99), 0, None, None)])); + let after = run.track.resolved()[1].expect("resolved"); + [(before.start, before.floored), (after.start, after.floored)] + } + + /// A time start resolves once too: a newer group starting before its time doesn't move it + /// once an update leaves the range live, group 0 included. One older than every group held + /// still reaches back to the oldest, rather than becoming a floor at group 0. + #[kio::test] + async fn lite07_an_added_time_start_keeps_its_start() { + let five: Vec<_> = (0..5).map(|sequence| (sequence, sequence * 1000)).collect(); + // 4.5 s falls in group 4, the newest starting at or before it, until group 5 starts at + // 4.2 s. + assert_eq!( + time_start_across_an_update(&five, 0, 4500, (5, 4200)).await, + [(4, true), (4, true)] + ); + // 1 s falls in group 0 until group 1 starts at 0.5 s. + assert_eq!( + time_start_across_an_update(&[(0, 0)], 0, 1000, (1, 500)).await, + [(0, true), (0, true)] + ); + // 0.1 s is older than group 5, the oldest held. assert_eq!( - run.track.resolved()[1], - Some((4, None)), - "the update kept where it started" + time_start_across_an_update(&[(5, 5000)], 5, 100, (6, 6000)).await, + [(0, false), (0, false)] ); } diff --git a/rs/moq-net/src/model/subscription.rs b/rs/moq-net/src/model/subscription.rs index c0d61d143af..a2e52d9ecf7 100644 --- a/rs/moq-net/src/model/subscription.rs +++ b/rs/moq-net/src/model/subscription.rs @@ -9,7 +9,8 @@ mod runs; mod window; pub(super) use runs::{Layered, Runs}; pub(crate) use window::Accounted; -pub(super) use window::{Containing, Held, Holdings, Resolved, Seen, Unheld, Window, later}; +pub(crate) use window::Resolved; +pub(super) use window::{Containing, Held, Holdings, Seen, Unheld, Window, later}; /// Subscriber-side preferences for receiving a track. /// diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index e1ba7c81892..9c9d45bed1c 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -16,9 +16,12 @@ use crate::{Error, Result, Timestamp}; /// One range resolved to groups: from `start` up to the exclusive `end`, `None` while it /// stays open. #[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub(in crate::model) struct Resolved { +pub(crate) struct Resolved { pub start: u64, pub end: Option, + /// Whether the start names a group, rather than reaching back to the oldest one (a + /// time start older than every group held, or the oldest start). + pub floored: bool, } /// The groups a publisher accounted for, which [`Window::covered_by`] checks against: a @@ -863,9 +866,11 @@ impl Window { .ranges .iter() .map(|range| match (range.start, range.end) { - (Anchor::Absolute(start), Some(Anchor::Absolute(end))) if range.is_empty() => { - Some(Resolved { start, end: Some(end) }) - } + (Anchor::Absolute(start), Some(Anchor::Absolute(end))) if range.is_empty() => Some(Resolved { + start, + end: Some(end), + floored: true, + }), _ => None, }) .collect(); @@ -876,6 +881,7 @@ impl Window { End::Before(end) => Some(end), End::Open | End::Time(_) => None, }, + floored: span.floored, }); } resolved @@ -1458,8 +1464,16 @@ mod tests { assert_eq!( window.resolved(), vec![ - Some(Resolved { start: 2, end: Some(4) }), - Some(Resolved { start: 7, end: None }), + Some(Resolved { + start: 2, + end: Some(4), + floored: true + }), + Some(Resolved { + start: 7, + end: None, + floored: true + }), ] ); // Delivery starting inside a range raises its start; past its end leaves it none. @@ -1518,13 +1532,27 @@ mod tests { let window = Window::new(&[range(time(3500), time(4500))], Some(6), &groups).unwrap(); assert_eq!(covered(&window, 8), vec![3, 4]); assert!(window.is_settled()); - assert_eq!(window.resolved(), vec![Some(Resolved { start: 3, end: Some(5) })]); + assert_eq!( + window.resolved(), + vec![Some(Resolved { + start: 3, + end: Some(5), + floored: true + })] + ); // Group 6 may yet start at 5000 too: read on speculation, the end left open. let window = Window::new(&[range(time(3500), time(5000))], Some(6), &groups).unwrap(); assert!(!window.is_settled()); assert_eq!(covered(&window, 9), vec![3, 4, 5, 6]); - assert_eq!(window.resolved(), vec![Some(Resolved { start: 3, end: None })]); + assert_eq!( + window.resolved(), + vec![Some(Resolved { + start: 3, + end: None, + floored: true + })] + ); assert_eq!(window.end(), None, "the cap is not the end"); assert_eq!(window.speculative().collect::>(), vec![6]); @@ -1665,13 +1693,15 @@ mod tests { assert!(resolved[..split].iter().all(|r| *r == Some(Resolved { start: 0, - end: Some(count) + end: Some(count), + floored: true, + }))); + assert!(resolved[split..].iter().all(|r| *r + == Some(Resolved { + start: 0, + end: None, + floored: true }))); - assert!( - resolved[split..] - .iter() - .all(|r| *r == Some(Resolved { start: 0, end: None })) - ); // Once a group starts past every end, they all settle. groups.held.insert(count + 1, Held::Started(at(2_000_000))); @@ -1855,7 +1885,14 @@ mod tests { asked: Cell::new(false), }; let window = Window::new(&[range(Anchor::Absolute(0), time(5000))], None, &racing).unwrap(); - assert_eq!(window.resolved(), vec![Some(Resolved { start: 0, end: Some(1) })]); + assert_eq!( + window.resolved(), + vec![Some(Resolved { + start: 0, + end: Some(1), + floored: true + })] + ); } #[test] @@ -1872,7 +1909,14 @@ mod tests { assert_eq!(covered(&window, 5), vec![0, 1], "2 waits on 1 starting"); groups.held.insert(1, Held::Started(at(3600))); window.settle(Some(3), &groups); - assert_eq!(window.resolved(), vec![Some(Resolved { start: 0, end: Some(1) })]); + assert_eq!( + window.resolved(), + vec![Some(Resolved { + start: 0, + end: Some(1), + floored: true + })] + ); } #[test] @@ -1883,7 +1927,8 @@ mod tests { window.resolved(), vec![Some(Resolved { start: u64::MAX, - end: None + end: None, + floored: true, })] ); assert!(window.contains(u64::MAX)); @@ -1911,7 +1956,8 @@ mod tests { None, Some(Resolved { start: 20, - end: Some(21) + end: Some(21), + floored: true, }) ] ); @@ -1922,12 +1968,23 @@ mod tests { assert!(window.settle(Some(9), &groups)); assert_eq!( window.resolved()[0], - Some(Resolved { start: 0, end: None }), + Some(Resolved { + start: 0, + end: None, + floored: false + }), "3 starts past it" ); groups.held.insert(2, Held::Started(at(2000))); let window = Window::new(&ranges, Some(9), &groups).unwrap(); - assert_eq!(window.resolved()[0], Some(Resolved { start: 2, end: None })); + assert_eq!( + window.resolved()[0], + Some(Resolved { + start: 2, + end: None, + floored: true + }) + ); assert!(window.is_settled()); let groups = Groups { @@ -1935,7 +1992,14 @@ mod tests { ..Groups::new(pending) }; let window = Window::new(&ranges, Some(9), &groups).unwrap(); - assert_eq!(window.resolved()[0], Some(Resolved { start: 0, end: None })); + assert_eq!( + window.resolved()[0], + Some(Resolved { + start: 0, + end: None, + floored: false + }) + ); } /// A relative end counts back from the latest group it was asked against, however many @@ -1959,7 +2023,8 @@ mod tests { window.resolved()[0], Some(Resolved { start: 2, - end: Some(10) + end: Some(10), + floored: true, }) ); } @@ -2040,7 +2105,14 @@ mod tests { assert!(!window.is_settled() && !window.contains(9) && window.resolved() == [None]); groups.held.insert(8, Held::Started(at(8000))); assert!(window.found(at(8500), Some(8), &groups)); - assert_eq!(window.resolved(), [Some(Resolved { start: 8, end: None })]); + assert_eq!( + window.resolved(), + [Some(Resolved { + start: 8, + end: None, + floored: true + })] + ); assert!(window.is_settled()); let window = Window::new(&[range(time(8500), None)], Some(9), &groups).unwrap(); @@ -2049,7 +2121,14 @@ mod tests { let mut window = Window::new(&[range(time(500), None)], Some(9), &groups).unwrap(); assert_eq!(window.lookups().collect::>(), [(at(500), 4)]); assert!(window.found(at(500), None, &groups)); - assert_eq!(window.resolved(), [Some(Resolved { start: 0, end: None })]); + assert_eq!( + window.resolved(), + [Some(Resolved { + start: 0, + end: None, + floored: false + })] + ); assert!(window.contains(4) && !window.missable(0).any(|run| *run.start() < 4)); } @@ -2063,7 +2142,14 @@ mod tests { assert_eq!(window.lookups().collect::>(), [(at(500), 4)]); assert!(window.found(at(500), None, &groups)); assert!(window.is_settled()); - assert_eq!(window.resolved(), [Some(Resolved { start: 0, end: Some(4) })]); + assert_eq!( + window.resolved(), + [Some(Resolved { + start: 0, + end: Some(4), + floored: false + })] + ); } /// Without a handler serving the history, the groups held decide alone. @@ -2075,8 +2161,16 @@ mod tests { assert_eq!( window.resolved(), [ - Some(Resolved { start: 4, end: None }), - Some(Resolved { start: 0, end: None }), + Some(Resolved { + start: 4, + end: None, + floored: true + }), + Some(Resolved { + start: 0, + end: None, + floored: false + }), ] ); } diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index b8eb9943004..c7689bf2035 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -22,7 +22,8 @@ use super::idle; use super::skipped::Skipped; use crate::ledger::{self, Ledger}; -use super::subscription::{Accounted, Containing, Held, Holdings, Layered, Resolved, Runs, Seen, Window, later}; +pub(crate) use super::subscription::Resolved; +use super::subscription::{Accounted, Containing, Held, Holdings, Layered, Runs, Seen, Window, later}; pub use super::subscription::{Anchor, Delay, Order, Position, Range, Subscription}; use rangemap::{RangeInclusiveSet, RangeMap, RangeSet}; @@ -8514,15 +8515,14 @@ impl Subscriber { /// live feed or a lookup. One empty as given, or that only resolves empty, ends at or /// below its start. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] - pub(crate) fn resolved(&mut self) -> Vec)>> { - let resolved = match &mut self.inner { + pub(crate) fn resolved(&mut self) -> Vec> { + match &mut self.inner { Inner::Plain(cursor) => { cursor.sync(); cursor.filter.window.resolved() } Inner::Resume(resume, _) => resume.resolved(), - }; - resolved.into_iter().map(|r| r.map(|r| (r.start, r.end))).collect() + } } /// The lowest group at or above `from` this subscriber's ranges and @@ -22143,7 +22143,11 @@ mod test { let mut subscriber = producer.subscribe(replay().with_ranges(ranges)); assert_eq!( subscriber.resolved_ranges(), - vec![Some(Resolved { start: 0, end: None })], + vec![Some(Resolved { + start: 0, + end: None, + floored: true + })], "group 1 may still start before the end" ); if dropped { @@ -22151,7 +22155,11 @@ mod test { assert_eq!(drain(&mut subscriber), vec![0]); assert_eq!( subscriber.resolved_ranges(), - vec![Some(Resolved { start: 0, end: Some(2) })], + vec![Some(Resolved { + start: 0, + end: Some(2), + floored: true + })], "the drop settles the end" ); continue; @@ -22191,7 +22199,11 @@ mod test { assert_eq!(reader.drain(), Vec::::new(), "ordered: {ordered}"); assert_eq!( reader.resolved(), - vec![Some(Resolved { start: 0, end: Some(1) })], + vec![Some(Resolved { + start: 0, + end: Some(1), + floored: true + })], "1 starting past the end settles it" ); } @@ -22241,7 +22253,11 @@ mod test { .unwrap(); assert_eq!( subscriber.resolved_ranges(), - vec![Some(Resolved { start: 0, end: Some(3) })] + vec![Some(Resolved { + start: 0, + end: Some(3), + floored: true + })] ); } From 10e2fe2bef184e6cd56ad7ecd396e6f77cfd5ba9 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 4 Nov 2026 14:33:20 -0800 Subject: [PATCH 28/30] fix(net): a fill retired before SUBSCRIBE_START never sets the live feed's start A fill's Range ID is kept until SUBSCRIBE_START arrives, so a fill an update retired before it is still told apart from the demand's ranges, and its start never lowers where the live feed is served from. Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/lite/subscriber.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 4b644c9b258..c813200e213 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -3673,6 +3673,16 @@ mod tests { assert_eq!(sub.fill_ids.iter().copied().collect::>(), [1, 2]); assert_eq!(sub.fill_range(1), Some(4..8)); assert_eq!(sub.fill_range(3), None, "the demand's new range is no fill"); + + // Before SUBSCRIBE_START, a fill an update retires keeps its Range ID, since the start + // still names it, and it must not count as the demand's. + sub.started = false; + sub.edit(&[moved, demand], 1).unwrap(); + assert_eq!(sub.fill_range(2), None, "retired"); + assert!(sub.fill_ids.contains(&2), "still a fill's"); + sub.started = true; + sub.edit(&[moved, demand], 1).unwrap(); + assert!(!sub.fill_ids.contains(&2), "forgotten once started"); } /// A lite-07 range an update adds owes nothing known until SUBSCRIBE_STARTED says where it @@ -8084,7 +8094,9 @@ struct SubStream { /// The demand the ranges ask for, `None` when they ask only for fills. demand: Option, /// The Range IDs of the fills' ranges; the rest are the demand's, which alone set where - /// the live feed is. Held by ID, since a fill may ask for exactly what a demand range does. + /// the live feed is. Held by ID, since a fill may ask for exactly what a demand range does, + /// and until SUBSCRIBE_START even once retired, since it answers every range the + /// SUBSCRIBE carried. fill_ids: std::collections::BTreeSet, /// The groups the ranges ask for fills. fill_groups: rangemap::RangeSet, @@ -8236,7 +8248,9 @@ impl SubStream { let changed = !self.live.iter().map(|(id, _)| id).eq(before); let live = &self.live; self.starts.retain(|id, _| live.get(*id).is_some()); - self.fill_ids.retain(|id| live.get(*id).is_some()); + if self.started { + self.fill_ids.retain(|id| live.get(*id).is_some()); + } self.fill_ids .extend(added.into_iter().filter(|(_, fill)| *fill).map(|(id, _)| id)); Ok((edit, changed)) @@ -10230,6 +10244,8 @@ impl ServeLoop { } self.range_fills.skip(groups, skipped); } + // A fill an update retired since says nothing of the live feed either. + None if active.fill_ids.contains(&id) => {} // Only the demand's ranges say where the live feed is served from. No range // starting anywhere leaves nothing owed. None => { @@ -10241,6 +10257,9 @@ impl ServeLoop { } } active.served = served; + // Only a live fill's Range ID matters from here. + let live = &active.live; + active.fill_ids.retain(|id| live.get(*id).is_some()); // One asking only for fills feeds nothing live. if active.demand.is_none() { active.started = true; From a4109aff333f00ddee25c36a63988dc989337187 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 4 Nov 2026 14:50:55 -0800 Subject: [PATCH 29/30] quest: name the JS port of lite-07 subscription fills in lite07-fetch Decided by the maintainer on moq-dev/moq#7872 (2026-11-04). Co-Authored-By: Claude Opus 5.5 --- quest/m1/subscribe-ranges/README.md | 2 +- quest/m1/subscribe-ranges/lite07-fetch.md | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/quest/m1/subscribe-ranges/README.md b/quest/m1/subscribe-ranges/README.md index 0477e680008..b287c92ceeb 100644 --- a/quest/m1/subscribe-ranges/README.md +++ b/quest/m1/subscribe-ranges/README.md @@ -99,7 +99,7 @@ every sequence delivered or dropped. ## Required - [JS head fills and time lookups](/quest/m1/subscribe-ranges/js-fills.md) - JS readers fill a cached group's missing head before handing it out, and look a time start up through `Fill.time` -- [Lite-07 FETCH is single-group only](/quest/m1/subscribe-ranges/lite07-fetch.md) - lite-07 FETCH reads one group or a slice of one, and SUBSCRIBE ranges of whole groups, in Rust, JS, and the draft +- [Lite-07 FETCH is single-group only](/quest/m1/subscribe-ranges/lite07-fetch.md) - lite-07 FETCH reads one group or a slice of one, and SUBSCRIBE ranges of whole groups, in Rust, JS, and the draft, with JS multi-group fills moving onto the subscription - [Delete the old start bookkeeping](/quest/m1/subscribe-ranges/start-bookkeeping.md) - what each lite cursor is owed and dropped lives in the model, and `TrackRun` only translates it per version ## Related diff --git a/quest/m1/subscribe-ranges/lite07-fetch.md b/quest/m1/subscribe-ranges/lite07-fetch.md index 152894d2153..75738354eb1 100644 --- a/quest/m1/subscribe-ranges/lite07-fetch.md +++ b/quest/m1/subscribe-ranges/lite07-fetch.md @@ -1,4 +1,4 @@ -# [M] Lite-07 FETCH is single-group only +# [L] Lite-07 FETCH is single-group only ## Goal @@ -32,6 +32,20 @@ on lite-07 (`track.Consumer.fetchGroup` in JS). group; a fill of more than one group goes through the subscription, as the Rust copy does since [moq-dev/moq#7872](https://github.com/moq-dev/moq/pull/7872). +- Decided 2026-11-04 (maintainer, on + [moq-dev/moq#7872](https://github.com/moq-dev/moq/pull/7872)): the JS port + of those subscription fills is this quest's, not #7872's. JS copies started + filling only in [moq-dev/moq#7845](https://github.com/moq-dev/moq/pull/7845), + after #7872 was scoped as the Rust copy's fill, and they FETCH each missing + group one at a time, which breaks no wire rule. Port all of #7872's Rust fill + rules to the JS subscriber (`Subscriber.#runFills`), with tests: + - Subscription ranges: a lite-07 miss of more than one whole group joins the + subscription's ranges beside the demand's, told apart by Range ID, or rides + a fills-only SUBSCRIBE when no demand is open. + - Recovery: a group the subscription drops as stale, or delivered before and + since evicted, is asked once on a SUBSCRIBE of its own with no max delay. + - Skips: a range start past what a fill asked refuses only the fills owing + those groups now, while that range is live, and records nothing. - `drafts/draft-lcurley-moq-lite.md` states the split in its Fetch and Subscribe sections. Run `just drafts check`. - Run `just test interop --all`. From 23a690e2bf11c5e56522a9d447fa9879ec1da450 Mon Sep 17 00:00:00 2001 From: quest fixer Date: Wed, 4 Nov 2026 16:04:40 -0800 Subject: [PATCH 30/30] fix(quest): remove completed lite-07 ranges prerequisite --- quest/m1/group-error-cache.md | 1 - 1 file changed, 1 deletion(-) diff --git a/quest/m1/group-error-cache.md b/quest/m1/group-error-cache.md index b51a73d9077..08b7284d10c 100644 --- a/quest/m1/group-error-cache.md +++ b/quest/m1/group-error-cache.md @@ -80,7 +80,6 @@ mirror, if a caller outside moq-net needs it; otherwise none. Wire: none. ## Required - [Drop set](/quest/m1/drop-set.md) - its per-cursor runs and their reasons are what this bounds; its PR, [moq-dev/moq#7670](https://github.com/moq-dev/moq/pull/7670), rewrites the same recording paths -- [Lite-07 ranges](/quest/m1/subscribe-ranges/lite.md) - its PR, [moq-dev/moq#7872](https://github.com/moq-dev/moq/pull/7872), makes the start skip and recovery non-permanent, the rule this generalizes ## Related