Quellcode durchsuchen

Save snapshot term with snapshot.

Jing Yang vor 2 Jahren
Ursprung
Commit
f879688a6b

+ 4 - 2
src/storage/log_persister.rs

@@ -64,6 +64,7 @@ where
         <T as RaftStoragePersisterTrait<LogEntry<Command>>>::update_snapshot(
             self,
             index_term.index,
+            index_term.term,
             snapshot,
         )
     }
@@ -102,7 +103,7 @@ impl RaftStoredState {
         self,
         log_array: &mut LogArray<Command>,
     ) {
-        log_array.reset(self.snapshot_index, Term(0), self.snapshot);
+        log_array.reset(self.snapshot_index, self.snapshot_term, self.snapshot);
         for entry in self.log.iter() {
             log_array.push(decode_log_entry(&entry.command));
         }
@@ -140,6 +141,7 @@ mod tests {
                 },
             ],
             snapshot_index: 0,
+            snapshot_term: Term(1),
             snapshot: vec![0x09, 0x90],
         };
 
@@ -150,7 +152,7 @@ mod tests {
         assert_eq!(log_array.end(), new_log_array.end());
         assert_eq!(log_array.at(1).command(), new_log_array.at(1).command());
         assert_eq!(log_array.at(2).command(), new_log_array.at(2).command());
-        assert_eq!(IndexTerm::pack(0, Term(0)), new_log_array.snapshot().0);
+        assert_eq!(IndexTerm::pack(0, Term(1)), new_log_array.snapshot().0);
         assert_eq!(&[0x09u8, 0x90u8], new_log_array.snapshot().1);
     }
 }

+ 2 - 1
src/storage/mod.rs

@@ -49,7 +49,7 @@ pub trait RaftStoragePersisterTrait<LogEntry: RaftLogEntryRef>:
     /// Store the application snapshot. The snapshot is computed from all log
     /// entries at and before `index`. After the snapshot is saved, any log
     /// entry on or before index can be discarded.
-    fn update_snapshot(&self, index: Index, snapshot: &[u8]);
+    fn update_snapshot(&self, index: Index, term: Term, snapshot: &[u8]);
 }
 
 /// An object that watches the underlying storage system and help Raft decide
@@ -75,6 +75,7 @@ pub struct RaftStoredState {
     pub voted_for: String,
     pub log: Vec<RaftStoredLogEntry>,
     pub snapshot_index: Index,
+    pub snapshot_term: Term,
     pub snapshot: Vec<u8>,
 }
 

+ 2 - 1
src/utils/do_nothing.rs

@@ -77,6 +77,7 @@ impl RaftStorageTrait for DoNothingRaftStorage {
             voted_for: "".to_string(),
             log: vec![],
             snapshot_index: 0,
+            snapshot_term: Term(0),
             snapshot: vec![],
         })
     }
@@ -114,5 +115,5 @@ impl<LogEntry: RaftLogEntryRef> RaftStoragePersisterTrait<LogEntry>
     {
     }
 
-    fn update_snapshot(&self, _index: Index, _snapshot: &[u8]) {}
+    fn update_snapshot(&self, _index: Index, _term: Term, _snapshot: &[u8]) {}
 }

+ 10 - 4
test_configs/src/in_memory_storage.rs

@@ -17,6 +17,7 @@ pub struct State {
     log: Vec<RaftStoredLogEntry>,
 
     snapshot_index: Index,
+    snapshot_term: Term,
     snapshot: Vec<u8>,
 
     log_size: usize,
@@ -30,6 +31,7 @@ impl State {
             voted_for: "".to_owned(),
             log: vec![],
             snapshot_index: 0,
+            snapshot_term: Term(0),
             snapshot: vec![],
             log_size: 0,
         }
@@ -101,6 +103,7 @@ impl RaftStorageTrait for InMemoryStorage {
             voted_for: stored.voted_for.clone(),
             log: organized_log,
             snapshot_index: stored.snapshot_index,
+            snapshot_term: stored.snapshot_term,
             snapshot: stored.snapshot.clone(),
         })
     }
@@ -161,9 +164,10 @@ impl<LogEntry: RaftLogEntryRef> RaftStoragePersisterTrait<LogEntry>
         }
     }
 
-    fn update_snapshot(&self, index: Index, snapshot: &[u8]) {
+    fn update_snapshot(&self, index: Index, term: Term, snapshot: &[u8]) {
         let mut stored = self.0.lock();
         stored.snapshot_index = index;
+        stored.snapshot_term = term;
         stored.snapshot = snapshot.to_vec();
     }
 }
@@ -387,10 +391,11 @@ mod tests {
             assert!(state.snapshot.is_empty());
         }
 
-        type_hint(&state).update_snapshot(7, &[0x01, 0x02]);
+        type_hint(&state).update_snapshot(7, Term(3), &[0x01, 0x02]);
 
         let state = state.0.lock();
         assert_eq!(7, state.snapshot_index);
+        assert_eq!(Term(3), state.snapshot_term);
         assert_eq!(&[0x01, 0x02], state.snapshot.as_slice());
     }
 
@@ -423,7 +428,7 @@ mod tests {
             Transaction::populate(7),
         ]);
         type_hint(&state).save_term_vote(Term(7), "voted_for".to_owned());
-        type_hint(&state).update_snapshot(1, &[0x99]);
+        type_hint(&state).update_snapshot(1, Term(0), &[0x99]);
 
         let raft_stored_state = storage
             .read_state()
@@ -433,6 +438,7 @@ mod tests {
         assert_eq!(3, raft_stored_state.log.len());
         assert_eq!(&[0x99], raft_stored_state.snapshot.as_slice());
         assert_eq!(1, raft_stored_state.snapshot_index);
+        assert_eq!(Term(0), raft_stored_state.snapshot_term);
 
         let entry = &raft_stored_state.log[0];
         assert_eq!(2, entry.index);
@@ -562,7 +568,7 @@ mod tests {
             assert!(state.snapshot.is_empty());
         }
 
-        type_hint(state.deref()).update_snapshot(7, &[0x01, 0x02]);
+        type_hint(state.deref()).update_snapshot(7, Term(3), &[0x01, 0x02]);
         assert_eq!(2, storage.snapshot_size());
     }
 }