diff options
| author | tv <tv@krebsco.de> | 2026-12-01 04:54:24 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-12-01 04:54:24 +0000 |
| commit | 79396943ec9eee8ffd64669baaa3484fcad33feb (patch) | |
| tree | 7232a8b5dac3f6e51778c503605c74e1faeec96d /src/federation | |
| parent | 2d72ed5272f1be73b11bc4af5073385d6f905e0c (diff) | |
federation: the store's worker looks when something changed, not every five seconds
Every five seconds it looked at the changes screening refused, wrote its
set of events to its file, and read its store anew. It now looks where
screening refuses a change, writes the set five seconds after it
changed, and wakes for what the set is to let go of. A node voting by
itself still looks every five seconds: its rules count time.
Diffstat (limited to 'src/federation')
| -rw-r--r-- | src/federation/held.rs | 57 | ||||
| -rw-r--r-- | src/federation/store/set.rs | 15 |
2 files changed, 67 insertions, 5 deletions
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]> { |
