diff --git a/doc/concept/moq-lite.md b/doc/concept/moq-lite.md index c2947245ed4..b17b0c30a6f 100644 --- a/doc/concept/moq-lite.md +++ b/doc/concept/moq-lite.md @@ -224,6 +224,13 @@ anything on its own. Both ends apply it: the publisher skips a group rather than sending it, and the subscriber skips it again as it reads, since the publisher only ever sees the most tolerant budget across its subscribers. +Across a native route failover, the reader still judges buffered groups against +the logical track's live edge, including groups it is draining from a retired +route. A successor group with no timestamp leaves the preceding group's reach +unbounded until its first frame arrives; if it is dropped first, the next group +takes its place. A cached open group's prefix remains +readable across repeated takeovers and idle resumes. + The publisher declares a retention window per track, which bounds how far back a fetch or late subscriber can reach. Media tracks default to 30 seconds so a segmented egress can still find its segments. diff --git a/quest/m1/README.md b/quest/m1/README.md index cc4e3a35c35..54194d3783e 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -31,7 +31,6 @@ transport, benchmark tooling); worktrees isolate commits, not semantics. - [WebTransport close upstream](/quest/m1/wt-close-upstream.md) - web-transport-moq delivers the close capsule itself, and moq-tokio's `CLOSE_LINGER` is deleted - [IETF hidden default](/quest/m1/ietf-hidden-default.md) - a moq-transport peer without the MoQ Hidden option is advertised hidden namespaces; one with it filters per subscription - [Watch refusal](/quest/m1/watch-refusal.md) - `` shows an origin refusal as an error instead of sitting offline -- [Splice edge cases](/quest/m1/splice-edges.md) - an unstamped successor, a pruned segment's boundary group, and a warm head during a takeover are each handled correctly - [Resumed groups](/quest/m1/resume-latest.md) - a half-delivered group ends once the new copy is past it, so a group-only reader never parks after a mid-group failover - [Track tail hardening](/quest/m1/track-tail-hardening.md) - Rust and JS wait out a track's tail by the same rules, with the known hang, count, truncation, grace, and memory holes closed - [JS close end](/quest/m1/js-track-close-end.md) - a JS track's clean close ends at its own last group, not a sibling producer's diff --git a/quest/m1/resume-latest.md b/quest/m1/resume-latest.md index f594bfc566c..fb878c5c1aa 100644 --- a/quest/m1/resume-latest.md +++ b/quest/m1/resume-latest.md @@ -71,4 +71,3 @@ Regression tests, on a paused clock (`#[tokio::test(start_paused = true)]`): ## Related - [SUBSCRIBE_DROP](/quest/m1/subscribe-drop.md) - tells a resumed latest group when the new copy dropped it -- [Splice edge cases](/quest/m1/splice-edges.md) - the same splice code and test harness diff --git a/quest/m1/splice-edges.md b/quest/m1/splice-edges.md deleted file mode 100644 index aa0d904500f..00000000000 --- a/quest/m1/splice-edges.md +++ /dev/null @@ -1,37 +0,0 @@ -# [S] Splice edge cases - -## Goal - -Three spliced-track cases in `rs/moq-net` stop losing or mis-judging groups, -each with a regression test that fails before its fix: - -- A group whose successor segment's first servable group is still unstamped - keeps an unbounded reach, instead of borrowing a later segment's start and - being skipped or ended with `Error::Old`. -- A reader still draining a pruned segment has its boundary group judged - against the later segments, so a stale boundary group is skipped like any - other. -- A reader draining a warm head keeps it when the upstream fails over again - before the track parks. - -## Plan - -Codex raised all three on https://github.com/moq-dev/moq/pull/4103 and -https://github.com/moq-dev/moq/pull/4104 after the fixes there landed; none -was answered. - -- `served_start` in `rs/moq-net/src/model/resume.rs` says an unstamped group - stops the search, but `find_map` reads a segment's `None` (no group, or its - first group has no frame yet) as a miss and moves on. The two cases need - to be distinguishable, as the per-track `served_start` in - `rs/moq-net/src/model/track.rs` already treats them. -- `ResumeState::successor` finds the cursor's segment by id, so a segment - `prune` removed while a reader still drains it yields no successor at all. - Resolve it from the boundary and the remaining later segments instead. -- An ordinary takeover in `rs/moq-net/src/model/origin.rs` (`Action::Splice` - with no warm copy) overwrites `io.head` with `None`. Dropping the - `WarmGroup` aborts the cached head that a reader resumed from an earlier - park may still be reading, and the next park cannot rebuild the full group. - Keep the existing head when the splice has no new one to take. - -These are independent; one PR is fine since they share the splice tests. diff --git a/rs/moq-net/src/model/origin.rs b/rs/moq-net/src/model/origin.rs index 8fa6facb730..1f72e5a9908 100644 --- a/rs/moq-net/src/model/origin.rs +++ b/rs/moq-net/src/model/origin.rs @@ -2486,7 +2486,9 @@ async fn run_front(task: FrontTask) { tracks.remove(&name); continue; } - io.head = io.warm.take().and_then(|mut warm| warm.edge.take()); + if let Some(head) = io.warm.take().and_then(|mut warm| warm.edge.take()) { + io.head = Some(head); + } // The new segment has produced nothing yet: this is the // edge the copy is asked to advance. io.edge = io.resume.resume_position(); @@ -5703,6 +5705,91 @@ mod tests { } } + #[tokio::test] + async fn warm_head_survives_another_takeover_before_park() { + tokio::time::pause(); + let producer = origin(1).produce(); + let _server = producer + .dynamic("room/alice", Route::default().with_hops(hops(&[10])).with_cost(5)) + .unwrap(); + let pending = producer.consume().request_broadcast("room/alice"); + let upstream = broadcast::Info::new().produce(); + let mut dynamic = upstream.dynamic(); + queued(&_server).await.accept(&upstream); + let resolved = pending.await.unwrap(); + + let track = resolved.track("log").unwrap(); + let subscribing = tokio::spawn(async move { track.subscribe(None).await }); + let source = dynamic.requested_track().await.unwrap().accept(None); + let mut group = source.create_group(0u64.into()).unwrap(); + group.write_frame(crate::Timestamp::ZERO, b"a".as_ref()).unwrap(); + group.write_frame(crate::Timestamp::ZERO, b"b".as_ref()).unwrap(); + let mut subscription = subscribing.await.unwrap().unwrap(); + let mut reading = subscription.recv_group().await.unwrap().unwrap(); + assert_eq!(&reading.read_frame().await.unwrap().unwrap().payload[..], b"a"); + drop(reading); + drop(subscription); + source.unused().await.unwrap(); + drop(group); + drop(source); + + let track = resolved.track("log").unwrap(); + let subscribing = tokio::spawn(async move { track.subscribe(None).await }); + let mut resumed = dynamic.requested_track().await.unwrap().resolving_start().accept(None); + resumed.start_at(0).unwrap(); + let mut subscription = subscribing.await.unwrap().unwrap(); + let mut reading = subscription.recv_group().await.unwrap().unwrap(); + assert_eq!(&reading.read_frame().await.unwrap().unwrap().payload[..], b"a"); + + let replacement_server = producer + .dynamic("room/alice", Route::default().with_hops(hops(&[10]))) + .unwrap(); + let replacement = broadcast::Info::new().produce(); + let mut replacement_dynamic = replacement.dynamic(); + queued(&replacement_server).await.accept(&replacement); + let mut source = replacement_dynamic + .requested_track() + .await + .unwrap() + .resolving_start() + .accept(None); + source.start_at(0).unwrap(); + let mut group = source.create_group(0u64.into()).unwrap(); + group.start_at(2).unwrap(); + group.write_frame(crate::Timestamp::ZERO, b"c".as_ref()).unwrap(); + let mut continuation = reading.clone(); + continuation.start_at(2); + let frame = tokio::time::timeout(Duration::from_secs(1), continuation.read_frame()) + .await + .expect("replacement frame") + .unwrap() + .unwrap(); + assert_eq!(&frame.payload[..], b"c"); + drop(continuation); + assert_eq!(&reading.read_frame().await.unwrap().unwrap().payload[..], b"b"); + assert_eq!(&reading.read_frame().await.unwrap().unwrap().payload[..], b"c"); + drop(reading); + drop(subscription); + source.unused().await.unwrap(); + drop(group); + drop(source); + + let track = resolved.track("log").unwrap(); + let subscribing = tokio::spawn(async move { track.subscribe(None).await }); + let mut next = replacement_dynamic + .requested_track() + .await + .unwrap() + .resolving_start() + .accept(None); + next.start_at(0).unwrap(); + let mut subscription = subscribing.await.unwrap().unwrap(); + let mut reading = subscription.recv_group().await.unwrap().unwrap(); + for expected in [b"a", b"b", b"c"] { + assert_eq!(&reading.read_frame().await.unwrap().unwrap().payload[..], expected); + } + } + /// The same holds for a reader returning to a parked track: its warm cache /// does not stand in for the copy it is waiting on. #[tokio::test] diff --git a/rs/moq-net/src/model/resume.rs b/rs/moq-net/src/model/resume.rs index 4669dd10234..089c361fafc 100644 --- a/rs/moq-net/src/model/resume.rs +++ b/rs/moq-net/src/model/resume.rs @@ -26,10 +26,8 @@ use std::collections::{BTreeMap, HashSet}; use std::ops::Bound; use std::task::{Poll, ready}; -#[cfg(test)] -use crate::Timestamp; -use crate::{Datagram, Error, Result, frame, group, track}; -use track::{Anchor, LiveEdge, Successor}; +use crate::{Datagram, Error, Result, Timestamp, frame, group, track}; +use track::{Anchor, LiveEdge}; use super::subscription::{Cap, Position, Subscription, max_some, min_some}; @@ -144,18 +142,60 @@ fn slice(prefs: &Subscription, start: Option, end: Option) - } } -/// The first servable group in `from..cap` across `segments`, with the slot identity a -/// later judgment needs. An unstamped group stops the search: skipping it for a later -/// start would shrink a reach that is not yet proven. -fn served_start(segments: &[Segment], from: u64, cap: Option) -> Option { - segments.iter().find_map(|segment| { +/// Where the first servable group in `from..cap` across `segments` starts presenting; +/// see [`track::Consumer::poll_first_start`]. An unstamped group stops the search: +/// skipping it for a later start would shrink a reach that is not yet proven. +fn poll_first_start<'a>( + segments: impl IntoIterator, + waiter: &kio::Waiter, + from: u64, + cap: Option, +) -> Option> { + segments.into_iter().find_map(|segment| { let start = segment.start.map_or(0, |start| start.group).max(from); segment .track - .served_start(start, min_some(cap, last_group(segment.end))) + .poll_first_start(waiter, start, min_some(cap, last_group(segment.end))) }) } +/// Where a splice continues past the exclusive group `boundary` of segment `after`, +/// below the reader's `cap`: the first group the later segments serve there. Holds the +/// question rather than a cached answer, so a group aborted or evicted before its first +/// frame hands the bound to whichever group serves there next. +#[derive(Clone)] +pub(crate) struct Successor { + state: kio::ConsumerWeak, + after: u64, + boundary: u64, + cap: Option, +} + +impl Successor { + /// Where the successor starts presenting: `None` while no later group is cached or + /// the first one is unstamped. Registers `waiter` for anything that could move it. + /// Takes the splice's and its segments' locks, so never call it under a track's. + pub(crate) fn poll_start(&self, waiter: &kio::Waiter) -> Option { + let mut start = None; + let _ = self.state.poll(waiter, |state| { + // A draining reader's own segment may already have been pruned. + let later = state.segments.iter().filter(|segment| segment.id > self.after); + start = poll_first_start(later, waiter, self.boundary, self.cap).flatten(); + Poll::<()>::Pending + }); + start + } +} + +impl PartialEq for Successor { + fn eq(&self, other: &Self) -> bool { + self.after == other.after + && self.boundary == other.boundary + && self.cap == other.cap + && self.state.same_channel(&other.state) + } +} + /// How many segments a logical track keeps before pruning terminal ones from the /// front: the live segment plus a couple of predecessors still draining to slow /// readers. Without a bound, every failover leaves one dead segment (pinning a @@ -248,14 +288,6 @@ impl ResumeState { .max_by_key(|edge| edge.sequence) } - /// Where the logical track continues past the exclusive group `boundary` of segment - /// `id`, below the reader's `cap`: the start of the first group the later segments - /// serve there. `None` while none is cached, or it has no frame yet. - fn successor(&self, id: u64, boundary: u64, cap: Option) -> Option { - let index = self.segments.iter().position(|segment| segment.id == id)?; - served_start(&self.segments[index + 1..], boundary, cap) - } - /// Append a segment serving the track from `start` onward, capping (or replacing) /// the previous segments so the ranges stay disjoint and ascending. fn switch(&mut self, track: track::Consumer, start: Option) -> Result<()> { @@ -706,10 +738,20 @@ impl Consumer { self.state.read().live_edge(cap) } - /// Where the first servable group in `from..cap` starts, with the identity to - /// revalidate it; see [`track::Consumer::served_start`]. - pub(crate) fn served_start(&self, from: u64, cap: Option) -> Option { - served_start(&self.state.read().segments, from, cap) + /// Where the first servable group in `from..cap` starts presenting; see + /// [`track::Consumer::poll_first_start`]. + pub(crate) fn poll_first_start( + &self, + waiter: &kio::Waiter, + from: u64, + cap: Option, + ) -> Option> { + let mut start = None; + let _ = self.state.poll(waiter, |state| { + start = poll_first_start(&state.segments, waiter, from, cap); + Poll::<()>::Pending + }); + start } /// The newest cached group across every spliced segment; see @@ -1721,16 +1763,21 @@ impl Subscriber { /// content the logical track holds, which a segment's own track never sees. /// /// When the boundary lowers the cap, the reader's next group past it lives in a - /// later segment, so `state` supplies where it starts ([`Anchor::successor`]). - /// Otherwise the logical anchor's own successor (a wrapping splice's) still holds. - fn segment_anchor(seg: &SegmentSub, anchor: Anchor, state: &ResumeState) -> Anchor { + /// later segment of `state` ([`Anchor::successor`]). Otherwise the logical anchor's + /// own successor (a wrapping splice's) still holds. + fn segment_anchor(seg: &SegmentSub, anchor: Anchor, state: &kio::Consumer) -> Anchor { let Some(boundary) = seg.last_group() else { return anchor; }; let cap = anchor.cap; let mut capped = anchor.capped(Some(boundary)); if capped.cap != cap { - capped.successor = state.successor(seg.id, boundary, cap); + capped.successor = Some(Successor { + state: state.weak(), + after: seg.id, + boundary, + cap, + }); } capped } @@ -1749,8 +1796,9 @@ impl Subscriber { /// logical edge moves whenever any of them grows. fn refresh_anchor(&mut self) { let outer = self.outer.clone().capped(self.end_sequence); - let state = self.state.read(); - let edge = state + let edge = self + .state + .read() .live_edge(outer.cap) .into_iter() .chain(outer.edge.clone()) @@ -1763,7 +1811,7 @@ impl Subscriber { *current = anchor.clone(); } for seg in &mut self.segments { - let anchor = Self::segment_anchor(seg, anchor.clone(), &state); + let anchor = Self::segment_anchor(seg, anchor.clone(), &self.state); seg.anchor = anchor.clone(); if let Some(sub) = seg.stale_sub_mut() { sub.set_anchor(anchor); @@ -3303,6 +3351,157 @@ mod test { assert_eq!(replayed, vec![0, 1, 2, 3], "a backlog inside the budget crosses whole"); } + #[tokio::test] + async fn unstamped_successor_segment_keeps_the_previous_group_unbounded() { + let (mut a, a_read) = track_pair("a"); + let (b, b_read) = track_pair("b"); + let (mut c, c_read) = track_pair("c"); + write_group_at(&mut a, 0, "a0", Duration::ZERO); + let _unstamped = b.create_group(1u64.into()).unwrap(); + write_group_at(&mut c, 2, "c2", Duration::from_secs(10)); + write_group_at(&mut c, 3, "c3", Duration::from_secs(20)); + let mut producer = Producer::new(); + producer.switch(a_read, None).unwrap(); + producer.switch(b_read, Position::group(1)).unwrap(); + producer.switch(c_read, Position::group(2)).unwrap(); + let mut sub = producer.consume().subscribe(None); + assert_eq!( + recv(&mut sub), + 0, + "the unstamped successor must not borrow segment C's start" + ); + } + + /// A handed-out group parked at its tail is re-judged once its unstamped successor in + /// the next segment presents a first frame. That frame touches only the successor's + /// group, not either track or the drift anchor, so the read has to watch it directly. + #[tokio::test] + async fn unstamped_successor_first_frame_wakes_a_parked_read() { + use std::task::Context; + + let (a, a_read) = track_pair("a"); + let (mut b, b_read) = track_pair("b"); + let mut head = a.create_group(0u64.into()).unwrap(); + head.write_frame(Timestamp::ZERO, b"a0".to_vec()).unwrap(); + let mut successor = b.create_group(1u64.into()).unwrap(); + write_group_at(&mut b, 2, "edge", Duration::from_secs(20)); + let mut producer = Producer::new(); + producer.switch(a_read, None).unwrap(); + producer.switch(b_read, Position::group(1)).unwrap(); + let mut sub = producer.consume().subscribe(None); + let mut reading = sub.recv_group().now_or_never().unwrap().unwrap().unwrap(); + assert_eq!(reading.sequence, 0, "an unbounded reach is not stale"); + assert_eq!(read(&mut reading), b"a0"); + + let (counter, waker) = CountWaker::new(); + let mut cx = Context::from_waker(&waker); + let mut next = std::pin::pin!(reading.read_frame()); + assert!(next.as_mut().poll(&mut cx).is_pending()); + + let before = counter.count(); + successor + .write_frame(Duration::from_secs(1).try_into().unwrap(), b"b1".to_vec()) + .unwrap(); + assert!(counter.count() > before, "the successor's first frame lost its wakeup"); + let result = next.as_mut().poll(&mut cx); + assert!(matches!(result, Poll::Ready(Ok(None))), "the head is stale: {result:?}"); + } + + /// An unstamped successor aborted before its first frame no longer names where the + /// track continues, so a read parked at the previous group's tail re-resolves it: + /// first to a later group already cached, then to one that arrives after the abort. + #[tokio::test] + async fn aborted_unstamped_successor_re_resolves_a_parked_read() { + use std::task::Context; + + let (a, a_read) = track_pair("a"); + let (mut b, b_read) = track_pair("b"); + let mut head = a.create_group(0u64.into()).unwrap(); + head.write_frame(Timestamp::ZERO, b"a0".to_vec()).unwrap(); + let successor = b.create_group(1u64.into()).unwrap(); + write_group_at(&mut b, 3, "edge", Duration::from_secs(20)); + let mut producer = Producer::new(); + producer.switch(a_read, None).unwrap(); + producer.switch(b_read, Position::group(1)).unwrap(); + let budget = Subscription::default().with_max_age(Duration::from_secs(10)); + let mut sub = producer.consume().subscribe(budget); + let mut reading = sub.recv_group().now_or_never().unwrap().unwrap().unwrap(); + assert_eq!(reading.sequence, 0, "an unbounded reach is not stale"); + assert_eq!(read(&mut reading), b"a0"); + + let (counter, waker) = CountWaker::new(); + let mut cx = Context::from_waker(&waker); + let mut next = std::pin::pin!(reading.read_frame()); + assert!(next.as_mut().poll(&mut cx).is_pending()); + + successor.abort(Error::Cancel).unwrap(); + // Group 3 is now the successor, and the edge itself: within the budget. + assert!(next.as_mut().poll(&mut cx).is_pending()); + + let before = counter.count(); + write_group_at(&mut b, 2, "b2", Duration::from_secs(1)); + assert!(counter.count() > before, "the replacement successor lost its wakeup"); + let result = next.as_mut().poll(&mut cx); + assert!(matches!(result, Poll::Ready(Ok(None))), "the head is stale: {result:?}"); + } + + /// The same across segments: once the next segment's only group is aborted, the + /// segment after it says where the track continues. + #[tokio::test] + async fn aborted_unstamped_successor_re_resolves_across_segments() { + use std::task::Context; + + let (a, a_read) = track_pair("a"); + let (b, b_read) = track_pair("b"); + let (mut c, c_read) = track_pair("c"); + let mut head = a.create_group(0u64.into()).unwrap(); + head.write_frame(Timestamp::ZERO, b"a0".to_vec()).unwrap(); + let successor = b.create_group(1u64.into()).unwrap(); + write_group_at(&mut c, 2, "c2", Duration::from_secs(1)); + write_group_at(&mut c, 3, "c3", Duration::from_secs(20)); + let mut producer = Producer::new(); + producer.switch(a_read, None).unwrap(); + producer.switch(b_read, Position::group(1)).unwrap(); + producer.switch(c_read, Position::group(2)).unwrap(); + let mut sub = producer.consume().subscribe(None); + let mut reading = sub.recv_group().now_or_never().unwrap().unwrap().unwrap(); + assert_eq!(reading.sequence, 0, "an unbounded reach is not stale"); + assert_eq!(read(&mut reading), b"a0"); + + let (counter, waker) = CountWaker::new(); + let mut cx = Context::from_waker(&waker); + let mut next = std::pin::pin!(reading.read_frame()); + assert!(next.as_mut().poll(&mut cx).is_pending()); + + let before = counter.count(); + successor.abort(Error::Cancel).unwrap(); + assert!(counter.count() > before, "the successor's abort lost its wakeup"); + let result = next.as_mut().poll(&mut cx); + assert!(matches!(result, Poll::Ready(Ok(None))), "the head is stale: {result:?}"); + } + + #[tokio::test] + async fn pruned_segment_boundary_is_judged_against_later_segments() { + let (mut a, a_read) = track_pair("a"); + let mut producer = Producer::new(); + producer.switch(a_read, None).unwrap(); + let mut sub = producer.consume().subscribe(None); + // Keep A's cursor draining while its newest group advances beyond the late boundary group. + write_group_at(&mut a, 0, "a0", Duration::ZERO); + assert_eq!(recv(&mut sub), 0); + write_group_at(&mut a, 3, "past-boundary", Duration::from_secs(3)); + for sequence in 3..=(2 + MAX_SEGMENTS as u64) { + let (mut track, consumer) = track_pair("later"); + producer.switch(consumer, Position::group(sequence)).unwrap(); + write_group_at(&mut track, sequence, "later", Duration::from_secs(sequence * 10)); + assert_eq!(recv(&mut sub), sequence); + } + assert!(producer.state.read().pruned.is_some()); + // A's boundary group arrives after A was pruned; it is stale against group 3's start. + write_group_at(&mut a, 2, "a2", Duration::from_secs(2)); + recv_pending(&mut sub); + } + /// A segment's track never sees the groups of the segments after it: its own edge /// freezes once a takeover caps it, and its last group has no successor there. /// Both spliced cursors judge its backlog as one plain track holding the same groups diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 10aac201e1e..49aa3ba2ecb 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -19,6 +19,7 @@ use crate::{broadcast, cache, group, stats}; use super::{Datagram, Requests}; use super::Cap; +use super::resume::Successor; pub use super::subscription::{Position, Subscription}; use std::{ @@ -555,40 +556,20 @@ impl TrackState { /// and `beyond` is where it starts when another track serves it (a splice's next /// segment; see [`Anchor::successor`]). fn reach(&self, sequence: u64, cap: Option, beyond: Option) -> Option { - match self.first_start(sequence.saturating_add(1), cap) { - Some(start) => start, + match self.first_servable(sequence.saturating_add(1), cap) { + Some(group) => group.timestamp(), None => beyond, } } - /// Where the first servable group in `from..cap` starts presenting: `None` when no - /// such group is cached, `Some(None)` while it has no frame yet. - fn first_start(&self, from: u64, cap: Option) -> Option> { - let slot = self - .lookup - .range(from..) - .map(|(_, slot)| slot) - .take_while(|slot| super::subscription::before_end(slot.group.sequence, cap)) - .find(|slot| slot.visible && !slot.group.is_aborted())?; - Some(slot.group.timestamp()) - } - - /// The first servable group's start in `from..cap`, with the slot identity a later - /// judgment needs to tell that group from whatever replaces it. `None` when no such - /// group is cached or it has no frame yet: an unstamped successor leaves reach - /// unbounded, and this does not skip past it to a later group. - fn served_start(&self, from: u64, cap: Option) -> Option { - let slot = self - .lookup + /// The first servable group in `from..cap`, stamped or not. + fn first_servable(&self, from: u64, cap: Option) -> Option<&group::Producer> { + self.lookup .range(from..) .map(|(_, slot)| slot) .take_while(|slot| super::subscription::before_end(slot.group.sequence, cap)) - .find(|slot| slot.visible && !slot.group.is_aborted())?; - Some(ServedStart { - sequence: slot.group.sequence, - stamp: slot.stamp, - timestamp: slot.group.timestamp()?, - }) + .find(|slot| slot.visible && !slot.group.is_aborted()) + .map(|slot| &slot.group) } /// Whether the group at `sequence` has drifted further behind `edge` than `budget` @@ -629,9 +610,9 @@ impl TrackState { .presentation .filter(|live| self.holds(live.sequence, live.stamp)) .map(|live| (live.sequence, live.timestamp)); - // `outer` and `successor` were revalidated on their own tracks before this lock - // was taken ([`LiveEdge::is_live`], [`Successor::start`]). There is no slot for - // them here, and taking their locks here would nest. + // `outer` was revalidated and `successor` resolved on their own tracks before this + // lock was taken ([`LiveEdge::is_live`], [`Successor::poll_start`]). There is no + // slot for them here, and taking their locks here would nest. let Some((_, timestamp)) = local .into_iter() .chain(edge.outer) @@ -2537,20 +2518,29 @@ impl Consumer { } } - /// Where the first servable group in `from..cap` starts, with enough identity to - /// revalidate it later. A splice answers from the first segment holding one. - pub(crate) fn served_start(&self, from: u64, cap: Option) -> Option { + /// Where the first servable group in `from..cap` starts presenting: `None` while no + /// such group is cached, `Some(None)` while it has no frame yet. Registers `waiter` + /// for whatever could move that answer: a group arriving or leaving, or the unstamped + /// one's first frame. A splice answers from the first segment holding one. + pub(crate) fn poll_first_start( + &self, + waiter: &kio::Waiter, + from: u64, + cap: Option, + ) -> Option> { match &self.inner { ConsumerKind::Plain(state) => { - let served = state.read().served_start(from, cap)?; - Some(Successor { - sequence: served.sequence, - timestamp: served.timestamp, - stamp: served.stamp, - track: state.weak(), - }) + let mut start = None; + let _ = state.poll(waiter, |state| { + start = state.first_servable(from, cap).map(|group| { + let _ = group.poll_timestamp(waiter); + group.timestamp() + }); + Poll::<()>::Pending + }); + start } - ConsumerKind::Spliced(resume) => resume.served_start(from, cap), + ConsumerKind::Spliced(resume) => resume.poll_first_start(waiter, from, cap), } } @@ -3164,8 +3154,8 @@ enum SubscriberKind { /// One poll's view of how far this subscription may drift: the clamped budget and the /// live edge to measure a candidate group against. Resolved once, then applied to every -/// group that poll considers. `outer` and `successor` are revalidated per candidate, -/// outside this track's lock. +/// group that poll considers. `outer` is revalidated and `successor` resolved per +/// candidate, outside this track's lock. #[derive(Clone)] struct Drift { budget: Duration, @@ -3208,7 +3198,10 @@ impl group::Expiry for GroupExpiry { .edge .filter(LiveEdge::is_live) .map(|live| (live.sequence, live.timestamp)); - let successor = anchor.successor.as_ref().and_then(Successor::start); + let successor = anchor + .successor + .as_ref() + .and_then(|successor| successor.poll_start(waiter)); let mut expired = false; let _ = self.state.poll(waiter, |state| { @@ -3297,8 +3290,8 @@ pub(crate) struct Anchor { /// Where the reader's next group past `cap` starts presenting, when another track /// serves it (a splice's next segment). The last group below the cap has no /// successor in its own track, so without this nothing bounds its reach and it is - /// never judged stale. `None` while unknown or unstamped. Revalidated like `edge`: - /// a cached start must not convict once that group is gone. + /// never judged stale. Resolved afresh on every judgment rather than cached, so a + /// group that goes away hands the bound to whichever group serves there next. pub successor: Option, } @@ -3344,48 +3337,6 @@ impl PartialEq for LiveEdge { } } -/// The first servable group past a segment boundary: where it starts, and which slot -/// that start was read from. -struct ServedStart { - sequence: u64, - stamp: u32, - timestamp: Timestamp, -} - -/// A successor pushed onto another track's cursor. The timestamp alone is not enough: -/// once the group is evicted, a later group can keep the outer edge valid while this -/// start is no longer where the track continues. -#[derive(Clone)] -pub(crate) struct Successor { - sequence: u64, - timestamp: Timestamp, - stamp: u32, - track: kio::ConsumerWeak, -} - -impl Successor { - /// The start this still names, re-read from its own track, or `None` once that - /// group is gone or no longer stamped. Takes that track's lock, so never call it - /// under another's. - fn start(&self) -> Option { - let state = self.track.read(); - let slot = state.lookup.get(&self.sequence)?; - if slot.stamp != self.stamp || slot.group.is_aborted() { - return None; - } - slot.group.timestamp() - } -} - -impl PartialEq for Successor { - fn eq(&self, other: &Self) -> bool { - self.sequence == other.sequence - && self.timestamp == other.timestamp - && self.stamp == other.stamp - && self.track.same_channel(&other.track) - } -} - /// The newest servable group that has presented at least one frame. #[derive(Clone, Copy)] struct PresentationEdge { @@ -3525,7 +3476,10 @@ impl PlainSubscriber { .as_ref() .filter(|live| live.is_live()) .map(|live| (live.sequence, live.timestamp)); - let successor = drift.successor.as_ref().and_then(Successor::start); + let successor = drift + .successor + .as_ref() + .and_then(|successor| successor.poll_start(waiter)); let presentation = drift.edge.presentation; let cap = drift.edge.cap; let budget = drift.budget; @@ -6443,47 +6397,6 @@ mod test { open.finish().unwrap(); } - /// A pushed successor is the same kind of cached fact. Evicting it must not keep - /// bounding the previous segment's last group while a later group still anchors the - /// outer edge. - #[tokio::test] - async fn an_evicted_successor_convicts_nothing() { - let producer = track_producer("a", None); - let mut subscriber = producer.subscribe(Subscription::default().with_max_age(Duration::from_secs(1))); - let mut open = producer.append_group().unwrap(); - open.write_frame(Timestamp::ZERO, bytes::Bytes::from_static(b"a")) - .unwrap(); - let mut group = subscriber.recv_group().await.unwrap().expect("group"); - assert!(group.read_frame().await.unwrap().is_some()); - - let mut next = track_producer("b", None); - // Early enough that group 0 is already past the budget, and not itself the edge. - let successor = append_at(&mut next, 10); - append_at(&mut next, 30_000); - let consumer = next.consume(); - subscriber.set_anchor(Anchor { - cap: None, - edge: consumer.live_edge(None), - successor: consumer.served_start(successor, None), - }); - - let mut control = group.clone(); - assert!( - matches!(control.read_frame().now_or_never(), Some(Ok(None))), - "the open group ends against the successor" - ); - - let slot = next.modify().unwrap().lookup.remove(&successor).unwrap(); - let _ = slot.group.abort(Error::Evicted); - - assert!( - group.read_frame().now_or_never().is_none(), - "a vanished successor is no reason to drop what is left" - ); - assert!(!group.latency_expired()); - open.finish().unwrap(); - } - #[tokio::test] async fn a_lower_sequence_is_never_the_live_edge() { let producer = track_producer("test", None);