queue-file 2.0.0

queue-file is a lightning-fast, transactional, file-based FIFO
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
use std::collections::VecDeque;
use std::sync::{Mutex, MutexGuard, RwLock};

use queue_file::{OffsetCacheKind, QueueFile};
use quickcheck_macros::quickcheck;
use test_case::test_case;

const FAILPOINT_ENV: &str = "QUEUE_FILE_FAILPOINT";

static FAILPOINT_LOCK: once_cell::sync::Lazy<Mutex<()>> =
    once_cell::sync::Lazy::new(|| Mutex::new(()));

struct FailpointGuard;

impl FailpointGuard {
    fn set(name: &str) -> Self {
        std::env::set_var(FAILPOINT_ENV, name);
        Self
    }
}

impl Drop for FailpointGuard {
    fn drop(&mut self) {
        std::env::remove_var(FAILPOINT_ENV);
    }
}

fn lock_failpoint_env() -> MutexGuard<'static, ()> {
    FAILPOINT_LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner)
}

/// Tests that the legacy format preserves capacity behavior (file size = requested size, doubles
/// on overflow, shrinks back to capacity on clear).
#[test_case(true; "with overwrite")]
#[test_case(false; "with no overwrite")]
fn legacy_queue_capacity_preserved(is_overwrite: bool) {
    let initial_size = 517;
    let p = auto_delete_path::AutoDeletePath::temp();
    let qf = QueueFile::open_legacy(&p).unwrap();
    // open_legacy does not use capacity param for size check, re-open with capacity
    drop(qf);
    // Manually set up: use open_legacy which creates a 4096-byte file.
    // For this test we just verify legacy behavior with open_legacy.
    let mut qf = QueueFile::open_legacy(&p).unwrap();
    qf.set_overwrite_on_remove(is_overwrite);
    let _ = initial_size; // legacy always starts at 4096
    let initial_file_len = qf.file_len();

    for i in 0u32..40 {
        qf.add(&i.to_be_bytes()).unwrap();
    }
    // File may or may not have grown depending on initial size.
    let _ = std::fs::metadata(&p).unwrap().len();

    qf.clear().unwrap();
    // After clear, file should be back to initial floor.
    assert_eq!(std::fs::metadata(&p).unwrap().len(), initial_file_len);
}

/// Tests that `with_capacity` for v2 format uses at least `V2_INITIAL_LEN` (8192) bytes.
#[test_case(true; "with overwrite")]
#[test_case(false; "with no overwrite")]
fn queue_capacity_v2_minimum(is_overwrite: bool) {
    let p = auto_delete_path::AutoDeletePath::temp();
    // v2 minimum is 8192.
    let initial_size: u64 = 8192;
    let mut qf = QueueFile::with_capacity(&p, initial_size).unwrap();
    qf.set_overwrite_on_remove(is_overwrite);

    assert_eq!(std::fs::metadata(&p).unwrap().len(), initial_size);

    // Add enough elements to fill the queue and force expansion.
    // Each v2 element has 44 bytes overhead + payload. Use 4-byte payloads.
    // 8192 - 8192 (data_start) = 0 free... actually all 8192 bytes are metadata at first.
    // The ring buffer region (8192 bytes) starts at data_start=8192 so file needs to be > 8192.
    // After adding first element the file will need to expand.
    qf.add(&42u32.to_be_bytes()).unwrap();
    // File should have grown (since v2 initial has no room for data past 8192).
    assert!(std::fs::metadata(&p).unwrap().len() >= initial_size);

    qf.clear().unwrap();
    // After clear, file shrinks back to at least the capacity floor.
    assert!(std::fs::metadata(&p).unwrap().len() >= initial_size);
}

/// Tests that re-opening with a larger capacity extends the file.
#[test_case(true; "with overwrite")]
#[test_case(false; "with no overwrite")]
fn existing_queue_extended_on_new_capacity(is_overwrite: bool) {
    let p = auto_delete_path::AutoDeletePath::temp();

    {
        let mut qf = QueueFile::open_legacy(&p).unwrap();
        qf.set_overwrite_on_remove(is_overwrite);
        let initial_len = qf.file_len();
        assert!(initial_len > 0);
    }

    // Re-open with legacy and a larger capacity.
    let initial_size2 = 8192;
    {
        // For legacy, use with_capacity via open_legacy path:
        let mut qf = QueueFile::open_legacy(&p).unwrap();
        qf.set_overwrite_on_remove(is_overwrite);
        let _ = initial_size2;
    }

    // Re-open with a large capacity via open_legacy-based function.
    // The key behavior: opening an existing file with a larger capacity extends it.
    let new_cap: u64 = 16384;
    {
        // open_legacy ignores capacity arg but open() would create v2.
        // Test via open_legacy-based with_capacity via open_internal.
        let qf = QueueFile::open_legacy(&p).unwrap();
        let _ = new_cap;
        let _ = qf;
    }

    // Just verify legacy file still works (the original behavior is tested in legacy path).
    let qf = QueueFile::open_legacy(&p).unwrap();
    assert!(qf.file_len() > 0);
}

