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
#[cfg(not(target_os = "wasi"))]
use super::{Arc, PathBuf, mpsc};
use super::{
    AtomicBool, DurabilityMode, Error, NativeFileBackend, Ordering, Path, PendingWalAppend, Result,
    Sequence, WAL_FILE_NAME, WAL_SHARD_FILE_DIGITS, WAL_SHARD_FILE_PREFIX, WalBatch,
    WalFrontDoorLane, WalLaneCommand, WalLaneCompletion, WalLaneReply, WalLaneWaiter, WalWriter,
    delete_confirmed_wal_marker_with_backend, invalid_wal, is_wal_rewrite_temporary_file_name,
    read_confirmed_wal_marker_with_backend, read_wal_object_with_backend_async,
    rewrite_batches_after_with_backend_async, wait_for_wal_storage_future,
    write_confirmed_wal_marker_with_backend,
};

pub(super) fn send_wal_lane_command(
    lane: &WalFrontDoorLane,
    command: impl FnOnce(WalLaneReply) -> WalLaneCommand,
) -> Result<()> {
    enqueue_wal_lane_command(lane, command)?.wait()
}

pub(super) fn enqueue_wal_lane_command(
    lane: &WalFrontDoorLane,
    command: impl FnOnce(WalLaneReply) -> WalLaneCommand,
) -> Result<WalLaneWaiter> {
    #[cfg(target_os = "wasi")]
    {
        let (reply, waiter) = WalLaneCompletion::pair();
        let mut state = lane
            .state
            .lock()
            .map_err(|_| wal_front_door_completion_poisoned())?;
        process_wal_lane_batch(
            &lane.backend,
            &lane.path,
            &lane.writer_open,
            &mut state,
            vec![command(reply)],
        );
        if waiter
            .completion
            .result
            .lock()
            .map_err(|_| wal_front_door_completion_poisoned())?
            .is_none()
        {
            waiter.completion.complete(Err(Error::Corruption {
                message: "WASI WAL lane command did not complete synchronously".to_owned(),
            }));
        }
        return Ok(waiter);
    }

    #[cfg(not(target_os = "wasi"))]
    {
        let sender = lane
            .sender
            .as_ref()
            .ok_or_else(wal_front_door_worker_stopped)?;
        let (reply, waiter) = WalLaneCompletion::pair();
        sender
            .send(command(reply))
            .map_err(|_| wal_front_door_worker_stopped())?;
        Ok(waiter)
    }
}

#[cfg(not(target_os = "wasi"))]
pub(super) fn try_enqueue_wal_lane_command(
    lane: &WalFrontDoorLane,
    command: impl FnOnce(WalLaneReply) -> WalLaneCommand,
) -> Result<WalLaneWaiter> {
    let sender = lane
        .sender
        .as_ref()
        .ok_or_else(wal_front_door_worker_stopped)?;
    let (reply, waiter) = WalLaneCompletion::pair();
    match sender.try_send(command(reply)) {
        Ok(()) => Ok(waiter),
        Err(mpsc::TrySendError::Full(_)) => Err(Error::runtime_busy(
            "WAL admission queue is full; retry after pending commits drain",
        )),
        Err(mpsc::TrySendError::Disconnected(_)) => Err(wal_front_door_worker_stopped()),
    }
}

#[allow(clippy::needless_pass_by_value)]
/// Maximum commits coalesced into one group-commit fsync. Bounds the latency and
/// memory of a single drain pass when the queue is flooded.
#[cfg(not(target_os = "wasi"))]
pub(super) const WAL_LANE_BATCH_MAX: usize = 1024;

#[derive(Debug, Default)]
pub(super) struct WalLaneWorkerState {
    writer: Option<WalWriter>,
    persisted_level: Option<DurabilityMode>,
    last_appended_sequence: Option<Sequence>,
    confirmed_sequence: Option<Sequence>,
    terminal_error: Option<String>,
}

