diff options
Diffstat (limited to 'src/federation')
| -rw-r--r-- | src/federation/held.rs | 156 |
1 files changed, 120 insertions, 36 deletions
diff --git a/src/federation/held.rs b/src/federation/held.rs index a081efb..9ea26ee 100644 --- a/src/federation/held.rs +++ b/src/federation/held.rs @@ -129,6 +129,9 @@ struct Inner { /// Its party's record, voting by itself, once read; and why it isn't, said once. record: Option<Record>, unrecorded: Option<String>, + /// What the document it left at a switch or a rejoin holds that a store starting at + /// the new one's checkpoint takes, in the order taken (`left`). + left: Vec<(Id, Vec<u8>)>, } /// What a remake's snapshot holds of the store (`docs/store.md`, Remakes), as a node reads @@ -438,25 +441,74 @@ impl Holding { } } - /// The events the store holds that `doc` lacks, each after its dependencies: what its - /// checkpoint keeps, then what it took after it, in that order. + /// The events the node has that `doc` lacks, each after its dependencies: of the + /// document it left, what a store starting at `doc`'s checkpoint takes (`left`); then + /// what its store's checkpoint keeps, then what it took after it. pub(crate) fn lacking(&self, doc: &impl ReadDoc) -> Vec<(Id, Vec<u8>)> { + let mut held: HashSet<Id> = events::ids(doc).into_iter().collect(); + let mut inner = self.inner.lock_or_recover(); + inner.left.retain(|(id, _)| !held.contains(id)); + let mut lacking = inner.left.clone(); + if let Some(store) = &inner.store { + lacking.extend( + store + .kept() + .into_iter() + .map(|bytes| (Id::of(&bytes), bytes)) + .chain( + store + .taken() + .iter() + .filter_map(|id| Some((*id, store.get(id)?.to_vec()))), + ), + ); + } + lacking.retain(|(id, _)| held.insert(*id)); + lacking + } + + /// What `left`, the document the node left at a switch or a rejoin, holds that a store + /// starting at `doc`'s checkpoint takes, by the node's own refusals and its operator's + /// drops, and `doc` lacks (`docs/store.md`, Remakes): kept, to put again (`lacking`). + /// So what the node held beyond the base goes again, and, where it moved to a later + /// checkpoint since, what a store starting at the snapshot's takes to reach it. + pub(crate) fn left(&self, doc: &impl ReadDoc, left: &impl ReadDoc) { + let reading = Holding { + pin: self.pin, + inner: Mutex::new(Inner { + kept: self.inner.lock_or_recover().kept.clone(), + ..Inner::default() + }), + ..Holding::default() + }; + let Some(unread) = reading.unread(doc, 0) else { + return; + }; + reading.read(unread); let held: HashSet<Id> = events::ids(doc).into_iter().collect(); - self.with(|store| { - store - .kept() - .into_iter() - .map(|bytes| (Id::of(&bytes), bytes)) - .chain( - store - .taken() - .iter() - .filter_map(|id| Some((*id, store.get(id)?.to_vec()))), - ) - .filter(|(id, _)| !held.contains(id)) - .collect() - }) - .unwrap_or_default() + let events: BTreeMap<Id, Vec<u8>> = events::ids(left) + .into_iter() + .filter(|id| !held.contains(id)) + .filter_map(|id| Some((id, events::bytes(left, &id)?))) + .collect(); + let mut put = Vec::new(); + if let Some(store) = reading.inner.lock_or_recover().store.as_mut() { + for bytes in events.values() { + if let Offered::Taken(ids) = store.offer(bytes.clone()) { + put.extend( + ids.into_iter() + .filter_map(|id| Some((id, events.get(&id)?.clone()))), + ); + } + } + } + if !put.is_empty() { + info!( + events = put.len(), + "Of the document left, events to put again" + ); + } + self.inner.lock_or_recover().left = put; } /// Whether `node`, of `party`, is a member of the store as it reads now: one the fold @@ -1034,6 +1086,56 @@ mod tests { } } + /// The snapshot of a compaction at `doc`'s heads, holding the store as `holding` + /// reads it there. + fn snapshot(doc: &Document, holding: &Holding) -> Document { + let heads = doc.get_heads(); + let policy = serde_json::json!({}); + crate::remake::snapshot::build( + doc, + &crate::remake::snapshot::SnapshotParams { + remake_id: "c-1", + coordinator: "a node", + previous: crate::document::replica::Epoch::GENESIS, + base_heads: &heads, + policy: &policy, + proposed_at: crate::clock::UnixMillis(0), + kind: "compact", + content: None, + translation: &serde_json::Value::Null, + held: Some(holding), + }, + ) + .unwrap() + .doc + } + + /// After a switch, what the document left holds beyond the base that a store starting + /// at the new document's checkpoint takes goes again; what nobody takes doesn't. + #[test] + fn a_node_puts_again_what_the_document_it_left_holds() { + let (tv, lass) = (Party::new("tv", 1), Party::new("lass", 2)); + let root = Spec::first(&[&tv, &lass], &[]).root(); + let write = tv.write(&[Id::of(&root)], "home/tv/a", harness::str("1")); + let mut old = events::started(Federation::NAME, &root, &[]).unwrap(); + put(&mut old, &write); + let holding = Holding::default(); + read(&holding, &old, 0); + let new = snapshot(&old, &holding); + let later = lass.write(&[Id::of(&write)], "home/lass/b", harness::str("2")); + let nobodys = Party::new("nobody", 9).write(&[Id::of(&root)], "home/x", harness::str("3")); + put(&mut old, &later); + put(&mut old, &nobodys); + let switched = Holding::default(); + switched.left(&new, &old); + let lacking: Vec<Id> = switched + .lacking(&new) + .into_iter() + .map(|(id, _)| id) + .collect(); + assert_eq!(lacking, vec![Id::of(&later)]); + } + /// A compaction of a swarm whose store moved to a checkpoint: its snapshot holds that /// checkpoint, the root, what the checkpoint keeps and what came after it, and the /// members' entries; not what the checkpoint forgot, an event nobody takes, nor the @@ -1091,25 +1193,7 @@ mod tests { taken.sort_unstable(); assert_ne!(Id::of(&checkpoint), Id::of(&root)); - let heads = doc.get_heads(); - let policy = serde_json::json!({}); - let snapshot = crate::remake::snapshot::build( - &doc, - &crate::remake::snapshot::SnapshotParams { - remake_id: "c-1", - coordinator: &member, - previous: crate::document::replica::Epoch::GENESIS, - base_heads: &heads, - policy: &policy, - proposed_at: crate::clock::UnixMillis(0), - kind: "compact", - content: None, - translation: &serde_json::Value::Null, - held: Some(&holding), - }, - ) - .unwrap() - .doc; + let snapshot = snapshot(&doc, &holding); assert_eq!(events::checkpoint(&snapshot), Some(checkpoint.clone())); let held = events::ids(&snapshot); for kept in [&root, &kept, &after] { |
