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
//! Low-level I/O operations for queue file reading and writing.
//!
//! This module provides the core I/O abstractions:
//! - [`QueueFileInner`] - the low-level file handle and buffer management
//! - [`DataRing`] - ring buffer abstraction for reading
//! - [`DataRingMut`] - ring buffer abstraction for writing
//!
//! The ring buffer handles the wraparound logic for the queue's circular
//! file layout, where writes can wrap from the end of the file to the beginning.

use std::cmp::min;
use std::fs::File;
use std::io as io_std;
#[cfg(windows)]
use std::io::Read;
use std::io::{Seek, SeekFrom, Write};
#[cfg(unix)]
use std::os::unix::fs::FileExt;
#[cfg(windows)]
use std::os::windows::fs::FileExt;

use crate::error::{Result, maybe_inject_failpoint};

/// Represents a phase of batched operations that can defer syncing.
///
/// Used to inject test failures at specific points during large operations.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DeferredSyncPhase {
    /// Copying data during file expansion.
    ExpansionCopy,
    /// Erasing data during clear.
    ClearErase,
    /// Rewriting backlinks after expansion.
    BacklinkRewrite,
    /// Appending multiple elements in a batch.
    AppendBatch,
}

impl DeferredSyncPhase {
    #[inline]
    pub const fn failpoint_name(self) -> &'static str {
        match self {
            Self::ExpansionCopy => "v2_expansion_copy_per_write_sync",
            Self::ClearErase => "clear_erase_per_write_sync",
            Self::BacklinkRewrite => "v2_backlink_rewrite_per_write_sync",
            Self::AppendBatch => "v2_add_batch_per_write_sync",
        }
    }
}

/// Low-level file handle and buffer management for queue operations.
///
/// This struct wraps the raw file handle and maintains internal state
/// for efficient I/O, including seek optimization and transfer buffers.
#[derive(Debug)]
pub struct QueueFileInner {
    /// The underlying file handle.
    pub file: Option<File>,
    /// Current file length.
    pub file_len: u64,
    /// The position we expect to be at after a seek.
    pub expected_seek: u64,
    /// The last position actually seeked to (for optimization).
    pub last_seek: Option<u64>,
    /// Buffer used for transfer operations (expansion, relocation).
    pub transfer_buf: Box<[u8]>,
    /// Whether to sync after each write.
    pub sync_writes: bool,
    /// Current deferred sync phase, if any.
    pub deferred_sync_phase: Option<DeferredSyncPhase>,
}

impl QueueFileInner {
    /// Size of the transfer buffer used for file expansion.
    pub const TRANSFER_BUFFER_SIZE: usize = 128 * 1024;

    /// Returns a reference to the underlying file.
    #[inline]
    pub fn file_ref(&self) -> io_std::Result<&File> {
        self.file.as_ref().ok_or_else(|| {
            io_std::Error::new(io_std::ErrorKind::Other, "queue file handle unavailable")
        })
    }

    /// Returns a mutable reference to the underlying file.
    #[inline]
    pub fn file_mut(&mut self) -> io_std::Result<&mut File> {
        self.file.as_mut().ok_or_else(|| {
            io_std::Error::new(io_std::ErrorKind::Other, "queue file handle unavailable")
        })
    }

    /// Sets the expected seek position (without actually seeking).
    #[inline]
    pub fn seek(&mut self, pos: u64) -> u64 {
        self.expected_seek = pos;
        pos
    }

    /// Performs the actual seek operation, using optimization to skip redundant seeks.
    pub fn real_seek(&mut self) -> io_std::Result<u64> {
        if Some(self.expected_seek) == self.last_seek {
            return Ok(self.expected_seek);
        }

        let expected_seek = self.expected_seek;
        let res = self.file_mut()?.seek(SeekFrom::Start(expected_seek));
        self.last_seek = res.as_ref().ok().copied();

        res
    }

    /// Writes the given buffer to the file at the expected seek position.
    ///
    /// Handles sync behavior based on `sync_writes` and `deferred_sync_phase`.
    pub fn write(&mut self, buf: &[u8]) -> Result<()> {
        self.real_seek()?;

        self.file_mut()?.write_all(buf)?;

        // Advance the logical cursor too, so consecutive writes append instead of rewriting
        // the same range.
        self.expected_seek += buf.len() as u64;
        if let Some(seek) = &mut self.last_seek {
            *seek += buf.len() as u64;
        }

        if self.sync_writes {
            if let Some(phase) = self.deferred_sync_phase {
                maybe_inject_failpoint(phase.failpoint_name())?;
            }
            self.file_mut()?.sync_data()?;
        }

        Ok(())
    }

