hawdb 0.40.0

An embedded Rust graph database for local data-plane workloads
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
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
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
// Copyright 2026 Nowledge
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Relational live row-overlay pressure drives automatic checkpoints (#1438).

use super::*;
use crate::Value;
use hawdb_storage::relational::{
    DEFAULT_MAX_RELATIONAL_ROW_CHANGES, DEFAULT_MAX_RELATIONAL_ROW_CHANGE_BYTES,
};

mod large_commit;

const ROWS: i64 = 512;
const BODY_BYTES: usize = 1024;

fn body(round: i64) -> String {
    format!("{round:08}").repeat(BODY_BYTES / 8)
}

fn config() -> DatabaseConfig {
    DatabaseConfig {
        // Neither age nor WAL bytes can trigger a checkpoint in this fixture.
        automatic_checkpoint_max_age: Duration::from_secs(3600),
        ..DatabaseConfig::default()
    }
}

fn load(db: &mut Database) {
    db.query_sql("CREATE TABLE hot (id BIGINT PRIMARY KEY, round BIGINT, body TEXT)")
        .unwrap();
    for id in 0..ROWS {
        db.query_sql_with_params(
            "INSERT INTO hot (id, round, body) VALUES ($1, $2, $3)",
            &[Value::Int(id), Value::Int(-1), Value::String(body(-1))],
        )
        .unwrap();
    }
    // Establish the canonical row base that the live overlay layers over.
    db.checkpoint().unwrap();
}

fn update_round(db: &mut Database, round: i64) -> Result<()> {
    let mut transaction = db.begin_transaction()?;
    transaction.query_sql_with_params(
        "UPDATE hot SET round = $1, body = $2 WHERE id >= 0",
        &[Value::Int(round), Value::String(body(round))],
    )?;
    transaction.commit().map(|_| ())
}

fn assert_round(rows: crate::executor::QueryRows, round: i64) {
    let rows = rows.into_rows();
    assert_eq!(rows.len(), ROWS as usize);
    for (id, row) in rows.iter().enumerate() {
        assert_eq!(row.get("id"), Some(&Value::Int(id as i64)));
        assert_eq!(row.get("round"), Some(&Value::Int(round)));
        assert_eq!(row.get("body"), Some(&Value::String(body(round))));
    }
}

fn live_rows(db: &Database) -> (usize, usize) {
    let report = db.storage_residency_report().unwrap().relational_rows;
    (report.live_entries, report.live_resident_bytes)
}

#[test]
fn steady_relational_updates_on_one_handle_never_exhaust_the_live_overlay() {
    let fixture = Fixture::new();
    let mut db = Database::open_with_config(&fixture.0, config()).unwrap();
    load(&mut db);
    let initial = db
        .automatic_checkpoint_report()
        .unwrap()
        .unwrap()
        .completed_checkpoints;
    let mut admitted_entries = 0usize;
    let mut peak = (0usize, 0usize);
    let mut previous_entries = 0usize;
    let mut drains = 0usize;
    // Each round rewrites every row. Without a drain, main rejects round 97
    // with the #1438 live-overlay admission error.
    let rounds = 400;
    let mut round = 0;
    let mut busy_retries = 0;
    while round < rounds {
        match update_round(&mut db, round) {
            Ok(()) => {}
            // The total capacity wait is bounded even while the owner makes
            // progress. This pre-WAL refusal applies no update; retry exactly
            // the same round without counting it as an admitted commit.
            Err(HawDBError::BranchBusy {
                resource: "automatic checkpoint capacity",
            }) if busy_retries < 32 => {
                busy_retries += 1;
                eprintln!(
                    "steady relational update round={round}, capacity_retry={busy_retries}; {:?}",
                    db.automatic_checkpoint_report()
                );
                continue;
            }
            Err(error) => panic!(
                "round {round} failed after {admitted_entries} admitted entries and \
                 {busy_retries} capacity retries: {error}; {:?}",
                db.automatic_checkpoint_report()
            ),
        }
        admitted_entries += ROWS as usize;
        let live = live_rows(&db);
        peak = (peak.0.max(live.0), peak.1.max(live.1));
        // Adopting a published drain empties the serving overlay.
        drains += usize::from(live.0 < previous_entries);
        previous_entries = live.0;
        round += 1;
    }
    eprintln!("steady relational updates: rounds={round}, capacity_retries={busy_retries}");
    // Payload bytes alone, before per-row allocation overhead, exceed the
    // default resident-byte envelope three times over.
    assert!(
        admitted_entries * BODY_BYTES > 3 * DEFAULT_MAX_RELATIONAL_ROW_CHANGE_BYTES,
        "fixture must exceed the live envelope several times"
    );
    assert!(peak.0 <= DEFAULT_MAX_RELATIONAL_ROW_CHANGES, "{peak:?}");
    assert!(
        peak.1 <= DEFAULT_MAX_RELATIONAL_ROW_CHANGE_BYTES,
        "{peak:?}"
    );
    let report = db.automatic_checkpoint_report().unwrap().unwrap();
    assert!(drains >= 2, "serving overlay must be folded: {report:?}");
    assert!(
        report.completed_checkpoints >= initial + 2,
        "live-overlay pressure must rotate checkpoints: {report:?}"
    );
    let query = "SELECT id, round, body FROM hot ORDER BY id";
    assert_round(db.query_sql(query).unwrap().rows, rounds - 1);
    drop(db);
    let mut reopened = Database::open(&fixture.0).unwrap();
    assert_round(reopened.query_sql(query).unwrap().rows, rounds - 1);
}

#[test]
fn relational_update_capacity_refusal_preserves_rows_and_can_retry() {
    let fixture = Fixture::new();
    let mut db = Database::open_with_config(&fixture.0, config()).unwrap();
    load(&mut db);
    let suspension = db.runtime.suspend_automatic_checkpoint().unwrap().unwrap();
    let control = db.runtime.checkpoint_control_for_test();
    let mut round = 0;
    while !preparation_needs_capacity(&control.lock().unwrap()) {
        assert!(round < 100, "fixture must reach capacity pressure");
        update_round(&mut db, round).unwrap();
        round += 1;
    }
    let (sealed, observed) = std::sync::mpsc::channel();
    let (resume, continuation) = std::sync::mpsc::channel();
    control.lock().unwrap().prefix_seal_probe = Some(Arc::new(PrefixSealProbe {
        sealed,
        resume: Mutex::new(continuation),
    }));
    drop(suspension);
    observed.recv_timeout(Duration::from_secs(15)).unwrap();
    let before = db
        .runtime
        .peek()
        .unwrap()
        .store
        .checkpoint_source_identity();
    let live_before = live_rows(&db);
    // The real owner is parked, so the production capacity deadline must refuse
    // this update independently of how quickly the host normally checkpoints.
    let result = update_round(&mut db, round);
    let after = db
        .runtime
        .peek()
        .unwrap()
        .store
        .checkpoint_source_identity();
    let live_after = live_rows(&db);
    // Release the owner before assertions or cleanup can wait on it.
    control.lock().unwrap().prefix_seal_probe = None;
    resume.send(()).unwrap();
    assert!(matches!(
        result,
        Err(HawDBError::BranchBusy {
            resource: "automatic checkpoint capacity",
        })
    ));
    assert_eq!(before, after, "refusal must not append WAL or publish data");
    assert_eq!(live_before, live_after, "refusal must not grow the overlay");
    db.checkpoint().unwrap();
    let query = "SELECT id, round, body FROM hot ORDER BY id";
    assert_round(db.query_sql(query).unwrap().rows, round - 1);
    update_round(&mut db, round).unwrap();
    assert_round(db.query_sql(query).unwrap().rows, round);
    drop(db);
    let mut reopened = Database::open(&fixture.0).unwrap();
    assert_round(reopened.query_sql(query).unwrap().rows, round);
}

fn debt(db: &Database) -> CheckpointDebtSnapshot {
    db.runtime
        .peek()
        .unwrap()
        .store
        .checkpoint_debt_snapshot()
        .unwrap()
}

#[test]
fn checkpoint_debt_reports_the_live_overlay_envelope_and_checkpoint_folds_it() {
    let fixture = Fixture::new();
    let mut db = Database::open_with_config(&fixture.0, config()).unwrap();
    load(&mut db);
    assert_eq!(debt(&db).relational_live.row_entries, 0);
    for round in 0..3 {
        update_round(&mut db, round).unwrap();
    }
    let live = debt(&db).relational_live;
    let (entries, resident_bytes) = live_rows(&db);
    assert_eq!(live.row_entries, 3 * ROWS as u64);
    assert_eq!(live.row_entries, entries as u64);
    assert_eq!(live.row_resident_bytes, resident_bytes as u64);
    assert_eq!(
        live.max_row_entries,
        Some(DEFAULT_MAX_RELATIONAL_ROW_CHANGES as u64)
    );
    assert_eq!(
        live.max_row_resident_bytes,
        Some(DEFAULT_MAX_RELATIONAL_ROW_CHANGE_BYTES as u64)
    );
    db.checkpoint().unwrap();
    let folded = debt(&db).relational_live;
    assert_eq!((folded.row_entries, folded.row_resident_bytes), (0, 0));
    assert_eq!(live_rows(&db), (0, 0));
    assert_round(
        db.query_sql("SELECT id, round, body FROM hot ORDER BY id")
            .unwrap()
            .rows,
        2,
    );
}

#[test]
fn live_overlay_pressure_uses_the_shared_soft_and_capacity_boundaries() {
    const LIMIT: u64 = 100_000;
    let soft = LIMIT * u64::from(hawdb_storage::pressure::STORAGE_PRESSURE_SOFT_RATIO_PER_MILLION)
        / 1_000_000;
    let defer = LIMIT
        * u64::from(hawdb_storage::pressure::STORAGE_PRESSURE_DEFER_RATIO_PER_MILLION)
        / 1_000_000;
    let capacity = defer - LIMIT / 10;
    let with = |pair: usize, retained: u64, limit: Option<u64>| {
        let mut live = hawdb_storage::store::RelationalLiveOverlayDebt::default();
        let slot = match pair {
            0 => (&mut live.row_entries, &mut live.max_row_entries),
            1 => (
                &mut live.row_resident_bytes,
                &mut live.max_row_resident_bytes,
            ),
            2 => (&mut live.index_entries, &mut live.max_index_entries),
            _ => (
                &mut live.index_encoded_bytes,
                &mut live.max_index_encoded_bytes,
            ),
        };
        *slot.0 = retained;
        *slot.1 = limit;
        State {
            debt: Some(CheckpointDebtSnapshot {
                commit_epoch: 2,
                checkpoint_commit_epoch: 1,
                wal_generation: 1,
                wal_bytes: 0,
                wal_age_millis: 0,
                max_wal_bytes: Some(u64::MAX),
                max_wal_record_bytes: Some(1),
                delta_bytes: 0,
                max_delta_bytes: None,
                read_only: false,
                wal_sync_group_active: false,
                relational_live: live,
            }),
            ..State::default()
        }
    };
    for pair in 0..4 {
        let below = with(pair, soft - 1, Some(LIMIT));
        assert!(!soft_pressure(below.debt.unwrap()), "pair {pair}");
        let at = with(pair, soft, Some(LIMIT));
        assert!(soft_pressure(at.debt.unwrap()), "pair {pair}");
        assert!(!needs_headroom(&at) && !preparation_needs_capacity(&at));
        let below = with(pair, capacity - 1, Some(LIMIT));
        assert!(!needs_headroom(&below) && !preparation_needs_capacity(&below));
        let at = with(pair, capacity, Some(LIMIT));
        assert!(
            needs_headroom(&at) && preparation_needs_capacity(&at),
            "pair {pair}"
        );
        // No serving live view has no live-overlay envelope to exhaust.
        let unbounded = with(pair, LIMIT, None);
        assert!(!soft_pressure(unbounded.debt.unwrap()));
        assert!(!needs_headroom(&unbounded) && !preparation_needs_capacity(&unbounded));
    }
    // The recovery delta that the next drain captures with the live overlay
    // counts toward both row pairs (#1464).
    for pair in 0..2 {
        let split = |delta: u64| {
            let mut state = with(pair, soft / 2, Some(LIMIT));
            let live = &mut state.debt.as_mut().unwrap().relational_live;
            if pair == 0 {
                live.row_recovery_delta_entries = delta;
            } else {
                live.row_recovery_delta_resident_bytes = delta;
            }
            state
        };
        assert!(!soft_pressure(split(soft - soft / 2 - 1).debt.unwrap()));
        assert!(soft_pressure(split(soft - soft / 2).debt.unwrap()));
        // Writers fence for an idle owner to start that drain. A running drain
        // already captured the delta, so its capacity wait counts live rows.
        let at = split(capacity - soft / 2);
        assert!(
            needs_headroom(&at) && !preparation_needs_capacity(&at),
            "pair {pair}"
        );
    }
}

#[test]
fn pinned_reader_survives_pressure_triggered_live_overlay_drains() {
    let fixture = Fixture::new();
    let mut db = Database::open_with_config(&fixture.0, config()).unwrap();
    load(&mut db);
    update_round(&mut db, 0).unwrap();
    let pinned = db.begin_read_transaction().unwrap();
    let initial = db
        .automatic_checkpoint_report()
        .unwrap()
        .unwrap()
        .completed_checkpoints;
    let query = "SELECT id, round, body FROM hot ORDER BY id";
    let mut round = 1;
    while db
        .automatic_checkpoint_report()
        .unwrap()
        .unwrap()
        .completed_checkpoints
        < initial + 2
    {
        assert!(round < 1_000, "live-overlay pressure must drain twice");
        update_round(&mut db, round).unwrap();
        round += 1;
    }
    assert_round(pinned.query_sql(query).unwrap().rows, 0);
    assert_round(db.query_sql(query).unwrap().rows, round - 1);
    drop(pinned);
    update_round(&mut db, round).unwrap();
    assert_round(db.query_sql(query).unwrap().rows, round);
}

const CRASH_CHILD_ENV: &str = "HAWDB_TEST_LIVE_OVERLAY_CRASH_CHILD";
const CRASH_PATH_ENV: &str = "HAWDB_TEST_LIVE_OVERLAY_CRASH_PATH";
const CRASH_POINT_ENV: &str = "HAWDB_TEST_PROCESS_CRASH_POINT";

fn acknowledged_path(root: &std::path::Path) -> PathBuf {
    root.with_extension("acknowledged")
}

#[test]
fn pressure_drain_crash_child() {
    if std::env::var_os(CRASH_CHILD_ENV).is_none() {
        return;
    }
    let root = PathBuf::from(std::env::var_os(CRASH_PATH_ENV).expect("crash database path"));
    let point = std::env::var(CRASH_POINT_ENV).expect("crash point");
    let mut db = Database::open_with_config(&root, config()).unwrap();
    // The only checkpoint this child can publish is the pressure-triggered
    // one; its publication failpoint terminates the process mid-drain.
    for round in 0..1_000 {
        update_round(&mut db, round).unwrap();
        std::fs::write(acknowledged_path(&root), round.to_string()).unwrap();
    }
    panic!("crash failpoint {point} was not reached by a pressure-triggered drain");
}

#[test]
fn crash_during_pressure_triggered_drain_recovers_whole_acknowledged_rounds() {
    for point in [
        "during_checkpoint_publication",
        "after_manifest_publication",
    ] {
        let fixture = Fixture::new();
        {
            let mut db = Database::open_with_config(&fixture.0, config()).unwrap();
            load(&mut db);
        }
        let status = std::process::Command::new(std::env::current_exe().unwrap())
            .arg("--exact")
            .arg(
                "api::automatic_checkpoint::tests::relational_live_overlay::pressure_drain_crash_child",
            )
            .arg("--nocapture")
            .env(CRASH_CHILD_ENV, "1")
            .env(CRASH_PATH_ENV, &fixture.0)
            .env(CRASH_POINT_ENV, point)
            .status()
            .unwrap();
        assert_eq!(status.code(), Some(86), "child did not stop at {point}");
        let acknowledged: i64 = std::fs::read_to_string(acknowledged_path(&fixture.0))
            .unwrap()
            .parse()
            .unwrap();
        let _ = std::fs::remove_file(acknowledged_path(&fixture.0));
        assert!(
            acknowledged as usize * ROWS as usize * BODY_BYTES
                > DEFAULT_MAX_RELATIONAL_ROW_CHANGE_BYTES / 2,
            "the drain must start under live-overlay pressure ({acknowledged} rounds at {point})"
        );
        let mut reopened = Database::open_with_config(&fixture.0, config()).unwrap();
        let rows = reopened
            .query_sql("SELECT id, round, body FROM hot ORDER BY id")
            .unwrap()
            .rows;
        // Every acknowledged round is durable; at most the one in flight when
        // the process stopped may also be present, and never partially.
        let recovered = match rows.get(0, "round") {
            Some(hawdb_core::ValueRef::Int(round)) => round,
            value => panic!("unexpected recovered round {value:?} after {point}"),
        };
        assert!(
            (acknowledged..=acknowledged + 1).contains(&recovered),
            "recovered round {recovered}, acknowledged {acknowledged} at {point}"
        );
        assert_round(rows, recovered);
        // The recovered handle keeps draining under the same pressure. A
        // slow checked-profile drain may exhaust the bounded capacity wait;
        // that refusal is pre-WAL and retryable, just as it is for the
        // growing-table fixture below.
        let next = recovered + 1;
        let mut busy_retries = 0;
        for round in next..next + 160 {
            loop {
                match update_round(&mut reopened, round) {
                    Ok(()) => break,
                    Err(HawDBError::BranchBusy {
                        resource: "automatic checkpoint capacity",
                    }) if busy_retries < 32 => busy_retries += 1,
                    Err(error) => panic!(
                        "post-recovery round {round} failed after {busy_retries} capacity retries at {point}: {error}; {:?}",
                        reopened.automatic_checkpoint_report()
                    ),
                }
            }
        }
        assert_round(
            reopened
                .query_sql("SELECT id, round, body FROM hot ORDER BY id")
                .unwrap()
                .rows,
            next + 159,
        );
    }
}

const ORDER_LINES_PER_COMMIT: i64 = 2_500;

fn create_order_line(db: &mut Database) {
    db.query_sql(
        "CREATE TABLE order_line (ol_o_id BIGINT NOT NULL, ol_d_id BIGINT NOT NULL, \
         ol_w_id BIGINT NOT NULL, ol_number BIGINT NOT NULL, ol_i_id BIGINT, \
         ol_supply_w_id BIGINT, ol_delivery_d TEXT, ol_quantity BIGINT, \
         ol_amount DOUBLE PRECISION, ol_dist_info TEXT, \
         PRIMARY KEY (ol_w_id, ol_d_id, ol_o_id, ol_number))",
    )
    .unwrap();
    db.query_sql("CREATE INDEX idx_order_line_order ON order_line (ol_w_id, ol_d_id, ol_o_id)")
        .unwrap();
}

/// TPC-C ORDER_LINE-shaped row `line` (about 120 encoded bytes): ten lines
/// per order, 3,000 orders per district.
fn order_line(line: i64) -> String {
    let (order, number) = (line / 10, line % 10);
    format!(
        "({}, {}, 1, {number}, {}, 1, '2026-11-01 00:00:00', 5, 12.5, '{}')",
        order % 3_000,
        order / 3_000 + 1,
        line * 7_919 % 100_000,
        "x".repeat(24),
    )
}

fn insert_order_lines(db: &mut Database, commit: i64) -> Result<()> {
    let lines = (commit * ORDER_LINES_PER_COMMIT..(commit + 1) * ORDER_LINES_PER_COMMIT)
        .map(order_line)
        .collect::<Vec<_>>()
        .join(", ");
    let mut transaction = db.begin_transaction()?;
    transaction.query_sql(&format!(
        "INSERT INTO order_line (ol_o_id, ol_d_id, ol_w_id, ol_number, ol_i_id, \
         ol_supply_w_id, ol_delivery_d, ol_quantity, ol_amount, ol_dist_info) VALUES {lines}"
    ))?;
    transaction.commit().map(|_| ())
}

/// Exact totals by full scan. Call it on a folded base: one full scan merges
/// at most the row snapshot's per-scan overlay-entry bound.
fn assert_order_line_totals(db: &mut Database, lines: i64) {
    let rows = db
        .query_sql(
            "SELECT COUNT(*) AS n, SUM(ol_i_id) AS items, MAX(ol_d_id) AS districts \
             FROM order_line",
        )
        .unwrap()
        .rows
        .into_rows();
    assert_eq!(rows[0].get("n"), Some(&Value::Int(lines)));
    assert_eq!(
        rows[0].get("items"),
        Some(&Value::Int(
            (0..lines).map(|line| line * 7_919 % 100_000).sum::<i64>()
        ))
    );
    assert_eq!(
        rows[0].get("districts"),
        Some(&Value::Int((lines - 1) / 10 / 3_000 + 1))
    );
}

/// Point and secondary-index reads through the serving overlay.
fn assert_order_lines(db: &mut Database, lines: i64) {
    for line in [0, lines / 2, lines - 1] {
        let (order, number) = (line / 10, line % 10);
        let (district, order) = (order / 3_000 + 1, order % 3_000);
        let point = db
            .query_sql(&format!(
                "SELECT ol_i_id FROM order_line WHERE ol_w_id = 1 AND ol_d_id = {district} \
                 AND ol_o_id = {order} AND ol_number = {number}"
            ))
            .unwrap()
            .rows
            .into_rows();
        assert_eq!(point.len(), 1, "line {line}");
        assert_eq!(
            point[0].get("ol_i_id"),
            Some(&Value::Int(line * 7_919 % 100_000))
        );
        let order_lines = db
            .query_sql(&format!(
                "SELECT ol_number FROM order_line WHERE ol_w_id = 1 AND ol_d_id = {district} \
                 AND ol_o_id = {order}"
            ))
            .unwrap()
            .rows
            .into_rows();
        assert_eq!(order_lines.len(), 10, "order of line {line}");
    }
}

#[test]
fn steady_inserts_into_a_growing_table_keep_draining_the_live_overlay() {
    let fixture = Fixture::new();
    let mut db = Database::open_with_config(&fixture.0, config()).unwrap();
    create_order_line(&mut db);
    let initial = db
        .automatic_checkpoint_report()
        .unwrap()
        .unwrap()
        .completed_checkpoints;
    let mut commits = 0;
    let mut peak_live = (0usize, 0usize);
    let mut peak_live_debt = [0u64; 4];
    let mut max_delta_entries = 0;
    let mut busy_retries = 0;
    let started = Instant::now();
    let mut last_log = started;
    let mut logged_checkpoints = initial;
    let initial_progress = db
        .runtime
        .checkpoint_control_for_test()
        .work_progress
        .load(Ordering::Relaxed);
    // Each drain folds about 47,000 lines (70% of the resident-byte envelope).
    // On c8d767cd the second drain's capture of the recovery delta plus the
    // live overlay is rejected three times, which stops automatic retries;
    // commit 48 (epoch 52) then fails with the #1464 admission error.
    while {
        let report = db.automatic_checkpoint_report().unwrap().unwrap();
        let continuing = report.completed_checkpoints < initial + 5;
        if continuing
            && (commits == 0
                || report.completed_checkpoints != logged_checkpoints
                || last_log.elapsed() >= Duration::from_secs(5))
        {
            let control = db.runtime.checkpoint_control_for_test();
            let diagnostics = {
                let state = control.state.try_lock().ok();
                wait_diagnostics(&control, state.as_deref(), started, initial_progress)
            };
            eprintln!(
                "steady insert commits={commits}, busy_retries={busy_retries}; {diagnostics}; \
                 report={report:?}; live={:?}",
                debt(&db).relational_live,
            );
            last_log = Instant::now();
            logged_checkpoints = report.completed_checkpoints;
        }
        continuing
    } {
        assert!(commits < 240, "live-overlay pressure must drain five times");
        match insert_order_lines(&mut db, commits) {
            Ok(()) => commits += 1,
            // Near the envelope a writer waits for the running drain. When
            // that drain makes no builder progress for the bounded wait (an
            // unoptimized build can take that long between work units), the
            // commit is rejected before WAL append with nothing applied, and
            // the writer retries it. A live-overlay admission error never is.
            Err(HawDBError::BranchBusy {
                resource: "automatic checkpoint capacity",
            }) if busy_retries < 100 => busy_retries += 1,
            Err(error) => panic!(
                "commit {commits} failed after {busy_retries} busy retries: {error}; {:?}; {:?}",
                debt(&db).relational_live,
                db.automatic_checkpoint_report()
            ),
        }
        let live = live_rows(&db);
        peak_live = (peak_live.0.max(live.0), peak_live.1.max(live.1));
        let overlay = debt(&db).relational_live;
        // The previous drain's catch-up suffix is base-relative debt for the
        // next one, charged at the live-overlay rate of 997 bytes per line.
        assert_eq!(
            overlay.row_recovery_delta_resident_bytes,
            overlay.row_recovery_delta_entries * 997
        );
        max_delta_entries = max_delta_entries.max(overlay.row_recovery_delta_entries);
        // Live occupancy never reaches the admission envelope. While a drain
        // runs, the delta it folds still counts in `pairs()`; that drain's
        // capture was admitted when it started (no failed attempt below).
        for (peak, (retained, limit)) in peak_live_debt.iter_mut().zip(overlay.admission_pairs()) {
            *peak = (*peak).max(retained);
            assert!(
                limit.is_none_or(|limit| retained < limit),
                "{retained} {limit:?}"
            );
        }
    }
    let report = db.automatic_checkpoint_report().unwrap().unwrap();
    eprintln!(
        "steady insert verification elapsed={:?}, commits={commits}, \
         busy_retries={busy_retries}, report={report:?}",
        started.elapsed(),
    );
    assert!(max_delta_entries > 0, "drains must overlap later commits");
    // Every rejected attempt left no trace: the table holds exactly the
    // acknowledged commits (checked below and after reopen).
    assert_eq!(report.failed_attempts, 0, "{report:?}");
    assert!(!report.operation_retry_exhausted, "{report:?}");
    assert!(
        peak_live.0 <= DEFAULT_MAX_RELATIONAL_ROW_CHANGES,
        "{peak_live:?}"
    );
    assert!(
        peak_live.1 <= DEFAULT_MAX_RELATIONAL_ROW_CHANGE_BYTES,
        "{peak_live:?}"
    );
    let lines = commits * ORDER_LINES_PER_COMMIT;
    assert!(
        lines as usize > 2 * DEFAULT_MAX_RELATIONAL_ROW_CHANGES,
        "the table must outgrow the live envelope several times: {lines}, {peak_live_debt:?}"
    );
    assert_order_lines(&mut db, lines);
    drop(db);
    let mut reopened = Database::open_with_config(&fixture.0, config()).unwrap();
    assert_order_lines(&mut reopened, lines);
    // The reopened handle keeps admitting inserts; folding everything into
    // one base then gives exact totals.
    insert_order_lines(&mut reopened, commits).unwrap();
    let lines = lines + ORDER_LINES_PER_COMMIT;
    assert_order_lines(&mut reopened, lines);
    reopened.checkpoint().unwrap();
    assert_order_line_totals(&mut reopened, lines);
}

/// Places a regular file where the next checkpoint generation stages its
/// private directory. Every automatic preparation then fails with an
/// operation error before any selector or WAL mutation, as a transient
/// filesystem fault (ENOSPC, EIO) would. Removing the file clears the fault.
fn obstruct_next_checkpoint(db: &mut Database) -> std::path::PathBuf {
    let store = &db.runtime.get().unwrap().store;
    let root = store.durable_root_path().unwrap().to_path_buf();
    let generation = store
        .checkpoint_debt_snapshot()
        .unwrap()
        .wal_generation
        .checked_add(1)
        .unwrap();
    let obstruction = root.join(format!(".checkpoint.{generation}.prepare"));
    std::fs::write(&obstruction, b"not a directory").unwrap();
    obstruction
}

/// The #1618 workload with its trigger injected: steady small inserts on one
/// handle while every checkpoint operation fails. Three failures stop the
/// circuit; on main nothing restarts it while the handle stays open, the
/// overlay fills and commit 26 is rejected with the non-retryable live-view
/// admission error. Here writers that need headroom wait (or get the
/// retryable `BranchBusy`) and re-arm one attempt per backoff, so the debt
/// never outgrows one drain; once the fault clears, the next re-armed
/// attempt drains and the circuit resets.
#[test]
fn steady_inserts_outlast_a_transient_checkpoint_fault_and_keep_draining() {
    let fixture = Fixture::new();
    let mut db = Database::open_with_config(&fixture.0, config()).unwrap();
    create_order_line(&mut db);
    let obstruction = obstruct_next_checkpoint(&mut db);
    let initial = db
        .automatic_checkpoint_report()
        .unwrap()
        .unwrap()
        .completed_checkpoints;
    let mut commits = 0;
    let mut busy_retries = 0;
    let mut cleared_at = None;
    loop {
        let report = db.automatic_checkpoint_report().unwrap().unwrap();
        if report.completed_checkpoints >= initial + 2 {
            break;
        }
        // One re-armed attempt has failed too: the fault is what stops the
        // circuit, not the first three failures alone.
        if cleared_at.is_none() && report.operation_failures > u64::from(MAX_OPERATION_FAILURES) {
            assert!(report.operation_retry_rearms >= 1, "{report:?}");
            assert_eq!(report.completed_checkpoints, initial, "{report:?}");
            std::fs::remove_file(&obstruction).unwrap();
            cleared_at = Some(commits);
        }
        assert!(commits < 120, "the overlay must drain twice: {report:?}");
        match insert_order_lines(&mut db, commits) {
            Ok(()) => commits += 1,
            // Nothing is applied; the writer retries. A live-overlay
            // admission error, which no retry can clear, never appears.
            Err(HawDBError::BranchBusy {
                resource: "automatic checkpoint capacity",
            }) if busy_retries < 200 => busy_retries += 1,
            Err(error) => panic!(
                "commit {commits} failed after {busy_retries} busy retries: {error}; {:?}; {:?}",
                debt(&db).relational_live,
                db.automatic_checkpoint_report()
            ),
        }
        let overlay = debt(&db).relational_live;
        for (retained, limit) in overlay.admission_pairs() {
            assert!(
                limit.is_none_or(|limit| retained < limit),
                "{retained} {limit:?}"
            );
        }
        if db
            .automatic_checkpoint_report()
            .unwrap()
            .unwrap()
            .completed_checkpoints
            == initial
        {
            // While the circuit is stopped, the base-relative debt that the
            // recovering drain must capture stays below the envelope too.
            for (retained, limit) in overlay.pairs() {
                assert!(
                    limit.is_none_or(|limit| retained < limit),
                    "{retained} {limit:?}"
                );
            }
        }
    }
    let report = db.automatic_checkpoint_report().unwrap().unwrap();
    eprintln!(
        "transient fault: commits={commits}, cleared_at={cleared_at:?}, \
         busy_retries={busy_retries}, report={report:?}"
    );
    assert!(cleared_at.is_some(), "{report:?}");
    assert!(!report.operation_retry_exhausted, "{report:?}");
    assert!(report.operation_retry_rearms >= 1, "{report:?}");
    // Re-arms grant one attempt each: the failures are the first three plus
    // one per re-arm, never a free-running retry loop.
    assert!(
        report.operation_failures
            <= u64::from(MAX_OPERATION_FAILURES) + report.operation_retry_rearms,
        "{report:?}"
    );
    assert!(report.last_operation_error.is_some(), "{report:?}");
    let lines = commits * ORDER_LINES_PER_COMMIT;
    assert_order_lines(&mut db, lines);
    drop(db);
    let mut reopened = Database::open_with_config(&fixture.0, config()).unwrap();
    assert_order_lines(&mut reopened, lines);
    reopened.checkpoint().unwrap();
    assert_order_line_totals(&mut reopened, lines);
}