tonbo 0.4.0-a1

Embedded database for serverless and edge runtimes, storing data as Parquet on S3
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
use std::sync::Arc;

#[cfg(test)]
use fusio::mem::fs::InMemoryFs;
use fusio::{
    dynamic::{MaybeSend, MaybeSync},
    executor::{Executor, Timer},
    fs::{Fs, FsCas},
    path::{Path, PathPart},
};
use fusio_manifest::{
    BackoffPolicy, CheckpointStoreImpl, HeadStoreImpl, LeaseStoreImpl, ManifestContext,
    SegmentStoreImpl, snapshot::Snapshot, types::Error as FusioManifestError,
};
use tracing::instrument;

use super::{
    ManifestFs,
    codec::{CatalogCodec, GcPlanCodec, ManifestCodec, VersionCodec},
    domain::{
        CurrentRootSet, GcAuthorizationSummary, GcPlanState, TableDefinition, TableHead, TableId,
        TableMeta, VersionState, WalSegmentRef,
    },
    driver::{Manifest, ManifestError, ManifestResult, Stores, VersionSnapshot},
    version::VersionEdit,
};
use crate::{
    id::FileIdGenerator,
    mvcc::Timestamp,
    observability::{log_debug, log_info},
};

/// Concrete manifest type alias for a given filesystem and executor.
pub(super) type ManifestInstance<C, FS, E> = Manifest<
    C,
    HeadStoreImpl<FS>,
    SegmentStoreImpl<FS>,
    CheckpointStoreImpl<FS>,
    LeaseStoreImpl<FS, E>,
    E,
>;

/// In-memory manifest type wired against `fusio`'s memory FS for dev/test flows.
#[cfg(all(test, feature = "tokio"))]
pub(super) type InMemoryManifest<E> = ManifestInstance<VersionCodec, InMemoryFs, E>;

/// In-memory catalog manifest type used for catalog-specific tests.
#[cfg(all(test, feature = "tokio"))]
pub(super) type InMemoryCatalogManifest<E> = ManifestInstance<CatalogCodec, InMemoryFs, E>;

/// Snapshot combining catalog metadata with the latest version state.
#[derive(Debug, Clone)]
pub(crate) struct TableSnapshot {
    pub _manifest_snapshot: Snapshot,
    pub head: TableHead,
    pub latest_version: Option<VersionState>,

    pub _table_meta: TableMeta,
}

impl TableSnapshot {
    pub(super) fn from_parts(version: VersionSnapshot, table_meta: TableMeta) -> Self {
        Self {
            _manifest_snapshot: version.manifest_snapshot,
            head: version.head,
            latest_version: version.latest_version,
            _table_meta: table_meta,
        }
    }
}

/// Primary manifest bundle exposed to the rest of the crate.
///
/// This struct bundles three manifest instances (version, catalog, gc_plan) sharing
/// the same filesystem and executor types for static dispatch.
pub(crate) struct TonboManifest<FS, E>
where
    FS: ManifestFs<E>,
    E: Executor + Timer + Clone + 'static,
    <FS as Fs>::File: fusio::durability::FileCommit,
{
    version: ManifestInstance<VersionCodec, FS, E>,
    catalog: ManifestInstance<CatalogCodec, FS, E>,
    gc_plan: ManifestInstance<GcPlanCodec, FS, E>,
}

impl<FS, E> Clone for TonboManifest<FS, E>
where
    FS: ManifestFs<E>,
    E: Executor + Timer + Clone + 'static,
    <FS as Fs>::File: fusio::durability::FileCommit,
    HeadStoreImpl<FS>: fusio_manifest::HeadStore + Clone,
    SegmentStoreImpl<FS>: fusio_manifest::SegmentIo + Clone,
    CheckpointStoreImpl<FS>: fusio_manifest::CheckpointStore + Clone,
    LeaseStoreImpl<FS, E>: fusio_manifest::LeaseStore + Clone,
{
    fn clone(&self) -> Self {
        Self {
            version: self.version.clone(),
            catalog: self.catalog.clone(),
            gc_plan: self.gc_plan.clone(),
        }
    }
}

