trine-kv 0.6.0

Embedded LSM MVCC key-value database.
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
use super::*;

#[test]
fn physical_content_reclamation_is_disabled_by_default() {
    assert_eq!(
        DbOptions::memory().content_reclamation,
        ContentReclamationMode::Disabled
    );
}

#[test]
fn wasi_storage_mode_does_not_alias_the_qualified_native_backend() {
    let options = DbOptions::wasi_persistent("wasi-content-reclaim-disabled")
        .with_content_reclamation(ContentReclamationMode::QualifiedNativeFilesystem);
    assert!(matches!(
        options.storage_mode,
        StorageMode::HostPersistent {
            backend: HostStorageBackend::Wasi { .. },
        }
    ));
    assert_eq!(
        options.content_reclamation,
        ContentReclamationMode::QualifiedNativeFilesystem
    );
}

#[test]
fn wasi_and_browser_reclamation_use_distinct_qualifications() {
    let wasi = DbOptions::wasi_persistent("wasi-content-reclaim-qualified")
        .with_content_reclamation(ContentReclamationMode::QualifiedWasiFilesystem);
    assert_eq!(
        wasi.content_reclamation,
        ContentReclamationMode::QualifiedWasiFilesystem
    );
    let browser = DbOptions::browser_persistent()
        .with_content_reclamation(ContentReclamationMode::QualifiedBrowserStorage);
    assert_eq!(
        browser.content_reclamation,
        ContentReclamationMode::QualifiedBrowserStorage
    );
}

#[test]
fn empty_content_upload_has_a_durable_zero_byte_reservation_and_seals() {
    block_on(async {
        let db = Db::open(DbOptions::memory())
            .await
            .expect("memory db opens");
        let upload = db
            .begin_content_upload(test_upload_options().with_expected_length(0))
            .await
            .expect("empty upload begins");
        let sealed = upload.seal().await.expect("empty upload seals");
        assert_eq!(sealed.content_id(), ContentId::for_bytes(b""));
        assert_eq!(sealed.len(), 0);
        let content = db
            .open_content(sealed.storage_domain_id(), sealed.content_id())
            .await
            .expect("empty content opens");
        assert_eq!(content.len(), 0);
        assert!(
            content
                .read_range(0, 1)
                .await
                .expect("empty read")
                .is_empty()
        );
    });
}

#[test]
fn object_store_reclamation_qualification_is_not_transferable_between_clients() {
    block_on(async {
        let qualified_client: Arc<dyn ObjectClient> = Arc::new(InMemoryObjectStore::new());
        let other_client = Arc::new(InMemoryObjectStore::new());
        let qualification = qualify_object_store_reclamation(
            Arc::clone(&qualified_client),
            "client-bound-reclamation",
            ObjectStoreReclamationAttestation::new(
                ObjectStoreReclamationEvidenceDigest::for_bytes(b"client-bound evidence"),
            ),
        )
        .await
        .expect("first client qualifies");
        assert!(matches!(
            Db::open_object_store_at(
                other_client,
                "client-bound-reclamation",
                DbOptions::object_store().with_content_reclamation(
                    ContentReclamationMode::QualifiedObjectStore(qualification),
                ),
            )
            .await,
            Err(Error::UnsupportedBackend { .. })
        ));
    });
}

#[test]
fn caller_supplied_upload_id_binds_options_and_recovers_exact_state() {
    block_on(async {
        let db = Db::open(DbOptions::memory()).await.expect("open database");
        let upload_id = UploadId::new().expect("upload identity");
        let options = test_upload_options().with_expected_length(6);

        let ContentUploadResume::Open(mut first) = db
            .begin_content_upload_with_id(upload_id, options)
            .await
            .expect("first begin")
        else {
            panic!("first begin must be open");
        };
        first.write(b"abc").await.expect("write prefix");
        drop(first);

        let different = options.with_expected_length(7);
        assert!(matches!(
            db.begin_content_upload_with_id(upload_id, different).await,
            Err(Error::InvalidOptions { .. })
        ));

        let ContentUploadResume::Open(mut retry) = db
            .begin_content_upload_with_id(upload_id, options)
            .await
            .expect("exact retry")
        else {
            panic!("retry before seal must be open");
        };
        assert_eq!(retry.len(), 3);
        retry.write(b"def").await.expect("write suffix");
        let sealed = retry.seal().await.expect("seal");

        let ContentUploadResume::Sealed(recovered) = db
            .begin_content_upload_with_id(upload_id, options)
            .await
            .expect("sealed retry")
        else {
            panic!("retry after seal must recover sealed result");
        };
        assert_eq!(recovered, sealed);
    });
}

