Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions quest/m1/flight-priority.md

This file was deleted.

23 changes: 23 additions & 0 deletions rs/moq-net/src/model/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3866,6 +3866,9 @@ pub(super) trait Expiry: Send + Sync + std::panic::UnwindSafe + std::panic::RefU

/// Keep the reader's budget and cap while following a replacement track's edge.
fn for_track(&self, track: &track::Consumer) -> Arc<dyn Expiry>;

/// What the reader asks a fill with, such as the flight it boards.
fn asking(&self) -> super::track::Asking;
}

/// The read cursor over a [`Producer`]'s shared state.
Expand Down Expand Up @@ -4069,6 +4072,18 @@ impl Consumer {
self
}

/// What this reader asks a fill with: its subscription's or fetch's, else the defaults.
pub(super) fn asking(&self) -> super::track::Asking {
self.expiry.as_ref().map_or(
super::track::Asking {
priority: 0,
max_delay: None,
order: super::subscription::Order::Ascending,
},
|expiry| expiry.asking(),
)
}

/// The hand-off of this copy's head, shared by every reader that could resume it
/// elsewhere.
pub(super) fn handoff(&self) -> Arc<Handoff> {
Expand Down Expand Up @@ -7962,6 +7977,14 @@ mod test {
fn for_track(&self, _: &track::Consumer) -> Arc<dyn Expiry> {
Arc::new(self.clone())
}

fn asking(&self) -> crate::model::track::Asking {
crate::model::track::Asking {
priority: 0,
max_delay: None,
order: crate::model::subscription::Order::Ascending,
}
}
}

/// A reader that took every frame of an unfinished group still fails with `Old` when
Expand Down
156 changes: 140 additions & 16 deletions rs/moq-net/src/model/track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use demand::Subscriptions;
mod fill;
mod hole;
mod miss;
pub(super) use fill::Asking;
pub use fill::Fill;
use fill::FillState;
pub(crate) use hole::Hole;
Expand Down Expand Up @@ -3843,7 +3844,8 @@ pub(super) enum Board {
}

/// Board `reader`'s group at its frame, which the copy it read shed: ride a copy that
/// admits it, else ask the track's [`Requests`] handlers for a flight from that frame.
/// admits it, else ask the track's [`Requests`] handlers for a flight from that frame,
/// with the reader's priority and budget.
/// `None` when no handler can serve one.
///
/// A compressed group's flight starts at frame 0, since a later frame builds on the ones
Expand All @@ -3858,15 +3860,17 @@ pub(super) fn board(track: &kio::Producer<TrackState>, reader: &group::Consumer)
}
let fills = state.fills.clone();
drop(state);
// One group either way, so it never turns a wider fill it joins newest first.
let asking = fill::Asking {
priority: 0,
max_delay: None,
order: Order::Ascending,
..reader.asking()
};
let (key, frame) = (reader.start, if compress { 0 } else { reader.index() });
let join = fills.lock().fly(&fills, key, frame, asking)?;
Some(Board::Wait(Boarding {
join: Box::new(join),
fills,
asking,
key,
frame,
}))
Expand All @@ -3886,6 +3890,9 @@ pub(super) fn hand_over(
pub(super) struct Boarding {
// Boxed: a reader holds one only while boarding.
join: Box<fill::Join>,
// Where to ask again, with what, when a tighter budget than this reader's refused it.
fills: kio::Shared<FillState>,
asking: fill::Asking,
key: u64,
frame: u64,
}
Expand All @@ -3906,15 +3913,36 @@ impl Boarding {
};
let key = self.key;
let answer = self.join.outcome().poll(waiter, |outcome| match outcome.refused(key) {
Some((err, _)) => Poll::Ready(Err(err.clone())),
None if outcome.is_accepted(key) => Poll::Ready(outcome.partial(key, frames).ok_or(Error::Cancel)),
Some((err, asked)) => Poll::Ready(Err((err.clone(), asked))),
None if outcome.is_accepted(key) => Poll::Ready(outcome.partial(key, frames).ok_or((Error::Cancel, None))),
None => Poll::Pending,
});
Poll::Ready(match ready!(answer) {
Ok(Ok(copy)) => copy.try_consume().ok_or(Error::Cancel),
Ok(Err(err)) => Err(err),
Err(_closed) => Err(Error::NotFound),
})
// The closed channel's guard borrows the join, which a retry replaces.
let (err, asked) = match ready!(answer.map(|answer| answer.map_err(drop))) {
Ok(Ok(copy)) => return Poll::Ready(copy.try_consume().ok_or(Error::Cancel)),
Ok(Err(refused)) => refused,
Err(_closed) => return Poll::Ready(Err(Error::NotFound)),
};
// Boarded a flight already asked under a tighter budget, which judged the group too
// old for that budget, not ours: ask again with ours. Terminates, since the new
// flight asks at least our budget.
if !matches!(err, Error::Old) || !looser(asked, self.asking.max_delay) {
return Poll::Ready(Err(err));
}
let Some(join) = self.fills.lock().fly(&self.fills, key, self.frame, self.asking) else {
return Poll::Ready(Err(err));
};
*self.join = join;
self.poll(waiter)
}
}

