vantage-vista 2.5.0

Universal, schema-bearing data handle for the Vantage data framework
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
//! Writes through a [`Transaction`] land together or not at all.

use std::future::Future;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::{Duration, Instant};

use async_trait::async_trait;
use tokio::sync::Barrier;
use tokio::time::{sleep, timeout};
use vantage_core::{Result, error};
use vantage_dataset::traits::{ReadableValueSet, WritableValueSet};
use vantage_types::Record;

use super::Probe;
use crate::{CborValue, FilterOp, Transaction, Vista};

type Rec = Record<CborValue>;

pub(super) const PROBE_DEADLINE: Duration = Duration::from_secs(2);
const COLLIDE_DEADLINE: Duration = Duration::from_secs(3);
/// The whole check's time, so its one panic lands before nextest's 5 s kill.
const CHECK_BUDGET: Duration = Duration::from_millis(4500);
/// How long `writers_collide` gives the second writer to (wrongly) get through.
const WRITER_WAIT: Duration = Duration::from_millis(100);
/// How long `lock_makes_a_reader_wait` gives the second reader to (wrongly) get through.
const READER_WAIT: Duration = Duration::from_millis(200);
pub(super) const FAILED_STATEMENT: &str = "a statement failed inside the transaction; rolled back";
const LOCK_NEEDS_TX: &str = "lock() needs a transaction";

/// Two empty tables on one datasource:
///   a(id text pk, n int NOT NULL CHECK (n >= 0))
///   b(id text pk, a_id text, n int NOT NULL CHECK (n >= 0))
/// with relation "b" on a (b.a_id = a.id). The check constraint gives
/// `failed_statement_fails_commit` a statement that must fail.
#[non_exhaustive]
pub struct TxTables {
    pub a: Vista,
    pub b: Vista,
}

impl TxTables {
    pub fn new(a: Vista, b: Vista) -> Self {
        Self { a, b }
    }
}

#[async_trait]
pub trait TxFixture: Send + Sync {
    /// New, empty tables on the normal pool.
    async fn fresh(&self) -> TxTables;
    /// New, empty tables, on the same datasource reopened with one connection.
    async fn fresh_pool_of_one(&self) -> TxTables;
    /// Postgres/MySQL report a deadlock; SQLite serialises writers instead.
    fn detects_deadlocks(&self) -> bool;
    /// Whether a wrapped Vista's `lock()` locks the rows it reads.
    fn supports_lock(&self) -> bool {
        false
    }
}

/// Runs every transaction probe on fresh tables, one after another, then
/// panics once, listing the probes that broke the contract, hung past their
/// deadline, or didn't run because the check's time budget was spent. Reads
/// "outside" the transaction go through the unwrapped Vistas.
pub async fn check_transactions(fixture: &impl TxFixture) {
    let mut runner = Runner::new();
    let fx = fixture;
    run(
        &mut runner,
        "commit_lands_every_write",
        PROBE_DEADLINE,
        commit_lands_every_write(fx),
    )
    .await;
    run(
        &mut runner,
        "rollback_lands_none",
        PROBE_DEADLINE,
        rollback_lands_none(fx),
    )
    .await;
    run(
        &mut runner,
        "drop_lands_none",
        PROBE_DEADLINE,
        drop_lands_none(fx),
    )
    .await;
    run(
        &mut runner,
        "uncommitted_is_invisible_outside",
        PROBE_DEADLINE,
        uncommitted_is_invisible_outside(fx),
    )
    .await;
    run(
        &mut runner,
        "reads_see_own_writes",
        PROBE_DEADLINE,
        reads_see_own_writes(fx),
    )
    .await;
    run(
        &mut runner,
        "nothing_begins_until_used",
        PROBE_DEADLINE,
        nothing_begins_until_used(fx),
    )
    .await;
    run(
        &mut runner,
        "ref_target_joins_the_transaction",
        PROBE_DEADLINE,
        ref_target_joins_the_transaction(fx),
    )
    .await;
    run(
        &mut runner,
        "narrowed_vista_keeps_narrowing",
        PROBE_DEADLINE,
        narrowed_vista_keeps_narrowing(fx),
    )
    .await;
    run(
        &mut runner,
        "concurrent_calls_share_the_transaction",
        PROBE_DEADLINE,
        concurrent_calls_share_the_transaction(fx),
    )
    .await;
    run(
        &mut runner,
        "failed_statement_fails_commit",
        PROBE_DEADLINE,
        failed_statement_fails_commit(fx),
    )
    .await;
    run(
        &mut runner,
        "use_after_finish_errors",
        PROBE_DEADLINE,
        use_after_finish_errors(fx),
    )
    .await;
    run(
        &mut runner,
        "writers_collide",
        COLLIDE_DEADLINE,
        writers_collide(fx),
    )
    .await;
    if fx.supports_lock() {
        run(
            &mut runner,
            "lock_outside_a_transaction_errors",
            PROBE_DEADLINE,
            lock_outside_a_transaction_errors(fx),
        )
        .await;
        run(
            &mut runner,
            "lock_makes_a_reader_wait",
            PROBE_DEADLINE,
            lock_makes_a_reader_wait(fx),
        )
        .await;
    }
    runner.finish();
}

