From 82bb426dcd22268a960d35d7b8c947b864a6ddf5 Mon Sep 17 00:00:00 2001 From: tv Date: Tue, 1 Dec 2026 06:10:15 +0000 Subject: swarm: the peer worker's periodic checks run when something happened, and rarely when quiet Every five seconds the peer worker looked at the forget records, the member registry, stability, whom the node reaches, its links, its clock, its measurements and the state file. It now looks five seconds after the document or a peer's state changed, every five seconds while something of these is under way (the node not ready or catching up, a forget record not acted on, a link's change not settled, the tier after a restart not decided, a neighbour not reached), and once a minute otherwise, for what changes with time alone. Stability takes two measurements more than two minutes apart for the node not having run. --- src/swarm/peer/forget.rs | 15 +++++++++++ src/swarm/peer/worker.rs | 67 ++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 77 insertions(+), 5 deletions(-) (limited to 'src/swarm/peer') diff --git a/src/swarm/peer/forget.rs b/src/swarm/peer/forget.rs index b911993..5eb5026 100644 --- a/src/swarm/peer/forget.rs +++ b/src/swarm/peer/forget.rs @@ -41,6 +41,21 @@ fn forgotten_peers(doc: &(impl Read + ?Sized)) -> Vec { /// The forget records this node acted on: peer → the record as it read then. pub(super) type AppliedForgets = HashMap; +/// Whether a forget record of a peer this node knows is not acted on yet (`applied`): +/// the peer was alive, and is looked at again. +pub(super) async fn unapplied(state: &AppState, applied: &AppliedForgets) -> bool { + let forgotten = forgotten_peers(&state.doc.read().await.doc); + for peer in forgotten { + if peer != state.local_id() + && !applied.contains_key(&peer) + && state.get_peer_state(&peer).await.is_some() + { + return true; + } + } + false +} + /// Acts on each forget record once (`applied`). A peer forgotten here may be heard of /// again before its cancel arrives (named in a peer list, or alive again), and is kept /// then; forgetting it again every check would only make it flap. A new record (another diff --git a/src/swarm/peer/worker.rs b/src/swarm/peer/worker.rs index e17eb34..5b25123 100644 --- a/src/swarm/peer/worker.rs +++ b/src/swarm/peer/worker.rs @@ -1,7 +1,9 @@ //! The peer worker: keepalives due, dormancy, HELLO retries, overdue sync replies and //! answers (`PeerState::answer_overdue`, `swarm::reach`), when the earliest of them is -//! due (or something may have made one due sooner, `Due`); every -//! `FORGET_CHECK_INTERVAL` (and at once when the node becomes ready), forget records, +//! due (or something may have made one due sooner, `Due`); and, after something +//! happened that they may have to act on, every `FORGET_CHECK_INTERVAL` while +//! something of them is under way, else every `QUIET_CHECK_INTERVAL` (and at once +//! when the node becomes ready), forget records, //! the member registry, stability, measurements and the state file; forget records //! also at once when they changed. @@ -20,7 +22,15 @@ use crate::swarm::topology; /// The least time between two passes, however often something is due. const MIN_PASS_GAP: Duration = Duration::from_millis(20); +/// The least time between two looks at what is kept in order beside the peers +/// (forget records, the member registry, stability, whom this node reaches, its +/// links, clock, measurements, the state file): so often while something of it is +/// under way (`under_way`), and after something happened that it may have to act on +/// (`AppState::housekeeping`). const FORGET_CHECK_INTERVAL: Duration = Duration::from_secs(5); +/// How often it looks at those while nothing happens: what they go by also changes +/// with time alone (a round trip going stale, a stamp to renew, an address learned). +const QUIET_CHECK_INTERVAL: Duration = Duration::from_secs(60); /// When the peer worker is next due. What makes something of its due sooner than it /// knows (a HELLO sent, a sync message waiting for its answer, a keepalive received, a @@ -59,10 +69,31 @@ impl Due { } } +/// Whether something the periodic checks keep in order is under way, so that they +/// look again soon: the node not ready or still catching up, a forget record not +/// acted on, a link's change not settled, its tier after a restart not decided, a +/// neighbour not reached (what looks for it again goes with these checks). +async fn under_way(state: &AppState, applied: &AppliedForgets) -> bool { + !state.is_ready().await + || crate::swarm::source::catching_up(state) + || !crate::swarm::working_links::quiet(state) + || !crate::swarm::stability::settled(state) + || super::forget::unapplied(state, applied).await + || { + let reached = crate::swarm::reach::reached_members(state, Instant::now()).await; + let neighbours = topology::our_neighbours(state); + neighbours.iter().any(|peer| !reached.contains(peer)) + } +} + pub(crate) async fn run_peer_worker(state: AppState) { info!("Starting peer maintenance worker"); let mut last_forget_check: Option = None; + // Something happened that the periodic checks may have to act on, since the last. + let mut happened = false; + // How long until they are next looked at by themselves. + let mut again = FORGET_CHECK_INTERVAL; let mut forgets_applied = AppliedForgets::new(); let mut was_ready = false; let mut awake = crate::swarm::suspend::Awake::default(); @@ -203,7 +234,10 @@ pub(crate) async fn run_peer_worker(state: AppState) { let records_changed = state .forget_records_changed .swap(false, std::sync::atomic::Ordering::AcqRel); - let periodic = last_forget_check.is_none_or(|t| t.elapsed() >= FORGET_CHECK_INTERVAL); + let periodic = last_forget_check.is_none_or(|checked| { + let since = checked.elapsed(); + since >= again || (happened && since >= FORGET_CHECK_INTERVAL) + }); if records_changed || periodic { apply_forget_records(&state, &mut forgets_applied).await; } @@ -240,13 +274,36 @@ pub(crate) async fn run_peer_worker(state: AppState) { // Fallback for what doesn't notify (`topology::run_worker`). state.topology_changed.notify_one(); last_forget_check = Some(Instant::now()); + happened = false; + again = if under_way(&state, &forgets_applied).await { + FORGET_CHECK_INTERVAL + } else { + QUIET_CHECK_INTERVAL + }; } if let Some(checked) = last_forget_check { - next = next.min(checked + FORGET_CHECK_INTERVAL); + let wait = if happened { + FORGET_CHECK_INTERVAL + } else { + again + }; + next = next.min(checked + wait); } let passed = Instant::now(); - state.peers_due.until(next).await; + // Until something of the peers is due, or of the periodic checks; what happens + // meanwhile that those may have to act on brings them forward. + loop { + tokio::select! { + () = state.peers_due.until(next) => break, + () = state.housekeeping.notified(), if !happened => { + happened = true; + if let Some(checked) = last_forget_check { + next = next.min(checked + FORGET_CHECK_INTERVAL); + } + } + } + } tokio::time::sleep_until(passed + MIN_PASS_GAP).await; } } -- cgit v1.3.1