kglite-bolt-server 0.19.7

Pure-Rust Bolt v5.x protocol server for kglite knowledge graphs, with a regression-tested Neo4j Python driver path.
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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
//! Writer admission at the backend: `--write-concurrency queue` against
//! `optimistic`, driven through the same `BoltBackend` calls boltr makes.

use std::collections::HashMap;
use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::time::Duration;

use boltr::error::BoltError;
use boltr::server::{BoltBackend, SessionHandle, TransactionHandle};
use boltr::types::{BoltDict, BoltValue};
use kglite::api::session::CsvImportPolicy;
use kglite::api::storage::{new_dir_graph_in_mode, StorageMode};
use kglite::api::Value;

use super::*;

const OUTDATED: &str = "Neo.TransientError.Transaction.Outdated";

fn backend(mode: WriteConcurrency, wait_ms: u64, idle_ms: u64) -> Arc<KgliteBackend> {
    backend_at(
        std::env::temp_dir().join("writer-queue-unused.kgl"),
        mode,
        wait_ms,
        idle_ms,
    )
}

fn backend_at(
    path: std::path::PathBuf,
    mode: WriteConcurrency,
    wait_ms: u64,
    idle_ms: u64,
) -> Arc<KgliteBackend> {
    let graph = new_dir_graph_in_mode(StorageMode::Memory, None).expect("memory graph");
    Arc::new(
        KgliteBackend::new(
            kglite::api::session::Session::new(graph),
            path,
            false,
            "127.0.0.1:0".into(),
            CsvImportPolicy::Denied,
            ServerIdentity::default(),
            None,
        )
        .with_writer_config(WriterConfig {
            mode,
            wait_timeout: (wait_ms > 0).then(|| Duration::from_millis(wait_ms)),
            idle_timeout: (idle_ms > 0).then(|| Duration::from_millis(idle_ms)),
        }),
    )
}

fn session(n: usize) -> SessionHandle {
    SessionHandle(format!("s{n}"))
}

fn read_mode() -> BoltDict {
    BoltDict::from([("mode".to_string(), BoltValue::String("r".into()))])
}

fn run(b: &KgliteBackend, tx: &TransactionHandle, q: &str) -> Result<(), BoltError> {
    b.execute_in_tx(&tx.0, q, HashMap::new(), None).map(|_| ())
}

fn scalar(b: &KgliteBackend, q: &str) -> i64 {
    let snap = b.session.snapshot();
    let params = HashMap::new();
    let opts = kglite::api::session::ExecuteOptions::new(&params);
    let out = kglite::api::session::execute_read(&snap, q, &opts)
        .expect("read")
        .result;
    match out.rows.first().and_then(|r| r.first()) {
        Some(Value::Int64(n)) => *n,
        other => panic!("expected Int64, got {other:?}"),
    }
}

fn code_of(e: &BoltError) -> String {
    match e {
        BoltError::Query { code, .. } => code.clone(),
        other => format!("{other:?}"),
    }
}

/// One BEGIN/write/COMMIT. `Err` carries the failure's status code.
async fn write_once(b: &KgliteBackend, s: &SessionHandle, q: &str) -> Result<(), String> {
    let tx = b
        .begin_transaction(s, &BoltDict::new())
        .await
        .map_err(|e| code_of(&e))?;
    run(b, &tx, q).map_err(|e| code_of(&e))?;
    b.commit(s, &tx).await.map(|_| ()).map_err(|e| code_of(&e))
}

