qwal 0.1.0

A disk baced Write Ahead Log that can functuin as a FIFO queue
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
// Copyright 2021, The Tremor Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::match_error;

use super::{Entry, Error, Result};

use std::{io::SeekFrom, mem::size_of};

#[cfg(test)]
macro_rules! trace {
    ($s:expr $(, $opt:expr)*) => {
        eprintln!(concat!("[{}:{}] ", $s), file!(), line!(), $($opt),*)
    };
}

#[cfg(not(test))]
macro_rules! trace {
    ($s:expr $(, $opt:expr)*) => {
        concat!("[{}:{}] ", $s);
    };
}
#[cfg(feature = "async-std")]
use async_std::{
    fs::{File, OpenOptions},
    io::prelude::*,
    path::Path,
};
use byteorder::{BigEndian, ByteOrder};
#[cfg(feature = "tokio")]
use std::path::Path;
#[cfg(feature = "tokio")]
use tokio::{
    fs::{File, OpenOptions},
    io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt},
};

/// Represents entries in a write-ahead-log index data file
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
enum WalData {
    // Entry is a data record
    Data {
        idx: u64,
        ack_idx: u64,
        data: Vec<u8>,
    },
    /// Entry is an acknowledgement record
    Ack { idx: u64, ack_idx: u64 },
}

//
//  format:

impl WalData {
    const OFFSET_LEN: usize = 0;
    const OFFSET_IDX: usize = Self::OFFSET_LEN + size_of::<u64>();
    const OFFSET_ACK: usize = Self::OFFSET_IDX + size_of::<u64>();
    const OFFSET_DATA: usize = Self::OFFSET_ACK + size_of::<u64>();
    const OFFSET_TAILING_LEN: usize = Self::OFFSET_ACK + size_of::<u64>();

    async fn read(f: &mut File) -> Result<Option<Self>> {
        let mut buf = vec![0u8; size_of::<u64>() * 3];

        // read size
        if f.read_exact(&mut buf).await.is_err() {
            return Ok(None);
        }
        let len = BigEndian::read_u64(&buf[Self::OFFSET_LEN..]);

        // read id
        let idx = BigEndian::read_u64(&buf[Self::OFFSET_IDX..]);
        // read ack_id
        let ack_idx = BigEndian::read_u64(&buf[Self::OFFSET_ACK..]);
        if len == u64::MAX {
            let mut buf = vec![0u8; size_of::<u64>()];
            // THIS is a ack token
            f.read_exact(&mut buf).await?;
            let len2 = BigEndian::read_u64(&buf);
            if len2 != 0 {
                Err(Error::InvalidAck)
            } else {
                Ok(Some(Self::Ack { ack_idx, idx }))
            }
        } else {
            // read data
            let mut data = vec![0u8; len as usize];
            // We set the len since we know the len and then
            unsafe { data.set_len(len as usize) };
            f.read_exact(&mut data).await?;

            let mut buf = vec![0u8; size_of::<u64>()];
            f.read_exact(&mut buf).await?;
            let len2 = BigEndian::read_u64(&buf);
            if len2 != len {
                Err(Error::InvalidEntry)
            } else {
                Ok(Some(Self::Data { idx, ack_idx, data }))
            }
        }
    }

    fn size_on_disk_from_len(len: u64) -> u64 {
        let len64 = size_of::<u64>() as u64;
        if len == u64::MAX {
            len64 * 4
        } else {
            len + (len64 * 4)
        }
    }

    fn size_on_disk(&self) -> u64 {
        match self {
            WalData::Data { data, .. } => WalData::size_on_disk_from_len(data.len() as u64),
            WalData::Ack { .. } => WalData::size_on_disk_from_len(0),
        }
    }

