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
2 changes: 1 addition & 1 deletion .claude/skills/verify-route/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ Sync commit: `cbb742cecae437ee94fbf25848521ac11b145fbc`, where [moq-dev/moq#8734
| --- | --- |
| `rs/moq-net/src/route/path.rs` `Router::run`, `Router::select`, `Router::retain` | `Select` plans live routes and their reflected sources before `SelectKey` decides holds. |
| `rs/moq-net/src/route/path.rs` `Offer::rank`, `Router::goaway`, `Router::drains`, `Router::report_draining`, `Router::dead` (`Dead::Draining`) | `Rank`, `Below`, `PathPrio`, `CostAt`, `GoAway`, `MarkDrain`, `Credited`, `ReportDrain`, `Recv` |
| `rs/moq-net/src/route/path.rs` `Router::pending`, `Router::hopes`, `Router::doubts_path` | `Pending`, `Hopes`, `Doubted`, `RelayDoubted` (a path's first node does not count), `Returned` |
| `rs/moq-net/src/route/path.rs` `Router::pending`, `Router::hopes`, `Router::kept_off`, `Router::doubts_path` | `Pending`, `Hopes`, `Doubted`, `RelayDoubted` (a path's first node does not count), `Returned` |
| `rs/moq-net/src/route/path.rs` `Router::suspect`, `Router::doubt_until` | `Since`: a doubt on the publisher that begins notes the generation of the relay's own session to it. The router shares one generation across a session's streams (`Session::generation`), which the model's one stream per session leaves out. |
| `rs/moq-net/src/route/path.rs` `Router::src` | `SrcKey` |
| `rs/moq-net/src/route/path.rs` `Router::usable_offer`, `Offer` | `SelectKey`'s `Down`, `Want0`'s export check, and `DownOnly`: the down-only flag is set on a route learned over an upstream link or from a down-only one, and never sent over an upstream link. |
Expand Down
10 changes: 8 additions & 2 deletions rs/moq-net/src/lite/subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2928,15 +2928,15 @@ impl AnnouncePrefix {
// be let go in between.
let retired = self.replacing.take().and_then(|old| old.retire());
// What the initial set put in the node is served before the
// snapshot lets readers see the origin synced.
// claims the old request served let go.
if let Some(routed) = &mut run.routed {
routed.outputs(&self.subscriber);
}
drop(retired);
if std::mem::replace(&mut run.synced, true) {
return Poll::Ready(Err(Error::ProtocolViolation));
}
self.snapshot = None;
// The snapshot lets go below, once the node shows every route.
continue;
}
if run.initial > 0 {
Expand Down Expand Up @@ -2975,6 +2975,12 @@ impl AnnouncePrefix {
run.announced.poll_serve(&self.subscriber, waiter);
if let Some(routed) = &mut run.routed {
routed.poll(&self.subscriber, waiter)?;
// The initial set is in once the node shows every route the peer sent: one
// that only doubt keeps the node off, as on a session redialed to a node
// still in doubt, reaches the origin once the doubt lifts.
if run.synced && self.snapshot.is_some() && routed.poll_undoubted(&self.subscriber, waiter).is_ready() {
self.snapshot = None;
}
}
Poll::Pending
}))
Expand Down
10 changes: 9 additions & 1 deletion rs/moq-net/src/lite/subscriber/routed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use std::{
collections::{BTreeMap, BTreeSet, HashMap},
sync::Arc,
task::Poll,
task::{Poll, ready},
};

use crate::{
Expand Down Expand Up @@ -870,6 +870,14 @@ impl Routed {
}
}

/// Ready once nothing the peer offers on this stream waits on a doubt to reach the
/// origin, with what the node served meanwhile already in it.
pub(super) fn poll_undoubted(&mut self, subscriber: &Subscriber, waiter: &kio::Waiter) -> Poll<()> {
ready!(self.stream.poll_undoubted(waiter));
self.outputs(subscriber);
Poll::Ready(())
}

/// Serve requests on every claim whose queue woke.
pub(super) fn poll(&mut self, subscriber: &Subscriber, waiter: &kio::Waiter) -> Result<(), Error> {
while let Poll::Ready(output) = self.stream.poll_output(waiter) {
Expand Down
4 changes: 3 additions & 1 deletion rs/moq-net/src/model/origin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8617,7 +8617,9 @@ impl Routes {
/// queued before then. A prefix not announced by then is not announced, rather
/// than not heard yet. A session joining the scope later, such as a reconnect,
/// takes this back until it syncs too; routes published on this origin
/// itself never wait.
/// itself never wait. On moq-lite-07 a session syncs only once this origin
/// shows every route its peer sent, so a redial waits out the doubt a closed
/// session left on that peer.
///
/// Synced is relative to the session's peer, never global: a relay answers with
/// what it holds without waiting on its own upstreams. Versions that cannot mark
Expand Down
16 changes: 16 additions & 0 deletions rs/moq-net/src/route/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,22 @@ impl Stream {
self.queue.lock().pop()
}

/// Ready once the peer offers no route on this stream that only doubt keeps the node
/// off ([`Router::doubted`]). Such a route waits on a doubt's timer, so this checks
/// again whenever the node's next deadline moves.
pub fn poll_undoubted(&self, waiter: &kio::Waiter) -> Poll<()> {
loop {
let armed = {
let state = self.node.lock();
if !state.router.doubted(self.key) {
return Poll::Ready(());
}
state.router.deadline()
};
ready!(self.node.poll_deadline(armed, waiter));
}
}

/// Date what the stream hands the node by its session's clock too.
pub fn with_clock(mut self, clock: Deadlines) -> Self {
self.clock = Some(clock);
Expand Down
25 changes: 20 additions & 5 deletions rs/moq-net/src/route/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2380,11 +2380,26 @@ impl<N: Key, A: Clone + Eq + std::fmt::Debug> Router<N, A> {

/// Whether a neighbour offers a route for `key` that only doubt keeps this node off.
fn hopes(&self, key: &RouteId) -> bool {
(self.rx.get(key).into_iter().flatten()).any(|(peer, heard)| {
heard.state == State::Live
&& !names(&heard.offer.path, self.hop)
&& heard.offer.metric.checked_add(self.sessions[peer].link.cost).is_some()
&& self.doubts_path(*peer, &heard.offer.path)
(self.rx.get(key).into_iter().flatten()).any(|(peer, heard)| self.kept_off(*peer, heard))
}

/// Whether only doubt keeps this node off the route `heard` from `peer`.
fn kept_off(&self, peer: N, heard: &Heard<A>) -> bool {
heard.state == State::Live
&& !names(&heard.offer.path, self.hop)
&& heard.offer.metric.checked_add(self.sessions[&peer].link.cost).is_some()
&& self.doubts_path(peer, &heard.offer.path)
}

/// Whether `peer` offers a route that only doubt keeps this node off, where it has
/// no live one: it takes the route once the doubt lifts.
pub fn doubted(&self, peer: N) -> bool {
let Some(session) = self.sessions.get(&peer) else {
return false;
};
session.heard.iter().any(|key| {
!self.routes.get(key).is_some_and(Route::live)
&& (self.rx.get(key).and_then(|heard| heard.get(&peer))).is_some_and(|heard| self.kept_off(peer, heard))
})
}

Expand Down
93 changes: 69 additions & 24 deletions rs/moq-net/tests/integration/synced.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
//! on the answer, as a best effort, on the rest.

use std::collections::BTreeSet;
use std::task::Poll;
use std::time::Duration;

use moq_net::{Version, origin};

use crate::support;
use support::harness::{MockConnectOptions, connect_mock};

/// Long enough, in virtual time, for anything in flight to reach the far side.
const SETTLE: Duration = Duration::from_secs(1);
/// Long enough, in virtual time, for anything in flight to reach the far side,
/// past the second a redialed session waits out the doubt on its peer.
const SETTLE: Duration = Duration::from_secs(2);

fn produce_origin(hop: u64) -> origin::Producer {
let (producer, driver) = origin::Producer::new(origin::Config::new(std::num::NonZeroU64::new(hop).unwrap()));
Expand All @@ -22,32 +24,37 @@ fn produce_origin(hop: u64) -> origin::Producer {
}

/// Read events until the consumer syncs, returning the prefixes then announced.
/// Sync is checked whenever no event is ready, so a consumer that syncs early is
/// caught at that moment rather than at the next event.
async fn read_until_synced(announced: &mut origin::Routes) -> BTreeSet<String> {
let mut seen = BTreeSet::new();
while !announced.poll_synced(&kio::Waiter::noop()).is_ready() {
let event = kio::clock::timeout(SETTLE, announced.next())
.await
.expect("synced in time")
.expect("the cursor stays open");
match event {
origin::RouteEvent {
kind: origin::RouteKind::Start | origin::RouteKind::Restart,
announce,
} => {
seen.insert(announce.prefix.to_string());
let read = kio::wait(|waiter| {
loop {
let event = match announced.poll_next(waiter) {
Poll::Ready(event) => event.expect("the cursor stays open"),
Poll::Pending => return announced.poll_synced(waiter),
};
match event {
origin::RouteEvent {
kind: origin::RouteKind::Start | origin::RouteKind::Restart,
announce,
} => {
seen.insert(announce.prefix.to_string());
}
origin::RouteEvent {
kind: origin::RouteKind::End | origin::RouteKind::Offline,
announce,
} => {
seen.remove(announce.prefix.as_str());
}
origin::RouteEvent {
kind: origin::RouteKind::Update,
..
} => {}
}
origin::RouteEvent {
kind: origin::RouteKind::End | origin::RouteKind::Offline,
announce,
} => {
seen.remove(announce.prefix.as_str());
}
origin::RouteEvent {
kind: origin::RouteKind::Update,
..
} => {}
}
}
});
kio::clock::timeout(SETTLE, read).await.expect("synced in time");
seen
}

Expand Down Expand Up @@ -125,6 +132,44 @@ async fn a_second_session_catches_up() {
drop(second);
}

/// A session redialed after a cut waits out the doubt on the publisher before
/// its route reaches the origin, and the consumer stays unsynced until then:
/// synced still means every route the peer announced is shown.
#[kio::test]
async fn a_redial_syncs_once_the_doubt_lets_its_routes_in() {
let publisher = produce_origin(1);
let a = publisher.route("a", moq_net::origin::RouteOptions::hidden()).unwrap();
a.announce().unwrap();

let subscriber = produce_origin(2);
let connect = || {
let mut options = MockConnectOptions::new("moq-lite-07-wip".parse::<Version>().unwrap());
options.server_publish = Some(publisher.consume());
options.client_subscribe = Some(subscriber.clone());
connect_mock(options)
};
let first = connect().await;
let mut announced = subscriber.consume().routes();
assert_eq!(
read_until_synced(&mut announced).await,
BTreeSet::from(["a".to_string()])
);

first.client.abort(moq_net::Error::Cancel);
first.server.abort(moq_net::Error::Cancel);
let gone = kio::clock::timeout(SETTLE, announced.next())
.await
.expect("withdrawn in time")
.expect("the cursor stays open");
assert!(matches!(gone.kind, origin::RouteKind::End | origin::RouteKind::Offline));

let _second = connect().await;
assert_eq!(
read_until_synced(&mut announced).await,
BTreeSet::from(["a".to_string()])
);
}

/// A cold relay's empty set is complete before an upstream joins it.
#[kio::test]
async fn a_cold_relay_syncs_then_forwards_upstream_routes_as_live() {
Expand Down