impl<FS, E> TonboManifest<FS, E>
where
    FS: ManifestFs<E>,
    E: Executor + Timer + Clone + 'static,
    <FS as Fs>::File: fusio::durability::FileCommit,
    HeadStoreImpl<FS>: fusio_manifest::HeadStore,
    SegmentStoreImpl<FS>: fusio_manifest::SegmentIo,
    CheckpointStoreImpl<FS>: fusio_manifest::CheckpointStore,
    LeaseStoreImpl<FS, E>: fusio_manifest::LeaseStore,
{
    fn new(
        version: ManifestInstance<VersionCodec, FS, E>,
        catalog: ManifestInstance<CatalogCodec, FS, E>,
        gc_plan: ManifestInstance<GcPlanCodec, FS, E>,
    ) -> Self {
        Self {
            version,
            catalog,
            gc_plan,
        }
    }

    pub(crate) async fn apply_version_edits(
        &self,
        table: TableId,
        edits: &[VersionEdit],
    ) -> ManifestResult<Timestamp> {
        self.version.apply_version_edits(table, edits).await
    }

    pub(crate) async fn apply_version_edits_cas(
        &self,
        table: TableId,
        expected_head: Option<Timestamp>,
        edits: &[VersionEdit],
    ) -> ManifestResult<Timestamp> {
        self.version
            .apply_version_edits_cas(table, expected_head, edits)
            .await
    }

    pub(crate) async fn snapshot_latest(&self, table: TableId) -> ManifestResult<TableSnapshot> {
        let version_snapshot = self.version.snapshot_latest(table).await?;
        let table_meta = self.catalog.table_meta(table).await?;
        Ok(TableSnapshot::from_parts(version_snapshot, table_meta))
    }

    /// Capture the latest manifest view, optionally falling back to a cached `TableMeta`
    /// when the catalog entry is missing (e.g. transient object-store visibility gaps).
    pub(crate) async fn snapshot_latest_with_fallback(
        &self,
        table: TableId,
        fallback: &TableMeta,
    ) -> ManifestResult<TableSnapshot> {
        match self.catalog.table_meta(table).await {
            Ok(meta) => {
                let version_snapshot = self.version.snapshot_latest(table).await?;
                Ok(TableSnapshot::from_parts(version_snapshot, meta))
            }
            Err(ManifestError::Invariant("catalog metadata missing for table_id")) => {
                let version_snapshot = self.version.snapshot_latest(table).await?;
                Ok(TableSnapshot::from_parts(
                    version_snapshot,
                    fallback.clone(),
                ))
            }
            Err(err) => Err(err),
        }
    }

    pub(crate) async fn wal_floor(&self, table: TableId) -> ManifestResult<Option<WalSegmentRef>> {
        self.version.wal_floor(table).await
    }

    async fn init_table_head(&self, table_id: TableId, head: TableHead) -> ManifestResult<()> {
        self.version.init_table_head(table_id, head).await
    }

    async fn init_catalog(&self) -> ManifestResult<()> {
        self.catalog.init_catalog_root().await
    }

    #[instrument(
        name = "manifest::register_table",
        skip(self, file_ids, definition),
        fields(component = "manifest")
    )]
    pub(crate) async fn register_table(
        &self,
        file_ids: &FileIdGenerator,
        definition: &TableDefinition,
    ) -> ManifestResult<TableMeta> {
        let meta = self.catalog.register_table(file_ids, definition).await?;
        self.init_table_head(
            meta.table_id,
            TableHead {
                table_id: meta.table_id,
                schema_version: meta.schema_version,
                wal_floor: None,
                last_manifest_txn: None,
            },
        )
        .await?;
        log_info!(
            component = "manifest",
            event = "manifest_table_registered",
            table_id = ?meta.table_id,
            schema_version = meta.schema_version,
        );
        Ok(meta)
    }

    #[cfg(test)]
    pub(crate) async fn record_gc_plan(
        &self,
        table: TableId,
        plan: GcPlanState,
    ) -> ManifestResult<()> {
        self.gc_plan.put_gc_plan(table, plan).await
    }

    pub(crate) async fn merge_gc_plan(
        &self,
        table: TableId,
        plan: GcPlanState,
    ) -> ManifestResult<(Option<GcPlanState>, Option<GcPlanState>)> {
        self.gc_plan
            .update_gc_plan(table, move |current| {
                let mut merged = current.unwrap_or_default();
                merged.merge(plan);
                (!merged.is_empty()).then_some(merged)
            })
            .await
    }

    pub(crate) async fn remove_gc_sst_candidates(
        &self,
        table: TableId,
        reclaimed: &[crate::manifest::GcSstRef],
    ) -> ManifestResult<(Option<GcPlanState>, Option<GcPlanState>)> {
        self.gc_plan
            .update_gc_plan(table, |current| {
                let mut plan = current?;
                plan.remove_sst_candidates(reclaimed);
                (!plan.is_empty()).then_some(plan)
            })
            .await
    }

    pub(crate) async fn peek_gc_plan(&self, table: TableId) -> ManifestResult<Option<GcPlanState>> {
        self.gc_plan.peek_gc_plan(table).await
    }

    /// Take staged GC candidates and authorize SST reclamation against the current root set.
    ///
    /// In the single-process model the latest manifest root set is the only source of truth for
    /// whether an SST is still live. `GcPlanState` remains a hint queue; this helper filters out
    /// any SST candidate that has become reachable again before a sweeper acts on it. Callers that
    /// only process part of the returned plan must re-queue any residual SST or WAL candidates.
    #[allow(dead_code)]
    pub(crate) async fn take_gc_plan_for_authorized_sweep(
        &self,
        table: TableId,
    ) -> ManifestResult<Option<GcPlanState>> {
        self.take_gc_plan_for_authorized_sweep_with_pins(table, &[])
            .await
    }

    pub(crate) async fn take_gc_plan_for_authorized_sweep_with_pins(
        &self,
        table: TableId,
        active_pins: &[Timestamp],
    ) -> ManifestResult<Option<GcPlanState>> {
        let Some(plan) = self.gc_plan.peek_gc_plan(table).await? else {
            return Ok(None);
        };
        let staged_sst_count = plan.obsolete_ssts.len();
        let root_set = self
            .version
            .current_root_set_with_pins(table, active_pins)
            .await?;
        let plan = plan.authorize_with_root_set(&root_set);
        let authorized_sst_count = plan.obsolete_ssts.len();
        log_debug!(
            component = "manifest",
            event = "gc_plan_authorized_for_sweep",
            table_id = ?table,
            protected_versions = root_set.protected_version_count(),
            protected_sst_objects = root_set.protected_object_count(),
            staged_sst_candidates = staged_sst_count,
            authorized_sst_candidates = authorized_sst_count,
            filtered_sst_candidates = staged_sst_count.saturating_sub(authorized_sst_count),
            obsolete_wal_segments = plan.obsolete_wal_segments.len(),
        );
        if plan.is_empty() {
            Ok(None)
        } else {
            Ok(Some(plan))
        }
    }

    /// Inspect the current GC plan against the latest manifest root set without mutating it.
    #[allow(dead_code)]
    pub(crate) async fn inspect_gc_plan_authorization(
        &self,
        table: TableId,
    ) -> ManifestResult<Option<GcAuthorizationSummary>> {
        self.inspect_gc_plan_authorization_with_pins(table, &[])
            .await
    }

    pub(crate) async fn inspect_gc_plan_authorization_with_pins(
        &self,
        table: TableId,
        active_pins: &[Timestamp],
    ) -> ManifestResult<Option<GcAuthorizationSummary>> {
        let Some(plan) = self.gc_plan.peek_gc_plan(table).await? else {
            return Ok(None);
        };
        let root_set = self
            .version
            .current_root_set_with_pins(table, active_pins)
            .await?;
        Ok(Some(plan.authorization_summary(&root_set)))
    }

    /// List committed versions of a table, ordered newest-first.
    ///
    /// Returns up to `limit` versions for time travel queries.
    pub async fn list_versions(
        &self,
        table: TableId,
        limit: usize,
    ) -> ManifestResult<Vec<VersionState>> {
        self.version.list_versions(table, limit).await
    }

    /// Build the current manifest root set that protects SST objects from GC.
    #[allow(dead_code)]
    pub(crate) async fn current_root_set(&self, table: TableId) -> ManifestResult<CurrentRootSet> {
        self.current_root_set_with_pins(table, &[]).await
    }

    #[allow(dead_code)]
    pub(crate) async fn current_root_set_with_pins(
        &self,
        table: TableId,
        active_pins: &[Timestamp],
    ) -> ManifestResult<CurrentRootSet> {
        self.version
            .current_root_set_with_pins(table, active_pins)
            .await
    }

    /// Snapshot a specific historical version by its manifest timestamp.
    ///
    /// Unlike `snapshot_latest` which always returns the current head version,
    /// this method loads the exact version that was committed at `manifest_ts`.
    pub(crate) async fn snapshot_at_version(
        &self,
        table: TableId,
        manifest_ts: Timestamp,
    ) -> ManifestResult<TableSnapshot> {
        let version_snapshot = self.version.snapshot_at_version(table, manifest_ts).await?;
        let table_meta = self.catalog.table_meta(table).await?;
        Ok(TableSnapshot::from_parts(version_snapshot, table_meta))
    }
}

