Struct jopemachine_raft::RaftLog
source · 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>
impl<T: Storage> RaftLog<T>
sourcepub fn new(store: T, logger: Arc<dyn Logger>) -> RaftLog<T>
pub fn new(store: T, logger: Arc<dyn Logger>) -> RaftLog<T>
Creates a new raft log with a given storage and tag.
sourcepub fn last_term(&self) -> u64
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.
sourcepub fn term(&self, idx: u64) -> Result<u64>
pub fn term(&self, idx: u64) -> Result<u64>
For a given index, finds the term associated with it.
sourcepub fn first_index(&self) -> u64
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.
sourcepub fn last_index(&self) -> u64
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.
sourcepub fn find_conflict(&self, ents: &[Entry]) -> u64
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.
sourcepub fn find_conflict_by_term(&self, index: u64, term: u64) -> (u64, Option<u64>)
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)
sourcepub fn match_term(&self, idx: u64, term: u64) -> bool
pub fn match_term(&self, idx: u64, term: u64) -> bool
Answers the question: Does this index belong to this term?
sourcepub fn maybe_append(
&mut self,
idx: u64,
term: u64,
committed: u64,
ents: &[Entry]
) -> Option<(u64, u64)>
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.
sourcepub fn commit_to(&mut self, to_commit: u64)
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.
sourcepub 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.
pub fn applied_to(&mut self, idx: u64)
Advance the applied index to the passed in value.
§Panics
Panics if the value passed in is not new or known.
sourcepub fn stable_entries(&mut self, index: u64, term: u64)
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.
sourcepub fn stable_snap(&mut self, index: u64)
pub fn stable_snap(&mut self, index: u64)
Clears the unstable snapshot.
sourcepub fn unstable_entries(&self) -> &[Entry]
pub fn unstable_entries(&self) -> &[Entry]
Returns slice of entries that are not persisted.
sourcepub fn unstable_snapshot(&self) -> &Option<Snapshot>
pub fn unstable_snapshot(&self) -> &Option<Snapshot>
Returns the snapshot that are not persisted.
sourcepub fn entries(
&self,
idx: u64,
max_size: impl Into<Option<u64>>,
context: GetEntriesContext
) -> Result<Vec<Entry>>
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.
sourcepub fn is_up_to_date(&self, last_index: u64, term: u64) -> bool
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.
sourcepub fn next_entries_since(
&self,
since_idx: u64,
max_size: Option<u64>
) -> Option<Vec<Entry>>
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).
sourcepub fn next_entries(&self, max_size: Option<u64>) -> Option<Vec<Entry>>
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.
sourcepub fn has_next_entries_since(&self, since_idx: u64) -> bool
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).
sourcepub fn has_next_entries(&self) -> bool
pub fn has_next_entries(&self) -> bool
Returns whether there are new entries.
sourcepub fn snapshot(&self, request_index: u64, to: u64) -> Result<Snapshot>
pub fn snapshot(&self, request_index: u64, to: u64) -> Result<Snapshot>
Returns the current snapshot
sourcepub fn maybe_commit(&mut self, max_index: u64, term: u64) -> bool
pub fn maybe_commit(&mut self, max_index: u64, term: u64) -> bool
Attempts to commit the index and term and returns whether it did.
sourcepub fn maybe_persist(&mut self, index: u64, term: u64) -> bool
pub fn maybe_persist(&mut self, index: u64, term: u64) -> bool
Attempts to persist the index and term and returns whether it did.
sourcepub fn maybe_persist_snap(&mut self, index: u64) -> bool
pub fn maybe_persist_snap(&mut self, index: u64) -> bool
Attempts to persist the snapshot and returns whether it did.
sourcepub fn slice(
&self,
low: u64,
high: u64,
max_size: impl Into<Option<u64>>,
context: GetEntriesContext
) -> Result<Vec<Entry>>
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.
sourcepub fn commit_info(&self) -> (u64, u64)
pub fn commit_info(&self) -> (u64, u64)
Returns the committed index and its term.