diff options
| -rw-r--r-- | src/document/screen.rs | 5 | ||||
| -rw-r--r-- | src/federation/held.rs | 57 | ||||
| -rw-r--r-- | src/federation/store/set.rs | 15 |
3 files changed, 72 insertions, 5 deletions
diff --git a/src/document/screen.rs b/src/document/screen.rs index 542a22e..62c86cd 100644 --- a/src/document/screen.rs +++ b/src/document/screen.rs @@ -69,6 +69,9 @@ pub(crate) struct Screening { epoch_seq: u32, pub(crate) waiting: Waiting, refused: std::collections::VecDeque<Refused>, + /// Notified where a change is kept as refused or dropped (`record`, + /// `keep_dropped`): who reads them looks then (`federation::held`). + pub(crate) refused_more: std::sync::Arc<tokio::sync::Notify>, refusals: Refusals, /// The members refused as a whole, by node ID (`refusing`). refusing: std::collections::BTreeMap<String, Refusing>, @@ -203,6 +206,7 @@ impl Screening { "Refused a change (_continuum/access/policy)" ); self.refused.push_back(refused); + self.refused_more.notify_one(); if self.refused.len() > MAX_REFUSED { self.refused.pop_front(); } @@ -227,6 +231,7 @@ impl Screening { pub(crate) fn keep_dropped(&mut self, dropped: Vec<Refused>) { for dropped in dropped { self.refused.push_back(dropped); + self.refused_more.notify_one(); if self.refused.len() > MAX_REFUSED { self.refused.pop_front(); } diff --git a/src/federation/held.rs b/src/federation/held.rs index 4846266..e6a108f 100644 --- a/src/federation/held.rs +++ b/src/federation/held.rs @@ -115,6 +115,9 @@ pub(crate) struct Holding { /// is; and how it stood as it was last written there. document: Option<(PathBuf, Arc<Files>)>, written: Mutex<Option<super::store::passing::Stamp>>, + /// Notified where the set of events carrying the store may have changed: the + /// worker writes it to its file a while after (`run_worker`). + changed: Arc<tokio::sync::Notify>, /// Whether a set of events carries the store (`carrying`), from the node's start on. of_a_set: bool, /// Whether the node holds the store in the document a swarm syncs otherwise too @@ -291,6 +294,7 @@ impl Holding { keeps_forgotten: Duration::ZERO, document: None, written: Mutex::default(), + changed: Arc::default(), inner: Mutex::new(Inner { kept, ..Inner::default() @@ -443,6 +447,9 @@ impl Holding { let Some(store) = &inner.store else { return; }; + if matches!(inner.store, Some(Carried::Set(_))) { + self.changed.notify_one(); + } let stopped = store.stopped().map(|(id, why, _)| (*id, why)); if stopped.map(|(id, _)| id) != inner.stopped { match stopped { @@ -1156,7 +1163,7 @@ impl Holding { return Ok(false); }; let now = Instant::now(); - self.change_set(|set| set.expire(now)); + self.expire(now); let mut written = self.written.lock_or_recover(); let Some((stands, bytes)) = self .with_set(|set| { @@ -1172,6 +1179,31 @@ impl Holding { Ok(true) } + /// Has the set let go of what a checkpoint forgot longer ago than it keeps it, at + /// `now`; settled only where it let go of anything. + fn expire(&self, now: Instant) { + let mut inner = self.inner.lock_or_recover(); + let Some(Carried::Set(set)) = inner.store.as_mut() else { + return; + }; + if set.expire(now) { + self.settle(&mut inner); + } + } + + /// Resolves once the set of events carrying the store may have changed. + pub(crate) fn changed(&self) -> impl Future<Output = ()> + Send + 'static { + let changed = Arc::clone(&self.changed); + async move { changed.notified().await } + } + + /// How long until the set lets go of something a checkpoint forgot, if it keeps + /// any. + pub(crate) fn next_expiry(&self) -> Option<Duration> { + self.with_set(|set| set.next_expiry(Instant::now())) + .flatten() + } + /// The kind of the document that carries the store: a set of events, or the /// document the node holds, which the store is read from. pub(crate) fn kind(&self) -> &'static str { @@ -1357,9 +1389,12 @@ pub(crate) async fn run_worker(state: crate::state::AppState) { let mut propose_due: Option<tokio::time::Instant> = None; // Whether this node is a member of its store, as last said. let mut listed: Option<bool> = None; - let mut looks = tokio::time::interval(LOOK); - looks.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay); - let mut look = false; + // What screening refused is looked at where it refuses more; first, what it holds. + let refused_more = Arc::clone(&state.screening.lock_or_recover().refused_more); + // A set of events goes to its file `LOOK` after it changed, and when it has + // something to let go of. + let mut save_due: Option<tokio::time::Instant> = Some(tokio::time::Instant::now() + LOOK); + let mut look = true; loop { let holding = Arc::clone(&state.holding); let doc = Arc::clone(&state.doc); @@ -1497,8 +1532,16 @@ pub(crate) async fn run_worker(state: crate::state::AppState) { } } } - _ = looks.tick() => { + // A node voting by itself decides by time, too (its rules: how long a + // proposal was held, how long since its store moved): it looks every `LOOK`. + () = tokio::time::sleep(LOOK), if state.holding.votes_by_itself() => {} + () = refused_more.notified() => { look = true; + } + () = state.holding.changed() => { + save_due.get_or_insert(tokio::time::Instant::now() + LOOK); + } + () = sleep_until(save_due), if save_due.is_some() => { // A set of events to its file, where it changed (`Holding::save`). if state.holding.has_a_set() { let holding = Arc::clone(&state.holding); @@ -1508,6 +1551,10 @@ pub(crate) async fn run_worker(state: crate::state::AppState) { error!(error = %error, "Couldn't write the store's document: a restart goes on from what the file holds"); } } + save_due = state + .holding + .next_expiry() + .map(|left| tokio::time::Instant::now() + left); } } } diff --git a/src/federation/store/set.rs b/src/federation/store/set.rs index 8c2be6d..a31d9bd 100644 --- a/src/federation/store/set.rs +++ b/src/federation/store/set.rs @@ -485,6 +485,21 @@ impl<F: Fold> Set<F> { self.beside.len() != before } + /// How long, at `now`, until the document lets go of something a checkpoint forgot + /// (`expire`), if it keeps any. + pub(crate) fn next_expiry(&self, now: Instant) -> Option<Duration> { + self.beside + .values() + .filter_map(|(_, why)| match why { + Beside::Forgotten(since) => Some( + (*since + self.keeps_forgotten).saturating_duration_since(now) + + Duration::from_millis(1), + ), + _ => None, + }) + .min() + } + /// The bytes of an event the document gives to others: one its store holds, one /// held beside it, or one the store holds back. pub(crate) fn get(&self, id: &Id) -> Option<&[u8]> { |
