Skip to main content

WalLog

Struct WalLog 

Source
pub struct WalLog { /* private fields */ }
Available on crate feature persistence only.
Expand description

A durable RaftLog whose entries and hard state survive a process restart.

Open it with open and hand it to RaftNode::with_log. Reads are served from an in-memory index; writes are appended to the underlying wal-db log and become durable when sync returns Ok.

§Examples

use raft_io::{LogEntry, RaftLog, WalLog};

let mut log = WalLog::open("raft.wal")?;
log.append(&[LogEntry::new(1, 1, b"set x = 1".to_vec())])?;
log.sync()?; // durable from here

// After a restart, reopening the same path recovers the entry.
let recovered = WalLog::open("raft.wal")?;
assert_eq!(recovered.last_index(), 1);

Implementations§

Source§

impl WalLog

Source

pub fn open(path: impl AsRef<Path>) -> Result<Self>

Opens the durable log at path, replaying any existing records to recover the log entries and hard state.

Creates the file if it does not exist. Recovery is exact: the recovered state is the logical result of every mutation that was appended before the process stopped.

§Errors

Returns Error::Storage if the WAL cannot be opened or a record fails to decode (for example, a checksum mismatch reported by wal-db).

Trait Implementations§

Source§

impl RaftLog for WalLog

Source§

fn last_index(&self) -> Index

Returns the index of the last entry, or 0 if the log is empty.
Source§

fn last_term(&self) -> Term

Returns the term of the last entry, or 0 if the log is empty.
Source§

fn term_at(&self, index: Index) -> Option<Term>

Returns the term of the entry at index. Read more
Source§

fn entry(&self, index: Index) -> Option<LogEntry>

Returns the entry at index, or None if there is none.
Source§

fn entries(&self, from: Index, to: Index) -> Vec<LogEntry>

Returns the entries in the inclusive index range [from, to]. Read more
Source§

fn append(&mut self, entries: &[LogEntry]) -> Result<()>

Appends entries to the end of the log. Read more
Source§

fn truncate(&mut self, from: Index) -> Result<()>

Removes every entry whose index is >= from. Read more
Source§

fn hard_state(&self) -> HardState

Returns the persisted HardState (current term and vote).
Source§

fn set_hard_state(&mut self, state: HardState) -> Result<()>

Persists state as the new HardState. Read more
Source§

fn sync(&mut self) -> Result<()>

Flushes all preceding writes to durable storage. Read more
Source§

fn snapshot_index(&self) -> Index

Returns the index the log has been compacted up to — the last index a snapshot includes — or 0 if there is no snapshot. Read more
Source§

fn snapshot(&self) -> Option<Snapshot>

Returns the current snapshot, if one exists. Read more
Source§

fn apply_snapshot(&mut self, snapshot: &Snapshot) -> Result<()>

Installs snapshot, replacing the prefix it subsumes. Read more

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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