holt 0.8.2

An adaptive-radix-tree metadata storage engine for path-shaped keys, with per-blob concurrency and crash-safe persistence.
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
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
//! I/O worker thread — drains the bounded queue and runs
//! `store.write_blobs` / `store.flush` on behalf of the
//! checkpoint planner.
//!
//! ## Why a separate thread
//!
//! Decouples I/O execution from planning so the planner can:
//! 1. Snapshot bytes under a brief shared read guard, then move on.
//! 2. Enqueue checkpoint epochs without waiting for data writes.
//! 3. Keep each epoch's write/sync/report boundary explicit, so
//!    WAL truncation can advance strictly in checkpoint FIFO order.
//!
//! For the current local-`pread`/`pwrite` store the parallelism
//! gain is modest (single thread, single FD). On Linux with the
//! `io-uring` feature, the I/O thread owns the SQ submit / CQ
//! drain path and feeds the ring with whole checkpoint batches.
//!
//! ## Shutdown
//!
//! The thread terminates on receiving [`IoTask::Stop`]. The
//! `Checkpointer` orchestrator sends one at the end of its `Drop`
//! sequence, after the final synchronous round has drained
//! everything through this same queue.

use crossbeam_channel::{Receiver, Sender};
use std::collections::{HashMap, HashSet, VecDeque};
use std::sync::Arc;

use crate::api::errors::{Error, Result};
use crate::engine;
use crate::layout::BlobGuid;
use crate::store::blob_store::BlobStore;
use crate::store::{
    BlobFrameRef, BufferManager, WriteThroughEntry, WriteThroughStatus, STRUCTURAL_SEQ,
};

use super::Shared;

/// One checkpoint epoch after the planner has drained dirty /
/// pending-delete state and cloned dirty blob bytes.
pub(crate) struct CheckpointEpoch {
    pub(crate) entries: Vec<WriteThroughEntry>,
    pub(crate) pending: HashMap<BlobGuid, u64>,
}

/// Completion payload for a checkpoint epoch.
pub(crate) struct CheckpointEpochReport {
    pub(crate) dirty_total: usize,
    pub(crate) dirty_flushed: usize,
    pub(crate) pending_total: usize,
    pub(crate) applied_deletes: usize,
    pub(crate) result: Result<()>,
}

pub(crate) type CheckpointEpochCompletion = Sender<CheckpointEpochReport>;

/// Work item handed to the I/O thread via the bounded queue.
pub(crate) enum IoTask {
    /// Commit one checkpoint epoch: write dirty blob images,
    /// run the pre-delete store sync, apply pending delete fences,
    /// then run the post-delete sync when a manifest delete applied.
    CommitEpoch {
        epoch: CheckpointEpoch,
        on_done: CheckpointEpochCompletion,
    },
    /// Graceful stop signal. Sent once during `Checkpointer::Drop`
    /// after the planner has joined and the final round has run.
    Stop,
}

#[derive(Clone, Copy)]
struct EpochProgress {
    dirty_total: usize,
    pending_total: usize,
}

struct BatchEntry {
    epoch_idx: usize,
    guid: BlobGuid,
    expected_seq: u64,
    entry: Option<WriteThroughEntry>,
    children: Vec<BlobGuid>,
    flushed: bool,
}

struct BatchWriteReport {
    dirty_flushed_by_epoch: Vec<usize>,
    deferred: bool,
}

struct PendingDeleteReport {
    per_epoch_failed: Vec<HashMap<BlobGuid, u64>>,
    per_epoch_first_err: Vec<Option<Error>>,
    manifest_deleted_total: usize,
}

/// Main loop for the I/O thread.
pub(crate) fn run(shared: &Arc<Shared>, rx: Receiver<IoTask>) {
    while let Ok(task) = rx.recv() {
        match task {
            IoTask::CommitEpoch { mut epoch, on_done } => {
                let mut reports = commit_epoch_batch(shared, std::slice::from_mut(&mut epoch));
                let _ = on_done.send(reports.remove(0));
            }
            IoTask::Stop => break,
        }
    }
}