/// Raw helper used by tests needing direct access to the concrete manifest.
#[cfg(all(test, feature = "tokio"))]
pub(super) async fn init_in_memory_manifest_raw<E>(
    schema_version: u32,
    file_ids: &FileIdGenerator,
    executor: E,
) -> ManifestResult<(InMemoryManifest<E>, TableId)>
where
    E: Executor + Timer + Clone + 'static,
    LeaseStoreImpl<InMemoryFs, E>: fusio_manifest::LeaseStore,
{
    let fs = InMemoryFs::new();
    let head = HeadStoreImpl::new(fs.clone(), "head.json");
    let segment = SegmentStoreImpl::new(fs.clone(), "segments");
    let checkpoint = CheckpointStoreImpl::new(fs.clone(), "");
    let lease = LeaseStoreImpl::new(fs, "", BackoffPolicy::default(), executor.clone());
    let ctx = Arc::new(ManifestContext::new(executor));
    let manifest = Manifest::open(Stores::new(head, segment, checkpoint, lease), ctx);
    let table_id = TableId::new(file_ids);
    manifest
        .init_table_head(
            table_id,
            TableHead {
                table_id,
                schema_version,
                wal_floor: None,
                last_manifest_txn: None,
            },
        )
        .await?;
    Ok((manifest, table_id))
}

