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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
|
//! Catching up with a newer epoch: the node rejoins.
//!
//! Peers on another epoch don't sync, so a node that missed a switch (new, late,
//! returning, or one whose own build failed) can't follow through ordinary sync. Once
//! an active peer advertises a newer generation, the node
//!
//! 1. stops serving requests and keeps its document aside;
//! 2. switches to an empty document labelled with the newer epoch: an empty history
//! conflicts with nothing, so ordinary sync fills it from peers on that epoch,
//! exactly like a new node joining;
//! 3. once caught up with a peer, checks that the history starts with the epoch's
//! snapshot change;
//! 4. decides about its own changes the cluster never saw (none, for a new node):
//! * the metadata of the epoch that followed the node's own records which changes
//! went into it (per-actor clock); the rest are this node's missing changes;
//! * within that epoch's replay window they are replayed (state per path, as in a
//! switch), otherwise they are quarantined for an operator
//! (`/admin/quarantine`), as are changes that can't be told apart;
//! 5. archives the old document and serves requests again.
//!
//! If no peer completes a sync in time, or the check fails, the node restores its old
//! document and epoch and tries again later.
use std::collections::BTreeMap;
use std::convert::Infallible;
use std::time::Duration;
use automerge::{AutoCommit, ChangeHash};
use tokio::time::Instant;
use tracing::{error, info, warn};
use uuid::Uuid;
use super::delta::{self, ReplayStats};
use super::local::{CatchUp, LocalStatus, Quarantined, Rejoin};
use super::records::unix_millis;
use super::snapshot::{self, EpochMetadata};
use super::Host;
use crate::replica::Epoch;
/// Pause before trying the same target again after a failed rejoin.
const RETRY: Duration = Duration::from_secs(30);
/// Lower bound for how long a rejoin may take to complete a sync.
const MIN_REJOIN_TIMEOUT: Duration = Duration::from_secs(60);
/// Called by the switch worker when there is nothing to switch to locally, and on every
/// tick while a rejoin is in progress.
pub(super) async fn check<H: Host>(host: &H) {
if let Some((target, started)) = host.compaction().rejoin_progress() {
let timeout = host.dormant_after().max(MIN_REJOIN_TIMEOUT);
if host.initial_sync_done() {
finish(host).await;
} else if started.elapsed() > timeout {
if let Some(rejoin) = host.compaction().take_rejoin() {
let reason = format!(
"no peer on {target} completed a sync within {}s",
timeout.as_secs()
);
abandon(host, rejoin, reason).await;
}
}
return;
}
let local = host.current_epoch();
let mut ahead: Vec<(String, Epoch)> = host
.peer_summaries()
.await
.into_iter()
.filter(|peer| peer.active)
.filter_map(|peer| peer.epoch.map(|epoch| (peer.id, epoch)))
.filter(|(_, epoch)| epoch.generation > local.generation)
.collect();
ahead.sort_by(|a, b| a.0.cmp(&b.0));
let Some(generation) = ahead.iter().map(|(_, epoch)| epoch.generation).max() else {
if host.compaction().catch_up().is_some() {
host.compaction().set_catch_up(None);
}
return;
};
let mut targets: Vec<Epoch> = ahead
.iter()
.filter(|(_, epoch)| epoch.generation == generation)
.map(|(_, epoch)| *epoch)
.collect();
targets.sort_by_key(|epoch| epoch.id);
targets.dedup();
let [target] = targets.as_slice() else {
let listed: Vec<String> = targets.iter().map(ToString::to_string).collect();
warn!(epochs = ?listed, "Peers disagree about the newest epoch; not catching up");
return;
};
let target = *target;
// Back off between attempts for the same target.
let previous = host
.compaction()
.catch_up()
.filter(|catch_up| catch_up.target == target);
if previous
.as_ref()
.is_some_and(|catch_up| catch_up.last_attempt.elapsed() < RETRY)
{
return;
}
let attempts = previous.map_or(0, |catch_up| catch_up.attempts) + 1;
start(host, local, target, attempts).await;
}
async fn start<H: Host>(host: &H, local: Epoch, target: Epoch, attempts: u32) {
host.compaction().set_catch_up(Some(CatchUp {
target,
attempts,
last_attempt: Instant::now(),
error: None,
}));
let was_ready = host.suspend().await;
let old = match host
.switch_epoch(target, |_live| Ok::<_, Infallible>(AutoCommit::new()))
.await
{
Ok(old) => old,
Err(never) => match never {},
};
// After the swap: messages of the old epoch are dropped from now on, so nothing
// can mark the new, empty document as caught up by mistake. A stale "done" from
// just before is caught in `finish` (the snapshot change is missing then).
host.reset_initial_sync();
info!(from = %local, to = %target, attempt = attempts, "Rejoining newer epoch");
host.compaction().start_rejoin(Rejoin {
previous: local,
target,
old,
started: Instant::now(),
was_ready,
});
}
/// What to do with this node's own changes the new epoch lacks.
enum Plan {
Nothing,
Replay(Vec<Vec<String>>),
Quarantine {
paths: Vec<Vec<String>>,
reason: String,
},
}
enum Prepared {
/// Caught up with someone, but not with the target epoch yet: keep waiting.
NotYet(AutoCommit),
Failed(AutoCommit, String),
Ready {
old: AutoCommit,
metadata: EpochMetadata,
plan: Plan,
own_missing: usize,
had_history: bool,
},
}
async fn finish<H: Host>(host: &H) {
let Some(rejoin) = host.compaction().take_rejoin() else {
return;
};
let Rejoin {
previous,
target,
old,
started,
was_ready,
} = rejoin;
// Everything heavy runs on a copy, without a lock.
let copy = host.read_doc(|doc| doc.clone()).await;
let prepared = tokio::task::spawn_blocking(move || prepare(copy, old, previous, target)).await;
let (old, metadata, plan, own_missing, had_history) = match prepared {
Ok(Prepared::Ready {
old,
metadata,
plan,
own_missing,
had_history,
}) => (old, metadata, plan, own_missing, had_history),
Ok(Prepared::NotYet(old)) => {
host.reset_initial_sync();
host.compaction().start_rejoin(Rejoin {
previous,
target,
old,
started,
was_ready,
});
return;
}
Ok(Prepared::Failed(old, reason)) => {
let rejoin = Rejoin {
previous,
target,
old,
started,
was_ready,
};
return abandon(host, rejoin, reason).await;
}
Err(join_error) => {
// The old document is gone with the panicked task; nothing to restore.
error!(error = %join_error, "Rejoin task failed; staying on the new epoch");
if was_ready {
host.resume().await;
}
return;
}
};
let mut stats = ReplayStats::default();
let mut quarantine_id = None;
let mut keep_archive = true;
let mut old = Some(old);
match plan {
Plan::Nothing => {}
Plan::Replay(paths) => {
let document = old.take().expect("old document");
let (replayed, document) = host
.commit_doc(move |doc| {
let stats = delta::replay_paths(&document, doc, &paths);
Ok::<_, Infallible>((stats, document))
})
.await
.unwrap_or_else(|never| match never {});
stats = replayed;
old = Some(document);
}
Plan::Quarantine { paths, reason } => {
let id = Uuid::new_v4().to_string();
warn!(quarantine = %id, epoch = %previous, paths = paths.len(), reason = %reason, "Quarantined local changes");
host.compaction().add_quarantine(Quarantined {
id: id.clone(),
epoch: previous,
target,
created_at: unix_millis(),
reason,
paths,
document: old.take().expect("old document"),
});
quarantine_id = Some(id);
keep_archive = false; // the quarantine holds the document
}
}
if let (true, true, Some(document)) = (had_history, keep_archive, old) {
host.compaction().archive(
previous,
document,
metadata.policy.archive_retention.clone(),
target.generation,
);
}
host.compaction().set_status(
&metadata.compaction_id,
LocalStatus {
switched_at: Some(unix_millis()),
replayed_paths: Some(stats.replayed),
failed_paths: Some(stats.failed),
quarantine: quarantine_id.clone(),
error: None,
},
);
host.compaction().set_catch_up(None);
if was_ready {
host.resume().await;
}
info!(
from = %previous,
to = %target,
own_changes = own_missing,
replayed = stats.replayed,
failed = stats.failed,
quarantine = ?quarantine_id,
secs = started.elapsed().as_secs(),
"Rejoined newer epoch"
);
}
/// Verifies the synced document and decides about the old document's own changes.
fn prepare(mut copy: AutoCommit, mut old: AutoCommit, previous: Epoch, target: Epoch) -> Prepared {
if copy.get_change_by_hash(&ChangeHash(target.id)).is_none() {
return Prepared::NotYet(old);
}
let metadata = match snapshot::verify_document(&mut copy, target) {
Ok(metadata) => metadata,
Err(reason) => return Prepared::Failed(old, reason),
};
let old_heads = old.get_heads();
if old_heads.is_empty() {
return Prepared::Ready {
old,
metadata,
plan: Plan::Nothing,
own_missing: 0,
had_history: false,
};
}
// The epoch that followed ours records (per actor) which of our changes it took.
// Snapshots carry earlier epochs' metadata forward, so this works across any gap.
let next = snapshot::read_epoch_metadata(©, previous.generation + 1)
.ok()
.filter(|next| next.previous == previous);
let own = old.get_actor().clone();
let clock = next
.as_ref()
.map_or_else(BTreeMap::new, |next| next.clock.clone());
let missing = delta::missing_from_snapshot(&mut old, &clock, &own);
if missing.own == 0 {
return Prepared::Ready {
old,
metadata,
plan: Plan::Nothing,
own_missing: 0,
had_history: true,
};
}
let paths = delta::changed_paths(&mut old, &missing.before, &old_heads);
let gap = target.generation - previous.generation;
let plan = match &next {
None => Plan::Quarantine {
paths,
reason: format!(
"the metadata of the epoch after {previous} is missing, so it is unknown which of {} local changes the cluster has",
missing.own
),
},
Some(next) => {
let away_ms = unix_millis().saturating_sub(next.proposed_at);
if next.policy.replay_window.covers(gap, away_ms) {
Plan::Replay(paths)
} else {
Plan::Quarantine {
paths,
reason: format!(
"{} local changes are outside the replay window ({gap} epochs behind, cluster moved on {}s ago)",
missing.own,
away_ms / 1000
),
}
}
}
};
Prepared::Ready {
old,
metadata,
plan,
own_missing: missing.own,
had_history: true,
}
}
/// Restores the old document and epoch, and retries later.
async fn abandon<H: Host>(host: &H, rejoin: Rejoin, reason: String) {
let Rejoin {
previous,
target,
old,
was_ready,
..
} = rejoin;
warn!(target = %target, reason = %reason, "Rejoin failed; restoring the previous epoch");
if let Err(never) = host
.switch_epoch(previous, move |_live| Ok::<_, Infallible>(old))
.await
{
match never {}
}
if was_ready {
host.resume().await;
}
let attempts = host
.compaction()
.catch_up()
.filter(|catch_up| catch_up.target == target)
.map_or(1, |catch_up| catch_up.attempts);
host.compaction().set_catch_up(Some(CatchUp {
target,
attempts,
last_attempt: Instant::now(),
error: Some(reason),
}));
}
#[cfg(test)]
mod tests {
use super::*;
use crate::compaction::snapshot::{build, SnapshotParams};
use automerge::{transaction::Transactable, ActorId, ROOT};
use serde_json::{json, Value as JsonValue};
/// An old-epoch document with one own change after the base, and the cluster's
/// document of the next epoch (snapshot plus one foreign change).
fn scenario(policy: &JsonValue) -> (AutoCommit, AutoCommit, Epoch) {
let mut old = AutoCommit::new().with_actor(ActorId::from(&b"late-node"[..]));
old.put(ROOT, "base", 1_i64).unwrap();
old.commit();
let base = old.get_heads();
let snapshot = build(
&mut old,
&SnapshotParams {
compaction_id: "c-1",
previous: Epoch::GENESIS,
base_heads: &base,
policy,
proposed_at: 0,
},
)
.unwrap();
let target = snapshot.epoch;
let mut cluster = snapshot.doc.with_actor(ActorId::from(&b"cluster-node"[..]));
cluster.put(ROOT, "theirs", 1_i64).unwrap();
cluster.commit();
old.put(ROOT, "mine", 1_i64).unwrap();
old.commit();
(old, cluster, target)
}
#[test]
fn replays_within_the_window() {
let (old, cluster, target) = scenario(&json!({}));
match prepare(cluster, old, Epoch::GENESIS, target) {
Prepared::Ready {
plan: Plan::Replay(paths),
own_missing,
..
} => {
assert_eq!(own_missing, 1);
assert_eq!(paths, vec![vec!["mine".to_owned()]]);
}
_ => panic!("expected a replay"),
}
}
#[test]
fn quarantines_outside_the_window() {
let (old, cluster, target) = scenario(&json!({ "replayWindow": { "epochs": 0 } }));
match prepare(cluster, old, Epoch::GENESIS, target) {
Prepared::Ready {
plan: Plan::Quarantine { paths, .. },
..
} => assert_eq!(paths, vec![vec!["mine".to_owned()]]),
_ => panic!("expected a quarantine"),
}
}
#[test]
fn new_node_has_nothing_to_replay() {
let (_, cluster, target) = scenario(&json!({}));
assert!(matches!(
prepare(cluster, AutoCommit::new(), Epoch::GENESIS, target),
Prepared::Ready {
plan: Plan::Nothing,
had_history: false,
..
}
));
}
#[test]
fn waits_for_the_target_epoch() {
let (old, _, target) = scenario(&json!({}));
assert!(matches!(
prepare(AutoCommit::new(), old, Epoch::GENESIS, target),
Prepared::NotYet(_)
));
}
}
|