trine-kv 0.5.13

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
#![allow(unused_imports)]

use super::*;

#[cfg(all(
    feature = "platform-io",
    feature = "platform-io-native",
    target_os = "linux"
))]
#[test]
fn platform_io_native_file_read_and_append_use_platform_driver() {
    let root = temp_storage_root("trine-kv-platform-io-storage");
    std::fs::create_dir_all(&root).expect("test dir creates");
    let table = StorageObjectId::native_file(StorageObjectKind::Table, root.join("table.trinet"));
    std::fs::write(table.path(), b"abcdef").expect("table file writes");

    let runtime = Runtime::new(RuntimeOptions::platform_io());
    let backend = NativeFileBackend::with_runtime(runtime);
    let capabilities = backend.capabilities();
    assert!(capabilities.supports(StorageCapability::PlatformAsyncIo));

    let object = backend
        .open_read_blocking(table)
        .expect("platform I/O read object opens");
    let object_len = block_on_test_future(object.len()).expect("platform I/O len completes");
    assert_eq!(object_len, 6);
    let buffer = block_on_test_future(object.read_exact_at_owned(2, 3))
        .expect("platform I/O read completes");
    assert_eq!(buffer.offset(), 2);
    assert_eq!(&*buffer.into_bytes(), b"cde");

    let wal = StorageObjectId::native_file(StorageObjectKind::Wal, root.join("trine.wal"));
    let mut append = block_on_test_future(backend.open_append(wal.clone()))
        .expect("platform I/O append object opens");
    block_on_test_future(StorageAppendObject::append(
        &mut append,
        b"first",
        DurabilityMode::Buffered,
    ))
    .expect("platform I/O append completes");
    block_on_test_future(StorageAppendObject::persist(
        &mut append,
        DurabilityMode::Buffered,
    ))
    .expect("platform I/O persist completes");
    assert_eq!(
        std::fs::read(wal.path()).expect("WAL object reads"),
        b"first"
    );

    let stats = backend.stats();
    assert_platform_task_accounting(&stats, 5, 0);
    assert!(
        stats
            .platform_io_operations
            .length_lookup
            .true_platform_async
            > 0,
        "Linux platform length lookup should report true platform async"
    );
    assert!(
        stats.platform_io_operations.random_read.true_platform_async > 0,
        "Linux platform random reads should report true platform async"
    );
    assert!(
        stats.platform_io_operations.append_open.true_platform_async > 0,
        "Linux platform append open should report true platform async"
    );
    assert!(
        stats.platform_io_operations.append.true_platform_async > 0,
        "Linux platform append should report true platform async"
    );
    assert!(
        stats.platform_io_operations.persist.true_platform_async > 0,
        "Linux platform persist should report true platform async"
    );
    assert!(
        stats
            .platform_io_operations
            .total()
            .uses_true_platform_async(),
        "Linux platform diagnostics should aggregate true platform async work"
    );

    std::fs::remove_dir_all(root).expect("test dir removes");
}

#[cfg(all(
    feature = "platform-io",
    feature = "platform-io-native",
    target_os = "linux"
))]
fn assert_platform_io_listing_managed_async(
    backend: &NativeFileBackend,
    directory: StorageDirectoryId,
    table: &StorageObjectId,
) {
    let list_request =
        StorageObjectListRequest::native_file(StorageObjectKind::Table, directory.path())
            .with_file_extension("trinet");
    let listed_objects = block_on_test_future(backend.list_objects(list_request.clone()))
        .expect("platform I/O object listing completes");
    assert_eq!(listed_objects, vec![table.clone()]);

    let listed_files = block_on_test_future(backend.list_directory_files(directory.clone()))
        .expect("platform I/O directory listing completes");
    assert!(
        listed_files.iter().any(|file| file.path() == table.path()),
        "directory listing should include the table"
    );
    assert_eq!(
        backend
            .list_objects_blocking(list_request)
            .expect("blocking object listing fallback completes"),
        vec![table.clone()]
    );
    assert!(
        backend
            .list_directory_files_blocking(directory)
            .expect("blocking directory listing fallback completes")
            .iter()
            .any(|file| file.path() == table.path()),
        "blocking directory listing should include the table"
    );
}