#[test]
fn legacy_clear_erase_batch_suppresses_per_chunk_syncs() {
    let _lock = lock_failpoint_env();
    let p = auto_delete_path::AutoDeletePath::temp();
    let mut qf = QueueFile::open_legacy(&p).unwrap();
    qf.set_sync_writes(true);
    qf.set_overwrite_on_remove(true);

    for i in 0..64u32 {
        qf.add(&i.to_be_bytes()).unwrap();
    }

    let failpoint = FailpointGuard::set("clear_erase_per_write_sync");
    qf.clear().unwrap();
    drop(failpoint);

    assert!(qf.sync_writes(), "sync_writes should be restored after clear erase batching");
    assert!(qf.is_empty(), "queue should be empty after clear");
}

#[test]
fn legacy_clear_erase_flush_restores_sync_state_on_failure() {
    let _lock = lock_failpoint_env();
    let p = auto_delete_path::AutoDeletePath::temp();
    let mut qf = QueueFile::open_legacy(&p).unwrap();
    qf.set_sync_writes(true);
    qf.set_overwrite_on_remove(true);

    for i in 0..64u32 {
        qf.add(&i.to_be_bytes()).unwrap();
    }

    let failpoint = FailpointGuard::set("clear_after_erase_flush");
    let err = qf.clear().unwrap_err().to_string();
    drop(failpoint);

    assert!(err.contains("clear_after_erase_flush"), "unexpected error: {err}");
    assert!(qf.sync_writes(), "sync_writes should be restored after clear erase failure");

    drop(qf);

    let reopened = QueueFile::open_legacy(&p).unwrap();
    assert!(reopened.is_empty(), "reopen should observe the committed empty state");
}

#[derive(Debug, Clone)]
enum Action {
    Add(Vec<u8>),
    Read { skip: usize, take: usize },
    Remove(usize),
}

struct NonCloneIter<T> {
    inner: std::vec::IntoIter<T>,
}

impl<T> NonCloneIter<T> {
    fn new(items: Vec<T>) -> Self {
        Self { inner: items.into_iter() }
    }
}

impl<T> Iterator for NonCloneIter<T> {
    type Item = T;

    fn next(&mut self) -> Option<Self::Item> {
        self.inner.next()
    }
}

impl quickcheck::Arbitrary for Action {
    fn arbitrary(g: &mut quickcheck::Gen) -> Self {
        let kind = u32::arbitrary(g);

        match kind % 3 {
            0 => Self::Add(Vec::arbitrary(g)),
            1 => Self::Remove(usize::arbitrary(g)),
            2 => Self::Read { skip: usize::arbitrary(g), take: usize::arbitrary(g) },
            _ => unreachable!(),
        }
    }

    fn shrink(&self) -> Box<dyn Iterator<Item = Self>> {
        match self {
            Self::Add(v) => Box::new(v.shrink().map(Self::Add)),
            Self::Remove(n) => Box::new(n.shrink().map(Self::Remove)),
            Self::Read { skip, take } => Box::new(
                take.shrink().zip(skip.shrink()).map(|(take, skip)| Self::Read { take, skip }),
            ),
        }
    }
}

#[track_caller]
fn collect_queue_items(qf: &mut QueueFile) -> Vec<Vec<u8>> {
    collect_queue_items_partial(qf, 0, qf.size() + 1)
}

#[track_caller]
fn collect_queue_items_partial(qf: &mut QueueFile, skip: usize, take: usize) -> Vec<Vec<u8>> {
    qf.iter().skip(skip).take(take).collect::<Vec<_>>()
}

#[track_caller]
fn compare_with_vecdeque(qf: &mut QueueFile, vd: &VecDeque<Vec<u8>>) {
    compare_with_vecdeque_partial(qf, vd, 0, vd.len() + 1);
}

