From 219bd018db5ee3c5dc5f0dc8689e4c8c7d14f67e Mon Sep 17 00:00:00 2001 From: Aleksandr Bogomiakov Date: Wed, 25 Nov 2026 00:09:53 +0200 Subject: [PATCH] Let healthy carrier comparisons recover from startup probe loss MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The first probe on a newly opened carrier can precede the responder’s private health profile. Keep measuring for up to 30 seconds if the 20-probe window still contains losses or too few samples. A clean link still qualifies after six seconds. The ordinary 60-percent link failure detector continues to count every missed probe. Tested the acquisition, recovery, insufficient data and persistent-loss decisions. The slow TLS and fast QUIC loopback fixture can expose its measurements through the existing test logging switch. Workspace Clippy and formatting passed. --- .../src/agent/network/selection.rs | 51 +++++++++++++------ crates/tsunagi-wg-quic/tests/carriers.rs | 1 + docs/routing.md | 7 ++- 3 files changed, 41 insertions(+), 18 deletions(-) diff --git a/crates/tsunagi-core/src/agent/network/selection.rs b/crates/tsunagi-core/src/agent/network/selection.rs index 99fd89f..de05a57 100644 --- a/crates/tsunagi-core/src/agent/network/selection.rs +++ b/crates/tsunagi-core/src/agent/network/selection.rs @@ -9,7 +9,9 @@ pub(super) const PROTOCOL: &str = "link-choice"; const RETRY: Duration = Duration::from_secs(3); const NEXT_TRIAL: Duration = Duration::from_secs(120); const MIN_WINDOW: Duration = Duration::from_secs(6); -const MAX_WINDOW: Duration = Duration::from_secs(18); +// Allow a full rolling health window to replace probes sent before the +// responder installed its profile. Persistent loss still disqualifies a trial. +const MAX_WINDOW: Duration = Duration::from_secs(30); pub(super) fn capability() -> PluginCapability { PluginCapability { @@ -25,12 +27,18 @@ fn supports_choice(caps: &[PluginCapability]) -> bool { .any(|c| c.enabled && c.protocol == PROTOCOL && c.version == 1) } -fn better(current: &LinkQuality, alternative: &LinkQuality) -> bool { +/// None keeps measuring on the alternative while user traffic stays on the incumbent. +fn promotion_decision(age: Duration, measured: Option<(LinkQuality, LinkQuality)>) -> Option { + if age < MIN_WINDOW { + return None; + } + let Some((current, alternative)) = measured.filter(|(current, alternative)| { + current.samples >= 5 && alternative.samples >= 5 && alternative.losses == 0 + }) else { + return (age >= MAX_WINDOW).then_some(false); + }; let gain = current.rtt.saturating_sub(alternative.rtt); - current.samples >= 5 - && alternative.samples >= 5 - && alternative.losses == 0 - && gain >= (current.rtt / 5).max(Duration::from_millis(5)) + Some(gain >= (current.rtt / 5).max(Duration::from_millis(5))) } /// Fixed-size identifiers from the two privately negotiated health profiles. @@ -207,17 +215,11 @@ impl Runtime { return; }; let age = now.duration_since(trial.started); - if age < MIN_WINDOW { + let measured = current.quality().zip(candidate.quality()); + let Some(promote) = promotion_decision(age, measured) else { return; - } - let measured = current - .quality() - .zip(candidate.quality()) - .filter(|(a, b)| a.samples >= 5 && b.samples >= 5); - if measured.is_none() && age < MAX_WINDOW { - return; - } - let promote = measured.is_some_and(|(a, b)| better(&a, &b)); + }; + tracing::debug!(peer = %peer.fmt_short(), incumbent = %other, candidate = %trial.protocol, ?measured, promote, "compared data carriers"); let (winner, loser) = if promote { (candidate, current) } else { @@ -327,8 +329,25 @@ mod tests { } } + #[test] + fn a_loss_during_profile_installation_can_age_out_before_deciding() { + let observed = + |samples, losses| Some((measurement(200, 20, 0), measurement(30, samples, losses))); + assert_eq!(promotion_decision(MIN_WINDOW, observed(5, 1)), None); + assert_eq!( + promotion_decision(Duration::from_secs(24), observed(20, 0)), + Some(true) + ); + assert_eq!(promotion_decision(MAX_WINDOW, observed(18, 2)), Some(false)); + assert_eq!(promotion_decision(MAX_WINDOW, observed(4, 0)), Some(false)); + assert_eq!(promotion_decision(MIN_WINDOW / 2, observed(5, 0)), None); + } + #[test] fn promote_only_a_sampled_reliable_and_materially_faster_path() { + let better = |current: &LinkQuality, candidate: &LinkQuality| { + promotion_decision(MAX_WINDOW, Some((*current, *candidate))) == Some(true) + }; let current = measurement(350, 20, 1); assert!(better(¤t, &measurement(60, 6, 0))); assert!(!better(¤t, &measurement(60, 1, 0))); diff --git a/crates/tsunagi-wg-quic/tests/carriers.rs b/crates/tsunagi-wg-quic/tests/carriers.rs index 9d51780..b59e6dd 100644 --- a/crates/tsunagi-wg-quic/tests/carriers.rs +++ b/crates/tsunagi-wg-quic/tests/carriers.rs @@ -75,6 +75,7 @@ impl Node { ports: Arc>>, register_transport: bool, ) -> Self { + tsunagi::testing::init_tracing(); let root = TempDir::new().unwrap(); let identity = tsunagi::storage::StateStore::open( tsunagi::StoragePaths::under(root.path()).state_db(), diff --git a/docs/routing.md b/docs/routing.md index 406df26..205ea85 100644 --- a/docs/routing.md +++ b/docs/routing.md @@ -111,8 +111,11 @@ a trial. A failed active link still triggers immediate fallback. The measurements use authenticated round trips over the data carrier. After at least six seconds and five successful probes on each link, the alternative must have zero observed loss in its window and a median RTT improvement of -at least 20% and 5 ms. Otherwise traffic stays on the incumbent. The agents -coordinate retirement over their authenticated control session. An older peer +at least 20% and 5 ms. If early probes are lost while the responder installs +its probe profile, the dialler keeps sampling for up to 30 seconds so those +losses can leave the 20-probe window. The incumbent stays selected if the +alternative fails to qualify. The agents coordinate retirement over their +authenticated control session. An older peer continues using the configured order and failure recovery. One enabled carrier restricts the pair to that carrier.