From 2796276ce42e50a3679efd44e04519ddf08c5b30 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 30 Oct 2026 01:03:43 -0700 Subject: [PATCH 01/12] chore(quest): claim quest/m2/archive-time-ranges.md Quest-Claim: 9a38224c1bf4daa5 From a13589707f88374b18752fc67bebc1f7c1e6478d Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 30 Oct 2026 01:27:54 -0700 Subject: [PATCH 02/12] wip: archive time ranges --- rs/moq-archive/src/consumer/index.rs | 65 +++++-- rs/moq-archive/src/consumer/mod.rs | 5 +- rs/moq-archive/src/consumer/tests.rs | 125 +++++++++++++ rs/moq-archive/src/consumer/track.rs | 98 +++++++++- rs/moq-net/src/model/subscription.rs | 7 +- rs/moq-net/src/model/subscription/window.rs | 95 ++++++++-- rs/moq-net/src/model/track.rs | 196 +++++++++++++++++++- rs/moq-net/src/model/track/fill.rs | 74 +++++++- rs/moq-net/src/model/track/miss.rs | 10 + 9 files changed, 633 insertions(+), 42 deletions(-) diff --git a/rs/moq-archive/src/consumer/index.rs b/rs/moq-archive/src/consumer/index.rs index a93e50ae47a..34d219a7d76 100644 --- a/rs/moq-archive/src/consumer/index.rs +++ b/rs/moq-archive/src/consumer/index.rs @@ -1,9 +1,9 @@ //! One track's object keys, listed once and extended after its newest key. -use std::collections::BTreeMap; +use std::collections::{BTreeMap, BTreeSet}; use futures::TryStreamExt; -use moq_net::Timescale; +use moq_net::{Timescale, Timestamp}; use object_store::path::Path; use object_store::{ObjectStore, ObjectStoreExt}; @@ -13,11 +13,15 @@ use crate::{Error, Info, Result}; /// Where an object starts: its first sequence and that sequence's first frame index. pub(super) type Start = (u64, u64); +/// One listed object: where it starts, its first frame's timestamp on a timed track, and +/// its key. +pub(super) type Listing = (Start, Option, Path); + /// What one listing of a track's directory found. #[derive(Default)] pub(super) struct Listed { /// The objects new since the cursor, oldest first. - pub objects: Vec<(Start, Path)>, + pub objects: Vec, /// The track's `end`, when asked for. pub end: Option, } @@ -26,7 +30,11 @@ pub(super) struct Listed { pub(super) struct Index { pub dir: Path, pub timescale: Option, - objects: BTreeMap, + /// Each object's first frame timestamp, on a timed track, and key. + objects: BTreeMap, Path)>, + /// Each timed object's first frame timestamp with where it starts, so a time finds the + /// object holding it in one lookup. Timestamps never rewind, so they sort as the starts do. + times: BTreeSet<(u64, Start)>, /// The newest key listed, which the next listing starts after. cursor: Path, } @@ -38,6 +46,7 @@ impl Index { dir, timescale: super::track::timescale(info)?, objects: BTreeMap::new(), + times: BTreeSet::new(), cursor, }) } @@ -52,9 +61,12 @@ impl Index { } /// Take in the objects a listing after [`Self::cursor`] found. - pub fn extend(&mut self, objects: &[(Start, Path)]) { - for (start, path) in objects { - self.objects.insert(*start, path.clone()); + pub fn extend(&mut self, objects: &[Listing]) { + for (start, pts, path) in objects { + self.objects.insert(*start, (*pts, path.clone())); + if let Some(pts) = pts { + self.times.insert((*pts, *start)); + } self.cursor = path.clone(); } } @@ -63,7 +75,7 @@ impl Index { pub fn newest(&self) -> Option<(Start, Path)> { self.objects .last_key_value() - .map(|(start, path)| (*start, path.clone())) + .map(|(start, (_, path))| (*start, path.clone())) } /// The objects holding group `sequence` from frame `frame` on, oldest first: the last one @@ -71,11 +83,11 @@ impl Index { /// object after them, which bounds a gap. pub fn holding(&self, sequence: u64, frame: u64) -> (Vec<(Start, Path)>, Option) { let mut holding = Vec::new(); - if let Some((start, path)) = self.objects.range(..=(sequence, frame)).next_back() { + if let Some((start, (_, path))) = self.objects.range(..=(sequence, frame)).next_back() { holding.push((*start, path.clone())); } let mut after = None; - for (start, path) in self.objects.range((sequence, frame.saturating_add(1))..) { + for (start, (_, path)) in self.objects.range((sequence, frame.saturating_add(1))..) { if start.0 != sequence { after = Some(*start); break; @@ -85,9 +97,36 @@ impl Index { (holding, after) } + /// The newest object whose first frame presents at or before `pts`: the one holding it, + /// if any does. `None` when every object starts after it, or on an untimed track. + pub fn at(&self, pts: Timestamp) -> Option<(Start, Path)> { + // A stored timestamp is whole, so it is at or before `pts` exactly when it is at or + // before `pts` rounded down to the track's timescale. + let limit = u64::try_from(pts.as_scale(self.timescale?)).unwrap_or(u64::MAX); + let (_, start) = self.times.range(..=(limit, (u64::MAX, u64::MAX))).next_back()?; + self.objects.get(start).map(|(_, path)| (*start, path.clone())) + } + + /// The object just before the one starting at `start`. + pub fn before(&self, start: Start) -> Option<(Start, Path)> { + let (start, (_, path)) = self.objects.range(..start).next_back()?; + Some((*start, path.clone())) + } + + /// The object just after the one starting at `start`, or the oldest for `None`. + pub fn after(&self, start: Option) -> Option<(Start, Path)> { + let (start, (_, path)) = match start { + Some(start) => self.objects.range((start.0, start.1.saturating_add(1))..).next()?, + None => self.objects.first_key_value()?, + }; + Some((*start, path.clone())) + } + /// Forget an object the store no longer has, such as one a DVR pruned. pub fn forget(&mut self, start: Start) { - self.objects.remove(&start); + if let Some((Some(pts), _)) = self.objects.remove(&start) { + self.times.remove(&(pts, start)); + } } } @@ -117,7 +156,9 @@ pub(super) async fn list( match Name::parse(filename, timed)? { Name::Info => {} Name::End => found = Some(meta.location), - Name::Object(start) => listed.objects.push(((start.group, start.frame), meta.location)), + Name::Object(start) => listed + .objects + .push(((start.group, start.frame), start.pts, meta.location)), } } listed.objects.sort(); diff --git a/rs/moq-archive/src/consumer/mod.rs b/rs/moq-archive/src/consumer/mod.rs index 5786bcc3ca8..050fba7d028 100644 --- a/rs/moq-archive/src/consumer/mod.rs +++ b/rs/moq-archive/src/consumer/mod.rs @@ -330,7 +330,10 @@ async fn refresh(shared: Arc>, mut tracks: Vec (listed.objects, 0), + true => ( + listed.objects.into_iter().map(|(start, _, path)| (start, path)).collect(), + 0, + ), // Only the newest sequence: fills serve the rest. false => { let Some((start, path)) = track.index.lock().unwrap().newest() else { diff --git a/rs/moq-archive/src/consumer/tests.rs b/rs/moq-archive/src/consumer/tests.rs index 505ad2ad613..8f02f354d1a 100644 --- a/rs/moq-archive/src/consumer/tests.rs +++ b/rs/moq-archive/src/consumer/tests.rs @@ -730,3 +730,128 @@ async fn an_unordered_listing_is_sorted() { } assert_eq!(super::tests::sequence(&mut reader).await, Some(3)); } + +/// Read `range` in order with no age limit: the sequences of the groups it delivers before +/// it stops delivering. A range ending before the live edge parks once read, as on any live +/// track. +async fn read_range(broadcast: &broadcast::Producer, track: &str, range: track::Range) -> Vec { + let track = broadcast.consume().track(track).unwrap(); + let subscription = Subscription::default() + .with_ranges([range]) + .with_order(Order::Ascending) + .with_max_delay(None); + let mut reader = track.subscribe(subscription).await.unwrap().ordered(); + let mut read = Vec::new(); + while let Ok(group) = timeout(5 * REFRESH, reader.next_group()).await { + match group.unwrap() { + Some(group) => read.push(group.sequence), + None => break, + } + } + read +} + +fn at(millis: u64) -> track::Anchor { + track::Anchor::Time(Timestamp::from_millis(millis).unwrap()) +} + +/// Groups `sequences` of a timed track, two to an object, each group starting at its +/// sequence in seconds. +async fn stored(archive: &Archive, sequences: impl IntoIterator) { + let sequences: Vec = sequences.into_iter().collect(); + for pair in sequences.chunks(2) { + let entries = pair.iter().map(|sequence| frames(true, *sequence, 0..3)).collect(); + archive.object("video", 0, entries).await; + } +} + +/// A time range older than the newest group resolves from the archive's keys, not from +/// the one group its cache holds: the start reaches the group containing it, and the end +/// stops just after the group containing it. +#[tokio::test(start_paused = true)] +async fn a_time_range_resolves_from_the_archive() { + let archive = Archive::new(); + archive.track("video", 0, true).await; + stored(&archive, 0..10).await; + + let (broadcast, _run) = serve(&archive.store).await; + let range = |start, end| track::Range { start, end }; + assert_eq!(read_range(&broadcast, "video", range(at(4_500), Some(at(6_500)))).await, [4, 5, 6]); + // A start on the first frame of its group, in the second object of a pair. + assert_eq!(read_range(&broadcast, "video", range(at(3_000), Some(at(3_001)))).await, [3]); + // An end alone, from the oldest group. + assert_eq!( + read_range(&broadcast, "video", range(track::Anchor::Absolute(0), Some(at(1_200)))).await, + [0, 1] + ); + + // A start alone reads on through the live edge. + let track = broadcast.consume().track("video").unwrap(); + let subscription = Subscription::default() + .with_ranges([range(at(7_002), None)]) + .with_order(Order::Ascending) + .with_max_delay(None); + let mut reader = track.subscribe(subscription).await.unwrap().ordered(); + for expected in 7..10 { + assert_eq!(sequence(&mut reader).await, Some(expected)); + } +} + +/// A time older than every stored object reaches back to the oldest stored group. +#[tokio::test(start_paused = true)] +async fn a_time_before_the_archive_starts_at_its_oldest_group() { + let archive = Archive::new(); + archive.track("video", 0, true).await; + // Groups 0 to 2 were pruned. + stored(&archive, 3..8).await; + + let (broadcast, _run) = serve(&archive.store).await; + let range = track::Range { + start: at(500), + end: Some(at(4_500)), + }; + assert_eq!(read_range(&broadcast, "video", range).await, [3, 4]); +} + +/// A time in a gap the archive never stored starts at the group before the gap, the newest +/// starting before it, and the gap reads as missing. +#[tokio::test(start_paused = true)] +async fn a_time_in_a_gap_starts_before_it() { + let archive = Archive::new(); + archive.track("video", 0, true).await; + stored(&archive, [0, 1, 2, 3, 6, 7, 8, 9]).await; + + let (broadcast, _run) = serve(&archive.store).await; + let range = track::Range { + start: at(4_500), + end: Some(at(6_500)), + }; + assert_eq!(read_range(&broadcast, "video", range).await, [3, 6]); +} + +/// An untimed track refuses a time range rather than looking it up. +#[tokio::test(start_paused = true)] +async fn an_untimed_track_refuses_a_time_range() { + let archive = Archive::new(); + archive.track("log", 0, false).await; + for sequence in 0..4 { + archive.object("log", 0, vec![frames(false, sequence, 0..1)]).await; + } + + let (broadcast, _run) = serve(&archive.store).await; + let track = broadcast.consume().track("log").unwrap(); + let subscription = Subscription::default() + .with_ranges([track::Range { + start: at(1_000), + end: None, + }]) + .with_max_delay(None); + let result = match track.subscribe(subscription).await { + Ok(subscriber) => timeout(Duration::from_secs(10), subscriber.ordered().next_group()) + .await + .expect("the read is refused") + .map(|group| group.map(|group| group.sequence)), + Err(err) => Err(err), + }; + assert!(matches!(result, Err(moq_net::Error::TimestampMismatch)), "{result:?}"); +} diff --git a/rs/moq-archive/src/consumer/track.rs b/rs/moq-archive/src/consumer/track.rs index 914afc5b5dd..c0003eb236f 100644 --- a/rs/moq-archive/src/consumer/track.rs +++ b/rs/moq-archive/src/consumer/track.rs @@ -164,6 +164,9 @@ impl Live { /// Serve `fill` from the archive, a group at a time in the fill's order, until every group is /// answered or nobody waits on it. pub(super) async fn serve(shared: Arc>, index: Arc>, fill: Fill) { + if let Some(pts) = fill.time() { + return look_up(&shared, &index, &fill, pts).await; + } let mut last = None; while fill.is_used() && let Some(sequence) = fill.next() @@ -174,6 +177,90 @@ pub(super) async fn serve(shared: Arc>, index: Arc(shared: &Shared, index: &Mutex, fill: &Fill, pts: Timestamp) { + let (at, timed, timescale) = { + let index = index.lock().unwrap(); + (index.at(pts), index.timed(), index.timescale) + }; + let Some(timescale) = timescale else { + // A reader's time start on an untimed track is refused before it is looked up. + return; + }; + let limit = pts.as_scale(timescale); + + // Back from the object holding it to the newest group starting at or before it: its + // own when the object continues one, since every frame presents after its group's first. + let mut next = at.clone(); + while let Some((start, path)) = next { + let Some(object) = load(shared, index, fill, start, &path, timed).await else { + return; + }; + let found = object + .entries + .iter() + .enumerate() + .filter_map(|(position, entry)| match entry { + Entry::Group { sequence, frames } => { + let first = frames.first()?; + let started = position == 0 && object.frame_start > 0 + || first.timestamp.is_some_and(|at| u128::from(at) <= limit); + started.then_some(*sequence) + } + Entry::Datagram { .. } => None, + }) + .last(); + if let Some(sequence) = found { + return serve_group(shared, index, fill, sequence).await; + } + next = index.lock().unwrap().before(start); + } + + // Every group starts after it: the oldest group is where its range reaches back to. + let mut next = index.lock().unwrap().after(at.map(|(start, _)| start)); + while let Some((start, path)) = next { + let Some(object) = load(shared, index, fill, start, &path, timed).await else { + return; + }; + let found = object.entries.iter().find_map(|entry| match entry { + Entry::Group { sequence, frames } if !frames.is_empty() => Some(*sequence), + _ => None, + }); + if let Some(sequence) = found { + return serve_group(shared, index, fill, sequence).await; + } + next = index.lock().unwrap().after(Some(start)); + } +} + +/// The object a lookup searches, `None` once nobody waits on the lookup or the store failed. +async fn load( + shared: &Shared, + index: &Mutex, + fill: &Fill, + start: (u64, u64), + path: &object_store::path::Path, + timed: bool, +) -> Option> { + let unused = std::pin::pin!(fill.unused()); + let load = std::pin::pin!(super::object(shared, path, start, timed)); + match future::select(unused, load).await { + Either::Left(_) => None, + Either::Right((Ok(object), _)) => Some(object), + Either::Right((Err(err), _)) => { + if let Error::NotFound(_) = err { + // Pruned since it was listed. + index.lock().unwrap().forget(start); + } + tracing::warn!(%path, %err, "archived object is unavailable for a time lookup"); + None + } + } +} + /// Answer group `sequence` of `fill` from the objects holding it, drop the run of sequences /// around it that the archive does not hold, or refuse it for now when the store fails. async fn serve_group(shared: &Shared, index: &Mutex, fill: &Fill, sequence: u64) { @@ -184,10 +271,17 @@ async fn serve_group(shared: &Shared, index: &Mutex, f (holding, after, index.timed(), index.timescale) }; let next = after.map_or(u64::MAX, |start| start.0); + // A lookup owes no group, so it waits on its readers rather than on this one's. + let lookup = fill.time().is_some(); let mut objects = Vec::with_capacity(holding.len()); for (start, path) in &holding { - let unwanted = std::pin::pin!(fill.unwanted(sequence)); + let unwanted = std::pin::pin!(async { + match lookup { + true => fill.unused().await, + false => fill.unwanted(sequence).await, + } + }); let load = std::pin::pin!(super::object(shared, path, *start, timed)); let loaded = match future::select(unwanted, load).await { // Every reader narrowed past it: the fill moves on to the groups it still owes. @@ -272,7 +366,7 @@ async fn serve_group(shared: &Shared, index: &Mutex, f } } - if !fill.is_wanted(sequence) { + if !lookup && !fill.is_wanted(sequence) { return; } let Ok(mut producer) = fill.accept(sequence, None) else { diff --git a/rs/moq-net/src/model/subscription.rs b/rs/moq-net/src/model/subscription.rs index 1fe7d5ebc6e..acf6980b171 100644 --- a/rs/moq-net/src/model/subscription.rs +++ b/rs/moq-net/src/model/subscription.rs @@ -135,8 +135,11 @@ pub enum Anchor { /// The group containing this presentation time: the newest one starting at or before it. /// /// Resolved from the starts of the groups the serving hop holds. When none starts at - /// or before it, a start reaches back to the oldest group, and while no group has - /// started at all it waits like `Relative(0)`. An end settles once a group starting + /// or before it, a hop whose [`Requests`](crate::track::Requests) handler serves its + /// history looks the start up there (see [`Fill::time`](crate::track::Fill::time)), + /// reading nothing until it answers; otherwise, or when the handler can't tell, a start + /// reaches back to the oldest group. While no group has started at all it waits like + /// `Relative(0)`. An end settles once a group starting /// after it is known and every sequence below that one is accounted for (started, /// dropped, or never to start). Until then reads stop at the first held group that /// may still start, so at the live edge at most that one group past the end is read diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index 2fc66d49297..973313ec85e 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -33,6 +33,14 @@ pub(in crate::model) trait Holdings { false } + /// Whether a [`Requests`](crate::track::Requests) handler serving the track's history + /// may look up a time its groups can't resolve: a time start before every group with a + /// known start then waits on that lookup, and a sequence below where the feed starts may + /// still arrive from that history. + fn looks_up(&self) -> bool { + false + } + /// Where `pts` falls among the groups with a known start. fn containing(&self, pts: Timestamp) -> Containing; @@ -139,6 +147,9 @@ struct Span { /// The lowest group a miss asks for. A time start before every group held asks none /// below the oldest: which of them it reaches is unknown until they start. reach: u64, + /// A time start before every group held, looked up from the track's history: the span + /// reads nothing until the lookup answers (see [`Window::found`]). + looking: Option, } impl Span { @@ -359,7 +370,7 @@ impl Window { return !self.pending; } let mut moved = false; - for span in &mut self.spans { + for span in self.spans.iter_mut().filter(|span| span.looking.is_none()) { let End::Time(time) = &mut span.end else { continue }; let limit = time.limit; if let Some(end) = time.advance(span.start, holdings) { @@ -378,10 +389,13 @@ impl Window { /// The groups an unsettled time end reads on speculation, which may start past it: a /// read parked on the cap wakes when one of them starts. pub fn speculative(&self) -> impl Iterator + '_ { - self.spans.iter().filter_map(|span| match span.end { - End::Time(time) => time.speculative, - _ => None, - }) + self.spans + .iter() + .filter(|span| span.looking.is_none()) + .filter_map(|span| match span.end { + End::Time(time) => time.speculative, + _ => None, + }) } /// The lowest sequence whose start could still move an unsettled time end, `None` @@ -389,6 +403,7 @@ impl Window { pub fn watch_starts(&self) -> Option { self.spans .iter() + .filter(|span| span.looking.is_none()) .filter_map(|span| match span.end { End::Time(time) => time.unknown(span.start), _ => None, @@ -464,12 +479,13 @@ impl Window { } } - /// Each range resolved, in the order given: `None` for one empty as given or still - /// waiting on a first group. One that only resolves empty keeps its span, ending at or - /// below its start. An unsettled time end is reported open, never at its cap. + /// Each range resolved, in the order given: `None` for one empty as given, still + /// waiting on a first group, or still looking up its start. One that only resolves + /// empty keeps its span, ending at or below its start. An unsettled time end is reported + /// open, never at its cap. pub fn resolved(&self) -> Vec> { let mut resolved = vec![None; self.ranges.len()]; - for span in &self.spans { + for span in self.spans.iter().filter(|span| span.looking.is_none()) { resolved[span.index] = Some(Resolved { start: span.start, end: match span.end { @@ -497,7 +513,12 @@ impl Window { /// The exclusive end past every group in range, `None` when one stays open. An /// unsettled time end counts as open: its cap only holds reads back meanwhile. pub fn end(&self) -> Option { - if self.pending || self.spans.iter().any(|span| matches!(span.end, End::Time(_))) { + if self.pending + || self + .spans + .iter() + .any(|span| span.looking.is_some() || matches!(span.end, End::Time(_))) + { return None; } match self.groups.iter().next_back() { @@ -558,6 +579,41 @@ impl Window { Some((*run.start()).max(range.start)..end) } + /// The time starts still looked up, each with the exclusive end of the run below every + /// group held, which holds the group containing it if the track's history does. + pub fn lookups(&self) -> impl Iterator + '_ { + self.spans + .iter() + .filter_map(|span| span.looking.map(|pts| (pts, span.reach))) + } + + /// The lookup of time start `pts` answered with the group containing it, or the oldest + /// group of the history when `pts` is older than all of it: its spans start there, once + /// that group has started. `None`, or an answer that never started, means the history + /// couldn't tell, so they reach back to the oldest group held. Returns whether the groups + /// in range moved. + pub fn found(&mut self, pts: Timestamp, group: Option, holdings: &impl Holdings) -> bool { + let mut moved = false; + for span in self.spans.iter_mut().filter(|span| span.looking == Some(pts)) { + span.looking = None; + moved = true; + let started = group.filter(|group| holdings.next_start(*group).is_some_and(|(found, _)| found == *group)); + (span.start, span.floored, span.reach) = match started { + Some(group) => (group, true, group), + None => (0, false, holdings.next_start(0).map_or(span.reach, |(oldest, _)| oldest)), + }; + // A time end walked from group 0 meanwhile starts its walk again at the start. + if let Some(Anchor::Time(end)) = self.ranges[span.index].end { + let mut time = TimeEnd::new(end, span.start); + span.end = time.advance(span.start, holdings).map_or(End::Time(time), End::Before); + } + } + if moved { + self.rebuild(); + } + moved + } + fn place(&mut self, latest: Option, holdings: &impl Holdings) -> Result<()> { let time = |anchor: Option| matches!(anchor, Some(Anchor::Time(_))); if !holdings.timed() @@ -586,9 +642,12 @@ impl Window { } end => end, }; - let reach = match range.start { - Anchor::Time(_) if !floored => holdings.next_start(0).map_or(start, |(oldest, _)| oldest), - _ => start, + let (reach, looking) = match range.start { + Anchor::Time(pts) if !floored => ( + holdings.next_start(0).map_or(start, |(oldest, _)| oldest), + holdings.looks_up().then_some(pts), + ), + _ => (start, None), }; self.spans.push(Span { index, @@ -596,6 +655,7 @@ impl Window { end, floored, reach, + looking, }); } self.rebuild(); @@ -603,10 +663,15 @@ impl Window { } fn rebuild(&mut self) { - self.settled = !self.pending && !self.spans.iter().any(|span| matches!(span.end, End::Time(_))); + self.settled = !self.pending + && !self + .spans + .iter() + .any(|span| span.looking.is_some() || matches!(span.end, End::Time(_))); self.groups = RangeInclusiveSet::new(); self.missable = RangeInclusiveSet::new(); - for span in &self.spans { + // A span still looking up its start reads nothing until it knows which group that is. + for span in self.spans.iter().filter(|span| span.looking.is_none()) { let last = match span.limit() { Some(end) => match end.checked_sub(1) { Some(last) if last >= span.start => last, diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index a60081f0023..42597ecddfd 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -737,6 +737,11 @@ impl Holdings for TrackState { self.received && !self.anchored } + fn looks_up(&self) -> bool { + // A received copy's handler is its session, which fetches by group alone. + !self.received && self.fills.read().has_handlers() + } + fn containing(&self, pts: Timestamp) -> Containing { let starts = self.starts.lock().unwrap_or_else(PoisonError::into_inner); let (Some((&first, &first_at)), Some((&last, &last_at))) = (starts.first_key_value(), starts.last_key_value()) @@ -781,7 +786,7 @@ impl Holdings for TrackState { fn gone(&self, sequence: u64) -> Option { // Without a declared start, the first arrival stands in for it, as the tail's does. let floor = self.start_sequence.or(self.arrivals.first()).unwrap_or(0); - if sequence < floor { + if sequence < floor && !self.looks_up() { return Some(floor); } self.drops @@ -795,6 +800,50 @@ impl Holdings for TrackState { } } +/// Where a cursor's groups start, for resolving its time anchors: its track's, with the +/// runs its own fills refused or it gave up counted gone, so a time end walking history a +/// handler serves passes what that history lacks. +struct Seen<'a> { + state: &'a TrackState, + misses: Option<&'a miss::Misses>, +} + +impl Holdings for Seen<'_> { + fn timed(&self) -> bool { + self.state.timed() + } + + fn drops_anchors(&self) -> bool { + self.state.drops_anchors() + } + + fn looks_up(&self) -> bool { + self.state.looks_up() + } + + fn containing(&self, pts: Timestamp) -> Containing { + self.state.containing(pts) + } + + fn next_start(&self, from: u64) -> Option<(u64, Timestamp)> { + self.state.next_start(from) + } + + fn next_held(&self, from: u64) -> Option<(u64, Held)> { + self.state.next_held(from) + } + + fn gone(&self, sequence: u64) -> Option { + self.state + .gone(sequence) + .or_else(|| self.misses?.gone(sequence)) + } + + fn complete(&self) -> bool { + self.state.complete() + } +} + impl TrackState { /// The upstream publisher's Largest Object, unless a frame or group received since its /// report outdates it, even one the cache has since evicted. @@ -5210,6 +5259,7 @@ fn queue(fills: &kio::Shared, sequence: u64, request: &FetchOptions) // One group either way, so it never turns a wider fill it joins newest first. order: Order::Ascending, }, + time: None, }; // The handler gate is atomic with a handler dropping (no fill stranded on a queue // nobody drains); with no handler, `Fetching::poll` fails fast. @@ -5865,6 +5915,15 @@ struct Cursor { drops: Option>, /// What this cursor misses and asks fills for, once it first reads. misses: Option>, + /// The time starts its window looks up from the track's history, each asked once. + lookups: Vec, +} + +/// A time start a [`Cursor`]'s window looks up through a fill. +struct Lookup { + pts: Timestamp, + /// The fill looking it up, `None` once its answer was taken in. + join: Option, } struct Peeked { @@ -6077,6 +6136,7 @@ impl Cursor { skipped: Skipped::default(), drops: None, misses: None, + lookups: Vec::new(), } } @@ -6164,6 +6224,7 @@ impl Cursor { Ok(window) => { self.filter.window = window; self.filter.refused = None; + self.lookups.clear(); } Err(err) => self.filter.refused = Some(err), } @@ -6174,7 +6235,13 @@ impl Cursor { // the first to arrive, or the newest once it went live, however many have arrived // or left since. None if self.filter.window.is_pending() => self.filter.window.settle(state.first_live(), &*state), - None => self.filter.window.settle(state.latest_live(), &*state), + None => { + let seen = Seen { + state: &state, + misses: self.misses.as_deref(), + }; + self.filter.window.settle(state.latest_live(), &seen) + } }; drop(state); if moved { @@ -6473,10 +6540,72 @@ impl Cursor { })) } + /// Look up each time start the window can't resolve from the cache, once, and take in + /// the answers, parking `waiter` on the lookups still out. + fn look_up(&mut self, asking: &miss::Asking, waiter: &kio::Waiter) { + let lookups: Vec<(Timestamp, u64)> = self.filter.window.lookups().collect(); + if lookups.is_empty() && self.lookups.iter().all(|lookup| lookup.join.is_none()) { + return; + } + let fills = self.state.read().fills.clone(); + for (pts, reach) in lookups { + if self.lookups.iter().any(|lookup| lookup.pts == pts) { + continue; + } + let ask = fill::Ask { + start: Position::group(0), + end: reach, + frame_end: None, + asking: *asking, + time: Some(pts), + }; + let (joins, _) = fills.lock().join(&fills, &ask); + self.lookups.push(Lookup { + pts, + join: joins.into_iter().next(), + }); + } + let mut moved = false; + for lookup in &mut self.lookups { + // The window stops looking once replaced; with no join, nothing can answer. + if !self.filter.window.lookups().any(|(pts, _)| pts == lookup.pts) { + lookup.join = None; + continue; + } + let answer = match &lookup.join { + None => None, + Some(join) => match join + .outcome() + .poll(waiter, |outcome| outcome.first_accepted().map_or(Poll::Pending, Poll::Ready)) + { + Poll::Ready(Ok(group)) => Some(group), + // Ended unanswered: its handler couldn't tell. + Poll::Ready(Err(closed)) => closed.first_accepted(), + Poll::Pending => continue, + }, + }; + let state = self.state.read(); + // The answer's first frame says whether it starts at or before the time. + if let Some(group) = answer + && let Some(slot) = state.lookup.get(&group) + && slot.start().is_none() + && slot.may_start() + { + let _ = slot.serving().poll_started(waiter); + continue; + } + lookup.join = None; + moved |= self.filter.window.found(lookup.pts, answer, &*state); + } + if moved { + self.filter.revision += 1; + self.update_servable(); + } + } + /// Ask fills for what this cursor misses from `from`, and take in what they answered, /// parking `waiter` on the fills still out. Whatever it gives up goes to the drop cursor. fn probe(&mut self, from: u64, drift: &Drift, waiter: &kio::Waiter) { - let (window, cap, revision) = (&self.filter.window, self.end_sequence, self.filter.revision); let asking = { let subscription = self.subscription.read(); miss::Asking { @@ -6485,6 +6614,8 @@ impl Cursor { order: subscription.order, } }; + self.look_up(&asking, waiter); + let (window, cap, revision) = (&self.filter.window, self.end_sequence, self.filter.revision); let fills = self.state.read().fills.clone(); let misses = self.misses.get_or_insert_with(Box::default); let mut given_up = Vec::new(); @@ -6527,6 +6658,11 @@ impl Cursor { drops.give_up(run); } } + // A refusal taken in may move a time end walking history past it, widening what + // is missed: the read goes again to ask for it. + if !self.filter.window.is_settled() && self.sync() { + waiter.waker().wake_by_ref(); + } } /// The next group a fill answered for this cursor in its order, cached and not handed @@ -18443,9 +18579,10 @@ mod test { assert!(fill.poll_unused(&waiter).is_pending(), "the fill still owes 1 and up"); } - /// A time start before every group held asks no fill below the oldest, since which of - /// those groups it reaches is unknown until they start; another range still asks for its - /// own misses. + /// A time start before every group held looks up its group through a fill that owes + /// none: a handler that can't resolve times drops it, and the start reaches back to the + /// oldest group held, asking no fill below it, since which of those groups it reaches is + /// unknown until they start. Another range still asks for its own misses. #[test] #[cfg(feature = "async")] fn a_time_start_before_the_cache_asks_nothing_below_it() { @@ -18464,13 +18601,20 @@ mod test { producer.start_at(10).unwrap(); write_group(&producer, 10); let waiter = kio::Waiter::noop(); + assert_eq!(drain_groups(&mut subscriber), Vec::::new(), "the time start waits on its lookup"); + let Poll::Ready(Ok(lookup)) = dynamic.poll_requested_fill(&waiter) else { + panic!("the time start is looked up"); + }; + assert_eq!((lookup.time(), lookup.start().group, lookup.end()), (Some(at(5)), 0, 10)); + assert_eq!(lookup.next(), None, "a lookup owes no group"); + drop(lookup); let mut read = drain_groups(&mut subscriber); let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) else { panic!("the group range misses its groups"); }; assert_eq!( - (fill.start().group, fill.end()), - (0, 2), + (fill.time(), fill.start().group, fill.end()), + (None, 0, 2), "nothing below 10 for the time start" ); while let Some(sequence) = fill.next() { @@ -18482,6 +18626,42 @@ mod test { assert!(dynamic.poll_requested_fill(&waiter).is_pending()); } + /// A time start before every group held starts at the group its lookup answers, and a + /// time end walks the history below the feed's start through fills, stopping just past + /// the group containing it. + #[test] + #[cfg(feature = "async")] + fn a_looked_up_time_range_reads_the_history() { + let mut producer = track_producer("test", Info::timed()); + let dynamic = producer.requests(); + let at = |millis| Timestamp::from_millis(millis).unwrap(); + let mut subscriber = producer.subscribe(replay().with_ranges([Range { + start: Anchor::Time(at(5)), + end: Some(Anchor::Time(at(7))), + }])); + producer.start_at(10).unwrap(); + write_group(&producer, 10); + let waiter = kio::Waiter::noop(); + assert_eq!(drain_groups(&mut subscriber), Vec::::new(), "nothing reads until it is found"); + let Poll::Ready(Ok(lookup)) = dynamic.poll_requested_fill(&waiter) else { + panic!("the time start is looked up"); + }; + assert_eq!(lookup.time(), Some(at(5))); + write_filled(lookup.accept(5, None), 5); + drop(lookup); + let mut read = drain_groups(&mut subscriber); + // One group at a time past the start, until one starts after the end. + while let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) { + assert_eq!(fill.time(), None); + while let Some(sequence) = fill.next() { + write_filled(fill.accept(sequence, None), sequence); + } + drop(fill); + read.extend(drain_groups(&mut subscriber)); + } + assert_eq!(read, [5, 6, 7]); + } + /// A wide fill refused group by group, each with its own error, holds only the answers its /// readers have yet to take, not one per refusal. #[test] diff --git a/rs/moq-net/src/model/track/fill.rs b/rs/moq-net/src/model/track/fill.rs index c529bd39465..f9ae48cc5e7 100644 --- a/rs/moq-net/src/model/track/fill.rs +++ b/rs/moq-net/src/model/track/fill.rs @@ -21,7 +21,7 @@ use std::{ use rangemap::{RangeMap, RangeSet}; use super::{Info, Order, Position, TrackState, clamp_max_delay}; -use crate::{Error, Result, StreamError, group, util::SendCell}; +use crate::{Error, Result, StreamError, Timestamp, group, util::SendCell}; /// The fills a track's readers asked for, queued or taken by a handler, under one lock. #[derive(Default)] @@ -44,6 +44,8 @@ struct Pending { end: u64, // Exclusive frame cap in the last group; None asks through the original end. frame_end: Option, + // The time a lookup asks for, which owes no group. + time: Option, // What its joins ask with now, shared with them and its handler. asks: kio::Shared, queued: bool, @@ -198,6 +200,11 @@ impl Outcome { self.accepted.contains(&sequence) } + /// The lowest group accepted: a lookup's answer. + pub(super) fn first_accepted(&self) -> Option { + self.accepted.iter().next().map(|run| run.start) + } + /// The partial copy accepted for `sequence`, if it still holds `frames`. pub(super) fn partial(&self, sequence: u64, frames: group::Bounds) -> Option { self.partial.get(&sequence).filter(|copy| copy.covers(frames)).cloned() @@ -267,6 +274,8 @@ pub(super) struct Ask { pub end: u64, pub frame_end: Option, pub asking: Asking, + /// A lookup of the group holding this time within the run, which owes none of it. + pub time: Option, } /// A reader's place in one fill: the part of it the reader wants. @@ -398,6 +407,15 @@ impl FillState { if span.is_empty() { return (joins, unfillable); } + if let Some(time) = ask.time { + // A lookup joins nothing and nothing joins it: it owes no group, so no other + // reader's run is answered through it. + match self.handlers { + 0 => unfillable.push(span), + _ => joins.push(self.queue_lookup(shared, ask, time)), + } + return (joins, unfillable); + } let covered: Vec<(ops::Range, u64)> = self .covers .overlapping(&span) @@ -493,6 +511,7 @@ impl FillState { start, end: groups.end, frame_end: if groups.end == ask.end { ask.frame_end } else { None }, + time: None, asks, queued: true, wants: RangeMap::from_iter([(groups.clone(), 1)]), @@ -504,6 +523,41 @@ impl FillState { (joins, unfillable) } + /// Queue a fill looking up `time` within `ask`'s run, with the join waiting on its answer. + fn queue_lookup(&mut self, shared: &kio::Shared, ask: &Ask, time: Timestamp) -> Join { + let id = self.next; + self.next += 1; + let result = kio::Producer::::default(); + let mut asks = Asks::default(); + asks.add(&ask.asking); + let asks = kio::Shared::new(asks); + let join = Join { + fills: shared.clone(), + id, + groups: ask.start.group..ask.end, + held: RangeSet::new(), + outcome: Some(result.consume()), + attempt: result.weak(), + asking: ask.asking, + asks: asks.clone(), + }; + self.pending.insert( + id, + Pending { + start: ask.start, + end: ask.end, + frame_end: None, + time: Some(time), + asks, + queued: true, + wants: RangeMap::new(), + result, + }, + ); + self.queue.push_back(id); + join + } + /// Count a join out of reading `groups` of fill `id`. What nobody reads any more is no /// longer routed to it: a reader wanting it later asks afresh rather than joining a fetch /// being cancelled, so a sequence a fill stops owing stays unowed. Its answers are @@ -610,7 +664,7 @@ pub(super) fn poll_requested( }) { let id = guard.pop().expect("predicate guaranteed a fill"); let pending = guard.pending.get(&id).expect("popped fill is pending"); - let (start, end, frame_end) = (pending.start, pending.end, pending.frame_end); + let (start, end, frame_end, time) = (pending.start, pending.end, pending.frame_end, pending.time); let (asks, result) = (pending.asks.clone(), pending.result.clone()); drop(guard); // Built with the fills unlocked, so no other lock nests under theirs. @@ -621,6 +675,7 @@ pub(super) fn poll_requested( start, end, frame_end, + time, asks, result, }; @@ -667,6 +722,7 @@ struct Inner { end: u64, // Exclusive frame cap in the last group; None asks through the original end. frame_end: Option, + time: Option, asks: kio::Shared, result: kio::Producer, } @@ -688,6 +744,20 @@ impl Fill { self.inner.end } + /// The presentation time a reader's range starts at that no group the cache holds + /// resolves, so this fill looks it up: [`accept`](Self::accept) the group containing it, + /// the newest one starting at or before it, or the oldest group when every group starts + /// after it. The reader's range then starts there, once that group's first frame is + /// written. + /// + /// A lookup owes no group, so [`next`](Self::next) is `None` and [`is_wanted`](Self::is_wanted) + /// false throughout; it waits only on its first accept, while [`is_used`](Self::is_used). + /// Dropped unanswered, as a handler that can't resolve times does, the reader's range + /// reaches back to the oldest group the cache holds. `None` for a fill of groups. + pub fn time(&self) -> Option { + self.inner.time + } + /// The most urgent delivery priority among the readers waiting on it now. pub fn priority(&self) -> i8 { self.inner.asks.read().priority() diff --git a/rs/moq-net/src/model/track/miss.rs b/rs/moq-net/src/model/track/miss.rs index 6bc2dc064ec..db350a650a3 100644 --- a/rs/moq-net/src/model/track/miss.rs +++ b/rs/moq-net/src/model/track/miss.rs @@ -190,6 +190,15 @@ impl Misses { &self.given_up } + /// The exclusive end of the run from `sequence` this reader's fills refused or it gave + /// up, `None` when it may still be read. + pub fn gone(&self, sequence: u64) -> Option { + [&self.refused, &self.given_up] + .into_iter() + .find_map(|runs| runs.get(&sequence)) + .map(|run| run.end) + } + /// The next answered group still cached in `order`, forgetting one evicted before it was /// read. pub fn next_filled( @@ -666,6 +675,7 @@ impl Misses { end: run.end, frame_end: None, asking: *asking, + time: None, }; let (joins, unfillable) = fills.lock().join(fills, &ask); self.wanted.insert(run.clone()); From c4941dcc1127977a1d955272720bb6d05258a85c Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 30 Oct 2026 01:31:44 -0700 Subject: [PATCH 03/12] wip: fmt --- rs/moq-archive/src/consumer/mod.rs | 6 ++++- rs/moq-archive/src/consumer/tests.rs | 10 ++++++-- rs/moq-net/src/model/subscription/window.rs | 6 ++++- rs/moq-net/src/model/track.rs | 28 +++++++++++++-------- 4 files changed, 36 insertions(+), 14 deletions(-) diff --git a/rs/moq-archive/src/consumer/mod.rs b/rs/moq-archive/src/consumer/mod.rs index 050fba7d028..d0988ec55d5 100644 --- a/rs/moq-archive/src/consumer/mod.rs +++ b/rs/moq-archive/src/consumer/mod.rs @@ -331,7 +331,11 @@ async fn refresh(shared: Arc>, mut tracks: Vec ( - listed.objects.into_iter().map(|(start, _, path)| (start, path)).collect(), + listed + .objects + .into_iter() + .map(|(start, _, path)| (start, path)) + .collect(), 0, ), // Only the newest sequence: fills serve the rest. diff --git a/rs/moq-archive/src/consumer/tests.rs b/rs/moq-archive/src/consumer/tests.rs index 8f02f354d1a..c75b3941d31 100644 --- a/rs/moq-archive/src/consumer/tests.rs +++ b/rs/moq-archive/src/consumer/tests.rs @@ -776,9 +776,15 @@ async fn a_time_range_resolves_from_the_archive() { let (broadcast, _run) = serve(&archive.store).await; let range = |start, end| track::Range { start, end }; - assert_eq!(read_range(&broadcast, "video", range(at(4_500), Some(at(6_500)))).await, [4, 5, 6]); + assert_eq!( + read_range(&broadcast, "video", range(at(4_500), Some(at(6_500)))).await, + [4, 5, 6] + ); // A start on the first frame of its group, in the second object of a pair. - assert_eq!(read_range(&broadcast, "video", range(at(3_000), Some(at(3_001)))).await, [3]); + assert_eq!( + read_range(&broadcast, "video", range(at(3_000), Some(at(3_001)))).await, + [3] + ); // An end alone, from the oldest group. assert_eq!( read_range(&broadcast, "video", range(track::Anchor::Absolute(0), Some(at(1_200)))).await, diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index 973313ec85e..d73a84df343 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -600,7 +600,11 @@ impl Window { let started = group.filter(|group| holdings.next_start(*group).is_some_and(|(found, _)| found == *group)); (span.start, span.floored, span.reach) = match started { Some(group) => (group, true, group), - None => (0, false, holdings.next_start(0).map_or(span.reach, |(oldest, _)| oldest)), + None => ( + 0, + false, + holdings.next_start(0).map_or(span.reach, |(oldest, _)| oldest), + ), }; // A time end walked from group 0 meanwhile starts its walk again at the start. if let Some(Anchor::Time(end)) = self.ranges[span.index].end { diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 42597ecddfd..820c96ab886 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -834,9 +834,7 @@ impl Holdings for Seen<'_> { } fn gone(&self, sequence: u64) -> Option { - self.state - .gone(sequence) - .or_else(|| self.misses?.gone(sequence)) + self.state.gone(sequence).or_else(|| self.misses?.gone(sequence)) } fn complete(&self) -> bool { @@ -6574,10 +6572,9 @@ impl Cursor { } let answer = match &lookup.join { None => None, - Some(join) => match join - .outcome() - .poll(waiter, |outcome| outcome.first_accepted().map_or(Poll::Pending, Poll::Ready)) - { + Some(join) => match join.outcome().poll(waiter, |outcome| { + outcome.first_accepted().map_or(Poll::Pending, Poll::Ready) + }) { Poll::Ready(Ok(group)) => Some(group), // Ended unanswered: its handler couldn't tell. Poll::Ready(Err(closed)) => closed.first_accepted(), @@ -18601,11 +18598,18 @@ mod test { producer.start_at(10).unwrap(); write_group(&producer, 10); let waiter = kio::Waiter::noop(); - assert_eq!(drain_groups(&mut subscriber), Vec::::new(), "the time start waits on its lookup"); + assert_eq!( + drain_groups(&mut subscriber), + Vec::::new(), + "the time start waits on its lookup" + ); let Poll::Ready(Ok(lookup)) = dynamic.poll_requested_fill(&waiter) else { panic!("the time start is looked up"); }; - assert_eq!((lookup.time(), lookup.start().group, lookup.end()), (Some(at(5)), 0, 10)); + assert_eq!( + (lookup.time(), lookup.start().group, lookup.end()), + (Some(at(5)), 0, 10) + ); assert_eq!(lookup.next(), None, "a lookup owes no group"); drop(lookup); let mut read = drain_groups(&mut subscriber); @@ -18642,7 +18646,11 @@ mod test { producer.start_at(10).unwrap(); write_group(&producer, 10); let waiter = kio::Waiter::noop(); - assert_eq!(drain_groups(&mut subscriber), Vec::::new(), "nothing reads until it is found"); + assert_eq!( + drain_groups(&mut subscriber), + Vec::::new(), + "nothing reads until it is found" + ); let Poll::Ready(Ok(lookup)) = dynamic.poll_requested_fill(&waiter) else { panic!("the time start is looked up"); }; From 228a8c2e0d7a54779fad0949b4f5bed3c3b270da Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 30 Oct 2026 01:33:40 -0700 Subject: [PATCH 04/12] wip: lookups in filter --- rs/moq-net/src/model/track.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 820c96ab886..db265833e39 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -5913,8 +5913,6 @@ struct Cursor { drops: Option>, /// What this cursor misses and asks fills for, once it first reads. misses: Option>, - /// The time starts its window looks up from the track's history, each asked once. - lookups: Vec, } /// A time start a [`Cursor`]'s window looks up through a fill. @@ -6070,6 +6068,8 @@ struct Filter { /// budget, scanned in the cache in the subscription's order and handed out ahead of the /// walk: each cached group in them is visited once. owed: RangeInclusiveSet, + /// The time starts [`Self::window`] looks up from the track's history, each asked once. + lookups: Vec, } impl Cursor { @@ -6112,6 +6112,7 @@ impl Cursor { admitted: RangeInclusiveSet::new(), admitted_at: None, owed: RangeInclusiveSet::new(), + lookups: Vec::new(), }; (filter, state.datagram_offset + start) }; @@ -6134,7 +6135,6 @@ impl Cursor { skipped: Skipped::default(), drops: None, misses: None, - lookups: Vec::new(), } } @@ -6222,7 +6222,7 @@ impl Cursor { Ok(window) => { self.filter.window = window; self.filter.refused = None; - self.lookups.clear(); + self.filter.lookups.clear(); } Err(err) => self.filter.refused = Some(err), } @@ -6542,12 +6542,12 @@ impl Cursor { /// the answers, parking `waiter` on the lookups still out. fn look_up(&mut self, asking: &miss::Asking, waiter: &kio::Waiter) { let lookups: Vec<(Timestamp, u64)> = self.filter.window.lookups().collect(); - if lookups.is_empty() && self.lookups.iter().all(|lookup| lookup.join.is_none()) { + if lookups.is_empty() && self.filter.lookups.iter().all(|lookup| lookup.join.is_none()) { return; } let fills = self.state.read().fills.clone(); for (pts, reach) in lookups { - if self.lookups.iter().any(|lookup| lookup.pts == pts) { + if self.filter.lookups.iter().any(|lookup| lookup.pts == pts) { continue; } let ask = fill::Ask { @@ -6558,13 +6558,13 @@ impl Cursor { time: Some(pts), }; let (joins, _) = fills.lock().join(&fills, &ask); - self.lookups.push(Lookup { + self.filter.lookups.push(Lookup { pts, join: joins.into_iter().next(), }); } let mut moved = false; - for lookup in &mut self.lookups { + for lookup in &mut self.filter.lookups { // The window stops looking once replaced; with no join, nothing can answer. if !self.filter.window.lookups().any(|(pts, _)| pts == lookup.pts) { lookup.join = None; From cc65eb47615aa022f61bac71536b762150eafa16 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 30 Oct 2026 01:58:55 -0700 Subject: [PATCH 05/12] feat(net)!: a time start the cache can't resolve is looked up through a fill The archive consumer answers the lookup from its key listing, so a Time(pts) range on an archive broadcast reaches the stored group containing pts, and a time end stops just after it, however old. Co-Authored-By: Claude Opus 5.5 --- quest/m2/README.md | 1 - quest/m2/archive-dvr.md | 1 - quest/m2/archive-time-ranges.md | 35 ------------------------------ quest/m3/studio/replay.md | 1 - rs/moq-archive/src/consumer/mod.rs | 4 ++++ 5 files changed, 4 insertions(+), 38 deletions(-) delete mode 100644 quest/m2/archive-time-ranges.md diff --git a/quest/m2/README.md b/quest/m2/README.md index f6d54a23242..0d2aa98860b 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -213,7 +213,6 @@ lite-07; bitrate enforcement is not part of this release. - [QUIC on the sim](/quest/m2/quic-sim.md) - MoQ sessions and the relay run over `moq-quic` on the seeded sim, reusing its `Pair` and `Link` - [Seeded sim](/quest/m2/sim-seed.md) - sim tests draw every jitter from one seeded generator and replay bit-exact -- [Archive time ranges](/quest/m2/archive-time-ranges.md) - a `Time(pts)` range on an archive consumer broadcast resolves from the archive's keys, however old - [DVR rewind](/quest/m2/archive-dvr.md) - follow-on capability after the release contract is stable - [An errored track end keeps its history](/quest/m2/errored-end.md) - a replayed archive ends with its source's error after serving every stored group, instead of a clean finish diff --git a/quest/m2/archive-dvr.md b/quest/m2/archive-dvr.md index b1f29bd5dd6..1846ef0d18e 100644 --- a/quest/m2/archive-dvr.md +++ b/quest/m2/archive-dvr.md @@ -31,4 +31,3 @@ over missing groups, and return to live. ## Required - [Subscribe ranges](/quest/m1/subscribe-ranges/README.md) - the `Time(pts)` range a seek sends -- [Archive time ranges](/quest/m2/archive-time-ranges.md) - the archive consumer resolves time ranges from its keys diff --git a/quest/m2/archive-time-ranges.md b/quest/m2/archive-time-ranges.md deleted file mode 100644 index 2814a22664d..00000000000 --- a/quest/m2/archive-time-ranges.md +++ /dev/null @@ -1,35 +0,0 @@ -# [M] Archive time ranges - -## Goal - -A `Time(pts)` range on an `archive::Consumer` broadcast resolves from the -archive's key listing, not only from the groups the consumer's cache holds: -a start reaches the stored group containing pts, and an end stops just after -it, however old. - -## Plan - -Decided 2026-10-23 (maintainer, on -[moq-dev/moq#6262](https://github.com/moq-dev/moq/pull/6262)): this stays in -m2, required by [DVR rewind](/quest/m2/archive-dvr.md), rather than blocking -the archive consumer, which ships without time anchors. - -The consumer creates each track live from its newest stored group and serves -older groups through `track::Fill`, so its cache holds almost nothing a time -anchor can resolve against: today a `Time(pts)` start older than the newest -group resolves to the oldest cached group instead. A `Fill` names group -positions only, so an anchor the cache can't resolve never reaches the -handler. [moq-dev/moq#6171](https://github.com/moq-dev/moq/pull/6171) deferred carrying the anchor to the consumer quest, which -landed without it. - -- `moq-net` passes a time anchor the cache can't resolve to the - `track::Requests` handler, unresolved, in its fill. -- The consumer resolves it from its index: every object key carries its first - frame's pts, so the object holding pts is one lookup, and the group within - it comes from the stored group timestamps. - -Test with the in-memory store: a start and an end older than the newest -group, an anchor before the oldest stored object, an anchor in a gap, and an -untimed track refusing it. - -Public API: `track::Fill` gains the anchor. Wire: none. diff --git a/quest/m3/studio/replay.md b/quest/m3/studio/replay.md index a0a0fcc6d86..2bc2fffd410 100644 --- a/quest/m3/studio/replay.md +++ b/quest/m3/studio/replay.md @@ -28,6 +28,5 @@ any other. ## Related -- [Archive time ranges](/quest/m2/archive-time-ranges.md) - ranges reaching past the cache - [Archive DVR](/quest/m2/archive-dvr.md) - viewer scrubbing beyond the relay cache - [Recording](/quest/m3/studio/recording.md) - clips use the same file writer diff --git a/rs/moq-archive/src/consumer/mod.rs b/rs/moq-archive/src/consumer/mod.rs index d0988ec55d5..875a3f7103f 100644 --- a/rs/moq-archive/src/consumer/mod.rs +++ b/rs/moq-archive/src/consumer/mod.rs @@ -7,6 +7,10 @@ //! demand from the store through [`track::Requests`](moq_net::track::Requests) fills, each sequence in its recorded form: a //! group as a group, a datagram as a datagram. //! +//! A reader's time start older than the newest group is looked up from the object keys, each of +//! which carries its first frame's timestamp, so the range starts at the stored group containing +//! it however old, and a time end stops just past the group containing it. +//! //! A growing archive is followed: every [`Config::refresh`], one delimiter LIST of the prefix //! finds added tracks, and one LIST per track after its newest key finds new objects. Once the //! prefix holds `.end`, each track ends as recorded and refreshing stops. Without it the archive From c65283602d9f2fe010615a757bd80e1940ef8145 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 30 Oct 2026 02:35:21 -0700 Subject: [PATCH 06/12] fix(net): look up a time start in a cache gap, and wait on lookups before ending Co-Authored-By: Claude Opus 5.5 --- rs/moq-archive/src/consumer/tests.rs | 23 ++++++++++++++ rs/moq-net/src/model/subscription/window.rs | 35 +++++++++++++++------ rs/moq-net/src/model/track.rs | 2 +- 3 files changed, 50 insertions(+), 10 deletions(-) diff --git a/rs/moq-archive/src/consumer/tests.rs b/rs/moq-archive/src/consumer/tests.rs index c75b3941d31..bf82d630782 100644 --- a/rs/moq-archive/src/consumer/tests.rs +++ b/rs/moq-archive/src/consumer/tests.rs @@ -790,6 +790,11 @@ async fn a_time_range_resolves_from_the_archive() { read_range(&broadcast, "video", range(track::Anchor::Absolute(0), Some(at(1_200)))).await, [0, 1] ); + // The cache now holds groups 6 and 9, but not what lies between: still looked up. + assert_eq!( + read_range(&broadcast, "video", range(at(8_500), Some(at(8_600)))).await, + [8] + ); // A start alone reads on through the live edge. let track = broadcast.consume().track("video").unwrap(); @@ -803,6 +808,24 @@ async fn a_time_range_resolves_from_the_archive() { } } +/// A completed archive's track ends only once a time range's lookup answers, so the range +/// still reads its history. +#[tokio::test(start_paused = true)] +async fn a_completed_archive_reads_a_time_range() { + let archive = Archive::new(); + archive.track("video", 0, true).await; + stored(&archive, 0..6).await; + archive.end("video", End::Finish(At { group: 6, frame: 0 })).await; + archive.complete().await; + + let (broadcast, _run) = serve(&archive.store).await; + let range = track::Range { + start: at(2_500), + end: None, + }; + assert_eq!(read_range(&broadcast, "video", range).await, [2, 3, 4, 5]); +} + /// A time older than every stored object reaches back to the oldest stored group. #[tokio::test(start_paused = true)] async fn a_time_before_the_archive_starts_at_its_oldest_group() { diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index d73a84df343..e03924ffeff 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -590,17 +590,17 @@ impl Window { /// The lookup of time start `pts` answered with the group containing it, or the oldest /// group of the history when `pts` is older than all of it: its spans start there, once /// that group has started. `None`, or an answer that never started, means the history - /// couldn't tell, so they reach back to the oldest group held. Returns whether the groups - /// in range moved. + /// couldn't tell, so they start where the groups held say. Returns whether the groups in + /// range moved. pub fn found(&mut self, pts: Timestamp, group: Option, holdings: &impl Holdings) -> bool { let mut moved = false; for span in self.spans.iter_mut().filter(|span| span.looking == Some(pts)) { span.looking = None; moved = true; let started = group.filter(|group| holdings.next_start(*group).is_some_and(|(found, _)| found == *group)); - (span.start, span.floored, span.reach) = match started { - Some(group) => (group, true, group), - None => ( + (span.start, span.floored, span.reach) = match (started, holdings.containing(pts)) { + (Some(group), _) | (None, Containing::Group(group)) => (group, true, group), + (None, _) => ( 0, false, holdings.next_start(0).map_or(span.reach, |(oldest, _)| oldest), @@ -647,10 +647,11 @@ impl Window { end => end, }; let (reach, looking) = match range.start { - Anchor::Time(pts) if !floored => ( - holdings.next_start(0).map_or(start, |(oldest, _)| oldest), - holdings.looks_up().then_some(pts), - ), + Anchor::Time(pts) => match unresolved(start, floored, holdings) { + Some(searched) => (searched, Some(pts)), + None if floored => (start, None), + None => (holdings.next_start(0).map_or(start, |(oldest, _)| oldest), None), + }, _ => (start, None), }; self.spans.push(Span { @@ -709,6 +710,22 @@ fn start(anchor: Anchor, latest: Option, holdings: &impl Holdings) -> Optio } } +/// The exclusive end of the run a lookup of a time start resolved to `start` searches, when +/// the groups held can't tell which group contains it and a handler may: it is before every +/// group known to start, or in a gap after `start`, the newest known to start at or before it, +/// that nothing says is gone. +fn unresolved(start: u64, floored: bool, holdings: &impl Holdings) -> Option { + if !holdings.looks_up() { + return None; + } + if !floored { + return holdings.next_start(0).map(|(oldest, _)| oldest); + } + let next = start.checked_add(1)?; + let (after, _) = holdings.next_start(next)?; + (after > next && holdings.gone(next).is_none_or(|end| end < after)).then_some(after) +} + /// Where a range stops, or `None` while it waits on a latest group. A time end's walk /// starts once its start is known. fn end(anchor: Option, latest: Option) -> Option { diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index d86b1f6febf..f3383c25a36 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -6718,7 +6718,7 @@ impl Cursor { /// Whether a fill still owes this cursor a group, so the end of the feed is not its end. fn awaits_fill(&self) -> bool { - self.misses.as_ref().is_some_and(|misses| misses.is_waiting()) + self.misses.as_ref().is_some_and(|misses| misses.is_waiting()) || self.filter.window.lookups().next().is_some() } fn poll_recv_group(&mut self, take: bool, waiter: &kio::Waiter) -> Poll>> { From a888e623cf628d6a6721fdf45da1099b360731fd Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 30 Oct 2026 03:01:12 -0700 Subject: [PATCH 07/12] fix(net): look up a time start in any cache gap, evicted arrivals included Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/model/subscription/window.rs | 53 +++++++++++++++++++-- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index e03924ffeff..653d936ebac 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -712,8 +712,8 @@ fn start(anchor: Anchor, latest: Option, holdings: &impl Holdings) -> Optio /// The exclusive end of the run a lookup of a time start resolved to `start` searches, when /// the groups held can't tell which group contains it and a handler may: it is before every -/// group known to start, or in a gap after `start`, the newest known to start at or before it, -/// that nothing says is gone. +/// group known to start, or in a gap after `start`, the newest known to start at or before it. +/// Even a gap the live feed accounted for may sit in the history once evicted. fn unresolved(start: u64, floored: bool, holdings: &impl Holdings) -> Option { if !holdings.looks_up() { return None; @@ -723,7 +723,7 @@ fn unresolved(start: u64, floored: bool, holdings: &impl Holdings) -> Option next && holdings.gone(next).is_none_or(|end| end < after)).then_some(after) + (after > next).then_some(after) } /// Where a range stops, or `None` while it waits on a latest group. A time end's walk @@ -757,6 +757,7 @@ mod tests { held: std::collections::BTreeMap, gone: Vec>, complete: bool, + looks_up: bool, asked: Cell, } @@ -778,6 +779,10 @@ mod tests { true } + fn looks_up(&self) -> bool { + self.looks_up + } + fn containing(&self, pts: Timestamp) -> Containing { let mut stamped = false; for (&sequence, held) in self.held.iter().rev() { @@ -1203,4 +1208,46 @@ mod tests { Err(Error::TimestampMismatch) )); } + + /// With a handler serving the history, a time start the groups held can't place is looked + /// up and reads nothing meanwhile: before every start, or in a gap after the newest group + /// starting at or before it, even one the live feed accounted for, since an evicted group + /// still sits in the history. The answer places it; none leaves what the groups held say. + #[test] + fn a_time_start_the_groups_cant_place_is_looked_up() { + let mut groups = Groups::new([Groups::started(4, 4000), Groups::started(9, 9000)]); + groups.gone = vec![5..9]; + groups.looks_up = true; + let mut window = Window::new(&[range(time(8500), None)], Some(9), &groups).unwrap(); + assert_eq!(window.lookups().collect::>(), [(at(8500), 9)]); + assert!(!window.is_settled() && !window.contains(9) && window.resolved() == [None]); + groups.held.insert(8, Held::Started(at(8000))); + assert!(window.found(at(8500), Some(8), &groups)); + assert_eq!(window.resolved(), [Some(Resolved { start: 8, end: None })]); + assert!(window.is_settled()); + + let window = Window::new(&[range(time(8500), None)], Some(9), &groups).unwrap(); + assert_eq!(window.lookups().count(), 0, "group 8 is held, with 9 after it"); + + let mut window = Window::new(&[range(time(500), None)], Some(9), &groups).unwrap(); + assert_eq!(window.lookups().collect::>(), [(at(500), 4)]); + assert!(window.found(at(500), None, &groups)); + assert_eq!(window.resolved(), [Some(Resolved { start: 0, end: None })]); + assert!(window.contains(4) && !window.missable(0).any(|run| *run.start() < 4)); + } + + /// Without a handler serving the history, the groups held decide alone. + #[test] + fn a_time_start_is_not_looked_up_without_a_history() { + let groups = Groups::new([Groups::started(4, 4000), Groups::started(9, 9000)]); + let window = Window::new(&[range(time(8500), None), range(time(500), None)], Some(9), &groups).unwrap(); + assert_eq!(window.lookups().count(), 0); + assert_eq!( + window.resolved(), + [ + Some(Resolved { start: 4, end: None }), + Some(Resolved { start: 0, end: None }), + ] + ); + } } From 253787d532a6f2c38d28228f378d6becb73bcde1 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 30 Oct 2026 03:04:35 -0700 Subject: [PATCH 08/12] test(net): push the gone run as the other window tests do Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/model/subscription/window.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index 653d936ebac..7e7d042ec35 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -1216,7 +1216,7 @@ mod tests { #[test] fn a_time_start_the_groups_cant_place_is_looked_up() { let mut groups = Groups::new([Groups::started(4, 4000), Groups::started(9, 9000)]); - groups.gone = vec![5..9]; + groups.gone.push(5..9); groups.looks_up = true; let mut window = Window::new(&[range(time(8500), None)], Some(9), &groups).unwrap(); assert_eq!(window.lookups().collect::>(), [(at(8500), 9)]); From 234162ed62907f9ad6a1dd18cdca17bf6980d96c Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 30 Oct 2026 22:45:41 -0700 Subject: [PATCH 09/12] wip(net,archive): bound time lookups by group starts, not evicted arrivals An archive lookup took the object whose key's first frame was at or before the time, but a continuation object's first frame is a mid-group frame that bounds no later group start. Index only objects starting a group, and search back from the next one past the time for the newest group starting at or before it. A time end walking history a handler serves took the live feed's evicted (or grace-folded) arrivals as gone, so it settled past groups that start after the end. On such a track only drops and a reader's own refusals count as gone. WIP: a_time_end_walks_evicted_arrivals_through_the_history still fails. A fill below the eviction watermark is Origin::Unverified, so its start is None and neither the lookup answer nor the end walk can place it; fixing that needs a decision on whether unverified starts count for time anchors. Co-Authored-By: Claude Opus 5.5 --- rs/moq-archive/src/consumer/index.rs | 51 ++++++++++++++------ rs/moq-archive/src/consumer/tests.rs | 47 +++++++++++++++++++ rs/moq-archive/src/consumer/track.rs | 30 ++++++------ rs/moq-net/src/model/subscription/window.rs | 4 +- rs/moq-net/src/model/track.rs | 52 +++++++++++++++++++-- 5 files changed, 146 insertions(+), 38 deletions(-) diff --git a/rs/moq-archive/src/consumer/index.rs b/rs/moq-archive/src/consumer/index.rs index 34d219a7d76..d8595470177 100644 --- a/rs/moq-archive/src/consumer/index.rs +++ b/rs/moq-archive/src/consumer/index.rs @@ -32,8 +32,10 @@ pub(super) struct Index { pub timescale: Option, /// Each object's first frame timestamp, on a timed track, and key. objects: BTreeMap, Path)>, - /// Each timed object's first frame timestamp with where it starts, so a time finds the - /// object holding it in one lookup. Timestamps never rewind, so they sort as the starts do. + /// Each timed object starting a group, by that group's start timestamp with where it + /// starts, so a time finds the newest such object at or before it in one lookup. Group + /// starts never rewind, so they sort as the objects do. An object continuing a group is + /// left out: its first frame says nothing about where the groups after it start. times: BTreeSet<(u64, Start)>, /// The newest key listed, which the next listing starts after. cursor: Path, @@ -64,7 +66,9 @@ impl Index { pub fn extend(&mut self, objects: &[Listing]) { for (start, pts, path) in objects { self.objects.insert(*start, (*pts, path.clone())); - if let Some(pts) = pts { + if let Some(pts) = pts + && start.1 == 0 + { self.times.insert((*pts, *start)); } self.cursor = path.clone(); @@ -97,20 +101,37 @@ impl Index { (holding, after) } - /// The newest object whose first frame presents at or before `pts`: the one holding it, - /// if any does. `None` when every object starts after it, or on an untimed track. - pub fn at(&self, pts: Timestamp) -> Option<(Start, Path)> { + /// The objects that may hold the newest group starting at or before `pts`, newest first: + /// from the newest object starting a group at or before it, or the oldest object when none + /// does, up to the next object starting a group after it, which holds only later groups. + /// One continuing a group the next object still continues starts none, so it is skipped. + /// Empty on an untimed track. + pub fn around(&self, pts: Timestamp) -> Vec<(Start, Path)> { + let Some(timescale) = self.timescale else { + return Vec::new(); + }; // A stored timestamp is whole, so it is at or before `pts` exactly when it is at or // before `pts` rounded down to the track's timescale. - let limit = u64::try_from(pts.as_scale(self.timescale?)).unwrap_or(u64::MAX); - let (_, start) = self.times.range(..=(limit, (u64::MAX, u64::MAX))).next_back()?; - self.objects.get(start).map(|(_, path)| (*start, path.clone())) - } - - /// The object just before the one starting at `start`. - pub fn before(&self, start: Start) -> Option<(Start, Path)> { - let (start, (_, path)) = self.objects.range(..start).next_back()?; - Some((*start, path.clone())) + let limit = u64::try_from(pts.as_scale(timescale)).unwrap_or(u64::MAX); + let from = self + .times + .range(..=(limit, (u64::MAX, u64::MAX))) + .next_back() + .map_or((0, 0), |(_, start)| *start); + + let mut around = Vec::new(); + let mut objects = self.objects.range(from..).peekable(); + while let Some((start, (first, path))) = objects.next() { + if start.1 == 0 && first.is_some_and(|first| first > limit) { + break; + } + let continued = start.1 > 0 && objects.peek().is_some_and(|(next, _)| next.0 == start.0); + if !continued { + around.push((*start, path.clone())); + } + } + around.reverse(); + around } /// The object just after the one starting at `start`, or the oldest for `None`. diff --git a/rs/moq-archive/src/consumer/tests.rs b/rs/moq-archive/src/consumer/tests.rs index bf82d630782..9f716841a93 100644 --- a/rs/moq-archive/src/consumer/tests.rs +++ b/rs/moq-archive/src/consumer/tests.rs @@ -858,6 +858,53 @@ async fn a_time_in_a_gap_starts_before_it() { assert_eq!(read_range(&broadcast, "video", range).await, [3, 6]); } +/// A continuation's first frame bounds no group start: an object continuing group 0 at a +/// later timestamp than the group 1 it then starts still holds the group containing a time +/// between the two. +#[tokio::test(start_paused = true)] +async fn a_continuation_does_not_bound_a_group_start() { + let archive = Archive::new(); + archive.track("video", 0, true).await; + let frame = |sequence: u64, index: u64, pts: u64| Frame { + timestamp: Some(pts), + payload: Bytes::from(format!("{sequence}/{index}")), + }; + archive + .object( + "video", + 0, + vec![Entry::Group { + sequence: 0, + frames: vec![frame(0, 0, 0)], + }], + ) + .await; + archive + .object( + "video", + 1, + vec![ + Entry::Group { + sequence: 0, + frames: vec![frame(0, 1, 1_000)], + }, + Entry::Group { + sequence: 1, + frames: vec![frame(1, 0, 500)], + }, + ], + ) + .await; + stored(&archive, 2..4).await; + + let (broadcast, _run) = serve(&archive.store).await; + let range = track::Range { + start: at(600), + end: Some(at(700)), + }; + assert_eq!(read_range(&broadcast, "video", range).await, [1]); +} + /// An untimed track refuses a time range rather than looking it up. #[tokio::test(start_paused = true)] async fn an_untimed_track_refuses_a_time_range() { diff --git a/rs/moq-archive/src/consumer/track.rs b/rs/moq-archive/src/consumer/track.rs index c0003eb236f..df8f25e2faa 100644 --- a/rs/moq-archive/src/consumer/track.rs +++ b/rs/moq-archive/src/consumer/track.rs @@ -178,13 +178,13 @@ pub(super) async fn serve(shared: Arc>, index: Arc(shared: &Shared, index: &Mutex, fill: &Fill, pts: Timestamp) { - let (at, timed, timescale) = { + let (around, timed, timescale) = { let index = index.lock().unwrap(); - (index.at(pts), index.timed(), index.timescale) + (index.around(pts), index.timed(), index.timescale) }; let Some(timescale) = timescale else { // A reader's time start on an untimed track is refused before it is looked up. @@ -192,10 +192,8 @@ async fn look_up(shared: &Shared, index: &Mutex, fill: }; let limit = pts.as_scale(timescale); - // Back from the object holding it to the newest group starting at or before it: its - // own when the object continues one, since every frame presents after its group's first. - let mut next = at.clone(); - while let Some((start, path)) = next { + // Newest first: only a group whose first frame an object holds says where it starts. + for (start, path) in around { let Some(object) = load(shared, index, fill, start, &path, timed).await else { return; }; @@ -204,23 +202,23 @@ async fn look_up(shared: &Shared, index: &Mutex, fill: .iter() .enumerate() .filter_map(|(position, entry)| match entry { - Entry::Group { sequence, frames } => { + Entry::Group { sequence, frames } if position > 0 || object.frame_start == 0 => { let first = frames.first()?; - let started = position == 0 && object.frame_start > 0 - || first.timestamp.is_some_and(|at| u128::from(at) <= limit); - started.then_some(*sequence) + first + .timestamp + .is_some_and(|at| u128::from(at) <= limit) + .then_some(*sequence) } - Entry::Datagram { .. } => None, + _ => None, }) .last(); if let Some(sequence) = found { return serve_group(shared, index, fill, sequence).await; } - next = index.lock().unwrap().before(start); } // Every group starts after it: the oldest group is where its range reaches back to. - let mut next = index.lock().unwrap().after(at.map(|(start, _)| start)); + let mut next = index.lock().unwrap().after(None); while let Some((start, path)) = next { let Some(object) = load(shared, index, fill, start, &path, timed).await else { return; diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index 7e7d042ec35..2ceb79547c5 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -35,8 +35,8 @@ pub(in crate::model) trait Holdings { /// Whether a [`Requests`](crate::track::Requests) handler serving the track's history /// may look up a time its groups can't resolve: a time start before every group with a - /// known start then waits on that lookup, and a sequence below where the feed starts may - /// still arrive from that history. + /// known start then waits on that lookup, and a sequence the live feed never delivered, + /// or delivered and the cache evicted, may still arrive from that history. fn looks_up(&self) -> bool { false } diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index f3383c25a36..c9aba7258a5 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -784,15 +784,19 @@ impl Holdings for TrackState { } fn gone(&self, sequence: u64) -> Option { + let dropped = self.drops.get_key_value(&sequence).map(|(dropped, _)| dropped.end); + // The history a handler serves may still hold what the live feed never delivered, or + // delivered and the cache evicted, starting anywhere: only a drop, or a reader's own + // refused fills (see `Seen`), says it is gone. + if self.looks_up() { + return dropped; + } // Without a declared start, the first arrival stands in for it, as the tail's does. let floor = self.start_sequence.or(self.arrivals.first()).unwrap_or(0); - if sequence < floor && !self.looks_up() { + if sequence < floor { return Some(floor); } - self.drops - .get_key_value(&sequence) - .map(|(dropped, _)| dropped.end) - .or_else(|| self.arrivals.run_end(sequence)) + dropped.or_else(|| self.arrivals.run_end(sequence)) } fn complete(&self) -> bool { @@ -18737,6 +18741,44 @@ mod test { assert_eq!(read, [5, 6, 7]); } + /// A time end walking the history a handler serves takes no group the live feed + /// delivered and the cache evicted as gone: the history still holds it, and it may start + /// inside the end, so the end settles on what the history answers. + #[test] + #[cfg(feature = "async")] + fn a_time_end_walks_evicted_arrivals_through_the_history() { + let mut producer = track_producer("test", Info::timed()); + let dynamic = producer.requests(); + let at = |millis| Timestamp::from_millis(millis).unwrap(); + for sequence in 0..10 { + write_group(&producer, sequence); + } + for sequence in [0, 1, 2, 3, 5, 6, 7, 8] { + evict(&producer, sequence); + } + let mut subscriber = producer.subscribe(replay().with_ranges([Range { + start: Anchor::Time(at(5)), + end: Some(Anchor::Time(at(6))), + }])); + let waiter = kio::Waiter::noop(); + assert_eq!(drain_groups(&mut subscriber), Vec::::new()); + let Poll::Ready(Ok(lookup)) = dynamic.poll_requested_fill(&waiter) else { + panic!("the time start in the evicted gap is looked up"); + }; + assert_eq!(lookup.time(), Some(at(5))); + write_filled(lookup.accept(5, None), 5); + drop(lookup); + let mut read = drain_groups(&mut subscriber); + while let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) { + while let Some(sequence) = fill.next() { + write_filled(fill.accept(sequence, None), sequence); + } + drop(fill); + read.extend(drain_groups(&mut subscriber)); + } + assert_eq!(read, [5, 6], "groups 7 and 8 start past the end"); + } + /// A wide fill refused group by group, each with its own error, holds only the answers its /// readers have yet to take, not one per refusal. #[test] From 746417a8362d846462e15c82331284cf1a58b141 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 31 Oct 2026 02:03:33 -0700 Subject: [PATCH 10/12] fix(net): count a filled group's start for time ranges on a history-served track A fill below the eviction watermark is accepted unverified, so it had no start: a time range over an evicted span could neither place the group its lookup answered nor walk its end through the history, reading [5, 4, 6, 7, 8] rather than [5, 6]. On a track whose history a local handler serves, every writer is local, so a time range counts an unverified group's first frame as its start. Expiry and the rewind check still ignore it, so a remote publisher's unverified start still bounds nothing. a_looked_up_time_range_reads_the_history now holds the answer's first frame back, so it checks the reader waits for the looked-up group to start rather than placing the range from the cache alone. Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/model/group.rs | 10 +++++++ rs/moq-net/src/model/track.rs | 49 +++++++++++++++++++++++------------ 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/rs/moq-net/src/model/group.rs b/rs/moq-net/src/model/group.rs index c779dc81435..882026251fe 100644 --- a/rs/moq-net/src/model/group.rs +++ b/rs/moq-net/src/model/group.rs @@ -2681,6 +2681,16 @@ impl Handle { } } + /// Where the group starts in presentation time, accepted unverified or not: what a time + /// range counts on a track whose history a local handler serves, where no remote + /// publisher wrote it (see [`Self::timestamp`]). + pub(super) fn first_timestamp(&self) -> Option { + match self.state.read().timeline { + Timeline::Timed { start, .. } => Some(start), + _ => None, + } + } + /// The start this group lends its neighbors' rewind check: its first frame's timestamp, /// once frame 0 was checked to fit between theirs. pub(super) fn bound(&self) -> Option { diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index c9aba7258a5..371d6f63791 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -689,16 +689,23 @@ impl Slot { /// Where the group starts in presentation time, from a live copy holding frame 0. A /// copy joined mid-group stamps a later frame, which proves nothing about the start. - fn start(&self) -> Option { + /// + /// Only for time anchors. `unverified` counts a start accepted below the eviction + /// watermark too, which a track whose history a local handler serves trusts (see + /// [`Holdings::looks_up`]): no remote publisher wrote it. + fn start(&self, unverified: bool) -> Option { self.copies() .filter(|group| group.live_first_frame() == Some(0)) - .find_map(group::Handle::timestamp) + .find_map(|group| match unverified { + true => group.first_timestamp(), + false => group.timestamp(), + }) } - /// Whether the group has no start yet but may still get one: a copy holding frame 0 - /// has not presented it. - fn may_start(&self) -> bool { - self.start().is_none() && self.copies().any(group::Handle::may_start) + /// Whether the group has no [`Self::start`] yet but may still get one: a copy holding + /// frame 0 has not presented it. + fn may_start(&self, unverified: bool) -> bool { + self.start(unverified).is_none() && self.copies().any(group::Handle::may_start) } /// The checked start of whichever copy began at frame 0. @@ -775,9 +782,9 @@ impl Holdings for TrackState { fn next_held(&self, from: u64) -> Option<(u64, Held)> { let (&sequence, slot) = self.lookup.range(from..).next()?; - let held = match slot.start() { + let held = match slot.start(self.looks_up()) { Some(at) => Held::Started(at), - None if slot.may_start() => Held::Pending, + None if slot.may_start(self.looks_up()) => Held::Pending, None => Held::Never, }; Some((sequence, held)) @@ -950,9 +957,11 @@ impl TrackState { } /// Index where cached group `sequence` starts, read back through its slot so only the - /// cached copy holding frame 0 counts, and wake the reads a time end parks on it. + /// cached copy holding frame 0 counts, and wake the reads a time end parks on it. On a + /// track whose history a handler serves, a start accepted unverified counts: the fills + /// below the eviction watermark that a time range walks are all unverified. fn index_start(&self, sequence: u64) { - let Some(start) = self.lookup.get(&sequence).and_then(Slot::start) else { + let Some(start) = self.lookup.get(&sequence).and_then(|slot| slot.start(self.looks_up())) else { return; }; self.starts @@ -6628,8 +6637,7 @@ impl Cursor { // The answer's first frame says whether it starts at or before the time. if let Some(group) = answer && let Some(slot) = state.lookup.get(&group) - && slot.start().is_none() - && slot.may_start() + && slot.may_start(state.looks_up()) { let _ = slot.serving().poll_started(waiter); continue; @@ -18701,9 +18709,9 @@ mod test { assert!(dynamic.poll_requested_fill(&waiter).is_pending()); } - /// A time start before every group held starts at the group its lookup answers, and a - /// time end walks the history below the feed's start through fills, stopping just past - /// the group containing it. + /// A time start before every group held starts at the group its lookup answers, once that + /// group's first frame says where it starts, asking nothing below it; a time end walks the + /// history below the feed's start through fills, stopping just past the group containing it. #[test] #[cfg(feature = "async")] fn a_looked_up_time_range_reads_the_history() { @@ -18726,13 +18734,20 @@ mod test { panic!("the time start is looked up"); }; assert_eq!(lookup.time(), Some(at(5))); - write_filled(lookup.accept(5, None), 5); + let found = lookup.accept(5, None); drop(lookup); + assert_eq!( + drain_groups(&mut subscriber), + Vec::::new(), + "nothing reads until the group found starts" + ); + write_filled(found, 5); let mut read = drain_groups(&mut subscriber); // One group at a time past the start, until one starts after the end. while let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) { assert_eq!(fill.time(), None); while let Some(sequence) = fill.next() { + assert!(sequence >= 5, "group {sequence} is below the looked-up start"); write_filled(fill.accept(sequence, None), sequence); } drop(fill); @@ -18747,7 +18762,7 @@ mod test { #[test] #[cfg(feature = "async")] fn a_time_end_walks_evicted_arrivals_through_the_history() { - let mut producer = track_producer("test", Info::timed()); + let producer = track_producer("test", Info::timed()); let dynamic = producer.requests(); let at = |millis| Timestamp::from_millis(millis).unwrap(); for sequence in 0..10 { From 94cdc3a3b231f0d3316d570e70c7b9df677fe834 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 31 Oct 2026 02:58:51 -0700 Subject: [PATCH 11/12] fix(net,archive): settle a time end after an unanswered lookup, skip pruned heads A lookup a handler drops leaves its span starting before the oldest group held, asking nothing below it, but the time end walked from group 0. On a track whose history a handler serves nothing there counts as gone, so the end waited on group 0 forever. It now walks from where the span reaches. An archive lookup older than every stored group answered the oldest object's first group even when the object continues a group whose head was pruned, which can't be served from its first frame, so the lookup went unanswered. It skips that group for the oldest one stored whole. Co-Authored-By: Claude Opus 5.5 --- rs/moq-archive/src/consumer/tests.rs | 20 ++++++++++++++++++++ rs/moq-archive/src/consumer/track.rs | 19 ++++++++++++++----- rs/moq-net/src/model/subscription/window.rs | 19 +++++++++++++++++-- 3 files changed, 51 insertions(+), 7 deletions(-) diff --git a/rs/moq-archive/src/consumer/tests.rs b/rs/moq-archive/src/consumer/tests.rs index 9f716841a93..dc83eb9304b 100644 --- a/rs/moq-archive/src/consumer/tests.rs +++ b/rs/moq-archive/src/consumer/tests.rs @@ -842,6 +842,26 @@ async fn a_time_before_the_archive_starts_at_its_oldest_group() { assert_eq!(read_range(&broadcast, "video", range).await, [3, 4]); } +/// A time older than an archive whose oldest object continues a pruned group reaches back to +/// the oldest group stored whole. +#[tokio::test(start_paused = true)] +async fn a_time_before_a_pruned_head_starts_at_the_oldest_whole_group() { + let archive = Archive::new(); + archive.track("video", 0, true).await; + archive + .object("video", 2, vec![frames(true, 5, 2..4), frames(true, 6, 0..2)]) + .await; + archive.object("video", 0, vec![frames(true, 7, 0..2)]).await; + archive.object("video", 0, vec![frames(true, 8, 0..1)]).await; + + let (broadcast, _run) = serve(&archive.store).await; + let range = track::Range { + start: at(500), + end: Some(at(7_500)), + }; + assert_eq!(read_range(&broadcast, "video", range).await, [6, 7]); +} + /// A time in a gap the archive never stored starts at the group before the gap, the newest /// starting before it, and the gap reads as missing. #[tokio::test(start_paused = true)] diff --git a/rs/moq-archive/src/consumer/track.rs b/rs/moq-archive/src/consumer/track.rs index df8f25e2faa..4f6f227d2fc 100644 --- a/rs/moq-archive/src/consumer/track.rs +++ b/rs/moq-archive/src/consumer/track.rs @@ -217,16 +217,25 @@ async fn look_up(shared: &Shared, index: &Mutex, fill: } } - // Every group starts after it: the oldest group is where its range reaches back to. + // Every group starts after it: the oldest group is where its range reaches back to, skipping + // one whose head was pruned, which can't be served from its first frame. let mut next = index.lock().unwrap().after(None); while let Some((start, path)) = next { let Some(object) = load(shared, index, fill, start, &path, timed).await else { return; }; - let found = object.entries.iter().find_map(|entry| match entry { - Entry::Group { sequence, frames } if !frames.is_empty() => Some(*sequence), - _ => None, - }); + let found = object + .entries + .iter() + .enumerate() + .find_map(|(position, entry)| match entry { + Entry::Group { sequence, frames } + if !frames.is_empty() && (position > 0 || object.frame_start == 0) => + { + Some(*sequence) + } + _ => None, + }); if let Some(sequence) = found { return serve_group(shared, index, fill, sequence).await; } diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index 2ceb79547c5..a063c451a5d 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -606,9 +606,11 @@ impl Window { holdings.next_start(0).map_or(span.reach, |(oldest, _)| oldest), ), }; - // A time end walked from group 0 meanwhile starts its walk again at the start. + // A time end walked from group 0 meanwhile walks again from where the span reaches: + // a start the history couldn't place asks nothing below the oldest group held, so + // none of those may hold the end open. if let Some(Anchor::Time(end)) = self.ranges[span.index].end { - let mut time = TimeEnd::new(end, span.start); + let mut time = TimeEnd::new(end, span.reach); span.end = time.advance(span.start, holdings).map_or(End::Time(time), End::Before); } } @@ -1236,6 +1238,19 @@ mod tests { assert!(window.contains(4) && !window.missable(0).any(|run| *run.start() < 4)); } + /// A time end on a start the history couldn't place walks from the oldest group held, + /// since nothing asks for those below it: none holds the end open. + #[test] + fn an_unanswered_lookup_settles_a_time_end() { + let mut groups = Groups::new([Groups::started(4, 4000), Groups::started(9, 9000)]); + groups.looks_up = true; + let mut window = Window::new(&[range(time(500), Some(time(3000)))], Some(9), &groups).unwrap(); + assert_eq!(window.lookups().collect::>(), [(at(500), 4)]); + assert!(window.found(at(500), None, &groups)); + assert!(window.is_settled()); + assert_eq!(window.resolved(), [Some(Resolved { start: 0, end: Some(4) })]); + } + /// Without a handler serving the history, the groups held decide alone. #[test] fn a_time_start_is_not_looked_up_without_a_history() { From dd73c2192a6d6db39890fed72a764b2be0fa62f5 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 31 Oct 2026 22:42:40 -0700 Subject: [PATCH 12/12] fix(archive): drop lookup cancellation after main removed Fill::unused main (#7036) removed Fill::unused()/is_used() and renamed next() to wanted(). A lookup owes no group, so nothing now tells it when its readers stop waiting: an abandoned lookup finishes its store read and answers nobody. Rename the test calls and fix the Fill::time() docs. Co-Authored-By: Claude Opus 5.5 --- rs/moq-archive/src/consumer/track.rs | 41 ++++++++++++++-------------- rs/moq-net/src/model/track.rs | 6 ++-- rs/moq-net/src/model/track/fill.rs | 4 +-- 3 files changed, 25 insertions(+), 26 deletions(-) diff --git a/rs/moq-archive/src/consumer/track.rs b/rs/moq-archive/src/consumer/track.rs index fabd0dbe8f5..5ca84109ef0 100644 --- a/rs/moq-archive/src/consumer/track.rs +++ b/rs/moq-archive/src/consumer/track.rs @@ -196,7 +196,7 @@ async fn look_up(shared: &Shared, index: &Mutex, fill: // Newest first: only a group whose first frame an object holds says where it starts. for (start, path) in around { - let Some(object) = load(shared, index, fill, start, &path, timed).await else { + let Some(object) = load(shared, index, start, &path, timed).await else { return; }; let found = object @@ -223,7 +223,7 @@ async fn look_up(shared: &Shared, index: &Mutex, fill: // one whose head was pruned, which can't be served from its first frame. let mut next = index.lock().unwrap().after(None); while let Some((start, path)) = next { - let Some(object) = load(shared, index, fill, start, &path, timed).await else { + let Some(object) = load(shared, index, start, &path, timed).await else { return; }; let found = object @@ -245,21 +245,20 @@ async fn look_up(shared: &Shared, index: &Mutex, fill: } } -/// The object a lookup searches, `None` once nobody waits on the lookup or the store failed. +/// The object a lookup searches, `None` once the store failed. +/// +/// An abandoned lookup still finishes the read and answers nobody: a lookup owes no group, so +/// nothing tells it when its readers stop waiting. async fn load( shared: &Shared, index: &Mutex, - fill: &Fill, start: (u64, u64), path: &object_store::path::Path, timed: bool, ) -> Option> { - let unused = std::pin::pin!(fill.unused()); - let load = std::pin::pin!(super::object(shared, path, start, timed)); - match future::select(unused, load).await { - Either::Left(_) => None, - Either::Right((Ok(object), _)) => Some(object), - Either::Right((Err(err), _)) => { + match super::object(shared, path, start, timed).await { + Ok(object) => Some(object), + Err(err) => { if let Error::NotFound(_) = err { // Pruned since it was listed. index.lock().unwrap().forget(start); @@ -280,22 +279,22 @@ async fn serve_group(shared: &Shared, index: &Mutex, f (holding, after, index.timed(), index.timescale) }; let next = after.map_or(u64::MAX, |start| start.0); - // A lookup owes no group, so it waits on its readers rather than on this one's. + // A lookup owes no group, so nothing says when its readers stop waiting: it finishes the read. let lookup = fill.time().is_some(); let mut objects = Vec::with_capacity(holding.len()); for (start, path) in &holding { - let unwanted = std::pin::pin!(async { - match lookup { - true => fill.unused().await, - false => fill.unwanted(sequence).await, + let load = super::object(shared, path, *start, timed); + let loaded = match lookup { + true => load.await, + false => { + let unwanted = std::pin::pin!(fill.unwanted(sequence)); + match future::select(unwanted, std::pin::pin!(load)).await { + // Every reader narrowed past it: the fill moves on to the groups it still owes. + Either::Left(_) => return, + Either::Right((loaded, _)) => loaded, + } } - }); - let load = std::pin::pin!(super::object(shared, path, *start, timed)); - let loaded = match future::select(unwanted, load).await { - // Every reader narrowed past it: the fill moves on to the groups it still owes. - Either::Left(_) => return, - Either::Right((loaded, _)) => loaded, }; match loaded { Ok(object) => objects.push(object), diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 08694b91fb8..850574bee82 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -19289,7 +19289,7 @@ mod test { (lookup.time(), lookup.start().group, lookup.end()), (Some(at(5)), 0, 10) ); - assert_eq!(lookup.next(), None, "a lookup owes no group"); + assert_eq!(lookup.wanted(), None, "a lookup owes no group"); drop(lookup); let mut read = drain_groups(&mut subscriber); let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) else { @@ -19346,7 +19346,7 @@ mod test { // One group at a time past the start, until one starts after the end. while let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) { assert_eq!(fill.time(), None); - while let Some(sequence) = fill.next() { + while let Some(sequence) = fill.wanted() { assert!(sequence >= 5, "group {sequence} is below the looked-up start"); write_filled(fill.accept(sequence, None), sequence); } @@ -19385,7 +19385,7 @@ mod test { drop(lookup); let mut read = drain_groups(&mut subscriber); while let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) { - while let Some(sequence) = fill.next() { + while let Some(sequence) = fill.wanted() { write_filled(fill.accept(sequence, None), sequence); } drop(fill); diff --git a/rs/moq-net/src/model/track/fill.rs b/rs/moq-net/src/model/track/fill.rs index c9e29f811bb..ae0cd791a82 100644 --- a/rs/moq-net/src/model/track/fill.rs +++ b/rs/moq-net/src/model/track/fill.rs @@ -766,8 +766,8 @@ impl Fill { /// after it. The reader's range then starts there, once that group's first frame is /// written. /// - /// A lookup owes no group, so [`next`](Self::next) is `None` and [`is_wanted`](Self::is_wanted) - /// false throughout; it waits only on its first accept, while [`is_used`](Self::is_used). + /// A lookup owes no group, so [`wanted`](Self::wanted) is `None` and + /// [`is_wanted`](Self::is_wanted) false throughout; it waits only on its first accept. /// Dropped unanswered, as a handler that can't resolve times does, the reader's range /// reaches back to the oldest group the cache holds. `None` for a fill of groups. pub fn time(&self) -> Option {