#[cfg(all(
    feature = "platform-io",
    feature = "platform-io-native",
    target_os = "linux"
))]
fn assert_platform_task_accounting(
    stats: &NativeFileStorageStats,
    min_driver_tasks: u64,
    min_blocking_fallback_tasks: u64,
) {
    assert!(stats.uses_platform_io_driver);
    assert!(stats.uses_platform_async_io);
    assert_eq!(stats.blocking_adapter_tasks, 0);

    let driver_tasks = stats
        .platform_async_io_tasks
        .saturating_add(stats.platform_thread_pool_managed_async_tasks);
    assert!(
        driver_tasks >= min_driver_tasks,
        "platform driver task count {driver_tasks} should be at least {min_driver_tasks}"
    );
    assert!(
        stats.platform_blocking_fallback_tasks >= min_blocking_fallback_tasks,
        "platform blocking fallback count {} should be at least {}",
        stats.platform_blocking_fallback_tasks,
        min_blocking_fallback_tasks
    );

    assert!(
        stats.platform_async_io_tasks > 0,
        "Linux platform backend should account true async work"
    );
}

#[cfg(all(
    feature = "platform-io",
    feature = "platform-io-native",
    target_os = "linux"
))]
#[test]
fn platform_io_native_file_management_ops_use_platform_driver() {
    let root = temp_storage_root("trine-kv-platform-io-management");
    let runtime = Runtime::new(RuntimeOptions::platform_io());
    let backend = NativeFileBackend::with_runtime(runtime);
    let directory = StorageDirectoryId::native_file(&root);

    block_on_test_future(backend.create_directory_all(directory.clone()))
        .expect("platform I/O directory create completes");
    assert!(root.is_dir(), "directory create should create root");

    let table = StorageObjectId::native_file(
        StorageObjectKind::Table,
        root.join("table-00000000000000000033.trinet"),
    );
    block_on_test_future(backend.write_object(
        table.clone(),
        Arc::from(&b"table bytes"[..]),
        DurabilityMode::Buffered,
    ))
    .expect("platform I/O object write completes");
    let table_bytes = block_on_test_future(backend.read_object_bytes(table.clone()))
        .expect("platform I/O object read completes")
        .expect("table object exists");
    assert_eq!(&*table_bytes, b"table bytes");

    let manifest = StorageObjectId::native_file(StorageObjectKind::Manifest, root.join("MANIFEST"));
    block_on_test_future(backend.publish_manifest(
        manifest.clone(),
        Arc::from(&b"manifest bytes"[..]),
        DurabilityMode::SyncAll,
    ))
    .expect("platform I/O manifest publish completes");
    let manifest_bytes = block_on_test_future(backend.read_current_manifest(manifest.clone()))
        .expect("platform I/O manifest read completes")
        .expect("manifest exists");
    assert_eq!(&*manifest_bytes, b"manifest bytes");

    let wal = StorageObjectId::native_file(StorageObjectKind::Wal, root.join("trine.wal"));
    let wal_tmp = StorageObjectId::native_file(StorageObjectKind::Wal, root.join("trine.wal.tmp"));
    std::fs::write(wal.path(), b"old wal").expect("old WAL writes");
    block_on_test_future(backend.rewrite_wal(
        wal.clone(),
        wal_tmp.clone(),
        Arc::from(&b"new wal"[..]),
        DurabilityMode::SyncAll,
    ))
    .expect("platform I/O WAL rewrite completes");
    assert_eq!(
        std::fs::read(wal.path()).expect("WAL object reads"),
        b"new wal"
    );
    assert!(
        !wal_tmp.path().exists(),
        "WAL rewrite should remove the temporary object"
    );

    let lease_object =
        StorageObjectId::native_file(StorageObjectKind::WriterLease, root.join("LOCK"));
    let lease = block_on_test_future(backend.acquire_writer_lease(lease_object.clone()))
        .expect("platform I/O writer lease acquires");
    assert!(
        lease_object.path().exists(),
        "writer lease marker should exist"
    );
    drop(lease);
    assert!(
        lease_object.path().exists(),
        "dropping writer lease should keep the lock file inode"
    );
    assert!(
        std::fs::read(lease_object.path())
            .expect("platform writer lease marker reads")
            .is_empty(),
        "dropping writer lease should clear owner text"
    );

    assert_platform_io_listing_managed_async(&backend, directory.clone(), &table);

    block_on_test_future(backend.sync_directory_after_renames(directory))
        .expect("platform I/O directory sync completes");
    block_on_test_future(backend.delete_object(table.clone()))
        .expect("platform I/O object delete completes");
    assert!(!table.path().exists(), "table object should be deleted");

    let stats = backend.stats();
    assert_platform_task_accounting(&stats, 11, 0);
    assert_linux_platform_management_counters(&stats);

    std::fs::remove_dir_all(root).expect("test dir removes");
}