/// Runs the probes in turn, each under its own deadline cut to what is left
/// of [`CHECK_BUDGET`].
pub(super) struct Runner {
    probe: Probe,
    ends: Instant,
}

impl Runner {
    pub(super) fn new() -> Self {
        Self {
            probe: Probe::default(),
            ends: Instant::now() + CHECK_BUDGET,
        }
    }

    #[track_caller]
    pub(super) fn finish(self) {
        self.probe.finish();
    }
}

pub(super) async fn run(
    runner: &mut Runner,
    name: &str,
    deadline: Duration,
    body: impl Future<Output = Result<()>>,
) {
    let left = runner.ends.saturating_duration_since(Instant::now());
    if left.is_zero() {
        return runner
            .probe
            .fail(format!("{name}: not run (time budget spent)"));
    }
    match timeout(deadline.min(left), body).await {
        Ok(Ok(())) => {}
        Ok(Err(e)) => runner.probe.fail(format!("{name}: {e}")),
        Err(_) => runner.probe.fail(format!("{name}: hung")),
    }
}

async fn commit_lands_every_write(fx: &impl TxFixture) -> Result<()> {
    let t = fx.fresh().await;
    let tx = Transaction::new();
    tx.wrap(&t.a).insert_value("x", &rec(1)).await?;
    tx.wrap(&t.b).insert_value("y", &rec_b("x", 1)).await?;
    tx.commit().await?;
    present(&t.a, "x", "after commit").await?;
    present(&t.b, "y", "after commit").await
}

async fn rollback_lands_none(fx: &impl TxFixture) -> Result<()> {
    let t = fx.fresh().await;
    let tx = Transaction::new();
    tx.wrap(&t.a).insert_value("x", &rec(1)).await?;
    tx.rollback().await?;
    absent(&t.a, "x", "after rollback").await
}

async fn drop_lands_none(fx: &impl TxFixture) -> Result<()> {
    let t = fx.fresh_pool_of_one().await;
    {
        let tx = Transaction::new();
        let a = tx.wrap(&t.a);
        a.insert_value("x", &rec(1)).await?;
    }
    absent(&t.a, "x", "after dropping the transaction").await
}

async fn uncommitted_is_invisible_outside(fx: &impl TxFixture) -> Result<()> {
    let t = fx.fresh().await;
    let tx = Transaction::new();
    tx.wrap(&t.a).insert_value("x", &rec(1)).await?;
    absent(&t.a, "x", "before commit").await?;
    tx.commit().await?;
    Ok(())
}

async fn reads_see_own_writes(fx: &impl TxFixture) -> Result<()> {
    let t = fx.fresh().await;
    let tx = Transaction::new();
    let a = tx.wrap(&t.a);
    a.insert_value("x", &rec(1)).await?;
    a.patch_value("x", &rec(2)).await?;
    let n = n_of(&a, "x").await?;
    tx.rollback().await?;
    ensure(
        n == int(2),
        format!("a wrapped get after patching n to 2 reads {n:?}"),
    )
}

async fn nothing_begins_until_used(fx: &impl TxFixture) -> Result<()> {
    let t = fx.fresh_pool_of_one().await;
    let tx = Transaction::new();
    let _unused = tx.wrap(&t.a);
    t.a.count().await?;
    tx.commit().await?;
    Ok(())
}

async fn ref_target_joins_the_transaction(fx: &impl TxFixture) -> Result<()> {
    let t = fx.fresh_pool_of_one().await;
    let tx = Transaction::new();
    let a = tx.wrap(&t.a);
    a.insert_value("x", &rec(1)).await?;
    let row_x: Rec = [
        ("id".to_string(), CborValue::Text("x".into())),
        ("n".to_string(), int(1)),
    ]
    .into_iter()
    .collect();
    let b = a.get_ref("b", Some(&row_x))?;
    b.insert_value("y", &rec_b("x", 1)).await?;
    tx.commit().await?;
    present(&t.b, "y", "after commit").await
}

