Trait raft::storage::Storage[][src]

pub trait Storage {
    fn initial_state(&self) -> Result<RaftState>;
fn entries(&self, low: u64, high: u64, max_size: u64) -> Result<Vec<Entry>>;
fn term(&self, idx: u64) -> Result<u64>;
fn first_index(&self) -> Result<u64>;
fn last_index(&self) -> Result<u64>;
fn snapshot(&self) -> Result<Snapshot>; }

Storage saves all the information about the current Raft implementation, including Raft Log, commit index, the leader to vote for, etc. Pay attention to what is returned when there is no Log but it needs to get the term at index first_index() - 1. To solve this, you can use a dummy Log entry to keep the last truncated Log entry. See entries: vec![Entry::new()] as a reference.

If any Storage method returns an error, the raft instance will become inoperable and refuse to paticipate in elections; the application is responsible for cleanup and recovery in this case.

Required Methods

initial_state is called when Raft is initialized. This interface will return a RaftState which contains HardState and ConfState;

Returns a slice of log entries in the range [low, high). max_size limits the total size of the log entries returned, but entries returns at least one entry if any.

Returns the term of entry idx, which must be in the range [first_index()-1, last_index()]. The term of the entry before first_index is retained for matching purpose even though the rest of that entry may not be available.

Returns the index of the first log entry that is possible available via entries (older entries have been incorporated into the latest snapshot; if storage only contains the dummy entry the first log entry is not available).

The index of the last entry in the log.

Returns the most recent snapshot.

If snapshot is temporarily unavailable, it should return SnapshotTemporarilyUnavailable, so raft state machine could know that Storage needs some time to prepare snapshot and call snapshot later.

Implementors