#[test]
#[allow(clippy::too_many_lines)] // one end-to-end lifecycle assertion is clearer than split setup
fn upload_maintenance_reclaims_orphans_and_prunes_only_sealed_idempotency_state() {
    block_on(async {
        let db = Db::open(DbOptions::memory()).await.expect("open database");
        let domain = test_scope().storage_domain_id();
        let abandoned_id = UploadId::new().expect("abandoned upload identity");
        let ContentUploadResume::Open(mut abandoned) = db
            .begin_content_upload_with_id(
                abandoned_id,
                test_upload_options().with_expected_length(6),
            )
            .await
            .expect("abandoned upload begins")
        else {
            panic!("new upload is open");
        };
        abandoned
            .write(b"abc")
            .await
            .expect("partial bytes persist");
        drop(abandoned);

        let listed = db.list_content_uploads().await.expect("uploads list");
        assert_eq!(listed.len(), 1);
        assert_eq!(listed[0].upload_id(), abandoned_id);
        assert_eq!(listed[0].state(), ContentUploadState::Open);
        assert_eq!(listed[0].len(), 3);
        assert!(listed[0].updated_at_unix_ms() > 0);
        assert_eq!(
            db.content_physical_quota(domain)
                .await
                .expect("reserved quota reads")
                .upload_reserved_bytes(),
            6
        );

        let reaped = db
            .reap_inactive_content_uploads(u64::MAX)
            .await
            .expect("inactive upload is reaped");
        assert_eq!(reaped.scanned(), 1);
        assert_eq!(reaped.aborted(), 1);
        assert!(
            db.list_content_uploads()
                .await
                .expect("uploads relist")
                .is_empty()
        );
        assert_eq!(
            db.content_physical_quota(domain)
                .await
                .expect("released quota reads")
                .upload_reserved_bytes(),
            0
        );
        assert!(matches!(
            db.resume_content_upload(abandoned_id).await,
            Err(Error::ContentUploadNotFound { .. })
        ));
        assert!(matches!(
            db.begin_content_upload_with_id(
                abandoned_id,
                test_upload_options().with_expected_length(1)
            )
            .await,
            Err(Error::ContentUploadNotFound { .. })
        ));

        let sealed_id = UploadId::new().expect("sealed upload identity");
        let ContentUploadResume::Open(mut upload) = db
            .begin_content_upload_with_id(sealed_id, test_upload_options().with_expected_length(3))
            .await
            .expect("sealed upload begins")
        else {
            panic!("new upload is open");
        };
        upload.write(b"xyz").await.expect("sealed bytes write");
        let sealed = upload.seal().await.expect("upload seals");
        let pruned = db
            .prune_sealed_content_uploads(u64::MAX)
            .await
            .expect("sealed state prunes");
        assert_eq!(pruned.pruned_sealed(), 1);
        assert!(matches!(
            db.resume_content_upload(sealed_id).await,
            Err(Error::ContentUploadSealed { .. })
        ));
        assert!(matches!(
            db.begin_content_upload_with_id(
                sealed_id,
                test_upload_options().with_expected_length(4)
            )
            .await,
            Err(Error::ContentUploadSealed { .. })
        ));
        let content = db
            .open_content(domain, sealed.content_id())
            .await
            .expect("content remains after idempotency state prune");
        assert_eq!(
            content
                .read_range(0, 3)
                .await
                .expect("content reads")
                .as_ref(),
            b"xyz"
        );
    });
}

