Kaynağa Gözat

Delete persister.

Jing Yang 2 yıl önce
ebeveyn
işleme
8db888f64d

+ 0 - 2
src/lib.rs

@@ -5,7 +5,6 @@ pub use crate::apply_command::ApplyCommandMessage;
 pub use crate::index_term::IndexTerm;
 pub use crate::index_term::IndexTerm;
 pub use crate::log_array::Index;
 pub use crate::log_array::Index;
 pub use crate::messages::*;
 pub use crate::messages::*;
-pub use crate::persister::Persister;
 pub use crate::raft::{Raft, Term};
 pub use crate::raft::{Raft, Term};
 pub use crate::remote_raft::RemoteRaft;
 pub use crate::remote_raft::RemoteRaft;
 pub use crate::replicable_command::ReplicableCommand;
 pub use crate::replicable_command::ReplicableCommand;
@@ -26,7 +25,6 @@ mod index_term;
 mod log_array;
 mod log_array;
 mod messages;
 mod messages;
 mod peer_progress;
 mod peer_progress;
-mod persister;
 mod process_append_entries;
 mod process_append_entries;
 mod process_install_snapshot;
 mod process_install_snapshot;
 mod process_request_vote;
 mod process_request_vote;

+ 0 - 63
src/persister.rs

@@ -1,63 +0,0 @@
-use std::convert::TryFrom;
-
-use bytes::Bytes;
-use serde::de::DeserializeOwned;
-use serde::ser::Serialize;
-use serde_derive::{Deserialize, Serialize};
-
-use crate::log_array::LogArray;
-use crate::{Peer, RaftState, Term};
-
-/// An object that saves bytes to permanent storage.
-///
-/// When the methods of this trait returns, data should have been persisted to
-/// the storage. These methods should never return failure except panicking.
-/// They should not block forever, either.
-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)]
-pub(crate) struct PersistedRaftState<Command> {
-    pub current_term: Term,
-    pub voted_for: Option<Peer>,
-    pub log: LogArray<Command>,
-}
-
-impl<Command: Clone, T: AsRef<RaftState<Command>>> From<T>
-    for PersistedRaftState<Command>
-{
-    fn from(raft_state: T) -> Self {
-        Self::from(raft_state.as_ref())
-    }
-}
-
-impl<Command: Clone> From<&RaftState<Command>> for PersistedRaftState<Command> {
-    fn from(raft_state: &RaftState<Command>) -> Self {
-        Self {
-            current_term: raft_state.current_term,
-            voted_for: raft_state.voted_for,
-            log: raft_state.log.clone(),
-        }
-    }
-}
-
-impl<Command: DeserializeOwned> TryFrom<Bytes> for PersistedRaftState<Command> {
-    type Error = bincode::Error;
-
-    fn try_from(value: Bytes) -> Result<Self, Self::Error> {
-        bincode::deserialize(value.as_ref())
-    }
-}
-
-impl<Command: Serialize> From<PersistedRaftState<Command>> for Bytes {
-    fn from(value: PersistedRaftState<Command>) -> Bytes {
-        bincode::serialize(&value)
-            .expect("Serialization should not fail")
-            .into()
-    }
-}

+ 1 - 9
src/raft_state.rs

@@ -1,6 +1,4 @@
-use crate::{
-    log_array::LogArray, persister::PersistedRaftState, Index, Peer, Term,
-};
+use crate::{log_array::LogArray, Index, Peer, Term};
 
 
 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
 pub(crate) enum State {
 pub(crate) enum State {
@@ -39,12 +37,6 @@ impl<Command> RaftState<Command> {
     }
     }
 }
 }
 
 
