Skip to content
Closed
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
42 changes: 39 additions & 3 deletions rs/moq-net/src/model/origin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,8 +519,8 @@ pub struct Route {

/// What pulling content via this route costs, accumulated per link: lower wins
/// among routes of the same anonymity, with ties broken by a broadcast published
/// on this origin, then hop length, then a deterministic hash, and finally the
/// most recently announced route. See [`Cost`].
/// on this origin, then hop length, then a deterministic hash of a route a cluster
/// peer forwarded, and finally the most recently announced route. See [`Cost`].
pub cost: Cost,

/// The announcing session's declared or assigned identity.
Expand Down Expand Up @@ -648,13 +648,21 @@ fn fnv_key(name: &str, origins: impl IntoIterator<Item = Hop>) -> u64 {
/// of the prefix and chain so every node converges on the same winner, and finally
/// the newest announcement, so a reconnect under an otherwise identical route wins
/// the moment it lands instead of after the transport retires the old session.
///
/// Only a route a cluster peer forwarded is hashed. One that entered here is ranked
/// by no other node, and a restarted publisher announces under a new first hop (its
/// own random id, or this relay's stamp), so a hash would keep its dead session
/// winning half the time until the transport times out.
fn route_order(prefix: &Path, entry: &RouteEntry) -> (bool, Cost, bool, usize, u64, Reverse<u64>) {
(
entry.is_anonymous(),
entry.cost,
!entry.local,
entry.hops.len(),
fnv_key(prefix.as_str(), entry.hops.iter().copied()),
match entry.peer {
true => fnv_key(prefix.as_str(), entry.hops.iter().copied()),
false => 0,
Comment on lines +662 to +664

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Update the public route-order documentation

For routes inserted through a non-peer Producer, this now replaces the hash with zero and orders equal candidates directly by recency, but the public Route::cost documentation still states that a deterministic hash always precedes the newest-announcement tie-break. This makes the documented, externally observable selection behavior incorrect for ordinary publisher routes; document the new peer-only hashing distinction alongside this change.

AGENTS.md reference: AGENTS.md:L27-L28

Useful? React with 👍 / 👎.

},
Reverse(entry.id),
)
}
Expand Down Expand Up @@ -7353,6 +7361,34 @@ mod tests {
}
}

/// A publisher that reconnects before its old session times out announces under a
/// new first hop: a random id when it names itself, or this relay's stamp when it
/// does not. The new session wins whichever chain hashes lower.
#[tokio::test(start_paused = true)]
async fn a_reconnect_under_a_new_hop_wins_over_the_old_session() {
for (old, new) in [
(&[10][..], &[11][..]),
(&[11], &[10]),
(&[10, 0], &[11, 0]),
(&[11, 0], &[10, 0]),
] {
let producer = origin(1).produce();
let mut announced = producer.consume().announced();
tokio::time::sleep(Duration::from_millis(1)).await;

let _old = producer.dynamic("room", Route::default().with_hops(hops(old))).unwrap();
assert_eq!(announced.assert_next_active("room").hops, hops(old));

let _new = producer.dynamic("room", Route::default().with_hops(hops(new))).unwrap();
let update = tokio::time::timeout(DEFAULT_UPDATE_HOLD * 2, announced.next())
.await
.unwrap_or_else(|_| panic!("{new:?} never took over from {old:?}"))
.unwrap();
assert_eq!(update.kind, AnnounceKind::Updated);
assert_eq!(update.route.hops, hops(new));
}
}

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