Struct 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>

source

pub fn new(c: &Config, store: T, logger: &Logger) -> Result<Self>

Creates a new raft for use on the node.

source

pub fn set_priority(&mut self, priority: i64)

Sets priority of node.

source

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.

source

pub fn store(&self) -> &T

Grabs an immutable reference to the store.

source

pub fn mut_store(&mut self) -> &mut T

Grabs a mutable reference to the store.

source

pub fn snap(&self) -> Option<&Snapshot>

Grabs a reference to the snapshot

source

pub fn pending_read_count(&self) -> usize

Returns the number of pending read-only messages.

source

pub fn ready_read_count(&self) -> usize

Returns how many read states exist.

source

pub fn soft_state(&self) -> SoftState

Returns a value representing the softstate at the time of calling.

source

pub fn hard_state(&self) -> HardState

Returns a value representing the hardstate at the time of calling.

source

pub fn in_lease(&self) -> bool

Returns whether the current raft is in lease.

source

pub fn election_timeout(&self) -> usize

Fetch the length of the election timeout.

source

pub fn heartbeat_timeout(&self) -> usize

Fetch the length of the heartbeat timeout

source

pub fn heartbeat_elapsed(&self) -> usize

Fetch the number of ticks elapsed since last heartbeat.

source

pub fn randomized_election_timeout(&self) -> usize

Return the length of the current randomized election timeout.

source

pub fn skip_bcast_commit(&mut self, skip: bool)

Set whether skip broadcast empty commit messages at runtime.

source

pub fn set_batch_append(&mut self, batch_append: bool)

Set whether batch append msg at runtime.

source

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.

source

pub fn group_commit(&self) -> bool

Whether enable group commit.

source

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.

source

pub fn clear_commit_group(&mut self)

Removes all commit group configurations.

source

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.

source

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.

source

pub fn apply_to_current_term(&self) -> bool

Checks if logs are applied to current term.

source

pub fn set_max_committed_size_per_ready(&mut self, size: u64)

Set max_committed_size_per_ready to size.

source

pub fn set_check_quorum(&mut self, check_quorum: bool)

Set whether or not check_quorum.

source§

impl<T: Storage> Raft<T>

source

pub fn inflight_buffers_size(&self) -> usize

Get the inflight buffer size.

source

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.

source

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().

source

pub fn ping(&mut self)

Broadcasts heartbeats to all the followers if it’s leader.

source

pub fn bcast_heartbeat(&mut self)

Sends RPC, without entries to all the peers.

source

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).

source

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.
source

pub fn reset(&mut self, term: u64)

Resets the current node to a given term.

source

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

source

pub fn on_persist_entries(&mut self, index: u64, term: u64)

Notifies that these raft logs have been persisted.

source

pub fn on_persist_snap(&mut self, index: u64)

Notifies that the snapshot have been persisted.

source

pub fn tick(&mut self) -> bool

Returns true to indicate that there will probably be some readiness need to be handled.

source

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.

source

pub fn become_follower(&mut self, term: u64, leader_id: u64)

Converts this node to a follower.

source

pub fn become_candidate(&mut self)

Converts this node to a candidate

Panics

Panics if a leader already exists.

source

pub fn become_pre_candidate(&mut self)

Converts this node to a pre-candidate

Panics

Panics if a leader already exists.

source

pub fn become_leader(&mut self)

Makes this raft the leader.

Panics

Panics if this is a follower node.

source

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.

source

pub fn request_snapshot(&mut self) -> Result<()>

Request a snapshot from a leader.

source

pub fn handle_append_entries(&mut self, m: &Message)

For a given message, append the entries to the log.

source

pub fn handle_heartbeat(&mut self, m: Message)

For a message, commit and send out heartbeat.

source

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.

source

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.

source

pub fn has_pending_conf(&self) -> bool

Check if there is any pending confchange.

This method can be false positive.

source

pub fn should_bcast_commit(&self) -> bool

Specifies if the commit should be broadcast.

source

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.

source

pub fn prs(&self) -> &ProgressTracker

Returns a read-only reference to the progress set.

source

pub fn mut_prs(&mut self) -> &mut ProgressTracker

Returns a mutable reference to the progress set.

source

pub fn load_state(&mut self, hs: &HardState)

For a given hardstate, load the state into self.

source

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].

source

pub fn reset_randomized_election_timeout(&mut self)

Regenerates and stores the election timeout.

source

pub fn send_timeout_now(&mut self, to: u64)

Issues a message to timeout immediately.

source

pub fn abort_leader_transfer(&mut self)

Stops the transfer of a leader.

source

pub fn reduce_uncommitted_size(&mut self, ents: &[Entry])

Reduce size of ‘ents’ from uncommitted size.

source

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.

source

pub fn uncommitted_size(&self) -> usize

Return current uncommitted size recorded by uncommitted_state

source

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.

source

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.

Trait Implementations§

source§

impl<T: Storage> Deref for Raft<T>

§

type Target = RaftCore<T>

The resulting type after dereferencing.
source§

fn deref(&self) -> &RaftCore<T>

Dereferences the value.
source§

impl<T: Storage> DerefMut for Raft<T>

source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for Raft<T>where T: RefUnwindSafe,

§

impl<T> Send for Raft<T>where T: Send,

§

impl<T> Sync for Raft<T>where T: Sync,

§

impl<T> Unpin for Raft<T>where T: Unpin,

§

impl<T> UnwindSafe for Raft<T>where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> SendSyncUnwindSafe for Twhere T: Send + Sync + UnwindSafe + ?Sized,