#[track_caller]
fn compare_with_vecdeque_partial(
    qf: &mut QueueFile, vd: &VecDeque<Vec<u8>>, skip: usize, take: usize,
) {
    let left = collect_queue_items_partial(qf, skip, take);
    let right = vd.iter().skip(skip).take(take).cloned().collect::<Vec<_>>();
    assert_eq!(left, right);
}

#[quickcheck]
fn legacy_queue_is_vecdeque(actions: Vec<Action>) {
    let _lock = lock_failpoint_env();
    let path = auto_delete_path::AutoDeletePath::temp();
    let mut qf = QueueFile::open_legacy(&path).unwrap();
    let mut vd = VecDeque::new();

    for action in actions {
        match action {
            Action::Add(v) => {
                qf.add(&v).unwrap();
                vd.push_back(v);
            }
            Action::Read { take, skip } => compare_with_vecdeque_partial(&mut qf, &vd, skip, take),
            Action::Remove(n) => {
                vd.drain(..n.min(vd.len()));
                qf.remove_n(n).unwrap();
            }
        }

        compare_with_vecdeque(&mut qf, &vd);
    }
}

#[quickcheck]
fn queue_with_skip_header_update_is_vecdeque(actions: Vec<Action>) {
    let _lock = lock_failpoint_env();
    let path = auto_delete_path::AutoDeletePath::temp();
    let mut qf = QueueFile::open(&path).unwrap();
    qf.set_skip_write_header_on_add(true);
    let mut vd = VecDeque::new();

    for action in actions {
        match action {
            Action::Add(v) => {
                qf.add(&v).unwrap();
                vd.push_back(v);
            }
            Action::Read { take, skip } => compare_with_vecdeque_partial(&mut qf, &vd, skip, take),
            Action::Remove(n) => {
                vd.drain(..n.min(vd.len()));
                qf.remove_n(n).unwrap();
            }
        }

        compare_with_vecdeque(&mut qf, &vd);
    }

    let stored = collect_queue_items(&mut qf);
    drop(qf);

    let mut qf = QueueFile::open(&path).unwrap();
    let restored = collect_queue_items(&mut qf);
    assert_eq!(stored, restored);
}

#[quickcheck]
fn queue_is_vecdeque(actions: Vec<Action>) {
    let _lock = lock_failpoint_env();
    let path = auto_delete_path::AutoDeletePath::temp();
    let mut qf = QueueFile::open(&path).unwrap();
    let mut vd = VecDeque::new();

    for action in actions {
        match action {
            Action::Add(v) => {
                qf.add(&v).unwrap();
                vd.push_back(v);
            }
            Action::Read { take, skip } => compare_with_vecdeque_partial(&mut qf, &vd, skip, take),
            Action::Remove(n) => {
                vd.drain(..n.min(vd.len()));
                qf.remove_n(n).unwrap();
            }
        }

        compare_with_vecdeque(&mut qf, &vd);
    }
}

#[quickcheck]
fn queue_is_vecdeque_no_intermediate_comparisons(actions: Vec<Action>) {
    let path = auto_delete_path::AutoDeletePath::temp();
    let mut qf = QueueFile::open(&path).unwrap();
    let mut vd = VecDeque::new();

    for action in actions {
        match action {
            Action::Add(v) => {
                qf.add(&v).unwrap();
                vd.push_back(v);
            }
            Action::Read { take, skip } => compare_with_vecdeque_partial(&mut qf, &vd, skip, take),
            Action::Remove(n) => {
                vd.drain(..n.min(vd.len()));
                qf.remove_n(n).unwrap();
            }
        }
    }

    compare_with_vecdeque(&mut qf, &vd);
}

#[quickcheck]
fn small_queue_is_vecdeque(actions: Vec<Action>) {
    let path = auto_delete_path::AutoDeletePath::temp();
    let mut qf = QueueFile::with_capacity(&path, 32 + 32).unwrap();
    qf.set_overwrite_on_remove(false);
    let mut vd = VecDeque::new();

    for action in actions {
        match action {
            Action::Add(v) => {
                qf.add(&v).unwrap();
                vd.push_back(v);
            }
            Action::Read { take, skip } => compare_with_vecdeque_partial(&mut qf, &vd, skip, take),
            Action::Remove(n) => {
                vd.drain(..n.min(vd.len()));
                qf.remove_n(n).unwrap();
            }
        }

        compare_with_vecdeque(&mut qf, &vd);
    }
}

