diff --git a/quest/m2/cluster-routing/routes/README.md b/quest/m2/cluster-routing/routes/README.md index cac32fc49f2..4b8d346b48f 100644 --- a/quest/m2/cluster-routing/routes/README.md +++ b/quest/m2/cluster-routing/routes/README.md @@ -537,7 +537,6 @@ benchmarks. ## Required -- [A relay folds received routes incrementally](/quest/m2/cluster-routing/routes/relay-receive.md) - a received route batch costs the claims it changes, not the origin's size - [Announcement availability](/quest/m2/cluster-routing/routes/availability.md) - announcements report availability, and a let-go is offline rather than END, in Rust and JS - [js/net route layer](/quest/m2/cluster-routing/routes/js.md) - `js/net` encodes, decodes, and resolves the route layer - [Route layer acceptance](/quest/m2/cluster-routing/routes/acceptance.md) - forwarding fixtures ported and the model deleted, incident fixtures, end-to-end checks, and benchmarks diff --git a/quest/m2/cluster-routing/routes/relay-receive.md b/quest/m2/cluster-routing/routes/relay-receive.md deleted file mode 100644 index e2099645052..00000000000 --- a/quest/m2/cluster-routing/routes/relay-receive.md +++ /dev/null @@ -1,28 +0,0 @@ -# [L] A relay folds received routes incrementally - -## Goal - -A relay's work per route batch it receives scales with the claims that -changed, not with the size of the origin they belong to. A benchmark swept -over both axes (claims per origin, claims changed per batch) shows the cost -flat in origin size. - -## Plan - -Planned 2026-11-09 (maintainer, /quest-complete follow-up of -[moq-dev/moq#8293](https://github.com/moq-dev/moq/pull/8293)). Milestone: ✅ -m2, a child of this epic (rejected: m3, dropping it). #8293 makes the origin -node and each stream's presentation incremental. The receive side still -does linear work per change: - -- each received batch clones the heard view and merges the whole view - (`View::merge`, `retire`); -- `node::State::home` walks every shown claim of the identity. - -So one claim change on a large origin costs a pass over all of its claims at -every relay that hears it. Fold each batch into the held view by the claims -it names, and keep whatever `home` derives up to date per change instead of -recomputing it. #8293's benchmark covers only the origin; extend it to the -receiving relay, so both axes are swept per the repository's fan-out rule. - -Public API: none expected. Wire: none. diff --git a/rs/moq-net/benches/routes.rs b/rs/moq-net/benches/routes.rs index 7abbf4c2d45..6f862af4cc0 100644 --- a/rs/moq-net/benches/routes.rs +++ b/rs/moq-net/benches/routes.rs @@ -1,14 +1,16 @@ -//! What one change to an origin's own claims costs its lite-07 route node. +//! What one change costs a lite-07 route node, at the origin and at a relay hearing it. //! -//! The node answers announce streams, each with its own interest, and each is sent -//! only the claims that changed among those its interest meets. Swept over the claims -//! the origin holds and the streams it answers: a cost that grows with the claims -//! rather than the streams the change reaches shows up as a slope. Sending a change -//! costs each stream it reaches its own batch, so the streams axis is expected to -//! grow; the claims axis is not. -//! -//! - watched: every stream watches the root, so each is sent the change. -//! - unwatched: every stream watches a prefix of its own, which the change misses. +//! - watched, unwatched: one change to the origin's own claims, swept over the claims +//! the origin holds and the streams it answers. Sending a change costs each stream +//! it reaches its own batch, so the streams axis is expected to grow; the claims +//! axis is not. In watched every stream watches the root, so each is sent the +//! change; in unwatched each watches a prefix of its own, which the change misses. +//! - received: a relay hears one batch from the origin and forwards it on, swept over +//! the claims the origin holds and the claims the batch changes. The changes axis is +//! expected to grow; the claims axis is not. +//! - lagging: a relay hears the origin on two streams, the second some one-claim +//! batches behind the first, and each hears its next batch. Swept over the claims +//! the origin holds and how far the second lags: neither axis is expected to grow. //! //! The route layer is crate-private, so this reaches it through the hidden `fuzz` module. //! @@ -20,6 +22,12 @@ use moq_net::fuzz; /// `(claims, streams)` shapes. const SHAPES: [(usize, usize); 4] = [(10, 10), (10_000, 10), (10, 1_000), (10_000, 1_000)]; +/// `(claims, changes)` shapes for a relay hearing a batch. +const RECEIVED: [(usize, usize); 4] = [(100, 1), (10_000, 1), (100, 100), (10_000, 100)]; + +/// `(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)]; + fn bench(c: &mut Criterion, name: &str, interest: fn(usize) -> String, sent: usize) { let mut group = c.benchmark_group(format!("routes/{name}")); for (claims, streams) in SHAPES { @@ -52,5 +60,34 @@ fn bench_unwatched(c: &mut Criterion) { bench(c, "unwatched", |i| format!("other/{i}"), 0); } -criterion_group!(benches, bench_watched, bench_unwatched); +fn bench_received(c: &mut Criterion) { + let mut group = c.benchmark_group("routes/received"); + for (claims, changes) in RECEIVED { + let id = BenchmarkId::from_parameter(format!("{claims}c_{changes}d")); + let mut fixture = None; + group.bench_function(id, |b| { + let relay = fixture.get_or_insert_with(|| fuzz::RouteRelay::heard(claims)); + // One serving change per claim changed, and one forwarded batch. + b.iter(|| assert_eq!(relay.change(changes), changes + 1)); + }); + } + group.finish(); +} + +fn bench_lagging(c: &mut Criterion) { + let mut group = c.benchmark_group("routes/lagging"); + for (claims, lag) in LAGGING { + let id = BenchmarkId::from_parameter(format!("{claims}c_{lag}l")); + let mut fixture = None; + group.bench_function(id, |b| { + let relay = fixture.get_or_insert_with(|| fuzz::RouteRelay::lagging(claims, lag)); + // The first stream's change is served through both streams and forwarded; + // the second's older one changes nothing. + b.iter(|| assert_eq!(relay.step(), 3)); + }); + } + group.finish(); +} + +criterion_group!(benches, bench_watched, bench_unwatched, bench_received, bench_lagging); criterion_main!(benches); diff --git a/rs/moq-net/src/fuzz.rs b/rs/moq-net/src/fuzz.rs index f7f8aae9924..69422535f43 100644 --- a/rs/moq-net/src/fuzz.rs +++ b/rs/moq-net/src/fuzz.rs @@ -554,6 +554,129 @@ impl RouteNode { } } +/// A relay's lite-07 route node hearing one origin's claims on streams it asked for, +/// and forwarding them on a stream it answers, for the routes bench. +pub struct RouteRelay { + // Dropped first: a stream disconnects from its node on drop. + receive: Vec, + answer: crate::route::node::Stream, + #[allow(dead_code, reason = "held so the streams keep a node to talk to")] + node: crate::route::Node, + instance: crate::Instance, + claims: usize, + /// The origin's revision as the first stream last heard it. + rev: u64, + /// The one-claim batches the first stream heard that the second has yet to: each + /// revision and the claim it changed. + behind: std::collections::VecDeque<(u64, usize)>, +} + +impl RouteRelay { + /// The origin's identity as the relay hears it. + const ORIGIN: u64 = 1; + + /// A relay that already heard and forwarded the origin's `claims` claims under + /// `live/`, on each of `streams` streams. + fn new(claims: usize, streams: usize) -> Self { + let node = crate::route::Node::new(2, 2); + let answer = node.answer(crate::route::Link::default(), Path::empty()).unwrap(); + let instance = crate::Instance::random(); + let rev = claims as u64; + let advert = crate::route::Advert { + seqno: 0, + metric: 0, + down_only: false, + }; + let receive = (0..streams) + .map(|_| { + let receive = node.receive(crate::route::Link::default(), Path::empty()).unwrap(); + receive.route(Self::ORIGIN, Some(crate::route::Reach::Finite(advert))); + let snapshot = (0..claims) + .map(|i| (Self::path(i), Some(Self::claim(&instance, 0)))) + .collect(); + receive.batch(Self::ORIGIN, snapshot, Some(rev)); + receive + }) + .collect(); + let relay = Self { + receive, + answer, + node, + instance, + claims, + rev, + behind: Default::default(), + }; + relay.drain(); + relay + } + + /// A relay hearing the origin's `claims` claims on one stream. + pub fn heard(claims: usize) -> Self { + Self::new(claims, 1) + } + + /// A relay hearing the origin's `claims` claims on two streams, the second `lag` + /// one-claim batches behind the first. + pub fn lagging(claims: usize, lag: usize) -> Self { + let mut relay = Self::new(claims, 2); + for _ in 0..lag { + relay.advance(); + } + relay.drain(); + relay + } + + fn path(i: usize) -> Path { + Path::new(&format!("live/{i}")).unwrap() + } + + fn claim(instance: &crate::Instance, cost: u64) -> crate::route::node::Claim { + crate::route::node::Claim { + instance: instance.clone(), + epoch: 0, + cost, + upstream: false, + learned: false, + } + } + + /// Hear one batch updating the first `changes` claims, returning how many outputs + /// the relay queued: what it serves into its origin, and what it forwards. + pub fn change(&mut self, changes: usize) -> usize { + self.rev += 1; + let claim = Self::claim(&self.instance, self.rev); + let batch = (0..changes).map(|i| (Self::path(i), Some(claim.clone()))).collect(); + self.receive[0].batch(Self::ORIGIN, batch, Some(self.rev)); + self.drain() + } + + /// The first stream hears the origin's next revision, which changes one claim. + fn advance(&mut self) { + self.rev += 1; + let i = self.rev as usize % self.claims; + let batch = vec![(Self::path(i), Some(Self::claim(&self.instance, self.rev)))]; + self.receive[0].batch(Self::ORIGIN, batch, Some(self.rev)); + self.behind.push_back((self.rev, i)); + } + + /// Each stream hears its next one-claim batch, the second still lagging, returning + /// how many outputs the relay queued. + pub fn step(&mut self) -> usize { + self.advance(); + let (rev, i) = self.behind.pop_front().expect("the second stream lags"); + let batch = vec![(Self::path(i), Some(Self::claim(&self.instance, rev)))]; + self.receive[1].batch(Self::ORIGIN, batch, Some(rev)); + self.drain() + } + + fn drain(&self) -> usize { + (self.receive.iter().chain([&self.answer])) + .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/route/mod.rs b/rs/moq-net/src/route/mod.rs index c53c4b812fa..0037f100cdc 100644 --- a/rs/moq-net/src/route/mod.rs +++ b/rs/moq-net/src/route/mod.rs @@ -262,10 +262,63 @@ struct Pending { struct Heard { /// As the neighbor sent it, to apply the next batch to. raw: View, - /// Within the neighbor's scope: what it certifies here. - view: View, + /// What it certifies here, where that is not `raw`: presented within the + /// neighbor's scope, or what it certified before its route ended. + kept: Option>, + /// The route ended: the next batch starts a fresh view. + ended: bool, /// The interest generation it answers. interest: u32, + /// The claims within the view's floors where the merged view holds something + /// else: the only ones merging the view can change. + differs: BTreeSet, +} + +impl Heard { + fn new(interest: u32) -> Self { + Self { + raw: View::default(), + kept: None, + ended: false, + interest, + differs: BTreeSet::new(), + } + } + + /// What the neighbor certifies here. + fn view(&self) -> &View { + self.kept.as_ref().unwrap_or(&self.raw) + } +} + +impl Heard { + /// Note whether `claim` differs from `merged`'s. + fn compare(&mut self, merged: Option<&View>, claim: &Path) { + let view = self.view(); + let mine = view.items.get(claim); + let differs = view.as_of(claim).is_some() && merged.and_then(|merged| merged.items.get(claim)) != mine; + match differs { + true => self.differs.insert(claim.clone()), + false => self.differs.remove(claim), + }; + } + + /// Compare every claim against `merged`. + fn resync(&mut self, merged: Option<&View>) { + 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(), + ); + } + self.differs.clear(); + for claim in &claims { + self.compare(merged, claim); + } + } } struct Peer { @@ -366,8 +419,9 @@ pub(crate) struct Router { dirty: BTreeMap, /// Each identity's context as last recomputed: a change marks every claim dirty. ctxs: BTreeMap>, - /// The identities recomputed since [`Self::changed`] last took them. - changed: BTreeSet, + /// The identities recomputed since [`Self::changed`] last took them, each with + /// the claims whose shown state changed. + changed: BTreeMap>, /// Mints a fresh identity on rotation. rng: u64, /// Per claim this node has shown, how many times it forgot that claim. @@ -412,7 +466,7 @@ impl Router { shown: BTreeMap::new(), dirty: BTreeMap::new(), ctxs: BTreeMap::new(), - changed: BTreeSet::new(), + changed: BTreeMap::new(), rng: seed | 1, #[cfg(test)] forgot: BTreeMap::new(), @@ -423,9 +477,9 @@ impl Router { } } - /// Take the identities recomputed since the last call: their selection or - /// shown claims may have changed. - pub fn changed(&mut self) -> BTreeSet { + /// Take the identities recomputed since the last call, whose selection may have + /// changed, each with the claims whose shown state did. + pub fn changed(&mut self) -> BTreeMap> { std::mem::take(&mut self.changed) } @@ -686,6 +740,8 @@ impl Router { view, }, ); + // The views heard for it merge from here on. + self.resync(old); self.mark_all(old); self.mark_all(id); self.update([old, id]); @@ -736,12 +792,13 @@ impl Router { if reach.is_none() && let Some(heard) = self.heard.get_mut(&(id, from)) { - heard.raw = View::default(); - let floors = &heard.view.floors; - heard - .view - .items + 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))); + // What it drops lay outside every floor, so nothing it differed in. + heard.ended = true; } self.update([id]); } @@ -764,35 +821,58 @@ impl Router { let scope = peer.link.scope.as_ref().map(|scope| scope.regions.clone()); let region = peer.asked.clone().unwrap_or_else(|| self.watch.clone()); let floors = floor.map(|rev| BTreeMap::from([(region, rev)])).unwrap_or_default(); - let heard = self.heard.entry((id, from)).or_insert_with(|| Heard { - raw: View::default(), - view: View::default(), - interest, - }); + let heard = self.heard.entry((id, from)).or_insert_with(|| Heard::new(interest)); // What the batch can change in this node's view: the claims it names, and // the claims a scope presents at its regions. A new stream, or certified // regions that moved, can change any claim. let mut all = heard.interest != interest; - // A new stream starts from an empty view. - if heard.interest != interest { + // A new stream, or a restarted route, starts from an empty view. + let fresh = heard.interest != interest || heard.ended; + if fresh { heard.raw = View::default(); heard.interest = interest; + heard.ended = false; } let mut dirty: Vec = changes.iter().map(|(claim, _)| claim.clone()).collect(); + let regions = heard.view().floors.keys().cloned().collect::>(); heard.raw.apply(changes, floors); - let regions = heard.view.floors.keys().cloned().collect::>(); - heard.view = match &scope { - Some(regions) => { - dirty.extend(regions.iter().cloned()); - heard.raw.present_all(regions) + match &scope { + Some(scope) => { + dirty.extend(scope.iter().cloned()); + let Heard { raw, kept, .. } = heard; + let view = match fresh { + true => kept.insert(View::default()), + false => kept.get_or_insert_default(), + }; + match fresh { + true => view.represent(raw, scope, raw.items.keys()), + false => view.represent(raw, scope, &dirty), + } } - None => heard.raw.clone(), - }; - all |= !heard.view.floors.keys().eq(regions.iter()); - let view = heard.view.clone(); + None => heard.kept = None, + } + #[cfg(test)] + if let Some(scope) = &scope { + assert_eq!(*heard.view(), heard.raw.present_all(scope), "incremental scoped view"); + } + let moved = !heard.view().floors.keys().eq(regions.iter()); + all |= moved; + // This node's own identities merge nothing, so they track no differences. + if !self.origins.contains_key(&id) { + let merged = self.views.get(&id); + match fresh || moved { + true => heard.resync(merged), + false => { + for claim in &dirty { + heard.compare(merged, claim); + } + } + } + } + let certifies: Vec = heard.view().floors.keys().cloned().collect(); // The batch certifies the regions its floors name. - for region in view.floors.keys() { + for region in &certifies { let Some(regions) = self.current.get_mut(&id) else { break; }; @@ -806,17 +886,40 @@ impl Router { } if !self.origins.contains_key(&id) { let merged = self.views.entry(id).or_default(); + let heard = &self.heard[&(id, from)]; let regions = merged.floors.keys().cloned().collect::>(); - dirty.extend(merged.merge(&view)); + #[cfg(test)] + let full = { + let mut full = merged.clone(); + full.merge(heard.view()); + full + }; + let changed = merged.merge_claims(heard.view(), &heard.differs); + #[cfg(test)] + assert_eq!(*merged, full, "incremental merge"); all |= !merged.floors.keys().eq(regions.iter()); - // The batch certifies the interest, so what lies outside can go. - let kept = merged.retire(&self.watch); - if kept != *merged { + // The batch certifies the interest, so what lies outside can go. While every + // floor lies within the interest, nothing does. + let outside = !merged.floors.keys().all(|region| within(region, &self.watch)); + #[cfg(test)] + if !outside { + assert_eq!(merged.retire(&self.watch), *merged, "nothing to retire"); + } + let retired = outside && { + let kept = merged.retire(&self.watch); + let retired = kept != *merged; *merged = kept; + retired + }; + if retired { all = true; let watch = [self.watch.clone()]; self.forgot_outside(id, &watch); + self.resync(id); + } else { + self.compare(id, &changed); } + dirty.extend(changed); } match all { true => self.mark_all(id), @@ -929,20 +1032,23 @@ impl Router { best.map(|(_, selected)| selected) } - /// Every usable feasible route to `id` through a neighbor that can carry - /// `claim`, with this node's metric through each. Any of them forwards without a - /// loop. - pub fn routes(&self, id: Id, claim: &Path) -> Vec<(N, Metric)> { + /// Every usable feasible route to `id`, with this node's metric through each. Any + /// of them forwards without a loop, if it [carries](Self::carries) the claim. + pub fn routes(&self, id: Id) -> Vec<(N, Metric)> { if self.origins.contains_key(&id) { return Vec::new(); } (self.rx.get(&id).into_iter().flatten()) .filter(|(_, entry)| !entry.held && self.feasible(id, entry.advert)) - .filter(|(next, _)| self.neighbors[*next].carries(claim)) .filter_map(|(&next, entry)| Some((next, add(entry.advert.metric, self.neighbors[&next].link.cost)?))) .collect() } + /// Whether a request for a claim at `claim` may go through `next`. + pub fn carries(&self, next: N, claim: &Path) -> bool { + self.neighbors.get(&next).is_some_and(|peer| peer.carries(claim)) + } + #[cfg(test)] /// The route this node would offer an upstream link for `id`. pub fn export(&self, id: Id) -> Option> { @@ -1144,7 +1250,7 @@ impl Router { .get(&(id, next)) .filter(|heard| heard.interest == self.interest)?; if held || !routes[&next].held { - regions.extend(heard.view.floors.keys().cloned()); + regions.extend(heard.view().floors.keys().cloned()); } } Some(regions) @@ -1182,15 +1288,35 @@ impl Router { } else { let mut rebuilt = View::default(); for next in self.certifiers(id) { - rebuilt.merge(&self.heard[&(id, next)].view); + rebuilt.merge(self.heard[&(id, next)].view()); } (rebuilt, Vec::new()) }; self.views.insert(id, rebuilt); + self.resync(id); self.forgot_outside(id, &remembered); self.mark_all(id); } + /// Compare `claims` of every view heard for `id` against the merged view, once + /// they changed there. + fn compare(&mut self, id: Id, claims: &[Path]) { + let merged = self.views.get(&id); + for (_, heard) in self.heard.range_mut((id, N::MIN)..=(id, N::MAX)) { + for claim in claims { + heard.compare(merged, claim); + } + } + } + + /// Compare every view heard for `id` against the merged view, once it was replaced. + fn resync(&mut self, id: Id) { + let merged = self.views.get(&id); + for (_, heard) in self.heard.range_mut((id, N::MIN)..=(id, N::MAX)) { + heard.resync(merged); + } + } + /// 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]) { @@ -1244,7 +1370,7 @@ impl Router { .get(&(id, next)) .filter(|heard| heard.interest == self.interest) .into_iter() - .flat_map(|heard| heard.view.uncertified()) + .flat_map(|heard| heard.view().uncertified()) .filter(move |(claim, _)| merged.is_none_or(|merged| merged.as_of(claim).is_none())) .map(move |(claim, item)| (claim, item, held)) }) @@ -1313,7 +1439,9 @@ impl Router { { self.touched += ids.len(); } - self.changed.extend(&ids); + for &id in &ids { + self.changed.entry(id).or_default(); + } for id in ids { self.prune(id); self.record(id, &peers); @@ -1397,7 +1525,7 @@ impl Router { if !answers { continue; } - let view = &self.heard[&(id, next)].view; + let view = self.heard[&(id, next)].view(); if let Some(item) = view.items.get(claim) && view.as_of(claim).is_none() { @@ -1431,7 +1559,7 @@ impl Router { .iter() .filter(|&&(_, _, answers)| answers) .any(|&(next, _, _)| { - let view = &self.heard[&(id, next)].view; + let view = self.heard[&(id, next)].view(); // A floor over the whole interest certifies everything below it. !view.floors.keys().any(|region| within(watch, region)) && below(&view.items, watch).any(|(claim, _)| { @@ -1741,10 +1869,16 @@ impl Router { for (claim, kind, item) in events { self.log(id, &claim, kind, &item); } + let changed = self.changed.entry(id).or_default(); for (claim, now) in now { + let key = (id, claim); + if self.shown.get(&key) == now.as_ref() { + continue; + } + changed.insert(key.1.clone()); match now { - Some(now) => self.shown.insert((id, claim), now), - None => self.shown.remove(&(id, claim)), + Some(now) => self.shown.insert(key, now), + None => self.shown.remove(&key), }; } } diff --git a/rs/moq-net/src/route/node.rs b/rs/moq-net/src/route/node.rs index 46a0ac02475..0a1fd43ab77 100644 --- a/rs/moq-net/src/route/node.rs +++ b/rs/moq-net/src/route/node.rs @@ -344,8 +344,15 @@ struct State { queues: Queues, /// Each shown claim of another identity, and the streams serving it. homes: BTreeMap<(Id, Path), Home>, + /// The usable feasible routes each identity's claims were last served through, + /// with the name each stream's peer gives it: while they stay the same, only a + /// claim whose shown state changed is served differently. + routes: HashMap, } +/// An identity's usable feasible routes: each stream, its metric, and the peer's name. +type Routes = Vec<(Key, Metric, Id)>; + /// Where one shown claim is served: each stream, what it was last sent, and /// whether it confirmed serving it. #[derive(Default)] @@ -399,6 +406,7 @@ impl Node { next_key: 1, queues: HashMap::new(), homes: BTreeMap::new(), + routes: HashMap::new(), })), } } @@ -646,43 +654,61 @@ impl State { self.router.echo(key, id); } } - // The shown claims below are read whole; the events only say which changed. + // The shown claims are read in place; the events only say which changed. self.router.events(); let changed = self.router.changed(); let declared = self.router.declared(); - for id in changed { + for (id, claims) in changed { if Some(id) != declared { - self.home(id); + self.home(id, claims); } } } /// Serve each shown claim of `id` through every stream with a usable feasible /// route to it that can carry the claim, keeping it where it is, unavailable, while - /// there is none. - fn home(&mut self, id: Id) { - let claims: Vec<(Path, Claim, bool)> = (self.router.shown().range((id, Path::empty())..)) - .take_while(|((shown, _), _)| *shown == id) - .map(|((_, claim), shown)| (claim.clone(), shown.item.clone(), shown.available)) - .collect(); - let mut gone: BTreeSet = (self.homes.range((id, Path::empty())..)) - .take_while(|((home, _), _)| *home == id) - .map(|((_, claim), _)| claim.clone()) + /// there is none. Only `claims`, whose shown state changed, are served anew, unless + /// the routes changed too. + fn home(&mut self, id: Id, claims: BTreeSet) { + let routes: Routes = (self.router.routes(id).into_iter()) + .map(|(key, metric)| (key, metric, self.router.name(key, id))) .collect(); + let last = self.routes.get(&id).map_or(&[][..], Vec::as_slice); + let claims = match last == routes.as_slice() { + true => claims, + false => { + match routes.is_empty() { + true => self.routes.remove(&id), + false => self.routes.insert(id, routes.clone()), + }; + let shown = + (self.router.shown().range((id, Path::empty())..)).take_while(|((shown, _), _)| *shown == id); + let homes = (self.homes.range((id, Path::empty())..)).take_while(|((home, _), _)| *home == id); + let mut claims: BTreeSet = shown.map(|((_, claim), _)| claim.clone()).collect(); + claims.extend(homes.map(|((_, claim), _)| claim.clone())); + claims + } + }; + let (shown, gone): (Vec, Vec) = + (claims.into_iter()).partition(|claim| self.router.shown().contains_key(&(id, claim.clone()))); // 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, item, available) in claims.into_iter().rev() { - gone.remove(&claim); - let routes = self.router.routes(id, &claim); + 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, Id)> = (routes.iter()) + .filter(|(key, _, _)| self.router.carries(*key, &claim)) + .copied() + .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. - let wanted: Vec<(Key, Metric, bool, Id)> = match routes.is_empty() { + let wanted: Vec<(Key, Metric, bool, Id)> = match carrying.is_empty() { true => (home.keys.iter()) .map(|(key, slot)| (*key, slot.served.metric, false, slot.named)) .collect(), - false => (routes.iter()) - .map(|&(key, metric)| (key, metric, available, self.router.name(key, id))) + false => (carrying.into_iter()) + .map(|(key, metric, named)| (key, metric, available, named)) .collect(), }; for slot in home.keys.values_mut() { @@ -739,13 +765,59 @@ impl State { ); } } + #[cfg(test)] + self.check(id, &routes); // Nothing left to recover: let the identity go. let shows = (self.router.shown().range((id, Path::empty())..).next()).is_some_and(|((shown, _), _)| *shown == id); if !self.router.holds(id) && !shows { + self.routes.remove(&id); self.router.collect(id); } } + + /// Every claim of `id` is served as serving it anew would: only shown claims have + /// a home, and each is served through every route carrying it, or kept unavailable. + #[cfg(test)] + fn check(&self, id: Id, routes: &Routes) { + let homes = (self.homes.range((id, Path::empty())..)).take_while(|((home, _), _)| *home == id); + for ((_, claim), _) in homes { + assert!( + self.router.shown().contains_key(&(id, claim.clone())), + "{claim:?} has a home but is not shown" + ); + } + let shown = (self.router.shown().range((id, Path::empty())..)).take_while(|((shown, _), _)| *shown == id); + for ((_, claim), shown) in shown { + let home = self.homes.get(&(id, claim.clone())); + let slots = home.into_iter().flat_map(|home| home.keys.iter()); + let carrying: Vec<_> = (routes.iter()) + .filter(|(key, _, _)| self.router.carries(*key, claim)) + .collect(); + if carrying.is_empty() { + for (_, slot) in slots { + assert_eq!(slot.served.claim, shown.item, "{claim:?} kept as shown"); + assert!(!slot.served.available, "{claim:?} kept unavailable"); + } + continue; + } + let slots: BTreeMap = slots.filter(|(_, slot)| !slot.leaving).map(|(k, s)| (*k, s)).collect(); + let want: BTreeMap = (carrying.iter()) + .map(|&&(key, metric, named)| { + let served = Served { + claim: shown.item.clone(), + metric, + available: shown.available, + }; + (key, (served, named)) + }) + .collect(); + let got: BTreeMap = (slots.iter()) + .map(|(key, slot)| (*key, (slot.served.clone(), slot.named))) + .collect(); + assert_eq!(got, want, "{claim:?} served through every route carrying it"); + } + } } /// The lite-06 hop a router identity travels as, so a relay translating its routes diff --git a/rs/moq-net/src/route/view.rs b/rs/moq-net/src/route/view.rs index c9d82f480f4..a1d7d3f0d49 100644 --- a/rs/moq-net/src/route/view.rs +++ b/rs/moq-net/src/route/view.rs @@ -58,17 +58,45 @@ impl View { } /// The view presented to the union of `regions`. + #[cfg(test)] pub fn present_all(&self, regions: &[Path]) -> Self { let mut out = Self::default(); + out.represent(self, regions, self.items.keys()); + out + } + + /// Bring this view of `raw`, presented to the union of `regions`, up to date once + /// `claims` of `raw` changed and its floors were replaced. + /// + /// A claim strictly below a region keeps its path, and each region is shown what + /// covers it (see [`covering`]), which is the claim exactly there when one is. Only + /// the regions' covering claims and the named claims can change, so the work + /// follows those rather than the view's size. + pub fn represent<'a>(&mut self, raw: &Self, regions: &[Path], claims: impl IntoIterator) { + for claim in claims { + // Shown at its region by the covering claim below. + if regions.contains(claim) { + continue; + } + let below = regions.iter().any(|region| claim != region && within(claim, region)); + match raw.items.get(claim).filter(|_| below) { + Some(item) => self.items.insert(claim.clone(), item.clone()), + None => self.items.remove(claim), + }; + } + for region in regions { + match covering(&raw.floors, region, |claim| raw.items.get(claim)) { + Some(item) => self.items.insert(region.clone(), item.clone()), + None => self.items.remove(region), + }; + } + self.floors.clear(); for region in regions { - let presented = self.present(region); - out.items.extend(presented.items); - for (region, floor) in presented.floors { - let entry = out.floors.entry(region).or_insert(floor); + for (region, floor) in narrow(&raw.floors, region) { + let entry = self.floors.entry(region).or_insert(floor); *entry = (*entry).max(floor); } } - out } /// What the view certifies within `prefix`: the view as presented there, less @@ -118,21 +146,40 @@ impl View { /// An uncertified claim of `other` is known at no revision, so it is not taken. /// Returns the claims whose state changed. pub fn merge(&mut self, other: &Self) -> Vec { - let mut changed = Vec::new(); let claims: BTreeSet = self.items.keys().chain(other.items.keys()).cloned().collect(); + self.merge_claims(other, &claims) + } + + /// [`Self::merge`], considering only `claims`: the same once they include every + /// claim whose state differs between the two views, since a claim held alike is + /// never taken. + pub fn merge_claims<'a>(&mut self, other: &Self, claims: impl IntoIterator) -> Vec { + // 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))); + if stale { + return Vec::new(); + } + let mut changed = Vec::new(); for claim in claims { - let (theirs, ours) = (other.as_of(&claim), self.as_of(&claim)); + let (theirs, ours) = (other.as_of(claim), self.as_of(claim)); // At one revision, a claim exactly on a wider region outranks the claim // above it that a floor on this path presented here. - let exact = theirs == ours && self.floors.contains_key(&claim) && !other.floors.contains_key(&claim); - if (theirs > ours || exact) && self.items.get(&claim) != other.items.get(&claim) { - match other.items.get(&claim) { - Some(item) => self.items.insert(claim.clone(), item.clone()), - None => self.items.remove(&claim), - }; - changed.push(claim); + let exact = theirs == ours && self.floors.contains_key(claim) && !other.floors.contains_key(claim); + if (theirs > ours || exact) && self.items.get(claim) != other.items.get(claim) { + changed.push(claim.clone()); } } + for claim in &changed { + match other.items.get(claim) { + Some(item) => self.items.insert(claim.clone(), item.clone()), + None => self.items.remove(claim), + }; + } for (region, &floor) in &other.floors { let entry = self.floors.entry(region.clone()).or_insert(floor); *entry = (*entry).max(floor);