diff --git a/js/net/src/lite/floors.test.ts b/js/net/src/lite/floors.test.ts index 77a01b0e89e..3c7fbc015d0 100644 --- a/js/net/src/lite/floors.test.ts +++ b/js/net/src/lite/floors.test.ts @@ -89,3 +89,21 @@ test("a regressed route's certified view decides only while a live floor certifi floors.join(NODE, lagging); expect(floors.shows(NODE, lagging.view, Path.from("live/y"))).toBeUndefined(); }); + +test("a route that leaves or floors reshows only the routes whose prefix meets its own", () => { + const floors = new Floors(); + const reshown: string[] = []; + const route = (prefix: string) => { + const { view } = carrier(prefix, 1n, [[`${prefix}/x`, A]]); + return { view, reshow: () => reshown.push(prefix) }; + }; + const routes = ["live/a", "live/a/b", "live", "live/c"].map(route); + for (const r of routes) floors.join(NODE, r); + + floors.floor(NODE, routes[0]); + expect(reshown.sort()).toEqual(["live", "live/a/b"]); + + reshown.length = 0; + floors.leave(NODE, routes[3]); + expect(reshown).toEqual(["live"]); +}); diff --git a/js/net/src/lite/floors.ts b/js/net/src/lite/floors.ts index 5f313f25e96..056a0493cd8 100644 --- a/js/net/src/lite/floors.ts +++ b/js/net/src/lite/floors.ts @@ -93,8 +93,9 @@ export class Floors { /** * The route ended or its stream closed. What its floor certified stays while another route to * the node may still carry a claim it decides; the last route takes the node's state with it. - * Every other route to the node is shown anew, since a path this one alone certified now shows - * what its streams carry. + * Every other route to the node whose prefix meets this one's is shown anew, since a path this + * one alone certified now shows what its streams carry; a route's claims lie within its prefix, + * so no other route's can change, and a session closing many prefixes costs each only its own. */ leave(node: bigint, carrier: Carrier): void { const entry = this.#nodes.get(node); @@ -107,7 +108,7 @@ export class Floors { const { prefix, hidden, revision, claims } = carrier.view; if (revision !== undefined) entry.retained.push({ prefix, hidden, revision, claims }); this.#prune(entry); - for (const other of entry.carriers) other.reshow(); + for (const other of entry.carriers) if (meets(other.view.prefix, prefix)) other.reshow(); } /** @@ -123,9 +124,10 @@ export class Floors { } /** - * A route to `node` applied a batch at its floor: every other route to it is shown anew. - * Returns whether any other view of the node existed, in which case the route's own unchanged - * claims may have changed state too, even if this floor pruned it. + * A route to `node` applied a batch at its floor: every other route to it whose prefix meets + * this one's is shown anew, as in `leave`. Returns whether any other view of the node existed, + * in which case the route's own unchanged claims may have changed state too, even if this floor + * pruned it. */ floor(node: bigint, carrier: Carrier): boolean { const entry = this.#nodes.get(node); @@ -133,7 +135,8 @@ export class Floors { const retained = entry.retained.length; this.#prune(entry); if (entry.carriers.size === 1 && retained === 0) return false; - for (const other of entry.carriers) if (other !== carrier) other.reshow(); + const prefix = carrier.view.prefix; + for (const other of entry.carriers) if (other !== carrier && meets(other.view.prefix, prefix)) other.reshow(); return true; } diff --git a/quest/m0/ci/nightly-relay-session-hour.md b/quest/m0/ci/nightly-relay-session-hour.md deleted file mode 100644 index 52738bed131..00000000000 --- a/quest/m0/ci/nightly-relay-session-hour.md +++ /dev/null @@ -1,64 +0,0 @@ -# [M] Nightly's relay session benchmarks fit their hour - -## Goal - -`Nightly (Relay session benchmarks)` (`just gh nightly rust-relay`, a paired -group that measures this commit and the previous Nightly's) finishes inside -its 60-minute `timeout-minutes` on a scheduled run and keeps its paired -regression gate. - -## Plan - -The job does not hang and the sweep did not grow: one cell's teardown went -super-linear. In -[the 11-10 run](https://github.com/moq-dev/moq/actions/runs/42842827718/job/129922278379) -each pass (this commit, then the base) spent about 22 minutes between the -`relay_session_delivery_prefixes/moq-lite-07-wip/broadcasts=1025/follow=1024` -cell and the next one; on -[11-07](https://github.com/moq-dev/moq/actions/runs/42377867523) that gap was -under a second. A local run of -`cargo bench -p moq-relay --features test-support --bench session -- --quick relay_session_delivery_prefixes/` -reproduces it: one thread spins at 100% for over 10 minutes after the lite-07 -`follow=1024` cell, while the next cell alone finishes in 2 seconds. - -The spinning thread is the cell's `retire`, dropping the viewer's lite-07 -session driver. Each of its 1024 `AnnouncePrefix` streams drops a -`route::node::Stream` (`rs/moq-net/src/route/node.rs` `Drop`), which calls -`disconnect`, then `update`, `recompute`, `prune` (`route/mod.rs`), and -`View::restrict_all` over every region still certified -(`route/view.rs`). `restrict_all` merges one restriction per region, and each -`merge_claims` costs claims x floors in `as_of` plus floors squared in its -stale check and floor `retain`. With about 1024 regions, claims, and floors, -that is about 1e9 steps per closed prefix and cubic or worse over the session. -A relay pays this whenever a lite-07 peer holding many announce prefixes -leaves, so it is a relay CPU cost a client controls, not only a benchmark -problem. - -✅ Decided 2026-11-12 (maintainer, on -[moq-dev/moq#8536](https://github.com/moq-dev/moq/pull/8536)): fix the route -layer's teardown to about linear (single-pass `restrict_all`, indexed floors, -batch disconnect), verified with `/verify-route` (rejected: temporarily drop -the lite-07 `follow=1024` Nightly cell, which hides a relay cost a client -controls). - -[Routes](/quest/m2/cluster-routing/routes/README.md) item 27 (decided -2026-11-11) replaces Babel's distance, the seqno, ROUTE_REQUEST, and the -ROUTE_END echo with per-scope paths, and -[the router](/quest/m2/cluster-routing/routes/scope.md) that builds it -deletes `prune` and the per-region floors this teardown spends its time in. -This fix still lands first, unblocked: Nightly loses its paired gate every -night until it does, the router waits behind its model, and the batch -disconnect carries over to the rebuild. Spend least on what the rebuild -deletes: prefer the batch disconnect and single-pass `restrict_all` over -indexing the per-region floors, and add the latter only if the former leave -the cell super-linear. - -Make the route layer's teardown of a neighbor's prefixes cost about linear in -what it held: for example, let `restrict_all` build one view from all regions -in a single pass instead of merging per region, index floors so `as_of` and -the floor `retain` stop scanning every floor, and disconnect a session's -prefixes as one batch. Keep `/verify-route` passing. Then confirm the Relay -session group finishes inside the hour (the 11-06 and 11-07 sweeps took -about 25 minutes) without dropping the `follow=1024` cell, which is the case -that exposed this. Add a route-layer benchmark sweeping held prefixes on -both axes if the session cell does not isolate the teardown cost. diff --git a/quest/m0/nightly/lite06-join-hang.md b/quest/m0/nightly/lite06-join-hang.md new file mode 100644 index 00000000000..b6fce85fc9b --- /dev/null +++ b/quest/m0/nightly/lite06-join-hang.md @@ -0,0 +1,36 @@ +# [M] A lite-06 viewer joining a relay reads its first group again + +## Goal + +`relay_session_join_broadcasts` and `relay_session_join_stale` finish their +moq-lite-06 cells, as they did before +[moq-dev/moq#8476](https://github.com/moq-dev/moq/pull/8476), so +`Nightly (Relay session benchmarks)` (`just gh nightly rust-relay`) finishes +inside its hour with every cell. + +## Plan + +Found 2026-11-12 while timing the relay session group for +[moq-dev/moq#8555](https://github.com/moq-dev/moq/pull/8555). The first +moq-lite-06 cell of `relay_session_join_broadcasts` +(`relays=1/publishers=1/broadcasts=1/viewers=16`) and of +`relay_session_join_stale` never leaves its warm-up: the bench thread parks in +the cell's `join` loop (`rs/moq-net/benches/session/mod.rs`) at 0% CPU, +waiting on a join, watch, or the first group read that never completes. The +moq-transport and moq-lite-07 cells of the same groups finish in seconds, and +`relay_session_join_relays` finishes on every version. + +`git bisect` over `c916855d16..75f00f92c8` (the 11-11 Nightly's commit, whose +lite-06 join cells finished in 0.2 s, to main on 2026-11-12) names +f3edb0b8d9 ([moq-dev/moq#8476](https://github.com/moq-dev/moq/pull/8476), "a +failed fetch below the end no longer holds it open") as the first hanging +commit. Reproduce with +`cargo bench -p moq-relay --features test-support --bench session -- --quick 'relay_session_join_broadcasts/moq-lite-06/'`. + +Find which await hangs, reproduce it as a moq-net integration test of a +lite-06 viewer joining a relay that holds history, and fix the cause. A +viewer that never receives a group it subscribed to is a product bug, not +only a bench one, so do not skip the cells. + +Then confirm a scheduled Nightly's relay session job finishes inside its +hour; never dispatch Nightly. diff --git a/quest/m2/cluster-routing/routes/scope.md b/quest/m2/cluster-routing/routes/scope.md index 4fbc8ef6136..ed22c1794db 100644 --- a/quest/m2/cluster-routing/routes/scope.md +++ b/quest/m2/cluster-routing/routes/scope.md @@ -83,8 +83,9 @@ Work: publisher's routes as the model has it. Keep the reader's view a function of what the router holds, computed incrementally the way `show` is today, with the test build comparing it against the whole computation. -- Keep a session's teardown about linear in the prefixes it held, as - [the relay session hour](/quest/m0/ci/nightly-relay-session-hour.md) makes it. +- Keep a session's teardown about linear in the prefixes it held, as the + routes bench's `teardown` and `refollow` cells measure + (`rs/moq-net/benches/routes.rs`). - What #8487 added to work around the old rule has no counterpart either: `recompute`'s `prune` switch, which skips pruning on a change of hops or RTT alone. Keep the qualified-key test in `infeasible` only if the new diff --git a/rs/moq-net/benches/routes.rs b/rs/moq-net/benches/routes.rs index 48aea45c498..44efbc4ca68 100644 --- a/rs/moq-net/benches/routes.rs +++ b/rs/moq-net/benches/routes.rs @@ -16,12 +16,19 @@ //! another epoch, then back at the shared one, each confirmed. Swept over the claims //! held and the streams answered: the streams axis is expected to grow, since each is //! sent the claim; the claims axis is not. +//! - teardown: a node following one origin on a stream per prefix, as a viewer session +//! holding many announce prefixes does, closes them all at once. Swept over the +//! prefixes followed and the claims each holds: both axes are expected to grow, no +//! faster than the claims held. +//! - refollow: the same node closes one prefix's stream and opens it again. The claims +//! axis is expected to grow, since the stream hears its prefix's claims again; the +//! prefixes axis no faster than the prefixes held. //! //! The route layer is crate-private, so this reaches it through the `fuzz` module. //! //! Run with `cargo bench -p moq-net --features fuzz --bench routes`. -use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main}; +use criterion::{BatchSize, BenchmarkId, Criterion, criterion_group, criterion_main}; use moq_net::fuzz; /// `(claims, streams)` shapes. @@ -33,6 +40,9 @@ const RECEIVED: [(usize, usize); 4] = [(100, 1), (10_000, 1), (100, 100), (10_00 /// `(claims, streams)` shapes for a relay holding back claims. const HELD: [(usize, usize); 4] = [(10, 10), (10_000, 10), (10, 100), (10_000, 100)]; +/// `(prefixes, claims)` shapes for a node following one origin per prefix. +const FOLLOWED: [(usize, usize); 4] = [(16, 1), (1_024, 1), (16, 16), (1_024, 16)]; + /// `(claims, lag)` shapes for a relay hearing a stream that lags another. const LAGGING: [(usize, usize); 4] = [(100, 1), (10_000, 1), (100, 100), (10_000, 10_000)]; @@ -111,12 +121,43 @@ fn bench_confirmed(c: &mut Criterion) { group.finish(); } +fn bench_teardown(c: &mut Criterion) { + let mut group = c.benchmark_group("routes/teardown"); + for (prefixes, claims) in FOLLOWED { + let id = BenchmarkId::from_parameter(format!("{prefixes}p_{claims}c")); + group.bench_function(id, |b| { + b.iter_batched( + || fuzz::RouteFollower::new(prefixes, claims), + fuzz::RouteFollower::teardown, + BatchSize::PerIteration, + ); + }); + } + group.finish(); +} + +fn bench_refollow(c: &mut Criterion) { + let mut group = c.benchmark_group("routes/refollow"); + for (prefixes, claims) in FOLLOWED { + let id = BenchmarkId::from_parameter(format!("{prefixes}p_{claims}c")); + let mut fixture = None; + group.bench_function(id, |b| { + let follower = fixture.get_or_insert_with(|| fuzz::RouteFollower::new(prefixes, claims)); + // The reopened stream serves its prefix's claims into the origin again. + b.iter(|| assert_eq!(follower.refollow(), claims)); + }); + } + group.finish(); +} + criterion_group!( benches, bench_watched, bench_unwatched, bench_received, bench_lagging, - bench_confirmed + bench_confirmed, + bench_teardown, + bench_refollow ); criterion_main!(benches); diff --git a/rs/moq-net/src/fuzz.rs b/rs/moq-net/src/fuzz.rs index dbbffbb4948..ba7746bea8e 100644 --- a/rs/moq-net/src/fuzz.rs +++ b/rs/moq-net/src/fuzz.rs @@ -800,6 +800,88 @@ impl RouteRelay { } } +/// A lite-07 route node following one origin on a stream per prefix, as a viewer +/// session holding many announce prefixes does, for the routes bench. +pub struct RouteFollower { + node: crate::route::Node, + /// Each prefix's stream, `None` only while [`Self::refollow`] replaces it. + streams: Vec>, + instance: crate::Instance, + claims: usize, + /// The next stream [`Self::refollow`] replaces. + next: usize, +} + +impl RouteFollower { + /// The origin's identity and revision as the node hears them. + const ORIGIN: u64 = 1; + const REV: u64 = 1; + + /// A node following `prefixes` prefixes `live/{k}`, each holding `claims` claims. + pub fn new(prefixes: usize, claims: usize) -> Self { + let mut follower = Self { + node: crate::route::Node::new(2, 2), + streams: Vec::new(), + instance: crate::Instance::random(), + claims, + next: 0, + }; + follower.streams = (0..prefixes).map(|k| Some(follower.follow(k))).collect(); + follower.drain(); + follower + } + + /// Open the stream for prefix `k`, which hears the origin's route and snapshot. + fn follow(&self, k: usize) -> crate::route::node::Stream { + let prefix = Path::new(&format!("live/{k}")).unwrap(); + let stream = self.node.receive(crate::route::Link::default(), prefix).unwrap(); + let advert = crate::route::Advert { + seqno: 0, + metric: 0, + down_only: false, + rtt: None, + hops: [].into(), + }; + stream.route(Self::ORIGIN, Some(crate::route::Reach::Finite(advert))); + let claim = crate::route::node::Claim { + instance: self.instance.clone(), + epoch: 0, + cost: 0, + upstream: false, + learned: false, + chain: crate::Hops::new(), + }; + let snapshot = (0..self.claims) + .map(|j| (Path::new(&format!("live/{k}/{j}")).unwrap(), Some(claim.clone()))) + .collect(); + stream.batch(Self::ORIGIN, snapshot, Some(Self::REV)); + stream + } + + /// Close one prefix's stream and open it again, returning how many outputs the + /// node queued. + pub fn refollow(&mut self) -> usize { + let k = self.next % self.streams.len(); + self.next += 1; + // Closed first, so the node lets go of the prefix before hearing it again. + drop(self.streams[k].take()); + self.streams[k] = Some(self.follow(k)); + self.drain() + } + + /// Close every stream at once, as a session ending does. + pub fn teardown(self) { + let _closing = self.node.closing(); + drop(self.streams); + } + + fn drain(&self) -> usize { + (self.streams.iter().flatten()) + .map(|stream| std::iter::from_fn(|| stream.try_output()).count()) + .sum() + } +} + /// What a client session fed into an origin hears it should ask its peer about, for /// the interests bench. pub struct Interests(crate::model::Interests); diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index e5726960cc8..0c0c50a2f76 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -290,6 +290,14 @@ impl Publisher { } } +impl Drop for Publisher { + fn drop(&mut self) { + // Every announce stream closes with the session: disconnect them together. + let _closing = self.shared.origin.route_closing(); + self.children = kio::Tasks::new(); + } +} + impl Publisher { /// The peer's latest PROBE target, or zero while it asks for none. pub fn peer_target(&self) -> kio::Consumer { diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 8cbbce1efd3..8a61a7e3af5 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -1210,6 +1210,9 @@ impl Drop for SubscriberDriver { // `abort` already recorded a session error when the driver failed. A driver // dropped without that still has to name an end before its setup machines drop. self.subscriber.note_end(&Error::Cancel); + // Every announce stream closes with the session: disconnect them together. + let _closing = self.subscriber.origin.route_closing(); + self.announces = kio::Tasks::new(); } } diff --git a/rs/moq-net/src/model/origin.rs b/rs/moq-net/src/model/origin.rs index 3b0a4fce79f..b1a1f4bf8a6 100644 --- a/rs/moq-net/src/model/origin.rs +++ b/rs/moq-net/src/model/origin.rs @@ -2353,6 +2353,13 @@ impl Producer { self.consume().route_node() } + /// See [`Consumer::route_closing`]. + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + pub(crate) fn route_closing(&self) -> Option { + let node = self.shared.lock().route_node.clone(); + node.map(|(node, _)| node.closing()) + } + /// The lite-06 hop this node's route identity travels as, once a moq-lite-07 /// session created the route node: a relay translating this node's claims for a /// lite-06 peer starts their chain with it. It is this origin's own hop until the @@ -7391,6 +7398,15 @@ impl Consumer { id.and_then(NonZeroU64::new) } + /// Hold back the disconnect of every route stream dropped until the guard is + /// released, so a session closing its streams costs the route layer one recompute; + /// `None` while no session created the route node, which then has no streams. + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + pub(crate) fn route_closing(&self) -> Option { + let node = self.shared.lock().route_node.clone(); + node.map(|(node, _)| node.closing()) + } + /// The route layer node every lite-07 session on this origin shares, created on /// first use along with the task that feeds it the routes this node serves /// itself: everything in the table the route layer did not select. It is caught up diff --git a/rs/moq-net/src/route/mod.rs b/rs/moq-net/src/route/mod.rs index a30d2296c53..cb4d194bd29 100644 --- a/rs/moq-net/src/route/mod.rs +++ b/rs/moq-net/src/route/mod.rs @@ -43,7 +43,7 @@ pub(crate) use proof::{Proof, Transcript}; #[cfg(feature = "async")] pub(crate) use rtt::LinkRtt; pub(crate) use view::{View, within}; -use view::{ancestors, below, covering, narrow}; +use view::{above, ancestors, below, covering, narrow, within_any}; /// An opaque routing identity, declared by an origin or derived by [`Scope`]. pub(crate) type Id = u64; @@ -334,11 +334,15 @@ impl Heard { let view = self.view(); let mut claims: BTreeSet = view.items.keys().cloned().collect(); if let Some(merged) = merged { - claims.extend( - (merged.items.keys()) - .filter(|claim| view.floors.keys().any(|region| within(claim, region))) - .cloned(), - ); + for region in view.floors.keys() { + let exact = merged.items.get_key_value(region); + claims.extend( + exact + .into_iter() + .chain(below(&merged.items, region)) + .map(|(claim, _)| claim.clone()), + ); + } } self.differs.clear(); for claim in &claims { @@ -551,26 +555,34 @@ impl Router { Ok(()) } - /// The session to `neighbor` closed: everything learned on it goes, and it - /// counts as the echo of every ROUTE_END sent on it. - pub fn disconnect(&mut self, neighbor: N) { - if self.neighbors.remove(&neighbor).is_none() { + /// The sessions to `neighbors` closed: everything learned on them goes, and each + /// counts as the echo of every ROUTE_END sent on it. Closing them together + /// recomputes each identity once, rather than once per neighbor. + pub fn disconnect(&mut self, neighbors: impl IntoIterator) { + let gone: BTreeSet = (neighbors.into_iter()) + .filter(|neighbor| self.neighbors.remove(neighbor).is_some()) + .collect(); + if gone.is_empty() { return; } let mut ids = BTreeSet::new(); for (id, routes) in self.rx.iter_mut() { - if routes.remove(&neighbor).is_some() { + let before = routes.len(); + routes.retain(|next, _| !gone.contains(next)); + if routes.len() != before { ids.insert(*id); } } self.rx.retain(|_, routes| !routes.is_empty()); for (id, pending) in self.pending.iter_mut() { - if pending.from.remove(&neighbor) { + let before = pending.from.len(); + pending.from.retain(|from| !gone.contains(from)); + if pending.from.len() != before { ids.insert(*id); } } let mut on = |(id, n): &(Id, N)| { - let on = *n == neighbor; + let on = gone.contains(n); if on { ids.insert(*id); } @@ -864,8 +876,7 @@ impl Router { let raw = std::mem::take(&mut heard.raw); let kept = heard.kept.get_or_insert(raw); let floors = &kept.floors; - kept.items - .retain(|claim, _| floors.keys().any(|region| within(claim, region))); + kept.items.retain(|claim, _| above(floors, claim).next().is_some()); // What it drops lay outside every floor, so nothing it differed in. heard.ended = true; } @@ -982,7 +993,7 @@ impl Router { }; if retired { all = true; - let watch = [self.watch.clone()]; + let watch = BTreeSet::from([self.watch.clone()]); self.forgot_outside(id, &watch); self.resync(id); } else { @@ -1059,7 +1070,7 @@ impl Router { self.heard.remove(&key); } self.pending.remove(&id); - self.forgot_outside(id, &[]); + self.forgot_outside(id, &BTreeSet::new()); self.mark_all(id); self.update([id]); } @@ -1131,6 +1142,14 @@ impl Router { self.neighbors.get(&next).is_some_and(|peer| peer.carries(claim)) } + /// The prefix this node asked `next` for, outside which it carries no claim: the + /// root when it asked for none. + pub fn asked(&self, next: N) -> Path { + (self.neighbors.get(&next)) + .and_then(|peer| peer.asked.clone()) + .unwrap_or_default() + } + #[cfg(test)] /// The route this node would offer an upstream link for `id`. pub fn export(&self, id: Id) -> Option> { @@ -1349,9 +1368,9 @@ impl Router { /// The regions of `id` that this node's certifiers certify, those whose /// route is held only when `held`, or `None` while it holds no route or a /// certifier has not yet answered the current interest. - fn certified(&self, id: Id, held: bool) -> Option> { + fn certified(&self, id: Id, held: bool) -> Option> { let routes = self.rx.get(&id)?; - let mut regions = Vec::new(); + let mut regions = BTreeSet::new(); for next in self.certifiers(id) { let heard = self .heard @@ -1377,10 +1396,8 @@ impl Router { let Some(merged) = self.views.get(&id) else { return; }; - let lost = merged - .floors - .iter() - .filter(|(floor, _)| !regions.iter().any(|region| within(floor, region))) + let lost = (merged.floors.iter()) + .filter(|(floor, _)| !within_any(floor, ®ions)) .map(|(_, &rev)| rev) .max(); let Some(lost) = lost else { @@ -1398,7 +1415,7 @@ impl Router { for next in self.certifiers(id) { rebuilt.merge(self.heard[&(id, next)].view()); } - (rebuilt, Vec::new()) + (rebuilt, BTreeSet::new()) }; self.views.insert(id, rebuilt); self.resync(id); @@ -1427,10 +1444,10 @@ impl Router { /// Count a forget against each shown claim of `id` overlapping none of `kept`. #[cfg_attr(not(test), allow(unused_variables, reason = "only fixtures count forgets"))] - fn forgot_outside(&mut self, id: Id, kept: &[Path]) { + fn forgot_outside(&mut self, id: Id, kept: &BTreeSet) { #[cfg(test)] for ((key, claim), count) in self.forgot.iter_mut() { - if *key == id && !kept.iter().any(|region| within(claim, region)) { + if *key == id && !within_any(claim, kept) { *count += 1; } } @@ -1970,9 +1987,7 @@ impl Router { } } None => { - let uncertified = regions - .as_ref() - .is_some_and(|regions| !regions.iter().any(|region| within(claim, region))) + let uncertified = regions.as_ref().is_some_and(|regions| !within_any(claim, regions)) || !ctx.own && self.views.get(&id).is_none_or(|view| view.as_of(claim).is_none()); // A claim presented at a narrower region moved up to the claim it // stood for, which is still shown. That claim may have changed @@ -2036,7 +2051,6 @@ impl Router { false => None, }; let current = self.current.get(&id); - let within_any = |claim: &Path, regions: &[Path]| regions.iter().any(|region| within(claim, region)); let at = |claim: &Path| { if !within(claim, &self.watch) { return None; @@ -2048,7 +2062,8 @@ impl Router { false => None, }; // A claim a stream answering the current interest carries is current. - let certified = loose.is_some() || current.is_none_or(|regions| within_any(claim, regions)); + let certified = + loose.is_some() || current.is_none_or(|regions| regions.iter().any(|region| within(claim, region))); if !certified { return None; } diff --git a/rs/moq-net/src/route/node.rs b/rs/moq-net/src/route/node.rs index 297418992a8..8b202c32a18 100644 --- a/rs/moq-net/src/route/node.rs +++ b/rs/moq-net/src/route/node.rs @@ -31,7 +31,7 @@ use rand::RngExt; use super::{ Error, GENERATED, Id, Link, Metric, Out, QUALIFIED, Reach, Rev, Router, Seqno, - view::{ancestors, below}, + view::{above, ancestors, below}, }; /// One announce stream in one direction. @@ -520,6 +520,12 @@ struct State { /// Answer streams of a node that does not forward, which join `links` once this /// node's route goes out on them. origins: HashSet, + /// How many [`Closing`] guards are held: while any is, a dropped stream joins + /// `closed` instead of disconnecting on its own. + closing: usize, + /// The streams dropped while a [`Closing`] guard was held, disconnected together + /// once the last is released. + closed: Vec, } /// Each shown claim of another identity, and the streams serving it. @@ -589,6 +595,8 @@ impl Node { routes: HashMap::new(), links: HashSet::new(), origins: HashSet::new(), + closing: 0, + closed: Vec::new(), })), } } @@ -663,7 +671,7 @@ impl Node { state.origins.insert(key); } if let Err(err) = open(&mut state.router, key) { - state.router.disconnect(key); + state.router.disconnect([key]); state.queues.remove(&key); state.origins.remove(&key); state.links.remove(&key); @@ -677,6 +685,14 @@ impl Node { }) } + /// Hold back the disconnect of every stream dropped until the guard is released, + /// then disconnect them together: a session closing many streams at once costs the + /// route layer one recompute rather than one per stream. + pub fn closing(&self) -> Closing { + self.lock().closing += 1; + Closing { node: self.clone() } + } + #[cfg(test)] pub(crate) fn retained(&self) -> super::Retained { self.lock().router.retained() @@ -820,24 +836,27 @@ impl Stream { impl Drop for Stream { fn drop(&mut self) { let mut state = self.node.lock(); - let state = &mut *state; - if let Some(queue) = state.queues.remove(&self.key) { - for (id, (held, _)) in &queue.read().withheld { - for path in held.keys() { - unwait(&mut state.waiting, *id, path, self.key); - } - } + match state.closing { + 0 => state.close(vec![self.key]), + _ => state.closed.push(self.key), } - state.router.disconnect(self.key); - // Its session is gone, and what it served with it. - for home in state.homes.values_mut() { - home.keys.remove(&self.key); - } - state.origins.remove(&self.key); - if state.links.remove(&self.key) { - let _ = state.router.bump(); + } +} + +/// Holds back the disconnect of each stream dropped meanwhile, then disconnects them +/// together on drop. See [`Node::closing`]. +pub(crate) struct Closing { + node: Node, +} + +impl Drop for Closing { + fn drop(&mut self) { + let mut state = self.node.lock(); + state.closing -= 1; + if state.closing == 0 && !state.closed.is_empty() { + let closed = std::mem::take(&mut state.closed); + state.close(closed); } - state.flush(); } } @@ -851,6 +870,32 @@ fn push(queues: &Queues, key: Key, output: Output) { impl State { /// Route the router's messages and shown claims to the streams. Claims are served /// before the batches carrying them go, so a batch knows what is not served yet. + /// Disconnect the dropped streams `keys`: their sessions are gone, and what they + /// served with them. + fn close(&mut self, keys: Vec) { + for &key in &keys { + if let Some(queue) = self.queues.remove(&key) { + for (id, (held, _)) in &queue.read().withheld { + for path in held.keys() { + unwait(&mut self.waiting, *id, path, key); + } + } + } + } + self.router.disconnect(keys.iter().copied()); + let keys: HashSet = keys.into_iter().collect(); + for home in self.homes.values_mut() { + home.keys.retain(|key, _| !keys.contains(key)); + } + self.origins.retain(|key| !keys.contains(key)); + let linked = self.links.len(); + self.links.retain(|key| !keys.contains(key)); + if self.links.len() != linked { + let _ = self.router.bump(); + } + self.flush(); + } + fn flush(&mut self) { loop { let outs = self.router.drain(); @@ -947,15 +992,26 @@ impl State { let changed: Vec = claims.iter().cloned().collect(); let (shown, gone): (Vec, Vec) = (claims.into_iter()).partition(|claim| self.router.shown().contains_key(&(id, claim.clone()))); + // Each route by the prefix its stream asked for, so a claim looks up the routes + // that may carry it per segment rather than checking every route. + let mut asked: BTreeMap> = BTreeMap::new(); + for (index, (key, ..)) in routes.iter().enumerate() { + asked.entry(self.router.asked(*key)).or_default().push(index); + } // Most specific first: a job continuing its claim's instance is served before // the claim moves on, so a reader resolved through the claim finds the job's route. for claim in shown.into_iter().rev() { let shown = &self.router.shown()[&(id, claim.clone())]; let (item, available) = (shown.item.clone(), shown.available); - let carrying: Vec<(Key, Metric, Option, Id)> = (routes.iter()) - .filter(|(key, ..)| self.router.carries(*key, &claim)) + let mut indices: Vec = above(&asked, &claim) + .flat_map(|(_, indices)| indices) .copied() .collect(); + indices.sort_unstable(); + let carrying: Vec<(Key, Metric, Option, Id)> = (indices.into_iter()) + .map(|index| routes[index]) + .filter(|(key, ..)| self.router.carries(*key, &claim)) + .collect(); let home = self.homes.entry((id, claim.clone())).or_default(); // With no route left, the router no longer knows the name a stream's peer // gave the identity, so it keeps the one it was served under. diff --git a/rs/moq-net/src/route/tests/compare.rs b/rs/moq-net/src/route/tests/compare.rs index 9e3e6c43f70..bfd764464bd 100644 --- a/rs/moq-net/src/route/tests/compare.rs +++ b/rs/moq-net/src/route/tests/compare.rs @@ -129,7 +129,7 @@ impl Net for Sim { self.acks.remove(&key); self.held.remove(&key); } - self.nodes[peer].disconnect(node); + self.nodes[peer].disconnect([node]); self.flush(); self.closed(peer); } diff --git a/rs/moq-net/src/route/tests/mod.rs b/rs/moq-net/src/route/tests/mod.rs index 30d75ac9d56..73ab437a4b8 100644 --- a/rs/moq-net/src/route/tests/mod.rs +++ b/rs/moq-net/src/route/tests/mod.rs @@ -261,7 +261,7 @@ impl Sim { self.queues.remove(&(x, y)); self.acks.remove(&(x, y)); self.held.remove(&(x, y)); - self.nodes[y].disconnect(x); + self.nodes[y].disconnect([x]); } self.flush(); } diff --git a/rs/moq-net/src/route/tests/routing.rs b/rs/moq-net/src/route/tests/routing.rs index 05be36f9dbb..397c2dc3679 100644 --- a/rs/moq-net/src/route/tests/routing.rs +++ b/rs/moq-net/src/route/tests/routing.rs @@ -992,12 +992,62 @@ fn a_fresh_uncertified_claim_waits_on_an_older_floor() { match refresh { true => router.batch(certifier, 7, vec![(p("live/x"), Some(item))], Some(2), 0), - false => router.disconnect(certifier), + false => router.disconnect([certifier]), } assert_eq!(router.shows(7, &p("live/x")), Some(true), "refresh {refresh}"); } } +/// Closing a session's streams together leaves the node as closing them one at a +/// time does: each stream asks its own prefix, and some share one, so a region one +/// closes stays certified by another while the rest let go. Once none is left, the +/// reader sees the same; the view stays, as a node with no route keeps it until a +/// route returns or it collects the identity. +#[test] +fn closing_streams_together_matches_closing_them_in_turn() { + let finite = Some(Reach::Finite(Advert { + seqno: 0, + metric: 1, + down_only: false, + rtt: None, + hops: [].into(), + })); + let item = |seed| Item { + seed, + epoch: None, + since: 1, + }; + let opened = || { + let mut router: Router = Router::new(99, 1); + for stream in 0..6 { + let prefix = format!("live/{}", stream % 4); + router.connect(stream, Link::default()).unwrap(); + router.asks(stream, Path::new(&prefix).unwrap()).unwrap(); + router.route(stream, 7, finite.clone()); + let claims = (0..3) + .map(|j| (Path::new(&format!("{prefix}/{j}")).unwrap(), Some(item(j)))) + .collect(); + router.batch(stream, 7, claims, Some(1), 0); + } + router + }; + for closed in [vec![0, 1, 4], vec![0, 1, 2, 3, 4, 5]] { + let mut together = opened(); + together.disconnect(closed.iter().copied()); + let mut in_turn = opened(); + for &stream in &closed { + in_turn.disconnect([stream]); + } + assert_eq!(together.shown(), in_turn.shown(), "closed {closed:?}"); + if closed.len() < 6 { + assert_eq!(together.view(7), in_turn.view(7), "closed {closed:?}"); + assert_eq!(together.retained(), in_turn.retained(), "closed {closed:?}"); + } else { + assert!(together.shown().is_empty()); + } + } +} + /// An echo travels apart from the echoing node's routes, so it can overtake a /// route that node derived from this one's. b learned its route from a and sent it /// back, then ended it on a's ROUTE_END. Its echo arrives first, so a drops its diff --git a/rs/moq-net/src/route/view.rs b/rs/moq-net/src/route/view.rs index a1d7d3f0d49..fd33433beb4 100644 --- a/rs/moq-net/src/route/view.rs +++ b/rs/moq-net/src/route/view.rs @@ -104,27 +104,36 @@ impl View { pub fn restrict(&self, prefix: &Path) -> Self { let mut out = self.present(prefix); let floors = &out.floors; - out.items - .retain(|claim, _| floors.keys().any(|region| within(claim, region))); + out.items.retain(|claim, _| above(floors, claim).next().is_some()); out } - /// The view restricted to the union of `regions`. - pub fn restrict_all(&self, regions: &[Path]) -> Self { + /// The view restricted to the union of `regions`, as merging each region's + /// restriction would leave it, in one pass. + /// + /// A region within another adds nothing the wider one's restriction lacks: the + /// floors over it narrow to the wider region, where they certify the claim at the + /// region itself at its own path. So only the widest regions count, and those are + /// disjoint, so their restrictions merge as a union. A floor a wider one at least + /// as new covers then goes, as [`Self::merge`] drops it. + pub fn restrict_all(&self, regions: &BTreeSet) -> Self { let mut out = Self::default(); for region in regions { - out.merge(&self.restrict(region)); + let wider = heads(region).filter(|&head| head != region.as_str()); + if wider.into_iter().any(|head| regions.contains(head)) { + continue; + } + let Self { floors, items } = self.restrict(region); + out.floors.extend(floors); + out.items.extend(items); } + settle(&mut out.floors); out } /// The newest revision this view knows `claim` at, if a region certifies it. pub fn as_of(&self, claim: &Path) -> Option { - self.floors - .iter() - .filter(|(region, _)| within(claim, region)) - .map(|(_, floor)| *floor) - .max() + above(&self.floors, claim).map(|(_, &floor)| floor).max() } /// The claims no floor certifies. @@ -157,10 +166,8 @@ impl View { // Every floor of `other` lies within a newer one here, so it knows no claim at a // newer revision and changes nothing: a lagging view costs nothing to merge, // however far it lags. - let stale = other - .floors - .iter() - .all(|(region, &floor)| (self.floors.iter()).any(|(wider, &newer)| newer > floor && within(region, wider))); + let stale = + (other.floors.iter()).all(|(region, &floor)| above(&self.floors, region).any(|(_, &newer)| newer > floor)); if stale { return Vec::new(); } @@ -184,12 +191,7 @@ impl View { let entry = self.floors.entry(region.clone()).or_insert(floor); *entry = (*entry).max(floor); } - let floors = self.floors.clone(); - self.floors.retain(|region, floor| { - !floors - .iter() - .any(|(wider, newer)| wider != region && within(region, wider) && newer >= floor) - }); + settle(&mut self.floors); changed } @@ -206,10 +208,9 @@ impl View { for (region, &floor) in &self.floors { if Some(floor) > newest && !kept.floors.contains_key(region) { kept.floors.insert(region.clone(), floor); - for (claim, item) in &self.items { - if within(claim, region) { - kept.items.insert(claim.clone(), item.clone()); - } + let exact = self.items.get_key_value(region); + for (claim, item) in exact.into_iter().chain(below(&self.items, region)) { + kept.items.insert(claim.clone(), item.clone()); } } } @@ -283,14 +284,14 @@ pub(crate) fn covering<'a, A>( if claim == *prefix { return Some(found); } - let over = || floors.iter().filter(|(region, _)| within(prefix, region)); - let Some(floor) = over().map(|(_, &rev)| rev).max() else { + let Some(floor) = above(floors, prefix).map(|(_, &rev)| rev).max() else { return Some(found); }; - let certifier = over() + // The most specific of the newest, since the root comes first. + let certifier = above(floors, prefix) .filter(|&(_, &rev)| rev == floor) .map(|(region, _)| region) - .max_by_key(|region| region.len()); + .last(); match certifier.is_some_and(|certifier| !within(&claim, certifier)) { true => None, false => Some(found), @@ -300,21 +301,46 @@ pub(crate) fn covering<'a, A>( /// `floors` narrowed to `prefix`: a region meeting it becomes whichever of the two /// lies within the other, keeping the newest floor where two narrow alike. pub(crate) fn narrow(floors: &BTreeMap, prefix: &Path) -> BTreeMap { - let mut out: BTreeMap = BTreeMap::new(); - for (region, &floor) in floors { - let narrowed = if within(region, prefix) { - region - } else if within(prefix, region) { - prefix - } else { - continue; - }; - let entry = out.entry(narrowed.clone()).or_insert(floor); - *entry = (*entry).max(floor); + let mut out: BTreeMap = (below(floors, prefix)) + .map(|(region, &floor)| (region.clone(), floor)) + .collect(); + if let Some(floor) = above(floors, prefix).map(|(_, &rev)| rev).max() { + out.insert(prefix.clone(), floor); } out } +/// The entries of `map` at `path` or above it, the root first: one lookup per +/// segment, however many entries the map holds. +pub(crate) fn above<'a, V>(map: &'a BTreeMap, path: &'a Path) -> impl Iterator { + heads(path).filter_map(|head| map.get_key_value(head)) +} + +/// Whether `path` lies within any of `regions`: one lookup per segment. +pub(crate) fn within_any(path: &Path, regions: &BTreeSet) -> bool { + heads(path).any(|head| regions.contains(head)) +} + +/// `path` and every prefix above it, the root first, as the strings a `Path`-keyed +/// map is looked up by. +fn heads(path: &Path) -> impl Iterator { + let s = path.as_str(); + let ends = s.match_indices('/').map(|(end, _)| end); + (std::iter::once(0).chain(ends).chain((!s.is_empty()).then_some(s.len()))).map(move |end| &s[..end]) +} + +/// Drop each region a wider one at least as new covers: what it certifies, the +/// wider one already does. +fn settle(floors: &mut BTreeMap) { + let covered: Vec = (floors.iter()) + .filter(|&(region, &floor)| above(floors, region).any(|(wider, &newer)| wider != region && newer >= floor)) + .map(|(region, _)| region.clone()) + .collect(); + for region in covered { + floors.remove(®ion); + } +} + #[cfg(test)] mod tests { use super::*; @@ -413,4 +439,62 @@ mod tests { got.apply(gone.diff(&new), gone.floors.clone()); assert_eq!(got, gone); } + + #[test] + fn restrict_all_ignores_a_region_within_another() { + // A newer floor over `live` but not `live/a`, and an older one exactly there: + // restricting to `live/a` alone presents `live`'s claim at `live/a`, which the + // wider region knows is no claim of its own. + let v = view(&[("live", 5), ("live/a", 3)], &[("live", 1)]); + let wide = view(&[("live", 5)], &[("live", 1)]); + assert_eq!(v.restrict_all(&BTreeSet::from([p("live/a"), p("live")])), wide); + } + + /// Random views over paths that share segments and string prefixes (`a` and `ab`). + #[test] + fn lookups_match_a_scan_of_every_floor() { + const PATHS: [&str; 9] = ["", "a", "a/b", "a/b/c", "a/c", "ab", "ab/c", "b", "b/a"]; + let mut seed = 0x9e37_79b9_7f4a_7c15_u64; + let mut next = |below: u64| { + seed ^= seed << 13; + seed ^= seed >> 7; + seed ^= seed << 17; + seed % below + }; + for _ in 0..2_000 { + let mut v = View::::default(); + for path in PATHS { + if next(3) == 0 { + v.floors.insert(p(path), next(4)); + } + if next(2) == 0 { + v.items.insert(p(path), next(3) as u32); + } + } + for path in PATHS.map(p) { + let scan = v.floors.iter().filter(|(region, _)| within(&path, region)); + assert_eq!(v.as_of(&path), scan.map(|(_, &floor)| floor).max()); + let mut narrowed = BTreeMap::new(); + for (region, &floor) in &v.floors { + let key = match (within(region, &path), within(&path, region)) { + (true, _) => region.clone(), + (_, true) => path.clone(), + _ => continue, + }; + let entry = narrowed.entry(key).or_insert(floor); + *entry = (*entry).max(floor); + } + assert_eq!(narrow(&v.floors, &path), narrowed); + } + let regions: BTreeSet = PATHS.iter().filter(|_| next(3) == 0).map(|path| p(path)).collect(); + let widest: Vec<&Path> = (regions.iter()) + .filter(|®ion| !regions.iter().any(|wider| wider != region && within(region, wider))) + .collect(); + let mut folded = View::default(); + for region in widest.iter().rev() { + folded.merge(&v.restrict(region)); + } + assert_eq!(v.restrict_all(®ions), folded); + } + } }