From 79396943ec9eee8ffd64669baaa3484fcad33feb Mon Sep 17 00:00:00 2001 From: tv Date: Tue, 1 Dec 2026 04:54:24 +0000 Subject: 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. --- src/federation/held.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 52 insertions(+), 5 deletions(-) (limited to 'src/federation/held.rs') 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)>, written: Mutex>, + /// 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, /// 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 + 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 { + 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 = None; // Whether this node is a member of its store, as last said. let mut listed: Option = 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 = 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); } } } -- cgit v1.3.1