aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/node.rs1
-rw-r--r--src/state/mod.rs5
-rw-r--r--src/swarm/peer/forget.rs15
-rw-r--r--src/swarm/peer/worker.rs67
-rw-r--r--src/swarm/source.rs2
-rw-r--r--src/swarm/stability.rs7
-rw-r--r--src/swarm/topology/mod.rs4
-rw-r--r--src/swarm/working_links.rs7
8 files changed, 100 insertions, 8 deletions
diff --git a/src/node.rs b/src/node.rs
index 792d62b..814ab6b 100644
--- a/src/node.rs
+++ b/src/node.rs
@@ -235,6 +235,7 @@ impl Node {
neighbours: Default::default(),
topology_changed: Default::default(),
join_progress: Default::default(),
+ housekeeping: Default::default(),
remake_wake: Default::default(),
forget_records_changed: Default::default(),
peers_due: Default::default(),
diff --git a/src/state/mod.rs b/src/state/mod.rs
index fe71b7b..e815550 100644
--- a/src/state/mod.rs
+++ b/src/state/mod.rs
@@ -244,6 +244,9 @@ pub(crate) struct AppState {
/// Something a joining node waits for may have happened: the swarm ID learned, the
/// initial sync done, a rejoin ended, a seed found (`swarm::bootstrap`).
pub(crate) join_progress: Arc<tokio::sync::Notify>,
+ /// Something happened that the peer worker's periodic checks may have to act on
+ /// (`swarm::peer::worker`): the document or a peer's state changed.
+ pub(crate) housekeeping: Arc<tokio::sync::Notify>,
/// A peer said another epoch than this node's (`swarm::peer::epoch`): the remake
/// worker looks whether to follow it (`remake::switch`).
pub(crate) remake_wake: Arc<tokio::sync::Notify>,
@@ -589,10 +592,12 @@ impl AppState {
pub(crate) fn new_peer_state(&self, pk: &str) -> Arc<Mutex<PeerState>> {
let leads = crate::swarm::pair::leads(self.local_id(), pk);
self.peers_due.now();
+ self.housekeeping.notify_one();
Arc::new(Mutex::new(PeerState::new(leads, &self.timing)))
}
pub(crate) fn notify_store_changed(&self) {
+ self.housekeeping.notify_one();
self.change_tx.send_modify(|version| {
*version = version.wrapping_add(1);
});
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<String> {
/// The forget records this node acted on: peer → the record as it read then.
pub(super) type AppliedForgets = HashMap<String, JsonValue>;
+/// 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<Instant> = 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;
}
}
diff --git a/src/swarm/source.rs b/src/swarm/source.rs
index 1288d86..e43a3d8 100644
--- a/src/swarm/source.rs
+++ b/src/swarm/source.rs
@@ -62,7 +62,7 @@ fn lock_avoided(state: &AppState) -> std::sync::MutexGuard<'_, Option<(String, I
state.catch_up_avoided.lock_or_recover()
}
-fn catching_up(state: &AppState) -> bool {
+pub(crate) fn catching_up(state: &AppState) -> bool {
!state.initial_sync_done.load(Ordering::Acquire)
}
diff --git a/src/swarm/stability.rs b/src/swarm/stability.rs
index c163778..07ee1b3 100644
--- a/src/swarm/stability.rs
+++ b/src/swarm/stability.rs
@@ -39,7 +39,7 @@ const BRIEF_ABSENCE: Duration = Duration::from_secs(3600);
/// How long after becoming ready we wait for a peer to tell us how long we were away.
const AWAY_REPORT_GRACE: Duration = Duration::from_secs(30);
/// A gap between two measurements longer than this means we didn't run (asleep).
-const MAX_TICK_GAP: Duration = Duration::from_secs(30);
+const MAX_TICK_GAP: Duration = Duration::from_secs(120);
#[derive(Debug, Default)]
pub(crate) struct Stability {
@@ -157,6 +157,11 @@ pub(crate) fn away(state: &AppState, gap: Duration) {
}
}
+/// Whether the tier this node found after a restart is decided (`tick`).
+pub(crate) fn settled(state: &AppState) -> bool {
+ lock(state).settled
+}
+
/// Measures, and updates our tier in the registry when it should change. Called every
/// few seconds by the peer worker.
pub(crate) async fn tick(state: &AppState) {
diff --git a/src/swarm/topology/mod.rs b/src/swarm/topology/mod.rs
index 1f99cd8..9e79225 100644
--- a/src/swarm/topology/mod.rs
+++ b/src/swarm/topology/mod.rs
@@ -640,13 +640,15 @@ pub(crate) fn change_peer<T>(
let result = change(peer_state);
if peer_state.neighbour_inputs() != before {
state.topology_changed.notify_one();
+ state.housekeeping.notify_one();
}
result
}
/// Recomputes the neighbours whenever something they depend on may have changed
/// (`AppState::topology_changed`): the member list (`swarm::changes::run_change_publisher`),
-/// a peer's state (`change_peer`), and every 5 s as a fallback (`swarm::peer`). A burst
+/// a peer's state (`change_peer`), and with the peer worker's periodic checks as a
+/// fallback (`swarm::peer`). A burst
/// of notifications coalesces into one recomputation.
pub(crate) async fn run_worker(state: AppState) {
info!(
diff --git a/src/swarm/working_links.rs b/src/swarm/working_links.rs
index ca3a22b..9ccfaa1 100644
--- a/src/swarm/working_links.rs
+++ b/src/swarm/working_links.rs
@@ -141,6 +141,13 @@ pub(crate) fn settled(state: &AppState) -> (u64, bool) {
(pending.checks, pending.since.is_empty())
}
+/// Whether no link's change waits to be recorded, and no member is being asked about
+/// one: nothing of `check` waits on time.
+pub(crate) fn quiet(state: &AppState) -> bool {
+ let pending = state.working_links.lock_or_recover();
+ pending.since.is_empty() && pending.probes.is_empty()
+}
+
/// With the peer worker's periodic pass (every 5 s): whom we reach now, against the
/// member list's graph and our record; a difference that held for long enough goes into
/// the record.