mvcc-core 0.1.0

Multi-version concurrency control for ordinary Rust structs. Add #[derive(Mvcc)] and get snapshot-isolated transactions with pluggable isolation levels.
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
//! Concurrency stress, aimed at the epoch-reclamation machinery.
//!
//! Aborting is the only path that frees a version (`defer_destroy`), so it is
//! the only path where a reader can be traversing a chain whose head is being
//! unlinked underneath it. These tests exist to hammer exactly that overlap.
//!
//! They cannot *prove* absence of a use-after-free; run under a sanitizer for
//! that. What they do is make the window very likely to be hit, so that a
//! mistake shows up as a crash or wrong answer in CI rather than in production.

use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::thread;

use mvcc::{Config, Database, Error, Mvcc, ReadCommitted, Result, Serializable, Snapshot};

#[derive(Mvcc, Clone, Debug)]
#[mvcc(table = "rows")]
struct Row {
    #[mvcc(primary_key)]
    id: u64,
    value: i64,
    /// A heap field, so a freed version reached through a stale pointer is
    /// likely to fault or read garbage rather than silently look plausible.
    label: String,
}

const KEYS: u64 = 8;

fn seeded(db: &Database) -> Result<()> {
    db.transaction(|tx| {
        for id in 0..KEYS {
            tx.insert(Row {
                id,
                value: 0,
                label: format!("row-{id}"),
            })?;
        }
        Ok(())
    })
}

#[test]
fn readers_survive_writers_aborting_underneath_them() -> Result<()> {
    let db = Arc::new(Database::open(Config::in_memory())?);
    db.register::<Row>()?;
    seeded(&db)?;

    let stop = Arc::new(AtomicBool::new(false));
    let reads = Arc::new(AtomicU64::new(0));
    let aborts = Arc::new(AtomicU64::new(0));

    // Writers that install a version and then always roll it back, so every
    // write ends in `defer_destroy` while readers are mid-traversal.
    let writers: Vec<_> = (0..4)
        .map(|t| {
            let db = Arc::clone(&db);
            let stop = Arc::clone(&stop);
            let aborts = Arc::clone(&aborts);
            thread::spawn(move || {
                let mut seed = 0x9e37_79b9_7f4a_7c15u64 ^ (t + 1);
                while !stop.load(Ordering::Relaxed) {
                    seed ^= seed << 13;
                    seed ^= seed >> 7;
                    seed ^= seed << 17;
                    let key = seed % KEYS;

                    let mut tx = db.begin_with::<Snapshot>();
                    match tx.update::<Row>(&key, |r| {
                        r.value += 1;
                        r.label = format!("pending-{key}");
                    }) {
                        Ok(_) => {
                            tx.abort();
                            aborts.fetch_add(1, Ordering::Relaxed);
                        }
                        Err(e) => assert!(e.is_retriable(), "unexpected: {e}"),
                    }
                }
            })
        })
        .collect();

    // Readers traversing chains the writers are churning.
    let readers: Vec<_> = (0..4)
        .map(|t| {
            let db = Arc::clone(&db);
            let stop = Arc::clone(&stop);
            let reads = Arc::clone(&reads);
            thread::spawn(move || -> Result<()> {
                let mut seed = 0xdead_beef_0bad_f00du64 ^ (t + 1);
                while !stop.load(Ordering::Relaxed) {
                    seed ^= seed << 13;
                    seed ^= seed >> 7;
                    seed ^= seed << 17;
                    let mut tx = db.begin_with::<Snapshot>();
                    let row = tx.get::<Row>(&(seed % KEYS))?.expect("seeded");
                    // Every abort is rolled back, so no committed value ever
                    // changes. Reading anything else means a stale or freed
                    // version was reachable.
                    assert_eq!(row.value, 0, "observed an aborted write");
                    assert_eq!(row.label, format!("row-{}", row.id), "torn version");
                    reads.fetch_add(1, Ordering::Relaxed);
                }
                Ok(())
            })
        })
        .collect();

    thread::sleep(std::time::Duration::from_millis(400));
    stop.store(true, Ordering::Relaxed);
    for w in writers {
        w.join().expect("writer panicked");
    }
    for r in readers {
        r.join().expect("reader panicked")?;
    }

    assert!(
        aborts.load(Ordering::Relaxed) > 100,
        "too few aborts to be meaningful"
    );
    assert!(
        reads.load(Ordering::Relaxed) > 100,
        "too few reads to be meaningful"
    );
    Ok(())
}

