diff options
| author | tv <tv@krebsco.de> | 2026-11-17 03:22:31 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-17 04:40:13 +0100 |
| commit | d5cc9482dd527ef9beb82671f6f35832b3280d57 (patch) | |
| tree | c3190c9496734f0cc32e28a41a4c079d668167cd /src | |
| parent | d6194cdc78b69914810cfc02bbd31aef41067be4 (diff) | |
held: a store starts from the checkpoint its document holds
A remake's snapshot holds the store's checkpoint at `_continuum/checkpoint`
where it isn't the root (`docs/store.md`, What the document holds), and
none of what came before it. A node reading its store anew from such a
document starts there, with what the checkpoint keeps; one pinning a
checkpoint finds it there too, where it can't build it from the events.
Diffstat (limited to 'src')
| -rw-r--r-- | src/document/events.rs | 16 | ||||
| -rw-r--r-- | src/document/meta.rs | 2 | ||||
| -rw-r--r-- | src/federation/held.rs | 99 |
3 files changed, 98 insertions, 19 deletions
diff --git a/src/document/events.rs b/src/document/events.rs index 95e99c9..1079999 100644 --- a/src/document/events.rs +++ b/src/document/events.rs @@ -163,6 +163,22 @@ pub(crate) fn started(fold: &str, root: &[u8], kept: &[Vec<u8>]) -> anyhow::Resu Ok(doc.with_actor(ActorId::random())) } +/// The checkpoint `doc` holds at `_continuum/checkpoint`, where a remake's snapshot holds +/// one other than the root (`snapshot`). +pub(crate) fn checkpoint(doc: &impl ReadDoc) -> Option<Vec<u8>> { + let continuum = match doc.get(automerge::ROOT, meta::ROOT) { + Ok(Some((Value::Object(ObjType::Map), id))) => id, + _ => return None, + }; + match doc.get(&continuum, meta::CHECKPOINT) { + Ok(Some((Value::Scalar(value), _))) => match value.as_ref() { + ScalarValue::Bytes(bytes) => Some(bytes.clone()), + _ => None, + }, + _ => None, + } +} + /// The ID of the store `doc` holds, its swarm's, where it holds one. pub(crate) fn swarm(doc: &impl ReadDoc) -> Option<Id> { match store::read_plain(doc, &meta::path(&[meta::SWARM])) { diff --git a/src/document/meta.rs b/src/document/meta.rs index 7a9a489..af7aac7 100644 --- a/src/document/meta.rs +++ b/src/document/meta.rs @@ -48,6 +48,8 @@ pub(crate) const SESSIONS: &str = "sessions"; pub(crate) const LOCAL: &str = "local"; #[cfg(feature = "federation")] pub(crate) const EVENTS: &str = "events"; +#[cfg(feature = "federation")] +pub(crate) const CHECKPOINT: &str = "checkpoint"; /// The store path `_continuum/<parts…>`. pub(crate) fn path(parts: &[&str]) -> Vec<String> { diff --git a/src/federation/held.rs b/src/federation/held.rs index 5595a08..ce97788 100644 --- a/src/federation/held.rs +++ b/src/federation/held.rs @@ -134,8 +134,10 @@ struct Inner { /// What a document holds that its store wasn't offered from it. pub(crate) struct Unread { epoch_seq: u32, - /// The root, where the store isn't started yet. + /// The root, and the checkpoint a remake's snapshot holds, if any, where the store + /// isn't started yet. root: Option<Vec<u8>>, + checkpoint: Option<Vec<u8>>, events: BTreeMap<Id, Vec<u8>>, } @@ -204,19 +206,22 @@ impl Holding { .filter(|id| !same || !inner.read.contains(id)) .filter_map(|id| Some((id, events::bytes(doc, &id)?))) .collect(); - let root = match &inner.store { - None => Some(events::bytes(doc, &swarm)?), - Some(_) => None, + let (root, checkpoint) = match &inner.store { + None => (Some(events::bytes(doc, &swarm)?), events::checkpoint(doc)), + Some(_) => (None, None), }; Some(Unread { epoch_seq, root, + checkpoint, events, }) } - /// Offers the store what was `unread`: started from the root first, by what the node - /// kept of its own. What it refuses of its own from then on, it keeps. + /// Offers the store what was `unread`: started first, by what the node kept of its + /// own, from the checkpoint its operator pinned, found in the document; else from the + /// document's checkpoint, where a remake's snapshot holds one; else from the root. + /// What it refuses of its own from then on, it keeps. pub(crate) fn read(&self, unread: Unread) { let mut inner = self.inner.lock_or_recover(); let inner = &mut *inner; @@ -230,20 +235,38 @@ impl Holding { }; let id = Id::of(&root); let (own, dropped) = inner.kept.ids().unwrap_or_default(); + // What a checkpoint keeps follows it in the document: a fork's root's too. + let kept = |checkpoint: &[u8]| -> Option<Vec<Vec<u8>>> { + Federation::parts(checkpoint) + .map_or_else(Vec::new, |(_, keeps)| keeps) + .iter() + .map(|keep| unread.events.get(keep).cloned()) + .collect() + }; let started = match self.pin { - // A fork's root keeps events, which follow it in the document. - None => { - let keeps = Federation::parts(&root).map_or_else(Vec::new, |(_, keeps)| keeps); - let kept: Option<Vec<Vec<u8>>> = keeps - .iter() - .map(|keep| unread.events.get(keep).cloned()) - .collect(); - let Some(kept) = kept else { - return; - }; - Store::new(root, kept) - } - Some(pin) => match found(&pin, &root, &unread.events) { + None => match &unread.checkpoint { + Some(checkpoint) => { + let Some(kept) = kept(checkpoint) else { + return; + }; + Store::pinned(id, checkpoint.clone(), kept) + } + None => { + let Some(kept) = kept(&root) else { + return; + }; + Store::new(root, kept) + } + }, + Some(pin) => match found(&pin, &root, &unread.events).or_else(|| { + let checkpoint = unread + .checkpoint + .clone() + .filter(|checkpoint| Id::of(checkpoint) == pin)?; + let kept = kept(&checkpoint)?; + let kept = kept.into_iter().map(|bytes| (Id::of(&bytes), bytes)); + Some((checkpoint, kept.collect())) + }) { Some((checkpoint, kept)) => { info!(store = %id, checkpoint = %pin, "Found the pinned checkpoint in the document"); Store::pinned(id, checkpoint, kept.into_values().collect()) @@ -918,6 +941,44 @@ mod tests { let elsewhere = Holding::open(None, files, Some(Id::of(b"elsewhere")), None).unwrap(); read(&elsewhere, &doc, 0); assert!(elsewhere.with(|_| ()).is_none()); + + // A document holding that checkpoint, as a remake's snapshot does, with what it + // keeps and what came after it, and none of what came before: a node starts there, + // and so does one pinning it. + let (checkpoint, after) = from_root + .with(|store| { + let mut after: BTreeMap<Id, Vec<u8>> = store + .kept() + .into_iter() + .map(|bytes| (Id::of(&bytes), bytes)) + .collect(); + for id in store.taken() { + after.insert(*id, store.get(id).unwrap().to_vec()); + } + (store.checkpoint().to_vec(), after) + }) + .unwrap(); + let mut snapshot = events::started(Federation::NAME, &root, &[]).unwrap(); + let mut write = snapshot.transaction(); + store::write_val_path( + &mut write, + &meta::path(&[meta::CHECKPOINT]), + &crate::document::typed::Val::Scalar(ScalarValue::Bytes(checkpoint)), + store::Writing::sticky(Default::default()), + ) + .unwrap(); + write.commit(); + for event in after.values() { + put(&mut snapshot, event); + } + for holding in [ + Holding::default(), + Holding::open(None, Arc::new(Files::default()), Some(pin), None).unwrap(), + ] { + read(&holding, &snapshot, 0); + assert_eq!(holding.with(|store| Id::of(store.checkpoint())), Some(pin)); + assert_eq!(taken(&holding), taken(&from_root)); + } } /// An event put at the API is offered to the store; what the operator drops is |
