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
29 changes: 29 additions & 0 deletions quest/m0/backport-relay-high-key.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# [M] Backport the far-ahead group key fix to release

## Goal

A relay on `release` holds no arrival entries for groups it no longer caches,
and a reader's wake no longer walks groups below its floor, so one publisher
that jumps to a far-ahead group key and keeps publishing below it cannot grow
a relay's memory or burn its shared runtime's CPU. Ships in a `moq-net` patch
in the next release.

## Plan

Decided 2026-11-17 (maintainer, [moq-dev/moq#9010](https://github.com/moq-dev/moq/pull/9010#issuecomment-6959731587)): ✅
backport the track fix to `release` as a separate PR after it lands on
`main`, per RELEASE.md (rejected: shipping it only with the cut).

Port the track half of the fix from
[moq-dev/moq#9044](https://github.com/moq-dev/moq/pull/9044) in
`rs/moq-net/src/model/track.rs`: arrival entries carry their own position, so
dead ones are compacted from anywhere once they outnumber the cached groups,
and the arrival walk moves a reader's cursor past leading entries it is never
handed (dead, or below its floor). `release` still indexes the queue by an
`offset`, so this is a hand port, not a cherry-pick. Reproduce first with the
PR's `a_far_ahead_key_pins_nothing_behind_it` test; if `release` cannot hit it,
delete this quest and say why in the PR.

The front's track-info cleanup stays on `main`: it is not part of the decision.

Public API: none. Wire: none.
37 changes: 0 additions & 37 deletions quest/m1/precut-fixes/relay-high-key.md

This file was deleted.

97 changes: 97 additions & 0 deletions rs/moq-net/benches/track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@
//! refusals and readers. Each reader consumes each refusal before the next, so the cost per
//! refusal must not grow with the history of answers.
//!
//! `track_high_key` writes one group below a far-ahead key every reader already took, behind
//! a backlog of groups written there before, swept over the readers and the backlog: the
//! protected live edge must pin none of it, and a reader never hands out a group under its
//! floor, so a walk over the backlog on each write shows up as a slope over it. Every
//! iteration adds to the backlog too, so such a walk also lifts every point far above one
//! reader's constant.
//!
//! Run with `cargo bench -p moq-net --bench track`.

#![expect(
Expand Down Expand Up @@ -339,6 +346,95 @@ impl Fanout {
}
}

/// A track whose readers took a far-ahead key, as the publisher writes on below it.
struct HighKey {
_broadcast: broadcast::Producer,
track: track::Producer,
subscribers: Vec<track::Subscriber>,
waiter: kio::Waiter,
payload: Bytes,
next: u64,
}

impl HighKey {
fn new(subscribers: usize, backlog: usize) -> Self {
let mut info = broadcast::Info::default();
let config = cache::Config::default()
.with_capacity(CACHE_CAPACITY)
.with_expiry(cache::DEFAULT_EXPIRY);
info.pool = cache::Pool::new(config);
let broadcast = broadcast::Producer::new(info);
let track = broadcast
.create_track("bench", moq_net::track::Info::default().with_max_age(None))
.unwrap();
// Each reads from the far-ahead key on, so every later group lands below its floor.
let mut subscribers: Vec<_> = (0..subscribers)
.map(|_| track.subscribe(None).with_range(HIGH..))
.collect();
let waiter = kio::Waiter::noop();
let mut high = Self {
_broadcast: broadcast,
track,
subscribers: Vec::new(),
waiter,
payload: Bytes::from(vec![0; PAYLOAD]),
next: 0,
};
high.write(HIGH);
for subscriber in &mut subscribers {
let Poll::Ready(Ok(Some(group))) = subscriber.poll_recv_group(&high.waiter) else {
unreachable!("the far-ahead group must be ready");
};
black_box(group);
}
high.subscribers = subscribers;
for _ in 0..backlog {
high.step();
}
high
}

fn write(&self, key: u64) {
let mut group = self.track.create_group(moq_net::group::Info { start: key }).unwrap();
group
.write_frame(Timestamp::unscaled(key), self.payload.clone())
.unwrap();
group.finish().unwrap();
}

/// Write the next group below the far-ahead key and wake every reader, none of which
/// hands it out.
fn step(&mut self) {
self.write(self.next);
self.next += 1;
for subscriber in &mut self.subscribers {
assert!(subscriber.poll_recv_group(&self.waiter).is_pending());
}
}
}

/// The far-ahead key `track_high_key` jumps to first.
const HIGH: u64 = 1 << 40;

/// Groups written below the far-ahead key before `track_high_key` measures.
const BACKLOG: [usize; 3] = [0, 1024, 16_384];

fn bench_high_key(c: &mut Criterion) {
let mut group = c.benchmark_group("track_high_key");
for subscribers in FANOUT {
for backlog in BACKLOG {
group.throughput(Throughput::Elements(subscribers as u64));
// Criterion calls the closure once per sample, so the fixture is built on the first call and kept.
let mut fixture = None;
group.bench_function(BenchmarkId::new(format!("{subscribers}r"), backlog), |b| {
let high = fixture.get_or_insert_with(|| HighKey::new(subscribers, backlog));
b.iter(|| high.step());
});
}
}
group.finish();
}

/// A cached aborted prefix followed by one live group.
struct AbortedScan {
_broadcast: broadcast::Producer,
Expand Down Expand Up @@ -2232,6 +2328,7 @@ criterion_group!(
bench_fanout,
bench_fanout_frame,
bench_evict_fanout,
bench_high_key,
bench_fetch_expiry,
bench_fetch_budgets,
bench_fetch_park,
Expand Down
15 changes: 10 additions & 5 deletions rs/moq-net/src/model/front.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ pub(super) struct Front {
resolved: bool,
tracks: BTreeMap<Arc<str>, Track>,
parked: BTreeSet<(Instant, Arc<str>)>,
/// Immutable track metadata, fixed by the first copy of each track. Every
/// source of the broadcast must serve the same content.
/// Immutable track metadata, fixed by the first copy of each track until it is
/// forgotten. Every source of the broadcast must serve the same content.
info: BTreeMap<Arc<str>, track::Info>,
/// How long an unread track stays before it is forgotten.
linger: Duration,
Expand Down Expand Up @@ -330,9 +330,9 @@ impl Front {
Event::Resolved { route, result } => self.resolved(route, result, &mut actions),
Event::SourceClosed { source } => self.source_closed(source, &mut actions),
// A fresh logical track, even under a name served before: an earlier
// verdict belonged to that request, and a later one asks afresh. The
// broadcast's track metadata is what persists. Unread until a reader
// shows up, so a lookup whose reader left early is forgotten too.
// verdict and metadata belonged to that request, and a later one asks
// afresh. Unread until a reader shows up, so a lookup whose reader left
// early is forgotten too.
Event::TrackAssigned { track, now } => {
self.tracks.insert(
track,
Expand Down Expand Up @@ -365,8 +365,10 @@ impl Front {
Event::Wanted { track } => self.wanted(track, &mut actions),
Event::Unwanted { track, now } => self.unwanted(track, now, &mut actions),
Event::Deadline { now } => self.deadline(now, &mut actions),
// Its metadata goes too, so a peer naming endless tracks grows nothing.
Event::Forgotten { track } => {
self.tracks.remove(&track);
self.info.remove(&track);
}
Event::Held => {
self.held = true;
Expand Down Expand Up @@ -2099,8 +2101,11 @@ mod tests {
}),
&[Action::Forget { track: name("video") }],
);
assert!(front.info.contains_key(&name("video")));
assert_actions(front.step(Event::Forgotten { track: name("video") }), &[]);
assert!(!front.tracks.contains_key(&name("video")));
// Its metadata goes with it, so endless track names grow nothing.
assert!(!front.info.contains_key(&name("video")));
}

/// A reader that looked the track up before the driver could forget it keeps
Expand Down
Loading