#[test]
fn concurrent_inserts_survive_the_slot_map_growing_under_readers() -> Result<()> {
    // Aimed at `engine::slotmap`. The existing tests here work over eight keys
    // seeded up front, so they never grow a bucket array and never race an
    // insert against a lookup — which is precisely the part of that module with
    // no lock to hide behind.
    //
    // 12k keys over 64 shards is roughly 190 per shard, so every shard doubles
    // its array four times from `INITIAL_CAPACITY`, with readers probing
    // throughout. Two failures are in scope: a reader following a bucket array
    // being replaced, and two inserters racing on the same key and each
    // installing a record — a key with two slots, which reads as a lost update
    // rather than as a crash.
    const N: u64 = 12_000;
    const WRITERS: u64 = 4;

    let db = Arc::new(Database::open(Config::in_memory())?);
    db.register::<Row>()?;

    let stop = Arc::new(AtomicBool::new(false));
    let probes = Arc::new(AtomicU64::new(0));

    // Readers probing for keys that are appearing underneath them. A key is
    // either absent or fully formed; a torn label or a missing row that was
    // already observed present would mean a bad publication.
    let readers: Vec<_> = (0..4)
        .map(|t| {
            let db = Arc::clone(&db);
            let stop = Arc::clone(&stop);
            let probes = Arc::clone(&probes);
            thread::spawn(move || -> Result<()> {
                let mut seed = 0xfeed_face_cafe_d00du64 ^ (t + 1);
                while !stop.load(Ordering::Relaxed) {
                    seed ^= seed << 13;
                    seed ^= seed >> 7;
                    seed ^= seed << 17;
                    let id = seed % N;
                    let mut tx = db.begin_with::<Snapshot>();
                    if let Some(row) = tx.get::<Row>(&id)? {
                        assert_eq!(row.id, id, "lookup returned another key's slot");
                        assert_eq!(row.label, format!("row-{id}"), "torn version");
                    }
                    probes.fetch_add(1, Ordering::Relaxed);
                }
                Ok(())
            })
        })
        .collect();

    // Every key is offered by *two* writers, so the same-key insert race is hit
    // continuously rather than incidentally.
    let writers: Vec<_> = (0..WRITERS)
        .map(|t| {
            let db = Arc::clone(&db);
            thread::spawn(move || -> Result<()> {
                for id in (t % 2..N).step_by(2) {
                    let outcome = db.transaction(|tx| {
                        tx.insert(Row {
                            id,
                            value: 0,
                            label: format!("row-{id}"),
                        })
                    });
                    // Losing the race is the expected outcome for one of the
                    // two writers offering each key, either way round.
                    if let Err(e) = outcome
                        && !matches!(e, Error::DuplicateKey { .. } | Error::WriteConflict { .. })
                    {
                        return Err(e);
                    }
                }
                Ok(())
            })
        })
        .collect();

    for w in writers {
        w.join().expect("writer panicked")?;
    }
    stop.store(true, Ordering::Relaxed);
    for r in readers {
        r.join().expect("reader panicked")?;
    }

    // Every key present exactly once. A duplicated slot shows up here as a
    // count above `N`, and a lost record as one below.
    let mut tx = db.begin();
    let rows = tx.scan::<Row>()?;
    assert_eq!(
        rows.len(),
        N as usize,
        "slot count wrong after concurrent growth"
    );
    let mut ids: Vec<u64> = rows.iter().map(|r| r.id).collect();
    ids.dedup();
    assert_eq!(ids.len(), N as usize, "a key was installed in two slots");
    drop(tx);

    // And each one is reachable by the index, not merely present in a scan —
    // a record appended to the chunk list but lost by the bucket array during a
    // resize would pass the assertions above and fail here.
    let mut tx = db.begin();
    for id in 0..N {
        assert!(
            tx.get::<Row>(&id)?.is_some(),
            "key {id} unreachable by lookup"
        );
    }

    assert!(
        probes.load(Ordering::Relaxed) > 1_000,
        "too few concurrent probes to be meaningful"
    );
    Ok(())
}