-impl<Command: Clone> RaftState<Command> {
-    pub fn persisted_state(&self) -> PersistedRaftState<Command> {
-        self.into()
-    }
-}
-
 impl<Command> RaftState<Command> {
 impl<Command> RaftState<Command> {
     pub fn is_leader(&self) -> bool {
     pub fn is_leader(&self) -> bool {
         self.state == State::Leader
         self.state == State::Leader

+ 1 - 19
src/utils/do_nothing.rs

@@ -1,7 +1,6 @@
 #![cfg(feature = "integration-test")]
 #![cfg(feature = "integration-test")]
 
 
 use async_trait::async_trait;
 use async_trait::async_trait;
-use bytes::Bytes;
 
 
 use crate::storage::{
 use crate::storage::{
     RaftLogEntryRef, RaftStorageMonitorTrait, RaftStoragePersisterTrait,
     RaftLogEntryRef, RaftStorageMonitorTrait, RaftStoragePersisterTrait,
@@ -9,26 +8,9 @@ use crate::storage::{
 };
 };
 use crate::{
 use crate::{
     AppendEntriesArgs, AppendEntriesReply, Index, InstallSnapshotArgs,
     AppendEntriesArgs, AppendEntriesReply, Index, InstallSnapshotArgs,
-    InstallSnapshotReply, Persister, RemoteRaft, RequestVoteArgs,
-    RequestVoteReply, Term,
+    InstallSnapshotReply, RemoteRaft, RequestVoteArgs, RequestVoteReply, Term,
 };
 };
 
 
-#[derive(Clone)]
-pub struct DoNothingPersister;
-impl Persister for DoNothingPersister {
-    fn read_state(&self) -> Bytes {
-        Bytes::new()
-    }
-
-    fn save_state(&self, _bytes: Bytes) {}
-
-    fn state_size(&self) -> usize {
-        0
-    }
-
-    fn save_snapshot_and_state(&self, _: Bytes, _: &[u8]) {}
-}
-
 #[derive(Clone)]
 #[derive(Clone)]
 pub struct DoNothingRemoteRaft;
 pub struct DoNothingRemoteRaft;
 #[async_trait]
 #[async_trait]

+ 0 - 2
test_configs/src/lib.rs

@@ -3,11 +3,9 @@
 mod in_memory_storage;
 mod in_memory_storage;
 pub mod interceptor;
 pub mod interceptor;
 pub mod kvraft;
 pub mod kvraft;
-mod persister;
 pub mod raft;
 pub mod raft;
 mod rpcs;
 mod rpcs;
 pub mod utils;
 pub mod utils;
 
 
 pub use in_memory_storage::InMemoryStorage;
 pub use in_memory_storage::InMemoryStorage;
-pub use persister::Persister;
 use rpcs::{register_kv_server, register_server, RpcClient};
 use rpcs::{register_kv_server, register_server, RpcClient};

+ 0 - 66
test_configs/src/persister.rs

@@ -1,66 +0,0 @@
-use parking_lot::Mutex;
-
-#[derive(Clone)]
-pub struct State {
-    pub bytes: bytes::Bytes,
-    pub snapshot: Vec<u8>,
-}
-
-pub struct Persister {
-    state: Mutex<State>,
-}
-
-impl Persister {
-    pub fn new() -> Self {
-        Self {
-            state: Mutex::new(State {
-                bytes: bytes::Bytes::new(),
-                snapshot: vec![],
-            }),
-        }
-    }
-}
-
-impl Default for Persister {
-    fn default() -> Self {
-        Self::new()
-    }
-}
-
-impl ruaft::Persister for Persister {
-    fn read_state(&self) -> bytes::Bytes {
-        self.state.lock().bytes.clone()
-    }
-
-    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::Bytes, snapshot: &[u8]) {
-        let mut this = self.state.lock();
-        this.bytes = state;
-        this.snapshot = snapshot.to_vec();
-    }
-}
-
-impl Persister {
-    pub fn read(&self) -> State {
-        self.state.lock().clone()
-    }
-
-    pub fn restore(&self, state: State) {
-        *self.state.lock() = state;
-    }
-
-    pub fn snapshot_size(&self) -> usize {
-        self.state.lock().snapshot.len()
-    }
-
-    pub fn downcast_unsafe(trait_obj: &dyn ruaft::Persister) -> &Self {
-        unsafe { &*(trait_obj as *const dyn ruaft::Persister as *const Self) }
-    }
-}