diff options
| author | tv <tv@krebsco.de> | 2026-11-29 14:05:32 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-29 14:05:32 +0000 |
| commit | d3f7a3edc492fb0a8787614d7a9358978f6ab989 (patch) | |
| tree | 6854632dc78bfed798bda783fe5713bd9d831005 /src | |
| parent | 61fb06c420d5e481badc8a9d3252b90cf9013746 (diff) | |
federation: a store tells what it took, and has no more wait than asked
Beside what it lets go of (Store::telling): what it took, in the order
taken (took), and what a move to a checkpoint left beside the heads
(Gone::NotAbove), so that what carries it follows what it holds without
reading it all again.
Store::waiting_no_more_than: past that many events waiting for what
they build on, the one waiting longest goes, as if it had never come.
A store not asked has any number wait, as before.
Diffstat (limited to 'src')
| -rw-r--r-- | src/federation/store/memory.rs | 68 |
1 files changed, 63 insertions, 5 deletions
diff --git a/src/federation/store/memory.rs b/src/federation/store/memory.rs index 0243d03..8f099be 100644 --- a/src/federation/store/memory.rs +++ b/src/federation/store/memory.rs @@ -1,7 +1,7 @@ //! A store held in memory (`docs/store.md`): a checkpoint, the events it keeps, and the //! events taken after it, each judged at its history. -use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; +use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque}; use super::{Fold, Id, Refused}; @@ -50,6 +50,9 @@ pub(crate) enum Gone { Own, /// It builds on one the store refuses, and wasn't judged. BuiltOn(Id), + /// A move to a checkpoint left it beside the heads: every node that moves refuses + /// it from then on. + NotAbove, } /// A checkpoint built: its bytes, and the events it keeps, by ID. @@ -99,6 +102,13 @@ pub(crate) struct Store<F: Fold> { /// What the store let go of since it was last asked, with the bytes, where it tells /// (`telling`): for what carries the store to pass on, or to keep a while. let_go: Option<Vec<(Id, Vec<u8>, Gone)>>, + /// What it took since it was last asked, in the order taken, where it tells. + took: Option<Vec<Id>>, + /// The most events it has wait for what they build on (0: any number), and those + /// that waited, the longest first: past that many, the one waiting longest goes, + /// as if it had never come. + waits_most: usize, + waited: VecDeque<Id>, } impl<F: Fold> Store<F> { @@ -146,6 +156,9 @@ impl<F: Fold> Store<F> { held_back: Vec::new(), holding_back: HashSet::new(), let_go: None, + took: None, + waits_most: 0, + waited: VecDeque::new(), }) } @@ -186,9 +199,47 @@ impl<F: Fold> Store<F> { /// The store telling what it lets go of (`let_go`). pub(crate) fn telling(mut self) -> Self { self.let_go = Some(Vec::new()); + self.took = Some(Vec::new()); self } + /// The store having no more than `most` events wait for what they build on. + pub(crate) fn waiting_no_more_than(mut self, most: usize) -> Self { + self.waits_most = most; + self + } + + /// Has `id` wait, the one waiting longest going where that is more than the store + /// has wait. + fn waits(&mut self, id: Id) { + if self.waits_most == 0 { + return; + } + while self.waiting.len() >= self.waits_most { + let Some(longest) = self.waited.pop_front() else { + break; + }; + if let Some(event) = self.waiting.remove(&longest) { + for dependency in &event.dependencies { + if let Some(waiting) = self.waits_for.get_mut(dependency) { + waiting.remove(&longest); + } + } + } + } + self.waits_for.retain(|_, waiting| !waiting.is_empty()); + if self.waited.len() >= 2 * self.waits_most { + self.waited.retain(|id| self.waiting.contains_key(id)); + } + self.waited.push_back(id); + } + + /// What the store took since last asked, in the order taken: nothing, where it + /// doesn't tell. + pub(crate) fn took(&mut self) -> Vec<Id> { + self.took.as_mut().map(std::mem::take).unwrap_or_default() + } + /// What the store let go of since last asked, each with its bytes and why: nothing, /// where it doesn't tell. pub(crate) fn let_go(&mut self) -> Vec<(Id, Vec<u8>, Gone)> { @@ -585,10 +636,13 @@ impl<F: Fold> Store<F> { .copied() .collect(); for id in below { - if let Some(event) = self.events.remove(&id) - && before.contains(&id) - { - self.tell(id, event.bytes, Gone::Forgotten); + if let Some(event) = self.events.remove(&id) { + let gone = if before.contains(&id) { + Gone::Forgotten + } else { + Gone::NotAbove + }; + self.tell(id, event.bytes, gone); } } let mut state = start.clone(); @@ -691,6 +745,7 @@ impl<F: Fold> Store<F> { for dependency in missing { self.waits_for.entry(dependency).or_default().insert(id); } + self.waits(id); self.waiting.insert(id, event); return Offered::Waiting; } @@ -720,6 +775,9 @@ impl<F: Fold> Store<F> { F::add(&mut self.state, &id, &event.bytes); self.events.insert(id, event); self.order.push(id); + if let Some(took) = self.took.as_mut() { + took.push(id); + } taken.push(id); Offered::Taken(vec![id]) } |
