Parcourir la source

Remove the "Default" requirement on command.

Jing Yang il y a 3 ans
Parent
commit
e3f2148ecb

+ 0 - 1
kvraft/src/common.rs

@@ -8,7 +8,6 @@ pub type ClerkId = u64;
     Clone,
     Copy,
     Debug,
-    Default,
     Hash,
     Ord,
     PartialOrd,

+ 1 - 9
kvraft/src/server.rs

@@ -30,7 +30,7 @@ pub struct KVServer {
     logger: LocalLogger,
 }
 
-#[derive(Clone, Debug, Default, Serialize, Deserialize)]
+#[derive(Clone, Debug, Serialize, Deserialize)]
 pub struct UniqueKVOp {
     op: KVOp,
     me: usize,
@@ -64,18 +64,11 @@ struct KVServerState {
 
 #[derive(Clone, Debug, Serialize, Deserialize)]
 enum KVOp {
-    NoOp,
     Get(String),
     Put(String, String),
     Append(String, String),
 }
 
-impl Default for KVOp {
-    fn default() -> Self {
-        KVOp::NoOp
-    }
-}
-
 struct ResultHolder {
     term: AtomicUsize,
     peeks: AtomicUsize,
@@ -173,7 +166,6 @@ impl KVServer {
         }
 
         let result = match op {
-            KVOp::NoOp => return,
             KVOp::Get(key) => CommitResult::Get(kv.get(&key).cloned()),
             KVOp::Put(key, value) => {
                 kv.insert(key, value);

+ 1 - 1
src/election.rs

@@ -72,7 +72,7 @@ impl ElectionState {
 // 3. serialize: they are converted to bytes to persist.
 impl<Command> Raft<Command>
 where
-    Command: 'static + Clone + Default + Send + serde::Serialize,
+    Command: 'static + Clone + Send + serde::Serialize,
 {
     /// Runs the election timer daemon that triggers elections.
     ///

+ 3 - 7
src/lib.rs

@@ -132,12 +132,8 @@ pub struct InstallSnapshotReply {
 // 5. default, because we need an element for the first entry.
 impl<Command> Raft<Command>
 where
-    Command: 'static
-        + Clone
-        + serde::Serialize
-        + serde::de::DeserializeOwned
-        + Send
-        + Default,
+    Command:
+        'static + Clone + serde::Serialize + serde::de::DeserializeOwned + Send,
 {
     /// Create a new raft instance.
     ///
@@ -241,7 +237,7 @@ where
 // 4. default: a default value is used as the first element of log.
 impl<Command> Raft<Command>
 where
-    Command: 'static + Clone + Send + serde::Serialize + Default,
+    Command: 'static + Clone + Send + serde::Serialize,
 {
     /// Adds a new command to the log, returns its index and the current term.
     ///

+ 2 - 2
src/log_array.rs

@@ -57,7 +57,7 @@ pub(crate) enum ValidationError {
     FutureTerm(Term, Index, Vec<IndexTerm>),
 }
 
-impl<C: Default> LogArray<C> {
+impl<C> LogArray<C> {
     /// Create the initial Raft log with no user-supplied commands.
     pub fn create() -> LogArray<C> {
         let ret = LogArray {
@@ -224,7 +224,7 @@ impl<C> LogArray<C> {
     }
 }
 
-impl<C: Default> LogArray<C> {
+impl<C> LogArray<C> {
     /// Shift the start of the array to `index`, and store a new snapshot that
     /// covers all commands before and at `index`.
     pub fn shift(&mut self, index: Index, snapshot: Vec<u8>) {

+ 1 - 5
src/process_append_entries.rs

@@ -7,11 +7,7 @@ use crate::{
 // Command must be
 // 1. clone: they are copied to the persister.
 // 2. serialize: they are converted to bytes to persist.
-// 3. default: a default value is used as the first element of the log.
-impl<Command> Raft<Command>
-where
-    Command: Clone + serde::Serialize + Default,
-{
+impl<Command: Clone + serde::Serialize> Raft<Command> {
     pub fn process_append_entries(
         &self,
         args: AppendEntriesArgs<Command>,

+ 1 - 1
src/process_install_snapshot.rs

@@ -2,7 +2,7 @@ use crate::check_or_record;
 use crate::daemon_env::ErrorKind;
 use crate::{InstallSnapshotArgs, InstallSnapshotReply, Raft, State};
 
-impl<C: Clone + Default + serde::Serialize> Raft<C> {
+impl<C: Clone + serde::Serialize> Raft<C> {
     pub fn process_install_snapshot(
         &self,
         args: InstallSnapshotArgs,

+ 1 - 5
src/process_request_vote.rs

@@ -3,11 +3,7 @@ use crate::{Raft, RequestVoteArgs, RequestVoteReply, State};
 // Command must be
 // 1. clone: they are copied to the persister.
 // 2. serialize: they are converted to bytes to persist.
-// 3. default: a default value is used as the first element of the log.
-impl<Command> Raft<Command>
-where
-    Command: Clone + serde::Serialize + Default,
-{
+impl<Command: Clone + serde::Serialize> Raft<Command> {
     pub fn process_request_vote(
         &self,
         args: RequestVoteArgs,

+ 1 - 1
src/raft_state.rs

@@ -30,7 +30,7 @@ pub(crate) struct RaftState<Command> {
     pub sentinel_commit_index: Index,
 }
 
-impl<Command: Default> RaftState<Command> {
+impl<Command> RaftState<Command> {
     pub fn create(peer_size: usize, me: Peer) -> Self {
         RaftState {
             current_term: Term(0),

+ 1 - 1
src/snapshot.rs

@@ -87,7 +87,7 @@ impl SnapshotDaemon {
     }
 }
 
-impl<C: 'static + Clone + Default + Send + serde::Serialize> Raft<C> {
+impl<C: 'static + Clone + Send + serde::Serialize> Raft<C> {
     /// Saves the snapshot into a staging area before it is applied to the log.
     ///
     /// Does nothing if the snapshot has a lower index than any snapshot before.

+ 1 - 2
src/sync_log_entries.rs

@@ -39,10 +39,9 @@ struct TaskNumber(usize);
 // 1. clone: they are copied to the persister.
 // 2. send: Arc<Mutex<Vec<LogEntry<Command>>>> must be send, it is moved to another thread.
 // 3. serialize: they are converted to bytes to persist.
-// 4. default: a default value is used as the first element of log.
 impl<Command> Raft<Command>
 where
-    Command: 'static + Clone + Send + serde::Serialize + Default,
+    Command: 'static + Clone + Send + serde::Serialize,
 {
     /// Runs a daemon thread that syncs log entries to peers.
     ///

+ 1 - 1
test_configs/src/rpcs.rs

@@ -146,7 +146,7 @@ where
 }
 
 pub fn register_server<
-    Command: 'static + Clone + Serialize + DeserializeOwned + Default,
+    Command: 'static + Clone + Serialize + DeserializeOwned,
     R: 'static + AsRef<Raft<Command>> + Send + Sync + Clone,
     S: AsRef<str>,
 >(