diff --git a/rs/moq-net/src/model/origin.rs b/rs/moq-net/src/model/origin.rs index 5702e23521..1473c30498 100644 --- a/rs/moq-net/src/model/origin.rs +++ b/rs/moq-net/src/model/origin.rs @@ -1099,11 +1099,12 @@ struct OriginConsumerState { /// The entries held back, by when a poll looks at them again. parked: BTreeMap<(Instant, u64), Path>, next_seq: u64, - /// Prefixes whose most recently delivered update was an announce. A pending - /// `Announce` is ambiguous on its own: it is an unseen initial announce (a - /// retraction cancels it entirely) or a metadata update on a route the - /// consumer already observed (a retraction must still be delivered). - delivered: BTreeSet, + /// Prefixes whose most recently delivered update was an announce, with the + /// instance it named. A pending `Announce` is ambiguous on its own: it is an + /// unseen initial announce (a retraction cancels it entirely) or a metadata + /// update on a route the consumer already observed (a retraction must still be + /// delivered). The instance tells a restart back to it from one onto another. + delivered: BTreeMap, /// Set by the origin's teardown: the cursor drains `pending`, then reports /// the end instead of parking forever on a table that can never fire again. ended: bool, @@ -1135,7 +1136,7 @@ impl OriginConsumerState { fn owe(&mut self) { self.owed = 0; for (prefix, pending) in &mut self.pending { - pending.owed = !self.delivered.contains(prefix) + pending.owed = !self.delivered.contains_key(prefix) || matches!( pending.update, Some(PendingUpdate::Unannounce(..) | PendingUpdate::UnannounceAnnounce { .. }) @@ -1217,8 +1218,11 @@ impl OriginConsumerState { Some(PendingUpdate::Unannounce(old, gone) | PendingUpdate::UnannounceAnnounce { old, gone, .. }) => { PendingUpdate::UnannounceAnnounce { old, gone, new: meta } } + // Back to the instance the consumer holds, before it saw the restart away: + // one continuous stream, so a metadata update. + _ if self.delivered.get(&prefix) == Some(&meta.0.3) => PendingUpdate::Announce(meta), Some(PendingUpdate::Restart(_)) => PendingUpdate::Restart(meta), - _ if self.delivered.contains(&prefix) => PendingUpdate::Restart(meta), + _ if self.delivered.contains_key(&prefix) => PendingUpdate::Restart(meta), // The consumer never saw the old instance, so this is its first announce. _ => PendingUpdate::Announce(meta), }; @@ -1230,7 +1234,7 @@ impl OriginConsumerState { match self.pending.get_mut(&prefix).and_then(|pending| pending.update.take()) { // The pending announce was never delivered and neither was any earlier // one, so the pair cancels entirely, and nothing is owed for it. - Some(PendingUpdate::Announce(_)) if !self.delivered.contains(&prefix) => { + Some(PendingUpdate::Announce(_)) if !self.delivered.contains_key(&prefix) => { let pending = self.remove(&prefix).unwrap(); self.owed -= usize::from(pending.owed); } @@ -1298,7 +1302,7 @@ impl OriginConsumerState { } let pending = self.pending.get_mut(prefix).unwrap(); let wake = match pending.update.as_ref().unwrap() { - PendingUpdate::Announce(_) if self.delivered.contains(prefix) => { + PendingUpdate::Announce(_) if self.delivered.contains_key(prefix) => { // The clock reads the driver's last advance, which may be stale: anchor // the hold on the first advance after the update arrived, by waking the // driver. @@ -1356,14 +1360,14 @@ impl OriginConsumerState { let ((meta, captures), kind) = match pending.update.unwrap() { PendingUpdate::Announce(meta) => { // The consumer has seen this prefix before, so it is a metadata update. - let kind = match self.delivered.insert(prefix.clone()) { - true => RouteKind::Start, - false => RouteKind::Update, + let kind = match self.delivered.insert(prefix.clone(), meta.0.3.clone()) { + None => RouteKind::Start, + Some(_) => RouteKind::Update, }; (meta, kind) } PendingUpdate::Restart(meta) => { - self.delivered.insert(prefix.clone()); + self.delivered.insert(prefix.clone(), meta.0.3.clone()); (meta, RouteKind::Restart) } PendingUpdate::Unannounce(meta, gone) => { @@ -11225,6 +11229,44 @@ mod tests { announced.expect_next_offline("room/alice").await; } + /// A restart onto another instance and back before the next poll leaves the instance + /// the consumer holds: an update, never a restart to the same instance. + #[kio::test] + #[cfg(feature = "async")] + async fn restart_away_and_back_is_an_update() { + let producer = origin(1).produce(); + let mut announced = producer.consume().routes(); + let held: crate::Instance = "01900000-0000-7000-8000-000000000002".parse().unwrap(); + let _held = producer + .forward( + "room/alice", + RouteInfo::new() + .with_instance(held.clone(), 0) + .with_hops(hops(&[10])) + .with_cost(3), + ) + .unwrap(); + announced.expect_next_active("room/alice").await; + + let other = producer + .forward( + "room/alice", + RouteInfo::new() + .with_instance(instance(), 0) + .with_hops(hops(&[11])) + .with_cost(1), + ) + .unwrap(); + drop(other); + match announced.expect_next().await { + RouteEvent { + kind: RouteKind::Update, + announce, + } => assert_eq!(announce.route.instance(), &held), + other => panic!("should be an update: got {other:?}"), + } + } + #[test] fn interest_prefixes_fall_back_above_an_escaped_slash() { let heads = |pattern: &str| interest_prefixes(&Patterns::from(pattern.parse::().unwrap()));