syncless 0.2.0

ordered, atomic storage without durability guarantees
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
use std::fs::File;
use std::path::{Path, PathBuf};
use std::collections::BTreeMap;
use std::io::{Read, Seek, SeekFrom};
use std::ops::Bound::*;
use std::cmp::min;
use std::marker::PhantomData;
use crate::Error;
use crate::header;
use crate::record;
use crate::Store;
use crate::{ReadOnly, Writable, WriteOpenMode};

/// An open Syncless store.
pub(crate) struct StoreBase {
    path: PathBuf,
    file: File,
    spans: BTreeMap<u64, Span>,
    file_size: u64,
}

impl StoreBase {
    pub fn size(&self) -> u64 {
        self.spans
            .last_key_value()
            .map(|(off, span)| off + span.len)
            .unwrap_or(0)
    }
}

pub(crate) struct Span {
    /// How long is the data in this span (in practice, less than MAX_RECORD_SIZE).
    pub len: u64,
    /// Where the physical file is the span data (i.e. after header).
    pub file_data_offset: u64,
    /// Did we freshly write this span?  If so, ZFS on Ubuntu (at least) may fart back zeroes
    /// at us: we need to recheck this and fdatasync if we see this.  Thanks Obama!
    pub validated: bool,
}

/// Parse header of new file, load up records.
fn read_newfile(base: &mut StoreBase, compatible: fn(&header::HeaderVer) -> bool) -> Result<(), Error>
{
    let hver = header::read_header(&mut base.file, &mut base.file_size)?;

    if !compatible(&hver) {
        return Err(Error::UnsupportedVersion);
    }

    while let Some(record) = record::read_next_record(&mut base.file, &mut base.file_size)? {
        record::add_record(&mut base.spans,
                           record.hdr.logical_offset,
                           record.hdr.length,
                           record.file_data_offset, true);
    }
    Ok(())
}

/// Opens an existing syncless store readonly.
///
/// On success, the returned [`Store`] represents a logically consistent
/// view reconstructed from the on-disk log.
///
/// # Errors
///
/// Returns an error if the file cannot be opened (using the
/// underlying OS error), is not a valid syncless store, or is a
/// future incompatible version.
pub fn open_readonly<P: AsRef<Path>>(
    path: P,
) -> Result<Store<ReadOnly>, Error> {
    let path = path.as_ref().to_path_buf();
    let mut oo = std::fs::OpenOptions::new();
    oo.read(true);

    let file = oo.open(&path)?;

    let mut base = StoreBase {
        path: path,
        file: file,
        spans: BTreeMap::new(),
        file_size: 0,
    };

    read_newfile(&mut base, header::HeaderVer::is_read_compatible)?;
    Ok(Store {base, writable: false, _mode: PhantomData })
}

pub(crate) fn open_writable_base<P: AsRef<Path>>(
    path: P,
    mode: WriteOpenMode,
) -> Result<StoreBase, Error> {
    let path = path.as_ref().to_path_buf();
    let mut oo = std::fs::OpenOptions::new();
    oo.read(true);
    oo.write(true);

    match mode {
        WriteOpenMode::MustExist => { oo.create(false); }
        WriteOpenMode::MustNotExist => { oo.create_new(true); }
        WriteOpenMode::MayExist => { oo.create(true); }
    }

    let file = oo.open(&path)?;

    let mut base = StoreBase {
        path: path,
        file: file,
        spans: BTreeMap::new(),
        file_size: 0,
    };

    // Special case: empty file, we write header.
    if base.file.metadata()?.len() == 0 {
        base.file_size = header::write_header(&mut base.file)?;
        base.file.sync_all()?;
    } else {
        read_newfile(&mut base, header::HeaderVer::is_write_compatible)?;
    }
    Ok(base)
}


/// Opens an existing syncless store for reading and writing.
///
/// On success, the returned [`Store`] represents a logically consistent
/// view reconstructed from the on-disk log.
///
/// # Errors
///
/// Returns an error if the file cannot be opened for writing (using the
/// underlying OS error), is not a valid syncless store, or is a
/// future incompatible version.
pub fn open<P: AsRef<Path>>(
    path: P,
    mode: WriteOpenMode,
) -> Result<Store<Writable>, Error> {
    Ok(Store {base: open_writable_base::<P>(path, mode)?,
              writable: true,
              _mode: PhantomData})
}