#[cfg(all(
    feature = "platform-io",
    feature = "platform-io-native",
    target_os = "linux"
))]
fn assert_linux_platform_management_counters(stats: &NativeFileStorageStats) {
    assert!(
        stats
            .platform_io_operations
            .temp_write_rename_publish
            .true_platform_async
            > 0,
        "Linux platform publish writes should report true platform async"
    );
    assert!(
        stats.platform_io_operations.wal_rewrite.true_platform_async > 0,
        "Linux platform WAL rewrite should report true platform async"
    );
    assert!(
        stats
            .platform_io_operations
            .whole_object_read
            .true_platform_async
            > 0,
        "Linux platform whole-object reads should report true platform async"
    );
    assert!(
        stats.platform_io_operations.delete.true_platform_async > 0,
        "Linux platform deletes should report true platform async"
    );
    assert!(
        stats
            .platform_io_operations
            .directory_create
            .true_platform_async
            > 0,
        "Linux platform directory create should report true platform async"
    );
    assert!(
        stats
            .platform_io_operations
            .writer_lease
            .thread_pool_managed_async
            > 0,
        "Linux platform writer lease should report thread-pool managed async"
    );
    assert!(
        stats
            .platform_io_operations
            .total()
            .uses_true_platform_async(),
        "Linux platform management diagnostics should aggregate true platform async work"
    );
    assert!(
        stats
            .platform_io_operations
            .directory_listing
            .thread_pool_managed_async
            > 0,
        "directory listing should report thread-pool managed async"
    );
    assert!(
        stats
            .platform_io_operations
            .directory_sync
            .true_platform_async
            > 0,
        "Linux platform directory sync should report true platform async"
    );
}

