Skip to main content

lora_wal/
config.rs

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