openraft 0.10.2

Advanced Raft consensus
Documentation
use std::collections::VecDeque;

use crate::RaftTypeConfig;
use crate::config::Config;
use crate::engine::Command;
use crate::engine::Condition;
use crate::engine::command_scheduler::CommandScheduler;
use crate::engine::pending_responds::PendingResponds;
use crate::engine::respond_command::PendingRespond;
use crate::storage::RaftStateMachine;

/// The entry of output from Engine to the runtime.
#[derive(Debug)]
pub(crate) struct EngineOutput<C, SM = ()>
where
    C: RaftTypeConfig,
    SM: RaftStateMachine<C>,
{
    /// Command queue that needs to be executed by `RaftRuntime`.
    pub(crate) commands: VecDeque<Command<C, SM>>,

    /// Pending responds waiting for IO conditions to be met before sending.
    pub(crate) pending_responds: PendingResponds<C>,
}

impl<C, SM> Default for EngineOutput<C, SM>
where
    C: RaftTypeConfig,
    SM: RaftStateMachine<C>,
{
    /// Many Raft groups in one process each hold these queues, so every queue starts without a
    /// buffer. A queue keeps the capacity it grows to.
    fn default() -> Self {
        Self {
            commands: VecDeque::new(),
            pending_responds: PendingResponds::default(),
        }
    }
}

impl<C, SM> EngineOutput<C, SM>
where
    C: RaftTypeConfig,
    SM: RaftStateMachine<C>,
{
    pub(crate) fn len(&self) -> usize {
        self.commands.len()
    }

    /// Push a command to the queue.
    pub(crate) fn push_command(&mut self, cmd: Command<C, SM>) {
        tracing::debug!("push command: {:?}", cmd);
        self.commands.push_back(cmd)
    }

    pub(crate) fn prepend_command(&mut self, cmd: Command<C, SM>) {
        tracing::debug!("push front command: {:?}", cmd);
        self.commands.push_front(cmd)
    }

    /// Put the command to the head of the queue or to a separate pending queue.
    ///
    /// This will be used when the command is not ready to be executed.
    ///
    /// Returns Ok if the cmd is put to a pending queue, means it is not put back, and other
    /// commands in the main queue can still be processed.
    pub(crate) fn postpone_command(&mut self, cmd: Command<C, SM>) -> Result<(), &'static str> {
        tracing::debug!("postpone command: {:?}", cmd);

        // For Respond command, put them to separate queue in order not to block other commands.
        // For other commands, put them back to the front of the command queue.
        match cmd {
            Command::Respond { when, resp } => {
                let pending_responds = &mut self.pending_responds;
                match when {
                    None => {
                        unreachable!("Respond command to postpone must have a condition");
                    }
                    Some(Condition::IOFlushed { io_id }) => {
                        pending_responds.on_log_io.push_back(PendingRespond::new(io_id, resp));
                    }
                    Some(Condition::LogFlushed { log_id }) => {
                        pending_responds.on_log_flush.push_back(PendingRespond::new(log_id, resp));
                    }
                    Some(Condition::Applied { log_id }) => {
                        pending_responds.on_apply.push_back(PendingRespond::new(log_id, resp));
                    }
                    Some(Condition::Snapshot { log_id }) => {
                        pending_responds.on_snapshot.push_back(PendingRespond::new(log_id, resp));
                    }
                }
                Ok(())
            }

            _ => {
                self.commands.push_front(cmd);
                Err("Put back to the front of command queue")
            }
        }
    }

    /// Pop the first command to run from the queue.
    pub(crate) fn pop_command(&mut self) -> Option<Command<C, SM>> {
        let cmd = self.commands.pop_front();
        // A drained VecDeque keeps its head where the last pop left it, so each
        // command would write the next slot until the whole allocated buffer is
        // resident. `clear()` on an empty queue moves the head back to slot 0.
        if self.commands.is_empty() {
            self.commands.clear();
        }
        cmd
    }

    /// Iterate all queued commands.
    pub(crate) fn iter_commands(&self) -> impl Iterator<Item = &Command<C, SM>> {
        self.commands.iter()
    }

    /// Take all queued commands and clear the queue.
    #[cfg(test)]
    pub(crate) fn take_commands(&mut self) -> Vec<Command<C, SM>> {
        self.commands.drain(..).collect()
    }

    /// Clear all queued commands.
    #[cfg(any(test, feature = "bench"))]
    pub(crate) fn clear_commands(&mut self) {
        self.commands.clear()
    }

    /// Reorganize commands for better I/O performance.
    ///
    /// Currently performs:
    /// - Merging consecutive `AppendEntries` commands at the front of the queue, with the same
    ///   committed vote, into one
    ///
    /// [`Config::max_append_entries()`] limits the merge, to avoid high latency from writing very
    /// large batches to storage: a command is merged only if the combined entry count stays at or
    /// below it. So 0 merges no two commands that hold entries. The first command is kept whole,
    /// even when it already holds more entries than the limit.
    pub(crate) fn sched_commands(&mut self, config: &Config) {
        let mut scheduler = CommandScheduler::new(config, self);
        scheduler.merge_front_append_entries();
    }
}

#[cfg(test)]
mod tests {
    use super::EngineOutput;
    use crate::batch::Batch;
    use crate::engine::Command;
    use crate::engine::Engine;
    use crate::engine::testing::UTConfig;
    use crate::engine::testing::UTLeaderId;
    use crate::impls::Vote;
    use crate::testing::blank_ent;
    use crate::vote::raft_vote::RaftVoteExt;

    fn append(index: u64) -> Command<UTConfig> {
        Command::AppendEntries {
            committed_vote: Vote::<UTLeaderId>::new(1, 0).to_committed(),
            entries: Batch::of([blank_ent::<UTConfig>(1, 0, index)]),
        }
    }

    #[test]
    fn test_new_engine_holds_no_queue_buffers() {
        let engine = Engine::<UTConfig>::testing_default(0);
        let output = &engine.output;
        let pending = &output.pending_responds;

        let capacities = [
            output.commands.capacity(),
            pending.on_log_io.capacity(),
            pending.on_log_flush.capacity(),
            pending.on_apply.capacity(),
            pending.on_snapshot.capacity(),
        ];
        assert_eq!([0; 5], capacities);
    }

    #[test]
    fn test_pop_command_reuses_slot_zero_once_empty() {
        let mut output: EngineOutput<UTConfig> = EngineOutput::default();
        output.push_command(append(1));
        let first_slot = output.commands.as_slices().0.as_ptr();

        assert!(output.pop_command().is_some());
        output.push_command(append(2));
        assert_eq!(output.commands.as_slices().0.as_ptr(), first_slot);
    }

    #[test]
    fn test_commands_grow_and_retain_capacity() {
        let mut output: EngineOutput<UTConfig> = EngineOutput::default();
        assert_eq!(0, output.commands.capacity());

        output.push_command(append(1));
        output.push_command(append(2));
        let capacity = output.commands.capacity();
        let grew = capacity > 1;
        assert!(grew);

        let mut commands = Vec::new();
        while let Some(command) = output.pop_command() {
            commands.push(command);
        }
        let expected = vec![append(1), append(2)];
        assert_eq!(expected, commands);
        let retained_capacity = output.commands.capacity();
        assert_eq!(capacity, retained_capacity);
    }
}