mindb 0.1.2

Lightweight embedded key–value store with write-ahead log and zstd compression.
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
#![allow(dead_code)]
//! Recovery utilities for restoring in-memory state from durable artifacts.
//!
//! The routines in this module are designed for the fast start-up path of the
//! prototype. They favour deterministic behaviour and predictable latency over
//! absolute throughput so that the system can recover within the sub-200 ms
//! budget suggested by the product requirements.

pub mod snapshot;

use std::fmt;
use std::fs::{self, File};
use std::io::{self, Read, Seek, SeekFrom};
use std::path::PathBuf;
use std::sync::Arc;
use std::time::{Duration, Instant};

use parking_lot::Mutex;
use xxhash_rust::xxh3::xxh3_64;

use crate::storage::FileManifest;
use crate::write::memtable::{MemTable, MemTableError};

const FRAME_HEADER_LEN: usize = 12;
const ALIGNMENT: usize = 4096;

/// Configuration knobs for the recovery subsystem.
#[derive(Clone, Debug)]
pub struct RecoveryOptions {
    /// Path to the persisted write-ahead log that should be replayed.
    pub wal_path: PathBuf,
    /// Path to the legacy manifest location, typically inside the data dir.
    pub manifest_source: PathBuf,
    /// Canonical manifest location where the active manifest should reside.
    pub manifest_target: PathBuf,
    /// Soft limit used when constructing replay memtables.
    pub memtable_limit_bytes: u64,
    /// When specified, only the provided number of bytes from the tail of the
    /// WAL will be replayed. This protects start-up latency on hosts with very
    /// large logs. The value is aligned down to the WAL alignment boundary.
    pub wal_tail_bytes: Option<u64>,
}

impl RecoveryOptions {
    pub fn new<P: Into<PathBuf>>(wal_path: P, manifest: P) -> Self {
        let manifest_path = manifest.into();
        Self {
            wal_path: wal_path.into(),
            manifest_source: manifest_path.clone(),
            manifest_target: manifest_path,
            memtable_limit_bytes: 512 * 1024 * 1024,
            wal_tail_bytes: None,
        }
    }

    pub fn with_manifest_target<P: Into<PathBuf>>(mut self, path: P) -> Self {
        self.manifest_target = path.into();
        self
    }

    pub fn with_memtable_limit(mut self, limit: u64) -> Self {
        self.memtable_limit_bytes = limit.max(1);
        self
    }

    pub fn with_wal_tail(mut self, bytes: Option<u64>) -> Self {
        self.wal_tail_bytes = bytes;
        self
    }
}

/// Result of the manifest remapping step executed during recovery.
#[derive(Clone, Debug)]
pub struct ManifestRemap {
    pub source: PathBuf,
    pub active: PathBuf,
    pub bytes_copied: u64,
    pub updated: bool,
}

/// Captures the outcome of a WAL replay run.
pub struct RecoveryOutcome {
    pub memtables: Vec<Arc<MemTable>>,
    pub manifest: ManifestRemap,
    pub replayed_entries: usize,
    pub corrupted_frames: usize,
    pub truncated_bytes: u64,
    pub elapsed: Duration,
}

impl fmt::Debug for RecoveryOutcome {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("RecoveryOutcome")
            .field("memtables", &self.memtables.len())
            .field("manifest", &self.manifest)
            .field("replayed_entries", &self.replayed_entries)
            .field("corrupted_frames", &self.corrupted_frames)
            .field("truncated_bytes", &self.truncated_bytes)
            .field("elapsed", &self.elapsed)
            .finish()
    }
}

