diff options
| author | tv <tv@krebsco.de> | 2026-11-30 17:57:55 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-30 17:57:55 +0000 |
| commit | 08ec65219991c7413c8cdef4ce77434f2a121331 (patch) | |
| tree | 2c9d6f07e435a1ae37c62571579b170374cfbb70 /src/swarm | |
| parent | 00e88cd983766abec32b5a14a8cbf20932a27f3c (diff) | |
A node of both kinds, for a swarm to move to sets by (--store-both)
With store-document, store-both has the node hold its store in the
Automerge document too, and sync with nodes of either kind:
* it says to each peer the kind that peer holds, and passes its set on
to those that hold sets (swarm::set::holds_a_set), the Automerge
document to the others;
* what its set takes of the document goes on to the peers on sets
(Holding::unpassed), and what its set takes of them is put into the
document, as events it lacks are (Holding::lacking);
* its entry in the document is put as its entry in the set
(swarm::set::mirror_entry), and it reads the members of both;
* without the set's file yet, its set is made of the store as it reads
it of its document, with what store-file kept of its own.
A simulation: two nodes on the Automerge document, one on a set, one
of both between them; an event put on either side reaches all four.
Diffstat (limited to 'src/swarm')
| -rw-r--r-- | src/swarm/bootstrap.rs | 35 | ||||
| -rw-r--r-- | src/swarm/peer/inbound.rs | 11 | ||||
| -rw-r--r-- | src/swarm/peer/keepalive.rs | 18 | ||||
| -rw-r--r-- | src/swarm/set.rs | 69 | ||||
| -rw-r--r-- | src/swarm/sync.rs | 6 |
5 files changed, 111 insertions, 28 deletions
diff --git a/src/swarm/bootstrap.rs b/src/swarm/bootstrap.rs index b21b579..45c86e6 100644 --- a/src/swarm/bootstrap.rs +++ b/src/swarm/bootstrap.rs @@ -344,14 +344,16 @@ impl StoreStart { /// at `path`: as the file has it, where there is one, which must be this store's; /// else begun from the root, where the node is given it and pins no checkpoint; else /// to be gathered of its peers, from the checkpoint `pin` names, or the root, by its - /// name. What a checkpoint forgot is kept for `keeps`. + /// name; of `both` kinds, none without a file. What a checkpoint forgot is kept for + /// `keeps`. pub(crate) fn set( &self, path: &Path, files: &Files, pin: Option<crate::federation::store::Id>, keeps: std::time::Duration, - ) -> Result<crate::federation::held::Begun> { + both: bool, + ) -> Result<Option<crate::federation::held::Begun>> { use crate::federation::fold::Federation; use crate::federation::held::Begun; use crate::federation::store::{memory::Store, passing::Start, set::Set}; @@ -376,7 +378,12 @@ impl StoreStart { ); } info!(swarm_id = %self.id, path = %path.display(), "Going on with the store's document"); - return Ok(Begun::Set(Box::new(set))); + return Ok(Some(Begun::Set(Box::new(set)))); + } + // A node of both kinds makes its set of the store as it reads it of its + // document (`Holding::of_both`). + if both { + return Ok(None); } match (&self.root, pin) { (Some((root, kept)), None) => { @@ -385,12 +392,12 @@ impl StoreStart { anyhow::anyhow!("The store's root is none: {}", refused.0) })?; info!(swarm_id = %self.id, "Starting the swarm holding the store from its root, as a set of events (--store-document)"); - Ok(Begun::Set(Box::new(Set::new(store, keeps)))) + Ok(Some(Begun::Set(Box::new(Set::new(store, keeps))))) } (_, pin) => { let at = pin.unwrap_or(self.id); info!(swarm_id = %self.id, from = %at, "Joining the swarm holding the store with nothing of it: its set begins from what members give by that name"); - Ok(Begun::Start(Start::new(self.id, at, keeps))) + Ok(Some(Begun::Start(Start::new(self.id, at, keeps)))) } } } @@ -753,9 +760,9 @@ mod tests { let files = Files::default(); let keeps = Duration::from_secs(60); use crate::federation::held::Begun; - let set_of = |start: &StoreStart, pin| match start.set(&path, &files, pin, keeps) { - Ok(Begun::Set(set)) => Some(*set), - Ok(Begun::Start(_)) => None, + let set_of = |start: &StoreStart, pin| match start.set(&path, &files, pin, keeps, false) { + Ok(Some(Begun::Set(set))) => Some(*set), + Ok(Some(Begun::Start(_)) | None) => None, Err(error) => panic!("{error:#}"), }; // No file: from the root, where given and no checkpoint is pinned; else to be @@ -780,9 +787,17 @@ mod tests { id: Id::of(&other_root), root: Some((other_root, Vec::new())), }; - assert!(other.set(&path, &files, None, keeps).is_err()); + assert!(other.set(&path, &files, None, keeps, false).is_err()); std::fs::write(&path, b"something else").unwrap(); - assert!(store.set(&path, &files, None, keeps).is_err()); + assert!(store.set(&path, &files, None, keeps, false).is_err()); + // A node of both kinds: the file, where there is one; else none here. + std::fs::remove_file(&path).unwrap(); + assert!( + store + .set(&path, &files, None, keeps, true) + .unwrap() + .is_none() + ); } /// A fork's root, and the event it keeps on a line of its own: the swarm starts from diff --git a/src/swarm/peer/inbound.rs b/src/swarm/peer/inbound.rs index 35d67c8..b6c486d 100644 --- a/src/swarm/peer/inbound.rs +++ b/src/swarm/peer/inbound.rs @@ -228,11 +228,14 @@ pub(crate) async fn handle_message( .map_err(|error| anyhow!("Failed to notify sync worker: {error}"))?; } transport::ParsedMessage::Sync(envelope) => { - // Of a set of events, where one carries this node's store: its own to - // take (`swarm::set`), and nothing of an Automerge document's. + // Of a set of events: this node's set's to take, where it holds one + // (`swarm::set`); and of an Automerge document nothing, where it holds a set + // alone. #[cfg(feature = "federation")] - if state.of_a_set() { - if let transport::SyncBody::Kind(body) = envelope.body { + if state.of_a_set() || matches!(envelope.body, transport::SyncBody::Kind(_)) { + if let transport::SyncBody::Kind(body) = envelope.body + && state.holding.has_a_set() + { crate::swarm::set::received(state, &peer, body).await; } return Ok(()); diff --git a/src/swarm/peer/keepalive.rs b/src/swarm/peer/keepalive.rs index 6ab49d8..bbcc6e3 100644 --- a/src/swarm/peer/keepalive.rs +++ b/src/swarm/peer/keepalive.rs @@ -182,13 +182,21 @@ pub(crate) async fn local_status(state: &AppState, peer: &str) -> transport::Sta let stamp = state.stamp.borrow(); (stamp.summary(), stamp.epoch, stamp.epoch_seq) }; - let syncs_with_you = match state.get_peer_state(peer).await { - Some(peer_state) => peer_state.lock().await.syncs_on(epoch), - None => false, + let (syncs_with_you, _of_a_set) = match state.get_peer_state(peer).await { + Some(peer_state) => { + let peer_state = peer_state.lock().await; + ( + peer_state.syncs_on(epoch), + peer_state + .kind + .is_some_and(|kind| kind != crate::document::KIND), + ) + } + None => (false, false), }; - // Where a set of events carries the store, where that stands (`swarm::set`). + // To a peer that holds a set of events, where this node's stands (`swarm::set`). #[cfg(feature = "federation")] - let heads = crate::swarm::set::stamp(state).unwrap_or(heads); + let heads = crate::swarm::set::stamp(state, _of_a_set).unwrap_or(heads); let (reaches, round_trips) = crate::swarm::reach::published_with_round_trips(state); transport::Status { heads, diff --git a/src/swarm/set.rs b/src/swarm/set.rs index 6df78ba..54a48fe 100644 --- a/src/swarm/set.rs +++ b/src/swarm/set.rs @@ -22,10 +22,22 @@ use crate::state::AppState; use crate::swarm::topology; use crate::transport::{self, HeadsSummary, ParsedMessage, SyncBody}; -/// Where this node's set stands, as its STATUSes say it; none, where no set carries its -/// store. -pub(crate) fn stamp(state: &AppState) -> Option<HeadsSummary> { - if !state.holding.of_a_set() { +/// Whether `peer` holds a set of events, as its handshakes said: what a node of both +/// kinds passes its set on to. A node that holds a set alone takes handshakes of no +/// other. +pub(crate) async fn holds_a_set(state: &AppState, peer: &str) -> bool { + match state.get_peer_state(peer).await { + Some(peer_state) => { + peer_state.lock().await.kind == Some(crate::federation::store::set::KIND) + } + None => false, + } +} + +/// Where this node's set stands, as its STATUSes say it to a peer that holds one +/// (`to_a_set`); none, where the node holds no set, or the peer doesn't. +pub(crate) fn stamp(state: &AppState, to_a_set: bool) -> Option<HeadsSummary> { + if !state.holding.has_a_set() || !(to_a_set || state.holding.of_a_set()) { return None; } // A node still gathering what its set begins from stands nowhere yet. @@ -45,9 +57,14 @@ pub(crate) fn heard_status( peer: &str, mut remote: transport::Status, ) -> transport::Status { - let Some(ours) = stamp(state) else { + // A peer that says where a set stands holds one. + let of_a_set = matches!(remote.heads, HeadsSummary::Kind(_)); + let Some(ours) = stamp(state, of_a_set) else { return remote; }; + if !of_a_set && !state.holding.of_a_set() { + return remote; + } if let HeadsSummary::Kind(bytes) = std::mem::replace(&mut remote.heads, ours) && let Ok(stamp) = rmp_serde::from_slice::<passing::Stamp>(&bytes) { @@ -174,7 +191,10 @@ async fn pass_on(state: &AppState, from: Option<&str>, took: Took) { } let message = Message::Units(units); for peer in state.active_peers_on_local_epoch().await { - if Some(peer.as_str()) != from && topology::is_neighbour(state, &peer) { + if Some(peer.as_str()) != from + && topology::is_neighbour(state, &peer) + && holds_a_set(state, &peer).await + { send(state, &peer, &message).await; } } @@ -219,3 +239,40 @@ pub(crate) async fn registered(state: &AppState) { ) .await; } + +/// On a node of both kinds, puts its entry as the document it holds has it as its entry +/// in the set, where the two differ, and passes it on: what the node says of itself, +/// it says to the peers of either kind. +pub(crate) async fn mirror_entry(state: &AppState) { + use crate::federation::store::entry::Entry; + use crate::federation::store::set::Put; + + if !state.holding.has_a_set() || state.holding.of_a_set() { + return; + } + let Some(key) = state.node_key.clone() else { + return; + }; + let node = state.local_id().to_owned(); + let own = crate::swarm::members::member_path(&node); + let written = crate::document::view::read_path(&state.doc.read().await.doc, &own); + let Ok(Some(serde_json::Value::Object(written))) = written else { + return; + }; + let put = state.holding.change_set(|set| { + let held = set.entry(&node); + if held.and_then(|entry| entry.value()).as_ref() == Some(&written) { + return None; + } + let count = held.map_or(1, |entry| entry.count() + 1); + Some(set.put_entry(Entry::new( + &set.store().id(), + &key, + count, + &serde_json::Value::Object(written), + ))) + }); + if put.flatten() == Some(Put::Taken) { + registered(state).await; + } +} diff --git a/src/swarm/sync.rs b/src/swarm/sync.rs index 5e736a1..3939377 100644 --- a/src/swarm/sync.rs +++ b/src/swarm/sync.rs @@ -63,10 +63,10 @@ async fn deliver_sync_to_peer(state: &AppState, peer: &str) -> Result<()> { } } - // A set of events is passed on otherwise (`swarm::set`): of the Automerge document - // a node holds beside one, nothing. + // A set of events is passed on otherwise (`swarm::set`): with a peer that holds + // one, and of the Automerge document a node holds beside one alone, no sync. #[cfg(feature = "federation")] - if state.of_a_set() { + if state.of_a_set() || crate::swarm::set::holds_a_set(state, peer).await { return Ok(()); } |