    #[allow(clippy::uninit_vec)]
    async fn write(&self, w: &mut File) -> Result<u64> {
        let size_on_disk = self.size_on_disk() as usize;
        let mut buf: Vec<u8> = vec![0; size_on_disk];
        match self {
            WalData::Data { idx, ack_idx, data } => {
                // id + ack_id + len + len (tailing len)
                let len = data.len();
                BigEndian::write_u64(&mut buf[Self::OFFSET_LEN..], len as u64);
                BigEndian::write_u64(&mut buf[Self::OFFSET_IDX..], *idx);
                BigEndian::write_u64(&mut buf[Self::OFFSET_ACK..], *ack_idx);
                buf[Self::OFFSET_DATA..(Self::OFFSET_DATA + len)].clone_from_slice(data);
                BigEndian::write_u64(&mut buf[(Self::OFFSET_DATA + len)..], len as u64); // len2
                w.write_all(&buf).await?;
            }
            WalData::Ack { ack_idx, idx } => {
                BigEndian::write_u64(&mut buf[Self::OFFSET_LEN..], u64::MAX);
                BigEndian::write_u64(&mut buf[Self::OFFSET_IDX..], *idx);
                BigEndian::write_u64(&mut buf[Self::OFFSET_ACK..], *ack_idx);
                BigEndian::write_u64(&mut buf[Self::OFFSET_TAILING_LEN..], 0);
                w.write_all(&buf).await?;
            }
        }
        Ok(self.size_on_disk())
    }

    /// Fetches the index of the most recent acknowledgement
    pub fn ack_idx(&self) -> u64 {
        match self {
            WalData::Data { ack_idx, .. } | WalData::Ack { ack_idx, .. } => *ack_idx,
        }
    }

    /// Fetches the curent index
    pub fn idx(&self) -> u64 {
        match self {
            WalData::Data { idx, .. } | WalData::Ack { idx, .. } => *idx,
        }
    }
}

/// Represents an data file in a write-ahead-log-structure
#[derive(Debug)]
pub struct WalFile {
    /// Reference to the data file
    pub(crate) file: File,
    /// The next index to be written
    pub(crate) next_idx_to_write: u64,
    /// The write offset
    pub(crate) write_offset: u64,
    /// The next index to be read
    pub(crate) next_idx_to_read: u64,
    /// The read offset
    pub(crate) read_pointer: u64,
    /// The index for acknowledged entries
    pub(crate) ack_idx: u64,
    /// The most recent acknowledgement offset
    pub(crate) ack_written: u64,
}

impl WalFile {
    async fn sync(&self) -> Result<()> {
        self.file.sync_all().await.map_err(Error::Io)
    }

    /// Persists an acknowledgement at the end of the data file at the after the last committed write operation
    pub(crate) async fn preserve_ack(&mut self) -> Result<()> {
        trace!("Appending ack index {} to {:?}", self.ack_idx, self.file);

        let ack_idx = self.ack_idx;
        self.ack_written = ack_idx;

        let data = WalData::Ack {
            // we remove this since we usually ack with the previos index and this is no real data
            idx: self.next_idx_to_write - 1,
            ack_idx,
        };
        self.file.seek(SeekFrom::Start(self.write_offset)).await?;
        self.write_offset += data.write(&mut self.file).await?;
        self.sync().await
    }

    /// Closes this write-ahead-log data file
    pub async fn close(mut self) -> Result<()> {
        trace!("Closing WAL file {:?}", self);
        if self.ack_written != self.ack_idx {
            self.preserve_ack().await?;
        }
        Ok(())
    }

    /// Push an entry into the write-ahead-log data file
    pub async fn push<E>(&mut self, data: E) -> std::result::Result<u64, Error<E::Error>>
    where
        E: Entry,
    {
        let idx = self.next_idx_to_write;
        self.next_idx_to_write += 1;

        let ack_idx = self.ack_idx;
        self.ack_written = ack_idx;
        let data = WalData::Data {
            idx,
            ack_idx,
            data: data.serialize().map_err(Error::Entry)?,
        };
        self.file.seek(SeekFrom::Start(self.write_offset)).await?;
        self.write_offset += data.write(&mut self.file).await.map_err(match_error)?;
        self.sync().await.map_err(match_error)?;
        Ok(idx)
    }