/// `writers` tasks x `rounds`, each round one transaction built by
/// `query(w, r)` and redone on conflict, as a driver would. Returns the
/// conflict count; any other failure panics.
async fn hammer(
    b: &Arc<KgliteBackend>,
    writers: usize,
    rounds: usize,
    query: impl Fn(usize, usize) -> String + Send + Sync + 'static,
) -> usize {
    let query = Arc::new(query);
    let mut tasks = Vec::new();
    for w in 0..writers {
        let (b, query) = (Arc::clone(b), Arc::clone(&query));
        tasks.push(tokio::spawn(async move {
            let s = session(w);
            let mut conflicts = 0;
            for r in 0..rounds {
                while let Err(code) = write_once(&b, &s, &query(w, r)).await {
                    assert_eq!(code, OUTDATED, "unexpected failure: {code}");
                    conflicts += 1;
                }
            }
            conflicts
        }));
    }
    let mut total = 0;
    for t in tasks {
        total += t.await.expect("writer task");
    }
    total
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn queue_serialises_overlapping_writers_without_conflicts() {
    let b = backend(WriteConcurrency::Queue, 0, 0);
    write_once(&b, &session(99), "CREATE (:Counter {id: 1, n: 0})")
        .await
        .expect("seed");
    let conflicts = hammer(&b, 4, 25, |_, _| {
        "MATCH (c:Counter {id: 1}) SET c.n = c.n + 1".to_string()
    })
    .await;
    assert_eq!(conflicts, 0, "queue mode must not conflict at COMMIT");
    assert_eq!(
        scalar(&b, "MATCH (c:Counter {id: 1}) RETURN c.n"),
        100,
        "every acknowledged increment must be applied"
    );
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn queue_serialises_disjoint_writers_without_conflicts() {
    let b = backend(WriteConcurrency::Queue, 0, 0);
    let conflicts = hammer(&b, 4, 25, |w, r| {
        format!("CREATE (:Item {{id: {}}})", w * 1000 + r)
    })
    .await;
    assert_eq!(conflicts, 0);
    assert_eq!(scalar(&b, "MATCH (i:Item) RETURN count(i)"), 100);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn optimistic_mode_conflicts_even_on_disjoint_keys() {
    // The control that keeps the two tests above from being vacuous: with
    // admission off, two overlapping transactions on disjoint keys conflict.
    let b = backend(WriteConcurrency::Optimistic, 0, 0);
    let (sa, sb) = (session(1), session(2));
    let a = b.begin_transaction(&sa, &BoltDict::new()).await.unwrap();
    let c = b.begin_transaction(&sb, &BoltDict::new()).await.unwrap();
    run(&b, &a, "CREATE (:Item {id: 1})").unwrap();
    run(&b, &c, "CREATE (:Item {id: 2})").unwrap();
    b.commit(&sa, &a).await.unwrap();
    let err = b
        .commit(&sb, &c)
        .await
        .expect_err("stale snapshot must conflict");
    assert_eq!(code_of(&err), OUTDATED);
    assert_eq!(scalar(&b, "MATCH (i:Item) RETURN count(i)"), 1);
}

#[tokio::test]
async fn second_write_begin_waits_then_proceeds_after_commit() {
    let b = backend(WriteConcurrency::Queue, 0, 0);
    let sa = session(1);
    let a = b.begin_transaction(&sa, &BoltDict::new()).await.unwrap();
    let b2 = Arc::clone(&b);
    let waiter = tokio::spawn(async move {
        let tx = b2
            .begin_transaction(&session(2), &BoltDict::new())
            .await
            .unwrap();
        // It began after A's commit, so it sees A's write.
        run(&b2, &tx, "MATCH (i:Item {id: 1}) SET i.seen = 1").unwrap();
        b2.commit(&session(2), &tx).await.unwrap();
    });
    tokio::time::sleep(Duration::from_millis(150)).await;
    assert!(
        !waiter.is_finished(),
        "the second writer must wait for the slot"
    );
    run(&b, &a, "CREATE (:Item {id: 1})").unwrap();
    b.commit(&sa, &a).await.unwrap();
    tokio::time::timeout(Duration::from_secs(5), waiter)
        .await
        .expect("waiter proceeds once the slot frees")
        .unwrap();
    assert_eq!(scalar(&b, "MATCH (i:Item {id: 1}) RETURN i.seen"), 1);
}

#[tokio::test]
async fn readers_never_wait_for_a_writer() {
    let b = backend(WriteConcurrency::Queue, 0, 0);
    let sw = session(1);
    let w = b.begin_transaction(&sw, &BoltDict::new()).await.unwrap();
    run(&b, &w, "CREATE (:Item {id: 1})").unwrap();

    let sr = session(2);
    let r = tokio::time::timeout(
        Duration::from_millis(500),
        b.begin_transaction(&sr, &read_mode()),
    )
    .await
    .expect("a read BEGIN must not wait for the writer slot")
    .unwrap();
    run(&b, &r, "MATCH (i:Item) RETURN count(i)").unwrap();
    b.commit(&sr, &r).await.unwrap();

    tokio::time::timeout(
        Duration::from_millis(500),
        b.execute(
            &sr,
            "MATCH (i:Item) RETURN count(i)",
            &HashMap::new(),
            &BoltDict::new(),
            None,
        ),
    )
    .await
    .expect("an auto-commit read must not wait")
    .unwrap();
    b.rollback(&sw, &w).await.unwrap();
}

#[tokio::test]
async fn wait_timeout_is_a_retriable_transient_failure() {
    let b = backend(WriteConcurrency::Queue, 200, 0);
    let held = b
        .begin_transaction(&session(1), &BoltDict::new())
        .await
        .unwrap();
    let started = std::time::Instant::now();
    let err = b
        .begin_transaction(&session(2), &BoltDict::new())
        .await
        .expect_err("slot is held");
    let code = code_of(&err);
    assert_eq!(
        code,
        "Neo.TransientError.Transaction.LockAcquisitionTimeout"
    );
    assert!(
        code.starts_with("Neo.TransientError."),
        "must be driver-retriable"
    );
    assert!(started.elapsed() >= Duration::from_millis(200));
    // The failed BEGIN left no transaction behind, and the slot still works.
    b.rollback(&session(1), &held).await.unwrap();
    b.begin_transaction(&session(2), &BoltDict::new())
        .await
        .expect("slot is free");
}

#[tokio::test]
async fn an_idle_holder_is_rolled_back_when_a_writer_waits() {
    let b = backend(WriteConcurrency::Queue, 5_000, 200);
    let sa = session(1);
    let a = b.begin_transaction(&sa, &BoltDict::new()).await.unwrap();
    run(&b, &a, "CREATE (:Item {id: 1})").unwrap();

    // No waiter: an idle holder is left alone.
    tokio::time::sleep(Duration::from_millis(400)).await;
    run(&b, &a, "CREATE (:Item {id: 2})").expect("idle holder with no waiter is undisturbed");

    let sb = session(2);
    let started = std::time::Instant::now();
    let tx = tokio::time::timeout(
        Duration::from_secs(3),
        b.begin_transaction(&sb, &BoltDict::new()),
    )
    .await
    .expect("the waiter must reclaim the idle slot")
    .unwrap();
    assert!(started.elapsed() >= Duration::from_millis(150));
    b.commit(&sb, &tx).await.unwrap();

    // The holder's next messages say why, and nothing it wrote survives.
    let err = run(&b, &a, "CREATE (:Item {id: 3})").expect_err("reclaimed");
    assert_eq!(
        code_of(&err),
        "Neo.ClientError.Transaction.TransactionTimedOut"
    );
    let err = b.commit(&sa, &a).await.expect_err("reclaimed");
    assert_eq!(
        code_of(&err),
        "Neo.ClientError.Transaction.TransactionTimedOut"
    );
    b.rollback(&sa, &a)
        .await
        .expect("ROLLBACK of a reclaimed tx is idempotent");
    assert_eq!(scalar(&b, "MATCH (i:Item) RETURN count(i)"), 0);
}

#[tokio::test]
async fn a_running_query_is_never_reclaimed() {
    let b = backend(WriteConcurrency::Queue, 400, 100);
    let a = b
        .begin_transaction(&session(1), &BoltDict::new())
        .await
        .unwrap();
    // A query in flight for longer than the idle timeout.
    let guard = {
        let txs = b.transactions.lock().unwrap();
        let st = txs.get(&a.0).unwrap().lock().unwrap();
        st.writer.as_ref().unwrap().activity().begin_query()
    };
    let err = b
        .begin_transaction(&session(2), &BoltDict::new())
        .await
        .expect_err("holder is busy, waiter times out");
    assert_eq!(
        code_of(&err),
        "Neo.TransientError.Transaction.LockAcquisitionTimeout"
    );
    drop(guard);
    run(&b, &a, "CREATE (:Item {id: 1})").expect("holder survived");
}

#[tokio::test]
async fn closing_or_resetting_the_session_frees_the_slot() {
    for reset in [false, true] {
        let b = backend(WriteConcurrency::Queue, 300, 0);
        let sa = session(1);
        let a = b.begin_transaction(&sa, &BoltDict::new()).await.unwrap();
        run(&b, &a, "CREATE (:Item {id: 1})").unwrap();
        if reset {
            b.reset_session(&sa).await.unwrap();
        } else {
            b.close_session(&sa).await.unwrap();
        }
        let tx = tokio::time::timeout(
            Duration::from_millis(200),
            b.begin_transaction(&session(2), &BoltDict::new()),
        )
        .await
        .expect("slot must be free immediately")
        .unwrap();
        b.commit(&session(2), &tx).await.unwrap();
        assert_eq!(scalar(&b, "MATCH (i:Item) RETURN count(i)"), 0);
    }
}

#[tokio::test]
async fn the_slot_is_held_until_the_transaction_ends() {
    // A COMMIT refused for the wrong session leaves the transaction (and the
    // slot) in place; finishing it releases the slot.
    let b = backend(WriteConcurrency::Queue, 300, 0);
    let a = b
        .begin_transaction(&session(1), &BoltDict::new())
        .await
        .unwrap();
    assert!(b.commit(&session(7), &a).await.is_err());
    assert!(b
        .begin_transaction(&session(2), &BoltDict::new())
        .await
        .is_err());
    b.commit(&session(1), &a).await.unwrap();
    b.begin_transaction(&session(2), &BoltDict::new())
        .await
        .expect("slot free");
}

#[tokio::test]
async fn read_mode_transactions_cannot_write_in_queue_mode_only() {
    let q = backend(WriteConcurrency::Queue, 0, 0);
    let r = q
        .begin_transaction(&session(1), &read_mode())
        .await
        .unwrap();
    let err = run(&q, &r, "CREATE (:Item {id: 1})").expect_err("refused");
    assert_eq!(code_of(&err), "Neo.ClientError.Statement.AccessMode");

    let o = backend(WriteConcurrency::Optimistic, 0, 0);
    let r = o
        .begin_transaction(&session(1), &read_mode())
        .await
        .unwrap();
    run(&o, &r, "CREATE (:Item {id: 1})").expect("optimistic mode keeps today's behaviour");
}

const DDL: &str = "CREATE INDEX FOR (n:Item) ON (n.id)";

async fn run_ddl(b: &KgliteBackend) -> Result<(), BoltError> {
    b.execute(&session(9), DDL, &HashMap::new(), &BoltDict::new(), None)
        .await
        .map(|_| ())
}

#[tokio::test]
async fn auto_commit_schema_ddl_takes_the_writer_slot() {
    let b = backend(WriteConcurrency::Queue, 0, 0);
    let sa = session(1);
    let a = b.begin_transaction(&sa, &BoltDict::new()).await.unwrap();
    run(&b, &a, "CREATE (:Item {id: 1})").unwrap();

    let b2 = Arc::clone(&b);
    let ddl = tokio::spawn(async move { run_ddl(&b2).await });
    tokio::time::sleep(Duration::from_millis(150)).await;
    assert!(!ddl.is_finished(), "DDL must wait behind the open writer");

    b.commit(&sa, &a).await.unwrap();
    tokio::time::timeout(Duration::from_secs(5), ddl)
        .await
        .expect("DDL proceeds once the slot frees")
        .unwrap()
        .expect("DDL commits without a conflict");
    // The slot is free again afterwards.
    let t = tokio::time::timeout(
        Duration::from_millis(300),
        b.begin_transaction(&session(2), &BoltDict::new()),
    )
    .await
    .expect("DDL released the slot")
    .unwrap();
    b.rollback(&session(2), &t).await.unwrap();
}

#[tokio::test]
async fn auto_commit_schema_ddl_times_out_like_any_writer_and_optimistic_skips_the_slot() {
    let q = backend(WriteConcurrency::Queue, 200, 0);
    let held = q
        .begin_transaction(&session(1), &BoltDict::new())
        .await
        .unwrap();
    let err = run_ddl(&q).await.expect_err("slot is held");
    assert_eq!(
        code_of(&err),
        "Neo.TransientError.Transaction.LockAcquisitionTimeout"
    );
    q.rollback(&session(1), &held).await.unwrap();
    run_ddl(&q).await.expect("slot is free");

    let o = backend(WriteConcurrency::Optimistic, 200, 0);
    let _open = o
        .begin_transaction(&session(1), &BoltDict::new())
        .await
        .unwrap();
    run_ddl(&o).await.expect("optimistic mode never waits");
}

const AUTO_WRITE: &str = "CREATE (:Item {id: 2})";

async fn run_auto_write(b: &KgliteBackend) -> Result<(), BoltError> {
    b.execute(
        &session(9),
        AUTO_WRITE,
        &HashMap::new(),
        &BoltDict::new(),
        None,
    )
    .await
    .map(|_| ())
}

#[tokio::test]
async fn auto_commit_data_write_takes_the_writer_slot() {
    let b = backend(WriteConcurrency::Queue, 0, 0);
    let sa = session(1);
    let a = b.begin_transaction(&sa, &BoltDict::new()).await.unwrap();
    run(&b, &a, "CREATE (:Item {id: 1})").unwrap();

    let b2 = Arc::clone(&b);
    let write = tokio::spawn(async move { run_auto_write(&b2).await });
    tokio::time::sleep(Duration::from_millis(150)).await;
    assert!(
        !write.is_finished(),
        "an auto-commit write must wait behind the open writer"
    );

    b.commit(&sa, &a).await.unwrap();
    tokio::time::timeout(Duration::from_secs(5), write)
        .await
        .expect("the write proceeds once the slot frees")
        .unwrap()
        .expect("queued writers never conflict");
    assert_eq!(scalar(&b, "MATCH (n:Item) RETURN count(n)"), 2);
    let t = tokio::time::timeout(
        Duration::from_millis(300),
        b.begin_transaction(&session(2), &BoltDict::new()),
    )
    .await
    .expect("the auto-commit write released the slot")
    .unwrap();
    b.rollback(&session(2), &t).await.unwrap();
}

#[tokio::test]
async fn auto_commit_data_write_times_out_like_any_writer_and_optimistic_skips_the_slot() {
    let q = backend(WriteConcurrency::Queue, 200, 0);
    let held = q
        .begin_transaction(&session(1), &BoltDict::new())
        .await
        .unwrap();
    let err = run_auto_write(&q).await.expect_err("slot is held");
    assert_eq!(
        code_of(&err),
        "Neo.TransientError.Transaction.LockAcquisitionTimeout"
    );
    assert_eq!(scalar(&q, "MATCH (n:Item) RETURN count(n)"), 0);
    q.rollback(&session(1), &held).await.unwrap();
    run_auto_write(&q).await.expect("slot is free");

    let o = backend(WriteConcurrency::Optimistic, 200, 0);
    let _open = o
        .begin_transaction(&session(1), &BoltDict::new())
        .await
        .unwrap();
    run_auto_write(&o)
        .await
        .expect("optimistic mode never waits");
}

/// Commit one `CREATE` on its own transaction: the competing writer that
/// lands between an auto-commit write's execution and its commit.
fn commit_competitor(b: &KgliteBackend) {
    let mut tx = b.session.begin();
    let working = tx.working_mut().expect("working copy");
    let params = HashMap::new();
    let opts = kglite::api::session::ExecuteOptions::new(&params);
    kglite::api::session::execute_mut(working, "CREATE (:Rival)", &opts).expect("competitor write");
    b.session.commit(tx, true);
}

#[tokio::test]
async fn optimistic_auto_commit_retries_a_lost_race_and_gives_up_after_three() {
    let b = backend(WriteConcurrency::Optimistic, 0, 0);
    let params = HashMap::new();
    let opts = kglite::api::session::ExecuteOptions::new(&params);

    // Two lost races, then the third attempt commits.
    let mut runs = 0;
    b.session
        .execute_auto_commit_observed(AUTO_WRITE, &opts, 3, &mut |attempt| {
            runs = attempt;
            if attempt <= 2 {
                commit_competitor(&b);
            }
        })
        .expect("the third attempt wins");
    assert_eq!(runs, 3);
    assert_eq!(scalar(&b, "MATCH (n:Item) RETURN count(n)"), 1);
    assert_eq!(scalar(&b, "MATCH (n:Rival) RETURN count(n)"), 2);

    // A race lost on every attempt surfaces the conflict, applying nothing.
    let err = b
        .session
        .execute_auto_commit_observed("CREATE (:Item {id: 3})", &opts, 3, &mut |_| {
            commit_competitor(&b)
        })
        .err()
        .expect("three lost races");
    assert_eq!(code_of(&kg_to_bolt(err)), OUTDATED);
    assert_eq!(scalar(&b, "MATCH (n:Item) RETURN count(n)"), 1);
    assert_eq!(scalar(&b, "MATCH (n:Rival) RETURN count(n)"), 5);
}

#[tokio::test]
async fn automatic_checkpoint_neither_waits_for_nor_blocks_the_slot_and_never_saves_uncommitted_work(
) {
    // A checkpoint saves the *published* graph under the session lock and
    // never takes the slot: an open writer's uncommitted work lives in its
    // transaction copy, outside what a save can see.
    let path = std::env::temp_dir().join(format!(
        "kglite-writer-queue-ckpt-{}-{}.kgl",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_nanos()
    ));
    let b = backend_at(path.clone(), WriteConcurrency::Queue, 5_000, 0);
    let state = b.checkpoint_state();
    let sa = session(1);
    let a = b.begin_transaction(&sa, &BoltDict::new()).await.unwrap();
    run(&b, &a, "CREATE (:Item {id: 1})").unwrap();
    let before = b.session.version();

    // A second writer is queued while the checkpoint runs.
    let b2 = Arc::clone(&b);
    let waiter = tokio::spawn(async move {
        let tx = b2
            .begin_transaction(&session(2), &BoltDict::new())
            .await
            .unwrap();
        b2.commit(&session(2), &tx).await.unwrap();
    });
    tokio::time::sleep(Duration::from_millis(100)).await;

    let (s, p, st) = (b.session_handle(), path.clone(), Arc::clone(&state));
    let outcome = tokio::time::timeout(
        Duration::from_secs(5),
        tokio::task::spawn_blocking(move || checkpoint_if_changed(&s, &p, &st)),
    )
    .await
    .expect("a checkpoint must not wait for the writer slot")
    .unwrap()
    .expect("checkpoint");
    assert!(matches!(outcome, CheckpointOutcome::Written(v) if v == before));
    assert_eq!(
        *state.lock().unwrap(),
        Some(before),
        "recorded version excludes the open transaction's work"
    );
    assert!(
        !waiter.is_finished(),
        "the checkpoint must not release the queue"
    );

    b.commit(&sa, &a).await.unwrap();
    tokio::time::timeout(Duration::from_secs(5), waiter)
        .await
        .expect("the queued writer proceeds after the holder commits")
        .unwrap();
    // The commit changed the graph, so the next checkpoint writes again.
    let (s, p, st) = (b.session_handle(), path.clone(), state);
    let next = tokio::task::spawn_blocking(move || checkpoint_if_changed(&s, &p, &st))
        .await
        .unwrap()
        .expect("checkpoint");
    assert!(matches!(next, CheckpointOutcome::Written(v) if v > before));
    let _ = std::fs::remove_file(&path);
}

/// A backend over a graph file durable at `full`, so auto-commit data writes
/// go through the engine's group-commit queue. The directory is removed on drop.
struct DurableBackend {
    backend: Arc<KgliteBackend>,
    dir: std::path::PathBuf,
}

impl Drop for DurableBackend {
    fn drop(&mut self) {
        let _ = std::fs::remove_dir_all(&self.dir);
    }
}

fn durable_full_backend(wait_ms: u64) -> DurableBackend {
    use kglite::api::durable::DurabilityLevel;
    use kglite::api::session::{open_path, OpenSpec};
    static SEQ: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);
    let dir = std::env::temp_dir().join(format!(
        "kglite-bolt-group-{}-{}",
        std::process::id(),
        SEQ.fetch_add(1, Ordering::Relaxed)
    ));
    std::fs::create_dir_all(&dir).unwrap();
    let path = dir.join("g.kgl");
    let spec = OpenSpec {
        durability: DurabilityLevel::Full,
        durability_explicit: true,
        lease_timeout: None,
        ..OpenSpec::writer()
    };
    let opened = open_path(&path, &spec).expect("open durable graph");
    let backend = Arc::new(
        KgliteBackend::new(
            opened.session,
            path,
            false,
            "127.0.0.1:0".into(),
            CsvImportPolicy::Denied,
            ServerIdentity::default(),
            None,
        )
        .with_writer_config(WriterConfig {
            mode: WriteConcurrency::Queue,
            wait_timeout: (wait_ms > 0).then(|| Duration::from_millis(wait_ms)),
            idle_timeout: None,
        }),
    );
    DurableBackend { backend, dir }
}

async fn auto_write(b: &KgliteBackend, n: usize, q: &str) -> Result<(), BoltError> {
    b.execute(&session(n), q, &HashMap::new(), &BoltDict::new(), None)
        .await
        .map(|_| ())
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn concurrent_auto_commit_writes_at_full_share_log_barriers() {
    let d = durable_full_backend(0);
    let b = &d.backend;
    auto_write(b, 0, "CREATE (:Warm)").await.unwrap();
    let writers = 48;
    let before = b.session.wal_barrier_count();
    let tasks: Vec<_> = (0..writers)
        .map(|i| {
            let b = Arc::clone(b);
            tokio::spawn(
                async move { auto_write(&b, i, &format!("CREATE (:Shared {{i: {i}}})")).await },
            )
        })
        .collect();
    for t in tasks {
        t.await.unwrap().expect("queued writers never conflict");
    }
    let barriers = b.session.wal_barrier_count() - before;
    assert_eq!(
        scalar(b, "MATCH (n:Shared) RETURN count(n)"),
        writers as i64
    );
    assert!(
        barriers < writers as u64,
        "{writers} commits took {barriers} barriers; none were shared"
    );
}

#[tokio::test]
async fn an_open_write_transaction_excludes_grouped_auto_commit_writes() {
    let d = durable_full_backend(0);
    let b = &d.backend;
    let sa = session(1);
    let tx = b.begin_transaction(&sa, &BoltDict::new()).await.unwrap();
    run(b, &tx, "CREATE (:Held)").unwrap();

    let b2 = Arc::clone(b);
    let write = tokio::spawn(async move { auto_write(&b2, 2, "CREATE (:Grouped)").await });
    tokio::time::sleep(Duration::from_millis(150)).await;
    assert!(
        !write.is_finished(),
        "a grouped write must wait behind the open writer"
    );
    b.commit(&sa, &tx)
        .await
        .expect("no grouped commit interleaved with the transaction");
    tokio::time::timeout(Duration::from_secs(5), write)
        .await
        .expect("the grouped write proceeds once the slot frees")
        .unwrap()
        .unwrap();
    assert_eq!(scalar(b, "MATCH (n) RETURN count(n)"), 2);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn write_transactions_commit_cleanly_amid_grouped_auto_commit_writes() {
    let d = durable_full_backend(0);
    let b = &d.backend;
    let grouped: Vec<_> = (0..24)
        .map(|i| {
            let b = Arc::clone(b);
            tokio::spawn(async move {
                auto_write(&b, 100 + i, &format!("CREATE (:Auto {{i: {i}}})")).await
            })
        })
        .collect();
    for r in 0..6 {
        write_once(b, &session(r), &format!("CREATE (:ViaTx {{r: {r}}})"))
            .await
            .expect("a queued transaction never conflicts");
    }
    for t in grouped {
        t.await.unwrap().unwrap();
    }
    assert_eq!(scalar(b, "MATCH (n:Auto) RETURN count(n)"), 24);
    assert_eq!(scalar(b, "MATCH (n:ViaTx) RETURN count(n)"), 6);
}