fusee 1.0.0

Embeddable durable execution at the speed of the disk: workflows whose steps are durable within microseconds of fdatasync
Documentation
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
//! Continuous backup end to end: workflows run while the backup ships the
//! log and checkpoints, the directory is deleted, and restores to several
//! points in time each hold exactly what was acknowledged by then, with
//! suspended runs resuming.

use fusee::backup::{self, BackupOptions, LocalTarget, RestoreOptions, Target};
use fusee::{Config, Ctx, Engine, Error};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Duration;

fn tmp(name: &str) -> PathBuf {
    let dir = std::env::temp_dir().join(format!(
        "fusee-backup-{name}-{}-{}",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_nanos()
    ));
    let _ = std::fs::remove_dir_all(&dir);
    dir
}

fn options() -> BackupOptions {
    BackupOptions {
        upload_interval: Some(Duration::from_millis(50)),
        resolution: Duration::from_millis(50),
        retention: Duration::from_secs(3600),
    }
}

fn cfg(dir: &Path) -> Config {
    let mut c = Config::local(dir)
        .shards(2)
        // Small segments: the log rolls, seals and is retired while the
        // backup runs; frequent checkpoints and parking exercise the store.
        .segment_bytes(256 << 10)
        .checkpoint_interval(Duration::from_millis(100))
        .park_after(Duration::from_millis(20));
    c.local.flush = fusee::FlushMethod::Fsync;
    c
}

fn backed_up(dir: &Path, target: &Arc<dyn Target>) -> Config {
    cfg(dir).backup(target.clone(), options())
}

/// Registers the test's workflow types.
fn register(engine: &Engine) {
    engine.register("work", |ctx: Ctx, n: u32| async move {
        let mut acc = 0u64;
        for i in 0..3u32 {
            let page: Vec<u8> = ctx
                .step(&format!("page-{i}"), move || async move {
                    Ok::<_, Error>(vec![(n % 251) as u8; 2048])
                })
                .await?;
            acc += page.len() as u64;
        }
        Ok(u64::from(n) * 2 + acc)
    });
    engine.register("waiter", |ctx: Ctx, base: u32| async move {
        let v: u32 = ctx.wait_for("go").await?.unwrap_or(0);
        Ok(base + v)
    });
}

fn expected(n: u32) -> u64 {
    u64::from(n) * 2 + 3 * 2048
}

async fn phase(engine: &Engine, prefix: &str, count: u32) {
    for i in 0..count {
        engine
            .start_raw(
                "work",
                &format!("{prefix}-{i}"),
                serde_json::to_vec(&i).unwrap().into(),
                Default::default(),
            )
            .await
            .unwrap();
    }
    for i in 0..count {
        let id = format!("{prefix}-{i}");
        assert_eq!(engine.run::<u64>(&id).result().await.unwrap(), expected(i));
    }
}

