diff --git a/cluster_benchmark/tests/benchmark/store.rs b/cluster_benchmark/tests/benchmark/store.rs index 1b9b2ea6f..4d3859f27 100644 --- a/cluster_benchmark/tests/benchmark/store.rs +++ b/cluster_benchmark/tests/benchmark/store.rs @@ -121,6 +121,10 @@ impl RaftLogReader for Arc { Ok(entries) } + + async fn read_vote(&mut self) -> Result>, StorageError> { + Ok(self.vote.read().await.clone()) + } } impl RaftSnapshotBuilder for Arc { @@ -204,10 +208,6 @@ impl RaftLogStorage for Arc { Ok(()) } - async fn read_vote(&mut self) -> Result>, StorageError> { - Ok(self.vote.read().await.clone()) - } - #[tracing::instrument(level = "debug", skip(self))] async fn truncate(&mut self, log_id: LogId) -> Result<(), StorageError> { let mut log = self.log.write().await; diff --git a/examples/memstore/src/log_store.rs b/examples/memstore/src/log_store.rs index 867465ac6..bbed1abe0 100644 --- a/examples/memstore/src/log_store.rs +++ b/examples/memstore/src/log_store.rs @@ -156,6 +156,11 @@ mod impl_log_store { let mut inner = self.inner.lock().await; inner.try_get_log_entries(range).await } + + async fn read_vote(&mut self) -> Result>, StorageError> { + let mut inner = self.inner.lock().await; + inner.read_vote().await + } } impl RaftLogStorage for LogStore @@ -183,11 +188,6 @@ mod impl_log_store { inner.save_vote(vote).await } - async fn read_vote(&mut self) -> Result>, StorageError> { - let mut inner = self.inner.lock().await; - inner.read_vote().await - } - async fn append(&mut self, entries: I, callback: LogFlushed) -> Result<(), StorageError> where I: IntoIterator { let mut inner = self.inner.lock().await; diff --git a/examples/raft-kv-memstore-singlethreaded/src/store.rs b/examples/raft-kv-memstore-singlethreaded/src/store.rs index 3739f2c12..b3a9a3193 100644 --- a/examples/raft-kv-memstore-singlethreaded/src/store.rs +++ b/examples/raft-kv-memstore-singlethreaded/src/store.rs @@ -127,6 +127,10 @@ impl RaftLogReader for Rc { let response = log.range(range.clone()).map(|(_, val)| val.clone()).collect::>(); Ok(response) } + + async fn read_vote(&mut self) -> Result>, StorageError> { + Ok(*self.vote.borrow()) + } } impl RaftSnapshotBuilder for Rc { @@ -313,10 +317,6 @@ impl RaftLogStorage for Rc { Ok(()) } - async fn read_vote(&mut self) -> Result>, StorageError> { - Ok(*self.vote.borrow()) - } - #[tracing::instrument(level = "trace", skip(self, entries, callback))] async fn append(&mut self, entries: I, callback: LogFlushed) -> Result<(), StorageError> where I: IntoIterator> { diff --git a/examples/raft-kv-rocksdb/src/store.rs b/examples/raft-kv-rocksdb/src/store.rs index d4c9f051e..13bca5463 100644 --- a/examples/raft-kv-rocksdb/src/store.rs +++ b/examples/raft-kv-rocksdb/src/store.rs @@ -392,6 +392,10 @@ impl RaftLogReader for LogStore { .map(|x| x.1) .collect() } + + async fn read_vote(&mut self) -> Result>, StorageError> { + self.get_vote_() + } } impl RaftLogStorage for LogStore { @@ -430,10 +434,6 @@ impl RaftLogStorage for LogStore { self.set_vote_(vote) } - async fn read_vote(&mut self) -> Result>, StorageError> { - self.get_vote_() - } - #[tracing::instrument(level = "trace", skip_all)] async fn append(&mut self, entries: I, callback: LogFlushed) -> StorageResult<()> where diff --git a/openraft/src/docs/getting_started/getting-started.md b/openraft/src/docs/getting_started/getting-started.md index ad3c68593..0b725ef3e 100644 --- a/openraft/src/docs/getting_started/getting-started.md +++ b/openraft/src/docs/getting_started/getting-started.md @@ -96,7 +96,6 @@ Follow the link to method document to see the details. | Write log: | [`truncate()`] | () | delete logs `[index, +oo)` | | Write log: | [`purge()`] | () | purge logs `(-oo, index]` | | Vote: | [`save_vote()`] | () | save vote | -| Vote: | [`read_vote()`] | [`Vote`] | read vote | | Kind | [`RaftStateMachine`] method | Return value | Description | |------------|--------------------------------|------------------------------|---------------------------------------| @@ -122,10 +121,12 @@ Most of the APIs are quite straightforward, except two indirect APIs: [`RaftLogReader`] defines the APIs to read logs, and is an also super trait of [`RaftLogStorage`] : - [`try_get_log_entries()`] get log entries in a range; + - [`read_vote()`] read vote; ```ignore trait RaftLogReader { async fn try_get_log_entries>(&mut self, range: RB) -> Result, ...>; + async fn read_vote(&mut self) -> Result>, ...>>; } ``` @@ -372,6 +373,7 @@ Additionally, two test scripts for setting up a cluster are available: [`RaftLogReader`]: `crate::storage::RaftLogReader` [`try_get_log_entries()`]: `crate::storage::RaftLogReader::try_get_log_entries` +[`read_vote()`]: `crate::storage::RaftLogReader::read_vote` [`RaftLogStorage::SnapshotBuilder`]: `crate::storage::RaftLogStorage::SnapshotBuilder` @@ -382,7 +384,6 @@ Additionally, two test scripts for setting up a cluster are available: [`truncate()`]: `crate::storage::RaftLogStorage::truncate` [`purge()`]: `crate::storage::RaftLogStorage::purge` [`save_vote()`]: `crate::storage::RaftLogStorage::save_vote` -[`read_vote()`]: `crate::storage::RaftLogStorage::read_vote` [`get_log_state()`]: `crate::storage::RaftLogStorage::get_log_state` [`get_log_reader()`]: `crate::storage::RaftLogStorage::get_log_reader` diff --git a/openraft/src/storage/mod.rs b/openraft/src/storage/mod.rs index ebeba17c4..893195698 100644 --- a/openraft/src/storage/mod.rs +++ b/openraft/src/storage/mod.rs @@ -29,6 +29,7 @@ use crate::OptionalSync; use crate::RaftTypeConfig; use crate::StorageError; use crate::StoredMembership; +use crate::Vote; #[derive(Debug, Clone, Default, PartialEq, Eq)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize), serde(bound = ""))] @@ -149,6 +150,9 @@ where C: RaftTypeConfig &mut self, range: RB, ) -> Result, StorageError>; + + /// Return the last saved vote by [`RaftLogStorage::save_vote`]. + async fn read_vote(&mut self) -> Result>, StorageError>; } /// A trait defining the interface for a Raft state machine snapshot subsystem. diff --git a/openraft/src/storage/v2.rs b/openraft/src/storage/v2.rs index a4a26e7e2..a1d3d212a 100644 --- a/openraft/src/storage/v2.rs +++ b/openraft/src/storage/v2.rs @@ -66,9 +66,6 @@ where C: RaftTypeConfig /// The vote must be persisted on disk before returning. async fn save_vote(&mut self, vote: &Vote) -> Result<(), StorageError>; - /// Return the last saved vote by [`Self::save_vote`]. - async fn read_vote(&mut self) -> Result>, StorageError>; - /// Saves the last committed log id to storage. /// /// # Optional feature diff --git a/stores/memstore/src/lib.rs b/stores/memstore/src/lib.rs index d2109c765..7a9abc100 100644 --- a/stores/memstore/src/lib.rs +++ b/stores/memstore/src/lib.rs @@ -243,6 +243,10 @@ impl RaftLogReader for Arc { Ok(entries) } + + async fn read_vote(&mut self) -> Result>, StorageError> { + Ok(*self.vote.read().await) + } } impl RaftSnapshotBuilder for Arc { @@ -352,10 +356,6 @@ impl RaftLogStorage for Arc { Ok(()) } - async fn read_vote(&mut self) -> Result>, StorageError> { - Ok(*self.vote.read().await) - } - async fn save_committed(&mut self, committed: Option>) -> Result<(), StorageError> { tracing::debug!(?committed, "save_committed"); let mut c = self.committed.write().await; diff --git a/stores/rocksstore/src/lib.rs b/stores/rocksstore/src/lib.rs index d6ad66546..8cd57a2ff 100644 --- a/stores/rocksstore/src/lib.rs +++ b/stores/rocksstore/src/lib.rs @@ -267,6 +267,10 @@ impl RaftLogReader for RocksLogStore { } Ok(res) } + + async fn read_vote(&mut self) -> Result>, StorageError> { + self.get_meta::() + } } impl RaftSnapshotBuilder for RocksStateMachine { @@ -346,10 +350,6 @@ impl RaftLogStorage for RocksLogStore { Ok(()) } - async fn read_vote(&mut self) -> Result>, StorageError> { - self.get_meta::() - } - async fn get_log_reader(&mut self) -> Self::LogReader { self.clone() } diff --git a/stores/sledstore/src/lib.rs b/stores/sledstore/src/lib.rs index c4a1c3cd0..24785b063 100644 --- a/stores/sledstore/src/lib.rs +++ b/stores/sledstore/src/lib.rs @@ -423,6 +423,10 @@ impl RaftLogReader for Arc { .collect(); logs } + + async fn read_vote(&mut self) -> Result>, StorageError> { + self.get_vote_() + } } impl RaftSnapshotBuilder for Arc { @@ -503,10 +507,6 @@ impl RaftLogStorage for Arc { self.set_vote_(vote).await } - async fn read_vote(&mut self) -> Result>, StorageError> { - self.get_vote_() - } - async fn get_log_reader(&mut self) -> Self::LogReader { self.clone() } diff --git a/tests/tests/fixtures/mod.rs b/tests/tests/fixtures/mod.rs index ea6850d92..b1cb6ec8b 100644 --- a/tests/tests/fixtures/mod.rs +++ b/tests/tests/fixtures/mod.rs @@ -51,6 +51,7 @@ use openraft::MessageSummary; use openraft::RPCTypes; use openraft::Raft; use openraft::RaftLogId; +use openraft::RaftLogReader; use openraft::RaftMetrics; use openraft::RaftState; use openraft::RaftTypeConfig; diff --git a/tests/tests/life_cycle/t50_single_follower_restart.rs b/tests/tests/life_cycle/t50_single_follower_restart.rs index 3df96be65..4b36705e5 100644 --- a/tests/tests/life_cycle/t50_single_follower_restart.rs +++ b/tests/tests/life_cycle/t50_single_follower_restart.rs @@ -4,6 +4,7 @@ use std::time::Duration; use maplit::btreeset; use openraft::storage::RaftLogStorage; use openraft::Config; +use openraft::RaftLogReader; use openraft::ServerState; use openraft::Vote; diff --git a/tests/tests/snapshot_streaming/t33_snapshot_delete_conflict_logs.rs b/tests/tests/snapshot_streaming/t33_snapshot_delete_conflict_logs.rs index 2e3aa8b45..e910fe01d 100644 --- a/tests/tests/snapshot_streaming/t33_snapshot_delete_conflict_logs.rs +++ b/tests/tests/snapshot_streaming/t33_snapshot_delete_conflict_logs.rs @@ -20,6 +20,7 @@ use openraft::Entry; use openraft::EntryPayload; use openraft::LogId; use openraft::Membership; +use openraft::RaftLogReader; use openraft::RaftSnapshotBuilder; use openraft::ServerState; use openraft::SnapshotPolicy;