async fn narrowed_vista_keeps_narrowing(fx: &impl TxFixture) -> Result<()> {
    let t = fx.fresh().await;
    t.a.insert_value("x", &rec(1)).await?;
    t.a.insert_value("z", &rec(2)).await?;
    let narrowed = t.a.clone().with_condition("n", FilterOp::Eq, int(1))?;
    let tx = Transaction::new();
    let listed = tx.wrap(&narrowed).list_values().await;
    tx.rollback().await?;
    let ids: Vec<String> = listed?.into_keys().collect();
    ensure(
        ids == ["x"],
        format!("a wrapped list narrowed to n = 1 returns {ids:?}, not [\"x\"]"),
    )
}

async fn concurrent_calls_share_the_transaction(fx: &impl TxFixture) -> Result<()> {
    let t = fx.fresh_pool_of_one().await;
    let tx = Transaction::new();
    let a = tx.wrap(&t.a);
    let (one, two) = (rec(1), rec(2));
    let (x, z) = tokio::join!(a.insert_value("x", &one), a.insert_value("z", &two));
    x?;
    z?;
    tx.commit().await?;
    present(&t.a, "x", "after commit").await?;
    present(&t.a, "z", "after commit").await
}

async fn failed_statement_fails_commit(fx: &impl TxFixture) -> Result<()> {
    let t = fx.fresh().await;
    let tx = Transaction::new();
    let a = tx.wrap(&t.a);
    a.insert_value("x", &rec(1)).await?;
    ensure(
        a.insert_value("z", &rec(-1)).await.is_err(),
        "an insert of z with n = -1 passed the check constraint".to_string(),
    )?;
    match tx.commit().await {
        Ok(_) => return Err(error!("commit after a failed statement is Ok")),
        Err(e) if !e.to_string().contains(FAILED_STATEMENT) => {
            return Err(error!(format!(
                "commit after a failed statement errs with `{e}`, not `{FAILED_STATEMENT}`"
            )));
        }
        Err(_) => {}
    }
    absent(&t.a, "x", "after the failed commit").await
}

async fn use_after_finish_errors(fx: &impl TxFixture) -> Result<()> {
    let t = fx.fresh().await;
    let tx = Transaction::new();
    let a = tx.wrap(&t.a);
    tx.commit().await?;
    match a.list_values().await {
        Ok(_) => Err(error!("a wrapped list after commit is Ok")),
        Err(e) if e.to_string().contains("transaction finished") => Ok(()),
        Err(e) => Err(error!(format!(
            "a wrapped list after commit errs with `{e}`, not `transaction finished`"
        ))),
    }
}

async fn writers_collide(fx: &impl TxFixture) -> Result<()> {
    let t = fx.fresh().await;
    t.a.insert_value("x", &rec(1)).await?;
    t.a.insert_value("z", &rec(1)).await?;
    let (tx1, tx2) = (Transaction::new(), Transaction::new());
    let (a1, a2) = (tx1.wrap(&t.a), tx2.wrap(&t.a));

    if fx.detects_deadlocks() {
        let barrier = Barrier::new(2);
        let (r1, r2) = tokio::join!(
            cross(&a1, &tx1, &barrier, "x", "z", 2),
            cross(&a2, &tx2, &barrier, "z", "x", 3),
        );
        return match (r1, r2) {
            (Ok(()), Err(_)) | (Err(_), Ok(())) => Ok(()),
            (Ok(()), Ok(())) => Err(error!(
                "both crossed writers committed; one should have failed on the deadlock"
            )),
            (Err(e1), Err(e2)) => Err(error!(format!(
                "neither crossed writer committed: tx1: {e1}; tx2: {e2}"
            ))),
        };
    }

    a1.patch_value("x", &rec(2)).await?;
    let second_done = AtomicBool::new(false);
    let second = async {
        let patched = a2.patch_value("z", &rec(3)).await;
        second_done.store(true, Ordering::SeqCst);
        patched?;
        tx2.commit().await.map(drop)
    };
    let first = async {
        sleep(WRITER_WAIT).await;
        let waited = !second_done.load(Ordering::SeqCst);
        (waited, tx1.commit().await.map(drop))
    };
    let ((waited, first), second) = tokio::join!(first, second);
    first?;
    second?;
    ensure(
        waited,
        "tx2 patched z while tx1 held an open write".to_string(),
    )?;
    let (x, z) = (n_of(&t.a, "x").await?, n_of(&t.a, "z").await?);
    ensure(
        x == int(2) && z == int(3),
        format!("after both commits x.n = {x:?} and z.n = {z:?}, not 2 and 3"),
    )
}