fn commit_epoch_batch(
    shared: &Arc<Shared>,
    epochs: &mut [CheckpointEpoch],
) -> Vec<CheckpointEpochReport> {
    // Serialize the complete write/sync/delete/sync transaction with manual
    // checkpoints. A stale older epoch must never overwrite a newer durable
    // parent after that newer epoch has already deleted one of its children.
    let _checkpoint_io = shared.bm.enter_checkpoint_io();
    let mut progresses = Vec::with_capacity(epochs.len());
    let mut entries = Vec::new();
    let mut collect_error = None;
    for (epoch_idx, epoch) in epochs.iter_mut().enumerate() {
        progresses.push(EpochProgress {
            dirty_total: epoch.entries.len(),
            pending_total: epoch.pending.len(),
        });
        for entry in epoch.entries.drain(..) {
            let children = match collect_entry_children(&entry) {
                Ok(children) => children,
                Err(e) => {
                    collect_error.get_or_insert(e);
                    Vec::new()
                }
            };
            entries.push(BatchEntry {
                epoch_idx,
                guid: entry.guid,
                expected_seq: entry.expected_seq,
                entry: Some(entry),
                children,
                flushed: false,
            });
        }
    }
    if let Some(e) = collect_error {
        restore_batch_entries(shared, &entries);
        restore_all_pending(shared, epochs);
        return reports_with_error(&progresses, vec![0; progresses.len()], e);
    }

    let dirty_flushed_by_epoch = if entries.is_empty() {
        if let Err(e) = shared.bm.flush_inner() {
            restore_all_pending(shared, epochs);
            return reports_with_error(&progresses, vec![0; progresses.len()], e);
        }
        vec![0; epochs.len()]
    } else {
        match write_entries_in_dependency_order(&shared.bm, &mut entries, epochs.len()) {
            Ok(report) => {
                if report.deferred {
                    restore_unflushed_batch_entries(shared, &entries);
                    restore_all_pending(shared, epochs);
                    return reports_without_delete_phase(
                        &progresses,
                        report.dirty_flushed_by_epoch,
                    );
                }
                report.dirty_flushed_by_epoch
            }
            Err(e) => {
                restore_batch_entries(shared, &entries);
                restore_all_pending(shared, epochs);
                return reports_with_error(&progresses, vec![0; progresses.len()], e);
            }
        }
    };

    let pending_report = apply_pending_deletes(shared, epochs);
    if pending_report.manifest_deleted_total > 0 {
        if let Err(e) = shared.bm.flush_inner() {
            restore_applied_manifest_deletes(shared, epochs, &pending_report.per_epoch_failed);
            return reports_with_error(&progresses, dirty_flushed_by_epoch, e);
        }
    }

    epochs
        .iter()
        .zip(progresses)
        .zip(dirty_flushed_by_epoch)
        .zip(pending_report.per_epoch_failed)
        .zip(pending_report.per_epoch_first_err)
        .map(
            |((((epoch, progress), dirty_flushed), failed), first_err)| CheckpointEpochReport {
                dirty_total: progress.dirty_total,
                dirty_flushed,
                pending_total: progress.pending_total,
                applied_deletes: epoch.pending.len() - failed.len(),
                result: first_err.map_or(Ok(()), Err),
            },
        )
        .collect()
}

fn apply_pending_deletes(shared: &Arc<Shared>, epochs: &[CheckpointEpoch]) -> PendingDeleteReport {
    let mut per_epoch_failed = Vec::with_capacity(epochs.len());
    let mut per_epoch_first_err = Vec::with_capacity(epochs.len());
    let mut manifest_deleted_total = 0usize;
    for epoch in epochs {
        let mut pending_failed = HashMap::new();
        let mut first_pending_err = None;
        for (guid, seq) in &epoch.pending {
            match shared.bm.execute_pending_delete(*guid, *seq) {
                Ok(true) => {
                    if *seq != STRUCTURAL_SEQ {
                        manifest_deleted_total += 1;
                    }
                }
                Ok(false) => {
                    pending_failed.insert(*guid, *seq);
                }
                Err(e) => {
                    pending_failed.insert(*guid, *seq);
                    first_pending_err.get_or_insert(e);
                }
            }
        }
        if !pending_failed.is_empty() {
            shared.bm.restore_pending_deletes(pending_failed.clone());
        }
        per_epoch_failed.push(pending_failed);
        per_epoch_first_err.push(first_pending_err);
    }
    PendingDeleteReport {
        per_epoch_failed,
        per_epoch_first_err,
        manifest_deleted_total,
    }
}

fn collect_entry_children(entry: &WriteThroughEntry) -> Result<Vec<BlobGuid>> {
    let frame = BlobFrameRef::wrap(entry.bytes.as_slice());
    engine::collect_blob_children_from_frame(frame)
}

fn write_entries_in_dependency_order(
    bm: &Arc<BufferManager>,
    entries: &mut [BatchEntry],
    epoch_count: usize,
) -> Result<BatchWriteReport> {
    // A previous data/manifest operation may have left store-local flush
    // debt without a corresponding dirty BM entry. Child readiness treats
    // any such debt as non-durable, so merely deferring the parent would
    // reproduce the same empty dependency wave forever. Cross that frontier
    // explicitly before evaluating dependencies; failure is reported to the
    // planner, which restores every entry and pending delete for retry.
    if bm.needs_flush() {
        bm.flush_inner()?;
    }

    let mut remaining_by_guid = HashMap::<BlobGuid, usize>::new();
    for entry in entries.iter() {
        *remaining_by_guid.entry(entry.guid).or_insert(0) += 1;
    }
    let mut durable_this_batch = HashSet::new();
    let mut dirty_flushed_by_epoch = vec![0; epoch_count];

    loop {
        let mut wave = Vec::new();
        for (idx, entry) in entries.iter().enumerate() {
            if !entry.flushed
                && children_ready(bm, &entry.children, &remaining_by_guid, &durable_this_batch)?
            {
                wave.push(idx);
            }
        }

        if wave.is_empty() {
            let deferred = entries.iter().any(|entry| !entry.flushed);
            if deferred {
                validate_no_progress_dependencies(
                    bm,
                    entries,
                    &remaining_by_guid,
                    &durable_this_batch,
                )?;
            }
            return Ok(BatchWriteReport {
                dirty_flushed_by_epoch,
                deferred,
            });
        }

        let wave_entries: Vec<_> = wave
            .iter()
            .map(|idx| {
                entries[*idx]
                    .entry
                    .take()
                    .expect("unflushed batch entry owns its write")
            })
            .collect();
        let report = bm.write_through_batch(&wave_entries)?;
        bm.flush_inner()?;

        let mut saw_stale = false;
        for (idx, status) in wave.into_iter().zip(report.statuses) {
            let entry = &mut entries[idx];
            match status {
                WriteThroughStatus::Written => {
                    entry.flushed = true;
                    dirty_flushed_by_epoch[entry.epoch_idx] += 1;
                    if let Some(count) = remaining_by_guid.get_mut(&entry.guid) {
                        *count -= 1;
                        if *count == 0 {
                            remaining_by_guid.remove(&entry.guid);
                        }
                    }
                    durable_this_batch.insert(entry.guid);
                }
                WriteThroughStatus::Stale => {
                    saw_stale = true;
                }
            }
        }
        if saw_stale {
            return Ok(BatchWriteReport {
                dirty_flushed_by_epoch,
                deferred: true,
            });
        }
    }
}

