spillbuf 0.1.0

Durable disk buffer: append + fdatasync, hub-ACK watermark, oldest-first drain
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
//! Durable spill buffer: length-prefixed frames, CRC, fdatasync, hub-ACK watermark.
//!
//! A crash mid-append leaves a partial last frame; open() truncates it. Pending
//! frames survive reboot and drain oldest-first. `ack_through` is the only
//! delete path — the hub's ACK, not a local fsync, retires a record.

use std::fs::{self, File, OpenOptions};
use std::io::{self, Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

const MAGIC: &[u8; 4] = b"SBF1";
const HEADER_LEN: u64 = 28;
const DEFAULT_SEGMENT_BYTES: u64 = 64 * 1024 * 1024;
const DEFAULT_CAP_BYTES: u64 = 64 * 1024 * 1024;

#[derive(Debug)]
pub enum SpillError {
    Io(io::Error),
    CapFull { depth_bytes: u64, cap_bytes: u64 },
    Corrupt { path: PathBuf, reason: String },
}

impl std::fmt::Display for SpillError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Io(error) => write!(f, "spillbuf io: {error}"),
            Self::CapFull {
                depth_bytes,
                cap_bytes,
            } => write!(f, "spillbuf cap full: {depth_bytes} >= {cap_bytes}"),
            Self::Corrupt { path, reason } => {
                write!(f, "spillbuf corrupt {}: {reason}", path.display())
            }
        }
    }
}

impl std::error::Error for SpillError {}

impl From<io::Error> for SpillError {
    fn from(error: io::Error) -> Self {
        Self::Io(error)
    }
}

pub type Result<T> = std::result::Result<T, SpillError>;

#[derive(Clone, Debug)]
pub struct Options {
    pub segment_bytes: u64,
    pub cap_bytes: u64,
}

impl Default for Options {
    fn default() -> Self {
        Self {
            segment_bytes: DEFAULT_SEGMENT_BYTES,
            cap_bytes: DEFAULT_CAP_BYTES,
        }
    }
}