fn validate_record_with_retry(
    file: &mut File,
    file_data_offset: u64,
    length: u64,
) -> Result<(), Error> {
    if record::validate(file, file_data_offset, length as usize)? {
        return Ok(());
    }

    file.sync_data()?;

    if record::validate(file, file_data_offset, length as usize)? {
        return Ok(());
    }

    Err(Error::CorruptRecord)
}

impl<M> Store<M>
{
    /// Returns the logical size of the store in bytes.
    ///
    /// Reading past this gives zeros.  Writing past this successfully is
    /// the only way to increase its value.
    pub fn size(&self) -> u64 {
        self.base.size()
    }

    /// Get offset of prior record (or 0)
    fn prev_offset(&self, offset: u64) -> u64 {
        self.base.spans
            .range((Included(0), Excluded(offset)))
            .next_back()
            .map(|(&off, _)| off)
            .unwrap_or(0)
    }

    /// Validate any spans in this range not already validated.
    fn validate_range(&mut self, start: u64, end: u64) -> Result<(), Error> {
        if !self.writable {
            return Ok(());
        }

        let to_validate: Vec<(u64, u64, u64)> = self.base.spans
            .range((Included(start), Excluded(end)))
            .filter_map(|(&off, span)| {
                if span.validated {
                    None
                } else {
                    Some((off, span.file_data_offset, span.len))
                }
            })
            .collect();

        // Validate them all.
        for &(_, file_data_offset, length) in &to_validate {
            validate_record_with_retry(&mut self.base.file, file_data_offset, length)?;
        }

        // Set them all valid.
        for &(off, _, _) in &to_validate {
            let span = self.base.spans.get_mut(&off).unwrap();
            span.validated = true;
        }
        Ok(())
    }

    /// Reads `buf.len()` bytes starting at `offset`.
    ///
    /// The read is performed against the reconstructed logical view of the
    /// store.  If there's a hole, or past EOF, it will read as all zeros.
    ///
    /// # Errors
    ///
    /// Return zeros past the logical size of the store (see size()), and an
    /// error on underlying I/O error.
    pub fn read(&mut self, mut offset: u64, mut buf: &mut [u8]) -> Result<(), Error> {
        // Holes are zeros, so simply zero it out to start.
        buf.fill(0);

        let prev = self.prev_offset(offset);
        self.validate_range(prev, offset + buf.len() as u64)?;

        // End of previous span may overlap.
        if let Some(span) = self.base.spans.get(&prev) {
            if prev + span.len > offset {
                // FIXME: mmap
                let bytes_before = offset - prev;
                let len = min(span.len - bytes_before, buf.len() as u64);
                self.base.file.seek(SeekFrom::Start(span.file_data_offset + bytes_before))?;
                self.base.file.read_exact(&mut buf[..len as usize])?;
                offset += len;
                buf = &mut buf[len as usize..];
            }
        }

        for (&off, span) in self.base.spans.range((Included(offset), Excluded(offset + buf.len() as u64))) {
            // Skip over any bytes not covered by span.
            let bytes_until_span = off - offset;
            if bytes_until_span != 0 {
                offset += bytes_until_span;
                buf = &mut buf[bytes_until_span as usize..];
            }

            // Read in span.
            let len = min(span.len, buf.len() as u64);
            self.base.file.seek(SeekFrom::Start(span.file_data_offset))?;
            self.base.file.read_exact(&mut buf[..len as usize])?;
            offset += len;
            buf = &mut buf[len as usize..];
        }
        Ok(())
    }
}

fn compact(base: &mut StoreBase) -> Result<StoreBase, Error> {
    let path = base.path.clone();
    let tmp = path.with_extension("compact");

    // Fresh file: if we crashed before, overwrite.
    let mut oo = std::fs::OpenOptions::new();
    oo.write(true);
    oo.create(true);
    oo.truncate(true);

    let mut file = oo.open(&tmp)?;
    let mut file_len = header::write_header(&mut file)?;

    // Suck up all the data.
    let mut data = vec![0u8; base.size() as usize];
    for (off, span) in &base.spans {
        base.file.seek(SeekFrom::Start(span.file_data_offset))?;
        base.file.read_exact(&mut data[*off as usize..(*off + span.len) as usize])?;
    }

    // Write it out, make sure it hit disk.
    record::write_record(&mut file, 0, &data, &mut file_len)?;
    file.sync_data()?;

    // atomic replace
    std::fs::rename(&tmp, &path)?;

    // It's possible that the atomic replace is not actually atomic,
    // but this is the best we can do.
    let parent = path.parent().unwrap();
    let dir = File::open(parent)?;
    dir.sync_all()?;

    // reopen into a fresh StoreBase
    open_writable_base(&path, WriteOpenMode::MustExist)
}    