/// Construct an in-memory manifest.
#[cfg(test)]
pub(crate) async fn init_in_memory_manifest<E>(
    executor: E,
) -> ManifestResult<TonboManifest<InMemoryFs, E>>
where
    E: Executor + Timer + Clone + 'static,
    LeaseStoreImpl<InMemoryFs, E>: fusio_manifest::LeaseStore,
    <InMemoryFs as Fs>::File: fusio::durability::FileCommit,
{
    let root = Path::default();
    init_fs_manifest(InMemoryFs::new(), &root, executor).await
}

/// Construct a manifest rooted under `root/manifest` using the provided filesystem backend.
#[instrument(
    name = "manifest::init_fs_manifest",
    skip(fs, root, executor),
    fields(component = "manifest")
)]
pub(crate) async fn init_fs_manifest<FS, E>(
    fs: FS,
    root: &Path,
    executor: E,
) -> ManifestResult<TonboManifest<FS, E>>
where
    FS: super::ManifestFs<E>,
    E: Executor + Timer + Clone + 'static,
    HeadStoreImpl<FS>: fusio_manifest::HeadStore,
    SegmentStoreImpl<FS>: fusio_manifest::SegmentIo,
    CheckpointStoreImpl<FS>: fusio_manifest::CheckpointStore,
    LeaseStoreImpl<FS, E>: fusio_manifest::LeaseStore,
    <FS as Fs>::File: fusio::durability::FileCommit,
{
    let manifest_root = append_segment(root, "manifest");
    let version_root = append_segment(&manifest_root, "version");
    let catalog_root = append_segment(&manifest_root, "catalog");
    let gc_root = append_segment(&manifest_root, "gc");

    ensure_manifest_dirs::<FS>(&version_root).await?;
    ensure_manifest_dirs::<FS>(&catalog_root).await?;
    ensure_manifest_dirs::<FS>(&gc_root).await?;

    let version_manifest =
        open_manifest_instance::<FS, VersionCodec, E>(fs.clone(), &version_root, executor.clone());
    let catalog_manifest =
        open_manifest_instance::<FS, CatalogCodec, E>(fs.clone(), &catalog_root, executor.clone());
    let gc_manifest = open_manifest_instance::<FS, GcPlanCodec, E>(fs.clone(), &gc_root, executor);
    let tonbo = TonboManifest::new(version_manifest, catalog_manifest, gc_manifest);
    tonbo.init_catalog().await?;
    log_debug!(component = "manifest", event = "manifest_initialized",);
    Ok(tonbo)
}

