pub struct RaftLog<T: Storage> {
    pub store: T,
    pub unstable: Unstable,
    pub committed: u64,
    pub persisted: u64,
    pub applied: u64,
}
Expand description

Raft log implementation

Fields§

§store: T

Contains all stable entries since the last snapshot.

§unstable: Unstable

Contains all unstable entries and snapshot. they will be saved into storage.

§committed: u64

The highest log position that is known to be in stable storage on a quorum of nodes.

Invariant: applied <= committed

§persisted: u64

The highest log position that is known to be persisted in stable storage. It’s used for limiting the upper bound of committed and persisted entries.

Invariant: persisted < unstable.offset && applied <= persisted

§applied: u64

The highest log position that the application has been instructed to apply to its state machine.

Invariant: applied <= min(committed, persisted)

Implementations§

source§

impl<T: Storage> RaftLog<T>

source

pub fn new(store: T, logger: Arc<dyn Logger>) -> RaftLog<T>

Creates a new raft log with a given storage and tag.

source

pub fn last_term(&self) -> u64

Grabs the term from the last entry.

§Panics

Panics if there are entries but the last term has been discarded.

source

pub fn store(&self) -> &T

Grab a read-only reference to the underlying storage.

source

pub fn mut_store(&mut self) -> &mut T

Grab a mutable reference to the underlying storage.

source

pub fn term(&self, idx: u64) -> Result<u64>

For a given index, finds the term associated with it.

source

pub fn first_index(&self) -> u64

Returns th first index in the store that is available via entries

§Panics

Panics if the store doesn’t have a first index.

source

pub fn last_index(&self) -> u64

Returns the last index in the store that is available via entries.

§Panics

Panics if the store doesn’t have a last index.

source

pub fn find_conflict(&self, ents: &[Entry]) -> u64

Finds the index of the conflict.

It returns the first index of conflicting entries between the existing entries and the given entries, if there are any.

If there are no conflicting entries, and the existing entries contain all the given entries, zero will be returned.

If there are no conflicting entries, but the given entries contains new entries, the index of the first new entry will be returned.

An entry is considered to be conflicting if it has the same index but a different term.

The first entry MUST have an index equal to the argument ‘from’. The index of the given entries MUST be continuously increasing.

source

pub fn find_conflict_by_term(&self, index: u64, term: u64) -> (u64, Option<u64>)

find_conflict_by_term takes an (index, term) pair (indicating a conflicting log entry on a leader/follower during an append) and finds the largest index in log with log.term <= term and log.index <= index. If no such index exists in the log, the log’s first index is returned.

The index provided MUST be equal to or less than self.last_index(). Invalid inputs log a warning and the input index is returned.

Return (index, term)

source

pub fn match_term(&self, idx: u64, term: u64) -> bool

Answers the question: Does this index belong to this term?

source

pub fn maybe_append( &mut self, idx: u64, term: u64, committed: u64, ents: &[Entry] ) -> Option<(u64, u64)>

Returns None if the entries cannot be appended. Otherwise, it returns Some((conflict_index, last_index)).

§Panics

Panics if it finds a conflicting index less than committed index.

source

pub fn commit_to(&mut self, to_commit: u64)

Sets the last committed value to the passed in value.

§Panics

Panics if the index goes past the last index.

source

pub fn applied_to(&mut self, idx: u64)

👎Deprecated: Call raft::commit_apply(idx) instead. Joint Consensus requires an on-apply hook to finalize a configuration change. This will become internal API in future versions.

Advance the applied index to the passed in value.

§Panics

Panics if the value passed in is not new or known.

source

pub fn applied(&self) -> u64

Returns the last applied index.

source

pub fn stable_entries(&mut self, index: u64, term: u64)

Clears the unstable entries and moves the stable offset up to the last index, if there is any.

source

pub fn stable_snap(&mut self, index: u64)

Clears the unstable snapshot.

source

pub fn unstable(&self) -> &Unstable

Returns a reference to the unstable log.

source

pub fn unstable_entries(&self) -> &[Entry]

Returns slice of entries that are not persisted.

source

pub fn unstable_snapshot(&self) -> &Option<Snapshot>

Returns the snapshot that are not persisted.

source

pub fn append(&mut self, ents: &[Entry]) -> u64

Appends a set of entries to the unstable list.

source

pub fn entries( &self, idx: u64, max_size: impl Into<Option<u64>>, context: GetEntriesContext ) -> Result<Vec<Entry>>

Returns entries starting from a particular index and not exceeding a bytesize.

source

pub fn is_up_to_date(&self, last_index: u64, term: u64) -> bool

Determines if the given (lastIndex,term) log is more up-to-date by comparing the index and term of the last entry in the existing logs. If the logs have last entry with different terms, then the log with the later term is more up-to-date. If the logs end with the same term, then whichever log has the larger last_index is more up-to-date. If the logs are the same, the given log is up-to-date.

source

pub fn next_entries_since( &self, since_idx: u64, max_size: Option<u64> ) -> Option<Vec<Entry>>

Returns committed and persisted entries since max(since_idx + 1, first_index).

source

pub fn next_entries(&self, max_size: Option<u64>) -> Option<Vec<Entry>>

Returns all the available entries for execution. If applied is smaller than the index of snapshot, it returns all committed entries after the index of snapshot.

source

pub fn has_next_entries_since(&self, since_idx: u64) -> bool

Returns whether there are committed and persisted entries since max(since_idx + 1, first_index).

source

pub fn has_next_entries(&self) -> bool

Returns whether there are new entries.

source

pub fn snapshot(&self, request_index: u64, to: u64) -> Result<Snapshot>

Returns the current snapshot

source

pub fn maybe_commit(&mut self, max_index: u64, term: u64) -> bool

Attempts to commit the index and term and returns whether it did.

source

pub fn maybe_persist(&mut self, index: u64, term: u64) -> bool

Attempts to persist the index and term and returns whether it did.

source

pub fn maybe_persist_snap(&mut self, index: u64) -> bool

Attempts to persist the snapshot and returns whether it did.

source

pub fn slice( &self, low: u64, high: u64, max_size: impl Into<Option<u64>>, context: GetEntriesContext ) -> Result<Vec<Entry>>

Grabs a slice of entries from the raft. Unlike a rust slice pointer, these are returned by value. The result is truncated to the max_size in bytes.

source

pub fn restore(&mut self, snapshot: Snapshot)

Restores the current log from a snapshot.

source

pub fn commit_info(&self) -> (u64, u64)

Returns the committed index and its term.

Trait Implementations§

source§

impl<T> ToString for RaftLog<T>
where T: Storage,

source§

fn to_string(&self) -> String

Converts the given value to a String. Read more

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for RaftLog<T>

§

impl<T> Send for RaftLog<T>
where T: Send,

§

impl<T> Sync for RaftLog<T>
where T: Sync,

§

impl<T> Unpin for RaftLog<T>
where 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>,

§

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>,

§

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<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V