// Thread entry point: it owns its lane state for the worker's lifetime.
#[allow(clippy::needless_pass_by_value)]
#[cfg(not(target_os = "wasi"))]
pub(super) fn run_wal_lane_worker(
    backend: NativeFileBackend,
    path: PathBuf,
    writer_open: Arc<AtomicBool>,
    receiver: mpsc::Receiver<WalLaneCommand>,
) {
    let mut state = WalLaneWorkerState::default();
    // Group commit: block for one command, then drain everything already queued
    // and serve the whole batch with a single fsync. Concurrent writers (or one
    // writer with many in-flight async commits) amortize the fsync cost; each
    // writer is still only completed after the fsync that covers its frame.
    while let Ok(first) = receiver.recv() {
        let mut batch = Vec::with_capacity(WAL_LANE_BATCH_MAX);
        batch.push(first);
        while batch.len() < WAL_LANE_BATCH_MAX {
            match receiver.try_recv() {
                Ok(command) => batch.push(command),
                Err(_) => break,
            }
        }
        let panic_replies = batch.iter().map(WalLaneCommand::reply).collect::<Vec<_>>();
        if std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            process_wal_lane_batch(&backend, &path, &writer_open, &mut state, batch);
        }))
        .is_err()
        {
            writer_open.store(false, Ordering::Release);
            for reply in panic_replies {
                reply.complete(Err(wal_lane_worker_panicked()));
            }
            // Remain alive only to fail commands that raced with the panic.
            // Dropping queued replies would strand their waiters forever.
            while let Ok(command) = receiver.recv() {
                command.reply().complete(Err(wal_lane_worker_panicked()));
            }
            return;
        }
    }
}

#[cfg(not(target_os = "wasi"))]
fn wal_lane_worker_panicked() -> Error {
    Error::Corruption {
        message:
            "WAL lane worker panicked after storage mutation may have started; reopen the database"
                .to_owned(),
    }
}

pub(super) fn process_wal_lane_batch(
    backend: &NativeFileBackend,
    path: &Path,
    writer_open: &AtomicBool,
    state: &mut WalLaneWorkerState,
    batch: Vec<WalLaneCommand>,
) {
    // Appended-but-not-yet-persisted waiters and the strongest durability any
    // of them requested. They are completed together by the next persist.
    let mut pending: Vec<PendingWalAppend> = Vec::new();
    let mut pending_durability = DurabilityMode::Buffered;

    for command in batch {
        if let Some(message) = state.terminal_error.as_deref() {
            command.reply().complete(Err(wal_lane_failed(message)));
            continue;
        }
        match command {
            WalLaneCommand::Append {
                sequence,
                frame,
                durability,
                reply,
            } => process_wal_append(
                backend,
                path,
                writer_open,
                state,
                &mut pending,
                &mut pending_durability,
                PendingWalAppend { sequence, reply },
                &frame,
                durability,
            ),
            WalLaneCommand::Persist { durability, reply } => {
                let combined =
                    if wal_durability_rank(durability) > wal_durability_rank(pending_durability) {
                        durability
                    } else {
                        pending_durability
                    };
                let result =
                    flush_wal_lane_batch(backend, path, writer_open, state, combined, &mut pending);
                reply.complete(duplicate_wal_lane_result(&result));
                pending_durability = DurabilityMode::Buffered;
            }
            WalLaneCommand::Rewrite {
                replay_floor,
                reply,
            } => {
                // A rewrite changes the file; flush queued appends first.
                let flush_result = flush_wal_lane_batch(
                    backend,
                    path,
                    writer_open,
                    state,
                    pending_durability,
                    &mut pending,
                );
                pending_durability = DurabilityMode::Buffered;
                let result = flush_result.and_then(|()| {
                    rewrite_wal_lane_after_replay_floor(
                        backend,
                        path,
                        &mut state.writer,
                        &mut state.persisted_level,
                        replay_floor,
                    )
                });
                if let Err(error) = &result
                    && state.terminal_error.is_none()
                {
                    let message = format!(
                        "WAL rewrite failed after the file namespace may have changed: {error}; \
                         the lane is closed until database reopen"
                    );
                    state.writer = None;
                    state.persisted_level = None;
                    state.terminal_error = Some(message);
                    writer_open.store(false, Ordering::Release);
                }
                reply.complete(result);
            }
        }
    }

    let _ = flush_wal_lane_batch(
        backend,
        path,
        writer_open,
        state,
        pending_durability,
        &mut pending,
    );
}

#[allow(clippy::too_many_arguments)]
fn process_wal_append(
    backend: &NativeFileBackend,
    path: &Path,
    writer_open: &AtomicBool,
    state: &mut WalLaneWorkerState,
    pending: &mut Vec<PendingWalAppend>,
    pending_durability: &mut DurabilityMode,
    append: PendingWalAppend,
    frame: &[u8],
    durability: DurabilityMode,
) {
    // Append without persisting; the batch persist below covers it.
    match append_wal_lane_frame(
        backend,
        path,
        &mut state.writer,
        writer_open,
        frame,
        DurabilityMode::Buffered,
    ) {
        Ok(()) => {
            if wal_durability_rank(durability) > wal_durability_rank(*pending_durability) {
                *pending_durability = durability;
            }
            // Mark the lane dirty: these bytes are not yet covered by the
            // requested storage boundary, so a later persist must touch the
            // backend.
            state.persisted_level = Some(DurabilityMode::Buffered);
            state.last_appended_sequence = Some(append.sequence);
            pending.push(append);
        }
        Err(error) => {
            let message = format!(
                "WAL append failed after the file may have changed: {error}; \
                 the lane is closed until database reopen"
            );
            state.writer = None;
            state.persisted_level = None;
            state.terminal_error = Some(message.clone());
            writer_open.store(false, Ordering::Release);
            for pending_append in pending.drain(..) {
                pending_append
                    .reply
                    .complete(Err(wal_lane_failed(message.as_str())));
            }
            append.reply.complete(Err(error));
        }
    }
}