/// Whether `ours` waits longer than the budget `asked` upstream, `None` being the longest.
fn looser(asked: Option<Duration>, ours: Option<Duration>) -> bool {
match (asked, ours) {
(Some(asked), Some(ours)) => ours > asked,
(Some(_), None) => true,
(None, _) => false,
}
}

Expand Down Expand Up @@ -6931,6 +6959,7 @@ impl Fetching {
cache,
reading: Reading::new(state.clone()),
key,
priority: self.priority,
max_delay: self.max_delay,
superseded: Superseded::default(),
});
Expand Down Expand Up @@ -7001,12 +7030,7 @@ impl Fetching {
// Joined after a tighter budget went on the wire, which judged the group too old
// for that budget, not ours: ask again with ours. Terminates, since the new
// fill asks at least our budget.
let looser = match (asked, self.max_delay) {
(Some(asked), Some(ours)) => ours > asked,
(Some(_), None) => true,
(None, _) => false,
};
if matches!(err, Error::Old) && looser {
if matches!(err, Error::Old) && looser(asked, self.max_delay) {
let options = group::Fetch {
priority: self.priority,
max_delay: self.max_delay,
Expand Down Expand Up @@ -7223,6 +7247,7 @@ struct FetchExpiry {
cache: Arc<cache::Track>,
reading: Reading,
key: u64,
priority: i8,
max_delay: Option<Duration>,
superseded: Superseded,
}
Expand Down Expand Up @@ -7278,11 +7303,20 @@ impl group::Expiry for FetchExpiry {
state,
cache,
key: self.key,
priority: self.priority,
max_delay: self.max_delay,
superseded: Superseded::default(),
})
}

fn asking(&self) -> Asking {
Asking {
priority: self.priority,
max_delay: self.max_delay,
order: Order::Ascending,
}
}

fn ended(&self) {
self.reading.release();
}
Expand Down Expand Up @@ -7327,6 +7361,15 @@ impl group::Expiry for GroupExpiry {
})
}

fn asking(&self) -> Asking {
let subscription = self.subscription.read();
Asking {
priority: subscription.priority,
max_delay: subscription.delay.max,
order: subscription.order,
}
}

fn ended(&self) {
self.reading.release();
}
Expand Down Expand Up @@ -27550,6 +27593,87 @@ mod test {
assert!(matches!(late.read_frame().now_or_never(), Some(Err(Error::Old))));
}