impl Options {
    pub fn from_env() -> Self {
        Self {
            segment_bytes: env_u64("SPILLBUF_SEGMENT_BYTES", DEFAULT_SEGMENT_BYTES).max(64),
            cap_bytes: env_u64("SPILLBUF_CAP_BYTES", DEFAULT_CAP_BYTES).max(64),
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Frame {
    pub seq: u64,
    pub unix_ms: u64,
    pub bytes: Vec<u8>,
}

#[derive(Clone, Debug)]
struct IndexEntry {
    seq: u64,
    unix_ms: u64,
    segment: u64,
    offset: u64,
    payload_len: u32,
}

pub struct SpillBuf {
    dir: PathBuf,
    opts: Options,
    pending: Vec<IndexEntry>,
    next_seq: u64,
    ack_seq: u64,
    current_segment: u64,
    writer: File,
}

impl SpillBuf {
    pub fn open(dir: impl AsRef<Path>, opts: Options) -> Result<Self> {
        let dir = dir.as_ref().to_path_buf();
        fs::create_dir_all(&dir)?;
        let ack_seq = read_ack(&dir)?;
        let (pending, next_seq, current_segment) = scan_segments(&dir, ack_seq)?;
        let writer = open_segment(&dir, current_segment)?;
        Ok(Self {
            dir,
            opts,
            pending,
            next_seq,
            ack_seq,
            current_segment,
            writer,
        })
    }

    pub fn append(&mut self, bytes: &[u8]) -> Result<u64> {
        let frame_bytes = HEADER_LEN + bytes.len() as u64;
        let depth_bytes = self.depth_bytes();
        if depth_bytes.saturating_add(frame_bytes) > self.opts.cap_bytes {
            return Err(SpillError::CapFull {
                depth_bytes,
                cap_bytes: self.opts.cap_bytes,
            });
        }
        let seq = self.next_seq;
        let unix_ms = now_unix_ms();
        let offset = self.writer.seek(SeekFrom::End(0))?;
        write_frame(&mut self.writer, seq, unix_ms, bytes)?;
        self.writer.sync_data()?;
        self.pending.push(IndexEntry {
            seq,
            unix_ms,
            segment: self.current_segment,
            offset,
            payload_len: bytes.len() as u32,
        });
        self.next_seq = seq.saturating_add(1);
        let len = self.writer.metadata()?.len();
        if len >= self.opts.segment_bytes {
            self.rotate()?;
        }
        Ok(seq)
    }

    pub fn drain_batch(&self, max: usize) -> Result<Vec<Frame>> {
        let mut out = Vec::new();
        for entry in self.pending.iter().take(max) {
            out.push(self.read_frame(entry)?);
        }
        Ok(out)
    }

    pub fn ack_through(&mut self, watermark: u64) -> Result<()> {
        if watermark < self.ack_seq {
            return Ok(());
        }
        self.ack_seq = watermark;
        persist_ack(&self.dir, self.ack_seq)?;
        let keep_from = self
            .pending
            .iter()
            .position(|entry| entry.seq > watermark)
            .unwrap_or(self.pending.len());
        let retired: Vec<IndexEntry> = self.pending.drain(..keep_from).collect();
        let live_segments: std::collections::BTreeSet<u64> =
            self.pending.iter().map(|entry| entry.segment).collect();
        let retired_segments: std::collections::BTreeSet<u64> = retired
            .iter()
            .map(|entry| entry.segment)
            .filter(|seg| !live_segments.contains(seg) && *seg != self.current_segment)
            .collect();
        for segment in retired_segments {
            let path = segment_path(&self.dir, segment);
            if path.exists() {
                fs::remove_file(path)?;
            }
        }
        Ok(())
    }

    pub fn depth(&self) -> u64 {
        self.pending.len() as u64
    }

    pub fn depth_bytes(&self) -> u64 {
        self.pending
            .iter()
            .map(|entry| HEADER_LEN + u64::from(entry.payload_len))
            .sum()
    }

    pub fn oldest_age(&self) -> Option<Duration> {
        let oldest = self.pending.first()?.unix_ms;
        let now = now_unix_ms();
        Some(Duration::from_millis(now.saturating_sub(oldest)))
    }

    pub fn oldest_unix_ms(&self) -> Option<u64> {
        self.pending.first().map(|entry| entry.unix_ms)
    }

    pub fn next_seq(&self) -> u64 {
        self.next_seq
    }

    pub fn ack_seq(&self) -> u64 {
        self.ack_seq
    }

    pub fn cap_bytes(&self) -> u64 {
        self.opts.cap_bytes
    }

    fn rotate(&mut self) -> Result<()> {
        self.writer.sync_all()?;
        self.current_segment = self.current_segment.saturating_add(1);
        self.writer = open_segment(&self.dir, self.current_segment)?;
        Ok(())
    }

    fn read_frame(&self, entry: &IndexEntry) -> Result<Frame> {
        let path = segment_path(&self.dir, entry.segment);
        let mut file = File::open(&path)?;
        file.seek(SeekFrom::Start(entry.offset))?;
        let mut header = [0u8; HEADER_LEN as usize];
        file.read_exact(&mut header)?;
        let parsed = parse_header(&header).ok_or_else(|| SpillError::Corrupt {
            path: path.clone(),
            reason: format!("bad header at {}", entry.offset),
        })?;
        let mut bytes = vec![0u8; parsed.len as usize];
        file.read_exact(&mut bytes)?;
        if crc32(&bytes) != parsed.crc {
            return Err(SpillError::Corrupt {
                path,
                reason: format!("crc mismatch seq={}", parsed.seq),
            });
        }
        Ok(Frame {
            seq: parsed.seq,
            unix_ms: parsed.unix_ms,
            bytes,
        })
    }
}

struct ParsedHeader {
    seq: u64,
    unix_ms: u64,
    len: u32,
    crc: u32,
}

fn write_frame(file: &mut File, seq: u64, unix_ms: u64, bytes: &[u8]) -> io::Result<()> {
    let len = bytes.len() as u32;
    let crc = crc32(bytes);
    file.write_all(MAGIC)?;
    file.write_all(&seq.to_le_bytes())?;
    file.write_all(&unix_ms.to_le_bytes())?;
    file.write_all(&len.to_le_bytes())?;
    file.write_all(&crc.to_le_bytes())?;
    file.write_all(bytes)?;
    Ok(())
}

fn parse_header(buf: &[u8]) -> Option<ParsedHeader> {
    if buf.len() < HEADER_LEN as usize || &buf[0..4] != MAGIC {
        return None;
    }
    Some(ParsedHeader {
        seq: u64::from_le_bytes(buf[4..12].try_into().ok()?),
        unix_ms: u64::from_le_bytes(buf[12..20].try_into().ok()?),
        len: u32::from_le_bytes(buf[20..24].try_into().ok()?),
        crc: u32::from_le_bytes(buf[24..28].try_into().ok()?),
    })
}

fn scan_segments(dir: &Path, ack_seq: u64) -> Result<(Vec<IndexEntry>, u64, u64)> {
    let mut ids = list_segments(dir)?;
    if ids.is_empty() {
        ids.push(0);
    }
    let mut pending = Vec::new();
    // Seq starts at 1. ack 0 means nothing retired; scan advances next_seq.
    let mut next_seq = 1.max(ack_seq.saturating_add(1));
    let last_id = *ids.last().unwrap_or(&0);
    for id in &ids {
        let path = segment_path(dir, *id);
        if !path.exists() {
            continue;
        }
        let mut file = OpenOptions::new().read(true).write(true).open(&path)?;
        let file_len = file.metadata()?.len();
        let mut offset = 0u64;
        loop {
            if offset + HEADER_LEN > file_len {
                if offset < file_len {
                    file.set_len(offset)?;
                }
                break;
            }
            file.seek(SeekFrom::Start(offset))?;
            let mut header = [0u8; HEADER_LEN as usize];
            match file.read_exact(&mut header) {
                Ok(()) => {}
                Err(error) if error.kind() == io::ErrorKind::UnexpectedEof => {
                    file.set_len(offset)?;
                    break;
                }
                Err(error) => return Err(error.into()),
            }
            let Some(parsed) = parse_header(&header) else {
                file.set_len(offset)?;
                break;
            };
            let payload_end = offset + HEADER_LEN + u64::from(parsed.len);
            if payload_end > file_len {
                file.set_len(offset)?;
                break;
            }
            let mut payload = vec![0u8; parsed.len as usize];
            file.read_exact(&mut payload)?;
            if crc32(&payload) != parsed.crc {
                file.set_len(offset)?;
                break;
            }
            if parsed.seq > ack_seq {
                pending.push(IndexEntry {
                    seq: parsed.seq,
                    unix_ms: parsed.unix_ms,
                    segment: *id,
                    offset,
                    payload_len: parsed.len,
                });
            }
            next_seq = next_seq.max(parsed.seq.saturating_add(1));
            offset = payload_end;
        }
    }
    pending.sort_by_key(|entry| entry.seq);
    Ok((pending, next_seq, last_id))
}

fn list_segments(dir: &Path) -> Result<Vec<u64>> {
    let mut ids = Vec::new();
    if !dir.exists() {
        return Ok(ids);
    }
    for entry in fs::read_dir(dir)? {
        let entry = entry?;
        let name = entry.file_name();
        let Some(name) = name.to_str() else {
            continue;
        };
        if let Some(rest) = name.strip_prefix("seg-") {
            if let Some(stem) = rest.strip_suffix(".log") {
                if let Ok(id) = stem.parse::<u64>() {
                    ids.push(id);
                }
            }
        }
    }
    ids.sort_unstable();
    Ok(ids)
}

fn open_segment(dir: &Path, id: u64) -> io::Result<File> {
    OpenOptions::new()
        .create(true)
        .read(true)
        .append(true)
        .open(segment_path(dir, id))
}

fn segment_path(dir: &Path, id: u64) -> PathBuf {
    dir.join(format!("seg-{id:08}.log"))
}

fn ack_path(dir: &Path) -> PathBuf {
    dir.join("ack.seq")
}

fn read_ack(dir: &Path) -> Result<u64> {
    let path = ack_path(dir);
    if !path.exists() {
        return Ok(0);
    }
    let raw = fs::read(&path)?;
    if raw.len() < 8 {
        return Err(SpillError::Corrupt {
            path,
            reason: "short ack.seq".into(),
        });
    }
    Ok(u64::from_le_bytes(raw[0..8].try_into().unwrap()))
}

fn persist_ack(dir: &Path, seq: u64) -> Result<()> {
    let path = ack_path(dir);
    let tmp = dir.join("ack.seq.tmp");
    let mut file = OpenOptions::new()
        .create(true)
        .write(true)
        .truncate(true)
        .open(&tmp)?;
    file.write_all(&seq.to_le_bytes())?;
    file.sync_all()?;
    fs::rename(tmp, path)?;
    Ok(())
}

fn now_unix_ms() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_millis() as u64)
        .unwrap_or(0)
}

fn env_u64(name: &str, default: u64) -> u64 {
    std::env::var(name)
        .ok()
        .and_then(|value| value.parse().ok())
        .unwrap_or(default)
}

/// IEEE CRC-32 (ISO 3309). Known vector: `b"123456789"` → `0xCBF43926`.
fn crc32(data: &[u8]) -> u32 {
    let mut crc = 0xFFFF_FFFFu32;
    for &byte in data {
        crc ^= u32::from(byte);
        for _ in 0..8 {
            crc = if crc & 1 == 1 {
                (crc >> 1) ^ 0xEDB8_8320
            } else {
                crc >> 1
            };
        }
    }
    !crc
}

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

