Преглед изворни кода

Add snapshot functions to persister.

Jing Yang пре 5 година
родитељ
комит
8e36805176
3 измењених фајлова са 22 додато и 0 уклоњено
  1. 3 0
      src/persister.rs
  2. 6 0
      src/rpcs.rs
  3. 13 0
      tests/config/persister/mod.rs

+ 3 - 0
src/persister.rs

@@ -9,6 +9,9 @@ use serde::Serialize;
 pub trait Persister: Send + Sync {
     fn read_state(&self) -> Bytes;
     fn save_state(&self, bytes: Bytes);
+    fn state_size(&self) -> usize;
+
+    fn save_snapshot_and_state(&self, state: Bytes, snapshot: &[u8]);
 }
 
 #[derive(Serialize, Deserialize)]

+ 6 - 0
src/rpcs.rs

@@ -173,6 +173,12 @@ mod tests {
         }
 
         fn save_state(&self, _bytes: Bytes) {}
+
+        fn state_size(&self) -> usize {
+            0
+        }
+
+        fn save_snapshot_and_state(&self, _: Bytes, _: &[u8]) {}
     }
 
     #[test]

+ 13 - 0
tests/config/persister/mod.rs

@@ -1,7 +1,9 @@
+use bytes::Bytes;
 use parking_lot::Mutex;
 
 struct State {
     bytes: bytes::Bytes,
+    snapshot: Vec<u8>,
 }
 
 pub struct Persister {
@@ -13,6 +15,7 @@ impl Persister {
         Self {
             state: Mutex::new(State {
                 bytes: bytes::Bytes::new(),
+                snapshot: vec![],
             }),
         }
     }
@@ -26,4 +29,14 @@ impl ruaft::Persister for Persister {
     fn save_state(&self, data: bytes::Bytes) {
         self.state.lock().bytes = data;
     }
+
+    fn state_size(&self) -> usize {
+        self.state.lock().bytes.len()
+    }
+
+    fn save_snapshot_and_state(&self, state: Bytes, snapshot: &[u8]) {
+        let mut this = self.state.lock();
+        this.bytes = state;
+        this.snapshot = snapshot.to_vec();
+    }
 }