diff --git a/document/graph-storage/src/document.rs b/document/graph-storage/src/document.rs index e20024b0c2..91cf04e06f 100644 --- a/document/graph-storage/src/document.rs +++ b/document/graph-storage/src/document.rs @@ -42,6 +42,9 @@ pub struct Document { pub(crate) next_node_counter: u64, /// Counts this peer's own hot ops, so each carries its position in a gap-free run. See [`HotOp::sequence`]. pub(crate) next_hot_sequence: u64, + /// Set while the registries are still folded from an older history, so a failed refold is retried + /// rather than leaving derived state behind the history it comes from. + pub(crate) refold_owed: bool, } impl Document { @@ -93,9 +96,11 @@ impl Document { self.apply_op_with(target, delta.kind, delta.timestamp, ApplyMode::Force) } - /// Apply a live broadcast op. Updates the registry via LWW and appends to the hot log. - /// Doesn't touch history or `head`, since hot ops are transient. - pub fn apply_hot_op(&mut self, hot_op: HotOp) -> Result<(), CrdtError> { + /// Apply a locally staged op. Updates the registry via LWW and appends to the hot log; doesn't touch + /// history or `head`, since hot ops are transient. Crate-private: it skips the retirement check that + /// an op off the wire needs, so outside callers go through [`Session::apply_hot_op`], while local + /// staging must not have a covered sequence silently drop its own work. + pub(crate) fn apply_hot_op(&mut self, hot_op: HotOp) -> Result<(), CrdtError> { self.apply_op(hot_op.op.clone(), hot_op.timestamp)?; self.hot_log.push(hot_op); Ok(()) diff --git a/document/graph-storage/src/session.rs b/document/graph-storage/src/session.rs index dd275bff95..11ab40a9f4 100644 --- a/document/graph-storage/src/session.rs +++ b/document/graph-storage/src/session.rs @@ -47,6 +47,7 @@ impl Session { last_broadcast_rev: None, next_node_counter: 0, next_hot_sequence: 0, + refold_owed: false, }, remote_tips: HashMap::new(), runtime_base: None, @@ -258,6 +259,7 @@ impl Session { last_broadcast_rev: None, next_node_counter, next_hot_sequence: 0, + refold_owed: false, }, remote_tips: HashMap::new(), runtime_base: None, @@ -345,6 +347,9 @@ impl Session { /// Rebuild both registries from canonical history, then re-layer the hot tail. A full replay, so /// callers check it is needed first. fn refold_registries(&mut self) -> Result<(), CrdtError> { + // Cleared only on the way out: the deltas are in history either way, so a failure here has to be + // retried rather than leaving the registries derived from an older history for good. + self.document.refold_owed = true; self.document.retired_snapshot = self.snapshot_from_history()?; // One left unapplicable is kept, not dropped: a later delta or hot op can still supply its referent. @@ -357,10 +362,12 @@ impl Session { } } - match failure { - Some(error) => Err(error), - None => Ok(()), + if let Some(error) = failure { + return Err(error); } + + self.document.refold_owed = false; + Ok(()) } /// Integrate `incoming` retired deltas (in causal order) from another peer. Moves `head` forward @@ -385,6 +392,12 @@ impl Session { self.document.history.push(delta); } if absorbed_ids.is_empty() { + // Nothing new, but a refold left owed by an earlier failure is still the only way the registries + // catch up with the history they are derived from. + if self.document.refold_owed { + self.refold_registries()?; + } + return Ok(MergeOutcome::NoOp); } self.document.history.canonical_sort(); @@ -413,7 +426,7 @@ impl Session { self.document.head = outcome.head(); // After `head`, since the fold is over its ancestry. - if !folded_in_canonical_order { + if !folded_in_canonical_order || self.document.refold_owed { self.refold_registries()?; } diff --git a/document/graph-storage/src/tests/crdt.rs b/document/graph-storage/src/tests/crdt.rs index bf31dfcb0f..e82686609f 100644 --- a/document/graph-storage/src/tests/crdt.rs +++ b/document/graph-storage/src/tests/crdt.rs @@ -1108,3 +1108,25 @@ fn snapshot_from_history_ignores_undone_deltas() { assert!(folded.attributes.contains_key("first"), "history still holds the kept interaction"); assert!(!folded.attributes.contains_key("second"), "a fold restored an undone delta"); } + +/// A refold that fails leaves the registries derived from an older history, and the deltas are already +/// in history so re-merging them absorbs nothing. The owed refold has to be retried on the next merge +/// or the registries never catch up. +#[test] +fn an_owed_refold_is_retried_by_a_merge_that_absorbs_nothing() { + let mut session = Session::with_peer(PeerId(1)); + commit_retired(&mut session, set_document_attribute("first", 1)); + + // Stand in for a refold that failed partway: history moved on, the snapshot did not. + session.document.retired_snapshot = crate::Registry::default(); + session.document.refold_owed = true; + + let outcome = session.merge(Vec::new()).expect("a merge absorbing nothing still settles the refold"); + + assert_eq!(outcome, crate::MergeOutcome::NoOp); + assert!(!session.document.refold_owed, "the refold must not stay owed"); + assert!( + session.retired_registry().attributes.contains_key("first"), + "the retried refold must bring the snapshot back to its history" + ); +}