    pub fn read_exact_at(&self, mut offset: u64, mut buf: &mut [u8]) -> io_std::Result<()> {
        while !buf.is_empty() {
            let read = self.read_at(offset, buf)?;
            if read == 0 {
                return Err(io_std::Error::new(
                    io_std::ErrorKind::UnexpectedEof,
                    "failed to fill whole buffer",
                ));
            }

            offset += read as u64;
            buf = &mut buf[read..];
        }

        Ok(())
    }

    #[cfg(unix)]
    pub fn read_at(&self, offset: u64, buf: &mut [u8]) -> io_std::Result<usize> {
        self.file_ref()?.read_at(buf, offset)
    }

    #[cfg(windows)]
    pub fn read_at(&self, offset: u64, buf: &mut [u8]) -> io_std::Result<usize> {
        self.file_ref()?.seek_read(buf, offset)
    }

    pub fn transfer(&mut self, read_pos: u64, write_pos: u64, count: u64) -> Result<()> {
        if write_pos > read_pos && write_pos < read_pos + count {
            return Err(crate::error::Error::Io {
                source: io_std::Error::new(
                    io_std::ErrorKind::InvalidInput,
                    "overlapping transfer ranges not supported",
                ),
            });
        }

        let file = self.file.as_mut().ok_or_else(|| crate::error::Error::Io {
            source: io_std::Error::new(io_std::ErrorKind::InvalidInput, "no file"),
        })?;

        let mut bytes_left = count;
        let mut current_read = read_pos;
        let mut current_write = write_pos;

        while bytes_left > 0 {
            let bytes_to_read = min(bytes_left, Self::TRANSFER_BUFFER_SIZE as u64) as usize;
            let slice = &mut self.transfer_buf[..bytes_to_read];

            #[cfg(unix)]
            {
                file.read_exact_at(slice, current_read)?;
                file.write_all_at(slice, current_write)?;
            }

            #[cfg(windows)]
            {
                file.seek(SeekFrom::Start(current_read))?;
                file.read_exact(slice)?;

                file.seek(SeekFrom::Start(current_write))?;
                file.write_all(slice)?;
            }

            current_read += bytes_to_read as u64;
            current_write += bytes_to_read as u64;
            bytes_left -= bytes_to_read as u64;
        }

        if self.sync_writes {
            file.sync_data()?;
        }

        self.expected_seek = current_write;
        self.last_seek = Some(current_write);

        Ok(())
    }

    pub fn sync_set_len(&mut self, new_len: u64) -> io_std::Result<()> {
        self.file_mut()?.set_len(new_len)?;
        self.file_len = new_len;
        self.file_mut()?.sync_all()
    }

    pub fn write_zeroes(&mut self, len: u64) -> Result<()> {
        static ZEROES: [u8; 64 * 1024] = [0; 64 * 1024];

        let mut remaining = len;
        while remaining > 0 {
            let chunk = min(remaining, ZEROES.len() as u64) as usize;
            self.write(&ZEROES[..chunk])?;
            remaining -= chunk as u64;
        }

        Ok(())
    }

    pub fn write_zero_chunks(&mut self, pos: u64, len: usize) -> Result<()> {
        self.seek(pos);
        self.write_zeroes(len as u64)
    }

    pub fn with_deferred_sync<T>(
        &mut self, phase: DeferredSyncPhase, f: impl FnOnce(&mut Self) -> Result<T>,
    ) -> Result<T> {
        let sync_writes = self.sync_writes;
        let deferred_sync_phase = self.deferred_sync_phase;
        self.sync_writes = false;
        self.deferred_sync_phase = Some(phase);

        let result = f(self);

        self.deferred_sync_phase = deferred_sync_phase;
        self.sync_writes = sync_writes;

        let value = result?;

        if sync_writes {
            self.file_mut()?.sync_data()?;
            maybe_inject_failpoint(match phase {
                DeferredSyncPhase::ExpansionCopy => "v2_after_expansion_copy_flush",
                DeferredSyncPhase::ClearErase => "clear_after_erase_flush",
                DeferredSyncPhase::BacklinkRewrite => "v2_after_backlink_rewrite_flush",
                DeferredSyncPhase::AppendBatch => "v2_after_add_batch_flush",
            })?;
        }

        Ok(value)
    }
}

