diff --git a/quest/m0/README.md b/quest/m0/README.md index 847b9ef5562..fe50d085c55 100644 --- a/quest/m0/README.md +++ b/quest/m0/README.md @@ -41,7 +41,7 @@ a published `@moq/watch` break. ## Required -- [Expiry wakes](/quest/m0/expiry-wakes.md) - an appended group wakes only the parked reads it expires, so a 2.5 ms-frame publisher with 2 s of parked serves stays near idle and the Python and Go interop rows pass +- [Held group wakes](/quest/m0/held-group-wakes.md) - a group held across a route switch is woken when its successor's first timestamp or abort makes it stale, not at the next unrelated append - [Draft-22 LOCATION_FILTER](/quest/m0/ietf-location-filter-22.md) - moqt-22 LOCATION_FILTER carries its type instead of a Length in Rust and JS, so a draft-22 peer reads our Next Object correctly - [Request caps](/quest/m0/request-caps.md) - lite message sizes, IETF request IDs, and per-session announces and subscriptions are bounded - [Prefix route fronts](/quest/m0/prefix-route-fronts.md) - a prefix route cannot be made to mint one front per requested path diff --git a/quest/m0/expiry-wakes.md b/quest/m0/expiry-wakes.md deleted file mode 100644 index 04df6091aa6..00000000000 --- a/quest/m0/expiry-wakes.md +++ /dev/null @@ -1,66 +0,0 @@ -# [M] An appended group wakes only the parked reads it expires - -## Goal - -A group appended to a track wakes only the parked group serves whose expiry -it crosses, not every parked serve. A publisher sending one 2.5 ms Opus frame -per group to a subscriber with a 2 s max age (about 800 parked serves) keeps -its runtime thread near idle, and `just test interop --all` passes its -Python and Go publisher rows again. A group held across a route switch is -woken when it goes stale, not at the next unrelated append. - -## Plan - -Root cause, found 2026-10-06 while chasing the red Interop nightly: since -#4162 raised the browser's audio max age to 2 s, an FFI publisher's -subscription parks up to ~800 group serves. Every parked serve registers on -track-state changes and on the next unstamped group past the edge -(`GroupExpiry::is_expired` in `rs/moq-net/src/model/track.rs`, reached from -`group::Consumer::poll_expired_while_pending` in the lite and IETF -publishers' `GroupServe`). Each append wakes all N, so the single moq-ffi -runtime thread sits at ~110% CPU, QUIC idles out, and subscribers see -"internal error" or RESET_STREAM. #4892 made each check O(log N) but kept the -N-wide wake per append. It depends on load and build profile, so it fails on -unrelated branches. - -Decided 2026-10-06: deadline-indexed wakes. A parked read goes stale only -once the live edge passes its reach plus its budget, so keep parked reads in -a track-level map ordered by that point and wake only those the advancing -edge crosses. Keep the register-before-judge order that prevents lost wakes. -Rejected: skipping expiry checks while a serve waits for stream credit -(stale serves linger, lost-wakeup risk, fan-out stays elsewhere), capping -backlog serves per subscription (changes delivery scheduling), and lowering -the browser audio max age or using 20 ms frames in the interop clients -(hides the bug). - -Decided 2026-10-06, folding in #4950: the index has a second caller. -`Recover::poll` in `rs/moq-net/src/model/resume.rs` gives up a held group on -an old route through `Consumer::poll_stale`, which registers only on the -track state, so its successor's first timestamp (or abort, or a newer -group's first timestamp moving the edge) never wakes it. It is judged with -`drifted` against a copy that usually lacks the group, and its reach ends -where the successor's first timestamp lands. Recover registers the held -group in the same index as `GroupServe`, and stamping or aborting the -successor re-keys the entry. One mechanism, not a per-reader helper beside -the index. Two differences the index must carry: an entry can wait on its -successor's first stamp with no deadline yet, and Recover's entry lives in -the serving copy's index (a different track from the one holding the -group), so it moves when the route changes. - -Tests: - -- A mocked-time regression test that counts wakes: N parked serves, one - append, and only the crossed serves wake. It fails today. -- Sweep `track_parked_read` in `rs/moq-net/benches/track.rs` over parked - readers, so a per-append cost that grows with N shows up as a slope. -- `just test interop --all` passes the Python and Go publisher rows on CI. -- #4950's `a_group_no_route_continues_wakes_when_its_successor_is_stamped` - in `resume.rs`: the successor's first timestamp wakes the held group, - which then reads `Error::Old`. It fails today; extend it to the successor - aborting and a newer group stamping the edge. - -Public API: none. Wire: none. - -## Closes - -- [#4950](https://github.com/moq-dev/moq/issues/4950) - a group held across a route switch is not woken when its successor gets its first timestamp diff --git a/quest/m0/held-group-wakes.md b/quest/m0/held-group-wakes.md new file mode 100644 index 00000000000..fc76d31537a --- /dev/null +++ b/quest/m0/held-group-wakes.md @@ -0,0 +1,31 @@ +# [S] A group held across a route switch wakes when it goes stale + +## Goal + +A group held across a route switch is woken when it goes stale: its +successor's first timestamp, its successor's abort, or a newer group's first +timestamp moving the edge. Today it waits for the next unrelated append. + +## Plan + +Split from the expiry-wakes quest, which landed the deadline index +(`model::expiry::Wakes`, owned by `cache::Track`) for `GroupServe` reads. + +`Recover::poll` in `rs/moq-net/src/model/resume.rs` gives up a held group on +an old route through `Consumer::poll_stale`, which registers only on the +track state. Register the held group in the same `Wakes` index instead. One +mechanism, not a per-reader helper beside the index. Two differences the +index must carry: an entry can wait on its successor's first stamp with no +deadline yet, and Recover's entry lives in the serving copy's index (a +different track from the one holding the group), so it moves when the route +changes. + +Tests: add #4950's `a_group_no_route_continues_wakes_when_its_successor_is_stamped` +to `resume.rs` (it would fail today), and extend it to the successor aborting +and a newer group stamping the edge. + +Public API: none. Wire: none. + +## Closes + +- [#4950](https://github.com/moq-dev/moq/issues/4950) - a group held across a route switch is not woken when its successor gets its first timestamp diff --git a/rs/moq-net/benches/track.rs b/rs/moq-net/benches/track.rs index 6a52f734e2b..7300f67e93e 100644 --- a/rs/moq-net/benches/track.rs +++ b/rs/moq-net/benches/track.rs @@ -19,6 +19,10 @@ //! `track_subscriber_join` measures a burst of viewers joining one track: each join //! registers in the subscription list, so any per-join walk of it shows up as a slope. //! +//! `track_parked_read` appends past a backlog of reads parked on their drift budget: +//! each append must wake only the reads it can expire, so waking the backlog shows up +//! as a slope over it. +//! //! Run with `cargo bench -p moq-net --bench track`. use std::hint::black_box; @@ -389,43 +393,85 @@ fn bench_subscriber_join(c: &mut Criterion) { group.finish(); } -/// Parked reads re-judge their reach, including the successor's abort waiter. -/// Sweep readers and cached groups so added per-reader scans show up as a slope. +/// Queues a parked read as woken, so the bench visits only what an append wakes. +struct Woken { + read: usize, + queue: Arc>>, +} + +impl std::task::Wake for Woken { + fn wake(self: Arc) { + self.wake_by_ref(); + } + + fn wake_by_ref(self: &Arc) { + self.queue.lock().unwrap().push(self.read); + } +} + +/// One append past a backlog of parked reads, one per group like a publisher's parked +/// group streams, re-polling whatever it wakes. Swept over the backlog so a per-append +/// cost that grows with the parked reads shows up as a slope. fn bench_parked_read(c: &mut Criterion) { let mut group = c.benchmark_group("track_parked_read"); - for cached in [8, 64, 512] { - for readers in [1, 8, 64] { - let broadcast = broadcast::Info::default().produce(); - let track = broadcast.create_track("bench", None).unwrap(); - let mut head = track.append_group().unwrap(); - head.write_frame(Timestamp::ZERO, Bytes::from_static(b"head")).unwrap(); - for _ in 1..cached { - let mut next = track.append_group().unwrap(); - next.write_frame(Timestamp::ZERO, Bytes::from_static(b"next")).unwrap(); - next.finish().unwrap(); - } - let waiters: Vec<_> = (0..readers).map(|_| kio::Waiter::noop()).collect(); - let mut held: Vec<_> = waiters - .iter() - .map(|waiter| { - let mut sub = - track.subscribe(track::Subscription::default().with_max_delay(Duration::from_secs(1))); - let Poll::Ready(Ok(Some(mut head))) = sub.poll_recv_group(waiter) else { - panic!("head is cached"); - }; - assert!(matches!(head.poll_read_frame(waiter), Poll::Ready(Ok(Some(_))))); - head - }) - .collect(); - group.throughput(Throughput::Elements(readers as u64)); - group.bench_function(BenchmarkId::new(format!("cached_{cached}"), readers), |b| { - b.iter(|| { - for (head, waiter) in held.iter_mut().zip(&waiters) { - assert!(black_box(head.poll_read_frame(waiter)).is_pending()); + group.throughput(Throughput::Elements(1)); + for parked in [8, 64, 512] { + group.bench_function(BenchmarkId::from_parameter(parked), |b| { + b.iter_custom(|iterations| { + let broadcast = broadcast::Info::default().produce(); + let track = broadcast.create_track("bench", None).unwrap(); + // Long enough that no read expires, so every append measures the same backlog. + let mut sub = track.subscribe(track::Subscription::default().with_max_delay(Duration::from_secs(3600))); + let mut micros = 0; + let mut open = Vec::with_capacity(parked); + let queue = Arc::new(std::sync::Mutex::new(Vec::new())); + let mut reads: Vec<_> = (0..parked) + .map(|read| { + let mut group = track.append_group().unwrap(); + group + .write_frame(Timestamp::from_micros(micros).unwrap(), Bytes::from_static(b"x")) + .unwrap(); + micros += 2500; + // Left open, so its read parks instead of ending. + open.push(group); + + let woken = Woken { + read, + queue: queue.clone(), + }; + let waiter = kio::Waiter::new(std::task::Waker::from(Arc::new(woken))); + let Poll::Ready(Ok(Some(mut read))) = sub.poll_recv_group(&waiter) else { + panic!("the group is cached"); + }; + assert!(matches!(read.poll_read_frame(&waiter), Poll::Ready(Ok(Some(_))))); + assert!(read.poll_read_frame(&waiter).is_pending()); + (waiter, read) + }) + .collect(); + + let mut woken = Vec::new(); + let mut repoll = |reads: &mut Vec<(kio::Waiter, moq_net::group::Consumer)>| { + std::mem::swap(&mut woken, &mut *queue.lock().unwrap()); + for read in woken.drain(..) { + let (waiter, read) = &mut reads[read]; + assert!(black_box(read.poll_read_frame(waiter)).is_pending()); } - }); + }; + // Each append above woke the reads before it: park them again. + repoll(&mut reads); + + let start = Instant::now(); + for _ in 0..iterations { + let mut next = track.append_group().unwrap(); + next.write_frame(Timestamp::from_micros(micros).unwrap(), Bytes::from_static(b"x")) + .unwrap(); + next.finish().unwrap(); + micros += 2500; + repoll(&mut reads); + } + start.elapsed() }); - } + }); } group.finish(); } diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 1283e6d89ff..94d8b688138 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -3493,7 +3493,7 @@ mod serve_group_test { relay.run.update(lite::SubscribeUpdate { priority: 0, - max_age: Duration::from_secs(30), + max_delay: Duration::from_secs(30), start_group: None, end_group: None, start_frame: 0, @@ -3519,7 +3519,7 @@ mod serve_group_test { relay.run.update(lite::SubscribeUpdate { priority: 0, - max_age: Duration::from_secs(30), + max_delay: Duration::from_secs(30), start_group: None, end_group: None, start_frame: 0, diff --git a/rs/moq-net/src/model/cache.rs b/rs/moq-net/src/model/cache.rs index 1eb2f410d2d..4b4b5f0d193 100644 --- a/rs/moq-net/src/model/cache.rs +++ b/rs/moq-net/src/model/cache.rs @@ -42,8 +42,8 @@ use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering}; use std::sync::{Arc, Mutex, OnceLock, Weak}; use std::time::Duration; -use super::group; use super::track::{self, TrackState}; +use super::{expiry, group}; /// Fixed bookkeeping charged per cached group on top of its frame payload bytes. /// @@ -498,6 +498,10 @@ pub(crate) struct Track { // This account's slot in the pool's sweep registry, absent when the pool has no // expiry window (nothing is registered) or for the detached default account. sweep: OnceLock, + + // The track's reads parked on their drift budget. Here because this account is the + // link every group's frame writes already follow back to the track. + wakes: expiry::Wakes, } impl Track { @@ -510,6 +514,7 @@ impl Track { expiry_cursor: AtomicUsize::new(0), state, sweep: OnceLock::new(), + wakes: Default::default(), }); if let Some(key) = track.pool.register(&track) { let _ = track.sweep.set(key); @@ -522,6 +527,11 @@ impl Track { &self.pool } + /// The track's reads parked on their drift budget. + pub(crate) fn wakes(&self) -> &expiry::Wakes { + &self.wakes + } + /// Charge a new group's fixed overhead, returning its [`Charge`]. pub(crate) fn charge(self: &Arc) -> Charge { self.pool.add(ENTRY_OVERHEAD); diff --git a/rs/moq-net/src/model/expiry.rs b/rs/moq-net/src/model/expiry.rs new file mode 100644 index 00000000000..29e4db40a97 --- /dev/null +++ b/rs/moq-net/src/model/expiry.rs @@ -0,0 +1,131 @@ +//! Wakes for group reads parked on their subscription's drift budget. + +use std::collections::BTreeMap; +use std::sync::atomic::{AtomicU64, Ordering}; +use std::time::Duration; + +use super::Timestamp; + +/// A track's parked group reads, indexed by the events that can expire them, so a frame +/// write or a group insert wakes only the reads whose verdict it could change. +/// +/// A parked read goes stale once the live edge reaches its deadline: where its immediate +/// successor starts, plus its budget. The edge only ever takes a timestamp some frame +/// write presented, so the write that crosses a deadline wakes the reads behind it and +/// nobody else. The successor itself is watched per group (its first frame, its abort), +/// except for a group landing between the read and it, which reads park on by sequence. +/// +/// Waking every parked read on every track change instead costs a backlog of N parked +/// reads N wakes per append: an audio backlog of ~800 one-frame groups pins a runtime. +pub(crate) struct Wakes { + /// The earliest parked deadline in floored micros, or `u64::MAX`, so a write that + /// crosses nothing skips the lock. + /// + /// Relaxed: a read registers before resolving the edge, under the group locks a + /// write takes before it presents, so a write the read missed sees its deadline. + earliest: AtomicU64, + parked: kio::Lock, +} + +#[derive(Default)] +struct Parked { + /// Reads by deadline, in floored micros: never later than the exact deadline, so a + /// write that crosses one is never skipped. + deadlines: BTreeMap, + /// Reads by their group's sequence, woken when a group lands above them. + landings: BTreeMap, +} + +impl Default for Wakes { + fn default() -> Self { + Self { + earliest: AtomicU64::new(u64::MAX), + parked: kio::Lock::default(), + } + } +} + +impl Wakes { + /// A frame write left its group presenting up to `latest`: wake the reads it expires. + pub(crate) fn presented(&self, latest: Timestamp) { + let at = micros(latest.as_micros()); + if at < self.earliest.load(Ordering::Relaxed) { + return; + } + let mut woken = Vec::new(); + { + let mut parked = self.parked.lock(); + while let Some(entry) = parked.deadlines.first_entry() + && *entry.key() <= at + { + woken.push(entry.remove()); + } + let earliest = parked.deadlines.first_key_value().map_or(u64::MAX, |(at, _)| *at); + self.earliest.store(earliest, Ordering::Relaxed); + } + woken.iter_mut().for_each(kio::WaiterList::wake); + } + + /// Park `waiter` until the edge reaches `reach` plus `budget`. + pub(crate) fn watch_deadline(&self, reach: Timestamp, budget: Duration, waiter: &kio::Waiter) { + let deadline = micros(reach.as_micros()).saturating_add(micros(budget.as_micros())); + // No timestamp reaches it. + if deadline == u64::MAX { + return; + } + let mut parked = self.parked.lock(); + parked.deadlines.entry(deadline).or_default().register(waiter); + self.earliest.fetch_min(deadline, Ordering::Relaxed); + } + + /// Park `waiter` until a group lands above `sequence`. + pub(crate) fn watch_landing(&self, sequence: u64, waiter: &kio::Waiter) { + self.parked + .lock() + .landings + .entry(sequence) + .or_default() + .register(waiter); + } + + /// A servable group landed at `sequence`, so it succeeds every read from `below`, the + /// nearest servable group beneath it, up. Reads below `oldest`, the first group still + /// cached, can no longer expire and are forgotten. + pub(crate) fn landed(&self, below: Option, sequence: u64, oldest: u64) { + let mut woken = Vec::new(); + { + let mut parked = self.parked.lock(); + while let Some(entry) = parked.landings.first_entry() + && *entry.key() < oldest + { + entry.remove(); + } + while let Some(at) = parked + .landings + .range(below.unwrap_or(0)..sequence) + .next() + .map(|(at, _)| *at) + { + woken.extend(parked.landings.remove(&at)); + } + } + woken.iter_mut().for_each(kio::WaiterList::wake); + } + + /// Wake every parked read, for a change that moves every budget at once. + pub(crate) fn wake_all(&self) { + let parked = { + let mut parked = self.parked.lock(); + self.earliest.store(u64::MAX, Ordering::Relaxed); + std::mem::take(&mut *parked) + }; + for mut list in parked.deadlines.into_values().chain(parked.landings.into_values()) { + list.wake(); + } + } +} + +/// Saturate a microsecond count into a deadline key. +fn micros(value: u128) -> u64 { + value.try_into().unwrap_or(u64::MAX) +} diff --git a/rs/moq-net/src/model/group.rs b/rs/moq-net/src/model/group.rs index 6995cea38c8..b2b65a62773 100644 --- a/rs/moq-net/src/model/group.rs +++ b/rs/moq-net/src/model/group.rs @@ -540,6 +540,7 @@ impl Producer { // With the group lock released (lock order is track then group), settle // eviction debt if enough has been written since the track last paid. self.cache.settle(now); + self.cache.wakes().presented(timestamp); // Ingress payload: one whole frame written. self.stats.frames(1); @@ -587,6 +588,7 @@ impl Producer { // The last frame's tick, reused below so settling does not re-read the clock. let mut now = None; + let mut latest = None; for mut frame in frames.drain() { frame.timestamp = frame .timestamp @@ -596,6 +598,7 @@ impl Producer { state.cache += size; now = state.charge.add(size); state.stamp(frame.timestamp); + latest = Some(frame.timestamp); state.frames.push_back(frame); } state.next_index = next_index; @@ -603,6 +606,9 @@ impl Producer { drop(state); self.cache.settle(now); + if let Some(latest) = latest { + self.cache.wakes().presented(latest); + } self.stats.frames(count as u64); self.stats.bytes(bytes); Ok(()) @@ -691,6 +697,7 @@ impl Producer { // With the group lock released (lock order is track then group), settle // eviction debt if enough has been written since the track last paid. self.cache.settle(now); + self.cache.wakes().presented(timestamp); // Ingress payload: one frame opened; its bytes are counted per chunk as the // producer writes them. diff --git a/rs/moq-net/src/model/mod.rs b/rs/moq-net/src/model/mod.rs index d0300712d8c..eba650c0294 100644 --- a/rs/moq-net/src/model/mod.rs +++ b/rs/moq-net/src/model/mod.rs @@ -17,6 +17,7 @@ mod front; mod bytes; pub(crate) mod clock; mod datagram; +mod expiry; mod requests; mod resume; mod subscription; diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index e15e1473280..a57a2bbe029 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -958,7 +958,10 @@ impl TrackState { /// Attach the publisher's immutable metadata without replacing it with local cache policy. fn install(&mut self, info: Info) { - self.info = Some(info); + // A replaced max age moves every parked read's budget. + if self.info.replace(info).is_some() { + self.cache.wakes().wake_all(); + } } /// Create the shared state for a track under `broadcast`, along with the cache @@ -1027,6 +1030,10 @@ impl TrackState { } self.max_sequence = Some(self.max_sequence.map_or(sequence, |max| max.max(sequence))); + let below = visible + .then(|| self.lookup.range(..sequence).rev().find(|(_, slot)| slot.is_live())) + .flatten() + .map(|(below, _)| *below); self.lookup.insert( sequence, Slot { @@ -1038,6 +1045,8 @@ impl TrackState { ); if visible { self.arrival.push_back((sequence, stamp)); + let oldest = self.lookup.first_key_value().map_or(sequence, |(oldest, _)| *oldest); + self.cache.wakes().landed(below, sequence, oldest); } } @@ -3309,75 +3318,40 @@ impl group::Expiry for GroupExpiry { Poll::<()>::Pending }); - let mut expired = false; - let _ = self.state.poll(waiter, |state| { - let budget = clamp_max_delay(max_delay, state.max_age_bound()); - loop { - // An abort can change reach even after the successor is stamped. - // Register before judging, so a racing abort is observed or wakes us. - // An abort only closes the group, never touching the track, so one that - // landed before registering must re-select or the replacement goes unwatched. - let successor = loop { - let successor = state.first_servable(self.sequence.saturating_add(1), cap); - match successor { - Some(group) if group.poll_closed(waiter).is_ready() && group.is_aborted() => continue, - successor => break successor, - } - }; - let edge = state.drift_edge(cap); - expired = state.is_stale(self.sequence, &edge, budget); - if expired { - break; - } - - // A first timestamp can change the verdict without mutating the track: - // on a group past the edge (a new edge), or on the candidate's - // unstamped immediate successor (a reach where there was none). - // Register on the candidate, its successor, and every servable group - // past the edge. If one raced this scan, resolve the edge again before - // Pending. Groups between the successor and the edge can move neither, - // and walking them would cost each of N parked serves the whole - // backlog, O(N^2) per track change. - let mut timestamp_raced = false; - if let Some(slot) = state.lookup.get(&self.sequence) { - let group = &slot.group; - if group.timestamp().is_none() - && group.poll_timestamp(waiter).is_ready() - && group.timestamp().is_some() - { - timestamp_raced = true; - } - } - let past = edge - .presentation - .map_or(self.sequence, |live| live.sequence.max(self.sequence)); - let beyond = state - .lookup - .range((std::ops::Bound::Excluded(past), std::ops::Bound::Unbounded)) - .map(|(_, slot)| slot) - .take_while(|slot| super::subscription::before_end(slot.group.sequence, cap)) - .filter(|slot| slot.visible && !slot.group.is_aborted()) - .map(|slot| &slot.group); - for group in successor.into_iter().chain(beyond) { - if group.timestamp().is_none() - && group.poll_timestamp(waiter).is_ready() - && group.timestamp().is_some() - { - timestamp_raced = true; - break; - } - } - if !timestamp_raced { - break; + let state = self.state.read(); + let budget = clamp_max_delay(max_delay, state.max_age_bound()); + let wakes = state.cache.wakes(); + // Park on exactly what can move the verdict: a group landing above this one, the + // successor's first frame or abort, and the edge crossing the deadline. Parking on + // every track change instead wakes each of N parked reads per append. + wakes.watch_landing(self.sequence, waiter); + let reach = loop { + // An abort can change reach even after the successor is stamped. + // Register before judging, so a racing abort is observed or wakes us. + // An abort only closes the group, never touching the track, so one that + // landed before registering must re-select or the replacement goes unwatched. + let successor = loop { + let successor = state.first_servable(self.sequence.saturating_add(1), cap); + match successor { + Some(group) if group.poll_closed(waiter).is_ready() && group.is_aborted() => continue, + successor => break successor, } + }; + // Unbounded until a group lands above, or the successor presents its first frame. + let Some(successor) = successor else { + return false; + }; + if let Some(reach) = successor.timestamp() { + break reach; } - - // Register on track changes even though the current answer is known: a - // newer group can move the live edge while this group read is pending. - Poll::<()>::Pending - }); - - expired + if successor.poll_timestamp(waiter).is_pending() || successor.timestamp().is_none() { + return false; + } + }; + // Registered before the edge is resolved, so a write crossing the deadline is + // either seen here or wakes us. + wakes.watch_deadline(reach, budget, waiter); + state.is_stale(self.sequence, &state.drift_edge(cap), budget) } } @@ -5854,12 +5828,96 @@ mod test { assert!(matches!(result, Ok(None)), "the held group ends: {result:?}"); } - /// A parked read registers only where a first timestamp can change its verdict: its - /// immediate successor and the groups past the edge. Walking every group between them - /// costs each parked publisher stream the whole backlog, enough for a 2s audio backlog - /// at 400 groups/s to pin a publisher's runtime and starve its connection. + /// An appended group wakes only the parked reads whose expiry it crosses, plus the + /// newest, whose successor it is. A 2s audio backlog at 400 groups/s parks ~800 reads, + /// and waking every one per append pins a publisher's runtime. + #[test] + fn an_append_wakes_only_the_parked_reads_it_expires() { + struct Count(std::sync::atomic::AtomicUsize); + impl std::task::Wake for Count { + fn wake(self: Arc) { + self.wake_by_ref(); + } + fn wake_by_ref(self: &Arc) { + self.0.fetch_add(1, Ordering::SeqCst); + } + } + + const FRAME: u64 = 2500; // micros, one Opus frame per group + const PARKED: u64 = 64; + let producer = track_producer("test", None); + let mut subscriber = producer.subscribe(Subscription::default().with_max_delay(Duration::from_millis(200))); + // Left open, so every read parks instead of ending. + let _open: Vec<_> = (0..PARKED) + .map(|i| { + let mut group = producer.append_group().unwrap(); + group + .write_frame( + Timestamp::from_micros(i * FRAME).unwrap(), + bytes::Bytes::from_static(b"x"), + ) + .unwrap(); + group + }) + .collect(); + + let counts: Vec<_> = (0..PARKED) + .map(|_| Arc::new(Count(std::sync::atomic::AtomicUsize::new(0)))) + .collect(); + let waiters: Vec<_> = counts + .iter() + .map(|count| kio::Waiter::new(std::task::Waker::from(count.clone()))) + .collect(); + let mut held: Vec<_> = waiters + .iter() + .map(|waiter| { + let Poll::Ready(Ok(Some(mut group))) = subscriber.poll_recv_group(waiter) else { + panic!("every group is cached"); + }; + assert!(matches!(group.poll_read_frame(waiter), Poll::Ready(Ok(Some(_))))); + group + }) + .collect(); + for (group, waiter) in held.iter_mut().zip(&waiters) { + assert!(group.poll_read_frame(waiter).is_pending(), "nothing is stale yet"); + } + for count in &counts { + count.0.store(0, Ordering::SeqCst); + } + + // Read `i` can reach its successor's start, `(i + 1) * FRAME`, so an edge at 210ms + // puts reads 0..=3 a full 200ms behind and leaves every later one in budget. + let mut next = producer.append_group().unwrap(); + next.write_frame( + Timestamp::from_micros(210_000).unwrap(), + bytes::Bytes::from_static(b"x"), + ) + .unwrap(); + + let woken: Vec<_> = (0..PARKED) + .filter(|&i| counts[i as usize].0.load(Ordering::SeqCst) > 0) + .collect(); + assert_eq!( + woken, + vec![0, 1, 2, 3, PARKED - 1], + "only the expired reads and the newest wake" + ); + + for (i, (group, waiter)) in held.iter_mut().zip(&waiters).enumerate() { + let result = group.poll_read_frame(waiter); + if i < 4 { + assert!(matches!(result, Poll::Ready(Ok(None))), "read {i} expired: {result:?}"); + } else { + assert!(result.is_pending(), "read {i} is in budget: {result:?}"); + } + } + } + + /// A parked read wakes only once the edge reaches its deadline: its successor's start + /// plus its budget. A first frame below the edge moves nothing, and a new edge short of + /// the deadline cannot convict it, so neither wakes it. #[test] - fn a_parked_read_ignores_first_frames_between_its_successor_and_the_edge() { + fn a_parked_read_wakes_only_once_the_edge_reaches_its_deadline() { let mut producer = track_producer("test", None); let mut subscriber = producer.subscribe(Subscription::default().with_max_delay(Duration::from_secs(10))); let mut head = producer.append_group().unwrap(); @@ -5892,14 +5950,22 @@ mod test { "a first frame below the edge moves neither the reach nor the edge" ); - // Control: a first frame past the edge is still observed. let mut beyond = producer.append_group().unwrap(); - assert!(next.as_mut().poll(&mut cx).is_pending()); - woken.store(false, Ordering::SeqCst); beyond .write_frame(Timestamp::from_millis(4000).unwrap(), bytes::Bytes::from_static(b"x")) .unwrap(); - assert!(woken.load(Ordering::SeqCst), "a new edge wakes the parked read"); + assert!(!woken.load(Ordering::SeqCst), "a new edge short of the deadline"); + + // The edge's own later frame reaches the deadline. + beyond + .write_frame(Timestamp::from_millis(11_000).unwrap(), bytes::Bytes::from_static(b"x")) + .unwrap(); + assert!( + woken.load(Ordering::SeqCst), + "the edge reaching the deadline wakes the read" + ); + let result = next.as_mut().poll(&mut cx); + assert!(matches!(result, Poll::Ready(Ok(None))), "the head is stale: {result:?}"); } /// An aborted successor hands the reach to the next group, which sits below the edge diff --git a/rs/moq-net/tests/route_change.rs b/rs/moq-net/tests/route_change.rs index d991f5ad883..f0b3506e1b1 100644 --- a/rs/moq-net/tests/route_change.rs +++ b/rs/moq-net/tests/route_change.rs @@ -586,7 +586,7 @@ async fn redundant_pair_fails_over(version: &str, loss: Loss) { let consumer = subscriber.consume(); let remote = consumer.request_broadcast("live").await.unwrap(); - let prefs = track::Subscription::default().with_max_age(Duration::from_secs(60)); + let prefs = track::Subscription::default().with_max_delay(Duration::from_secs(60)); let mut rx = read(remote.track("video").unwrap().subscribe(prefs).await.unwrap()); let mut expected = Vec::new();