/// Classify a dependency planner stall. Only dependencies owned by dirty or
/// flushing work outside this batch may be retried: a missing durable child
/// and any cycle wholly inside the remaining batch are persistent corruption,
/// not scheduling backpressure.
fn validate_no_progress_dependencies(
    bm: &Arc<BufferManager>,
    entries: &[BatchEntry],
    remaining_by_guid: &HashMap<BlobGuid, usize>,
    durable_this_batch: &HashSet<BlobGuid>,
) -> Result<()> {
    let mut graph = HashMap::<BlobGuid, HashSet<BlobGuid>>::new();
    for guid in remaining_by_guid.keys().copied() {
        graph.entry(guid).or_default();
    }
    let mut external_transient = false;

    for entry in entries.iter().filter(|entry| !entry.flushed) {
        for child in &entry.children {
            if durable_this_batch.contains(child) {
                continue;
            }
            if remaining_by_guid.contains_key(child) {
                graph.entry(entry.guid).or_default().insert(*child);
                continue;
            }
            if bm.has_unflushed_blob(*child) {
                external_transient = true;
                continue;
            }
            if !bm.store_has_blob(*child)? {
                return Err(
                    Error::node_corrupt("checkpoint dependency references missing child")
                        .with_blob_guid(entry.guid),
                );
            }
            if !bm.store_has_durable_blob(*child)? {
                external_transient = true;
            }
        }
    }

    if let Some(guid) = dependency_cycle(&graph) {
        return Err(Error::node_corrupt("checkpoint dependency cycle").with_blob_guid(guid));
    }
    if external_transient {
        return Ok(());
    }
    Err(Error::Internal(
        "checkpoint dependency planner made no progress",
    ))
}

fn dependency_cycle(graph: &HashMap<BlobGuid, HashSet<BlobGuid>>) -> Option<BlobGuid> {
    // The graph is derived from potentially corrupt persisted frames, so its
    // depth is attacker-/corruption-controlled. Kahn's algorithm keeps the
    // diagnostic path iterative and bounded to O(V + E) heap memory instead
    // of risking a process stack overflow on a long acyclic chain.
    let mut indegree = graph
        .keys()
        .copied()
        .map(|guid| (guid, 0usize))
        .collect::<HashMap<_, _>>();
    for children in graph.values() {
        for child in children {
            if let Some(degree) = indegree.get_mut(child) {
                *degree = degree.saturating_add(1);
            }
        }
    }

    let mut ready = indegree
        .iter()
        .filter_map(|(guid, degree)| (*degree == 0).then_some(*guid))
        .collect::<VecDeque<_>>();
    let mut visited = 0usize;
    while let Some(guid) = ready.pop_front() {
        visited += 1;
        if let Some(children) = graph.get(&guid) {
            for child in children {
                let Some(degree) = indegree.get_mut(child) else {
                    continue;
                };
                *degree -= 1;
                if *degree == 0 {
                    ready.push_back(*child);
                }
            }
        }
    }

    (visited != indegree.len())
        .then(|| {
            indegree
                .into_iter()
                .find_map(|(guid, degree)| (degree != 0).then_some(guid))
        })
        .flatten()
}

fn children_ready(
    bm: &Arc<BufferManager>,
    children: &[BlobGuid],
    remaining_by_guid: &HashMap<BlobGuid, usize>,
    durable_this_batch: &HashSet<BlobGuid>,
) -> Result<bool> {
    for child in children {
        if durable_this_batch.contains(child) {
            continue;
        }
        if remaining_by_guid.contains_key(child) || bm.has_unflushed_blob(*child) {
            return Ok(false);
        }
        if !bm.store_has_durable_blob(*child)? {
            return Ok(false);
        }
    }
    Ok(true)
}

/// Write one synchronous checkpoint snapshot in the same child-before-parent
/// durability waves used by the background I/O worker.
///
/// Every returned entry was either stale or deferred because one of its child
/// references was not yet durable. The caller must restore those entries to
/// the dirty map and retry them in a later checkpoint round. The caller must
/// hold [`BufferManager::enter_checkpoint_io`] across this call and its
/// pending-delete phase.
pub(crate) fn write_entries_child_first(
    bm: &Arc<BufferManager>,
    entries: Vec<WriteThroughEntry>,
) -> Result<Vec<(BlobGuid, u64)>> {
    let mut batch_entries = Vec::with_capacity(entries.len());
    for entry in entries {
        let children = collect_entry_children(&entry)?;
        batch_entries.push(BatchEntry {
            epoch_idx: 0,
            guid: entry.guid,
            expected_seq: entry.expected_seq,
            entry: Some(entry),
            children,
            flushed: false,
        });
    }

    let _ = write_entries_in_dependency_order(bm, &mut batch_entries, 1)?;
    Ok(batch_entries
        .iter()
        .filter(|entry| !entry.flushed)
        .map(|entry| (entry.guid, entry.expected_seq))
        .collect())
}

fn restore_batch_entries(shared: &Arc<Shared>, entries: &[BatchEntry]) {
    if entries.is_empty() {
        return;
    }
    let mut failed = HashMap::with_capacity(entries.len());
    for entry in entries {
        failed.insert(entry.guid, entry.expected_seq);
    }
    shared.bm.restore_dirty(failed);
}