/// Convenience wrapper for in-memory manifests (tests/dev).
#[cfg(all(test, feature = "tokio"))]
pub(crate) async fn init_fs_manifest_in_memory<E>(
    fs: InMemoryFs,
    root: &Path,
    executor: E,
) -> ManifestResult<TonboManifest<InMemoryFs, E>>
where
    E: Executor + Timer + Clone + 'static,
{
    init_fs_manifest(fs, root, executor).await
}

async fn ensure_dir_path<FS>(path: &Path) -> ManifestResult<()>
where
    FS: Fs,
{
    FS::create_dir_all(path)
        .await
        .map_err(|err| ManifestError::Backend(FusioManifestError::Io(err)))?;
    Ok(())
}

pub(crate) async fn ensure_manifest_dirs<FS>(base: &Path) -> ManifestResult<()>
where
    FS: Fs,
{
    ensure_dir_path::<FS>(base).await?;
    ensure_dir_path::<FS>(&base.child(PathPart::parse("segments").expect("segments part"))).await?;
    ensure_dir_path::<FS>(&base.child(PathPart::parse("checkpoints").expect("checkpoints part")))
        .await?;
    ensure_dir_path::<FS>(&base.child(PathPart::parse("leases").expect("leases part"))).await?;
    Ok(())
}

fn open_manifest_instance<FS, C, E>(
    fs: FS,
    prefix: &Path,
    executor: E,
) -> ManifestInstance<C, FS, E>
where
    C: ManifestCodec,
    FS: Fs + FsCas + Clone + MaybeSend + MaybeSync + 'static,
    E: Executor + Timer + Clone + 'static,
    HeadStoreImpl<FS>: fusio_manifest::HeadStore,
    SegmentStoreImpl<FS>: fusio_manifest::SegmentIo,
    CheckpointStoreImpl<FS>: fusio_manifest::CheckpointStore,
    LeaseStoreImpl<FS, E>: fusio_manifest::LeaseStore,
    <FS as Fs>::File: fusio::durability::FileCommit,
{
    let head = HeadStoreImpl::new(
        fs.clone(),
        prefix.child(PathPart::parse("head.json").expect("head")),
    );
    let segment = SegmentStoreImpl::new(
        fs.clone(),
        prefix.child(PathPart::parse("segments").expect("segments")),
    );
    let checkpoint = CheckpointStoreImpl::new(
        fs.clone(),
        prefix.child(PathPart::parse("checkpoints").expect("checkpoints")),
    );
    // LeaseStoreImpl appends the `leases/` suffix internally; pass the manifest prefix so
    // lease records land under `<prefix>/leases/...` alongside other manifest metadata.
    let lease_prefix = prefix.as_ref().to_string();
    let lease = LeaseStoreImpl::new(fs, lease_prefix, BackoffPolicy::default(), executor.clone());
    let ctx = Arc::new(ManifestContext::new(executor));
    Manifest::open(Stores::new(head, segment, checkpoint, lease), ctx)
}

fn append_segment(base: &Path, segment: &str) -> Path {
    if base.as_ref().is_empty() {
        Path::parse(segment).expect("segment path")
    } else {
        base.child(PathPart::parse(segment).expect("segment part"))
    }
}

#[cfg(all(test, feature = "tokio"))]
mod tests {
    use std::time::Duration;

    use fusio::{
        disk::LocalFs,
        executor::tokio::TokioExecutor,
        mem::fs::InMemoryFs,
        path::{Path as FusioPath, PathPart},
    };
    use fusio_manifest::{LeaseKind, LeaseStore};
    use futures::TryStreamExt;

    use super::*;
    use crate::{
        id::FileIdGenerator,
        manifest::{SstEntry, TableDefinition, VersionEdit},
        mvcc::Timestamp,
        ondisk::sstable::{SsTableId, SsTableStats},
    };

