1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
//! Replaying a node's changes after the base into the new epoch.
//!
//! Changes from the old epoch can't be applied to the new document (their history is
//! unrelated), so they are replayed as state: for every path that changed between two
//! sets of heads, the new document gets whatever the old document's merged view holds
//! there now (or a delete if nothing). Replayed values are fresh operations, so they win
//! over what the snapshot holds at the same path.
//!
//! Granularity:
//! * Map entries are replayed per key, so changes to sibling keys made on different
//! nodes don't overwrite each other.
//! * Lists and text are replayed whole: concurrent edits of the same list or text on two
//! nodes around a compaction resolve as last-writer-wins.
//! * Edits inside a map that lost a merge conflict produce no patch in Automerge and are
//! not replayed. Such maps only exist briefly between concurrent creations.
//!
//! The protocol's own metadata (`_continuum/compactions`, `_continuum/epochs`) is never
//! replayed: the snapshot owns it.
use std::collections::{BTreeMap, HashSet};
use automerge::{ActorId, AutoCommit, ChangeHash, PatchAction, Prop};
use tracing::warn;
use super::snapshot::{COMPACTIONS, EPOCHS, META_ROOT};
use crate::store::{self, StoreError};
#[derive(Debug, Default, Clone, Copy)]
pub(crate) struct ReplayStats {
pub(crate) replayed: usize,
pub(crate) failed: usize,
}
/// Replays into `dst` everything that changed in `src` between `before` and `after`,
/// as one commit. Both head sets must be in `src`.
pub(crate) fn replay(
src: &mut AutoCommit,
before: &[ChangeHash],
after: &[ChangeHash],
dst: &mut AutoCommit,
) -> ReplayStats {
let mut stats = ReplayStats::default();
if before == after {
return stats;
}
for path in changed_paths(src, before, after) {
replay_path(src, dst, &path, &mut stats);
}
let _ = dst.commit();
stats
}
fn replay_path(src: &AutoCommit, dst: &mut AutoCommit, path: &[String], stats: &mut ReplayStats) {
if is_protocol_path(path) {
return;
}
// Replacing an ancestor of the protocol metadata as a whole would clobber it; replay
// its children one by one instead.
if contains_protocol_path(path) {
let mut keys = store::keys_at(src, path).unwrap_or_default();
keys.extend(store::keys_at(dst, path).unwrap_or_default());
keys.sort();
keys.dedup();
for key in keys {
let mut child = path.to_vec();
child.push(key);
replay_path(src, dst, &child, stats);
}
return;
}
match store::copy_path(src, dst, path) {
Ok(()) => stats.replayed += 1,
Err(error) => {
log_failure(path, &error);
stats.failed += 1;
}
}
}
fn log_failure(path: &[String], error: &StoreError) {
warn!(path = ?path, error = %error, "Could not replay change into the new epoch");
}
fn is_protocol_path(path: &[String]) -> bool {
matches!(path, [root, part, ..] if root == META_ROOT && (part == COMPACTIONS || part == EPOCHS))
}
/// True for the root and for `_continuum` itself.
fn contains_protocol_path(path: &[String]) -> bool {
matches!(path, [] | [_]) && path.first().is_none_or(|root| root == META_ROOT)
}
/// What a late node has that a snapshot lacks.
#[derive(Debug)]
pub(crate) struct Missing {
/// Heads of everything except `own` changes the snapshot lacks (and changes built on
/// them). Replaying from these heads to the document's heads replays exactly those.
pub(crate) before: Vec<ChangeHash>,
/// Changes by `own` that the snapshot lacks.
pub(crate) own: usize,
}
/// Splits `doc`'s history against a snapshot `clock`: a change (actor, seq) is in the
/// snapshot iff seq <= clock[actor].
///
/// Only changes this node wrote itself (actor `own`) are worth replaying: other nodes'
/// changes after the base were replayed by those nodes when they switched, and replaying
/// them again would overwrite newer writes in the new epoch with stale ones.
pub(crate) fn missing_from_snapshot(
doc: &mut AutoCommit,
clock: &BTreeMap<String, u64>,
own: &ActorId,
) -> Missing {
let own_hex = own.to_string();
let changes: Vec<(ChangeHash, Vec<ChangeHash>, bool)> = doc
.get_changes(&[])
.iter()
.map(|change| {
let actor = change.actor_id().to_string();
let in_snapshot = clock.get(&actor).is_some_and(|&seq| change.seq() <= seq);
let own_missing = actor == own_hex && !in_snapshot;
(change.hash(), change.deps().to_vec(), own_missing)
})
.collect();
// Excluded: own missing changes and everything that (transitively) depends on them.
// A fixpoint, so it doesn't rely on the order `get_changes` returns.
let mut excluded: HashSet<ChangeHash> = changes
.iter()
.filter(|(_, _, own_missing)| *own_missing)
.map(|(hash, _, _)| *hash)
.collect();
let own_count = excluded.len();
loop {
let before = excluded.len();
for (hash, deps, _) in &changes {
if !excluded.contains(hash) && deps.iter().any(|dep| excluded.contains(dep)) {
excluded.insert(*hash);
}
}
if excluded.len() == before {
break;
}
}
// Heads of the kept part: kept changes no kept change depends on.
let kept: Vec<&(ChangeHash, Vec<ChangeHash>, bool)> = changes
.iter()
.filter(|(hash, _, _)| !excluded.contains(hash))
.collect();
let depended_on: HashSet<ChangeHash> = kept
.iter()
.flat_map(|(_, deps, _)| deps.iter().copied())
.collect();
let mut before: Vec<ChangeHash> = kept
.iter()
.map(|(hash, _, _)| *hash)
.filter(|hash| !depended_on.contains(hash))
.collect();
before.sort();
Missing {
before,
own: own_count,
}
}
/// The minimal set of store paths covering every change between `before` and `after`:
/// sorted, and no path is a prefix of another.
pub(crate) fn changed_paths(
doc: &mut AutoCommit,
before: &[ChangeHash],
after: &[ChangeHash],
) -> Vec<Vec<String>> {
let mut paths: Vec<Vec<String>> = doc.diff(before, after).iter().map(patch_path).collect();
paths.sort();
paths.dedup();
// Sorted order puts a prefix right before its extensions, so comparing with the
// last kept path is enough.
let mut minimal: Vec<Vec<String>> = Vec::new();
for path in paths {
if minimal.last().is_some_and(|kept| path.starts_with(kept)) {
continue;
}
minimal.push(path);
}
minimal
}
fn patch_path(patch: &automerge::Patch) -> Vec<String> {
let mut path = Vec::new();
for (_, prop) in &patch.path {
match prop {
Prop::Map(key) => path.push(key.clone()),
// Inside a list: the list is replayed as a whole.
Prop::Seq(_) => return path,
}
}
match &patch.action {
PatchAction::PutMap { key, .. } | PatchAction::DeleteMap { key, .. } => {
path.push(key.clone())
}
PatchAction::Increment {
prop: Prop::Map(key),
..
} => path.push(key.clone()),
// List and text edits: the containing object.
_ => {}
}
path
}
#[cfg(test)]
mod tests {
use super::*;
use automerge::{transaction::Transactable, ObjType, ReadDoc, ScalarValue, ROOT};
use serde_json::json;
fn view(doc: &AutoCommit) -> serde_json::Value {
store::read_path(doc, &[]).unwrap().unwrap()
}
#[test]
fn minimal_paths() {
let mut doc = AutoCommit::new();
doc.put(ROOT, "untouched", 1_i64).unwrap();
let before = doc.get_heads();
let m = doc.put_object(ROOT, "m", ObjType::Map).unwrap();
doc.put(&m, "a", 1_i64).unwrap();
doc.put(ROOT, "x", 1_i64).unwrap();
doc.put(ROOT, "c", ScalarValue::counter(0)).unwrap();
doc.commit();
let middle = doc.get_heads();
doc.increment(ROOT, "c", 2).unwrap();
let list = doc.put_object(ROOT, "l", ObjType::List).unwrap();
doc.insert(&list, 0, "e").unwrap();
doc.commit();
let after = doc.get_heads();
let all = changed_paths(&mut doc, &before, &after);
assert_eq!(
all,
vec![
vec!["c".to_owned()],
vec!["l".to_owned()],
vec!["m".to_owned()],
vec!["x".to_owned()],
]
);
let later = changed_paths(&mut doc, &middle, &after);
assert_eq!(later, vec![vec!["c".to_owned()], vec!["l".to_owned()]]);
}
#[test]
fn replays_state_per_key() {
// Base shared by the old document and the snapshot.
let mut old = AutoCommit::new();
let m = old.put_object(ROOT, "m", ObjType::Map).unwrap();
old.put(&m, "keep", "base").unwrap();
old.put(&m, "change", "base").unwrap();
old.put(ROOT, "gone", true).unwrap();
old.commit();
let base = old.get_heads();
let mut new = AutoCommit::new();
store::materialize_into(&old, &mut new, &|_| false).unwrap();
// Something the new epoch already holds that the delta must not clobber.
let Some((_, m_new)) = new.get(ROOT, "m").unwrap() else {
panic!("m missing");
};
new.put(&m_new, "keep", "new epoch").unwrap();
new.commit();
// The delta: one key changed, one key deleted.
old.put(&m, "change", "delta").unwrap();
old.delete(ROOT, "gone").unwrap();
old.commit();
let after = old.get_heads();
let stats = replay(&mut old, &base, &after, &mut new);
assert_eq!(stats.failed, 0);
assert_eq!(
view(&new),
json!({ "m": { "keep": "new epoch", "change": "delta" } })
);
}
#[test]
fn missing_counts_only_own_changes() {
let own = ActorId::from(&b"own-actor"[..]);
let other = ActorId::from(&b"other-actor"[..]);
let mut doc = AutoCommit::new().with_actor(own.clone());
doc.put(ROOT, "base", 1_i64).unwrap();
doc.commit();
let base = doc.get_heads();
// The snapshot contains the base only.
let clock: BTreeMap<String, u64> = [(own.to_string(), 1)].into_iter().collect();
// A foreign change after the base (replayed by its own node) ...
let mut foreign = doc.fork().with_actor(other);
foreign.put(ROOT, "theirs", 1_i64).unwrap();
foreign.commit();
doc.merge(&mut foreign).unwrap();
let with_foreign = doc.get_heads();
// ... and one of ours.
doc.put(ROOT, "mine", 1_i64).unwrap();
doc.commit();
let after = doc.get_heads();
let missing = missing_from_snapshot(&mut doc, &clock, &own);
assert_eq!(missing.own, 1);
assert_eq!(missing.before, with_foreign);
assert_ne!(missing.before, base);
assert_eq!(
changed_paths(&mut doc, &missing.before, &after),
vec![vec!["mine".to_owned()]]
);
// Nothing of ours missing: nothing to replay.
let full: BTreeMap<String, u64> = [(own.to_string(), 2)].into_iter().collect();
let none = missing_from_snapshot(&mut doc, &full, &own);
assert_eq!(none.own, 0);
let mut heads = after.clone();
heads.sort();
assert_eq!(none.before, heads);
}
#[test]
fn protocol_metadata_is_never_replayed() {
let mut old = AutoCommit::new();
old.put(ROOT, "x", 1_i64).unwrap();
old.commit();
let base = old.get_heads();
let mut new = AutoCommit::new();
store::write_path(
&mut new,
&[META_ROOT.into(), EPOCHS.into(), "1".into()],
&json!({ "generation": 1 }),
)
.unwrap();
new.commit();
// The old epoch replaces `_continuum` wholesale and writes a compaction record.
store::write_path(
&mut old,
&[META_ROOT.into()],
&json!({ "forgotten": { "pk": 1 } }),
)
.unwrap();
store::write_path(
&mut old,
&[META_ROOT.into(), COMPACTIONS.into(), "c".into()],
&json!({ "x": 1 }),
)
.unwrap();
old.commit();
let after = old.get_heads();
replay(&mut old, &base, &after, &mut new);
let meta = &view(&new)[META_ROOT];
assert_eq!(meta[EPOCHS]["1"]["generation"], json!(1));
assert_eq!(meta["forgotten"]["pk"], json!(1));
assert!(meta.get(COMPACTIONS).is_none());
}
}
|