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;
#[derive(Debug)]
pub(crate) struct EngineOutput<C, SM = ()>
where
C: RaftTypeConfig,
SM: RaftStateMachine<C>,
{
pub(crate) commands: VecDeque<Command<C, SM>>,
pub(crate) pending_responds: PendingResponds<C>,
}
impl<C, SM> Default for EngineOutput<C, SM>
where
C: RaftTypeConfig,
SM: RaftStateMachine<C>,
{
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()
}
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)
}
pub(crate) fn postpone_command(&mut self, cmd: Command<C, SM>) -> Result<(), &'static str> {
tracing::debug!("postpone command: {:?}", cmd);
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")
}
}
}
pub(crate) fn pop_command(&mut self) -> Option<Command<C, SM>> {
let cmd = self.commands.pop_front();
if self.commands.is_empty() {
self.commands.clear();
}
cmd
}
pub(crate) fn iter_commands(&self) -> impl Iterator<Item = &Command<C, SM>> {
self.commands.iter()
}
#[cfg(test)]
pub(crate) fn take_commands(&mut self) -> Vec<Command<C, SM>> {
self.commands.drain(..).collect()
}
#[cfg(any(test, feature = "bench"))]
pub(crate) fn clear_commands(&mut self) {
self.commands.clear()
}
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);
}
}