Skip to main content

OpenPolicy

Enum OpenPolicy 

Source
#[non_exhaustive]
pub enum OpenPolicy { TruncateTail, Strict, }
Expand description

What opening a log does when its scan stops before the end of the store.

Opening a log scans it from the head and stops at the first record that is incomplete or fails its checksum. Nothing from that point on is part of the recovered log. The policy decides whether those bytes may be cut.

The common cause is a torn tail: a crash partway through an append, leaving a final record that was never acknowledged durable. Cutting it loses nothing the caller was promised. The other cause is damage in the middle of the log (bit rot, a bad sector, a missing segment file), where the records after the damage are intact and were acknowledged. Cutting those loses committed data. TruncateTail cuts in both cases; Strict cuts only in the first.

Set it with WalConfig::with_open_policy. Under either policy, Wal::truncated_on_open reports how many bytes were cut.

§Examples

use wal_db::{OpenPolicy, Wal, WalConfig, WalError};

// Two records, then damage in the first one's payload.
let wal = Wal::open(&path)?;
wal.append(b"first")?;
wal.append(b"second")?;
wal.sync()?;
drop(wal);
let mut bytes = std::fs::read(&path).map_err(WalError::from)?;
bytes[8] ^= 0xFF;
std::fs::write(&path, &bytes).map_err(WalError::from)?;

// Strict open refuses, because an intact record follows the damage, and
// leaves the file as it was.
let strict = WalConfig::new().with_open_policy(OpenPolicy::Strict);
let refused = Wal::open_with(&path, strict);
assert!(matches!(refused, Err(WalError::Corruption { offset: 0, .. })));
assert_eq!(std::fs::read(&path).map_err(WalError::from)?, bytes);

// The default cuts the log at the damage, intact second record included.
let wal = Wal::open(&path)?;
assert_eq!(wal.truncated_on_open(), 27);
assert!(wal.is_empty());

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

TruncateTail

Cut everything from the first damaged record to the end, then open.

The default, and the behaviour of every 1.0 release. A torn tail is cut and the log opens with a clean append boundary. Damage in the middle of the log is cut the same way, together with every intact record after it.

§

Strict

Cut a damaged final record, but refuse to open if intact data follows the damage.

When the scan stops at a damaged record, every byte offset after it is checked for the start of an intact record: a full header, a length within WalConfig::max_record_size, the whole payload present, and a matching checksum. Every offset is tried because the damage may be in the length field, which leaves the next record’s position unknown. If none is found, the damage is a torn tail and is cut as under TruncateTail. If one is found, or the store cannot read back bytes it reports holding (a missing segment file, say), opening fails with WalError::Corruption at the damaged record’s offset and the store is not modified.

A record rejected only because its length is above WalConfig::max_record_size (a log written with a larger limit) is also refused when the rest of it is intact, last record or not, rather than cut as a torn tail. Checking it costs one read of that record.

§When it refuses a log that was only torn

The check cannot tell damage from a crash that left a later record complete behind an incomplete earlier one. That happens when several appends are in flight together and the process stops before they all finish: concurrent appenders, or several appends before one sync with write-back persisting them out of order. None of those records was acknowledged durable, but strict open refuses anyway. It is exact for a log written one record at a time, each made durable (append_and_sync, or append then sync) before the next is appended. Two rarer cases also refuse: a torn record whose payload itself contains a framed record, and random bytes matching a checksum by chance (about one in 2^32 per offset whose length field is plausible). Every refusal fails closed: nothing is cut, and reopening with TruncateTail after inspection cuts as before.

§Cost

Nothing on a log that opens clean. When there is damage, one pass over the bytes after it: a few nanoseconds per byte, plus a checksum for each offset whose length field is plausible. A candidate that fits in the 64 KiB read window is checksummed from it; a longer one is checked from prefix checksums kept every 4 KiB, so its cost does not grow with its length. Memory: the window, plus 4 bytes per 4 KiB scanned.

Trait Implementations§

Source§

impl Clone for OpenPolicy

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for OpenPolicy

Source§

impl Debug for OpenPolicy

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for OpenPolicy

Source§

impl PartialEq for OpenPolicy

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for OpenPolicy

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<E> WithErrorCode<E> for E

Source§

fn with_code(self, code: impl Into<String>) -> CodedError<E>

Attach an error code to an error