Kaynağa Gözat

Move process request vote to its own file.

Jing Yang 4 yıl önce
ebeveyn
işleme
14cffb8a86
2 değiştirilmiş dosya ile 63 ekleme ve 61 silme
  1. 1 61
      src/lib.rs
  2. 62 0
      src/process_request_vote.rs

+ 1 - 61
src/lib.rs

@@ -37,6 +37,7 @@ mod install_snapshot;
 mod log_array;
 mod persister;
 mod process_append_entries;
+mod process_request_vote;
 mod raft_state;
 pub mod rpcs;
 mod snapshot;
@@ -199,67 +200,6 @@ where
     }
 }
 
-// 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,
-{
-    pub(crate) fn process_request_vote(
-        &self,
-        args: RequestVoteArgs,
-    ) -> RequestVoteReply {
-        // Note: do not change this to `let _ = ...`.
-        let _guard = self.daemon_env.for_scope();
-
-        let mut rf = self.inner_state.lock();
-
-        let term = rf.current_term;
-        #[allow(clippy::comparison_chain)]
-        if args.term < term {
-            return RequestVoteReply {
-                term,
-                vote_granted: false,
-            };
-        } else if args.term > term {
-            rf.current_term = args.term;
-            rf.voted_for = None;
-            rf.state = State::Follower;
-
-            self.election.reset_election_timer();
-            self.persister.save_state(rf.persisted_state().into());
-        }
-
-        let voted_for = rf.voted_for;
-        let last_log = rf.log.last_index_term();
-        if (voted_for.is_none() || voted_for == Some(args.candidate_id))
-            && (args.last_log_term > last_log.term
-                || (args.last_log_term == last_log.term
-                    && args.last_log_index >= last_log.index))
-        {
-            rf.voted_for = Some(args.candidate_id);
-
-            // It is possible that we have set a timer above when updating the
-            // current term. It does not hurt to update the timer again.
-            // We do need to persist, though.
-            self.election.reset_election_timer();
-            self.persister.save_state(rf.persisted_state().into());
-
-            RequestVoteReply {
-                term: args.term,
-                vote_granted: true,
-            }
-        } else {
-            RequestVoteReply {
-                term: args.term,
-                vote_granted: false,
-            }
-        }
-    }
-}
-
 // Command must be
 // 0. 'static: Raft<Command> must be 'static, it is moved to another thread.
 // 1. clone: they are copied to the persister.

+ 62 - 0
src/process_request_vote.rs

@@ -0,0 +1,62 @@
+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,
+{
+    pub(crate) fn process_request_vote(
+        &self,
+        args: RequestVoteArgs,
+    ) -> RequestVoteReply {
+        // Note: do not change this to `let _ = ...`.
+        let _guard = self.daemon_env.for_scope();
+
+        let mut rf = self.inner_state.lock();
+
+        let term = rf.current_term;
+        #[allow(clippy::comparison_chain)]
+        if args.term < term {
+            return RequestVoteReply {
+                term,
+                vote_granted: false,
+            };
+        } else if args.term > term {
+            rf.current_term = args.term;
+            rf.voted_for = None;
+            rf.state = State::Follower;
+
+            self.election.reset_election_timer();
+            self.persister.save_state(rf.persisted_state().into());
+        }
+
+        let voted_for = rf.voted_for;
+        let last_log = rf.log.last_index_term();
+        if (voted_for.is_none() || voted_for == Some(args.candidate_id))
+            && (args.last_log_term > last_log.term
+                || (args.last_log_term == last_log.term
+                    && args.last_log_index >= last_log.index))
+        {
+            rf.voted_for = Some(args.candidate_id);
+
+            // It is possible that we have set a timer above when updating the
+            // current term. It does not hurt to update the timer again.
+            // We do need to persist, though.
+            self.election.reset_election_timer();
+            self.persister.save_state(rf.persisted_state().into());
+
+            RequestVoteReply {
+                term: args.term,
+                vote_granted: true,
+            }
+        } else {
+            RequestVoteReply {
+                term: args.term,
+                vote_granted: false,
+            }
+        }
+    }
+}