#[test]
fn upload_maintenance_revisits_tombstones_to_remove_late_visible_chunks() {
    block_on(async {
        let db = Db::open(DbOptions::memory()).await.expect("open database");

        let aborted_id = UploadId::new().expect("aborted upload identity");
        let ContentUploadResume::Open(upload) = db
            .begin_content_upload_with_id(aborted_id, test_upload_options())
            .await
            .expect("aborted upload begins")
        else {
            panic!("new upload is open");
        };
        upload.abort().await.expect("upload aborts");
        db.write_content_partial_chunk(aborted_id, 0, 99, Arc::from(b"late".as_slice()))
            .await
            .expect("plant late-visible aborted chunk");

        db.reap_inactive_content_uploads(u64::MAX)
            .await
            .expect("aborted tombstone cleanup");
        assert!(
            db.read_content_partial_chunk(aborted_id, 0, 99)
                .await
                .expect("read aborted orphan")
                .is_none()
        );

        let sealed_id = UploadId::new().expect("sealed upload identity");
        let domain = test_scope().storage_domain_id();
        let ContentUploadResume::Open(mut upload) = db
            .begin_content_upload_with_id(sealed_id, test_upload_options().with_expected_length(3))
            .await
            .expect("sealed upload begins")
        else {
            panic!("new upload is open");
        };
        upload.write(b"xyz").await.expect("sealed bytes write");
        let sealed = upload.seal().await.expect("upload seals");
        db.prune_sealed_content_uploads(u64::MAX)
            .await
            .expect("sealed state retires");
        db.write_content_partial_chunk(sealed_id, 0, 100, Arc::from(b"late".as_slice()))
            .await
            .expect("plant late-visible sealed partial");

        db.prune_sealed_content_uploads(u64::MAX)
            .await
            .expect("sealed tombstone cleanup");
        assert!(
            db.read_content_partial_chunk(sealed_id, 0, 100)
                .await
                .expect("read sealed orphan")
                .is_none()
        );
        let content = db
            .open_content(domain, sealed.content_id())
            .await
            .expect("canonical sealed content survives");
        assert_eq!(
            content
                .read_range(0, 3)
                .await
                .expect("content reads")
                .as_ref(),
            b"xyz"
        );
    });
}

#[test]
fn object_store_upload_state_rejects_a_stale_revision_transition() {
    block_on(async {
        let client = Arc::new(InMemoryObjectStore::new());
        let db = Db::open_object_store(client, DbOptions::object_store())
            .await
            .expect("open object store");
        let upload_id = UploadId::new().expect("upload identity");
        let ContentUploadResume::Open(mut upload) = db
            .begin_content_upload_with_id(upload_id, test_upload_options().with_expected_length(1))
            .await
            .expect("begin upload")
        else {
            panic!("new upload is open");
        };
        let stale: UploadSessionState = db
            .require_upload_state(upload_id)
            .await
            .expect("read initial state");
        upload.write(b"x").await.expect("advance durable revision");

        let stale_transition = stale.into_aborting().expect("build stale transition");
        assert!(matches!(
            db.write_upload_state(&stale_transition).await,
            Err(Error::ContentUploadConflict { .. })
        ));
        assert_eq!(
            db.require_upload_state(upload_id)
                .await
                .expect("current state remains")
                .revision(),
            1
        );
    });
}

#[test]
fn object_store_upload_can_durably_extend_the_same_partial_chunk() {
    block_on(async {
        let client = Arc::new(InMemoryObjectStore::new());
        let db = Db::open_object_store(client, DbOptions::object_store())
            .await
            .expect("open object store");
        let mut upload = db
            .begin_content_upload(test_upload_options().with_expected_length(2))
            .await
            .expect("begin upload");

        upload
            .write(b"a")
            .await
            .expect("write first partial prefix");
        upload
            .write(b"b")
            .await
            .expect("extend the same staging chunk");
        let sealed = upload.seal().await.expect("seal extended upload");
        let content = db
            .open_content(sealed.storage_domain_id(), sealed.content_id())
            .await
            .expect("open sealed content");
        assert_eq!(
            content
                .read_range(0, 2)
                .await
                .expect("read content")
                .as_ref(),
            b"ab"
        );
    });
}