    fn tmp() -> tempfile::TempDir {
        tempfile::tempdir().expect("tempdir")
    }

    fn open_small(dir: &Path, segment: u64, cap: u64) -> SpillBuf {
        SpillBuf::open(
            dir,
            Options {
                segment_bytes: segment,
                cap_bytes: cap,
            },
        )
        .expect("open")
    }

    #[test]
    fn crc32_known_vector() {
        assert_eq!(crc32(b"123456789"), 0xCBF4_3926);
    }

    #[test]
    fn append_drain_ack_round_trip() {
        let dir = tmp();
        let mut buf = open_small(dir.path(), 64 * 1024, 64 * 1024);
        let a = buf.append(b"one").unwrap();
        let b = buf.append(b"two").unwrap();
        assert_eq!(a, 1);
        assert_eq!(b, 2);
        assert_eq!(buf.depth(), 2);
        let batch = buf.drain_batch(10).unwrap();
        assert_eq!(batch.len(), 2);
        assert_eq!(batch[0].bytes, b"one");
        assert_eq!(batch[1].bytes, b"two");
        buf.ack_through(a).unwrap();
        assert_eq!(buf.depth(), 1);
        let rest = buf.drain_batch(10).unwrap();
        assert_eq!(rest[0].bytes, b"two");
    }

