sorock 0.12.0

A Multi-Raft implementation in Rust
Documentation
use super::*;

pub struct Effect {
    pub voter: Voter,
}
impl Effect {
    fn state_machine(&self) -> &Read<StateMachine> {
        &self.voter.state_machine
    }

    fn peers(&self) -> &Read<Peers> {
        &self.voter.peers
    }

    /// If the latest config doesn't contain itself, then it steps down
    /// by transferring the leadership to another node.
    pub async fn exec(self) -> Result<()> {
        ensure!(std::matches!(
            self.voter.read_election_state(),
            voter::ElectionState::Leader
        ));

        // Make sure the membership entry is truly committed
        // otherwise the configuration change entry may be lost.
        let last_membership_change_index = {
            let index = self
                .state_machine()
                .membership_pointer
                .load(Ordering::SeqCst);
            ensure!(index <= self.state_machine().commit_pointer.load(Ordering::SeqCst));
            index
        };

        let config = self
            .state_machine()
            .try_read_membership(last_membership_change_index)
            .await?
            .context(Error::BadLogState)?;
        ensure!(!config.contains(&self.voter.driver.self_node_id()));

        info!("step down");
        self.voter
            .write_election_state(voter::ElectionState::Follower);
        self.peers().transfer_leadership().await?;

        Ok(())
    }
}