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
69 changes: 62 additions & 7 deletions rs/moq-net/benches/origin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
//! (cursors), so a cost that grows with the size of the table rather than with
//! the tree around the touched path shows up as a slope. The table is a trie
//! keyed by path segment: an announcement visits only the cursors on the walk
//! down to its prefix and beneath it, and each recomputes its best route from
//! the entries at that prefix alone.
//! down to its prefix and beneath it. Competing routes at that prefix are
//! ranked once per change, then each cursor selects its first visible entry.
//!
//! Run with `cargo bench -p moq-net --bench origin`.

Expand All @@ -20,9 +20,9 @@ use moq_net::{Hop, Hops, Pattern, Patterns, Timestamp, announce, broadcast, kio,
const SHAPES: [(usize, usize); 3] = [(100, 10), (1_000, 100), (1_000, 1_000)];

/// `(duplicates, subscribers)` shapes for one *contended* prefix: how many
/// routes cover the same path, against how many cursors watch it. The first row
/// is a live fleet's mesh width; the rest sweep past it so the slope is visible.
const CONTENDED: [(usize, usize); 4] = [(30, 30), (60, 60), (240, 60), (240, 240)];
/// routes cover the same path, against how many cursors watch it. Includes
/// sparse fanout, a live fleet's mesh width, and larger contended tables.
const CONTENDED: [(usize, usize); 6] = [(1, 1), (30, 1), (30, 30), (60, 60), (240, 60), (240, 240)];

/// An origin with `publishers` broadcasts under `room/` and `subscribers`
/// cursors watching everything. The handles are held: dropping a broadcast
Expand Down Expand Up @@ -175,8 +175,7 @@ fn bench_announce_fleet(c: &mut Criterion) {
/// publisher produces. A mesh produces the other one: the same path arrives
/// once per peer it can travel through, so a prefix carries one entry per peer.
/// The trie narrows a change to the touched prefix, but picking the winner
/// there still visits every entry at it, once per watching cursor, so both are
/// swept.
/// there shares the ranking across its watching cursors, so both are swept.
///
/// The arriving route is priced below every incumbent so it takes the prefix
/// outright: each cursor is told twice per iteration, once for the new winner
Expand Down Expand Up @@ -219,6 +218,61 @@ fn bench_announce_duplicate(c: &mut Criterion) {
group.finish();
}

/// Re-price a live mesh route with cursors watching different scopes.
/// The challenger alternates between winning and losing, so selection must
/// honor each cursor's scope rather than reuse one global winner.
fn bench_reprice_duplicate(c: &mut Criterion) {
let mut group = c.benchmark_group("origin/reprice_duplicate");
for (duplicates, subscribers) in CONTENDED {
let id = BenchmarkId::from_parameter(format!("{duplicates}d_{subscribers}s"));
group.bench_function(id, |b| {
let (producer, _driver) = origin::Producer::new(origin::Config::default());
let _routes: Vec<_> = (1..=duplicates)
.map(|peer| {
producer
.scope(
"",
&Patterns::from(Pattern::subtree(&format!("{PATH}/p{}", (peer - 1) % 2)).unwrap()),
)
.unwrap()
.dynamic(PATH, peer_route(peer as u64, INCUMBENT_COST))
.unwrap()
})
.collect();
let challenger = duplicates as u64 + 1;
let route = producer
.dynamic(PATH, peer_route(challenger, INCUMBENT_COST + 1))
.unwrap();
let mut cursors: Vec<announce::Consumer> = (0..subscribers)
.map(|peer| {
producer
.consume()
.scope(
"",
&Patterns::from(Pattern::subtree(&format!("{PATH}/p{}", peer % 2)).unwrap()),
)
.unwrap()
.with_hidden(true)
.announced()
})
.collect();
for cursor in &mut cursors {
while cursor.next().now_or_never().flatten().is_some() {}
}
b.iter(|| {
for cost in [INCUMBENT_COST - 1, INCUMBENT_COST + 1] {
route.update(peer_route(challenger, cost)).unwrap();
for cursor in &mut cursors {
let update = cursor.next().now_or_never().flatten().expect("winner changed");
assert_eq!(update.route.cost, moq_net::origin::Cost::new(cost.min(INCUMBENT_COST)));
}
}
});
});
}
group.finish();
}

/// The contended path: one node's stats feed, which every peer in the mesh
/// carries a route to.
const PATH: &str = ".stats/p0/node/edge0";
Expand Down Expand Up @@ -429,6 +483,7 @@ criterion_group!(
bench_announce_mounted,
bench_announce_fleet,
bench_announce_duplicate,
bench_reprice_duplicate,
bench_announce_fronts,
bench_serve_idle,
bench_subscribe,
Expand Down
177 changes: 136 additions & 41 deletions rs/moq-net/src/model/origin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,50 @@ impl TableCursor {
&& self.named.as_ref().is_none_or(|(mount, _)| mount.names(&entry.prefix))
}

/// Deliver a changed winner at this presented prefix.
fn update(&mut self, presented: &PathOwned, best: Option<&RouteEntry>) {
match best {
Some(entry) => {
let meta = (entry.hops.clone(), entry.cost, entry.entered());
let served = entry.server.is_some();
let captures = self.captures(&entry.prefix);
let previous = self
.current
.insert(presented.clone(), (entry.id, meta.clone(), served, captures.clone()));
match previous {
// Unchanged metadata and servability: nothing the consumer could
// act on, even if the winning entry itself changed (a reconnect
// under an identical route is invisible, which is the point). A
// servability flip is delivered: a request that failed Unroutable
// under an advertise-only route retries on the update, and hiding
// it would park that waiter forever.
Some((_, prev, prev_served, prev_captures))
if prev == meta && prev_served == served && prev_captures == captures => {}
// Captures are consumer identity, not route metadata. Replace the
// old identity explicitly so capture-keyed consumers can remove it.
Some((_, prev, _, prev_captures)) if prev_captures != captures => {
if let Ok(mut state) = self.state.write() {
state.apply_unannounce(self.under.join(presented), prev, prev_captures);
state.apply_announce(self.under.join(presented), meta, captures);
}
}
_ => {
if let Ok(mut state) = self.state.write() {
state.apply_announce(self.under.join(presented), meta, captures);
}
}
}
}
None => {
if let Some((_, last, _, captures)) = self.current.remove(presented)
&& let Ok(mut state) = self.state.write()
{
state.apply_unannounce(self.under.join(presented), last, captures);
}
}
}
}

/// Whether the hidden rule lets this cursor discover a route at `prefix`.
fn discovers(&self, prefix: &Path) -> bool {
self.hidden.discovers(&self.heads, prefix)
Expand Down Expand Up @@ -3206,12 +3250,32 @@ impl OriginState {
fn sync_route(&mut self, prefix: &Path, claim: &Pattern) {
// Split borrows: the recompute reads `routes` while mutating a cursor.
let routes = &self.routes;
let mut candidates = None;
for id in routes.cursors_touching(prefix) {
let Some(cursor) = self.cursors.get_mut(&id) else {
continue;
};
if let Some(presented) = cursor.presented(prefix, claim) {
Self::sync_cursor(routes, cursor, &presented);
if presented.is_empty() {
// A cursor root can collapse several covering prefixes into one.
Self::sync_cursor(routes, cursor, &presented);
} else {
// Every non-root presentation refers to this exact prefix. Rank
// its routes once, then take each cursor's first visible entry.
let candidates = candidates.get_or_insert_with(|| {
let mut entries: Vec<_> = routes
.at(prefix)
.map(|entry| (route_order(prefix, entry), entry))
.collect();
entries.sort_unstable_by_key(|(order, _)| *order);
entries
});
let best = candidates
.iter()
.map(|(_, entry)| *entry)
.find(|entry| cursor.visible(entry));
cursor.update(&presented, best);
}
}
}
// The fronts and requesters under the prefix re-select from the table.
Expand Down Expand Up @@ -3257,46 +3321,7 @@ impl OriginState {
.min_by_key(|entry| route_order(&entry.prefix, entry))
});

match best {
Some(entry) => {
let meta = (entry.hops.clone(), entry.cost, entry.entered());
let served = entry.server.is_some();
let captures = cursor.captures(&entry.prefix);
let previous = cursor
.current
.insert(presented.clone(), (entry.id, meta.clone(), served, captures.clone()));
match previous {
// Unchanged metadata and servability: nothing the consumer could
// act on, even if the winning entry itself changed (a reconnect
// under an identical route is invisible, which is the point). A
// servability flip is delivered: a request that failed Unroutable
// under an advertise-only route retries on the update, and hiding
// it would park that waiter forever.
Some((_, prev, prev_served, prev_captures))
if prev == meta && prev_served == served && prev_captures == captures => {}
// Captures are consumer identity, not route metadata. Replace the
// old identity explicitly so capture-keyed consumers can remove it.
Some((_, prev, _, prev_captures)) if prev_captures != captures => {
if let Ok(mut state) = cursor.state.write() {
state.apply_unannounce(cursor.under.join(presented), prev, prev_captures);
state.apply_announce(cursor.under.join(presented), meta, captures);
}
}
_ => {
if let Ok(mut state) = cursor.state.write() {
state.apply_announce(cursor.under.join(presented), meta, captures);
}
}
}
}
None => {
if let Some((_, last, _, captures)) = cursor.current.remove(presented)
&& let Ok(mut state) = cursor.state.write()
{
state.apply_unannounce(cursor.under.join(presented), last, captures);
}
}
}
cursor.update(presented, best);
}

/// Register a cursor and replay the current best route per presented prefix.
Expand Down Expand Up @@ -6104,6 +6129,76 @@ mod tests {
assert_eq!(video.assert_next_active("").cost, Cost::new(5));
}

#[tokio::test]
async fn route_changes_preserve_each_cursors_visible_winner() {
let producer = origin(1).produce();
let consumer = producer.consume();
let mut all = consumer.clone().announced();
let mut excluded = consumer.clone().excluding(origin(7)).announced();
let mut video = consumer
.clone()
.scope("", &scopes(&["room/live/video"]))
.unwrap()
.announced();
let mut rooted = consumer
.scope("room/live", &Patterns::from(Pattern::all()))
.unwrap()
.announced();

let chat = producer
.scope("", &scopes(&["room/live/chat"]))
.unwrap()
.dynamic("room/live", Route::default().with_hops(hops(&[7])).with_cost(1))
.unwrap();
assert_eq!(all.assert_next_active("room/live").cost, Cost::new(1));
assert_eq!(rooted.assert_next_active("").cost, Cost::new(1));
excluded.assert_next_wait();
video.assert_next_wait();

let video_route = producer
.scope("", &scopes(&["room/live/video"]))
.unwrap()
.dynamic("room/live", Route::default().with_hops(hops(&[8])).with_cost(5))
.unwrap();
all.assert_next_wait();
rooted.assert_next_wait();
assert_eq!(excluded.assert_next_active("room/live").cost, Cost::new(5));
assert_eq!(video.assert_next_active("room/live").cost, Cost::new(5));

chat.update(Route::default().with_hops(hops(&[7])).with_cost(9))
.unwrap();
assert_eq!(all.assert_next_active("room/live").cost, Cost::new(5));
assert_eq!(rooted.assert_next_active("").cost, Cost::new(5));
excluded.assert_next_wait();
video.assert_next_wait();

drop(video_route);
assert_eq!(all.assert_next_active("room/live").cost, Cost::new(9));
assert_eq!(rooted.assert_next_active("").cost, Cost::new(9));
excluded.assert_next_ended("room/live");
video.assert_next_ended("room/live");
}

#[tokio::test]
async fn root_cursor_keeps_the_more_specific_covering_route() {
let producer = origin(1).produce();
let broad = producer.dynamic("room", Route::default().with_cost(1)).unwrap();
let narrow = producer.dynamic("room/live", Route::default().with_cost(9)).unwrap();
let mut announced = producer
.consume()
.scope("room/live/video", &Patterns::from(Pattern::all()))
.unwrap()
.announced();
assert_eq!(announced.assert_next_active("").cost, Cost::new(9));

broad.update(Route::default().with_cost(0)).unwrap();
announced.assert_next_wait();
narrow.update(Route::default().with_cost(8)).unwrap();
assert_eq!(announced.assert_next_active("").cost, Cost::new(8));
drop(narrow);
assert_eq!(announced.assert_next_active("").cost, Cost::new(0));
}

#[tokio::test]
async fn dynamic_accepts_a_max_depth_prefix() {
let producer = origin(1).produce();
Expand Down
Loading