Skip to main content

lora_wal/
config.rs

1use std::path::PathBuf;
2
3/// Durability mode for committed transactions.
4///
5/// The engine has at most one concurrent committer at a time, so the
6/// classical group-commit win — overlapping fsyncs across many committers —
7/// does not apply here. [`SyncMode::PerCommit`] is the default. The other two
8/// modes exist for narrow operational profiles.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
10pub enum SyncMode {
11    /// `fsync` the active segment before the committing thread releases
12    /// the store write lock. The strongest durability guarantee the WAL
13    /// offers; every observed query result is fully durable.
14    #[default]
15    PerCommit,
16
17    /// Write commit bytes to the OS immediately, then `fsync` on a fixed
18    /// cadence on a background thread. This survives ordinary process death
19    /// after `flush()` returns, but can still trade the last `interval_ms` of
20    /// commits on power loss or kernel crash for higher throughput on
21    /// bulk-load workloads.
22    ///
23    /// A background fsync failure poisons the WAL: the next `commit` /
24    /// `flush` / `force_fsync` returns [`crate::WalError::Poisoned`] and
25    /// `Wal::bg_failure` reports the underlying cause. Operators
26    /// inspect that via `/admin/wal/status` (`bgFailure`) and recover
27    /// by restarting from the last consistent snapshot + WAL.
28    Group { interval_ms: u32 },
29
30    /// Append but never `fsync` from the WAL; rely on whatever the OS
31    /// happens to flush. Intended for CDC-only deployments where the WAL
32    /// is consumed by an external reader and durability is provided
33    /// elsewhere.
34    None,
35}
36
37/// Configuration for opening a [`Wal`](crate::wal::Wal).
38///
39/// `Disabled` is the zero-overhead variant `lora-database` falls back to
40/// when the operator has not configured a WAL directory; it does not
41/// install a recorder or open any files.
42#[derive(Debug, Clone, Default)]
43pub enum WalConfig {
44    #[default]
45    Disabled,
46    Enabled {
47        dir: PathBuf,
48        sync_mode: SyncMode,
49        /// Target size of an active segment before rotation. Rotation
50        /// only happens at a `TxBegin` boundary so a transaction never
51        /// spans segments.
52        segment_target_bytes: u64,
53    },
54}
55
56impl WalConfig {
57    /// Quick constructor for the default `PerCommit` mode and an 8 MiB
58    /// segment target.
59    pub fn enabled(dir: impl Into<PathBuf>) -> Self {
60        Self::Enabled {
61            dir: dir.into(),
62            sync_mode: SyncMode::PerCommit,
63            segment_target_bytes: 8 * 1024 * 1024,
64        }
65    }
66}