From 3c6603540ecb74735d3f262a8f2337deab95e057 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 18 Oct 2026 22:12:09 -0700 Subject: [PATCH 01/13] quest(m1): claim archive/subscribe-all Co-Authored-By: Claude Opus 5.5 From ba2db364ca353ecd7305d40c9608dc592baf74a9 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 18 Oct 2026 22:30:32 -0700 Subject: [PATCH 02/13] wip(moq-net): list a broadcast's tracks and an origin's broadcasts Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/model/broadcast.rs | 106 ++++++++- rs/moq-net/src/model/listing.rs | 366 ++++++++++++++++++++++++++++++ rs/moq-net/src/model/mod.rs | 4 +- rs/moq-net/src/model/origin.rs | 11 + rs/moq-net/src/model/track.rs | 12 + 5 files changed, 496 insertions(+), 3 deletions(-) create mode 100644 rs/moq-net/src/model/listing.rs diff --git a/rs/moq-net/src/model/broadcast.rs b/rs/moq-net/src/model/broadcast.rs index 14e0ba9aa8e..2aa620a12f0 100644 --- a/rs/moq-net/src/model/broadcast.rs +++ b/rs/moq-net/src/model/broadcast.rs @@ -10,7 +10,7 @@ //! [Info] is the broadcast's static metadata, fixed for its lifetime. use crate::{cache, stats, track}; use std::{ - collections::HashMap, + collections::{BTreeMap, HashMap}, sync::Arc, task::{Poll, ready}, time::Duration, @@ -107,6 +107,12 @@ struct BroadcastState { // swept as the map grows, so requests for names never served stay bounded. sequences: HashMap, track::Sequence>, + // The tracks the producer created, as opposed to served on request, keyed by the order + // they were listed so a [`Tracks`] cursor resumes after the last one it saw. Closed + // entries are swept as the map grows. + listed: BTreeMap, + next_listed: u64, + // Set once the broadcast ends: `Producer::close()` or the last producer-side // handle dropping. Every lookup after it answers `Unroutable`. closing: bool, @@ -122,6 +128,18 @@ impl BroadcastState { } } + /// Add a track the producer created to what [`Tracks`] cursors yield. + fn list(&mut self, weak: track::TrackWeak) { + use super::WeakEntry; + // Swept once per doubling, so a broadcast churning track names stays bounded by its + // live tracks at constant amortized cost. + if self.listed.len() >= 16 && self.listed.len().is_power_of_two() { + self.listed.retain(|_, track| !track.is_closed()); + } + self.listed.insert(self.next_listed, weak); + self.next_listed += 1; + } + /// The sequence namespace for `name`, shared with every earlier track of the name. fn sequence(&mut self, name: &Arc) -> track::Sequence { if self.sequences.len() == self.sequences.capacity() { @@ -319,6 +337,7 @@ impl Producer { // live same-name entry cannot exist (its presence would have kept // the request from queuing). let _ = state.tracks.insert(name, track.weak()); + state.list(track.weak()); return Ok(track); } @@ -327,6 +346,7 @@ impl Producer { .with_stats(self.stats.clone()) .with_sequence(sequence); state.insert_track(track.weak())?; + state.list(track.weak()); Ok(track) } @@ -348,9 +368,28 @@ impl Producer { .with_stats(self.stats.clone()) .with_sequence(state.sequence(&name)); state.insert_track(request.weak())?; + state.list(request.weak()); Ok(request) } + /// List the live track `name` as if this producer had created it, so [`Tracks`] cursors + /// yield it. A relay calls it when a peer announces a track this broadcast already + /// serves on request, joining that copy instead of creating a second one. Returns + /// whether such a track exists. + #[cfg_attr(not(feature = "async"), allow(dead_code))] + // pending: /quest/m1/public-model/answer.md + pub(crate) fn list_track(&self, name: &str) -> bool { + let mut state = self.state.lock(); + let Some(weak) = state.tracks.get(name) else { + return false; + }; + use super::WeakEntry; + if !state.listed.values().any(|listed| listed.same_channel(&weak)) { + state.list(weak); + } + true + } + /// Create a track with a unique name using the given suffix. /// /// Uses [`Self::unique_name`]; minted names are never reused, even after closure. @@ -998,6 +1037,19 @@ impl Consumer { ) } + /// Every track the producer creates: the ones open now, in the order they were + /// created, then each new one as it appears, until the broadcast ends. + /// + /// A track a [`Dynamic`] serves only because someone asked for it is never yielded, + /// and the cursor never asks for one. Each track it does yield is that one instance, + /// so a later track of the same name is yielded again rather than replacing it. + pub fn tracks(&self) -> Tracks { + Tracks { + broadcast: self.clone(), + next: 0, + } + } + /// Join the demand of the track serving `name`, requesting it from a [`Dynamic`] /// when nothing serves it yet. Errors as [`Self::track`] does. pub(super) fn resolve_track(&self, name: &str) -> Result, Error> { @@ -1148,6 +1200,58 @@ impl super::WeakEntry for WeakConsumer { } } +/// The tracks a broadcast's producer creates, from [`Consumer::tracks`]. +pub struct Tracks { + broadcast: Consumer, + // The listing order of the next track to yield. + next: u64, +} + +impl Tracks { + /// The broadcast these tracks belong to. + pub fn broadcast(&self) -> &Consumer { + &self.broadcast + } + + /// Poll for the next track the producer created, or `None` once the broadcast ended. + pub fn poll_next(&mut self, waiter: &kio::Waiter) -> Poll> { + use super::WeakEntry; + let next = self.next; + let state = ready!(self.broadcast.state.poll(waiter, |state| { + let open = state.listed.range(next..).any(|(_, track)| !track.is_closed()); + match open || state.closing { + true => Poll::Ready(()), + false => Poll::Pending, + } + })); + let found = state + .listed + .range(next..) + .find(|(_, track)| !track.is_closed()) + .map(|(order, track)| (*order, track.clone())); + drop(state); + let Some((order, track)) = found else { + // Only reached once the broadcast is closing with nothing left to yield. + return Poll::Ready(None); + }; + self.next = order + 1; + Poll::Ready(Some( + track::Consumer::listed( + track.name().clone(), + self.broadcast.info.clone(), + track.peek(), + ) + .with_stats(self.broadcast.stats.clone()), + )) + } + + /// Wait for the next track the producer created, or `None` once the broadcast ended. + #[cfg(feature = "async")] + pub async fn next(&mut self) -> Option { + kio::wait(|waiter| self.poll_next(waiter)).await + } +} + /// A cloneable, watch-only handle to a broadcast's subscriber demand. /// /// Obtained from [`Producer::demand`] or [`Consumer::demand`]; the broadcast-level sibling of diff --git a/rs/moq-net/src/model/listing.rs b/rs/moq-net/src/model/listing.rs new file mode 100644 index 00000000000..b335e358753 --- /dev/null +++ b/rs/moq-net/src/model/listing.rs @@ -0,0 +1,366 @@ +//! Every broadcast published under a prefix, found through the routes covering it. +//! +//! A route claims capability, not inventory, so the route table alone cannot say which +//! broadcasts exist under a prefix. A broadcast published on this origin is its own +//! answer. A route a handler serves ([`Dynamic`]) is asked for a [`Listing`] of the +//! prefix, which the handler fills as it learns of broadcasts: a session forwards the +//! request to its peer. A handler that never answers lists nothing, which is how a +//! service serving any path on request stays out of the listing. + +use std::{ + collections::{BTreeMap, HashMap, VecDeque}, + task::{Poll, ready}, +}; + +use super::{AnnounceConsumer, AnnounceEvent, Consumer, Dynamic, ServeState}; +use crate::{AsPath, Error, PathOwned, broadcast}; + +/// The broadcasts a handler lists under one prefix, in the order it learned them. +#[derive(Default)] +pub(crate) struct ListingState { + // Keyed by insertion order, so a reader resumes after the last one it saw. + broadcasts: BTreeMap)>, + next: u64, +} + +/// The handler's side of a listing: the broadcasts it holds under the requested prefix. +/// +/// Holding a broadcast here holds it for the readers to come. Readers going away is +/// [`Self::poll_unused`]; dropping the listing ends it for them. +pub(crate) struct Listing { + prefix: PathOwned, + state: kio::Producer, +} + +impl Listing { + /// The absolute prefix whose broadcasts are listed. + pub fn prefix(&self) -> &crate::Path<'_> { + &self.prefix + } + + /// List a broadcast, published by the instance `epoch` names, returning the key that + /// [`Self::remove`] takes. + pub fn insert(&self, broadcast: broadcast::Consumer, epoch: Option) -> u64 { + let Ok(mut state) = self.state.write() else { + return 0; + }; + let key = state.next; + state.next += 1; + state.broadcasts.insert(key, (broadcast, epoch)); + key + } + + /// Stop listing a broadcast: it ended, and nobody should find it here. + pub fn remove(&self, key: u64) { + if let Ok(mut state) = self.state.write() { + state.broadcasts.remove(&key); + } + } + + /// Ready once nobody reads the listing. + pub fn poll_unused(&self, waiter: &kio::Waiter) -> Poll<()> { + self.state.poll_unused(waiter).map(|_| ()) + } + + /// Whether this lists `prefix` into the same readers as `other`. + pub fn same_channel(&self, other: &Self) -> bool { + self.state.same_channel(&other.state) + } +} + +/// What a [`Broadcasts`] cursor reads from one listing. +struct ListingReader { + state: kio::Consumer, + next: u64, +} + +impl ListingReader { + /// The next listed broadcast, `Ready(None)` once the handler dropped the listing. + fn poll_next(&mut self, waiter: &kio::Waiter) -> Poll)>> { + let next = self.next; + let found = self.state.poll(waiter, |state| match state.broadcasts.range(next..).next() { + Some((key, (broadcast, epoch))) => Poll::Ready((*key, broadcast.clone(), epoch.clone())), + None => Poll::Pending, + }); + match ready!(found) { + Ok((key, broadcast, epoch)) => { + self.next = key + 1; + Poll::Ready(Some((broadcast, epoch))) + } + Err(_) => Poll::Ready(None), + } + } +} + +/// The listings a route's handler has yet to take, and the ones its readers share. +#[derive(Default)] +pub(super) struct Listings { + queued: VecDeque, + // The readers' side of each prefix's listing, so a second reader joins the first. + live: HashMap>, +} + +impl Listings { + /// Read the listing of `prefix`, asking the handler for one unless a reader already did. + fn read(&mut self, prefix: &PathOwned) -> ListingReader { + if let Some(weak) = self.live.get(prefix).filter(|weak| !weak.is_closed()) { + let state = weak.consume(); + return ListingReader { state, next: 0 }; + } + // Unread listings the handler never took are dead weight; drop them as we go. + self.queued.retain(|listing| listing.state.is_used()); + let state = kio::Producer::::default(); + let reader = state.consume(); + self.live.retain(|_, weak| !weak.is_closed()); + self.live.insert(prefix.clone(), reader.weak()); + self.queued.push_back(Listing { + prefix: prefix.clone(), + state, + }); + ListingReader { state: reader, next: 0 } + } + + /// Drop every listing the handler has yet to take: the route is gone. + pub(super) fn close(&mut self) { + self.queued.clear(); + self.live.clear(); + } +} + +impl Dynamic { + /// Poll for the next prefix a reader wants the broadcasts of, under this route. + /// + /// Fill the [`Listing`] with every broadcast the publisher behind the route holds + /// under its prefix, and keep it current. A handler that serves paths only on request + /// never polls this, so nothing it would serve that way is ever listed. + #[cfg_attr(not(feature = "async"), allow(dead_code))] + // pending: /quest/m1/public-model/answer.md + pub(crate) fn poll_requested_listing(&self, waiter: &kio::Waiter) -> Poll> { + let mut state = ready!(self.state.poll(waiter, |state| { + match state.closed || !state.listings.queued.is_empty() { + true => Poll::Ready(()), + false => Poll::Pending, + } + })); + if state.closed { + return Poll::Ready(Err(Error::Closed)); + } + let listing = state.listings.queued.pop_front().expect("predicate guaranteed a listing"); + Poll::Ready(Ok(listing)) + } +} + +/// What serves an announced prefix, as far as listing goes. +enum Behind { + /// A broadcast published on this origin at exactly the prefix. + Local, + /// A handler, asked for a listing. + Server(kio::Shared), + /// Nothing that lists: an advertisement without a server. + Nothing, +} + +/// Every broadcast published under a prefix, from [`Consumer::broadcasts`]. +/// +/// Each one is yielded once, as it appears: a broadcast published on this origin +/// when it announces, and one behind a peer's route once the peer lists it. A new +/// publisher instance at the same path is another broadcast, yielded again. A +/// broadcast's [`tracks`](broadcast::Consumer::tracks) are every track its producer +/// creates. +pub struct Broadcasts { + consumer: Consumer, + announced: AnnounceConsumer, + // What serves each announced prefix (absolute), to drop its listing once none does. + routes: HashMap>>, + // One reader per handler, shared by every route that handler serves. + listings: Vec<(kio::Shared, ListingReader)>, + // Ready to yield, and what was yielded, to skip a broadcast reached twice. + ready: VecDeque<(broadcast::Consumer, Option)>, + yielded: Vec, + // The prefix as the origin names it, for listing requests, and as the consumer does. + prefix: PathOwned, + relative: PathOwned, +} + +impl Consumer { + /// Every broadcast published under `prefix`, as each appears. + /// + /// Together with [`broadcast::Consumer::tracks`], this is every track of every + /// broadcast under the prefix. A broadcast published on this origin is yielded once it + /// announces; one behind a peer's route once the peer lists it (moq-lite's + /// SUBSCRIBE_ALL). Nothing is asked of a handler that serves paths only on request, so + /// its broadcasts and tracks are never created for this. + /// + /// Fails with [`Error::Unauthorized`] when this consumer's scope cannot reach the + /// prefix. + pub fn broadcasts(&self, prefix: impl AsPath) -> Result { + let prefix = prefix.as_path(); + let scoped = self.scope(&prefix, &crate::Patterns::from(crate::Pattern::subtree("")?))?; + let named = self.root.join(&prefix).to_owned(); + let absolute = self.scope.resolve(&named).ok_or(Error::Unauthorized)?.to_owned(); + // A lookup, not announce forwarding: it must not drive the egress announce guards. + let announced = scoped.untagged().announced(); + Ok(Broadcasts { + consumer: self.clone(), + announced, + routes: HashMap::new(), + listings: Vec::new(), + ready: VecDeque::new(), + yielded: Vec::new(), + prefix: absolute, + relative: prefix.to_owned(), + }) + } +} + +impl Broadcasts { + /// Poll for the next broadcast under the prefix, or `None` once the origin closes. + pub fn poll_next(&mut self, waiter: &kio::Waiter) -> Poll> { + loop { + if let Some((broadcast, epoch)) = self.ready.pop_front() { + if let Some(broadcast) = self.hand_out(broadcast, epoch) { + return Poll::Ready(Some(broadcast)); + } + continue; + } + + let mut progress = false; + let mut ended = false; + match self.announced.poll_next(waiter) { + Poll::Ready(Some(event)) => { + self.route(event); + progress = true; + } + Poll::Ready(None) => ended = true, + Poll::Pending => {} + } + + let mut index = 0; + while index < self.listings.len() { + match self.listings[index].1.poll_next(waiter) { + Poll::Ready(Some(listed)) => { + self.ready.push_back(listed); + progress = true; + index += 1; + } + // The handler dropped the listing: its broadcasts carry on, but it lists + // no new ones. + Poll::Ready(None) => { + self.listings.swap_remove(index); + } + Poll::Pending => index += 1, + } + } + + if progress { + continue; + } + if ended { + return Poll::Ready(None); + } + return Poll::Pending; + } + } + + /// Wait for the next broadcast under the prefix, or `None` once the origin closes. + #[cfg(feature = "async")] + pub async fn next(&mut self) -> Option { + kio::wait(|waiter| self.poll_next(waiter)).await + } + + /// Apply an announcement: list what serves the prefix, or forget what no longer does. + fn route(&mut self, event: AnnounceEvent) { + let (AnnounceEvent::Start(announce) | AnnounceEvent::Update(announce) | AnnounceEvent::Restart(announce)) = + event + else { + let AnnounceEvent::End(announce) = event else { + unreachable!() + }; + let absolute = self.announced.absolute(&announce.prefix).to_owned(); + self.routes.remove(&absolute); + self.release(); + return; + }; + let absolute = self.announced.absolute(&announce.prefix).to_owned(); + match self.behind(&absolute) { + Behind::Local => { + self.routes.insert(absolute.clone(), None); + if let Some(source) = self.local(&absolute) { + self.ready.push_back((source, announce.route.epoch.clone())); + } + } + Behind::Server(server) => { + self.routes.insert(absolute, Some(server.clone())); + if !self.listings.iter().any(|(listed, _)| listed.same_channel(&server)) { + // A route above the prefix lists the prefix; one beneath it lists the + // whole prefix too, so a handler serving several routes beneath it + // answers one request. + let reader = server.lock().listings.read(&self.prefix); + self.listings.push((server, reader)); + } + } + Behind::Nothing => { + self.routes.remove(&absolute); + } + } + self.release(); + } + + /// Stop reading the listing of every handler no announced route is served by. + fn release(&mut self) { + let routes = &self.routes; + self.listings.retain(|(server, _)| { + routes + .values() + .any(|serving| serving.as_ref().is_some_and(|serving| serving.same_channel(server))) + }); + } + + /// What wins `path` for this cursor's reader. + fn behind(&self, path: &PathOwned) -> Behind { + let table = self.consumer.shared.lock(); + let horizon = self.consumer.horizon.effective(&table.routes, &path.as_path()); + let Some(best) = table.best_route(&path.as_path(), horizon, &Default::default(), |_| true) else { + return Behind::Nothing; + }; + if best.source.is_some() && best.prefix == *path { + return Behind::Local; + } + match &best.server { + Some(server) => Behind::Server(server.clone()), + None => Behind::Nothing, + } + } + + /// The broadcast published on this origin at `path`, if it still wins it. + fn local(&self, path: &PathOwned) -> Option { + let table = self.consumer.shared.lock(); + let horizon = self.consumer.horizon.effective(&table.routes, &path.as_path()); + let best = table.best_route(&path.as_path(), horizon, &Default::default(), |_| true)?; + best.source.clone().filter(|_| best.prefix == *path) + } + + /// Stamp a broadcast as this cursor names it, unless it was already yielded or ended. + fn hand_out(&mut self, broadcast: broadcast::Consumer, epoch: Option) -> Option { + self.yielded.retain(|yielded| !yielded.is_closed()); + if broadcast.is_closed() || self.yielded.iter().any(|yielded| yielded.is_clone(&broadcast)) { + return None; + } + // Named as the consumer names it: the requested prefix, then the path beneath it. + let suffix = broadcast.info().path.strip_prefix(&self.prefix)?.to_owned(); + let path = self.relative.join(&suffix).to_owned(); + // A replica of an instance already yielded, reached through another handler. + if epoch.is_some() + && self + .yielded + .iter() + .any(|yielded| yielded.info().epoch == epoch && yielded.info().path == path) + { + return None; + } + let scope = self.consumer.stats.egress(&self.consumer.root.join(&path).to_owned()); + let broadcast = broadcast.with_path(path).with_epoch(epoch).with_stats(scope); + self.yielded.push(broadcast.clone()); + Some(broadcast) + } +} diff --git a/rs/moq-net/src/model/mod.rs b/rs/moq-net/src/model/mod.rs index 04e7d797958..2065b5abce6 100644 --- a/rs/moq-net/src/model/mod.rs +++ b/rs/moq-net/src/model/mod.rs @@ -55,8 +55,8 @@ pub use timed::Timed; /// Publishing broadcasts, announcing routes, and consuming both through an origin. pub mod origin { pub use super::origin_impl::{ - Config, Consumer, Cost, DEFAULT_UPDATE_HOLD, Driver, Dynamic, Preference, Producer, Request, Requesting, Route, - Routing, Source, + Broadcasts, Config, Consumer, Cost, DEFAULT_UPDATE_HOLD, Driver, Dynamic, Preference, Producer, Request, + Requesting, Route, Routing, Source, }; } diff --git a/rs/moq-net/src/model/origin.rs b/rs/moq-net/src/model/origin.rs index 4947b99e5f8..d42ed1b60c5 100644 --- a/rs/moq-net/src/model/origin.rs +++ b/rs/moq-net/src/model/origin.rs @@ -12,6 +12,12 @@ use std::{ use rand::RngExt; +#[path = "listing.rs"] +mod listing; +pub use listing::Broadcasts; +#[cfg_attr(not(feature = "async"), allow(unused_imports))] +pub(crate) use listing::Listing; + use super::{ Requests, WeakCache, WeakEntry, front::{Action, Candidate, Event, Front, Refusal}, @@ -1195,6 +1201,9 @@ struct ServeState { // since the release wakes the front while the updater still holds it. renewals: Arc, + // The prefixes readers want the broadcasts of; see [`Broadcasts`]. + listings: listing::Listings, + // Set when the announcement is retracted or the origin tears down: new requests // fail immediately and the handler observes the end instead of parking forever. closed: bool, @@ -2474,6 +2483,7 @@ impl AnnounceProducer { if let Some(server) = &entry.server { let mut server = server.lock(); server.closed = true; + server.listings.close(); for producer in server.requests.drain_all() { if let Ok(mut request) = producer.write() { request.resolved.get_or_insert(Err(Error::Unroutable)); @@ -2621,6 +2631,7 @@ impl DriverState { for server in servers { let mut server = server.lock(); server.closed = true; + server.listings.close(); for producer in server.requests.drain_all() { if let Ok(mut request) = producer.write() { request.resolved.get_or_insert(Err(Error::Closed)); diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 75dfa3981ed..f56fab00836 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -3496,6 +3496,18 @@ impl Consumer { } } + /// A handle bound to one track instance, as [`broadcast::Tracks`] yields it: requests + /// read that instance even once its broadcast serves another under the name. + pub(super) fn listed(name: Arc, info: Arc, state: kio::ConsumerWeak) -> Self { + Self { + name, + broadcast: info, + source: Source::Track(state), + stats: stats::Scope::default(), + largest: None, + } + } + /// The track a request made now reads, joined to its consumer count. A broadcast /// that cannot serve the name answers with a track closed with its error. fn resolve(&self) -> kio::Consumer { From 6c1d5225c41565ad790eff225d8694ea2cbd1ae9 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 18 Oct 2026 22:46:00 -0700 Subject: [PATCH 03/13] wip(moq-net): SUBSCRIBE_ALL codec and publisher Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/lite/all_serve.rs | 592 +++++++++++++++++++++++++++ rs/moq-net/src/lite/mod.rs | 2 + rs/moq-net/src/lite/publisher.rs | 113 +++-- rs/moq-net/src/lite/stream.rs | 4 + rs/moq-net/src/lite/subscribe.rs | 4 +- rs/moq-net/src/lite/subscribe_all.rs | 436 ++++++++++++++++++++ rs/moq-net/src/lite/subscriber.rs | 91 +++- rs/moq-net/src/lite/version.rs | 18 +- rs/moq-net/src/priority.rs | 13 +- 9 files changed, 1219 insertions(+), 54 deletions(-) create mode 100644 rs/moq-net/src/lite/all_serve.rs create mode 100644 rs/moq-net/src/lite/subscribe_all.rs diff --git a/rs/moq-net/src/lite/all_serve.rs b/rs/moq-net/src/lite/all_serve.rs new file mode 100644 index 00000000000..10c6c4e7ecb --- /dev/null +++ b/rs/moq-net/src/lite/all_serve.rs @@ -0,0 +1,592 @@ +//! Serves a SUBSCRIBE_ALL: every track of every broadcast under a prefix, on one stream. +//! +//! Each broadcast and each track is its own task, so a wake costs the track that woke +//! rather than every one the request carries. They write their responses to a queue the +//! stream drains in order, which is what keeps the implicit Broadcast and Track IDs in +//! step with the wire. + +use super::*; + +use crate::coding::StreamCodes; + +/// The request-wide delivery preferences a SUBSCRIBE_ALL_UPDATE moves. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +struct AllSteer { + priority: u8, + order: Order, + max_delay: Duration, +} + +/// What every broadcast and track of one request shares. +struct AllCtx { + shared: Arc>, + /// The SUBSCRIBE_ALL's id, named by every group stream it opens. + id: u64, + /// Responses in wire order, drained onto the stream. + out: kio::Queue>, + /// Assigned as each TRACK_START is queued, which is the order the peer counts them. + next_track: AtomicU64, + steer: kio::Consumer, +} + +impl AllCtx { + fn send(&self, msg: lite::SubscribeAllResponse<'static>) { + // Closed only once the request is over, when nobody reads it. + let _ = self.out.try_push(msg); + } +} + +/// Answers a Subscribe All Stream until the requester closes it. +pub(super) struct AllServe { + shared: Arc>, + // Taken to abort. + stream: Option>, + state: AllState, + // Counted in `Shared::owed` until the stream ends, so a draining close waits for it. + owing: bool, + // One place under the session's subscription cap, whatever the request carries. + _slot: Option, + gate: Option, + // Log context, filled in after the decode. + prefix: crate::PathOwned, + // The requester's update failed to decode: the peer's fault, unlike a failed reply. + malformed: bool, +} + +enum AllState { + Decode, + /// Waiting on the split-horizon origin, which waits on the peer's SETUP. + Resolve { + msg: lite::SubscribeAll<'static>, + }, + Run(Box>), + /// FIN and wait for the acknowledgement. + Finish { + finished: bool, + }, +} + +impl AllServe { + pub(super) fn new(shared: Arc>, stream: Stream) -> Self { + shared.owed.fetch_add(1, Ordering::Relaxed); + Self { + shared, + stream: Some(stream), + state: AllState::Decode, + owing: true, + _slot: None, + gate: None, + prefix: Default::default(), + malformed: false, + } + } + + pub(super) fn poll(&mut self, waiter: &kio::Waiter) -> Poll> { + match ready!(self.poll_serve(waiter)) { + Ok(()) => { + tracing::info!(prefix = %self.prefix, "subscribe all complete"); + Poll::Ready(Ok(())) + } + // The peer's own malformed bytes are a control violation. + Err(err) if matches!(self.state, AllState::Decode) || self.malformed => Poll::Ready(Err(err)), + Err(err) => { + match &err { + err if is_cancel(err) => tracing::info!(prefix = %self.prefix, "subscribe all cancelled"), + err => tracing::warn!(prefix = %self.prefix, %err, "subscribe all error"), + } + self.stream.take().expect("stream present").writer.abort(&err); + Poll::Ready(Ok(())) + } + } + } + + fn poll_serve(&mut self, waiter: &kio::Waiter) -> Poll> { + if let Some(gate) = &mut self.gate + && gate.poll_denied(waiter).is_ready() + { + return Poll::Ready(Err(Error::Unauthorized)); + } + let mut cx = waiter.context(); + loop { + if matches!(self.state, AllState::Resolve { .. } | AllState::Run(_)) + && let Poll::Ready(res) = self.poll_requester(&mut cx) + { + // A FIN cancels the request: everything it carries ends with it. + res?; + self.state = AllState::Finish { finished: false }; + } + + let stream = self.stream.as_mut().expect("stream present"); + match &mut self.state { + AllState::Decode => { + let msg = ready!(stream.reader.poll_decode::(&mut cx))?; + self.prefix = self.shared.origin.absolute(&msg.prefix).to_owned(); + match self.shared.interests.slots.acquire() { + Ok(slot) => self._slot = Some(slot), + // A peer past its limits loses the session, not just this request. + Err(err) => { + self.shared.session.clone().close( + crate::lite::error::to_session_code(&crate::SessionError::from(&err)), + "too many subscriptions", + ); + return Poll::Ready(Err(err)); + } + } + tracing::info!(prefix = %self.prefix, request = ?msg, "subscribe all started"); + let mut gate = self.shared.gate(&msg.prefix); + let denied = gate.poll_denied(waiter).is_ready(); + self.gate = Some(gate); + self.state = AllState::Resolve { msg }; + if denied { + return Poll::Ready(Err(Error::Unauthorized)); + } + } + AllState::Resolve { msg } => { + let origin = ready!(self.shared.poll_serving_origin(waiter)); + let broadcasts = origin.broadcasts(&msg.prefix)?; + let steer = AllSteer { + priority: msg.priority, + order: msg.order, + max_delay: serving_max_delay(self.shared.version, msg.max_delay), + }; + let run = AllRun::new(self.shared.clone(), msg, broadcasts, steer); + self.state = AllState::Run(Box::new(run)); + } + AllState::Run(run) => { + ready!(run.poll(&mut stream.writer, waiter))?; + self.state = AllState::Finish { finished: false }; + } + AllState::Finish { finished } => { + if !*finished { + ready!(stream.writer.poll_flush(&mut cx))?; + stream.writer.finish()?; + *finished = true; + } + let res = ready!(stream.writer.poll_close(&mut cx)); + self.settle(); + return Poll::Ready(res); + } + } + } + } + + /// Read what the requester sends: ready once it closes its send direction, `Ok` for a + /// FIN. Each update reaches every track. + fn poll_requester(&mut self, cx: &mut Context<'_>) -> Poll> { + let stream = self.stream.as_mut().expect("stream present"); + loop { + let update = match ready!(stream.reader.poll_decode_maybe::(cx)) { + Ok(Some(update)) => update, + Ok(None) => return Poll::Ready(Ok(())), + Err(err) => { + self.malformed = matches!(err, Error::Decode(_)); + return Poll::Ready(Err(err)); + } + }; + match &mut self.state { + AllState::Run(run) => run.update(update, self.shared.version), + // Not started yet: the request's own preferences become the update's. + AllState::Resolve { msg } => { + msg.priority = update.priority; + msg.order = update.order; + msg.max_delay = update.max_delay; + } + AllState::Decode | AllState::Finish { .. } => {} + } + } + } + + /// The request no longer owes the peer anything a draining close should wait for. + fn settle(&mut self) { + if std::mem::take(&mut self.owing) { + self.shared.owed.fetch_sub(1, Ordering::Relaxed); + } + } +} + +impl Drop for AllServe { + fn drop(&mut self) { + self.settle(); + } +} + +/// The running request: lists each broadcast as it appears and drains the responses. +struct AllRun { + ctx: Arc>, + prefix: crate::PathOwned, + broadcasts: origin::Broadcasts, + // The origin closed: no broadcast will appear. + listed_all: bool, + steer: kio::Producer, + next_broadcast: u64, + children: kio::Tasks>, + // A request that always has another broadcast ready still lets the session's other tasks run. + budget: kio::coop::Budget, +} + +impl AllRun { + fn new( + shared: Arc>, + msg: &lite::SubscribeAll<'static>, + broadcasts: origin::Broadcasts, + steer: AllSteer, + ) -> Self { + let steer = kio::Producer::new(steer); + Self { + ctx: Arc::new(AllCtx { + shared, + id: msg.id, + out: kio::Queue::new(), + next_track: AtomicU64::new(0), + steer: steer.consume(), + }), + prefix: msg.prefix.to_owned(), + broadcasts, + listed_all: false, + steer, + next_broadcast: 0, + children: kio::Tasks::new(), + budget: kio::coop::Budget::new(32), + } + } + + fn update(&mut self, update: lite::SubscribeAllUpdate, version: Version) { + if let Ok(mut steer) = self.steer.write() { + *steer = AllSteer { + priority: update.priority, + order: update.order, + max_delay: serving_max_delay(version, update.max_delay), + }; + } + } + + /// Serve until the origin closes and every track it listed has ended. + fn poll(&mut self, writer: &mut Writer, waiter: &kio::Waiter) -> Poll> { + let mut cx = waiter.context(); + loop { + ready!(self.budget.poll_yield(waiter)); + ready!(writer.poll_flush(&mut cx))?; + + let mut wrote = false; + while let Ok(Some(msg)) = self.ctx.out.try_pop() { + writer.buffer(&msg)?; + wrote = true; + } + if wrote { + continue; + } + + let _ = self.children.poll(waiter); + if !self.ctx.out.is_empty() { + continue; + } + + if !self.listed_all { + match self.broadcasts.poll_next(waiter) { + Poll::Ready(Some(broadcast)) => { + self.list(broadcast); + continue; + } + Poll::Ready(None) => self.listed_all = true, + Poll::Pending => {} + } + } + + if self.listed_all && self.children.is_empty() { + return Poll::Ready(Ok(())); + } + return Poll::Pending; + } + } + + /// Announce a broadcast and start listing its tracks, unless the session does not allow it. + fn list(&mut self, broadcast: crate::broadcast::Consumer) { + let path = broadcast.info().path.clone(); + let Some(suffix) = path.strip_prefix(&self.prefix).map(|suffix| suffix.to_owned()) else { + return; + }; + let mut gate = self.ctx.shared.gate(&path); + if gate.poll_denied(&kio::Waiter::noop()).is_ready() { + tracing::debug!(broadcast = %path, "not listing a broadcast outside the grant"); + return; + } + let id = self.next_broadcast; + self.next_broadcast += 1; + self.ctx + .send(lite::SubscribeAllResponse::BroadcastStart(lite::BroadcastStart { + suffix, + epoch: broadcast.info().epoch.clone(), + })); + tracing::debug!(broadcast = %path, id, "listing broadcast"); + self.children.push(ListedBroadcast { + ctx: self.ctx.clone(), + id, + tracks: Some(broadcast.tracks()), + gate, + path, + children: kio::Tasks::new(), + }); + } +} + +/// One listed broadcast: lists each track its producer creates until it ends. +struct ListedBroadcast { + ctx: Arc>, + id: u64, + /// `None` once BROADCAST_END went out. + tracks: Option, + gate: crate::auth::Gate, + path: crate::PathOwned, + children: kio::Tasks>, +} + +impl kio::Task for ListedBroadcast { + type Output = (); + + fn poll(&mut self, waiter: &kio::Waiter) -> Poll<()> { + let _ = self.children.poll(waiter); + while let Some(tracks) = &mut self.tracks { + // A broadcast the session stops allowing lists nothing more; each of its tracks + // ends on its own gate. + let ended = match self.gate.poll_denied(waiter).is_ready() { + true => true, + false => match tracks.poll_next(waiter) { + Poll::Ready(Some(track)) => { + self.children.push(ListedTrack { + ctx: self.ctx.clone(), + broadcast: self.id, + gate: self.ctx.shared.gate(&self.path), + steer: self.ctx.steer.clone(), + seen: *self.ctx.steer.read(), + state: ListedState::Query { + querying: track.query().into_inner(), + track, + }, + id: None, + budget: kio::coop::Budget::new(32), + }); + continue; + } + Poll::Ready(None) => true, + Poll::Pending => false, + }, + }; + if !ended { + break; + } + tracing::debug!(broadcast = %self.path, id = self.id, "broadcast ended"); + self.ctx + .send(lite::SubscribeAllResponse::BroadcastEnd(lite::BroadcastEnd { broadcast: self.id })); + self.tracks = None; + } + + // Newly listed tracks start now rather than on the next wake. + match self.children.poll(waiter) { + Poll::Ready(()) if self.tracks.is_none() => Poll::Ready(()), + _ => Poll::Pending, + } + } +} + +/// One listed track: its TRACK_START, then a subscription served like a SUBSCRIBE. +struct ListedTrack { + ctx: Arc>, + broadcast: u64, + gate: crate::auth::Gate, + steer: kio::Consumer, + // The preferences this track last applied. + seen: AllSteer, + state: ListedState, + // The Track ID, once TRACK_START went out. + id: Option, + // A track that always has another group ready still lets its siblings run. + budget: kio::coop::Budget, +} + +// A state machine's enum is its storage: one transient instance per track, so the big +// variant is the working state, not padding held in bulk. +#[allow(clippy::large_enum_variant)] +enum ListedState { + /// Waiting for the track's immutable properties. + Query { + track: track::Consumer, + querying: track::Querying, + }, + /// Waiting for the model subscription to be confirmed. + Confirm { subscribing: track::Subscribing }, + Run(Box>), + /// The track finished: draining the in-flight group streams. + Drain { + children: kio::Tasks>, + // Demand lasts until the groups drain; see `SubscribeServe::Drain`. + _track: Box, + }, + /// Only in passing between states. + Done, +} + +impl kio::Task for ListedTrack { + type Output = (); + + fn poll(&mut self, waiter: &kio::Waiter) -> Poll<()> { + let Err(err) = ready!(self.poll_serve(waiter)) else { + return Poll::Ready(()); + }; + // A track that never went out has nothing to reset. + if let Some(track) = self.id { + match &err { + err if is_cancel(err) => tracing::debug!(track, "listed track cancelled"), + err => tracing::warn!(track, %err, "listed track error"), + } + let code = self + .ctx + .shared + .version + .encode_stream_code(&crate::StreamError::from(&err)); + self.ctx + .send(lite::SubscribeAllResponse::TrackReset(lite::TrackReset { + track, + code: code.into(), + })); + } + Poll::Ready(()) + } +} + +impl ListedTrack { + fn poll_serve(&mut self, waiter: &kio::Waiter) -> Poll> { + if self.gate.poll_denied(waiter).is_ready() { + return Poll::Ready(Err(Error::Unauthorized)); + } + loop { + ready!(self.budget.poll_yield(waiter)); + match &mut self.state { + ListedState::Query { track, querying } => { + let info = ready!(querying.poll_ok(waiter))?; + let id = self.ctx.next_track.fetch_add(1, Ordering::Relaxed); + self.id = Some(id); + self.ctx + .send(lite::SubscribeAllResponse::TrackStart(lite::TrackStart { + broadcast: self.broadcast, + name: track.name().to_string().into(), + info: lite::TrackInfo { + priority: info.priority, + max_age: info.max_age, + timescale: info.timescale, + }, + })); + self.seen = *self.steer.read(); + let subscription = crate::track::Subscription { + priority: self.seen.priority, + max_delay: self.seen.max_delay, + order: self.seen.order, + ..Default::default() + }; + let subscribing = track.subscribe(subscription).into_inner(); + self.state = ListedState::Confirm { subscribing }; + } + ListedState::Confirm { subscribing } => { + let track = ready!(subscribing.poll_ok(waiter))?; + let id = self.id.expect("assigned with TRACK_START"); + let shared = &self.ctx.shared; + let steer = Steer { + priority: self.seen.priority, + order: self.seen.order, + }; + let steer_tx = kio::Producer::new(steer); + let mut session = shared.session.clone(); + let group = session.send_group(self.seen.priority.into()); + let sub = Subscription { + session, + id: self.ctx.id, + track_name: Arc::from(track.name()), + priority: shared.priority.clone(), + group, + steer: steer_tx.consume(), + steer_seen: steer, + version: shared.version, + timescale: shared.version.wire_timescale(track.info().timescale), + runtime: shared.runtime.clone(), + opens: Default::default(), + track: Some(id), + }; + let bounds = Bounds { + start_group: None, + start_frame: 0, + end_group: None, + end_frame: None, + }; + self.state = ListedState::Run(Box::new(TrackRun::new(sub, track, bounds, steer_tx))); + } + ListedState::Run(run) => { + let current = *self.steer.read(); + if current != self.seen { + self.seen = current; + run.update(lite::SubscribeUpdate { + priority: current.priority, + max_delay: current.max_delay, + start_group: None, + end_group: None, + start_frame: 0, + end_frame: None, + order: current.order, + }); + } + // Wakes on the next update. + let _ = self.steer.poll(waiter, |steer| match **steer != current { + true => Poll::Ready(()), + false => Poll::Pending, + }); + + let mut out = Responder { + track: self.id.expect("assigned with TRACK_START"), + out: &self.ctx.out, + }; + if ready!(run.poll_step(&mut out, waiter))?.is_continue() { + continue; + } + let ListedState::Run(run) = std::mem::replace(&mut self.state, ListedState::Done) else { + unreachable!() + }; + let TrackRun { children, track, .. } = *run; + self.state = ListedState::Drain { + children, + _track: Box::new(track), + }; + } + ListedState::Drain { children, .. } => { + ready!(children.poll(waiter)); + return Poll::Ready(Ok(())); + } + ListedState::Done => return Poll::Ready(Ok(())), + } + } + } +} + +/// A listed track's responses, tagged with its Track ID on the shared stream. +struct Responder<'a> { + track: u64, + out: &'a kio::Queue>, +} + +impl Respond for Responder<'_> { + fn poll_flush(&mut self, _: &mut Context<'_>) -> Poll> { + // The request drains the queue onto its stream. + Poll::Ready(Ok(())) + } + + fn respond(&mut self, resp: lite::SubscribeResponse) -> Result<(), Error> { + let track = self.track; + let msg = match resp { + lite::SubscribeResponse::Start(start) => lite::SubscribeAllResponse::TrackOk(lite::TrackOk { track, start }), + lite::SubscribeResponse::End(end) => lite::SubscribeAllResponse::TrackEnd(lite::TrackEnd { track, end }), + // Lite-07 has neither, and it is the only version with SUBSCRIBE_ALL. + lite::SubscribeResponse::Ok(_) | lite::SubscribeResponse::Drop(_) => { + return Err(crate::coding::EncodeError::Version.into()); + } + }; + let _ = self.out.try_push(msg); + Ok(()) + } +} diff --git a/rs/moq-net/src/lite/mod.rs b/rs/moq-net/src/lite/mod.rs index d1d4702fff3..2e01e9bcc52 100644 --- a/rs/moq-net/src/lite/mod.rs +++ b/rs/moq-net/src/lite/mod.rs @@ -29,6 +29,7 @@ mod session; mod setup; mod stream; mod subscribe; +mod subscribe_all; #[cfg(feature = "async")] mod subscriber; #[cfg(all(test, feature = "async"))] @@ -58,6 +59,7 @@ pub(super) use session::*; pub use setup::*; pub use stream::*; pub use subscribe::*; +pub use subscribe_all::*; #[cfg(feature = "async")] use subscriber::*; #[allow(unused_imports)] diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 3695ea33c8b..4c432391a23 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -22,6 +22,10 @@ use crate::{ use super::Version; +#[path = "all_serve.rs"] +mod all_serve; +use all_serve::AllServe; + pub(super) struct PublisherConfig { /// The runtime that arms the publisher's timers. pub runtime: crate::time::Deadlines, @@ -298,6 +302,7 @@ enum ControlState { TrackInfo(RequestServe), Probe(ProbeServe), Auth(AuthServe), + SubscribeAll(AllServe), /// Decoding the peer's GOAWAY, surfaced through [`crate::Session::draining`]. Goaway { stream: Stream, @@ -337,6 +342,7 @@ impl Control { lite::ControlType::Goaway => version.has_goaway(), lite::ControlType::Fetch | lite::ControlType::Track => version.has_track_stream(), lite::ControlType::Auth => version.has_auth(), + lite::ControlType::SubscribeAll => version.has_subscribe_all(), _ => true, }); @@ -364,6 +370,9 @@ impl Control { ControlState::Probe(ProbeServe::new(self.shared.clone(), self.runtime.clone(), stream)) } lite::ControlType::Auth => ControlState::Auth(AuthServe::new(self.shared.clone(), stream)?), + lite::ControlType::SubscribeAll => { + ControlState::SubscribeAll(AllServe::new(self.shared.clone(), stream)) + } lite::ControlType::Goaway => ControlState::Goaway { stream }, lite::ControlType::Session => return Poll::Ready(Err(Error::UnexpectedStream)), }; @@ -374,6 +383,7 @@ impl Control { ControlState::TrackInfo(serve) => return serve.poll(waiter), ControlState::Probe(serve) => return serve.poll(waiter), ControlState::Auth(serve) => return serve.poll(waiter), + ControlState::SubscribeAll(serve) => return serve.poll(waiter), ControlState::Goaway { stream } => { let mut cx = waiter.context(); let msg = ready!(stream.reader.poll_decode::(&mut cx))?; @@ -2027,6 +2037,7 @@ impl Request for SubscribeServe { timescale, runtime: shared.runtime.clone(), opens: Default::default(), + track: None, }; let bounds = Bounds::from(msg.as_ref()); @@ -3211,6 +3222,8 @@ struct Subscription { runtime: crate::time::Deadlines, /// The group streams this subscription opened, shared by every group it serves. opens: Arc, + /// The Track ID within a SUBSCRIBE_ALL, or `None` for a SUBSCRIBE. + track: Option, } /// Counts a subscription's group streams for lite-07's SUBSCRIBE_END. @@ -3319,6 +3332,26 @@ impl Subscription { } } +/// Where a [`TrackRun`] sends its SUBSCRIBE responses: its own Subscribe Stream, or the +/// Subscribe All Stream it shares with the other tracks of a SUBSCRIBE_ALL. +trait Respond { + /// Deliver what was buffered before more is selected. + fn poll_flush(&mut self, cx: &mut Context<'_>) -> Poll>; + + /// Buffer one response. + fn respond(&mut self, resp: lite::SubscribeResponse) -> Result<(), Error>; +} + +impl Respond for Writer { + fn poll_flush(&mut self, cx: &mut Context<'_>) -> Poll> { + Writer::poll_flush(self, cx) + } + + fn respond(&mut self, resp: lite::SubscribeResponse) -> Result<(), Error> { + self.buffer(&resp) + } +} + /// A subscription's run loop: one subscriber cursor serving groups, datagrams, /// and SUBSCRIBE_UPDATE messages, with an in-flight group machine per group. struct TrackRun { @@ -3366,7 +3399,8 @@ impl TrackRun { let emit_range = ctx.version.has_track_stream(); let count_streams = ctx.version.has_stream_count(); - let datagrams = ctx.version.has_datagrams() && ctx.session.max_datagram_size() > 0; + // A SUBSCRIBE_ALL track is delivered on streams only. + let datagrams = ctx.track.is_none() && ctx.version.has_datagrams() && ctx.session.max_datagram_size() > 0; // Lite03 and Lite04 declare no end, so neither side could settle on a drop. let served = (emit_range && !count_streams).then(RangeSet::new); @@ -3458,8 +3492,8 @@ impl TrackRun { /// Serve until the live edge reaches the track's boundary. #[cfg(test)] - fn poll(&mut self, writer: &mut Writer, waiter: &kio::Waiter) -> Poll> { - while ready!(self.poll_step(writer, waiter))?.is_continue() {} + fn poll(&mut self, out: &mut impl Respond, waiter: &kio::Waiter) -> Poll> { + while ready!(self.poll_step(out, waiter))?.is_continue() {} Poll::Ready(Ok(())) } @@ -3468,12 +3502,12 @@ impl TrackRun { /// requester while work stays ready. fn poll_step( &mut self, - writer: &mut Writer, + out: &mut impl Respond, waiter: &kio::Waiter, ) -> Poll, Error>> { let mut cx = waiter.context(); // Deliver the buffered range messages before selecting more work. - ready!(writer.poll_flush(&mut cx))?; + ready!(out.poll_flush(&mut cx))?; // Drive the in-flight group machines; completions just retire. let _ = self.children.poll(waiter); @@ -3484,7 +3518,7 @@ impl TrackRun { return Poll::Ready(Err(err)); } - if self.poll_forward_drop(writer, waiter)? { + if self.poll_forward_drop(out, waiter)? { return Poll::Ready(Ok(ControlFlow::Continue(()))); } @@ -3493,7 +3527,7 @@ impl TrackRun { if let Some(group) = self.first.take() { match self.track.poll_start(waiter) { Poll::Ready(source) => { - self.start(group, source, writer)?; + self.start(group, source, out)?; return Poll::Ready(Ok(ControlFlow::Continue(()))); } Poll::Pending => { @@ -3521,7 +3555,7 @@ impl TrackRun { && start > largest { self.start = Some(start.group); - writer.buffer(&lite::SubscribeResponse::Start(lite::SubscribeStart { + out.respond(lite::SubscribeResponse::Start(lite::SubscribeStart { group: start.group, largest: Some(largest), }))?; @@ -3558,7 +3592,7 @@ impl TrackRun { && let Some(end) = group.sequence.checked_add(1) { // Lite-05/06 name it now; a no-op on other versions. - self.drop_range(group.sequence..end, writer)?; + self.drop_range(group.sequence..end, out)?; } return Poll::Ready(Ok(ControlFlow::Continue(()))); } @@ -3573,7 +3607,7 @@ impl TrackRun { // even if trailing groups (below `group`) are still in flight, then // keep serving them until the live edge reaches the boundary. self.end_sent = true; - writer.buffer(&lite::SubscribeResponse::End(lite::SubscribeEnd { group, streams: 0 }))?; + out.respond(lite::SubscribeResponse::End(lite::SubscribeEnd { group, streams: 0 }))?; } Recv::Finished if self.count_streams && !self.end_sent => { // The count is final only once no served group is still waiting to @@ -3586,10 +3620,10 @@ impl TrackRun { let group = final_sequence(ready!(self.track.poll_finished(waiter))?); let streams = self.ctx.opens.opened.load(Ordering::Relaxed); self.end_sent = true; - writer.buffer(&lite::SubscribeResponse::End(lite::SubscribeEnd { group, streams }))?; + out.respond(lite::SubscribeResponse::End(lite::SubscribeEnd { group, streams }))?; } Recv::Finished => { - self.drop_unserved(writer)?; + self.drop_unserved(out)?; return Poll::Ready(Ok(ControlFlow::Break(()))); } } @@ -3597,7 +3631,7 @@ impl TrackRun { } // Receiving can queue a stale skip after the poll above found nothing, without a wake. - if self.poll_forward_drop(writer, waiter)? { + if self.poll_forward_drop(out, waiter)? { return Poll::Ready(Ok(ControlFlow::Continue(()))); } Poll::Pending @@ -3608,7 +3642,7 @@ impl TrackRun { /// for it instead. fn poll_forward_drop( &mut self, - writer: &mut Writer, + out: &mut impl Respond, waiter: &kio::Waiter, ) -> Result { if self.start.is_none() || self.served.is_none() { @@ -3617,7 +3651,7 @@ impl TrackRun { let Poll::Ready(range) = self.track.poll_drop(waiter) else { return Ok(false); }; - self.drop_range(range, writer)?; + self.drop_range(range, out)?; Ok(true) } @@ -3640,7 +3674,7 @@ impl TrackRun { } /// Drop whatever part of `range` this subscription has not been served or dropped yet. - fn drop_range(&mut self, range: Range, writer: &mut Writer) -> Result<(), Error> { + fn drop_range(&mut self, range: Range, out: &mut impl Respond) -> Result<(), Error> { let Some(served) = &mut self.served else { return Ok(()); }; @@ -3650,7 +3684,7 @@ impl TrackRun { let gaps: Vec<_> = served.gaps(&range).collect(); for gap in gaps { served.insert(gap.clone()); - writer.buffer(&lite::SubscribeResponse::Drop(lite::SubscribeDrop { + out.respond(lite::SubscribeResponse::Drop(lite::SubscribeDrop { start: gap.start, end: gap.end - 1, error: 0, @@ -3662,7 +3696,7 @@ impl TrackRun { /// Name every sequence from the start to the end that this subscription never got. The /// track has ended, so none of them will be served now: groups it never produced (a /// skipped sequence), and groups the cursor passed over (stale, or missing their head). - fn drop_unserved(&mut self, writer: &mut Writer) -> Result<(), Error> { + fn drop_unserved(&mut self, out: &mut impl Respond) -> Result<(), Error> { let Some(start) = self.start else { // Without a SUBSCRIBE_START the subscriber owes itself no group (see // `SubStream::owed`) and settles on the FIN alone, so there is nothing to drop. @@ -3675,7 +3709,7 @@ impl TrackRun { }; // A cap below the end bounds what the subscriber is owed, as on its side. let end = final_sequence(self.track.subscription().end().map_or(fin, |end| end.min(fin))); - self.drop_range(start..end, writer) + self.drop_range(start..end, out) } } @@ -3691,7 +3725,7 @@ impl TrackRun { &mut self, group: group::Consumer, source: Option, - writer: &mut Writer, + out: &mut impl Respond, ) -> Result<(), Error> { let mut start = source.map_or(group.sequence, |source| source.min(group.sequence)); // A skipped group is never served, so it cannot be where the feed starts. This stops @@ -3708,7 +3742,7 @@ impl TrackRun { Poll::Ready(largest) => largest, Poll::Pending => None, }; - writer.buffer(&lite::SubscribeResponse::Start(lite::SubscribeStart { + out.respond(lite::SubscribeResponse::Start(lite::SubscribeStart { group: start, largest, }))?; @@ -3746,7 +3780,11 @@ impl TrackRun { None => Rank::Queue( self.ctx .priority - .insert(Priority::new(steer.priority, self.ctx.id, sequence).with_order(steer.order)), + .insert( + Priority::new(steer.priority, self.ctx.id, sequence) + .with_lane(self.ctx.track.unwrap_or(0)) + .with_order(steer.order), + ), ), }; self.children @@ -3901,12 +3939,24 @@ impl GroupServe { self.settle_open(true); let mut writer = Writer::new(stream, self.ctx.version); - let msg = lite::Group { - subscribe: self.ctx.id, - sequence: self.sequence, - frame_start: self.frame_start, + let header = match self.ctx.track { + None => writer.buffer(&lite::DataType::Group).and_then(|()| { + writer.buffer(&lite::Group { + subscribe: self.ctx.id, + sequence: self.sequence, + frame_start: self.frame_start, + }) + }), + Some(track) => writer.buffer(&lite::DataType::TrackGroup).and_then(|()| { + writer.buffer(&lite::TrackGroup { + subscribe: self.ctx.id, + track, + sequence: self.sequence, + frame_start: self.frame_start, + }) + }), }; - if let Err(err) = writer.buffer(&lite::DataType::Group).and_then(|()| writer.buffer(&msg)) { + if let Err(err) = header { // The header wrote nothing, so the group-stream reset has nothing to pin // it to this subscription. The run loop reads the slot and resets the // subscribe stream instead. @@ -4238,6 +4288,7 @@ mod serve_group_test { timescale: Some(crate::Timescale::MILLI), runtime: crate::time::sim(), opens: Default::default(), + track: None, }; let track = track::Producer::new( @@ -4283,6 +4334,7 @@ mod serve_group_test { timescale: Some(crate::Timescale::MILLI), runtime: crate::time::sim(), opens: Default::default(), + track: None, }; let track = track::Producer::new( @@ -4332,6 +4384,7 @@ mod serve_group_test { timescale: Some(crate::Timescale::MILLI), runtime: crate::time::sim(), opens: Default::default(), + track: None, }; let track = track::Producer::new( @@ -4399,6 +4452,7 @@ mod serve_group_test { timescale: Some(crate::Timescale::MILLI), runtime: crate::time::sim(), opens: Default::default(), + track: None, }; let track = track::Producer::new( @@ -4475,6 +4529,7 @@ mod serve_group_test { timescale: Some(crate::Timescale::MILLI), runtime: crate::time::sim(), opens: Default::default(), + track: None, }; let track = track::Producer::new( @@ -4523,6 +4578,7 @@ mod serve_group_test { timescale: Some(crate::Timescale::MILLI), runtime: crate::time::sim(), opens: Default::default(), + track: None, }; let track = track::Producer::new( @@ -4570,6 +4626,7 @@ mod serve_group_test { timescale: Some(crate::Timescale::MILLI), runtime: crate::time::sim(), opens: Default::default(), + track: None, }; let track = track::Producer::new( @@ -4629,6 +4686,7 @@ mod serve_group_test { timescale: Some(crate::Timescale::MILLI), runtime: crate::time::sim(), opens: Default::default(), + track: None, }; let bounds = Bounds { start_group: Some(0), @@ -5460,6 +5518,7 @@ mod serve_group_test { timescale: Some(crate::Timescale::MILLI), runtime: crate::time::sim(), opens: opens.clone(), + track: None, }; let bounds = Bounds { start_group: Some(start_group), diff --git a/rs/moq-net/src/lite/stream.rs b/rs/moq-net/src/lite/stream.rs index 4ccadbe4140..41254bbc568 100644 --- a/rs/moq-net/src/lite/stream.rs +++ b/rs/moq-net/src/lite/stream.rs @@ -15,6 +15,8 @@ pub enum ControlType { Goaway = 5, Track = 6, Auth = 7, + /// Every track of every broadcast under a prefix. Lite07+ only. + SubscribeAll = 8, } impl Decode for ControlType { @@ -39,6 +41,8 @@ pub enum DataType { Group = 0, /// The lite-05+ SETUP stream: one SETUP message, then FIN. Setup = 1, + /// A group of a SUBSCRIBE_ALL track. Lite07+ only. + TrackGroup = 2, } impl Decode for DataType { diff --git a/rs/moq-net/src/lite/subscribe.rs b/rs/moq-net/src/lite/subscribe.rs index f34b38d3d3a..1d3ea64b4a7 100644 --- a/rs/moq-net/src/lite/subscribe.rs +++ b/rs/moq-net/src/lite/subscribe.rs @@ -138,7 +138,7 @@ pub(super) fn pad_group_order(w: &mut Encoder<'_>, version: Version) -> Result<( /// Decode the byte after the subscriber priority shared by SUBSCRIBE and SUBSCRIBE_UPDATE: /// lite-07's `Group Order`, or the retired `Ordered` byte an older layout still carries. -fn decode_order(r: &mut Decoder<'_>, version: Version) -> Result { +pub(super) fn decode_order(r: &mut Decoder<'_>, version: Version) -> Result { if !version.has_order() { skip_group_order(r, version)?; return Ok(Order::Descending); @@ -152,7 +152,7 @@ fn decode_order(r: &mut Decoder<'_>, version: Version) -> Result, version: Version, order: Order) -> Result<(), EncodeError> { +pub(super) fn encode_order(w: &mut Encoder<'_>, version: Version, order: Order) -> Result<(), EncodeError> { if !version.has_order() { return pad_group_order(w, version); } diff --git a/rs/moq-net/src/lite/subscribe_all.rs b/rs/moq-net/src/lite/subscribe_all.rs new file mode 100644 index 00000000000..df397a7c5ad --- /dev/null +++ b/rs/moq-net/src/lite/subscribe_all.rs @@ -0,0 +1,436 @@ +use std::{borrow::Cow, time::Duration}; + +use crate::{ + Path, + coding::{Decode, DecodeError, Decoder, Encode, EncodeError, Encoder}, + track::Order, +}; + +use super::{Message, SubscribeEnd, SubscribeStart, TrackInfo, Version}; + +/// Sent by the subscriber on a Subscribe All Stream (0x8) to subscribe to every track of +/// every broadcast under a prefix, including tracks and broadcasts that appear later. +/// +/// Lite07+ only. +#[derive(Clone, Debug)] +pub struct SubscribeAll<'a> { + /// Shares the SUBSCRIBE id space: group streams name it. + pub id: u64, + pub prefix: Path<'a>, + pub priority: u8, + pub order: Order, + pub max_delay: Duration, +} + +impl Message for SubscribeAll<'_> { + fn decode_msg(r: &mut Decoder<'_>, version: Version) -> Result { + if !version.has_subscribe_all() { + return Err(DecodeError::Version); + } + + Ok(Self { + id: r.varint()?, + prefix: Path::decode(r, version)?, + priority: r.u8()?, + order: super::subscribe::decode_order(r, version)?, + max_delay: Duration::from_millis(r.varint()?), + }) + } + + fn encode_msg(&self, w: &mut Encoder<'_>, version: Version) -> Result<(), EncodeError> { + if !version.has_subscribe_all() { + return Err(EncodeError::Version); + } + + w.varint(self.id)?; + self.prefix.encode(w, version)?; + w.u8(self.priority); + super::subscribe::encode_order(w, version, self.order)?; + w.varint(u64::try_from(self.max_delay.as_millis()).map_err(|_| EncodeError::BoundsExceeded)?) + } +} + +/// Sent by the subscriber to change every track of a SUBSCRIBE_ALL at once. +/// +/// Lite07+ only. +#[derive(Clone, Debug)] +pub struct SubscribeAllUpdate { + pub priority: u8, + pub order: Order, + pub max_delay: Duration, +} + +impl Message for SubscribeAllUpdate { + fn decode_msg(r: &mut Decoder<'_>, version: Version) -> Result { + if !version.has_subscribe_all() { + return Err(DecodeError::Version); + } + + Ok(Self { + priority: r.u8()?, + order: super::subscribe::decode_order(r, version)?, + max_delay: Duration::from_millis(r.varint()?), + }) + } + + fn encode_msg(&self, w: &mut Encoder<'_>, version: Version) -> Result<(), EncodeError> { + if !version.has_subscribe_all() { + return Err(EncodeError::Version); + } + + w.u8(self.priority); + super::subscribe::encode_order(w, version, self.order)?; + w.varint(u64::try_from(self.max_delay.as_millis()).map_err(|_| EncodeError::BoundsExceeded)?) + } +} + +/// A broadcast under the prefix, implicitly assigned the next Broadcast ID on the stream. +#[derive(Clone, Debug)] +pub struct BroadcastStart<'a> { + /// The broadcast path relative to the requested prefix. + pub suffix: Path<'a>, + pub epoch: Option, +} + +impl Message for BroadcastStart<'_> { + fn decode_msg(r: &mut Decoder<'_>, version: Version) -> Result { + Ok(Self { + suffix: Path::decode(r, version)?, + epoch: super::epoch::decode_epoch(r, version)?, + }) + } + + fn encode_msg(&self, w: &mut Encoder<'_>, version: Version) -> Result<(), EncodeError> { + self.suffix.encode(w, version)?; + super::epoch::encode_epoch(w, version, self.epoch.as_ref()) + } +} + +/// A broadcast that takes no new tracks. Its announced tracks end on their own. +#[derive(Clone, Debug)] +pub struct BroadcastEnd { + pub broadcast: u64, +} + +impl Message for BroadcastEnd { + fn decode_msg(r: &mut Decoder<'_>, _: Version) -> Result { + Ok(Self { broadcast: r.varint()? }) + } + + fn encode_msg(&self, w: &mut Encoder<'_>, _: Version) -> Result<(), EncodeError> { + w.varint(self.broadcast) + } +} + +/// A track of a listed broadcast with its immutable properties, implicitly assigned the +/// next Track ID on the stream. +#[derive(Clone, Debug)] +pub struct TrackStart<'a> { + pub broadcast: u64, + pub name: Cow<'a, str>, + pub info: TrackInfo, +} + +impl Message for TrackStart<'_> { + fn decode_msg(r: &mut Decoder<'_>, version: Version) -> Result { + Ok(Self { + broadcast: r.varint()?, + name: Cow::Owned(r.string()?), + info: TrackInfo::decode_msg(r, version)?, + }) + } + + fn encode_msg(&self, w: &mut Encoder<'_>, version: Version) -> Result<(), EncodeError> { + w.varint(self.broadcast)?; + w.string(&self.name)?; + self.info.encode_msg(w, version) + } +} + +/// A listed track's SUBSCRIBE_OK. +#[derive(Clone, Debug)] +pub struct TrackOk { + pub track: u64, + pub start: SubscribeStart, +} + +impl Message for TrackOk { + fn decode_msg(r: &mut Decoder<'_>, version: Version) -> Result { + Ok(Self { + track: r.varint()?, + start: SubscribeStart::decode_msg(r, version)?, + }) + } + + fn encode_msg(&self, w: &mut Encoder<'_>, version: Version) -> Result<(), EncodeError> { + w.varint(self.track)?; + self.start.encode_msg(w, version) + } +} + +/// A listed track's SUBSCRIBE_END. +#[derive(Clone, Debug)] +pub struct TrackEnd { + pub track: u64, + pub end: SubscribeEnd, +} + +impl Message for TrackEnd { + fn decode_msg(r: &mut Decoder<'_>, version: Version) -> Result { + Ok(Self { + track: r.varint()?, + end: SubscribeEnd::decode_msg(r, version)?, + }) + } + + fn encode_msg(&self, w: &mut Encoder<'_>, version: Version) -> Result<(), EncodeError> { + w.varint(self.track)?; + self.end.encode_msg(w, version) + } +} + +/// A listed track that failed, standing in for the reset of its own Subscribe Stream. +#[derive(Clone, Debug)] +pub struct TrackReset { + pub track: u64, + /// A stream error code. + pub code: u64, +} + +impl Message for TrackReset { + fn decode_msg(r: &mut Decoder<'_>, _: Version) -> Result { + Ok(Self { + track: r.varint()?, + code: r.varint()?, + }) + } + + fn encode_msg(&self, w: &mut Encoder<'_>, _: Version) -> Result<(), EncodeError> { + w.varint(self.track)?; + w.varint(self.code) + } +} + +/// A message the publisher sends on a Subscribe All Stream, prefixed with its type. +#[derive(Clone, Debug)] +pub enum SubscribeAllResponse<'a> { + BroadcastStart(BroadcastStart<'a>), + BroadcastEnd(BroadcastEnd), + TrackStart(TrackStart<'a>), + TrackOk(TrackOk), + TrackEnd(TrackEnd), + TrackReset(TrackReset), +} + +impl Encode for SubscribeAllResponse<'_> { + fn encode(&self, w: &mut Encoder<'_>, version: Version) -> Result<(), EncodeError> { + if !version.has_subscribe_all() { + return Err(EncodeError::Version); + } + match self { + Self::BroadcastStart(msg) => { + w.varint(0)?; + msg.encode(w, version) + } + Self::BroadcastEnd(msg) => { + w.varint(1)?; + msg.encode(w, version) + } + Self::TrackStart(msg) => { + w.varint(2)?; + msg.encode(w, version) + } + Self::TrackOk(msg) => { + w.varint(3)?; + msg.encode(w, version) + } + Self::TrackEnd(msg) => { + w.varint(4)?; + msg.encode(w, version) + } + Self::TrackReset(msg) => { + w.varint(5)?; + msg.encode(w, version) + } + } + } +} + +impl Decode for SubscribeAllResponse<'_> { + fn decode(r: &mut Decoder<'_>, version: Version) -> Result { + if !version.has_subscribe_all() { + return Err(DecodeError::Version); + } + match r.varint()? { + 0 => Ok(Self::BroadcastStart(BroadcastStart::decode(r, version)?)), + 1 => Ok(Self::BroadcastEnd(BroadcastEnd::decode(r, version)?)), + 2 => Ok(Self::TrackStart(TrackStart::decode(r, version)?)), + 3 => Ok(Self::TrackOk(TrackOk::decode(r, version)?)), + 4 => Ok(Self::TrackEnd(TrackEnd::decode(r, version)?)), + 5 => Ok(Self::TrackReset(TrackReset::decode(r, version)?)), + typ => Err(DecodeError::InvalidMessage(typ)), + } + } +} + +/// The header of a group stream (0x2) carrying a SUBSCRIBE_ALL track. +#[derive(Clone, Debug)] +pub struct TrackGroup { + pub subscribe: u64, + pub track: u64, + pub sequence: u64, + /// See [`super::Group::frame_start`]. + pub frame_start: u64, +} + +impl Message for TrackGroup { + fn decode_msg(r: &mut Decoder<'_>, version: Version) -> Result { + if !version.has_subscribe_all() { + return Err(DecodeError::Version); + } + Ok(Self { + subscribe: r.varint()?, + track: r.varint()?, + sequence: r.varint()?, + frame_start: r.varint()?, + }) + } + + fn encode_msg(&self, w: &mut Encoder<'_>, version: Version) -> Result<(), EncodeError> { + if !version.has_subscribe_all() { + return Err(EncodeError::Version); + } + w.varint(self.subscribe)?; + w.varint(self.track)?; + w.varint(self.sequence)?; + w.varint(self.frame_start) + } +} + +#[cfg(test)] +mod test { + use super::*; + + fn encode>(msg: &T, version: Version) -> Result, EncodeError> { + let mut buf = Vec::new(); + msg.encode(&mut Encoder::new(&mut buf, version.into()), version)?; + Ok(buf) + } + + #[test] + fn request_roundtrips() { + let msg = SubscribeAll { + id: 3, + prefix: Path::new("room/a"), + priority: 7, + order: Order::Ascending, + max_delay: Duration::from_millis(250), + }; + let buf = encode(&msg, Version::Lite07).unwrap(); + let (got, _) = SubscribeAll::decode_slice(&buf, Version::Lite07).unwrap(); + assert_eq!(got.id, 3); + assert_eq!(got.prefix.as_str(), "room/a"); + assert_eq!((got.priority, got.order, got.max_delay), (7, Order::Ascending, msg.max_delay)); + + let update = SubscribeAllUpdate { + priority: 1, + order: Order::Descending, + max_delay: Duration::ZERO, + }; + let buf = encode(&update, Version::Lite07).unwrap(); + let (got, _) = SubscribeAllUpdate::decode_slice(&buf, Version::Lite07).unwrap(); + assert_eq!((got.priority, got.order, got.max_delay), (1, Order::Descending, Duration::ZERO)); + } + + /// Older versions have no Subscribe All Stream, and nothing emulates it. + #[test] + fn refused_before_lite07() { + let msg = SubscribeAll { + id: 0, + prefix: Path::new(""), + priority: 0, + order: Order::Descending, + max_delay: Duration::ZERO, + }; + assert!(encode(&msg, Version::Lite06).is_err()); + let buf = encode(&msg, Version::Lite07).unwrap(); + assert!(SubscribeAll::decode_slice(&buf, Version::Lite06).is_err()); + } + + #[test] + fn responses_roundtrip() { + let epoch = crate::Epoch::mint(); + let responses = [ + SubscribeAllResponse::BroadcastStart(BroadcastStart { + suffix: Path::new("alice"), + epoch: Some(epoch.clone()), + }), + SubscribeAllResponse::BroadcastEnd(BroadcastEnd { broadcast: 4 }), + SubscribeAllResponse::TrackStart(TrackStart { + broadcast: 4, + name: "video".into(), + info: TrackInfo { + priority: 2, + max_age: Some(Duration::from_secs(1)), + timescale: Some(crate::Timescale::MILLI), + }, + }), + SubscribeAllResponse::TrackOk(TrackOk { + track: 9, + start: SubscribeStart { + group: 5, + largest: Some(crate::track::Position { group: 5, frame: 1 }), + }, + }), + SubscribeAllResponse::TrackEnd(TrackEnd { + track: 9, + end: SubscribeEnd { group: 6, streams: 2 }, + }), + SubscribeAllResponse::TrackReset(TrackReset { track: 9, code: 0x3b }), + ]; + for msg in responses { + let buf = encode(&msg, Version::Lite07).unwrap(); + let (got, _) = SubscribeAllResponse::decode_slice(&buf, Version::Lite07).unwrap(); + match (msg, got) { + (SubscribeAllResponse::BroadcastStart(a), SubscribeAllResponse::BroadcastStart(b)) => { + assert_eq!((a.suffix.as_str(), a.epoch), (b.suffix.as_str(), b.epoch)) + } + (SubscribeAllResponse::BroadcastEnd(a), SubscribeAllResponse::BroadcastEnd(b)) => { + assert_eq!(a.broadcast, b.broadcast) + } + (SubscribeAllResponse::TrackStart(a), SubscribeAllResponse::TrackStart(b)) => { + assert_eq!((a.broadcast, a.name), (b.broadcast, b.name)); + assert_eq!( + (a.info.priority, a.info.max_age, a.info.timescale), + (b.info.priority, b.info.max_age, b.info.timescale) + ); + } + (SubscribeAllResponse::TrackOk(a), SubscribeAllResponse::TrackOk(b)) => { + assert_eq!((a.track, a.start.group, a.start.largest), (b.track, b.start.group, b.start.largest)) + } + (SubscribeAllResponse::TrackEnd(a), SubscribeAllResponse::TrackEnd(b)) => { + assert_eq!((a.track, a.end.group, a.end.streams), (b.track, b.end.group, b.end.streams)) + } + (SubscribeAllResponse::TrackReset(a), SubscribeAllResponse::TrackReset(b)) => { + assert_eq!((a.track, a.code), (b.track, b.code)) + } + (a, b) => panic!("decoded {b:?} for {a:?}"), + } + } + } + + #[test] + fn group_header_roundtrips() { + let msg = TrackGroup { + subscribe: 1, + track: 2, + sequence: 3, + frame_start: 4, + }; + let buf = encode(&msg, Version::Lite07).unwrap(); + assert_eq!(buf, [4, 1, 2, 3, 4]); + let (got, _) = TrackGroup::decode_slice(&buf, Version::Lite07).unwrap(); + assert_eq!((got.subscribe, got.track, got.sequence, got.frame_start), (1, 2, 3, 4)); + assert!(encode(&msg, Version::Lite06).is_err()); + } +} diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 828082fa9ea..507df682aba 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -68,7 +68,7 @@ pub(super) struct Subscriber { // `Hop::UNKNOWN` (0), the reserved "no identity" value. The assigned id stays // local and is never written into a hop chain. session_origin: crate::Hop, - subscribes: Lock>, + subscribes: Lock>, /// Why this session ended, once it has. A track still waiting on TRACK_INFO is /// not in [`Self::subscribes`], so dropping its request reads this instead of /// becoming [`Error::Dropped`]. @@ -90,6 +90,22 @@ pub(super) struct Subscriber { pub(super) announces: crate::session::Slots, } +/// What a group stream names: a SUBSCRIBE, or one track of a SUBSCRIBE_ALL. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +struct Feed { + subscribe: u64, + track: Option, +} + +impl Feed { + fn subscribe(id: u64) -> Self { + Self { + subscribe: id, + track: None, + } + } +} + #[derive(Clone)] struct TrackEntry { producer: track::Producer, @@ -495,7 +511,7 @@ impl Subscriber { /// Remove a subscription, releasing the session's handle on its producer. fn remove_subscribe(&self, id: u64) { - self.subscribes.lock().remove(&id); + self.subscribes.lock().remove(&Feed::subscribe(id)); } /// Decode one datagram body and hand it to the matching subscription's producer. @@ -507,9 +523,11 @@ impl Subscriber { let mut subscribes = self.subscribes.lock(); // An unknown subscription's datagram is dropped below, however it parses. let dg = lite::Datagram::decode(payload, self.version, |id| { - subscribes.get(&id).is_some_and(|entry| entry.timescale.is_some()) + subscribes + .get(&Feed::subscribe(id)) + .is_some_and(|entry| entry.timescale.is_some()) })?; - let Some(entry) = subscribes.get_mut(&dg.subscribe) else { + let Some(entry) = subscribes.get_mut(&Feed::subscribe(dg.subscribe)) else { // Unknown or already-closed subscription: drop the datagram. return Ok(()); }; @@ -546,7 +564,7 @@ impl Subscriber { // Owns the active subscriptions for exactly as long as the driver. A dropped // driver is how a cancelled session unwinds, so cleanup cannot depend on any // poll returning Ready. -struct SubscriptionCleanup(Lock>); +struct SubscriptionCleanup(Lock>); impl SubscriptionCleanup { /// End every active subscription with the session's error. Group machines own @@ -781,11 +799,21 @@ impl UniServe { if matches!(kind, lite::DataType::Setup) && self.subscriber.version.has_setup_stream() { self.subscriber.peer_setup.claim()?; } - let UniState::Start { reader } = std::mem::replace(&mut self.state, UniState::Done) else { + let UniState::Start { mut reader } = std::mem::replace(&mut self.state, UniState::Done) else { unreachable!() }; self.state = match kind { - lite::DataType::Group => UniState::Group(GroupRecv::new(self.subscriber.clone(), reader)), + lite::DataType::Group => { + UniState::Group(GroupRecv::new(self.subscriber.clone(), reader, false)) + } + lite::DataType::TrackGroup if self.subscriber.version.has_subscribe_all() => { + UniState::Group(GroupRecv::new(self.subscriber.clone(), reader, true)) + } + // A stream type this version predates resets only that stream. + lite::DataType::TrackGroup => { + reader.abort(&Error::UnexpectedStream); + return Poll::Ready(Ok(())); + } lite::DataType::Setup => UniState::Setup { reader }, }; } @@ -829,6 +857,8 @@ impl UniServe { struct GroupRecv { subscriber: Subscriber, reader: Reader, + /// Whether the stream carries a SUBSCRIBE_ALL track (0x2) rather than a SUBSCRIBE's. + listed: bool, state: GroupRecvState, } @@ -852,24 +882,45 @@ enum GroupRecvState { } impl GroupRecv { - fn new(subscriber: Subscriber, reader: Reader) -> Self { + fn new(subscriber: Subscriber, reader: Reader, listed: bool) -> Self { Self { subscriber, reader, + listed, state: GroupRecvState::Header, } } + /// Read the stream's header: the feed it belongs to, then the group's sequence and + /// first frame. + fn poll_header(&mut self, cx: &mut std::task::Context<'_>) -> Poll> { + if !self.listed { + let hdr = ready!(self.reader.poll_decode::(cx))?; + return Poll::Ready(Ok((Feed::subscribe(hdr.subscribe), hdr))); + } + let hdr = ready!(self.reader.poll_decode::(cx))?; + let feed = Feed { + subscribe: hdr.subscribe, + track: Some(hdr.track), + }; + let group = lite::Group { + subscribe: hdr.subscribe, + sequence: hdr.sequence, + frame_start: hdr.frame_start, + }; + Poll::Ready(Ok((feed, group))) + } + fn poll_serve(&mut self, waiter: &kio::Waiter) -> Poll> { loop { match &mut self.state { GroupRecvState::Header => { let mut cx = waiter.context(); - let hdr = ready!(self.reader.poll_decode::(&mut cx))?; + let (feed, hdr) = ready!(self.poll_header(&mut cx))?; let (group, track, timescale, reading) = { let mut subs = self.subscriber.subscribes.lock(); - let entry = subs.get_mut(&hdr.subscribe).ok_or(Error::Cancel)?; + let entry = subs.get_mut(&feed).ok_or(Error::Cancel)?; let group_info = group::Info { sequence: hdr.sequence }; // Stats (groups/frames/bytes) are counted in the model as the group @@ -2181,7 +2232,7 @@ mod tests { ); let mut consumer = track.consume().subscribe(None).await.unwrap(); subscriber.subscribes.lock().insert( - 7, + Feed::subscribe(7), TrackEntry { producer: track, timescale: Some(Timescale::MILLI), @@ -2237,7 +2288,7 @@ mod tests { ); track.finish_at(Position::group(3)).unwrap(); subscriber.subscribes.lock().insert( - 7, + Feed::subscribe(7), TrackEntry { producer: track.clone(), timescale: Some(Timescale::MILLI), @@ -2246,7 +2297,7 @@ mod tests { ); let (_, recv) = session.open_bi(Default::default()).await.unwrap(); - let mut group = GroupRecv::new(subscriber, Reader::new(recv, version)); + let mut group = GroupRecv::new(subscriber, Reader::new(recv, version), false); let res = kio::wait(|waiter| group.poll_serve(waiter)).await; match version { Version::Lite05 => { @@ -2307,7 +2358,7 @@ mod tests { .unwrap(); before.finish().unwrap(); subscriber.subscribes.lock().insert( - 7, + Feed::subscribe(7), TrackEntry { producer: track.clone(), timescale: Some(Timescale::MILLI), @@ -2316,7 +2367,7 @@ mod tests { ); let (_, recv) = session.open_bi(Default::default()).await.unwrap(); - let mut group = GroupRecv::new(subscriber, Reader::new(recv, version)); + let mut group = GroupRecv::new(subscriber, Reader::new(recv, version), false); let res = kio::wait(|waiter| group.poll_serve(waiter)).await; (res, track) } @@ -2374,7 +2425,7 @@ mod tests { cached.finish().unwrap(); let tail = kio::Producer::new(Tail::default()); subscriber.subscribes.lock().insert( - 7, + Feed::subscribe(7), TrackEntry { producer: track.clone(), timescale: Some(Timescale::MILLI), @@ -2383,7 +2434,7 @@ mod tests { ); let (_, recv) = session.open_bi(Default::default()).await.unwrap(); - let mut group = GroupRecv::new(subscriber, Reader::new(recv, version)); + let mut group = GroupRecv::new(subscriber, Reader::new(recv, version), false); let res = kio::wait(|waiter| group.poll_serve(waiter)).await; assert!(matches!(res, Err(Error::Duplicate)), "{res:?}"); drop(group); @@ -2652,7 +2703,7 @@ mod tests { .unwrap(); let mut received = producer.subscribe(None); subscriber.subscribes.lock().insert( - 7, + Feed::subscribe(7), TrackEntry { producer, timescale: Some(Timescale::MILLI), @@ -2850,7 +2901,7 @@ mod tests { // Parked on the first write: the stream is open and nothing has been sent yet. assert!(futures::poll!(establish.as_mut()).is_pending()); assert_eq!(session.log.bi_opens(), 1); - assert!(subscribes.lock().contains_key(&0), "registered before the wire"); + assert!(subscribes.lock().contains_key(&Feed::subscribe(0)), "registered before the wire"); let Ok(mut open) = gate.write() else { panic!("gate closed") @@ -5184,7 +5235,7 @@ impl TrackServe { let tail = kio::Producer::new(Tail::new(tail::grace(subscription.max_delay))); self.subscriber.subscribes.lock().insert( - id, + Feed::subscribe(id), TrackEntry { producer: producer.clone(), timescale, diff --git a/rs/moq-net/src/lite/version.rs b/rs/moq-net/src/lite/version.rs index 9749b2ea0aa..2b37f12ef42 100644 --- a/rs/moq-net/src/lite/version.rs +++ b/rs/moq-net/src/lite/version.rs @@ -21,9 +21,10 @@ pub enum Version { /// a `.`-prefixed segment below the requested prefix is left out. SUBSCRIBE_END /// carries the number of group streams opened, replacing SUBSCRIBE_DROP. /// ANNOUNCE_START and ANNOUNCE_UPDATE may copy a path head or hop-chain tail from a - /// live announcement on the same stream. Adds the Auth Stream. The wire format is - /// still work-in-progress, so it is advertised over ALPN as `moq-lite-07-wip` and - /// only when explicitly requested; the default version sets leave it out. + /// live announcement on the same stream. Adds the Auth Stream and the Subscribe All + /// Stream. The wire format is still work-in-progress, so it is advertised over ALPN + /// as `moq-lite-07-wip` and only when explicitly requested; the default version sets + /// leave it out. Lite07, } @@ -135,6 +136,17 @@ impl Version { } } + /// Whether the session has the Subscribe All Stream (0x8) and the group stream (0x2) + /// that carries its tracks. Added in lite-07; nothing emulates it on older versions. + #[allow(clippy::match_like_matches_macro)] + pub fn has_subscribe_all(self) -> bool { + // Match form so future versions default forward (AGENTS.md convention). + match self { + Self::Lite01 | Self::Lite02 | Self::Lite03 | Self::Lite04 | Self::Lite05 | Self::Lite06 => false, + _ => true, + } + } + /// Whether announcements carry implicit announce ids: each `active` /// ANNOUNCE_BROADCAST assigns the next per-stream ordinal, and `ended`/`restart` /// reference that id instead of repeating the path. Added in lite-06. diff --git a/rs/moq-net/src/priority.rs b/rs/moq-net/src/priority.rs index d7340d07fca..1e28a39f96f 100644 --- a/rs/moq-net/src/priority.rs +++ b/rs/moq-net/src/priority.rs @@ -20,8 +20,8 @@ use crate::track::Order; // Priority ordering: higher track value = higher priority, then subscription, then // group sequence in that subscription's own direction. -/// A group stream's rank: its subscription's track priority, the subscription it -/// belongs to, its group sequence in that subscription's [`Order`], and the first object +/// A group stream's rank: its subscription's track priority, the subscription (and the +/// track within it, for a SUBSCRIBE_ALL) it belongs to, its group sequence in that subscription's [`Order`], and the first object /// it carries. /// /// Higher `track` always wins. `group` breaks ties only *within* one `subscribe`: @@ -38,6 +38,7 @@ use crate::track::Order; pub struct Priority { track: u8, subscribe: u64, + lane: u64, order: Order, group: u64, start: u64, @@ -49,12 +50,19 @@ impl Priority { Self { track, subscribe, + lane: 0, order: Order::Descending, group, start: 0, } } + /// Scope the group tie-break to one track of a subscription that carries several. + pub fn with_lane(mut self, lane: u64) -> Self { + self.lane = lane; + self + } + /// Rank groups of the same subscription in `order` instead. pub fn with_order(mut self, order: Order) -> Self { self.order = order; @@ -78,6 +86,7 @@ impl Ord for Priority { // stable and independent of either one's direction. The older (lower id) // subscription goes first. .then(self.subscribe.cmp(&other.subscribe)) + .then(self.lane.cmp(&other.lane)) // Only an update in flight leaves one subscription's groups in two orders, and // keeping the two apart keeps the comparison total meanwhile. .then((self.order == Order::Ascending).cmp(&(other.order == Order::Ascending))) From 504edca306322a5a4887703722f9594078fbc7f8 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 18 Oct 2026 22:56:11 -0700 Subject: [PATCH 04/13] wip(moq-net): SUBSCRIBE_ALL subscriber and integration tests Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/lite/all_recv.rs | 408 ++++++++++++++++++ rs/moq-net/src/lite/subscriber.rs | 85 +++- rs/moq-net/src/model/listing.rs | 5 - rs/moq-net/src/model/mod.rs | 4 + rs/moq-net/tests/integration/main.rs | 1 + rs/moq-net/tests/integration/subscribe_all.rs | 362 ++++++++++++++++ 6 files changed, 859 insertions(+), 6 deletions(-) create mode 100644 rs/moq-net/src/lite/all_recv.rs create mode 100644 rs/moq-net/tests/integration/subscribe_all.rs diff --git a/rs/moq-net/src/lite/all_recv.rs b/rs/moq-net/src/lite/all_recv.rs new file mode 100644 index 00000000000..9c09ca23a9d --- /dev/null +++ b/rs/moq-net/src/lite/all_recv.rs @@ -0,0 +1,408 @@ +//! The SUBSCRIBE_ALL a session opens upstream for one prefix, feeding every listing of it. +//! +//! Each route the peer announces is its own handler, so a reader listing a prefix asks +//! every one of them. The session answers all of those with a single request per prefix: +//! the peer lists everything it holds there, whichever of its routes serves it. + +use super::*; + +use crate::coding::StreamCodes; + +/// One upstream SUBSCRIBE_ALL and the listings it fills. +pub(super) struct ListServe { + subscriber: Subscriber, + /// The prefix as the wire names it, relative to the session's root. + prefix: PathOwned, + /// Further listings of the same prefix, joining this request. + input: kio::Queue, + outputs: Vec, + id: u64, + state: ListState, + broadcasts: HashMap, + next_broadcast: u64, + tracks: HashMap>, + /// The TRACK_STARTs read, which a group stream ahead of its own waits on. + started: kio::Producer, + /// Ended tracks waiting for their last group streams. + settling: kio::Tasks, + // Dedicated handles for opening the stream and watching the session close. + session: S, + closed: S, +} + +/// One listing this request fills, and the key of each broadcast it holds there. +struct Output { + listing: crate::model::Listing, + keys: HashMap, +} + +/// A broadcast the peer listed. +struct Listed { + /// `None` for one outside what the session may subscribe to: its tracks are ignored. + source: Option, + epoch: Option, + /// Closes the source's serve machine once the peer ends the broadcast. + _live: kio::Producer<()>, +} + +enum ListState { + Open, + Send { stream: Stream }, + Run { stream: Stream }, + /// Cancelled: FIN and wait for the acknowledgement. + Finish { writer: crate::coding::Writer }, + Done, +} + +impl ListServe { + pub(super) fn new( + subscriber: Subscriber, + prefix: PathOwned, + input: kio::Queue, + listing: crate::model::Listing, + ) -> Self { + let id = subscriber.next_id.fetch_add(1, atomic::Ordering::Relaxed); + let started = kio::Producer::new(0); + subscriber.started.lock().insert(id, started.consume()); + let session = subscriber.session.clone(); + let closed = subscriber.session.clone(); + Self { + session, + subscriber, + prefix, + input, + outputs: vec![Output { + listing, + keys: HashMap::new(), + }], + id, + state: ListState::Open, + broadcasts: HashMap::new(), + next_broadcast: 0, + tracks: HashMap::new(), + started, + settling: kio::Tasks::new(), + closed, + } + } + + /// Fill another listing of the prefix with every broadcast listed so far. + fn attach(&mut self, listing: crate::model::Listing) { + let mut keys = HashMap::new(); + for (id, listed) in &self.broadcasts { + if let Some(source) = &listed.source { + keys.insert(*id, listing.insert(source.consume(), listed.epoch.clone())); + } + } + self.outputs.push(Output { listing, keys }); + } + + /// Whether no reader is left on any listing, dropping the ones nobody reads. + fn poll_unused(&mut self, waiter: &kio::Waiter) -> bool { + self.outputs + .retain(|output| output.listing.poll_unused(waiter).is_pending()); + self.outputs.is_empty() + } + + fn poll_serve(&mut self, waiter: &kio::Waiter) -> Poll> { + let mut cx = waiter.context(); + loop { + // The session's end aborts the tracks with its own error. + if self.closed.poll_closed(&mut cx).is_ready() { + return Poll::Ready(Ok(())); + } + while let Ok(Some(listing)) = self.input.try_pop() { + self.attach(listing); + } + let _ = self.settling.poll(waiter); + + // Nobody reads any listing: cancel, unless one joins in the meantime. + if !matches!(self.state, ListState::Finish { .. } | ListState::Done) && self.poll_unused(waiter) { + self.input.close(); + while let Ok(Some(listing)) = self.input.try_pop() { + self.attach(listing); + } + if self.outputs.is_empty() { + tracing::debug!(prefix = %self.subscriber.log_path(&self.prefix), "subscribe all canceled (unused)"); + self.end(&Error::Cancel); + match std::mem::replace(&mut self.state, ListState::Done) { + ListState::Send { mut stream } | ListState::Run { mut stream } => { + if stream.writer.finish().is_ok() { + self.state = ListState::Finish { writer: stream.writer }; + } + } + ListState::Open | ListState::Finish { .. } | ListState::Done => {} + } + continue; + } + } + + match &mut self.state { + ListState::Open => { + let mut stream = ready!(Stream::poll_open( + &mut self.session, + self.subscriber.version, + &mut cx + ))?; + let defaults = Subscription::default(); + stream.writer.buffer(&lite::ControlType::SubscribeAll)?; + stream.writer.buffer(&lite::SubscribeAll { + id: self.id, + prefix: self.prefix.as_path(), + priority: defaults.priority, + order: defaults.order, + max_delay: defaults.max_delay, + })?; + tracing::info!(id = self.id, prefix = %self.subscriber.log_path(&self.prefix), "subscribe all started"); + self.state = ListState::Send { stream }; + } + ListState::Send { stream } => { + ready!(stream.writer.poll_flush(&mut cx))?; + let ListState::Send { stream } = std::mem::replace(&mut self.state, ListState::Done) else { + unreachable!() + }; + self.state = ListState::Run { stream }; + } + ListState::Run { stream } => { + match ready!(stream.reader.poll_decode_maybe::(&mut cx)) { + Ok(Some(msg)) => self.handle(msg)?, + // The publisher ended the request: its origin closed. + Ok(None) => { + self.end(&Error::Closed); + return Poll::Ready(Ok(())); + } + Err(err) => return Poll::Ready(Err(err)), + } + } + ListState::Finish { writer } => { + let _ = ready!(writer.poll_close(&mut cx)); + self.state = ListState::Done; + } + ListState::Done => return Poll::Ready(Ok(())), + } + } + } + + fn handle(&mut self, msg: lite::SubscribeAllResponse<'_>) -> Result<(), Error> { + match msg { + lite::SubscribeAllResponse::BroadcastStart(start) => { + let id = self.next_broadcast; + self.next_broadcast += 1; + let path = self.prefix.join(&start.suffix).to_owned(); + let live = kio::Producer::new(()); + let allowed = self + .subscriber + .auth + .within_limit(crate::auth::Direction::Subscribe, path.as_str()); + let source = allowed.then(|| self.mint(&path, start.epoch.clone(), &live)); + if let Some(source) = &source { + for output in &mut self.outputs { + let key = output.listing.insert(source.consume(), start.epoch.clone()); + output.keys.insert(id, key); + } + } + tracing::debug!(broadcast = %self.subscriber.log_path(&path), id, allowed, "listed broadcast"); + self.broadcasts.insert( + id, + Listed { + source, + epoch: start.epoch, + _live: live, + }, + ); + } + lite::SubscribeAllResponse::BroadcastEnd(end) => { + // Dropping it closes the source: no new tracks, while the listed ones run to + // their own end. + self.broadcasts.remove(&end.broadcast).ok_or(Error::ProtocolViolation)?; + for output in &mut self.outputs { + if let Some(key) = output.keys.remove(&end.broadcast) { + output.listing.remove(key); + } + } + } + lite::SubscribeAllResponse::TrackStart(start) => { + let id = *self.started.read(); + let listed = self.broadcasts.get(&start.broadcast).ok_or(Error::ProtocolViolation)?; + let producer = match &listed.source { + Some(source) => self.create(source, id, &start)?, + None => None, + }; + self.tracks.insert(id, producer); + // Registered first, so a group stream waiting on it finds the track. + if let Ok(mut started) = self.started.write() { + *started += 1; + } + } + lite::SubscribeAllResponse::TrackOk(ok) => { + let entry = self.tracks.get_mut(&ok.track).ok_or(Error::ProtocolViolation)?; + if let Some(producer) = entry { + producer.set_live(ok.start.largest); + if let Some(largest) = ok.start.largest { + producer.report_largest(largest); + } + let _ = producer.start_at(ok.start.group); + } + } + lite::SubscribeAllResponse::TrackEnd(end) => { + let entry = self.tracks.remove(&end.track).ok_or(Error::ProtocolViolation)?; + let Some(mut producer) = entry else { + return Ok(()); + }; + let feed = Feed { + subscribe: self.id, + track: Some(end.track), + }; + // Lower groups may still be on the wire behind the end, so readers wait at a + // hole until the tail settles. A boundary below a received group contradicts it. + if producer.finish_at_pending(Position::group(end.end.group)).is_err() { + return Err(Error::ProtocolViolation); + } + let Some(tail) = self.subscriber.subscribes.lock().get(&feed).map(|entry| entry.tail.clone()) else { + return Ok(()); + }; + if let Ok(mut tail) = tail.write() { + tail.expire(self.subscriber.runtime.now()); + } + self.settling.push(SettleTrack { + settle: Settle::new(&self.subscriber.runtime, tail.consume()), + streams: end.end.streams, + producer, + feed, + subscribes: self.subscriber.subscribes.clone(), + }); + } + lite::SubscribeAllResponse::TrackReset(reset) => { + let entry = self.tracks.remove(&reset.track).ok_or(Error::ProtocolViolation)?; + if let Some(producer) = entry { + let code = u32::try_from(reset.code).unwrap_or(u32::MAX); + let err = Error::Stream(self.subscriber.version.decode_stream_code(code)); + let _ = producer.abort(err); + } + self.subscriber.subscribes.lock().remove(&Feed { + subscribe: self.id, + track: Some(reset.track), + }); + } + } + Ok(()) + } + + /// A source for a listed broadcast, served like one minted for a request so the + /// tracks the peer does not list can still be asked for. + fn mint(&self, path: &PathOwned, epoch: Option, live: &kio::Producer<()>) -> crate::broadcast::Producer { + let source = self.subscriber.origin.create_source(path); + let dynamic = source.dynamic(); + let _ = self.subscriber.sources.try_push(MintedSource { + validity: Default::default(), + path: path.clone(), + epoch, + source: crate::model::broadcast::SourceGuard::new(source.clone()), + dynamic, + route: live.consume(), + }); + source + } + + /// Create a listed track in its broadcast's source and route its group streams to it. + fn create( + &self, + source: &crate::broadcast::Producer, + id: u64, + start: &lite::TrackStart<'_>, + ) -> Result, Error> { + let info = track::Info::default() + .with_timescale(start.info.timescale) + .with_max_age(start.info.max_age) + .with_priority(start.info.priority); + let producer = match source.create_track(start.name.as_ref(), info) { + Ok(producer) => producer, + // A reader asked for the track before the peer listed it, and its own + // subscription feeds it: list that one, and let this feed's groups go. + Err(Error::Duplicate) => { + source.list_track(&start.name); + return Ok(None); + } + Err(err) => return Err(err), + }; + let tail = kio::Producer::new(Tail::new(tail::grace(Subscription::default().max_delay))); + self.subscriber.subscribes.lock().insert( + Feed { + subscribe: self.id, + track: Some(id), + }, + TrackEntry { + producer: producer.clone(), + timescale: start.info.timescale, + tail, + }, + ); + Ok(Some(producer)) + } + + /// Stop feeding everything: the request is over. + fn end(&mut self, err: &Error) { + let mut subscribes = self.subscriber.subscribes.lock(); + for (id, producer) in self.tracks.drain() { + subscribes.remove(&Feed { + subscribe: self.id, + track: Some(id), + }); + if let Some(producer) = producer { + let _ = producer.abort(err.clone()); + } + } + drop(subscribes); + self.broadcasts.clear(); + self.outputs.clear(); + self.input.close(); + } +} + +impl kio::Task for ListServe { + type Output = (); + + fn poll(&mut self, waiter: &kio::Waiter) -> Poll<()> { + let res = ready!(self.poll_serve(waiter)); + if let Err(err) = res { + let err = lite::error::control(&mut self.subscriber.session.clone(), err); + tracing::debug!(prefix = %self.subscriber.log_path(&self.prefix), %err, "subscribe all error"); + self.end(&err); + if let ListState::Send { stream } | ListState::Run { stream } = + std::mem::replace(&mut self.state, ListState::Done) + { + stream.writer.abort(&err); + } + } + // Tracks that ended before the request did still settle. + self.settling.poll(waiter) + } +} + +impl Drop for ListServe { + fn drop(&mut self) { + self.subscriber.started.lock().remove(&self.id); + } +} + +/// A listed track the peer ended, waiting for its last group streams. +struct SettleTrack { + settle: Settle, + streams: u64, + producer: track::Producer, + feed: Feed, + subscribes: Lock>, +} + +impl kio::Task for SettleTrack { + type Output = (); + + fn poll(&mut self, waiter: &kio::Waiter) -> Poll<()> { + let streams = self.streams; + ready!(self.settle.poll(waiter, |tail| tail.streams() >= streams)); + self.subscribes.lock().remove(&self.feed); + self.producer.set_tail_pending(false); + let _ = self.producer.finish(); + Poll::Ready(()) + } +} diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 507df682aba..8f33b623839 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -18,6 +18,10 @@ use crate::tail::{self, Reading, Settle, Tail}; use kio::Lock; +#[path = "all_recv.rs"] +mod all_recv; +use all_recv::ListServe; + pub(super) struct SubscriberConfig { pub runtime: crate::time::Deadlines, pub session: S, @@ -82,6 +86,12 @@ pub(super) struct Subscriber { /// Sources minted by the announce half, drained by the driver into /// [`SourceServe`] machines. sources: kio::Queue, + /// Listings the announce half's routes were asked for, drained by the driver into + /// one [`ListServe`] per prefix. + listings: kio::Queue, + /// How many TRACK_STARTs each open SUBSCRIBE_ALL has read, for a group stream that + /// arrives ahead of its track's. + started: Lock>>, going_away: crate::goaway::GoingAway, auth: crate::auth::Handle, /// What this session may allocate up front for frames still arriving. @@ -137,6 +147,8 @@ impl Subscriber { peer_setup: config.peer_setup, cost: config.cost, sources: kio::Queue::new(), + listings: kio::Queue::new(), + started: Default::default(), going_away: config.going_away, auth: config.auth, frames: Default::default(), @@ -603,6 +615,9 @@ pub(super) struct SubscriberDriver { datagrams: Option>, /// One machine per minted source, serving the origin's track requests. sources: kio::Tasks>, + /// Each prefix's upstream SUBSCRIBE_ALL, by the queue it takes further listings on. + lists: HashMap>, + list_serves: kio::Tasks>, } impl SubscriberDriver { @@ -621,6 +636,8 @@ impl SubscriberDriver { bandwidth: Some(RecvBandwidth::new(subscriber.clone())), datagrams: Some(DatagramRecv::new(subscriber.clone())), sources: kio::Tasks::new(), + lists: HashMap::new(), + list_serves: kio::Tasks::new(), subscriber, } } @@ -674,8 +691,34 @@ impl SubscriberDriver { } let _ = self.sources.poll(waiter); + while let Poll::Ready(Ok(listing)) = self.subscriber.listings.poll_pop(waiter) { + self.list(listing); + } + let _ = self.list_serves.poll(waiter); + Poll::Pending } + + /// Fill a listing from the prefix's upstream SUBSCRIBE_ALL, opening it if none is live. + fn list(&mut self, listing: crate::model::Listing) { + // The listing names the prefix absolutely; the wire speaks relative to our root. + let Some(prefix) = listing.prefix().strip_prefix(self.subscriber.origin.root()) else { + return; + }; + let prefix = prefix.to_owned(); + let listing = match self.lists.get(&prefix) { + Some(input) => match input.try_push(listing) { + Ok(()) => return, + Err(err) => err.into_inner(), + }, + None => listing, + }; + self.lists.retain(|_, input| !input.is_closed()); + let input = kio::Queue::new(); + self.lists.insert(prefix.clone(), input.clone()); + self.list_serves + .push(ListServe::new(self.subscriber.clone(), prefix, input, listing)); + } } impl Drop for SubscriberDriver { @@ -859,6 +902,10 @@ struct GroupRecv { reader: Reader, /// Whether the stream carries a SUBSCRIBE_ALL track (0x2) rather than a SUBSCRIBE's. listed: bool, + /// A header read ahead of its track's TRACK_START. + header: Option<(Feed, lite::Group)>, + /// How many TRACK_STARTs the stream's SUBSCRIBE_ALL has read, while waiting for one. + started: Option>, state: GroupRecvState, } @@ -887,6 +934,8 @@ impl GroupRecv { subscriber, reader, listed, + header: None, + started: None, state: GroupRecvState::Header, } } @@ -916,7 +965,33 @@ impl GroupRecv { match &mut self.state { GroupRecvState::Header => { let mut cx = waiter.context(); - let (feed, hdr) = ready!(self.poll_header(&mut cx))?; + let (feed, hdr) = match self.header.take() { + Some(header) => header, + None => ready!(self.poll_header(&mut cx))?, + }; + // A SUBSCRIBE_ALL track's group can beat its TRACK_START, which travels on + // another stream: wait for the request to read it. + if let Some(track) = feed.track + && !self.subscriber.subscribes.lock().contains_key(&feed) + { + if self.started.is_none() { + let started = self.subscriber.started.lock().get(&feed.subscribe).cloned(); + self.started = Some(started.ok_or(Error::Cancel)?); + } + let started = self.started.as_ref().expect("set above"); + match started.poll(waiter, |count| match **count > track { + true => Poll::Ready(()), + false => Poll::Pending, + }) { + Poll::Ready(Ok(_)) => {} + // The request ended before it named the track. + Poll::Ready(Err(_)) => return Poll::Ready(Err(Error::Cancel)), + Poll::Pending => { + self.header = Some((feed, hdr)); + return Poll::Pending; + } + } + } let (group, track, timescale, reading) = { let mut subs = self.subscriber.subscribes.lock(); @@ -4916,6 +4991,14 @@ impl Announced { route: entry.live.consume(), }); } + // A reader listing a prefix this route covers. The session answers every route's + // listing of a prefix with one SUBSCRIBE_ALL. An older peer has none, and nothing + // emulates it: the listing stays empty. + while let Poll::Ready(Ok(listing)) = entry.dynamic.poll_requested_listing(route_waiter) { + if subscriber.version.has_subscribe_all() { + let _ = subscriber.listings.try_push(listing); + } + } } } diff --git a/rs/moq-net/src/model/listing.rs b/rs/moq-net/src/model/listing.rs index b335e358753..92ce174a43a 100644 --- a/rs/moq-net/src/model/listing.rs +++ b/rs/moq-net/src/model/listing.rs @@ -61,11 +61,6 @@ impl Listing { pub fn poll_unused(&self, waiter: &kio::Waiter) -> Poll<()> { self.state.poll_unused(waiter).map(|_| ()) } - - /// Whether this lists `prefix` into the same readers as `other`. - pub fn same_channel(&self, other: &Self) -> bool { - self.state.same_channel(&other.state) - } } /// What a [`Broadcasts`] cursor reads from one listing. diff --git a/rs/moq-net/src/model/mod.rs b/rs/moq-net/src/model/mod.rs index 2065b5abce6..b2381da5179 100644 --- a/rs/moq-net/src/model/mod.rs +++ b/rs/moq-net/src/model/mod.rs @@ -80,6 +80,10 @@ pub(crate) use origin_impl::interest_prefixes; // pending: /quest/m1/public-model/answer.md pub(crate) use origin_impl::Snapshot; +// A route's answer to a reader listing the broadcasts under a prefix. +#[cfg(feature = "async")] +pub(crate) use origin_impl::Listing; + // The advertise-only route guard, for tests shaping the route table. #[cfg(all(test, feature = "async"))] // private: test diff --git a/rs/moq-net/tests/integration/main.rs b/rs/moq-net/tests/integration/main.rs index 458ccd8e1c9..71a42fc3390 100644 --- a/rs/moq-net/tests/integration/main.rs +++ b/rs/moq-net/tests/integration/main.rs @@ -44,6 +44,7 @@ mod session_close; mod session_panic; mod settled_end_survives_abort; mod source_errors; +mod subscribe_all; mod subscribe_ranges; mod subscribe_tracks; mod subscription_end_integrity; diff --git a/rs/moq-net/tests/integration/subscribe_all.rs b/rs/moq-net/tests/integration/subscribe_all.rs new file mode 100644 index 00000000000..45c6d6ec0a8 --- /dev/null +++ b/rs/moq-net/tests/integration/subscribe_all.rs @@ -0,0 +1,362 @@ +#![cfg(feature = "async")] + +//! moq-lite-07's SUBSCRIBE_ALL, end to end: listing every broadcast under a prefix and +//! every track each one's producer creates, directly and through a relay. +//! +//! Deterministic: paused time and a single-threaded runtime over the mock transport. + +use crate::support; + +use std::time::Duration; + +use moq_net::{Epoch, Hop, Timestamp, Version, broadcast, origin, track}; +use support::harness::{MockConnectOptions, MockPair, connect_mock}; + +const TIMEOUT: Duration = Duration::from_secs(10); + +/// The Subscribe All Stream's type. +const SUBSCRIBE_ALL: u8 = 0x8; + +fn version() -> Version { + "moq-lite-07-wip".parse().unwrap() +} + +fn timed() -> track::Info { + track::Info::default().with_timescale(moq_net::Timescale::MILLI) +} + +fn produce_origin(hop: u64) -> origin::Producer { + let (producer, driver) = origin::Producer::new(origin::Config::new(Hop::new(hop).unwrap())); + support::harness::spawn(driver); + producer +} + +/// Have `to` pull everything `from` publishes. +async fn link(from: &origin::Producer, to: &origin::Producer) -> MockPair { + let mut options = MockConnectOptions::new(version()); + options.server_publish = Some(from.consume()); + options.client_subscribe = Some(to.clone()); + connect_mock(options).await +} + +/// A publisher, and the origin a subscriber reads it through, with `relay` hops between. +struct Chain { + publisher: origin::Producer, + subscriber: origin::Producer, + /// The session the subscriber's side opened toward the publisher's, first hop first. + links: Vec, + _relays: Vec, +} + +impl Chain { + async fn new(relay: bool) -> Self { + let publisher = produce_origin(1); + let subscriber = produce_origin(3); + let mut links = Vec::new(); + let mut relays = Vec::new(); + match relay { + true => { + let middle = produce_origin(2); + links.push(link(&publisher, &middle).await); + links.push(link(&middle, &subscriber).await); + relays.push(middle); + } + false => links.push(link(&publisher, &subscriber).await), + } + Self { + publisher, + subscriber, + links, + _relays: relays, + } + } +} + +/// Publish `path` with one timed track per name, each holding group 0. +fn publish(origin: &origin::Producer, path: &str, tracks: &[&str]) -> (broadcast::Producer, Vec) { + let broadcast = origin.create_broadcast(path).unwrap(); + let tracks = tracks + .iter() + .map(|name| { + let track = broadcast.create_track(*name, timed()).unwrap(); + write(&track, 0); + track + }) + .collect(); + broadcast.announce(Default::default()).unwrap(); + (broadcast, tracks) +} + +fn write(track: &track::Producer, sequence: u64) { + let mut group = track.create_group(moq_net::group::Info { sequence }).unwrap(); + group + .write_frame(Timestamp::from_millis(sequence).unwrap(), &b"x"[..]) + .unwrap(); + group.finish().unwrap(); +} + +async fn next_broadcast(cursor: &mut origin::Broadcasts) -> broadcast::Consumer { + moq_clock::timeout(TIMEOUT, cursor.next()) + .await + .expect("listing hung") + .expect("listing ended") +} + +async fn next_track(tracks: &mut broadcast::Tracks) -> track::Consumer { + moq_clock::timeout(TIMEOUT, tracks.next()) + .await + .expect("tracks hung") + .expect("tracks ended") +} + +/// The first group the track delivers, read whole. +async fn read_group(track: &track::Consumer) -> u64 { + let subscription = track::Subscription::default().with_max_delay(Duration::from_secs(60)); + let mut subscriber = moq_clock::timeout(TIMEOUT, track.subscribe(subscription)) + .await + .expect("subscribe hung") + .expect("subscribe"); + let mut group = moq_clock::timeout(TIMEOUT, subscriber.recv_group()) + .await + .expect("group hung") + .expect("group") + .expect("track ended"); + let frame = moq_clock::timeout(TIMEOUT, group.read_frame()) + .await + .expect("frame hung") + .expect("frame"); + assert!(frame.is_some(), "group {} has its frame", group.sequence); + group.sequence +} + +/// Tracks present at the request arrive, as does one the producer creates later. +async fn tracks_now_and_later(relay: bool) { + let chain = Chain::new(relay).await; + let (broadcast, _tracks) = publish(&chain.publisher, "room/a", &["video", "audio"]); + + let consumer = chain.subscriber.consume(); + moq_clock::timeout(TIMEOUT, consumer.routed("room/a")) + .await + .expect("announce hung"); + let mut cursor = consumer.broadcasts("room").unwrap(); + let listed = next_broadcast(&mut cursor).await; + assert_eq!(listed.info().path.as_str(), "room/a"); + + let mut tracks = listed.tracks(); + let mut names = Vec::new(); + for _ in 0..2 { + let track = next_track(&mut tracks).await; + assert_eq!(read_group(&track).await, 0, "relay={relay}"); + names.push(track.name().to_string()); + } + names.sort(); + assert_eq!(names, ["audio", "video"], "relay={relay}"); + + let late = broadcast.create_track("late", timed()).unwrap(); + let track = next_track(&mut tracks).await; + assert_eq!(track.name(), "late"); + write(&late, 3); + assert_eq!(read_group(&track).await, 3, "relay={relay}"); +} + +#[moq_clock::test] +async fn lists_tracks_now_and_later() { + tracks_now_and_later(false).await; +} + +#[moq_clock::test] +async fn lists_tracks_now_and_later_through_a_relay() { + tracks_now_and_later(true).await; +} + +/// A broadcast that appears after the request is listed; once it ends, so do its +/// tracks and its listing. +async fn broadcast_appears_and_ends(relay: bool) { + let chain = Chain::new(relay).await; + let (_first, _first_tracks) = publish(&chain.publisher, "room/a", &["video"]); + + let consumer = chain.subscriber.consume(); + moq_clock::timeout(TIMEOUT, consumer.routed("room/a")) + .await + .expect("announce hung"); + let mut cursor = consumer.broadcasts("room").unwrap(); + assert_eq!(next_broadcast(&mut cursor).await.info().path.as_str(), "room/a"); + + let (second, mut second_tracks) = publish(&chain.publisher, "room/b", &["video"]); + let listed = next_broadcast(&mut cursor).await; + assert_eq!(listed.info().path.as_str(), "room/b", "relay={relay}"); + let mut tracks = listed.tracks(); + let track = next_track(&mut tracks).await; + let subscription = track::Subscription::default().with_max_delay(Duration::from_secs(60)); + let mut subscriber = track.subscribe(subscription).await.expect("subscribe"); + + let video = second_tracks.pop().unwrap(); + video.finish().unwrap(); + drop(video); + drop(second); + + // Group 0, then the end of the track. + let group = moq_clock::timeout(TIMEOUT, subscriber.recv_group()) + .await + .expect("group hung") + .expect("group"); + assert_eq!(group.map(|group| group.sequence), Some(0), "relay={relay}"); + let end = moq_clock::timeout(TIMEOUT, subscriber.recv_group()) + .await + .expect("track end hung"); + assert!(matches!(end, Ok(None)), "relay={relay}: {:?}", end.map(|group| group.map(|group| group.sequence))); + let ended = moq_clock::timeout(TIMEOUT, tracks.next()) + .await + .expect("tracks end hung"); + assert!(ended.is_none(), "relay={relay}"); +} + +#[moq_clock::test] +async fn lists_a_broadcast_that_appears_and_ends() { + broadcast_appears_and_ends(false).await; +} + +#[moq_clock::test] +async fn lists_a_broadcast_that_appears_and_ends_through_a_relay() { + broadcast_appears_and_ends(true).await; +} + +fn epochs() -> (Epoch, Epoch) { + ( + "01900000-0000-7000-8000-000000000001".parse().unwrap(), + "01900000-0000-7000-8000-000000000002".parse().unwrap(), + ) +} + +/// A new publisher instance at the same path is another broadcast, listed again. +async fn new_epoch(relay: bool) { + let chain = Chain::new(relay).await; + let (old, new) = epochs(); + let first = chain.publisher.create_broadcast("room/a").unwrap(); + let _first_track = first.create_track("video", timed()).unwrap(); + first + .announce(origin::Route::default().with_epoch(old.clone())) + .unwrap(); + + let consumer = chain.subscriber.consume(); + moq_clock::timeout(TIMEOUT, consumer.routed("room/a")) + .await + .expect("announce hung"); + let mut cursor = consumer.broadcasts("room").unwrap(); + let listed = next_broadcast(&mut cursor).await; + assert_eq!(listed.info().epoch, Some(old), "relay={relay}"); + + let second = chain.publisher.create_broadcast("room/a").unwrap(); + let _second_track = second.create_track("video", timed()).unwrap(); + second + .announce(origin::Route::default().with_epoch(new.clone())) + .unwrap(); + let listed = next_broadcast(&mut cursor).await; + assert_eq!(listed.info().path.as_str(), "room/a"); + assert_eq!(listed.info().epoch, Some(new), "relay={relay}"); + assert_eq!(next_track(&mut listed.tracks()).await.name(), "video"); +} + +#[moq_clock::test] +async fn lists_a_new_epoch_at_the_same_path() { + new_epoch(false).await; +} + +#[moq_clock::test] +async fn lists_a_new_epoch_at_the_same_path_through_a_relay() { + new_epoch(true).await; +} + +/// A track served only on request is never created by listing. +async fn dynamic_only(relay: bool) { + let chain = Chain::new(relay).await; + let broadcast = chain.publisher.create_broadcast("room/a").unwrap(); + let mut dynamic = broadcast.dynamic(); + let track = broadcast.create_track("static", timed()).unwrap(); + write(&track, 0); + broadcast.announce(Default::default()).unwrap(); + + let consumer = chain.subscriber.consume(); + moq_clock::timeout(TIMEOUT, consumer.routed("room/a")) + .await + .expect("announce hung"); + let mut cursor = consumer.broadcasts("room").unwrap(); + let listed = next_broadcast(&mut cursor).await; + let mut tracks = listed.tracks(); + let listed_track = next_track(&mut tracks).await; + assert_eq!(listed_track.name(), "static"); + assert_eq!(read_group(&listed_track).await, 0); + + assert!( + moq_clock::timeout(Duration::from_secs(1), tracks.next()).await.is_err(), + "relay={relay}: nothing else is listed" + ); + assert!( + moq_clock::timeout(Duration::from_secs(1), dynamic.requested_track()) + .await + .is_err(), + "relay={relay}: listing asked the handler for a track" + ); +} + +#[moq_clock::test] +async fn never_creates_a_dynamic_track() { + dynamic_only(false).await; +} + +#[moq_clock::test] +async fn never_creates_a_dynamic_track_through_a_relay() { + dynamic_only(true).await; +} + +/// Two downstream listings of one prefix share the relay's one upstream SUBSCRIBE_ALL. +#[moq_clock::test] +async fn downstream_requests_share_one_upstream() { + let chain = Chain::new(true).await; + let (_broadcast, _tracks) = publish(&chain.publisher, "room/a", &["video"]); + + let other = produce_origin(4); + let _other_link = link(&chain._relays[0], &other).await; + + let mut readers = Vec::new(); + for origin in [&chain.subscriber, &other] { + let consumer = origin.consume(); + moq_clock::timeout(TIMEOUT, consumer.routed("room/a")) + .await + .expect("announce hung"); + let mut cursor = consumer.broadcasts("room").unwrap(); + let listed = next_broadcast(&mut cursor).await; + let track = next_track(&mut listed.tracks()).await; + assert_eq!(read_group(&track).await, 0); + readers.push(cursor); + } + + let upstream = chain.links[0] + .client_transport + .bidi_types() + .into_iter() + .filter(|kind| *kind == SUBSCRIBE_ALL) + .count(); + assert_eq!(upstream, 1, "one upstream SUBSCRIBE_ALL for both downstreams"); +} + +/// An older peer has no SUBSCRIBE_ALL, and nothing emulates it: the listing stays empty +/// while ordinary requests still work. +#[moq_clock::test] +async fn older_versions_list_nothing() { + let publisher = produce_origin(1); + let subscriber = produce_origin(3); + let (_broadcast, _tracks) = publish(&publisher, "room/a", &["video"]); + let mut options = MockConnectOptions::new("moq-lite-06".parse().unwrap()); + options.server_publish = Some(publisher.consume()); + options.client_subscribe = Some(subscriber.clone()); + let pair = connect_mock(options).await; + + let consumer = subscriber.consume(); + moq_clock::timeout(TIMEOUT, consumer.routed("room/a")) + .await + .expect("announce hung"); + let mut cursor = consumer.broadcasts("room").unwrap(); + assert!(moq_clock::timeout(Duration::from_secs(1), cursor.next()).await.is_err()); + assert!(!pair.client_transport.bidi_types().contains(&SUBSCRIBE_ALL)); +} From 937165632e01986970b3bb6d891789152ff66fac Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 18 Oct 2026 23:02:23 -0700 Subject: [PATCH 05/13] wip(moq-lite): specify SUBSCRIBE_ALL in lite-07 Co-Authored-By: Claude Opus 5.5 --- drafts/draft-lcurley-moq-lite.md | 173 ++++++++++++++++++++++++++++++- rs/moq-net/src/lite/publisher.rs | 8 +- 2 files changed, 177 insertions(+), 4 deletions(-) diff --git a/drafts/draft-lcurley-moq-lite.md b/drafts/draft-lcurley-moq-lite.md index 4320d28fdd8..908a9b6265f 100644 --- a/drafts/draft-lcurley-moq-lite.md +++ b/drafts/draft-lcurley-moq-lite.md @@ -396,6 +396,8 @@ There's a 1-byte STREAM_TYPE at the beginning of each stream. | ------- | ------------- | ----------- | | 0x7 | Auth | Either | | ------- | ------------- | ----------- | +| 0x8 | Subscribe All | Subscriber | +| ------- | ------------- | ----------- | ### Announce A subscriber can open an Announce Stream to discover routes matching a prefix. @@ -519,6 +521,27 @@ A publisher closing its session gracefully MUST wait for the subscriber's FIN or A subscriber MAY cancel by closing its side (FIN); the publisher then closes its side (FIN) without owing SUBSCRIBE_END. Unbounded subscriptions stay open until SUBSCRIBE_END, and either endpoint MAY reset the stream at any time. +### Subscribe All {#subscribe-all-stream} +A subscriber opens a Subscribe All Stream (0x8) to subscribe to every Track of every Broadcast under a prefix, including Tracks and Broadcasts that appear later. +A subscriber that wants every Track, such as an archive, needs no catalog and opens no stream per Track. + +The subscriber sends a SUBSCRIBE_ALL message followed by any number of SUBSCRIBE_ALL_UPDATE messages, each of which applies to every Track of the request. +The publisher sends a BROADCAST_START for each Broadcast it holds under the prefix, as it learns of it, and a BROADCAST_START for a new publisher instance at the same path even while the old one is listed. +A Broadcast whose path has a `.`-prefixed segment below the prefix is not listed (see [Hidden Paths](#hidden)). +The publisher then sends a TRACK_START for each Track the Broadcast's producer creates, and serves it as if the subscriber had sent a SUBSCRIBE for it with the request's preferences and no bounds: TRACK_OK and TRACK_END stand in for the Track's SUBSCRIBE_OK and SUBSCRIBE_END, its groups arrive on Track Group Streams (0x2), and a TRACK_RESET stands in for the reset of its Subscribe Stream. +A Track served only on request, which a producer never creates, is never listed. +A publisher MUST NOT deliver a Track of a SUBSCRIBE_ALL as a datagram. +Once a Broadcast ends, the publisher sends a BROADCAST_END and lists no more of its Tracks; each listed Track still ends with its own TRACK_END or TRACK_RESET. + +A Track Group Stream can arrive before the TRACK_START naming its Track, since the two travel on different streams. +The subscriber MUST NOT reject such a stream while the request is open; it waits for the TRACK_START. + +The request needs subscribe permission on the prefix, and the publisher MUST NOT list a Broadcast the session does not allow. +It counts as one subscription against the publisher's bound (see [Resource Exhaustion](#resource-exhaustion)). +A relay MAY answer every SUBSCRIBE_ALL for a prefix with one request upstream. +The subscriber cancels by closing its side (FIN), and the publisher then closes its own without owing anything further. +The publisher closes its side once it will list no more Broadcasts and every listed Track has ended. + ### Fetch A subscriber opens a Fetch Stream (0x3) to request a single Group from a Track. @@ -698,6 +721,8 @@ Unidirectional streams are used for data transmission. | ------ | -------- | ----------- | | 0x1 | Setup | Either | | ------ | -------- | ----------- | +| 0x2 | Track Group | Publisher | +| ------ | -------- | ----------- | ### Setup {#setup-stream} Each endpoint MUST open a Setup Stream (0x1) at the start of the session to advertise the optional capabilities and extensions it supports. @@ -709,7 +734,8 @@ An endpoint with no optional capabilities sends a SETUP with an empty parameter See the [Session](#session) section for how an endpoint avoids waiting on the peer's SETUP before exchanging other streams. ### Group -A publisher creates Group Streams in response to a Subscribe Stream. +A publisher creates Group Streams in response to a Subscribe Stream, and Track Group Streams (0x2) for a Track of a [Subscribe All Stream](#subscribe-all-stream). +A Track Group Stream starts with a TRACK_GROUP message instead of a GROUP message and is otherwise the same. A Group Stream MUST start with a GROUP message and MAY be followed by any number of FRAME messages. A Group MAY contain zero FRAME messages, potentially indicating a gap in the track. @@ -1341,6 +1367,129 @@ The publisher MUST NOT send SUBSCRIBE_END until every Group Stream it will open The subscriber has received every Group Stream once it has read the header of `Stream Count` of them, which MAY happen after SUBSCRIBE_END or the FIN since streams are not ordered. A Group Stream reset before its header arrived is never seen, so a subscriber SHOULD bound how long it waits for the rest, for example by `Subscriber Max Age`. +## SUBSCRIBE_ALL {#subscribe-all} +SUBSCRIBE_ALL is the first message on a Subscribe All Stream. + +~~~ +SUBSCRIBE_ALL Message { + Message Length (i) + Subscribe ID (i) + Broadcast Path Prefix (s) + Subscriber Priority (8) + Group Order (8) + Subscriber Max Age (i) +} +~~~ + +**Subscribe ID**: +Drawn from the same space as the SUBSCRIBE `Subscribe ID`, and named by every TRACK_GROUP of the request. + +**Broadcast Path Prefix**: +The prefix whose Broadcasts are listed. + +See [SUBSCRIBE](#subscribe) for the other fields, which apply to every listed Track. + +## SUBSCRIBE_ALL_UPDATE +SUBSCRIBE_ALL_UPDATE changes the preferences of every Track of a SUBSCRIBE_ALL. + +~~~ +SUBSCRIBE_ALL_UPDATE Message { + Message Length (i) + Subscriber Priority (8) + Group Order (8) + Subscriber Max Age (i) +} +~~~ + +## BROADCAST_START {#broadcast-start} +BROADCAST_START lists a Broadcast on a Subscribe All Stream. +Each one implicitly assigns the next Broadcast ID, starting at 0. + +~~~ +BROADCAST_START Message { + Type (i) = 0x0 + Message Length (i) + Broadcast Path Suffix (s) + Epoch (b) +} +~~~ + +**Broadcast Path Suffix**: +The Broadcast's path, relative to the requested prefix. + +**Epoch**: +The publisher instance, encoded as in [ANNOUNCE_START](#announce-start). + +## BROADCAST_END +BROADCAST_END says a listed Broadcast has ended: no further Track of it is listed. + +~~~ +BROADCAST_END Message { + Type (i) = 0x1 + Message Length (i) + Broadcast ID (i) +} +~~~ + +## TRACK_START {#track-start} +TRACK_START lists a Track of a listed Broadcast, with its [TRACK_INFO](#track-info) fields. +Each one implicitly assigns the next Track ID, starting at 0. +A Broadcast ID that is unknown or has ended is a PROTOCOL_VIOLATION. + +~~~ +TRACK_START Message { + Type (i) = 0x2 + Message Length (i) + Broadcast ID (i) + Track Name (s) + Publisher Priority (8) + Publisher Max Age (i) + Timescale (i) +} +~~~ + +## TRACK_OK +TRACK_OK is a listed Track's [SUBSCRIBE_OK](#subscribe-ok). + +~~~ +TRACK_OK Message { + Type (i) = 0x3 + Message Length (i) + Track ID (i) + Group (i) + Largest Group (i) + [Largest Frame (i)] +} +~~~ + +## TRACK_END +TRACK_END is a listed Track's [SUBSCRIBE_END](#subscribe-end), counting its Track Group Streams. + +~~~ +TRACK_END Message { + Type (i) = 0x4 + Message Length (i) + Track ID (i) + Group (i) + Stream Count (i) +} +~~~ + +## TRACK_RESET +TRACK_RESET ends a listed Track with an error, as a reset of its Subscribe Stream would. + +~~~ +TRACK_RESET Message { + Type (i) = 0x5 + Message Length (i) + Track ID (i) + Error Code (i) +} +~~~ + +**Error Code**: +A stream error code (see [Stream Error Codes](#stream-error-codes)). + ## FETCH FETCH is sent by a subscriber to request a single group from a track. @@ -1554,6 +1703,27 @@ A non-zero value means the leading frames are not on this stream, either because The subscriber MUST NOT assume it will receive the missing frames on another stream; they are a gap unless a separate subscription or FETCH covers them. +## TRACK_GROUP +The TRACK_GROUP message starts a Track Group Stream: a GROUP of a Track listed by a SUBSCRIBE_ALL. + +~~~ +TRACK_GROUP Message { + Message Length (i) + Subscribe ID (i) + Track ID (i) + Group Sequence (i) + Frame Start (i) +} +~~~ + +**Subscribe ID**: +The SUBSCRIBE_ALL's Subscribe ID. + +**Track ID**: +The Track, as assigned by its TRACK_START. + +See [GROUP](#group) for the other fields. + ## FRAME The FRAME message is a payload within a group. @@ -1632,6 +1802,7 @@ The `Message Length` describes the payload size on the wire. - Any Setup Stream or bidirectional stream message that fails to decode closes the session with PROTOCOL_VIOLATION; previously only an unexpected Message Length did, as a SHOULD. A stream ending mid-message outside the Setup and Auth Streams, a malformed GROUP or FRAME, and a stream type the negotiated version predates still affect only that stream. - A Report-level publisher reads and ignores PROBE targets instead of resetting the Probe Stream. - Added NOT_SUPPORTED (0x30) to session errors; unsupported Auth Stream tokens and grants use AUTH_ERROR before or after a grant. +- Added the Subscribe All Stream (0x8) and the Track Group Stream (0x2): SUBSCRIBE_ALL subscribes to every Track of every Broadcast under a prefix, listed with BROADCAST_START, BROADCAST_END, and TRACK_START, and served with TRACK_OK, TRACK_END, and TRACK_RESET. Earlier versions have neither. - Added the Auth Stream (0x7) with AUTH, AUTH_OK, and AUTH_ERROR: either endpoint presents a token on its own stream and learns the [path patterns](#path-pattern) it may publish and subscribe to. The union of a session's open grants is its scope. A shrink withdraws what it no longer covers, and an announcement outside the scope closes the session with UNAUTHORIZED. A peer without the stream resets it. ## moq-lite-06 diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 4c432391a23..37316c717c8 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -6015,10 +6015,12 @@ mod tests { async fn unknown_control_types_reset_only_their_stream() { use kio::Task; // An unassigned type everywhere, then types a version predates: PROBE before lite-03, - // GOAWAY before lite-04, FETCH and TRACK before lite-05, AUTH before lite-07. + // GOAWAY before lite-04, FETCH and TRACK before lite-05, AUTH and SUBSCRIBE_ALL + // before lite-07. let cases = [ - (Version::Lite06, 8), - (Version::Lite07, 8), + (Version::Lite06, 9), + (Version::Lite07, 9), + (Version::Lite06, lite::ControlType::SubscribeAll as u8), (Version::Lite02, lite::ControlType::Probe as u8), (Version::Lite03, lite::ControlType::Goaway as u8), (Version::Lite04, lite::ControlType::Fetch as u8), From 75c42cbfc92cdfc001ff263240b771b2d6b1dee8 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 18 Oct 2026 23:34:36 -0700 Subject: [PATCH 06/13] wip(moq-net): one lister per session, and a SUBSCRIBE_ALL benchmark Every route a session announced was its own listing handler, so a relay's cursor read B copies of each broadcast and a new broadcast cost O(B) at every hop. Routes now share their session's lister. Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/Cargo.toml | 5 + rs/moq-net/benches/subscribe-all.rs | 278 ++++++++++++++++++++++++++++ rs/moq-net/src/lite/all_recv.rs | 6 +- rs/moq-net/src/lite/subscriber.rs | 28 ++- rs/moq-net/src/model/listing.rs | 226 ++++++++++++++-------- rs/moq-net/src/model/mod.rs | 4 +- rs/moq-net/src/model/origin.rs | 23 ++- 7 files changed, 470 insertions(+), 100 deletions(-) create mode 100644 rs/moq-net/benches/subscribe-all.rs diff --git a/rs/moq-net/Cargo.toml b/rs/moq-net/Cargo.toml index cb2a5a3cf0e..db67aadf59f 100644 --- a/rs/moq-net/Cargo.toml +++ b/rs/moq-net/Cargo.toml @@ -187,3 +187,8 @@ required-features = ["async"] name = "memory" harness = false required-features = ["async"] + +[[bench]] +name = "subscribe-all" +harness = false +required-features = ["async"] diff --git a/rs/moq-net/benches/subscribe-all.rs b/rs/moq-net/benches/subscribe-all.rs new file mode 100644 index 00000000000..c57a3866736 --- /dev/null +++ b/rs/moq-net/benches/subscribe-all.rs @@ -0,0 +1,278 @@ +//! SUBSCRIBE_ALL through a relay: listers each open one `broadcasts(prefix)` that covers +//! every track of a publisher, over the in-memory mock transport. +//! +//! `subscribe_all_round` times one round: every track writes a group and every lister +//! reads it. `subscribe_round` is its control, each track requested with its own SUBSCRIBE. +//! `subscribe_all_new_broadcast` times one broadcast's life: created with one track, +//! listed and read by every lister, then dropped; `subscribe_new_broadcast` is its control. Each sweeps listers against the +//! broadcasts already listed, so a cost that grows with the table instead of the touched +//! track shows as a slope. +//! +//! Everything runs on one current-thread runtime, so the only work measured is the +//! protocol and model code, never scheduling across threads. +//! +//! Run with `cargo bench -p moq-net --bench subscribe-all`. + +// Custom timings measure the real time the work took, not the ambient clock. +#![allow(clippy::disallowed_methods)] + +#[path = "../tests/integration/support/mod.rs"] +mod support; + +use std::time::{Duration, Instant}; + +use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main}; +use moq_net::{Hop, Timestamp, Version, broadcast, origin, track}; +use support::harness::{MockConnectOptions, MockPair, connect_mock}; + +const VERSION: &str = "moq-lite-07-wip"; + +/// Tracks per broadcast, as a small rendition ladder plus audio. +const TRACKS: usize = 4; + +/// Listers sharing the relay. +const LISTERS: [usize; 3] = [1, 8, 32]; + +/// Broadcasts already published under the prefix. +const BROADCASTS: [usize; 3] = [1, 16, 64]; + +fn runtime() -> tokio::runtime::Runtime { + tokio::runtime::Builder::new_current_thread() + .enable_time() + .build() + .unwrap() +} + +fn timed() -> track::Info { + track::Info::default().with_timescale(moq_net::Timescale::MILLI) +} + +fn produce(hop: u64) -> origin::Producer { + let (producer, driver) = origin::Producer::new(origin::Config::new(Hop::new(hop).unwrap())); + support::harness::spawn(driver); + producer +} + +/// Have `to` pull everything `from` publishes. +async fn link(from: &origin::Producer, to: &origin::Producer) -> MockPair { + let mut options = MockConnectOptions::new(VERSION.parse::().unwrap()); + options.server_publish = Some(from.consume()); + options.client_subscribe = Some(to.clone()); + connect_mock(options).await +} + +fn write(track: &track::Producer, sequence: u64) { + let mut group = track.create_group(moq_net::group::Info { sequence }).unwrap(); + group + .write_frame(Timestamp::from_millis(sequence).unwrap(), &[0u8; 64][..]) + .unwrap(); + group.finish().unwrap(); +} + +/// Read the next group whole, returning its sequence. +async fn read(subscriber: &mut track::Subscriber) -> u64 { + let mut group = subscriber.recv_group().await.unwrap().expect("track ended"); + while group.read_frame().await.unwrap().is_some() {} + group.sequence +} + +fn subscription() -> track::Subscription { + track::Subscription::default().with_max_delay(Duration::from_secs(60)) +} + +/// A lister's open request, with every listed track subscribed. +struct Lister { + /// None for the control, which requests each track by name. + cursor: Option, + subscribers: Vec, + origin: origin::Producer, + _pair: MockPair, +} + +/// A publisher with `broadcasts` broadcasts of [`TRACKS`] tracks, a relay, and `listers` +/// listers that have read group 0 of every track. +struct Room { + publisher: origin::Producer, + tracks: Vec, + listers: Vec, + _broadcasts: Vec, + _relay: origin::Producer, + _pair: MockPair, + sequence: u64, +} + +impl Room { + /// `listed` subscribes through SUBSCRIBE_ALL, otherwise through a SUBSCRIBE per track. + async fn new(broadcasts: usize, listers: usize, listed: bool) -> Self { + let publisher = produce(1); + let relay = produce(2); + let pair = link(&publisher, &relay).await; + + let mut tracks = Vec::new(); + let mut held = Vec::new(); + for index in 0..broadcasts { + let broadcast = publisher.create_broadcast(format!("room/{index}")).unwrap(); + for name in 0..TRACKS { + let track = broadcast.create_track(name.to_string(), timed()).unwrap(); + write(&track, 0); + tracks.push(track); + } + broadcast.announce(Default::default()).unwrap(); + held.push(broadcast); + } + + let mut room = Self { + publisher, + tracks, + listers: Vec::new(), + _broadcasts: held, + _relay: relay.clone(), + _pair: pair, + sequence: 0, + }; + for lister in 0..listers { + let origin = produce(3 + lister as u64); + let pair = link(&relay, &origin).await; + let consumer = origin.consume(); + consumer.routed(format!("room/{}", broadcasts - 1)).await.expect("announced"); + let mut tracks = Vec::new(); + let cursor = match listed { + true => { + let mut cursor = consumer.broadcasts("room").unwrap(); + let mut listed = Vec::new(); + for _ in 0..broadcasts { + listed.push(cursor.next().await.expect("listing ended").tracks()); + } + for listed in &mut listed { + for _ in 0..TRACKS { + tracks.push(listed.next().await.expect("tracks ended")); + } + } + Some(cursor) + } + false => { + for index in 0..broadcasts { + let broadcast = consumer.routed_broadcast(format!("room/{index}")).await.unwrap(); + for name in 0..TRACKS { + tracks.push(broadcast.track(&name.to_string()).unwrap()); + } + } + None + } + }; + let mut subscribers = Vec::new(); + for track in tracks { + let mut subscriber = track.subscribe(subscription()).await.unwrap(); + assert_eq!(read(&mut subscriber).await, 0); + subscribers.push(subscriber); + } + room.listers.push(Lister { + cursor, + subscribers, + origin, + _pair: pair, + }); + } + room + } + + /// Every track writes a group and every lister reads it. + async fn round(&mut self) { + self.sequence += 1; + for track in &self.tracks { + write(track, self.sequence); + } + for lister in &mut self.listers { + for subscriber in &mut lister.subscribers { + assert_eq!(read(subscriber).await, self.sequence); + } + } + } + + /// A new broadcast with one track, listed and read by every lister, then dropped. + async fn new_broadcast(&mut self, index: usize) { + let broadcast = self.publisher.create_broadcast(format!("room/new{index}")).unwrap(); + let track = broadcast.create_track("0", timed()).unwrap(); + write(&track, 0); + broadcast.announce(Default::default()).unwrap(); + for lister in &mut self.listers { + let consumer = match lister.cursor.as_mut() { + Some(cursor) => { + let listed = cursor.next().await.expect("listing ended"); + listed.tracks().next().await.expect("tracks ended") + } + None => { + let path = format!("room/new{index}"); + let listed = lister.origin.consume().routed_broadcast(path).await.unwrap(); + listed.track("0").unwrap() + } + }; + let mut subscriber = consumer.subscribe(subscription()).await.unwrap(); + assert_eq!(read(&mut subscriber).await, 0); + } + } +} + +fn round(c: &mut Criterion) { + round_with(c, "subscribe_all_round", true); + round_with(c, "subscribe_round", false); +} + +fn round_with(c: &mut Criterion, name: &str, listed: bool) { + let rt = runtime(); + let mut group = c.benchmark_group(name); + for listers in LISTERS { + for broadcasts in BROADCASTS { + let mut room = rt.block_on(Room::new(broadcasts, listers, listed)); + group.throughput(Throughput::Elements((listers * broadcasts * TRACKS) as u64)); + group.bench_function( + BenchmarkId::new(format!("listers={listers}"), format!("broadcasts={broadcasts}")), + |b| { + b.iter_custom(|iterations| { + let start = Instant::now(); + for _ in 0..iterations { + rt.block_on(room.round()); + } + start.elapsed() + }) + }, + ); + } + } + group.finish(); +} + +fn new_broadcast(c: &mut Criterion) { + new_broadcast_with(c, "subscribe_all_new_broadcast", true); + new_broadcast_with(c, "subscribe_new_broadcast", false); +} + +fn new_broadcast_with(c: &mut Criterion, name: &str, listed: bool) { + let rt = runtime(); + let mut group = c.benchmark_group(name); + for listers in LISTERS { + for broadcasts in BROADCASTS { + let mut room = rt.block_on(Room::new(broadcasts, listers, listed)); + let mut next = 0; + group.throughput(Throughput::Elements(listers as u64)); + group.bench_function( + BenchmarkId::new(format!("listers={listers}"), format!("broadcasts={broadcasts}")), + |b| { + b.iter_custom(|iterations| { + let start = Instant::now(); + for _ in 0..iterations { + rt.block_on(room.new_broadcast(next)); + next += 1; + } + start.elapsed() + }) + }, + ); + } + } + group.finish(); +} + + +criterion_group!(benches, round, new_broadcast); +criterion_main!(benches); diff --git a/rs/moq-net/src/lite/all_recv.rs b/rs/moq-net/src/lite/all_recv.rs index 9c09ca23a9d..6a9388a5172 100644 --- a/rs/moq-net/src/lite/all_recv.rs +++ b/rs/moq-net/src/lite/all_recv.rs @@ -1,8 +1,8 @@ //! The SUBSCRIBE_ALL a session opens upstream for one prefix, feeding every listing of it. //! -//! Each route the peer announces is its own handler, so a reader listing a prefix asks -//! every one of them. The session answers all of those with a single request per prefix: -//! the peer lists everything it holds there, whichever of its routes serves it. +//! Every route the peer announces shares the session's lister, so readers ask the session +//! once per prefix, and the session answers with a single request: the peer lists +//! everything it holds there, whichever of its routes serves it. use super::*; diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 8f33b623839..3613d0b0628 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -86,9 +86,9 @@ pub(super) struct Subscriber { /// Sources minted by the announce half, drained by the driver into /// [`SourceServe`] machines. sources: kio::Queue, - /// Listings the announce half's routes were asked for, drained by the driver into - /// one [`ListServe`] per prefix. - listings: kio::Queue, + /// Where readers ask every route this session announces for a listing, taken by the + /// driver into one [`ListServe`] per prefix. + lister: crate::model::Lister, /// How many TRACK_STARTs each open SUBSCRIBE_ALL has read, for a group stream that /// arrives ahead of its track's. started: Lock>>, @@ -147,7 +147,7 @@ impl Subscriber { peer_setup: config.peer_setup, cost: config.cost, sources: kio::Queue::new(), - listings: kio::Queue::new(), + lister: Default::default(), started: Default::default(), going_away: config.going_away, auth: config.auth, @@ -691,8 +691,11 @@ impl SubscriberDriver { } let _ = self.sources.poll(waiter); - while let Poll::Ready(Ok(listing)) = self.subscriber.listings.poll_pop(waiter) { - self.list(listing); + // An older peer has no SUBSCRIBE_ALL, and nothing emulates it: the listing ends empty. + while let Poll::Ready(listing) = self.subscriber.lister.poll_requested(waiter) { + if self.subscriber.version.has_subscribe_all() { + self.list(listing); + } } let _ = self.list_serves.poll(waiter); @@ -4877,7 +4880,10 @@ impl Announced { return false; } // An error means the prefix is outside our origin's scope, so don't serve it. - match subscriber.origin.dynamic(&path, route.clone()) { + match subscriber + .origin + .dynamic_listed(&path, route.clone(), subscriber.lister.clone()) + { Ok(dynamic) => { self.attach(path, route, dynamic); true @@ -4991,14 +4997,6 @@ impl Announced { route: entry.live.consume(), }); } - // A reader listing a prefix this route covers. The session answers every route's - // listing of a prefix with one SUBSCRIBE_ALL. An older peer has none, and nothing - // emulates it: the listing stays empty. - while let Poll::Ready(Ok(listing)) = entry.dynamic.poll_requested_listing(route_waiter) { - if subscriber.version.has_subscribe_all() { - let _ = subscriber.listings.try_push(listing); - } - } } } diff --git a/rs/moq-net/src/model/listing.rs b/rs/moq-net/src/model/listing.rs index 92ce174a43a..168486da3e5 100644 --- a/rs/moq-net/src/model/listing.rs +++ b/rs/moq-net/src/model/listing.rs @@ -2,17 +2,18 @@ //! //! A route claims capability, not inventory, so the route table alone cannot say which //! broadcasts exist under a prefix. A broadcast published on this origin is its own -//! answer. A route a handler serves ([`Dynamic`]) is asked for a [`Listing`] of the -//! prefix, which the handler fills as it learns of broadcasts: a session forwards the -//! request to its peer. A handler that never answers lists nothing, which is how a -//! service serving any path on request stays out of the listing. +//! answer. A route a handler serves ([`Dynamic`](super::Dynamic)) is asked, through the +//! handler's [`Lister`], for a [`Listing`] of the prefix, which the handler fills as it +//! learns of broadcasts: a session forwards the request to its peer. A handler that never +//! answers lists nothing, which is how a service serving any path on request stays out of +//! the listing. use std::{ collections::{BTreeMap, HashMap, VecDeque}, task::{Poll, ready}, }; -use super::{AnnounceConsumer, AnnounceEvent, Consumer, Dynamic, ServeState}; +use super::{AnnounceConsumer, AnnounceEvent, Consumer}; use crate::{AsPath, Error, PathOwned, broadcast}; /// The broadcasts a handler lists under one prefix, in the order it learned them. @@ -87,9 +88,47 @@ impl ListingReader { } } -/// The listings a route's handler has yet to take, and the ones its readers share. +/// Where a handler takes the listings readers ask of it. +/// +/// One per handler, shared by every route it serves: a reader asks it once per prefix +/// however many of its routes cover the prefix, and the handler fills one listing. +#[derive(Clone, Default)] +pub(crate) struct Lister { + state: kio::Shared, +} + +impl Lister { + /// Poll for the next prefix a reader wants the broadcasts of. + /// + /// Fill the [`Listing`] with every broadcast the publisher behind the handler holds + /// under its prefix, and keep it current. A handler that serves paths only on request + /// never polls this, so nothing it would serve that way is ever listed. + #[cfg_attr(not(feature = "async"), allow(dead_code))] + pub fn poll_requested(&self, waiter: &kio::Waiter) -> Poll { + let mut state = ready!(self.state.poll(waiter, |state| match state.queued.is_empty() { + true => Poll::Pending, + false => Poll::Ready(()), + })); + Poll::Ready(state.queued.pop_front().expect("predicate guaranteed a listing")) + } + + fn read(&self, prefix: &PathOwned) -> ListingReader { + self.state.lock().read(prefix) + } + + fn same_channel(&self, other: &Self) -> bool { + self.state.same_channel(&other.state) + } + + /// Drop every listing the handler has yet to take: the origin is gone. + pub(super) fn close(&self) { + self.state.lock().clear(); + } +} + +/// The listings a handler has yet to take, and the ones its readers share. #[derive(Default)] -pub(super) struct Listings { +struct Listings { queued: VecDeque, // The readers' side of each prefix's listing, so a second reader joins the first. live: HashMap>, @@ -115,42 +154,18 @@ impl Listings { ListingReader { state: reader, next: 0 } } - /// Drop every listing the handler has yet to take: the route is gone. - pub(super) fn close(&mut self) { + fn clear(&mut self) { self.queued.clear(); self.live.clear(); } } -impl Dynamic { - /// Poll for the next prefix a reader wants the broadcasts of, under this route. - /// - /// Fill the [`Listing`] with every broadcast the publisher behind the route holds - /// under its prefix, and keep it current. A handler that serves paths only on request - /// never polls this, so nothing it would serve that way is ever listed. - #[cfg_attr(not(feature = "async"), allow(dead_code))] - // pending: /quest/m1/public-model/answer.md - pub(crate) fn poll_requested_listing(&self, waiter: &kio::Waiter) -> Poll> { - let mut state = ready!(self.state.poll(waiter, |state| { - match state.closed || !state.listings.queued.is_empty() { - true => Poll::Ready(()), - false => Poll::Pending, - } - })); - if state.closed { - return Poll::Ready(Err(Error::Closed)); - } - let listing = state.listings.queued.pop_front().expect("predicate guaranteed a listing"); - Poll::Ready(Ok(listing)) - } -} - /// What serves an announced prefix, as far as listing goes. enum Behind { /// A broadcast published on this origin at exactly the prefix. Local, /// A handler, asked for a listing. - Server(kio::Shared), + Server(Lister), /// Nothing that lists: an advertisement without a server. Nothing, } @@ -165,13 +180,16 @@ enum Behind { pub struct Broadcasts { consumer: Consumer, announced: AnnounceConsumer, - // What serves each announced prefix (absolute), to drop its listing once none does. - routes: HashMap>>, - // One reader per handler, shared by every route that handler serves. - listings: Vec<(kio::Shared, ListingReader)>, - // Ready to yield, and what was yielded, to skip a broadcast reached twice. + // The handler serving each announced prefix (absolute), to drop its listing once none does. + routes: HashMap, + // One reader per handler, with how many announced prefixes it serves. Few: a session + // is one handler however many routes it announces. + listings: Vec<(Lister, ListingReader, usize)>, + // Ready to yield, and what was yielded by path, to skip a broadcast reached twice. ready: VecDeque<(broadcast::Consumer, Option)>, - yielded: Vec, + yielded: HashMap>, + // The yielded paths after the last sweep of ended broadcasts, to sweep once it doubles. + swept: usize, // The prefix as the origin names it, for listing requests, and as the consumer does. prefix: PathOwned, relative: PathOwned, @@ -201,7 +219,8 @@ impl Consumer { routes: HashMap::new(), listings: Vec::new(), ready: VecDeque::new(), - yielded: Vec::new(), + yielded: HashMap::new(), + swept: 0, prefix: absolute, relative: prefix.to_owned(), }) @@ -272,43 +291,56 @@ impl Broadcasts { unreachable!() }; let absolute = self.announced.absolute(&announce.prefix).to_owned(); - self.routes.remove(&absolute); - self.release(); + if let Some(lister) = self.routes.remove(&absolute) { + self.release(&lister); + } return; }; let absolute = self.announced.absolute(&announce.prefix).to_owned(); - match self.behind(&absolute) { + let lister = match self.behind(&absolute) { Behind::Local => { - self.routes.insert(absolute.clone(), None); if let Some(source) = self.local(&absolute) { self.ready.push_back((source, announce.route.epoch.clone())); } + None } - Behind::Server(server) => { - self.routes.insert(absolute, Some(server.clone())); - if !self.listings.iter().any(|(listed, _)| listed.same_channel(&server)) { - // A route above the prefix lists the prefix; one beneath it lists the - // whole prefix too, so a handler serving several routes beneath it - // answers one request. - let reader = server.lock().listings.read(&self.prefix); - self.listings.push((server, reader)); - } + Behind::Server(lister) => Some(lister), + Behind::Nothing => None, + }; + let previous = match lister { + Some(lister) => { + self.hold(&lister); + self.routes.insert(absolute, lister) } - Behind::Nothing => { - self.routes.remove(&absolute); + None => self.routes.remove(&absolute), + }; + if let Some(previous) = previous { + self.release(&previous); + } + } + + /// Count another announced prefix `lister`'s handler serves, reading its listing on the + /// first. A route above the prefix lists the prefix; one beneath it lists the whole + /// prefix too, so a handler serving several routes beneath it answers one request. + fn hold(&mut self, lister: &Lister) { + match self.listings.iter_mut().find(|(held, ..)| held.same_channel(lister)) { + Some((.., routes)) => *routes += 1, + None => { + let reader = lister.read(&self.prefix); + self.listings.push((lister.clone(), reader, 1)); } } - self.release(); } - /// Stop reading the listing of every handler no announced route is served by. - fn release(&mut self) { - let routes = &self.routes; - self.listings.retain(|(server, _)| { - routes - .values() - .any(|serving| serving.as_ref().is_some_and(|serving| serving.same_channel(server))) - }); + /// Count one fewer prefix `lister`'s handler serves, no longer reading its listing at none. + fn release(&mut self, lister: &Lister) { + let Some(index) = self.listings.iter().position(|(held, ..)| held.same_channel(lister)) else { + return; + }; + self.listings[index].2 -= 1; + if self.listings[index].2 == 0 { + self.listings.swap_remove(index); + } } /// What wins `path` for this cursor's reader. @@ -322,7 +354,7 @@ impl Broadcasts { return Behind::Local; } match &best.server { - Some(server) => Behind::Server(server.clone()), + Some(server) => Behind::Server(server.lock().lister.clone()), None => Behind::Nothing, } } @@ -337,25 +369,71 @@ impl Broadcasts { /// Stamp a broadcast as this cursor names it, unless it was already yielded or ended. fn hand_out(&mut self, broadcast: broadcast::Consumer, epoch: Option) -> Option { - self.yielded.retain(|yielded| !yielded.is_closed()); - if broadcast.is_closed() || self.yielded.iter().any(|yielded| yielded.is_clone(&broadcast)) { + if broadcast.is_closed() { return None; } // Named as the consumer names it: the requested prefix, then the path beneath it. let suffix = broadcast.info().path.strip_prefix(&self.prefix)?.to_owned(); let path = self.relative.join(&suffix).to_owned(); - // A replica of an instance already yielded, reached through another handler. - if epoch.is_some() - && self - .yielded - .iter() - .any(|yielded| yielded.info().epoch == epoch && yielded.info().path == path) + let yielded = self.yielded.entry(path.clone()).or_default(); + yielded.retain(|yielded| !yielded.is_closed()); + // The same broadcast again, or a replica of an instance already yielded, reached + // through another handler. + if yielded + .iter() + .any(|yielded| yielded.is_clone(&broadcast) || (epoch.is_some() && yielded.info().epoch == epoch)) { return None; } let scope = self.consumer.stats.egress(&self.consumer.root.join(&path).to_owned()); let broadcast = broadcast.with_path(path).with_epoch(epoch).with_stats(scope); - self.yielded.push(broadcast.clone()); + yielded.push(broadcast.clone()); + // Ended broadcasts at paths never reached again, swept once their count doubles. + if self.yielded.len() > 2 * self.swept.max(16) { + self.yielded.retain(|_, yielded| { + yielded.retain(|yielded| !yielded.is_closed()); + !yielded.is_empty() + }); + self.swept = self.yielded.len(); + } Some(broadcast) } } + +#[cfg(all(test, feature = "async"))] +mod tests { + use std::time::Duration; + + use super::*; + use crate::{Hop, origin::Route}; + use super::super::ProduceTest; + + /// Routes sharing a handler's lister ask it once per prefix, and losing one of them + /// keeps the listing for the rest. + #[moq_clock::test] + async fn routes_sharing_a_lister_are_asked_once() { + let origin = Hop::new(1).unwrap().produce(); + let lister = Lister::default(); + let a = origin.dynamic_listed("room/a", Route::default(), lister.clone()).unwrap(); + let _b = origin.dynamic_listed("room/b", Route::default(), lister.clone()).unwrap(); + let mut cursor = origin.consume().broadcasts("room").unwrap(); + let quiet = Duration::from_millis(10); + assert!(moq_clock::timeout(quiet, cursor.next()).await.is_err(), "nothing listed yet"); + + let Poll::Ready(listing) = lister.poll_requested(&kio::Waiter::noop()) else { + panic!("the cursor asked for a listing"); + }; + assert!(lister.poll_requested(&kio::Waiter::noop()).is_pending(), "asked once"); + assert_eq!(listing.prefix().as_str(), "room"); + + drop(a); + assert!(moq_clock::timeout(quiet, cursor.next()).await.is_err(), "nothing listed yet"); + let source = origin.create_source("room/b"); + listing.insert(source.consume(), None); + let listed = moq_clock::timeout(quiet, cursor.next()) + .await + .expect("still reading the listing") + .expect("origin open"); + assert_eq!(listed.info().path.as_str(), "room/b"); + } +} diff --git a/rs/moq-net/src/model/mod.rs b/rs/moq-net/src/model/mod.rs index b2381da5179..0947e6f8c28 100644 --- a/rs/moq-net/src/model/mod.rs +++ b/rs/moq-net/src/model/mod.rs @@ -80,9 +80,9 @@ pub(crate) use origin_impl::interest_prefixes; // pending: /quest/m1/public-model/answer.md pub(crate) use origin_impl::Snapshot; -// A route's answer to a reader listing the broadcasts under a prefix. +// A handler's answer to a reader listing the broadcasts under a prefix. #[cfg(feature = "async")] -pub(crate) use origin_impl::Listing; +pub(crate) use origin_impl::{Lister, Listing}; // The advertise-only route guard, for tests shaping the route table. #[cfg(all(test, feature = "async"))] diff --git a/rs/moq-net/src/model/origin.rs b/rs/moq-net/src/model/origin.rs index d42ed1b60c5..ebfde2b86ae 100644 --- a/rs/moq-net/src/model/origin.rs +++ b/rs/moq-net/src/model/origin.rs @@ -16,7 +16,7 @@ use rand::RngExt; mod listing; pub use listing::Broadcasts; #[cfg_attr(not(feature = "async"), allow(unused_imports))] -pub(crate) use listing::Listing; +pub(crate) use listing::{Lister, Listing}; use super::{ Requests, WeakCache, WeakEntry, @@ -1201,8 +1201,8 @@ struct ServeState { // since the release wakes the front while the updater still holds it. renewals: Arc, - // The prefixes readers want the broadcasts of; see [`Broadcasts`]. - listings: listing::Listings, + // Where the handler takes the prefixes readers want the broadcasts of; see [`Broadcasts`]. + lister: listing::Lister, // Set when the announcement is retracted or the origin tears down: new requests // fail immediately and the handler observes the end instead of parking forever. @@ -2081,9 +2081,19 @@ impl Producer { /// The prefix must overlap this producer's pattern scope. Individual requests /// remain authoritative and are refused when they do not match the scope. pub fn dynamic(&self, prefix: impl AsPath, route: Route) -> Result { + self.dynamic_listed(prefix, route, Lister::default()) + } + + /// [`Self::dynamic`], with readers listing the route asking `lister`, which the handler + /// shares between every route it serves. + pub(crate) fn dynamic_listed(&self, prefix: impl AsPath, route: Route, lister: Lister) -> Result { let announcing = Announcing::new(self, prefix)?; let serve = kio::Shared::::default(); - serve.lock().requests.add_handler(); + { + let mut state = serve.lock(); + state.requests.add_handler(); + state.lister = lister; + } let announcement = announcing.announce( route, Serving { @@ -2482,8 +2492,9 @@ impl AnnounceProducer { // already handed to the handler resolves through its own `Request`. if let Some(server) = &entry.server { let mut server = server.lock(); + // The lister stays open: the handler may share it with routes still live, and + // a reader stops reading it once no route it knows is served by it. server.closed = true; - server.listings.close(); for producer in server.requests.drain_all() { if let Ok(mut request) = producer.write() { request.resolved.get_or_insert(Err(Error::Unroutable)); @@ -2631,7 +2642,7 @@ impl DriverState { for server in servers { let mut server = server.lock(); server.closed = true; - server.listings.close(); + server.lister.close(); for producer in server.requests.drain_all() { if let Ok(mut request) = producer.write() { request.resolved.get_or_insert(Err(Error::Closed)); From c960069a79c4b59694ad3b1a3575d03f47a4472d Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 18 Oct 2026 23:35:26 -0700 Subject: [PATCH 07/13] quest(m1): split SUBSCRIBE_ALL's JS half into its own quest Co-Authored-By: Claude Opus 5.5 --- quest/m1/archive/README.md | 1 + quest/m1/archive/subscribe-all-js.md | 35 ++++++++++++++++++++++++++++ quest/m1/archive/subscribe-all.md | 8 +++---- quest/m1/lite07-finalize.md | 1 + 4 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 quest/m1/archive/subscribe-all-js.md diff --git a/quest/m1/archive/README.md b/quest/m1/archive/README.md index be321e5fc98..b16b2a0f28a 100644 --- a/quest/m1/archive/README.md +++ b/quest/m1/archive/README.md @@ -77,6 +77,7 @@ from a host. Each child fixes the stale docs it touches. ## Required - [SUBSCRIBE_ALL](/quest/m1/archive/subscribe-all.md) - one prefix-scoped moq-lite-07 request delivers every track of every broadcast under a path, including later ones, and relays fan it out from one upstream request +- [SUBSCRIBE_ALL in js/net](/quest/m1/archive/subscribe-all-js.md) - `@moq/net` lists and serves every track of every broadcast under a prefix over lite-07, mirroring the Rust names - [Archive producer](/quest/m1/archive/producer.md) - `archive::Producer` stores every matching broadcast as duration-cut per-track objects keyed by group and pts, recording gaps instead of stalling, with an optional DVR mode - [Archive consumer](/quest/m1/archive/consumer.md) - `archive::Consumer` serves a stored prefix as a broadcast from its key listing, and `moq export/import archive` use the new pair - [Catalog HLS field](/quest/m1/archive/catalog-hls.md) - the root `archive` section is gone; an opt-in per-rendition `hls` field marks each aligned HLS segment's first group and pts and declares its nominal duration diff --git a/quest/m1/archive/subscribe-all-js.md b/quest/m1/archive/subscribe-all-js.md new file mode 100644 index 00000000000..10d740c526e --- /dev/null +++ b/quest/m1/archive/subscribe-all-js.md @@ -0,0 +1,35 @@ +# [L] SUBSCRIBE_ALL in js/net + +## Goal + +`@moq/net` speaks moq-lite-07's SUBSCRIBE_ALL in both directions. A JS +consumer lists every broadcast under a prefix and every track each one's +producer creates, with the Rust names mirrored (`broadcasts(prefix)` on the +origin consumer, `tracks()` on the broadcast consumer). A JS publisher answers +a peer's SUBSCRIBE_ALL from its own broadcasts. Older versions refuse the +stream, as in Rust. + +## Plan + +See [SUBSCRIBE_ALL](/quest/m1/archive/subscribe-all.md) for the decisions and +the wire, which the Rust half fixed in `drafts/draft-lcurley-moq-lite.md`. + +- Codec: SUBSCRIBE_ALL, SUBSCRIBE_ALL_UPDATE, the six typed responses, and the + TRACK_GROUP header (uni 0x2), refused before lite-07. +- Subscriber: one request per prefix per session, shared by every listing of + it; a TRACK_GROUP stream ahead of its TRACK_START waits for it. +- Publisher: list each broadcast as it appears and each track as its producer + creates it, never one served only on request; no datagrams. +- Mirror whatever the Rust half settles for the open decisions it left + (start floor, sharing listed tracks with ordinary requests). + +Tests: Rust and JS list each other's broadcasts over lite-07, directly and +through moq-relay, including a track added later and a broadcast ending; a +lite-06 session lists nothing. + +Public API: `broadcasts(prefix)` and `tracks()` in `@moq/net`. Wire: none +beyond the Rust half. + +## Required + +- [SUBSCRIBE_ALL](/quest/m1/archive/subscribe-all.md) - fixes the lite-07 wire and the Rust model this mirrors diff --git a/quest/m1/archive/subscribe-all.md b/quest/m1/archive/subscribe-all.md index de814093b9e..73b50b2f33a 100644 --- a/quest/m1/archive/subscribe-all.md +++ b/quest/m1/archive/subscribe-all.md @@ -9,7 +9,8 @@ request and broadcasts that appear later, and delivers their groups; a broadcast's tracks end when it ends. It never creates a track the publisher serves only on request (an any-name `broadcast::Dynamic` handler such as a transcoder). A relay answers many downstream SUBSCRIBE_ALLs from one upstream -request. Rust and JS both speak it. +request. Rust speaks it here, and +[SUBSCRIBE_ALL in js/net](/quest/m1/archive/subscribe-all-js.md) mirrors it. ## Plan @@ -60,15 +61,14 @@ Guidance: delivered. - Fan-out: benchmark downstream requests against broadcast and track count per the AGENTS.md fan-out rule. -- Split the JS half into its own quest if the PR grows too large to review. +- The JS half is [SUBSCRIBE_ALL in js/net](/quest/m1/archive/subscribe-all-js.md). Test on mock time, directly and through a relay: tracks present at the request, a track added later (its first group arrives), a broadcast appearing and ending under the prefix, a new epoch at the same path, a dynamic-only track never created, and two downstream requests sharing one upstream request. -Public API: the new prefix hook in Rust and `@moq/net`, mirrored names. Wire: -a new lite-07-wip request. +Public API: the new prefix hook in Rust. Wire: a new lite-07-wip request. ## Related diff --git a/quest/m1/lite07-finalize.md b/quest/m1/lite07-finalize.md index cafd9f22258..a1fc4dade0b 100644 --- a/quest/m1/lite07-finalize.md +++ b/quest/m1/lite07-finalize.md @@ -89,6 +89,7 @@ is published; older versions are unchanged. - [Compressed tracks in Rust](/quest/m1/compress/core.md) - TRACK_INFO carries the compress flag and the frame header each compressed frame's inflated size - [Compressed tracks in js/net](/quest/m1/compress/js.md) - JS reads and writes compressed lite-07 tracks instead of refusing them - [SUBSCRIBE_ALL](/quest/m1/archive/subscribe-all.md) - lite-07 carries the prefix-scoped request for every track of every broadcast under a path +- [SUBSCRIBE_ALL in js/net](/quest/m1/archive/subscribe-all-js.md) - JS speaks SUBSCRIBE_ALL in both directions instead of refusing it ## Related From bdc5cce70193837cfac3d375106b5dc5334d8ec1 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 18 Oct 2026 23:38:24 -0700 Subject: [PATCH 08/13] wip(moq-net): document SUBSCRIBE_ALL and format Co-Authored-By: Claude Opus 5.5 --- doc/concept/moq-lite.md | 7 ++++ rs/moq-net/benches/subscribe-all.rs | 6 ++- rs/moq-net/src/lite/all_recv.rs | 33 ++++++++++----- rs/moq-net/src/lite/all_serve.rs | 40 ++++++++++--------- rs/moq-net/src/lite/publisher.rs | 31 ++++---------- rs/moq-net/src/lite/subscribe_all.rs | 20 ++++++++-- rs/moq-net/src/lite/subscriber.rs | 5 ++- rs/moq-net/src/model/broadcast.rs | 8 +--- rs/moq-net/src/model/listing.rs | 32 ++++++++++----- rs/moq-net/tests/integration/subscribe_all.rs | 6 ++- 10 files changed, 113 insertions(+), 75 deletions(-) diff --git a/doc/concept/moq-lite.md b/doc/concept/moq-lite.md index 4a207f14975..ed9f5a6c18c 100644 --- a/doc/concept/moq-lite.md +++ b/doc/concept/moq-lite.md @@ -209,6 +209,13 @@ JSON snapshot). Groups can also be fetched by sequence number, which is how the [HLS gateway](/bin/hls) and the relay's [HTTP fetch](/bin/relay/http) serve history. +On moq-lite 07 (`moq-lite-07-wip`, opt-in) a subscriber can instead ask for +every track of every broadcast under a path prefix in one request, including +tracks and broadcasts that appear later, so it records everything without +reading a catalog. Only tracks a publisher creates are listed, never ones it +serves on request, and a relay answers every such request for a prefix with +one request upstream. Rust only for now. + Each subscription carries the knobs that decide behavior under congestion: | Knob | Effect | diff --git a/rs/moq-net/benches/subscribe-all.rs b/rs/moq-net/benches/subscribe-all.rs index c57a3866736..9bc2be6f8be 100644 --- a/rs/moq-net/benches/subscribe-all.rs +++ b/rs/moq-net/benches/subscribe-all.rs @@ -134,7 +134,10 @@ impl Room { let origin = produce(3 + lister as u64); let pair = link(&relay, &origin).await; let consumer = origin.consume(); - consumer.routed(format!("room/{}", broadcasts - 1)).await.expect("announced"); + consumer + .routed(format!("room/{}", broadcasts - 1)) + .await + .expect("announced"); let mut tracks = Vec::new(); let cursor = match listed { true => { @@ -273,6 +276,5 @@ fn new_broadcast_with(c: &mut Criterion, name: &str, listed: bool) { group.finish(); } - criterion_group!(benches, round, new_broadcast); criterion_main!(benches); diff --git a/rs/moq-net/src/lite/all_recv.rs b/rs/moq-net/src/lite/all_recv.rs index 6a9388a5172..b7e8f57e2a1 100644 --- a/rs/moq-net/src/lite/all_recv.rs +++ b/rs/moq-net/src/lite/all_recv.rs @@ -47,10 +47,16 @@ struct Listed { enum ListState { Open, - Send { stream: Stream }, - Run { stream: Stream }, + Send { + stream: Stream, + }, + Run { + stream: Stream, + }, /// Cancelled: FIN and wait for the acknowledgement. - Finish { writer: crate::coding::Writer }, + Finish { + writer: crate::coding::Writer, + }, Done, } @@ -139,11 +145,7 @@ impl ListServe { match &mut self.state { ListState::Open => { - let mut stream = ready!(Stream::poll_open( - &mut self.session, - self.subscriber.version, - &mut cx - ))?; + let mut stream = ready!(Stream::poll_open(&mut self.session, self.subscriber.version, &mut cx))?; let defaults = Subscription::default(); stream.writer.buffer(&lite::ControlType::SubscribeAll)?; stream.writer.buffer(&lite::SubscribeAll { @@ -258,7 +260,13 @@ impl ListServe { if producer.finish_at_pending(Position::group(end.end.group)).is_err() { return Err(Error::ProtocolViolation); } - let Some(tail) = self.subscriber.subscribes.lock().get(&feed).map(|entry| entry.tail.clone()) else { + let Some(tail) = self + .subscriber + .subscribes + .lock() + .get(&feed) + .map(|entry| entry.tail.clone()) + else { return Ok(()); }; if let Ok(mut tail) = tail.write() { @@ -290,7 +298,12 @@ impl ListServe { /// A source for a listed broadcast, served like one minted for a request so the /// tracks the peer does not list can still be asked for. - fn mint(&self, path: &PathOwned, epoch: Option, live: &kio::Producer<()>) -> crate::broadcast::Producer { + fn mint( + &self, + path: &PathOwned, + epoch: Option, + live: &kio::Producer<()>, + ) -> crate::broadcast::Producer { let source = self.subscriber.origin.create_source(path); let dynamic = source.dynamic(); let _ = self.subscriber.sources.try_push(MintedSource { diff --git a/rs/moq-net/src/lite/all_serve.rs b/rs/moq-net/src/lite/all_serve.rs index 10c6c4e7ecb..1a119946058 100644 --- a/rs/moq-net/src/lite/all_serve.rs +++ b/rs/moq-net/src/lite/all_serve.rs @@ -376,7 +376,9 @@ impl kio::Task for ListedBroadcast { } tracing::debug!(broadcast = %self.path, id = self.id, "broadcast ended"); self.ctx - .send(lite::SubscribeAllResponse::BroadcastEnd(lite::BroadcastEnd { broadcast: self.id })); + .send(lite::SubscribeAllResponse::BroadcastEnd(lite::BroadcastEnd { + broadcast: self.id, + })); self.tracks = None; } @@ -413,7 +415,9 @@ enum ListedState { querying: track::Querying, }, /// Waiting for the model subscription to be confirmed. - Confirm { subscribing: track::Subscribing }, + Confirm { + subscribing: track::Subscribing, + }, Run(Box>), /// The track finished: draining the in-flight group streams. Drain { @@ -443,11 +447,10 @@ impl kio::Task for ListedTrack { .shared .version .encode_stream_code(&crate::StreamError::from(&err)); - self.ctx - .send(lite::SubscribeAllResponse::TrackReset(lite::TrackReset { - track, - code: code.into(), - })); + self.ctx.send(lite::SubscribeAllResponse::TrackReset(lite::TrackReset { + track, + code: code.into(), + })); } Poll::Ready(()) } @@ -465,16 +468,15 @@ impl ListedTrack { let info = ready!(querying.poll_ok(waiter))?; let id = self.ctx.next_track.fetch_add(1, Ordering::Relaxed); self.id = Some(id); - self.ctx - .send(lite::SubscribeAllResponse::TrackStart(lite::TrackStart { - broadcast: self.broadcast, - name: track.name().to_string().into(), - info: lite::TrackInfo { - priority: info.priority, - max_age: info.max_age, - timescale: info.timescale, - }, - })); + self.ctx.send(lite::SubscribeAllResponse::TrackStart(lite::TrackStart { + broadcast: self.broadcast, + name: track.name().to_string().into(), + info: lite::TrackInfo { + priority: info.priority, + max_age: info.max_age, + timescale: info.timescale, + }, + })); self.seen = *self.steer.read(); let subscription = crate::track::Subscription { priority: self.seen.priority, @@ -579,7 +581,9 @@ impl Respond for Responder<'_> { fn respond(&mut self, resp: lite::SubscribeResponse) -> Result<(), Error> { let track = self.track; let msg = match resp { - lite::SubscribeResponse::Start(start) => lite::SubscribeAllResponse::TrackOk(lite::TrackOk { track, start }), + lite::SubscribeResponse::Start(start) => { + lite::SubscribeAllResponse::TrackOk(lite::TrackOk { track, start }) + } lite::SubscribeResponse::End(end) => lite::SubscribeAllResponse::TrackEnd(lite::TrackEnd { track, end }), // Lite-07 has neither, and it is the only version with SUBSCRIBE_ALL. lite::SubscribeResponse::Ok(_) | lite::SubscribeResponse::Drop(_) => { diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 37316c717c8..2b6ef21251d 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -3500,11 +3500,7 @@ impl TrackRun { /// Serve one ready group or datagram (`Continue`), or `Break` once the live edge /// reaches the track's boundary. Returning per item lets the caller watch the /// requester while work stays ready. - fn poll_step( - &mut self, - out: &mut impl Respond, - waiter: &kio::Waiter, - ) -> Poll, Error>> { + fn poll_step(&mut self, out: &mut impl Respond, waiter: &kio::Waiter) -> Poll, Error>> { let mut cx = waiter.context(); // Deliver the buffered range messages before selecting more work. ready!(out.poll_flush(&mut cx))?; @@ -3640,11 +3636,7 @@ impl TrackRun { /// Lite-05/06 name a sequence as soon as this cursor gives it up; whether one went out. /// Before SUBSCRIBE_START there is no owed range yet, so the eventual start accounts /// for it instead. - fn poll_forward_drop( - &mut self, - out: &mut impl Respond, - waiter: &kio::Waiter, - ) -> Result { + fn poll_forward_drop(&mut self, out: &mut impl Respond, waiter: &kio::Waiter) -> Result { if self.start.is_none() || self.served.is_none() { return Ok(false); } @@ -3721,12 +3713,7 @@ impl TrackRun { /// serves. `source` is where the source's feed starts, raised to this cursor's floor, /// so the resolved start is the lower of the two: resolving from the later group would /// drop the older one for good. - fn start( - &mut self, - group: group::Consumer, - source: Option, - out: &mut impl Respond, - ) -> Result<(), Error> { + fn start(&mut self, group: group::Consumer, source: Option, out: &mut impl Respond) -> Result<(), Error> { let mut start = source.map_or(group.sequence, |source| source.min(group.sequence)); // A skipped group is never served, so it cannot be where the feed starts. This stops // at the held group at the latest, since it was not skipped. @@ -3778,13 +3765,11 @@ impl TrackRun { // subscription on the session, and only groups of the same one may be // ranked against each other by sequence, in that subscription's order. None => Rank::Queue( - self.ctx - .priority - .insert( - Priority::new(steer.priority, self.ctx.id, sequence) - .with_lane(self.ctx.track.unwrap_or(0)) - .with_order(steer.order), - ), + self.ctx.priority.insert( + Priority::new(steer.priority, self.ctx.id, sequence) + .with_lane(self.ctx.track.unwrap_or(0)) + .with_order(steer.order), + ), ), }; self.children diff --git a/rs/moq-net/src/lite/subscribe_all.rs b/rs/moq-net/src/lite/subscribe_all.rs index df397a7c5ad..1d5e8586779 100644 --- a/rs/moq-net/src/lite/subscribe_all.rs +++ b/rs/moq-net/src/lite/subscribe_all.rs @@ -330,7 +330,10 @@ mod test { let (got, _) = SubscribeAll::decode_slice(&buf, Version::Lite07).unwrap(); assert_eq!(got.id, 3); assert_eq!(got.prefix.as_str(), "room/a"); - assert_eq!((got.priority, got.order, got.max_delay), (7, Order::Ascending, msg.max_delay)); + assert_eq!( + (got.priority, got.order, got.max_delay), + (7, Order::Ascending, msg.max_delay) + ); let update = SubscribeAllUpdate { priority: 1, @@ -339,7 +342,10 @@ mod test { }; let buf = encode(&update, Version::Lite07).unwrap(); let (got, _) = SubscribeAllUpdate::decode_slice(&buf, Version::Lite07).unwrap(); - assert_eq!((got.priority, got.order, got.max_delay), (1, Order::Descending, Duration::ZERO)); + assert_eq!( + (got.priority, got.order, got.max_delay), + (1, Order::Descending, Duration::ZERO) + ); } /// Older versions have no Subscribe All Stream, and nothing emulates it. @@ -406,10 +412,16 @@ mod test { ); } (SubscribeAllResponse::TrackOk(a), SubscribeAllResponse::TrackOk(b)) => { - assert_eq!((a.track, a.start.group, a.start.largest), (b.track, b.start.group, b.start.largest)) + assert_eq!( + (a.track, a.start.group, a.start.largest), + (b.track, b.start.group, b.start.largest) + ) } (SubscribeAllResponse::TrackEnd(a), SubscribeAllResponse::TrackEnd(b)) => { - assert_eq!((a.track, a.end.group, a.end.streams), (b.track, b.end.group, b.end.streams)) + assert_eq!( + (a.track, a.end.group, a.end.streams), + (b.track, b.end.group, b.end.streams) + ) } (SubscribeAllResponse::TrackReset(a), SubscribeAllResponse::TrackReset(b)) => { assert_eq!((a.track, a.code), (b.track, b.code)) diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 3613d0b0628..b45e48b8ca5 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -2979,7 +2979,10 @@ mod tests { // Parked on the first write: the stream is open and nothing has been sent yet. assert!(futures::poll!(establish.as_mut()).is_pending()); assert_eq!(session.log.bi_opens(), 1); - assert!(subscribes.lock().contains_key(&Feed::subscribe(0)), "registered before the wire"); + assert!( + subscribes.lock().contains_key(&Feed::subscribe(0)), + "registered before the wire" + ); let Ok(mut open) = gate.write() else { panic!("gate closed") diff --git a/rs/moq-net/src/model/broadcast.rs b/rs/moq-net/src/model/broadcast.rs index 2aa620a12f0..b737f208ef0 100644 --- a/rs/moq-net/src/model/broadcast.rs +++ b/rs/moq-net/src/model/broadcast.rs @@ -1236,12 +1236,8 @@ impl Tracks { }; self.next = order + 1; Poll::Ready(Some( - track::Consumer::listed( - track.name().clone(), - self.broadcast.info.clone(), - track.peek(), - ) - .with_stats(self.broadcast.stats.clone()), + track::Consumer::listed(track.name().clone(), self.broadcast.info.clone(), track.peek()) + .with_stats(self.broadcast.stats.clone()), )) } diff --git a/rs/moq-net/src/model/listing.rs b/rs/moq-net/src/model/listing.rs index 168486da3e5..7626f0b5b3f 100644 --- a/rs/moq-net/src/model/listing.rs +++ b/rs/moq-net/src/model/listing.rs @@ -74,10 +74,12 @@ impl ListingReader { /// The next listed broadcast, `Ready(None)` once the handler dropped the listing. fn poll_next(&mut self, waiter: &kio::Waiter) -> Poll)>> { let next = self.next; - let found = self.state.poll(waiter, |state| match state.broadcasts.range(next..).next() { - Some((key, (broadcast, epoch))) => Poll::Ready((*key, broadcast.clone(), epoch.clone())), - None => Poll::Pending, - }); + let found = self + .state + .poll(waiter, |state| match state.broadcasts.range(next..).next() { + Some((key, (broadcast, epoch))) => Poll::Ready((*key, broadcast.clone(), epoch.clone())), + None => Poll::Pending, + }); match ready!(found) { Ok((key, broadcast, epoch)) => { self.next = key + 1; @@ -385,7 +387,7 @@ impl Broadcasts { { return None; } - let scope = self.consumer.stats.egress(&self.consumer.root.join(&path).to_owned()); + let scope = self.consumer.stats.egress(self.consumer.root.join(&path).to_owned()); let broadcast = broadcast.with_path(path).with_epoch(epoch).with_stats(scope); yielded.push(broadcast.clone()); // Ended broadcasts at paths never reached again, swept once their count doubles. @@ -404,9 +406,9 @@ impl Broadcasts { mod tests { use std::time::Duration; + use super::super::ProduceTest; use super::*; use crate::{Hop, origin::Route}; - use super::super::ProduceTest; /// Routes sharing a handler's lister ask it once per prefix, and losing one of them /// keeps the listing for the rest. @@ -414,11 +416,18 @@ mod tests { async fn routes_sharing_a_lister_are_asked_once() { let origin = Hop::new(1).unwrap().produce(); let lister = Lister::default(); - let a = origin.dynamic_listed("room/a", Route::default(), lister.clone()).unwrap(); - let _b = origin.dynamic_listed("room/b", Route::default(), lister.clone()).unwrap(); + let a = origin + .dynamic_listed("room/a", Route::default(), lister.clone()) + .unwrap(); + let _b = origin + .dynamic_listed("room/b", Route::default(), lister.clone()) + .unwrap(); let mut cursor = origin.consume().broadcasts("room").unwrap(); let quiet = Duration::from_millis(10); - assert!(moq_clock::timeout(quiet, cursor.next()).await.is_err(), "nothing listed yet"); + assert!( + moq_clock::timeout(quiet, cursor.next()).await.is_err(), + "nothing listed yet" + ); let Poll::Ready(listing) = lister.poll_requested(&kio::Waiter::noop()) else { panic!("the cursor asked for a listing"); @@ -427,7 +436,10 @@ mod tests { assert_eq!(listing.prefix().as_str(), "room"); drop(a); - assert!(moq_clock::timeout(quiet, cursor.next()).await.is_err(), "nothing listed yet"); + assert!( + moq_clock::timeout(quiet, cursor.next()).await.is_err(), + "nothing listed yet" + ); let source = origin.create_source("room/b"); listing.insert(source.consume(), None); let listed = moq_clock::timeout(quiet, cursor.next()) diff --git a/rs/moq-net/tests/integration/subscribe_all.rs b/rs/moq-net/tests/integration/subscribe_all.rs index 45c6d6ec0a8..b288a35495c 100644 --- a/rs/moq-net/tests/integration/subscribe_all.rs +++ b/rs/moq-net/tests/integration/subscribe_all.rs @@ -204,7 +204,11 @@ async fn broadcast_appears_and_ends(relay: bool) { let end = moq_clock::timeout(TIMEOUT, subscriber.recv_group()) .await .expect("track end hung"); - assert!(matches!(end, Ok(None)), "relay={relay}: {:?}", end.map(|group| group.map(|group| group.sequence))); + assert!( + matches!(end, Ok(None)), + "relay={relay}: {:?}", + end.map(|group| group.map(|group| group.sequence)) + ); let ended = moq_clock::timeout(TIMEOUT, tracks.next()) .await .expect("tracks end hung"); From f05ee7211fc850b0b9dbb4ff4f29bf11741097ec Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 18 Oct 2026 23:49:43 -0700 Subject: [PATCH 09/13] wip(moq-net): mark the listing hook pending and lint without async Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/model/listing.rs | 7 ++++++- rs/moq-net/src/model/mod.rs | 1 + rs/moq-net/src/model/origin.rs | 2 ++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/rs/moq-net/src/model/listing.rs b/rs/moq-net/src/model/listing.rs index 7626f0b5b3f..33f79f8fe5a 100644 --- a/rs/moq-net/src/model/listing.rs +++ b/rs/moq-net/src/model/listing.rs @@ -17,8 +17,9 @@ use super::{AnnounceConsumer, AnnounceEvent, Consumer}; use crate::{AsPath, Error, PathOwned, broadcast}; /// The broadcasts a handler lists under one prefix, in the order it learned them. +#[cfg_attr(not(feature = "async"), allow(dead_code))] #[derive(Default)] -pub(crate) struct ListingState { +struct ListingState { // Keyed by insertion order, so a reader resumes after the last one it saw. broadcasts: BTreeMap)>, next: u64, @@ -28,11 +29,14 @@ pub(crate) struct ListingState { /// /// Holding a broadcast here holds it for the readers to come. Readers going away is /// [`Self::poll_unused`]; dropping the listing ends it for them. +#[cfg_attr(not(feature = "async"), allow(dead_code))] +// pending: /quest/m1/public-model/answer.md pub(crate) struct Listing { prefix: PathOwned, state: kio::Producer, } +#[cfg_attr(not(feature = "async"), allow(dead_code))] impl Listing { /// The absolute prefix whose broadcasts are listed. pub fn prefix(&self) -> &crate::Path<'_> { @@ -95,6 +99,7 @@ impl ListingReader { /// One per handler, shared by every route it serves: a reader asks it once per prefix /// however many of its routes cover the prefix, and the handler fills one listing. #[derive(Clone, Default)] +// pending: /quest/m1/public-model/answer.md pub(crate) struct Lister { state: kio::Shared, } diff --git a/rs/moq-net/src/model/mod.rs b/rs/moq-net/src/model/mod.rs index 0947e6f8c28..367a92e4d1a 100644 --- a/rs/moq-net/src/model/mod.rs +++ b/rs/moq-net/src/model/mod.rs @@ -82,6 +82,7 @@ pub(crate) use origin_impl::Snapshot; // A handler's answer to a reader listing the broadcasts under a prefix. #[cfg(feature = "async")] +// pending: /quest/m1/public-model/answer.md pub(crate) use origin_impl::{Lister, Listing}; // The advertise-only route guard, for tests shaping the route table. diff --git a/rs/moq-net/src/model/origin.rs b/rs/moq-net/src/model/origin.rs index ebfde2b86ae..ecaa7186b95 100644 --- a/rs/moq-net/src/model/origin.rs +++ b/rs/moq-net/src/model/origin.rs @@ -16,6 +16,7 @@ use rand::RngExt; mod listing; pub use listing::Broadcasts; #[cfg_attr(not(feature = "async"), allow(unused_imports))] +// pending: /quest/m1/public-model/answer.md pub(crate) use listing::{Lister, Listing}; use super::{ @@ -2086,6 +2087,7 @@ impl Producer { /// [`Self::dynamic`], with readers listing the route asking `lister`, which the handler /// shares between every route it serves. + // pending: /quest/m1/public-model/answer.md pub(crate) fn dynamic_listed(&self, prefix: impl AsPath, route: Route, lister: Lister) -> Result { let announcing = Announcing::new(self, prefix)?; let serve = kio::Shared::::default(); From ce49a22b6508e39bb177ddb488b3c5c58ba5e898 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 18 Oct 2026 23:58:55 -0700 Subject: [PATCH 10/13] wip(doc): count the Subscribe All row in the draft table test Co-Authored-By: Claude Opus 5.5 --- doc/.vitepress/drafts.test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/doc/.vitepress/drafts.test.ts b/doc/.vitepress/drafts.test.ts index df79be4f232..476f408c437 100644 --- a/doc/.vitepress/drafts.test.ts +++ b/doc/.vitepress/drafts.test.ts @@ -128,20 +128,20 @@ describe("tables render as tables", () => { const rendered = html(page as DraftPage); - // The Bidirectional Streams table: one header, seven body rows, each + // The Bidirectional Streams table: one header, eight body rows, each // separated by a kramdown rule in the source. Cells carry the source's // alignment, so match around the style attribute. const header = /]*>Stream<\/th>/.exec(rendered); expect(header).not.toBeNull(); - for (const stream of ["Announce", "Subscribe", "Fetch", "Probe", "Goaway", "Track", "Auth"]) { + for (const stream of ["Announce", "Subscribe", "Fetch", "Probe", "Goaway", "Track", "Auth", "Subscribe All"]) { expect(rendered).toMatch(new RegExp(`]*>${stream}`)); } - // One header row plus seven body rows, in a single table. + // One header row plus eight body rows, in a single table. const start = rendered.lastIndexOf("")[0]; - expect(rows.match(//g) ?? []).toHaveLength(8); + expect(rows.match(//g) ?? []).toHaveLength(9); }); }); From f074d09e7865c7136ea26a178217eb2ac4502d6e Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 19 Oct 2026 10:27:53 -0700 Subject: [PATCH 11/13] fix(net): one copy per listed track, and its floor goes upstream A track both listed by SUBSCRIBE_ALL and requested by name is one copy per session: requests and listings resolve a broadcast through the copy the session already holds for its epoch, and a requested track hands its SUBSCRIBE over to the listing's feed once the feed covers it. A reader asking below a listed track's start opens a bounded SUBSCRIBE for the gap, at every hop. A listed track's demand is its resolved start, so a relay sees a lower floor instead of folding it into "oldest". The start names past the groups the publisher gave up before resolving it, so a warm relay answers as a cold one. SUBSCRIBE_ALL_UPDATE carries the readers' aggregate preferences upstream. Co-Authored-By: Claude Opus 5.5 --- drafts/draft-lcurley-moq-lite.md | 2 + rs/moq-net/benches/subscribe-all.rs | 11 +- rs/moq-net/src/lite/all_recv.rs | 430 ++++++++++++------ rs/moq-net/src/lite/publisher.rs | 85 +++- rs/moq-net/src/lite/subscriber.rs | 356 ++++++++++++++- rs/moq-net/src/model/broadcast.rs | 19 + rs/moq-net/tests/integration/subscribe_all.rs | 252 ++++++++++ 7 files changed, 988 insertions(+), 167 deletions(-) diff --git a/drafts/draft-lcurley-moq-lite.md b/drafts/draft-lcurley-moq-lite.md index 908a9b6265f..31e351ec2dc 100644 --- a/drafts/draft-lcurley-moq-lite.md +++ b/drafts/draft-lcurley-moq-lite.md @@ -539,6 +539,8 @@ The subscriber MUST NOT reject such a stream while the request is open; it waits The request needs subscribe permission on the prefix, and the publisher MUST NOT list a Broadcast the session does not allow. It counts as one subscription against the publisher's bound (see [Resource Exhaustion](#resource-exhaustion)). A relay MAY answer every SUBSCRIBE_ALL for a prefix with one request upstream. +Such a relay sends the highest `Subscriber Priority` and `Subscriber Max Age` its downstream readers ask for, with SUBSCRIBE_ALL_UPDATE as they change. +A subscriber that wants a listed Track's groups below its TRACK_OK `Group` sends a SUBSCRIBE for them that ends before that `Group`; a relay without them does the same upstream. The subscriber cancels by closing its side (FIN), and the publisher then closes its own without owing anything further. The publisher closes its side once it will list no more Broadcasts and every listed Track has ended. diff --git a/rs/moq-net/benches/subscribe-all.rs b/rs/moq-net/benches/subscribe-all.rs index 9bc2be6f8be..34dfe082b0c 100644 --- a/rs/moq-net/benches/subscribe-all.rs +++ b/rs/moq-net/benches/subscribe-all.rs @@ -22,7 +22,7 @@ mod support; use std::time::{Duration, Instant}; use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main}; -use moq_net::{Hop, Timestamp, Version, broadcast, origin, track}; +use moq_net::{Epoch, Hop, Timestamp, Version, broadcast, origin, track}; use support::harness::{MockConnectOptions, MockPair, connect_mock}; const VERSION: &str = "moq-lite-07-wip"; @@ -76,6 +76,11 @@ async fn read(subscriber: &mut track::Subscriber) -> u64 { group.sequence } +/// Announced with an epoch, so a relay's listing and its requests share one copy. +fn route() -> origin::Route { + origin::Route::default().with_epoch(Epoch::mint()) +} + fn subscription() -> track::Subscription { track::Subscription::default().with_max_delay(Duration::from_secs(60)) } @@ -117,7 +122,7 @@ impl Room { write(&track, 0); tracks.push(track); } - broadcast.announce(Default::default()).unwrap(); + broadcast.announce(route()).unwrap(); held.push(broadcast); } @@ -197,7 +202,7 @@ impl Room { let broadcast = self.publisher.create_broadcast(format!("room/new{index}")).unwrap(); let track = broadcast.create_track("0", timed()).unwrap(); write(&track, 0); - broadcast.announce(Default::default()).unwrap(); + broadcast.announce(route()).unwrap(); for lister in &mut self.listers { let consumer = match lister.cursor.as_mut() { Some(cursor) => { diff --git a/rs/moq-net/src/lite/all_recv.rs b/rs/moq-net/src/lite/all_recv.rs index b7e8f57e2a1..b950905ccd9 100644 --- a/rs/moq-net/src/lite/all_recv.rs +++ b/rs/moq-net/src/lite/all_recv.rs @@ -3,11 +3,143 @@ //! Every route the peer announces shares the session's lister, so readers ask the session //! once per prefix, and the session answers with a single request: the peer lists //! everything it holds there, whichever of its routes serves it. +//! +//! A listed broadcast resolves to the session's copy of it, the one a request the origin +//! routes here gets too, and each listed track is served by the same serve loop as a +//! requested one. The feed carries the track from its live edge, so the loop subscribes +//! upstream only for a floor a reader asks for below it; a track a request already serves +//! hands its subscription over to the feed instead. The request carries the aggregate of +//! what its tracks' readers ask for, updated as that moves. use super::*; use crate::coding::StreamCodes; +/// The request-wide preferences a SUBSCRIBE_ALL and its updates carry. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(super) struct Prefs { + priority: u8, + order: crate::track::Order, + max_delay: Duration, +} + +impl Prefs { + fn of(subscription: &Subscription) -> Self { + Self { + priority: subscription.priority, + order: subscription.order, + max_delay: subscription.max_delay, + } + } +} + +impl Default for Prefs { + fn default() -> Self { + Self::of(&Subscription::default()) + } +} + +/// What the readers of one request's tracks ask for, counted per track, so the request +/// carries their aggregate the way one track's subscribers combine: the highest priority, +/// the longest max delay, and ascending order only when every track asks for it. +#[derive(Default)] +pub(super) struct Demands { + priority: BTreeMap, + max_delay: BTreeMap, + tracks: usize, + descending: usize, +} + +impl Demands { + fn add(&mut self, prefs: Prefs) { + *self.priority.entry(prefs.priority).or_default() += 1; + *self.max_delay.entry(prefs.max_delay).or_default() += 1; + self.tracks += 1; + if prefs.order == crate::track::Order::Descending { + self.descending += 1; + } + } + + fn remove(&mut self, prefs: Prefs) { + fn take(counts: &mut BTreeMap, key: K) { + if let std::collections::btree_map::Entry::Occupied(mut entry) = counts.entry(key) { + *entry.get_mut() -= 1; + if *entry.get() == 0 { + entry.remove(); + } + } + } + take(&mut self.priority, prefs.priority); + take(&mut self.max_delay, prefs.max_delay); + self.tracks -= 1; + if prefs.order == crate::track::Order::Descending { + self.descending -= 1; + } + } + + fn aggregate(&self) -> Prefs { + let none = Prefs::default(); + Prefs { + priority: self.priority.keys().next_back().copied().unwrap_or(none.priority), + max_delay: self.max_delay.keys().next_back().copied().unwrap_or(none.max_delay), + order: match (self.tracks, self.descending) { + (0, _) => none.order, + (_, 0) => crate::track::Order::Ascending, + _ => crate::track::Order::Descending, + }, + } + } +} + +/// A listed track's TRACK_OK, TRACK_END or TRACK_RESET, for its serve loop. +pub(super) enum ListedResponse { + Ok(lite::SubscribeStart), + End(lite::SubscribeEnd), + Reset(Error), +} + +/// One track's feed from a SUBSCRIBE_ALL, as its serve loop holds it. +/// +/// Dropping it stops routing the feed's group streams to the track and takes its readers +/// out of the request's aggregate. +pub(super) struct ListedFeed { + /// The track's responses in wire order, closed once the request stops feeding it. + pub(super) responses: kio::Queue, + /// The feed's group streams, counted against its end. + pub(super) tail: kio::Producer, + feed: Feed, + subscribes: Lock>, + demands: kio::Producer, + counted: Option, +} + +impl ListedFeed { + /// Count what the track's readers ask for, `None` for no reader, in the request's + /// aggregate. + pub(super) fn count(&mut self, subscription: Option<&Subscription>) { + let prefs = subscription.map(Prefs::of); + if prefs == self.counted { + return; + } + if let Ok(mut demands) = self.demands.write() { + if let Some(counted) = self.counted { + demands.remove(counted); + } + if let Some(prefs) = prefs { + demands.add(prefs); + } + } + self.counted = prefs; + } +} + +impl Drop for ListedFeed { + fn drop(&mut self) { + self.count(None); + self.subscribes.lock().remove(&self.feed); + } +} + /// One upstream SUBSCRIBE_ALL and the listings it fills. pub(super) struct ListServe { subscriber: Subscriber, @@ -18,13 +150,19 @@ pub(super) struct ListServe { outputs: Vec, id: u64, state: ListState, - broadcasts: HashMap, + broadcasts: HashMap, next_broadcast: u64, - tracks: HashMap>, + /// Each listed track's responses, `None` for one this request does not feed. + tracks: HashMap>>, /// The TRACK_STARTs read, which a group stream ahead of its own waits on. started: kio::Producer, - /// Ended tracks waiting for their last group streams. - settling: kio::Tasks, + /// The serve loops of the tracks this request created, which outlive it while readers + /// hold them. + serving: kio::Tasks>, + demands: kio::Producer, + demanded: kio::Consumer, + /// The preferences the request carries now. + sent: Prefs, // Dedicated handles for opening the stream and watching the session close. session: S, closed: S, @@ -37,12 +175,14 @@ struct Output { } /// A broadcast the peer listed. -struct Listed { - /// `None` for one outside what the session may subscribe to: its tracks are ignored. +struct ListedBroadcast { + /// The session's copy, `None` for one outside what the session may subscribe to: its + /// tracks are ignored. source: Option, + path: PathOwned, epoch: Option, - /// Closes the source's serve machine once the peer ends the broadcast. - _live: kio::Producer<()>, + /// Closes the copy this listing minted once the peer ends the broadcast. + _live: Option>, } enum ListState { @@ -72,6 +212,7 @@ impl ListServe { subscriber.started.lock().insert(id, started.consume()); let session = subscriber.session.clone(); let closed = subscriber.session.clone(); + let demands = kio::Producer::::default(); Self { session, subscriber, @@ -87,7 +228,10 @@ impl ListServe { next_broadcast: 0, tracks: HashMap::new(), started, - settling: kio::Tasks::new(), + serving: kio::Tasks::new(), + demanded: demands.consume(), + demands, + sent: Prefs::default(), closed, } } @@ -120,7 +264,7 @@ impl ListServe { while let Ok(Some(listing)) = self.input.try_pop() { self.attach(listing); } - let _ = self.settling.poll(waiter); + let _ = self.serving.poll(waiter); // Nobody reads any listing: cancel, unless one joins in the meantime. if !matches!(self.state, ListState::Finish { .. } | ListState::Done) && self.poll_unused(waiter) { @@ -146,14 +290,14 @@ impl ListServe { match &mut self.state { ListState::Open => { let mut stream = ready!(Stream::poll_open(&mut self.session, self.subscriber.version, &mut cx))?; - let defaults = Subscription::default(); + self.sent = self.demands.read().aggregate(); stream.writer.buffer(&lite::ControlType::SubscribeAll)?; stream.writer.buffer(&lite::SubscribeAll { id: self.id, prefix: self.prefix.as_path(), - priority: defaults.priority, - order: defaults.order, - max_delay: defaults.max_delay, + priority: self.sent.priority, + order: self.sent.order, + max_delay: self.sent.max_delay, })?; tracing::info!(id = self.id, prefix = %self.subscriber.log_path(&self.prefix), "subscribe all started"); self.state = ListState::Send { stream }; @@ -166,6 +310,26 @@ impl ListServe { self.state = ListState::Run { stream }; } ListState::Run { stream } => { + // What the tracks' readers ask for moved: carry it to every track at once. + let sent = self.sent; + let moved = match self.demanded.poll(waiter, |demands| match demands.aggregate() { + prefs if prefs != sent => Poll::Ready(prefs), + _ => Poll::Pending, + }) { + Poll::Ready(Ok(prefs)) => Some(prefs), + _ => None, + }; + if let Some(prefs) = moved { + stream.writer.buffer(&lite::SubscribeAllUpdate { + priority: prefs.priority, + order: prefs.order, + max_delay: prefs.max_delay, + })?; + self.sent = prefs; + } + if let Poll::Ready(Err(err)) = stream.writer.poll_flush(&mut cx) { + return Poll::Ready(Err(err)); + } match ready!(stream.reader.poll_decode_maybe::(&mut cx)) { Ok(Some(msg)) => self.handle(msg)?, // The publisher ended the request: its origin closed. @@ -191,12 +355,17 @@ impl ListServe { let id = self.next_broadcast; self.next_broadcast += 1; let path = self.prefix.join(&start.suffix).to_owned(); - let live = kio::Producer::new(()); let allowed = self .subscriber .auth .within_limit(crate::auth::Direction::Subscribe, path.as_str()); - let source = allowed.then(|| self.mint(&path, start.epoch.clone(), &live)); + let (source, live) = match allowed { + true => { + let (source, live) = self.resolve(&path, start.epoch.as_ref()); + (Some(source), live) + } + false => (None, None), + }; if let Some(source) = &source { for output in &mut self.outputs { let key = output.listing.insert(source.consume(), start.epoch.clone()); @@ -206,16 +375,17 @@ impl ListServe { tracing::debug!(broadcast = %self.subscriber.log_path(&path), id, allowed, "listed broadcast"); self.broadcasts.insert( id, - Listed { + ListedBroadcast { source, + path, epoch: start.epoch, _live: live, }, ); } lite::SubscribeAllResponse::BroadcastEnd(end) => { - // Dropping it closes the source: no new tracks, while the listed ones run to - // their own end. + // Dropping it closes the copy it minted: no new tracks, while the listed ones + // run to their own end. self.broadcasts.remove(&end.broadcast).ok_or(Error::ProtocolViolation)?; for output in &mut self.outputs { if let Some(key) = output.keys.remove(&end.broadcast) { @@ -226,146 +396,159 @@ impl ListServe { lite::SubscribeAllResponse::TrackStart(start) => { let id = *self.started.read(); let listed = self.broadcasts.get(&start.broadcast).ok_or(Error::ProtocolViolation)?; - let producer = match &listed.source { - Some(source) => self.create(source, id, &start)?, + let responses = match &listed.source { + Some(source) => { + let (source, path, epoch) = (source.clone(), listed.path.clone(), listed.epoch.clone()); + self.create(&source, path, epoch, id, &start) + } None => None, }; - self.tracks.insert(id, producer); + self.tracks.insert(id, responses); // Registered first, so a group stream waiting on it finds the track. if let Ok(mut started) = self.started.write() { *started += 1; } } lite::SubscribeAllResponse::TrackOk(ok) => { - let entry = self.tracks.get_mut(&ok.track).ok_or(Error::ProtocolViolation)?; - if let Some(producer) = entry { - producer.set_live(ok.start.largest); - if let Some(largest) = ok.start.largest { - producer.report_largest(largest); - } - let _ = producer.start_at(ok.start.group); + let responses = self.tracks.get(&ok.track).ok_or(Error::ProtocolViolation)?; + if let Some(responses) = responses { + let _ = responses.try_push(ListedResponse::Ok(ok.start)); } } lite::SubscribeAllResponse::TrackEnd(end) => { - let entry = self.tracks.remove(&end.track).ok_or(Error::ProtocolViolation)?; - let Some(mut producer) = entry else { - return Ok(()); - }; - let feed = Feed { - subscribe: self.id, - track: Some(end.track), - }; - // Lower groups may still be on the wire behind the end, so readers wait at a - // hole until the tail settles. A boundary below a received group contradicts it. - if producer.finish_at_pending(Position::group(end.end.group)).is_err() { - return Err(Error::ProtocolViolation); - } - let Some(tail) = self - .subscriber - .subscribes - .lock() - .get(&feed) - .map(|entry| entry.tail.clone()) - else { - return Ok(()); - }; - if let Ok(mut tail) = tail.write() { - tail.expire(self.subscriber.runtime.now()); + let responses = self.tracks.remove(&end.track).ok_or(Error::ProtocolViolation)?; + if let Some(responses) = responses { + let _ = responses.try_push(ListedResponse::End(end.end)); + responses.close(); } - self.settling.push(SettleTrack { - settle: Settle::new(&self.subscriber.runtime, tail.consume()), - streams: end.end.streams, - producer, - feed, - subscribes: self.subscriber.subscribes.clone(), - }); } lite::SubscribeAllResponse::TrackReset(reset) => { - let entry = self.tracks.remove(&reset.track).ok_or(Error::ProtocolViolation)?; - if let Some(producer) = entry { + let responses = self.tracks.remove(&reset.track).ok_or(Error::ProtocolViolation)?; + if let Some(responses) = responses { let code = u32::try_from(reset.code).unwrap_or(u32::MAX); let err = Error::Stream(self.subscriber.version.decode_stream_code(code)); - let _ = producer.abort(err); + let _ = responses.try_push(ListedResponse::Reset(err)); + responses.close(); } - self.subscriber.subscribes.lock().remove(&Feed { - subscribe: self.id, - track: Some(reset.track), - }); } } Ok(()) } - /// A source for a listed broadcast, served like one minted for a request so the - /// tracks the peer does not list can still be asked for. - fn mint( + /// The session's copy of a listed broadcast: the one a request already resolved for + /// the same publisher instance, or a new source served like one minted for a request, + /// so the tracks the peer does not list can still be asked for. A minted copy closes + /// with the returned handle, once the peer ends the broadcast. + fn resolve( &self, path: &PathOwned, - epoch: Option, - live: &kio::Producer<()>, - ) -> crate::broadcast::Producer { + epoch: Option<&crate::Epoch>, + ) -> (crate::broadcast::Producer, Option>) { + if let Some(copy) = self.subscriber.copy(path, epoch) { + return (copy, None); + } + let live = kio::Producer::new(()); let source = self.subscriber.origin.create_source(path); + self.subscriber.hold_copy(path, epoch, &source); let dynamic = source.dynamic(); let _ = self.subscriber.sources.try_push(MintedSource { validity: Default::default(), path: path.clone(), - epoch, + epoch: epoch.cloned(), source: crate::model::broadcast::SourceGuard::new(source.clone()), dynamic, route: live.consume(), }); - source + (source, Some(live)) } - /// Create a listed track in its broadcast's source and route its group streams to it. + /// Feed a listed track: create it in the broadcast's copy with a serve loop of its own, + /// or hand the feed to the loop already serving it. Returns where its responses go, + /// `None` when nothing reads this feed. fn create( - &self, + &mut self, source: &crate::broadcast::Producer, + path: PathOwned, + epoch: Option, id: u64, start: &lite::TrackStart<'_>, - ) -> Result, Error> { + ) -> Option> { + let feed = Feed { + subscribe: self.id, + track: Some(id), + }; + let tail = kio::Producer::new(Tail::new(tail::grace(self.sent.max_delay))); + let responses = kio::Queue::new(); + let listed = ListedFeed { + responses: responses.clone(), + tail: tail.clone(), + feed, + subscribes: self.subscriber.subscribes.clone(), + demands: self.demands.clone(), + counted: None, + }; + let entry = |producer: &track::Producer| TrackEntry { + producer: producer.clone(), + timescale: start.info.timescale, + tail: tail.clone(), + }; let info = track::Info::default() .with_timescale(start.info.timescale) .with_max_age(start.info.max_age) .with_priority(start.info.priority); - let producer = match source.create_track(start.name.as_ref(), info) { - Ok(producer) => producer, - // A reader asked for the track before the peer listed it, and its own - // subscription feeds it: list that one, and let this feed's groups go. + match source.create_track(start.name.as_ref(), info) { + Ok(mut producer) => { + // Readers wait for TRACK_OK to say where the feed starts, as they wait for + // SUBSCRIBE_START on lite-07. + let _ = producer.request_start(None); + self.subscriber.subscribes.lock().insert(feed, entry(&producer)); + let serve = TrackServe { + validity: Default::default(), + subscriber: self.subscriber.clone(), + path, + epoch, + name: start.name.to_string(), + }; + self.serving + .push(TrackServeRun::listed(serve, producer, start.info.timescale, listed)); + } + // A request got there first: feed its copy, whose subscription hands over to the + // feed. One still asking the peer about the track keeps its own subscription. Err(Error::Duplicate) => { source.list_track(&start.name); - return Ok(None); + let key = (path, Arc::from(start.name.as_ref())); + let served = self + .subscriber + .served + .lock() + .get(&key) + .and_then(|loops| loops.iter().find(|served| source.serves(&served.producer)).cloned())?; + self.subscriber.subscribes.lock().insert(feed, entry(&served.producer)); + served.attach.try_push(listed).ok()?; } - Err(err) => return Err(err), - }; - let tail = kio::Producer::new(Tail::new(tail::grace(Subscription::default().max_delay))); - self.subscriber.subscribes.lock().insert( - Feed { - subscribe: self.id, - track: Some(id), - }, - TrackEntry { - producer: producer.clone(), - timescale: start.info.timescale, - tail, - }, - ); - Ok(Some(producer)) + // The copy closed: it takes no new tracks. + Err(err) => { + tracing::debug!(track = %start.name, %err, "listed track not created"); + return None; + } + } + Some(responses) } - /// Stop feeding everything: the request is over. + /// Stop feeding everything: the request is over. A track readers still hold carries on + /// as a requested one when the request was only cancelled, and ends with it otherwise. fn end(&mut self, err: &Error) { - let mut subscribes = self.subscriber.subscribes.lock(); - for (id, producer) in self.tracks.drain() { - subscribes.remove(&Feed { - subscribe: self.id, - track: Some(id), - }); - if let Some(producer) = producer { - let _ = producer.abort(err.clone()); + for (_, responses) in self.tracks.drain() { + let Some(responses) = responses else { + continue; + }; + if !matches!(err, Error::Cancel) { + let _ = responses.try_push(ListedResponse::Reset(err.clone())); } + responses.close(); } - drop(subscribes); + self.subscriber.started.lock().remove(&self.id); + let _ = self.started.close(); self.broadcasts.clear(); self.outputs.clear(); self.input.close(); @@ -376,7 +559,10 @@ impl kio::Task for ListServe { type Output = (); fn poll(&mut self, waiter: &kio::Waiter) -> Poll<()> { - let res = ready!(self.poll_serve(waiter)); + let Poll::Ready(res) = self.poll_serve(waiter) else { + let _ = self.serving.poll(waiter); + return Poll::Pending; + }; if let Err(err) = res { let err = lite::error::control(&mut self.subscriber.session.clone(), err); tracing::debug!(prefix = %self.subscriber.log_path(&self.prefix), %err, "subscribe all error"); @@ -387,8 +573,8 @@ impl kio::Task for ListServe { stream.writer.abort(&err); } } - // Tracks that ended before the request did still settle. - self.settling.poll(waiter) + // The tracks readers still hold outlive the request. + self.serving.poll(waiter) } } @@ -397,25 +583,3 @@ impl Drop for ListServe { self.subscriber.started.lock().remove(&self.id); } } - -/// A listed track the peer ended, waiting for its last group streams. -struct SettleTrack { - settle: Settle, - streams: u64, - producer: track::Producer, - feed: Feed, - subscribes: Lock>, -} - -impl kio::Task for SettleTrack { - type Output = (); - - fn poll(&mut self, waiter: &kio::Waiter) -> Poll<()> { - let streams = self.streams; - ready!(self.settle.poll(waiter, |tail| tail.streams() >= streams)); - self.subscribes.lock().remove(&self.feed); - self.producer.set_tail_pending(false); - let _ = self.producer.finish(); - Poll::Ready(()) - } -} diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 2b6ef21251d..addac11f883 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -3371,8 +3371,9 @@ struct TrackRun { start: Option, // The first servable group, held until the source resolves where its feed starts. first: Option, - // Groups skipped for a missing head before the start resolved, which it must not name. - skipped: BTreeSet, + // Groups given up before the start resolved (a missing head, stale, or dropped + // upstream), which it must not name. + skipped: RangeSet, end_sent: bool, // Lite07+ sends SUBSCRIBE_END with the stream count instead of as soon as the // boundary is known, once every group below it has opened its stream. @@ -3413,7 +3414,7 @@ impl TrackRun { emit_range, start: None, first: None, - skipped: BTreeSet::new(), + skipped: RangeSet::new(), end_sent: false, count_streams, served, @@ -3446,12 +3447,12 @@ impl TrackRun { let requested = self.track.subscription().start().map(|start| start.group); let floored = requested.is_some_and(|group| group > 0); let bounds = Bounds::from(&upd); - let _ = self.track.update(crate::track::Subscription { + let _ = self.track.update(self.listed_demand(crate::track::Subscription { priority: upd.priority, max_delay: serving_max_delay(self.ctx.version, upd.max_delay), order: upd.order, ..bounds.positions() - }); + })); // An explicit start moves the read cursor. Lite-06+ encodes an absent // start as no floor, so a subscription that had one above group 0 has to // drop back to 0: the cursor only rises on its own, and a finished group @@ -3459,10 +3460,11 @@ impl TrackRun { // cursor that was already at 0 stays where the first served group put it. // No fresh SUBSCRIBE_START follows: the subscriber clears its permanent-miss // floor on the same update, so both sides have to change together. - // Pre-06 an absent start means the latest group: see the re-pin below. + // Pre-06 an absent start means the latest group: see the re-pin below. A listed + // track's floor is its resolved start (see `listed_demand`), which no update moves. let lowered = match upd.start_group { Some(start_group) => Some(start_group), - None if floored && self.ctx.version.resolves_start() => Some(0), + None if floored && self.ctx.version.resolves_start() && self.ctx.track.is_none() => Some(0), None => None, }; // `position_cursor` pins an unfloored SUBSCRIBE to the latest group, so a pre-06 @@ -3583,7 +3585,7 @@ impl TrackRun { // only be reset; the next servable group resolves the start. tracing::debug!(subscribe = self.ctx.id, track = %self.ctx.track_name, sequence = group.sequence, "skipping group with a missing head"); if self.emit_range && self.start.is_none() { - self.skipped.insert(group.sequence); + self.skipped.insert(group.sequence..group.sequence.saturating_add(1)); } else if self.start.is_some() && let Some(end) = group.sequence.checked_add(1) { @@ -3633,17 +3635,21 @@ impl TrackRun { Poll::Pending } - /// Lite-05/06 name a sequence as soon as this cursor gives it up; whether one went out. + /// Lite-05/06 name a sequence as soon as this cursor gives it up; whether one was taken. /// Before SUBSCRIBE_START there is no owed range yet, so the eventual start accounts - /// for it instead. + /// for it instead: polled from the first step, the cursor records what it gives up on + /// the way to its first group. fn poll_forward_drop(&mut self, out: &mut impl Respond, waiter: &kio::Waiter) -> Result { - if self.start.is_none() || self.served.is_none() { + if !self.emit_range || (self.start.is_some() && self.served.is_none()) { return Ok(false); } let Poll::Ready(range) = self.track.poll_drop(waiter) else { return Ok(false); }; - self.drop_range(range, out)?; + match self.start { + None => self.skipped.insert(range), + Some(_) => self.drop_range(range, out)?, + } Ok(true) } @@ -3715,13 +3721,16 @@ impl TrackRun { /// drop the older one for good. fn start(&mut self, group: group::Consumer, source: Option, out: &mut impl Respond) -> Result<(), Error> { let mut start = source.map_or(group.sequence, |source| source.min(group.sequence)); - // A skipped group is never served, so it cannot be where the feed starts. This stops - // at the held group at the latest, since it was not skipped. - while self.skipped.contains(&start) { - start += 1; + // A group given up is never served, so it cannot be where the feed starts. This stops + // at the held group at the latest. + if let Some(skipped) = self.skipped.get(&start) { + start = skipped.end.min(group.sequence); } - self.skipped.clear(); + self.skipped = RangeSet::new(); self.start = Some(start); + if self.ctx.track.is_some() { + let _ = self.track.update(self.listed_demand(self.track.subscription())); + } // Only the group: 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. @@ -3750,6 +3759,18 @@ impl TrackRun { Ok(()) } + /// A SUBSCRIBE_ALL track's demand from its resolved start on. It asked for no bounds, + /// so once its start resolves that is all it asks for, and a relay serving it still + /// sees another reader asking below it. + fn listed_demand(&self, mut subscription: crate::track::Subscription) -> crate::track::Subscription { + if self.ctx.track.is_some() + && let Some(start) = self.start + { + subscription.set_bounds(Some(track::Position::group(start)), subscription.end()); + } + subscription + } + /// Open a group machine for `group`. fn serve(&mut self, group: group::Consumer) { let sequence = group.sequence; @@ -5469,6 +5490,36 @@ mod serve_group_test { assert_eq!(drops(&mut writer, &log, version).await, [(1, 1), (3, 3)]); } + /// SUBSCRIBE_START names where delivery starts, so a start the source declared below + /// cached groups this subscription's budget skips is raised past them: a warm relay + /// answers as a cold one would. + #[moq_clock::test] + async fn the_start_names_past_the_stale_groups_it_skips() { + let version = Version::Lite07; + let mut track = track::Producer::new( + Arc::new(broadcast::Info::default()), + "test", + crate::track::Info::timed(), + ); + track.start_at(0).unwrap(); + for sequence in 0..3 { + write_group(&mut track, sequence, sequence * 10); + } + let subscriber = track.subscribe(track::Subscription::default()); + let (mut run, mut writer, log) = lite_run(version, SinkSession::new(Log::default()), subscriber); + assert!(kio::wait(|waiter| run.poll(&mut writer, waiter)).now_or_never().is_none()); + kio::wait(|waiter| writer.poll_flush(&mut waiter.context())) + .await + .unwrap(); + + let writes = log.writes.lock().unwrap().clone(); + let (msg, _) = lite::SubscribeResponse::decode_slice(&writes, version).unwrap(); + let lite::SubscribeResponse::Start(start) = msg else { + panic!("expected SUBSCRIBE_START, got {msg:?}"); + }; + assert_eq!(start.group, 2, "named groups it skipped as stale"); + } + /// A lite-07 run over a relay's track, whose upstream subscription still waits on the /// source's SUBSCRIBE_START, on a subscribe stream the test can push updates onto. struct RelayRun { diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index b45e48b8ca5..0784acc9baf 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -20,7 +20,7 @@ use kio::Lock; #[path = "all_recv.rs"] mod all_recv; -use all_recv::ListServe; +use all_recv::{ListServe, ListedFeed, ListedResponse}; pub(super) struct SubscriberConfig { pub runtime: crate::time::Deadlines, @@ -92,6 +92,13 @@ pub(super) struct Subscriber { /// How many TRACK_STARTs each open SUBSCRIBE_ALL has read, for a group stream that /// arrives ahead of its track's. started: Lock>>, + /// The copy this session holds of each broadcast an epoch names, by path: a request the + /// origin routes here and a SUBSCRIBE_ALL listing the broadcast resolve to the same one. + /// Without an epoch nothing says two are the same instance, so each keeps its own. + copies: Lock>>, + /// Every track a serve loop feeds, by broadcast path and name, for a SUBSCRIBE_ALL + /// listing one to join rather than feed a second copy. + served: Lock), Vec>>, going_away: crate::goaway::GoingAway, auth: crate::auth::Handle, /// What this session may allocate up front for frames still arriving. @@ -149,6 +156,8 @@ impl Subscriber { sources: kio::Queue::new(), lister: Default::default(), started: Default::default(), + copies: Default::default(), + served: Default::default(), going_away: config.going_away, auth: config.auth, frames: Default::default(), @@ -526,6 +535,40 @@ impl Subscriber { self.subscribes.lock().remove(&Feed::subscribe(id)); } + /// The open copy of the broadcast `epoch` names at `path`, if this session holds one. + fn copy(&self, path: &PathOwned, epoch: Option<&crate::Epoch>) -> Option { + let epoch = epoch?; + let copies = self.copies.lock(); + let (_, source) = copies + .get(path)? + .iter() + .find(|(held, source)| held == epoch && !source.is_closed())?; + Some(source.clone()) + } + + /// Record the copy of the broadcast `epoch` names at `path`, for the next request or + /// listing of it. + fn hold_copy(&self, path: &PathOwned, epoch: Option<&crate::Epoch>, source: &crate::broadcast::Producer) { + let Some(epoch) = epoch else { + return; + }; + let mut copies = self.copies.lock(); + let held = copies.entry(path.clone()).or_default(); + held.retain(|(_, source)| !source.is_closed()); + held.push((epoch.clone(), source.clone())); + } + + /// Forget the closed copies at `path`. + fn forget_copies(&self, path: &PathOwned) { + let mut copies = self.copies.lock(); + if let Some(held) = copies.get_mut(path) { + held.retain(|(_, source)| !source.is_closed()); + if held.is_empty() { + copies.remove(path); + } + } + } + /// Decode one datagram body and hand it to the matching subscription's producer. fn route_datagram(&self, payload: bytes::Bytes) -> Result<(), Error> { // Write through the map rather than cloning the entry out: a `TrackEntry` clone @@ -1771,6 +1814,14 @@ impl kio::Task for SourceServe { } } +impl Drop for SourceServe { + fn drop(&mut self) { + // Closed here rather than by the guard after this body, so the copy is forgotten. + self.source.close(); + self.subscriber.forget_copies(&self.path); + } +} + #[cfg(test)] mod tests { use super::*; @@ -4979,7 +5030,13 @@ impl Announced { continue; }; let path = path.to_owned(); + // A copy a SUBSCRIBE_ALL listed, or an earlier request resolved, serves it. + if let Some(copy) = subscriber.copy(&path, entry.route.epoch.as_ref()) { + request.accept(©); + continue; + } let source = subscriber.origin.create_source(&path); + subscriber.hold_copy(&path, entry.route.epoch.as_ref(), &source); // The handler exists before the requester sees the source, so a track it // asks for before the serve machine first runs queues instead of failing // `NotFound`. Accepted before the push, so the requester holds the source @@ -5582,6 +5639,25 @@ impl TrackServeRun { Self { serve, state, gate } } + /// Serve a track a SUBSCRIBE_ALL created, fed by `feed`: the upstream subscription + /// carries only what readers ask for below it. + fn listed( + serve: TrackServe, + producer: track::Producer, + timescale: Option, + feed: ListedFeed, + ) -> Self { + let dynamic = producer.dynamic(); + let mut serve_loop = ServeLoop::serving(&serve, producer, dynamic, timescale); + serve_loop.listed = Some(Listed { feed, covered: None }); + let gate = serve.gate(); + Self { + serve, + state: TrackRunState::Serve(serve_loop), + gate, + } + } + /// The session no longer allows the broadcast: end the track and cancel the /// upstream subscription and fetches, leaving the rest of the session alone. fn revoke(&mut self) { @@ -5849,6 +5925,66 @@ struct ServeLoop { /// The TRACK stream that answered, until a SUBSCRIBE has a response or nobody holds /// the copy. held: Option>, + /// The SUBSCRIBE_ALL feeding this track from its live edge, while one does. A + /// subscription then asks only for what the feed does not carry: a floor below it. + listed: Option, + /// Where a SUBSCRIBE_ALL listing the track after a request did hands its feed over. + attach: kio::Queue, + _served: ServedGuard, +} + +/// A listed track's feed, and how far down it and the subscriptions beside it reach. +struct Listed { + feed: ListedFeed, + /// Everything from here on is the feed's or already delivered, once TRACK_OK said where + /// the feed starts. + covered: Option, +} + +/// A track a serve loop feeds, for a SUBSCRIBE_ALL listing it to join. +#[derive(Clone)] +struct Served { + producer: track::Producer, + attach: kio::Queue, +} + +/// Keeps a serve loop's track in [`Subscriber::served`] for as long as it runs. +struct ServedGuard { + served: Lock), Vec>>, + key: (PathOwned, Arc), + attach: kio::Queue, +} + +impl ServedGuard { + fn new(serve: &TrackServe, producer: &track::Producer) -> Self { + let key = (serve.path.clone(), Arc::from(serve.name.as_str())); + let attach = kio::Queue::new(); + let mut served = serve.subscriber.served.lock(); + served.entry(key.clone()).or_default().push(Served { + producer: producer.clone(), + attach: attach.clone(), + }); + drop(served); + Self { + served: serve.subscriber.served.clone(), + key, + attach, + } + } +} + +impl Drop for ServedGuard { + fn drop(&mut self) { + // A feed handed over too late drops with the queue, unregistering its groups. + self.attach.close(); + let mut served = self.served.lock(); + if let Some(loops) = served.get_mut(&self.key) { + loops.retain(|served| !served.attach.same_channel(&self.attach)); + if loops.is_empty() { + served.remove(&self.key); + } + } + } } // A state machine's enum is its storage: one transient instance per stream, so the @@ -6035,8 +6171,22 @@ impl ServeLoop { false => request, }; let serving = request.accept(info); + Self::serving(serve, serving, dynamic, timescale) + } + + /// Serve a copy already accepted, `dynamic` fetching what its cache misses. + fn serving( + serve: &TrackServe, + serving: track::Producer, + dynamic: track::Dynamic, + timescale: Option, + ) -> Self { serving.received(); + let served = ServedGuard::new(serve, &serving); Self { + attach: served.attach.clone(), + _served: served, + listed: None, demand: serving.demand(), serving, dynamic, @@ -6053,6 +6203,162 @@ impl ServeLoop { } } + /// Apply a demand change: subscribe upstream for what the listed feed does not carry, + /// which for a track nothing lists is all of it. + fn steer(&mut self, serve: &TrackServe, pref: Option) -> Result<(), ServeEnd> { + let pref = match &self.listed { + None => pref, + // Until TRACK_OK says where the feed starts, nothing says what it leaves out. + Some(Listed { covered: None, .. }) => return Ok(()), + Some(Listed { + covered: Some(covered), .. + }) => { + let covered = *covered; + // A floor below the feed's start, up to it. A subscription already open keeps + // delivering until it reaches the feed, so no group falls between the two. + let gap = pref.and_then(|mut pref| { + if matches!(self.sub, Sub::None) && pref.start().is_none_or(|start| start >= covered) { + return None; + } + let end = pref.end().map_or(covered, |end| end.min(covered)); + pref.set_bounds(pref.start(), Some(end)); + (end > pref.start().unwrap_or_default()).then_some(pref) + }); + match gap { + Some(gap) => Some(gap), + None => { + self.release(serve); + return Ok(()); + } + } + } + }; + match serve.begin_subscription( + &mut self.serving, + &mut self.sub, + pref, + self.supports_update, + self.timescale, + ) { + Ok(Begin::Establish(est)) => self.mode = ServeMode::Establish(est), + Ok(Begin::None) => {} + // Updating the upstream failed: hand the track back for another route to resume. + Err(err) => return Err(ServeEnd::GiveBack(err)), + } + Ok(()) + } + + /// Cancel the upstream subscription beside a listed feed, which keeps the copy live. + fn release(&mut self, serve: &TrackServe) { + let Sub::Active(active) = &mut self.sub else { + return; + }; + serve.subscriber.remove_subscribe(active.id); + let _ = active.stream.writer.finish(); + tracing::debug!(track = %serve.name, "subscribe canceled (listed)"); + self.sub = Sub::None; + // The copy starts where the feed and what the subscription filled cover, which also + // answers a start the subscription still owed. + if let Some(Listed { + covered: Some(covered), .. + }) = &self.listed + { + let _ = self.serving.start_at(covered.group); + } + } + + /// Take a feed handed over, then apply the listed feed's responses. `Ready(Ok)` once + /// the subscription should follow, saying whether the feed stopped with the track still + /// open, or `Ready(Err)` with how the track ends. + fn poll_listed(&mut self, serve: &TrackServe, waiter: &kio::Waiter) -> Poll> { + let mut changed = false; + while let Poll::Ready(Ok(mut feed)) = self.attach.poll_pop(waiter) { + // One SUBSCRIBE_ALL feeds the track at a time: another's groups go unread. + if self.listed.is_none() { + feed.count(self.serving.subscription().as_ref()); + self.listed = Some(Listed { feed, covered: None }); + changed = true; + } + } + while let Some(listed) = &mut self.listed { + match listed.feed.responses.poll_pop(waiter) { + Poll::Ready(Ok(ListedResponse::Ok(start))) => { + self.serving.set_live(start.largest); + if let Some(largest) = start.largest { + self.serving.report_largest(largest); + } + // An open subscription declared its own floor, below the feed's. + if matches!(self.sub, Sub::None) { + let _ = self.serving.start_at(start.group); + } + listed.covered = Some(Position::group(start.group)); + changed = true; + } + Poll::Ready(Ok(ListedResponse::End(end))) => { + // Lower groups may still be on the wire behind the end, so readers wait at + // a hole until the feed's tail settles. + if self.serving.finish_at_pending(Position::group(end.group)).is_err() { + return Poll::Ready(Err(ServeEnd::GiveBack(Error::ProtocolViolation))); + } + if let Ok(mut tail) = listed.feed.tail.write() { + tail.expire(serve.subscriber.runtime.now()); + } + self.mode = ServeMode::Tail { + settle: Settle::new(&serve.subscriber.runtime, listed.feed.tail.consume()), + owed: None, + streams: Some(end.streams), + }; + return Poll::Ready(Ok(false)); + } + Poll::Ready(Ok(ListedResponse::Reset(err))) => return Poll::Ready(Err(ServeEnd::GiveBack(err))), + // The request stopped feeding the track: it carries on like a requested one. + Poll::Ready(Err(_)) => { + self.listed = None; + return Poll::Ready(Ok(true)); + } + Poll::Pending => break, + } + } + match changed { + true => Poll::Ready(Ok(false)), + false => Poll::Pending, + } + } + + /// Ready once a subscription beside the listed feed delivered everything below it, which + /// cancels it: the copy then holds every group from its floor on. + fn poll_filled(&mut self, serve: &TrackServe, waiter: &kio::Waiter) -> Poll<()> { + let ( + Some(Listed { + covered: Some(covered), .. + }), + Sub::Active(active), + ) = (&self.listed, &self.sub) + else { + return Poll::Pending; + }; + let covered = *covered; + let Some(floor) = active.start.or(active.served.map(Position::group)) else { + return Poll::Pending; + }; + let groups = floor.group..covered.group; + let tail = active.tail.consume(); + let filled = tail + .poll(waiter, |tail| match tail.covers(groups.clone()) { + true => Poll::Ready(()), + false => Poll::Pending, + }) + .is_ready(); + if !filled { + return Poll::Pending; + } + if let Some(listed) = &mut self.listed { + listed.covered = Some(floor.min(covered)); + } + self.release(serve); + Poll::Ready(()) + } + fn poll(&mut self, serve: &TrackServe, waiter: &kio::Waiter) -> Poll { loop { match &mut self.mode { @@ -6112,6 +6418,34 @@ impl ServeLoop { // Biased: demand first, then completions, then closures. + // (0) A SUBSCRIBE_ALL's feed of the track: what it says, or a feed handed + // over, moves what the subscription has to carry. + match self.poll_listed(serve, waiter) { + Poll::Ready(Ok(took_over)) => { + if matches!(self.mode, ServeMode::Select) { + let mut pref = self.serving.subscription(); + // A feed that stopped leaves the cache current to its newest group, + // so ask from there rather than from a live edge past a gap. + if took_over + && matches!(self.sub, Sub::None) + && let Some(pref) = &mut pref + && pref.start().is_none() + { + pref.set_bounds(self.serving.latest_sequence().map(Position::group), pref.end()); + } + if let Err(end) = self.steer(serve, pref) { + return Poll::Ready(end); + } + } + continue; + } + Poll::Ready(Err(end)) => return Poll::Ready(end), + Poll::Pending => {} + } + if self.poll_filled(serve, waiter).is_ready() { + continue; + } + // (1) Track demand: a fetch, a subscription change, or the origin // handing the track to another route. @@ -6134,18 +6468,11 @@ impl ServeLoop { } match self.serving.poll_subscription_changed(waiter) { Poll::Ready(Ok(pref)) => { - match serve.begin_subscription( - &mut self.serving, - &mut self.sub, - pref, - self.supports_update, - self.timescale, - ) { - Ok(Begin::Establish(est)) => self.mode = ServeMode::Establish(est), - Ok(Begin::None) => {} - // Updating the upstream failed: hand the track back for - // another route to resume. - Err(err) => return Poll::Ready(ServeEnd::GiveBack(err)), + if let Some(listed) = &mut self.listed { + listed.feed.count(pref.as_ref()); + } + if let Err(end) = self.steer(serve, pref) { + return Poll::Ready(end); } continue; } @@ -6163,7 +6490,8 @@ 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() && self.demand.poll_unused(waiter).is_ready() { + // A listed copy stays for as long as its feed: it is part of the listing. + if self.listed.is_none() && self.fetches.is_empty() && self.demand.poll_unused(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/model/broadcast.rs b/rs/moq-net/src/model/broadcast.rs index b737f208ef0..7c618aae34e 100644 --- a/rs/moq-net/src/model/broadcast.rs +++ b/rs/moq-net/src/model/broadcast.rs @@ -390,6 +390,25 @@ impl Producer { true } + /// Whether `track` is the live track this broadcast serves under its name. + #[cfg_attr(not(feature = "async"), allow(dead_code))] + // pending: /quest/m1/public-model/answer.md + pub(crate) fn serves(&self, track: &track::Producer) -> bool { + use super::WeakEntry; + let mut state = self.state.lock(); + state + .tracks + .get(track.name()) + .is_some_and(|live| live.same_channel(&track.weak())) + } + + /// Whether the broadcast has ended: it takes no new tracks. + #[cfg_attr(not(feature = "async"), allow(dead_code))] + // pending: /quest/m1/public-model/answer.md + pub(crate) fn is_closed(&self) -> bool { + self.state.lock().closing + } + /// Create a track with a unique name using the given suffix. /// /// Uses [`Self::unique_name`]; minted names are never reused, even after closure. diff --git a/rs/moq-net/tests/integration/subscribe_all.rs b/rs/moq-net/tests/integration/subscribe_all.rs index b288a35495c..eae5610a613 100644 --- a/rs/moq-net/tests/integration/subscribe_all.rs +++ b/rs/moq-net/tests/integration/subscribe_all.rs @@ -364,3 +364,255 @@ async fn older_versions_list_nothing() { assert!(moq_clock::timeout(Duration::from_secs(1), cursor.next()).await.is_err()); assert!(!pair.client_transport.bidi_types().contains(&SUBSCRIBE_ALL)); } + +/// The Subscribe Stream's type. +const SUBSCRIBE: u8 = 0x2; + +/// How many SUBSCRIBEs the subscriber side of `link` opened. +fn subscribes(link: &MockPair) -> usize { + link.client_transport + .bidi_types() + .into_iter() + .filter(|kind| *kind == SUBSCRIBE) + .count() +} + +/// Publish `room/a` under an epoch with one timed track, `video`, holding group 0. +fn publish_video(origin: &origin::Producer) -> (broadcast::Producer, track::Producer) { + let broadcast = origin.create_broadcast("room/a").unwrap(); + let video = broadcast.create_track("video", timed()).unwrap(); + write(&video, 0); + broadcast + .announce(origin::Route::default().with_epoch(epochs().0)) + .unwrap(); + (broadcast, video) +} + +/// `room/a` as a request through `origin` resolves it. +async fn request(origin: &origin::Producer) -> broadcast::Consumer { + let consumer = origin.consume(); + moq_clock::timeout(TIMEOUT, consumer.routed("room/a")) + .await + .expect("announce hung") + .expect("routed"); + moq_clock::timeout(TIMEOUT, consumer.request_broadcast("room/a", None)) + .await + .expect("resolve hung") + .expect("broadcast resolves") +} + +/// The first track listed under `room` through `origin`, with the cursor that listed it. +async fn list_video(origin: &origin::Producer) -> (origin::Broadcasts, track::Consumer) { + let consumer = origin.consume(); + moq_clock::timeout(TIMEOUT, consumer.routed("room/a")) + .await + .expect("announce hung"); + let mut cursor = consumer.broadcasts("room").unwrap(); + let listed = next_broadcast(&mut cursor).await; + let track = next_track(&mut listed.tracks()).await; + assert_eq!(track.name(), "video"); + (cursor, track) +} + +async fn subscribe(track: &track::Consumer, subscription: track::Subscription) -> track::Subscriber { + moq_clock::timeout(TIMEOUT, track.subscribe(subscription.with_max_delay(Duration::from_secs(60)))) + .await + .expect("subscribe hung") + .expect("subscribe") +} + +/// Read groups until `sequence` arrives. +async fn read_until(subscriber: &mut track::Subscriber, sequence: u64) { + loop { + let group = moq_clock::timeout(TIMEOUT, subscriber.recv_group()) + .await + .expect("group hung") + .expect("group") + .expect("track ended"); + if group.sequence == sequence { + return; + } + } +} + +/// A track both listed and requested is one copy at every hop: a request reads the +/// listed feed, and nothing is subscribed upstream for it. +#[moq_clock::test] +async fn a_listed_track_serves_requests_too() { + let chain = Chain::new(true).await; + let (_broadcast, video) = publish_video(&chain.publisher); + let (_cursor, listed) = list_video(&chain.subscriber).await; + assert_eq!(read_group(&listed).await, 0); + + // The subscriber asks for the track by name, and so does another downstream of the relay. + let other = produce_origin(4); + let _other_link = link(&chain._relays[0], &other).await; + let mut readers = Vec::new(); + for origin in [&chain.subscriber, &other] { + let track = request(origin).await.track("video").unwrap(); + readers.push(subscribe(&track, Default::default()).await); + } + write(&video, 1); + for reader in &mut readers { + read_until(reader, 1).await; + } + + assert_eq!(subscribes(&chain.links[0]), 0, "the relay subscribed upstream"); + assert_eq!(subscribes(&chain.links[1]), 0, "the subscriber subscribed upstream"); +} + +/// A track a request already subscribed hands over to a SUBSCRIBE_ALL that lists it: the +/// publisher then sends each new group once, not once per request. +#[moq_clock::test] +async fn a_subscribed_track_hands_over_to_its_listing() { + let chain = Chain::new(true).await; + let (_broadcast, video) = publish_video(&chain.publisher); + + let other = produce_origin(4); + let _other_link = link(&chain._relays[0], &other).await; + let track = request(&other).await.track("video").unwrap(); + let mut viewer = subscribe(&track, Default::default()).await; + read_until(&mut viewer, 0).await; + assert_eq!(subscribes(&chain.links[0]), 1); + + let (_cursor, listed) = list_video(&chain.subscriber).await; + let mut lister = subscribe(&listed, Default::default()).await; + write(&video, 1); + read_until(&mut viewer, 1).await; + read_until(&mut lister, 1).await; + // Let the relay retire its own subscription once the feed covers it. + moq_clock::sleep(Duration::from_millis(10)).await; + + let publisher = &chain.links[0].server_transport; + publisher.hold_unis(); + write(&video, 2); + moq_clock::sleep(Duration::from_millis(10)).await; + let streams = 1 + publisher.release_newest_uni(); + publisher.release_unis(); + assert_eq!(streams, 1, "group 2 went upstream once per request"); + read_until(&mut viewer, 2).await; + read_until(&mut lister, 2).await; +} + +/// Write group `sequence` with `frames` frames, each payload naming its position. +fn write_frames(track: &track::Producer, sequence: u64, frames: u64) { + let mut group = track.create_group(moq_net::group::Info { sequence }).unwrap(); + for frame in 0..frames { + let at = Timestamp::from_millis(sequence * 10 + frame).unwrap(); + group + .write_frame(at, format!("{sequence}.{frame}").into_bytes()) + .unwrap(); + } + group.finish().unwrap(); +} + +/// A reader resuming a listed track mid-group reads that group from its frame on, then the +/// live groups. The floor goes upstream as an ordinary subscriber's would, and a relay +/// answers the same whether its cache already holds the group (`warm`) or not. +async fn resumes_mid_group(relay: bool, warm: bool) { + let chain = Chain::new(relay).await; + let broadcast = chain.publisher.create_broadcast("room/a").unwrap(); + let video = broadcast.create_track("video", timed()).unwrap(); + write_frames(&video, 0, 1); + write_frames(&video, 1, 4); + write_frames(&video, 2, 1); + broadcast + .announce(origin::Route::default().with_epoch(epochs().0)) + .unwrap(); + + // Another downstream of the relay already read the whole track through it. + let other = produce_origin(4); + let mut _warmed = None; + if warm { + let link = link(&chain._relays[0], &other).await; + let track = request(&other).await.track("video").unwrap(); + let mut reader = subscribe(&track, track::Subscription::default().with_groups(0..)).await; + read_until(&mut reader, 2).await; + _warmed = Some((link, reader)); + } + let upstream = subscribes(&chain.links[0]); + + let (_cursor, listed) = list_video(&chain.subscriber).await; + let floor = track::Range { + start: track::Anchor::Absolute(track::Position { group: 1, frame: 2 }), + end: None, + }; + let mut resumed = subscribe(&listed, track::Subscription::default().with_ranges([floor])).await; + // In arrival order: the live group is already here, and the floor's group follows. + let mut group = loop { + let group = moq_clock::timeout(TIMEOUT, resumed.recv_group()) + .await + .unwrap_or_else(|_| panic!("relay={relay} warm={warm}: group 1 never arrived")) + .expect("group") + .expect("track ended"); + if group.sequence == 1 { + break group; + } + }; + group.skip_to(2); + let mut frames = Vec::new(); + while let Some(frame) = moq_clock::timeout(TIMEOUT, group.read_frame()) + .await + .expect("frame hung") + .expect("frame") + { + frames.push(String::from_utf8(frame.payload.to_vec()).unwrap()); + } + assert_eq!(frames, ["1.2", "1.3"], "relay={relay} warm={warm}"); + + // The publisher answered the floor, unless the relay's cache already held it. + let asked = subscribes(&chain.links[0]) - upstream; + assert_eq!(asked, usize::from(!warm), "relay={relay} warm={warm}"); +} + +#[moq_clock::test] +async fn resumes_a_listed_track_mid_group() { + resumes_mid_group(false, false).await; +} + +#[moq_clock::test] +async fn resumes_a_listed_track_mid_group_through_a_cold_relay() { + resumes_mid_group(true, false).await; +} + +#[moq_clock::test] +async fn resumes_a_listed_track_mid_group_through_a_warm_relay() { + resumes_mid_group(true, true).await; +} + +/// A SUBSCRIBE_ALL carries what its tracks' readers ask for, updating it as they +/// subscribe, through a relay too. +async fn carries_reader_preferences(relay: bool) { + let chain = Chain::new(relay).await; + let (_broadcast, mut video) = publish_video(&chain.publisher); + let (_cursor, listed) = list_video(&chain.subscriber).await; + let mut reader = subscribe(&listed, track::Subscription::default().with_priority(7)).await; + read_until(&mut reader, 0).await; + + let carried = kio::wait(|waiter| { + loop { + match video.poll_subscription_changed(waiter) { + std::task::Poll::Ready(Ok(Some(subscription))) if subscription.priority == 7 => { + return std::task::Poll::Ready(subscription); + } + std::task::Poll::Ready(Ok(_)) => continue, + std::task::Poll::Ready(Err(err)) => panic!("track aborted: {err}"), + std::task::Poll::Pending => return std::task::Poll::Pending, + } + } + }); + let carried = moq_clock::timeout(TIMEOUT, carried) + .await + .unwrap_or_else(|_| panic!("relay={relay}: the reader's priority never reached the publisher")); + assert_eq!(carried.max_delay, Duration::from_secs(60), "relay={relay}"); +} + +#[moq_clock::test] +async fn carries_reader_preferences_upstream() { + carries_reader_preferences(false).await; +} + +#[moq_clock::test] +async fn carries_reader_preferences_through_a_relay() { + carries_reader_preferences(true).await; +} From 87a3547a7c0bed2ecb1ec8ad4e4e1c40dbd8b214 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 19 Oct 2026 10:30:21 -0700 Subject: [PATCH 12/13] fix(net): lints after merging main Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/lite/publisher.rs | 3 ++- rs/moq-net/src/lite/subscriber.rs | 7 +++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 925e0668264..4727a490a42 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -1,7 +1,7 @@ use crate::{SessionError, announce, frame, group, origin, track}; use rangemap::RangeSet; use std::{ - collections::{BTreeSet, HashMap}, + collections::HashMap, ops::{Bound, ControlFlow, Range}, sync::{ Arc, Mutex, @@ -4380,6 +4380,7 @@ mod serve_group_test { timescale: Some(crate::Timescale::MILLI), runtime: crate::time::sim(), opens: Default::default(), + track: None, }; let track = track::Producer::new( diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 0784acc9baf..6b2b431ffb1 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -98,7 +98,7 @@ pub(super) struct Subscriber { copies: Lock>>, /// Every track a serve loop feeds, by broadcast path and name, for a SUBSCRIBE_ALL /// listing one to join rather than feed a second copy. - served: Lock), Vec>>, + served: ServedLoops, going_away: crate::goaway::GoingAway, auth: crate::auth::Handle, /// What this session may allocate up front for frames still arriving. @@ -5941,6 +5941,9 @@ struct Listed { covered: Option, } +/// The serve loops of a session's requested tracks, by broadcast path and track name. +type ServedLoops = Lock), Vec>>; + /// A track a serve loop feeds, for a SUBSCRIBE_ALL listing it to join. #[derive(Clone)] struct Served { @@ -5950,7 +5953,7 @@ struct Served { /// Keeps a serve loop's track in [`Subscriber::served`] for as long as it runs. struct ServedGuard { - served: Lock), Vec>>, + served: ServedLoops, key: (PathOwned, Arc), attach: kio::Queue, } From 87c893f57fcffa9985743d1cd4fd168cc372b1ab Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 19 Oct 2026 10:37:05 -0700 Subject: [PATCH 13/13] style(net): format Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/lite/publisher.rs | 6 +++++- rs/moq-net/src/lite/subscriber.rs | 7 +------ rs/moq-net/tests/integration/subscribe_all.rs | 11 +++++++---- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 4727a490a42..c8012cf524b 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -5547,7 +5547,11 @@ mod serve_group_test { } let subscriber = track.subscribe(track::Subscription::default()); let (mut run, mut writer, log) = lite_run(version, SinkSession::new(Log::default()), subscriber); - assert!(kio::wait(|waiter| run.poll(&mut writer, waiter)).now_or_never().is_none()); + assert!( + kio::wait(|waiter| run.poll(&mut writer, waiter)) + .now_or_never() + .is_none() + ); kio::wait(|waiter| writer.poll_flush(&mut waiter.context())) .await .unwrap(); diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 6b2b431ffb1..a241b675a5e 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -5641,12 +5641,7 @@ impl TrackServeRun { /// Serve a track a SUBSCRIBE_ALL created, fed by `feed`: the upstream subscription /// carries only what readers ask for below it. - fn listed( - serve: TrackServe, - producer: track::Producer, - timescale: Option, - feed: ListedFeed, - ) -> Self { + fn listed(serve: TrackServe, producer: track::Producer, timescale: Option, feed: ListedFeed) -> Self { let dynamic = producer.dynamic(); let mut serve_loop = ServeLoop::serving(&serve, producer, dynamic, timescale); serve_loop.listed = Some(Listed { feed, covered: None }); diff --git a/rs/moq-net/tests/integration/subscribe_all.rs b/rs/moq-net/tests/integration/subscribe_all.rs index eae5610a613..d6c38533216 100644 --- a/rs/moq-net/tests/integration/subscribe_all.rs +++ b/rs/moq-net/tests/integration/subscribe_all.rs @@ -415,10 +415,13 @@ async fn list_video(origin: &origin::Producer) -> (origin::Broadcasts, track::Co } async fn subscribe(track: &track::Consumer, subscription: track::Subscription) -> track::Subscriber { - moq_clock::timeout(TIMEOUT, track.subscribe(subscription.with_max_delay(Duration::from_secs(60)))) - .await - .expect("subscribe hung") - .expect("subscribe") + moq_clock::timeout( + TIMEOUT, + track.subscribe(subscription.with_max_delay(Duration::from_secs(60))), + ) + .await + .expect("subscribe hung") + .expect("subscribe") } /// Read groups until `sequence` arrives.