regolith 0.1.2

ACID, performance oriented, embedded key-value database engine for edge systems
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
//! Group-commit behaviour through the public API.
//!
//! The three properties these tests defend, in priority order:
//!
//! 1. A snapshot never observes a torn batch. The read horizon moves only
//!    after a whole group is durable and applied.
//! 2. Every writer that returns `Ok` really did commit, and every writer in
//!    a group that failed learns it did not.
//! 3. N concurrent durable writers cost far fewer than N fsyncs, which is
//!    the whole point of the change and is the one thing here that is
//!    deterministic enough to assert on.

use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::{Arc, Barrier};
use std::thread;
use std::time::{Duration, Instant};

use regolith::{Db, DurabilityMode, Options, Statistics, Ticker, WriteBatch, WriteOptions};
use tempfile::TempDir;

fn durable_opts(stats: Option<Arc<Statistics>>) -> Options {
    Options {
        durability: DurabilityMode::Immediate,
        statistics: stats,
        ..Options::default()
    }
}

#[test]
fn concurrent_durable_writers_cost_far_fewer_fsyncs_than_writes() {
    // The deterministic proof that group commit works. Throughput on this
    // host is not characterisable, but a counter is.
    const WRITERS: usize = 8;
    const PER_WRITER: usize = 250;
    // Group commit amortises an fsync across writers that are inside the
    // commit path at the same time. A barrier gets them to start
    // together but cannot keep them overlapping, so on a loaded host
    // every write can legitimately form its own group and the ratio does
    // not appear. That is a scheduling outcome, not a defect, and one
    // attempt at it is a coin flip: this test failed under full-suite
    // load while passing on its own.
    //
    // So the workload runs until amortisation is observed. If group
    // commit works, a round where eight threads hammer produces a
    // multi-writer group almost immediately; if none of the attempts
    // ever does, the mechanism is broken and that is worth failing on.
    const ATTEMPTS: usize = 5;

    let mut observed = None;
    for attempt in 1..=ATTEMPTS {
        let dir = TempDir::new().unwrap();
        let stats = Arc::new(Statistics::new());
        let db = Arc::new(Db::open(dir.path(), durable_opts(Some(Arc::clone(&stats)))).unwrap());

        let gate = Arc::new(Barrier::new(WRITERS));
        let mut handles = Vec::with_capacity(WRITERS);
        for w in 0..WRITERS {
            let (db, gate) = (Arc::clone(&db), Arc::clone(&gate));
            handles.push(thread::spawn(move || {
                gate.wait();
                for i in 0..PER_WRITER {
                    db.put(format!("w{w}k{i:05}").as_bytes(), b"value").unwrap();
                }
            }));
        }
        for handle in handles {
            handle.join().unwrap();
        }

        let writes = (WRITERS * PER_WRITER) as u64;
        let syncs = stats.get_ticker(Ticker::WalSyncCount);
        assert!(syncs >= 1, "durable writes must reach stable storage");

        // Every write that returned Ok must be readable, on every
        // attempt, whether or not this one amortised.
        for w in 0..WRITERS {
            for i in 0..PER_WRITER {
                let key = format!("w{w}k{i:05}");
                assert_eq!(
                    db.get(key.as_bytes()).unwrap(),
                    Some(b"value".to_vec()),
                    "every write that returned Ok must be readable"
                );
            }
        }

        if syncs < writes {
            observed = Some((attempt, syncs, writes));
            break;
        }
    }

    let (attempt, syncs, writes) = observed.unwrap_or_else(|| {
        panic!(
            "group commit never amortised an fsync across {WRITERS} concurrent writers in \
             {ATTEMPTS} attempts of {PER_WRITER} writes each: every write formed its own group"
        )
    });
    println!("group commit: {syncs} syncs for {writes} durable writes, on attempt {attempt}");
}

#[test]
fn a_serial_durable_writer_still_syncs_once_per_write() {
    // The other side of the amortisation: with nothing to batch with, a
    // durable write is still a durable write.
    let dir = TempDir::new().unwrap();
    let stats = Arc::new(Statistics::new());
    let db = Db::open(dir.path(), durable_opts(Some(Arc::clone(&stats)))).unwrap();

    for i in 0..16 {
        db.put(format!("k{i:03}").as_bytes(), b"v").unwrap();
    }
    assert_eq!(stats.get_ticker(Ticker::WalSyncCount), 16);
}