#[test]
fn physical_quota_reserves_streams_reconciles_and_releases() {
    block_on(async {
        let db = Db::open(DbOptions::memory()).await.expect("open database");
        let domain = test_scope().storage_domain_id();
        let configured = db
            .set_content_physical_quota(domain, Some(8))
            .await
            .expect("physical quota configures");
        assert_eq!(configured.accounted_bytes(), 0);
        assert_eq!(configured.remaining(), Some(8));

        let upload_id = UploadId::new().expect("known upload identity");
        let ContentUploadResume::Open(known) = db
            .begin_content_upload_with_id(upload_id, test_upload_options().with_expected_length(6))
            .await
            .expect("known upload reserves")
        else {
            panic!("new known upload is open");
        };
        assert_eq!(
            db.content_physical_quota(domain)
                .await
                .expect("known reservation reads")
                .upload_reserved_bytes(),
            6
        );
        assert!(matches!(
            db.begin_content_upload(test_upload_options().with_expected_length(3))
                .await,
            Err(Error::ContentPhysicalQuotaExceeded {
                limit: 8,
                requested_bytes: 3,
                ..
            })
        ));
        known.abort().await.expect("known reservation aborts");
        assert_eq!(
            db.content_physical_quota(domain)
                .await
                .expect("released reservation reads")
                .accounted_bytes(),
            0
        );

        let mut streamed = db
            .begin_content_upload(test_upload_options())
            .await
            .expect("unknown upload begins");
        streamed
            .write(b"abcde")
            .await
            .expect("stream reserves bytes");
        assert_eq!(
            db.content_physical_quota(domain)
                .await
                .expect("stream reservation reads")
                .upload_reserved_bytes(),
            5
        );
        assert!(matches!(
            streamed.write(b"wxyz").await,
            Err(Error::ContentPhysicalQuotaExceeded {
                limit: 8,
                requested_bytes: 4,
                ..
            })
        ));
        let sealed = streamed.seal().await.expect("stream seals");
        assert_eq!(sealed.len(), 5);
        let reconciled = db
            .content_physical_quota(domain)
            .await
            .expect("sealed accounting reads");
        assert_eq!(reconciled.unique_content_bytes(), 5);
        assert_eq!(reconciled.upload_reserved_bytes(), 0);
        assert_eq!(reconciled.remaining(), Some(3));
        assert!(matches!(
            db.set_content_physical_quota(domain, Some(4)).await,
            Err(Error::ContentPhysicalQuotaExceeded {
                limit: 4,
                requested_bytes: 0,
                ..
            })
        ));
    });
}

#[test]
fn expected_upload_length_is_a_pre_write_physical_quota_boundary() {
    block_on(async {
        let db = Db::open(DbOptions::memory()).await.expect("open database");
        let domain = test_scope().storage_domain_id();
        db.set_content_physical_quota(domain, Some(1))
            .await
            .expect("physical quota configures");

        let mut upload = db
            .begin_content_upload(test_upload_options().with_expected_length(1))
            .await
            .expect("expected upload reserves");
        assert!(matches!(
            upload.write(b"too large").await,
            Err(Error::ContentLengthMismatch {
                expected: 1,
                actual: 9
            })
        ));
        assert_eq!(upload.len(), 0);
        assert_eq!(upload.buffered_bytes(), 0);
        let quota = db
            .content_physical_quota(domain)
            .await
            .expect("rejected write leaves accounting readable");
        assert_eq!(quota.upload_reserved_bytes(), 1);
        assert_eq!(quota.accounted_bytes(), 1);

        upload
            .write(b"x")
            .await
            .expect("writer remains usable at the declared boundary");
        let sealed = upload.seal().await.expect("bounded upload seals");
        assert_eq!(sealed.len(), 1);
        let quota = db
            .content_physical_quota(domain)
            .await
            .expect("sealed accounting reads");
        assert_eq!(quota.unique_content_bytes(), 1);
        assert_eq!(quota.upload_reserved_bytes(), 0);
    });
}

#[test]
fn physical_quota_concurrent_reservations_do_not_overcommit() {
    block_on(async {
        let db = Db::open(DbOptions::memory()).await.expect("open database");
        let domain = test_scope().storage_domain_id();
        db.set_content_physical_quota(domain, Some(10))
            .await
            .expect("physical quota configures");
        let options = test_upload_options().with_expected_length(6);
        let (first, second) = futures::join!(
            db.begin_content_upload(options),
            db.begin_content_upload(options)
        );
        let successes = usize::from(first.is_ok()) + usize::from(second.is_ok());
        let rejections = usize::from(matches!(
            &first,
            Err(Error::ContentPhysicalQuotaExceeded { .. } | Error::Conflict { .. })
        )) + usize::from(matches!(
            &second,
            Err(Error::ContentPhysicalQuotaExceeded { .. } | Error::Conflict { .. })
        ));
        assert_eq!((successes, rejections), (1, 1));
        assert_eq!(
            db.content_physical_quota(domain)
                .await
                .expect("concurrent accounting reads")
                .upload_reserved_bytes(),
            6
        );
        if let Ok(upload) = first {
            upload.abort().await.expect("first winner aborts");
        }
        if let Ok(upload) = second {
            upload.abort().await.expect("second winner aborts");
        }
        assert_eq!(
            db.content_physical_quota(domain)
                .await
                .expect("released concurrent accounting reads")
                .accounted_bytes(),
            0
        );
    });
}