#[cfg(all(
    feature = "platform-io",
    feature = "platform-io-native",
    any(
        windows,
        target_os = "macos",
        target_os = "freebsd",
        target_os = "illumos",
        target_os = "solaris"
    )
))]
#[test]
fn platform_io_partial_native_storage_ops_use_platform_driver() {
    let root = temp_storage_root("trine-kv-platform-io-partial-native-storage");
    std::fs::create_dir_all(&root).expect("test dir creates");
    let table = StorageObjectId::native_file(StorageObjectKind::Table, root.join("table.trinet"));
    std::fs::write(table.path(), b"abcdef").expect("table file writes");

    let runtime = Runtime::new(RuntimeOptions::platform_io());
    let backend = NativeFileBackend::with_runtime(runtime);
    let capabilities = backend.capabilities();
    assert!(capabilities.supports(StorageCapability::PlatformAsyncIo));
    assert!(capabilities.supports(StorageCapability::BlockingAdapter));

    let object = backend
        .open_read_blocking(table)
        .expect("read object opens with platform driver fallback");
    let buffer = block_on_test_future(object.read_exact_at_owned(2, 3))
        .expect("read completes through platform driver fallback");
    assert_eq!(buffer.offset(), 2);
    assert_eq!(&*buffer.into_bytes(), b"cde");

    let stats = backend.stats();
    assert!(!stats.uses_blocking_adapter);
    assert!(stats.uses_platform_io_driver);
    assert!(stats.uses_platform_async_io);
    assert!(
        stats.platform_async_io_tasks > 0,
        "partial native platform work should count as platform async I/O"
    );
    assert_eq!(
        stats.platform_thread_pool_managed_async_tasks, 0,
        "single random read should not be counted as thread-pool managed async"
    );
    assert_eq!(stats.platform_blocking_fallback_tasks, 0);
    assert_eq!(stats.blocking_adapter_tasks, 0);
    assert!(
        stats
            .platform_io_operations
            .random_read
            .platform_native_async_but_partial
            > 0,
        "platform random read should report partial native async"
    );
    let platform_total = stats.platform_io_operations.total();
    assert!(
        platform_total.uses_non_true_platform_async(),
        "non-Linux platform diagnostics should aggregate non-true-platform work"
    );
    assert!(
        !platform_total.uses_true_platform_async(),
        "partial native diagnostics should not report whole-operation true async work"
    );

    std::fs::remove_dir_all(root).expect("test dir removes");
}

#[cfg(all(
    feature = "platform-io",
    not(feature = "platform-io-native"),
    any(unix, windows)
))]
#[test]
fn platform_io_threadpool_storage_ops_use_platform_driver() {
    let root = temp_storage_root("trine-kv-platform-io-threadpool-storage");
    std::fs::create_dir_all(&root).expect("test dir creates");
    let table = StorageObjectId::native_file(StorageObjectKind::Table, root.join("table.trinet"));
    std::fs::write(table.path(), b"abcdef").expect("table file writes");

    let runtime = Runtime::new(RuntimeOptions::platform_io());
    let backend = NativeFileBackend::with_runtime(runtime);
    let capabilities = backend.capabilities();
    assert!(capabilities.supports(StorageCapability::PlatformAsyncIo));
    assert!(capabilities.supports(StorageCapability::BlockingAdapter));

    let object = backend
        .open_read_blocking(table)
        .expect("read object opens with platform driver fallback");
    let buffer = block_on_test_future(object.read_exact_at_owned(2, 3))
        .expect("read completes through platform driver fallback");
    assert_eq!(buffer.offset(), 2);
    assert_eq!(&*buffer.into_bytes(), b"cde");

    let stats = backend.stats();
    assert!(!stats.uses_blocking_adapter);
    assert!(stats.uses_platform_io_driver);
    assert!(stats.uses_platform_async_io);
    assert_eq!(stats.platform_async_io_tasks, 0);
    assert!(
        stats.platform_thread_pool_managed_async_tasks > 0,
        "platform driver should account thread-pool managed async tasks"
    );
    assert_eq!(stats.platform_blocking_fallback_tasks, 0);
    assert_eq!(stats.blocking_adapter_tasks, 0);
    assert!(
        stats
            .platform_io_operations
            .random_read
            .thread_pool_managed_async
            > 0,
        "platform random read should report thread-pool managed async"
    );
    let platform_total = stats.platform_io_operations.total();
    assert!(
        platform_total.uses_non_true_platform_async(),
        "thread-pool diagnostics should aggregate non-true-platform work"
    );
    assert!(
        !platform_total.uses_true_platform_async(),
        "thread-pool diagnostics should not report true platform async work"
    );

    std::fs::remove_dir_all(root).expect("test dir removes");
}