Forráskód Böngészése

Implement a do-nothing storage.

Jing Yang 2 éve
szülő
commit
eae9fd0ccc
1 módosított fájl, 68 hozzáadás és 2 törlés
  1. 68 2
      src/utils/do_nothing.rs

+ 68 - 2
src/utils/do_nothing.rs

@@ -3,10 +3,14 @@
 use async_trait::async_trait;
 use bytes::Bytes;
 
+use crate::storage::{
+    RaftLogEntryRef, RaftStorageMonitorTrait, RaftStoragePersisterTrait,
+    RaftStorageTrait, RaftStoredState,
+};
 use crate::{
-    AppendEntriesArgs, AppendEntriesReply, InstallSnapshotArgs,
+    AppendEntriesArgs, AppendEntriesReply, Index, InstallSnapshotArgs,
     InstallSnapshotReply, Persister, RemoteRaft, RequestVoteArgs,
-    RequestVoteReply,
+    RequestVoteReply, Term,
 };
 
 #[derive(Clone)]
@@ -50,3 +54,65 @@ impl<Command: 'static + Send> RemoteRaft<Command> for DoNothingRemoteRaft {
         unimplemented!()
     }
 }
+
+pub struct DoNothingRaftStorage;
+
+impl RaftStorageTrait for DoNothingRaftStorage {
+    type RaftStoragePersister<LogEntry: RaftLogEntryRef> =
+        DoNothingRaftStoragePersister;
+    type RaftStorageMonitor = DoNothingRaftStorageMonitor;
+
+    fn persister<LogEntry>(
+        self,
+    ) -> std::sync::Arc<DoNothingRaftStoragePersister>
+    where
+        LogEntry: RaftLogEntryRef,
+    {
+        std::sync::Arc::new(DoNothingRaftStoragePersister)
+    }
+
+    fn read_state(&self) -> std::io::Result<RaftStoredState> {
+        Ok(RaftStoredState {
+            current_term: Term(0),
+            voted_for: "".to_string(),
+            log: vec![],
+            snapshot_index: 0,
+            snapshot: vec![],
+        })
+    }
+
+    fn log_compaction_required(&self) -> bool {
+        false
+    }
+
+    fn monitor(&self) -> DoNothingRaftStorageMonitor {
+        DoNothingRaftStorageMonitor
+    }
+}
+
+pub struct DoNothingRaftStorageMonitor;
+
+impl RaftStorageMonitorTrait for DoNothingRaftStorageMonitor {
+    fn should_compact_log_now(&self) -> bool {
+        return false;
+    }
+}
+
+pub struct DoNothingRaftStoragePersister;
+
+impl<LogEntry: RaftLogEntryRef> RaftStoragePersisterTrait<LogEntry>
+    for DoNothingRaftStoragePersister
+{
+    fn save_term_vote(&self, _term: Term, _voted_for: String) {}
+
+    fn append_one_entry(&self, _entry: &LogEntry) {}
+
+    fn append_entries<'a, LogEntryList>(&self, _entries: LogEntryList)
+    where
+        LogEntry: 'a,
+        LogEntryList: IntoIterator<Item = &'a LogEntry>,
+    {
+    }
+
+    fn update_snapshot(&self, _index: Index, _snapshot: &[u8]) {}
+}