async fn lock_outside_a_transaction_errors(fx: &impl TxFixture) -> Result<()> {
    let mut a = fx.fresh().await.a;
    match a.lock() {
        Ok(()) => Err(error!("lock() on an unwrapped vista is Ok")),
        Err(e) if e.to_string().contains(LOCK_NEEDS_TX) => Ok(()),
        Err(e) => Err(error!(format!(
            "lock() on an unwrapped vista errs with `{e}`, not `{LOCK_NEEDS_TX}`"
        ))),
    }
}

/// tx1 reads x locked; tx2's locked read of x waits until tx1 commits its
/// patch, then sees it.
async fn lock_makes_a_reader_wait(fx: &impl TxFixture) -> Result<()> {
    let t = fx.fresh().await;
    t.a.insert_value("x", &rec(1)).await?;
    let (tx1, tx2) = (Transaction::new(), Transaction::new());
    let (mut a1, mut a2) = (tx1.wrap(&t.a), tx2.wrap(&t.a));
    a1.lock()?;
    a2.lock()?;
    a1.get_value("x").await?;

    let second_done = AtomicBool::new(false);
    let second = async {
        let n = n_of(&a2, "x").await;
        second_done.store(true, Ordering::SeqCst);
        tx2.rollback().await?;
        n
    };
    let first = async {
        sleep(READER_WAIT).await;
        let waited = !second_done.load(Ordering::SeqCst);
        let committed = async {
            a1.patch_value("x", &rec(2)).await?;
            tx1.commit().await.map(drop)
        }
        .await;
        // Releases x, so the waiting reader returns. Rollback itself is always Ok.
        if committed.is_err() {
            let _ = tx1.rollback().await;
        }
        (waited, committed)
    };
    let ((waited, first), n) = tokio::join!(first, second);
    first?;
    let n = n?;
    ensure(waited, "tx2 read x while tx1 held it locked".to_string())?;
    ensure(
        n == int(2),
        format!("tx2's locked read after tx1 committed n = 2 reads {n:?}"),
    )
}

/// One of two crossed writers: patch `first`, meet the other at `barrier`,
/// then patch `second` and commit. Rolls back on any error. Meets the
/// barrier even when the first patch fails, so the other side never waits
/// for it.
async fn cross(
    a: &Vista,
    tx: &Transaction,
    barrier: &Barrier,
    first: &str,
    second: &str,
    n: i64,
) -> Result<()> {
    let patched = a.patch_value(first, &rec(n)).await;
    barrier.wait().await;
    let out = async {
        patched?;
        a.patch_value(second, &rec(n)).await?;
        tx.commit().await.map(drop)
    }
    .await;
    if out.is_err() {
        tx.rollback().await?;
    }
    out
}

fn int(n: i64) -> CborValue {
    CborValue::Integer(n.into())
}

fn rec(n: i64) -> Rec {
    [("n".to_string(), int(n))].into_iter().collect()
}

fn rec_b(a_id: &str, n: i64) -> Rec {
    [
        ("a_id".to_string(), CborValue::Text(a_id.into())),
        ("n".to_string(), int(n)),
    ]
    .into_iter()
    .collect()
}

pub(super) fn ensure(holds: bool, broken: String) -> Result<()> {
    if holds { Ok(()) } else { Err(error!(broken)) }
}

pub(super) async fn present(vista: &Vista, id: &str, when: &str) -> Result<()> {
    match vista.get_value(id).await? {
        Some(_) => Ok(()),
        None => Err(error!(format!(
            "{}/{id} is not found outside {when}",
            vista.name()
        ))),
    }
}

pub(super) async fn absent(vista: &Vista, id: &str, when: &str) -> Result<()> {
    match vista.get_value(id).await? {
        Some(_) => Err(error!(format!(
            "{}/{id} is found outside {when}",
            vista.name()
        ))),
        None => Ok(()),
    }
}

async fn n_of(vista: &Vista, id: &str) -> Result<CborValue> {
    let row = vista
        .get_value(id)
        .await?
        .ok_or_else(|| error!(format!("{}/{id} is not found", vista.name())))?;
    Ok(row.get("n").cloned().unwrap_or(CborValue::Null))
}