Parcourir la source

Move `last_applied` to the apply_command daemon.

Jing Yang il y a 3 ans
Parent
commit
e3ea70c318
3 fichiers modifiés avec 7 ajouts et 10 suppressions
  1. 7 6
      src/apply_command.rs
  2. 0 2
      src/daemon_env.rs
  3. 0 2
      src/raft_state.rs

+ 7 - 6
src/apply_command.rs

@@ -57,10 +57,11 @@ impl<Command: ReplicableCommand> Raft<Command> {
         move || {
             log::info!("{:?} apply command daemon running ...", me);
 
+            let mut last_applied = 0;
             while keep_running.load(Ordering::Relaxed) {
                 let messages = {
                     let mut rf = rf.lock();
-                    if rf.last_applied >= rf.commit_index {
+                    if last_applied >= rf.commit_index {
                         // We have applied all committed log entries, wait until
                         // new log entries are committed.
                         condvar.wait_for(&mut rf, HEARTBEAT_INTERVAL);
@@ -69,17 +70,17 @@ impl<Command: ReplicableCommand> Raft<Command> {
                     // always smaller than or equal to commit index, as
                     // guaranteed by the SNAPSHOT_INDEX_INVARIANT.
                     assert!(rf.log.start() <= rf.commit_index);
-                    if rf.last_applied < rf.log.start() {
+                    if last_applied < rf.log.start() {
                         let (index_term, data) = rf.log.snapshot();
                         let messages =
                             vec![ApplyCommandMessage::Snapshot(Snapshot {
                                 last_included_index: index_term.index,
                                 data: data.to_vec(),
                             })];
-                        rf.last_applied = rf.log.start();
+                        last_applied = rf.log.start();
                         messages
-                    } else if rf.last_applied < rf.commit_index {
-                        let index = rf.last_applied + 1;
+                    } else if last_applied < rf.commit_index {
+                        let index = last_applied + 1;
                         let last_one = rf.commit_index + 1;
                         // This is safe because commit_index is always smaller
                         // than log.end(), see COMMIT_INDEX_INVARIANT.
@@ -95,7 +96,7 @@ impl<Command: ReplicableCommand> Raft<Command> {
                                 )
                             })
                             .collect();
-                        rf.last_applied = rf.commit_index;
+                        last_applied = rf.commit_index;
                         messages
                     } else {
                         continue;

+ 0 - 2
src/daemon_env.rs

@@ -158,7 +158,6 @@ impl DaemonEnv {
             voted_for: raft.voted_for,
             log: raft.log.all_index_term(),
             commit_index: raft.commit_index,
-            last_applied: raft.last_applied,
             state: raft.state,
             leader_id: raft.leader_id,
         }
@@ -172,7 +171,6 @@ struct StrippedRaftState {
     voted_for: Option<Peer>,
     log: Vec<IndexTerm>,
     commit_index: usize,
-    last_applied: usize,
     state: State,
     leader_id: Peer,
 }

+ 0 - 2
src/raft_state.rs

@@ -16,7 +16,6 @@ pub(crate) struct RaftState<Command> {
     pub log: LogArray<Command>,
 
     pub commit_index: Index,
-    pub last_applied: Index,
 
     pub match_index: Vec<Index>,
 
@@ -32,7 +31,6 @@ impl<Command> RaftState<Command> {
             voted_for: None,
             log: LogArray::create(),
             commit_index: 0,
-            last_applied: 0,
             match_index: vec![0; peer_size],
             state: State::Follower,
             leader_id: me,