#[test]
fn physical_quota_reservation_survives_reopen_and_resume() {
    let path = temp_db_path("content-physical-quota-reopen");
    let upload_id = UploadId::new().expect("upload identity generates");
    block_on(async {
        let db = Db::open(DbOptions::new(&path))
            .await
            .expect("database opens");
        let domain = test_scope().storage_domain_id();
        db.set_content_physical_quota(domain, Some(32))
            .await
            .expect("physical quota configures");
        let ContentUploadResume::Open(mut upload) = db
            .begin_content_upload_with_id(upload_id, test_upload_options())
            .await
            .expect("unknown upload begins")
        else {
            panic!("new upload is open");
        };
        upload.write(b"durable").await.expect("prefix writes");
        drop(upload);
        assert_eq!(
            db.content_physical_quota(domain)
                .await
                .expect("reservation reads")
                .upload_reserved_bytes(),
            7
        );
        db.close().await.expect("database closes");
    });
    block_on(async {
        let db = Db::open(DbOptions::new(&path))
            .await
            .expect("database reopens");
        let domain = test_scope().storage_domain_id();
        assert_eq!(
            db.content_physical_quota(domain)
                .await
                .expect("reopened reservation reads")
                .upload_reserved_bytes(),
            7
        );
        let ContentUploadResume::Open(upload) = db
            .resume_content_upload(upload_id)
            .await
            .expect("upload resumes")
        else {
            panic!("resumed upload is open");
        };
        assert_eq!(upload.len(), 7);
        upload.abort().await.expect("resumed upload aborts");
        assert_eq!(
            db.content_physical_quota(domain)
                .await
                .expect("reopened release reads")
                .accounted_bytes(),
            0
        );
        db.close().await.expect("database closes after release");
    });
    std::fs::remove_dir_all(path).expect("test database removes");
}

#[test]
fn aborting_upload_retains_quota_until_chunk_cleanup_recovers() {
    let path = temp_db_path("content-physical-quota-abort-recovery");
    let upload_id = UploadId::new().expect("upload identity generates");
    block_on(async {
        let db = Db::open(DbOptions::new(&path))
            .await
            .expect("database opens");
        let domain = test_scope().storage_domain_id();
        db.set_content_physical_quota(domain, Some(32))
            .await
            .expect("physical quota configures");
        let ContentUploadResume::Open(mut upload) = db
            .begin_content_upload_with_id(upload_id, test_upload_options())
            .await
            .expect("upload begins")
        else {
            panic!("new upload is open");
        };
        upload
            .write(b"durable")
            .await
            .expect("partial chunk writes");
        let delete_fault = StorageFaultGuard::install(
            &path,
            StorageFaultPoint::ObjectDelete,
            Some(StorageObjectKind::ContentChunk),
            1,
        );
        assert!(matches!(upload.abort().await, Err(Error::Io(_))));
        assert_eq!(
            db.content_physical_quota(domain)
                .await
                .expect("failed abort accounting reads")
                .upload_reserved_bytes(),
            7
        );
        drop(delete_fault);
        db.close().await.expect("database closes");
    });
    block_on(async {
        let db = Db::open(DbOptions::new(&path))
            .await
            .expect("database reopens");
        assert!(matches!(
            db.resume_content_upload(upload_id).await,
            Err(Error::ContentUploadNotFound { .. })
        ));
        assert_eq!(
            db.content_physical_quota(test_scope().storage_domain_id())
                .await
                .expect("recovered abort accounting reads")
                .accounted_bytes(),
            0
        );
        db.close().await.expect("database closes after recovery");
    });
    std::fs::remove_dir_all(path).expect("test database removes");
}