diff options
Diffstat (limited to 'src/federation')
| -rw-r--r-- | src/federation/held.rs | 78 |
1 files changed, 64 insertions, 14 deletions
diff --git a/src/federation/held.rs b/src/federation/held.rs index 154544f..c5527cb 100644 --- a/src/federation/held.rs +++ b/src/federation/held.rs @@ -120,6 +120,9 @@ pub(crate) struct Holding { /// Whether the node holds the store in the document a swarm syncs otherwise too /// (`of_both`): a node of both kinds, by which a running swarm moves to sets. both: bool, + /// For how long a set made of the store read from the document keeps what a + /// checkpoint forgot (`of_both`). + keeps_forgotten: Duration, // A std mutex: never held while waiting for the document. inner: Mutex<Inner>, } @@ -188,6 +191,9 @@ struct Inner { /// A set of events to be, where the node started with nothing of its store and /// gathers what its set begins from of its peers (`store::passing::Start`). starting: Option<super::store::passing::Start<Federation>>, + /// What the set took of the document, to be passed on to the peers that hold sets + /// (`unpassed`): a node of both kinds takes into each what the other holds. + unpassed: Vec<Id>, /// The document read from (`Replica::epoch_seq`), and its events read. epoch_seq: Option<u32>, read: HashSet<Id>, @@ -282,6 +288,7 @@ impl Holding { voter: None, of_a_set: false, both: false, + keeps_forgotten: Duration::ZERO, document: None, written: Mutex::default(), inner: Mutex::new(Inner { @@ -387,9 +394,13 @@ impl Holding { match started { Ok(store) => { info!(store = %id, refused = own.len(), dropped = dropped.len(), "Reading the store from the document"); - inner.store = Some(Carried::Store(Box::new( - store.refusing(&own, &dropped, &writes), - ))); + let store = store.refusing(&own, &dropped, &writes); + // A node of both kinds: the store in a set too, from here on. + inner.store = Some(if self.of_a_set { + Carried::Set(Box::new(Set::new(store, self.keeps_forgotten))) + } else { + Carried::Store(Box::new(store)) + }); } Err(why) => { if inner.none.replace(id) != Some(id) { @@ -404,7 +415,13 @@ impl Holding { for (id, bytes) in unread.events { inner.read.insert(id); match store.offer(bytes) { - Offered::Taken(ids) => taken += ids.len(), + Offered::Taken(ids) => { + taken += ids.len(); + // To pass on, where a set carries the store too (`unpassed`). + if self.of_a_set { + inner.unpassed.extend(ids); + } + } Offered::Refused(why) => { refused += 1; info!(event = %id, why = ?why, "The store refuses an event the document holds"); @@ -558,8 +575,8 @@ impl Holding { /// what its store's checkpoint keeps, then what it took after it, then what it holds /// back, stopped (`Store::held_back`). pub(crate) fn lacking(&self, doc: &impl ReadDoc) -> Vec<(Id, Vec<u8>)> { - // A set of events is the store's own document: it lacks none of it. - if self.of_a_set { + // A set of events alone is the store's own document: it lacks none of it. + if self.of_a_set && !self.both { return Vec::new(); } let mut held: HashSet<Id> = events::ids(doc).into_iter().collect(); @@ -1062,6 +1079,11 @@ impl Holding { self } + /// What the set took of the document since last asked, in the order taken. + pub(crate) fn unpassed(&self) -> Vec<Id> { + std::mem::take(&mut self.inner.lock_or_recover().unpassed) + } + /// The names a node that started with nothing asks a peer for, to begin its set /// from; none, once it holds one. pub(crate) fn start_wants(&self) -> Vec<crate::document::ranges::Name> { @@ -1098,7 +1120,7 @@ impl Holding { } /// Itself, its store carried by the set `begun`, held or to be, and kept in the - /// file at `path`; itself, where the node is given no set. + /// file at `path`; itself, where the node is given no set and holds none. pub(crate) fn carrying_if( self, begun: Option<Begun>, @@ -1108,11 +1130,13 @@ impl Holding { let holding = match begun { Some(Begun::Set(set)) => self.carrying(*set), Some(Begun::Start(start)) => self.awaiting(start), - None => return self, + None => self, }; match path { - Some(path) => holding.kept_in(path, files), - None => holding, + // A set held, to be gathered, or to be made of the store read from the + // document (`of_both`). + Some(path) if holding.of_a_set => holding.kept_in(path, files), + _ => holding, } } @@ -1162,9 +1186,18 @@ impl Holding { self.of_a_set } - /// Itself, holding its store in the Automerge document too, beside the set. - pub(crate) fn of_both(mut self) -> Self { + /// [`of_both`](Self::of_both), where `both`. + pub(crate) fn of_both_if(self, both: bool, keeps: Duration) -> Self { + if both { self.of_both(keeps) } else { self } + } + + /// Itself, holding its store in the Automerge document and in a set of events: the + /// set it is given (`carrying`), or, given none, one made of the store as it is + /// first read from the document, keeping what a checkpoint forgot for `keeps`. + pub(crate) fn of_both(mut self, keeps: Duration) -> Self { self.both = true; + self.of_a_set = true; + self.keeps_forgotten = keeps; self } @@ -1369,6 +1402,16 @@ pub(crate) async fn run_worker(state: crate::state::AppState) { error!(error = %error, "Reading the store from the document failed"); (false, false, false, Vec::new(), Vec::new()) }); + // What the set took of the document goes on to the peers that hold sets, and + // the node's entry in the document is its entry in the set: a node of both + // kinds takes into each what the other holds. + if state.holding.has_a_set() && !state.of_a_set() { + let unpassed = state.holding.unpassed(); + if !unpassed.is_empty() { + crate::swarm::set::wrote(&state, unpassed).await; + } + crate::swarm::set::mirror_entry(&state).await; + } for bytes in signed { if let Err(error) = write(&state, None, bytes).await { warn!(error = %error, "Couldn't put what it signed for its party: put again later"); @@ -1455,7 +1498,7 @@ pub(crate) async fn run_worker(state: crate::state::AppState) { _ = looks.tick() => { look = true; // A set of events to its file, where it changed (`Holding::save`). - if state.of_a_set() { + if state.holding.has_a_set() { let holding = Arc::clone(&state.holding); if let Ok(Err(error)) = tokio::task::spawn_blocking(move || holding.save()).await @@ -1525,7 +1568,14 @@ pub(crate) async fn write( }, ) .await - .map(|_| ()) + .map(|_| ())?; + // A node of both kinds: on to the peers that hold sets, too (`swarm::set`). + if state.holding.has_a_set() + && state.holding.with(|store| store.get(&id).is_some()) == Some(true) + { + crate::swarm::set::wrote(state, vec![id]).await; + } + Ok(()) } async fn put_again(state: &crate::state::AppState) { |