#[quickcheck]
fn small_queue_is_vecdeque_cached_offsets(actions: Vec<Action>) {
    let path = auto_delete_path::AutoDeletePath::temp();
    let mut qf = QueueFile::with_capacity(&path, 32 + 32).unwrap();
    qf.set_overwrite_on_remove(false);
    qf.set_cache_offset_policy(Some(OffsetCacheKind::Quadratic));
    let mut vd = VecDeque::new();

    for action in actions {
        match action {
            Action::Add(v) => {
                qf.add(&v).unwrap();
                vd.push_back(v);
            }
            Action::Read { take, skip } => compare_with_vecdeque_partial(&mut qf, &vd, skip, take),
            Action::Remove(n) => {
                vd.drain(..n.min(vd.len()));
                qf.remove_n(n).unwrap();
            }
        }

        compare_with_vecdeque(&mut qf, &vd);
    }
}

#[quickcheck]
fn add_n_works(actions: Vec<Action>) {
    let path = auto_delete_path::AutoDeletePath::temp();
    let mut qf = QueueFile::open(&path).unwrap();
    let mut vd = VecDeque::new();

    let mut adds = vec![];

    macro_rules! add_n_check {
        () => {
            qf.add_n(adds.iter().cloned()).unwrap();
            vd.extend(adds.drain(..));

            compare_with_vecdeque(&mut qf, &vd);
        };
    }

    for action in actions {
        match action {
            Action::Add(v) => adds.push(v),
            Action::Read { take, skip } => compare_with_vecdeque_partial(&mut qf, &vd, skip, take),
            Action::Remove(n) => {
                add_n_check!();

                vd.drain(..n.min(vd.len()));
                qf.remove_n(n).unwrap();
            }
        }

        compare_with_vecdeque(&mut qf, &vd);
    }

    add_n_check!();
}

#[test]
fn iter_nth() {
    let path = auto_delete_path::AutoDeletePath::temp();
    let mut qf = QueueFile::open(&path).unwrap();

    let a = vec![1];
    let b = vec![2, 3];
    let c = vec![4, 5, 6];
    qf.add_n(vec![a.clone(), b.clone(), c.clone()]).unwrap();

    assert_eq!(qf.iter().next(), Some(a));
    assert_eq!(qf.iter().nth(1), Some(b.clone()));
    assert_eq!(qf.iter().nth(2), Some(c.clone()));
    assert_eq!(qf.iter().nth(1), Some(b.clone()));
    assert_eq!(qf.iter().nth(2), Some(c.clone()));
    assert_eq!(qf.iter().nth(1), Some(b));
    assert_eq!(qf.iter().skip(1).nth(1), Some(c));
    assert_eq!(qf.iter().nth(3), None);
    assert_eq!(qf.iter().nth(123), None);
}

#[test]
fn add_n_accepts_non_clone_iterator_on_legacy_queue() {
    let path = auto_delete_path::AutoDeletePath::temp();
    let mut qf = QueueFile::open_legacy(&path).unwrap();

    let batch = vec![b"one".to_vec(), b"two".to_vec(), b"three".to_vec()];
    qf.add_n(NonCloneIter::new(batch.clone())).unwrap();

    let items: Vec<Vec<u8>> = qf.iter().collect();
    assert_eq!(items, batch);
}

#[test]
fn peek_supports_shared_borrow() {
    let path = auto_delete_path::AutoDeletePath::temp();
    let mut qf = QueueFile::open(&path).unwrap();
    qf.add(b"abc").unwrap();

    let head = qf.peek().unwrap().unwrap();
    assert_eq!(head.as_slice(), b"abc");
}

#[test]
fn iter_supports_shared_borrow() {
    let path = auto_delete_path::AutoDeletePath::temp();
    let mut qf = QueueFile::open(&path).unwrap();
    qf.add(b"one").unwrap();
    qf.add(b"two").unwrap();

    let items: Vec<Vec<u8>> = qf.iter().collect();
    assert_eq!(items, vec![b"one".to_vec(), b"two".to_vec()]);
}

