Skip to main content

RaftLog

Struct RaftLog 

Source
pub struct RaftLog<T: Types> { /* private fields */ }
Expand description

RaftLog is a Write-Ahead-Log implementation for the Raft consensus protocol.

It provides persistent storage for Raft log entries and state, with the following features:

  • Append-only log storage with chunk-based organization
  • In-memory caching of log payloads
  • Exclusive file locking for thread-safe operations
  • Support for log truncation and purging
  • Statistics tracking for monitoring

Implementations§

Source§

impl<T: Types> RaftLog<T>

Source

pub fn dump_data(&self) -> DumpRaftLog<T>

Dump the RaftLog data for debugging purposes.

Returns a DumpRaftLog struct containing a complete snapshot of the RaftLog state.

Source

pub fn dump(&self) -> RefDump<'_, T>

Dump the WAL data in this Raft-log for debugging purposes.

This method returns a reference type RefDump containing the RaftLog configuration and the RaftLog instance itself.

Source

pub fn config(&self) -> &Config

Get a reference to the RaftLog configuration.

Source

pub fn open(config: Arc<Config>) -> Result<Self, Error>

Opens a RaftLog at the specified directory.

This operation:

  1. Acquires an exclusive lock on the directory
  2. Loads existing chunks in order
  3. Replays WAL records to rebuild the state
  4. Creates a new open chunk for future writes
§Errors

Returns an error if:

  • Directory operations fail
  • There are gaps between chunk offsets
  • WAL records are invalid
Source

pub fn load_chunk_ids(config: &Config) -> Result<Vec<ChunkId>, Error>

Source

pub fn update_state(&mut self, state: RaftLogState<T>) -> Result<Segment, Error>

Update the RaftLog state.

This method updates the RaftLog state with a new state and appends it to the WAL.

Source

pub fn read( &self, from: u64, to: u64, ) -> impl Iterator<Item = Result<(T::LogId, T::LogPayload), Error>> + '_

Reads log entries in the specified index range.

Returns an iterator over log entries, attempting to serve them from cache first, falling back to disk reads if necessary.

Source

pub fn log_state(&self) -> &RaftLogState<T>

Get a reference to the latest RaftLog state.

The state is the latest state of the RaftLog, even if the corresponding WAL record is not committed yet.

Source

pub fn stat(&self) -> Stat<T>

Get a reference to the RaftLog statistics.

This method returns a Stat struct containing statistics about the RaftLog, including:

  • The number of closed chunks
  • The open chunk statistics
Source

pub fn access_stat(&self) -> &AccessStat

Get a reference to the access statistics.

This method returns a reference to the AccessStat struct, which contains statistics about the access patterns of the RaftLog.

Source

pub fn wait_worker_idle(&self)

Block until the FlushWorker has processed all queued requests.

After this returns, all file writes, syncs, and cache eviction boundary updates issued before this call are complete. Note that the payload cache item count may still be non-deterministic because eviction is lazy; call drain_cache_evictable() afterwards to normalize it.

Source

pub fn drain_cache_evictable(&self)

Drain all evictable entries from the payload cache.

Call after wait_worker_idle() to normalize the cache to a deterministic state. See PayloadCache::drain_evictable for details.

Source

pub fn on_disk_size(&self) -> u64

Returns the current size of the log on disk in bytes.

This includes all closed chunks and the open chunk, measuring from the start of the earliest chunk to the end of the open chunk.

Trait Implementations§

Source§

impl<T: Debug + Types> Debug for RaftLog<T>

Source§

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

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

impl<T: Types> RaftLogWriter<T> for RaftLog<T>

Source§

fn truncate(&mut self, index: u64) -> Result<Segment, Error>

Truncate at index, keep the record before index.

Source§

fn save_user_data( &mut self, user_data: Option<T::UserData>, ) -> Result<Segment, Error>

Update the user data of the Raft log. Read more
Source§

fn save_vote(&mut self, vote: T::Vote) -> Result<Segment, Error>

Save the vote information. Read more
Source§

fn append<I>(&mut self, entries: I) -> Result<Segment, Error>
where I: IntoIterator<Item = (T::LogId, T::LogPayload)>,

Append a batch of entries to the log. Read more
Source§

fn purge(&mut self, upto: T::LogId) -> Result<Segment, Error>

Purge log entries up to and including the given log id. Read more
Source§

fn commit(&mut self, log_id: T::LogId) -> Result<Segment, Error>

Update the committed log id. Read more
Source§

fn flush(&mut self, callback: T::Callback) -> Result<(), Error>

Initiate an asynchronous flush operation to persist all written data. Read more

Auto Trait Implementations§

§

impl<T> Freeze for RaftLog<T>
where <T as Types>::Vote: Freeze, <T as Types>::LogId: Freeze, <T as Types>::UserData: Freeze,

§

impl<T> RefUnwindSafe for RaftLog<T>

§

impl<T> Send for RaftLog<T>
where <T as Types>::Vote: Send, <T as Types>::UserData: Send, T: Send,

§

impl<T> Sync for RaftLog<T>
where <T as Types>::Vote: Sync, <T as Types>::UserData: Sync, T: Sync,

§

impl<T> Unpin for RaftLog<T>
where <T as Types>::Vote: Unpin, <T as Types>::LogId: Unpin, <T as Types>::UserData: Unpin, T: Unpin,

§

impl<T> UnwindSafe for RaftLog<T>

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.