diff options
Diffstat (limited to 'src/remake')
| -rw-r--r-- | src/remake/local.rs | 22 | ||||
| -rw-r--r-- | src/remake/mod.rs | 4 | ||||
| -rw-r--r-- | src/remake/records.rs | 7 | ||||
| -rw-r--r-- | src/remake/switch.rs | 72 |
4 files changed, 100 insertions, 5 deletions
diff --git a/src/remake/local.rs b/src/remake/local.rs index cf6f566..cdea68a 100644 --- a/src/remake/local.rs +++ b/src/remake/local.rs @@ -275,6 +275,28 @@ impl RemakeState { prune(&mut self.lock().archive, current_generation, now); } + /// How long until something kept for a time goes (`prune_archive`, + /// `prune_contents`), at `now`: an archived + /// document whose age limit is ahead, a remake's content within its grace. None + /// where nothing waits on time: what is kept by a count of epochs, or by a remake + /// that refers to it, goes when those change. + pub(crate) fn next_expiry(&self, now: UnixMillis) -> Option<Duration> { + let inner = self.lock(); + let archives = inner.archive.iter().filter_map(|archived| { + archived + .retention + .age_left(now.ms_since(archived.archived_at)) + }); + let contents = inner + .contents + .values() + .filter_map(|(_, stored)| CONTENT_GRACE.checked_sub(stored.elapsed())); + archives + .chain(contents) + .min() + .map(|left| left + Duration::from_millis(1)) + } + pub(crate) fn catch_up(&self) -> Option<CatchUp> { self.lock().catch_up.clone() } diff --git a/src/remake/mod.rs b/src/remake/mod.rs index 90ae436..4847d5c 100644 --- a/src/remake/mod.rs +++ b/src/remake/mod.rs @@ -311,6 +311,10 @@ pub(crate) trait Host: Clone + Send + Sync + 'static { /// Makes the next sync count as initial again. fn reset_initial_sync(&self); + /// Resolves when the remake worker has something to look at that no change of the + /// document tells: a peer said another epoch than this node's. + fn remake_woken(&self) -> impl Future<Output = ()> + Send; + /// Store paths touched by each published document change. fn subscribe_changes(&self) -> broadcast::Receiver<Arc<ChangeSet>>; diff --git a/src/remake/records.rs b/src/remake/records.rs index 22aaee3..9b0c8f1 100644 --- a/src/remake/records.rs +++ b/src/remake/records.rs @@ -75,6 +75,13 @@ impl Retention { self.epochs.is_some_and(|limit| epochs <= limit) || self.age_ms.is_some_and(|limit| age_ms <= limit) } + + /// How long until its age limit passes, `age_ms` after the fact, where that is + /// ahead: none where it has none, or it passed, and the count of epochs decides. + pub(crate) fn age_left(&self, age_ms: u64) -> Option<std::time::Duration> { + let left = self.age_ms?.checked_sub(age_ms)?; + Some(std::time::Duration::from_millis(left)) + } } #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] 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. |
