diff --git a/quest/m2/README.md b/quest/m2/README.md index f09abc92486..4d30d856c3b 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -185,7 +185,6 @@ lite-07; bitrate enforcement is not part of this release. - [The relay over QUIC on the sim](/quest/m2/quic-sim.md) - the relay and moq-tokio's `Client` and `Server` run over `moq-quic` on the seeded sim - [moq-shaper's own socket never passes for path loss](/quest/m2/shaper-socket.md) - relay-sized buffers and its own socket drops reported beside path drops -- [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 - [Archive lookups cancel when abandoned](/quest/m2/archive-lookup-withdrawal.md) - a public withdrawal signal on `Fill` stops a time lookup's store read once its readers leave 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 bd64a3fb18d..0dfbb0e2044 100644 --- a/quest/m3/studio/replay.md +++ b/quest/m3/studio/replay.md @@ -30,6 +30,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/index.rs b/rs/moq-archive/src/consumer/index.rs index a93e50ae47a..d8595470177 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,13 @@ 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 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, } @@ -38,6 +48,7 @@ impl Index { dir, timescale: super::track::timescale(info)?, objects: BTreeMap::new(), + times: BTreeSet::new(), cursor, }) } @@ -52,9 +63,14 @@ 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 + && start.1 == 0 + { + self.times.insert((*pts, *start)); + } self.cursor = path.clone(); } } @@ -63,7 +79,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 +87,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 +101,53 @@ impl Index { (holding, after) } + /// 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(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`. + 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 +177,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 9e1125a01a1..7fc6fff1ea9 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 @@ -330,7 +334,14 @@ 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 a0785df0d56..a44bb4a59ae 100644 --- a/rs/moq-archive/src/consumer/tests.rs +++ b/rs/moq-archive/src/consumer/tests.rs @@ -739,3 +739,224 @@ 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] + ); + // 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(); + 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 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() { + 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 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)] +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]); +} + +/// 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() { + 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 d0ba0372d70..5ca84109ef0 100644 --- a/rs/moq-archive/src/consumer/track.rs +++ b/rs/moq-archive/src/consumer/track.rs @@ -167,6 +167,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 let Some(sequence) = fill.wanted() && last != Some(sequence) @@ -176,6 +179,96 @@ pub(super) async fn serve(shared: Arc>, index: Arc(shared: &Shared, index: &Mutex, fill: &Fill, pts: Timestamp) { + let (around, timed, timescale) = { + let index = index.lock().unwrap(); + (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. + return; + }; + let limit = pts.as_scale(timescale); + + // 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, start, &path, timed).await else { + return; + }; + let found = object + .entries + .iter() + .enumerate() + .filter_map(|(position, entry)| match entry { + Entry::Group { sequence, frames } if position > 0 || object.frame_start == 0 => { + let first = frames.first()?; + first + .timestamp + .is_some_and(|at| u128::from(at) <= limit) + .then_some(*sequence) + } + _ => None, + }) + .last(); + if let Some(sequence) = found { + return serve_group(shared, index, fill, sequence).await; + } + } + + // 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, start, &path, timed).await else { + return; + }; + 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; + } + next = index.lock().unwrap().after(Some(start)); + } +} + +/// 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, + start: (u64, u64), + path: &object_store::path::Path, + timed: bool, +) -> Option> { + 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); + } + 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) { @@ -186,15 +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 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!(fill.unwanted(sequence)); - 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, + 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, + } + } }; match loaded { Ok(object) => objects.push(object), @@ -274,7 +374,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/group.rs b/rs/moq-net/src/model/group.rs index 9660405062d..531058f59f1 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/subscription.rs b/rs/moq-net/src/model/subscription.rs index addaeb24cdd..08bf5ae5d66 100644 --- a/rs/moq-net/src/model/subscription.rs +++ b/rs/moq-net/src/model/subscription.rs @@ -138,8 +138,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 48683800611..f35ef12e15c 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -52,6 +52,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 the live feed never delivered, + /// or delivered and the cache evicted, 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; @@ -161,6 +169,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 { @@ -377,7 +388,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) { @@ -396,10 +407,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` @@ -407,6 +421,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, @@ -484,12 +499,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 { @@ -599,6 +615,47 @@ 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 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, 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), + ), + }; + // 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.reach); + 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() @@ -627,9 +684,13 @@ 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) => 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 { index, @@ -637,6 +698,7 @@ impl Window { end, floored, reach, + looking, }); } self.rebuild(); @@ -645,10 +707,15 @@ impl Window { fn rebuild(&mut self) { self.covered = None; - 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, @@ -682,6 +749,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. +/// 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; + } + if !floored { + return holdings.next_start(0).map(|(oldest, _)| oldest); + } + let next = start.checked_add(1)?; + let (after, _) = holdings.next_start(next)?; + (after > next).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 { @@ -713,6 +796,7 @@ mod tests { held: std::collections::BTreeMap, gone: Vec>, complete: bool, + looks_up: bool, asked: Cell, } @@ -734,6 +818,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() { @@ -1208,4 +1296,59 @@ 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.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)]); + 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)); + } + + /// 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() { + 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 }), + ] + ); + } } diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 086bb1b603b..c4df5a62a67 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -747,16 +747,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. @@ -795,6 +802,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()) @@ -828,24 +840,28 @@ 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)) } 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 { 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 { @@ -853,6 +869,48 @@ 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. @@ -961,9 +1019,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 @@ -5525,6 +5585,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. @@ -6256,6 +6317,13 @@ struct Cursor { finished: Option>>, } +/// 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, +} + /// A group a cursor found but has not moved past; see [`Cursor::take`]. pub(super) struct Peeked { group: group::Consumer, @@ -6404,6 +6472,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, /// Groups skipped as stale, which account for a bounded window as handed out would, /// until a wider budget owes them again. Kept under an open window too, which a reader /// may narrow to a bounded one. @@ -6450,6 +6520,7 @@ impl Cursor { admitted: RangeInclusiveSet::new(), admitted_at: None, owed: RangeInclusiveSet::new(), + lookups: Vec::new(), written_off: RangeSet::new(), }; (filter, state.datagram_offset + start, state.cache.clone()) @@ -6573,6 +6644,7 @@ impl Cursor { Ok(window) => { self.filter.window = window; self.filter.refused = None; + self.filter.lookups.clear(); } Err(err) => self.filter.refused = Some(err), } @@ -6583,7 +6655,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 { @@ -6997,11 +7075,71 @@ 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.filter.lookups.iter().all(|lookup| lookup.join.is_none()) { + return; + } + let fills = self.state.read().fills.clone(); + for (pts, reach) in lookups { + if self.filter.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.filter.lookups.push(Lookup { + pts, + join: joins.into_iter().next(), + }); + } + let mut moved = false; + 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; + 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.may_start(state.looks_up()) + { + 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. /// `taken` is what its subscriber handed out, which an eviction never makes a miss. fn probe(&mut self, from: u64, drift: &Drift, taken: &Delivered, 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 { @@ -7010,6 +7148,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 misses = self.misses.get_or_insert_with(Box::default); let mut given_up = Vec::new(); misses.retain((from, revision, cap), || below(window.missable(from), cap)); @@ -7057,6 +7197,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 @@ -7076,7 +7221,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() } /// Find the next group in arrival order, or in sequence order for `ordered`, without @@ -19616,9 +19761,10 @@ mod test { ); } - /// 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() { @@ -19637,13 +19783,27 @@ 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.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 { 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.wanted() { @@ -19655,6 +19815,91 @@ mod test { assert!(dynamic.poll_requested_fill(&waiter).is_pending()); } + /// 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() { + 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))); + 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.wanted() { + assert!(sequence >= 5, "group {sequence} is below the looked-up start"); + write_filled(fill.accept(sequence, None), sequence); + } + drop(fill); + read.extend(drain_groups(&mut subscriber)); + } + 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 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.wanted() { + 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] diff --git a/rs/moq-net/src/model/track/fill.rs b/rs/moq-net/src/model/track/fill.rs index 3c4a651ac08..f582f0f4a18 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. @@ -403,6 +412,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) @@ -498,6 +516,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)]), @@ -509,6 +528,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 @@ -615,7 +669,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. @@ -626,6 +680,7 @@ pub(super) fn poll_requested( start, end, frame_end, + time, asks, result, }; @@ -688,6 +743,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, } @@ -709,6 +765,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 [`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 { + 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 eff1d137c70..20b4e340f2f 100644 --- a/rs/moq-net/src/model/track/miss.rs +++ b/rs/moq-net/src/model/track/miss.rs @@ -212,6 +212,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) + } + /// What was refilled after an eviction and not handed out yet. #[cfg(all(test, feature = "async"))] pub fn refilled(&self) -> &RangeSet { @@ -821,6 +830,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());