Struct jopemachine_raft::Raft
source · 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: Arc<dyn Logger>) -> Result<Self>
pub fn new(c: &Config, store: T, logger: Arc<dyn 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.