pub struct Raft<T: Storage> {
pub msgs: Vec<Message>,
pub r: RaftCore<T>,
/* private fields */
}Expand description
A struct that represents the raft consensus itself. Stores details concerning the current and possible state the system can take.
Fields§
§msgs: Vec<Message>The list of messages.
r: RaftCore<T>Internal raftCore.
Implementations§
Source§impl<T: Storage> Raft<T>
impl<T: Storage> Raft<T>
Sourcepub fn new(c: &Config, store: T, logger: &Logger) -> Result<Self>
pub fn new(c: &Config, store: T, logger: &Logger) -> Result<Self>
Creates a new raft for use on the node.
Sourcepub fn set_priority(&mut self, priority: i64)
pub fn set_priority(&mut self, priority: i64)
Sets priority of node.
Sourcepub fn with_default_logger(c: &Config, store: T) -> Result<Self>
pub fn with_default_logger(c: &Config, store: T) -> Result<Self>
Creates a new raft for use on the node with the default logger.
The default logger is an slog to log adapter.
Sourcepub fn pending_read_count(&self) -> usize
pub fn pending_read_count(&self) -> usize
Returns the number of pending read-only messages.
Sourcepub fn ready_read_count(&self) -> usize
pub fn ready_read_count(&self) -> usize
Returns how many read states exist.
Sourcepub fn soft_state(&self) -> SoftState
pub fn soft_state(&self) -> SoftState
Returns a value representing the softstate at the time of calling.
Sourcepub fn hard_state(&self) -> HardState
pub fn hard_state(&self) -> HardState
Returns a value representing the hardstate at the time of calling.
Sourcepub fn election_timeout(&self) -> usize
pub fn election_timeout(&self) -> usize
Fetch the length of the election timeout.
Sourcepub fn heartbeat_timeout(&self) -> usize
pub fn heartbeat_timeout(&self) -> usize
Fetch the length of the heartbeat timeout
Sourcepub fn heartbeat_elapsed(&self) -> usize
pub fn heartbeat_elapsed(&self) -> usize
Fetch the number of ticks elapsed since last heartbeat.
Sourcepub fn randomized_election_timeout(&self) -> usize
pub fn randomized_election_timeout(&self) -> usize
Return the length of the current randomized election timeout.
Sourcepub fn skip_bcast_commit(&mut self, skip: bool)
pub fn skip_bcast_commit(&mut self, skip: bool)
Set whether skip broadcast empty commit messages at runtime.
Sourcepub fn set_batch_append(&mut self, batch_append: bool)
pub fn set_batch_append(&mut self, batch_append: bool)
Set whether batch append msg at runtime.
Sourcepub fn enable_group_commit(&mut self, enable: bool)
pub fn enable_group_commit(&mut self, enable: bool)
Configures group commit.
If group commit is enabled, only logs replicated to at least two different groups are committed.
You should use assign_commit_groups to configure peer groups.
Sourcepub fn group_commit(&self) -> bool
pub fn group_commit(&self) -> bool
Whether enable group commit.
Sourcepub fn assign_commit_groups(&mut self, ids: &[(u64, u64)])
pub fn assign_commit_groups(&mut self, ids: &[(u64, u64)])
Assigns groups to peers.
The tuple is (peer_id, group_id). group_id should be larger than 0.
The group information is only stored in memory. So you need to configure it every time a raft state machine is initialized or a snapshot is applied.
Sourcepub fn clear_commit_group(&mut self)
pub fn clear_commit_group(&mut self)
Removes all commit group configurations.
Sourcepub fn check_group_commit_consistent(&mut self) -> Option<bool>
pub fn check_group_commit_consistent(&mut self) -> Option<bool>
Checks whether the raft group is using group commit and consistent over group.
If it can’t get a correct answer, None is returned.
Sourcepub fn commit_to_current_term(&self) -> bool
pub fn commit_to_current_term(&self) -> bool
Checks if logs are committed to its term.
The check is useful usually when raft is leader.
Sourcepub fn apply_to_current_term(&self) -> bool
pub fn apply_to_current_term(&self) -> bool
Checks if logs are applied to current term.
Sourcepub fn set_max_committed_size_per_ready(&mut self, size: u64)
pub fn set_max_committed_size_per_ready(&mut self, size: u64)
Set max_committed_size_per_ready to size.
Sourcepub fn set_check_quorum(&mut self, check_quorum: bool)
pub fn set_check_quorum(&mut self, check_quorum: bool)
Set whether or not check_quorum.
Source§impl<T: Storage> Raft<T>
impl<T: Storage> Raft<T>
Sourcepub fn inflight_buffers_size(&self) -> usize
pub fn inflight_buffers_size(&self) -> usize
Get the inflight buffer size.
Sourcepub fn send_append(&mut self, to: u64)
pub fn send_append(&mut self, to: u64)
Sends an append RPC with new entries (if any) and the current commit index to the given peer.
Sourcepub fn bcast_append(&mut self)
pub fn bcast_append(&mut self)
Sends RPC, with entries to all peers that are not up-to-date according to the progress recorded in r.prs().
Sourcepub fn bcast_heartbeat(&mut self)
pub fn bcast_heartbeat(&mut self)
Sends RPC, without entries to all the peers.
Sourcepub fn maybe_commit(&mut self) -> bool
pub fn maybe_commit(&mut self) -> bool
Attempts to advance the commit index. Returns true if the commit index
changed (in which case the caller should call r.bcast_append).
Sourcepub fn commit_apply(&mut self, applied: u64)
pub fn commit_apply(&mut self, applied: u64)
Commit that the Raft peer has applied up to the given index.
Registers the new applied index to the Raft log.
§Hooks
- Post: Checks to see if it’s time to finalize a Joint Consensus state.
Sourcepub fn append_entry(&mut self, es: &mut [Entry]) -> bool
pub fn append_entry(&mut self, es: &mut [Entry]) -> bool
Appends a slice of entries to the log. The entries are updated to match the current index and term. Only called by leader currently
Sourcepub fn on_persist_entries(&mut self, index: u64, term: u64)
pub fn on_persist_entries(&mut self, index: u64, term: u64)
Notifies that these raft logs have been persisted.
Sourcepub fn on_persist_snap(&mut self, index: u64)
pub fn on_persist_snap(&mut self, index: u64)
Notifies that the snapshot have been persisted.
Sourcepub fn tick(&mut self) -> bool
pub fn tick(&mut self) -> bool
Returns true to indicate that there will probably be some readiness need to be handled.
Sourcepub fn tick_election(&mut self) -> bool
pub fn tick_election(&mut self) -> bool
Run by followers and candidates after self.election_timeout.
Returns true to indicate that there will probably be some readiness need to be handled.
Sourcepub fn become_follower(&mut self, term: u64, leader_id: u64)
pub fn become_follower(&mut self, term: u64, leader_id: u64)
Converts this node to a follower.
Sourcepub fn become_candidate(&mut self)
pub fn become_candidate(&mut self)
Sourcepub fn become_pre_candidate(&mut self)
pub fn become_pre_candidate(&mut self)
Sourcepub fn become_leader(&mut self)
pub fn become_leader(&mut self)
Sourcepub fn step(&mut self, m: Message) -> Result<()>
pub fn step(&mut self, m: Message) -> Result<()>
Steps the raft along via a message. This should be called everytime your raft receives a message from a peer.
Sourcepub fn request_snapshot(&mut self) -> Result<()>
pub fn request_snapshot(&mut self) -> Result<()>
Request a snapshot from a leader.
Sourcepub fn handle_append_entries(&mut self, m: &Message)
pub fn handle_append_entries(&mut self, m: &Message)
For a given message, append the entries to the log.
Sourcepub fn handle_heartbeat(&mut self, m: Message)
pub fn handle_heartbeat(&mut self, m: Message)
For a message, commit and send out heartbeat.
Sourcepub fn restore(&mut self, snap: Snapshot) -> bool
pub fn restore(&mut self, snap: Snapshot) -> bool
Recovers the state machine from a snapshot. It restores the log and the configuration of state machine.
Sourcepub fn post_conf_change(&mut self) -> ConfState
pub fn post_conf_change(&mut self) -> ConfState
Updates the in-memory state and, when necessary, carries out additional actions such as reacting to the removal of nodes or changed quorum requirements.
Sourcepub fn has_pending_conf(&self) -> bool
pub fn has_pending_conf(&self) -> bool
Check if there is any pending confchange.
This method can be false positive.
Sourcepub fn should_bcast_commit(&self) -> bool
pub fn should_bcast_commit(&self) -> bool
Specifies if the commit should be broadcast.
Sourcepub fn promotable(&self) -> bool
pub fn promotable(&self) -> bool
Indicates whether state machine can be promoted to leader, which is true when it’s a voter and its own id is in progress list.
Sourcepub fn prs(&self) -> &ProgressTracker
pub fn prs(&self) -> &ProgressTracker
Returns a read-only reference to the progress set.
Sourcepub fn mut_prs(&mut self) -> &mut ProgressTracker
pub fn mut_prs(&mut self) -> &mut ProgressTracker
Returns a mutable reference to the progress set.
Sourcepub fn load_state(&mut self, hs: &HardState)
pub fn load_state(&mut self, hs: &HardState)
For a given hardstate, load the state into self.
Sourcepub fn pass_election_timeout(&self) -> bool
pub fn pass_election_timeout(&self) -> bool
pass_election_timeout returns true iff election_elapsed is greater
than or equal to the randomized election timeout in
[election_timeout, 2 * election_timeout - 1].
Sourcepub fn reset_randomized_election_timeout(&mut self)
pub fn reset_randomized_election_timeout(&mut self)
Regenerates and stores the election timeout.
Sourcepub fn send_timeout_now(&mut self, to: u64)
pub fn send_timeout_now(&mut self, to: u64)
Issues a message to timeout immediately.
Sourcepub fn abort_leader_transfer(&mut self)
pub fn abort_leader_transfer(&mut self)
Stops the transfer of a leader.
Sourcepub fn reduce_uncommitted_size(&mut self, ents: &[Entry])
pub fn reduce_uncommitted_size(&mut self, ents: &[Entry])
Reduce size of ‘ents’ from uncommitted size.
Sourcepub fn maybe_increase_uncommitted_size(&mut self, ents: &[Entry]) -> bool
pub fn maybe_increase_uncommitted_size(&mut self, ents: &[Entry]) -> bool
Increase size of ‘ents’ to uncommitted size. Return true when size limit is satisfied. Otherwise return false and uncommitted size remains unchanged. For raft with no limit(or non-leader raft), it always return true.
Sourcepub fn uncommitted_size(&self) -> usize
pub fn uncommitted_size(&self) -> usize
Return current uncommitted size recorded by uncommitted_state
Sourcepub fn maybe_free_inflight_buffers(&mut self)
pub fn maybe_free_inflight_buffers(&mut self)
A Raft leader allocates a vector with capacity max_inflight_msgs for every peer.
It takes a lot of memory if there are too many Raft groups. maybe_free_inflight_buffers
is used to free memory if necessary.
Sourcepub fn adjust_max_inflight_msgs(&mut self, target: u64, cap: usize)
pub fn adjust_max_inflight_msgs(&mut self, target: u64, cap: usize)
To adjust max_inflight_msgs for the specified peer.
Set to 0 will disable the progress.