    /// Pop an entry from the write-ahead-log data file
    pub async fn pop<E>(&mut self) -> std::result::Result<Option<(u64, E::Output)>, Error<E::Error>>
    where
        E: Entry,
    {
        self.file.seek(SeekFrom::Start(self.read_pointer)).await?;
        loop {
            let data = WalData::read(&mut self.file).await.map_err(match_error)?;
            let advance_by = data.as_ref().map(WalData::size_on_disk).unwrap_or_default();
            trace!("Advance read pointer by: {}", advance_by);
            self.read_pointer += advance_by;
            match data {
                None => return Ok(None),
                Some(WalData::Data { idx, data, .. }) => {
                    self.next_idx_to_read = idx + 1;
                    return Ok(Some((idx, E::deserialize(data).map_err(Error::Entry)?)));
                }
                Some(WalData::Ack { .. }) => {}
            }
        }
    }

    /// Convenience for debugging
    pub async fn inspect<P, E>(path: P) -> Result<()>
    where
        P: AsRef<Path>,
        E: Entry,
    {
        let mut o = OpenOptions::new();
        o.create(false);
        o.write(false);
        o.read(true);
        let mut file = o.open(&path).await?;
        let mut offset = 0;
        while let Some(data) = WalData::read(&mut file).await? {
            println!("{offset:9}: {:?}", data);
            offset = file.seek(SeekFrom::Current(0)).await?
        }
        Ok(())
    }

    /// Open a write-ahead-log data file
    pub async fn open<P>(path: P) -> Result<Self>
    where
        P: AsRef<Path>,
    {
        let p: &Path = path.as_ref();
        let mut o = OpenOptions::new();
        trace!("Opening existing WAL file: {:?}", p.to_string_lossy());
        if exists(p).await {
            trace!("  Opening...");
            o.create(false);
            o.write(true);
            o.read(true);

            let mut file = o.open(&path).await?;

            file.seek(SeekFrom::End(-8)).await?;

            let mut len = vec![0u8; 8];
            file.read_exact(&mut len).await?;
            let len = BigEndian::read_u64(&len);
            let read_offset = file
                .seek(SeekFrom::End(-(WalData::size_on_disk_from_len(len) as i64)))
                .await?;

            let data = WalData::read(&mut file).await?.ok_or(Error::InvalidFile)?;
            let write_offset = file.seek(SeekFrom::Current(0)).await?;

            let next_idx_to_read = data.ack_idx() + 1;
            let mut wal = WalFile {
                file,
                next_idx_to_write: data.idx() + 1,
                write_offset,
                next_idx_to_read,
                read_pointer: read_offset,
                ack_idx: data.ack_idx(),
                ack_written: data.ack_idx(),
            };

            if data.idx() != wal.next_idx_to_read {
                wal.seek_to(wal.next_idx_to_read).await?
            }
            trace!("Wal opened: {:?}", wal);
            Ok(wal)
        } else {
            trace!("  Creating...");
            let mut o = OpenOptions::new();
            o.create(true);
            o.read(true);
            o.write(true);
            Ok(WalFile {
                file: o.open(path).await?,
                next_idx_to_write: 1,
                write_offset: 0,
                next_idx_to_read: 1,
                read_pointer: 0,
                ack_idx: 0,
                ack_written: 0,
            })
        }
    }

    /// Retrieve the write offset for this data file
    pub fn size(&self) -> u64 {
        self.write_offset
    }

