aboutsummaryrefslogtreecommitdiffstats
path: root/src/federation/held.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/federation/held.rs')
-rw-r--r--src/federation/held.rs149
1 files changed, 149 insertions, 0 deletions
diff --git a/src/federation/held.rs b/src/federation/held.rs
index ce97788..a081efb 100644
--- a/src/federation/held.rs
+++ b/src/federation/held.rs
@@ -131,6 +131,17 @@ struct Inner {
unrecorded: Option<String>,
}
+/// What a remake's snapshot holds of the store (`docs/store.md`, Remakes), as a node reads
+/// it at the remake's base (`Holding::at_base`).
+pub(crate) struct AtBase {
+ /// The checkpoint the store reaches there, where it isn't the root.
+ pub(crate) checkpoint: Option<Vec<u8>>,
+ /// The root, the events that checkpoint keeps, and those after it, by ID.
+ pub(crate) events: BTreeMap<Id, Vec<u8>>,
+ /// The members there, by node ID.
+ pub(crate) members: BTreeSet<String>,
+}
+
/// What a document holds that its store wasn't offered from it.
pub(crate) struct Unread {
epoch_seq: u32,
@@ -672,6 +683,48 @@ impl Holding {
pub(crate) fn with<T>(&self, f: impl FnOnce(&Store<Federation>) -> T) -> Option<T> {
self.inner.lock_or_recover().store.as_ref().map(f)
}
+
+ /// What a remake's snapshot holds of the store: as this node reads it anew from
+ /// `base`, the document at the remake's base, by its own refusals and its operator's
+ /// drops, as it reads its document whenever it starts. None where `base` holds no
+ /// store, or none this node reads there.
+ pub(crate) fn at_base(&self, base: &impl ReadDoc) -> Option<AtBase> {
+ let reading = Holding {
+ pin: self.pin,
+ inner: Mutex::new(Inner {
+ kept: self.inner.lock_or_recover().kept.clone(),
+ ..Inner::default()
+ }),
+ ..Holding::default()
+ };
+ reading.read(reading.unread(base, 0)?);
+ let swarm = events::swarm(base)?;
+ let root = events::bytes(base, &swarm)?;
+ reading.with(|store| {
+ let checkpoint = store.checkpoint();
+ let mut events: BTreeMap<Id, Vec<u8>> = store
+ .kept()
+ .into_iter()
+ .map(|bytes| (Id::of(&bytes), bytes))
+ .collect();
+ for id in store.taken() {
+ if let Some(bytes) = store.get(id) {
+ events.insert(*id, bytes.to_vec());
+ }
+ }
+ events.insert(swarm, root);
+ AtBase {
+ checkpoint: (Id::of(checkpoint) != swarm).then(|| checkpoint.to_vec()),
+ events,
+ members: store
+ .state()
+ .members()
+ .into_iter()
+ .map(|(node, _)| node)
+ .collect(),
+ }
+ })
+ }
}
/// The checkpoint `pin` names, found in the document's `events`: at heads a proposal's
@@ -981,6 +1034,102 @@ mod tests {
}
}
+ /// 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
+ /// entry of a node that isn't a member. A node reading it reaches the same store.
+ #[test]
+ fn a_snapshot_holds_the_store_read_at_its_base() {
+ let (tv, lass, makefu) = (
+ Party::new("tv", 1),
+ Party::new("lass", 2),
+ Party::new("makefu", 3),
+ );
+ let node = |seed: u8| crate::identity::NodeKey::from_seed([seed; 32]).id();
+ let (member, stranger) = (node(1), node(2));
+ let mut first = Spec::first(&[&tv, &lass, &makefu], &[]);
+ first.parties[0].nodes = vec![member.clone()];
+ let root = first.root();
+ let on = first.hash();
+ let gone = tv.write(&[Id::of(&root)], "home/tv/a", harness::str("1"));
+ let kept = tv.write(&[Id::of(&gone)], "home/tv/a", harness::str("2"));
+ let head = lass.write(&[Id::of(&kept)], "home/lass/b", harness::str("3"));
+ let mut next = first.after(on);
+ next.checkpoint = Some(vec![Id::of(&head)]);
+ let proposal = tv.propose(&[Id::of(&head)], &next.json());
+ let votes = [
+ tv.vote(&[Id::of(&proposal)], on, next.hash(), true),
+ lass.vote(&[Id::of(&proposal)], on, next.hash(), true),
+ ];
+ let written = harness::version_event(&[Id::of(&votes[0]), Id::of(&votes[1])], next.hash());
+ let after = makefu.write(&[Id::of(&written)], "home/makefu/c", harness::str("3"));
+ let nobodys = Party::new("nobody", 9).write(&[Id::of(&root)], "home/x", harness::str("4"));
+ let mut doc = events::started(Federation::NAME, &root, &[]).unwrap();
+ for event in [
+ &gone, &kept, &head, &proposal, &votes[0], &votes[1], &written,
+ ]
+ .into_iter()
+ .chain([&after, &nobodys])
+ {
+ put(&mut doc, event);
+ }
+ for entry in [&member, &stranger] {
+ let mut write = doc.transaction();
+ store::write_path(
+ &mut write,
+ &meta::path(&[meta::MEMBERS, entry]),
+ &serde_json::json!({"alias": entry}),
+ )
+ .unwrap();
+ write.commit();
+ }
+ let holding = Holding::default();
+ read(&holding, &doc, 0);
+ let (checkpoint, mut taken) = holding
+ .with(|store| (store.checkpoint().to_vec(), store.taken().to_vec()))
+ .unwrap();
+ 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;
+ assert_eq!(events::checkpoint(&snapshot), Some(checkpoint.clone()));
+ let held = events::ids(&snapshot);
+ for kept in [&root, &kept, &after] {
+ assert!(held.contains(&Id::of(kept)));
+ }
+ for left in [&gone, &nobodys] {
+ assert!(!held.contains(&Id::of(left)));
+ }
+ let entries = store::keys_at(&snapshot, &meta::path(&[meta::MEMBERS])).unwrap();
+ assert_eq!(entries, Some(vec![member.clone()]));
+
+ let anew = Holding::default();
+ read(&anew, &snapshot, 1);
+ let (reached, mut read_taken) = anew
+ .with(|store| (store.checkpoint().to_vec(), store.taken().to_vec()))
+ .unwrap();
+ read_taken.sort_unstable();
+ assert_eq!((reached, read_taken), (checkpoint, taken));
+ }
+
/// An event put at the API is offered to the store; what the operator drops is
/// refused, and kept so, until it allows it again.
#[test]