nodedb 0.0.0-beta.1

Local-first, real-time, edge-to-cloud hybrid database for multi-modal workloads
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
//! Continuous WAL archiving for disaster recovery.
//!
//! WAL segments are streamed to remote storage (S3/GCS). Combined with
//! layered snapshots, this enables PITR to any microsecond before failure.
//!
//! The archiver runs on the Control Plane (Tokio) — it never touches the
//! Data Plane. It reads completed WAL segment files and uploads them.

use std::path::{Path, PathBuf};

use tracing::{info, warn};

use crate::types::Lsn;

/// Configuration for the WAL archiver.
#[derive(Debug, Clone)]
pub struct ArchiverConfig {
    /// Local WAL directory to watch for completed segments.
    pub wal_dir: PathBuf,
    /// Remote archive destination (e.g., "s3://bucket/wal/").
    pub archive_url: String,
    /// Maximum number of segments to archive in one batch.
    pub batch_size: usize,
    /// Whether to verify checksums before archiving.
    pub verify_checksums: bool,
    /// Whether to delete local segments after successful archive.
    pub delete_after_archive: bool,
}

impl Default for ArchiverConfig {
    fn default() -> Self {
        Self {
            wal_dir: PathBuf::from("/tmp/nodedb/wal"),
            archive_url: String::new(),
            batch_size: 16,
            verify_checksums: true,
            delete_after_archive: false,
        }
    }
}

/// Tracks the archival state of WAL segments.
#[derive(Debug, Clone)]
pub struct ArchiverState {
    /// Last LSN that has been successfully archived.
    pub last_archived_lsn: Lsn,
    /// Total segments archived.
    pub segments_archived: u64,
    /// Total bytes archived.
    pub bytes_archived: u64,
    /// Archive failures (for alerting).
    pub failures: u64,
}

impl ArchiverState {
    pub fn new() -> Self {
        Self {
            last_archived_lsn: Lsn::ZERO,
            segments_archived: 0,
            bytes_archived: 0,
            failures: 0,
        }
    }

    /// RPO (Recovery Point Objective) gap: how far behind is the archive?
    pub fn rpo_gap(&self, current_lsn: Lsn) -> u64 {
        current_lsn
            .as_u64()
            .saturating_sub(self.last_archived_lsn.as_u64())
    }
}

impl Default for ArchiverState {
    fn default() -> Self {
        Self::new()
    }
}

/// A WAL segment file ready for archival.
#[derive(Debug, Clone)]
pub struct WalSegment {
    /// Local file path.
    pub path: PathBuf,
    /// First LSN in this segment.
    pub first_lsn: Lsn,
    /// Last LSN in this segment.
    pub last_lsn: Lsn,
    /// File size in bytes.
    pub size_bytes: u64,
}

/// WAL archiver — manages continuous streaming of WAL segments to remote storage.
///
/// The archiver itself does NOT perform network I/O. It produces `ArchiveTask`
/// descriptors that the caller (Control Plane async runtime) executes via
/// `object_store` or equivalent.
pub struct WalArchiver {
    config: ArchiverConfig,
    state: ArchiverState,
}

impl WalArchiver {
    pub fn new(config: ArchiverConfig) -> Self {
        Self {
            config,
            state: ArchiverState::new(),
        }
    }

    /// Scan the WAL directory for segments that need archiving.
    ///
    /// Returns segments with LSN > last_archived_lsn, sorted by LSN.
    pub fn pending_segments(&self) -> std::io::Result<Vec<WalSegment>> {
        let dir = &self.config.wal_dir;
        if !dir.exists() {
            return Ok(Vec::new());
        }

        let mut segments = Vec::new();
        for entry in std::fs::read_dir(dir)? {
            let entry = entry?;
            let path = entry.path();
            if !path.is_file() {
                continue;
            }

            // Parse segment filename: "wal-{first_lsn}-{last_lsn}.seg"
            if let Some(seg) = parse_segment_filename(&path)
                && seg.last_lsn > self.state.last_archived_lsn
            {
                segments.push(seg);
            }
        }

        segments.sort_by_key(|s| s.first_lsn);
        if segments.len() > self.config.batch_size {
            segments.truncate(self.config.batch_size);
        }

        Ok(segments)
    }