fn restore_unflushed_batch_entries(shared: &Arc<Shared>, entries: &[BatchEntry]) {
    let mut failed = HashMap::new();
    for entry in entries.iter().filter(|entry| !entry.flushed) {
        failed.insert(entry.guid, entry.expected_seq);
    }
    shared.bm.restore_dirty(failed);
}

fn restore_all_pending(shared: &Arc<Shared>, epochs: &mut [CheckpointEpoch]) {
    let mut all_pending = HashMap::new();
    for epoch in epochs {
        all_pending.extend(std::mem::take(&mut epoch.pending));
    }
    shared.bm.restore_pending_deletes(all_pending);
}

fn reports_without_delete_phase(
    progresses: &[EpochProgress],
    dirty_flushed_by_epoch: Vec<usize>,
) -> Vec<CheckpointEpochReport> {
    progresses
        .iter()
        .zip(dirty_flushed_by_epoch)
        .map(|(progress, dirty_flushed)| CheckpointEpochReport {
            dirty_total: progress.dirty_total,
            dirty_flushed,
            pending_total: progress.pending_total,
            applied_deletes: 0,
            result: Ok(()),
        })
        .collect()
}

fn restore_applied_manifest_deletes(
    shared: &Arc<Shared>,
    epochs: &[CheckpointEpoch],
    per_epoch_failed: &[HashMap<BlobGuid, u64>],
) {
    let mut all_applied = HashMap::new();
    for (epoch, failed) in epochs.iter().zip(per_epoch_failed) {
        all_applied.extend(
            epoch
                .pending
                .iter()
                .filter(|(guid, seq)| **seq != STRUCTURAL_SEQ && !failed.contains_key(*guid))
                .map(|(guid, seq)| (*guid, *seq)),
        );
    }
    shared.bm.restore_pending_deletes(all_applied);
}