/// Ring buffer abstraction for reading from the queue data region.
///
/// Provides logical addressing where positions wrap around, handling
/// the circular nature of the queue file.
#[derive(Debug, Clone, Copy)]
pub struct DataRing<'a> {
    inner: &'a QueueFileInner,
    /// Offset where data region starts in the file.
    pub data_start: u64,
}

impl<'a> DataRing<'a> {
    #[inline]
    pub const fn new(inner: &'a QueueFileInner, data_start: u64) -> Self {
        Self { inner, data_start }
    }

    #[inline]
    pub const fn capacity(&self) -> u64 {
        self.inner.file_len - self.data_start
    }

    #[inline]
    #[allow(dead_code)]
    pub const fn phys_pos(&self, logical_pos: u64) -> u64 {
        self.data_start + (logical_pos % self.capacity())
    }

    #[inline]
    pub const fn add(&self, logical_pos: u64, delta: u64) -> u64 {
        (logical_pos + delta) % self.capacity()
    }

    #[inline]
    pub const fn distance(&self, from: u64, to: u64) -> u64 {
        let cap = self.capacity();
        (to + cap - from) % cap
    }

    pub fn read_at(&self, logical_pos: u64, buf: &mut [u8]) -> io_std::Result<()> {
        let cap = self.capacity();
        let mut pos = logical_pos % cap;
        let mut bytes_read = 0;
        let mut remaining = buf.len();

        while remaining > 0 {
            let phys = self.data_start + pos;
            let can_read = min(remaining as u64, cap - pos) as usize;
            self.inner.read_exact_at(phys, &mut buf[bytes_read..bytes_read + can_read])?;

            bytes_read += can_read;
            remaining -= can_read;
            pos = 0;
        }

        Ok(())
    }
}

/// Ring buffer abstraction for writing to the queue data region.
#[derive(Debug)]
pub struct DataRingMut<'a> {
    /// Mutable reference to the inner queue file state.
    pub inner: &'a mut QueueFileInner,
    /// Offset where data region starts in the file.
    pub data_start: u64,
}

impl<'a> DataRingMut<'a> {
    #[inline]
    pub fn new(inner: &'a mut QueueFileInner, data_start: u64) -> Self {
        Self { inner, data_start }
    }

    #[inline]
    pub fn as_read_only(&self) -> DataRing<'_> {
        DataRing { inner: self.inner, data_start: self.data_start }
    }

    #[inline]
    pub fn capacity(&self) -> u64 {
        self.as_read_only().capacity()
    }

    #[inline]
    pub fn add(&self, logical_pos: u64, delta: u64) -> u64 {
        self.as_read_only().add(logical_pos, delta)
    }

    pub fn write_at(&mut self, logical_pos: u64, buf: &[u8]) -> Result<()> {
        let cap = self.capacity();
        let mut pos = logical_pos % cap;
        let mut bytes_written = 0;
        let mut remaining = buf.len();

        while remaining > 0 {
            let phys = self.data_start + pos;
            let can_write = min(remaining as u64, cap - pos) as usize;
            self.inner.seek(phys);
            self.inner.write(&buf[bytes_written..bytes_written + can_write])?;

            bytes_written += can_write;
            remaining -= can_write;
            pos = 0;
        }

        Ok(())
    }

    pub fn erase_at(&mut self, logical_pos: u64, len: usize) -> Result<()> {
        let cap = self.capacity();
        let mut pos = logical_pos % cap;
        let mut remaining = len as u64;

        while remaining > 0 {
            let phys = self.data_start + pos;
            let can_erase = min(remaining, cap - pos);
            self.inner.write_zero_chunks(phys, can_erase as usize)?;

            remaining -= can_erase;
            pos = 0;
        }

        Ok(())
    }

    pub fn relocate(&mut self, orig_file_len: u64, moved_count: u64) -> Result<()> {
        if moved_count == 0 {
            return Ok(());
        }

        let data_start = self.data_start;
        self.inner.with_deferred_sync(DeferredSyncPhase::ExpansionCopy, |inner| {
            inner.transfer(data_start, orig_file_len, moved_count)
        })
    }
}