/// Persist the buffered appends with one backend request and complete waiters.
///
/// When `pending` is non-empty there are freshly appended bytes, so a persist at
/// `durability` is forced; for a standalone persist with no new appends the
/// existing `persisted_level` can satisfy it without a redundant backend call.
pub(super) fn flush_wal_lane_batch(
    backend: &NativeFileBackend,
    path: &Path,
    writer_open: &AtomicBool,
    state: &mut WalLaneWorkerState,
    durability: DurabilityMode,
    pending: &mut Vec<PendingWalAppend>,
) -> Result<()> {
    let has_new_appends = !pending.is_empty();
    let pending_max_sequence = pending.iter().map(|append| append.sequence).max();
    let result = persist_wal_lane_batch(
        backend,
        path,
        state,
        durability,
        has_new_appends,
        pending_max_sequence.or(state.last_appended_sequence),
    );
    if let Err(error) = &result {
        let message = format!(
            "WAL persist failed with an uncertain durability boundary: {error}; \
             the lane is closed until database reopen"
        );
        state.writer = None;
        state.persisted_level = None;
        state.terminal_error = Some(message);
        writer_open.store(false, Ordering::Release);
    }
    for pending in pending.drain(..) {
        let reply = pending.reply;
        reply.complete(duplicate_wal_lane_result(&result));
    }
    result
}

fn wal_lane_failed(message: &str) -> Error {
    Error::Corruption {
        message: message.to_owned(),
    }
}

pub(super) fn persist_wal_lane_batch(
    backend: &NativeFileBackend,
    path: &Path,
    state: &mut WalLaneWorkerState,
    durability: DurabilityMode,
    has_new_appends: bool,
    confirm_sequence: Option<Sequence>,
) -> Result<()> {
    let Some(writer) = state.writer.as_mut() else {
        return Ok(());
    };
    if !wal_durability_requires_persist(durability) {
        return Ok(());
    }
    // Freshly appended bytes are unpersisted, so a backend persist is mandatory;
    // a standalone persist can skip when the lane is already covered.
    if has_new_appends || wal_lane_needs_persist(state.persisted_level, durability) {
        writer.persist(durability)?;
        state.persisted_level = Some(durability);
    }
    if let Some(sequence) = confirm_sequence
        && state
            .confirmed_sequence
            .is_none_or(|confirmed| sequence > confirmed)
    {
        write_confirmed_wal_marker_with_backend(backend, path, sequence, durability)?;
        state.confirmed_sequence = Some(sequence);
    }
    Ok(())
}

pub(super) const fn wal_durability_requires_persist(durability: DurabilityMode) -> bool {
    wal_durability_rank(durability) >= wal_durability_rank(DurabilityMode::Flush)
}

/// `Error` is not `Clone`, so reproduce it for each fan-out waiter, preserving
/// the I/O error kind and message for the common fsync-failure case.
pub(super) fn duplicate_wal_lane_result(result: &Result<()>) -> Result<()> {
    match result {
        Ok(()) => Ok(()),
        Err(Error::Io(error)) => Err(Error::Io(std::io::Error::new(
            error.kind(),
            error.to_string(),
        ))),
        Err(error) => Err(Error::Corruption {
            message: format!("group commit persist failed: {error}"),
        }),
    }
}

pub(super) fn append_wal_lane_frame(
    backend: &NativeFileBackend,
    path: &Path,
    writer: &mut Option<WalWriter>,
    writer_open: &AtomicBool,
    frame: &[u8],
    durability: DurabilityMode,
) -> Result<()> {
    if writer.is_none() {
        *writer = Some(WalWriter::open_append_with_backend(backend, path)?);
        writer_open.store(true, Ordering::Release);
    }
    writer
        .as_mut()
        .expect("writer opens before append")
        .append_frame(frame, durability)
}

pub(super) fn persist_wal_lane(
    writer: &mut Option<WalWriter>,
    persisted_level: &mut Option<DurabilityMode>,
    durability: DurabilityMode,
) -> Result<()> {
    if let Some(writer) = writer.as_mut()
        && wal_lane_needs_persist(*persisted_level, durability)
    {
        writer.persist(durability)?;
        *persisted_level = Some(durability);
    }
    Ok(())
}