/// A flight asks upstream with its rider's priority and budget, a subscriber's or a
/// fetch's, as any other fill does, so a late joiner's replay is not ranked below live
/// traffic. It asks oldest first whatever the rider's order, since it holds one group.
#[test]
#[cfg(feature = "async")]
fn a_flight_asks_with_its_riders_priority() {
let budget = Duration::from_secs(10);
let producer = track_producer("test", Info::default());
let requests = producer.requests();
let mut sub = producer.subscribe(
Subscription::default()
.with_priority(5)
.with_delay(Delay::default().with_max(budget))
.with_order(Order::Descending),
);
let mut live = producer.append_next().unwrap();
for index in 0..3_000u32 {
live.write_frame(crate::Timestamp::unscaled(live.start), numbered(index))
.unwrap();
}
let mut late = sub.recv_group().now_or_never().unwrap().unwrap().expect("group 0");
assert_eq!(read_numbered(&mut late), None, "boarding");
let fill = asked(&requests).expect("a flight");
assert_eq!(fill.start(), Position { group: 0, frame: 0 });
assert_eq!(fill.priority(), 5);
assert_eq!(fill.max_delay(), Some(budget));
assert_eq!(fill.order(), Order::Ascending);
drop(late);
drop(fill);

let fetch = group::Fetch::default().with_priority(-3).with_max_delay(budget);
let mut fetched = producer
.consume()
.fetch_group(0, fetch)
.now_or_never()
.expect("cached")
.expect("group 0");
assert_eq!(read_numbered(&mut fetched), None, "boarding");
let fill = asked(&requests).expect("a flight");
assert_eq!(fill.priority(), -3);
assert_eq!(fill.max_delay(), Some(budget));
}

/// A rider that boarded a flight already asked under a tighter budget is not failed by
/// that budget's `Old`: it asks a flight of its own with its budget.
#[test]
#[cfg(feature = "async")]
fn a_looser_rider_asks_again_after_old() {
let producer = track_producer("test", Info::default());
let requests = producer.requests();
let subscribe = |budget: u64| {
let delay = Delay::default().with_max(Duration::from_secs(budget));
producer.subscribe(Subscription::default().with_delay(delay))
};
let (mut tight, mut loose) = (subscribe(1), subscribe(10));
let mut live = producer.append_next().unwrap();
for index in 0..3_000u32 {
live.write_frame(crate::Timestamp::unscaled(live.start), numbered(index))
.unwrap();
}
let recv = |sub: &mut Subscriber| sub.recv_group().now_or_never().unwrap().unwrap().expect("group 0");
let (mut tight, mut loose) = (recv(&mut tight), recv(&mut loose));

assert_eq!(read_numbered(&mut tight), None, "boarding");
let req = requests.requested_fill_group().now_or_never().unwrap().unwrap();
assert_eq!(req.max_delay(), Some(Duration::from_secs(1)));
assert_eq!(read_numbered(&mut loose), None, "boards the same flight");
assert!(requests.requested_fill_group().now_or_never().is_none(), "one flight");

req.reject(Error::Old);
assert!(matches!(tight.read_frame().now_or_never(), Some(Err(Error::Evicted))));
assert_eq!(read_numbered(&mut loose), None, "asks again");
let req = requests.requested_fill_group().now_or_never().unwrap().unwrap();
assert_eq!(req.max_delay(), Some(Duration::from_secs(10)));
let mut flight = req.accept(None).unwrap();
flight
.write_frame(crate::Timestamp::unscaled(flight.start), numbered(0))
.unwrap();
assert_eq!(read_numbered(&mut loose), Some(0));
}

/// Pool debt sheds a flight even once the cached copy of its group was evicted whole.
#[test]
#[cfg(feature = "async")]
Expand Down
2 changes: 1 addition & 1 deletion rs/moq-net/src/model/track/fill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl Pending {

/// What a reader asks its fills with, which it may change while they are out.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) struct Asking {
pub(in crate::model) struct Asking {
pub priority: i8,
pub max_delay: Option<Duration>,
pub order: Order,
Expand Down