diff options
Diffstat (limited to 'src/federation/store/set.rs')
| -rw-r--r-- | src/federation/store/set.rs | 163 |
1 files changed, 146 insertions, 17 deletions
diff --git a/src/federation/store/set.rs b/src/federation/store/set.rs index cb55136..5b0a63b 100644 --- a/src/federation/store/set.rs +++ b/src/federation/store/set.rs @@ -24,7 +24,7 @@ use serde::{Deserialize, Serialize}; use super::entry::Entry; use super::memory::{Gone, Offered, Refusal, Store}; use super::{Fold, Id, Refused}; -use crate::document::ranges::Name; +use crate::document::ranges::{Name, Names}; /// The kind, as nodes name it where they meet. pub(crate) const KIND: &str = "events"; @@ -44,6 +44,9 @@ pub(crate) enum Beside { Forgotten(Instant), } +/// The most events the store has wait for what they build on. +const WAITS: usize = 1024; + /// The most names of strangers' entries remembered. const STRANGERS: usize = 1024; @@ -114,14 +117,22 @@ pub(crate) struct Set<F: Fold> { /// Entries of nodes that are no members, by name: not asked for again until who is /// one changes. No more than `STRANGERS`. strangers: BTreeSet<Name>, + /// The names of all the document gives to others, kept as it changes. + names: Names, } impl<F: Fold> Set<F> { /// The document of `store`, keeping what a checkpoint forgot for `keeps_forgotten`. pub(crate) fn new(store: Store<F>, keeps_forgotten: Duration) -> Self { let members = members_of::<F>(&store); + let names = store + .taken() + .iter() + .map(|id| id.0) + .chain(store.kept().iter().map(|bytes| Id::of(bytes).0)) + .collect(); Self { - store: store.telling(), + store: store.telling().waiting_no_more_than(WAITS), beside: BTreeMap::new(), unjudged: VecDeque::new(), unjudged_bytes: 0, @@ -129,6 +140,7 @@ impl<F: Fold> Set<F> { members, entries: BTreeMap::new(), strangers: BTreeSet::new(), + names, } } @@ -154,7 +166,12 @@ impl<F: Fold> Set<F> { Put::Older } _ => { - self.entries.insert(node, entry); + self.names.insert(entry.name()); + if let Some(older) = self.entries.insert(node, entry) { + self.names.remove(&older.name()); + } + #[cfg(test)] + self.check(); Put::Taken } } @@ -198,6 +215,40 @@ impl<F: Fold> Set<F> { offered } + /// Offers events to the store, at `now`, as `offer` does each: those the store took + /// by them, in the order taken. + pub(crate) fn offer_all( + &mut self, + events: impl IntoIterator<Item = Vec<u8>>, + now: Instant, + ) -> Vec<Id> { + let mut taken = Vec::new(); + for bytes in events { + if let Offered::Taken(ids) = self.store.offer(bytes) { + taken.extend(ids); + } + } + self.settle(now); + taken + } + + /// Reads anew who is a member, and lets go of the entries of those who aren't. + fn members_anew(&mut self) { + let members = members_of::<F>(&self.store); + if members != self.members { + let names = &mut self.names; + self.entries.retain(|node, entry| { + let member = members.contains(node); + if !member { + names.remove(&entry.name()); + } + member + }); + self.strangers.clear(); + self.members = members; + } + } + /// Has the store drop events it took (`Store::drop_taken`), at `now`: the document /// holds them on, for others. pub(crate) fn drop_taken(&mut self, ids: &[Id], now: Instant) -> Result<Vec<Id>, Refused> { @@ -222,6 +273,7 @@ impl<F: Fold> Set<F> { // order, the store has what comes early wait. for id in again { if let Some((bytes, why)) = self.beside.remove(&id) { + self.names.remove(&id.0); if why == Beside::Unjudged { self.unjudged.retain(|other| *other != id); self.unjudged_bytes -= bytes.len(); @@ -234,18 +286,27 @@ impl<F: Fold> Set<F> { /// Takes in what the store let go of. fn settle(&mut self, now: Instant) { - let members = members_of::<F>(&self.store); - if members != self.members { - self.entries.retain(|node, _| members.contains(node)); - self.strangers.clear(); - self.members = members; + let took = self.store.took(); + let gone = self.store.let_go(); + // Who is a member is the fold's to say of the store's state, and costs what + // reading that costs: asked only where the store changed. + if !took.is_empty() || !gone.is_empty() { + self.members_anew(); } - for (id, bytes, gone) in self.store.let_go() { + for id in took { + self.names.insert(id.0); + } + for (id, bytes, gone) in gone { let why = match gone { Gone::Forgotten => Beside::Forgotten(now), Gone::Own => Beside::Own, + Gone::NotAbove => { + self.names.remove(&id.0); + continue; + } Gone::BuiltOn(on) => { if !self.own(&on) { + self.names.remove(&id.0); continue; } Beside::Unjudged @@ -255,6 +316,7 @@ impl<F: Fold> Set<F> { self.unjudged.push_back(id); self.unjudged_bytes += bytes.len(); } + self.names.insert(id.0); self.beside.insert(id, (bytes, why)); } while self.unjudged.len() > UNJUDGED || self.unjudged_bytes > UNJUDGED_BYTES { @@ -262,6 +324,7 @@ impl<F: Fold> Set<F> { break; }; if let Some((bytes, _)) = self.beside.remove(&oldest) { + self.names.remove(&oldest.0); self.unjudged_bytes -= bytes.len(); } } @@ -276,6 +339,8 @@ impl<F: Fold> Set<F> { } !held }); + #[cfg(test)] + self.check(); } /// Whether the store refuses `id` as the node's own, or for building on such: what @@ -305,10 +370,19 @@ impl<F: Fold> Set<F> { pub(crate) fn expire(&mut self, now: Instant) -> bool { let keeps = self.keeps_forgotten; let before = self.beside.len(); - self.beside.retain(|_, (_, why)| match why { - Beside::Forgotten(since) => now.saturating_duration_since(*since) < keeps, - _ => true, + let names = &mut self.names; + self.beside.retain(|id, (_, why)| { + let keep = match why { + Beside::Forgotten(since) => now.saturating_duration_since(*since) < keeps, + _ => true, + }; + if !keep { + names.remove(&id.0); + } + keep }); + #[cfg(test)] + self.check(); self.beside.len() != before } @@ -429,6 +503,7 @@ impl<F: Fold> Set<F> { Beside::Forgotten(now.checked_sub(Duration::from_secs(ago)).unwrap_or(now)) } }; + set.names.insert(id.0); set.beside.insert(id, (bytes, why)); } set.settle(now); @@ -441,10 +516,29 @@ impl<F: Fold> Set<F> { Ok(set) } + /// That the names kept are those of what the document holds: looked at each time + /// while they are few, and now and then when many. + #[cfg(test)] + fn check(&self) { + if self.names.len() > 256 && !self.names.len().is_multiple_of(61) { + return; + } + assert!( + self.names.iter().eq(self.named().iter()), + "the names kept aren't what the document holds" + ); + } + /// The names of all the document gives to others, to catch up by /// (`document::ranges`): what its store took and what its checkpoint keeps, and what /// it holds beside. - pub(crate) fn names(&self) -> BTreeSet<Name> { + pub(crate) fn names(&self) -> &Names { + &self.names + } + + /// The names anew, from what the document holds: what `names` keeps. + #[cfg(test)] + pub(crate) fn named(&self) -> BTreeSet<Name> { self.store .taken() .iter() @@ -486,7 +580,7 @@ mod tests { let id = taken(set.offer(put.clone(), now)); assert_eq!(set.get(&id), Some(put.as_slice())); assert_eq!(set.beside(&id), None); - assert_eq!(set.names(), BTreeSet::from([id.0])); + assert_eq!(set.named(), BTreeSet::from([id.0])); } #[test] @@ -622,13 +716,13 @@ mod tests { let first_id = taken(set.offer(first.clone(), now)); let second = taken(set.offer(testfold::put(&[first_id], "a", "2"), now)); let lock = taken(set.offer(testfold::lock(&[second], "b"), now)); - let before = set.names(); + let before = set.named(); taken(set.offer(testfold::cut(&[lock], 1, &[lock]), now)); // The store moved, and forgot the first put; the document didn't. assert_eq!(set.store().get(&first_id), None); assert_eq!(set.get(&first_id), Some(first.as_slice())); assert_eq!(set.beside(&first_id), Some(Beside::Forgotten(now))); - assert!(set.names().is_superset(&before)); + assert!(set.named().is_superset(&before)); // Until its time is up. assert!(!set.expire(now + KEEPS - Duration::from_secs(1))); assert_eq!(set.get(&first_id), Some(first.as_slice())); @@ -774,7 +868,7 @@ mod tests { let later = now + Duration::from_secs(10); let read = Set::<TestFold>::load(&set.save(later), KEEPS, later).expect("a document"); - assert_eq!(read.names(), set.names()); + assert_eq!(read.named(), set.named()); assert_eq!(read.store().id(), set.store().id()); assert_eq!(read.store().checkpoint(), set.store().checkpoint()); assert_eq!(read.store().taken(), set.store().taken()); @@ -819,4 +913,39 @@ mod tests { cut.truncate(cut.len() - 3); assert!(Set::<TestFold>::load(&cut, KEEPS, Instant::now()).is_err()); } + + #[test] + fn what_a_move_leaves_beside_the_heads_is_held_no_more() { + let mut set = set(); + let now = Instant::now(); + let root = set.store().id(); + let first = taken(set.offer(testfold::put(&[root], "a", "1"), now)); + let lock = taken(set.offer(testfold::lock(&[first], "b"), now)); + // Written beside the lock, on the first put: not above the heads to come. + let beside = taken(set.offer(testfold::put(&[first], "c", "1"), now)); + assert!(set.names().contains(&beside.0)); + taken(set.offer(testfold::cut(&[lock], 1, &[lock]), now)); + assert_eq!(set.get(&beside), None); + assert!(!set.names().contains(&beside.0)); + assert!(!set.wants(&beside.0)); + } + + #[test] + fn no_more_wait_than_the_bound() { + let mut set = set(); + let now = Instant::now(); + let mut waiting = Vec::new(); + for n in 0..WAITS + 5 { + let on = Id::of(format!("nowhere {n}").as_bytes()); + let put = testfold::put(&[on], "a", "1"); + waiting.push(Id::of(&put)); + assert_eq!(set.offer(put, now), Offered::Waiting); + } + // The ones waiting longest went, as if they had never come: wanted again. + assert!(set.wants(&waiting[0].0)); + assert!(set.wants(&waiting[4].0)); + assert!(!set.wants(&waiting[5].0)); + assert!(!set.wants(&waiting[WAITS + 4].0)); + assert_eq!(set.store().lacks().len(), WAITS); + } } |