    #[test]
    fn crash_mid_append_discards_partial() {
        let dir = tmp();
        let mut buf = open_small(dir.path(), 64 * 1024, 64 * 1024);
        buf.append(b"durable").unwrap();
        drop(buf);
        let seg = segment_path(dir.path(), 0);
        let mut file = OpenOptions::new().append(true).open(&seg).unwrap();
        file.write_all(&[1, 2, 3, 4, 5]).unwrap();
        drop(file);
        let buf = open_small(dir.path(), 64 * 1024, 64 * 1024);
        let batch = buf.drain_batch(10).unwrap();
        assert_eq!(batch.len(), 1);
        assert_eq!(batch[0].bytes, b"durable");
        assert_eq!(
            fs::metadata(&seg).unwrap().len(),
            HEADER_LEN + b"durable".len() as u64
        );
    }

    #[test]
    fn replay_preserves_oldest_first_order() {
        let dir = tmp();
        let mut buf = open_small(dir.path(), 64 * 1024, 64 * 1024);
        buf.append(b"a").unwrap();
        buf.append(b"b").unwrap();
        buf.append(b"c").unwrap();
        drop(buf);
        let buf = open_small(dir.path(), 64 * 1024, 64 * 1024);
        let batch = buf.drain_batch(10).unwrap();
        let bodies: Vec<&[u8]> = batch.iter().map(|f| f.bytes.as_slice()).collect();
        assert_eq!(bodies, [b"a".as_slice(), b"b", b"c"]);
        assert_eq!(buf.depth(), 3);
    }

    #[test]
    fn rotation_splits_segments_and_acks_delete_old() {
        let dir = tmp();
        let payload = vec![b'x'; 40];
        let mut buf = open_small(dir.path(), 80, 64 * 1024);
        let s1 = buf.append(&payload).unwrap();
        let s2 = buf.append(&payload).unwrap();
        let s3 = buf.append(&payload).unwrap();
        let segs = list_segments(dir.path()).unwrap();
        assert!(
            segs.len() >= 2,
            "expected rotation, segs={segs:?} s={s1},{s2},{s3}"
        );
        buf.ack_through(s2).unwrap();
        let after = list_segments(dir.path()).unwrap();
        assert!(
            !after.contains(&0) || buf.depth() > 0,
            "acked prefix should drop idle segments: {after:?}"
        );
        let rest = buf.drain_batch(10).unwrap();
        assert_eq!(rest.len(), 1);
        assert_eq!(rest[0].seq, s3);
    }

    #[test]
    fn cap_rejects_when_full() {
        let dir = tmp();
        let mut buf = open_small(dir.path(), 64 * 1024, HEADER_LEN + 8);
        buf.append(b"abcd").unwrap();
        let err = buf.append(b"efgh").unwrap_err();
        match err {
            SpillError::CapFull { .. } => {}
            other => panic!("expected CapFull, got {other}"),
        }
        assert_eq!(buf.depth(), 1);
    }

    #[test]
    fn ack_survives_reboot_and_does_not_redeliver() {
        let dir = tmp();
        let mut buf = open_small(dir.path(), 64 * 1024, 64 * 1024);
        let a = buf.append(b"keep-me-acked").unwrap();
        buf.append(b"still-pending").unwrap();
        buf.ack_through(a).unwrap();
        drop(buf);
        let buf = open_small(dir.path(), 64 * 1024, 64 * 1024);
        let batch = buf.drain_batch(10).unwrap();
        assert_eq!(batch.len(), 1);
        assert_eq!(batch[0].bytes, b"still-pending");
    }

    #[test]
    fn oldest_age_is_some_when_pending() {
        let dir = tmp();
        let mut buf = open_small(dir.path(), 64 * 1024, 64 * 1024);
        assert!(buf.oldest_age().is_none());
        buf.append(b"x").unwrap();
        let age = buf.oldest_age().expect("age");
        assert!(age < Duration::from_secs(5));
    }
}