Просмотр исходного кода

Include snapshot in persisted state.

The `LogArray::all()` method is now only used in tests.
Jing Yang 4 лет назад
Родитель
Сommit
b3067671d6
3 измененных файлов с 8 добавлено и 13 удалено
  1. 1 2
      src/lib.rs
  2. 2 7
      src/log_array.rs
  3. 5 4
      src/persister.rs

+ 1 - 2
src/lib.rs

@@ -161,8 +161,7 @@ where
         {
             state.current_term = persisted_state.current_term;
             state.voted_for = persisted_state.voted_for;
-            state.log =
-                log_array::LogArray::restore(persisted_state.log).unwrap();
+            state.log = persisted_state.log;
         }
 
         let election = ElectionState {

+ 2 - 7
src/log_array.rs

@@ -26,6 +26,7 @@ use crate::{Index, LogEntry, Term};
 /// All APIs **will** panic if the given index(es) are out of bound.
 ///
 /// NOT THREAD SAFE.
+#[derive(Clone, Serialize, Deserialize)]
 pub(crate) struct LogArray<C> {
     inner: Vec<LogEntry<C>>,
     snapshot: Vec<u8>,
@@ -41,13 +42,6 @@ impl<C: Default> LogArray<C> {
         ret.check_one_element();
         ret
     }
-
-    pub fn restore(inner: Vec<LogEntry<C>>) -> std::io::Result<Self> {
-        Ok(LogArray {
-            inner,
-            snapshot: vec![],
-        })
-    }
 }
 
 // Log accessors
@@ -93,6 +87,7 @@ impl<C> LogArray<C> {
     }
 
     /// All log entries stored in the array.
+    #[allow(dead_code)]
     pub fn all(&self) -> &[LogEntry<C>] {
         &self.inner[..]
     }

+ 5 - 4
src/persister.rs

@@ -1,11 +1,12 @@
 use std::convert::TryFrom;
 
 use bytes::Bytes;
-
-use crate::{LogEntry, Peer, RaftState, Term};
 use serde::de::DeserializeOwned;
 use serde::Serialize;
 
+use crate::log_array::LogArray;
+use crate::{Peer, RaftState, Term};
+
 pub trait Persister: Send + Sync {
     fn read_state(&self) -> Bytes;
     fn save_state(&self, bytes: Bytes);
@@ -18,7 +19,7 @@ pub trait Persister: Send + Sync {
 pub(crate) struct PersistedRaftState<Command> {
     pub current_term: Term,
     pub voted_for: Option<Peer>,
-    pub log: Vec<LogEntry<Command>>,
+    pub log: LogArray<Command>,
 }
 
 impl<Command: Clone, T: AsRef<RaftState<Command>>> From<T>
@@ -34,7 +35,7 @@ impl<Command: Clone> From<&RaftState<Command>> for PersistedRaftState<Command> {
         Self {
             current_term: raft_state.current_term,
             voted_for: raft_state.voted_for,
-            log: raft_state.log.all().to_vec(),
+            log: raft_state.log.clone(),
         }
     }
 }