aboutsummaryrefslogtreecommitdiffstats
path: root/src/remake/switch.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/remake/switch.rs')
-rw-r--r--src/remake/switch.rs72
1 files changed, 67 insertions, 5 deletions
diff --git a/src/remake/switch.rs b/src/remake/switch.rs
index 3baa164..91591c2 100644
--- a/src/remake/switch.rs
+++ b/src/remake/switch.rs
@@ -41,6 +41,9 @@ use crate::document::store::paths_related;
use crate::document::{ActorId, ChangeHash, Document};
use crate::locks::MutexExt;
+/// How often the worker looks while something waits on time: a remake of this epoch
+/// open or committed, a rejoin, a catch-up, a member on another epoch. Otherwise it
+/// waits for what wakes it (`look_again`).
const TICK: Duration = Duration::from_secs(1);
pub(crate) async fn run_worker<H: Host>(host: H) {
@@ -49,16 +52,24 @@ pub(crate) async fn run_worker<H: Host>(host: H) {
let records_dir = meta::path(&[REMAKES]);
let mut changes = host.subscribe_changes();
let mut changes_closed = false;
- let mut tick = tokio::time::interval(TICK);
- tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
+ // What it finds at its start: a remake committed while the node was down.
+ let mut again = Some(tokio::time::Instant::now());
loop {
- // React promptly to changes of the records; the timer covers the switch delay.
+ // React promptly to changes of the records, and to a peer saying another
+ // epoch; a timer only while something waits on time (`look_again`).
// A vote waiting for its random delay (`vote`) is cast when due, not at the next
// tick: with a short voting period, a second can make it too late.
let vote_wait = host.remake().vote_wait(host.now_millis());
let should_check = tokio::select! {
- _ = tick.tick() => true,
+ () = tokio::time::sleep_until(again.unwrap_or_else(tokio::time::Instant::now)), if again.is_some() => true,
+ // A peer said another epoch: looked at within `TICK`, not at once, as its
+ // message is still being taken, and others saying the same are on their way.
+ () = host.remake_woken() => {
+ let soon = tokio::time::Instant::now() + TICK;
+ again = Some(again.map_or(soon, |at| at.min(soon)));
+ false
+ }
_ = tokio::time::sleep(vote_wait.unwrap_or_default()), if vote_wait.is_some() => true,
received = changes.recv(), if !changes_closed => match received {
Ok(set) => set.paths.iter().any(|path| paths_related(path, &records_dir)),
@@ -66,17 +77,68 @@ pub(crate) async fn run_worker<H: Host>(host: H) {
Err(broadcast::error::RecvError::Closed) => {
warn!("Change events closed; remake worker continues on its timer only");
changes_closed = true;
- false
+ again.is_none()
}
}
};
if should_check {
check(&host).await;
+ let wait = look_again(&host).await;
+ let wait = if changes_closed {
+ wait.or(Some(TICK))
+ } else {
+ wait
+ };
+ again = wait.map(|wait| tokio::time::Instant::now() + wait);
}
}
}
+/// How long until the worker looks again by itself (`check`); none while nothing it
+/// does waits on time, and what changes that wakes it: a change of the remake
+/// records, a peer saying another epoch than this node's (`Host::remake_woken`), a
+/// vote due.
+async fn look_again<H: Host>(host: &H) -> Option<Duration> {
+ #[cfg(feature = "federation")]
+ if host.holding().of_a_set() {
+ return None;
+ }
+ let remake = host.remake();
+ // A rejoin runs against its timeout, a catch-up against its retries.
+ if remake.is_rejoining() || remake.fetching_snapshot() || remake.catch_up().is_some() {
+ return Some(TICK);
+ }
+ let local = host.current_epoch();
+ let (now, grace) = (host.now_millis(), host.dormant_after().as_millis() as u64);
+ // A remake of this epoch still voted on, to expire, or committed and to switch to.
+ let local_record = EpochRecord::from(local);
+ let open = host
+ .read_doc(move |doc| {
+ records::load_all(doc).iter().any(|(_, record)| {
+ record.proposal.from_epoch == local_record && record.phase(now, grace).is_open()
+ })
+ })
+ .await;
+ if open {
+ return Some(TICK);
+ }
+ // A member on another epoch than ours, not an earlier one: to be followed once
+ // enough say so, or once it is ready (`catchup::check`).
+ let elsewhere = host.peer_summaries().await.into_iter().any(|peer| {
+ peer.active
+ && peer.member
+ && peer
+ .epoch
+ .is_some_and(|epoch| epoch != local && epoch.generation >= local.generation)
+ });
+ if elsewhere {
+ return Some(TICK);
+ }
+ // What it keeps for a time: archived documents, a remake's content.
+ remake.next_expiry(now)
+}
+
async fn check<H: Host>(host: &H) {
// A set of events has no epochs: the document a node holds beside one stays as
// it is, whatever epoch its peers of both kinds say.