    // Seek to a specified index for the next read operation
    pub async fn seek_to(&mut self, next_idx_to_read: u64) -> Result<()> {
        trace!("Seeking to {} in {:?}", next_idx_to_read, self.file);
        self.file.seek(SeekFrom::Start(0)).await?;
        match WalData::read(&mut self.file).await? {
            // This would mean we want to seek infront of the file, in this case
            // just stick with the first element
            Some(data) if data.idx() > next_idx_to_read => {
                trace!("First index {} > {}", data.idx(), next_idx_to_read);
                self.read_pointer = 0;
                self.next_idx_to_read = data.idx();
            }
            // This is the correct element, we set the offset to zero and read from here on
            Some(data) if data.idx() == next_idx_to_read => {
                // since the currently read data is the data we wanted to seek to
                // we have to move the read pointer one back
                let read_offset = 0;
                trace!(
                    "First index {} == {} => read_offset: {}",
                    data.idx(),
                    next_idx_to_read,
                    read_offset
                );
                self.read_pointer = read_offset;
                self.next_idx_to_read = next_idx_to_read;
            }
            Some(_) => loop {
                let read_offset = self.pos().await?;
                if let Some(data) = WalData::read(&mut self.file).await? {
                    trace!(
                        "Testing {} > {} @ {}",
                        data.idx(),
                        next_idx_to_read,
                        read_offset
                    );
                    if data.idx() >= next_idx_to_read {
                        self.read_pointer = read_offset;
                        self.next_idx_to_read = next_idx_to_read;
                        break;
                    }
                } else {
                    trace!(
                        "EOF Reached next read: {} @ {}",
                        next_idx_to_read,
                        read_offset
                    );
                    self.read_pointer = read_offset;
                    self.next_idx_to_read = next_idx_to_read;
                    break;
                }
            },
            None => {
                trace!("No entries found setting read_idx and read_offset to 0");
                self.read_pointer = 0;
                self.next_idx_to_read = 0;
            }
        }
        Ok(())
    }

    async fn pos(&mut self) -> Result<u64> {
        self.file
            .seek(SeekFrom::Current(0))
            .await
            .map_err(Error::Io)
    }

    // Mark up to the specified index as acknowledged
    pub fn ack(&mut self, idx: u64) {
        trace!("Marking ack as {} in {:?}", idx, self.file);
        self.ack_idx = idx;
    }
}

#[cfg(feature = "async-std")]
async fn exists(p: &Path) -> bool {
    p.exists().await
}

#[cfg(feature = "tokio")]
async fn exists(p: &Path) -> bool {
    p.exists()
}

#[cfg(test)]
mod test {

    use super::*;
    use tempfile::Builder as TempDirBuilder;

    #[cfg_attr(feature = "async-std", async_std::test)]
    #[cfg_attr(feature = "tokio", tokio::test)]
    async fn file() -> Result<()> {
        let temp_dir = TempDirBuilder::new().prefix("tremor-wal").tempdir()?;
        let mut path = temp_dir.path().to_path_buf();
        path.push("wal.file");

        {
            let mut w = WalFile::open(&path).await?;

            assert_eq!(w.push(b"1".to_vec()).await?, 1);
            assert_eq!(w.pop::<Vec<u8>>().await?, Some((1, b"1".to_vec())));

            w.close().await?;
        }
        {
            let mut w = WalFile::open(&path).await?;

            assert_eq!(w.pop::<Vec<u8>>().await?, Some((1, b"1".to_vec())));
            w.ack(1);

            assert_eq!(w.push(b"22".to_vec()).await?, 2);
            assert_eq!(w.pop::<Vec<u8>>().await?, Some((2, b"22".to_vec())));
            w.close().await?;
        }
        {
            let mut w = WalFile::open(&path).await?;
            assert_eq!(w.pop::<Vec<u8>>().await?, Some((2, b"22".to_vec())));
            w.ack(2);

            assert_eq!(w.push(b"33".to_vec()).await?, 3);
            assert_eq!(w.pop::<Vec<u8>>().await?, Some((3, b"33".to_vec())));
            w.ack(3);

            w.close().await?;
        }
        let mut w = WalFile::open(&path).await?;
        assert_eq!(w.pop::<Vec<u8>>().await?, None);

        Ok(())
    }
}