From 1534a6de0ab99e57ef671827a7ae5e7263753273 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 22:54:09 -0800 Subject: [PATCH 01/14] chore(quest): claim quest/m2/outcome-enums/fill-lookups.md Quest-Claim: 994426207daf2a11 From bb7b4841090e111c980967883388a032b1854258 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 23:10:03 -0800 Subject: [PATCH 02/14] wip: a time start is a group fill (moq-net model) --- rs/moq-net/benches/group.rs | 2 +- rs/moq-net/benches/track.rs | 32 +- rs/moq-net/src/ietf/subscriber.rs | 24 +- rs/moq-net/src/lite/subscriber.rs | 22 +- rs/moq-net/src/lite/subscriber/fills.rs | 4 +- rs/moq-net/src/model/resume.rs | 20 +- rs/moq-net/src/model/resume_model.rs | 2 +- rs/moq-net/src/model/subscription.rs | 7 +- rs/moq-net/src/model/subscription/window.rs | 54 +-- rs/moq-net/src/model/track.rs | 348 ++++++++++-------- rs/moq-net/src/model/track/fill.rs | 178 ++++----- rs/moq-net/src/model/track/miss.rs | 5 +- rs/moq-net/tests/integration/fetch_cancel.rs | 4 +- .../tests/integration/fetch_pipeline.rs | 1 - rs/moq-net/tests/integration/flights.rs | 2 +- rs/moq-net/tests/integration/support/mod.rs | 9 - rs/moq-net/tests/integration/untimed.rs | 2 +- 17 files changed, 365 insertions(+), 351 deletions(-) diff --git a/rs/moq-net/benches/group.rs b/rs/moq-net/benches/group.rs index 35db53141f..bd20e1bc5d 100644 --- a/rs/moq-net/benches/group.rs +++ b/rs/moq-net/benches/group.rs @@ -1112,7 +1112,7 @@ fn replay_flights(ctx: &Flights, payloads: &[Bytes], readers: usize, apart: usiz *read += 1; } } - while let Poll::Ready(Ok(track::Fill::Groups(fill))) = ctx.requests.poll_requested_fill(&waiter) { + while let Poll::Ready(Ok(fill)) = ctx.requests.poll_requested_fill(&waiter) { let start = fill.start(); let group = fill.accept(start.group, None).unwrap().expect("a fresh group"); let demand = group.demand(); diff --git a/rs/moq-net/benches/track.rs b/rs/moq-net/benches/track.rs index ac09c38514..b785fd2066 100644 --- a/rs/moq-net/benches/track.rs +++ b/rs/moq-net/benches/track.rs @@ -2051,9 +2051,9 @@ impl Fills { /// Every reader reads what is cached and asks for its misses, then raises its priority, /// which reaches every fill it waits on. A run past the first starts at a time below the - /// feed's start, which each reader looks up first: the handler can't resolve times, so it - /// drops each lookup, and the readers then ask that run as a miss. The handler drops each - /// fill they share, and every reader settles the drops. + /// feed's start, whose group each reader asks a fill of its own for first: the handler + /// can't resolve times, so it refuses each, and the readers then ask that run as a miss. + /// The handler drops each fill they share, and every reader settles the drops. fn run(&mut self) { let waiter = kio::Waiter::noop(); for reader in &mut self.readers { @@ -2066,16 +2066,16 @@ impl Fills { assert!(reader.poll_recv_group(&waiter).is_pending()); } let (mut lookups, mut fills) = (0, 0); - // The lookups go out first, and the misses they leave behind them. + // The time starts go out first, and the misses they leave behind them. for _ in 0..2 { while let Poll::Ready(Ok(fill)) = self.dynamic.poll_requested_fill(&waiter) { - match fill { - track::Fill::Lookup(lookup) => { - assert_eq!(lookup.priority(), 1, "the update reached the lookup"); + assert_eq!(fill.priority(), 1, "the update reached the fill"); + match fill.floor() < fill.start().group { + true => { + fill.refuse_groups(fill.start().group..fill.end(), moq_net::Error::NotFound); lookups += 1; } - track::Fill::Groups(fill) => { - assert_eq!(fill.priority(), 1, "the update reached the fill"); + false => { fill.drop_groups(fill.start().group..fill.end()); fills += 1; } @@ -2160,21 +2160,21 @@ impl RangeFills { /// The readers ask for their misses, the handler accepts every group of every fill once, /// and each reader drains what they filled. Each range starts at a time below the feed's - /// start, which each reader looks up first: the handler can't resolve times, so it drops - /// each lookup, and the readers then ask that range as a miss. + /// start, whose group each reader asks a fill of its own for first: the handler can't + /// resolve times, so it refuses each, and the readers then ask that range as a miss. fn run(&mut self) { let waiter = kio::Waiter::noop(); for reader in &mut self.readers { assert!(reader.poll_recv_group(&waiter).is_pending(), "everything is a miss"); } let mut filled = vec![0; self.readers.len()]; - // The lookups go out first, and the misses they leave behind them. + // The time starts go out first, and the misses they leave behind them. for _ in 0..2 { while let Poll::Ready(Ok(fill)) = self.dynamic.poll_requested_fill(&waiter) { - // A lookup is dropped unanswered. - let track::Fill::Groups(fill) = fill else { + if fill.floor() < fill.start().group { + fill.refuse_groups(fill.start().group..fill.end(), moq_net::Error::NotFound); continue; - }; + } while let Some(key) = fill.wanted() { let mut group = fill.accept(key, None).unwrap().expect("a fresh group"); group @@ -2265,7 +2265,7 @@ impl RefusedFill { reader }) .collect(); - let Poll::Ready(Ok(track::Fill::Groups(fill))) = dynamic.poll_requested_fill(&waiter) else { + let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) else { panic!("the miss below the start is asked"); }; assert!( diff --git a/rs/moq-net/src/ietf/subscriber.rs b/rs/moq-net/src/ietf/subscriber.rs index cc5ddb229e..0d06b32cb6 100644 --- a/rs/moq-net/src/ietf/subscriber.rs +++ b/rs/moq-net/src/ietf/subscriber.rs @@ -4755,7 +4755,7 @@ impl SubscribeOnce { } live.poll_joining(waiter); // An error is the track closing, which the arms below report. - if let Poll::Ready(Ok(requested)) = live.dynamic.poll_requested_groups(waiter) { + if let Poll::Ready(Ok(requested)) = live.dynamic.poll_requested_fill(waiter) { return Poll::Ready(ServeEnd::Fill(requested)); } let _ = group_fetches.poll(waiter); @@ -5303,7 +5303,7 @@ impl Linger { LingerStep::Closed } else if gate.poll_denied(waiter).is_ready() { LingerStep::Revoked - } else if let Poll::Ready(Ok(fill)) = idle.dynamic.poll_requested_groups(waiter) { + } else if let Poll::Ready(Ok(fill)) = idle.dynamic.poll_requested_fill(waiter) { LingerStep::Fill(fill) } else { let _ = group_fetches.poll(waiter); @@ -5931,7 +5931,7 @@ impl LearnTrackStatus { Some(SetupResponse::Cancelled(this.version.transport_error(err))) } else { // A fetch needs nothing from the status to go out, only to read its objects. - while let Poll::Ready(Ok(fill)) = asked.dynamic.poll_requested_groups(waiter) { + while let Poll::Ready(Ok(fill)) = asked.dynamic.poll_requested_fill(waiter) { group_fetches.push(ServeFill::new( this.clone(), broadcast_path.clone(), @@ -10088,7 +10088,7 @@ mod tests { let dynamic = track.requests(); let mut pending = Box::pin(track.consume().fetch_group(0, None)); assert!(futures::poll!(pending.as_mut()).is_pending()); - let request = dynamic.requested_groups().await.expect("group request"); + let request = dynamic.requested_fill().await.expect("group request"); group = Some(pending); accepted = Some(track); Box::pin(subscriber.clone().run_group_fetch( @@ -17301,7 +17301,7 @@ mod stitch_tests { .await .unwrap(); assert!(futures::FutureExt::now_or_never(reading.recv_group()).is_none()); - let fill = dynamic.requested_groups().await.unwrap(); + let fill = dynamic.requested_fill().await.unwrap(); let bounded = consumer.fetch_group(last, None); let slot = kio::Producer::new(GroupFetch::Ready(RangeFetch { fill: std::sync::Arc::new(fill), @@ -17398,7 +17398,7 @@ mod stitch_tests { let waiter = kio::Waiter::noop(); assert!(reader.poll_drop(&waiter).is_pending()); assert!(reader.poll_recv_group(&waiter).is_pending()); - let Poll::Ready(Ok(fill)) = requests.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(fill)) = requests.poll_requested_fill(&waiter) else { panic!("{version}: missing history was not requested"); }; assert_eq!((fill.start().group, fill.end()), (0, 3)); @@ -17449,7 +17449,7 @@ mod stitch_tests { .await .unwrap(); assert!(futures::FutureExt::now_or_never(reading.recv_group()).is_none()); - let fill = dynamic.requested_groups().await.unwrap(); + let fill = dynamic.requested_fill().await.unwrap(); assert_eq!( (fill.start().group, fill.end()), (0, 7), @@ -17649,7 +17649,7 @@ mod stitch_tests { let dynamic = self.track.requests(); let mut fetch = Box::pin(self.track.consume().fetch_group(KEY, None)); assert!(futures::poll!(fetch.as_mut()).is_pending()); - let fill = dynamic.requested_groups().await.expect("group request"); + let fill = dynamic.requested_fill().await.expect("group request"); let slot = kio::Producer::new(GroupFetch::Ready(RangeFetch { fill: std::sync::Arc::new(fill), timescale, @@ -18774,7 +18774,7 @@ mod joining_fetch_tests { let consumer = track.consume(); let mut fetch = Box::pin(consumer.fetch_group(GROUP, None)); assert!(futures::poll!(fetch.as_mut()).is_pending()); - let request = dynamic.requested_groups().await.expect("group request"); + let request = dynamic.requested_fill().await.expect("group request"); let outcome = request.joined(); let mut run = Box::pin(subscriber.clone().run_group_fetch( Path::from_static("broadcast"), @@ -19021,7 +19021,7 @@ mod joining_fetch_tests { let consumer = track.consume(); let mut fetch = std::pin::pin!(consumer.fetch_group(GROUP, None)); assert!(futures::poll!(fetch.as_mut()).is_pending()); - let request = dynamic.requested_groups().await.expect("no group requested"); + let request = dynamic.requested_fill().await.expect("no group requested"); subscriber .clone() @@ -19091,7 +19091,7 @@ mod joining_fetch_tests { } )); assert!(futures::poll!(fetch.as_mut()).is_pending()); - let request = dynamic.requested_groups().await.expect("no group requested"); + let request = dynamic.requested_fill().await.expect("no group requested"); subscriber .clone() @@ -19217,7 +19217,7 @@ mod joining_fetch_tests { } )); assert!(futures::poll!(fetch.as_mut()).is_pending()); - let request = dynamic.requested_groups().await.expect("no group requested"); + let request = dynamic.requested_fill().await.expect("no group requested"); let serving = kio::clock::sim::spawn(subscriber.clone().run_group_fetch( Path::from_static("broadcast"), diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index a681670af0..817bc037a4 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -3192,7 +3192,7 @@ mod tests { track.received(); let dynamic = track.requests(); let fetching = track.consume().fetch_group(key, None); - let req = dynamic.requested_groups().now_or_never().expect("queued").unwrap(); + let req = dynamic.requested_fill().now_or_never().expect("queued").unwrap(); (track, fetching, req) } @@ -3443,7 +3443,7 @@ mod tests { let mut requests = Vec::new(); for key in keys { callers.push(track.consume().fetch_group(key, None)); - requests.push(dynamic.requested_groups().now_or_never().expect("queued").unwrap()); + requests.push(dynamic.requested_fill().now_or_never().expect("queued").unwrap()); } (track, callers, requests) } @@ -3625,7 +3625,7 @@ mod tests { let waiter = kio::Waiter::noop(); // Reading judges the reader's misses. let _ = reader.poll_recv_group(&waiter); - let fill = dynamic.requested_groups().now_or_never().expect("queued").unwrap(); + let fill = dynamic.requested_fill().now_or_never().expect("queued").unwrap(); assert_eq!((fill.start().group, fill.end()), (0, WIDE)); let mut fetches = FeedFetches::new(&clock); @@ -3635,7 +3635,7 @@ mod tests { assert_eq!(fetches.polled, 1); assert!(reader.poll_recv_group(&waiter).is_pending()); assert!( - dynamic.requested_groups().now_or_never().is_none(), + dynamic.requested_fill().now_or_never().is_none(), "a refusal is not asked again" ); } @@ -3660,7 +3660,7 @@ mod tests { let mut joined: Vec<_> = (1..=3).map(|key| track.consume().fetch_group(key, None)).collect(); assert!( - dynamic.requested_groups().now_or_never().is_none(), + dynamic.requested_fill().now_or_never().is_none(), "joins existing fills" ); drop(callers); @@ -3673,7 +3673,7 @@ mod tests { assert!(fetches.held.is_empty(), "the last reader leaving releases every fill"); assert!(fetches.ready.read().is_empty()); assert_eq!(fetches.fold.deadline(), None); - assert!(dynamic.requested_groups().now_or_never().is_none()); + assert!(dynamic.requested_fill().now_or_never().is_none()); } #[kio::test] @@ -3690,7 +3690,7 @@ mod tests { drop(caller); let next = track.consume().fetch_group(1, None); let req = dynamic - .requested_groups() + .requested_fill() .now_or_never() .expect("replacement queued") .unwrap(); @@ -5615,7 +5615,7 @@ mod tests { ) .ordered(); assert!(reader.poll_next_group(&waiter).is_pending(), "groups 0..4 are owed"); - let Poll::Ready(Ok(fill)) = requests.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(fill)) = requests.poll_requested_fill(&waiter) else { panic!("groups 0..4 are a miss"); }; let mut fills = fills::RangeFills::default(); @@ -11347,7 +11347,7 @@ impl FinishedServe { let Some(serving) = &self.serving else { return Poll::Ready(()); }; - match self.dynamic.poll_requested_groups(waiter) { + match self.dynamic.poll_requested_fill(waiter) { Poll::Ready(Ok(fill)) => { let info = FetchInfo::Known(serving.info().clone()); self.fetches.push(FillServeRun::new(serve.clone(), fill, info)); @@ -11681,7 +11681,7 @@ impl TrackServeRun { .. } => { // A fetch needs nothing from TRACK_INFO to go out, only to read its answer. - while let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(waiter) { + while let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(waiter) { fetches.push(FillServeRun::new(self.serve.clone(), fill, known.later())); } // Abandonment was checked at the top of this poll, arming its wait. @@ -12895,7 +12895,7 @@ impl ServeLoop { // A fill is bounded and serves readers already waiting, so serve it ahead of // subscription churn. - match self.dynamic.poll_requested_groups(waiter) { + match self.dynamic.poll_requested_fill(waiter) { Poll::Ready(Ok(fill)) => { // Lite-07 asks for more than one group through the subscription. One // group, or a slice of one, is what FETCH carries. diff --git a/rs/moq-net/src/lite/subscriber/fills.rs b/rs/moq-net/src/lite/subscriber/fills.rs index ee7adceca1..ebe71857b2 100644 --- a/rs/moq-net/src/lite/subscriber/fills.rs +++ b/rs/moq-net/src/lite/subscriber/fills.rs @@ -853,7 +853,7 @@ mod tests { ) .ordered(); assert!(reader.poll_next_group(&waiter).is_pending(), "groups 0..3 are owed"); - let Poll::Ready(Ok(fill)) = requests.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(fill)) = requests.poll_requested_fill(&waiter) else { panic!("groups 0..3 are a miss"); }; (reader, fill) @@ -902,7 +902,7 @@ mod tests { .ordered(); let _ = reader.poll_next_group(&waiter); assert!( - requests.poll_requested_groups(&waiter).is_pending(), + requests.poll_requested_fill(&waiter).is_pending(), "a lost group is not asked again" ); } diff --git a/rs/moq-net/src/model/resume.rs b/rs/moq-net/src/model/resume.rs index 7858b13fff..559d54a32a 100644 --- a/rs/moq-net/src/model/resume.rs +++ b/rs/moq-net/src/model/resume.rs @@ -5118,7 +5118,7 @@ mod test { let dynamic = b.requests(); routes.serve(b.consume()); assert!(reading.read_frame().now_or_never().is_none(), "fails={fails}"); - let _pending = dynamic.requested_groups().now_or_never().unwrap().unwrap(); + let _pending = dynamic.requested_fill().now_or_never().unwrap().unwrap(); if abort { b.abort(Error::Stream(StreamError::App(5))).unwrap(); @@ -5669,7 +5669,7 @@ mod test { let mut fetching = logical.consume().fetch_group(0, None); assert!((&mut fetching).now_or_never().is_none()); let fill = dynamic - .requested_groups() + .requested_fill() .now_or_never() .expect("the fetch reached the staged copy") .unwrap(); @@ -5703,7 +5703,7 @@ mod test { let staged = routes.stage(refused.consume()); let mut fetching = logical.consume().fetch_group(0, None); assert!((&mut fetching).now_or_never().is_none()); - let fill = refused_dynamic.requested_groups().now_or_never().unwrap().unwrap(); + let fill = refused_dynamic.requested_fill().now_or_never().unwrap().unwrap(); let wrong = refused.accept(crate::track::Info::timed()); let mut group = fill .accept(0, crate::track::Info::timed()) @@ -5718,7 +5718,7 @@ mod test { let staged = routes.stage(request.consume()); assert!((&mut fetching).now_or_never().is_none()); let fill = dynamic - .requested_groups() + .requested_fill() .now_or_never() .expect("asked again of the next route") .unwrap(); @@ -5747,7 +5747,7 @@ mod test { let mut fetching = logical.consume().fetch_group(0, None); assert!((&mut fetching).now_or_never().is_none()); assert!( - dynamic.requested_groups().now_or_never().is_none(), + dynamic.requested_fill().now_or_never().is_none(), "the withdrawn copy is not asked" ); @@ -5769,7 +5769,7 @@ mod test { let mut fetching = logical.consume().fetch_group(0, None); assert!((&mut fetching).now_or_never().is_none()); assert!( - dynamic.requested_groups().now_or_never().is_some(), + dynamic.requested_fill().now_or_never().is_some(), "splice={splice}: the newer stage is asked" ); } @@ -5803,7 +5803,7 @@ mod test { let mut fetching = logical.consume().fetch_group(0, None); let waiter = kio::Waiter::noop(); assert!(kio::Task::poll(&mut *fetching, &waiter).is_pending()); - let fill = dynamic.requested_groups().now_or_never().unwrap().unwrap(); + let fill = dynamic.requested_fill().now_or_never().unwrap().unwrap(); if refused { fill.refuse_groups(0..1, Error::Unauthorized); assert!( @@ -5832,7 +5832,7 @@ mod test { let _staged = routes.stage(request.consume()); let mut fetching = logical.consume().fetch_group(0, None); assert!((&mut fetching).now_or_never().is_none()); - let fill = dynamic.requested_groups().now_or_never().unwrap().unwrap(); + let fill = dynamic.requested_fill().now_or_never().unwrap().unwrap(); let _staged_copy = request.accept(crate::track::Info::timed()); let mut group = fill .accept(0, crate::track::Info::timed()) @@ -5851,7 +5851,7 @@ mod test { "the stage's answer is not handed out" ); let fill = b_dynamic - .requested_groups() + .requested_fill() .now_or_never() .expect("asked of the serving copy") .unwrap(); @@ -5877,7 +5877,7 @@ mod test { let logical = logical(&routes); let mut fetching = logical.consume().fetch_group(0, None); assert!((&mut fetching).now_or_never().is_none()); - let fill = dynamic.requested_groups().now_or_never().unwrap().unwrap(); + let fill = dynamic.requested_fill().now_or_never().unwrap().unwrap(); fill.refuse_groups(0..1, Error::Dropped); assert!((&mut fetching).now_or_never().is_none(), "the next route is asked"); diff --git a/rs/moq-net/src/model/resume_model.rs b/rs/moq-net/src/model/resume_model.rs index e941713108..5f35e8b9a5 100644 --- a/rs/moq-net/src/model/resume_model.rs +++ b/rs/moq-net/src/model/resume_model.rs @@ -555,7 +555,7 @@ impl Model { for slot in 0..SLOTS { while let Some(Poll::Ready(Ok(fill))) = self.instances[instance].routes[slot] .as_ref() - .map(|copy| copy.fills.poll_requested_groups(&kio::Waiter::noop())) + .map(|copy| copy.fills.poll_requested_fill(&kio::Waiter::noop())) { filled = true; let (start, end) = (fill.start().group, fill.end()); diff --git a/rs/moq-net/src/model/subscription.rs b/rs/moq-net/src/model/subscription.rs index 8ca1f36de3..5815674267 100644 --- a/rs/moq-net/src/model/subscription.rs +++ b/rs/moq-net/src/model/subscription.rs @@ -213,9 +213,10 @@ pub enum Anchor { /// /// Resolved from the starts of the groups the serving hop holds. When none starts at /// or before it, a hop whose [`Requests`](crate::track::Requests) handler serves its - /// history looks the start up there (see [`FillLookup`](crate::track::FillLookup)), - /// reading nothing until it answers; otherwise, or when the handler can't tell, a start - /// reaches back to the oldest group. While no group is held at all it waits for one, + /// history asks it for the group containing the time (see + /// [`FillGroups::floor`](crate::track::FillGroups::floor)), reading nothing until it + /// answers; otherwise, or when the handler can't tell, a start reaches back to the oldest + /// group. While no group is held at all it waits for one, /// reading nothing of its range meanwhile. An end includes every group starting at or /// before it. /// diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index 444aa35415..93658b5841 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -214,8 +214,8 @@ struct Span { end: Option, /// Whether the start names a group, rather than reaching back to the oldest one. floored: bool, - /// A time start the groups held can't place, looked up from the track's history: the - /// span reads nothing until the lookup answers. + /// A time start the groups held can't place, whose group a fill asks the track's history + /// for: the span reads nothing until it answers. looking: Option, /// A time start the groups held can't place yet: the span reads only the groups from /// that time until it can. @@ -226,12 +226,12 @@ struct Span { missable: Option>, } -/// A time start being looked up: its time, and the exclusive end of the run below every -/// group held, which holds the group containing it if the track's history does. +/// A time start being looked up: its time, and the oldest key the group containing it may +/// start at, past every group held below it. #[derive(Clone, Copy, Debug)] struct Looking { time: u64, - below: u64, + floor: u64, } impl Window { @@ -408,8 +408,8 @@ impl Window { let mut span = self.spans.remove(&key).expect("a waiting span"); self.uncount(key, &span); (span.start, span.floored, span.waiting) = (start, floored, false); - if let Some(below) = unresolved(time, start, floored, holdings).filter(|_| asked) { - span.looking = Some(Looking { time, below }); + if let Some(floor) = unresolved(time, start, floored, holdings).filter(|_| asked) { + span.looking = Some(Looking { time, floor }); } self.count(key, span, holdings); } @@ -593,18 +593,19 @@ 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. + /// The time starts still looked up, each with the oldest key the group containing it may + /// start at: past every group held below the time, which holds it if the track's history + /// has one. pub fn lookups(&self) -> impl Iterator + '_ { self.looking .iter() - .filter_map(|(_, key)| self.spans[key].looking.map(|looking| (looking.time, looking.below))) + .filter_map(|(_, key)| self.spans[key].looking.map(|looking| (looking.time, looking.floor))) } - /// The lookup of time start `time` answered with the group containing it, or the oldest - /// group of the history when `time` is older than all of it: its spans start there, once - /// that group is held. `None`, or an answer not held, means the history couldn't tell, so - /// they start where the groups held say. Returns whether the groups in range moved. + /// The lookup of time start `time` answered with the group containing it: its spans start + /// there, once that group is held. `None`, or an answer not held, means the history + /// couldn't tell, or holds no group there, so they start where the groups held say. + /// Returns whether the groups in range moved. pub fn found(&mut self, time: u64, group: Option, holdings: &impl Holdings) -> bool { let found: Vec = self .looking @@ -696,7 +697,7 @@ impl Window { // join's reach back never looks one up: it starts from what this hop holds. let looking = match (range.start, anchor) { (Anchor::Time(_), Anchor::Time(time)) => { - unresolved(time, start, floored, holdings).map(|below| Looking { time, below }) + unresolved(time, start, floored, holdings).map(|floor| Looking { time, floor }) } _ => None, }; @@ -900,21 +901,22 @@ fn start(anchor: Anchor, latest: Option, holdings: &impl Holdings) -> Start } } -/// The exclusive end of the run a lookup of time start `time`, resolved to `start`, searches, -/// when the groups held can't tell which group contains it and a handler may: it is before -/// every group held, or a group the cache evicted may start after `start`, the newest held -/// at or before it, and still at or before `time`. Keys the producer skipped hold none. +/// The oldest key a fill of the group containing time start `time`, resolved to `start`, +/// asks from, when the groups held can't tell which group contains it and a handler may: it +/// is before every group held, or a group the cache evicted may start after `start`, the +/// newest held at or before it, and still at or before `time`. Keys the producer skipped +/// hold none. fn unresolved(time: u64, start: u64, floored: bool, holdings: &impl Holdings) -> Option { if !holdings.looks_up() { return None; } if !floored { - return holdings.next_start(0); + return holdings.next_start(0).map(|_| 0); } let next = start.checked_add(1)?; let after = holdings.next_start(next)?; let gap = next..after.min(time.saturating_add(1)); - (!holdings.never_produced(gap)).then_some(after) + (!holdings.never_produced(gap)).then_some(next) } /// Where a range stops, exclusive, or `None` while it waits on a latest group. `Some(None)` @@ -1404,8 +1406,8 @@ mod tests { assert!(window.covered_by(0, None, &accounted)); } - /// 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 with a group the cache evicted + /// With a handler serving the history, a time start the groups held can't place asks a + /// fill for its group from past the newest held below it, and reads nothing meanwhile: before every start, or with a group the cache evicted /// possibly starting between the newest held at or before it and the time. The answer /// places it; none leaves what the groups held say. #[test] @@ -1413,7 +1415,7 @@ mod tests { let mut keys = Keys::new([4000, 9000]); keys.looks_up = true; let mut window = Window::new(&[range(time(8500), None)], None, Some(9000), &keys).unwrap(); - assert_eq!(window.lookups().collect::>(), [(8500, 9000)]); + assert_eq!(window.lookups().collect::>(), [(8500, 4001)]); assert!(!window.is_settled() && !window.contains(9000) && window.resolved() == [None]); keys.held.insert(8000); assert!(window.found(8500, Some(8000), &keys)); @@ -1433,7 +1435,7 @@ mod tests { assert_eq!(window.lookups().count(), 0, "8000 starts exactly there"); let mut window = Window::new(&[range(time(500), None)], None, Some(9000), &keys).unwrap(); - assert_eq!(window.lookups().collect::>(), [(500, 4000)]); + assert_eq!(window.lookups().collect::>(), [(500, 0)]); assert!(window.found(500, None, &keys)); assert_eq!( window.resolved(), @@ -1465,7 +1467,7 @@ mod tests { let mut keys = Keys::new([4000, 9000]); keys.looks_up = true; let mut window = Window::new(&[range(time(500), time(3000))], None, Some(9000), &keys).unwrap(); - assert_eq!(window.lookups().collect::>(), [(500, 4000)]); + assert_eq!(window.lookups().collect::>(), [(500, 0)]); assert!(window.found(500, None, &keys)); assert!(window.is_settled()); assert_eq!( diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 1916410c2b..3c6de0a5b5 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -37,7 +37,7 @@ mod hole; mod miss; pub(super) use fill::Asking; use fill::FillState; -pub use fill::{Fill, FillGroups, FillLookup}; +pub use fill::FillGroups; pub(crate) use hole::Hole; #[cfg_attr(not(feature = "async"), allow(unused_imports, reason = "async-only"))] @@ -5685,11 +5685,11 @@ impl Finished { /// Most tracks never serve old content, so this capability lives on a dedicated /// handle rather than [`Producer`]: a relay creates one (via /// [`Producer::requests`] or [`Request::requests`]) to pull past groups -/// from upstream. While at least one is alive a reader's miss queues a [`Fill`] for it +/// from upstream. While at least one is alive a reader's miss queues a [`FillGroups`] for it /// to serve; with none, the miss is named dropped, and a cache-miss /// [`Consumer::fetch_group`] on an accepted track fails fast with [`Error::NotFound`]. -/// Keep taking fills until it drops: a reader waits on its [`FillLookup`] until it is -/// answered or dropped, so a handle left alive but unread stalls that reader. +/// Keep taking fills until it drops: a reader waits on its fill until it is answered or +/// dropped, so a handle left alive but unread stalls that reader. /// /// A live handle also lets a reader at a frame a group copy shed replay it: a [`FillGroups`] of /// that one group from that frame asks for a flight, a copy of its own paced by the readers @@ -5738,33 +5738,22 @@ impl Requests { &self.name } - /// The next [`Fill`] readers ask: a run of groups they miss, to serve via - /// [`FillGroups::accept`] and [`FillGroups::drop_groups`], or a time to look up. + /// The next run of groups readers miss, to serve via [`FillGroups::accept`] and + /// [`FillGroups::drop_groups`], including the group containing a reader's time start that + /// no group the cache holds places (see [`FillGroups::floor`]). /// /// A relay fetches the run upstream; an origin serves the groups its cache evicted /// from its own store, as `moq-archive` does. Errors once the track is aborted. #[cfg_attr(feature = "async", kio::wrapper)] - pub fn poll_requested_fill(&self, waiter: &kio::Waiter) -> Poll> { + pub fn poll_requested_fill(&self, waiter: &kio::Waiter) -> Poll> { fill::poll_requested(&self.state, &self.fills, waiter) } - /// [`Self::poll_requested_fill`] for a handler that can't resolve times: each lookup is - /// dropped unanswered, so its reader falls back to the groups held. - #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] - #[cfg_attr(feature = "async", kio::wrapper)] - pub(crate) fn poll_requested_groups(&self, waiter: &kio::Waiter) -> Poll> { - loop { - if let Fill::Groups(fill) = ready!(self.poll_requested_fill(waiter))? { - return Poll::Ready(Ok(fill)); - } - } - } - /// The next fill, which a test expects to name a single group. #[cfg(test)] #[cfg_attr(feature = "async", kio::wrapper)] pub(crate) fn poll_requested_fill_group(&self, waiter: &kio::Waiter) -> Poll> { - self.poll_requested_groups(waiter).map_ok(|fill| { + self.poll_requested_fill(waiter).map_ok(|fill| { assert_eq!(fill.end() - fill.start().group, 1, "a one-group fill"); fill.group(fill.start().group) }) @@ -7473,7 +7462,6 @@ fn queue(fills: &kio::Shared, key: u64, request: &FetchOptions) -> Op // One group either way, so it never turns a wider fill it joins newest first. order: Order::Ascending, }, - time: None, fetch: true, }; // The handler gate is atomic with a handler dropping (no fill stranded on a queue @@ -9722,15 +9710,15 @@ 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. + /// Ask a fill for the group containing each time start the window can't resolve from the + /// cache, once, and take in the answers, parking `waiter` on the fills still out. /// /// Indexed by time, so a pass costs O(L log L) in the lookups out rather than O(L²). fn look_up(&mut self, waiter: &kio::Waiter) { - // The first span looking a time up sets how far its lookup reaches. + // The first span looking a time up sets the floor its fill asks from. let mut lookups = BTreeMap::new(); - for (time, reach) in self.filter.window.lookups() { - lookups.entry(time).or_insert(reach); + for (time, floor) in self.filter.window.lookups() { + lookups.entry(time).or_insert(floor); } if lookups.is_empty() && self.filter.lookups.iter().all(|lookup| lookup.join.is_none()) { return; @@ -9738,23 +9726,12 @@ impl Cursor { let asking = &asking(&self.subscription); let fills = self.state.read().fills.clone(); let asked: BTreeSet = self.filter.lookups.iter().map(|lookup| lookup.time).collect(); - for (&time, &reach) in &lookups { + for (&time, &floor) in &lookups { if asked.contains(&time) { continue; } - let ask = fill::Ask { - start: Position::group(0), - end: reach, - frame_end: None, - asking: *asking, - time: Some(time), - fetch: false, - }; - let (joins, _) = fills.lock().join(&fills, &ask); - self.filter.lookups.push(Lookup { - time, - join: joins.into_iter().next(), - }); + let join = fills.lock().time_start(&fills, time, floor, *asking); + self.filter.lookups.push(Lookup { time, join }); } let mut moved = false; for lookup in &mut self.filter.lookups { @@ -9768,14 +9745,16 @@ impl Cursor { if let Some(join) = &mut lookup.join { join.update(asking); } + let time = lookup.time; 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), + Some(join) => match join + .outcome() + .poll(waiter, |outcome| outcome.time_start(time).map_or(Poll::Pending, Poll::Ready)) + { + Poll::Ready(Ok(group)) => group, // Ended unanswered: its handler couldn't tell. - Poll::Ready(Err(closed)) => closed.first_accepted(), + Poll::Ready(Err(closed)) => closed.time_start(time).flatten(), Poll::Pending => continue, }, }; @@ -14173,7 +14152,7 @@ mod test { let consumer = producer.consume(); let fetch = consumer.fetch_group(u64::MAX, None).now_or_never(); assert!(matches!(fetch, Some(Err(Error::NotFound)))); - assert!(dynamic.requested_groups().now_or_never().is_none(), "nothing to fill"); + assert!(dynamic.requested_fill().now_or_never().is_none(), "nothing to fill"); } #[test] @@ -15492,7 +15471,7 @@ mod test { ); let waiter = kio::Waiter::noop(); assert_eq!(read_now(&mut reader, false), None); - let Poll::Ready(Ok(fill)) = requests.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(fill)) = requests.poll_requested_fill(&waiter) else { panic!("cached: {cached}: missing history must be requested"); }; assert_eq!( @@ -15547,7 +15526,7 @@ mod test { let mut reader = producer.subscribe(unlimited.clone()); let waiter = kio::Waiter::noop(); assert_eq!(read_now(&mut reader, true), None); - let Poll::Ready(Ok(fill)) = requests.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(fill)) = requests.poll_requested_fill(&waiter) else { panic!("missing history must be requested"); }; assert_eq!( @@ -15663,7 +15642,7 @@ mod test { assert_eq!(read_now(&mut reader, ordered), Some(Some(3))); } assert_eq!(read_now(&mut reader, ordered), None, "the missing run is owed"); - let Poll::Ready(Ok(fill)) = requests.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(fill)) = requests.poll_requested_fill(&waiter) else { panic!("the missing history was never requested"); }; assert_eq!((fill.start(), fill.end()), (Position::group(1), 3)); @@ -25212,10 +25191,10 @@ mod test { let first = consumer.fetch_group(3, None); let second = consumer.fetch_group(3, None); let waiter = kio::Waiter::noop(); - let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) else { panic!("one fill for both fetches"); }; - assert!(dynamic.poll_requested_groups(&waiter).is_pending(), "the second joined"); + assert!(dynamic.poll_requested_fill(&waiter).is_pending(), "the second joined"); assert_eq!((fill.start(), fill.end()), (Position::group(3), 4)); assert_eq!(fill.wanted(), Some(3)); drop(first); @@ -25238,7 +25217,7 @@ mod test { let consumer = producer.consume(); let mut fetch = consumer.fetch_group(3, None); let waiter = kio::Waiter::noop(); - let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) else { panic!("the fill is ready"); }; let group = fill.accept(3, Info::timed()).unwrap().expect("a fresh group"); @@ -25294,7 +25273,7 @@ mod test { read.push(group.start); } assert_eq!(read, [2, 3]); - let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) else { panic!("below the start is a miss"); }; assert_eq!((fill.start().group, fill.end()), (0, 2)); @@ -25310,7 +25289,7 @@ mod test { elapse(&producer, Duration::from_secs(31)); assert_eq!(drained_drops(&mut subscriber), [(1, 2)]); assert!( - dynamic.poll_requested_groups(&waiter).is_pending(), + dynamic.poll_requested_fill(&waiter).is_pending(), "a refusal is not asked again" ); } @@ -25339,7 +25318,7 @@ mod test { let waiter = kio::Waiter::noop(); assert_eq!(drain_groups(&mut subscriber), [2, 3]); assert!( - dynamic.poll_requested_groups(&waiter).is_pending(), + dynamic.poll_requested_fill(&waiter).is_pending(), "below the start is no miss" ); assert!(drained_drops(&mut subscriber).is_empty()); @@ -25347,7 +25326,7 @@ mod test { // The re-offer of a widened budget. write(&producer, 1); assert_eq!(drain_groups(&mut subscriber), [1]); - assert!(dynamic.poll_requested_groups(&waiter).is_pending()); + assert!(dynamic.poll_requested_fill(&waiter).is_pending()); } /// Below a start its publisher resolved, a reader asks for the history its budget @@ -25368,7 +25347,7 @@ mod test { drained_drops(&mut reader).is_empty(), "nothing below the start is given up unasked" ); - let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) else { panic!("the reader asks below the start"); }; assert_eq!((fill.start().group, fill.end()), (0, 2)); @@ -25389,13 +25368,13 @@ mod test { let waiter = kio::Waiter::noop(); assert_eq!(drain_groups(&mut reader), [2, 3]); assert!( - dynamic.poll_requested_groups(&waiter).is_pending(), + dynamic.poll_requested_fill(&waiter).is_pending(), "below a declared start is no miss" ); producer.start_resolved(2).unwrap(); assert!(reader.poll_recv_group(&waiter).is_pending()); - let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) else { panic!("the reader asks below the resolved start"); }; assert_eq!((fill.start().group, fill.end()), (0, 2)); @@ -25409,7 +25388,7 @@ mod test { let (producer, dynamic, mut first) = started_late(); let waiter = kio::Waiter::noop(); while let Poll::Ready(Ok(Some(_))) = first.poll_recv_group(&waiter) {} - let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) else { panic!("below the start is a miss"); }; while let Some(key) = fill.wanted() { @@ -25423,7 +25402,7 @@ mod test { read.sort(); assert_eq!(read, [0, 1, 2, 3]); assert!( - dynamic.poll_requested_groups(&waiter).is_pending(), + dynamic.poll_requested_fill(&waiter).is_pending(), "the cache answers it" ); } @@ -25441,7 +25420,7 @@ mod test { write_group(&producer, 2); let waiter = kio::Waiter::noop(); while let Poll::Ready(Ok(Some(_))) = subscriber.poll_recv_group(&waiter) {} - let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) else { panic!("below the start is a miss"); }; while let Some(key) = fill.wanted() { @@ -25473,7 +25452,7 @@ mod test { }; assert_eq!(next(&mut subscriber), None, "groups 0 and 1 are being filled"); // The handler takes the fill and never answers it. - let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) else { panic!("below the start is a miss"); }; elapse(&producer, Duration::from_secs(29)); @@ -25502,7 +25481,7 @@ mod test { "group 0 is being filled" ); // The handler takes the fill and never answers it. - let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) else { panic!("below the start is a miss"); }; assert_eq!((fill.start().group, fill.end()), (0, 10)); @@ -25539,7 +25518,7 @@ mod test { write_group(&producer, 10); let waiter = kio::Waiter::noop(); let fetch = producer.consume().fetch_group(3, None); - let Poll::Ready(Ok(first)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(first)) = dynamic.poll_requested_fill(&waiter) else { panic!("below the start is a miss"); }; let group = first.accept(3, None).unwrap().expect("a fresh group"); @@ -25552,7 +25531,7 @@ mod test { subscriber.poll_next_group(&waiter).is_pending(), "group 0 is being filled" ); - let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) else { panic!("the aborted group is a miss again"); }; assert_eq!((fill.start().group, fill.end()), (0, 10)); @@ -25578,7 +25557,7 @@ mod test { ..Default::default() }, ); - let Poll::Ready(Ok(first)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(first)) = dynamic.poll_requested_fill(&waiter) else { panic!("an evicted group is a miss"); }; first.refuse_groups(3..4, Error::Unsupported); @@ -25592,7 +25571,7 @@ mod test { subscriber.poll_next_group(&waiter).is_pending(), "group 3 is being filled" ); - let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) else { panic!("the source may still have group 3"); }; assert!(fill.is_wanted(3)); @@ -25611,7 +25590,7 @@ mod test { evict(&producer, 3); let waiter = kio::Waiter::noop(); let fetch = producer.consume().fetch_group(3, None); - let Poll::Ready(Ok(first)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(first)) = dynamic.poll_requested_fill(&waiter) else { panic!("an evicted group is a miss"); }; first.group(3).reject(Error::NotFound); @@ -25625,7 +25604,7 @@ mod test { subscriber.poll_next_group(&waiter).is_pending(), "group 3 is being filled" ); - let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) else { panic!("the source may still have part of group 3"); }; assert!(fill.is_wanted(3)); @@ -25644,7 +25623,7 @@ mod test { write_group(&producer, 10); let waiter = kio::Waiter::noop(); while let Poll::Ready(Ok(Some(_))) = subscriber.poll_recv_group(&waiter) {} - let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) else { panic!("below the start is a miss"); }; // The window inside `FillGroups::accept` between caching the group and publishing the answer. @@ -25675,7 +25654,7 @@ mod test { subscriber.poll_next_group(&waiter).is_pending(), "group 0 is being filled" ); - let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) else { panic!("below the start is a miss"); }; let _writing = fill.accept(0, None).unwrap().expect("a fresh group"); @@ -25701,7 +25680,7 @@ mod test { write_group(&producer, 10); let noop = kio::Waiter::noop(); while let Poll::Ready(Ok(Some(_))) = subscriber.poll_recv_group(&noop) {} - let Poll::Ready(Ok(mut fill)) = dynamic.poll_requested_groups(&noop) else { + let Poll::Ready(Ok(mut fill)) = dynamic.poll_requested_fill(&noop) else { panic!("below the start is a miss"); }; let (flag, waiter) = Flag::waiter(); @@ -25748,7 +25727,7 @@ mod test { write_group(&producer, 1); let noop = kio::Waiter::noop(); while let Poll::Ready(Ok(Some(_))) = subscriber.poll_recv_group(&noop) {} - let Poll::Ready(Ok(mut fill)) = dynamic.poll_requested_groups(&noop) else { + let Poll::Ready(Ok(mut fill)) = dynamic.poll_requested_fill(&noop) else { panic!("below the start is a miss"); }; let (flag, waiter) = Flag::waiter(); @@ -25777,7 +25756,7 @@ mod test { write_group(&producer, 10); let waiter = kio::Waiter::noop(); while let Poll::Ready(Ok(Some(_))) = subscriber.poll_recv_group(&waiter) {} - let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) else { panic!("below the start is a miss"); }; assert_eq!(fill.wanted(), Some(9)); @@ -25820,7 +25799,7 @@ mod test { write_group(&producer, 10); let waiter = kio::Waiter::noop(); while let Poll::Ready(Ok(Some(_))) = subscriber.poll_recv_group(&waiter) {} - let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) else { panic!("below the start is a miss"); }; let _writing = [ @@ -25857,7 +25836,7 @@ mod test { write_group(&producer, 10); let waiter = kio::Waiter::noop(); while let Poll::Ready(Ok(Some(_))) = subscriber.poll_recv_group(&waiter) {} - let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) else { panic!("below the start is a miss"); }; let group = fill.group(9); @@ -25874,10 +25853,11 @@ mod test { ); } - /// 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 - /// group containing its time, which is unknown, so it asks no fill below the time itself: - /// every key from there is in range. Another range still asks for its own misses. + /// A time start before every group held asks a fill for the group containing its time, + /// one key wide from the time and floored at 0, which no other reader joins: a handler + /// that can't tell refuses it, and the start reaches back to that group, which is unknown, + /// so it asks no fill below the time itself: every key from there is in range. Another + /// range still asks for its own misses. #[test] #[cfg(feature = "async")] fn a_time_start_before_the_cache_asks_nothing_below_it() { @@ -25898,16 +25878,18 @@ mod test { assert_eq!( drain_groups(&mut subscriber), Vec::::new(), - "the time start waits on its lookup" + "the time start waits on its fill" ); - let Poll::Ready(Ok(Fill::Lookup(lookup))) = dynamic.poll_requested_fill(&waiter) else { - panic!("the time start is looked up"); + let Poll::Ready(Ok(start)) = dynamic.poll_requested_fill(&waiter) else { + panic!("the time start asks for its group"); }; - assert_eq!((lookup.time(), lookup.span()), (5, 0..10)); - drop(lookup); + assert_eq!((start.start(), start.floor(), start.end()), (Position::group(5), 0, 6)); + assert_eq!(start.wanted(), Some(5), "a handler knowing only keys serves the time's"); + start.refuse_groups(5..6, Error::NotFound); + drop(start); let mut read = drain_groups(&mut subscriber); let mut asked = Vec::new(); - while let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) { + while let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) { asked.push((fill.start().group, fill.end())); while let Some(key) = fill.wanted() { write_filled(fill.accept(key, None), key); @@ -25919,7 +25901,7 @@ mod test { assert_eq!(read, [10, 0, 1, 5, 6, 7, 8, 9]); } - /// A time start before every group held starts at the group its lookup answers, whose key + /// A time start before every group held starts at the group its fill answers, whose key /// says where it starts, asking nothing below it; the misses past it through the time end /// are filled. #[test] @@ -25939,17 +25921,17 @@ mod test { Vec::::new(), "nothing reads until it is found" ); - let Poll::Ready(Ok(Fill::Lookup(lookup))) = dynamic.poll_requested_fill(&waiter) else { - panic!("the time start is looked up"); + let Poll::Ready(Ok(start)) = dynamic.poll_requested_fill(&waiter) else { + panic!("the time start asks for its group"); }; - assert_eq!(lookup.time(), 5); - let found = lookup.accept(5, None); - drop(lookup); + assert_eq!(start.start().group, 5); + let found = start.accept(5, None); + drop(start); let mut read = drain_groups(&mut subscriber); assert_eq!(read, [5], "the group found is read at once: its key is its start"); write_filled(found, 5); // The misses past the start, through the end. - while let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) { + while let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) { while let Some(key) = fill.wanted() { assert!(key >= 5, "group {key} is below the looked-up start"); write_filled(fill.accept(key, None), key); @@ -25961,6 +25943,70 @@ mod test { assert_eq!(read, [5, 7, 6]); } + /// A handler that can tell which group holds a time, as from an index, answers a time + /// start with a group starting below it, down to the floor: the reader starts there and + /// asks the keys after it as misses. Below the floor is outside the fill, since the + /// cache holds a newer group at or before the time there. + #[test] + #[cfg(feature = "async")] + fn a_time_start_is_answered_by_the_group_below_it() { + let mut producer = track_producer("test", Info::timed()); + let dynamic = producer.requests(); + let mut subscriber = producer.subscribe(replay().with_order(Order::Ascending).with_ranges([Range { + start: Anchor::Time(7), + end: Some(Anchor::Time(8)), + }])); + producer.start_at(10).unwrap(); + write_group(&producer, 10); + let waiter = kio::Waiter::noop(); + assert_eq!(drain_groups(&mut subscriber), Vec::::new()); + let Poll::Ready(Ok(start)) = dynamic.poll_requested_fill(&waiter) else { + panic!("the time start asks for its group"); + }; + assert_eq!((start.start().group, start.floor()), (7, 0)); + write_filled(start.accept(4, None), 4); + assert!(start.accept(7, None).unwrap().is_none(), "the start is answered"); + assert_eq!(start.wanted(), None); + drop(start); + let mut read = drain_groups(&mut subscriber); + let mut asked = Vec::new(); + while let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) { + asked.push((fill.start().group, fill.end())); + // The index says 5 through 7 hold none, as the answer did. + fill.drop_groups(5..8); + while let Some(key) = fill.wanted() { + write_filled(fill.accept(key, None), key); + } + drop(fill); + read.extend(drain_groups(&mut subscriber)); + } + assert_eq!(asked, [(5, 9)]); + assert_eq!(read, [4, 8]); + + // Past a group held below the time, the floor is past it too. + let producer = track_producer("test", Info::timed()); + let dynamic = producer.requests(); + for key in 0..10 { + write_group(&producer, key); + } + for key in 5..9 { + evict(&producer, key); + } + let mut subscriber = producer.subscribe(replay().with_ranges([Range { + start: Anchor::Time(7), + end: Some(Anchor::Time(7)), + }])); + assert_eq!(drain_groups(&mut subscriber), Vec::::new()); + let Poll::Ready(Ok(start)) = dynamic.poll_requested_fill(&waiter) else { + panic!("the time start asks for its group"); + }; + assert_eq!((start.start().group, start.floor()), (7, 5)); + assert!(matches!(start.accept(4, None), Err(Error::InvalidRange))); + write_filled(start.accept(6, None), 6); + drop(start); + assert_eq!(drain_groups(&mut subscriber), [6]); + } + /// 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. @@ -25981,14 +26027,14 @@ mod test { }])); let waiter = kio::Waiter::noop(); assert_eq!(drain_groups(&mut subscriber), Vec::::new()); - let Poll::Ready(Ok(Fill::Lookup(lookup))) = dynamic.poll_requested_fill(&waiter) else { - panic!("the time start in the evicted gap is looked up"); + let Poll::Ready(Ok(start)) = dynamic.poll_requested_fill(&waiter) else { + panic!("the time start in the evicted gap asks for its group"); }; - assert_eq!(lookup.time(), 5); - write_filled(lookup.accept(5, None), 5); - drop(lookup); + assert_eq!((start.start().group, start.floor()), (5, 5)); + write_filled(start.accept(5, None), 5); + drop(start); let mut read = drain_groups(&mut subscriber); - while let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) { + while let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) { while let Some(key) = fill.wanted() { write_filled(fill.accept(key, None), key); } @@ -26032,7 +26078,7 @@ mod test { None => ended = true, } } - let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&kio::Waiter::noop()) else { + let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&kio::Waiter::noop()) else { break; }; while let Some(key) = fill.wanted() { @@ -26055,7 +26101,7 @@ mod test { write_group(&producer, 100); let waiter = kio::Waiter::noop(); while let Poll::Ready(Ok(Some(_))) = subscriber.poll_recv_group(&waiter) {} - let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) else { panic!("below the start is a miss"); }; assert_eq!((fill.start().group, fill.end()), (0, 100)); @@ -26081,7 +26127,7 @@ mod test { write_group(&producer, 10); let waiter = kio::Waiter::noop(); while let Poll::Ready(Ok(Some(_))) = narrowed.poll_recv_group(&waiter) {} - let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) else { panic!("below the start is a miss"); }; narrowed.update(descending(Range::groups(0..8))).unwrap(); @@ -26089,7 +26135,7 @@ mod test { let mut later = producer.subscribe(descending(Range::groups(0..))); while let Poll::Ready(Ok(Some(_))) = later.poll_recv_group(&waiter) {} - let Poll::Ready(Ok(again)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(again)) = dynamic.poll_requested_fill(&waiter) else { panic!("what the first fill stopped owing is asked afresh"); }; assert_eq!((again.start().group, again.end()), (8, 10)); @@ -26113,7 +26159,7 @@ mod test { let waiter = kio::Waiter::noop(); while let Poll::Ready(Ok(Some(_))) = subscriber.poll_recv_group(&waiter) {} let mut asked = Vec::new(); - while let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) { + while let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) { asked.push((fill.start().group, fill.end())); } assert_eq!(asked, [(5, 6), (0, 2)]); @@ -26139,7 +26185,7 @@ mod test { evict(&producer, 0); evict(&producer, 1); assert_eq!(drain_groups(&mut subscriber), [2]); - let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) else { panic!("group 1 is a miss now"); }; assert_eq!((fill.start().group, fill.end()), (1, 2), "group 0 was taken"); @@ -26147,7 +26193,7 @@ mod test { write_filled(fill.accept(1, None), 1); drop(fill); assert_eq!(drain_groups(&mut subscriber), [1]); - assert!(dynamic.poll_requested_groups(&waiter).is_pending()); + assert!(dynamic.poll_requested_fill(&waiter).is_pending()); assert!(drained_drops(&mut subscriber).is_empty(), "nothing was given up"); let Inner::Plain(cursor) = &subscriber.inner else { unreachable!("a plain subscription"); @@ -26194,7 +26240,7 @@ mod test { evict(&producer, 3); let _ = next(&mut reader); let mut asked = Vec::new(); - while let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) { + while let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) { asked.push((fill.start().group, fill.end())); } assert!( @@ -26276,7 +26322,7 @@ mod test { evict(&producer, 0); evict(&producer, 1); assert!(drained_drops(&mut subscriber).is_empty()); - assert!(dynamic.poll_requested_groups(&waiter).is_pending(), "both were taken"); + assert!(dynamic.poll_requested_fill(&waiter).is_pending(), "both were taken"); } /// An ordered reader waits on the refill of a group evicted at or above its next @@ -26299,7 +26345,7 @@ mod test { evict(&producer, 1); evict(&producer, 2); assert_eq!(next(&mut subscriber), None, "group 1 comes before 3"); - let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) else { panic!("groups 1 and 2 are misses now"); }; assert_eq!((fill.start().group, fill.end()), (1, 3)); @@ -26331,7 +26377,7 @@ mod test { evict(&producer, 1); evict(&producer, 2); assert_eq!(next(&mut subscriber), None, "group 1 comes before 3"); - let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) else { panic!("groups 1 and 2 are misses now"); }; write_filled(fill.accept(2, None), 2); @@ -26342,7 +26388,7 @@ mod test { write_filled(fill.accept(1, None), 1); drop(fill); assert_eq!([next(&mut subscriber), next(&mut subscriber)], [Some(1), Some(3)]); - assert!(dynamic.poll_requested_groups(&waiter).is_pending(), "refilled once"); + assert!(dynamic.poll_requested_fill(&waiter).is_pending(), "refilled once"); } /// A refill evicted again before its reader took it is given up rather than asked a @@ -26360,7 +26406,7 @@ mod test { assert!(matches!(subscriber.poll_recv_group(&waiter), Poll::Ready(Ok(Some(_))))); evict(&producer, 1); assert_eq!(drain_groups(&mut subscriber), [2]); - let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) else { panic!("group 1 is a miss now"); }; assert_eq!(fill.wanted(), Some(1)); @@ -26368,7 +26414,7 @@ mod test { drop(fill); evict(&producer, 1); assert!(drain_groups(&mut subscriber).is_empty()); - assert!(dynamic.poll_requested_groups(&waiter).is_pending(), "refilled once"); + assert!(dynamic.poll_requested_fill(&waiter).is_pending(), "refilled once"); assert_eq!(drained_drops(&mut subscriber), [(1, 2)]); } @@ -26392,7 +26438,7 @@ mod test { let mut refilled = 0; let mut fills = Vec::new(); let mut serve = || { - while let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) { + while let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) { fills.push(fill); } fills.retain_mut(|fill| { @@ -26494,7 +26540,7 @@ mod test { write(producer.create_group(group::Info { start: GROUPS }).map(Some), GROUPS); let waiter = kio::Waiter::noop(); assert!(subscriber.poll_next_group(&waiter).is_pending(), "group 0 is a miss"); - let Poll::Ready(Ok(mut fill)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(mut fill)) = dynamic.poll_requested_fill(&waiter) else { panic!("the reader asked for the batch"); }; assert_eq!((fill.start().group, fill.end()), (0, GROUPS)); @@ -26530,7 +26576,7 @@ mod test { write_group(&producer, 4); let waiter = kio::Waiter::noop(); assert!(ordered.poll_next_group(&waiter).is_pending(), "group 0 is a miss"); - let Poll::Ready(Ok(mut fill)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(mut fill)) = dynamic.poll_requested_fill(&waiter) else { panic!("the reader asked for the batch"); }; assert_eq!(fill.order(), Order::Descending); @@ -26559,7 +26605,7 @@ mod test { write_group(&producer, 4); let waiter = kio::Waiter::noop(); assert!(subscriber.poll_next_group(&waiter).is_pending(), "group 0 is a miss"); - let Poll::Ready(Ok(mut fill)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(mut fill)) = dynamic.poll_requested_fill(&waiter) else { panic!("the reader asked for the batch"); }; assert_eq!(fill.poll_wanted_changed(&waiter), Poll::Ready(Some(0))); @@ -26594,14 +26640,14 @@ mod test { subscriber.poll_recv_group(&waiter).is_pending(), "groups 0..4 are a miss" ); - let Poll::Ready(Ok(mut fill)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(mut fill)) = dynamic.poll_requested_fill(&waiter) else { panic!("the reader asked for the batch"); }; assert_eq!((fill.start().group, fill.end()), (0, 4)); let mut fetch = producer.consume().fetch_group(2, None); assert!(kio::Task::poll(&mut *fetch, &waiter).is_pending()); assert!( - dynamic.poll_requested_groups(&waiter).is_pending(), + dynamic.poll_requested_fill(&waiter).is_pending(), "the fetch joined the batch" ); @@ -26669,7 +26715,7 @@ mod test { write_group(&producer, 4); let waiter = kio::Waiter::noop(); while let Poll::Ready(Ok(Some(_))) = middle.poll_recv_group(&waiter) {} - let Poll::Ready(Ok(taken)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(taken)) = dynamic.poll_requested_fill(&waiter) else { panic!("group 5 is a miss"); }; assert_eq!((taken.start().group, taken.end()), (5, 6)); @@ -26677,7 +26723,7 @@ mod test { let mut subscriber = producer.subscribe(replay().with_range(0..).with_order(Order::Descending)); while let Poll::Ready(Ok(Some(_))) = subscriber.poll_recv_group(&waiter) {} let mut asked = Vec::new(); - while let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) { + while let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) { asked.push((fill.start().group, fill.end())); } assert_eq!(asked, [(6, 8), (0, 4)]); @@ -26691,7 +26737,7 @@ mod test { let waiter = kio::Waiter::noop(); assert!(drained_drops(&mut subscriber).is_empty()); while let Poll::Ready(Ok(Some(_))) = subscriber.poll_recv_group(&waiter) {} - let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) else { panic!("below the start is a miss"); }; fill.drop_groups(0..2); @@ -26712,7 +26758,7 @@ mod test { let (producer, dynamic, mut subscriber) = started_late(); let waiter = kio::Waiter::noop(); while let Poll::Ready(Ok(Some(_))) = subscriber.poll_recv_group(&waiter) {} - let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) else { panic!("below the start is a miss"); }; fill.drop_groups(0..2); @@ -26780,13 +26826,13 @@ mod test { elapse(&producer, Duration::from_secs(31)); assert_eq!(drained_drops(&mut subscriber), [(0, 2)]); assert!( - dynamic.poll_requested_groups(&waiter).is_pending(), + dynamic.poll_requested_fill(&waiter).is_pending(), "the miss asked nothing" ); let mut fetch = producer.consume().fetch_group(1, None); assert!(kio::Task::poll(&mut *fetch, &waiter).is_pending()); - let Poll::Ready(Ok(fill)) = dynamic.clone().poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(fill)) = dynamic.clone().poll_requested_fill(&waiter) else { panic!("the fetch is taken"); }; assert_eq!((fill.start().group, fill.end()), (1, 2)); @@ -26831,7 +26877,7 @@ mod test { write_group(&producer, WIDE); assert_eq!(drain_groups(&mut subscriber), [WIDE]); - let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&kio::Waiter::noop()) else { + let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&kio::Waiter::noop()) else { panic!("the miss below the start is asked"); }; for refused in 1..=REFUSED { @@ -26861,13 +26907,13 @@ mod test { let _ = ascending.poll_recv_group(&kio::Waiter::noop()); let _ = descending.poll_recv_group(&kio::Waiter::noop()); - let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&kio::Waiter::noop()) else { + let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&kio::Waiter::noop()) else { panic!("the miss below the start is asked"); }; assert_eq!((fill.start().group, fill.end()), (0, 4)); assert_eq!(fill.order(), Order::Descending); assert!( - dynamic.poll_requested_groups(&kio::Waiter::noop()).is_pending(), + dynamic.poll_requested_fill(&kio::Waiter::noop()).is_pending(), "both readers share the fill" ); } @@ -26884,7 +26930,7 @@ mod test { producer.start_at(6).unwrap(); write_group(&producer, 6); let _ = descending.poll_recv_group(&kio::Waiter::noop()); - let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&kio::Waiter::noop()) else { + let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&kio::Waiter::noop()) else { panic!("the miss below the start is asked"); }; assert_eq!(fill.order(), Order::Descending); @@ -26923,7 +26969,7 @@ mod test { subscriber.update(subscriber.subscription().with_priority(3)).unwrap(); assert!(subscriber.poll_recv_group(&waiter).is_pending()); - let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) else { panic!("the miss below the start is asked"); }; assert_eq!((fill.start().group, fill.end()), (0, 4)); @@ -26961,7 +27007,7 @@ mod test { _ => panic!("the read ended"), }; assert_eq!(next(), None, "group 0 is being filled"); - let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) else { panic!("below the start is a miss"); }; write_filled(fill.accept(0, None), 0); @@ -26980,7 +27026,7 @@ mod test { let (producer, dynamic, mut subscriber) = started_late(); let waiter = kio::Waiter::noop(); while let Poll::Ready(Ok(Some(_))) = subscriber.poll_recv_group(&waiter) {} - let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) else { panic!("the gap is a miss"); }; drop(subscriber); @@ -26994,7 +27040,7 @@ mod test { let consumer = producer.consume(); let mut fetch = consumer.fetch_group(3, None); let waiter = kio::Waiter::noop(); - let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) else { panic!("the fill is ready"); }; match drop_all { @@ -28127,7 +28173,7 @@ mod test { let waiter = kio::Waiter::noop(); assert_eq!(drain_groups(&mut subscriber), [3]); assert!( - dynamic.poll_requested_groups(&waiter).is_pending(), + dynamic.poll_requested_fill(&waiter).is_pending(), "the stale run settles the reader" ); @@ -28135,7 +28181,7 @@ mod test { .update(narrow.with_delay(Delay::default().with_max(Duration::from_secs(60)))) .unwrap(); assert!(subscriber.poll_next_group(&waiter).is_pending()); - let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) else { panic!("the wider budget asks for the stale run"); }; assert_eq!((fill.start().group, fill.end()), (0, 3)); @@ -28168,7 +28214,7 @@ mod test { // A reader of the run alone, which the verdict does not settle, waits on the same fill. let mut bounded = producer.subscribe(terms().with_ranges([Range::groups(0..3)])); assert!(drain_groups(&mut bounded).is_empty()); - let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) else { panic!("a fill is asked below the start"); }; fill.reject_groups(0..3, Error::Old); @@ -28179,7 +28225,7 @@ mod test { [(0, 3)], "the readers waiting now are refused" ); - assert!(dynamic.poll_requested_groups(&waiter).is_pending()); + assert!(dynamic.poll_requested_fill(&waiter).is_pending()); assert_eq!( producer.state.read().drops.get(&0), Some(&Hole::stale(max_age)), @@ -28190,7 +28236,7 @@ mod test { assert_eq!(drain_groups(&mut later), [3]); assert_eq!(drained_drops(&mut later), [(0, 3)]); assert!( - dynamic.poll_requested_groups(&waiter).is_pending(), + dynamic.poll_requested_fill(&waiter).is_pending(), "the verdict is not asked again" ); @@ -28198,7 +28244,7 @@ mod test { let mut again = producer.subscribe(terms()); assert_eq!(drain_groups(&mut again), [3]); assert!( - matches!(dynamic.poll_requested_groups(&waiter), Poll::Ready(Ok(_))), + matches!(dynamic.poll_requested_fill(&waiter), Poll::Ready(Ok(_))), "a forgotten verdict is asked again" ); } @@ -28227,7 +28273,7 @@ mod test { let mut open = reader(budget, Range::groups(0..)); assert_eq!(open.poll_drop(&waiter), Poll::Ready((0..2, Hole::stale(budget)))); assert_eq!(drain_groups(&mut open), [3]); - assert!(dynamic.poll_requested_groups(&waiter).is_pending(), "nothing is asked"); + assert!(dynamic.poll_requested_fill(&waiter).is_pending(), "nothing is asked"); let mut bounded = reader(budget, Range::groups(0..2)); assert!( @@ -28238,7 +28284,7 @@ mod test { assert_eq!(drain_groups(&mut wider), [3]); assert!(wider.poll_drop(&waiter).is_pending(), "it still wants the groups"); let mut asked = Vec::new(); - while let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) { + while let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) { asked.push((fill.start().group, fill.end())); } assert_eq!(asked, [(0, 2)], "one fill, which both join"); @@ -28268,7 +28314,7 @@ mod test { let waiter = kio::Waiter::noop(); let mut first = reader(); assert!(first.poll_recv_group(&waiter).is_pending()); - let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) else { panic!("the unsettled stale run is asked for"); }; assert_eq!((fill.start().group, fill.end()), (0, 2)); @@ -28542,7 +28588,7 @@ mod test { }; assert_eq!(first.start, 0); assert!( - dynamic.poll_requested_groups(&waiter).is_pending(), + dynamic.poll_requested_fill(&waiter).is_pending(), "a dropped run asks no fill" ); @@ -28550,7 +28596,7 @@ mod test { pool.sweep(); assert!(producer.state.read().drops.is_empty(), "the run expired"); let _ = subscriber.poll_recv_group(&waiter); - let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&waiter) else { + let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) else { panic!("the forgotten run is asked of a fill"); }; assert_eq!(fill.start().group..fill.end(), 1..2); @@ -28569,7 +28615,7 @@ mod test { producer.start_at(START).unwrap(); write_group(&producer, START); assert_eq!(drain_groups(&mut subscriber), [START]); - let Poll::Ready(Ok(fill)) = dynamic.poll_requested_groups(&kio::Waiter::noop()) else { + let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&kio::Waiter::noop()) else { panic!("the miss below the start is asked"); }; // Nothing arrives once the feed ends, so a refusal is a verdict on the track. @@ -29021,7 +29067,7 @@ mod test { live.start_at(1).unwrap(); live.write_frame(live.key_time(), &b"delta"[..]).unwrap(); let _fetch = consumer.fetch_group(0, None); - let fill = dynamic.requested_groups().await.unwrap(); + let fill = dynamic.requested_fill().await.unwrap(); let mut head = fill.accept(0, Info::timed()).unwrap().expect("a fresh group"); head.write_frame(head.key_time(), &b"snapshot"[..]).unwrap(); @@ -30224,7 +30270,7 @@ mod test { // Frame 0 was shed: the late reader asks the handler for a flight from it. assert_eq!(read_numbered(&mut late), None); - let fill = match requests.poll_requested_groups(&kio::Waiter::noop()) { + let fill = match requests.poll_requested_fill(&kio::Waiter::noop()) { Poll::Ready(fill) => fill.unwrap(), Poll::Pending => panic!("a flight is asked"), }; @@ -30349,7 +30395,7 @@ mod test { /// The next fill asked of `requests`, if any. #[cfg(feature = "async")] fn asked(requests: &Requests) -> Option { - match requests.poll_requested_groups(&kio::Waiter::noop()) { + match requests.poll_requested_fill(&kio::Waiter::noop()) { Poll::Ready(fill) => Some(fill.unwrap()), Poll::Pending => None, } @@ -30924,14 +30970,14 @@ mod test { let mut reader = producer.subscribe(replay().with_order(Order::Ascending).with_range(0..2)); assert_eq!(read_now(&mut reader, false), None); assert!( - dynamic.requested_groups().now_or_never().is_none(), + dynamic.requested_fill().now_or_never().is_none(), "an idle copy a feed may still answer asks nothing" ); producer.set_unfed(true); assert_eq!(read_now(&mut reader, false), None, "the fill is out"); let fill = dynamic - .requested_groups() + .requested_fill() .now_or_never() .expect("the holes are asked at once") .unwrap(); diff --git a/rs/moq-net/src/model/track/fill.rs b/rs/moq-net/src/model/track/fill.rs index cfce50eb7a..d54b5ca16b 100644 --- a/rs/moq-net/src/model/track/fill.rs +++ b/rs/moq-net/src/model/track/fill.rs @@ -47,8 +47,9 @@ 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, + // The oldest key the first group may start at: below `start` for a time start, whose + // group is the newest one starting at or before it. + floor: u64, // What its joins ask with now, shared with them and its handler. asks: kio::Shared, queued: bool, @@ -212,9 +213,14 @@ impl Outcome { self.accepted.contains(&key) } - /// The lowest group accepted: a lookup's answer. - pub(super) fn first_accepted(&self) -> Option { - self.accepted.iter().next().map(|run| run.start) + /// Where a time start's fill says its group is, once `time`'s key is answered: the group + /// accepted, or `None` when it was refused or dropped instead. + pub(super) fn time_start(&self, time: u64) -> Option> { + let first = self.accepted.iter().next().map(|run| run.start); + match first.is_some() || self.resolved.contains(&time) { + true => Some(first), + false => None, + } } /// The partial copy accepted for `key`, if it still holds `frames`. @@ -286,8 +292,6 @@ 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, /// Asked by a fetch rather than a reader's miss, so a fetch-only handler takes it. pub fetch: bool, } @@ -475,15 +479,6 @@ 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) @@ -581,7 +576,7 @@ impl FillState { start, end: groups.end, frame_end: if groups.end == ask.end { ask.frame_end } else { None }, - time: None, + floor: start.group, asks, queued: true, flight: false, @@ -633,7 +628,7 @@ impl FillState { start: Position { group: key, frame }, end: groups.end, frame_end: None, - time: None, + floor: key, asks: kio::Shared::new(asks), queued: true, flight: true, @@ -660,42 +655,56 @@ impl FillState { }) } - /// 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: u64) -> Join { + /// Queue a fill of the group containing `time`, a reader's time start no held group + /// resolves, with the join waiting on its answer: the group starting at `time`, or else the + /// newest one before it, starting at or after `floor`. It joins nothing and nothing joins + /// it, since an answer below `time` says nothing of the keys another reader asks. `None` + /// while no handler can take it. + pub(super) fn time_start( + &mut self, + shared: &kio::Shared, + time: u64, + floor: u64, + asking: Asking, + ) -> Option { + let groups = time..time.checked_add(1)?; + if self.handlers == 0 { + return None; + } let id = self.next; self.next += 1; let result = kio::Producer::::default(); let mut asks = Asks::default(); - asks.add(&ask.asking); + asks.add(&asking); let asks = kio::Shared::new(asks); let join = Join { fills: shared.clone(), id, - groups: ask.start.group..ask.end, - held: RangeSet::new(), + groups: groups.clone(), + held: RangeSet::from_iter([groups.clone()]), outcome: Some(result.consume()), attempt: result.weak(), - asking: ask.asking, + asking, asks: asks.clone(), }; self.pending.insert( id, Pending { - start: ask.start, - end: ask.end, + start: Position::group(time), + end: groups.end, frame_end: None, - time: Some(time), + floor: floor.min(time), asks, queued: true, flight: false, fetch: false, - wants: RangeMap::new(), + wants: RangeMap::from_iter([(groups, 1)]), unseen: RangeSet::new(), result, }, ); self.queue.push_back(id); - join + Some(join) } /// Count a join out of reading `groups` of fill `id`. What nobody reads any more is no @@ -805,7 +814,7 @@ pub(super) fn poll_requested( state: &kio::Producer, fills: &kio::Shared, waiter: &kio::Waiter, -) -> Poll> { +) -> Poll> { // Prefer serving a queued fill, even if the track has since aborted. if let Poll::Ready(mut guard) = fills.poll(waiter, |fills| match fills.has_queued() { true => Poll::Ready(()), @@ -813,11 +822,11 @@ 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, time, flight) = ( + let (start, end, frame_end, floor, flight) = ( pending.start, pending.end, pending.frame_end, - pending.time, + pending.floor, pending.flight, ); let (asks, result) = (pending.asks.clone(), pending.result.clone()); @@ -830,14 +839,14 @@ pub(super) fn poll_requested( start, end, frame_end, + floor, flight, asks, result, }; - let inner = Arc::new(SendCell::new(inner)); - return Poll::Ready(Ok(match time { - Some(time) => Fill::Lookup(FillLookup { inner, time }), - None => Fill::Groups(FillGroups { inner, prev: None }), + return Poll::Ready(Ok(FillGroups { + inner: Arc::new(SendCell::new(inner)), + prev: None, })); } @@ -852,19 +861,12 @@ pub(super) fn poll_requested( } } -/// What readers ask a [`Requests`](super::Requests) handler for: a run of groups to serve, or a -/// time to look up. -pub enum Fill { - /// Groups readers miss. - Groups(FillGroups), - /// A time a reader's range starts at that no cached group resolves. - Lookup(FillLookup), -} - /// A run of groups readers miss, for a [`Requests`](super::Requests) handler to serve. /// /// It names `start.group` up to the exclusive [`end`](Self::end), the first group from -/// [`start`](Self::start)'s frame. Its [`priority`](Self::priority), +/// [`start`](Self::start)'s frame. A reader's time start that no group the cache holds places +/// asks for the group containing that time as a fill one key wide, from the time, whose first +/// group may start below it: see [`floor`](Self::floor). Its [`priority`](Self::priority), /// [`max_delay`](Self::max_delay), and [`order`](Self::order) follow its readers as they /// change, so read them as each group is served. The handler calls [`accept`](Self::accept) for each group /// it has and [`drop_groups`](Self::drop_groups) for the rest, one group at a time as @@ -906,66 +908,32 @@ struct Inner { end: u64, // Exclusive frame cap in the last group; None asks through the original end. frame_end: Option, + // The oldest key the first group may start at; see [`FillGroups::floor`]. + floor: u64, // Answered with a copy of its own rather than the cache's; see [`FillState::fly`]. flight: bool, asks: kio::Shared, result: kio::Producer, } -/// A time a reader's range starts at that no group the cache holds resolves, for a -/// [`Requests`](super::Requests) handler to look 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. -/// -/// A lookup owes no group, so 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 newest group -/// the cache holds at or before the time, or, with none, reads every group the cache holds and -/// asks none starting before the time. -pub struct FillLookup { - inner: Arc>, - time: u64, -} - -impl FillLookup { - /// The time to look up, in the track's timescale (a bare start on a scale-less track). - pub fn time(&self) -> u64 { - self.time - } - - /// The most urgent delivery priority among the readers waiting on it now. - pub fn priority(&self) -> i8 { - self.inner.asks.read().priority() - } - - /// The most tolerant age budget among the readers waiting on it now, or `None` for the - /// publisher's max age alone. Each reader still enforces its own. - pub fn max_delay(&self) -> Option { - self.inner.max_delay() - } - - /// Insert group `key`, the one containing [`time`](Self::time), into the track's cache, - /// handing it to the readers waiting on the lookup, and return a [`group::Producer`] to fill. - /// - /// `info` sets the track's [`Info`] if it hasn't been accepted yet, and is ignored otherwise. - /// Returns `None` if the group was already answered or cached. Errors with - /// [`Error::Skipped`] if the track skipped it, or the track's abort error. - pub fn accept(&self, key: u64, info: impl Into>) -> Result> { - self.inner.accept(key, info.into()) - } - - /// The groups the answer may lie in, which a test checks the lookup was asked over. - #[cfg(all(test, feature = "async"))] - pub(crate) fn span(&self) -> ops::Range { - self.inner.start.group..self.inner.end - } -} - impl FillGroups { /// The first group to fill, and the frame within it to start from. pub fn start(&self) -> Position { self.inner.start } + /// The oldest group the first group may start at: [`start`](Self::start)'s, unless that is a + /// reader's time start no group the cache holds places. Then the first group is the one + /// containing that time: the group starting at it, or else the newest one starting before + /// it, at or after the floor. A handler knowing its groups only by key serves that key as + /// any other; one that can tell which group holds the time, as from an index, may + /// [`accept`](Self::accept) that group instead, or [`drop_groups`](Self::drop_groups) the + /// key when none does. The reader starts where the answer says; left unanswered or + /// refused, it starts where the groups the cache holds say. + pub fn floor(&self) -> u64 { + self.inner.floor + } + /// Whether a [`Consumer::fetch_group`](super::Consumer::fetch_group) asked or joined it, /// rather than only readers that missed it. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] @@ -1010,11 +978,12 @@ impl FillGroups { /// it, and return a [`group::Producer`] to fill. /// /// The group starts at [`start`](Self::start)'s frame when it is the first group, so the - /// frames written keep the indices they have in the group. `info` sets the track's - /// [`Info`] if it hasn't been accepted yet (a fetch with no live subscription), and is - /// ignored otherwise. Returns `None` if the group was already answered or cached. Errors - /// with [`Error::InvalidRange`] outside the fill, [`Error::Skipped`] if the track skipped - /// it, or the track's abort error. + /// frames written keep the indices they have in the group. A key from the + /// [`floor`](Self::floor) up to the start's answers the start with that group, from its + /// first frame. `info` sets the track's [`Info`] if it hasn't been accepted yet (a fetch + /// with no live subscription), and is ignored otherwise. Returns `None` if the group was + /// already answered or cached. Errors with [`Error::InvalidRange`] outside the fill, + /// [`Error::Skipped`] if the track skipped it, or the track's abort error. pub fn accept(&self, key: u64, info: impl Into>) -> Result> { self.inner.accept(key, info.into()) } @@ -1299,7 +1268,7 @@ impl FillGroups { impl Inner { fn contains(&self, key: u64) -> bool { - (self.start.group..self.end).contains(&key) + (self.floor..self.end).contains(&key) } fn is_wanted(&self, key: u64) -> bool { @@ -1370,7 +1339,13 @@ impl Inner { let Some(end) = key.checked_add(1) else { return Err(Error::InvalidRange); }; - if self.result.read().resolved.contains(&key) { + // A key below the start answers the start, so no other group starts from it through + // the start. + let answered = match key < self.start.group { + true => key..self.start.group + 1, + false => key..end, + }; + if self.result.read().resolved.overlaps(&answered) { return Ok(None); } // Written for a reader that has yet to pick it up, which the next group waits on. @@ -1405,6 +1380,7 @@ impl Inner { } if let Ok(mut outcome) = self.result.write() { outcome.accept(key..end); + outcome.resolved.insert(answered); if let Ok(Some(group)) = &res && (self.flight || frames.start > 0 || frames.end.is_some()) { diff --git a/rs/moq-net/src/model/track/miss.rs b/rs/moq-net/src/model/track/miss.rs index c1ae7c4369..a8ad8d36f0 100644 --- a/rs/moq-net/src/model/track/miss.rs +++ b/rs/moq-net/src/model/track/miss.rs @@ -1319,7 +1319,6 @@ impl Misses { end: run.end, frame_end: None, asking: *asking, - time: None, fetch: false, }; let (joins, unfillable) = fills.lock().join(fills, &ask); @@ -2454,7 +2453,7 @@ mod tests { ); // One fill answers: only its join is looked at again, and the later task wakes. - let fill = std::future::poll_fn(|cx| requests.poll_requested_groups(&kio::Waiter::new(cx.waker().clone()))) + let fill = std::future::poll_fn(|cx| requests.poll_requested_fill(&kio::Waiter::new(cx.waker().clone()))) .await .unwrap(); let woken = second.0.load(Ordering::Relaxed); @@ -2531,7 +2530,7 @@ mod tests { "a no-op poll leaves the real registrations in place" ); - let fill = std::future::poll_fn(|cx| requests.poll_requested_groups(&kio::Waiter::new(cx.waker().clone()))) + let fill = std::future::poll_fn(|cx| requests.poll_requested_fill(&kio::Waiter::new(cx.waker().clone()))) .await .unwrap(); let (read_woken, drops_woken) = (read.0.load(Ordering::Relaxed), drops.0.load(Ordering::Relaxed)); diff --git a/rs/moq-net/tests/integration/fetch_cancel.rs b/rs/moq-net/tests/integration/fetch_cancel.rs index d9b7cc7f13..5620837034 100644 --- a/rs/moq-net/tests/integration/fetch_cancel.rs +++ b/rs/moq-net/tests/integration/fetch_cancel.rs @@ -66,7 +66,7 @@ async fn abandoned_fetch_reaches_the_publisher(version: &str, relays: u64, stage let _track = request.accept(timed()); let mut request = match futures::future::select(std::pin::pin!(groups.requested_fill()), &mut waiting).await { futures::future::Either::Left((request, _)) => { - support::groups(request.expect("the fetch reaches the publisher")) + request.expect("the fetch reaches the publisher") } futures::future::Either::Right(_) => panic!("nothing answered the fetch"), }; @@ -200,7 +200,7 @@ async fn a_refetch_within_the_linger_stays_on_the_relay(version: &str) { let _track = request.accept(timed()); let request = match futures::future::select(std::pin::pin!(groups.requested_fill()), &mut waiting).await { futures::future::Either::Left((request, _)) => { - support::groups(request.expect("the fetch reaches the publisher")) + request.expect("the fetch reaches the publisher") } futures::future::Either::Right(_) => panic!("nothing answered the fetch"), }; diff --git a/rs/moq-net/tests/integration/fetch_pipeline.rs b/rs/moq-net/tests/integration/fetch_pipeline.rs index 20cfc198a7..20b8266d79 100644 --- a/rs/moq-net/tests/integration/fetch_pipeline.rs +++ b/rs/moq-net/tests/integration/fetch_pipeline.rs @@ -84,7 +84,6 @@ async fn fetch_ahead( let fill = kio::clock::timeout(TIMEOUT, request.requests().requested_fill()) .await .unwrap_or_else(|_| panic!("{version}: the FETCH waited for the info")) - .map(support::groups) .unwrap(); let held = Held { _publisher: publisher, diff --git a/rs/moq-net/tests/integration/flights.rs b/rs/moq-net/tests/integration/flights.rs index a995b586e7..45336d5162 100644 --- a/rs/moq-net/tests/integration/flights.rs +++ b/rs/moq-net/tests/integration/flights.rs @@ -83,7 +83,7 @@ async fn write_paced( /// whole group, so each fill finishes after its last frame. fn serve(requests: track::Requests, written: Arc, ended: Arc, finish: bool) { drop(kio::clock::sim::spawn(async move { - while let Ok(track::Fill::Groups(fill)) = requests.requested_fill().await { + while let Ok(fill) = requests.requested_fill().await { let start = fill.start(); let mut group = fill.accept(start.group, None).expect("accept").expect("a fresh group"); let mut demand = group.demand(); diff --git a/rs/moq-net/tests/integration/support/mod.rs b/rs/moq-net/tests/integration/support/mod.rs index 26dee79172..818c80e846 100644 --- a/rs/moq-net/tests/integration/support/mod.rs +++ b/rs/moq-net/tests/integration/support/mod.rs @@ -49,15 +49,6 @@ impl AtStart for group::Producer { } } -/// A fill the test expects to name groups rather than look up a time. -#[allow(dead_code, reason = "shared by several test binaries, each using only part of it")] -pub fn groups(fill: track::Fill) -> track::FillGroups { - let track::Fill::Groups(fill) = fill else { - panic!("a run of groups, not a time lookup"); - }; - fill -} - /// The groups from the one containing `keys`' start through the one containing its end, as /// time anchors: on a track whose groups start at consecutive keys, the groups with those keys. /// diff --git a/rs/moq-net/tests/integration/untimed.rs b/rs/moq-net/tests/integration/untimed.rs index b88b1beb72..81efd6222e 100644 --- a/rs/moq-net/tests/integration/untimed.rs +++ b/rs/moq-net/tests/integration/untimed.rs @@ -300,7 +300,7 @@ async fn a_fetched_untimed_frame_arrives_untimed() { let _track = request.accept(untimed()); let request = match futures::future::select(std::pin::pin!(groups.requested_fill()), &mut waiting).await { futures::future::Either::Left((request, _)) => { - support::groups(request.expect("the fetch reaches the publisher")) + request.expect("the fetch reaches the publisher") } futures::future::Either::Right(_) => panic!("nothing answered the fetch"), }; From 017061bbc8e15ce6c648b916b4aadeb2043dffa8 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 19 Nov 2026 23:15:00 -0800 Subject: [PATCH 03/14] wip: handlers serve a time start as a group fill --- quest/m2/outcome-enums/README.md | 4 +- quest/m2/outcome-enums/fill-lookups.md | 38 ----- quest/m3/unbounded-groups/README.md | 2 +- quest/m4/archive-dvr.md | 2 +- quest/m5/archive-time-followups.md | 37 +++-- quest/m5/net-wrap/traits.md | 4 +- rs/libmoq/src/publish.rs | 8 +- rs/moq-archive/src/consumer/index.rs | 9 -- rs/moq-archive/src/consumer/track.rs | 162 +++++++------------ rs/moq-ffi/src/producer.rs | 15 +- rs/moq-net/src/model/track.rs | 13 +- rs/moq-net/tests/integration/fetch_cancel.rs | 8 +- rs/moq-net/tests/integration/untimed.rs | 4 +- rs/moq-transcode/src/lib.rs | 24 +-- rs/moq-transcode/src/rung.rs | 67 +++----- 15 files changed, 122 insertions(+), 275 deletions(-) delete mode 100644 quest/m2/outcome-enums/fill-lookups.md diff --git a/quest/m2/outcome-enums/README.md b/quest/m2/outcome-enums/README.md index c66e719aac..340a8b6757 100644 --- a/quest/m2/outcome-enums/README.md +++ b/quest/m2/outcome-enums/README.md @@ -33,6 +33,4 @@ Decided: The test the audit applied: when two callers collapse an enum the same way, the API should return the collapsed form. -Public API: every child is breaking; each lists its migration. Wire: none, -unless [fill lookups](/quest/m2/outcome-enums/fill-lookups.md) needs a time -start on the wire. +Public API: every child is breaking; each lists its migration. Wire: none. diff --git a/quest/m2/outcome-enums/fill-lookups.md b/quest/m2/outcome-enums/fill-lookups.md deleted file mode 100644 index a7e15121ad..0000000000 --- a/quest/m2/outcome-enums/fill-lookups.md +++ /dev/null @@ -1,38 +0,0 @@ -# [L] A time lookup is a group fill - -## Goal - -A track's request handler serves one kind of fill. A reader that starts or -ends at a time no cached group resolves asks for the groups covering that -time, as an ordinary group fill, so `track::Fill`, `Fill::Lookup`, and -`FillLookup` are gone from moq-net, js/net, moq-ffi, and libmoq, and no -handler skips lookups by hand. - -## Plan - -Decided 2026-11-18 (maintainer, /quest-plan): ✅ fold lookups into group fills, -"we should have one mechanism" (rejected: lookups as an opt-in second kind; -leaving it). Groups are addressed by their start timestamp on lite-07, so a -time range maps onto groups: a reader wanting timestamps 40-60 may be filled -with the groups spanning 30-70, from the group holding 40 through the one -holding 60. - -Today a reader's window that cannot resolve a time start asks a fill with -`time: Some(t)` (`look_up` in `rs/moq-net/src/model/track.rs`, `Ask::time` -in `track/fill.rs`), which surfaces as `Fill::Lookup`. Only moq-archive -answers it, from its index; libmoq, moq-ffi, moq-transcode, and js/net's lite -and ietf subscribers drop it, and js/net notes "the wire fetches by group -alone, so a lookup goes unanswered and its reader falls back". - -Design the fill so a handler resolves a time-bounded start the way it -resolves any start: moq-archive from its index, an upstream subscription by -asking its peer, any other handler by dropping groups it does not have. -Check first whether a lite-07 SUBSCRIBE or FETCH can carry a time-bounded -start (Range Anchors compare times against starts), so a relay forwards it -instead of dropping it; record what the wire allows in this Plan before -building. If the wire cannot carry it, the relay drops such a fill as today, -and the API still has one kind. - -Public API: breaking in moq-net, js/net, moq-ffi, and libmoq; handlers lose -their lookup arm. Wire: none expected; a wire change would follow the -lite-07 rules. diff --git a/quest/m3/unbounded-groups/README.md b/quest/m3/unbounded-groups/README.md index 405c434765..41d4f3b201 100644 --- a/quest/m3/unbounded-groups/README.md +++ b/quest/m3/unbounded-groups/README.md @@ -169,7 +169,7 @@ Decided 2026-10-16 (maintainer interview): draft's 32 MiB interop cap. The [m1 compression migration](https://github.com/moq-dev/moq/pull/7046) deleted the flate packages. There is no new page. -A flight's fill is the `track::Requests` fill (`track::Fill`). A handler serves a range by writing into a +A flight's fill is the `track::Requests` fill (`track::FillGroups`). A handler serves a range by writing into a group producer bounded to that range, the same API as a live write, so an origin can regenerate data from disk or an archive. diff --git a/quest/m4/archive-dvr.md b/quest/m4/archive-dvr.md index 08674a0fc6..2cce9795d8 100644 --- a/quest/m4/archive-dvr.md +++ b/quest/m4/archive-dvr.md @@ -31,7 +31,7 @@ route, so the archive lives at its own path. quest of its own. Rejected: a synchronous `GroupSource` callback on the live track, since a callback forces history reads to block (bad over S3). An application with its own store answers evicted groups through - `track::Producer::requests()`, whose `Fill` it serves asynchronously, or + `track::Producer::requests()`, whose `FillGroups` it serves asynchronously, or records with `moq-archive`. Rejected: serving the deep window on the live track itself, since its `max_age` marks those groups `Old`; the archive stays at its own path. diff --git a/quest/m5/archive-time-followups.md b/quest/m5/archive-time-followups.md index 8fa3d43b78..65439b5f8e 100644 --- a/quest/m5/archive-time-followups.md +++ b/quest/m5/archive-time-followups.md @@ -2,26 +2,32 @@ ## Goal -When an `archive::Consumer` cannot answer a `Time(pts)` lookup because a +When an `archive::Consumer` cannot answer a `Time(pts)` start because a store GET fails or an object does not decode, the subscription fails with a -typed error naming the lookup failure instead of silently falling back to -the cache. A time range on a history-served track also counts an unverified +typed error naming the failure instead of silently falling back to the +cache. A time range on a history-served track also counts an unverified group's start even when its handler registered after the group's first -frame, and moq-ffi plus the bindings expose time lookups (`track::FillLookup`). +frame, and moq-ffi plus the bindings expose a fill's `floor` +(`track::FillGroups::floor`), so a binding-side handler can answer a time +start with the group below it. ## Plan Decided 2026-10-31 (user, /quest-plan of the follow-ups from the 2026-10-31 /quest-complete run), from [moq-dev/moq#7310](https://github.com/moq-dev/moq/pull/7310) (merged), -which added `track::Fill::time()` and the lookup and finished the archive -time ranges quest. +which added the time lookup and finished the archive time ranges quest. +[fill-lookups](https://github.com/moq-dev/moq/pull/9228) since made the lookup an +ordinary group fill: a time start's fill is one key wide from the time, and +`FillGroups::floor` says how far below it the group containing the time may +start. - What a lookup's storage or decode failure does: - Silent cache fallback, as [moq-dev/moq#7310](https://github.com/moq-dev/moq/pull/7310) documents with a warning. - - ✅ Refuse + report. Add a lookup refusal to `FillLookup` (for example - `FillLookup::refuse(err)`); the reader's span fails - and the subscription ends with a typed error. No retry here: the store + - ✅ Refuse + report. The archive already refuses a time start's fill + when the store fails (`FillGroups::refuse_groups`); make that refusal + fail the reader's span, so the subscription ends with a typed error, + instead of falling back to the groups cached. No retry here: the store client already retries. Reason: a cache fallback silently serves the wrong range, which a DVR seek cannot tell from success. - Retry the lookup in moq-net. @@ -32,19 +38,20 @@ Also, from [moq-dev/moq#7310](https://github.com/moq-dev/moq/pull/7310)'s follow using whether a history-serving handler exists then. A handler registered later misses those groups, so index them (or re-check) when the handler registers. -- Mirror `track::FillLookup` and the new refusal in `rs/moq-ffi` and moq-c - (whose `requested_fill` and fill handler drop lookups unanswered today) and - the binding wrappers, so a binding-side history handler can answer lookups. +- Mirror `track::FillGroups::floor` in `rs/moq-ffi` and moq-c (whose + handlers serve a time start's key like any other today) and the binding + wrappers, so a binding-side history handler can answer a time start with + the group below it. - A time end walks the history one fill round trip per group. Measure a long range against the in-memory store; if DVR seeks need it, look the - end up as well. Otherwise record the cost in the `FillLookup` docs. + end up as well. Otherwise record the cost in the `FillGroups::floor` docs. Tests: a failing GET and a malformed object each fail the subscription with the typed error; a handler registered after a group started still resolves a time range over it; an FFI handler answers a lookup. -Public API: `track::FillLookup` gains a refusal; moq-ffi gains a lookup -handle with `time()` and the refusal. Wire: none. +Public API: a refused time start fails its reader; moq-ffi's fill gains +`floor()`. Wire: none. ## Related diff --git a/quest/m5/net-wrap/traits.md b/quest/m5/net-wrap/traits.md index b503a70218..4b8bf21e1e 100644 --- a/quest/m5/net-wrap/traits.md +++ b/quest/m5/net-wrap/traits.md @@ -30,7 +30,7 @@ the methods exactly as moq-net has them today. `broadcast::Requests`, whose `requested_track` yields its own `track::Request`, so generic code serves undeclared names (as `moq_e2ee::broadcast::Requests` does). A - `track::Requests`'s `requested_fill` yields its own `track::Fill`, whose + `track::Requests`'s `requested_fill` yields its own `track::FillGroups`, whose `accept` returns its own group producer and whose `drop_groups` names the rest dropped. - Pending operations: `track::Consumer::subscribe` resolves to the @@ -68,7 +68,7 @@ Public API: new moq-net traits, plus the hidden test-wrapper feature. Wire: none Decided 2026-10-22 (maintainer): the traits break moq-net's public API, so they wait for the breaking release cut. -Decided 2026-11-07 (maintainer, /quest-plan from the API sweep of open PRs and remaining quests): trait only what adopters call: the broadcast, track, group, and frame producer chain, `track::Consumer` to `Subscriber` to `Ordered`, `requests` with `requested_track`, `tracks`, and datagrams. `Fill`, `Fetching`, and `Resolving` stay concrete until a wrapped consumer needs them. +Decided 2026-11-07 (maintainer, /quest-plan from the API sweep of open PRs and remaining quests): trait only what adopters call: the broadcast, track, group, and frame producer chain, `track::Consumer` to `Subscriber` to `Ordered`, `requests` with `requested_track`, `tracks`, and datagrams. `FillGroups`, `Fetching`, and `Resolving` stay concrete until a wrapped consumer needs them. ## Required diff --git a/rs/libmoq/src/publish.rs b/rs/libmoq/src/publish.rs index 5fcf679ef4..a15e9e09f8 100644 --- a/rs/libmoq/src/publish.rs +++ b/rs/libmoq/src/publish.rs @@ -30,7 +30,7 @@ struct TrackRequest { /// several times the size of a fill. enum Request { Track(Box), - Fill(moq_net::track::Fill), + Fill(moq_net::track::FillGroups), Datagram(DatagramRequest), } @@ -1059,12 +1059,6 @@ impl Publish { Request::Track(request) => State::lock().publish.track_request.insert(*request)?, Request::Datagram(request) => State::lock().publish.datagram_request.insert(request)?, Request::Fill(fill) => { - // A lookup of a time owes no group, and a request names one: drop it - // unanswered, as a handler that can't resolve times does, so the reader - // falls back to the groups held. - let moq_net::track::Fill::Groups(fill) = fill else { - continue; - }; let mut serve = Box::pin(Self::serve_fill(callback, Arc::new(fill))); fills.push(move |waiter: &kio::Waiter| waiter.poll_future(serve.as_mut())); continue; diff --git a/rs/moq-archive/src/consumer/index.rs b/rs/moq-archive/src/consumer/index.rs index 64fbeb37e6..b8089c6239 100644 --- a/rs/moq-archive/src/consumer/index.rs +++ b/rs/moq-archive/src/consumer/index.rs @@ -155,15 +155,6 @@ impl Index { 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())) - } } /// Take in the objects new since `index`'s cursor and return them, with the track's `end` when diff --git a/rs/moq-archive/src/consumer/track.rs b/rs/moq-archive/src/consumer/track.rs index 47dcdb06bc..1f5d1aa810 100644 --- a/rs/moq-archive/src/consumer/track.rs +++ b/rs/moq-archive/src/consumer/track.rs @@ -3,7 +3,7 @@ use std::sync::{Arc, Mutex}; use futures::future::{self, Either}; -use moq_net::track::{Fill, FillGroups, FillLookup, Position}; +use moq_net::track::{FillGroups, Position}; use moq_net::{Timescale, Timestamp, group, track}; use object_store::ObjectStore; @@ -143,67 +143,56 @@ impl Live { /// Serve `fill` from the archive, a group at a time in the fill's order as its readers pick /// them up, until every group is answered or nobody waits on it. -pub(super) async fn serve(shared: Arc>, index: Arc>, fill: Fill) { - match fill { - Fill::Lookup(lookup) => look_up(&shared, &index, &lookup).await, - Fill::Groups(mut fill) => { - while let Some(sequence) = fill.wanted_changed().await { - serve_group(&shared, &index, Asker::Fill(&fill), sequence).await; - } - } - } -} - -/// Who a served group answers: a fill's readers, or a lookup, which owes no group. -#[derive(Clone, Copy)] -enum Asker<'a> { - Fill(&'a FillGroups), - Lookup(&'a FillLookup), -} - -impl Asker<'_> { - /// Name `groups` as never to be delivered; a lookup owes none. - fn drop_groups(self, groups: std::ops::Range) { - if let Self::Fill(fill) = self { - fill.drop_groups(groups); - } - } - - fn accept(self, sequence: u64) -> moq_net::Result> { - match self { - Self::Fill(fill) => fill.accept(sequence, None), - Self::Lookup(lookup) => lookup.accept(sequence, None), +pub(super) async fn serve(shared: Arc>, index: Arc>, mut fill: FillGroups) { + while let Some(sequence) = fill.wanted_changed().await { + match sequence == fill.start().group && fill.floor() < sequence { + true => look_up(&shared, &index, &fill, sequence).await, + false => serve_group(&shared, &index, &fill, sequence, sequence).await, } } } -/// Answer `lookup`'s time, in the track's timescale, with the group containing it: the -/// newest group starting at or before it, found among the objects whose keys say they may hold -/// it, or the oldest stored group when every one starts after it. A group starts at its key, -/// as the live cache resolves it. Left unanswered when the archive holds no group, or the store -/// fails. -async fn look_up(shared: &Shared, index: &Mutex, lookup: &FillLookup) { +/// Answer `fill`'s start at `pts`, a time in the track's timescale, with the group containing +/// it: the newest group starting at or before it, found among the objects whose keys say they +/// may hold it. A group starts at its key, as the live cache resolves it. Dropped when the +/// archive holds none from the fill's floor through it, and refused for now when the store +/// fails or the group found can't be served. +async fn look_up(shared: &Shared, index: &Mutex, fill: &FillGroups, pts: u64) { loop { - match search(shared, index, lookup.time()).await { - Search::Found(sequence) => return serve_group(shared, index, Asker::Lookup(lookup), sequence).await, + match search(shared, index, pts).await { + Search::Found(sequence) if sequence >= fill.floor() => { + serve_group(shared, index, fill, pts, sequence).await; + break; + } + // The cache holds a newer group at or before it than the archive does. + Search::Found(_) | Search::None => { + fill.drop_groups(fill.floor()..pts + 1); + return; + } // The index dropped what the store pruned, at least the object found gone, which no // listing adds back, so the search starts over on what it retains. Each retry drops an // object for good, so only a store pruning what it lists faster than a GET keeps // this going, at one LIST and GET per object it writes. Search::Pruned => {} - Search::Unanswered => return, + Search::Failed => break, } } + // Left unanswered while a reader still waits, as when the group found was pruned since. + if fill.is_wanted(pts) { + fill.refuse_groups(pts..pts + 1, moq_net::Error::Stream(moq_net::StreamError::Internal)); + } } -/// How a lookup's search ended. +/// How a search for the group containing a time ended. enum Search { /// At the group starting at this sequence. Found(u64), + /// With no group starting at or before it. + None, /// At an object the store pruned since it was listed. Pruned, - /// With no group, or a store failure. - Unanswered, + /// With a store failure. + Failed, } /// Search the index for the group containing `pts`; see [`look_up`]. @@ -231,33 +220,10 @@ async fn search(shared: &Shared, index: &Mutex, pts: u return Search::Found(sequence); } } - - // 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 object = match load(shared, index, start, &path, timed).await { - Ok(object) => object, - Err(ended) => return ended, - }; - let found = object - .entries - .iter() - .enumerate() - .find(|(position, entry)| !entry.frames.is_empty() && (*position > 0 || object.frame_start == 0)) - .map(|(_, entry)| entry.sequence); - if let Some(sequence) = found { - return Search::Found(sequence); - } - next = index.lock().unwrap().after(Some(start)); - } - Search::Unanswered + Search::None } -/// The object a lookup searches, or how the search ends without it. -/// -/// An abandoned lookup still finishes the read and answers nobody: a lookup owes no group, so -/// nothing tells it when its readers stop waiting. +/// The object a search reads, or how the search ends without it. async fn load( shared: &Shared, index: &Mutex, @@ -275,17 +241,20 @@ async fn load( Err(err) => err, }; tracing::warn!(%path, %err, "archived object is unavailable for a time lookup"); - Err(Search::Unanswered) + Err(Search::Failed) } -/// Answer group `sequence` for `asker` 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, asker: Asker<'_>, sequence: u64) { - let frame = match asker { - Asker::Fill(fill) => fill.frame_start(sequence), - // A lookup is answered from the group's first frame. - Asker::Lookup(_) => 0, - }; +/// Answer `fill`'s `key` with group `sequence` 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. The group is the key's own, or one below it containing a time start. +async fn serve_group( + shared: &Shared, + index: &Mutex, + fill: &FillGroups, + key: u64, + sequence: u64, +) { + let frame = fill.frame_start(sequence); let (holding, after, timed, timescale) = { let index = index.lock().unwrap(); let (holding, after) = index.holding(sequence, frame); @@ -305,32 +274,23 @@ async fn serve_group(shared: &Shared, index: &Mutex, a } loaded }; - let loaded = match asker { - // A lookup owes no group, so nothing says when its readers stop waiting: it finishes - // the read. - Asker::Lookup(_) => load.await, - Asker::Fill(fill) => { - 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 unwanted = std::pin::pin!(fill.unwanted(key)); + let loaded = 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), Err(Error::NotFound(_)) => { - asker.drop_groups(sequence..sequence + 1); + fill.drop_groups(sequence..sequence + 1); return; } Err(err) => { // The store still lists it, so a later reader asks again. tracing::warn!(%path, sequence, %err, "archived group is unavailable"); let err = moq_net::Error::Stream(moq_net::StreamError::Internal); - if let Asker::Fill(fill) = asker { - fill.refuse_groups(sequence..sequence + 1, err); - } + fill.refuse_groups(key..key + 1, err); return; } } @@ -338,13 +298,13 @@ async fn serve_group(shared: &Shared, index: &Mutex, a let Some(first) = objects.first() else { // Older than the oldest stored object: pruned, or never stored. - asker.drop_groups(0..next); + fill.drop_groups(0..next); return; }; let Some(position) = first.entries.iter().position(|entry| entry.sequence >= sequence) else { // A gap after the object's last entry. let last = first.entries.last().map_or(0, |entry| entry.sequence + 1); - asker.drop_groups(last..next); + fill.drop_groups(last..next); return; }; let found = &first.entries[position]; @@ -354,7 +314,7 @@ async fn serve_group(shared: &Shared, index: &Mutex, a 0 => sequence, _ => first.entries[position - 1].sequence + 1, }; - asker.drop_groups(gap..found.sequence); + fill.drop_groups(gap..found.sequence); return; } let start = match position { @@ -363,7 +323,7 @@ async fn serve_group(shared: &Shared, index: &Mutex, a }; if start > frame { // Its head was pruned. - asker.drop_groups(sequence..sequence + 1); + fill.drop_groups(sequence..sequence + 1); return; } @@ -377,18 +337,16 @@ async fn serve_group(shared: &Shared, index: &Mutex, a } _ => { // A continuation went missing, so the group can't be served whole. - asker.drop_groups(sequence..sequence + 1); + fill.drop_groups(sequence..sequence + 1); return; } } } - if let Asker::Fill(fill) = asker - && !fill.is_wanted(sequence) - { + if !fill.is_wanted(key) { return; } - let Ok(Some(mut producer)) = asker.accept(sequence) else { + let Ok(Some(mut producer)) = fill.accept(sequence, None) else { return; }; for (index, stored) in stored { diff --git a/rs/moq-ffi/src/producer.rs b/rs/moq-ffi/src/producer.rs index 7e5addeeef..e6e0151272 100644 --- a/rs/moq-ffi/src/producer.rs +++ b/rs/moq-ffi/src/producer.rs @@ -542,21 +542,12 @@ impl MoqTrackRequests { /// Wait for the next run of groups readers miss: a fetch of an uncached group, or /// history a subscription reads that the cache no longer holds. /// - /// Cached groups are served without reaching this method. - /// - /// A reader's time start that no cached group places is looked up rather than filled; a - /// fill names groups, so the lookup is dropped unanswered here, and the reader falls back - /// to the groups held. + /// Cached groups are served without reaching this method. A reader's time start that no + /// cached group places asks for the group starting at that time, as any other. pub async fn requested_fill(&self) -> Result, MoqError> { let fill = self .reads - .wait(|requests, waiter| { - loop { - if let moq_net::track::Fill::Groups(fill) = ready!(requests.poll_requested_fill(waiter))? { - return Poll::Ready(Ok(fill)); - } - } - }) + .wait(|requests, waiter| requests.poll_requested_fill(waiter).map_err(Into::into)) .await?; Ok(Arc::new(MoqFill::new(fill))) } diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 3c6de0a5b5..96d4d295ec 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -36,8 +36,8 @@ mod fill; mod hole; mod miss; pub(super) use fill::Asking; -use fill::FillState; pub use fill::FillGroups; +use fill::FillState; pub(crate) use hole::Hole; #[cfg_attr(not(feature = "async"), allow(unused_imports, reason = "async-only"))] @@ -846,7 +846,7 @@ pub(crate) struct TrackState { // [`Self::deferred`]). A woken waker may read this track, so none fires under its lock. deferred: Vec, - // The fills readers queued for groups they miss (see [`Fill`]), same reasoning: a miss + // The fills readers queued for groups they miss (see [`FillGroups`]), same reasoning: a miss // queues here and a `Requests` drains. fills: kio::Shared, @@ -3976,7 +3976,7 @@ impl TrackState { Ok(None) } - /// Insert a group a [`Fill`] answered, setting the track's [`Info`] + /// Insert a group a [`FillGroups`] answered, setting the track's [`Info`] /// if it isn't accepted yet. The group's timescale comes from that info, so a /// fetch can serve an as-yet-unaccepted track (e.g. a relay with no live /// subscription). The group lands in the cache so a waiting @@ -9748,10 +9748,9 @@ impl Cursor { let time = lookup.time; let answer = match &lookup.join { None => None, - Some(join) => match join - .outcome() - .poll(waiter, |outcome| outcome.time_start(time).map_or(Poll::Pending, Poll::Ready)) - { + Some(join) => match join.outcome().poll(waiter, |outcome| { + outcome.time_start(time).map_or(Poll::Pending, Poll::Ready) + }) { Poll::Ready(Ok(group)) => group, // Ended unanswered: its handler couldn't tell. Poll::Ready(Err(closed)) => closed.time_start(time).flatten(), diff --git a/rs/moq-net/tests/integration/fetch_cancel.rs b/rs/moq-net/tests/integration/fetch_cancel.rs index 5620837034..cab13f18c8 100644 --- a/rs/moq-net/tests/integration/fetch_cancel.rs +++ b/rs/moq-net/tests/integration/fetch_cancel.rs @@ -65,9 +65,7 @@ async fn abandoned_fetch_reaches_the_publisher(version: &str, relays: u64, stage let groups = request.requests(); let _track = request.accept(timed()); let mut request = match futures::future::select(std::pin::pin!(groups.requested_fill()), &mut waiting).await { - futures::future::Either::Left((request, _)) => { - request.expect("the fetch reaches the publisher") - } + futures::future::Either::Left((request, _)) => request.expect("the fetch reaches the publisher"), futures::future::Either::Right(_) => panic!("nothing answered the fetch"), }; @@ -199,9 +197,7 @@ async fn a_refetch_within_the_linger_stays_on_the_relay(version: &str) { let groups = request.requests(); let _track = request.accept(timed()); let request = match futures::future::select(std::pin::pin!(groups.requested_fill()), &mut waiting).await { - futures::future::Either::Left((request, _)) => { - request.expect("the fetch reaches the publisher") - } + futures::future::Either::Left((request, _)) => request.expect("the fetch reaches the publisher"), futures::future::Either::Right(_) => panic!("nothing answered the fetch"), }; let mut group = request.accept(0, timed()).unwrap().expect("a fresh group"); diff --git a/rs/moq-net/tests/integration/untimed.rs b/rs/moq-net/tests/integration/untimed.rs index 81efd6222e..63dea25fa2 100644 --- a/rs/moq-net/tests/integration/untimed.rs +++ b/rs/moq-net/tests/integration/untimed.rs @@ -299,9 +299,7 @@ async fn a_fetched_untimed_frame_arrives_untimed() { let groups = request.requests(); let _track = request.accept(untimed()); let request = match futures::future::select(std::pin::pin!(groups.requested_fill()), &mut waiting).await { - futures::future::Either::Left((request, _)) => { - request.expect("the fetch reaches the publisher") - } + futures::future::Either::Left((request, _)) => request.expect("the fetch reaches the publisher"), futures::future::Either::Right(_) => panic!("nothing answered the fetch"), }; let mut group = request.accept(3, timed()).unwrap().expect("a fresh group"); diff --git a/rs/moq-transcode/src/lib.rs b/rs/moq-transcode/src/lib.rs index 80e6fa6850..cae28b3dad 100644 --- a/rs/moq-transcode/src/lib.rs +++ b/rs/moq-transcode/src/lib.rs @@ -287,14 +287,6 @@ mod tests { use super::*; - /// A fill the test expects to name groups rather than look up a time. - pub(crate) fn groups(fill: moq_net::track::Fill) -> moq_net::track::FillGroups { - let moq_net::track::Fill::Groups(fill) = fill else { - panic!("a run of groups, not a time lookup"); - }; - fill - } - /// Constructing a transcoder registers its catalog synchronously, so even an unpolled /// run future owns published state that must end deliberately when abandoned. The /// output itself stays open while the caller holds it. @@ -944,11 +936,7 @@ mod tests { // A past group, the only demand on the rung. let rung = consumer.track("video/120p").unwrap(); let fetching = rung.fetch_group(7, None); - let request = source_fetches - .requested_fill() - .await - .map(groups) - .expect("the source track closed"); + let request = source_fetches.requested_fill().await.expect("the source track closed"); let mut group = request.accept(7, None).unwrap().expect("a fresh group"); write_keyframe(&mut group); group.finish().unwrap(); @@ -1012,7 +1000,7 @@ mod tests { let _subscriber = rung.subscribe(past).await.unwrap(); tokio::spawn(async move { - while let Ok(request) = source_fetches.requested_fill().await.map(groups) { + while let Ok(request) = source_fetches.requested_fill().await { let mut group = request .accept(request.start().group, None) .unwrap() @@ -1639,11 +1627,7 @@ mod tests { // The rung's fetch task is now inside its source read, which is upstream of // both its decoder and the `FillGroups::accept` that claims output group 7. - let request = source_fetches - .requested_fill() - .await - .map(groups) - .expect("the source track closed"); + let request = source_fetches.requested_fill().await.expect("the source track closed"); assert_eq!(request.start().group, 7); // Retire the rung with the fetch parked there, and let the retirement land @@ -1720,7 +1704,6 @@ mod tests { let mut abandoned = kio::clock::timeout(std::time::Duration::from_secs(5), source_fetches.requested_fill()) .await .expect("the fetch never reached the source") - .map(groups) .expect("the source track closed"); assert_eq!(abandoned.start().group, 7); @@ -1739,7 +1722,6 @@ mod tests { let fresh = kio::clock::timeout(std::time::Duration::from_secs(5), source_fetches.requested_fill()) .await .expect("the retry never fetched the source") - .map(groups) .expect("the source track closed"); assert_eq!(fresh.start().group, 7); drop(abandoned); diff --git a/rs/moq-transcode/src/rung.rs b/rs/moq-transcode/src/rung.rs index 5c87ebf6aa..2b36a45bb7 100644 --- a/rs/moq-transcode/src/rung.rs +++ b/rs/moq-transcode/src/rung.rs @@ -602,11 +602,7 @@ async fn fetches( }, }; - // The rung can't place a time in its source without fetching it, so a lookup is dropped - // unanswered and its reader falls back to the groups held. - if let moq_net::track::Fill::Groups(fill) = fill { - spawn_fill(&mut tasks, rung.clone(), fill, limit.clone()); - } + spawn_fill(&mut tasks, rung.clone(), fill, limit.clone()); } if retired { @@ -626,8 +622,7 @@ async fn fetches( tokio::select! { biased; fill = dynamic.requested_fill() => match fill { - Ok(moq_net::track::Fill::Groups(fill)) => spawn_fill(&mut tasks, rung.clone(), fill, limit.clone()), - Ok(moq_net::track::Fill::Lookup(_)) => {} + Ok(fill) => spawn_fill(&mut tasks, rung.clone(), fill, limit.clone()), // The output track closed; nothing left to serve. Err(_) => break, }, @@ -1143,7 +1138,6 @@ fn scale_hint(size: moq_video::Size, output: moq_video::Output) -> Option Date: Thu, 19 Nov 2026 23:19:13 -0800 Subject: [PATCH 04/14] wip: js/net mirrors a time start as a group fill --- doc/lib/js/net.md | 2 +- js/net/src/cache.test.ts | 3 +- js/net/src/fill.test.ts | 200 ++++++++++++++-------------- js/net/src/fill.ts | 212 ++++++++++++------------------ js/net/src/ietf/subscriber.ts | 5 - js/net/src/lite/copy_fill.test.ts | 18 +-- js/net/src/lite/fills.test.ts | 19 ++- js/net/src/lite/subscriber.ts | 5 - js/net/src/mock.ts | 12 -- js/net/src/resume.test.ts | 5 +- js/net/src/track.ts | 19 +-- js/net/src/window.test.ts | 6 +- js/net/src/window.ts | 41 +++--- 13 files changed, 239 insertions(+), 308 deletions(-) diff --git a/doc/lib/js/net.md b/doc/lib/js/net.md index 61b037dc39..64510d8ae0 100644 --- a/doc/lib/js/net.md +++ b/doc/lib/js/net.md @@ -53,7 +53,7 @@ route.announce(); - **One receive estimate per connection.** `connection.viewers` divides it among watched broadcasts, max-min fair by each viewer's ceiling, so several players on one pooled connection don't each select against the whole downlink. Holding a viewer requests PROBE, and the viewers' ceilings, summed, are the PROBE target a relay advertising `Increase` probes the downlink up to. `setTarget(0)` opts out. `watch`, `update`, and `setTarget` throw a `RangeError` when the ceilings or the target would pass `Viewers.MAX_TARGET` (`Number.MAX_SAFE_INTEGER`), the most a PROBE target carries. - **List a broadcast's tracks.** `for await (const event of broadcast.tracks())` reports each track the broadcast lists (`added`, with its `Track.Info`), one `synced`, then `added` and `removed` as tracks come and go, until the broadcast ends; leaving the loop stops listing. Over a session it needs `moq-lite-07-wip`, and syncs when the peer does, so a track added after `synced` is new at the source. A track served only on request is never listed. See [discovery](/concept/moq-lite#discovery). - **One track per name.** Concurrent subscriptions share one request and producer. `createTrack` on a name with a request still queued answers it. Groups are addressed by their start: `appendGroup(start)` opens one at a `Time.Timestamp`, in the track's timescale (rounded down), past every group the track holds, and declares the starts between it and the previous append skipped. The broadcast keeps nothing of an ended track, and a name always means the same content, so a track that replaces it keeps its starts moving forward, as its media time does. Starts are `bigint`, so every ID the wire carries fits, past 2^53 included: `track.createGroup(5n)`, `{ time: 10n }`. A frame's position in its group is its `index`, a `number`. -- **Serve history on request.** `track.requests()` opens a handle that serves the groups readers miss: ones their ranges name below the newest group written that the cache no longer holds, or never did. Each run of misses arrives as a `Track.FillGroups` (`kind: "groups"`) from `await requests.requestedFill()`, shared by every reader missing it. `accept(start)` it into the cache and write the group's frames as first published, `dropGroups(start, end)` what the source lacks (starts are `bigint`), and serve `wanted()` until it is `undefined`: a reader that leaves or narrows stops wanting its groups. `close()` the fill when done; whatever is still wanted is named dropped. A `fetchGroup` of an uncached group asks the handle at once, even for a group not written yet, which a live write then answers; with no handle open it fails with `NotFound`, as in Rust, and a reader's miss is given up. A reader only asks for what its `delay.max` still wants, so a reader at the live edge never asks. A range starting at a `time` before every cached group arrives as a `Track.FillLookup` (`kind: "lookup"`) instead: `accept` the group containing its `time` (a `bigint` in the track's timescale), or `close()` it and the reader falls back to the groups cached. This serves lite FETCH and lite-07 ranged subscriptions from the same track, as Rust's `requests()` does. A moq-transport FETCH is refused, so a moq-transport reader never reaches it. +- **Serve history on request.** `track.requests()` opens a handle that serves the groups readers miss: ones their ranges name below the newest group written that the cache no longer holds, or never did. Each run of misses arrives as a `Track.FillGroups` from `await requests.requestedFill()`, shared by every reader missing it. `accept(start)` it into the cache and write the group's frames as first published, `dropGroups(start, end)` what the source lacks (starts are `bigint`), and serve `wanted()` until it is `undefined`: a reader that leaves or narrows stops wanting its groups. `close()` the fill when done; whatever is still wanted is named dropped. A `fetchGroup` of an uncached group asks the handle at once, even for a group not written yet, which a live write then answers; with no handle open it fails with `NotFound`, as in Rust, and a reader's miss is given up. A reader only asks for what its `delay.max` still wants, so a reader at the live edge never asks. A range starting at a `time` no cached group places asks for the group containing it as a fill one key wide from that time, whose `floor` says how far below it that group may start: a handler that knows its groups only by key serves the key as any other, one with an index may `accept` the group below it, and one that can't tell refuses it, so the reader falls back to the groups cached. This serves lite FETCH and lite-07 ranged subscriptions from the same track, as Rust's `requests()` does. A moq-transport FETCH is refused, so a moq-transport reader never reaches it. - **Timing is per track, with no default.** A track that declares a `timescale` carries a `timestamp` on every frame; one without is [untimed](/concept/moq-lite#subscriptions) and each frame carries `Time.Timestamp.unscaled(group.start)`, its group's key, which has no time: `as`, `asMillis`, and `asMicros` throw on it. A scaled timestamp on an untimed track, an unscaled one on a timed track, or another key throws `TimestampMismatch`. Omitting it means untimed, as in Rust. Nothing stamps a frame for you: the publisher passes the timestamp, and the `@moq/json` producers take it beside each value, as `update(value, at)`. - **Durations and instants are `Clock` types.** Every duration (`maxAge`, `delay`, timeouts, RTT) is a `Clock.Duration` and every monotonic time a `Clock.Instant`, from [`@moq/clock`](./clock), which `@moq/net` re-exports as `Clock`: `maxAge: Moq.Clock.Duration.secs(30)`. Neither converts to a number implicitly; read one with an explicit unit, such as `asMillis()`. The wire still carries whole milliseconds, rounded up. Frame timestamps stay `Time.Timestamp`, a tick count at the track's timescale. - **A subscription's `delay` is a `Track.Delay { min, max }`, as in Rust.** Staleness is `delay.max`: it counts from a group's successor, by media time and by wall clock, whichever reaches it first. `delay.min` is the target latency, carried on moq-lite 07; a peer that cannot carry it leaves it `null` (not stated). Passing the old `maxAge` or `maxDelay` key throws a `TypeError` naming `delay.max`. The publisher's own staleness bound is the separate `Track.Info.maxAge`, which caps `delay.max` and fetches. diff --git a/js/net/src/cache.test.ts b/js/net/src/cache.test.ts index 09527874c9..6eff5b642a 100644 --- a/js/net/src/cache.test.ts +++ b/js/net/src/cache.test.ts @@ -4,7 +4,6 @@ import { Manual } from "@moq/clock/test"; import * as cache from "./cache.ts"; import { StreamCode, StreamError, TooFarBehind } from "./error.ts"; import { hooks, staleHole } from "./internal.ts"; -import { groupsOf } from "./mock.ts"; import { Timestamp } from "./time.ts"; import { Producer as TrackProducer } from "./track.ts"; @@ -185,7 +184,7 @@ test("a forgotten stale run is asked of a fill", async () => { const next = reader.recvGroup(); const asked: bigint[][] = []; for (;;) { - const fill = groupsOf(await Promise.race([requests.requestedFill(), yieldNow().then(() => undefined)])); + const fill = await Promise.race([requests.requestedFill(), yieldNow().then(() => undefined)]); if (!fill) break; asked.push([fill.start, fill.end]); } diff --git a/js/net/src/fill.test.ts b/js/net/src/fill.test.ts index 75e115cd76..a4f9e368ad 100644 --- a/js/net/src/fill.test.ts +++ b/js/net/src/fill.test.ts @@ -6,11 +6,11 @@ import { DEFAULT_EXPIRY } from "./cache.ts"; import { accept as acceptSession, connect as connectSession } from "./connection/index.ts"; import { defined } from "./defined.test-util.ts"; import { NotFound, StreamCode, StreamError, toStreamCode } from "./error.ts"; -import { type FillLookup, rejectGroups } from "./fill.ts"; +import { rejectGroups } from "./fill.ts"; import type { Consumer as GroupConsumer } from "./group.ts"; import { hooks, staleHole } from "./internal.ts"; import * as Lite from "./lite/index.ts"; -import { createMockTransportPair, groupsOf, requestedGroups } from "./mock.ts"; +import { createMockTransportPair } from "./mock.ts"; import { Producer as OriginProducer } from "./origin.ts"; import * as Path from "./path.ts"; import { Timescale, Timestamp } from "./time.ts"; @@ -31,7 +31,7 @@ type Archive = Map; function respond(requests: Requests, archive: Archive): FillGroups[] { const fills: FillGroups[] = []; void (async () => { - for (let fill = await requestedGroups(requests); fill; fill = await requestedGroups(requests)) { + for (let fill = await requests.requestedFill(); fill; fill = await requests.requestedFill()) { fills.push(fill); serve(fill, archive); } @@ -130,8 +130,8 @@ test("fill: a fetch of a group about to be written asks the handler at once, and const { broadcast, track } = resumed(5n); const requests = track.requests(); const fetching = wireOf(broadcast).fetchGroup("video", 7n); - const fill = await requestedGroups(requests); - if (fill?.kind !== "groups") throw new Error("no fill of groups"); + const fill = await requests.requestedFill(); + if (!fill) throw new Error("no fill of groups"); expect([fill.start, fill.end]).toEqual([7n, 8n]); expect(fill.wanted()).toBe(7n); @@ -163,8 +163,8 @@ test("fill: concurrent fetches of one group share one fill", async () => { const a = wireOf(broadcast).fetchGroup("video", 3n); const b = wireOf(broadcast).fetchGroup("video", 3n); - const fill = await requestedGroups(requests); - if (fill?.kind !== "groups") throw new Error("no fill of groups"); + const fill = await requests.requestedFill(); + if (!fill) throw new Error("no fill of groups"); expect([fill.start, fill.end]).toEqual([3n, 4n]); // The second fetch joined it rather than queueing its own. await yieldNow(); @@ -186,8 +186,8 @@ test("fill: a fetch cancelled while its fill awaits a reply withdraws the fill", const controller = new AbortController(); const fetching = wireOf(broadcast).fetchGroup("video", 2n, { signal: controller.signal }); - const fill = await requestedGroups(requests); - if (fill?.kind !== "groups") throw new Error("no fill of groups"); + const fill = await requests.requestedFill(); + if (!fill) throw new Error("no fill of groups"); expect(await fill.wantedChanged()).toBe(2n); expect(fill.isWanted(2n)).toBe(true); @@ -199,7 +199,7 @@ test("fill: a fetch cancelled while its fill awaits a reply withdraws the fill", // Nothing reads it, so closing the withdrawn fill records nothing. fill.close(); const later = wireOf(broadcast).fetchGroup("video", 2n); - const refill = await requestedGroups(requests); + const refill = await requests.requestedFill(); expect(refill?.start).toBe(2n); if (refill) serve(refill, new Map([[2n, ["late"]]])); expect(await frames(await later)).toEqual(["late"]); @@ -214,8 +214,8 @@ test("fill: an answer aborted below the end does not hold the end open", async ( const { broadcast, track } = resumed(1n); const requests = track.requests(); const fetching = wireOf(broadcast).fetchGroup("video", 0n); - const fill = await requestedGroups(requests); - if (fill?.kind !== "groups") throw new Error("no fill of groups"); + const fill = await requests.requestedFill(); + if (!fill) throw new Error("no fill of groups"); const group = defined(fill.accept(0n)); group.writeFrame({ payload: enc.encode("backfill"), timestamp: Timestamp.unscaled(group.start) }); if (parked) { @@ -256,8 +256,8 @@ async function failedAnswerHoldsTheEnd(error: Error) { expect((await ordered.nextGroup())?.start).toBe(0n); const requests = track.requests(); const fetching = wireOf(broadcast).fetchGroup("video", 1n); - const fill = await requestedGroups(requests); - if (fill?.kind !== "groups") throw new Error("no fill of groups"); + const fill = await requests.requestedFill(); + if (!fill) throw new Error("no fill of groups"); defined(fill.accept(1n)).abort(error); fill.close(); await fetching.catch(() => undefined); @@ -299,7 +299,7 @@ test("fill: a group a lowered floor asks the feed for is not asked of a fill", a hooks.receiveGroup(track, { start: 1n, offset: 0 }).finish(); expect((await live.recvGroup())?.start).toBe(1n); const requests = track.requests(); - const asked = requestedGroups(requests); + const asked = requests.requestedFill(); const history = track.subscribe({ ranges: [{ start: { time: 0n }, end: { time: 0n } }], @@ -326,8 +326,8 @@ test("fill: a failed answer is withheld from a subscribed reader that reads at o const reader = track.subscribe({ ranges: [{ start: { time: 0n } }], delay: { max: null } }); const requests = track.requests(); const fetching = wireOf(broadcast).fetchGroup("video", 1n); - const fill = await requestedGroups(requests); - if (fill?.kind !== "groups") throw new Error("no fill of groups"); + const fill = await requests.requestedFill(); + if (!fill) throw new Error("no fill of groups"); defined(fill.accept(1n)).abort(new Error("archive failed")); const seen: unknown[] = []; for (let recv = hooks.tryRecvGroup(reader, dispositions); recv.kind !== "idle"; ) { @@ -350,8 +350,8 @@ test("fill: a held answer that fails keeps its frames and its failure", async () const track = defined(broadcast.createTrack("video", { maxAge: null })); const requests = track.requests(); const fetching = wireOf(broadcast).fetchGroup("video", 1n); - const fill = await requestedGroups(requests); - if (fill?.kind !== "groups") throw new Error("no fill of groups"); + const fill = await requests.requestedFill(); + if (!fill) throw new Error("no fill of groups"); const answer = defined(fill.accept(1n)); answer.writeFrame({ payload: enc.encode("fill 1"), timestamp: Timestamp.unscaled(answer.start) }); const group = await fetching; @@ -378,9 +378,9 @@ test("fill: an answer withheld after a probe judged it is asked again", async () }); const ordered = reader.ordered(); const first = ordered.nextGroup(); - const fill = await requestedGroups(requests); + const fill = await requests.requestedFill(); expect(fill && [fill.start, fill.end]).toEqual([0n, 4n]); - if (fill?.kind !== "groups") throw new Error("no fill of groups"); + if (!fill) throw new Error("no fill of groups"); defined(fill.accept(0n)).finish(); const answer = defined(fill.accept(2n)); answer.writeFrame({ payload: enc.encode("fill 2"), timestamp: Timestamp.unscaled(answer.start) }); @@ -393,7 +393,7 @@ test("fill: an answer withheld after a probe judged it is asked again", async () expect((await second)?.start).toBe(1n); const third = ordered.nextGroup(); - const again = groupsOf(await Promise.race([requests.requestedFill(), yieldNow().then(() => undefined)])); + const again = await Promise.race([requests.requestedFill(), yieldNow().then(() => undefined)]); expect(again && [again.start, again.end]).toEqual([2n, 3n]); if (again) serve(again, new Map([[2n, ["again"]]])); expect((await third)?.start).toBe(2n); @@ -422,7 +422,7 @@ test("fill: a gap below a group that fails after a probe is asked", async () => group.abort(new StreamError(StreamCode.Cancel)); await yieldNow(); const next = ordered.nextGroup(); - const fill = groupsOf(await Promise.race([requests.requestedFill(), yieldNow().then(() => undefined)])); + const fill = await Promise.race([requests.requestedFill(), yieldNow().then(() => undefined)]); expect(fill && [fill.start, fill.end]).toEqual([0n, 2n]); fill?.close(); @@ -444,7 +444,7 @@ test("fill: a gap the newest group rises over after a probe is asked", async () group.finish(); expect(reader.tryRecvGroup()?.start).toBe(5n); reader.tryRecvGroup(); - const fill = groupsOf(await Promise.race([requests.requestedFill(), yieldNow().then(() => undefined)])); + const fill = await Promise.race([requests.requestedFill(), yieldNow().then(() => undefined)]); expect(fill && [fill.start, fill.end]).toEqual([3n, 5n]); fill?.close(); @@ -460,7 +460,7 @@ test("fill: a miss judged with no handler open is asked once one opens", async ( const reader = track.subscribe({ ranges: [{ start: { time: 0n } }], delay: { max: null } }); expect(reader.tryRecvGroup()?.start).toBe(2n); reader.tryRecvGroup(); - const out = await requestedGroups(first); + const out = await first.requestedFill(); expect(out && [out.start, out.end]).toEqual([0n, 2n]); first.close(); const group = track.createGroup(5n); @@ -470,7 +470,7 @@ test("fill: a miss judged with no handler open is asked once one opens", async ( const second = track.requests(); reader.tryRecvGroup(); - const fill = groupsOf(await Promise.race([second.requestedFill(), yieldNow().then(() => undefined)])); + const fill = await Promise.race([second.requestedFill(), yieldNow().then(() => undefined)]); expect(fill && [fill.start, fill.end]).toEqual([3n, 5n]); fill?.close(); @@ -548,7 +548,7 @@ test("fill: closing a fill drops what is still wanted, and closing the handler d const { broadcast, track } = resumed(5n); const requests = track.requests(); const a = wireOf(broadcast).fetchGroup("video", 1n); - const fill = await requestedGroups(requests); + const fill = await requests.requestedFill(); fill?.close(); await expect(a).rejects.toBeInstanceOf(NotFound); @@ -566,15 +566,15 @@ test("fill: a refusal fails the readers waiting now, and a later reader asks aga const { broadcast, track } = resumed(5n); const requests = track.requests(); const waiting = wireOf(broadcast).fetchGroup("video", 2n); - const fill = await requestedGroups(requests); - if (fill?.kind !== "groups") throw new Error("no fill of groups"); + const fill = await requests.requestedFill(); + if (!fill) throw new Error("no fill of groups"); const refusal = new StreamError(StreamCode.Cancel, { message: "storage busy" }); fill.refuseGroups(2n, 3n, refusal); fill.close(); await expect(waiting).rejects.toBe(refusal); const later = wireOf(broadcast).fetchGroup("video", 2n); - const again = await requestedGroups(requests); + const again = await requests.requestedFill(); expect(again?.start).toBe(2n); if (again) serve(again, new Map([[2n, ["ok"]]])); expect(await frames(await later)).toEqual(["ok"]); @@ -587,8 +587,8 @@ test("fill: a group arriving after a refusal reaches the reader the refusal name const { broadcast, track } = resumed(5n); const requests = track.requests(); const waiting = wireOf(broadcast).fetchGroup("video", 2n); - const fill = await requestedGroups(requests); - if (fill?.kind !== "groups") throw new Error("no fill of groups"); + const fill = await requests.requestedFill(); + if (!fill) throw new Error("no fill of groups"); fill.refuseGroups(2n, 3n, new StreamError(StreamCode.Cancel, { message: "storage busy" })); // Before the waiting fetch looks again. const group = track.createGroup(2n); @@ -609,8 +609,8 @@ test("fill: the track's skip of a run a reader's fill refused is what the reader delay: { max: null }, }); const first = reader.recvGroup(); - const fill = await requestedGroups(requests); - if (fill?.kind !== "groups") throw new Error("no fill of groups"); + const fill = await requests.requestedFill(); + if (!fill) throw new Error("no fill of groups"); fill.refuseGroups(0n, 2n, new StreamError(StreamCode.Cancel, { message: "storage busy" })); // The publisher declares the refused run never produced; its verdict wins, as in Rust. track.skipGroups(0n, 2n); @@ -627,8 +627,8 @@ test("fill: a fill names the most urgent priority and loosest budget among its r const requests = track.requests(); const low = track.subscribe({ ...history, priority: 1, delay: { max: Duration.millis(60_000) } }); void drain(low); - const fill = await requestedGroups(requests); - if (fill?.kind !== "groups") throw new Error("no fill of groups"); + const fill = await requests.requestedFill(); + if (!fill) throw new Error("no fill of groups"); expect([fill.start, fill.end]).toEqual([0n, 5n]); expect(fill.priority).toBe(1); expect(fill.maxDelay).toEqual(Duration.millis(60_000)); @@ -660,8 +660,8 @@ test("fill: a stale verdict leaves an outstanding fill owed", async () => { const requests = track.requests(); track.createGroup(3n).finish(); const fetching = wireOf(broadcast).fetchGroup("video", 2n); - const fill = await requestedGroups(requests); - if (fill?.kind !== "groups") throw new Error("no fill of groups"); + const fill = await requests.requestedFill(); + if (!fill) throw new Error("no fill of groups"); expect(fill.wanted()).toBe(2n); hooks.dropTrackGroups(track, 2n, 3n, staleHole(500)); @@ -683,8 +683,8 @@ test("fill: a group arriving live answers the fill", async () => { const late = track.createGroup(3n); late.finish(); const fetching = wireOf(broadcast).fetchGroup("video", 1n); - const fill = await requestedGroups(requests); - if (fill?.kind !== "groups") throw new Error("no fill of groups"); + const fill = await requests.requestedFill(); + if (!fill) throw new Error("no fill of groups"); // The producer writes it itself before the handler gets to it. const live = track.createGroup(1n); @@ -712,7 +712,7 @@ test("fill: an ordered reader stops waiting on an unanswered fill once its budge }); const next = reader.ordered().nextGroup(); // The handler takes the fill for the missing groups and never answers. - const fill = await requestedGroups(requests); + const fill = await requests.requestedFill(); expect([fill?.start, fill?.end]).toEqual([0n, 5n]); // Group 5 arrived when the reader asked, so its budget gives up the holes below a second on. @@ -741,7 +741,7 @@ test("fill: an arrival-order reader stops waiting on an unanswered fill once its }); expect((await reader.recvGroup())?.start).toBe(5n); const next = reader.recvGroup(); - const fill = await requestedGroups(requests); + const fill = await requests.requestedFill(); expect([fill?.start, fill?.end]).toEqual([0n, 5n]); // The fill never answers; the holes go past the budget, and the finished track ends the read. @@ -771,8 +771,8 @@ test("fill: a handler hears that a reader stopped wanting what it handed out pas order: "ascending", }); const next = reader.ordered().nextGroup(); - const fill = await requestedGroups(requests); - if (fill?.kind !== "groups") throw new Error("no fill of groups"); + const fill = await requests.requestedFill(); + if (!fill) throw new Error("no fill of groups"); expect(await fill.wantedChanged()).toBe(0n); const changed = fill.wantedChanged(); const unwanted = fill.unwanted(0n); @@ -821,7 +821,7 @@ test("fill: a bounded reader keeps waiting on a fresh fill once an older one goe const next = reader.recvGroup(); const fills: FillGroups[] = []; for (let i = 0; i < 2; i++) { - const fill = await requestedGroups(requests); + const fill = await requests.requestedFill(); if (fill) fills.push(fill); } expect(fills.map((fill) => [fill.start, fill.end])).toEqual([ @@ -854,8 +854,8 @@ test("fill: a reader joining a wider fill is owed only its own ranges", async () order: "ascending", }); void drain(wide); - const fill = await requestedGroups(requests); - if (fill?.kind !== "groups") throw new Error("no fill of groups"); + const fill = await requests.requestedFill(); + if (!fill) throw new Error("no fill of groups"); expect([fill.start, fill.end]).toEqual([0n, 10n]); // A narrower reader joins the same fill for 2 and 7 alone. @@ -874,14 +874,16 @@ test("fill: a reader joining a wider fill is owed only its own ranges", async () group.close(); } })(); - // Its time starts precede every group held, so each is looked up, but it asks no fill of groups. - const looked: FillLookup[] = []; + // Its time starts precede every group held, so each asks for the group containing it, one key + // wide and floored at 0, but it asks no other fill. + const looked: FillGroups[] = []; for (let i = 0; i < 2; i++) { const lookup = await requests.requestedFill(); - if (lookup?.kind !== "lookup") throw new Error("no lookup"); + if (!lookup) throw new Error("no time start"); + expect([lookup.floor, lookup.end - lookup.start]).toEqual([0n, 1n]); looked.push(lookup); } - expect(looked.map((lookup) => lookup.time).sort()).toEqual([2n, 7n]); + expect(looked.map((lookup) => lookup.start).sort()).toEqual([2n, 7n]); await yieldNow(); expect(await Promise.race([requests.requestedFill(), yieldNow().then(() => "none")])).toBe("none"); @@ -890,8 +892,8 @@ test("fill: a reader joining a wider fill is owed only its own ranges", async () group.writeFrame({ payload: enc.encode(`old ${key}`), timestamp: Timestamp.unscaled(group.start) }); group.finish(); } - // A handler that can't tell leaves each start at the group held containing its time. - for (const lookup of looked) lookup.close(); + // Each start's group landed through the wide fill, which answers it. + for (const lookup of looked) expect(lookup.wanted()).toBeUndefined(); // The wide reader still waits on the rest of the fill; the narrow one has all it reads. await done; expect(got).toEqual([2n, 7n]); @@ -910,8 +912,8 @@ test("fill: a group no reader reads any more stays withdrawn, and widening back const ranges = (end: bigint) => [{ start: { time: 0n }, end: { time: end } }]; const reader = track.subscribe({ ranges: ranges(4n), delay: { max: null }, order: "ascending" }); void drain(reader); - const fill = await requestedGroups(requests); - if (fill?.kind !== "groups") throw new Error("no fill of groups"); + const fill = await requests.requestedFill(); + if (!fill) throw new Error("no fill of groups"); expect([fill.start, fill.end]).toEqual([0n, 5n]); reader.update({ ranges: ranges(2n), delay: { max: null }, order: "ascending" }); @@ -922,7 +924,7 @@ test("fill: a group no reader reads any more stays withdrawn, and widening back reader.update({ ranges: ranges(4n), delay: { max: null }, order: "ascending" }); await yieldNow(); expect(fill.isWanted(3n)).toBe(false); - const again = await requestedGroups(requests); + const again = await requests.requestedFill(); expect([again?.start, again?.end]).toEqual([3n, 5n]); // Each narrowing and widening back asks anew, never spending the refill an eviction gets. @@ -930,7 +932,7 @@ test("fill: a group no reader reads any more stays withdrawn, and widening back reader.update({ ranges: ranges(4n), delay: { max: null }, order: "ascending" }); await yieldNow(); expect(again?.isWanted(3n)).toBe(false); - const third = await requestedGroups(requests); + const third = await requests.requestedFill(); expect([third?.start, third?.end]).toEqual([3n, 5n]); third?.close(); @@ -954,14 +956,14 @@ test("fill: a refusal under an old budget is asked afresh on each widening, neve const old = new StreamError(StreamCode.Old, { message: "old" }); // Each widening past the last refusal's budget asks a new fill for the run. for (const budget of [60_000, 120_000, 240_000]) { - const fill = await requestedGroups(requests); + const fill = await requests.requestedFill(); expect([fill?.start, fill?.end]).toEqual([0n, 5n]); - if (fill?.kind !== "groups") throw new Error("no fill of groups"); + if (!fill) throw new Error("no fill of groups"); rejectGroups(fill, 0n, 5n, old, { budget, everyone: false }); fill.close(); reader.update(terms(budget * 2)); } - expect(await requestedGroups(requests)).toMatchObject({ start: 0n, end: 5n }); + expect(await requests.requestedFill()).toMatchObject({ start: 0n, end: 5n }); reader.close(); requests.close(); @@ -974,8 +976,8 @@ test("fill: what a reader's sequence cursor passes, a fill no longer owes it", a const reader = track.subscribe(history); // A logical reader that already holds what lies below the floor raises its copy's cursor. expect(hooks.tryNextGroup(reader, 0n).kind).toBe("idle"); - const fill = await requestedGroups(requests); - if (fill?.kind !== "groups") throw new Error("no fill of groups"); + const fill = await requests.requestedFill(); + if (!fill) throw new Error("no fill of groups"); expect(fill.wanted()).toBe(0n); const changed = fill.wantedChanged(); await changed; @@ -1000,8 +1002,8 @@ test("fill: a filled group stays wanted only while a reader will still read it", const reader = track.subscribe(history); const next = reader.ordered().nextGroup(); - const fill = await requestedGroups(requests); - if (fill?.kind !== "groups") throw new Error("no fill of groups"); + const fill = await requests.requestedFill(); + if (!fill) throw new Error("no fill of groups"); expect(fill.wanted()).toBe(0n); const source = defined(fill.accept(0n)); source.writeFrame({ payload: enc.encode("g0"), timestamp: Timestamp.unscaled(source.start) }); @@ -1039,8 +1041,8 @@ test("fill: a reader changing only its order wakes the handler", async () => { const requests = track.requests(); const reader = track.subscribe(history); void drain(reader); - const fill = await requestedGroups(requests); - if (fill?.kind !== "groups") throw new Error("no fill of groups"); + const fill = await requests.requestedFill(); + if (!fill) throw new Error("no fill of groups"); expect(await fill.wantedChanged()).toBe(0n); const changed = fill.wantedChanged(); @@ -1064,7 +1066,7 @@ test("fill: a reader parked on a miss asks once a handler opens", async () => { // A handler opening wakes it to ask. const requests = track.requests(); - const fill = await requestedGroups(requests); + const fill = await requests.requestedFill(); expect([fill?.start, fill?.end]).toEqual([0n, 5n]); if (fill) serve(fill, new Map([[0n, ["old 0"]]])); expect((await next)?.start).toBe(0n); @@ -1078,8 +1080,8 @@ test("fill: a reader joins a fill still served after its handler's handle closed const { broadcast, track } = resumed(5n); const requests = track.requests(); const first = wireOf(broadcast).fetchGroup("video", 2n); - const fill = await requestedGroups(requests); - if (fill?.kind !== "groups") throw new Error("no fill of groups"); + const fill = await requests.requestedFill(); + if (!fill) throw new Error("no fill of groups"); requests.close(); // The fill is still out, so a second fetch joins it rather than failing at once. @@ -1098,8 +1100,8 @@ test("fill: a group the track gives up elsewhere answers the fill", async () => const { broadcast, track } = resumed(5n); const requests = track.requests(); const fetching = wireOf(broadcast).fetchGroup("video", 2n); - const fill = await requestedGroups(requests); - if (fill?.kind !== "groups") throw new Error("no fill of groups"); + const fill = await requests.requestedFill(); + if (!fill) throw new Error("no fill of groups"); expect(fill.wanted()).toBe(2n); // An upstream SUBSCRIBE_DROP, say: the handler is no longer asked for it. @@ -1125,7 +1127,7 @@ test("fill: a stale run that does not settle the reader is still asked for", asy delay: { max: Duration.millis(500) }, }); void drain(reader); - const fill = groupsOf(await Promise.race([requests.requestedFill(), yieldNow().then(() => undefined)])); + const fill = await Promise.race([requests.requestedFill(), yieldNow().then(() => undefined)]); expect(fill && [fill.start, fill.end]).toEqual([0n, 2n]); fill?.close(); @@ -1150,7 +1152,7 @@ test("fill: a stale run given up after a probe is asked on the next", async () = const asked = async () => { const fills: bigint[][] = []; for (;;) { - const fill = groupsOf(await Promise.race([requests.requestedFill(), yieldNow().then(() => undefined)])); + const fill = await Promise.race([requests.requestedFill(), yieldNow().then(() => undefined)]); if (!fill) return fills; fills.push([fill.start, fill.end]); fill.close(); @@ -1176,8 +1178,8 @@ test("fill: a stale run that does not settle the reader leaves its own refusal r delay: { max: Duration.millis(500) }, }); const first = reader.recvGroup(); - const fill = await requestedGroups(requests); - if (fill?.kind !== "groups") throw new Error("no fill of groups"); + const fill = await requests.requestedFill(); + if (!fill) throw new Error("no fill of groups"); const refusal = new StreamError(StreamCode.Cancel, { message: "storage busy" }); fill.refuseGroups(0n, 2n, refusal); expect(hooks.tryDrop(reader)).toMatchObject({ kind: "drop", start: 0n, end: 2n, error: refusal }); @@ -1196,8 +1198,8 @@ test("fill: a stale run that does not settle the reader keeps its pending refusa delay: { max: Duration.millis(500) }, }); const first = reader.recvGroup(); - const fill = await requestedGroups(requests); - if (fill?.kind !== "groups") throw new Error("no fill of groups"); + const fill = await requests.requestedFill(); + if (!fill) throw new Error("no fill of groups"); const refusal = new StreamError(StreamCode.Cancel, { message: "storage busy" }); fill.refuseGroups(0n, 1n, refusal); fill.refuseGroups(3n, 4n, refusal); @@ -1224,8 +1226,8 @@ test("fill: a cache-miss fetch of a dynamic track serves the one track the broad const group = track.createGroup(4n); group.finish(); - const fill = await requestedGroups(requests); - if (fill?.kind !== "groups") throw new Error("no fill of groups"); + const fill = await requests.requestedFill(); + if (!fill) throw new Error("no fill of groups"); serve(fill, new Map([[1n, ["one"]]])); expect(await frames(await fetching)).toEqual(["one"]); @@ -1233,7 +1235,7 @@ test("fill: a cache-miss fetch of a dynamic track serves the one track the broad const second = wireOf(broadcast).fetchGroup("video", 2n); const queued = await Promise.race([wireOf(broadcast).requested(), yieldNow().then(() => "none")]); expect(queued).toBe("none"); - const refill = await requestedGroups(requests); + const refill = await requests.requestedFill(); if (refill) serve(refill, new Map([[2n, ["two"]]])); expect(await frames(await second)).toEqual(["two"]); @@ -1262,7 +1264,7 @@ async function session(alpn: string) { const requests = track.requests(); const fills: FillGroups[] = []; void (async () => { - for (let fill = await requestedGroups(requests); fill; fill = await requestedGroups(requests)) { + for (let fill = await requests.requestedFill(); fill; fill = await requests.requestedFill()) { fills.push(fill); for (let key = fill.wanted(); key !== undefined; key = fill.wanted()) { if (key === 1n) { @@ -1362,7 +1364,7 @@ test("fill: an OLD answer under the max age is held as stale until forgotten", a const asked = async () => { const fills: bigint[][] = []; for (;;) { - const fill = groupsOf(await Promise.race([requests.requestedFill(), yieldNow().then(() => undefined)])); + const fill = await Promise.race([requests.requestedFill(), yieldNow().then(() => undefined)]); if (!fill) return fills; fills.push([fill.start, fill.end]); fill.close(); @@ -1374,8 +1376,8 @@ test("fill: an OLD answer under the max age is held as stale until forgotten", a // A reader of the run alone, which the verdict does not settle, waits on the same fill. const bounded = track.subscribe({ ...terms, ranges: [{ start: { time: 0n }, end: { time: 2n } }] }); bounded.tryRecvGroup(); - const fill = await requestedGroups(requests); - if (fill?.kind !== "groups") throw new Error("no fill of groups"); + const fill = await requests.requestedFill(); + if (!fill) throw new Error("no fill of groups"); expect([fill.start, fill.end]).toEqual([0n, 3n]); const old = new StreamError(StreamCode.Old, { message: "old" }); rejectGroups(fill, 0n, 3n, old, { budget: 10_000, everyone: true }); @@ -1463,7 +1465,7 @@ test("fill: a refused head gives its group up, and the bounded reader moves past const { broadcast, track } = headless(); const requests = track.requests(); void (async () => { - for (let fill = await requestedGroups(requests); fill; fill = await requestedGroups(requests)) { + for (let fill = await requests.requestedFill(); fill; fill = await requests.requestedFill()) { fill.refuseGroups(fill.start, fill.end, new NotFound("no head")); fill.close(); } @@ -1489,7 +1491,7 @@ test.each([ const requests = track.requests(); let asked = 0; void (async () => { - for (let fill = await requestedGroups(requests); fill; fill = await requestedGroups(requests)) { + for (let fill = await requests.requestedFill(); fill; fill = await requests.requestedFill()) { // A reader asking again would loop forever: refuse past a few, so the test still ends. if (++asked > 3) fill.refuseGroups(fill.start, fill.end, new NotFound("asked again")); else if (end === "drop") fill.dropGroups(fill.start, fill.end); @@ -1514,7 +1516,7 @@ test("fill: a drop of a held group landing while its head is asked leaves the he const requests = track.requests(); const ordered = track.subscribe(twoAndThree).ordered(); const first = ordered.nextGroup(); - const fill = await requestedGroups(requests); + const fill = await requests.requestedFill(); if (!fill) throw new Error("no head asked"); expect([fill.start, fill.end]).toEqual([2n, 3n]); @@ -1577,16 +1579,18 @@ function timed(): { broadcast: BroadcastProducer; track: Producer } { return { broadcast, track }; } -// Answer each lookup with `lookup(time)`, closing it unanswered when that is undefined, and serve -// every fill of groups from the archive. Returns the lookups' times, in milliseconds. +// Answer each time start with `lookup(time)`, refusing it when that is undefined, as a handler that +// can't tell does, and serve every other fill from the archive. Returns the time starts' times, in +// milliseconds. function lookups(requests: Requests, lookup: (time: bigint) => bigint | undefined): bigint[] { const times: bigint[] = []; void (async () => { for (let fill = await requests.requestedFill(); fill; fill = await requests.requestedFill()) { - if (fill.kind === "lookup") { - times.push(fill.time); - const answer = lookup(fill.time); - if (answer !== undefined) { + if (fill.floor < fill.start) { + times.push(fill.start); + const answer = lookup(fill.start); + if (answer === undefined) fill.refuseGroups(fill.start, fill.end, new Error("no index")); + else { const group = defined(fill.accept(answer)); group.writeFrame({ payload: enc.encode(`g${answer}`), @@ -1676,8 +1680,8 @@ test("fill: a lookup's answer is read at once, before its first frame: its key i const next = reader.ordered().nextGroup(); // The handler answers with group 2 but writes no frame yet. const lookup = await requests.requestedFill(); - if (lookup?.kind !== "lookup") throw new Error("no lookup"); - expect(lookup.time).toBe(2n); + if (!lookup) throw new Error("no time start"); + expect([lookup.start, lookup.floor]).toEqual([2n, 0n]); const found = defined(lookup.accept(2n)); expect((await next)?.start).toBe(2n); @@ -1687,7 +1691,7 @@ test("fill: a lookup's answer is read at once, before its first frame: its key i broadcast.close(); }); -test("fill: a lookup closed unanswered starts the range at its time, asking nothing below it", async () => { +test("fill: a time start refused starts the range at its time, asking nothing below it", async () => { const { broadcast, track } = timed(); const requests = track.requests(); const times = lookups(requests, () => undefined); @@ -1726,8 +1730,8 @@ test("fill: replacing the ranges withdraws a lookup still out", async () => { const reader = track.subscribe(fromTime(2n)); expect(reader.tryRecvGroup()).toBeUndefined(); const lookup = await requests.requestedFill(); - if (lookup?.kind !== "lookup") throw new Error("no lookup"); - expect(lookup.time).toBe(2n); + if (!lookup) throw new Error("no time start"); + expect(lookup.start).toBe(2n); // It follows the readers waiting on it: the one with no age budget, until it leaves. expect(lookup.maxDelay).toBeNull(); diff --git a/js/net/src/fill.ts b/js/net/src/fill.ts index 9aea90be29..b6dd2939f1 100644 --- a/js/net/src/fill.ts +++ b/js/net/src/fill.ts @@ -1,10 +1,10 @@ /** * Serving the groups a track's readers miss: the on-demand request surface. Mirrors the Rust - * `track::Requests` and `track::Fill` (`rs/moq-net/src/model/track/fill.rs`). + * `track::Requests` and `track::FillGroups` (`rs/moq-net/src/model/track/fill.rs`). * * A reader misses a group its ranges name that the track no longer caches, or never did, below * the newest group the producer wrote: nothing above it is a miss, since it arrives live. While a - * {@link Requests} handle is open, each run of misses queues a {@link Fill} for it to serve, and + * {@link Requests} handle is open, each run of misses queues a {@link FillGroups} for it to serve, and * readers missing the same groups join the one fill rather than asking twice. With none open, a * miss is given up as it always was. * @@ -86,6 +86,12 @@ type Live = Run & { pending: Pending }; class Pending { readonly start: bigint; readonly end: bigint; + // The oldest key the first group may start at: below `start` for a time start, whose group is + // the newest one starting at or before it. + readonly floor: bigint; + // A time start's, which no other reader joins: it owes its key while its reader waits on it, + // whatever the reader's window reads, as the Rust `Join` holds its key. + readonly exclusive: boolean; readonly askers = new Set(); // The groups answered: accepted, delivered live, dropped, or refused. readonly resolved: Run[] = []; @@ -98,25 +104,23 @@ class Pending { done = false; // Bumped on every change to what the fill owes, for its handler to wake on. readonly revision = new Signal(0); - // The time a lookup asks for, which owes no group. - readonly time?: bigint; - // The first group a lookup's handler accepted. + // The group a time start's handler accepted. answer?: bigint; - constructor(start: bigint, end: bigint, time?: bigint) { + constructor(start: bigint, end: bigint, floor = start, exclusive = false) { this.start = start; this.end = end; - this.time = time; + this.floor = floor; + this.exclusive = exclusive; } bump(): void { this.revision.update((revision) => revision + 1); } - // The parts of `[start, end)` within the fill still unanswered and not withdrawn: none for a - // lookup, which owes no group. + // The parts of `[start, end)` within the fill still unanswered and not withdrawn. unresolved(start = this.start, end = this.end): Run[] { - if (this.done || this.time !== undefined) return []; + if (this.done) return []; return runGaps(this.resolved, max(start, this.start), min(end, this.end)).flatMap((gap) => runGaps(this.withdrawn, gap.start, gap.end), ); @@ -124,6 +128,7 @@ class Pending { // The parts of `[start, end)` still unanswered that a reader still reads. owed(start = this.start, end = this.end): Run[] { + if (this.exclusive) return this.askers.size > 0 ? this.unresolved(start, end) : []; const owed: Run[] = []; for (const gap of this.unresolved(start, end)) { for (const asker of this.askers) { @@ -149,6 +154,9 @@ export class FillState { // What each fill still owes, by key: its unanswered, unwithdrawn parts, which no two fills // share, so a reader's ask or an arrival costs the fills it touches, never every fill out. #live: Live[] = []; + // The time starts' fills out, by their key, which no live part holds: a group landing there + // answers each, as the Rust `FillGroups::wanted` finds it cached. + readonly #starts = new Map>(); // Bumped as each handler opens, so readers parked on a miss nobody could take ask again. readonly #opened = new Signal(0); @@ -197,14 +205,18 @@ export class FillState { } /** - * Queue a fill looking up `time` among the groups below `end` for `asker`, as the Rust - * `FillState::queue_lookup` does. Undefined while no handler is open to answer it. + * Queue a fill of the group containing `time` for `asker`: the group starting at it, or else the + * newest one before it, starting at or after `floor`, as the Rust `FillState::time_start` does. + * No other reader joins it. Undefined while no handler is open to answer it. */ - lookup(time: bigint, end: bigint, asker: Asker): Lookup | undefined { + timeStart(time: bigint, floor: bigint, asker: Asker): Lookup | undefined { if (this.#handlers === 0 || this.#track.closed()) return undefined; - const pending = new Pending(0n, end, time); + const pending = new Pending(time, time + 1n, min(floor, time), true); pending.askers.add(asker); this.#pending.add(pending); + const starts = this.#starts.get(time) ?? new Set(); + starts.add(pending); + this.#starts.set(time, starts); this.#queue.push(pending); this.#queued.update((revision) => revision + 1); return { @@ -263,6 +275,10 @@ export class FillState { arrived(key: bigint): void { const [live] = this.#overlapping(key, key + 1n); if (live) this.#resolve(live.pending, key, key + 1n); + for (const pending of this.#starts.get(key) ?? []) { + pending.answer = key; + this.#resolve(pending, key, key + 1n); + } } /** @@ -289,8 +305,10 @@ export class FillState { return found; } - // Take `runs` of one fill out of the live parts, once it no longer owes them. - #settled(runs: readonly Run[]): void { + // Take `runs` of `pending` out of the live parts, once it no longer owes them. A time start's + // fill was never among them. + #settled(pending: Pending, runs: readonly Run[]): void { + if (pending.exclusive) return; for (const run of runs) cutRun(this.#live, run.start, run.end); } @@ -329,14 +347,9 @@ export class FillState { } } - // Withdraw what no reader reads any more for good, and `pending` once that is all of it. A - // lookup owes no group, so it is withdrawn once nobody waits on it. + // Withdraw what no reader reads any more for good, and `pending` once that is all of it. #judge(pending: Pending): void { if (pending.done) return; - if (pending.time !== undefined) { - if (pending.askers.size === 0) this.#finish(pending); - return; - } const owed = pending.owed(); const withdrawn: Run[] = []; for (const gap of pending.unresolved()) { @@ -345,7 +358,7 @@ export class FillState { withdrawn.push(run); } } - this.#settled(withdrawn); + this.#settled(pending, withdrawn); if (owed.length > 0) { pending.bump(); return; @@ -358,7 +371,7 @@ export class FillState { const unresolved = pending.unresolved(start, end); if (unresolved.length === 0) return; for (const run of unresolved) insertRun(pending.resolved, run.start, run.end); - this.#settled(unresolved); + this.#settled(pending, unresolved); if (pending.unresolved().length === 0) this.#finish(pending); else pending.bump(); } @@ -373,9 +386,11 @@ export class FillState { // Forget `pending`: it owes nothing more. #finish(pending: Pending): void { if (pending.done) return; - this.#settled(pending.unresolved()); + this.#settled(pending, pending.unresolved()); pending.done = true; this.#pending.delete(pending); + const starts = pending.exclusive ? this.#starts.get(pending.start) : undefined; + if (starts?.delete(pending) && starts.size === 0) this.#starts.delete(pending.start); if (pending.queued) this.#queue.splice(this.#queue.indexOf(pending), 1); pending.bump(); } @@ -390,18 +405,20 @@ export class FillState { static { fillOps = { accept: (state, pending, key, info) => { - if (key < pending.start || key >= pending.end) { - throw new RangeError(`group ${key} is outside the fill ${pending.start}..${pending.end}`); + if (key < pending.floor || key >= pending.end) { + throw new RangeError(`group ${key} is outside the fill ${pending.floor}..${pending.end}`); } - if (pending.resolved.some((run) => run.start <= key && key < run.end)) return undefined; - // A copy missing its head is replaced from frame 0, as a bounded reader asked. - if (state.#track.holds(key) === true) return undefined; - // Landing in the cache answers it, as `arrived` records. - const group = state.#track.accept(key, info); - // A lookup waits only on its first answer. - if (pending.time !== undefined && pending.answer === undefined && !pending.done) { + // A key below the start answers the start, so no other group starts from it through the + // start. + const answers = key < pending.start ? pending.start : key; + if (pending.resolved.some((run) => run.start <= answers && answers < run.end)) return undefined; + // A copy missing its head is replaced from frame 0, as a bounded reader asked. Landing in + // the cache answers it, as `arrived` records. + const group = state.#track.holds(key) === true ? undefined : state.#track.accept(key, info); + // A time start's fill is no live part, and a group below its key lands nowhere it owes. + if (pending.exclusive && !pending.done) { pending.answer = key; - state.#finish(pending); + state.#resolve(pending, pending.start, pending.end); } return group; }, @@ -449,12 +466,12 @@ export interface Judged { } /** - * A reader's time lookup, from {@link FillState.lookup}. + * A reader's time start, from {@link FillState.timeStart}. * * @internal */ export interface Lookup { - /** The first group its handler accepted, undefined until one is. */ + /** The group its handler accepted, undefined until one is. */ answer(): bigint | undefined; /** Whether it is over: answered, or closed unanswered. */ done(): boolean; @@ -485,7 +502,6 @@ export let rejectGroups: ( // Constructs the public handles from within this module; assigned in their static blocks. let makeGroups: (state: FillState, pending: Pending) => FillGroups; -let makeLookup: (state: FillState, pending: Pending, time: bigint) => FillLookup; let makeRequests: (state: FillState) => Requests; // The most urgent delivery priority among the readers waiting on `pending` now. @@ -517,10 +533,10 @@ export function requestsOf(state: FillState): Requests { * * Most tracks never serve old content, so this lives on its own handle rather than the producer: * a publisher backed by storage opens one to serve groups its live cache no longer holds, or - * never held. While at least one is open, a reader missing a run of groups queues a {@link Fill} - * here; with none open, the miss is given up. {@link close} it once done, and keep taking fills - * until then: a reader waits on its {@link FillLookup} until it is answered or closed, so a - * handle left open but unread stalls that reader. + * never held. While at least one is open, a reader missing a run of groups queues a + * {@link FillGroups} here; with none open, the miss is given up. {@link close} it once done, and keep + * taking fills until then: a reader waits on its fill until it is answered or closed, so a handle + * left open but unread stalls that reader. * * @public */ @@ -544,14 +560,14 @@ export class Requests { } /** - * Wait until readers miss a run of groups or a time, returning the {@link Fill} to serve: - * a {@link FillGroups} or a {@link FillLookup}, told apart by `kind`. Resolves `undefined` - * once the track closes with nothing queued, or this handle closes, even while it waits. + * Wait until readers miss a run of groups, returning the {@link FillGroups} to serve, including + * the group containing a reader's time start that no cached group places (see + * {@link FillGroups.floor}). Resolves `undefined` once the track closes with nothing queued, or + * this handle closes, even while it waits. */ - async requestedFill(): Promise { + async requestedFill(): Promise { const pending = await this.#state.next(this.#closed); if (!pending) return undefined; - if (pending.time !== undefined) return makeLookup(this.#state, pending, pending.time); return makeGroups(this.#state, pending); } @@ -570,18 +586,11 @@ export class Requests { } } -/** - * What a track's readers miss, from {@link Requests.requestedFill}: a {@link FillGroups} run of - * groups to serve, or a {@link FillLookup} of the group containing a time. Mirrors the Rust - * `track::Fill`; tell them apart by `kind`. - * - * @public - */ -export type Fill = FillGroups | FillLookup; - /** * A run of groups readers miss, from {@link Requests.requestedFill}: `start` up to the exclusive - * `end`. Mirrors the Rust `track::FillGroups`. + * `end`. Mirrors the Rust `track::FillGroups`. A reader's time start that no cached group places + * asks for the group containing that time as a fill one key wide, from the time, whose first group + * may start below it: see {@link floor}. * * Its {@link priority}, {@link maxDelay}, and {@link order} follow its readers as they change, so * read them as each group is served. Call {@link accept} for each group the source has, writing @@ -596,12 +605,20 @@ export type Fill = FillGroups | FillLookup; * @public */ export class FillGroups { - /** Tells it apart from a {@link FillLookup}. */ - readonly kind = "groups"; /** The first group to fill. */ readonly start: bigint; /** The exclusive end group. */ readonly end: bigint; + /** + * The oldest group the first group may start at: {@link start}, unless that is a reader's time + * start no cached group places. Then the first group is the one containing that time: the group + * starting at it, or else the newest one starting before it, at or after the floor. A handler + * knowing its groups only by key serves that key as any other; one that can tell which group + * holds the time, as from an index, may {@link accept} that group instead, or + * {@link dropGroups} the key when none does. The reader starts where the answer says; closed + * unanswered or refused, it starts where the groups the cache holds say. + */ + readonly floor: bigint; #state: FillState; #pending: Pending; @@ -613,6 +630,7 @@ export class FillGroups { this.#pending = pending; this.start = pending.start; this.end = pending.end; + this.floor = pending.floor; } static { @@ -692,9 +710,10 @@ export class FillGroups { /** * Insert group `key` into the track's cache, handing it to the readers waiting on it, * and return the producer to write its frames into. `info` commits the track's {@link Info} - * if it has none yet. A cached copy missing its first frames, which a reader asked whole, is - * replaced. Returns `undefined` if the group was already answered or cached whole. Throws a - * `RangeError` outside the fill, and an error if the track is closed. + * if it has none yet. A key from the {@link floor} up to the start answers the start with that + * group. A cached copy missing its first frames, which a reader asked whole, is replaced. Returns + * `undefined` if the group was already answered or cached whole. Throws a `RangeError` outside + * the fill, and an error if the track is closed. */ accept(start: bigint, info?: Info): GroupProducer | undefined { checkKey(start); @@ -733,72 +752,3 @@ export class FillGroups { this.close(); } } - -/** - * A reader's range starts at a {@link time} no group the cache holds resolves, so it asks the - * source which group contains it: from {@link Requests.requestedFill}. Mirrors the Rust - * `track::FillLookup`. - * - * {@link accept} the group containing the time: 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 it waits only on its first accept. - * Closed unanswered, as a handler that can't resolve times does, the reader's range reaches back - * to the oldest group the cache holds. - * - * @public - */ -export class FillLookup { - /** Tells it apart from a {@link FillGroups}. */ - readonly kind = "lookup"; - /** The time to look up, in the track's timescale (a bare start on a track without one). */ - readonly time: bigint; - - #state: FillState; - #pending: Pending; - - private constructor(state: FillState, pending: Pending, time: bigint) { - this.#state = state; - this.#pending = pending; - this.time = time; - } - - static { - makeLookup = (state, pending, time) => new FillLookup(state, pending, time); - } - - /** The most urgent delivery priority among the readers waiting on it now. */ - // fallow-ignore-next-line unused-class-member - get priority(): number { - return priorityOf(this.#pending); - } - - /** - * The most tolerant age budget among the readers waiting on it now, or `null` for none: the - * publisher's {@link Info.maxAge} alone bounds it. Each reader still enforces its own. - */ - get maxDelay(): Duration | null { - return maxDelayOf(this.#pending); - } - - /** - * Insert the group starting at `start`, the one containing {@link time}, into the track's - * cache, answering the lookup, and return the producer to write its frames into. `info` commits the track's - * {@link Info} if it has none yet. Returns `undefined` if the group is cached whole. Throws a - * `RangeError` at or past the live edge the lookup was asked below, and an error if the track - * is closed. - */ - accept(start: bigint, info?: Info): GroupProducer | undefined { - checkKey(start); - return fillOps.accept(this.#state, this.#pending, start, info); - } - - /** Done serving: unanswered, the reader's range falls back to the groups cached. Idempotent. */ - close(): void { - fillOps.settle(this.#state, this.#pending); - } - - /** Closes the lookup; see {@link close}. */ - [Symbol.dispose](): void { - this.close(); - } -} diff --git a/js/net/src/ietf/subscriber.ts b/js/net/src/ietf/subscriber.ts index 7809b78a22..20835b17b1 100644 --- a/js/net/src/ietf/subscriber.ts +++ b/js/net/src/ietf/subscriber.ts @@ -1159,11 +1159,6 @@ export class Subscriber { for (;;) { const fill = await untilAborted(requests.requestedFill(), signal); if (!fill) break; - // FETCH asks by group alone, so a lookup goes unanswered and its reader falls back. - if (fill.kind === "lookup") { - fill.close(); - continue; - } const run = async () => { try { // The join may already bring a miss in this run. Let it answer first instead diff --git a/js/net/src/lite/copy_fill.test.ts b/js/net/src/lite/copy_fill.test.ts index 8f5f69f0c4..eae8e3ea64 100644 --- a/js/net/src/lite/copy_fill.test.ts +++ b/js/net/src/lite/copy_fill.test.ts @@ -12,7 +12,7 @@ import { defined } from "../defined.test-util.ts"; import { StreamCode, StreamError } from "../error.ts"; import type { Consumer as GroupConsumer } from "../group.ts"; import { hooks } from "../internal.ts"; -import { createMockTransportPair, requestedGroups } from "../mock.ts"; +import { createMockTransportPair } from "../mock.ts"; import { lines } from "../open.ts"; import { Producer as OriginProducer } from "../origin.ts"; import * as Path from "../path.ts"; @@ -41,7 +41,7 @@ async function peers(alpn: string) { const track = defined(broadcast.createTrack("video", { timescale: Timescale.MILLI, maxAge: null })); const requests = track.requests(); void (async () => { - for (let fill = await requestedGroups(requests); fill; fill = await requestedGroups(requests)) { + for (let fill = await requests.requestedFill(); fill; fill = await requests.requestedFill()) { for (let key = fill.wanted(); key !== undefined; key = fill.wanted()) { if (key === 1n) { fill.dropGroups(1n, 2n); @@ -257,8 +257,8 @@ test("copy fill: a filled group stops downloading once every reader left or pass const requests = track.requests(); const fills: FillGroups[] = []; const archived = (async () => { - const fill = await requestedGroups(requests); - if (fill?.kind !== "groups") throw new Error("no fill of groups"); + const fill = await requests.requestedFill(); + if (!fill) throw new Error("no fill of groups"); fills.push(fill); const group = defined(fill.accept(0n)); group.writeFrame({ payload: enc.encode("old 0"), timestamp: Timestamp.fromMillis(0) }); @@ -324,8 +324,8 @@ test("copy fill: a filled group nobody holds any more stops downloading", async const requests = track.requests(); const fills: FillGroups[] = []; const archived = (async () => { - const fill = await requestedGroups(requests); - if (fill?.kind !== "groups") throw new Error("no fill of groups"); + const fill = await requests.requestedFill(); + if (!fill) throw new Error("no fill of groups"); fills.push(fill); const group = defined(fill.accept(0n)); group.writeFrame({ payload: enc.encode("old 0"), timestamp: Timestamp.fromMillis(0) }); @@ -396,8 +396,8 @@ test("copy fill: a group reset to make room ends", async () => { const requests = track.requests(); const fills: FillGroups[] = []; const archived = (async () => { - const fill = await requestedGroups(requests); - if (fill?.kind !== "groups") throw new Error("no fill of groups"); + const fill = await requests.requestedFill(); + if (!fill) throw new Error("no fill of groups"); fills.push(fill); const group = defined(fill.accept(0n)); group.writeFrame({ payload: enc.encode("old 0"), timestamp: Timestamp.fromMillis(0) }); @@ -498,7 +498,7 @@ test.each([Lite.ALPN_05, Lite.ALPN_06, Lite.ALPN_07_WIP])( let archived = false; const requests = track.requests(); void (async () => { - for (let fill = await requestedGroups(requests); fill; fill = await requestedGroups(requests)) { + for (let fill = await requests.requestedFill(); fill; fill = await requests.requestedFill()) { for (let key = fill.wanted(); key !== undefined; key = fill.wanted()) { if (key !== 2n || !archived) { fill.refuseGroups(key, key + 1n, new StreamError(StreamCode.NotFound)); diff --git a/js/net/src/lite/fills.test.ts b/js/net/src/lite/fills.test.ts index 3f947721c5..12db91b028 100644 --- a/js/net/src/lite/fills.test.ts +++ b/js/net/src/lite/fills.test.ts @@ -7,7 +7,6 @@ import { Producer as BroadcastProducer } from "../broadcast.ts"; import { defined } from "../defined.test-util.ts"; import { StreamCode, StreamError } from "../error.ts"; import { hooks, type Run } from "../internal.ts"; -import { requestedGroups } from "../mock.ts"; import { Tail } from "../tail.ts"; import { Timestamp } from "../time.ts"; import type { FillGroups, Producer } from "../track.ts"; @@ -30,8 +29,8 @@ async function missing(): Promise<{ broadcast: BroadcastProducer; track: Produce .ordered() .nextGroup() .catch(() => undefined); - const fill = await requestedGroups(requests); - if (fill?.kind !== "groups") throw new Error("no fill of groups"); + const fill = await requests.requestedFill(); + if (!fill) throw new Error("no fill of groups"); expect([fill.start, fill.end]).toEqual([0n, 4n]); return { broadcast, @@ -298,7 +297,7 @@ test("range fills: a range start past the asked groups refuses only the fills ow .ordered() .nextGroup() .catch(() => undefined); - const again = await requestedGroups(requests); + const again = await requests.requestedFill(); expect(again?.wanted()).toBe(0n); again?.close(); @@ -359,8 +358,8 @@ test("range fills: a passing refusal ends with the fills it answered", async () .nextGroup() .then(() => reading.nextGroup()) .catch(() => undefined); - const again = await requestedGroups(requests); - if (again?.kind !== "groups") throw new Error("no fill of groups"); + const again = await requests.requestedFill(); + if (!again) throw new Error("no fill of groups"); fills.push(again); fills.poll(tail, true); expect(again.wanted()).toBe(1n); @@ -435,8 +434,8 @@ test("range fills: a recovery's refusal is taken once", async () => { .nextGroup() .then(() => reading.nextGroup()) .catch(() => undefined); - const again = await requestedGroups(requests); - if (again?.kind !== "groups") throw new Error("no fill of groups"); + const again = await requests.requestedFill(); + if (!again) throw new Error("no fill of groups"); fills.push(again); fills.poll(tail, true); expect(again.wanted()).toBe(1n); @@ -487,8 +486,8 @@ test("range fills: an older recovery ending keeps a newer one's coverage", async .nextGroup() .then(() => reading.nextGroup()) .catch(() => undefined); - const again = await requestedGroups(requests); - if (again?.kind !== "groups") throw new Error("no fill of groups"); + const again = await requests.requestedFill(); + if (!again) throw new Error("no fill of groups"); fills.push(again); fills.poll(tail, true); expect(opened.map((entry) => entry.groups)).toEqual([ diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index 20f498e71c..74e9e3ae29 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -2088,11 +2088,6 @@ export class Subscriber { const maxAge = (await producer.info().catch(() => undefined))?.maxAge; const framing = { compress: entry.compress, timescale: entry.timescale, maxAge }; for (let fill = await requests.requestedFill(); fill; fill = await requests.requestedFill()) { - // The wire fetches by group alone, so a lookup goes unanswered and its reader falls back. - if (fill.kind === "lookup") { - fill.close(); - continue; - } // One group is what FETCH carries; more go through the subscription. if (fills && fill.end - fill.start > 1n) { fills.push(fill); diff --git a/js/net/src/mock.ts b/js/net/src/mock.ts index 5071711c0b..f92a7fe3c6 100644 --- a/js/net/src/mock.ts +++ b/js/net/src/mock.ts @@ -9,22 +9,10 @@ import { Manual } from "@moq/clock/test"; import type { Getter } from "@moq/signals"; import { Consumer as BroadcastConsumer } from "./broadcast.ts"; import type { TransportStats } from "./connection/stats.ts"; -import type { Fill, FillGroups, Requests } from "./fill.ts"; import type { Frame } from "./group.ts"; import type { SendStream } from "./stream.ts"; import { Timestamp } from "./time.ts"; -/** `fill` as a run of groups, for a test that expects no lookup: a lookup throws. */ -export function groupsOf(fill: Fill | undefined): FillGroups | undefined { - if (fill?.kind === "lookup") throw new Error("expected a fill of groups, got a lookup"); - return fill; -} - -/** The next fill `requests` hands out, for a test that expects only runs of groups. */ -export async function requestedGroups(requests: Requests): Promise { - return groupsOf(await requests.requestedFill()); -} - /** A UTF-8 text frame stamped `timestamp` (now by default), for a test writing to a timed track. */ export function textFrame(text: string, timestamp = Timestamp.now()): Frame { return { payload: new TextEncoder().encode(text), timestamp }; diff --git a/js/net/src/resume.test.ts b/js/net/src/resume.test.ts index 85c11e2537..9c2512c546 100644 --- a/js/net/src/resume.test.ts +++ b/js/net/src/resume.test.ts @@ -552,8 +552,9 @@ test("a backfill waits on a fill the finished route still owes", async () => { const fills: track.FillGroups[] = []; void (async () => { for (let fill = await requests.requestedFill(); fill; fill = await requests.requestedFill()) { - // The replacement asks from the reader's newest group, which this route never held. - if (fill.kind === "lookup") fill.close(); + // The replacement asks for the group containing the reader's newest group's start, which + // this route never held. + if (fill.floor < fill.start) fill.close(); else fills.push(fill); } })(); diff --git a/js/net/src/track.ts b/js/net/src/track.ts index a3af08b498..1e89ae8653 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -60,7 +60,7 @@ import { checkName } from "./util/utf8.ts"; import { bounded, type Holdings, joinsLive, rangesOf, UNHELD, Window, wireSpan } from "./window.ts"; import { type Broadcast as BroadcastWire, registerTrackConsumer } from "./wire.ts"; -export { type Fill, FillGroups, FillLookup, Requests } from "./fill.ts"; +export { FillGroups, Requests } from "./fill.ts"; export { rescale, rescaleIn } from "./rescale.ts"; // The group a non-blocking read takes, skipping drops and throwing the track's error; @@ -317,9 +317,9 @@ export interface Groups { * reaches back to the oldest as a start, and ends before them all as an end. * * A time start resolves from the starts of the groups the serving hop holds. When none starts at - * or before it, a hop whose fill handler serves its history looks the start up there (see - * {@link FillLookup}), reading nothing until it answers; otherwise, or when the handler can't - * tell, it reaches back to the oldest group. While no group is held at all it waits for one, + * or before it, a hop whose fill handler serves its history asks it for the group containing the + * time (see {@link FillGroups.floor}), reading nothing until it answers; otherwise, or when the + * handler can't tell, it reaches back to the oldest group. While no group is held at all it waits for one, * reading only the groups from its time meanwhile. * * Older moq-lite versions carry only time anchors, as group IDs, the live start, and the oldest @@ -1811,7 +1811,7 @@ export class Producer { * never need one; a publisher backed by storage opens one to serve its history. Mirrors the * Rust `Producer::requests`. * - * While one is open, each run of misses queues a {@link Fill}, which readers missing the same + * While one is open, each run of misses queues a {@link FillGroups}, which readers missing the same * groups share; with none open, a miss is given up and {@link Consumer.fetchGroup} of an * uncached group fails with `NotFound`. A reader only asks for what its budget still wants, * so a reader at the live edge never asks. The track stays one track: answered groups land @@ -3783,8 +3783,9 @@ export class Subscriber { return undefined; } - // Look up each time start the window can't resolve from the cache, once, and take in the - // answers. Returns whether the groups in range moved. Mirrors the Rust `Cursor::look_up`. + // Ask a fill for the group containing each time start the window can't resolve from the cache, + // once, and take in the answers. Returns whether the groups in range moved. Mirrors the Rust + // `Cursor::look_up`. #lookUp(): boolean { const wanted = this.#window.lookups(); if (wanted.length === 0 && this.#lookups.length === 0) return false; @@ -3795,10 +3796,10 @@ export class Subscriber { lookup.lookup?.leave(); return false; }); - for (const { time, below } of wanted) { + for (const { time, floor } of wanted) { if (this.#lookups.some((lookup) => lookup.time === time)) continue; // With no handler open any more, nothing can answer. - const lookup = producer && fillsOf(producer).lookup(time, below, this.#askerOf()); + const lookup = producer && fillsOf(producer).timeStart(time, floor, this.#askerOf()); this.#lookups.push({ time, lookup }); } let moved = false; diff --git a/js/net/src/window.test.ts b/js/net/src/window.test.ts index 131e5207e6..cde6296577 100644 --- a/js/net/src/window.test.ts +++ b/js/net/src/window.test.ts @@ -300,7 +300,7 @@ describe("Window", () => { const held = new Keys([4000n, 9000n]); held.looks = true; let window = new Window([range(time(8500n))], 9000n, held); - expect(window.lookups()).toEqual([{ time: 8500n, below: 9000n }]); + expect(window.lookups()).toEqual([{ time: 8500n, floor: 4001n }]); expect([window.settled, window.contains(9000n), window.resolved()]).toEqual([false, false, [undefined]]); held.held.add(8000n); expect(window.found(8500n, 8000n, held)).toBe(true); @@ -313,7 +313,7 @@ describe("Window", () => { expect(new Window([range(time(8000n))], 9000n, held).lookups()).toHaveLength(0); window = new Window([range(time(500n))], 9000n, held); - expect(window.lookups()).toEqual([{ time: 500n, below: 4000n }]); + expect(window.lookups()).toEqual([{ time: 500n, floor: 0n }]); expect(window.found(500n, undefined, held)).toBe(true); expect(window.resolved()).toEqual([{ start: 0n, end: undefined, floored: false }]); expect(window.contains(4000n)).toBe(true); @@ -347,7 +347,7 @@ describe("Window", () => { const held = new Keys([4000n, 9000n]); held.looks = true; const window = new Window([range(time(500n), time(3000n))], 9000n, held); - expect(window.lookups()).toEqual([{ time: 500n, below: 4000n }]); + expect(window.lookups()).toEqual([{ time: 500n, floor: 0n }]); expect(window.found(500n, undefined, held)).toBe(true); expect(window.settled).toBe(true); expect(window.resolved()).toEqual([{ start: 0n, end: 3001n, floored: false }]); diff --git a/js/net/src/window.ts b/js/net/src/window.ts index f99e24ac20..8ab88437ae 100644 --- a/js/net/src/window.ts +++ b/js/net/src/window.ts @@ -404,8 +404,8 @@ type Span = { /** Whether the start names a group, rather than reaching back to the oldest one. */ floored: boolean; /** - * A time start the groups held can't place, looked up from the track's history: the span reads - * nothing until the lookup answers. + * A time start the groups held can't place, whose group a fill asks the track's history for: the + * span reads nothing until it answers. */ looking?: Lookup; /** A time start the groups held can't place yet: the span reads only the groups from that time until it can. */ @@ -421,13 +421,12 @@ type Span = { }; /** - * A time start being looked up: its time, and the exclusive end of the run below every group - * held, which holds the group containing it if the track's history does. Mirrors the Rust - * `Looking`. + * A time start being looked up: its time, and the oldest key the group containing it may start at, + * past every group held below it. Mirrors the Rust `Looking`. */ export interface Lookup { time: bigint; - below: bigint; + floor: bigint; } /** @@ -643,8 +642,8 @@ export class Window { span.start = start; span.floored = floored; span.waiting = false; - const below = asked ? unresolved(at, start, floored, holdings) : undefined; - if (below !== undefined) span.looking = { time: at, below }; + const floor = asked ? unresolved(at, start, floored, holdings) : undefined; + if (floor !== undefined) span.looking = { time: at, floor }; this.#count(key, span); } @@ -743,11 +742,10 @@ export class Window { } /** - * The lookup of time start `time` answered with `group`, the group containing it or the oldest - * group of the history when `time` is older than all of it: its spans start there, once that - * group is held. Undefined, or an answer not held, means the history couldn't tell, so they - * start where the groups held say. Returns whether the groups in range moved. Mirrors the Rust - * `Window::found`. + * The lookup of time start `time` answered with `group`, the group containing it: its spans + * start there, once that group is held. Undefined, or an answer not held, means the history + * couldn't tell, or holds no group there, so they start where the groups held say. Returns + * whether the groups in range moved. Mirrors the Rust `Window::found`. */ found(at: bigint, group: bigint | undefined, holdings: Holdings = UNHELD): boolean { const found = this.#looking.take(at); @@ -848,8 +846,8 @@ export class Window { // reach back never looks one up: it starts from what this hop holds. const at = time(anchor); const asked = time(range.start) !== undefined && at === time(range.start); - const below = at === undefined || !asked ? undefined : unresolved(at, start.group, start.floored, holdings); - const looking = at !== undefined && below !== undefined ? { time: at, below } : undefined; + const floor = at === undefined || !asked ? undefined : unresolved(at, start.group, start.floored, holdings); + const looking = at !== undefined && floor !== undefined ? { time: at, floor } : undefined; const counted = !start.floored && relative(anchor) !== undefined; this.#count(key, { anchor, @@ -1050,19 +1048,20 @@ function startOf( } /** - * The exclusive end of the run a lookup of time start `at`, resolved to `start`, searches, when - * the groups held can't tell which group contains it and a handler may: it is before every group - * held, or a group the cache evicted may start after `start`, the newest held at or before it, and - * still at or before `at`. Starts the producer skipped hold none. Mirrors the Rust `unresolved`. + * The oldest key a fill of the group containing time start `at`, resolved to `start`, asks from, + * when the groups held can't tell which group contains it and a handler may: it is before every + * group held, or a group the cache evicted may start after `start`, the newest held at or before + * it, and still at or before `at`. Starts the producer skipped hold none. Mirrors the Rust + * `unresolved`. */ function unresolved(at: bigint, start: bigint, floored: boolean, holdings: Holdings): bigint | undefined { if (!holdings.looksUp?.()) return undefined; - if (!floored) return holdings.nextStart(0n); + if (!floored) return holdings.nextStart(0n) === undefined ? undefined : 0n; const next = start + 1n; const after = holdings.nextStart(next); if (after === undefined) return undefined; const gap = min(after, at + 1n); - return holdings.neverProduced?.(next, gap) || next >= gap ? undefined : after; + return holdings.neverProduced?.(next, gap) || next >= gap ? undefined : next; } /** From 58d11d24050baf212b11941936246232e2952c34 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 20 Nov 2026 00:01:04 -0800 Subject: [PATCH 05/14] test(net): a time start reaches a history handler as a group fill A real lite-07 session: a time-aware handler answers with the group containing the time, and a key-by-key handler serves the key. The interop clients no longer skip lookups, and the JS API report drops Fill and FillLookup. Co-Authored-By: Claude Opus 5.5 --- js/net/api/index.api.md | 14 +- js/net/src/fill.ts | 3 +- rs/moq-net/src/model/track.rs | 2 +- rs/moq-net/tests/integration/main.rs | 1 + .../tests/integration/time_start_fill.rs | 154 ++++++++++++++++++ test/interop/clients/js-native/replace.ts | 5 +- test/interop/clients/js-native/tail.ts | 4 - 7 files changed, 162 insertions(+), 21 deletions(-) create mode 100644 rs/moq-net/tests/integration/time_start_fill.rs diff --git a/js/net/api/index.api.md b/js/net/api/index.api.md index 096ceaada0..e393453a20 100644 --- a/js/net/api/index.api.md +++ b/js/net/api/index.api.md @@ -695,15 +695,14 @@ export namespace Track { priority?: number; signal?: AbortSignal; } - export type Fill = Track.FillGroups | Track.FillLookup; export class FillGroups { [Symbol.dispose](): void; accept(start: bigint, info?: Track.Info): Group.Producer | undefined; close(): void; dropGroups(start: bigint, end: bigint): void; readonly end: bigint; + readonly floor: bigint; isWanted(key: bigint): boolean; - readonly kind: "groups"; get maxDelay(): Clock.Duration | null; get order(): Track.Order; get priority(): number; @@ -713,15 +712,6 @@ export namespace Track { wanted(): bigint | undefined; wantedChanged(): Promise; } - export class FillLookup { - [Symbol.dispose](): void; - accept(start: bigint, info?: Track.Info): Group.Producer | undefined; - close(): void; - readonly kind: "lookup"; - get maxDelay(): Clock.Duration | null; - get priority(): number; - readonly time: bigint; - } export interface Finished { abort(error: Error): void; close(): void; @@ -826,7 +816,7 @@ export namespace Track { [Symbol.dispose](): void; close(): void; get name(): string; - requestedFill(): Promise; + requestedFill(): Promise; } export class Subscriber { close(abort?: Error): void; diff --git a/js/net/src/fill.ts b/js/net/src/fill.ts index b6dd2939f1..316e45a6d8 100644 --- a/js/net/src/fill.ts +++ b/js/net/src/fill.ts @@ -410,7 +410,8 @@ export class FillState { } // A key below the start answers the start, so no other group starts from it through the // start. - const answers = key < pending.start ? pending.start : key; + let answers = key; + if (answers < pending.start) answers = pending.start; if (pending.resolved.some((run) => run.start <= answers && answers < run.end)) return undefined; // A copy missing its head is replaced from frame 0, as a bounded reader asked. Landing in // the cache answers it, as `arrived` records. diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 96d4d295ec..17cccdb8a4 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -4074,7 +4074,7 @@ impl TrackState { } impl TrackState { - /// Store a flight a [`Fill`] answered: a copy of group `key` of its own, from the + /// Store a flight a [`FillGroups`] answered: a copy of group `key` of its own, from the /// frame its riders start at, which the cache never serves in place of its cached copy. pub(super) fn insert_flight( &mut self, diff --git a/rs/moq-net/tests/integration/main.rs b/rs/moq-net/tests/integration/main.rs index 67fccffef8..c8de57db0a 100644 --- a/rs/moq-net/tests/integration/main.rs +++ b/rs/moq-net/tests/integration/main.rs @@ -96,6 +96,7 @@ mod subscription_end_integrity; #[cfg(feature = "async")] mod support; mod synced; +mod time_start_fill; mod track_abort_over_session; mod track_info_cancel; mod track_stream_demand; diff --git a/rs/moq-net/tests/integration/time_start_fill.rs b/rs/moq-net/tests/integration/time_start_fill.rs new file mode 100644 index 0000000000..dfcd0c924d --- /dev/null +++ b/rs/moq-net/tests/integration/time_start_fill.rs @@ -0,0 +1,154 @@ +#![cfg(feature = "async")] + +//! A lite-07 reader's time start that the publisher's cache can't place reaches the +//! publisher's history handler as an ordinary group fill, at the start's key with a floor +//! below it. A handler that knows its groups' times answers with the group containing the +//! time; one serving key by key serves the key, which is that group when one starts there. + +use crate::support; + +use std::collections::BTreeSet; +use std::time::Duration; + +use moq_net::track::{Anchor, Delay, FillGroups, Info, Order, Range, Requests, Subscription}; +use moq_net::{Timescale, Timestamp, Version}; +use support::harness::peer; + +const TIMEOUT: Duration = Duration::from_secs(10); + +/// A budget no group outlives, so only the range decides what is read. +const FOREVER: Duration = Duration::from_millis((1 << 53) - 1); + +/// The groups only the handler holds, each keyed by its start in milliseconds. +const HISTORY: [u64; 4] = [0, 1000, 2000, 3000]; + +fn produce_origin(hop: u64) -> moq_net::origin::Producer { + let (producer, driver) = + moq_net::origin::Producer::new(moq_net::origin::Config::new(std::num::NonZeroU64::new(hop).unwrap())); + support::harness::spawn(driver); + producer.with_proxy(true) +} + +fn info() -> Info { + Info::default().with_timescale(Timescale::MILLI).with_max_age(FOREVER) +} + +fn write(mut group: moq_net::group::Producer, key: u64) { + group + .write_frame(Timestamp::from_millis(key).unwrap(), &b"x"[..]) + .unwrap(); + group.finish().unwrap(); +} + +/// What the handler saw: each fill's start key and floor. +type Seen = std::sync::Arc>>; + +/// Serve [`HISTORY`] for every fill, answering a time start with the newest group at or +/// below it when `by_time`, and key by key otherwise. +async fn serve(requests: Requests, by_time: bool, seen: Seen) { + while let Ok(fill) = requests.requested_fill().await { + seen.lock().unwrap().push((fill.start().group, fill.floor())); + drop(kio::clock::sim::spawn(serve_fill(fill, by_time))); + } +} + +async fn serve_fill(mut fill: FillGroups, by_time: bool) { + let held: BTreeSet = HISTORY.into(); + while let Some(key) = fill.wanted_changed().await { + let start = fill.start().group; + let found = match by_time && key == start && fill.floor() < start { + true => held.range(fill.floor()..=key).next_back().copied(), + false => held.contains(&key).then_some(key), + }; + match found { + Some(found) => { + if let Some(group) = fill.accept(found, None).unwrap() { + write(group, found); + } + } + None => { + // Nothing in the history at this key: drop it, up to the next held group. + let next = held.range(key + 1..).next().copied().unwrap_or(fill.end()); + fill.drop_groups(key..next.min(fill.end())); + } + } + } +} + +/// A publisher whose cache holds 4000 and 5000 with [`HISTORY`] behind a handler, then what a +/// lite-07 subscriber to `range` reads, and the fills the handler saw. +async fn read(range: Range, by_time: bool) -> (BTreeSet, Vec<(u64, u64)>) { + let publisher = produce_origin(1); + let broadcast = publisher + .route("room", moq_net::origin::RouteOptions::hidden()) + .unwrap(); + let mut track = broadcast.broadcast().create_track("data", info()).unwrap(); + let seen = Seen::default(); + let handler = kio::clock::sim::spawn(serve(track.requests(), by_time, seen.clone())); + broadcast.announce().unwrap(); + track.start_at(4000).unwrap(); + for key in [4000, 5000] { + write(track.create_group(moq_net::group::Info { start: key }).unwrap(), key); + } + + let subscriber = produce_origin(2); + let _pair = peer("moq-lite-07-wip".parse::().unwrap(), &publisher, &subscriber).await; + let remote = kio::clock::timeout(TIMEOUT, subscriber.consume().follow("room", None).unwrap().available()) + .await + .expect("resolve timeout") + .expect("broadcast resolves") + .unwrap(); + + let subscription = Subscription::default() + .with_delay(Delay::default().with_max(FOREVER)) + .with_order(Order::Ascending) + .with_ranges([range]); + let mut reader = kio::clock::timeout(TIMEOUT, remote.track("data").unwrap().subscribe(subscription)) + .await + .expect("subscribe timeout") + .unwrap(); + + let mut read = BTreeSet::new(); + while let Ok(group) = kio::clock::timeout(TIMEOUT, reader.recv_group()).await { + match group.unwrap() { + Some(group) => assert!(read.insert(group.start), "read once"), + None => break, + } + } + drop(handler); + let seen = seen.lock().unwrap().clone(); + (read, seen) +} + +/// A handler that knows its groups' times answers a time start between them with the group +/// containing it, below the start's key. +#[kio::test] +async fn a_time_start_is_answered_by_the_group_containing_it() { + let range = Range { + start: Anchor::Time(2500), + end: Some(Anchor::Time(4500)), + }; + let (read, seen) = read(range, true).await; + assert_eq!(read, [2000, 3000, 4000].into()); + assert_eq!( + seen.first(), + Some(&(2500, 0)), + "the start reaches the handler as key 2500" + ); +} + +/// A handler serving key by key serves a time start at a group's own key as that group. +#[kio::test] +async fn a_time_start_at_a_key_is_served_key_by_key() { + let range = Range { + start: Anchor::Time(2000), + end: Some(Anchor::Time(4500)), + }; + let (read, seen) = read(range, false).await; + assert_eq!(read, [2000, 3000, 4000].into()); + assert_eq!( + seen.first(), + Some(&(2000, 0)), + "the start reaches the handler as key 2000" + ); +} diff --git a/test/interop/clients/js-native/replace.ts b/test/interop/clients/js-native/replace.ts index 6f0952838d..4b6a40362b 100644 --- a/test/interop/clients/js-native/replace.ts +++ b/test/interop/clients/js-native/replace.ts @@ -107,13 +107,12 @@ try { write(sequence); const future = sequence + 1n; // Serve every request until the round ends, not only the fill asserted here: a relay - // moving its range below the replacement's first group asks where that time starts, - // and an unanswered lookup holds back the subscription that asked. + // moving its range below the replacement's first group asks for the group containing + // that time, and an unanswered fill holds back the subscription that asked. const filled = Promise.withResolvers(); const serving = (async () => { for (let fill = await requests.requestedFill(); fill; fill = await requests.requestedFill()) { try { - if (fill.kind === "lookup") continue; // closed unanswered: no time lookups here if (fill.start < sequence) continue; // history the replacement no longer holds assert.equal(fill.start, future); assert.equal(fill.end, future + 1n); diff --git a/test/interop/clients/js-native/tail.ts b/test/interop/clients/js-native/tail.ts index 5b399aebed..c5cebd43b5 100644 --- a/test/interop/clients/js-native/tail.ts +++ b/test/interop/clients/js-native/tail.ts @@ -96,10 +96,6 @@ function serveHistory(track: Moq.Track.Producer) { const requests = track.requests(); void (async () => { for (let fill = await requests.requestedFill(); fill; fill = await requests.requestedFill()) { - if (fill.kind === "lookup") { - fill.close(); - continue; - } for (let sequence = fill.wanted(); sequence !== undefined; sequence = fill.wanted()) { const group = fill.accept(sequence); if (!group) throw new Error("the group was already answered"); From a4fe498bc764688207db8621fe946613e3ff5b58 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 20 Nov 2026 00:29:37 -0800 Subject: [PATCH 06/14] fix(net): an eviction during a time start's lookup asks it again from below A time start's floor is past the newest group the cache held below the time. If the cache evicts that group while the fill is out, the handler's answer falls below the floor, and an empty answer used to start the reader where the groups still held say, silently skipping the group containing the time. The window now looks the time up again from the lower floor; each retry needs another eviction, so it ends. Co-Authored-By: Claude Opus 5.5 --- js/net/src/fill.test.ts | 44 ++++++++++++ js/net/src/track.ts | 30 ++++++--- js/net/src/window.test.ts | 21 ++++++ js/net/src/window.ts | 10 ++- rs/moq-net/src/model/subscription/window.rs | 49 ++++++++++++-- rs/moq-net/src/model/track.rs | 75 +++++++++++++++++---- 6 files changed, 200 insertions(+), 29 deletions(-) diff --git a/js/net/src/fill.test.ts b/js/net/src/fill.test.ts index a4f9e368ad..598f7a037c 100644 --- a/js/net/src/fill.test.ts +++ b/js/net/src/fill.test.ts @@ -1673,6 +1673,50 @@ test("fill: a lookup answered below the eviction watermark starts the range ther } }); +test("fill: a time start whose floor's group is evicted while it is out is asked again from below", async () => { + const clock = new Manual(); + const installed = clock.install(); + try { + const broadcast = new BroadcastProducer(); + const track = defined(broadcast.createTrack("video", { timescale: Timescale.MILLI, maxAge: null })); + const requests = track.requests(); + const write = (n: bigint) => { + const group = track.createGroup(n); + group.writeFrame({ payload: enc.encode(`g${n}`), timestamp: Timestamp.fromMillis(Number(n)) }); + group.finish(); + }; + write(1n); + write(9n); + const reader = track.subscribe({ + ranges: [{ start: { time: 7n }, end: { time: 7n } }], + delay: { max: null }, + order: "ascending", + }); + const next = reader.ordered().nextGroup(); + const first = defined(await requests.requestedFill()); + expect([first.start, first.floor]).toEqual([7n, 2n]); + // Idle past the cache window, so the next write evicts every group but the new edge. + clock.jump(Duration.millis(DEFAULT_EXPIRY.asMillis() + 1000)); + write(10n); + // The index says 1 holds the time, below the floor: nothing from 2 does. + first.dropGroups(2n, 8n); + first.close(); + const again = defined(await requests.requestedFill()); + expect([again.start, again.floor]).toEqual([7n, 0n]); + const found = defined(again.accept(1n)); + found.writeFrame({ payload: enc.encode("g1"), timestamp: Timestamp.fromMillis(1) }); + found.finish(); + again.close(); + expect((await next)?.start).toBe(1n); + + reader.close(); + requests.close(); + broadcast.close(); + } finally { + installed.close(); + } +}); + test("fill: a lookup's answer is read at once, before its first frame: its key is its start", async () => { const { broadcast, track } = timed(); const requests = track.requests(); diff --git a/js/net/src/track.ts b/js/net/src/track.ts index 1e89ae8653..1511f7642a 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -3122,9 +3122,9 @@ export class Subscriber { // Bumped whenever the fills this reader joined are rejudged, which may withdraw groups it reads // again by the time it looks (a narrowing undone in one turn moves no signal), so it asks anew. #fillMoves = new Signal(0); - // The time starts this reader asked its fills to look up, each with its fill, absent when no - // handler could take it. - #lookups: { time: bigint; lookup?: Lookup }[] = []; + // The time starts this reader asked its fills to look up, each with the floor it asked from and + // its fill, absent when no handler could take it. + #lookups: { time: bigint; floor: bigint; lookup?: Lookup }[] = []; #drift(whole = this.#fetchBudget !== undefined): Drift { // A fetch judges its group against the whole track, as the Rust `fetch_group` does: a @@ -3787,12 +3787,24 @@ export class Subscriber { // once, and take in the answers. Returns whether the groups in range moved. Mirrors the Rust // `Cursor::look_up`. #lookUp(): boolean { + let moved = false; + // An answer the window takes in may look its time up again from lower down. + for (let again = true; again; ) { + const pass = this.#lookUpPass(); + moved ||= pass.moved; + again = pass.again; + } + return moved; + } + + // One pass of `#lookUp`, which says whether the window looks up a time again. + #lookUpPass(): { moved: boolean; again: boolean } { const wanted = this.#window.lookups(); - if (wanted.length === 0 && this.#lookups.length === 0) return false; + if (wanted.length === 0 && this.#lookups.length === 0) return { moved: false, again: false }; const producer = this.#state.producer; - // The window stops looking once replaced. + // The window stops looking once replaced, or looks again from lower down. this.#lookups = this.#lookups.filter((lookup) => { - if (wanted.some((want) => want.time === lookup.time)) return true; + if (wanted.some((want) => want.time === lookup.time && want.floor === lookup.floor)) return true; lookup.lookup?.leave(); return false; }); @@ -3800,9 +3812,10 @@ export class Subscriber { if (this.#lookups.some((lookup) => lookup.time === time)) continue; // With no handler open any more, nothing can answer. const lookup = producer && fillsOf(producer).timeStart(time, floor, this.#askerOf()); - this.#lookups.push({ time, lookup }); + this.#lookups.push({ time, floor, lookup }); } let moved = false; + let again = false; for (const lookup of [...this.#lookups]) { const answer = lookup.lookup?.answer(); if (answer === undefined && lookup.lookup && !lookup.lookup.done()) continue; @@ -3818,8 +3831,9 @@ export class Subscriber { this.#lookups.splice(this.#lookups.indexOf(lookup), 1); lookup.lookup?.leave(); if (this.#window.found(lookup.time, answer, holdings)) moved = true; + if (this.#window.lookups().some((want) => want.time === lookup.time)) again = true; } - return moved; + return { moved, again }; } // Narrow the local bounds to the window into the hull every read starts from. diff --git a/js/net/src/window.test.ts b/js/net/src/window.test.ts index cde6296577..dfaa134a31 100644 --- a/js/net/src/window.test.ts +++ b/js/net/src/window.test.ts @@ -323,6 +323,27 @@ describe("Window", () => { expect(window.found(500n, undefined, held)).toBe(false); }); + test("an eviction during a lookup looks it up again", () => { + const held = new Keys([4000n, 9000n]); + held.looks = true; + let window = new Window([range(time(8500n))], 9000n, held); + expect(window.lookups()).toEqual([{ time: 8500n, floor: 4001n }]); + held.held.delete(4000n); + expect(window.found(8500n, undefined, held)).toBe(true); + // Looked up from below, reading nothing meanwhile. + expect(window.lookups()).toEqual([{ time: 8500n, floor: 0n }]); + expect(window.contains(9000n)).toBe(false); + held.held.add(4000n); + expect(window.found(8500n, 4000n, held)).toBe(true); + expect(window.resolved()[0]?.start).toBe(4000n); + + // With nothing evicted, an empty answer starts where the groups held say. + window = new Window([range(time(8500n))], 9000n, held); + expect(window.found(8500n, undefined, held)).toBe(true); + expect(window.lookups()).toHaveLength(0); + expect(window.resolved()[0]?.start).toBe(4000n); + }); + test("a skipped gap needs no lookup", () => { const held = new Keys([0n, 33n, 66n]); held.looks = true; diff --git a/js/net/src/window.ts b/js/net/src/window.ts index 8ab88437ae..0de59d1d66 100644 --- a/js/net/src/window.ts +++ b/js/net/src/window.ts @@ -744,8 +744,9 @@ export class Window { /** * The lookup of time start `time` answered with `group`, the group containing it: its spans * start there, once that group is held. Undefined, or an answer not held, means the history - * couldn't tell, or holds no group there, so they start where the groups held say. Returns - * whether the groups in range moved. Mirrors the Rust `Window::found`. + * couldn't tell, or holds no group there, so they start where the groups held say, unless the + * cache since evicted the group their floor was past: then they look it up again from lower + * down. Returns whether the groups in range moved. Mirrors the Rust `Window::found`. */ found(at: bigint, group: bigint | undefined, holdings: Holdings = UNHELD): boolean { const found = this.#looking.take(at); @@ -757,9 +758,14 @@ export class Window { const span = this.#spans.get(key); if (!span) continue; this.#uncount(key, span); + const asked = span.looking?.floor; span.looking = undefined; span.start = start ?? 0n; span.floored = start !== undefined; + if (held === undefined && asked !== undefined) { + const floor = unresolved(at, span.start, span.floored, holdings); + if (floor !== undefined && floor < asked) span.looking = { time: at, floor }; + } this.#count(key, span); } return true; diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index 93658b5841..34d10b01bd 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -604,8 +604,11 @@ impl Window { /// The lookup of time start `time` answered with the group containing it: its spans start /// there, once that group is held. `None`, or an answer not held, means the history - /// couldn't tell, or holds no group there, so they start where the groups held say. - /// Returns whether the groups in range moved. + /// couldn't tell, or holds no group there, so they start where the groups held say, + /// unless the cache since evicted the group their floor was past: then the history may + /// hold the answer below that floor, so they look it up again from lower down. Each + /// look-up again needs another eviction, so it ends. Returns whether the groups in range + /// moved. pub fn found(&mut self, time: u64, group: Option, holdings: &impl Holdings) -> bool { let found: Vec = self .looking @@ -619,11 +622,16 @@ impl Window { for key in found { let mut span = self.spans.remove(&key).expect("a span looking up its start"); self.uncount(key, &span); - span.looking = None; + let asked = span.looking.take().map(|looking| looking.floor); (span.start, span.floored) = match (held, holdings.containing(time)) { (Some(group), _) | (None, Containing::Group(group)) => (group, true), (None, _) => (0, false), }; + if held.is_none() { + span.looking = unresolved(time, span.start, span.floored, holdings) + .filter(|floor| asked.is_some_and(|asked| *floor < asked)) + .map(|floor| Looking { time, floor }); + } self.count(key, span, holdings); } self.covered = None; @@ -1407,9 +1415,10 @@ mod tests { } /// With a handler serving the history, a time start the groups held can't place asks a - /// fill for its group from past the newest held below it, and reads nothing meanwhile: before every start, or with a group the cache evicted - /// possibly starting between the newest held at or before it and the time. The answer - /// places it; none leaves what the groups held say. + /// fill for its group from past the newest held below it, and reads nothing meanwhile: + /// before every start, or with a group the cache evicted possibly starting between the + /// newest held at or before it and the time. 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 keys = Keys::new([4000, 9000]); @@ -1448,6 +1457,34 @@ mod tests { assert!(window.contains(4000) && !window.missable(0).any(|run| *run.start() < 500)); } + /// The group a lookup's floor was past evicted while it was out, an empty answer looks + /// the time up again from below, where the history may hold the group containing it, + /// rather than starting where the groups still held say. + #[test] + fn an_eviction_during_a_lookup_looks_it_up_again() { + let mut keys = Keys::new([4000, 9000]); + keys.looks_up = true; + let mut window = Window::new(&[range(time(8500), None)], None, Some(9000), &keys).unwrap(); + assert_eq!(window.lookups().collect::>(), [(8500, 4001)]); + keys.held.remove(&4000); + assert!(window.found(8500, None, &keys)); + assert_eq!( + window.lookups().collect::>(), + [(8500, 0)], + "looked up from below" + ); + assert!(!window.contains(9000), "reads nothing meanwhile"); + keys.held.insert(4000); + assert!(window.found(8500, Some(4000), &keys)); + assert_eq!(window.resolved()[0].map(|resolved| resolved.start), Some(4000)); + + // With nothing evicted, an empty answer starts where the groups held say. + let mut window = Window::new(&[range(time(8500), None)], None, Some(9000), &keys).unwrap(); + assert!(window.found(8500, None, &keys)); + assert_eq!(window.lookups().count(), 0); + assert_eq!(window.resolved()[0].map(|resolved| resolved.start), Some(4000)); + } + /// Keys the producer skipped hold no group, even in the history, so a start in a skipped /// span between two held groups is placed without a lookup. #[test] diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 17cccdb8a4..f2c0de12b7 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -8677,6 +8677,8 @@ struct Cursor { /// A time start a [`Cursor`]'s window looks up through a fill. struct Lookup { time: u64, + /// The oldest key the fill asks from; a lower one is a new look-up. + floor: u64, /// The fill looking it up, `None` once its answer was taken in. join: Option, } @@ -9715,29 +9717,46 @@ impl Cursor { /// /// Indexed by time, so a pass costs O(L log L) in the lookups out rather than O(L²). fn look_up(&mut self, waiter: &kio::Waiter) { + let mut moved = false; + // An answer the window takes in may look its time up again from lower down. + while self.look_up_pass(waiter, &mut moved) {} + if moved { + self.filter.revision += 1; + self.update_servable(); + } + } + + /// One pass of [`Self::look_up`]: returns whether the window looks up a time again. + fn look_up_pass(&mut self, waiter: &kio::Waiter, moved: &mut bool) -> bool { // The first span looking a time up sets the floor its fill asks from. let mut lookups = BTreeMap::new(); for (time, floor) in self.filter.window.lookups() { lookups.entry(time).or_insert(floor); } if lookups.is_empty() && self.filter.lookups.iter().all(|lookup| lookup.join.is_none()) { - return; + return false; } let asking = &asking(&self.subscription); let fills = self.state.read().fills.clone(); - let asked: BTreeSet = self.filter.lookups.iter().map(|lookup| lookup.time).collect(); + let asked: BTreeSet<(u64, u64)> = self + .filter + .lookups + .iter() + .map(|lookup| (lookup.time, lookup.floor)) + .collect(); for (&time, &floor) in &lookups { - if asked.contains(&time) { + if asked.contains(&(time, floor)) { continue; } let join = fills.lock().time_start(&fills, time, floor, *asking); - self.filter.lookups.push(Lookup { time, join }); + self.filter.lookups.push(Lookup { time, floor, join }); } - let mut moved = false; + let mut again = false; for lookup in &mut self.filter.lookups { - // The window stops looking once replaced; with no join, nothing can answer. An - // answer taken in below stops only the spans looking up that time. - if !lookups.contains_key(&lookup.time) { + // The window stops looking once replaced, or looks again from lower down; with no + // join, nothing can answer. An answer taken in below stops only the spans looking + // up that time. + if lookups.get(&lookup.time) != Some(&lookup.floor) { lookup.join = None; continue; } @@ -9759,12 +9778,10 @@ impl Cursor { }; let state = self.state.read(); lookup.join = None; - moved |= self.filter.window.found(lookup.time, answer, &*state); - } - if moved { - self.filter.revision += 1; - self.update_servable(); + *moved |= self.filter.window.found(lookup.time, answer, &*state); + again |= self.filter.window.lookups().any(|(time, _)| time == lookup.time); } + again } /// Ask fills for what this cursor misses from `from`, and take in what they answered, @@ -26004,6 +26021,38 @@ mod test { write_filled(start.accept(6, None), 6); drop(start); assert_eq!(drain_groups(&mut subscriber), [6]); + + // The group the floor was past evicted while the start was out, the handler's answer + // below the floor is asked again from lower down rather than lost. + let producer = track_producer("test", Info::timed()); + let dynamic = producer.requests(); + for key in 0..10 { + write_group(&producer, key); + } + for key in 5..9 { + evict(&producer, key); + } + let mut subscriber = producer.subscribe(replay().with_ranges([Range { + start: Anchor::Time(7), + end: Some(Anchor::Time(7)), + }])); + assert_eq!(drain_groups(&mut subscriber), Vec::::new()); + let Poll::Ready(Ok(start)) = dynamic.poll_requested_fill(&waiter) else { + panic!("the time start asks for its group"); + }; + assert_eq!(start.floor(), 5); + evict(&producer, 4); + // The index says 4 holds the time, below the floor: nothing from 5 does. + start.drop_groups(5..8); + drop(start); + assert_eq!(drain_groups(&mut subscriber), Vec::::new()); + let Poll::Ready(Ok(start)) = dynamic.poll_requested_fill(&waiter) else { + panic!("the time start asks again from below"); + }; + assert_eq!((start.start().group, start.floor()), (7, 4)); + write_filled(start.accept(4, None), 4); + drop(start); + assert_eq!(drain_groups(&mut subscriber), [4]); } /// A time end walking the history a handler serves takes no group the live feed From 5b41a19d2437a4ed44dba1e084815cf6c8e5bf96 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 20 Nov 2026 00:44:08 -0800 Subject: [PATCH 07/14] fix(net): a lookup retried after an eviction asks from 0 once the cache empties Co-Authored-By: Claude Opus 5.5 --- js/net/src/window.test.ts | 8 ++++++++ js/net/src/window.ts | 8 +++++++- rs/moq-net/src/model/subscription/window.rs | 19 ++++++++++++++++++- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/js/net/src/window.test.ts b/js/net/src/window.test.ts index dfaa134a31..6d3ab832d2 100644 --- a/js/net/src/window.test.ts +++ b/js/net/src/window.test.ts @@ -337,6 +337,14 @@ describe("Window", () => { expect(window.found(8500n, 4000n, held)).toBe(true); expect(window.resolved()[0]?.start).toBe(4000n); + // Even with the cache emptied. + window = new Window([range(time(8500n))], 9000n, held); + const all = held.held; + held.held = new Set(); + expect(window.found(8500n, undefined, held)).toBe(true); + expect(window.lookups()).toEqual([{ time: 8500n, floor: 0n }]); + held.held = all; + // With nothing evicted, an empty answer starts where the groups held say. window = new Window([range(time(8500n))], 9000n, held); expect(window.found(8500n, undefined, held)).toBe(true); diff --git a/js/net/src/window.ts b/js/net/src/window.ts index 0de59d1d66..a3b879a3d7 100644 --- a/js/net/src/window.ts +++ b/js/net/src/window.ts @@ -763,7 +763,13 @@ export class Window { span.start = start ?? 0n; span.floored = start !== undefined; if (held === undefined && asked !== undefined) { - const floor = unresolved(at, span.start, span.floored, holdings); + // Nothing held at or before the time any more, maybe nothing at all: the group containing + // it may start anywhere below the floor asked. + const floor = span.floored + ? unresolved(at, span.start, true, holdings) + : holdings.looksUp?.() + ? 0n + : undefined; if (floor !== undefined && floor < asked) span.looking = { time: at, floor }; } this.#count(key, span); diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index 34d10b01bd..9162c32960 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -628,7 +628,13 @@ impl Window { (None, _) => (0, false), }; if held.is_none() { - span.looking = unresolved(time, span.start, span.floored, holdings) + let floor = match span.floored { + true => unresolved(time, span.start, true, holdings), + // Nothing held at or before the time any more, maybe nothing at all: the group + // containing it may start anywhere below the floor asked. + false => holdings.looks_up().then_some(0), + }; + span.looking = floor .filter(|floor| asked.is_some_and(|asked| *floor < asked)) .map(|floor| Looking { time, floor }); } @@ -1478,6 +1484,17 @@ mod tests { assert!(window.found(8500, Some(4000), &keys)); assert_eq!(window.resolved()[0].map(|resolved| resolved.start), Some(4000)); + // Even with the cache emptied. + let mut window = Window::new(&[range(time(8500), None)], None, Some(9000), &keys).unwrap(); + let all = std::mem::take(&mut keys.held); + assert!(window.found(8500, None, &keys)); + assert_eq!( + window.lookups().collect::>(), + [(8500, 0)], + "looked up from below" + ); + keys.held = all; + // With nothing evicted, an empty answer starts where the groups held say. let mut window = Window::new(&[range(time(8500), None)], None, Some(9000), &keys).unwrap(); assert!(window.found(8500, None, &keys)); From 9ee5c5f66c34bd985d95a0b754b694ca56ef8df9 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 20 Nov 2026 01:06:06 -0800 Subject: [PATCH 08/14] wip: a dropped or refused time start never falls back to the cache --- js/net/src/fill.test.ts | 61 ++++++++--- js/net/src/fill.ts | 49 ++++++--- js/net/src/group.ts | 4 - js/net/src/internal.ts | 5 - js/net/src/track.ts | 49 ++++----- js/net/src/window.test.ts | 53 +++++++--- js/net/src/window.ts | 61 ++++++----- rs/moq-net/src/model/subscription.rs | 2 +- rs/moq-net/src/model/subscription/window.rs | 109 ++++++++++++++------ rs/moq-net/src/model/track.rs | 108 ++++++++++++++++--- rs/moq-net/src/model/track/fill.rs | 62 ++++++++--- 11 files changed, 407 insertions(+), 156 deletions(-) diff --git a/js/net/src/fill.test.ts b/js/net/src/fill.test.ts index 598f7a037c..21176c8529 100644 --- a/js/net/src/fill.test.ts +++ b/js/net/src/fill.test.ts @@ -1579,9 +1579,9 @@ function timed(): { broadcast: BroadcastProducer; track: Producer } { return { broadcast, track }; } -// Answer each time start with `lookup(time)`, refusing it when that is undefined, as a handler that -// can't tell does, and serve every other fill from the archive. Returns the time starts' times, in -// milliseconds. +// Answer each time start with `lookup(time)`, dropping it when that is undefined, as a handler whose +// index holds none there does, and serve every other fill from the archive. Returns the time starts' +// times, in milliseconds. function lookups(requests: Requests, lookup: (time: bigint) => bigint | undefined): bigint[] { const times: bigint[] = []; void (async () => { @@ -1589,7 +1589,7 @@ function lookups(requests: Requests, lookup: (time: bigint) => bigint | undefine if (fill.floor < fill.start) { times.push(fill.start); const answer = lookup(fill.start); - if (answer === undefined) fill.refuseGroups(fill.start, fill.end, new Error("no index")); + if (answer === undefined) fill.dropGroups(fill.start, fill.end); else { const group = defined(fill.accept(answer)); group.writeFrame({ @@ -1673,7 +1673,7 @@ test("fill: a lookup answered below the eviction watermark starts the range ther } }); -test("fill: a time start whose floor's group is evicted while it is out is asked again from below", async () => { +test("fill: a time start dropped after its floor's group is evicted starts at that group", async () => { const clock = new Manual(); const installed = clock.install(); try { @@ -1698,15 +1698,18 @@ test("fill: a time start whose floor's group is evicted while it is out is asked // Idle past the cache window, so the next write evicts every group but the new edge. clock.jump(Duration.millis(DEFAULT_EXPIRY.asMillis() + 1000)); write(10n); - // The index says 1 holds the time, below the floor: nothing from 2 does. + // The index says 1 holds the time, below the floor: nothing from 2 does. Mirrors the Rust + // `a_time_start_is_answered_by_the_group_below_it`: the reader starts at 1 whatever the cache + // holds now, asking it back as a miss rather than the time again. first.dropGroups(2n, 8n); first.close(); - const again = defined(await requests.requestedFill()); - expect([again.start, again.floor]).toEqual([7n, 0n]); - const found = defined(again.accept(1n)); + const miss = defined(await requests.requestedFill()); + expect([miss.start, miss.floor]).toEqual([1n, 1n]); + const found = defined(miss.accept(1n)); found.writeFrame({ payload: enc.encode("g1"), timestamp: Timestamp.fromMillis(1) }); found.finish(); - again.close(); + miss.dropGroups(2n, 8n); + miss.close(); expect((await next)?.start).toBe(1n); reader.close(); @@ -1735,18 +1738,18 @@ test("fill: a lookup's answer is read at once, before its first frame: its key i broadcast.close(); }); -test("fill: a time start refused starts the range at its time, asking nothing below it", async () => { +test("fill: a time start dropped from 0 starts the range at its time, asking nothing below it", async () => { const { broadcast, track } = timed(); const requests = track.requests(); const times = lookups(requests, () => undefined); - // Mirrors the Rust `a_time_start_before_the_cache_asks_nothing_below_it`: the group containing - // the time is unknown, so every key from the time on is in range, and the archive fills from 2. + // Mirrors the Rust `a_time_start_before_the_cache_asks_nothing_below_it`: no group starts at or + // before the time, so the range reads from it, and the archive fills from 3, past the key dropped. const reader = track.subscribe(fromTime(2n)); const ordered = reader.ordered(); const got: bigint[] = []; - for (let i = 0; i < 4; i++) got.push((await ordered.nextGroup())?.start ?? -1n); - expect(got).toEqual([2n, 3n, 4n, 5n]); + for (let i = 0; i < 3; i++) got.push((await ordered.nextGroup())?.start ?? -1n); + expect(got).toEqual([3n, 4n, 5n]); expect(times).toEqual([2n]); reader.close(); @@ -1754,6 +1757,34 @@ test("fill: a time start refused starts the range at its time, asking nothing be broadcast.close(); }); +test("fill: a refused time start fails its reader until its ranges change", async () => { + const { broadcast, track } = timed(); + const requests = track.requests(); + // Mirrors the Rust `a_refused_time_start_fails_its_reader`. + const reader = track.subscribe(fromTime(2n)); + const ordered = reader.ordered(); + const next = ordered.nextGroup(); + const start = defined(await requests.requestedFill()); + expect([start.start, start.floor]).toEqual([2n, 0n]); + const refusal = new Error("store down"); + start.refuseGroups(start.start, start.end, refusal); + start.close(); + // Not group 5, which the cache holds past the time. + await expect(next).rejects.toBe(refusal); + await expect(ordered.nextGroup()).rejects.toBe(refusal); + + const times = lookups(requests, (ms) => ms); + reader.update(fromTime(3n)); + const got: bigint[] = []; + for (let i = 0; i < 3; i++) got.push((await ordered.nextGroup())?.start ?? -1n); + expect(got).toEqual([3n, 4n, 5n]); + expect(times).toEqual([3n]); + + reader.close(); + requests.close(); + broadcast.close(); +}); + test("fill: a time start the held groups place asks no lookup", async () => { const { broadcast, track } = timed(); const requests = track.requests(); diff --git a/js/net/src/fill.ts b/js/net/src/fill.ts index 316e45a6d8..a395002abb 100644 --- a/js/net/src/fill.ts +++ b/js/net/src/fill.ts @@ -104,8 +104,8 @@ class Pending { done = false; // Bumped on every change to what the fill owes, for its handler to wake on. readonly revision = new Signal(0); - // The group a time start's handler accepted. - answer?: bigint; + // What a time start's handler answered. + answer?: TimeStart; constructor(start: bigint, end: bigint, floor = start, exclusive = false) { this.start = start; @@ -276,7 +276,7 @@ export class FillState { const [live] = this.#overlapping(key, key + 1n); if (live) this.#resolve(live.pending, key, key + 1n); for (const pending of this.#starts.get(key) ?? []) { - pending.answer = key; + pending.answer = { kind: "group", group: key }; this.#resolve(pending, key, key + 1n); } } @@ -327,7 +327,11 @@ export class FillState { removeHandler(): void { this.#handlers--; if (this.#handlers > 0) return; - for (const pending of this.#queue.splice(0)) this.#settle(pending); + // A time start nobody took is left unanswered, as the Rust `release_queued` ends it. + for (const pending of this.#queue.splice(0)) { + if (pending.exclusive) this.#finish(pending); + else this.#settle(pending); + } } /** @@ -378,8 +382,9 @@ export class FillState { // Answer what `pending` still owes as dropped, then forget it. #settle(pending: Pending): void { - for (const run of pending.owed()) - this.#track.drop(run.start, run.end, { error: new NotFound(`group ${run.start}`) }); + const owed = pending.owed(); + if (pending.exclusive && owed.length > 0) pending.answer ??= { kind: "dropped" }; + for (const run of owed) this.#track.drop(run.start, run.end, { error: new NotFound(`group ${run.start}`) }); this.#finish(pending); } @@ -418,13 +423,15 @@ export class FillState { const group = state.#track.holds(key) === true ? undefined : state.#track.accept(key, info); // A time start's fill is no live part, and a group below its key lands nowhere it owes. if (pending.exclusive && !pending.done) { - pending.answer = key; + pending.answer = { kind: "group", group: key }; state.#resolve(pending, pending.start, pending.end); } return group; }, drop: (state, pending, start, end, hole) => { - for (const run of pending.owed(start, end)) { + const owed = pending.owed(start, end); + if (pending.exclusive && owed.length > 0) pending.answer ??= { kind: "dropped" }; + for (const run of owed) { state.#track.drop(run.start, run.end, hole ?? { error: new NotFound(`group ${run.start}`) }); } // A stale verdict settles only readers that read every group above it, so the readers @@ -434,6 +441,13 @@ export class FillState { else state.#resolve(pending, start, end); }, refuse: (state, pending, start, end, error, stale) => { + // A time start's refusal reaches its reader through the lookup, as the error its + // reads fail with, not as a group given up. + if (pending.exclusive) { + if (pending.unresolved(start, end).length > 0) pending.answer ??= { kind: "refused", error }; + state.#resolve(pending, start, end); + return; + } for (const gap of pending.unresolved(start, end)) { for (const asker of pending.askers) { for (const run of asker.wants(gap.start, gap.end)) @@ -466,14 +480,22 @@ export interface Judged { everyone: boolean; } +/** + * What a time start's handler answered: the group containing the time, no group from the floor + * through it, or a refusal saying nothing of where it falls. Mirrors the Rust `fill::TimeStart`. + * + * @internal + */ +export type TimeStart = { kind: "group"; group: bigint } | { kind: "dropped" } | { kind: "refused"; error: Error }; + /** * A reader's time start, from {@link FillState.timeStart}. * * @internal */ export interface Lookup { - /** The group its handler accepted, undefined until one is. */ - answer(): bigint | undefined; + /** What its handler answered, undefined until it does, or when it closed unanswered. */ + answer(): TimeStart | undefined; /** Whether it is over: answered, or closed unanswered. */ done(): boolean; /** Bumped as it changes. */ @@ -616,8 +638,11 @@ export class FillGroups { * starting at it, or else the newest one starting before it, at or after the floor. A handler * knowing its groups only by key serves that key as any other; one that can tell which group * holds the time, as from an index, may {@link accept} that group instead, or - * {@link dropGroups} the key when none does. The reader starts where the answer says; closed - * unanswered or refused, it starts where the groups the cache holds say. + * {@link dropGroups} the key when none does. The reader starts where the answer says, never + * where the cached groups happen to say: a drop, or the fill closed unanswered, says no group + * starts from the floor through the time, so it starts at the group just below the floor, or + * past the time when the floor is 0. A {@link refuseGroups refusal} fails the reader with its + * error until its ranges change. */ readonly floor: bigint; diff --git a/js/net/src/group.ts b/js/net/src/group.ts index 06422451f9..a64a3ad1d6 100644 --- a/js/net/src/group.ts +++ b/js/net/src/group.ts @@ -313,10 +313,6 @@ export class Producer { }; hooks.groupOffset = (group) => group.#state.offset; hooks.groupHeadless = (group) => group.#state.index > 0 || headShed(group.#state.frames.peek().first); - hooks.groupMayStart = (group) => { - const state = group.#state; - return state.offset === 0 && state.total.peek() === 0 && state.closed.peek() === undefined; - }; hooks.holdGroup = (group) => { group.#holds++; let held = true; diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index bbde94521e..a1dd9a2702 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -974,8 +974,6 @@ export const hooks: { groupOffset: (group: GroupProducer) => number; /** Whether this source copy can no longer serve frame 0: it began past it, or shed it. */ groupHeadless: (group: GroupProducer) => boolean; - /** Whether this source copy began at frame 0 and may still write it. */ - groupMayStart: (group: GroupProducer) => boolean; writeGroupFrame: (group: GroupProducer, frame: Frame, inflated?: number) => void; /** Payload footprint including a shared inflated copy, for measurements. */ groupCacheBytes: (group: GroupProducer) => number; @@ -1376,9 +1374,6 @@ export const hooks: { groupHeadless: () => { throw new Error("group.ts not loaded"); }, - groupMayStart: () => { - throw new Error("group.ts not loaded"); - }, startGroup: () => { throw new Error("group.ts not loaded"); }, diff --git a/js/net/src/track.ts b/js/net/src/track.ts index 1511f7642a..c750b11afd 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -57,7 +57,7 @@ import { rescale } from "./rescale.ts"; import { floorAt, Timescale, type Timestamp } from "./time.ts"; import { checkEnd, checkKey, max, min, UNBOUNDED } from "./util/key.ts"; import { checkName } from "./util/utf8.ts"; -import { bounded, type Holdings, joinsLive, rangesOf, UNHELD, Window, wireSpan } from "./window.ts"; +import { bounded, type Found, type Holdings, joinsLive, rangesOf, UNHELD, Window, wireSpan } from "./window.ts"; import { type Broadcast as BroadcastWire, registerTrackConsumer } from "./wire.ts"; export { FillGroups, Requests } from "./fill.ts"; @@ -964,9 +964,6 @@ function removeKey(keys: bigint[], key: bigint): void { let cachedMirror: (producer: Producer, key: bigint, from: number) => GroupConsumer | undefined; // Whether the producer still caches a group that has not failed; assigned in its static block. let cachedHolds: (producer: Producer, key: bigint) => boolean; -// Whether group `key` is cached as a copy from frame 0 that has not written it yet, so its -// start may still land, as the Rust `Slot::may_start` says. -let cachedMayStart: (producer: Producer, key: bigint) => boolean; // Whether the producer is a copy a session receives, whose handler fetches by group alone. let receivedCopy: (producer: Producer) => boolean; // The keys in `[start, end)` the producer caches, unsorted, costing the smaller of the span @@ -1576,10 +1573,6 @@ export class Producer { const entry = producer.#cached.get(key); return entry !== undefined && !(entry.group.closed.peek() instanceof Error); }; - cachedMayStart = (producer, key) => { - const entry = producer.#cached.get(key); - return entry !== undefined && hooks.groupMayStart(entry.group); - }; receivedCopy = (producer) => producer.#wire !== undefined; cachedIn = (producer, start, end) => { if (end - start <= BigInt(producer.#cached.size)) { @@ -3125,6 +3118,9 @@ export class Subscriber { // The time starts this reader asked its fills to look up, each with the floor it asked from and // its fill, absent when no handler could take it. #lookups: { time: bigint; floor: bigint; lookup?: Lookup }[] = []; + // A time start's refusal, which every read fails with until the ranges change. Mirrors the Rust + // `Filter::refused`. + #refusal?: Error; #drift(whole = this.#fetchBudget !== undefined): Drift { // A fetch judges its group against the whole track, as the Rust `fetch_group` does: a @@ -3590,6 +3586,7 @@ export class Subscriber { // Resolve this reader's ranges again, as when they are replaced. #resolve(): void { const update = this.#state.update.peek(); + this.#refusal = undefined; this.#window = new Window( update?.ranges ?? [{}], this.#latestGroup(), @@ -3775,18 +3772,19 @@ export class Subscriber { if (this.#state.dropsAnchors.peek() && !this.#window.wired) { return new Error("a relative or time anchor other than the live start needs moq-lite-07"); } - if (this.#resume || this.#window.settled) return undefined; + if (this.#refusal || this.#resume || this.#window.settled) return this.#refusal; // The live feed had no group when the ranges resolved, so the newest one then was the first. const latest = this.#window.pending ? this.#state.first : this.#latestGroup(); const looked = this.#lookUp(); if (this.#window.settle(latest, this.#holdings()) || looked) this.#moved(); - return undefined; + return this.#refusal; } // Ask a fill for the group containing each time start the window can't resolve from the cache, - // once, and take in the answers. Returns whether the groups in range moved. Mirrors the Rust - // `Cursor::look_up`. + // once, and take in the answers. A refused one fails the reads until the ranges change. Returns + // whether the groups in range moved. Mirrors the Rust `Cursor::look_up`. #lookUp(): boolean { + if (this.#refusal) return false; let moved = false; // An answer the window takes in may look its time up again from lower down. for (let again = true; again; ) { @@ -3819,18 +3817,21 @@ export class Subscriber { for (const lookup of [...this.#lookups]) { const answer = lookup.lookup?.answer(); if (answer === undefined && lookup.lookup && !lookup.lookup.done()) continue; - // The answer counts once held. - const holdings = this.#holdings(); - if ( - answer !== undefined && - producer && - holdings.nextStart(answer) !== answer && - cachedMayStart(producer, answer) - ) - continue; this.#lookups.splice(this.#lookups.indexOf(lookup), 1); lookup.lookup?.leave(); - if (this.#window.found(lookup.time, answer, holdings)) moved = true; + // Where the time falls is unknown, so the reads fail rather than guess from the cache. The + // ranges resolve afresh once they change, asking again. + if (answer?.kind === "refused") { + this.#refusal = answer.error; + return { moved: true, again: false }; + } + const found: Found = + answer === undefined + ? { kind: "unanswered" } + : answer.kind === "group" + ? answer + : { kind: "dropped", floor: lookup.floor }; + if (this.#window.found(lookup.time, found, this.#holdings())) moved = true; if (this.#window.lookups().some((want) => want.time === lookup.time)) again = true; } return { moved, again }; @@ -5710,7 +5711,9 @@ export class Subscriber { const moved = this.#state.update.edit({ retire: edit.retire, add, ...scalars }); // A live join not yet started reaches back by the Min Age stated when it starts. this.#window.min = scalars.delay.min ?? undefined; - if (moved) { + // A refused time start resolves every range again, as the Rust `Filter::stale` has it. + if (moved && this.#refusal) this.#resolve(); + else if (moved) { this.#window.edit(edit.retire, add, this.#latestGroup(), this.#holdings()); // Leaves the lookups the retired ranges asked, which a settled window never reads again. if (this.#lookups.length > 0) this.#lookUp(); diff --git a/js/net/src/window.test.ts b/js/net/src/window.test.ts index 6d3ab832d2..4ca51c37fb 100644 --- a/js/net/src/window.test.ts +++ b/js/net/src/window.test.ts @@ -6,6 +6,7 @@ import { type Containing, clamped, EMPTY, + type Found, type Holdings, isEmpty, isWire, @@ -303,7 +304,7 @@ describe("Window", () => { expect(window.lookups()).toEqual([{ time: 8500n, floor: 4001n }]); expect([window.settled, window.contains(9000n), window.resolved()]).toEqual([false, false, [undefined]]); held.held.add(8000n); - expect(window.found(8500n, 8000n, held)).toBe(true); + expect(window.found(8500n, { kind: "group", group: 8000n }, held)).toBe(true); expect(window.resolved()).toEqual([{ start: 8000n, end: undefined, floored: true }]); expect(window.settled).toBe(true); @@ -314,40 +315,63 @@ describe("Window", () => { window = new Window([range(time(500n))], 9000n, held); expect(window.lookups()).toEqual([{ time: 500n, floor: 0n }]); - expect(window.found(500n, undefined, held)).toBe(true); - expect(window.resolved()).toEqual([{ start: 0n, end: undefined, floored: false }]); + // A drop from 0 says no group starts at or before it, so it reads from the time on. + expect(window.found(500n, { kind: "dropped", floor: 0n }, held)).toBe(true); + expect(window.resolved()).toEqual([{ start: 500n, end: undefined, floored: true }]); expect(window.contains(4000n)).toBe(true); expect(window.missable(0n).some((run) => run.start < 500n)).toBe(false); + // A group below the time held since is none of the answer's. + held.held.add(200n); + expect(window.contains(200n)).toBe(false); + held.held.delete(200n); // A lookup nobody asked for is ignored. - expect(window.found(500n, undefined, held)).toBe(false); + expect(window.found(500n, { kind: "unanswered" }, held)).toBe(false); }); - test("an eviction during a lookup looks it up again", () => { + test("a dropped lookup starts below its floor", () => { + const history = (keys: bigint[]) => { + const held = new Keys(keys); + held.looks = true; + return held; + }; + // 4000 evicted while the lookup was out, or 8000 arrived between, as another reader's fill + // brings it. + for (const held of [[9000n], [4000n, 8000n, 9000n]]) { + const window = new Window([range(time(8500n))], 9000n, history([4000n, 9000n])); + expect(window.lookups()).toEqual([{ time: 8500n, floor: 4001n }]); + expect(window.found(8500n, { kind: "dropped", floor: 4001n }, history(held))).toBe(true); + // Answered, not looked up again. + expect(window.lookups()).toHaveLength(0); + expect(window.resolved()).toEqual([{ start: 4000n, end: undefined, floored: true }]); + } + }); + + test("an eviction during an unanswered lookup looks it up again", () => { const held = new Keys([4000n, 9000n]); held.looks = true; let window = new Window([range(time(8500n))], 9000n, held); expect(window.lookups()).toEqual([{ time: 8500n, floor: 4001n }]); held.held.delete(4000n); - expect(window.found(8500n, undefined, held)).toBe(true); + expect(window.found(8500n, { kind: "unanswered" }, held)).toBe(true); // Looked up from below, reading nothing meanwhile. expect(window.lookups()).toEqual([{ time: 8500n, floor: 0n }]); expect(window.contains(9000n)).toBe(false); held.held.add(4000n); - expect(window.found(8500n, 4000n, held)).toBe(true); + expect(window.found(8500n, { kind: "group", group: 4000n }, held)).toBe(true); expect(window.resolved()[0]?.start).toBe(4000n); // Even with the cache emptied. window = new Window([range(time(8500n))], 9000n, held); const all = held.held; held.held = new Set(); - expect(window.found(8500n, undefined, held)).toBe(true); + expect(window.found(8500n, { kind: "unanswered" }, held)).toBe(true); expect(window.lookups()).toEqual([{ time: 8500n, floor: 0n }]); held.held = all; - // With nothing evicted, an empty answer starts where the groups held say. + // With nothing evicted, it starts where the groups held say. window = new Window([range(time(8500n))], 9000n, held); - expect(window.found(8500n, undefined, held)).toBe(true); + expect(window.found(8500n, { kind: "unanswered" }, held)).toBe(true); expect(window.lookups()).toHaveLength(0); expect(window.resolved()[0]?.start).toBe(4000n); }); @@ -377,7 +401,7 @@ describe("Window", () => { held.looks = true; const window = new Window([range(time(500n), time(3000n))], 9000n, held); expect(window.lookups()).toEqual([{ time: 500n, floor: 0n }]); - expect(window.found(500n, undefined, held)).toBe(true); + expect(window.found(500n, { kind: "unanswered" }, held)).toBe(true); expect(window.settled).toBe(true); expect(window.resolved()).toEqual([{ start: 0n, end: 3001n, floored: false }]); }); @@ -606,7 +630,12 @@ test("window: settling by steps matches trying every start and rebuilding from t } else if (op === 2) { const lookup = fast.lookups()[0]; if (lookup === undefined) continue; - const answer = value % 2n === 0n ? value : undefined; + const answer: Found = + value % 3n === 0n + ? { kind: "group", group: value } + : value % 3n === 1n + ? { kind: "dropped", floor: lookup.floor } + : { kind: "unanswered" }; fast.found(lookup.time, answer, keys); full.found(lookup.time, answer, blind(keys)); } else if (op === 3) keys.done = true; diff --git a/js/net/src/window.ts b/js/net/src/window.ts index a3b879a3d7..7285fa8c39 100644 --- a/js/net/src/window.ts +++ b/js/net/src/window.ts @@ -429,6 +429,13 @@ export interface Lookup { floor: bigint; } +/** + * What the track's history answered a time start's lookup with: the group containing the time, no + * group from `floor`, the oldest key it asked from, through the time, or nothing, with no handler + * left to. Mirrors the Rust `window::Found`. + */ +export type Found = { kind: "group"; group: bigint } | { kind: "dropped"; floor: bigint } | { kind: "unanswered" }; + /** * A subscriber's ranges resolved to the group starts it reads, each by the key its caller gave * it, so an edit resolves only the ranges it adds and leaves every other where it resolved. @@ -742,35 +749,43 @@ export class Window { } /** - * The lookup of time start `time` answered with `group`, the group containing it: its spans - * start there, once that group is held. Undefined, or an answer not held, means the history - * couldn't tell, or holds no group there, so they start where the groups held say, unless the - * cache since evicted the group their floor was past: then they look it up again from lower - * down. Returns whether the groups in range moved. Mirrors the Rust `Window::found`. + * The lookup of time start `at` answered: its spans start where the answer says, never where the + * groups held happen to. Left unanswered because no handler is left, they start where the groups + * held say, as with no handler at all, unless the cache since evicted the group their floor was + * past and a handler is back: then they look it up again from lower down. Returns whether the + * groups in range moved. Mirrors the Rust `Window::found`. */ - found(at: bigint, group: bigint | undefined, holdings: Holdings = UNHELD): boolean { - const found = this.#looking.take(at); - if (found === undefined) return false; - const held = group !== undefined && holdings.nextStart(group) === group ? group : undefined; - const containing = holdings.containing(at); - const start = held ?? (typeof containing === "object" ? containing.group : undefined); - for (const key of found) { + found(at: bigint, found: Found, holdings: Holdings = UNHELD): boolean { + const keys = this.#looking.take(at); + if (keys === undefined) return false; + for (const key of keys) { const span = this.#spans.get(key); if (!span) continue; this.#uncount(key, span); const asked = span.looking?.floor; span.looking = undefined; - span.start = start ?? 0n; - span.floored = start !== undefined; - if (held === undefined && asked !== undefined) { - // Nothing held at or before the time any more, maybe nothing at all: the group containing - // it may start anywhere below the floor asked. - const floor = span.floored - ? unresolved(at, span.start, true, holdings) - : holdings.looksUp?.() - ? 0n - : undefined; - if (floor !== undefined && floor < asked) span.looking = { time: at, floor }; + if (found.kind === "group") { + span.start = found.group; + span.floored = true; + } else if (found.kind === "dropped") { + // A floor is one past the group held below the time, which then holds it; from 0, no + // group starts at or before it, so the span reads from the time on. + span.start = found.floor > 0n ? found.floor - 1n : at; + span.floored = true; + } else { + const containing = holdings.containing(at); + span.start = typeof containing === "object" ? containing.group : 0n; + span.floored = typeof containing === "object"; + if (asked !== undefined) { + // Nothing held at or before the time any more, maybe nothing at all: the group containing + // it may start anywhere below the floor asked. + const floor = span.floored + ? unresolved(at, span.start, true, holdings) + : holdings.looksUp?.() + ? 0n + : undefined; + if (floor !== undefined && floor < asked) span.looking = { time: at, floor }; + } } this.#count(key, span); } diff --git a/rs/moq-net/src/model/subscription.rs b/rs/moq-net/src/model/subscription.rs index 5815674267..65d3edc1e1 100644 --- a/rs/moq-net/src/model/subscription.rs +++ b/rs/moq-net/src/model/subscription.rs @@ -8,7 +8,7 @@ mod window; pub(super) use cover::{Cover, minus}; pub(crate) use window::Accounted; pub(crate) use window::Resolved; -pub(super) use window::{Containing, Holdings, Unheld, Unplaced, Window}; +pub(super) use window::{Containing, Found, Holdings, Unheld, Unplaced, Window}; /// Subscriber-side preferences for receiving a track. /// diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index 9162c32960..8686974693 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -122,6 +122,17 @@ pub(in crate::model) enum Containing { Unknown, } +/// What the track's history answered a time start's lookup with; see [`Window::found`]. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum Found { + /// The group containing the time. + Group(u64), + /// No group starts from `floor`, the oldest key the lookup asked from, through the time. + Dropped { floor: u64 }, + /// Nothing answered: no handler was left to. + Unanswered, +} + /// The times a hop's [`Holdings::containing`] can't place, as up to two runs: every time in /// them is [`Containing::Unknown`], except the few that `mark` places another way. #[derive(Clone, Debug, PartialEq, Eq)] @@ -602,32 +613,37 @@ impl Window { .filter_map(|(_, key)| self.spans[key].looking.map(|looking| (looking.time, looking.floor))) } - /// The lookup of time start `time` answered with the group containing it: its spans start - /// there, once that group is held. `None`, or an answer not held, means the history - /// couldn't tell, or holds no group there, so they start where the groups held say, - /// unless the cache since evicted the group their floor was past: then the history may + /// The lookup of time start `time` answered: its spans start where the answer says, + /// never where the groups held happen to. Left unanswered because no handler is left, + /// they start where the groups held say, as with no handler at all, unless the cache since + /// evicted the group their floor was past and a handler is back: then the history may /// hold the answer below that floor, so they look it up again from lower down. Each /// look-up again needs another eviction, so it ends. Returns whether the groups in range /// moved. - pub fn found(&mut self, time: u64, group: Option, holdings: &impl Holdings) -> bool { - let found: Vec = self + pub fn found(&mut self, time: u64, found: Found, holdings: &impl Holdings) -> bool { + let keys: Vec = self .looking .range((time, 0)..=(time, u64::MAX)) .map(|(_, key)| *key) .collect(); - if found.is_empty() { + if keys.is_empty() { return false; } - let held = group.filter(|group| holdings.next_start(*group) == Some(*group)); - for key in found { + for key in keys { let mut span = self.spans.remove(&key).expect("a span looking up its start"); self.uncount(key, &span); let asked = span.looking.take().map(|looking| looking.floor); - (span.start, span.floored) = match (held, holdings.containing(time)) { - (Some(group), _) | (None, Containing::Group(group)) => (group, true), - (None, _) => (0, false), + (span.start, span.floored) = match found { + Found::Group(group) => (group, true), + // A floor is one past the group held below the time, which then holds it; from 0, + // no group starts at or before it, so the span reads from the time on. + Found::Dropped { floor } => (floor.checked_sub(1).unwrap_or(time), true), + Found::Unanswered => match holdings.containing(time) { + Containing::Group(group) => (group, true), + _ => (0, false), + }, }; - if held.is_none() { + if let Found::Unanswered = found { let floor = match span.floored { true => unresolved(time, span.start, true, holdings), // Nothing held at or before the time any more, maybe nothing at all: the group @@ -1104,10 +1120,14 @@ mod tests { 0 => drop(keys.held.insert(value)), 1 => keys.dropped = keys.dropped.max(value), 2 => { - let Some((time, _)) = fast.lookups().next() else { + let Some((time, floor)) = fast.lookups().next() else { continue; }; - let answer = (value % 2 == 0).then_some(value); + let answer = match value % 3 { + 0 => Found::Group(value), + 1 => Found::Dropped { floor }, + _ => Found::Unanswered, + }; fast.found(time, answer, &keys); full.found(time, answer, &Blind(&keys)); } @@ -1423,8 +1443,8 @@ mod tests { /// With a handler serving the history, a time start the groups held can't place asks a /// fill for its group from past the newest held below it, and reads nothing meanwhile: /// before every start, or with a group the cache evicted possibly starting between the - /// newest held at or before it and the time. The answer places it; none leaves what the - /// groups held say. + /// newest held at or before it and the time. The answer places it: a drop from 0 says no + /// group starts at or before it, so it reads from the time on. #[test] fn a_time_start_the_groups_cant_place_is_looked_up() { let mut keys = Keys::new([4000, 9000]); @@ -1433,7 +1453,7 @@ mod tests { assert_eq!(window.lookups().collect::>(), [(8500, 4001)]); assert!(!window.is_settled() && !window.contains(9000) && window.resolved() == [None]); keys.held.insert(8000); - assert!(window.found(8500, Some(8000), &keys)); + assert!(window.found(8500, Found::Group(8000), &keys)); assert_eq!( window.resolved(), [Some(Resolved { @@ -1451,21 +1471,50 @@ mod tests { let mut window = Window::new(&[range(time(500), None)], None, Some(9000), &keys).unwrap(); assert_eq!(window.lookups().collect::>(), [(500, 0)]); - assert!(window.found(500, None, &keys)); + assert!(window.found(500, Found::Dropped { floor: 0 }, &keys)); assert_eq!( window.resolved(), [Some(Resolved { - start: 0, + start: 500, end: None, - floored: false + floored: true })] ); assert!(window.contains(4000) && !window.missable(0).any(|run| *run.start() < 500)); + // A group below the time held since is none of the answer's. + keys.held.insert(200); + assert!(!window.contains(200)); + } + + /// A dropped lookup starts at the group its floor was past, which then holds the time, + /// whatever the cache holds by the answer: that group evicted, or another arrived between. + #[test] + fn a_dropped_lookup_starts_below_its_floor() { + let history = |held: &[u64]| Keys { + looks_up: true, + ..Keys::new(held.iter().copied()) + }; + // 4000 evicted while the lookup was out, or 8000 arrived between, as another reader's + // fill brings it. + for held in [&[9000][..], &[4000, 8000, 9000]] { + let mut window = Window::new(&[range(time(8500), None)], None, Some(9000), &history(&[4000, 9000])).unwrap(); + assert_eq!(window.lookups().collect::>(), [(8500, 4001)]); + assert!(window.found(8500, Found::Dropped { floor: 4001 }, &history(held))); + assert_eq!(window.lookups().count(), 0, "answered, not looked up again"); + assert_eq!( + window.resolved(), + [Some(Resolved { + start: 4000, + end: None, + floored: true + })] + ); + } } - /// The group a lookup's floor was past evicted while it was out, an empty answer looks - /// the time up again from below, where the history may hold the group containing it, - /// rather than starting where the groups still held say. + /// The group a lookup's floor was past evicted while it was out, a lookup left unanswered + /// looks the time up again from below once a handler is back, where the history may hold + /// the group containing it, rather than starting where the groups still held say. #[test] fn an_eviction_during_a_lookup_looks_it_up_again() { let mut keys = Keys::new([4000, 9000]); @@ -1473,7 +1522,7 @@ mod tests { let mut window = Window::new(&[range(time(8500), None)], None, Some(9000), &keys).unwrap(); assert_eq!(window.lookups().collect::>(), [(8500, 4001)]); keys.held.remove(&4000); - assert!(window.found(8500, None, &keys)); + assert!(window.found(8500, Found::Unanswered, &keys)); assert_eq!( window.lookups().collect::>(), [(8500, 0)], @@ -1481,13 +1530,13 @@ mod tests { ); assert!(!window.contains(9000), "reads nothing meanwhile"); keys.held.insert(4000); - assert!(window.found(8500, Some(4000), &keys)); + assert!(window.found(8500, Found::Group(4000), &keys)); assert_eq!(window.resolved()[0].map(|resolved| resolved.start), Some(4000)); // Even with the cache emptied. let mut window = Window::new(&[range(time(8500), None)], None, Some(9000), &keys).unwrap(); let all = std::mem::take(&mut keys.held); - assert!(window.found(8500, None, &keys)); + assert!(window.found(8500, Found::Unanswered, &keys)); assert_eq!( window.lookups().collect::>(), [(8500, 0)], @@ -1495,9 +1544,9 @@ mod tests { ); keys.held = all; - // With nothing evicted, an empty answer starts where the groups held say. + // With nothing evicted, it starts where the groups held say. let mut window = Window::new(&[range(time(8500), None)], None, Some(9000), &keys).unwrap(); - assert!(window.found(8500, None, &keys)); + assert!(window.found(8500, Found::Unanswered, &keys)); assert_eq!(window.lookups().count(), 0); assert_eq!(window.resolved()[0].map(|resolved| resolved.start), Some(4000)); } @@ -1522,7 +1571,7 @@ mod tests { keys.looks_up = true; let mut window = Window::new(&[range(time(500), time(3000))], None, Some(9000), &keys).unwrap(); assert_eq!(window.lookups().collect::>(), [(500, 0)]); - assert!(window.found(500, None, &keys)); + assert!(window.found(500, Found::Unanswered, &keys)); assert!(window.is_settled()); assert_eq!( window.resolved(), diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index f2c0de12b7..eb5cbf882d 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -23,7 +23,7 @@ use super::record::{Pending, Record, Report}; use crate::ledger::{self, Ledger}; pub(crate) use super::subscription::Resolved; -use super::subscription::{Accounted, Containing, Cover, Holdings, Unplaced, Window, minus}; +use super::subscription::{Accounted, Containing, Cover, Found, Holdings, Unplaced, Window, minus}; pub use super::subscription::{Anchor, Delay, Order, Position, Range, Subscription}; use rangemap::{RangeInclusiveSet, RangeMap, RangeSet}; @@ -9713,10 +9713,14 @@ impl Cursor { } /// Ask a fill for the group containing each time start the window can't resolve from the - /// cache, once, and take in the answers, parking `waiter` on the fills still out. + /// cache, once, and take in the answers, parking `waiter` on the fills still out. A + /// refused one fails the reads until the ranges change. /// /// Indexed by time, so a pass costs O(L log L) in the lookups out rather than O(L²). fn look_up(&mut self, waiter: &kio::Waiter) { + if self.filter.refused.is_some() { + return; + } let mut moved = false; // An answer the window takes in may look its time up again from lower down. while self.look_up_pass(waiter, &mut moved) {} @@ -9766,19 +9770,31 @@ impl Cursor { } let time = lookup.time; let answer = match &lookup.join { - None => None, + None => fill::TimeStart::Unanswered, Some(join) => match join.outcome().poll(waiter, |outcome| { outcome.time_start(time).map_or(Poll::Pending, Poll::Ready) }) { - Poll::Ready(Ok(group)) => group, - // Ended unanswered: its handler couldn't tell. - Poll::Ready(Err(closed)) => closed.time_start(time).flatten(), + Poll::Ready(Ok(answer)) => answer, + // Ended unanswered: no handler is left to answer it. + Poll::Ready(Err(closed)) => closed.time_start(time).unwrap_or(fill::TimeStart::Unanswered), Poll::Pending => continue, }, }; - let state = self.state.read(); lookup.join = None; - *moved |= self.filter.window.found(lookup.time, answer, &*state); + let found = match answer { + fill::TimeStart::Group(group) => Found::Group(group), + fill::TimeStart::Dropped => Found::Dropped { floor: lookup.floor }, + fill::TimeStart::Unanswered => Found::Unanswered, + // Where the time falls is unknown, so the reads fail rather than guess from the + // cache. The ranges resolve afresh once they change, asking again. + fill::TimeStart::Refused(err) => { + (self.filter.refused, self.filter.stale) = (Some(err), true); + *moved = true; + return false; + } + }; + let state = self.state.read(); + *moved |= self.filter.window.found(lookup.time, found, &*state); again |= self.filter.window.lookups().any(|(time, _)| time == lookup.time); } again @@ -9919,6 +9935,10 @@ impl Cursor { let drift = ready!(self.poll_drift(&self.servable, waiter))?; self.reoffer(drift.budget); self.probe(self.floor(), &drift, waiter); + // A time start's lookup the probe took in may have been refused. + if let Some(err) = &self.filter.refused { + return Poll::Ready(Err(err.clone())); + } if let Some(misses) = &mut self.misses { misses.pick_up(); } @@ -10226,6 +10246,10 @@ impl Cursor { // One scan for the whole poll, so walking a backlog off stays linear in its size. let drift = ready!(self.poll_drift(&self.servable, waiter))?; self.probe(floor, &drift, waiter); + // A time start's lookup the probe took in may have been refused. + if let Some(err) = &self.filter.refused { + return Poll::Ready(Err(err.clone())); + } if let Some(misses) = &mut self.misses { misses.pick_up(); } @@ -25871,8 +25895,8 @@ mod test { /// A time start before every group held asks a fill for the group containing its time, /// one key wide from the time and floored at 0, which no other reader joins: a handler - /// that can't tell refuses it, and the start reaches back to that group, which is unknown, - /// so it asks no fill below the time itself: every key from there is in range. Another + /// knowing only keys drops it, saying no group starts at or before the time, so the + /// reader asks no fill below the time itself: every key from there is in range. Another /// range still asks for its own misses. #[test] #[cfg(feature = "async")] @@ -25901,7 +25925,7 @@ mod test { }; assert_eq!((start.start(), start.floor(), start.end()), (Position::group(5), 0, 6)); assert_eq!(start.wanted(), Some(5), "a handler knowing only keys serves the time's"); - start.refuse_groups(5..6, Error::NotFound); + start.drop_groups(5..6); drop(start); let mut read = drain_groups(&mut subscriber); let mut asked = Vec::new(); @@ -26022,8 +26046,9 @@ mod test { drop(start); assert_eq!(drain_groups(&mut subscriber), [6]); - // The group the floor was past evicted while the start was out, the handler's answer - // below the floor is asked again from lower down rather than lost. + // The group the floor was past evicted while the start was out, a drop still starts the + // reader there, the group holding the time, which it asks back as a miss rather than + // starting wherever the groups still held say. let producer = track_producer("test", Info::timed()); let dynamic = producer.requests(); for key in 0..10 { @@ -26046,13 +26071,62 @@ mod test { start.drop_groups(5..8); drop(start); assert_eq!(drain_groups(&mut subscriber), Vec::::new()); + let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) else { + panic!("the start's group is a miss"); + }; + assert_eq!((fill.start().group, fill.floor()), (4, 4), "a miss, not the time again"); + write_filled(fill.accept(4, None), 4); + fill.drop_groups(5..8); + drop(fill); + assert_eq!(drain_groups(&mut subscriber), [4]); + } + + /// A handler refusing a time start fails its reader with that refusal, rather than + /// starting where the groups held happen to say, until its ranges change, which asks again. + #[test] + #[cfg(feature = "async")] + fn a_refused_time_start_fails_its_reader() { + let producer = track_producer("test", Info::timed()); + let dynamic = producer.requests(); + for key in 0..10 { + write_group(&producer, key); + } + for key in 5..9 { + evict(&producer, key); + } + let since = replay().with_ranges([Range { + start: Anchor::Time(7), + end: None, + }]); + let mut subscriber = producer.subscribe(since); + let waiter = kio::Waiter::noop(); + assert!(subscriber.poll_recv_group(&waiter).is_pending()); let Poll::Ready(Ok(start)) = dynamic.poll_requested_fill(&waiter) else { - panic!("the time start asks again from below"); + panic!("the time start asks for its group"); }; - assert_eq!((start.start().group, start.floor()), (7, 4)); - write_filled(start.accept(4, None), 4); + assert_eq!((start.start().group, start.floor()), (7, 5)); + start.refuse_groups(7..8, Error::Timeout); drop(start); - assert_eq!(drain_groups(&mut subscriber), [4]); + for _ in 0..2 { + assert!( + matches!(subscriber.poll_recv_group(&waiter), Poll::Ready(Err(Error::Timeout))), + "not group 4, which the cache holds below the time" + ); + } + + subscriber + .update(replay().with_ranges([Range { + start: Anchor::Time(6), + end: None, + }])) + .unwrap(); + assert!(subscriber.poll_recv_group(&waiter).is_pending()); + let Poll::Ready(Ok(start)) = dynamic.poll_requested_fill(&waiter) else { + panic!("the time start asks again"); + }; + write_filled(start.accept(6, None), 6); + drop(start); + assert_eq!(drain_groups(&mut subscriber), [6, 9]); } /// A time end walking the history a handler serves takes no group the live feed diff --git a/rs/moq-net/src/model/track/fill.rs b/rs/moq-net/src/model/track/fill.rs index d54b5ca16b..0bc412cdce 100644 --- a/rs/moq-net/src/model/track/fill.rs +++ b/rs/moq-net/src/model/track/fill.rs @@ -213,13 +213,17 @@ impl Outcome { self.accepted.contains(&key) } - /// Where a time start's fill says its group is, once `time`'s key is answered: the group - /// accepted, or `None` when it was refused or dropped instead. - pub(super) fn time_start(&self, time: u64) -> Option> { - let first = self.accepted.iter().next().map(|run| run.start); - match first.is_some() || self.resolved.contains(&time) { - true => Some(first), - false => None, + /// What a time start's fill answered, once `time`'s key is: `None` while it is out. + pub(super) fn time_start(&self, time: u64) -> Option { + if let Some(run) = self.accepted.iter().next() { + return Some(TimeStart::Group(run.start)); + } + match self.refused.get(&time) { + Some(refusal) if refusal.dropped => Some(TimeStart::Dropped), + Some(refusal) => Some(TimeStart::Refused(refusal.err.clone())), + // Answered and forgotten: nobody reads it any more. + None if self.resolved.contains(&time) => Some(TimeStart::Unanswered), + None => None, } } @@ -256,17 +260,37 @@ impl Outcome { self.resolved.insert(groups); } - fn refuse(&mut self, groups: ops::Range, err: Error, asked: Option) { + fn refuse(&mut self, groups: ops::Range, err: Error, asked: Option, dropped: bool) { if groups.is_empty() { return; } let id = self.refusals; self.refusals += 1; - self.refused.insert(groups.clone(), Refusal { id, err, asked }); + self.refused.insert( + groups.clone(), + Refusal { + id, + err, + asked, + dropped, + }, + ); self.resolved.insert(groups); } } +/// What a time start's fill answered. +pub(super) enum TimeStart { + /// The group containing the time. + Group(u64), + /// No group from the fill's floor through the time. + Dropped, + /// Refused with this error, saying nothing of where the time falls. + Refused(Error), + /// Nothing answers it: no handler is left to. + Unanswered, +} + /// One refusal, equal only to itself: the map keeps a run it refused whole, but never merges /// two refusals whose errors may differ. #[derive(Clone)] @@ -276,6 +300,8 @@ struct Refusal { // The budget its source was asked under, so a looser reader can tell that an // [`Error::Old`] was not its own. asked: Option, + // Named dropped, the source's word that it has none of them, rather than refused for now. + dropped: bool, } impl PartialEq for Refusal { @@ -928,8 +954,11 @@ impl FillGroups { /// it, at or after the floor. A handler knowing its groups only by key serves that key as /// any other; one that can tell which group holds the time, as from an index, may /// [`accept`](Self::accept) that group instead, or [`drop_groups`](Self::drop_groups) the - /// key when none does. The reader starts where the answer says; left unanswered or - /// refused, it starts where the groups the cache holds say. + /// key when none does. The reader starts where the answer says, never where the groups + /// the cache holds happen to say: a drop, or the fill closed unanswered, says no group + /// starts from the floor through the time, so it starts at the group just below the floor, + /// or past the time when the floor is 0. A [refusal](Self::refuse_groups) fails the reader + /// with its error until its ranges change. pub fn floor(&self) -> u64 { self.inner.floor } @@ -1402,7 +1431,7 @@ impl Inner { state.drop_unfed(groups.clone(), hole.clone()); } } - self.refuse(groups, err, asked); + self.answer(groups, err, asked, true); } /// The part of `groups` inside the fill that a reader still reads and nothing answered yet. @@ -1431,12 +1460,17 @@ impl Inner { } fn refuse(&self, groups: &RangeSet, err: Error, asked: Option) { + self.answer(groups, err, asked, false); + } + + /// Answer `groups` with `err`, `dropped` when the source has none of them. + fn answer(&self, groups: &RangeSet, err: Error, asked: Option, dropped: bool) { if groups.is_empty() { return; } if let Ok(mut outcome) = self.result.write() { for groups in groups.iter() { - outcome.refuse(groups.clone(), err.clone(), asked); + outcome.refuse(groups.clone(), err.clone(), asked, dropped); } } for groups in groups.iter() { @@ -1528,7 +1562,7 @@ impl Drop for Inner { } if let Ok(mut outcome) = self.result.write() { for groups in left.iter() { - outcome.refuse(groups.clone(), Error::Dropped, asked); + outcome.refuse(groups.clone(), Error::Dropped, asked, true); } } } From 1d7afe4578b9d94437b0d36c56a976c53f328466 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 20 Nov 2026 01:49:13 -0800 Subject: [PATCH 09/14] wip: archive store failures fail a time range; quest and docs --- doc/lib/js/net.md | 2 +- quest/m5/archive-time-followups.md | 40 ++++++++++------------------ rs/moq-archive/src/consumer/tests.rs | 35 ++++++++++++++++++++++++ rs/moq-archive/src/consumer/track.rs | 4 +-- 4 files changed, 52 insertions(+), 29 deletions(-) diff --git a/doc/lib/js/net.md b/doc/lib/js/net.md index 64510d8ae0..e5bae61afe 100644 --- a/doc/lib/js/net.md +++ b/doc/lib/js/net.md @@ -53,7 +53,7 @@ route.announce(); - **One receive estimate per connection.** `connection.viewers` divides it among watched broadcasts, max-min fair by each viewer's ceiling, so several players on one pooled connection don't each select against the whole downlink. Holding a viewer requests PROBE, and the viewers' ceilings, summed, are the PROBE target a relay advertising `Increase` probes the downlink up to. `setTarget(0)` opts out. `watch`, `update`, and `setTarget` throw a `RangeError` when the ceilings or the target would pass `Viewers.MAX_TARGET` (`Number.MAX_SAFE_INTEGER`), the most a PROBE target carries. - **List a broadcast's tracks.** `for await (const event of broadcast.tracks())` reports each track the broadcast lists (`added`, with its `Track.Info`), one `synced`, then `added` and `removed` as tracks come and go, until the broadcast ends; leaving the loop stops listing. Over a session it needs `moq-lite-07-wip`, and syncs when the peer does, so a track added after `synced` is new at the source. A track served only on request is never listed. See [discovery](/concept/moq-lite#discovery). - **One track per name.** Concurrent subscriptions share one request and producer. `createTrack` on a name with a request still queued answers it. Groups are addressed by their start: `appendGroup(start)` opens one at a `Time.Timestamp`, in the track's timescale (rounded down), past every group the track holds, and declares the starts between it and the previous append skipped. The broadcast keeps nothing of an ended track, and a name always means the same content, so a track that replaces it keeps its starts moving forward, as its media time does. Starts are `bigint`, so every ID the wire carries fits, past 2^53 included: `track.createGroup(5n)`, `{ time: 10n }`. A frame's position in its group is its `index`, a `number`. -- **Serve history on request.** `track.requests()` opens a handle that serves the groups readers miss: ones their ranges name below the newest group written that the cache no longer holds, or never did. Each run of misses arrives as a `Track.FillGroups` from `await requests.requestedFill()`, shared by every reader missing it. `accept(start)` it into the cache and write the group's frames as first published, `dropGroups(start, end)` what the source lacks (starts are `bigint`), and serve `wanted()` until it is `undefined`: a reader that leaves or narrows stops wanting its groups. `close()` the fill when done; whatever is still wanted is named dropped. A `fetchGroup` of an uncached group asks the handle at once, even for a group not written yet, which a live write then answers; with no handle open it fails with `NotFound`, as in Rust, and a reader's miss is given up. A reader only asks for what its `delay.max` still wants, so a reader at the live edge never asks. A range starting at a `time` no cached group places asks for the group containing it as a fill one key wide from that time, whose `floor` says how far below it that group may start: a handler that knows its groups only by key serves the key as any other, one with an index may `accept` the group below it, and one that can't tell refuses it, so the reader falls back to the groups cached. This serves lite FETCH and lite-07 ranged subscriptions from the same track, as Rust's `requests()` does. A moq-transport FETCH is refused, so a moq-transport reader never reaches it. +- **Serve history on request.** `track.requests()` opens a handle that serves the groups readers miss: ones their ranges name below the newest group written that the cache no longer holds, or never did. Each run of misses arrives as a `Track.FillGroups` from `await requests.requestedFill()`, shared by every reader missing it. `accept(start)` it into the cache and write the group's frames as first published, `dropGroups(start, end)` what the source lacks (starts are `bigint`), and serve `wanted()` until it is `undefined`: a reader that leaves or narrows stops wanting its groups. `close()` the fill when done; whatever is still wanted is named dropped. A `fetchGroup` of an uncached group asks the handle at once, even for a group not written yet, which a live write then answers; with no handle open it fails with `NotFound`, as in Rust, and a reader's miss is given up. A reader only asks for what its `delay.max` still wants, so a reader at the live edge never asks. A range starting at a `time` no cached group places asks for the group containing it as a fill one key wide from that time, whose `floor` says how far below it that group may start: a handler that knows its groups only by key serves the key as any other, one with an index may `accept` the group below it or `dropGroups` the key when no group starts from the floor through the time, and the reader starts where the answer says, never where the cached groups happen to. A refusal fails the reader with its error until its ranges change. This serves lite FETCH and lite-07 ranged subscriptions from the same track, as Rust's `requests()` does. A moq-transport FETCH is refused, so a moq-transport reader never reaches it. - **Timing is per track, with no default.** A track that declares a `timescale` carries a `timestamp` on every frame; one without is [untimed](/concept/moq-lite#subscriptions) and each frame carries `Time.Timestamp.unscaled(group.start)`, its group's key, which has no time: `as`, `asMillis`, and `asMicros` throw on it. A scaled timestamp on an untimed track, an unscaled one on a timed track, or another key throws `TimestampMismatch`. Omitting it means untimed, as in Rust. Nothing stamps a frame for you: the publisher passes the timestamp, and the `@moq/json` producers take it beside each value, as `update(value, at)`. - **Durations and instants are `Clock` types.** Every duration (`maxAge`, `delay`, timeouts, RTT) is a `Clock.Duration` and every monotonic time a `Clock.Instant`, from [`@moq/clock`](./clock), which `@moq/net` re-exports as `Clock`: `maxAge: Moq.Clock.Duration.secs(30)`. Neither converts to a number implicitly; read one with an explicit unit, such as `asMillis()`. The wire still carries whole milliseconds, rounded up. Frame timestamps stay `Time.Timestamp`, a tick count at the track's timescale. - **A subscription's `delay` is a `Track.Delay { min, max }`, as in Rust.** Staleness is `delay.max`: it counts from a group's successor, by media time and by wall clock, whichever reaches it first. `delay.min` is the target latency, carried on moq-lite 07; a peer that cannot carry it leaves it `null` (not stated). Passing the old `maxAge` or `maxDelay` key throws a `TypeError` naming `delay.max`. The publisher's own staleness bound is the separate `Track.Info.maxAge`, which caps `delay.max` and fetches. diff --git a/quest/m5/archive-time-followups.md b/quest/m5/archive-time-followups.md index 65439b5f8e..cb2171d0d7 100644 --- a/quest/m5/archive-time-followups.md +++ b/quest/m5/archive-time-followups.md @@ -1,15 +1,12 @@ -# [M] A failed archive time lookup fails the subscription +# [M] Archive time ranges: late handlers, binding floors, and time ends ## Goal -When an `archive::Consumer` cannot answer a `Time(pts)` start because a -store GET fails or an object does not decode, the subscription fails with a -typed error naming the failure instead of silently falling back to the -cache. A time range on a history-served track also counts an unverified -group's start even when its handler registered after the group's first -frame, and moq-ffi plus the bindings expose a fill's `floor` -(`track::FillGroups::floor`), so a binding-side handler can answer a time -start with the group below it. +A time range on a history-served track counts an unverified group's start +even when its handler registered after the group's first frame, moq-ffi plus +the bindings expose a fill's `floor` (`track::FillGroups::floor`), so a +binding-side handler can answer a time start with the group below it, and a +long time end's cost against the history is measured. ## Plan @@ -20,19 +17,12 @@ which added the time lookup and finished the archive time ranges quest. [fill-lookups](https://github.com/moq-dev/moq/pull/9228) since made the lookup an ordinary group fill: a time start's fill is one key wide from the time, and `FillGroups::floor` says how far below it the group containing the time may -start. +start. That PR also made a refused time start fail its reader with the +handler's error, and a dropped one start below its floor, never from what the +cache holds, so a store failure in `archive::Consumer` already fails the +subscription. -- What a lookup's storage or decode failure does: - - Silent cache fallback, as [moq-dev/moq#7310](https://github.com/moq-dev/moq/pull/7310) documents with a warning. - - ✅ Refuse + report. The archive already refuses a time start's fill - when the store fails (`FillGroups::refuse_groups`); make that refusal - fail the reader's span, so the subscription ends with a typed error, - instead of falling back to the groups cached. No retry here: the store - client already retries. Reason: a cache fallback silently serves the - wrong range, which a DVR seek cannot tell from success. - - Retry the lookup in moq-net. - -Also, from [moq-dev/moq#7310](https://github.com/moq-dev/moq/pull/7310)'s follow-ups: +From [moq-dev/moq#7310](https://github.com/moq-dev/moq/pull/7310)'s follow-ups: - Unverified starts are indexed when a group's first frame is written, using whether a history-serving handler exists then. A handler registered @@ -46,12 +36,10 @@ Also, from [moq-dev/moq#7310](https://github.com/moq-dev/moq/pull/7310)'s follow long range against the in-memory store; if DVR seeks need it, look the end up as well. Otherwise record the cost in the `FillGroups::floor` docs. -Tests: a failing GET and a malformed object each fail the subscription with -the typed error; a handler registered after a group started still resolves -a time range over it; an FFI handler answers a lookup. +Tests: a handler registered after a group started still resolves a time +range over it; an FFI handler answers a lookup. -Public API: a refused time start fails its reader; moq-ffi's fill gains -`floor()`. Wire: none. +Public API: moq-ffi's fill gains `floor()`. Wire: none. ## Related diff --git a/rs/moq-archive/src/consumer/tests.rs b/rs/moq-archive/src/consumer/tests.rs index c0cc6530a2..6709d5ea35 100644 --- a/rs/moq-archive/src/consumer/tests.rs +++ b/rs/moq-archive/src/consumer/tests.rs @@ -804,6 +804,41 @@ async fn a_time_range_resolves_from_the_archive() { } } +/// A time start whose lookup fails at the store, by a failing GET or an object that doesn't +/// decode, fails the subscription with that failure, rather than starting where the groups +/// cached happen to say. +#[tokio::test(start_paused = true)] +async fn a_failed_time_lookup_fails_the_subscription() { + for malformed in [false, true] { + let archive = Archive::new(); + archive.track("video", 0, true).await; + stored(&archive, 0..10).await; + + let (broadcast, _run) = serve(&archive.store).await; + // The object holding groups 4 and 5. + let object = "video/0000000000000004000-0000000000000000000"; + match malformed { + true => archive.raw(object, b"x").await, + false => drop(archive.store.inner().fail_gets(&format!("rec/{object}"))), + } + let track = broadcast.consume().track("video").unwrap(); + let subscription = Subscription::default() + .with_ranges([track::Range { + start: at(4_500), + end: None, + }]) + .with_order(Order::Ascending) + .with_delay(moq_net::track::Delay::default().with_max(None)); + let mut reader = track.subscribe(subscription).await.unwrap().ordered(); + let read = timeout(5 * REFRESH, reader.next_group()).await.expect("answered"); + let err = read.map(|group| group.map(|group| group.start)).unwrap_err(); + assert!( + matches!(err, moq_net::Error::Stream(moq_net::StreamError::Internal)), + "malformed {malformed}: {err}" + ); + } +} + /// 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)] diff --git a/rs/moq-archive/src/consumer/track.rs b/rs/moq-archive/src/consumer/track.rs index 1f5d1aa810..2732f079a7 100644 --- a/rs/moq-archive/src/consumer/track.rs +++ b/rs/moq-archive/src/consumer/track.rs @@ -155,8 +155,8 @@ pub(super) async fn serve(shared: Arc>, index: Arc(shared: &Shared, index: &Mutex, fill: &FillGroups, pts: u64) { loop { match search(shared, index, pts).await { From a55685293e904b527664c95a8ed803558f897b4e Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 20 Nov 2026 02:02:40 -0800 Subject: [PATCH 10/14] style: rustfmt --- rs/moq-net/src/model/subscription/window.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index 8686974693..649330cc8b 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -1497,7 +1497,8 @@ mod tests { // 4000 evicted while the lookup was out, or 8000 arrived between, as another reader's // fill brings it. for held in [&[9000][..], &[4000, 8000, 9000]] { - let mut window = Window::new(&[range(time(8500), None)], None, Some(9000), &history(&[4000, 9000])).unwrap(); + let mut window = + Window::new(&[range(time(8500), None)], None, Some(9000), &history(&[4000, 9000])).unwrap(); assert_eq!(window.lookups().collect::>(), [(8500, 4001)]); assert!(window.found(8500, Found::Dropped { floor: 4001 }, &history(held))); assert_eq!(window.lookups().count(), 0, "answered, not looked up again"); From c2d3fb3e92ede4751c2bf656cd08552e597b3d96 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 20 Nov 2026 02:24:02 -0800 Subject: [PATCH 11/14] fix: a C handler's rejected time start reads on; a refused start's rebuild keeps Range IDs --- js/net/src/fill.test.ts | 10 ++++++- js/net/src/track.ts | 10 ++++--- rs/libmoq/src/api.rs | 6 ++-- rs/libmoq/src/publish.rs | 13 +++++--- rs/libmoq/src/test.rs | 65 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 93 insertions(+), 11 deletions(-) diff --git a/js/net/src/fill.test.ts b/js/net/src/fill.test.ts index 21176c8529..c82868227e 100644 --- a/js/net/src/fill.test.ts +++ b/js/net/src/fill.test.ts @@ -1773,12 +1773,20 @@ test("fill: a refused time start fails its reader until its ranges change", asyn await expect(next).rejects.toBe(refusal); await expect(ordered.nextGroup()).rejects.toBe(refusal); + // An edit replacing it resolves every range again, each by its Range ID. const times = lookups(requests, (ms) => ms); - reader.update(fromTime(3n)); + hooks.editSubscriber(reader, { + retire: [0n], + add: [[1n, { start: { time: 3n } }]], + priority: 0, + order: "ascending", + delay: { max: null }, + }); const got: bigint[] = []; for (let i = 0; i < 3; i++) got.push((await ordered.nextGroup())?.start ?? -1n); expect(got).toEqual([3n, 4n, 5n]); expect(times).toEqual([3n]); + expect(hooks.servingResolvedKeys(reader, [1n])[0]?.start).toBe(3n); reader.close(); requests.close(); diff --git a/js/net/src/track.ts b/js/net/src/track.ts index c750b11afd..0f0b9755a5 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -3585,13 +3585,15 @@ export class Subscriber { // Resolve this reader's ranges again, as when they are replaced. #resolve(): void { - const update = this.#state.update.peek(); + const update = this.#state.update; + const scalars = update.scalars(); this.#refusal = undefined; - this.#window = new Window( - update?.ranges ?? [{}], + // By the keys the ranges hold, which an edit's Range IDs set, as the Rust `Change::All` has it. + this.#window = Window.keyed( + scalars ? update.entries() : [[0n, {}]], this.#latestGroup(), this.#holdings(), - update?.delay?.min ?? undefined, + scalars?.delay.min ?? undefined, ); // Leaves the lookups the old ranges asked, which a settled window never reads again. if (this.#lookups.length > 0) this.#lookUp(); diff --git a/rs/libmoq/src/api.rs b/rs/libmoq/src/api.rs index d7ab441ac7..e712e00b02 100644 --- a/rs/libmoq/src/api.rs +++ b/rs/libmoq/src/api.rs @@ -4166,8 +4166,10 @@ pub extern "C" fn moq_group_request_accept(request: u32) -> i32 { /// Reject a group request, failing the readers waiting on it now as not found. /// -/// A later reader of the group asks again. The error code is not delivered. Consumes the -/// request handle. Returns a zero on success, or a negative code on failure. +/// A later reader of the group asks again. A request for a reader's time start, a time no +/// cached group places, says instead that no group starts there, so that reader starts at the +/// group below it. The error code is not delivered. Consumes the request handle. Returns a +/// zero on success, or a negative code on failure. #[unsafe(no_mangle)] pub extern "C" fn moq_group_request_abort(request: u32, error_code: u16) -> i32 { ffi::enter(move || { diff --git a/rs/libmoq/src/publish.rs b/rs/libmoq/src/publish.rs index a15e9e09f8..edbcaa6d9d 100644 --- a/rs/libmoq/src/publish.rs +++ b/rs/libmoq/src/publish.rs @@ -1197,12 +1197,17 @@ impl Publish { } /// Drop a group request, failing the readers waiting on it as not found. The group is - /// refused rather than dropped, so a later reader asks again. + /// refused rather than dropped, so a later reader asks again. A reader's time start is + /// dropped instead: a C handler knows its groups only by key, so one lacking the time's key + /// says no group starts there, and its reader starts at the group below rather than failing. pub fn group_request_free(&mut self, request: Id) -> Result<(), Error> { let request = self.group_request.remove(request).ok_or(Error::NotFound)?; - request - .fill - .refuse_groups(request.start..request.start + 1, moq_net::Error::NotFound); + let fill = &request.fill; + let groups = request.start..request.start + 1; + match fill.floor() < fill.start().group { + true => fill.drop_groups(groups), + false => fill.refuse_groups(groups, moq_net::Error::NotFound), + } Ok(()) } diff --git a/rs/libmoq/src/test.rs b/rs/libmoq/src/test.rs index c32f00b567..9adcc3cf3b 100644 --- a/rs/libmoq/src/test.rs +++ b/rs/libmoq/src/test.rs @@ -5457,6 +5457,71 @@ fn a_group_request_nobody_reads_is_withdrawn() { assert_eq!(moq_origin_close(origin), 0); } +/// A C handler knows its groups only by key, so rejecting a reader's time start says no group +/// starts at that time: the reader reads on from it rather than failing. +#[test] +fn a_rejected_time_start_reads_on() { + let origin = id(moq_origin_create()); + let broadcast = publish_broadcast(origin, b"track-dynamic-time-start"); + let name = b"data"; + let track = + id(unsafe { moq_publish_track(broadcast, name.as_ptr() as *const c_char, name.len(), std::ptr::null()) }); + let group_cb = Callback::new(); + let dynamic = id(unsafe { moq_publish_track_requests(track, Some(channel_callback), group_cb.ptr) }); + + let mut reader = { + let mut state = State::lock(); + let producer = state.publish.track_producer(Id::try_from(track).unwrap()).unwrap(); + let reader = producer.subscribe( + moq_net::track::Subscription::default() + .with_delay(moq_net::track::Delay::default().with_max(None)) + .with_ranges([moq_net::track::Range { + start: moq_net::track::Anchor::Time(7), + end: None, + }]), + ); + producer.start_at(10).unwrap(); + let mut group = producer.create_group(moq_net::group::Info { start: 10 }).unwrap(); + group + .write_frame(moq_net::Timestamp::from_millis(10).unwrap(), &b"live"[..]) + .unwrap(); + group.finish().unwrap(); + reader + }; + let request = id(group_cb.recv_reading(&mut reader)); + let mut start = 0u64; + assert_eq!(unsafe { moq_group_request_start(request, &mut start) }, 0); + assert_eq!(start, 7); + assert_eq!(moq_group_request_free(request), 0); + // The keys from the time up to the live group are asked as misses, rejected too. + let waiter = kio::Waiter::noop(); + let group = loop { + match reader.poll_recv_group(&waiter) { + std::task::Poll::Ready(Ok(Some(group))) => break group, + std::task::Poll::Ready(res) => panic!("the reader reads on: {:?}", res.map(|group| group.map(|group| group.start))), + std::task::Poll::Pending => {} + } + if let Ok(code) = group_cb.rx.recv_timeout(std::time::Duration::from_millis(10)) { + assert_eq!(moq_group_request_free(id(code)), 0); + } + }; + assert_eq!(group.start, 10); + + drop(reader); + assert_eq!(moq_publish_requests_cancel(dynamic), 0); + // A miss asked before the reader left may still be queued ahead of the terminal. + let terminal = loop { + match group_cb.recv() { + code if code > 0 => drop(moq_group_request_free(id(code))), + code => break code, + } + }; + assert_eq!(terminal, 0); + assert_eq!(moq_publish_track_finish(track), 0); + assert_eq!(moq_publish_close(broadcast), 0); + assert_eq!(moq_origin_close(origin), 0); +} + /// Rejecting a group request fails only the readers waiting on it: the group is not named /// dropped, so a later subscriber asks again. #[test] From 101f3fa4d9b5456566658a69721b357d59cc5283 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 20 Nov 2026 02:30:29 -0800 Subject: [PATCH 12/14] style: rustfmt --- rs/libmoq/src/test.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/rs/libmoq/src/test.rs b/rs/libmoq/src/test.rs index 9adcc3cf3b..5fa17cad0a 100644 --- a/rs/libmoq/src/test.rs +++ b/rs/libmoq/src/test.rs @@ -5498,7 +5498,10 @@ fn a_rejected_time_start_reads_on() { let group = loop { match reader.poll_recv_group(&waiter) { std::task::Poll::Ready(Ok(Some(group))) => break group, - std::task::Poll::Ready(res) => panic!("the reader reads on: {:?}", res.map(|group| group.map(|group| group.start))), + std::task::Poll::Ready(res) => panic!( + "the reader reads on: {:?}", + res.map(|group| group.map(|group| group.start)) + ), std::task::Poll::Pending => {} } if let Ok(code) = group_cb.rx.recv_timeout(std::time::Duration::from_millis(10)) { From c4af3e1e50b373435159326b16e5004c3080cbca Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 20 Nov 2026 03:04:55 -0800 Subject: [PATCH 13/14] fix(net): a time start's fill says it is one; an answered lookup admits the groups the walk passed FillGroups::is_time_start tells a time start whose floor is its own time from a key fill, so libmoq drops a C handler's rejection of either. A lookup answered during a read's probe now admits the cached groups the window newly covers, which nothing woke the read for once the window settled. Co-Authored-By: Claude Opus 5.5 --- js/net/src/fill.test.ts | 36 +++++ .../publish-stuck-clock-delta.md | 13 ++ rs/libmoq/src/publish.rs | 2 +- rs/libmoq/src/test.rs | 148 ++++++++++-------- rs/moq-net/src/model/track.rs | 51 +++++- rs/moq-net/src/model/track/fill.rs | 27 ++-- 6 files changed, 203 insertions(+), 74 deletions(-) create mode 100644 quest/m0/test-flakes-3/publish-stuck-clock-delta.md diff --git a/js/net/src/fill.test.ts b/js/net/src/fill.test.ts index c82868227e..49e762bb49 100644 --- a/js/net/src/fill.test.ts +++ b/js/net/src/fill.test.ts @@ -1757,6 +1757,42 @@ test("fill: a time start dropped from 0 starts the range at its time, asking not broadcast.close(); }); +test("fill: a time start just past a held group, dropped, starts at that group", async () => { + // Mirrors the Rust `a_time_start_just_past_a_held_group_drops_to_it`: the floor is the time + // itself, so the fill asks only its own key, and its drop starts the reader at group 4, which the + // walk passed while the lookup was out. + for (const ordered of [false, true]) { + const broadcast = new BroadcastProducer(); + const track = defined(broadcast.createTrack("video", { timescale: Timescale.MILLI, maxAge: null })); + for (const n of [4n, 10n]) { + const group = track.createGroup(n); + group.writeFrame({ payload: enc.encode(`g${n}`), timestamp: Timestamp.fromMillis(Number(n)) }); + group.finish(); + } + const requests = track.requests(); + const floors: bigint[][] = []; + void (async () => { + for (let fill = await requests.requestedFill(); fill; fill = await requests.requestedFill()) { + floors.push([fill.start, fill.floor]); + fill.dropGroups(fill.start, fill.end); + fill.close(); + } + })(); + const reader = track.subscribe(fromTime(5n)); + const got: bigint[] = []; + if (ordered) { + const keys = reader.ordered(); + for (let i = 0; i < 2; i++) got.push((await keys.nextGroup())?.start ?? -1n); + } else for (let i = 0; i < 2; i++) got.push((await reader.recvGroup())?.start ?? -1n); + expect(got).toEqual([4n, 10n]); + expect(floors[0]).toEqual([5n, 5n]); + + reader.close(); + requests.close(); + broadcast.close(); + } +}); + test("fill: a refused time start fails its reader until its ranges change", async () => { const { broadcast, track } = timed(); const requests = track.requests(); diff --git a/quest/m0/test-flakes-3/publish-stuck-clock-delta.md b/quest/m0/test-flakes-3/publish-stuck-clock-delta.md new file mode 100644 index 0000000000..3d80c9a537 --- /dev/null +++ b/quest/m0/test-flakes-3/publish-stuck-clock-delta.md @@ -0,0 +1,13 @@ +# [XS] A stuck source clock's arrival deltas are exact + +## Goal + +`js/publish/src/video/processor.test.ts`'s "falls back to arrival time when the source clock is stuck" passes however the wall clock reads when it runs. + +## Plan + +It failed once in a loaded `just check` on [moq-dev/moq#9228](https://github.com/moq-dev/moq/pull/9228), which touches no `@moq/publish` code, with `Expected: 10000` and `Received: 9999.999999999767`, and passed on its own right after. +The fake worker stamps each frame's arrival as `origin().asUnixMillis() + #at + frames * arrivalStep` (line 48), with `#at` a fractional millisecond read from `now()`, so the 10ms steps sit on a float base near 1.7e12 and their microsecond deltas lose precision depending on that base. +Freeze the base on a value the steps add to exactly, such as a whole millisecond or a mocked `origin()`, rather than loosening the assertion with `toBeCloseTo`; then check whether `TrackProcessor` itself turns arrival milliseconds into microseconds in a way a real browser clock trips over the same way, and fix it there if it does. + +Public API: none. Wire: none. diff --git a/rs/libmoq/src/publish.rs b/rs/libmoq/src/publish.rs index edbcaa6d9d..2d39c1697d 100644 --- a/rs/libmoq/src/publish.rs +++ b/rs/libmoq/src/publish.rs @@ -1204,7 +1204,7 @@ impl Publish { let request = self.group_request.remove(request).ok_or(Error::NotFound)?; let fill = &request.fill; let groups = request.start..request.start + 1; - match fill.floor() < fill.start().group { + match fill.is_time_start() { true => fill.drop_groups(groups), false => fill.refuse_groups(groups, moq_net::Error::NotFound), } diff --git a/rs/libmoq/src/test.rs b/rs/libmoq/src/test.rs index 5fa17cad0a..9bbd146f1b 100644 --- a/rs/libmoq/src/test.rs +++ b/rs/libmoq/src/test.rs @@ -5458,71 +5458,95 @@ fn a_group_request_nobody_reads_is_withdrawn() { } /// A C handler knows its groups only by key, so rejecting a reader's time start says no group -/// starts at that time: the reader reads on from it rather than failing. +/// starts at that time: the reader reads on from it, or from the group held just below it, +/// rather than failing. That includes a time just past a held group, whose fill asks only the +/// time's own key. #[test] fn a_rejected_time_start_reads_on() { - let origin = id(moq_origin_create()); - let broadcast = publish_broadcast(origin, b"track-dynamic-time-start"); - let name = b"data"; - let track = - id(unsafe { moq_publish_track(broadcast, name.as_ptr() as *const c_char, name.len(), std::ptr::null()) }); - let group_cb = Callback::new(); - let dynamic = id(unsafe { moq_publish_track_requests(track, Some(channel_callback), group_cb.ptr) }); - - let mut reader = { - let mut state = State::lock(); - let producer = state.publish.track_producer(Id::try_from(track).unwrap()).unwrap(); - let reader = producer.subscribe( - moq_net::track::Subscription::default() - .with_delay(moq_net::track::Delay::default().with_max(None)) - .with_ranges([moq_net::track::Range { - start: moq_net::track::Anchor::Time(7), - end: None, - }]), - ); - producer.start_at(10).unwrap(); - let mut group = producer.create_group(moq_net::group::Info { start: 10 }).unwrap(); - group - .write_frame(moq_net::Timestamp::from_millis(10).unwrap(), &b"live"[..]) - .unwrap(); - group.finish().unwrap(); - reader - }; - let request = id(group_cb.recv_reading(&mut reader)); - let mut start = 0u64; - assert_eq!(unsafe { moq_group_request_start(request, &mut start) }, 0); - assert_eq!(start, 7); - assert_eq!(moq_group_request_free(request), 0); - // The keys from the time up to the live group are asked as misses, rejected too. - let waiter = kio::Waiter::noop(); - let group = loop { - match reader.poll_recv_group(&waiter) { - std::task::Poll::Ready(Ok(Some(group))) => break group, - std::task::Poll::Ready(res) => panic!( - "the reader reads on: {:?}", - res.map(|group| group.map(|group| group.start)) - ), - std::task::Poll::Pending => {} - } - if let Ok(code) = group_cb.rx.recv_timeout(std::time::Duration::from_millis(10)) { - assert_eq!(moq_group_request_free(id(code)), 0); - } - }; - assert_eq!(group.start, 10); + for (time, held, first) in [(7, &[10][..], 10), (5, &[4, 10][..], 4)] { + let origin = id(moq_origin_create()); + let broadcast = publish_broadcast(origin, b"track-dynamic-time-start"); + let name = b"data"; + // Timed in milliseconds, with no publisher age limit, so the groups behind the live one + // stay fresh. + let info = moq_track_info { + priority: 0, + max_age_us: 0, + max_age_unlimited: true, + timescale: 1000, + timescale_present: true, + refresh: 0, + refresh_present: false, + refresh_interval_us: 0, + refresh_interval_present: false, + compression: 0, + }; + let track = id(unsafe { moq_publish_track(broadcast, name.as_ptr() as *const c_char, name.len(), &info) }); + let group_cb = Callback::new(); + let dynamic = id(unsafe { moq_publish_track_requests(track, Some(channel_callback), group_cb.ptr) }); - drop(reader); - assert_eq!(moq_publish_requests_cancel(dynamic), 0); - // A miss asked before the reader left may still be queued ahead of the terminal. - let terminal = loop { - match group_cb.recv() { - code if code > 0 => drop(moq_group_request_free(id(code))), - code => break code, - } - }; - assert_eq!(terminal, 0); - assert_eq!(moq_publish_track_finish(track), 0); - assert_eq!(moq_publish_close(broadcast), 0); - assert_eq!(moq_origin_close(origin), 0); + // Subscribed once the groups are held, which then place the time or ask for it. + let mut reader = { + let mut state = State::lock(); + let producer = state.publish.track_producer(Id::try_from(track).unwrap()).unwrap(); + producer.start_at(held[0]).unwrap(); + for &key in held { + let mut group = producer.create_group(moq_net::group::Info { start: key }).unwrap(); + group + .write_frame(moq_net::Timestamp::from_millis(key).unwrap(), &b"live"[..]) + .unwrap(); + group.finish().unwrap(); + } + producer.subscribe( + moq_net::track::Subscription::default() + .with_delay(moq_net::track::Delay::default().with_max(None)) + .with_order(moq_net::track::Order::Ascending) + .with_ranges([moq_net::track::Range { + start: moq_net::track::Anchor::Time(time), + end: None, + }]), + ) + }; + // Nothing is read before the time start is answered. + let waiter = kio::Waiter::noop(); + assert!(reader.poll_recv_group(&waiter).is_pending()); + let request = id(group_cb.recv_reading(&mut reader)); + let mut start = 0u64; + assert_eq!(unsafe { moq_group_request_start(request, &mut start) }, 0); + assert_eq!(start, time); + assert_eq!(moq_group_request_free(request), 0); + // The keys after it up to the next group held are asked as misses, rejected too. + let group = loop { + match reader.poll_recv_group(&waiter) { + std::task::Poll::Ready(Ok(Some(group))) => break group, + std::task::Poll::Ready(res) => { + panic!( + "the reader reads on: {:?}", + res.map(|group| group.map(|group| group.start)) + ) + } + std::task::Poll::Pending => {} + } + if let Ok(code) = group_cb.rx.recv_timeout(std::time::Duration::from_millis(10)) { + assert_eq!(moq_group_request_free(id(code)), 0); + } + }; + assert_eq!(group.start, first, "time {time}"); + + drop(reader); + assert_eq!(moq_publish_requests_cancel(dynamic), 0); + // A miss asked before the reader left may still be queued ahead of the terminal. + let terminal = loop { + match group_cb.recv() { + code if code > 0 => drop(moq_group_request_free(id(code))), + code => break code, + } + }; + assert_eq!(terminal, 0); + assert_eq!(moq_publish_track_finish(track), 0); + assert_eq!(moq_publish_close(broadcast), 0); + assert_eq!(moq_origin_close(origin), 0); + } } /// Rejecting a group request fails only the readers waiting on it: the group is not named diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index eb5cbf882d..7d2378cdda 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -9939,6 +9939,9 @@ impl Cursor { if let Some(err) = &self.filter.refused { return Poll::Ready(Err(err.clone())); } + // Or answered, moving the window over groups the walk passed while it read nothing, + // which nothing wakes it for once the window settles. + self.admit(); if let Some(misses) = &mut self.misses { misses.pick_up(); } @@ -26081,6 +26084,51 @@ mod test { assert_eq!(drain_groups(&mut subscriber), [4]); } + /// A time start just past a held group asks only the time's own key, from a floor at the + /// time itself: still a time start, which a drop starts at the held group below. + #[test] + #[cfg(feature = "async")] + fn a_time_start_just_past_a_held_group_drops_to_it() { + for ordered in [false, true] { + let producer = track_producer("test", Info::timed()); + let dynamic = producer.requests(); + write_group(&producer, 4); + write_group(&producer, 10); + let subscriber = producer.subscribe(replay().with_order(Order::Ascending).with_ranges([Range { + start: Anchor::Time(5), + end: None, + }])); + let waiter = kio::Waiter::noop(); + let (mut arrival, mut keyed) = match ordered { + true => (None, Some(subscriber.ordered())), + false => (Some(subscriber), None), + }; + let mut read = || match (&mut arrival, &mut keyed) { + (Some(subscriber), _) => drain_groups(subscriber), + (_, Some(ordered)) => drain_ordered(ordered), + _ => unreachable!("one reader"), + }; + assert_eq!(read(), Vec::::new()); + let Poll::Ready(Ok(start)) = dynamic.poll_requested_fill(&waiter) else { + panic!("the time start asks for its group"); + }; + assert_eq!( + (start.start().group, start.floor(), start.is_time_start()), + (5, 5, true) + ); + start.drop_groups(5..6); + drop(start); + let mut got = read(); + while let Poll::Ready(Ok(fill)) = dynamic.poll_requested_fill(&waiter) { + assert!(!fill.is_time_start(), "a miss"); + fill.drop_groups(fill.start().group..fill.end()); + drop(fill); + got.extend(read()); + } + assert_eq!(got, [4, 10], "ordered {ordered}"); + } + } + /// A handler refusing a time start fails its reader with that refusal, rather than /// starting where the groups held happen to say, until its ranges change, which asks again. #[test] @@ -26126,7 +26174,8 @@ mod test { }; write_filled(start.accept(6, None), 6); drop(start); - assert_eq!(drain_groups(&mut subscriber), [6, 9]); + // In arrival order: 9 was cached before the fill brought 6. + assert_eq!(drain_groups(&mut subscriber), [9, 6]); } /// A time end walking the history a handler serves takes no group the live feed diff --git a/rs/moq-net/src/model/track/fill.rs b/rs/moq-net/src/model/track/fill.rs index 0bc412cdce..eaada91ab8 100644 --- a/rs/moq-net/src/model/track/fill.rs +++ b/rs/moq-net/src/model/track/fill.rs @@ -47,9 +47,9 @@ struct Pending { end: u64, // Exclusive frame cap in the last group; None asks through the original end. frame_end: Option, - // The oldest key the first group may start at: below `start` for a time start, whose - // group is the newest one starting at or before it. - floor: u64, + // For a time start, the oldest key its group, the newest starting at or before the + // time, may start at. + floor: Option, // What its joins ask with now, shared with them and its handler. asks: kio::Shared, queued: bool, @@ -602,7 +602,7 @@ impl FillState { start, end: groups.end, frame_end: if groups.end == ask.end { ask.frame_end } else { None }, - floor: start.group, + floor: None, asks, queued: true, flight: false, @@ -654,7 +654,7 @@ impl FillState { start: Position { group: key, frame }, end: groups.end, frame_end: None, - floor: key, + floor: None, asks: kio::Shared::new(asks), queued: true, flight: true, @@ -719,7 +719,7 @@ impl FillState { start: Position::group(time), end: groups.end, frame_end: None, - floor: floor.min(time), + floor: Some(floor.min(time)), asks, queued: true, flight: false, @@ -934,8 +934,8 @@ struct Inner { end: u64, // Exclusive frame cap in the last group; None asks through the original end. frame_end: Option, - // The oldest key the first group may start at; see [`FillGroups::floor`]. - floor: u64, + // A time start's floor; see [`FillGroups::floor`]. + floor: Option, // Answered with a copy of its own rather than the cache's; see [`FillState::fly`]. flight: bool, asks: kio::Shared, @@ -960,7 +960,14 @@ impl FillGroups { /// or past the time when the floor is 0. A [refusal](Self::refuse_groups) fails the reader /// with its error until its ranges change. pub fn floor(&self) -> u64 { - self.inner.floor + self.inner.floor.unwrap_or(self.inner.start.group) + } + + /// Whether it answers a reader's time start, whose first group is the one containing the + /// time from the [`floor`](Self::floor) through it, even when the floor is the time + /// itself: a drop then says no group starts there, and a refusal fails the reader. + pub fn is_time_start(&self) -> bool { + self.inner.floor.is_some() } /// Whether a [`Consumer::fetch_group`](super::Consumer::fetch_group) asked or joined it, @@ -1297,7 +1304,7 @@ impl FillGroups { impl Inner { fn contains(&self, key: u64) -> bool { - (self.floor..self.end).contains(&key) + (self.floor.unwrap_or(self.start.group)..self.end).contains(&key) } fn is_wanted(&self, key: u64) -> bool { From d105a5ec1b98e4b51a851e4a7f183b1e41c07fce Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 20 Nov 2026 03:20:24 -0800 Subject: [PATCH 14/14] fix(libmoq): an aborted time start fails its reader, a freed one is no group there Co-Authored-By: Claude Opus 5.5 --- rs/libmoq/src/api.rs | 15 +++++++---- rs/libmoq/src/publish.rs | 18 +++++++++----- rs/libmoq/src/test.rs | 54 ++++++++++++++++++++++++++++++++++++---- 3 files changed, 71 insertions(+), 16 deletions(-) diff --git a/rs/libmoq/src/api.rs b/rs/libmoq/src/api.rs index e712e00b02..2ec7b7e331 100644 --- a/rs/libmoq/src/api.rs +++ b/rs/libmoq/src/api.rs @@ -4164,12 +4164,13 @@ pub extern "C" fn moq_group_request_accept(request: u32) -> i32 { }) } -/// Reject a group request, failing the readers waiting on it now as not found. +/// Reject a group request, as serving it failed, failing the readers waiting on it now as not +/// found. /// /// A later reader of the group asks again. A request for a reader's time start, a time no -/// cached group places, says instead that no group starts there, so that reader starts at the -/// group below it. The error code is not delivered. Consumes the request handle. Returns a -/// zero on success, or a negative code on failure. +/// cached group places, fails that reader until its ranges change; free it instead when you +/// have no group there. The error code is not delivered. Consumes the request handle. Returns +/// a zero on success, or a negative code on failure. #[unsafe(no_mangle)] pub extern "C" fn moq_group_request_abort(request: u32, error_code: u16) -> i32 { ffi::enter(move || { @@ -4178,7 +4179,11 @@ pub extern "C" fn moq_group_request_abort(request: u32, error_code: u16) -> i32 }) } -/// Free a group request without accepting it, which rejects it like [moq_group_request_abort]. +/// Free a group request without accepting it, as you have no such group, which rejects it like +/// [moq_group_request_abort]. +/// +/// A request for a reader's time start says instead that no group starts at that time, so the +/// reader starts at the group held below it, or at the time when none is, rather than failing. /// /// Returns a zero on success, or a negative code if the handle is unknown. #[unsafe(no_mangle)] diff --git a/rs/libmoq/src/publish.rs b/rs/libmoq/src/publish.rs index 2d39c1697d..c4e65bbd77 100644 --- a/rs/libmoq/src/publish.rs +++ b/rs/libmoq/src/publish.rs @@ -1191,15 +1191,21 @@ impl Publish { self.insert_group(group) } - /// Reject a group request like [`Self::group_request_free`]; `error_code` is not delivered. + /// Refuse a group request, failing the readers waiting on it now as not found, so a later + /// reader asks again; `error_code` is not delivered. A reader's time start fails with it + /// until its ranges change, as serving it failed. pub fn group_request_abort(&mut self, request: Id, _error_code: u16) -> Result<(), Error> { - self.group_request_free(request) + let request = self.group_request.remove(request).ok_or(Error::NotFound)?; + request + .fill + .refuse_groups(request.start..request.start + 1, moq_net::Error::NotFound); + Ok(()) } - /// Drop a group request, failing the readers waiting on it as not found. The group is - /// refused rather than dropped, so a later reader asks again. A reader's time start is - /// dropped instead: a C handler knows its groups only by key, so one lacking the time's key - /// says no group starts there, and its reader starts at the group below rather than failing. + /// Free a group request unanswered: refused as [`Self::group_request_abort`] refuses it, + /// except a reader's time start, which is dropped. A C handler knows its groups only by + /// key, so one lacking the time's key says no group starts there, and its reader starts at + /// the group below rather than failing. pub fn group_request_free(&mut self, request: Id) -> Result<(), Error> { let request = self.group_request.remove(request).ok_or(Error::NotFound)?; let fill = &request.fill; diff --git a/rs/libmoq/src/test.rs b/rs/libmoq/src/test.rs index 9bbd146f1b..0f4ff1fedf 100644 --- a/rs/libmoq/src/test.rs +++ b/rs/libmoq/src/test.rs @@ -5457,13 +5457,18 @@ fn a_group_request_nobody_reads_is_withdrawn() { assert_eq!(moq_origin_close(origin), 0); } -/// A C handler knows its groups only by key, so rejecting a reader's time start says no group +/// A C handler knows its groups only by key, so freeing a reader's time start says no group /// starts at that time: the reader reads on from it, or from the group held just below it, /// rather than failing. That includes a time just past a held group, whose fill asks only the -/// time's own key. -#[test] -fn a_rejected_time_start_reads_on() { - for (time, held, first) in [(7, &[10][..], 10), (5, &[4, 10][..], 4)] { +/// time's own key. Aborting it instead says serving it failed, which fails the reader and +/// records nothing, so a later reader asks again. +#[test] +fn a_freed_time_start_reads_on_and_an_aborted_one_fails() { + for (time, held, first) in [ + (7, &[10][..], Some(10)), + (5, &[4, 10][..], Some(4)), + (7, &[10][..], None), + ] { let origin = id(moq_origin_create()); let broadcast = publish_broadcast(origin, b"track-dynamic-time-start"); let name = b"data"; @@ -5514,6 +5519,45 @@ fn a_rejected_time_start_reads_on() { let mut start = 0u64; assert_eq!(unsafe { moq_group_request_start(request, &mut start) }, 0); assert_eq!(start, time); + let Some(first) = first else { + assert_eq!(moq_group_request_abort(request, 9), 0); + for _ in 0..2 { + assert!( + matches!(reader.poll_recv_group(&waiter), std::task::Poll::Ready(Err(_))), + "not group 10, which the cache holds past the time" + ); + } + // Nothing recorded the time's key dropped, so a fresh reader asks for it again. + let mut again = { + let mut state = State::lock(); + let producer = state.publish.track_producer(Id::try_from(track).unwrap()).unwrap(); + producer.subscribe( + moq_net::track::Subscription::default() + .with_delay(moq_net::track::Delay::default().with_max(None)) + .with_ranges([moq_net::track::Range { + start: moq_net::track::Anchor::Time(time), + end: None, + }]), + ) + }; + let request = id(group_cb.recv_reading(&mut again)); + assert_eq!(unsafe { moq_group_request_start(request, &mut start) }, 0); + assert_eq!(start, time); + assert_eq!(moq_group_request_free(request), 0); + drop((reader, again)); + assert_eq!(moq_publish_requests_cancel(dynamic), 0); + let terminal = loop { + match group_cb.recv() { + code if code > 0 => drop(moq_group_request_free(id(code))), + code => break code, + } + }; + assert_eq!(terminal, 0); + assert_eq!(moq_publish_track_finish(track), 0); + assert_eq!(moq_publish_close(broadcast), 0); + assert_eq!(moq_origin_close(origin), 0); + continue; + }; assert_eq!(moq_group_request_free(request), 0); // The keys after it up to the next group held are asked as misses, rejected too. let group = loop {