async fn start_waiter(engine: &Engine, id: &str, base: u32) {
    engine
        .start_raw(
            "waiter",
            id,
            serde_json::to_vec(&base).unwrap().into(),
            Default::default(),
        )
        .await
        .unwrap();
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn restores_each_point_in_time_with_exactly_what_was_acknowledged() {
    let dir = tmp("dir");
    let store = tmp("target");
    let target: Arc<dyn Target> = Arc::new(LocalTarget::new(&store).unwrap());
    let engine = Engine::open(backed_up(&dir, &target)).await.unwrap();
    register(&engine);
    let b = engine.backup().expect("backup");

    phase(&engine, "a", 40).await;
    start_waiter(&engine, "wa", 100).await;
    let p1 = b.sync().await.unwrap();
    tokio::time::sleep(Duration::from_millis(30)).await;
    phase(&engine, "b", 40).await;
    start_waiter(&engine, "wb", 200).await;
    let p2 = b.sync().await.unwrap();
    tokio::time::sleep(Duration::from_millis(30)).await;
    phase(&engine, "c", 40).await;
    let p3 = b.sync().await.unwrap();
    assert!(
        p1.time_ms < p2.time_ms && p2.time_ms < p3.time_ms,
        "{p1:?} {p2:?} {p3:?}"
    );
    let status = b.status().unwrap();
    assert!(
        status.objects_uploaded > 0 && status.manifest_writes > 0,
        "{status:?}"
    );
    engine.shutdown().await.unwrap();
    // The disk is gone.
    std::fs::remove_dir_all(&dir).unwrap();

    let checked = backup::verify(&*target).unwrap();
    assert!(
        checked.missing.is_empty() && checked.damaged.is_empty(),
        "{checked:?}"
    );

    // (restore at, prefixes present, waiters present)
    let cases: Vec<(Option<u64>, Vec<&str>, Vec<(&str, u32)>, Option<u64>)> = vec![
        (Some(p1.time_ms), vec!["a"], vec![("wa", 100)], Some(p1.seq)),
        // After p1 and before the next phase began: still p1.
        (
            Some(p1.time_ms + 10),
            vec!["a"],
            vec![("wa", 100)],
            Some(p1.seq),
        ),
        (
            Some(p2.time_ms),
            vec!["a", "b"],
            vec![("wa", 100), ("wb", 200)],
            Some(p2.seq),
        ),
        (
            Some(p3.time_ms),
            vec!["a", "b", "c"],
            vec![("wa", 100), ("wb", 200)],
            None,
        ),
        (
            None,
            vec!["a", "b", "c"],
            vec![("wa", 100), ("wb", 200)],
            None,
        ),
    ];
    for (k, (at, present, waiters, seq)) in cases.into_iter().enumerate() {
        let out = tmp(&format!("restored-{k}"));
        let r = backup::restore(
            &*target,
            &out,
            &RestoreOptions {
                at_ms: at,
                timeline: None,
            },
        )
        .unwrap();
        if let Some(seq) = seq {
            assert_eq!(r.seq, seq, "case {k}: {r:?}");
        }
        let engine = Engine::open(cfg(&out)).await.unwrap();
        register(&engine);
        for prefix in ["a", "b", "c"] {
            for i in 0..40 {
                let id = format!("{prefix}-{i}");
                let status = engine.status(&id).await.unwrap();
                if present.contains(&prefix) {
                    assert_eq!(
                        engine.run::<u64>(&id).result().await.unwrap(),
                        expected(i),
                        "case {k}: {id}"
                    );
                } else {
                    assert!(
                        status.is_none(),
                        "case {k}: {id} should not exist: {status:?}"
                    );
                }
            }
        }
        for w in ["wa", "wb"] {
            let want = waiters.iter().find(|(id, _)| *id == w);
            match want {
                Some((_, base)) => {
                    // Suspended at the restore point; resumes now.
                    engine.run::<u32>(w).signal("go", &5u32).await.unwrap();
                    assert_eq!(engine.run::<u32>(w).result().await.unwrap(), base + 5);
                }
                None => assert!(engine.status(w).await.unwrap().is_none(), "case {k}: {w}"),
            }
        }
        engine.shutdown().await.unwrap();
        std::fs::remove_dir_all(&out).unwrap();
    }
    std::fs::remove_dir_all(&store).unwrap();
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_backup_continues_across_restarts_and_a_restored_copy_starts_a_new_timeline() {
    let dir = tmp("restart");
    let store = tmp("restart-target");
    let target: Arc<dyn Target> = Arc::new(LocalTarget::new(&store).unwrap());
    let mut first_round = None;
    for (round, prefix) in ["a", "b", "c"].iter().enumerate() {
        let engine = Engine::open(backed_up(&dir, &target)).await.unwrap();
        register(&engine);
        phase(&engine, prefix, 20).await;
        if round == 0 {
            start_waiter(&engine, "w", 1).await;
        }
        let p = engine.backup().unwrap().sync().await.unwrap();
        first_round.get_or_insert(p);
        engine.shutdown().await.unwrap();
    }
    let (_, state) = backup::load_state(&*target).unwrap().unwrap();
    assert_eq!(state.timelines.len(), 1, "one history across restarts");
    let first_round = first_round.unwrap();

    // Restore the newest point: everything, and it continues the same
    // timeline when backed up again.
    let copy = tmp("restart-copy");
    backup::restore(&*target, &copy, &RestoreOptions::default()).unwrap();
    let engine = Engine::open(backed_up(&copy, &target)).await.unwrap();
    register(&engine);
    for prefix in ["a", "b", "c"] {
        for i in 0..20 {
            assert_eq!(
                engine
                    .run::<u64>(&format!("{prefix}-{i}"))
                    .result()
                    .await
                    .unwrap(),
                expected(i)
            );
        }
    }
    phase(&engine, "d", 5).await;
    engine.backup().unwrap().sync().await.unwrap();
    engine.shutdown().await.unwrap();
    let (_, state) = backup::load_state(&*target).unwrap().unwrap();
    assert_eq!(
        state.timelines.len(),
        1,
        "the newest point continues its timeline"
    );

    // Restore an older point and back that up: its future differs, so it
    // is a new timeline, and the old one stays restorable.
    let older = tmp("restart-older");
    backup::restore(
        &*target,
        &older,
        &RestoreOptions {
            at_ms: Some(first_round.time_ms),
            timeline: None,
        },
    )
    .unwrap();
    let engine = Engine::open(backed_up(&older, &target)).await.unwrap();
    register(&engine);
    assert!(engine.status("b-0").await.unwrap().is_none());
    phase(&engine, "e", 5).await;
    engine.run::<u32>("w").signal("go", &1u32).await.unwrap();
    assert_eq!(engine.run::<u32>("w").result().await.unwrap(), 2);
    engine.backup().unwrap().sync().await.unwrap();
    engine.shutdown().await.unwrap();
    let (_, state) = backup::load_state(&*target).unwrap().unwrap();
    assert_eq!(
        state.timelines.len(),
        2,
        "{:?}",
        state.timelines.iter().map(|t| &t.id).collect::<Vec<_>>()
    );
    let checked = backup::verify(&*target).unwrap();
    assert!(
        checked.missing.is_empty() && checked.damaged.is_empty(),
        "{checked:?}"
    );
    assert!(checked.timelines.iter().all(|t| t.2 > 0), "{checked:?}");

    // The newest point is the new timeline's.
    let latest = tmp("restart-latest");
    backup::restore(&*target, &latest, &RestoreOptions::default()).unwrap();
    let engine = Engine::open(cfg(&latest)).await.unwrap();
    register(&engine);
    assert!(engine.status("e-0").await.unwrap().is_some());
    assert!(engine.status("d-0").await.unwrap().is_none());
    engine.shutdown().await.unwrap();

    for d in [dir, copy, older, latest, store] {
        let _ = std::fs::remove_dir_all(d);
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn restore_refuses_a_directory_in_use_and_finishes_after_an_interrupted_attempt() {
    let dir = tmp("refuse");
    let store = tmp("refuse-target");
    let target: Arc<dyn Target> = Arc::new(LocalTarget::new(&store).unwrap());
    let engine = Engine::open(backed_up(&dir, &target)).await.unwrap();
    register(&engine);
    phase(&engine, "a", 5).await;
    engine.backup().unwrap().sync().await.unwrap();
    engine.shutdown().await.unwrap();
    let err = backup::restore(&*target, &dir, &RestoreOptions::default()).unwrap_err();
    assert!(err.to_string().contains("not empty"), "{err}");
    // An earlier attempt died halfway: its staging directory is replaced.
    let out = tmp("refuse-out");
    let staging = out.parent().unwrap().join(format!(
        ".{}.restoring",
        out.file_name().unwrap().to_string_lossy()
    ));
    std::fs::create_dir_all(staging.join("wal")).unwrap();
    std::fs::write(staging.join("wal/junk"), b"x").unwrap();
    backup::restore(&*target, &out, &RestoreOptions::default()).unwrap();
    assert!(!staging.exists());
    let engine = Engine::open(cfg(&out)).await.unwrap();
    register(&engine);
    assert_eq!(
        engine.run::<u64>("a-3").result().await.unwrap(),
        expected(3)
    );
    engine.shutdown().await.unwrap();
    for d in [dir, out, store] {
        let _ = std::fs::remove_dir_all(d);
    }
}

/// An S3 bucket in memory behind the S3 target's transport: the backup
/// and restore go through request signing, addressing, listing pages and
/// error handling as they would against S3, without a network.
#[cfg(feature = "s3")]
mod s3 {
    use super::*;
    use fusee::backup::s3::{Credentials, Request, Response, S3Options, S3Target, Transport};
    use std::collections::BTreeMap;
    use std::sync::Mutex;

    #[derive(Default)]
    struct Bucket {
        objects: Mutex<BTreeMap<String, Vec<u8>>>,
        requests: Mutex<usize>,
    }

    fn decode(s: &str) -> String {
        let b = s.as_bytes();
        let mut out = Vec::new();
        let mut i = 0;
        while i < b.len() {
            if b[i] == b'%' {
                out.push(u8::from_str_radix(&s[i + 1..i + 3], 16).unwrap());
                i += 3;
            } else {
                out.push(b[i]);
                i += 1;
            }
        }
        String::from_utf8(out).unwrap()
    }

    struct Shared(Arc<Bucket>);

    impl Transport for Shared {
        fn send(&self, req: &Request) -> std::io::Result<Response> {
            let this = &self.0;
            *this.requests.lock().unwrap() += 1;
            let auth = req.headers.iter().find(|(k, _)| k == "authorization");
            assert!(
                auth.is_some_and(|(_, v)| v.starts_with("AWS4-HMAC-SHA256 Credential=AK/")),
                "unsigned request {req:?}"
            );
            let key = decode(req.path.strip_prefix("/bucket/").unwrap_or(""));
            let mut objects = this.objects.lock().unwrap();
            let ok = |body: Vec<u8>| Response {
                status: 200,
                headers: Vec::new(),
                body,
            };
            Ok(match req.method.as_str() {
                "PUT" => {
                    objects.insert(key, req.body.clone());
                    ok(Vec::new())
                }
                "DELETE" => {
                    objects.remove(&key);
                    Response {
                        status: 204,
                        ..Default::default()
                    }
                }
                "GET" if req.path == "/bucket/" => {
                    let q = |name: &str| {
                        req.query
                            .iter()
                            .find(|(k, _)| k == name)
                            .map(|(_, v)| v.clone())
                    };
                    let prefix = q("prefix").unwrap_or_default();
                    let after = q("continuation-token").unwrap_or_default();
                    // Pages of three keys, to exercise continuation.
                    let keys: Vec<&String> = objects
                        .keys()
                        .filter(|k| k.starts_with(&prefix) && **k > after)
                        .collect();
                    let page = &keys[..keys.len().min(3)];
                    let mut xml = format!(
                        "<ListBucketResult><IsTruncated>{}</IsTruncated>",
                        keys.len() > 3
                    );
                    for k in page {
                        xml.push_str(&format!("<Contents><Key>{k}</Key></Contents>"));
                    }
                    if keys.len() > 3 {
                        xml.push_str(&format!(
                            "<NextContinuationToken>{}</NextContinuationToken>",
                            page[2]
                        ));
                    }
                    xml.push_str("</ListBucketResult>");
                    ok(xml.into_bytes())
                }
                "GET" => match objects.get(&key) {
                    Some(b) => ok(b.clone()),
                    None => Response {
                        status: 404,
                        body: b"<Error><Code>NoSuchKey</Code></Error>".to_vec(),
                        ..Default::default()
                    },
                },
                m => panic!("unexpected {m}"),
            })
        }
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn a_backup_to_s3_restores() {
        let bucket = Arc::new(Bucket::default());
        let target: Arc<dyn Target> = Arc::new(
            S3Target::with_transport(
                S3Options {
                    bucket: "bucket".into(),
                    prefix: "fusee/test".into(),
                    region: "us-east-1".into(),
                    endpoint: Some("http://127.0.0.1:9".into()),
                    path_style: true,
                    credentials: Credentials {
                        access_key: "AK".into(),
                        secret_key: "SK".into(),
                        session_token: None,
                    },
                    ca_file: None,
                    timeout: Duration::from_secs(1),
                },
                Box::new(Shared(bucket.clone())),
                Box::new(std::time::SystemTime::now),
            )
            .unwrap(),
        );
        let dir = tmp("s3");
        let engine = Engine::open(backed_up(&dir, &target)).await.unwrap();
        register(&engine);
        phase(&engine, "a", 30).await;
        start_waiter(&engine, "w", 3).await;
        let p = engine.backup().unwrap().sync().await.unwrap();
        phase(&engine, "b", 10).await;
        engine.shutdown().await.unwrap();
        std::fs::remove_dir_all(&dir).unwrap();
        assert!(
            bucket
                .objects
                .lock()
                .unwrap()
                .contains_key("fusee/test/manifest.json")
        );
        let checked = backup::verify(&*target).unwrap();
        assert!(checked.missing.is_empty() && checked.damaged.is_empty());
        for (at, b_present) in [(Some(p.time_ms), false), (None, true)] {
            let out = tmp("s3-out");
            backup::restore(
                &*target,
                &out,
                &RestoreOptions {
                    at_ms: at,
                    timeline: None,
                },
            )
            .unwrap();
            let engine = Engine::open(cfg(&out)).await.unwrap();
            register(&engine);
            assert_eq!(
                engine.run::<u64>("a-29").result().await.unwrap(),
                expected(29)
            );
            assert_eq!(engine.status("b-0").await.unwrap().is_some(), b_present);
            engine.run::<u32>("w").signal("go", &1u32).await.unwrap();
            assert_eq!(engine.run::<u32>("w").result().await.unwrap(), 4);
            engine.shutdown().await.unwrap();
            std::fs::remove_dir_all(&out).unwrap();
        }
        assert!(*bucket.requests.lock().unwrap() > 10);
    }
}

/// Offloaded records (results and signals over the payload offload
/// threshold) are shipped with the log chunks that reference them, and a
/// restore that replays the log reads them back whole.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn large_payloads_in_the_blob_area_are_backed_up_and_restored() {
    let dir = tmp("blobs-dir");
    let store = tmp("blobs-target");
    let target: Arc<dyn Target> = Arc::new(LocalTarget::new(&store).unwrap());
    // No checkpoint before the restore point: the restore replays the whole
    // log, references and all.
    let config = backed_up(&dir, &target).checkpoint_interval(Duration::from_secs(3600));
    let engine = open_big(config).await;
    for n in 0..4u32 {
        engine
            .start_raw(
                "big",
                &format!("big-{n}"),
                serde_json::to_vec(&n).unwrap().into(),
                Default::default(),
            )
            .await
            .unwrap();
        engine
            .run::<(bool, usize)>(&format!("big-{n}"))
            .signal("note", &"x".repeat(200_000))
            .await
            .unwrap();
    }
    for n in 0..4u32 {
        let r = engine
            .run::<(bool, usize)>(&format!("big-{n}"))
            .result()
            .await
            .unwrap();
        assert_eq!(r, (true, 200_000));
    }
    assert!(dir.join("wal/blobs").is_dir());
    let point = engine.backup().unwrap().sync().await.unwrap();
    let packs = files_under(&store)
        .iter()
        .filter(|p| p.to_string_lossy().ends_with(".blobs"))
        .count();
    assert!(packs > 0, "blob objects in the target");
    let v = backup::verify(&*target).unwrap();
    assert!(v.missing.is_empty() && v.damaged.is_empty(), "{v:?}");
    let restored = tmp("blobs-restored");
    backup::restore(
        &*target,
        &restored,
        &RestoreOptions {
            at_ms: Some(point.time_ms),
            timeline: None,
        },
    )
    .unwrap();
    assert!(restored.join("wal/blobs").is_dir());
    engine.shutdown().await.unwrap();
    let engine = open_big(cfg(&restored)).await;
    for n in 0..4u32 {
        let r = engine
            .run::<(bool, usize)>(&format!("big-{n}"))
            .result()
            .await
            .unwrap();
        assert_eq!(r, (true, 200_000), "big-{n} after the restore");
    }
    engine.shutdown().await.unwrap();
    for d in [dir, store, restored] {
        let _ = std::fs::remove_dir_all(d);
    }
}

fn big_page(n: u32) -> String {
    format!("{n:08}").repeat(40_000)
}

async fn open_big(c: Config) -> Engine {
    let engine = Engine::open(c).await.unwrap();
    engine.register("big", move |ctx: Ctx, n: u32| async move {
        let p: String = ctx
            .step("page", move || async move { Ok::<_, Error>(big_page(n)) })
            .await?;
        let note: String = ctx.wait_for("note").await?.unwrap_or_default();
        Ok((p == big_page(n), note.len()))
    });
    engine
}

fn files_under(dir: &Path) -> Vec<PathBuf> {
    let mut out = Vec::new();
    if let Ok(rd) = std::fs::read_dir(dir) {
        for e in rd.flatten() {
            let p = e.path();
            if p.is_dir() {
                out.extend(files_under(&p));
            } else {
                out.push(p);
            }
        }
    }
    out
}