#[test]
fn contended_serializable_transfers_conserve_and_terminate() -> Result<()> {
    // Every transfer touches two of eight keys at `Serializable`, so aborts,
    // retries, SIREAD registration and reclamation all run flat out and overlap.
    let db = Arc::new(Database::open(Config::in_memory())?);
    db.register::<Row>()?;
    db.transaction(|tx| {
        for id in 0..KEYS {
            tx.insert(Row {
                id,
                value: 1_000,
                label: format!("row-{id}"),
            })?;
        }
        Ok(())
    })?;

    let threads: Vec<_> = (0..4)
        .map(|t| {
            let db = Arc::clone(&db);
            thread::spawn(move || -> Result<()> {
                let mut seed = 0x51_7c_c1_b7u64 ^ (t + 1);
                for _ in 0..300 {
                    seed ^= seed << 13;
                    seed ^= seed >> 7;
                    seed ^= seed << 17;
                    let (from, to) = (seed % KEYS, (seed >> 8) % KEYS);
                    if from == to {
                        continue;
                    }
                    let outcome = db.transaction_with::<Serializable, _, _>(|tx| {
                        let balance = tx.get::<Row>(&from)?.map(|r| r.value).unwrap_or(0);
                        if balance < 10 {
                            return Ok(());
                        }
                        tx.update::<Row>(&from, |r| r.value -= 10)?;
                        tx.update::<Row>(&to, |r| r.value += 10)?;
                        Ok(())
                    });
                    if let Err(e) = outcome {
                        // Retries are bounded, so exhausting them under this
                        // much contention is a legitimate outcome; anything
                        // else is a real failure.
                        match e {
                            Error::SerializationFailure | Error::WriteConflict { .. } => {}
                            e => return Err(e),
                        }
                    }
                }
                Ok(())
            })
        })
        .collect();

    for t in threads {
        t.join().expect("worker panicked")?;
    }

    let mut tx = db.begin();
    let total: i64 = tx.scan::<Row>()?.iter().map(|r| r.value).sum();
    assert_eq!(total, KEYS as i64 * 1_000, "money was created or destroyed");
    Ok(())
}