fn reports_with_error(
    progresses: &[EpochProgress],
    dirty_flushed_by_epoch: Vec<usize>,
    first_error: Error,
) -> Vec<CheckpointEpochReport> {
    let mut first_error = Some(first_error);
    progresses
        .iter()
        .zip(dirty_flushed_by_epoch)
        .map(|(progress, dirty_flushed)| CheckpointEpochReport {
            dirty_total: progress.dirty_total,
            dirty_flushed,
            pending_total: progress.pending_total,
            applied_deletes: 0,
            result: Err(first_error
                .take()
                .unwrap_or(Error::Internal("checkpoint epoch write failed"))),
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::checkpoint::CheckpointConfig;
    use crate::concurrency::{CommitGate, Gate};
    use crate::layout::{BlobNode, NodeType};
    use crate::store::blob_store::{AlignedBlobBuf, BlobStore, FileBlobStore, MemoryBlobStore};
    use crate::store::{BlobFrame, BufferManager};
    use crossbeam_channel::bounded;
    use std::io;
    use std::mem::size_of;
    use std::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering};
    use std::sync::{Arc, Barrier, Mutex};

    #[derive(Debug, PartialEq, Eq)]
    enum StoreEvent {
        Write(Vec<BlobGuid>),
        Flush,
    }

    struct CountingBatchStore {
        inner: MemoryBlobStore,
        write_batches: AtomicUsize,
        flushes: AtomicUsize,
        events: Mutex<Vec<StoreEvent>>,
        pending_flush: AtomicBool,
        fail_writes: bool,
        fail_flush: bool,
    }

    struct BlockingFirstBatchStore {
        inner: MemoryBlobStore,
        write_batches: AtomicUsize,
        first_entered: Barrier,
        release_first: Barrier,
    }

    impl BlockingFirstBatchStore {
        fn new() -> Self {
            Self {
                inner: MemoryBlobStore::new(),
                write_batches: AtomicUsize::new(0),
                first_entered: Barrier::new(2),
                release_first: Barrier::new(2),
            }
        }
    }

    impl BlobStore for BlockingFirstBatchStore {
        fn read_blob(&self, guid: BlobGuid, dst: &mut AlignedBlobBuf) -> Result<()> {
            self.inner.read_blob(guid, dst)
        }

        fn write_blob(&self, guid: BlobGuid, src: &AlignedBlobBuf) -> Result<()> {
            self.inner.write_blob(guid, src)
        }

        fn write_blobs_with_data_sync(&self, writes: &[(BlobGuid, &AlignedBlobBuf)]) -> Result<()> {
            let ordinal = self.write_batches.fetch_add(1, Ordering::AcqRel) + 1;
            if ordinal == 1 {
                // Pause the older epoch after its BM content validation but
                // before its store write becomes visible.
                self.first_entered.wait();
                self.release_first.wait();
            }
            self.inner.write_blobs(writes)
        }

        fn delete_blob(&self, guid: BlobGuid) -> Result<()> {
            self.inner.delete_blob(guid)
        }

        fn list_blobs(&self) -> Result<Vec<BlobGuid>> {
            self.inner.list_blobs()
        }

        fn flush(&self) -> Result<()> {
            self.inner.flush()
        }

        fn needs_flush(&self) -> bool {
            self.inner.needs_flush()
        }

        fn has_blob(&self, guid: BlobGuid) -> Result<bool> {
            self.inner.has_blob(guid)
        }
    }

    impl CountingBatchStore {
        fn new() -> Self {
            Self {
                inner: MemoryBlobStore::new(),
                write_batches: AtomicUsize::new(0),
                flushes: AtomicUsize::new(0),
                events: Mutex::new(Vec::new()),
                pending_flush: AtomicBool::new(false),
                fail_writes: false,
                fail_flush: false,
            }
        }

        fn failing_writes() -> Self {
            Self {
                inner: MemoryBlobStore::new(),
                write_batches: AtomicUsize::new(0),
                flushes: AtomicUsize::new(0),
                events: Mutex::new(Vec::new()),
                pending_flush: AtomicBool::new(false),
                fail_writes: true,
                fail_flush: false,
            }
        }

        fn failing_flush() -> Self {
            Self {
                inner: MemoryBlobStore::new(),
                write_batches: AtomicUsize::new(0),
                flushes: AtomicUsize::new(0),
                events: Mutex::new(Vec::new()),
                pending_flush: AtomicBool::new(false),
                fail_writes: false,
                fail_flush: true,
            }
        }

        fn mark_pending_flush(&self) {
            self.pending_flush.store(true, Ordering::Release);
        }
    }

    impl BlobStore for CountingBatchStore {
        fn read_blob(&self, guid: BlobGuid, dst: &mut AlignedBlobBuf) -> Result<()> {
            self.inner.read_blob(guid, dst)
        }

        fn write_blob(&self, guid: BlobGuid, src: &AlignedBlobBuf) -> Result<()> {
            self.pending_flush.store(true, Ordering::Release);
            self.inner.write_blob(guid, src)
        }

        fn write_blobs_with_data_sync(&self, writes: &[(BlobGuid, &AlignedBlobBuf)]) -> Result<()> {
            self.write_batches.fetch_add(1, Ordering::AcqRel);
            self.events.lock().unwrap().push(StoreEvent::Write(
                writes.iter().map(|(guid, _)| *guid).collect(),
            ));
            if self.fail_writes {
                return Err(Error::BlobStoreIo(io::Error::other(
                    "injected write failure",
                )));
            }
            self.pending_flush.store(true, Ordering::Release);
            self.inner.write_blobs(writes)
        }

        fn delete_blob(&self, guid: BlobGuid) -> Result<()> {
            self.pending_flush.store(true, Ordering::Release);
            self.inner.delete_blob(guid)
        }

        fn list_blobs(&self) -> Result<Vec<BlobGuid>> {
            self.inner.list_blobs()
        }

        fn flush(&self) -> Result<()> {
            self.flushes.fetch_add(1, Ordering::AcqRel);
            self.events.lock().unwrap().push(StoreEvent::Flush);
            if self.fail_flush {
                return Err(Error::BlobStoreIo(io::Error::other(
                    "injected flush failure",
                )));
            }
            self.inner.flush()?;
            self.pending_flush.store(false, Ordering::Release);
            Ok(())
        }

        fn needs_flush(&self) -> bool {
            self.pending_flush.load(Ordering::Acquire) || self.inner.needs_flush()
        }
    }

    fn test_shared<S: BlobStore + 'static>(store: Arc<S>) -> Arc<Shared> {
        let (io_tx, _io_rx) = bounded(1);
        Arc::new(Shared {
            bm: Arc::new(BufferManager::new(store, 8)),
            journal: None,
            commit_gate: Arc::new(CommitGate::new()),
            maintenance_gate: Arc::new(Gate::new()),
            cfg: CheckpointConfig::default(),
            io_tx,
            checkpoint_stop: AtomicBool::new(false),
            eviction_stop: AtomicBool::new(false),
            rounds_attempted: AtomicU64::new(0),
            rounds_succeeded: AtomicU64::new(0),
            rounds_failed: AtomicU64::new(0),
            blobs_flushed: AtomicU64::new(0),
            merges_total: AtomicU64::new(0),
            truncates: AtomicU64::new(0),
            evictions: AtomicU64::new(0),
            last_dirty_count: AtomicUsize::new(0),
            last_pending_delete_count: AtomicUsize::new(0),
            last_round_micros: AtomicU64::new(0),
        })
    }

    fn epoch(guid: BlobGuid, byte: u8) -> CheckpointEpoch {
        let mut buf = AlignedBlobBuf::zeroed();
        {
            let _frame = BlobFrame::init(buf.as_mut_slice(), guid).unwrap();
        }
        buf.as_mut_slice()[100] = byte;
        CheckpointEpoch {
            entries: vec![WriteThroughEntry {
                guid,
                bytes: buf,
                expected_seq: u64::from(byte),
                content_version: None,
            }],
            pending: HashMap::new(),
        }
    }

    fn child_blob(guid: BlobGuid, byte: u8) -> AlignedBlobBuf {
        let mut buf = AlignedBlobBuf::zeroed();
        {
            let _frame = BlobFrame::init(buf.as_mut_slice(), guid).unwrap();
        }
        buf.as_mut_slice()[100] = byte;
        buf
    }

    fn parent_blob(parent: BlobGuid, child: BlobGuid) -> AlignedBlobBuf {
        let mut buf = AlignedBlobBuf::zeroed();
        {
            let mut frame = BlobFrame::init(buf.as_mut_slice(), parent).unwrap();
            let out = frame.alloc_node(NodeType::Blob).unwrap();
            let off = frame.offset_of_slot(out.slot).unwrap();
            let node = BlobNode::new(&[], child);
            // Write the BlobNode body by raw offset (its `node_type` byte
            // isn't set yet, so `body_at_offset_mut` can't resolve it).
            let body = frame
                .bytes_at_mut(off, size_of::<BlobNode>() as u32)
                .unwrap();
            let bytes = unsafe {
                std::slice::from_raw_parts(std::ptr::from_ref(&node).cast(), size_of::<BlobNode>())
            };
            body.copy_from_slice(bytes);
            frame.header_mut().root_slot = crate::store::encode_child_off(off);
        }
        buf
    }

    fn entry(guid: BlobGuid, seq: u64, bytes: AlignedBlobBuf) -> WriteThroughEntry {
        WriteThroughEntry {
            guid,
            bytes,
            expected_seq: seq,
            content_version: None,
        }
    }

    #[test]
    fn epoch_batch_shares_one_store_batch_and_sync() {
        let store = Arc::new(CountingBatchStore::new());
        let shared = test_shared(Arc::clone(&store));
        let first = epoch([0xA1; 16], 1);
        let second = epoch([0xA2; 16], 2);

        let mut epochs = vec![first, second];
        let reports = commit_epoch_batch(&shared, &mut epochs);

        assert_eq!(reports.len(), 2);
        assert!(reports.iter().all(|report| report.result.is_ok()));
        assert_eq!(store.write_batches.load(Ordering::Acquire), 1);
        assert_eq!(store.flushes.load(Ordering::Acquire), 1);
        assert_eq!(shared.bm.list_blobs().unwrap().len(), 2);
    }

    #[test]
    fn epoch_batch_preserves_repeated_blob_order() {
        let store = Arc::new(CountingBatchStore::new());
        let shared = test_shared(Arc::clone(&store));
        let guid = [0xC1; 16];
        let first = epoch(guid, 1);
        let second = epoch(guid, 2);

        let mut epochs = vec![first, second];
        let reports = commit_epoch_batch(&shared, &mut epochs);

        assert_eq!(reports.len(), 2);
        assert!(reports.iter().all(|report| report.result.is_ok()));
        assert_eq!(store.write_batches.load(Ordering::Acquire), 1);
        assert_eq!(store.flushes.load(Ordering::Acquire), 1);

        let mut out = AlignedBlobBuf::zeroed();
        shared.bm.read_blob(guid, &mut out).unwrap();
        assert_eq!(out.as_slice()[100], 2);
    }

    #[test]
    fn epoch_write_error_restores_without_sync() {
        let store = Arc::new(CountingBatchStore::failing_writes());
        let shared = test_shared(Arc::clone(&store));
        let first = epoch([0xB1; 16], 1);

        let mut epochs = vec![first];
        let reports = commit_epoch_batch(&shared, &mut epochs);

        assert_eq!(reports.len(), 1);
        assert!(reports[0].result.is_err());
        assert_eq!(reports[0].dirty_flushed, 0);
        assert_eq!(store.write_batches.load(Ordering::Acquire), 1);
        assert_eq!(store.flushes.load(Ordering::Acquire), 0);
        assert_eq!(shared.bm.dirty_count(), 1);
    }

    #[test]
    fn epoch_flush_error_restores_dirty_entry() {
        let store = Arc::new(CountingBatchStore::failing_flush());
        let shared = test_shared(Arc::clone(&store));
        let first = epoch([0xD1; 16], 1);

        let mut epochs = vec![first];
        let reports = commit_epoch_batch(&shared, &mut epochs);

        assert_eq!(reports.len(), 1);
        assert!(reports[0].result.is_err());
        assert_eq!(reports[0].dirty_flushed, 0);
        assert_eq!(store.write_batches.load(Ordering::Acquire), 1);
        assert_eq!(store.flushes.load(Ordering::Acquire), 1);
        assert_eq!(shared.bm.dirty_count(), 1);
    }

    #[test]
    fn stale_dirty_write_defers_pending_deletes() {
        let store = Arc::new(CountingBatchStore::new());
        let shared = test_shared(Arc::clone(&store));
        let parent = [0xD3; 16];
        let child = [0xD4; 16];
        let old_parent = parent_blob(parent, child);
        store.inner.write_blob(parent, &old_parent).unwrap();
        store
            .inner
            .write_blob(child, &child_blob(child, 9))
            .unwrap();

        let pin = shared.bm.pin(parent).unwrap();
        let old_version = pin.content_version();
        {
            let mut guard = pin.write();
            guard.as_mut_slice()[100] = 0xEE;
        }
        drop(pin);

        let mut pending = HashMap::new();
        pending.insert(child, 77);
        let mut epochs = vec![CheckpointEpoch {
            entries: vec![WriteThroughEntry {
                guid: parent,
                bytes: old_parent,
                expected_seq: 77,
                content_version: Some(old_version),
            }],
            pending,
        }];

        let reports = commit_epoch_batch(&shared, &mut epochs);

        assert_eq!(reports.len(), 1);
        assert!(reports[0].result.is_ok());
        assert_eq!(reports[0].dirty_flushed, 0);
        assert_eq!(reports[0].applied_deletes, 0);
        assert_eq!(shared.bm.dirty_count(), 1);
        assert_eq!(shared.bm.pending_delete_count(), 1);
        assert!(
            store.inner.has_blob(child).unwrap(),
            "child delete must wait until parent write is durable"
        );
    }

    #[test]
    fn checkpoint_flushes_child_manifest_before_parent_reference() {
        let store = Arc::new(CountingBatchStore::new());
        let shared = test_shared(Arc::clone(&store));
        let parent = [0xE1; 16];
        let child = [0xE2; 16];
        let mut epochs = vec![CheckpointEpoch {
            entries: vec![
                entry(parent, 1, parent_blob(parent, child)),
                entry(child, 2, child_blob(child, 9)),
            ],
            pending: HashMap::new(),
        }];

        let reports = commit_epoch_batch(&shared, &mut epochs);

        assert_eq!(reports.len(), 1);
        assert!(reports[0].result.is_ok());
        assert_eq!(reports[0].dirty_flushed, 2);
        assert_eq!(
            *store.events.lock().unwrap(),
            vec![
                StoreEvent::Write(vec![child]),
                StoreEvent::Flush,
                StoreEvent::Write(vec![parent]),
                StoreEvent::Flush,
            ]
        );
    }

    #[test]
    fn child_first_checkpoint_rejects_self_dependency_cycle() {
        let bm = Arc::new(BufferManager::new(Arc::new(MemoryBlobStore::new()), 8));
        let guid = [0xE7; 16];

        let error = write_entries_child_first(&bm, vec![entry(guid, 1, parent_blob(guid, guid))])
            .unwrap_err();

        assert!(matches!(
            error,
            Error::NodeCorrupt {
                context: "checkpoint dependency cycle",
                blob_guid: Some(_),
                ..
            }
        ));
    }

    #[test]
    fn child_first_checkpoint_rejects_two_blob_dependency_cycle() {
        let bm = Arc::new(BufferManager::new(Arc::new(MemoryBlobStore::new()), 8));
        let first = [0xE8; 16];
        let second = [0xE9; 16];

        let error = write_entries_child_first(
            &bm,
            vec![
                entry(first, 1, parent_blob(first, second)),
                entry(second, 1, parent_blob(second, first)),
            ],
        )
        .unwrap_err();

        assert!(matches!(
            error,
            Error::NodeCorrupt {
                context: "checkpoint dependency cycle",
                blob_guid: Some(_),
                ..
            }
        ));
    }

    #[test]
    fn dependency_cycle_handles_long_corrupt_graph_without_recursion() {
        const NODES: u32 = 50_000;
        let guid = |index: u32| {
            let mut guid = [0xA7; 16];
            guid[..4].copy_from_slice(&index.to_le_bytes());
            guid
        };
        let mut graph = HashMap::<BlobGuid, HashSet<BlobGuid>>::with_capacity(NODES as usize);
        for index in 0..NODES {
            let mut children = HashSet::new();
            if index + 1 < NODES {
                children.insert(guid(index + 1));
            }
            graph.insert(guid(index), children);
        }

        assert_eq!(dependency_cycle(&graph), None);
        graph.get_mut(&guid(NODES - 1)).unwrap().insert(guid(1));
        assert!(dependency_cycle(&graph).is_some());
    }

    #[test]
    fn child_first_checkpoint_rejects_permanently_missing_child() {
        let bm = Arc::new(BufferManager::new(Arc::new(MemoryBlobStore::new()), 8));
        let parent = [0xEA; 16];
        let missing = [0xEB; 16];

        let error =
            write_entries_child_first(&bm, vec![entry(parent, 1, parent_blob(parent, missing))])
                .unwrap_err();

        assert!(matches!(
            error,
            Error::NodeCorrupt {
                context: "checkpoint dependency references missing child",
                blob_guid: Some(guid),
                ..
            } if guid == parent
        ));
    }

    #[test]
    fn child_first_checkpoint_defers_external_dirty_child() {
        let bm = Arc::new(BufferManager::new(Arc::new(MemoryBlobStore::new()), 8));
        let parent = [0xEC; 16];
        let child = [0xED; 16];
        bm.install_new_blob(child, child_blob(child, 9), 1);

        let deferred =
            write_entries_child_first(&bm, vec![entry(parent, 2, parent_blob(parent, child))])
                .unwrap();

        assert_eq!(deferred, vec![(parent, 2)]);
        assert!(bm.has_unflushed_blob(child));
    }

    #[test]
    fn checkpoint_flushes_existing_child_manifest_before_parent_reference() {
        let store = Arc::new(CountingBatchStore::new());
        let shared = test_shared(Arc::clone(&store));
        let parent = [0xE3; 16];
        let child = [0xE4; 16];
        store
            .inner
            .write_blob(child, &child_blob(child, 9))
            .unwrap();
        store.mark_pending_flush();
        let mut epochs = vec![CheckpointEpoch {
            entries: vec![entry(parent, 1, parent_blob(parent, child))],
            pending: HashMap::new(),
        }];

        let reports = commit_epoch_batch(&shared, &mut epochs);

        assert_eq!(reports.len(), 1);
        assert!(reports[0].result.is_ok());
        assert_eq!(reports[0].dirty_flushed, 1);
        assert_eq!(
            *store.events.lock().unwrap(),
            vec![
                StoreEvent::Flush,
                StoreEvent::Write(vec![parent]),
                StoreEvent::Flush,
            ],
            "outstanding child manifest debt must become durable before parent write",
        );
        assert_eq!(shared.bm.dirty_count(), 0);
    }

    #[test]
    fn checkpoint_preflush_failure_restores_parent_for_retry() {
        let store = Arc::new(CountingBatchStore::failing_flush());
        let shared = test_shared(Arc::clone(&store));
        let parent = [0xE5; 16];
        let child = [0xE6; 16];
        store
            .inner
            .write_blob(child, &child_blob(child, 9))
            .unwrap();
        store.mark_pending_flush();
        let mut epochs = vec![CheckpointEpoch {
            entries: vec![entry(parent, 1, parent_blob(parent, child))],
            pending: HashMap::new(),
        }];

        let reports = commit_epoch_batch(&shared, &mut epochs);

        assert_eq!(reports.len(), 1);
        assert!(reports[0].result.is_err());
        assert_eq!(reports[0].dirty_flushed, 0);
        assert_eq!(*store.events.lock().unwrap(), vec![StoreEvent::Flush]);
        assert_eq!(store.write_batches.load(Ordering::Acquire), 0);
        assert_eq!(shared.bm.dirty_count(), 1);
    }

    #[test]
    fn synchronous_child_first_manifest_survives_torn_parent_tail() {
        let dir = tempfile::tempdir().unwrap();
        let parent: BlobGuid = [
            0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E,
            0x9F, 0xA0,
        ];
        let child: BlobGuid = [
            0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE,
            0xAF, 0xB0,
        ];

        {
            let file = match FileBlobStore::open(dir.path()) {
                Ok(store) => Arc::new(store),
                Err(Error::BlobStoreIo(e)) if e.raw_os_error() == Some(libc::EINVAL) => {
                    eprintln!("skipping: O_DIRECT not supported on this fs");
                    return;
                }
                Err(e) => panic!("unexpected open error: {e}"),
            };
            // Keep an old durable parent mapping in the manifest. The later
            // checkpoint overwrites that same slot with a parent that points
            // at a newly-created child.
            file.write_blob(parent, &child_blob(parent, 1)).unwrap();
            file.flush().unwrap();

            let file_dyn: Arc<dyn BlobStore> = file;
            let bm = Arc::new(BufferManager::new(file_dyn, 8));
            write_entries_child_first(
                &bm,
                vec![
                    entry(parent, 2, parent_blob(parent, child)),
                    entry(child, 2, child_blob(child, 2)),
                ],
            )
            .unwrap();
        }

        let log_path = dir.path().join("manifest.log");
        let log = std::fs::read(&log_path).unwrap();
        let positions = |needle: &BlobGuid| {
            log.windows(needle.len())
                .enumerate()
                .filter_map(|(idx, bytes)| (bytes == needle).then_some(idx))
                .collect::<Vec<_>>()
        };
        let parent_positions = positions(&parent);
        let child_positions = positions(&child);
        assert_eq!(parent_positions.len(), 2, "initial + rewritten parent Set");
        assert_eq!(child_positions.len(), 1, "one newly-created child Set");
        assert!(
            child_positions[0] < parent_positions[1],
            "the child manifest record must be durable before the rewritten parent record",
        );

        // Keep the child Set and only a torn prefix of the later parent Set.
        // Reopen truncates that tail to the last complete record. The parent
        // data slot may already contain its new child edge, so the child must
        // still be present in the recovered manifest.
        const MANIFEST_RECORD_HEADER_SIZE: u64 = 9;
        let rewritten_parent_record =
            u64::try_from(parent_positions[1]).unwrap() - MANIFEST_RECORD_HEADER_SIZE;
        let f = std::fs::OpenOptions::new()
            .write(true)
            .open(&log_path)
            .unwrap();
        f.set_len(rewritten_parent_record + 3).unwrap();
        f.sync_all().unwrap();
        drop(f);

        let reopened = FileBlobStore::open(dir.path()).unwrap();
        let mut parent_bytes = AlignedBlobBuf::zeroed();
        reopened.read_blob(parent, &mut parent_bytes).unwrap();
        let children =
            engine::collect_blob_children_from_frame(BlobFrameRef::wrap(parent_bytes.as_slice()))
                .unwrap();
        assert_eq!(children, vec![child]);
        let mut child_bytes = AlignedBlobBuf::zeroed();
        reopened.read_blob(child, &mut child_bytes).unwrap();
    }

    #[test]
    fn checkpoint_io_serializes_old_parent_before_new_parent_delete_epoch() {
        let store = Arc::new(BlockingFirstBatchStore::new());
        let parent = [0xB1; 16];
        let child = [0xB2; 16];
        store
            .inner
            .write_blob(child, &child_blob(child, 1))
            .unwrap();
        let shared = test_shared(Arc::clone(&store));
        shared.bm.mark_for_delete(child, 2);

        let old_shared = Arc::clone(&shared);
        let old_epoch = std::thread::spawn(move || {
            let mut epochs = vec![CheckpointEpoch {
                entries: vec![entry(parent, 1, parent_blob(parent, child))],
                pending: HashMap::new(),
            }];
            commit_epoch_batch(&old_shared, &mut epochs)
        });
        store.first_entered.wait();

        let new_shared = Arc::clone(&shared);
        let (new_done_tx, new_done_rx) = std::sync::mpsc::sync_channel(1);
        let new_epoch = std::thread::spawn(move || {
            let mut epochs = vec![CheckpointEpoch {
                entries: vec![entry(parent, 2, child_blob(parent, 2))],
                pending: HashMap::from([(child, 2)]),
            }];
            let reports = commit_epoch_batch(&new_shared, &mut epochs);
            new_done_tx.send(()).unwrap();
            reports
        });

        assert!(
            new_done_rx
                .recv_timeout(std::time::Duration::from_millis(100))
                .is_err(),
            "the newer parent+delete epoch must wait for the older epoch's complete I/O phase",
        );
        assert_eq!(
            store.write_batches.load(Ordering::Acquire),
            1,
            "no second epoch may enter store I/O while the first is paused",
        );
        store.release_first.wait();

        let old_reports = old_epoch.join().unwrap();
        new_done_rx
            .recv_timeout(std::time::Duration::from_secs(2))
            .unwrap();
        let new_reports = new_epoch.join().unwrap();
        assert!(old_reports.iter().all(|report| report.result.is_ok()));
        assert!(new_reports.iter().all(|report| report.result.is_ok()));
        assert!(!store.inner.has_blob(child).unwrap());
        let mut durable_parent = AlignedBlobBuf::zeroed();
        store.inner.read_blob(parent, &mut durable_parent).unwrap();
        assert!(
            engine::collect_blob_children_from_frame(
                BlobFrameRef::wrap(durable_parent.as_slice(),)
            )
            .unwrap()
            .is_empty(),
            "the final durable parent must be the newer child-free epoch",
        );
    }
}