Trait raft::storage::Storage

source ·
pub trait Storage {
    // Required methods
    fn initial_state(&self) -> Result<RaftState>;
    fn entries(
        &self,
        low: u64,
        high: u64,
        max_size: impl Into<Option<u64>>,
        context: GetEntriesContext
    ) -> 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, request_index: u64, to: u64) -> Result<Snapshot>;
}
Expand description

Storage saves all the information about the current Raft implementation, including Raft Log, commit index, the leader to vote for, etc.

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

Required Methods§

source

fn initial_state(&self) -> Result<RaftState>

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

RaftState could be initialized or not. If it’s initialized it means the Storage is created with a configuration, and its last index and term should be greater than 0.

source

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

Returns a slice of log entries in the range [low, high). max_size limits the total size of the log entries returned if not None, however the slice of entries returned will always have length at least 1 if entries are found in the range.

Entries are supported to be fetched asynchorously depending on the context. Async is optional. Storage should check context.can_async() first and decide whether to fetch entries asynchorously based on its own implementation. If the entries are fetched asynchorously, storage should return LogTemporarilyUnavailable, and application needs to call on_entries_fetched(context) to trigger re-fetch of the entries after the storage finishes fetching the entries.

Panics

Panics if high is higher than Storage::last_index(&self) + 1.

source

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

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.

source

fn first_index(&self) -> Result<u64>

Returns the index of the first log entry that is possible available via entries, which will always equal to truncated index plus 1.

New created (but not initialized) Storage can be considered as truncated at 0 so that 1 will be returned in this case.

source

fn last_index(&self) -> Result<u64>

The index of the last entry replicated in the Storage.

source

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

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. A snapshot’s index must not less than the request_index. to indicates which peer is requesting the snapshot.

Implementors§