impl Store<Writable> {
    /// Writes `buf.len()` bytes starting at `offset`.
    ///
    /// You can write anywhere, but if you create holes they will be
    /// zero-filled.  Writes are ordered and become atomically visible
    /// on success.  No durability guarantees: the effects of this
    /// write may be lost on crash or power failure.  However, the
    /// effects of this write will never be observed without also
    /// observing the effects of all previous successful writes.
    ///
    /// This will rewrite the file (using fsync and rename) if it gets
    /// more than 100x bigger than the contents.
    ///
    /// # Errors
    ///
    /// Returns an error on underlying I/O problems (probably out of disk space).
    pub fn write(&mut self, mut offset: u64, mut buf: &[u8]) -> Result<(), Error> {
        // Validate anything we're going to overwrite.
        self.validate_range(self.prev_offset(offset), offset + buf.len() as u64)?;

        while !buf.is_empty() {
            let chunk = &buf[..min(buf.len(), record::MAX_RECORD_SIZE)];

            let data_off = record::write_record(&mut self.base.file, offset, chunk, &mut self.base.file_size)?;
            record::add_record(&mut self.base.spans, offset, chunk.len() as u64, data_off, false);
            buf = &buf[chunk.len()..];
            offset += chunk.len() as u64;
        }

        // Compact when we're over 100x larger than we should be (unless we're tiny anyway)
        if self.base.file_size > 1_000_000 && self.base.file_size * 100 > self.size() {
            self.validate_range(0, self.size())?;
            self.base = compact(&mut self.base)?;
        }

        Ok(())
    }

    /// Convert this writable store into a readonly one.
    pub fn into_readonly(mut self) -> Result<Store<ReadOnly>, Error> {
        // Before we make it readonly, make sure all spans are validated!
        self.validate_range(0, self.size())?;

        Ok(Store {
            base: self.base,
            writable: false,
            _mode: PhantomData,
        })
    }
}

#[cfg(test)]

#[test]
fn empty_store_size_zero() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("s");

    let store = open(&path, WriteOpenMode::MayExist).unwrap();
    assert_eq!(store.size(), 0);
}

#[test]
fn write_then_read() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("s");

    let mut store = open(&path, WriteOpenMode::MayExist).unwrap();

    store.write(0, b"hello").unwrap();
    assert_eq!(store.size(), 5);

    let mut buf = [0u8; 5];
    store.read(0, &mut buf).unwrap();

    assert_eq!(&buf, b"hello");
}

#[test]
fn overwrite_middle() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("s");

    let mut store = open(&path, WriteOpenMode::MayExist).unwrap();

    store.write(0, b"abcdefgh").unwrap();
    store.write(2, b"XYZ").unwrap();

    let mut buf = [0u8; 8];
    store.read(0, &mut buf).unwrap();

    assert_eq!(&buf, b"abXYZfgh");
}

#[test]
fn holes_are_zero() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("s");

    let mut store = open(&path, WriteOpenMode::MayExist).unwrap();
    store.write(10, b"hi").unwrap();

    let mut buf = [0u8; 12];
    store.read(0, &mut buf).unwrap();

    assert_eq!(&buf[..10], &[0u8; 10]);
    assert_eq!(&buf[10..12], b"hi");
}

#[test]
fn replay_reconstructs_state() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("s");

    {
        let mut store = open(&path, WriteOpenMode::MayExist).unwrap();
        store.write(0, b"abc").unwrap();
        store.write(5, b"xyz").unwrap();
    }

    let mut store = open(&path, WriteOpenMode::MustExist).unwrap();

    let mut buf = [0u8; 8];
    store.read(0, &mut buf).unwrap();

    assert_eq!(&buf, b"abc\0\0xyz");
}