From 70efa20bd5d82522c7f9038b8293e053bdcf3251 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 7 Oct 2026 12:46:30 -0700 Subject: [PATCH 1/2] chore: claim quest/m1/ietf-cold-largest Co-authored-by: Grok 4.5 From 2bd3a967df6099c7affbd3b0aac84ea60c4f9e77 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 7 Oct 2026 13:11:31 -0700 Subject: [PATCH 2/2] fix(net): report a cold relay's upstream Largest Drafts 14 to 19 repeat the upstream SUBSCRIBE_OK Largest while a relay has no cached object, so a relative joining FETCH at offset 0 is the current group instead of INVALID_RANGE. Co-Authored-By: Grok 4.5 --- rs/moq-net/src/ietf/publisher.rs | 228 ++++++++++++++++++++++++------ rs/moq-net/src/ietf/subscriber.rs | 34 ++++- rs/moq-net/src/model/track.rs | 37 +++++ rs/moq-net/tests/cold_largest.rs | 97 +++++++++++++ 4 files changed, 349 insertions(+), 47 deletions(-) create mode 100644 rs/moq-net/tests/cold_largest.rs diff --git a/rs/moq-net/src/ietf/publisher.rs b/rs/moq-net/src/ietf/publisher.rs index 30a8faa5fef..4b20f20ff60 100644 --- a/rs/moq-net/src/ietf/publisher.rs +++ b/rs/moq-net/src/ietf/publisher.rs @@ -4889,6 +4889,76 @@ mod serve_tests { ); } + /// Nothing is cached, but the copy learned an upstream Largest. SUBSCRIBE_OK + /// repeats it, and a relative joining FETCH at offset 0 is the current group + /// from object 0, filled by the one-group upstream fetch. + #[moq_net_sim::test] + async fn a_cold_copy_reports_the_upstream_largest() { + const GROUP: u64 = 4; + let version = Version::Draft16; + let mut h = serve(version); + h.track + .set_upstream_largest(Some(track::Position { group: GROUP, frame: 2 })); + let dynamic = h.track.dynamic(); + let filling = moq_net_sim::spawn(async move { + let request = dynamic.requested_group().await.expect("upstream fill"); + assert_eq!(request.sequence(), GROUP); + assert_eq!(request.frame_start(), 0); + let mut group = request.accept(None).expect("fill"); + for payload in [b"o0".as_slice(), b"o1", b"o2"] { + group.write_frame(timestamp(), payload).unwrap(); + } + group.finish().unwrap(); + }); + + settle().await; + let stream = Stream::open(&mut h.session.clone(), version).await.unwrap(); + let mut serving = std::pin::pin!( + h.publisher + .clone() + .run_subscribe_stream(stream, subscribe(Filter::NextObject, None)) + ); + registered(&h, serving.as_mut()).await; + + let writes = h.log.writes.lock().unwrap().clone(); + let mut buf = writes.as_slice(); + assert_eq!( + crate::coding::decode_varint(&mut buf, version).unwrap(), + ietf::SubscribeOk::ID + ); + assert_eq!( + crate::coding::decode_buf(&mut buf, version, ietf::SubscribeOk::decode) + .unwrap() + .largest, + Some(Location { + group: GROUP, + object: 2 + }), + ); + + let mark = h.log.writes.lock().unwrap().len(); + let response = joining_fetch(&h, mark).await.expect("joining FETCH"); + let (ok, objects) = fetch_answer(bytes::Bytes::from(response), version); + assert_eq!( + ok.end_location, + Location { + group: GROUP, + object: 3 + }, + ); + assert_eq!( + objects, + vec![ + (GROUP, 0, "o0".to_string()), + (GROUP, 1, "o1".to_string()), + (GROUP, 2, "o2".to_string()), + ], + ); + assert!(h.log.resets().is_empty()); + filling.await.expect("fill task"); + assert!(futures::poll!(serving.as_mut()).is_pending()); + } + /// The filter's object bounds trim what `run_group` writes: the skipped head is not /// sent, the first written object's delta is its absolute id, and a capped tail stops /// early. Extensions are off so the wire is just deltas, sizes, and payloads. @@ -6681,9 +6751,10 @@ mod tests { struct LiveEdge { /// The newest group sequence, `None` before any group exists. latest: Option, - /// The precise Largest Object. `None` when the track is empty, or when the newest - /// group's frames cannot be read right now (none written yet), in which case nothing - /// is advertised and no fill is servable. + /// The precise Largest Object. `None` when the track is empty and no upstream + /// named one, or when the newest group's frames cannot be read right now (none + /// written yet) and the upstream Largest names a different group. Nothing is + /// advertised then, and no fill is servable. largest: Option, /// One past the Largest Object, which is where a Next Object subscription begins. /// When the edge is imprecise this falls back to the next group boundary: never below @@ -6693,51 +6764,78 @@ struct LiveEdge { /// Snapshot the live edge of a track. fn live_edge(track: &track::Consumer) -> LiveEdge { - let Some(latest) = track.latest() else { - return LiveEdge::default(); - }; - - match track.peek_latest() { - Some(group) if group.sequence == latest => { - let count = group.frame_count() as u64; - let largest = match count.checked_sub(1) { - Some(object) => Some(Location { group: latest, object }), - // A group with no frames yet has no objects, so the largest sits in an - // earlier group. Walk back through the cache to find it, or a peer that - // subscribes in the instant between a group's creation and its first - // frame is told the track is empty and gets no fill. - None => largest_before(track, latest), - }; - // One past the edge, even when the edge sits below the newest group: a group - // may keep writing after a newer one exists, and a floor above the true Next - // Object would strand those objects between the fill cap and the - // subscription. With no readable object anywhere, the newest group's start - // excludes nothing the cache can still name. - let next = match largest { - Some(largest) => Location { - group: largest.group, - object: largest.object.saturating_add(1), - }, - None => Location { - group: latest, - object: 0, - }, - }; - LiveEdge { - latest: Some(latest), - largest, - next: Some(next), + let edge = match track.latest() { + None => LiveEdge::default(), + Some(latest) => match track.peek_latest() { + Some(group) if group.sequence == latest => { + let count = group.frame_count() as u64; + let largest = match count.checked_sub(1) { + Some(object) => Some(Location { group: latest, object }), + // A group with no frames yet has no objects, so the largest sits in an + // earlier group. Walk back through the cache to find it, or a peer that + // subscribes in the instant between a group's creation and its first + // frame is told the track is empty and gets no fill. + None => largest_before(track, latest), + }; + // One past the edge, even when the edge sits below the newest group: a group + // may keep writing after a newer one exists, and a floor above the true Next + // Object would strand those objects between the fill cap and the + // subscription. With no readable object anywhere, the newest group's start + // excludes nothing the cache can still name. + let next = match largest { + Some(largest) => Location { + group: largest.group, + object: largest.object.saturating_add(1), + }, + None => Location { + group: latest, + object: 0, + }, + }; + LiveEdge { + latest: Some(latest), + largest, + next: Some(next), + } } - } - _ => LiveEdge { - latest: Some(latest), - largest: None, - next: Some(Location { - group: latest.saturating_add(1), - object: 0, - }), + _ => LiveEdge { + latest: Some(latest), + largest: None, + next: Some(Location { + group: latest.saturating_add(1), + object: 0, + }), + }, }, + }; + // A relay learns this from upstream before any object is cached. Reporting it + // is what lets a joining FETCH ask for the current group instead of being told + // the track is empty. A cached object is newer. So is an empty group past the + // one upstream named: that edge has moved, and the old object is not it. + apply_upstream_largest(track, edge) +} + +/// Use the upstream Largest when the cache cannot name an object. +fn apply_upstream_largest(track: &track::Consumer, mut edge: LiveEdge) -> LiveEdge { + if edge.largest.is_some() { + return edge; } + let Some(upstream) = track.upstream_largest() else { + return edge; + }; + if edge.latest.is_some_and(|latest| latest != upstream.group) { + return edge; + } + edge.latest = Some(upstream.group); + edge.largest = Some(Location { + group: upstream.group, + object: upstream.frame, + }); + edge.next = Some(Location { + group: upstream.group, + object: upstream.frame.saturating_add(1), + }); + edge } /// The last object below `sequence`: the nearest earlier cached group that has started a @@ -7135,6 +7233,46 @@ mod range_tests { assert_eq!(edge.next, Some(Location { group: 0, object: 1 })); } + /// No cached object: the edge is the Largest the upstream SUBSCRIBE_OK named. + #[test] + fn an_empty_track_reports_the_upstream_largest() { + let mut track = track::Producer::new(std::sync::Arc::new(crate::broadcast::Info::default()), "video", None); + track.set_upstream_largest(Some(track::Position { group: 4, frame: 2 })); + + let edge = live_edge(&track.consume()); + assert_eq!(edge.latest, Some(4)); + assert_eq!(edge.largest, Some(Location { group: 4, object: 2 })); + assert_eq!(edge.next, Some(Location { group: 4, object: 3 })); + } + + /// Once an object is cached it is the edge, not the older upstream answer. + #[test] + fn a_cached_object_outranks_the_upstream_largest() { + let mut track = track::Producer::new(std::sync::Arc::new(crate::broadcast::Info::default()), "video", None); + track.set_upstream_largest(Some(track::Position { group: 4, frame: 9 })); + let mut group = track.create_group(group::Info { sequence: 4 }).unwrap(); + group + .write_frame(crate::Timestamp::from_millis(0).unwrap(), b"only".as_slice()) + .unwrap(); + + let edge = live_edge(&track.consume()); + assert_eq!(edge.largest, Some(Location { group: 4, object: 0 })); + assert_eq!(edge.next, Some(Location { group: 4, object: 1 })); + } + + /// An empty group past the upstream Largest means the edge moved. The old + /// object is not advertised in its place. + #[test] + fn a_newer_empty_group_does_not_revive_an_older_upstream_largest() { + let mut track = track::Producer::new(std::sync::Arc::new(crate::broadcast::Info::default()), "video", None); + track.set_upstream_largest(Some(track::Position { group: 1, frame: 3 })); + let _open = track.create_group(group::Info { sequence: 5 }).unwrap(); + + let edge = live_edge(&track.consume()); + assert_eq!(edge.latest, Some(5)); + assert_eq!(edge.largest, None); + } + /// Both ends carry through, object bounds included, so the boundary groups can be /// trimmed rather than widened. #[test] diff --git a/rs/moq-net/src/ietf/subscriber.rs b/rs/moq-net/src/ietf/subscriber.rs index f144d58419d..e7e8a7f4908 100644 --- a/rs/moq-net/src/ietf/subscriber.rs +++ b/rs/moq-net/src/ietf/subscriber.rs @@ -1984,12 +1984,21 @@ where frame: largest.object, }) }); + // The group below is only the read floor. The object is what a downstream + // SUBSCRIBE_OK repeats while this copy has cached nothing, so its joining + // FETCH can still name the current group. It is stored before accept wakes + // a reader: that reader can snapshot the edge as soon as the track exists. + let upstream_largest = largest.map(|largest| track::Position { + group: largest.group, + frame: largest.object, + }); let (mut track, dynamic) = match resumed { - Some(idle) => { + Some(mut idle) => { if !self.state.lock().subscribes.contains_key(&request_id) { // Aborted with the session while the answer was in hand. return None; } + idle.track.set_upstream_largest(upstream_largest); (idle.track, idle.dynamic) } None => { @@ -2005,7 +2014,7 @@ where // Serves cache misses with a group FETCH. Registered before accepting, so a // miss queued meanwhile waits for it rather than failing for want of a handler. let dynamic = request.dynamic(); - (request.accept(info), dynamic) + (request.with_upstream_largest(upstream_largest).accept(info), dynamic) } }; // A live join starts at the publisher's edge; an absolute one where it asked. @@ -8297,6 +8306,10 @@ mod joining_fetch_tests { .expect("the subscription is registered"); track.fill.read().outstanding() } + + fn track(&self) -> &track::Consumer { + &self._hold.1 + } } impl Drop for JoinRun { @@ -8305,6 +8318,23 @@ mod joining_fetch_tests { } } + /// SUBSCRIBE_OK's Largest is kept on the copy before any object is cached. + #[moq_net_sim::test] + async fn a_cold_copy_records_the_subscribe_ok_largest() { + let largest = ietf::Location { group: 4, object: 1 }; + let version = Version::Draft16; + let run = JoinRun::start(version, None, subscribe_ok(version, Some(largest)), fetch_ok(version)).await; + + assert_eq!(run.track().latest(), None, "the copy has no cached group"); + assert_eq!( + run.track().upstream_largest(), + Some(track::Position { + group: largest.group, + frame: largest.object, + }), + ); + } + /// Every pre-draft-20 live join is Largest Object on the SUBSCRIBE and a relative /// joining FETCH at offset 0 that names the subscribe's request id. #[moq_net_sim::test] diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index a57a2bbe029..733b692cb03 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -267,6 +267,10 @@ pub(crate) struct TrackState { // The newest group cached when the track went idle: what the route's answer is // judged against, whatever lands before it. idle_newest: Option, + // Largest Location from the upstream SUBSCRIBE_OK. Reported downstream while + // no object is cached, so a joining FETCH can name the current group. A cached + // object supersedes it. + upstream_largest: Option, // Where production stopped, snapshotted when the open groups are released (an // abort, or the last producer dropping). Computed live from the cache otherwise; @@ -1702,6 +1706,15 @@ impl Producer { } } + /// Record the upstream SUBSCRIBE_OK Largest, or clear it when that answer named none. + /// + /// A downstream subscribe reports this while the copy has no cached object. + pub(crate) fn set_upstream_largest(&mut self, largest: Option) { + if let Ok(mut state) = self.modify() { + state.upstream_largest = largest; + } + } + /// Whether readers may take from the cache; see [`Self::set_idle`]. pub(crate) fn is_live(&self) -> bool { self.state.read().live @@ -2828,6 +2841,16 @@ impl Consumer { self.state.read().max_sequence } + /// The upstream Largest recorded by [`Producer::set_upstream_largest`]. + /// + /// A front answers from the route serving it. + pub(crate) fn upstream_largest(&self) -> Option { + if let Some(serving) = self.serving() { + return serving.upstream_largest(); + } + self.state.read().upstream_largest + } + /// The declared exclusive final sequence, or `None` while the track is open ended. pub(crate) fn final_sequence(&self) -> Option { if let Some(serving) = self.serving() { @@ -4196,6 +4219,10 @@ pub struct Request { // Served from a front's routes; see [`Self::routes`]. routes: Option, + + // Upstream SUBSCRIBE_OK Largest, written in the same update as [`Self::accept`] + // so a reader woken by that update already sees it. + upstream_largest: Option, } impl Request { @@ -4214,6 +4241,7 @@ impl Request { stats: stats::Scope::default(), resolving_start: false, routes: None, + upstream_largest: None, } } @@ -4234,6 +4262,14 @@ impl Request { self } + /// Record the upstream SUBSCRIBE_OK Largest, applied in the same write as + /// [`Self::accept`]. A downstream subscribe reports it while the copy has no + /// cached object. `None` leaves the field clear. + pub(crate) fn with_upstream_largest(mut self, largest: Option) -> Self { + self.upstream_largest = largest; + self + } + /// Attach an ingress stats scope, applied to the [`Producer`] on accept. Set by /// a tagged [`broadcast::Producer::reserve_track`]. pub(crate) fn with_stats(mut self, scope: stats::Scope) -> Self { @@ -4302,6 +4338,7 @@ impl Request { state.accept(info.clone()); state.start_pending = self.resolving_start; state.routes = self.routes; + state.upstream_largest = self.upstream_largest; } // Accepting the request creates the track producer: count it as one ingress // subscription (closed when the last handle drops). No-op when untagged. diff --git a/rs/moq-net/tests/cold_largest.rs b/rs/moq-net/tests/cold_largest.rs new file mode 100644 index 00000000000..74ee1af8b1f --- /dev/null +++ b/rs/moq-net/tests/cold_largest.rs @@ -0,0 +1,97 @@ +//! A relay with nothing cached still names its upstream's Largest, so a draft-16 +//! live join receives the current group from object 0. + +mod support; + +use std::time::Duration; + +use moq_net::{Hop, Timestamp, Version}; +use support::harness::{MockConnectOptions, connect_mock}; + +const TIMEOUT: Duration = Duration::from_secs(5); + +fn produce_origin(hop: u64) -> moq_net::origin::Producer { + let (producer, driver) = moq_net::origin::Producer::new(moq_net::origin::Config::new(Hop::new(hop).unwrap())); + support::harness::spawn(driver); + producer +} + +async fn read(group: &mut moq_net::group::Consumer) -> Vec { + moq_net_sim::timeout(TIMEOUT, group.read_frame()) + .await + .expect("no frame") + .expect("read_frame") + .expect("group ended") + .payload + .to_vec() +} + +/// Publisher, cold relay, draft-16 subscriber. The relay has no object cached when +/// the subscriber joins. The join is a relative FETCH at offset 0, and the group +/// arrives from its first object. +#[moq_net_sim::test] +async fn a_cold_relay_serves_the_current_group_from_object_zero() { + let version: Version = "moq-transport-16".parse().unwrap(); + let publisher = produce_origin(1); + let broadcast = publisher.create_broadcast("bcast").unwrap(); + let track = broadcast.create_track("video", None).unwrap(); + broadcast.announce(Default::default()).unwrap(); + + let relay = produce_origin(2); + let mut upstream = MockConnectOptions::new(version); + upstream.server_publish = Some(publisher.consume()); + upstream.client_subscribe = Some(relay.clone()); + let _upstream = connect_mock(upstream).await; + + let peer = produce_origin(3); + let mut downstream = MockConnectOptions::new(version); + downstream.server_publish = Some(relay.consume()); + downstream.client_subscribe = Some(peer.clone()); + let _downstream = connect_mock(downstream).await; + + let consumer = peer.consume(); + moq_net_sim::timeout(TIMEOUT, consumer.routed("bcast")) + .await + .expect("announce timeout") + .expect("routed"); + + // The objects exist only at the publisher. The relay subscribes when the peer + // does, so its cache is empty at that SUBSCRIBE_OK. + const GROUP: u64 = 7; + let mut group = track.create_group(moq_net::group::Info { sequence: GROUP }).unwrap(); + for payload in [b"o0".as_slice(), b"o1", b"o2"] { + group.write_frame(Timestamp::from_millis(0).unwrap(), payload).unwrap(); + } + group.finish().unwrap(); + + let remote = moq_net_sim::timeout(TIMEOUT, consumer.request_broadcast("bcast")) + .await + .expect("resolve timeout") + .expect("broadcast resolves"); + let mut sub = moq_net_sim::timeout(TIMEOUT, remote.track("video").unwrap().subscribe(None)) + .await + .expect("subscribe timeout") + .expect("subscribe"); + let mut got = moq_net_sim::timeout(TIMEOUT, sub.recv_group()) + .await + .expect("no group") + .expect("recv_group") + .expect("group"); + assert_eq!(got.sequence, GROUP); + assert_eq!(read(&mut got).await, b"o0"); + assert_eq!(read(&mut got).await, b"o1"); + assert_eq!(read(&mut got).await, b"o2"); + + drop(( + got, + sub, + group, + track, + broadcast, + _downstream, + _upstream, + publisher, + relay, + peer, + )); +}