소스 검색

Add the prevote state and check it after prevote is done.

Jing Yang 3 년 전
부모
커밋
0bf0fcc0dd
2개의 변경된 파일5개의 추가작업 그리고 3개의 파일을 삭제
  1. 4 2
      src/election.rs
  2. 1 1
      src/raft_state.rs

+ 4 - 2
src/election.rs

@@ -246,7 +246,7 @@ impl<Command: ReplicableCommand> Raft<Command> {
         timer_count: usize,
     ) -> Option<futures_channel::oneshot::Sender<()>> {
         let (term, last_log_index) = {
-            let rf = self.inner_state.lock();
+            let mut rf = self.inner_state.lock();
 
             // The previous election is faster and reached the critical section
             // before us. We should stop and not run this election.
@@ -255,6 +255,8 @@ impl<Command: ReplicableCommand> Raft<Command> {
                 return None;
             }
 
+            rf.state = State::Prevote;
+
             (rf.current_term, rf.log.last_index_term())
         };
 
@@ -307,7 +309,7 @@ impl<Command: ReplicableCommand> Raft<Command> {
         let (next_term, (last_log_index, last_log_term)) = {
             let mut rf = candidate.rf.lock();
 
-            if term != rf.current_term {
+            if term != rf.current_term || rf.state != State::Prevote {
                 return;
             }
 

+ 1 - 1
src/raft_state.rs

@@ -5,8 +5,8 @@ use crate::{
 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
 pub(crate) enum State {
     Follower,
+    Prevote,
     Candidate,
-    // TODO: add PreVote
     Leader,
 }