#[test]
fn a_snapshot_never_observes_a_torn_batch() {
    // The invariant the engine already guaranteed and group commit must
    // not break: a batch is all-or-nothing to every reader.
    const BATCH: usize = 32;
    const ROUNDS: usize = 400;

    let dir = TempDir::new().unwrap();
    let db = Arc::new(Db::open(dir.path(), Options::default()).unwrap());
    let stop = Arc::new(AtomicBool::new(false));
    let reads = Arc::new(AtomicUsize::new(0));

    let reader = {
        let db = Arc::clone(&db);
        let stop = Arc::clone(&stop);
        let reads = Arc::clone(&reads);
        thread::spawn(move || {
            while !stop.load(Ordering::Acquire) {
                let snap = db.snapshot();
                for round in 0..ROUNDS {
                    let first = snap.get(format!("b{round:04}_00").as_bytes()).unwrap();
                    let last = snap
                        .get(format!("b{round:04}_{:02}", BATCH - 1).as_bytes())
                        .unwrap();
                    assert_eq!(
                        first.is_some(),
                        last.is_some(),
                        "round {round} was observed torn: first={:?} last={:?}",
                        first.is_some(),
                        last.is_some()
                    );
                    if first.is_some() {
                        for i in 0..BATCH {
                            assert!(
                                snap.get(format!("b{round:04}_{i:02}").as_bytes())
                                    .unwrap()
                                    .is_some(),
                                "round {round} key {i} missing from a batch whose ends are both present"
                            );
                        }
                    }
                    reads.fetch_add(1, Ordering::Relaxed);
                }
            }
        })
    };

    let mut writers = Vec::new();
    for lane in 0..4 {
        let db = Arc::clone(&db);
        writers.push(thread::spawn(move || {
            for round in (lane..ROUNDS).step_by(4) {
                let mut batch = WriteBatch::new();
                for i in 0..BATCH {
                    batch.put(format!("b{round:04}_{i:02}").as_bytes(), b"v");
                }
                db.write(batch).unwrap();
            }
        }));
    }
    for handle in writers {
        handle.join().unwrap();
    }
    stop.store(true, Ordering::Release);
    reader.join().unwrap();

    assert!(reads.load(Ordering::Relaxed) > 0, "the reader never ran");
    for round in 0..ROUNDS {
        for i in 0..BATCH {
            assert_eq!(
                db.get(format!("b{round:04}_{i:02}").as_bytes()).unwrap(),
                Some(b"v".to_vec())
            );
        }
    }
}

#[test]
fn every_writer_reads_its_own_write_immediately() {
    // The read horizon is published before any follower is released, so a
    // writer that returns from `put` and snapshots right away sees itself.
    const WRITERS: usize = 8;
    const PER_WRITER: usize = 500;

    let dir = TempDir::new().unwrap();
    let db = Arc::new(Db::open(dir.path(), Options::default()).unwrap());

    let mut handles = Vec::with_capacity(WRITERS);
    for w in 0..WRITERS {
        let db = Arc::clone(&db);
        handles.push(thread::spawn(move || {
            for i in 0..PER_WRITER {
                let key = format!("ryw{w}_{i:05}");
                db.put(key.as_bytes(), b"mine").unwrap();
                let snap = db.snapshot();
                assert_eq!(
                    snap.get(key.as_bytes()).unwrap(),
                    Some(b"mine".to_vec()),
                    "writer {w} could not see its own committed write"
                );
            }
        }));
    }
    for handle in handles {
        handle.join().unwrap();
    }
}

#[test]
fn every_committed_write_survives_a_reopen() {
    const WRITERS: usize = 6;
    const PER_WRITER: usize = 200;

    let dir = TempDir::new().unwrap();
    {
        let db = Arc::new(Db::open(dir.path(), durable_opts(None)).unwrap());
        let mut handles = Vec::with_capacity(WRITERS);
        for w in 0..WRITERS {
            let db = Arc::clone(&db);
            handles.push(thread::spawn(move || {
                for i in 0..PER_WRITER {
                    db.put(format!("d{w}_{i:04}").as_bytes(), b"durable")
                        .unwrap();
                }
            }));
        }
        for handle in handles {
            handle.join().unwrap();
        }
        db.close().unwrap();
    }

    let db = Db::open(dir.path(), Options::default()).unwrap();
    for w in 0..WRITERS {
        for i in 0..PER_WRITER {
            assert_eq!(
                db.get(format!("d{w}_{i:04}").as_bytes()).unwrap(),
                Some(b"durable".to_vec()),
                "a durable write from thread {w} did not survive the reopen"
            );
        }
    }
}