pub(super) fn wal_lane_needs_persist(
    persisted_level: Option<DurabilityMode>,
    durability: DurabilityMode,
) -> bool {
    persisted_level.is_none_or(|level| wal_durability_rank(level) < wal_durability_rank(durability))
}

pub(super) const fn wal_durability_rank(mode: DurabilityMode) -> u8 {
    match mode {
        DurabilityMode::Buffered => 0,
        DurabilityMode::Flush => 1,
        DurabilityMode::SyncData => 2,
        DurabilityMode::SyncAll => 3,
        DurabilityMode::SyncAllStrict => 4,
    }
}

pub(super) fn rewrite_wal_lane_after_replay_floor(
    backend: &NativeFileBackend,
    path: &Path,
    writer: &mut Option<WalWriter>,
    persisted_level: &mut Option<DurabilityMode>,
    replay_floor: Sequence,
) -> Result<()> {
    let rewrite_durability = filesystem_wal_rewrite_durability();
    if writer.is_some() {
        persist_wal_lane(writer, persisted_level, rewrite_durability)?;
    } else if wait_for_wal_storage_future(read_wal_object_with_backend_async(backend, path))?
        .is_none()
    {
        return Ok(());
    }
    wait_for_wal_storage_future(rewrite_batches_after_with_backend_async(
        backend,
        path,
        replay_floor,
    ))?;
    if read_confirmed_wal_marker_with_backend(backend, path)?
        .is_some_and(|sequence| sequence <= replay_floor)
    {
        delete_confirmed_wal_marker_with_backend(backend, path)?;
    }
    if let Some(writer) = writer.as_mut() {
        writer.reopen_append_with_backend(backend, path)?;
        *persisted_level = Some(rewrite_durability);
    }
    Ok(())
}

const fn filesystem_wal_rewrite_durability() -> DurabilityMode {
    #[cfg(target_os = "wasi")]
    {
        DurabilityMode::Flush
    }

    #[cfg(not(target_os = "wasi"))]
    {
        DurabilityMode::SyncAll
    }
}

#[cfg(not(target_os = "wasi"))]
pub(super) fn wal_front_door_worker_stopped() -> Error {
    Error::Corruption {
        message: "WAL front door worker stopped".to_owned(),
    }
}

pub(super) fn wal_front_door_completion_poisoned() -> Error {
    Error::runtime_busy("WAL front door completion state is poisoned")
}

pub(super) fn validate_wal_stream_order(batches: &[WalBatch]) -> Result<()> {
    let mut last_seen = Sequence::ZERO;
    for batch in batches {
        if batch.sequence <= last_seen {
            return Err(invalid_wal("WAL stream sequence did not increase"));
        }
        last_seen = batch.sequence;
    }
    Ok(())
}

pub(super) fn wal_shard_index_from_path(path: &Path) -> Result<usize> {
    let file_name = path
        .file_name()
        .and_then(|name| name.to_str())
        .ok_or_else(|| Error::Corruption {
            message: format!("WAL file name is not valid UTF-8: {}", path.display()),
        })?;
    wal_shard_index_from_file_name(file_name)?.ok_or_else(|| Error::Corruption {
        message: format!("not a WAL shard file: {}", path.display()),
    })
}

pub(super) fn wal_shard_index_from_file_name(file_name: &str) -> Result<Option<usize>> {
    if file_name == WAL_FILE_NAME {
        return Ok(Some(0));
    }
    if is_wal_rewrite_temporary_file_name(file_name) {
        return Ok(None);
    }
    wal_shard_index_from_final_file_name(file_name)
}

pub(super) fn wal_shard_index_from_final_file_name(file_name: &str) -> Result<Option<usize>> {
    let Some(suffix) = file_name.strip_prefix(WAL_SHARD_FILE_PREFIX) else {
        return Ok(None);
    };
    if suffix.len() != WAL_SHARD_FILE_DIGITS || !suffix.bytes().all(|byte| byte.is_ascii_digit()) {
        return Err(Error::Corruption {
            message: format!("malformed WAL shard file name: {file_name}"),
        });
    }
    let shard_index = suffix.parse::<usize>().map_err(|error| Error::Corruption {
        message: format!("malformed WAL shard file name {file_name}: {error}"),
    })?;
    if shard_index == 0 {
        return Err(Error::Corruption {
            message: "WAL shard 0 must use the legacy trine.wal file name".to_owned(),
        });
    }
    Ok(Some(shard_index))
}