/// A unique index under a genuine race, rather than a hand-built interleaving.
///
/// Every thread tries to take the same key at the same time, once per round,
/// each with its own primary key — so no two of them touch a common slot and
/// nothing in first-updater-wins, the snapshot, or SSI is between them. The
/// only thing that is, is the claim on the index key.
///
/// The failure this guards is quiet: a duplicate is not a crash and not a torn
/// read, so a test that only watched for those would pass while the constraint
/// silently did nothing. Checked by breaking it — with the claim removed, every
/// round admits two to four winners.
#[test]
fn only_one_transaction_may_take_a_unique_key() -> Result<()> {
    use std::sync::Barrier;

    #[derive(Mvcc, Clone, Debug)]
    #[mvcc(table = "unique_rows")]
    struct Member {
        #[mvcc(primary_key)]
        id: u64,
        #[mvcc(index(unique))]
        email: String,
    }

    const THREADS: u64 = 4;
    const ROUNDS: u64 = 200;

    let db = Arc::new(Database::open(Config::in_memory())?);
    db.register::<Member>()?;
    let barrier = Arc::new(Barrier::new(THREADS as usize));
    let winners = Arc::new(AtomicU64::new(0));

    let threads: Vec<_> = (0..THREADS)
        .map(|t| {
            let db = Arc::clone(&db);
            let barrier = Arc::clone(&barrier);
            let winners = Arc::clone(&winners);
            thread::spawn(move || -> Result<()> {
                for round in 0..ROUNDS {
                    barrier.wait();
                    let email = format!("round-{round}@example.com");
                    let outcome = db.transaction(|tx| {
                        tx.insert(Member {
                            id: round * THREADS + t,
                            email: email.clone(),
                        })
                    });
                    match outcome {
                        Ok(()) => {
                            winners.fetch_add(1, Ordering::Relaxed);
                        }
                        // The two ways of losing: the key was already committed
                        // by the winner, or the winner was still in flight and
                        // the retries ran out.
                        Err(Error::DuplicateKey { .. } | Error::WriteConflict { .. }) => {}
                        Err(e) => return Err(e),
                    }
                }
                Ok(())
            })
        })
        .collect();

    for t in threads {
        t.join().expect("worker panicked")?;
    }

    // One winner per round, and — the assertion that actually catches a lost
    // constraint — one row per key in the table.
    let mut tx = db.begin();
    let rows = tx.scan::<Member>()?;
    assert_eq!(
        rows.len(),
        ROUNDS as usize,
        "{} rows for {ROUNDS} unique keys",
        rows.len()
    );
    let mut emails: Vec<String> = rows.iter().map(|m| m.email.clone()).collect();
    emails.sort();
    let distinct = emails.len();
    emails.dedup();
    assert_eq!(emails.len(), distinct, "a unique key was taken twice");
    assert_eq!(
        winners.load(Ordering::Relaxed),
        ROUNDS,
        "exactly one transaction per round may win"
    );
    Ok(())
}

/// An insert must never quietly become an update.
///
/// The primary key check in `insert` runs before the slot lock, so under a race
/// it is stale by the time the write lands: every thread here looks at a key
/// that does not exist yet, and only then contends for the slot. Above
/// `ReadCommitted` first-committer-wins catches the loser, which is why this
/// runs at `ReadCommitted` — the level with no such check, and the only one
/// where the stale look was the last word.
///
/// The symptom is a lost insert rather than a crash: two transactions both
/// report success and one row is left holding one of the two values.
#[test]
fn a_racing_insert_does_not_overwrite_a_committed_row() -> Result<()> {
    use std::sync::Barrier;

    const THREADS: u64 = 4;
    const ROUNDS: u64 = 200;

    let db = Arc::new(Database::open(Config::in_memory())?);
    db.register::<Row>()?;
    let barrier = Arc::new(Barrier::new(THREADS as usize));
    let winners = Arc::new(AtomicU64::new(0));

    let threads: Vec<_> = (0..THREADS)
        .map(|t| {
            let db = Arc::clone(&db);
            let barrier = Arc::clone(&barrier);
            let winners = Arc::clone(&winners);
            thread::spawn(move || -> Result<()> {
                for round in 0..ROUNDS {
                    barrier.wait();
                    // One fresh key per round, contended by every thread.
                    let outcome = db.transaction_with::<ReadCommitted, _, _>(|tx| {
                        tx.insert(Row {
                            id: round,
                            value: t as i64,
                            label: format!("by-{t}"),
                        })
                    });
                    match outcome {
                        Ok(()) => {
                            winners.fetch_add(1, Ordering::Relaxed);
                        }
                        Err(Error::DuplicateKey { .. } | Error::WriteConflict { .. }) => {}
                        Err(e) => return Err(e),
                    }
                }
                Ok(())
            })
        })
        .collect();

    for t in threads {
        t.join().expect("worker panicked")?;
    }

    assert_eq!(
        winners.load(Ordering::Relaxed),
        ROUNDS,
        "every key must have exactly one successful inserter"
    );
    let mut tx = db.begin();
    assert_eq!(tx.scan::<Row>()?.len(), ROUNDS as usize);
    Ok(())
}