diff options
| author | tv <tv@krebsco.de> | 2026-11-30 19:36:56 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-30 19:36:56 +0000 |
| commit | 6a06bc4a955cdf2b5a183c9ff21e25c79a4e5d9f (patch) | |
| tree | 8ca41fca8d70f938e8444b0cceed880975c9797a /src | |
| parent | 3bc200a2fdae7b928c68b4a1f8bfee9a919e8ff8 (diff) | |
federation: a stopped node on a set passes on what it holds back
A store that stopped at an event it can't judge holds back what comes
after, and its set gave none of it to others: what was put through such
a node reached nobody, and a restart lost it. The set now names what its
store holds back among what it gives, passes it on at once as it does
what it took, and keeps it in its file; once the store takes again, it
gives of it what the store took, or refused of its own.
Diffstat (limited to 'src')
| -rw-r--r-- | src/federation/held.rs | 3 | ||||
| -rw-r--r-- | src/federation/store/memory.rs | 8 | ||||
| -rw-r--r-- | src/federation/store/set.rs | 122 | ||||
| -rw-r--r-- | src/simtest/holding.rs | 72 |
4 files changed, 192 insertions, 13 deletions
diff --git a/src/federation/held.rs b/src/federation/held.rs index c5527cb..39d6102 100644 --- a/src/federation/held.rs +++ b/src/federation/held.rs @@ -1545,7 +1545,8 @@ pub(crate) async fn write( if state.of_a_set() { let holding = Arc::clone(&state.holding); let _ = tokio::task::spawn_blocking(move || holding.offer(bytes)).await; - let held = state.holding.with(|store| store.get(&id).is_some()); + // What the set gives: taken, or held back by a store that stopped. + let held = state.holding.with_set(|set| set.get(&id).is_some()); if held == Some(true) { crate::swarm::set::wrote(state, vec![id]).await; } diff --git a/src/federation/store/memory.rs b/src/federation/store/memory.rs index 8f099be..6f4ee5d 100644 --- a/src/federation/store/memory.rs +++ b/src/federation/store/memory.rs @@ -332,6 +332,14 @@ impl<F: Fold> Store<F> { .map(|(id, why)| (id, why.as_str(), self.held_back.len())) } + /// The events the store holds back, stopped, in the order offered: the one it stopped + /// at, and those offered since. None while it isn't stopped. + pub(crate) fn holds_back(&self) -> impl ExactSizeIterator<Item = (Id, &[u8])> { + self.held_back + .iter() + .map(|(id, event)| (*id, event.bytes.as_slice())) + } + /// What the store holds back, stopped (`Fold::stops`): the events offered since, and /// those waiting for events it doesn't hold, as it can't tell which will be taken; each /// after those of its dependencies among them, by ID otherwise. Nothing while it isn't diff --git a/src/federation/store/set.rs b/src/federation/store/set.rs index b033927..d0dbe18 100644 --- a/src/federation/store/set.rs +++ b/src/federation/store/set.rs @@ -9,6 +9,8 @@ //! oldest going first: whoever mints events can mint these; //! * what a move to a checkpoint forgot, for as long as the node's operator says: a node //! still before the checkpoint builds it from them. +//! * what the store holds back, stopped at an event it can't judge (`Fold::stops`): the +//! node can't tell which of it will be taken, and a node that judges it takes it. //! //! And each member's entry (`entry`): its node's newest, and none of a node the store //! doesn't name a member as it reads now. @@ -123,6 +125,9 @@ pub(crate) struct Set<F: Fold> { /// The unjudged among them, the oldest first, and their bytes in all. unjudged: VecDeque<Id>, unjudged_bytes: usize, + /// What the store holds back, stopped, by ID: where among what it holds back + /// (`Store::holds_back`). + held_back: BTreeMap<Id, usize>, /// For how long what a checkpoint forgot is kept. keeps_forgotten: Duration, /// Who carries the store, as it reads now (`Fold::members`). @@ -157,6 +162,7 @@ impl<F: Fold> Set<F> { beside: BTreeMap::new(), unjudged: VecDeque::new(), unjudged_bytes: 0, + held_back: BTreeMap::new(), keeps_forgotten, members, entries: BTreeMap::new(), @@ -245,12 +251,13 @@ impl<F: Fold> Set<F> { /// document keeps as far as it gives it to others. pub(crate) fn offer(&mut self, bytes: Vec<u8>, now: Instant) -> Offered { let offered = self.store.offer(bytes); - self.settle(now); + let _ = self.settle(now); offered } /// Offers events to the store, at `now`, as `offer` does each: those the store took - /// by them, in the order taken. + /// by them, in the order taken, and those it holds back since, stopped: what the + /// document gives to others by them. pub(crate) fn offer_all( &mut self, events: impl IntoIterator<Item = Vec<u8>>, @@ -262,7 +269,7 @@ impl<F: Fold> Set<F> { taken.extend(ids); } } - self.settle(now); + taken.extend(self.settle(now)); taken } @@ -289,7 +296,7 @@ impl<F: Fold> Set<F> { /// holds them on, for others. pub(crate) fn drop_taken(&mut self, ids: &[Id], now: Instant) -> Result<Vec<Id>, Refused> { let dropped = self.store.drop_taken(ids); - self.settle(now); + let _ = self.settle(now); dropped } @@ -317,11 +324,12 @@ impl<F: Fold> Set<F> { self.store.offer(bytes); } } - self.settle(now); + let _ = self.settle(now); } - /// Takes in what the store let go of. - fn settle(&mut self, now: Instant) { + /// Takes in what the store let go of, and what it holds back, stopped: the events it + /// holds back since last asked, in the order offered. + fn settle(&mut self, now: Instant) -> Vec<Id> { 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 @@ -376,8 +384,42 @@ impl<F: Fold> Set<F> { } !held }); + let held_back = self.hold_back(); #[cfg(test)] self.check(); + held_back + } + + /// Follows what the store holds back, stopped: its names among those the document + /// gives, and those held back since last asked, in the order offered. + fn hold_back(&mut self) -> Vec<Id> { + let holds = self.store.holds_back().len(); + // The store holds back more, after what it held back: the common case. + let more = holds >= self.held_back.len() + && self.held_back.len().checked_sub(1).is_none_or(|last| { + self.store + .holds_back() + .nth(last) + .is_some_and(|(id, _)| self.held_back.get(&id) == Some(&last)) + }); + let from = if more { self.held_back.len() } else { 0 }; + if !more { + // It takes again, and may have stopped anew: what it held back, it took, + // refused, or holds back again. + for id in std::mem::take(&mut self.held_back).into_keys() { + if self.store.get(&id).is_none() && !self.beside.contains_key(&id) { + self.names.remove(&id.0); + } + } + } + let mut since = Vec::new(); + for (at, (id, _)) in self.store.holds_back().enumerate().skip(from) { + self.held_back.insert(id, at); + if self.names.insert(id.0) { + since.push(id); + } + } + since } /// Whether the store refuses `id` as the node's own, or for building on such: what @@ -423,12 +465,16 @@ impl<F: Fold> Set<F> { self.beside.len() != before } - /// The bytes of an event the document gives to others: one its store holds, or one - /// held beside it. + /// 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]> { self.store .get(id) .or_else(|| self.beside.get(id).map(|(bytes, _)| bytes.as_slice())) + .or_else(|| { + let at = *self.held_back.get(id)?; + self.store.holds_back().nth(at).map(|(_, bytes)| bytes) + }) } /// Why the document holds `id` beside its store, where it does. @@ -436,7 +482,8 @@ impl<F: Fold> Set<F> { self.beside.get(id).map(|(_, why)| *why) } - /// The document as bytes, at `now`: what its file holds. All it holds but what waits + /// The document as bytes, at `now`: what its file holds, what its store holds back + /// among it. All it holds but what waits /// for what it builds on, which comes again; and what its node refuses of its own, /// which a store read anew is told before anything is offered to it. pub(crate) fn save(&self, now: Instant) -> Vec<u8> { @@ -451,6 +498,12 @@ impl<F: Fold> Set<F> { .iter() .filter_map(|id| self.store.get(id)) .map(|bytes| bytes.to_vec().into()) + // What the store holds back, after: offered again, it stops there again. + .chain( + self.store + .holds_back() + .map(|(_, bytes)| bytes.to_vec().into()), + ) .collect(), own: own.iter().map(|id| id.0).collect(), dropped: dropped.iter().map(|id| id.0).collect(), @@ -521,7 +574,7 @@ impl<F: Fold> Set<F> { for event in saved.taken { set.store.offer(event.into_vec()); } - set.settle(now); + let _ = set.settle(now); for (bytes, why) in saved.beside { let bytes = bytes.into_vec(); let id = Id::of(&bytes); @@ -543,7 +596,7 @@ impl<F: Fold> Set<F> { set.names.insert(id.0); set.beside.insert(id, (bytes, why)); } - set.settle(now); + let _ = set.settle(now); set.expire(now); for entry in saved.entries { if let Some(entry) = Entry::decode(&entry) { @@ -595,6 +648,7 @@ impl<F: Fold> Set<F> { .map(|id| id.0) .chain(self.store.kept().iter().map(|bytes| Id::of(bytes).0)) .chain(self.beside.keys().map(|id| id.0)) + .chain(self.held_back.keys().map(|id| id.0)) .chain(self.entries.values().map(Entry::name)) .collect() } @@ -998,4 +1052,48 @@ mod tests { assert!(!set.wants(&waiting[WAITS + 4].0)); assert_eq!(set.store().lacks().len(), WAITS); } + + /// What a store that stopped holds back, the document gives to others, as the node + /// can't tell which of it will be taken; its file keeps it; and once the store takes + /// again, the document gives what the store took of it and no more. + #[test] + fn what_a_stopped_store_holds_back_is_given_to_others() { + let mut set = set(); + let now = Instant::now(); + let root = set.store().id(); + let mark = taken(set.offer(testfold::put(&[root], "mark", "1"), now)); + let new = testfold::era(&[mark], 1, "new"); + let at = Id::of(&new); + let later = testfold::put(&[mark], "b", "1"); + let junk = testfold::junk(&[mark]); + let (later_id, junk_id) = (Id::of(&later), Id::of(&junk)); + // Held back, and given: by name, to catch up by, and as what was taken by them. + assert_eq!( + set.offer_all([new.clone(), later.clone(), junk.clone()], now), + vec![at, later_id, junk_id] + ); + assert!(set.store().stopped().is_some()); + for (id, bytes) in [(at, &new), (later_id, &later), (junk_id, &junk)] { + assert_eq!(set.get(&id), Some(bytes.as_slice())); + assert!(set.names().contains(&id.0)); + assert!(!set.wants(&id.0)); + } + // Offered again, nothing is given anew. + assert!(set.offer_all([later.clone()], now).is_empty()); + // Its file keeps what it holds back: read, the store stops there again. + let mut read = Set::<TestFold>::load(&set.save(now), KEEPS, now).expect("it reads"); + assert_eq!(read.store().stopped().map(|(id, _, _)| *id), Some(at)); + assert_eq!(read.named(), set.named()); + assert_eq!(read.get(&later_id), Some(later.as_slice())); + // Its operator has it drop the event it stopped at: what it held back is taken, + // or refused, and what is refused at its history is given no more. + read.drop_taken(&[at], now).expect("dropped"); + assert!(read.store().stopped().is_none()); + assert_eq!(read.store().taken(), &[mark, later_id]); + assert_eq!(read.get(&junk_id), None); + assert!(!read.names().contains(&junk_id.0)); + // What it dropped is the node's own, which others may take. + assert_eq!(read.beside(&at), Some(Beside::Own)); + assert!(read.names().contains(&at.0)); + } } diff --git a/src/simtest/holding.rs b/src/simtest/holding.rs index 8091cfc..8f601dd 100644 --- a/src/simtest/holding.rs +++ b/src/simtest/holding.rs @@ -1807,6 +1807,78 @@ async fn a_swarm_of_sets_passes_entries_and_events_on() { } } +/// On sets of events, a version of a federation this continuum doesn't know passes, and +/// a version event writes it: every node stops there. What is put through one of them +/// meanwhile, it holds back and passes on, as it can't tell what will be taken: the +/// others hold it back too. Their operators have them drop that event: they take again, +/// all alike. +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn a_stopped_node_of_a_set_passes_on_what_it_holds_back() { + let sim = Sim::with_identity(99); + let (tv, lass, makefu) = three(); + let (first, root, id) = federation(&[(&tv, &["a"]), (&lass, &["b"]), (&makefu, &["c"])]); + let nodes: Vec<AppState> = [("a", "tv"), ("b", "lass"), ("c", "makefu")] + .into_iter() + .map(|(name, party)| { + let seeds: Vec<&str> = if name == "a" { Vec::new() } else { vec!["a"] }; + let root = root.clone(); + sim.start_with( + name, + &seeds, + Start::FromDocument(Origin::Root), + move |config| { + config.store = Some(id); + config.holding = of_a_set(party, &root); + }, + ) + }) + .collect(); + let members: Vec<&AppState> = nodes.iter().collect(); + let all = &members; + sim.wait_ready(all, SOON).await; + sim.wait_until("each holds the three entries", SOON, move || async move { + all.iter().all(|node| entries(node) == 3) + }) + .await; + + let on = first.hash(); + let text = String::from_utf8(first.after(on).json().bytes()) + .expect("UTF-8") + .replace(r#""federation":1"#, r#""federation":2"#); + let unknown = crate::federation::jcs::read(text.as_bytes()).expect("JCS"); + let hash = Id::of(&unknown.bytes()); + let proposal = tv.propose(&[id], &unknown); + let votes = [&tv, &lass].map(|party| party.vote(&[Id::of(&proposal)], on, hash, true)); + let written = harness::version_event(&[Id::of(&votes[0]), Id::of(&votes[1])], hash); + let at = Id::of(&written); + put(all[0], &[&proposal, &votes[0], &votes[1], &written]).await; + sim.wait_until("every node stopped there", LONG, move || async move { + all.iter().all(|node| stopped(node) == Some(at)) + }) + .await; + + let meanwhile = tv.write(&[id], "home/tv/a", s("1")); + put(all[0], &[&meanwhile]).await; + let meanwhile = Id::of(&meanwhile); + sim.wait_until("every node holds it back", LONG, move || async move { + all.iter() + .all(|node| holds_back(node, &[at, meanwhile], false)) + }) + .await; + + for node in all.iter() { + let dropped = node.holding.drop_taken(&[at]).expect("a store"); + assert_eq!(dropped, Ok(vec![at])); + } + sim.wait_until("every node takes again", LONG, move || async move { + all.iter() + .all(|node| stopped(node).is_none() && holds(node, &[meanwhile])) + }) + .await; + let read: Vec<Option<Counts>> = all.iter().map(|node| counts(node)).collect(); + assert!(read.iter().all(|counts| counts == &read[0]), "{read:?}"); +} + /// Starts `name`, of `party`, on a set of events `set`, kept in the file at `path` where /// given: the first of them with no seed, the others seeded by `a`. fn start_on_a_set( |
