#[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};
// Three records, then damage in the second one's payload.
let wal = Wal::open(&path)?;
wal.append(b"first")?;
let second = wal.append(b"second")?;
wal.append(b"third")?;
wal.sync()?;
drop(wal);
let mut bytes = std::fs::read(&path).map_err(WalError::from)?;
bytes[second.get() as usize + 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: 13, .. })));
assert_eq!(std::fs::read(&path).map_err(WalError::from)?, bytes);
// The default cuts the log at the damage, intact third record included.
let wal = Wal::open(&path)?;
assert_eq!(wal.truncated_on_open(), 27);
assert_eq!(wal.iter()?.count(), 1);Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
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.