Skip to content

Commit

Permalink
Don't require Copy for StateMachineTransition
Browse files Browse the repository at this point in the history
Heap-allocated things like Strings and Vecs are not copyable so don't
require Copy trait for StateMachineTransition to simplify state machine
transition implementation.
  • Loading branch information
penberg committed Nov 12, 2021
1 parent ffee5ec commit 00704a6
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion little_raft/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::state_machine::StateMachineTransition;

/// LogEntry is a state machine transition along with some metadata needed for
/// Raft.
#[derive(Clone, Debug, Copy, PartialEq, Eq, PartialOrd)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd)]
pub struct LogEntry<T>
where
T: StateMachineTransition,
Expand Down
12 changes: 6 additions & 6 deletions little_raft/src/replica.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::{
time::{Duration, Instant},
};

#[derive(Clone, Copy, PartialEq, Debug)]
#[derive(Clone, PartialEq, Debug)]
enum State {
Follower,
Candidate,
Expand Down Expand Up @@ -143,9 +143,9 @@ where
log: vec![LogEntry {
term: 0,
index: 0,
transition: noop_transition,
transition: noop_transition.clone(),
}],
noop_transition: noop_transition,
noop_transition: noop_transition.clone(),
commit_index: 0,
last_applied: 0,
next_index: BTreeMap::new(),
Expand Down Expand Up @@ -351,7 +351,7 @@ where
while self.commit_index > self.last_applied {
self.last_applied += 1;
let mut state_machine = self.state_machine.lock().unwrap();
state_machine.apply_transition(self.log[self.last_applied].transition);
state_machine.apply_transition(self.log[self.last_applied].transition.clone());
state_machine.register_transition_state(
self.log[self.last_applied].transition.get_id(),
TransitionState::Applied,
Expand All @@ -367,7 +367,7 @@ where
if self.state == State::Leader {
self.log.push(LogEntry {
index: self.log.len(),
transition: transition,
transition: transition.clone(),
term: self.current_term,
});

Expand Down Expand Up @@ -674,7 +674,7 @@ where
// in the part 8 of the paper.
self.log.push(LogEntry {
index: self.log.len(),
transition: self.noop_transition,
transition: self.noop_transition.clone(),
term: self.current_term,
});
}
Expand Down
4 changes: 2 additions & 2 deletions little_raft/src/state_machine.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt::Debug;

/// TransitionState describes the state of a particular transition.
#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq)]
pub enum TransitionState {
/// Queued transitions have been received from the user but have not been
/// processed yet. They are in the queue.
Expand All @@ -20,7 +20,7 @@ pub enum TransitionState {

/// StateMachineTransition describes a user-defined transition that can be
/// applied to the state machine replicated by Raft.
pub trait StateMachineTransition: Copy + Clone + Debug {
pub trait StateMachineTransition: Clone + Debug {
/// TransitionID is used to identify the transition.
type TransitionID: Eq;

Expand Down
3 changes: 2 additions & 1 deletion little_raft/tests/raft_stable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const MAX_ELECTION_TIMEOUT: Duration = Duration::from_millis(950);

// Our state machine will carry out simple plus and minus operations on a
// number, starting from zero.
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Debug)]
struct ArithmeticOperation {
id: usize,
delta: i32,
Expand Down Expand Up @@ -288,6 +288,7 @@ fn run_replicas() {
let state_machines = create_state_machines(n, applied_transitions_tx);
let (message_tx, transition_tx, message_rx, transition_rx) = create_notifiers(n);
for i in 0..n {
let noop = noop.clone();
let local_peer_ids = peer_ids[i].clone();
let cluster = clusters[i].clone();
let state_machine = state_machines[i].clone();
Expand Down
3 changes: 2 additions & 1 deletion little_raft/tests/raft_unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const MAX_ELECTION_TIMEOUT: Duration = Duration::from_millis(950);

// Our state machine will carry out simple plus and minus operations on a
// number, starting from zero.
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Debug)]
struct ArithmeticOperation {
id: usize,
delta: i32,
Expand Down Expand Up @@ -288,6 +288,7 @@ fn run_replicas() {
let state_machines = create_state_machines(n, applied_transitions_tx);
let (message_tx, transition_tx, message_rx, transition_rx) = create_notifiers(n);
for i in 0..n {
let noop = noop.clone();
let local_peer_ids = peer_ids[i].clone();
let cluster = clusters[i].clone();
let state_machine = state_machines[i].clone();
Expand Down

0 comments on commit 00704a6

Please sign in to comment.