diff options
| author | tv <tv@krebsco.de> | 2026-11-17 04:10:55 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-17 05:33:11 +0100 |
| commit | 171f1fcabcdb62b93696f5028120e9d5390b118a (patch) | |
| tree | f934a5d25ff7fda8417545db999540b02bb486fa | |
| parent | b88b3abffceb3f91bb5bee08d9b1889a41e7b852 (diff) | |
remake: holding a store, a node puts again what the document it left holds
After a switch or a rejoin, a node of a swarm holding a store puts again
what the document it left holds that a store starting at the new document's
checkpoint takes, by its own refusals and drops, and the new one lacks
(`docs/store.md`, Remakes, `Holding::left`): what it held beyond the base,
and, where it moved to a later checkpoint since, what a store starting at
the snapshot's takes to reach it. Its events aren't replayed into the new
document, so none nobody takes go along.
| -rw-r--r-- | src/federation/held.rs | 156 | ||||
| -rw-r--r-- | src/remake/catchup.rs | 11 | ||||
| -rw-r--r-- | src/remake/delta.rs | 15 | ||||
| -rw-r--r-- | src/remake/switch.rs | 11 |
4 files changed, 157 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] { diff --git a/src/remake/catchup.rs b/src/remake/catchup.rs index 35add67..e7184be 100644 --- a/src/remake/catchup.rs +++ b/src/remake/catchup.rs @@ -460,6 +460,17 @@ async fn finish<H: Host>(host: &H) { }) .await; let quarantine_id = quarantine::add(host, previous, target, &old, unplaced); + // Holding a store: what the old document holds that the new one's store takes goes + // again (`Holding::left`). + #[cfg(feature = "federation")] + let old = { + let holding = host.holding(); + host.read_doc(move |doc| { + holding.left(doc, &old); + old + }) + .await + }; // Unless the quarantine holds the document. if !old.get_heads().is_empty() && !quarantined_whole { diff --git a/src/remake/delta.rs b/src/remake/delta.rs index c3688cb..7543549 100644 --- a/src/remake/delta.rs +++ b/src/remake/delta.rs @@ -123,6 +123,15 @@ pub(crate) fn replay_newest( // Under a strict policy, the other writes go with the links by history too: one may // build on what a link grants (a capability, a key). let strict = At::new(src, after).is_some_and(|now| crate::access::policy::of(&now).strict()); + // Holding a store, its events aren't replayed: the node puts again those its store + // takes (`federation::held`, `Holding::left`). + #[cfg(feature = "federation")] + let holding = At::new(src, after).is_some_and(|now| { + matches!( + crate::access::policy::of(&now), + crate::access::policy::Policy::Store(_) + ) + }); // Values their users signed, under a strict policy: replayed as chains, below. let (signed, paths) = signed_places( src, @@ -200,6 +209,12 @@ pub(crate) fn replay_newest( if user.is_none() && others_entry(&path, node) { continue; } + #[cfg(feature = "federation")] + if holding + && matches!(path.as_slice(), [root, events, ..] if root == meta::ROOT && events == meta::EVENTS) + { + continue; + } match newest(&theirs, &path) { Some((newer, _)) if newer > at => { stats.superseded += 1; diff --git a/src/remake/switch.rs b/src/remake/switch.rs index 94956f6..657b0f5 100644 --- a/src/remake/switch.rs +++ b/src/remake/switch.rs @@ -297,6 +297,17 @@ async fn switch<H: Host>( &old, std::mem::take(&mut stats.quarantined), ); + // Holding a store: what the old document holds that the new one's store takes goes + // again (`Holding::left`). + #[cfg(feature = "federation")] + let old = { + let holding = host.holding(); + host.read_doc(move |doc| { + holding.left(doc, &old); + old + }) + .await + }; host.remake().archive( previous, old, |