    /// Generate archive tasks for pending segments.
    ///
    /// Each task describes a source (local path) and destination (remote URL).
    /// The caller executes these asynchronously.
    pub fn plan_archive(&self, segments: &[WalSegment]) -> Vec<ArchiveTask> {
        segments
            .iter()
            .map(|seg| {
                let remote_key = format!(
                    "{}/wal-{:016x}-{:016x}.seg",
                    self.config.archive_url.trim_end_matches('/'),
                    seg.first_lsn.as_u64(),
                    seg.last_lsn.as_u64(),
                );
                ArchiveTask {
                    source: seg.path.clone(),
                    destination: remote_key,
                    first_lsn: seg.first_lsn,
                    last_lsn: seg.last_lsn,
                    size_bytes: seg.size_bytes,
                    verify_checksum: self.config.verify_checksums,
                }
            })
            .collect()
    }

    /// Mark a segment as successfully archived.
    pub fn mark_archived(&mut self, task: &ArchiveTask) {
        if task.last_lsn > self.state.last_archived_lsn {
            self.state.last_archived_lsn = task.last_lsn;
        }
        self.state.segments_archived += 1;
        self.state.bytes_archived += task.size_bytes;

        info!(
            last_archived_lsn = task.last_lsn.as_u64(),
            total_segments = self.state.segments_archived,
            total_bytes = self.state.bytes_archived,
            "WAL segment archived"
        );

        // Delete local segment if configured.
        if self.config.delete_after_archive
            && let Err(e) = std::fs::remove_file(&task.source)
        {
            warn!(
                path = %task.source.display(),
                error = %e,
                "failed to delete archived WAL segment"
            );
        }
    }

    /// Record an archive failure.
    pub fn mark_failed(&mut self, _task: &ArchiveTask, reason: &str) {
        self.state.failures += 1;
        warn!(failures = self.state.failures, reason, "WAL archive failed");
    }

    pub fn state(&self) -> &ArchiverState {
        &self.state
    }

    pub fn config(&self) -> &ArchiverConfig {
        &self.config
    }
}

/// A task describing one WAL segment to archive.
#[derive(Debug, Clone)]
pub struct ArchiveTask {
    /// Local source path.
    pub source: PathBuf,
    /// Remote destination URL/key.
    pub destination: String,
    /// First LSN in the segment.
    pub first_lsn: Lsn,
    /// Last LSN in the segment.
    pub last_lsn: Lsn,
    /// Segment size.
    pub size_bytes: u64,
    /// Whether to verify checksum before upload.
    pub verify_checksum: bool,
}