/// Errors produced by the recovery subsystem.
#[derive(Debug, thiserror::Error)]
pub enum RecoveryError {
    #[error("io error: {0}")]
    Io(#[from] io::Error),
    #[error("manifest not found at {0}")]
    MissingManifest(PathBuf),
    #[error("failed to prepare manifest directory: {0}")]
    ManifestDir(String),
    #[error("malformed wal frame at offset {offset}: {reason}")]
    WalCorruption { offset: u64, reason: String },
    #[error("memtable error during replay: {0}")]
    MemTable(#[from] MemTableError),
}

/// Coordinator that executes the recovery pipeline.
pub struct RecoveryManager {
    options: RecoveryOptions,
    /// Scratch buffer reused when parsing WAL frames to reduce allocations.
    payload: Mutex<Vec<u8>>,
}

impl RecoveryManager {
    pub fn new(options: RecoveryOptions) -> Self {
        Self {
            options,
            payload: Mutex::new(Vec::with_capacity(4096)),
        }
    }

    /// Runs the manifest remapping logic and WAL replay, returning the recovered
    /// memtables ready to be handed to the write controller.
    pub fn recover(&self) -> Result<RecoveryOutcome, RecoveryError> {
        let start = Instant::now();
        let manifest = self.remap_manifest()?;
        let replay = self.replay_wal()?;

        Ok(RecoveryOutcome {
            memtables: replay.memtables,
            manifest,
            replayed_entries: replay.entries,
            corrupted_frames: replay.corrupted_frames,
            truncated_bytes: replay.truncated_bytes,
            elapsed: start.elapsed(),
        })
    }

    fn remap_manifest(&self) -> Result<ManifestRemap, RecoveryError> {
        let source = &self.options.manifest_source;
        let target = &self.options.manifest_target;

        let (active, bytes_copied, updated) = if target.exists() {
            (target.clone(), 0, false)
        } else if source.exists() {
            if let Some(parent) = target.parent() {
                fs::create_dir_all(parent)
                    .map_err(|err| RecoveryError::ManifestDir(err.to_string()))?;
            }
            let copied = fs::copy(source, target)?;
            (target.clone(), copied as u64, true)
        } else {
            return Err(RecoveryError::MissingManifest(source.clone()));
        };

        Ok(ManifestRemap {
            source: source.clone(),
            active,
            bytes_copied,
            updated,
        })
    }

    fn replay_wal(&self) -> Result<WalReplay, RecoveryError> {
        let path = &self.options.wal_path;
        if !path.exists() {
            return Ok(WalReplay::empty());
        }

        let mut file = File::open(path)?;
        let mut start_offset = 0u64;
        let wal_len = file.metadata()?.len();
        if let Some(tail) = self.options.wal_tail_bytes {
            if wal_len > tail {
                let offset = wal_len - tail;
                let aligned = offset - (offset % ALIGNMENT as u64);
                start_offset = aligned;
                file.seek(SeekFrom::Start(aligned))?;
            }
        }

        let mut memtable = Arc::new(MemTable::with_limit(self.options.memtable_limit_bytes));
        let mut tables = vec![Arc::clone(&memtable)];
        let mut entries = 0usize;
        let mut corrupted_frames = 0usize;
        let mut truncated_bytes = 0u64;
        let mut last_good = start_offset;

        loop {
            let frame_offset = file.stream_position()?;
            match read_frame(frame_offset, &mut file, &self.payload) {
                Ok(Some(frame)) => {
                    last_good = frame_offset + frame.total_len as u64;
                    if frame.valid_checksum {
                        match decode_entries(&frame.payload) {
                            Ok(decoded) => {
                                for record in decoded {
                                    loop {
                                        match apply_record(&memtable, &record) {
                                            Ok(()) => {
                                                entries += 1;
                                                break;
                                            }
                                            Err(MemTableError::Backpressure) => {
                                                memtable = Arc::new(MemTable::with_limit(
                                                    self.options.memtable_limit_bytes,
                                                ));
                                                tables.push(Arc::clone(&memtable));
                                            }
                                            Err(other) => return Err(other.into()),
                                        }
                                    }
                                }
                            }
                            Err(reason) => {
                                corrupted_frames += 1;
                                eprintln!("wal decode error at frame {frame_offset}: {reason}");
                            }
                        }
                    } else {
                        corrupted_frames += 1;
                    }
                }
                Ok(None) => break,
                Err(err) => {
                    return Err(err);
                }
            }
        }

        truncated_bytes = truncated_bytes.max(wal_len.saturating_sub(last_good));

        Ok(WalReplay {
            memtables: tables,
            entries,
            corrupted_frames,
            truncated_bytes,
        })
    }
}

struct WalReplay {
    memtables: Vec<Arc<MemTable>>,
    entries: usize,
    corrupted_frames: usize,
    truncated_bytes: u64,
}

impl WalReplay {
    fn empty() -> Self {
        Self {
            memtables: vec![Arc::new(MemTable::new())],
            entries: 0,
            corrupted_frames: 0,
            truncated_bytes: 0,
        }
    }
}

struct WalFrame {
    payload: Vec<u8>,
    total_len: usize,
    valid_checksum: bool,
}

fn read_frame(
    offset: u64,
    file: &mut File,
    scratch: &Mutex<Vec<u8>>,
) -> Result<Option<WalFrame>, RecoveryError> {
    let mut header = [0u8; FRAME_HEADER_LEN];
    match file.read_exact(&mut header) {
        Ok(()) => {}
        Err(err) if err.kind() == io::ErrorKind::UnexpectedEof => return Ok(None),
        Err(err) => return Err(RecoveryError::Io(err)),
    }

    let payload_len = u32::from_le_bytes([header[0], header[1], header[2], header[3]]) as usize;
    let frame_len = u32::from_le_bytes([header[4], header[5], header[6], header[7]]) as usize;
    let checksum = u32::from_le_bytes([header[8], header[9], header[10], header[11]]);

    if payload_len == 0 && frame_len == 0 && checksum == 0 {
        return Ok(None);
    }

    if frame_len != FRAME_HEADER_LEN {
        return Err(RecoveryError::WalCorruption {
            offset,
            reason: format!("unexpected frame header length {frame_len}"),
        });
    }

    let mut payload = scratch.lock();
    payload.resize(payload_len, 0);
    if let Err(err) = file.read_exact(&mut payload[..]) {
        return Err(RecoveryError::WalCorruption {
            offset,
            reason: format!("incomplete frame payload: {err}"),
        });
    }

    let computed = (xxh3_64(&payload) & 0xffff_ffff) as u32;
    let frame_total = align_up(payload_len + FRAME_HEADER_LEN, ALIGNMENT);
    if frame_total > payload_len + FRAME_HEADER_LEN {
        file.seek(SeekFrom::Current(
            (frame_total - (payload_len + FRAME_HEADER_LEN)) as i64,
        ))?;
    }

    Ok(Some(WalFrame {
        payload: payload.clone(),
        total_len: frame_total,
        valid_checksum: checksum == computed,
    }))
}

fn decode_entries(payload: &[u8]) -> Result<Vec<WalRecord>, String> {
    let mut cursor = 0usize;
    let mut entries = Vec::new();
    while cursor < payload.len() {
        if payload.len() - cursor < 17 {
            return Err("truncated wal entry".into());
        }
        let tombstone = payload[cursor] != 0;
        cursor += 1;

        let key_len = u32::from_le_bytes([
            payload[cursor],
            payload[cursor + 1],
            payload[cursor + 2],
            payload[cursor + 3],
        ]) as usize;
        cursor += 4;
        let value_len = u32::from_le_bytes([
            payload[cursor],
            payload[cursor + 1],
            payload[cursor + 2],
            payload[cursor + 3],
        ]) as usize;
        cursor += 4;
        cursor += 8; // skip sequence number

        if payload.len() < cursor + key_len + value_len {
            return Err("wal entry extends beyond frame".into());
        }

        let key = payload[cursor..cursor + key_len].to_vec();
        cursor += key_len;
        let value = payload[cursor..cursor + value_len].to_vec();
        cursor += value_len;

        entries.push(WalRecord {
            tombstone,
            key,
            value,
        });
    }
    Ok(entries)
}

fn apply_record(table: &Arc<MemTable>, record: &WalRecord) -> Result<(), MemTableError> {
    if record.tombstone {
        table.delete(&record.key)?;
    } else {
        table.put(&record.key, record.value.clone())?;
    }
    Ok(())
}

fn align_up(len: usize, align: usize) -> usize {
    if len % align == 0 {
        len
    } else {
        len + (align - (len % align))
    }
}

#[derive(Clone, Debug)]
struct WalRecord {
    tombstone: bool,
    key: Vec<u8>,
    value: Vec<u8>,
}

/// Utility that allows recovery tooling to eagerly open the remapped manifest.
pub fn open_remapped_manifest(
    remap: &ManifestRemap,
) -> Result<FileManifest, crate::storage::StorageError> {
    FileManifest::open(&remap.active)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn align_up_rounds_correctly() {
        assert_eq!(align_up(0, 4096), 0);
        assert_eq!(align_up(1, 4096), 4096);
        assert_eq!(align_up(4096, 4096), 4096);
        assert_eq!(align_up(4097, 4096), 8192);
    }
}