#[test]
fn every_synced_write_replays_from_the_wal_without_a_close() {
    // No `close()` and a write buffer large enough that nothing flushes,
    // so recovery has to come out of the grouped WAL records alone.
    const WRITERS: usize = 4;
    const PER_WRITER: usize = 200;

    let dir = TempDir::new().unwrap();
    {
        let db = Arc::new(
            Db::open(
                dir.path(),
                Options {
                    write_buffer_size: 64 * 1024 * 1024,
                    ..durable_opts(None)
                },
            )
            .unwrap(),
        );
        let mut handles = Vec::with_capacity(WRITERS);
        for w in 0..WRITERS {
            let db = Arc::clone(&db);
            handles.push(thread::spawn(move || {
                for i in 0..PER_WRITER {
                    db.put(format!("wal{w}_{i:04}").as_bytes(), b"v").unwrap();
                }
            }));
        }
        for handle in handles {
            handle.join().unwrap();
        }
    }

    let db = Db::open(dir.path(), Options::default()).unwrap();
    for w in 0..WRITERS {
        for i in 0..PER_WRITER {
            assert_eq!(
                db.get(format!("wal{w}_{i:04}").as_bytes()).unwrap(),
                Some(b"v".to_vec()),
                "a write that returned Ok under Immediate durability did not replay"
            );
        }
    }
}

#[test]
fn mixed_durability_writers_all_commit() {
    const PER_LANE: usize = 200;

    let dir = TempDir::new().unwrap();
    let db = Arc::new(Db::open(dir.path(), Options::default()).unwrap());

    let lanes: Vec<WriteOptions> = vec![
        WriteOptions {
            sync: true,
            ..WriteOptions::default()
        },
        WriteOptions::default(),
        WriteOptions {
            disable_wal: true,
            ..WriteOptions::default()
        },
    ];

    let mut handles = Vec::new();
    for (lane, opts) in lanes.into_iter().enumerate() {
        let db = Arc::clone(&db);
        handles.push(thread::spawn(move || {
            for i in 0..PER_LANE {
                db.put_opt(&opts, format!("m{lane}_{i:04}").as_bytes(), b"v")
                    .unwrap();
            }
        }));
    }
    for handle in handles {
        handle.join().unwrap();
    }

    for lane in 0..3 {
        for i in 0..PER_LANE {
            assert_eq!(
                db.get(format!("m{lane}_{i:04}").as_bytes()).unwrap(),
                Some(b"v".to_vec())
            );
        }
    }
}

#[test]
fn administrative_operations_still_exclude_writers() {
    // `compact_range` and `close` take the same pipeline mutex the commit
    // leader does, so they cannot interleave with a group.
    let dir = TempDir::new().unwrap();
    let db = Arc::new(
        Db::open(
            dir.path(),
            Options {
                write_buffer_size: 8 * 1024,
                ..Options::default()
            },
        )
        .unwrap(),
    );

    let stop = Arc::new(AtomicBool::new(false));
    let mut writers = Vec::new();
    for w in 0..4 {
        let db = Arc::clone(&db);
        let stop = Arc::clone(&stop);
        writers.push(thread::spawn(move || {
            let mut i = 0usize;
            while !stop.load(Ordering::Acquire) {
                db.put(format!("a{w}_{i:05}").as_bytes(), &[b'x'; 128])
                    .unwrap();
                i += 1;
            }
            i
        }));
    }

    let deadline = Instant::now() + Duration::from_millis(500);
    while Instant::now() < deadline {
        db.compact_range(None, None).unwrap();
    }
    stop.store(true, Ordering::Release);

    let counts: Vec<usize> = writers.into_iter().map(|h| h.join().unwrap()).collect();
    for (w, count) in counts.iter().enumerate() {
        assert!(*count > 0, "writer {w} made no progress against compaction");
        for i in 0..*count {
            assert_eq!(
                db.get(format!("a{w}_{i:05}").as_bytes()).unwrap(),
                Some(vec![b'x'; 128]),
                "writer {w} lost key {i} across a concurrent compaction"
            );
        }
    }
}

#[test]
fn a_batch_of_many_operations_gets_one_sync() {
    let dir = TempDir::new().unwrap();
    let stats = Arc::new(Statistics::new());
    let db = Db::open(dir.path(), durable_opts(Some(Arc::clone(&stats)))).unwrap();

    let mut batch = WriteBatch::new();
    for i in 0..256 {
        batch.put(format!("batch{i:04}").as_bytes(), b"v");
    }
    db.write(batch).unwrap();

    assert_eq!(
        stats.get_ticker(Ticker::WalSyncCount),
        1,
        "a 256-operation batch is one group and therefore one fdatasync"
    );
    for i in 0..256 {
        assert_eq!(
            db.get(format!("batch{i:04}").as_bytes()).unwrap(),
            Some(b"v".to_vec())
        );
    }
}