/// Parse a WAL segment filename into a WalSegment.
///
/// Expected format: `wal-{first_lsn_hex}-{last_lsn_hex}.seg`
fn parse_segment_filename(path: &Path) -> Option<WalSegment> {
    let stem = path.file_stem()?.to_str()?;
    let parts: Vec<&str> = stem.split('-').collect();
    if parts.len() != 3 || parts[0] != "wal" {
        return None;
    }

    let first_lsn = u64::from_str_radix(parts[1], 16).ok()?;
    let last_lsn = u64::from_str_radix(parts[2], 16).ok()?;
    let size_bytes = std::fs::metadata(path).ok()?.len();

    Some(WalSegment {
        path: path.to_path_buf(),
        first_lsn: Lsn::new(first_lsn),
        last_lsn: Lsn::new(last_lsn),
        size_bytes,
    })
}

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

    #[test]
    fn archiver_state_defaults() {
        let state = ArchiverState::new();
        assert_eq!(state.last_archived_lsn, Lsn::ZERO);
        assert_eq!(state.segments_archived, 0);
        assert_eq!(state.rpo_gap(Lsn::new(100)), 100);
    }

    #[test]
    fn rpo_gap_calculation() {
        let mut state = ArchiverState::new();
        state.last_archived_lsn = Lsn::new(90);
        assert_eq!(state.rpo_gap(Lsn::new(100)), 10);
        assert_eq!(state.rpo_gap(Lsn::new(90)), 0);
    }

    #[test]
    fn parse_valid_segment_filename() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("wal-000000000000000a-0000000000000064.seg");
        std::fs::write(&path, b"test data").unwrap();

        let seg = parse_segment_filename(&path).unwrap();
        assert_eq!(seg.first_lsn, Lsn::new(10));
        assert_eq!(seg.last_lsn, Lsn::new(100));
        assert_eq!(seg.size_bytes, 9);
    }

    #[test]
    fn parse_invalid_filenames() {
        let dir = tempfile::tempdir().unwrap();

        // Wrong prefix.
        let path = dir.path().join("data-0a-64.seg");
        std::fs::write(&path, b"x").unwrap();
        assert!(parse_segment_filename(&path).is_none());

        // Wrong part count.
        let path = dir.path().join("wal-0a.seg");
        std::fs::write(&path, b"x").unwrap();
        assert!(parse_segment_filename(&path).is_none());
    }

    #[test]
    fn pending_segments_empty_dir() {
        let dir = tempfile::tempdir().unwrap();
        let config = ArchiverConfig {
            wal_dir: dir.path().to_path_buf(),
            ..Default::default()
        };
        let archiver = WalArchiver::new(config);
        let pending = archiver.pending_segments().unwrap();
        assert!(pending.is_empty());
    }

    #[test]
    fn pending_segments_finds_new() {
        let dir = tempfile::tempdir().unwrap();

        // Create two segment files.
        std::fs::write(
            dir.path().join("wal-0000000000000001-000000000000000a.seg"),
            b"seg1",
        )
        .unwrap();
        std::fs::write(
            dir.path().join("wal-000000000000000b-0000000000000014.seg"),
            b"seg2",
        )
        .unwrap();

        let config = ArchiverConfig {
            wal_dir: dir.path().to_path_buf(),
            ..Default::default()
        };
        let archiver = WalArchiver::new(config);
        let pending = archiver.pending_segments().unwrap();
        assert_eq!(pending.len(), 2);
        assert!(pending[0].first_lsn < pending[1].first_lsn); // sorted
    }

    #[test]
    fn plan_archive_generates_tasks() {
        let config = ArchiverConfig {
            archive_url: "s3://my-bucket/wal".into(),
            ..Default::default()
        };
        let archiver = WalArchiver::new(config);

        let segments = vec![WalSegment {
            path: PathBuf::from("/tmp/wal-01-0a.seg"),
            first_lsn: Lsn::new(1),
            last_lsn: Lsn::new(10),
            size_bytes: 4096,
        }];

        let tasks = archiver.plan_archive(&segments);
        assert_eq!(tasks.len(), 1);
        assert!(tasks[0].destination.starts_with("s3://my-bucket/wal/"));
        assert!(tasks[0].verify_checksum);
    }

    #[test]
    fn mark_archived_advances_lsn() {
        let config = ArchiverConfig::default();
        let mut archiver = WalArchiver::new(config);

        let task = ArchiveTask {
            source: PathBuf::from("/tmp/seg"),
            destination: "s3://bucket/seg".into(),
            first_lsn: Lsn::new(1),
            last_lsn: Lsn::new(100),
            size_bytes: 1024,
            verify_checksum: false,
        };

        archiver.mark_archived(&task);
        assert_eq!(archiver.state().last_archived_lsn, Lsn::new(100));
        assert_eq!(archiver.state().segments_archived, 1);
        assert_eq!(archiver.state().bytes_archived, 1024);
    }

    #[test]
    fn mark_failed_increments_counter() {
        let config = ArchiverConfig::default();
        let mut archiver = WalArchiver::new(config);

        let task = ArchiveTask {
            source: PathBuf::from("/tmp/seg"),
            destination: "s3://bucket/seg".into(),
            first_lsn: Lsn::new(1),
            last_lsn: Lsn::new(10),
            size_bytes: 512,
            verify_checksum: false,
        };

        archiver.mark_failed(&task, "network timeout");
        assert_eq!(archiver.state().failures, 1);
    }

    #[test]
    fn batch_size_limits_pending() {
        let dir = tempfile::tempdir().unwrap();
        for i in 0u64..20 {
            std::fs::write(
                dir.path()
                    .join(format!("wal-{:016x}-{:016x}.seg", i * 10 + 1, (i + 1) * 10)),
                b"data",
            )
            .unwrap();
        }

        let config = ArchiverConfig {
            wal_dir: dir.path().to_path_buf(),
            batch_size: 5,
            ..Default::default()
        };
        let archiver = WalArchiver::new(config);
        let pending = archiver.pending_segments().unwrap();
        assert_eq!(pending.len(), 5);
    }
}