    #[tokio::test(flavor = "multi_thread")]
    async fn init_disk_manifest_smoke() {
        let dir = tempfile::tempdir().expect("temp dir");
        let path = FusioPath::from_filesystem_path(dir.path()).expect("fs path");
        init_fs_manifest(LocalFs {}, &path, TokioExecutor::default())
            .await
            .expect("disk manifest");
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn lease_store_writes_under_leases_prefix() {
        let fs = InMemoryFs::new();
        let root = FusioPath::default();
        let manifest_root = append_segment(&root, "manifest");
        let version_root = append_segment(&manifest_root, "version");
        ensure_manifest_dirs::<InMemoryFs>(&version_root)
            .await
            .expect("dirs created");

        let executor = TokioExecutor::default();
        let lease_store = LeaseStoreImpl::new(
            fs.clone(),
            version_root.as_ref().to_string(),
            BackoffPolicy::default(),
            executor,
        );
        let lease = lease_store
            .create(1, None, None, LeaseKind::Read, Duration::from_secs(30))
            .await
            .expect("lease created");

        let leases_dir = version_root.child(PathPart::parse("leases").expect("leases part"));
        let files: Vec<_> = fs
            .list(&leases_dir)
            .await
            .expect("list leases dir")
            .try_collect()
            .await
            .expect("collect leases dir entries");

        assert!(
            files
                .iter()
                .any(|meta| meta.path.as_ref().contains(&lease.id.0)),
            "lease documents must be placed under the leases directory"
        );
        assert!(
            files
                .iter()
                .all(|meta| meta.path.as_ref().starts_with(leases_dir.as_ref())),
            "all lease files should live under the leases/ prefix"
        );
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn manifest_history_remains_queryable_while_root_set_defaults_to_head() {
        let manifest = init_in_memory_manifest(TokioExecutor::default())
            .await
            .expect("manifest");
        let file_ids = FileIdGenerator::default();
        let table = manifest
            .register_table(
                &file_ids,
                &TableDefinition {
                    name: "manifest-history".into(),
                    schema_fingerprint: "fingerprint".into(),
                    primary_key_columns: vec!["id".into()],
                    schema_version: 1,
                },
            )
            .await
            .expect("register table")
            .table_id;

        manifest
            .apply_version_edits(
                table,
                &[VersionEdit::AddSsts {
                    level: 0,
                    entries: vec![SstEntry::new(
                        SsTableId::new(1),
                        Some(SsTableStats::default()),
                        None,
                        FusioPath::from("L0/1.parquet"),
                        None,
                    )],
                }],
            )
            .await
            .expect("first version");
        manifest
            .apply_version_edits(
                table,
                &[
                    VersionEdit::RemoveSsts {
                        level: 0,
                        sst_ids: vec![SsTableId::new(1)],
                    },
                    VersionEdit::AddSsts {
                        level: 1,
                        entries: vec![SstEntry::new(
                            SsTableId::new(2),
                            Some(SsTableStats::default()),
                            None,
                            FusioPath::from("L1/2.parquet"),
                            None,
                        )],
                    },
                ],
            )
            .await
            .expect("second version");
        manifest
            .apply_version_edits(
                table,
                &[
                    VersionEdit::RemoveSsts {
                        level: 1,
                        sst_ids: vec![SsTableId::new(2)],
                    },
                    VersionEdit::AddSsts {
                        level: 2,
                        entries: vec![SstEntry::new(
                            SsTableId::new(3),
                            Some(SsTableStats::default()),
                            None,
                            FusioPath::from("L2/3.parquet"),
                            None,
                        )],
                    },
                ],
            )
            .await
            .expect("third version");

        let versions = manifest
            .list_versions(table, 10)
            .await
            .expect("list versions");
        assert_eq!(
            versions
                .iter()
                .map(VersionState::commit_timestamp)
                .collect::<Vec<_>>(),
            vec![Timestamp::new(3), Timestamp::new(2), Timestamp::new(1)]
        );

        let snapshot = manifest
            .snapshot_at_version(table, Timestamp::new(1))
            .await
            .expect("historical version should remain available");
        assert_eq!(
            snapshot
                .latest_version
                .as_ref()
                .map(VersionState::commit_timestamp),
            Some(Timestamp::new(1))
        );

        let root_set = manifest.current_root_set(table).await.expect("root set");
        assert_eq!(root_set.protected_version_count(), 1);
        assert!(
            !root_set.contains_path(&FusioPath::from("L0/1.parquet")),
            "historical SSTs should not be protected unless a live snapshot pins them",
        );
        assert!(root_set.contains_path(&FusioPath::from("L2/3.parquet")));
    }
}