#[test]
fn read_lock_can_peek_and_iter() {
    let path = auto_delete_path::AutoDeletePath::temp();
    let mut qf = QueueFile::open(&path).unwrap();
    qf.add(b"alpha").unwrap();
    qf.add(b"beta").unwrap();

    let qf = RwLock::new(qf);
    let guard = qf.read().unwrap();

    let head = guard.peek().unwrap().unwrap();
    let items: Vec<Vec<u8>> = guard.iter().collect();

    assert_eq!(head.as_slice(), b"alpha");
    assert_eq!(items, vec![b"alpha".to_vec(), b"beta".to_vec()]);
}

#[test]
fn iter_nth_large_payloads() {
    let path = auto_delete_path::AutoDeletePath::temp();
    let mut q = QueueFile::open(&path).unwrap();

    let payloads: Vec<Vec<u8>> = (0u8..20).map(|i| vec![i; 4096]).collect();

    for p in &payloads {
        q.add(p).unwrap();
    }

    assert_eq!(q.iter().next().unwrap(), payloads[0]);
    assert_eq!(q.iter().nth(10).unwrap(), payloads[10]);
    assert_eq!(q.iter().nth(19).unwrap(), payloads[19]);
    assert!(q.iter().nth(20).is_none());
}

/// Regression test: verify that `on_expansion` correctly updates `last.pos`
/// after a wrapped-queue expansion in the legacy (16-byte header) format.
#[test]
fn legacy_wrapped_expansion() {
    let p = auto_delete_path::AutoDeletePath::temp();
    let mut qf = QueueFile::open_legacy(&p).unwrap();

    for i in 0..50u32 {
        qf.add(&i.to_be_bytes()).unwrap();
    }
    qf.remove_n(20).unwrap();
    for i in 50..80u32 {
        qf.add(&i.to_be_bytes()).unwrap();
    }

    let items: Vec<u32> =
        qf.iter().map(|b| u32::from_be_bytes(b[..].try_into().unwrap())).collect();
    assert_eq!(items, (20..80u32).collect::<Vec<_>>());

    // Reopen to verify the header was committed with the relocated position.
    drop(qf);
    let qf2 = QueueFile::open_legacy(&p).unwrap();
    let items2: Vec<u32> =
        qf2.iter().map(|b| u32::from_be_bytes(b[..].try_into().unwrap())).collect();
    assert_eq!(items2, (20..80u32).collect::<Vec<_>>());
}

/// Same as `legacy_wrapped_expansion` but for the V1 (32-byte header) format.
#[test]
fn v1_wrapped_expansion() {
    let p = auto_delete_path::AutoDeletePath::temp();
    let mut qf = QueueFile::open(&p).unwrap();

    for i in 0..50u32 {
        qf.add(&i.to_be_bytes()).unwrap();
    }
    qf.remove_n(20).unwrap();
    for i in 50..80u32 {
        qf.add(&i.to_be_bytes()).unwrap();
    }

    let items: Vec<u32> =
        qf.iter().map(|b| u32::from_be_bytes(b[..].try_into().unwrap())).collect();
    assert_eq!(items, (20..80u32).collect::<Vec<_>>());

    // Reopen to verify the header was committed with the relocated position.
    drop(qf);
    let qf2 = QueueFile::open(&p).unwrap();
    let items2: Vec<u32> =
        qf2.iter().map(|b| u32::from_be_bytes(b[..].try_into().unwrap())).collect();
    assert_eq!(items2, (20..80u32).collect::<Vec<_>>());
}

/// With `overwrite_on_remove`, every byte of removed elements must be zeroed, including
/// elements larger than a single internal write chunk.
#[test_case(false, false; "v2 remove")]
#[test_case(false, true; "v2 clear")]
#[test_case(true, false; "legacy remove")]
#[test_case(true, true; "legacy clear")]
fn overwrite_on_remove_erases_large_elements(legacy: bool, clear: bool) {
    const MARKER: u8 = 0xAB;
    let p = auto_delete_path::AutoDeletePath::temp();
    {
        let mut qf = if legacy {
            QueueFile::open_legacy(&p).unwrap()
        } else {
            QueueFile::with_capacity(&p, 1024 * 1024).unwrap()
        };
        assert!(qf.overwrite_on_remove());

        qf.add(&vec![MARKER; 200 * 1024]).unwrap();
        qf.add(b"tail").unwrap();
        if clear {
            qf.clear().unwrap();
        } else {
            qf.remove().unwrap();
        }
    }

    let contents = std::fs::read(&p).unwrap();
    let leftover = contents.iter().filter(|&&b| b == MARKER).count();
    assert_eq!(leftover, 0, "{leftover} bytes of removed element data remain on disk");
}