From fc73dc7a8d7982cfdd6e39edc90338b8ed686dc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=82=8E=E6=B3=BC?= Date: Wed, 20 Mar 2024 12:42:57 +0800 Subject: [PATCH] Change: Consolidate type parameters into `C` Affected types: - Membership types; - RPC request and response types; - Error types; Upgrade tip: To adapt to this change, update type parameters with the single generic `C` constrained by `RaftTypeConfig`: ```rust,ignore // Membership types: MembershipState --> MembershipState EffectiveMembership --> EffectiveMembership Membership --> Membership // RPC types: VoteRequest --> VoteRequest VoteResponse --> VoteResponse AppendEntriesResponse --> AppendEntriesResponse InstallSnapshotResponse --> InstallSnapshotResponse SnapshotResponse --> SnapshotResponse SnapshotMeta --> SnapshotMeta // Errors: InitializeError --> InitializeError NotInMembers --> NotInMembers ClientWriteError --> ClientWriteError CheckIsLeaderError --> CheckIsLeaderError RaftError --> RaftError RPCError --> RPCError RemoteError --> RemoteError ChangeMembershipError --> ChangeMembershipError ForwardToLeader --> ForwardToLeader HigherVote --> HigherVote InProgress --> InProgress LearnerNotFound --> LearnerNotFound NotAllowed --> NotAllowed QuorumNotEnough --> QuorumNotEnough Timeout --> Timeout RejectVoteRequest --> RejectVoteRequest RejectAppendEntries --> RejectAppendEntries Fatal --> Fatal ``` --- cluster_benchmark/tests/benchmark/network.rs | 10 +- cluster_benchmark/tests/benchmark/store.rs | 8 +- .../src/api.rs | 2 +- .../src/lib.rs | 17 +- .../src/network.rs | 8 +- .../src/store.rs | 9 +- .../src/api.rs | 2 +- .../src/lib.rs | 17 +- .../src/network.rs | 8 +- .../src/store.rs | 9 +- .../src/api.rs | 2 +- .../src/lib.rs | 14 +- .../src/network.rs | 8 +- .../src/store.rs | 9 +- examples/raft-kv-memstore/src/lib.rs | 14 +- examples/raft-kv-memstore/src/network/api.rs | 5 +- examples/raft-kv-memstore/src/network/raft.rs | 3 +- .../src/network/raft_network_impl.rs | 10 +- examples/raft-kv-memstore/src/store/mod.rs | 9 +- examples/raft-kv-rocksdb/src/client.rs | 3 +- examples/raft-kv-rocksdb/src/lib.rs | 14 +- examples/raft-kv-rocksdb/src/network/api.rs | 5 +- examples/raft-kv-rocksdb/src/network/raft.rs | 6 +- .../src/network/raft_network_impl.rs | 15 +- examples/raft-kv-rocksdb/src/store.rs | 9 +- openraft/src/core/notify.rs | 2 +- openraft/src/core/raft_core.rs | 44 ++-- openraft/src/core/raft_msg/mod.rs | 19 +- openraft/src/core/sm/response.rs | 4 +- openraft/src/engine/command.rs | 12 +- openraft/src/engine/engine_config.rs | 41 +-- openraft/src/engine/engine_impl.rs | 39 +-- .../following_handler/append_entries_test.rs | 6 +- .../following_handler/commit_entries_test.rs | 6 +- .../do_append_entries_test.rs | 10 +- .../install_snapshot_test.rs | 12 +- .../engine/handler/following_handler/mod.rs | 10 +- .../following_handler/truncate_logs_test.rs | 14 +- .../update_committed_membership_test.rs | 14 +- .../leader_handler/append_entries_test.rs | 18 +- .../src/engine/handler/leader_handler/mod.rs | 2 +- .../leader_handler/send_heartbeat_test.rs | 10 +- .../log_handler/calc_purge_upto_test.rs | 2 +- .../src/engine/handler/log_handler/mod.rs | 2 +- .../handler/log_handler/purge_log_test.rs | 2 +- .../append_membership_test.rs | 22 +- .../engine/handler/replication_handler/mod.rs | 4 +- .../update_matching_test.rs | 10 +- .../handler/server_state_handler/mod.rs | 2 +- .../update_server_state_test.rs | 10 +- .../engine/handler/snapshot_handler/mod.rs | 2 +- .../snapshot_handler/trigger_snapshot_test.rs | 2 +- .../snapshot_handler/update_snapshot_test.rs | 10 +- .../handler/vote_handler/accept_vote_test.rs | 10 +- .../vote_handler/handle_message_vote_test.rs | 6 +- .../src/engine/handler/vote_handler/mod.rs | 6 +- openraft/src/engine/testing.rs | 13 +- .../src/engine/tests/append_entries_test.rs | 14 +- openraft/src/engine/tests/elect_test.rs | 6 +- .../src/engine/tests/handle_vote_req_test.rs | 6 +- .../src/engine/tests/handle_vote_resp_test.rs | 10 +- openraft/src/engine/tests/initialize_test.rs | 8 +- openraft/src/engine/tests/startup_test.rs | 14 +- .../engine/tests/trigger_purge_log_test.rs | 6 +- openraft/src/entry/mod.rs | 4 +- openraft/src/entry/payload.rs | 6 +- openraft/src/entry/traits.rs | 4 +- openraft/src/error.rs | 249 ++++++++---------- openraft/src/error/streaming_error.rs | 8 +- openraft/src/membership/bench/is_quorum.rs | 9 +- .../src/membership/effective_membership.rs | 88 +++---- .../membership/effective_membership_test.rs | 9 +- openraft/src/membership/membership.rs | 121 ++++----- openraft/src/membership/membership_test.rs | 40 +-- openraft/src/membership/stored_membership.rs | 41 ++- openraft/src/metrics/raft_metrics.rs | 6 +- openraft/src/network/network.rs | 21 +- openraft/src/network/snapshot_transport.rs | 8 +- openraft/src/raft/core_state.rs | 14 +- openraft/src/raft/message/append_entries.rs | 15 +- openraft/src/raft/message/client_write.rs | 2 +- openraft/src/raft/message/install_snapshot.rs | 21 +- openraft/src/raft/message/vote.rs | 32 ++- openraft/src/raft/mod.rs | 49 ++-- openraft/src/raft/raft_inner.rs | 8 +- openraft/src/raft/trigger.rs | 8 +- .../membership_state/change_handler.rs | 23 +- .../membership_state/change_handler_test.rs | 17 +- .../membership_state/membership_state_test.rs | 19 +- .../src/raft_state/membership_state/mod.rs | 59 ++--- openraft/src/raft_state/mod.rs | 6 +- .../tests/forward_to_leader_test.rs | 4 +- openraft/src/replication/callbacks.rs | 8 +- openraft/src/replication/mod.rs | 12 +- openraft/src/replication/request.rs | 4 +- openraft/src/storage/helper.rs | 4 +- openraft/src/storage/mod.rs | 40 ++- openraft/src/storage/v2.rs | 4 +- stores/memstore/src/lib.rs | 8 +- stores/rocksstore/src/lib.rs | 8 +- stores/sledstore/src/lib.rs | 15 +- tests/tests/fixtures/mod.rs | 35 ++- 102 files changed, 783 insertions(+), 892 deletions(-) diff --git a/cluster_benchmark/tests/benchmark/network.rs b/cluster_benchmark/tests/benchmark/network.rs index ff16edcce..1f911435f 100644 --- a/cluster_benchmark/tests/benchmark/network.rs +++ b/cluster_benchmark/tests/benchmark/network.rs @@ -26,6 +26,7 @@ use openraft::Raft; use crate::store::LogStore; use crate::store::NodeId; use crate::store::StateMachineStore; +use crate::store::TypeConfig; use crate::store::TypeConfig as MemConfig; pub type BenchRaft = Raft; @@ -100,7 +101,7 @@ impl RaftNetwork for Network { &mut self, rpc: AppendEntriesRequest, _option: RPCOption, - ) -> Result, RPCError>> { + ) -> Result, RPCError>> { let resp = self.target_raft.append_entries(rpc).await.map_err(|e| RemoteError::new(self.target, e))?; Ok(resp) } @@ -109,16 +110,17 @@ impl RaftNetwork for Network { &mut self, rpc: InstallSnapshotRequest, _option: RPCOption, - ) -> Result, RPCError>> { + ) -> Result, RPCError>> + { let resp = self.target_raft.install_snapshot(rpc).await.map_err(|e| RemoteError::new(self.target, e))?; Ok(resp) } async fn vote( &mut self, - rpc: VoteRequest, + rpc: VoteRequest, _option: RPCOption, - ) -> Result, RPCError>> { + ) -> Result, RPCError>> { let resp = self.target_raft.vote(rpc).await.map_err(|e| RemoteError::new(self.target, e))?; Ok(resp) } diff --git a/cluster_benchmark/tests/benchmark/store.rs b/cluster_benchmark/tests/benchmark/store.rs index 69bb62b1a..1b9b2ea6f 100644 --- a/cluster_benchmark/tests/benchmark/store.rs +++ b/cluster_benchmark/tests/benchmark/store.rs @@ -52,14 +52,14 @@ openraft::declare_raft_types!( #[derive(Debug)] pub struct StoredSnapshot { - pub meta: SnapshotMeta, + pub meta: SnapshotMeta, pub data: Vec, } #[derive(Serialize, Deserialize, Debug, Default, Clone)] pub struct StateMachine { pub last_applied_log: Option>, - pub last_membership: StoredMembership, + pub last_membership: StoredMembership, } pub struct LogStore { @@ -250,7 +250,7 @@ impl RaftLogStorage for Arc { impl RaftStateMachine for Arc { async fn applied_state( &mut self, - ) -> Result<(Option>, StoredMembership), StorageError> { + ) -> Result<(Option>, StoredMembership), StorageError> { let sm = self.sm.read().await; Ok((sm.last_applied_log, sm.last_membership.clone())) } @@ -285,7 +285,7 @@ impl RaftStateMachine for Arc { #[tracing::instrument(level = "trace", skip(self, snapshot))] async fn install_snapshot( &mut self, - meta: &SnapshotMeta, + meta: &SnapshotMeta, snapshot: Box>, ) -> Result<(), StorageError> { let new_snapshot = StoredSnapshot { diff --git a/examples/raft-kv-memstore-generic-snapshot-data/src/api.rs b/examples/raft-kv-memstore-generic-snapshot-data/src/api.rs index 62550634f..299a5c0a1 100644 --- a/examples/raft-kv-memstore-generic-snapshot-data/src/api.rs +++ b/examples/raft-kv-memstore-generic-snapshot-data/src/api.rs @@ -31,7 +31,7 @@ pub async fn read(app: &mut App, req: String) -> String { let state_machine = app.state_machine.state_machine.lock().unwrap(); let value = state_machine.data.get(&key).cloned(); - let res: Result>> = + let res: Result>> = Ok(value.unwrap_or_default()); res } diff --git a/examples/raft-kv-memstore-generic-snapshot-data/src/lib.rs b/examples/raft-kv-memstore-generic-snapshot-data/src/lib.rs index b6cb211e9..b3e79435e 100644 --- a/examples/raft-kv-memstore-generic-snapshot-data/src/lib.rs +++ b/examples/raft-kv-memstore-generic-snapshot-data/src/lib.rs @@ -40,7 +40,6 @@ pub type LogStore = store::LogStore; pub type StateMachineStore = store::StateMachineStore; pub mod typ { - use openraft::BasicNode; use crate::NodeId; use crate::TypeConfig; @@ -48,22 +47,22 @@ pub mod typ { pub type Raft = openraft::Raft; pub type Vote = openraft::Vote; - pub type SnapshotMeta = openraft::SnapshotMeta; + pub type SnapshotMeta = openraft::SnapshotMeta; pub type SnapshotData = ::SnapshotData; pub type Snapshot = openraft::Snapshot; pub type Infallible = openraft::error::Infallible; - pub type Fatal = openraft::error::Fatal; - pub type RaftError = openraft::error::RaftError; - pub type RPCError = openraft::error::RPCError>; + pub type Fatal = openraft::error::Fatal; + pub type RaftError = openraft::error::RaftError; + pub type RPCError = openraft::error::RPCError>; pub type StreamingError = openraft::error::StreamingError; pub type RaftMetrics = openraft::RaftMetrics; - pub type ClientWriteError = openraft::error::ClientWriteError; - pub type CheckIsLeaderError = openraft::error::CheckIsLeaderError; - pub type ForwardToLeader = openraft::error::ForwardToLeader; - pub type InitializeError = openraft::error::InitializeError; + pub type ClientWriteError = openraft::error::ClientWriteError; + pub type CheckIsLeaderError = openraft::error::CheckIsLeaderError; + pub type ForwardToLeader = openraft::error::ForwardToLeader; + pub type InitializeError = openraft::error::InitializeError; pub type ClientWriteResponse = openraft::raft::ClientWriteResponse; } diff --git a/examples/raft-kv-memstore-generic-snapshot-data/src/network.rs b/examples/raft-kv-memstore-generic-snapshot-data/src/network.rs index 20842ac8c..55534f3f2 100644 --- a/examples/raft-kv-memstore-generic-snapshot-data/src/network.rs +++ b/examples/raft-kv-memstore-generic-snapshot-data/src/network.rs @@ -41,7 +41,7 @@ impl RaftNetwork for Connection { &mut self, req: AppendEntriesRequest, _option: RPCOption, - ) -> Result, typ::RPCError> { + ) -> Result, typ::RPCError> { let resp = self .router .send(self.target, "/raft/append", req) @@ -57,7 +57,7 @@ impl RaftNetwork for Connection { snapshot: Snapshot, _cancel: impl Future + OptionalSend, _option: RPCOption, - ) -> Result, typ::StreamingError> { + ) -> Result, typ::StreamingError> { let resp = self .router .send::<_, _, typ::Infallible>(self.target, "/raft/snapshot", (vote, snapshot.meta, snapshot.snapshot)) @@ -68,9 +68,9 @@ impl RaftNetwork for Connection { async fn vote( &mut self, - req: VoteRequest, + req: VoteRequest, _option: RPCOption, - ) -> Result, typ::RPCError> { + ) -> Result, typ::RPCError> { let resp = self .router .send(self.target, "/raft/vote", req) diff --git a/examples/raft-kv-memstore-generic-snapshot-data/src/store.rs b/examples/raft-kv-memstore-generic-snapshot-data/src/store.rs index 61a0ee48a..ec825cff7 100644 --- a/examples/raft-kv-memstore-generic-snapshot-data/src/store.rs +++ b/examples/raft-kv-memstore-generic-snapshot-data/src/store.rs @@ -6,7 +6,6 @@ use std::sync::Mutex; use openraft::alias::SnapshotDataOf; use openraft::storage::RaftStateMachine; use openraft::storage::Snapshot; -use openraft::BasicNode; use openraft::Entry; use openraft::EntryPayload; use openraft::LogId; @@ -44,7 +43,7 @@ pub struct Response { #[derive(Debug)] pub struct StoredSnapshot { - pub meta: SnapshotMeta, + pub meta: SnapshotMeta, /// The data of the state machine at the time of this snapshot. pub data: Box, @@ -57,7 +56,7 @@ pub struct StoredSnapshot { pub struct StateMachineData { pub last_applied: Option>, - pub last_membership: StoredMembership, + pub last_membership: StoredMembership, /// Application data. pub data: BTreeMap, @@ -132,7 +131,7 @@ impl RaftStateMachine for Arc { async fn applied_state( &mut self, - ) -> Result<(Option>, StoredMembership), StorageError> { + ) -> Result<(Option>, StoredMembership), StorageError> { let state_machine = self.state_machine.lock().unwrap(); Ok((state_machine.last_applied, state_machine.last_membership.clone())) } @@ -176,7 +175,7 @@ impl RaftStateMachine for Arc { #[tracing::instrument(level = "trace", skip(self, snapshot))] async fn install_snapshot( &mut self, - meta: &SnapshotMeta, + meta: &SnapshotMeta, snapshot: Box>, ) -> Result<(), StorageError> { tracing::info!("install snapshot"); diff --git a/examples/raft-kv-memstore-opendal-snapshot-data/src/api.rs b/examples/raft-kv-memstore-opendal-snapshot-data/src/api.rs index 62550634f..299a5c0a1 100644 --- a/examples/raft-kv-memstore-opendal-snapshot-data/src/api.rs +++ b/examples/raft-kv-memstore-opendal-snapshot-data/src/api.rs @@ -31,7 +31,7 @@ pub async fn read(app: &mut App, req: String) -> String { let state_machine = app.state_machine.state_machine.lock().unwrap(); let value = state_machine.data.get(&key).cloned(); - let res: Result>> = + let res: Result>> = Ok(value.unwrap_or_default()); res } diff --git a/examples/raft-kv-memstore-opendal-snapshot-data/src/lib.rs b/examples/raft-kv-memstore-opendal-snapshot-data/src/lib.rs index ca46be7f3..290492278 100644 --- a/examples/raft-kv-memstore-opendal-snapshot-data/src/lib.rs +++ b/examples/raft-kv-memstore-opendal-snapshot-data/src/lib.rs @@ -39,7 +39,6 @@ pub type LogStore = store::LogStore; pub type StateMachineStore = store::StateMachineStore; pub mod typ { - use openraft::BasicNode; use crate::NodeId; use crate::TypeConfig; @@ -47,22 +46,22 @@ pub mod typ { pub type Raft = openraft::Raft; pub type Vote = openraft::Vote; - pub type SnapshotMeta = openraft::SnapshotMeta; + pub type SnapshotMeta = openraft::SnapshotMeta; pub type SnapshotData = ::SnapshotData; pub type Snapshot = openraft::Snapshot; pub type Infallible = openraft::error::Infallible; - pub type Fatal = openraft::error::Fatal; - pub type RaftError = openraft::error::RaftError; - pub type RPCError = openraft::error::RPCError>; + pub type Fatal = openraft::error::Fatal; + pub type RaftError = openraft::error::RaftError; + pub type RPCError = openraft::error::RPCError>; pub type StreamingError = openraft::error::StreamingError; pub type RaftMetrics = openraft::RaftMetrics; - pub type ClientWriteError = openraft::error::ClientWriteError; - pub type CheckIsLeaderError = openraft::error::CheckIsLeaderError; - pub type ForwardToLeader = openraft::error::ForwardToLeader; - pub type InitializeError = openraft::error::InitializeError; + pub type ClientWriteError = openraft::error::ClientWriteError; + pub type CheckIsLeaderError = openraft::error::CheckIsLeaderError; + pub type ForwardToLeader = openraft::error::ForwardToLeader; + pub type InitializeError = openraft::error::InitializeError; pub type ClientWriteResponse = openraft::raft::ClientWriteResponse; } diff --git a/examples/raft-kv-memstore-opendal-snapshot-data/src/network.rs b/examples/raft-kv-memstore-opendal-snapshot-data/src/network.rs index 20842ac8c..55534f3f2 100644 --- a/examples/raft-kv-memstore-opendal-snapshot-data/src/network.rs +++ b/examples/raft-kv-memstore-opendal-snapshot-data/src/network.rs @@ -41,7 +41,7 @@ impl RaftNetwork for Connection { &mut self, req: AppendEntriesRequest, _option: RPCOption, - ) -> Result, typ::RPCError> { + ) -> Result, typ::RPCError> { let resp = self .router .send(self.target, "/raft/append", req) @@ -57,7 +57,7 @@ impl RaftNetwork for Connection { snapshot: Snapshot, _cancel: impl Future + OptionalSend, _option: RPCOption, - ) -> Result, typ::StreamingError> { + ) -> Result, typ::StreamingError> { let resp = self .router .send::<_, _, typ::Infallible>(self.target, "/raft/snapshot", (vote, snapshot.meta, snapshot.snapshot)) @@ -68,9 +68,9 @@ impl RaftNetwork for Connection { async fn vote( &mut self, - req: VoteRequest, + req: VoteRequest, _option: RPCOption, - ) -> Result, typ::RPCError> { + ) -> Result, typ::RPCError> { let resp = self .router .send(self.target, "/raft/vote", req) diff --git a/examples/raft-kv-memstore-opendal-snapshot-data/src/store.rs b/examples/raft-kv-memstore-opendal-snapshot-data/src/store.rs index 3848bcc76..b99b392fd 100644 --- a/examples/raft-kv-memstore-opendal-snapshot-data/src/store.rs +++ b/examples/raft-kv-memstore-opendal-snapshot-data/src/store.rs @@ -7,7 +7,6 @@ use opendal::Operator; use openraft::alias::SnapshotDataOf; use openraft::storage::RaftStateMachine; use openraft::storage::Snapshot; -use openraft::BasicNode; use openraft::Entry; use openraft::EntryPayload; use openraft::LogId; @@ -47,7 +46,7 @@ pub struct Response { #[derive(Debug)] pub struct StoredSnapshot { - pub meta: SnapshotMeta, + pub meta: SnapshotMeta, /// The data of the state machine at the time of this snapshot. pub data: Box, @@ -60,7 +59,7 @@ pub struct StoredSnapshot { pub struct StateMachineData { pub last_applied: Option>, - pub last_membership: StoredMembership, + pub last_membership: StoredMembership, /// Application data. pub data: BTreeMap, @@ -153,7 +152,7 @@ impl RaftStateMachine for Arc { async fn applied_state( &mut self, - ) -> Result<(Option>, StoredMembership), StorageError> { + ) -> Result<(Option>, StoredMembership), StorageError> { let state_machine = self.state_machine.lock().unwrap(); Ok((state_machine.last_applied, state_machine.last_membership.clone())) } @@ -197,7 +196,7 @@ impl RaftStateMachine for Arc { #[tracing::instrument(level = "trace", skip(self, snapshot))] async fn install_snapshot( &mut self, - meta: &SnapshotMeta, + meta: &SnapshotMeta, snapshot: Box>, ) -> Result<(), StorageError> { tracing::info!("install snapshot"); diff --git a/examples/raft-kv-memstore-singlethreaded/src/api.rs b/examples/raft-kv-memstore-singlethreaded/src/api.rs index 2ce84589b..d54a1e755 100644 --- a/examples/raft-kv-memstore-singlethreaded/src/api.rs +++ b/examples/raft-kv-memstore-singlethreaded/src/api.rs @@ -30,7 +30,7 @@ pub async fn read(app: &mut App, req: String) -> String { let state_machine = app.state_machine.state_machine.borrow(); let value = state_machine.data.get(&key).cloned(); - let res: Result>> = + let res: Result>> = Ok(value.unwrap_or_default()); res } diff --git a/examples/raft-kv-memstore-singlethreaded/src/lib.rs b/examples/raft-kv-memstore-singlethreaded/src/lib.rs index c6ff3323c..18e6df8da 100644 --- a/examples/raft-kv-memstore-singlethreaded/src/lib.rs +++ b/examples/raft-kv-memstore-singlethreaded/src/lib.rs @@ -62,20 +62,18 @@ pub type StateMachineStore = store::StateMachineStore; pub type Raft = openraft::Raft; pub mod typ { - use openraft::BasicNode; - use crate::NodeId; use crate::TypeConfig; - pub type RaftError = openraft::error::RaftError; - pub type RPCError = openraft::error::RPCError>; + pub type RaftError = openraft::error::RaftError; + pub type RPCError = openraft::error::RPCError>; pub type RaftMetrics = openraft::RaftMetrics; - pub type ClientWriteError = openraft::error::ClientWriteError; - pub type CheckIsLeaderError = openraft::error::CheckIsLeaderError; - pub type ForwardToLeader = openraft::error::ForwardToLeader; - pub type InitializeError = openraft::error::InitializeError; + pub type ClientWriteError = openraft::error::ClientWriteError; + pub type CheckIsLeaderError = openraft::error::CheckIsLeaderError; + pub type ForwardToLeader = openraft::error::ForwardToLeader; + pub type InitializeError = openraft::error::InitializeError; pub type ClientWriteResponse = openraft::raft::ClientWriteResponse; } diff --git a/examples/raft-kv-memstore-singlethreaded/src/network.rs b/examples/raft-kv-memstore-singlethreaded/src/network.rs index c7c7e39fa..cf0e1808b 100644 --- a/examples/raft-kv-memstore-singlethreaded/src/network.rs +++ b/examples/raft-kv-memstore-singlethreaded/src/network.rs @@ -37,7 +37,7 @@ impl RaftNetwork for Connection { &mut self, req: AppendEntriesRequest, _option: RPCOption, - ) -> Result, typ::RPCError> { + ) -> Result, typ::RPCError> { let resp = self .router .send(self.target, "/raft/append", req) @@ -50,7 +50,7 @@ impl RaftNetwork for Connection { &mut self, req: InstallSnapshotRequest, _option: RPCOption, - ) -> Result, typ::RPCError> { + ) -> Result, typ::RPCError> { let resp = self .router .send(self.target, "/raft/snapshot", req) @@ -61,9 +61,9 @@ impl RaftNetwork for Connection { async fn vote( &mut self, - req: VoteRequest, + req: VoteRequest, _option: RPCOption, - ) -> Result, typ::RPCError> { + ) -> Result, typ::RPCError> { let resp = self .router .send(self.target, "/raft/vote", req) diff --git a/examples/raft-kv-memstore-singlethreaded/src/store.rs b/examples/raft-kv-memstore-singlethreaded/src/store.rs index c50fbbdcd..3739f2c12 100644 --- a/examples/raft-kv-memstore-singlethreaded/src/store.rs +++ b/examples/raft-kv-memstore-singlethreaded/src/store.rs @@ -12,7 +12,6 @@ use openraft::storage::LogState; use openraft::storage::RaftLogStorage; use openraft::storage::RaftStateMachine; use openraft::storage::Snapshot; -use openraft::BasicNode; use openraft::Entry; use openraft::EntryPayload; use openraft::LogId; @@ -74,7 +73,7 @@ pub struct Response { #[derive(Debug)] pub struct StoredSnapshot { - pub meta: SnapshotMeta, + pub meta: SnapshotMeta, /// The data of the state machine at the time of this snapshot. pub data: Vec, @@ -87,7 +86,7 @@ pub struct StoredSnapshot { pub struct StateMachineData { pub last_applied: Option>, - pub last_membership: StoredMembership, + pub last_membership: StoredMembership, /// Application data. pub data: BTreeMap, @@ -186,7 +185,7 @@ impl RaftStateMachine for Rc { async fn applied_state( &mut self, - ) -> Result<(Option>, StoredMembership), StorageError> { + ) -> Result<(Option>, StoredMembership), StorageError> { let state_machine = self.state_machine.borrow(); Ok((state_machine.last_applied, state_machine.last_membership.clone())) } @@ -230,7 +229,7 @@ impl RaftStateMachine for Rc { #[tracing::instrument(level = "trace", skip(self, snapshot))] async fn install_snapshot( &mut self, - meta: &SnapshotMeta, + meta: &SnapshotMeta, snapshot: Box>, ) -> Result<(), StorageError> { tracing::info!( diff --git a/examples/raft-kv-memstore/src/lib.rs b/examples/raft-kv-memstore/src/lib.rs index 4e3bac8f1..5956979c8 100644 --- a/examples/raft-kv-memstore/src/lib.rs +++ b/examples/raft-kv-memstore/src/lib.rs @@ -44,18 +44,16 @@ pub type StateMachineStore = store::StateMachineStore; pub type Raft = openraft::Raft; pub mod typ { - use openraft::BasicNode; - use crate::NodeId; use crate::TypeConfig; - pub type RaftError = openraft::error::RaftError; - pub type RPCError = openraft::error::RPCError>; + pub type RaftError = openraft::error::RaftError; + pub type RPCError = openraft::error::RPCError>; - pub type ClientWriteError = openraft::error::ClientWriteError; - pub type CheckIsLeaderError = openraft::error::CheckIsLeaderError; - pub type ForwardToLeader = openraft::error::ForwardToLeader; - pub type InitializeError = openraft::error::InitializeError; + pub type ClientWriteError = openraft::error::ClientWriteError; + pub type CheckIsLeaderError = openraft::error::CheckIsLeaderError; + pub type ForwardToLeader = openraft::error::ForwardToLeader; + pub type InitializeError = openraft::error::InitializeError; pub type ClientWriteResponse = openraft::raft::ClientWriteResponse; } diff --git a/examples/raft-kv-memstore/src/network/api.rs b/examples/raft-kv-memstore/src/network/api.rs index 0cf9a0204..7fe6407d1 100644 --- a/examples/raft-kv-memstore/src/network/api.rs +++ b/examples/raft-kv-memstore/src/network/api.rs @@ -5,12 +5,11 @@ use actix_web::Responder; use openraft::error::CheckIsLeaderError; use openraft::error::Infallible; use openraft::error::RaftError; -use openraft::BasicNode; use web::Json; use crate::app::App; use crate::store::Request; -use crate::NodeId; +use crate::TypeConfig; /** * Application API @@ -47,7 +46,7 @@ pub async fn consistent_read(app: Data, req: Json) -> actix_web::Re let key = req.0; let value = state_machine.data.get(&key).cloned(); - let res: Result>> = + let res: Result>> = Ok(value.unwrap_or_default()); Ok(Json(res)) } diff --git a/examples/raft-kv-memstore/src/network/raft.rs b/examples/raft-kv-memstore/src/network/raft.rs index aec0f3e94..b34843bdd 100644 --- a/examples/raft-kv-memstore/src/network/raft.rs +++ b/examples/raft-kv-memstore/src/network/raft.rs @@ -7,13 +7,12 @@ use openraft::raft::InstallSnapshotRequest; use openraft::raft::VoteRequest; use crate::app::App; -use crate::NodeId; use crate::TypeConfig; // --- Raft communication #[post("/raft-vote")] -pub async fn vote(app: Data, req: Json>) -> actix_web::Result { +pub async fn vote(app: Data, req: Json>) -> actix_web::Result { let res = app.raft.vote(req.0).await; Ok(Json(res)) } diff --git a/examples/raft-kv-memstore/src/network/raft_network_impl.rs b/examples/raft-kv-memstore/src/network/raft_network_impl.rs index 5a4ca8dbc..561b38f5f 100644 --- a/examples/raft-kv-memstore/src/network/raft_network_impl.rs +++ b/examples/raft-kv-memstore/src/network/raft_network_impl.rs @@ -28,7 +28,7 @@ impl Network { target_node: &BasicNode, uri: &str, req: Req, - ) -> Result> + ) -> Result> where Req: Serialize, Err: std::error::Error + DeserializeOwned, @@ -85,7 +85,7 @@ impl RaftNetwork for NetworkConnection { &mut self, req: AppendEntriesRequest, _option: RPCOption, - ) -> Result, typ::RPCError> { + ) -> Result, typ::RPCError> { self.owner.send_rpc(self.target, &self.target_node, "raft-append", req).await } @@ -93,15 +93,15 @@ impl RaftNetwork for NetworkConnection { &mut self, req: InstallSnapshotRequest, _option: RPCOption, - ) -> Result, typ::RPCError> { + ) -> Result, typ::RPCError> { self.owner.send_rpc(self.target, &self.target_node, "raft-snapshot", req).await } async fn vote( &mut self, - req: VoteRequest, + req: VoteRequest, _option: RPCOption, - ) -> Result, typ::RPCError> { + ) -> Result, typ::RPCError> { self.owner.send_rpc(self.target, &self.target_node, "raft-vote", req).await } } diff --git a/examples/raft-kv-memstore/src/store/mod.rs b/examples/raft-kv-memstore/src/store/mod.rs index afadbaa64..14614ee57 100644 --- a/examples/raft-kv-memstore/src/store/mod.rs +++ b/examples/raft-kv-memstore/src/store/mod.rs @@ -7,7 +7,6 @@ use std::sync::Mutex; use openraft::alias::SnapshotDataOf; use openraft::storage::RaftStateMachine; use openraft::storage::Snapshot; -use openraft::BasicNode; use openraft::Entry; use openraft::EntryPayload; use openraft::LogId; @@ -51,7 +50,7 @@ pub struct Response { #[derive(Debug)] pub struct StoredSnapshot { - pub meta: SnapshotMeta, + pub meta: SnapshotMeta, /// The data of the state machine at the time of this snapshot. pub data: Vec, @@ -64,7 +63,7 @@ pub struct StoredSnapshot { pub struct StateMachineData { pub last_applied_log: Option>, - pub last_membership: StoredMembership, + pub last_membership: StoredMembership, /// Application data. pub data: BTreeMap, @@ -139,7 +138,7 @@ impl RaftStateMachine for Arc { async fn applied_state( &mut self, - ) -> Result<(Option>, StoredMembership), StorageError> { + ) -> Result<(Option>, StoredMembership), StorageError> { let state_machine = self.state_machine.read().await; Ok((state_machine.last_applied_log, state_machine.last_membership.clone())) } @@ -183,7 +182,7 @@ impl RaftStateMachine for Arc { #[tracing::instrument(level = "trace", skip(self, snapshot))] async fn install_snapshot( &mut self, - meta: &SnapshotMeta, + meta: &SnapshotMeta, snapshot: Box>, ) -> Result<(), StorageError> { tracing::info!( diff --git a/examples/raft-kv-rocksdb/src/client.rs b/examples/raft-kv-rocksdb/src/client.rs index 557ee368c..fd81d6fc8 100644 --- a/examples/raft-kv-rocksdb/src/client.rs +++ b/examples/raft-kv-rocksdb/src/client.rs @@ -14,7 +14,6 @@ use serde::Deserialize; use serde::Serialize; use crate::typ; -use crate::Node; use crate::NodeId; use crate::Request; use crate::TypeConfig; @@ -119,7 +118,7 @@ impl ExampleClient { &self, uri: &str, req: Option<&Req>, - ) -> Result> + ) -> Result> where Req: Serialize + 'static, Resp: Serialize + DeserializeOwned, diff --git a/examples/raft-kv-rocksdb/src/lib.rs b/examples/raft-kv-rocksdb/src/lib.rs index de2fb90b7..776a948ef 100644 --- a/examples/raft-kv-rocksdb/src/lib.rs +++ b/examples/raft-kv-rocksdb/src/lib.rs @@ -54,19 +54,17 @@ openraft::declare_raft_types!( pub mod typ { use openraft::error::Infallible; - use crate::Node; - use crate::NodeId; use crate::TypeConfig; pub type Entry = openraft::Entry; - pub type RaftError = openraft::error::RaftError; - pub type RPCError = openraft::error::RPCError>; + pub type RaftError = openraft::error::RaftError; + pub type RPCError = openraft::error::RPCError>; - pub type ClientWriteError = openraft::error::ClientWriteError; - pub type CheckIsLeaderError = openraft::error::CheckIsLeaderError; - pub type ForwardToLeader = openraft::error::ForwardToLeader; - pub type InitializeError = openraft::error::InitializeError; + pub type ClientWriteError = openraft::error::ClientWriteError; + pub type CheckIsLeaderError = openraft::error::CheckIsLeaderError; + pub type ForwardToLeader = openraft::error::ForwardToLeader; + pub type InitializeError = openraft::error::InitializeError; pub type ClientWriteResponse = openraft::raft::ClientWriteResponse; } diff --git a/examples/raft-kv-rocksdb/src/network/api.rs b/examples/raft-kv-rocksdb/src/network/api.rs index 53e3f6485..b8208e7e1 100644 --- a/examples/raft-kv-rocksdb/src/network/api.rs +++ b/examples/raft-kv-rocksdb/src/network/api.rs @@ -8,9 +8,8 @@ use tide::Response; use tide::StatusCode; use crate::app::App; -use crate::Node; -use crate::NodeId; use crate::Server; +use crate::TypeConfig; pub fn rest(app: &mut Server) { let mut api = app.at("/api"); @@ -52,7 +51,7 @@ async fn consistent_read(mut req: Request>) -> tide::Result { let value = kvs.get(&key); - let res: Result> = Ok(value.cloned().unwrap_or_default()); + let res: Result> = Ok(value.cloned().unwrap_or_default()); Ok(Response::builder(StatusCode::Ok).body(Body::from_json(&res)?).build()) } e => Ok(Response::builder(StatusCode::Ok).body(Body::from_json(&e)?).build()), diff --git a/examples/raft-kv-rocksdb/src/network/raft.rs b/examples/raft-kv-rocksdb/src/network/raft.rs index 2fd4aa5df..71ec68eef 100644 --- a/examples/raft-kv-rocksdb/src/network/raft.rs +++ b/examples/raft-kv-rocksdb/src/network/raft.rs @@ -23,7 +23,7 @@ impl Raft { } #[export_method] - pub async fn vote(&self, vote: VoteRequest) -> Result, toy_rpc::Error> { + pub async fn vote(&self, vote: VoteRequest) -> Result, toy_rpc::Error> { self.app.raft.vote(vote).await.map_err(|e| toy_rpc::Error::Internal(Box::new(e))) } @@ -31,7 +31,7 @@ impl Raft { pub async fn append( &self, req: AppendEntriesRequest, - ) -> Result, toy_rpc::Error> { + ) -> Result, toy_rpc::Error> { tracing::debug!("handle append"); self.app.raft.append_entries(req).await.map_err(|e| toy_rpc::Error::Internal(Box::new(e))) } @@ -40,7 +40,7 @@ impl Raft { pub async fn snapshot( &self, req: InstallSnapshotRequest, - ) -> Result, toy_rpc::Error> { + ) -> Result, toy_rpc::Error> { self.app.raft.install_snapshot(req).await.map_err(|e| toy_rpc::Error::Internal(Box::new(e))) } } diff --git a/examples/raft-kv-rocksdb/src/network/raft_network_impl.rs b/examples/raft-kv-rocksdb/src/network/raft_network_impl.rs index 40feacdcc..bb9dafda6 100644 --- a/examples/raft-kv-rocksdb/src/network/raft_network_impl.rs +++ b/examples/raft-kv-rocksdb/src/network/raft_network_impl.rs @@ -51,7 +51,7 @@ pub struct NetworkConnection { impl NetworkConnection { async fn c( &mut self, - ) -> Result<&Client, RPCError> { + ) -> Result<&Client, RPCError> { if self.client.is_none() { self.client = Client::dial_websocket(&self.addr).await.ok(); } @@ -70,7 +70,7 @@ impl Display for ErrWrap { impl std::error::Error for ErrWrap {} -fn to_error(e: toy_rpc::Error, target: NodeId) -> RPCError { +fn to_error(e: toy_rpc::Error, target: NodeId) -> RPCError { match e { toy_rpc::Error::IoError(e) => RPCError::Network(NetworkError::new(&e)), toy_rpc::Error::ParseError(e) => RPCError::Network(NetworkError::new(&ErrWrap(e))), @@ -97,7 +97,7 @@ fn to_error(e: toy_rpc::Error, target: N // // --> src/network/raft_network_impl.rs:99:91 // | -// 99 | ) -> Result, RPCError>> +// 99 | ) -> Result, RPCError>> // { // | ___________________________________________________________________________________________^ // 100 | | tracing::debug!(req = debug(&req), "append_entries"); @@ -117,7 +117,7 @@ impl RaftNetwork for NetworkConnection { &mut self, req: AppendEntriesRequest, _option: RPCOption, - ) -> Result, RPCError>> { + ) -> Result, RPCError>> { tracing::debug!(req = debug(&req), "append_entries"); let c = self.c().await?; @@ -134,7 +134,8 @@ impl RaftNetwork for NetworkConnection { &mut self, req: InstallSnapshotRequest, _option: RPCOption, - ) -> Result, RPCError>> { + ) -> Result, RPCError>> + { tracing::debug!(req = debug(&req), "install_snapshot"); self.c().await?.raft().snapshot(req).await.map_err(|e| to_error(e, self.target)) } @@ -142,9 +143,9 @@ impl RaftNetwork for NetworkConnection { #[tracing::instrument(level = "debug", skip_all, err(Debug))] async fn vote( &mut self, - req: VoteRequest, + req: VoteRequest, _option: RPCOption, - ) -> Result, RPCError>> { + ) -> Result, RPCError>> { tracing::debug!(req = debug(&req), "vote"); self.c().await?.raft().vote(req).await.map_err(|e| to_error(e, self.target)) } diff --git a/examples/raft-kv-rocksdb/src/store.rs b/examples/raft-kv-rocksdb/src/store.rs index 4c1bac8b9..d4c9f051e 100644 --- a/examples/raft-kv-rocksdb/src/store.rs +++ b/examples/raft-kv-rocksdb/src/store.rs @@ -37,7 +37,6 @@ use serde::Serialize; use tokio::sync::RwLock; use crate::typ; -use crate::Node; use crate::NodeId; use crate::SnapshotData; use crate::TypeConfig; @@ -68,7 +67,7 @@ pub struct Response { #[derive(Serialize, Deserialize, Debug, Clone)] pub struct StoredSnapshot { - pub meta: SnapshotMeta, + pub meta: SnapshotMeta, /// The data of the state machine at the time of this snapshot. pub data: Vec, @@ -92,7 +91,7 @@ pub struct StateMachineStore { pub struct StateMachineData { pub last_applied_log_id: Option>, - pub last_membership: StoredMembership, + pub last_membership: StoredMembership, /// State built from applying the raft logs pub kvs: Arc>>, @@ -201,7 +200,7 @@ impl RaftStateMachine for StateMachineStore { async fn applied_state( &mut self, - ) -> Result<(Option>, StoredMembership), StorageError> { + ) -> Result<(Option>, StoredMembership), StorageError> { Ok((self.data.last_applied_log_id, self.data.last_membership.clone())) } @@ -249,7 +248,7 @@ impl RaftStateMachine for StateMachineStore { async fn install_snapshot( &mut self, - meta: &SnapshotMeta, + meta: &SnapshotMeta, snapshot: Box, ) -> Result<(), StorageError> { let new_snapshot = StoredSnapshot { diff --git a/openraft/src/core/notify.rs b/openraft/src/core/notify.rs index f68e23ad6..7e67e17bd 100644 --- a/openraft/src/core/notify.rs +++ b/openraft/src/core/notify.rs @@ -11,7 +11,7 @@ where C: RaftTypeConfig { VoteResponse { target: C::NodeId, - resp: VoteResponse, + resp: VoteResponse, /// The candidate that sent the vote request. /// diff --git a/openraft/src/core/raft_core.rs b/openraft/src/core/raft_core.rs index f2afc83aa..b5527ec23 100644 --- a/openraft/src/core/raft_core.rs +++ b/openraft/src/core/raft_core.rs @@ -92,8 +92,6 @@ use crate::Instant; use crate::LogId; use crate::Membership; use crate::MessageSummary; -use crate::Node; -use crate::NodeId; use crate::OptionalSend; use crate::RaftTypeConfig; use crate::StorageError; @@ -102,13 +100,13 @@ use crate::Vote; /// A temp struct to hold the data for a node that is being applied. #[derive(Debug)] -pub(crate) struct ApplyingEntry { - log_id: LogId, - membership: Option>, +pub(crate) struct ApplyingEntry { + log_id: LogId, + membership: Option>, } -impl ApplyingEntry { - pub(crate) fn new(log_id: LogId, membership: Option>) -> Self { +impl ApplyingEntry { + pub(crate) fn new(log_id: LogId, membership: Option>) -> Self { Self { log_id, membership } } } @@ -118,7 +116,7 @@ pub(crate) struct ApplyResult { pub(crate) since: u64, pub(crate) end: u64, pub(crate) last_applied: LogId, - pub(crate) applying_entries: Vec>, + pub(crate) applying_entries: Vec>, pub(crate) apply_results: Vec, } @@ -217,7 +215,7 @@ where SM: RaftStateMachine, { /// The main loop of the Raft protocol. - pub(crate) async fn main(mut self, rx_shutdown: OneshotReceiverOf) -> Result<(), Fatal> { + pub(crate) async fn main(mut self, rx_shutdown: OneshotReceiverOf) -> Result<(), Fatal> { let span = tracing::span!(parent: &self.span, Level::DEBUG, "main"); let res = self.do_main(rx_shutdown).instrument(span).await; @@ -241,7 +239,7 @@ where } #[tracing::instrument(level="trace", skip_all, fields(id=display(self.id), cluster=%self.config.cluster_name))] - async fn do_main(&mut self, rx_shutdown: OneshotReceiverOf) -> Result<(), Fatal> { + async fn do_main(&mut self, rx_shutdown: OneshotReceiverOf) -> Result<(), Fatal> { tracing::debug!("raft node is initializing"); self.engine.startup(); @@ -434,7 +432,7 @@ where &mut self, changes: ChangeMembers, retain: bool, - tx: ResultSender, ClientWriteError>, + tx: ResultSender, ClientWriteError>, ) { let res = self.engine.state.membership_state.change_handler().apply(changes, retain); let new_membership = match res { @@ -487,9 +485,7 @@ where pub fn send_heartbeat(&mut self, emitter: impl Display) -> bool { tracing::debug!(now = debug(InstantOf::::now()), "send_heartbeat"); - let mut lh = if let Some((lh, _)) = - self.engine.get_leader_handler_or_reject::<(), ClientWriteError>(None) - { + let mut lh = if let Some((lh, _)) = self.engine.get_leader_handler_or_reject::<(), ClientWriteError>(None) { lh } else { tracing::debug!( @@ -606,7 +602,7 @@ where pub(crate) fn handle_initialize( &mut self, member_nodes: BTreeMap, - tx: ResultSender>, + tx: ResultSender>, ) { tracing::debug!(member_nodes = debug(&member_nodes), "{}", func_name!()); @@ -630,7 +626,7 @@ where /// Reject a request due to the Raft node being in a state which prohibits the request. #[tracing::instrument(level = "trace", skip(self, tx))] pub(crate) fn reject_with_forward_to_leader(&self, tx: ResultSender) - where E: From> + OptionalSend { + where E: From> + OptionalSend { let mut leader_id = self.current_leader(); let leader_node = self.get_leader_node(leader_id); @@ -768,7 +764,7 @@ where /// Send result of applying a log entry to its client. #[tracing::instrument(level = "debug", skip_all)] - pub(super) fn send_response(entry: ApplyingEntry, resp: C::R, tx: Option>) { + pub(super) fn send_response(entry: ApplyingEntry, resp: C::R, tx: Option>) { tracing::debug!(entry = debug(&entry), "send_response"); let tx = match tx { @@ -888,7 +884,7 @@ where /// Run an event handling loop #[tracing::instrument(level="debug", skip_all, fields(id=display(self.id)))] - async fn runtime_loop(&mut self, mut rx_shutdown: OneshotReceiverOf) -> Result<(), Fatal> { + async fn runtime_loop(&mut self, mut rx_shutdown: OneshotReceiverOf) -> Result<(), Fatal> { // Ratio control the ratio of number of RaftMsg to process to number of Notify to process. let mut balancer = Balancer::new(10_000); @@ -959,7 +955,7 @@ where /// /// It returns the number of processed message. /// If the input channel is closed, it returns `Fatal::Stopped`. - async fn process_raft_msg(&mut self, at_most: u64) -> Result> { + async fn process_raft_msg(&mut self, at_most: u64) -> Result> { for i in 0..at_most { let res = self.rx_api.try_recv(); let msg = match res { @@ -994,7 +990,7 @@ where /// /// It returns the number of processed notifications. /// If the input channel is closed, it returns `Fatal::Stopped`. - async fn process_notify(&mut self, at_most: u64) -> Result> { + async fn process_notify(&mut self, at_most: u64) -> Result> { for i in 0..at_most { let res = self.rx_notify.try_recv(); let notify = match res { @@ -1027,7 +1023,7 @@ where /// Spawn parallel vote requests to all cluster members. #[tracing::instrument(level = "trace", skip_all, fields(vote=vote_req.summary()))] - async fn spawn_parallel_vote_requests(&mut self, vote_req: &VoteRequest) { + async fn spawn_parallel_vote_requests(&mut self, vote_req: &VoteRequest) { let members = self.engine.state.membership_state.effective().voter_ids(); let vote = vote_req.vote; @@ -1058,7 +1054,7 @@ where Ok(res) => res, Err(_timeout) => { - let timeout_err = Timeout { + let timeout_err = Timeout:: { action: RPCTypes::Vote, id, target, @@ -1090,7 +1086,7 @@ where } #[tracing::instrument(level = "debug", skip_all)] - pub(super) fn handle_vote_request(&mut self, req: VoteRequest, tx: VoteTx) { + pub(super) fn handle_vote_request(&mut self, req: VoteRequest, tx: VoteTx) { tracing::info!(req = display(req.summary()), func = func_name!()); let resp = self.engine.handle_vote_req(req); @@ -1203,7 +1199,7 @@ where // TODO: Make this method non-async. It does not need to run any async command in it. #[tracing::instrument(level = "debug", skip_all, fields(state = debug(self.engine.state.server_state), id=display(self.id)))] - pub(crate) fn handle_notify(&mut self, notify: Notify) -> Result<(), Fatal> { + pub(crate) fn handle_notify(&mut self, notify: Notify) -> Result<(), Fatal> { tracing::debug!("recv from rx_notify: {}", notify.summary()); match notify { diff --git a/openraft/src/core/raft_msg/mod.rs b/openraft/src/core/raft_msg/mod.rs index 1b2c5d66d..72ddfbc7e 100644 --- a/openraft/src/core/raft_msg/mod.rs +++ b/openraft/src/core/raft_msg/mod.rs @@ -13,8 +13,6 @@ use crate::raft::SnapshotResponse; use crate::raft::VoteRequest; use crate::raft::VoteResponse; use crate::type_config::alias::LogIdOf; -use crate::type_config::alias::NodeIdOf; -use crate::type_config::alias::NodeOf; use crate::type_config::alias::OneshotReceiverOf; use crate::type_config::alias::OneshotSenderOf; use crate::type_config::alias::SnapshotDataOf; @@ -32,17 +30,16 @@ pub(crate) type ResultSender = OneshotSenderOf = OneshotReceiverOf>; /// TX for Vote Response -pub(crate) type VoteTx = ResultSender>>; +pub(crate) type VoteTx = ResultSender>; /// TX for Append Entries Response -pub(crate) type AppendEntriesTx = ResultSender>>; +pub(crate) type AppendEntriesTx = ResultSender>; /// TX for Client Write Response -pub(crate) type ClientWriteTx = ResultSender, ClientWriteError, NodeOf>>; +pub(crate) type ClientWriteTx = ResultSender, ClientWriteError>; /// TX for Linearizable Read Response -pub(crate) type ClientReadTx = - ResultSender>, Option>), CheckIsLeaderError, NodeOf>>; +pub(crate) type ClientReadTx = ResultSender>, Option>), CheckIsLeaderError>; /// A message sent by application to the [`RaftCore`]. /// @@ -56,14 +53,14 @@ where C: RaftTypeConfig }, RequestVote { - rpc: VoteRequest, + rpc: VoteRequest, tx: VoteTx, }, InstallFullSnapshot { vote: Vote, snapshot: Snapshot, - tx: ResultSender>, + tx: ResultSender>, }, /// Begin receiving a snapshot from the leader. @@ -87,7 +84,7 @@ where C: RaftTypeConfig Initialize { members: BTreeMap, - tx: ResultSender>, + tx: ResultSender>, }, ChangeMembership { @@ -97,7 +94,7 @@ where C: RaftTypeConfig /// config will be converted into learners, otherwise they will be removed. retain: bool, - tx: ResultSender, ClientWriteError>, + tx: ResultSender, ClientWriteError>, }, ExternalCoreRequest { diff --git a/openraft/src/core/sm/response.rs b/openraft/src/core/sm/response.rs index 4790e84f5..a563b8f70 100644 --- a/openraft/src/core/sm/response.rs +++ b/openraft/src/core/sm/response.rs @@ -10,12 +10,12 @@ pub(crate) enum Response where C: RaftTypeConfig { /// Build a snapshot, it returns result via the universal RaftCore response channel. - BuildSnapshot(SnapshotMeta), + BuildSnapshot(SnapshotMeta), /// When finishing installing a snapshot. /// /// It does not return any value to RaftCore. - InstallSnapshot(Option>), + InstallSnapshot(Option>), /// Send back applied result to RaftCore. Apply(ApplyResult), diff --git a/openraft/src/engine/command.rs b/openraft/src/engine/command.rs index 6673ebc75..c81c3c2b0 100644 --- a/openraft/src/engine/command.rs +++ b/openraft/src/engine/command.rs @@ -79,7 +79,7 @@ where C: RaftTypeConfig SaveVote { vote: Vote }, /// Send vote to all other members - SendVote { vote_req: VoteRequest }, + SendVote { vote_req: VoteRequest }, /// Purge log from the beginning to `upto`, inclusive. PurgeLog { upto: LogId }, @@ -223,12 +223,12 @@ where NID: NodeId pub(crate) enum Respond where C: RaftTypeConfig { - Vote(ValueSender, Infallible>>), - AppendEntries(ValueSender, Infallible>>), + Vote(ValueSender, Infallible>>), + AppendEntries(ValueSender, Infallible>>), ReceiveSnapshotChunk(ValueSender>), - InstallSnapshot(ValueSender, InstallSnapshotError>>), - InstallFullSnapshot(ValueSender, Infallible>>), - Initialize(ValueSender>>), + InstallSnapshot(ValueSender, InstallSnapshotError>>), + InstallFullSnapshot(ValueSender, Infallible>>), + Initialize(ValueSender>>), } impl Respond diff --git a/openraft/src/engine/engine_config.rs b/openraft/src/engine/engine_config.rs index 82dc946ac..04521eec2 100644 --- a/openraft/src/engine/engine_config.rs +++ b/openraft/src/engine/engine_config.rs @@ -1,17 +1,17 @@ use std::time::Duration; use crate::engine::time_state; -use crate::AsyncRuntime; +use crate::type_config::alias::AsyncRuntimeOf; use crate::Config; -use crate::NodeId; +use crate::RaftTypeConfig; use crate::SnapshotPolicy; /// Config for Engine #[derive(Clone, Debug)] #[derive(PartialEq, Eq)] -pub(crate) struct EngineConfig { +pub(crate) struct EngineConfig { /// The id of this node. - pub(crate) id: NID, + pub(crate) id: C::NodeId, /// The snapshot policy to use for a Raft node. pub(crate) snapshot_policy: SnapshotPolicy, @@ -28,22 +28,11 @@ pub(crate) struct EngineConfig { pub(crate) timer_config: time_state::Config, } -impl Default for EngineConfig { - fn default() -> Self { - Self { - id: NID::default(), - snapshot_policy: SnapshotPolicy::LogsSinceLast(5000), - max_in_snapshot_log_to_keep: 1000, - purge_batch_size: 256, - max_payload_entries: 300, - timer_config: time_state::Config::default(), - } - } -} - -impl EngineConfig { - pub(crate) fn new(id: NID, config: &Config) -> Self { - let election_timeout = Duration::from_millis(config.new_rand_election_timeout::()); +impl EngineConfig +where C: RaftTypeConfig +{ + pub(crate) fn new(id: C::NodeId, config: &Config) -> Self { + let election_timeout = Duration::from_millis(config.new_rand_election_timeout::>()); Self { id, snapshot_policy: config.snapshot_policy.clone(), @@ -57,4 +46,16 @@ impl EngineConfig { }, } } + + #[allow(dead_code)] + pub(crate) fn new_default(id: C::NodeId) -> Self { + Self { + id, + snapshot_policy: SnapshotPolicy::LogsSinceLast(5000), + max_in_snapshot_log_to_keep: 1000, + purge_batch_size: 256, + max_payload_entries: 300, + timer_config: time_state::Config::default(), + } + } } diff --git a/openraft/src/engine/engine_impl.rs b/openraft/src/engine/engine_impl.rs index 3c587761f..2ca3a3fe5 100644 --- a/openraft/src/engine/engine_impl.rs +++ b/openraft/src/engine/engine_impl.rs @@ -60,11 +60,11 @@ use crate::Vote; /// This structure only contains necessary information to run raft algorithm, /// but none of the application specific data. /// TODO: make the fields private -#[derive(Debug, Default)] +#[derive(Debug)] pub(crate) struct Engine where C: RaftTypeConfig { - pub(crate) config: EngineConfig, + pub(crate) config: EngineConfig, /// The state of this raft node. pub(crate) state: Valid>, @@ -86,7 +86,7 @@ where C: RaftTypeConfig impl Engine where C: RaftTypeConfig { - pub(crate) fn new(init_state: RaftState, config: EngineConfig) -> Self { + pub(crate) fn new(init_state: RaftState, config: EngineConfig) -> Self { Self { config, state: Valid::new(init_state), @@ -96,6 +96,14 @@ where C: RaftTypeConfig } } + /// Create a default Engine for testing. + #[allow(dead_code)] + pub(crate) fn testing_default(id: C::NodeId) -> Self { + let config = EngineConfig::new_default(id); + let state = RaftState::default(); + Self::new(state, config) + } + #[tracing::instrument(level = "debug", skip_all)] pub(crate) fn startup(&mut self) { // Allows starting up as a leader. @@ -150,7 +158,7 @@ where C: RaftTypeConfig /// /// [precondition]: crate::docs::cluster_control::cluster_formation#preconditions-for-initialization #[tracing::instrument(level = "debug", skip_all)] - pub(crate) fn initialize(&mut self, mut entry: C::Entry) -> Result<(), InitializeError> { + pub(crate) fn initialize(&mut self, mut entry: C::Entry) -> Result<(), InitializeError> { self.check_initialize()?; self.state.assign_log_ids([&mut entry]); @@ -222,7 +230,7 @@ where C: RaftTypeConfig where T: OptionalSend, E: OptionalSend, - E: From>, + E: From>, { let res = self.leader_handler(); let forward_err = match res { @@ -241,7 +249,7 @@ where C: RaftTypeConfig } #[tracing::instrument(level = "debug", skip_all)] - pub(crate) fn handle_vote_req(&mut self, req: VoteRequest) -> VoteResponse { + pub(crate) fn handle_vote_req(&mut self, req: VoteRequest) -> VoteResponse { let now = InstantOf::::now(); let lease = self.config.timer_config.leader_lease; let vote = self.state.vote_ref(); @@ -326,7 +334,7 @@ where C: RaftTypeConfig } #[tracing::instrument(level = "debug", skip(self, resp))] - pub(crate) fn handle_vote_resp(&mut self, target: C::NodeId, resp: VoteResponse) { + pub(crate) fn handle_vote_resp(&mut self, target: C::NodeId, resp: VoteResponse) { tracing::info!( resp = display(resp.summary()), target = display(target), @@ -404,7 +412,7 @@ where C: RaftTypeConfig let is_ok = res.is_ok(); if let Some(tx) = tx { - let resp: AppendEntriesResponse = res.into(); + let resp: AppendEntriesResponse = res.into(); self.output.push_command(Command::Respond { when: None, resp: Respond::new(Ok(resp), tx), @@ -418,7 +426,7 @@ where C: RaftTypeConfig vote: &Vote, prev_log_id: Option>, entries: Vec, - ) -> Result<(), RejectAppendEntries> { + ) -> Result<(), RejectAppendEntries> { self.vote_handler().update_vote(vote)?; // Vote is legal. @@ -451,7 +459,7 @@ where C: RaftTypeConfig &mut self, vote: Vote, snapshot: Snapshot, - tx: ResultSender>, + tx: ResultSender>, ) { tracing::info!(vote = display(vote), snapshot = display(&snapshot), "{}", func_name!()); @@ -522,7 +530,7 @@ where C: RaftTypeConfig /// - Engine only keeps the snapshot meta with the greatest last-log-id; /// - and a snapshot smaller than last-committed is not allowed to be installed. #[tracing::instrument(level = "debug", skip_all)] - pub(crate) fn finish_building_snapshot(&mut self, meta: SnapshotMeta) { + pub(crate) fn finish_building_snapshot(&mut self, meta: SnapshotMeta) { tracing::info!(snapshot_meta = display(&meta), "{}", func_name!()); self.state.io_state_mut().set_building_snapshot(false); @@ -656,7 +664,7 @@ where C: RaftTypeConfig /// /// It is allowed to initialize only when `last_log_id.is_none()` and `vote==(term=0, /// node_id=0)`. See: [Conditions for initialization](https://datafuselabs.github.io/openraft/cluster-formation.html#conditions-for-initialization) - fn check_initialize(&self) -> Result<(), NotAllowed> { + fn check_initialize(&self) -> Result<(), NotAllowed> { if self.state.last_log_id().is_none() && self.state.vote_ref() == &Vote::default() { return Ok(()); } @@ -675,10 +683,7 @@ where C: RaftTypeConfig /// When initialize, the node that accept initialize request has to be a member of the initial /// config. - fn check_members_contain_me( - &self, - m: &Membership, - ) -> Result<(), NotInMembers> { + fn check_members_contain_me(&self, m: &Membership) -> Result<(), NotInMembers> { if !m.is_voter(&self.config.id) { let e = NotInMembers { node_id: self.config.id, @@ -738,7 +743,7 @@ where C: RaftTypeConfig } } - pub(crate) fn leader_handler(&mut self) -> Result, ForwardToLeader> { + pub(crate) fn leader_handler(&mut self) -> Result, ForwardToLeader> { let leader = match self.internal_server_state.leading_mut() { None => { tracing::debug!("this node is NOT a leader: {:?}", self.state.server_state); diff --git a/openraft/src/engine/handler/following_handler/append_entries_test.rs b/openraft/src/engine/handler/following_handler/append_entries_test.rs index 693458457..1dba15d27 100644 --- a/openraft/src/engine/handler/following_handler/append_entries_test.rs +++ b/openraft/src/engine/handler/following_handler/append_entries_test.rs @@ -13,16 +13,16 @@ use crate::MembershipState; use crate::TokioInstant; use crate::Vote; -fn m01() -> Membership { +fn m01() -> Membership { Membership::new(vec![btreeset! {0,1}], None) } -fn m23() -> Membership { +fn m23() -> Membership { Membership::new(vec![btreeset! {2,3}], None) } fn eng() -> Engine { - let mut eng = Engine::default(); + let mut eng = Engine::testing_default(0); eng.state.enable_validation(false); // Disable validation for incomplete state eng.config.id = 2; diff --git a/openraft/src/engine/handler/following_handler/commit_entries_test.rs b/openraft/src/engine/handler/following_handler/commit_entries_test.rs index 4502ab609..0d34c7cc9 100644 --- a/openraft/src/engine/handler/following_handler/commit_entries_test.rs +++ b/openraft/src/engine/handler/following_handler/commit_entries_test.rs @@ -12,16 +12,16 @@ use crate::EffectiveMembership; use crate::Membership; use crate::MembershipState; -fn m01() -> Membership { +fn m01() -> Membership { Membership::new(vec![btreeset! {0,1}], None) } -fn m23() -> Membership { +fn m23() -> Membership { Membership::new(vec![btreeset! {2,3}], None) } fn eng() -> Engine { - let mut eng = Engine::default(); + let mut eng = Engine::testing_default(0); eng.state.enable_validation(false); // Disable validation for incomplete state eng.state.committed = Some(log_id(1, 1, 1)); diff --git a/openraft/src/engine/handler/following_handler/do_append_entries_test.rs b/openraft/src/engine/handler/following_handler/do_append_entries_test.rs index d5b14e7b1..a4f5344e9 100644 --- a/openraft/src/engine/handler/following_handler/do_append_entries_test.rs +++ b/openraft/src/engine/handler/following_handler/do_append_entries_test.rs @@ -16,24 +16,24 @@ use crate::EntryPayload; use crate::Membership; use crate::MembershipState; -fn m01() -> Membership { +fn m01() -> Membership { Membership::new(vec![btreeset! {0,1}], None) } -fn m23() -> Membership { +fn m23() -> Membership { Membership::new(vec![btreeset! {2,3}], None) } -fn m34() -> Membership { +fn m34() -> Membership { Membership::new(vec![btreeset! {3,4}], None) } -fn m45() -> Membership { +fn m45() -> Membership { Membership::new(vec![btreeset! {4,5}], None) } fn eng() -> Engine { - let mut eng = Engine::default(); + let mut eng = Engine::testing_default(0); eng.state.enable_validation(false); // Disable validation for incomplete state eng.config.id = 2; diff --git a/openraft/src/engine/handler/following_handler/install_snapshot_test.rs b/openraft/src/engine/handler/following_handler/install_snapshot_test.rs index 57eade688..3cfcbafce 100644 --- a/openraft/src/engine/handler/following_handler/install_snapshot_test.rs +++ b/openraft/src/engine/handler/following_handler/install_snapshot_test.rs @@ -19,16 +19,16 @@ use crate::StoredMembership; use crate::TokioInstant; use crate::Vote; -fn m12() -> Membership { - Membership::::new(vec![btreeset! {1,2}], None) +fn m12() -> Membership { + Membership::::new(vec![btreeset! {1,2}], None) } -fn m1234() -> Membership { - Membership::::new(vec![btreeset! {1,2,3,4}], None) +fn m1234() -> Membership { + Membership::::new(vec![btreeset! {1,2,3,4}], None) } fn eng() -> Engine { - let mut eng = Engine::default(); + let mut eng = Engine::testing_default(0); eng.state.enable_validation(false); // Disable validation for incomplete state eng.state.vote.update(TokioInstant::now(), Vote::new_committed(2, 1)); @@ -163,7 +163,7 @@ fn test_install_snapshot_conflict() -> anyhow::Result<()> { // Snapshot will be installed, all non-committed log will be deleted. // And there should be no conflicting logs left. let mut eng = { - let mut eng = Engine::::default(); + let mut eng = Engine::::testing_default(0); eng.state.enable_validation(false); // Disable validation for incomplete state eng.state.vote.update(TokioInstant::now(), Vote::new_committed(2, 1)); diff --git a/openraft/src/engine/handler/following_handler/mod.rs b/openraft/src/engine/handler/following_handler/mod.rs index cbb6f2bd5..504080a65 100644 --- a/openraft/src/engine/handler/following_handler/mod.rs +++ b/openraft/src/engine/handler/following_handler/mod.rs @@ -35,7 +35,7 @@ use crate::StoredMembership; pub(crate) struct FollowingHandler<'x, C> where C: RaftTypeConfig { - pub(crate) config: &'x mut EngineConfig, + pub(crate) config: &'x mut EngineConfig, pub(crate) state: &'x mut RaftState, pub(crate) output: &'x mut EngineOutput, } @@ -92,7 +92,7 @@ where C: RaftTypeConfig pub(crate) fn ensure_log_consecutive( &mut self, prev_log_id: Option>, - ) -> Result<(), RejectAppendEntries> { + ) -> Result<(), RejectAppendEntries> { if let Some(ref prev) = prev_log_id { if !self.state.has_log_id(prev) { let local = self.state.get_log_id(prev.index); @@ -224,7 +224,7 @@ where C: RaftTypeConfig /// Update membership state with a committed membership config #[tracing::instrument(level = "debug", skip_all)] - fn update_committed_membership(&mut self, membership: EffectiveMembership) { + fn update_committed_membership(&mut self, membership: EffectiveMembership) { tracing::debug!("update committed membership: {}", membership.summary()); let m = Arc::new(membership); @@ -294,9 +294,7 @@ where C: RaftTypeConfig /// /// A follower/learner reverts the effective membership to the previous one, /// when conflicting logs are found. - fn last_two_memberships<'a>( - entries: impl DoubleEndedIterator, - ) -> Vec> + fn last_two_memberships<'a>(entries: impl DoubleEndedIterator) -> Vec> where C::Entry: 'a { let mut memberships = vec![]; diff --git a/openraft/src/engine/handler/following_handler/truncate_logs_test.rs b/openraft/src/engine/handler/following_handler/truncate_logs_test.rs index 37073d011..2b280e9a9 100644 --- a/openraft/src/engine/handler/following_handler/truncate_logs_test.rs +++ b/openraft/src/engine/handler/following_handler/truncate_logs_test.rs @@ -13,20 +13,20 @@ use crate::Membership; use crate::MembershipState; use crate::ServerState; -fn m01() -> Membership { - Membership::::new(vec![btreeset! {0,1}], None) +fn m01() -> Membership { + Membership::::new(vec![btreeset! {0,1}], None) } -fn m12() -> Membership { - Membership::::new(vec![btreeset! {1,2}], None) +fn m12() -> Membership { + Membership::::new(vec![btreeset! {1,2}], None) } -fn m23() -> Membership { - Membership::::new(vec![btreeset! {2,3}], None) +fn m23() -> Membership { + Membership::::new(vec![btreeset! {2,3}], None) } fn eng() -> Engine { - let mut eng = Engine::default(); + let mut eng = Engine::testing_default(0); eng.state.enable_validation(false); // Disable validation for incomplete state eng.config.id = 2; diff --git a/openraft/src/engine/handler/following_handler/update_committed_membership_test.rs b/openraft/src/engine/handler/following_handler/update_committed_membership_test.rs index f4f93af63..42e81961a 100644 --- a/openraft/src/engine/handler/following_handler/update_committed_membership_test.rs +++ b/openraft/src/engine/handler/following_handler/update_committed_membership_test.rs @@ -10,20 +10,20 @@ use crate::EffectiveMembership; use crate::Membership; use crate::MembershipState; -fn m01() -> Membership { - Membership::::new(vec![btreeset! {0,1}], None) +fn m01() -> Membership { + Membership::::new(vec![btreeset! {0,1}], None) } -fn m23() -> Membership { - Membership::::new(vec![btreeset! {2,3}], None) +fn m23() -> Membership { + Membership::::new(vec![btreeset! {2,3}], None) } -fn m34() -> Membership { - Membership::::new(vec![btreeset! {3,4}], None) +fn m34() -> Membership { + Membership::::new(vec![btreeset! {3,4}], None) } fn eng() -> Engine { - let mut eng = Engine::default(); + let mut eng = Engine::testing_default(0); eng.config.id = 2; eng.state.membership_state = MembershipState::new( Arc::new(EffectiveMembership::new(Some(log_id(1, 1, 1)), m01())), diff --git a/openraft/src/engine/handler/leader_handler/append_entries_test.rs b/openraft/src/engine/handler/leader_handler/append_entries_test.rs index f524c6c88..6680b2d85 100644 --- a/openraft/src/engine/handler/leader_handler/append_entries_test.rs +++ b/openraft/src/engine/handler/leader_handler/append_entries_test.rs @@ -24,25 +24,25 @@ use crate::MembershipState; use crate::TokioInstant; use crate::Vote; -fn m01() -> Membership { - Membership::::new(vec![btreeset! {0,1}], None) +fn m01() -> Membership { + Membership::::new(vec![btreeset! {0,1}], None) } -fn m1() -> Membership { - Membership::::new(vec![btreeset! {1}], None) +fn m1() -> Membership { + Membership::::new(vec![btreeset! {1}], None) } /// members: {1}, learners: {2} -fn m1_2() -> Membership { - Membership::::new(vec![btreeset! {1}], Some(btreeset! {2})) +fn m1_2() -> Membership { + Membership::::new(vec![btreeset! {1}], Some(btreeset! {2})) } -fn m23() -> Membership { - Membership::::new(vec![btreeset! {2,3}], btreeset! {1,2,3}) +fn m23() -> Membership { + Membership::::new(vec![btreeset! {2,3}], btreeset! {1,2,3}) } fn eng() -> Engine { - let mut eng = Engine::default(); + let mut eng = Engine::testing_default(0); eng.state.enable_validation(false); // Disable validation for incomplete state eng.config.id = 1; diff --git a/openraft/src/engine/handler/leader_handler/mod.rs b/openraft/src/engine/handler/leader_handler/mod.rs index 7dbe95610..dc30f5f3d 100644 --- a/openraft/src/engine/handler/leader_handler/mod.rs +++ b/openraft/src/engine/handler/leader_handler/mod.rs @@ -22,7 +22,7 @@ use crate::RaftTypeConfig; pub(crate) struct LeaderHandler<'x, C> where C: RaftTypeConfig { - pub(crate) config: &'x mut EngineConfig, + pub(crate) config: &'x mut EngineConfig, pub(crate) leader: &'x mut Leading>, pub(crate) state: &'x mut RaftState, pub(crate) output: &'x mut EngineOutput, diff --git a/openraft/src/engine/handler/leader_handler/send_heartbeat_test.rs b/openraft/src/engine/handler/leader_handler/send_heartbeat_test.rs index 9425c612d..1c98f6d38 100644 --- a/openraft/src/engine/handler/leader_handler/send_heartbeat_test.rs +++ b/openraft/src/engine/handler/leader_handler/send_heartbeat_test.rs @@ -18,16 +18,16 @@ use crate::MembershipState; use crate::TokioInstant; use crate::Vote; -fn m01() -> Membership { - Membership::::new(vec![btreeset! {0,1}], None) +fn m01() -> Membership { + Membership::::new(vec![btreeset! {0,1}], None) } -fn m23() -> Membership { - Membership::::new(vec![btreeset! {2,3}], btreeset! {1,2,3}) +fn m23() -> Membership { + Membership::::new(vec![btreeset! {2,3}], btreeset! {1,2,3}) } fn eng() -> Engine { - let mut eng = Engine::default(); + let mut eng = Engine::testing_default(0); eng.state.enable_validation(false); // Disable validation for incomplete state eng.config.id = 1; diff --git a/openraft/src/engine/handler/log_handler/calc_purge_upto_test.rs b/openraft/src/engine/handler/log_handler/calc_purge_upto_test.rs index 9e11eb80f..f1d3e88b3 100644 --- a/openraft/src/engine/handler/log_handler/calc_purge_upto_test.rs +++ b/openraft/src/engine/handler/log_handler/calc_purge_upto_test.rs @@ -12,7 +12,7 @@ fn log_id(term: u64, index: u64) -> LogId { } fn eng() -> Engine { - let mut eng = Engine::default(); + let mut eng = Engine::testing_default(0); eng.state.enable_validation(false); // Disable validation for incomplete state eng.state.log_ids = LogIdList::new(vec![ diff --git a/openraft/src/engine/handler/log_handler/mod.rs b/openraft/src/engine/handler/log_handler/mod.rs index ccd70bc74..2c36e6a05 100644 --- a/openraft/src/engine/handler/log_handler/mod.rs +++ b/openraft/src/engine/handler/log_handler/mod.rs @@ -15,7 +15,7 @@ use crate::RaftTypeConfig; pub(crate) struct LogHandler<'x, C> where C: RaftTypeConfig { - pub(crate) config: &'x mut EngineConfig, + pub(crate) config: &'x mut EngineConfig, pub(crate) state: &'x mut RaftState, pub(crate) output: &'x mut EngineOutput, } diff --git a/openraft/src/engine/handler/log_handler/purge_log_test.rs b/openraft/src/engine/handler/log_handler/purge_log_test.rs index 8aceffcde..e44840366 100644 --- a/openraft/src/engine/handler/log_handler/purge_log_test.rs +++ b/openraft/src/engine/handler/log_handler/purge_log_test.rs @@ -6,7 +6,7 @@ use crate::raft_state::LogStateReader; use crate::testing::log_id; fn eng() -> Engine { - let mut eng = Engine::default(); + let mut eng = Engine::testing_default(0); eng.state.enable_validation(false); // Disable validation for incomplete state eng.state.log_ids = LogIdList::new(vec![log_id(2, 1, 2), log_id(4, 1, 4), log_id(4, 1, 6)]); diff --git a/openraft/src/engine/handler/replication_handler/append_membership_test.rs b/openraft/src/engine/handler/replication_handler/append_membership_test.rs index a44f8e7d9..94f90433d 100644 --- a/openraft/src/engine/handler/replication_handler/append_membership_test.rs +++ b/openraft/src/engine/handler/replication_handler/append_membership_test.rs @@ -20,28 +20,28 @@ use crate::MembershipState; use crate::TokioInstant; use crate::Vote; -fn m01() -> Membership { - Membership::::new(vec![btreeset! {0,1}], None) +fn m01() -> Membership { + Membership::::new(vec![btreeset! {0,1}], None) } -fn m23() -> Membership { - Membership::::new(vec![btreeset! {2,3}], None) +fn m23() -> Membership { + Membership::::new(vec![btreeset! {2,3}], None) } -fn m23_45() -> Membership { - Membership::::new(vec![btreeset! {2,3}], Some(btreeset! {4,5})) +fn m23_45() -> Membership { + Membership::::new(vec![btreeset! {2,3}], Some(btreeset! {4,5})) } -fn m34() -> Membership { - Membership::::new(vec![btreeset! {3,4}], None) +fn m34() -> Membership { + Membership::::new(vec![btreeset! {3,4}], None) } -fn m4_356() -> Membership { - Membership::::new(vec![btreeset! {4}], Some(btreeset! {3,5,6})) +fn m4_356() -> Membership { + Membership::::new(vec![btreeset! {4}], Some(btreeset! {3,5,6})) } fn eng() -> Engine { - let mut eng = Engine::default(); + let mut eng = Engine::testing_default(0); eng.config.id = 2; eng.state.membership_state = MembershipState::new( Arc::new(EffectiveMembership::new(Some(log_id(1, 1, 1)), m01())), diff --git a/openraft/src/engine/handler/replication_handler/mod.rs b/openraft/src/engine/handler/replication_handler/mod.rs index 4fbac7192..7ca046504 100644 --- a/openraft/src/engine/handler/replication_handler/mod.rs +++ b/openraft/src/engine/handler/replication_handler/mod.rs @@ -40,7 +40,7 @@ use crate::ServerState; pub(crate) struct ReplicationHandler<'x, C> where C: RaftTypeConfig { - pub(crate) config: &'x mut EngineConfig, + pub(crate) config: &'x mut EngineConfig, pub(crate) leader: &'x mut Leading>, pub(crate) state: &'x mut RaftState, pub(crate) output: &'x mut EngineOutput, @@ -79,7 +79,7 @@ where C: RaftTypeConfig /// /// It is called by the leader when a new membership log is appended to log store. #[tracing::instrument(level = "debug", skip_all)] - pub(crate) fn append_membership(&mut self, log_id: &LogId, m: &Membership) { + pub(crate) fn append_membership(&mut self, log_id: &LogId, m: &Membership) { tracing::debug!("update effective membership: log_id:{} {}", log_id, m.summary()); debug_assert!( diff --git a/openraft/src/engine/handler/replication_handler/update_matching_test.rs b/openraft/src/engine/handler/replication_handler/update_matching_test.rs index 303a5855e..a0f61a981 100644 --- a/openraft/src/engine/handler/replication_handler/update_matching_test.rs +++ b/openraft/src/engine/handler/replication_handler/update_matching_test.rs @@ -17,16 +17,16 @@ use crate::MembershipState; use crate::TokioInstant; use crate::Vote; -fn m01() -> Membership { - Membership::::new(vec![btreeset! {0,1}], None) +fn m01() -> Membership { + Membership::::new(vec![btreeset! {0,1}], None) } -fn m123() -> Membership { - Membership::::new(vec![btreeset! {1,2,3}], None) +fn m123() -> Membership { + Membership::::new(vec![btreeset! {1,2,3}], None) } fn eng() -> Engine { - let mut eng = Engine::default(); + let mut eng = Engine::testing_default(0); eng.state.enable_validation(false); // Disable validation for incomplete state eng.config.id = 2; diff --git a/openraft/src/engine/handler/server_state_handler/mod.rs b/openraft/src/engine/handler/server_state_handler/mod.rs index 6793d2a3a..148da0e3c 100644 --- a/openraft/src/engine/handler/server_state_handler/mod.rs +++ b/openraft/src/engine/handler/server_state_handler/mod.rs @@ -11,7 +11,7 @@ use crate::ServerState; pub(crate) struct ServerStateHandler<'st, C> where C: RaftTypeConfig { - pub(crate) config: &'st EngineConfig, + pub(crate) config: &'st EngineConfig, pub(crate) state: &'st mut RaftState, pub(crate) output: &'st mut EngineOutput, } diff --git a/openraft/src/engine/handler/server_state_handler/update_server_state_test.rs b/openraft/src/engine/handler/server_state_handler/update_server_state_test.rs index 8474a4cde..d8cc6a388 100644 --- a/openraft/src/engine/handler/server_state_handler/update_server_state_test.rs +++ b/openraft/src/engine/handler/server_state_handler/update_server_state_test.rs @@ -15,16 +15,16 @@ use crate::ServerState; use crate::TokioInstant; use crate::Vote; -fn m01() -> Membership { - Membership::::new(vec![btreeset! {0,1}], None) +fn m01() -> Membership { + Membership::::new(vec![btreeset! {0,1}], None) } -fn m123() -> Membership { - Membership::::new(vec![btreeset! {1,2,3}], None) +fn m123() -> Membership { + Membership::::new(vec![btreeset! {1,2,3}], None) } fn eng() -> Engine { - let mut eng = Engine::default(); + let mut eng = Engine::testing_default(0); eng.state.enable_validation(false); // Disable validation for incomplete state eng.config.id = 2; diff --git a/openraft/src/engine/handler/snapshot_handler/mod.rs b/openraft/src/engine/handler/snapshot_handler/mod.rs index 1ab398c83..206154665 100644 --- a/openraft/src/engine/handler/snapshot_handler/mod.rs +++ b/openraft/src/engine/handler/snapshot_handler/mod.rs @@ -49,7 +49,7 @@ where C: RaftTypeConfig /// /// [`RaftStateMachine`]: crate::storage::RaftStateMachine #[tracing::instrument(level = "debug", skip_all)] - pub(crate) fn update_snapshot(&mut self, meta: SnapshotMeta) -> bool { + pub(crate) fn update_snapshot(&mut self, meta: SnapshotMeta) -> bool { tracing::info!("update_snapshot: {:?}", meta); if meta.last_log_id <= self.state.snapshot_last_log_id().copied() { diff --git a/openraft/src/engine/handler/snapshot_handler/trigger_snapshot_test.rs b/openraft/src/engine/handler/snapshot_handler/trigger_snapshot_test.rs index f329fd52e..100657e7b 100644 --- a/openraft/src/engine/handler/snapshot_handler/trigger_snapshot_test.rs +++ b/openraft/src/engine/handler/snapshot_handler/trigger_snapshot_test.rs @@ -6,7 +6,7 @@ use crate::engine::Command; use crate::engine::Engine; fn eng() -> Engine { - let mut eng = Engine::default(); + let mut eng = Engine::testing_default(0); eng.state.enable_validation(false); // Disable validation for incomplete state eng diff --git a/openraft/src/engine/handler/snapshot_handler/update_snapshot_test.rs b/openraft/src/engine/handler/snapshot_handler/update_snapshot_test.rs index 75368409e..f0485c1df 100644 --- a/openraft/src/engine/handler/snapshot_handler/update_snapshot_test.rs +++ b/openraft/src/engine/handler/snapshot_handler/update_snapshot_test.rs @@ -8,16 +8,16 @@ use crate::Membership; use crate::SnapshotMeta; use crate::StoredMembership; -fn m12() -> Membership { - Membership::::new(vec![btreeset! {1,2}], None) +fn m12() -> Membership { + Membership::::new(vec![btreeset! {1,2}], None) } -fn m1234() -> Membership { - Membership::::new(vec![btreeset! {1,2,3,4}], None) +fn m1234() -> Membership { + Membership::::new(vec![btreeset! {1,2,3,4}], None) } fn eng() -> Engine { - let mut eng = Engine::default(); + let mut eng = Engine::testing_default(0); eng.state.enable_validation(false); // Disable validation for incomplete state eng.state.snapshot_meta = SnapshotMeta { diff --git a/openraft/src/engine/handler/vote_handler/accept_vote_test.rs b/openraft/src/engine/handler/vote_handler/accept_vote_test.rs index 7de499e7a..95f4f42ff 100644 --- a/openraft/src/engine/handler/vote_handler/accept_vote_test.rs +++ b/openraft/src/engine/handler/vote_handler/accept_vote_test.rs @@ -19,13 +19,13 @@ use crate::Membership; use crate::TokioInstant; use crate::Vote; -fn m01() -> Membership { - Membership::::new(vec![btreeset! {0,1}], None) +fn m01() -> Membership { + Membership::::new(vec![btreeset! {0,1}], None) } /// Make a sample VoteResponse -fn mk_res() -> Result, Infallible> { - Ok::, Infallible>(VoteResponse { +fn mk_res() -> Result, Infallible> { + Ok::, Infallible>(VoteResponse { vote: Vote::new(2, 1), vote_granted: false, last_log_id: None, @@ -33,7 +33,7 @@ fn mk_res() -> Result, Infallible> { } fn eng() -> Engine { - let mut eng = Engine::default(); + let mut eng = Engine::testing_default(0); eng.state.enable_validation(false); // Disable validation for incomplete state eng.config.id = 0; diff --git a/openraft/src/engine/handler/vote_handler/handle_message_vote_test.rs b/openraft/src/engine/handler/vote_handler/handle_message_vote_test.rs index 8643704b2..b08e2953e 100644 --- a/openraft/src/engine/handler/vote_handler/handle_message_vote_test.rs +++ b/openraft/src/engine/handler/vote_handler/handle_message_vote_test.rs @@ -16,12 +16,12 @@ use crate::Membership; use crate::TokioInstant; use crate::Vote; -fn m01() -> Membership { - Membership::::new(vec![btreeset! {0,1}], None) +fn m01() -> Membership { + Membership::::new(vec![btreeset! {0,1}], None) } fn eng() -> Engine { - let mut eng = Engine::default(); + let mut eng = Engine::testing_default(0); eng.state.enable_validation(false); // Disable validation for incomplete state eng.config.id = 0; diff --git a/openraft/src/engine/handler/vote_handler/mod.rs b/openraft/src/engine/handler/vote_handler/mod.rs index b6adfa5bf..08a514a08 100644 --- a/openraft/src/engine/handler/vote_handler/mod.rs +++ b/openraft/src/engine/handler/vote_handler/mod.rs @@ -28,7 +28,7 @@ use crate::Vote; pub(crate) struct VoteHandler<'st, C> where C: RaftTypeConfig { - pub(crate) config: &'st EngineConfig, + pub(crate) config: &'st EngineConfig, pub(crate) state: &'st mut RaftState, pub(crate) output: &'st mut EngineOutput, pub(crate) internal_server_state: &'st mut InternalServerState, @@ -56,7 +56,7 @@ where C: RaftTypeConfig T: Debug + Eq + OptionalSend, E: Debug + Eq + OptionalSend, Respond: From>>, - F: Fn(&RaftState, RejectVoteRequest) -> Result, + F: Fn(&RaftState, RejectVoteRequest) -> Result, { let vote_res = self.update_vote(vote); @@ -83,7 +83,7 @@ where C: RaftTypeConfig /// Note: This method does not check last-log-id. handle-vote-request has to deal with /// last-log-id itself. #[tracing::instrument(level = "debug", skip_all)] - pub(crate) fn update_vote(&mut self, vote: &Vote) -> Result<(), RejectVoteRequest> { + pub(crate) fn update_vote(&mut self, vote: &Vote) -> Result<(), RejectVoteRequest> { // Partial ord compare: // Vote does not has to be total ord. // `!(a >= b)` does not imply `a < b`. diff --git a/openraft/src/engine/testing.rs b/openraft/src/engine/testing.rs index 24ac5be18..a549545a4 100644 --- a/openraft/src/engine/testing.rs +++ b/openraft/src/engine/testing.rs @@ -6,13 +6,22 @@ use crate::TokioRuntime; /// Trivial Raft type config for Engine related unit tests, /// with an optional custom node type `N` for Node type. -#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Ord, PartialOrd)] +#[derive(Debug, Default, Eq, PartialEq, Ord, PartialOrd)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] pub(crate) struct UTConfig { _p: std::marker::PhantomData, } + +impl Clone for UTConfig { + fn clone(&self) -> Self { + *self + } +} + +impl Copy for UTConfig {} + impl RaftTypeConfig for UTConfig -where N: Node + Copy + Ord +where N: Node + Ord { type D = (); type R = (); diff --git a/openraft/src/engine/tests/append_entries_test.rs b/openraft/src/engine/tests/append_entries_test.rs index b7161ea82..5b41a5db5 100644 --- a/openraft/src/engine/tests/append_entries_test.rs +++ b/openraft/src/engine/tests/append_entries_test.rs @@ -20,20 +20,20 @@ use crate::MembershipState; use crate::TokioInstant; use crate::Vote; -fn m01() -> Membership { - Membership::::new(vec![btreeset! {0,1}], None) +fn m01() -> Membership { + Membership::::new(vec![btreeset! {0,1}], None) } -fn m23() -> Membership { - Membership::::new(vec![btreeset! {2,3}], None) +fn m23() -> Membership { + Membership::::new(vec![btreeset! {2,3}], None) } -fn m34() -> Membership { - Membership::::new(vec![btreeset! {3,4}], None) +fn m34() -> Membership { + Membership::::new(vec![btreeset! {3,4}], None) } fn eng() -> Engine { - let mut eng = Engine::default(); + let mut eng = Engine::testing_default(0); eng.state.enable_validation(false); // Disable validation for incomplete state eng.config.id = 2; diff --git a/openraft/src/engine/tests/elect_test.rs b/openraft/src/engine/tests/elect_test.rs index 176dccbee..ef6c399b7 100644 --- a/openraft/src/engine/tests/elect_test.rs +++ b/openraft/src/engine/tests/elect_test.rs @@ -21,16 +21,16 @@ use crate::Membership; use crate::TokioInstant; use crate::Vote; -fn m1() -> Membership { +fn m1() -> Membership { Membership::new(vec![btreeset! {1}], None) } -fn m12() -> Membership { +fn m12() -> Membership { Membership::new(vec![btreeset! {1,2}], None) } fn eng() -> Engine { - let mut eng = Engine::default(); + let mut eng = Engine::testing_default(0); eng.state.log_ids = LogIdList::new([LogId::new(CommittedLeaderId::new(0, 0), 0)]); eng.state.enable_validation(false); // Disable validation for incomplete state eng diff --git a/openraft/src/engine/tests/handle_vote_req_test.rs b/openraft/src/engine/tests/handle_vote_req_test.rs index 737275127..b7877cf47 100644 --- a/openraft/src/engine/tests/handle_vote_req_test.rs +++ b/openraft/src/engine/tests/handle_vote_req_test.rs @@ -17,12 +17,12 @@ use crate::Membership; use crate::TokioInstant; use crate::Vote; -fn m01() -> Membership { - Membership::::new(vec![btreeset! {0,1}], None) +fn m01() -> Membership { + Membership::::new(vec![btreeset! {0,1}], None) } fn eng() -> Engine { - let mut eng = Engine::default(); + let mut eng = Engine::testing_default(0); eng.state.enable_validation(false); // Disable validation for incomplete state eng.config.id = 1; diff --git a/openraft/src/engine/tests/handle_vote_resp_test.rs b/openraft/src/engine/tests/handle_vote_resp_test.rs index e6066b60b..b3c43520c 100644 --- a/openraft/src/engine/tests/handle_vote_resp_test.rs +++ b/openraft/src/engine/tests/handle_vote_resp_test.rs @@ -24,16 +24,16 @@ use crate::Membership; use crate::TokioInstant; use crate::Vote; -fn m12() -> Membership { - Membership::::new(vec![btreeset! {1,2}], None) +fn m12() -> Membership { + Membership::::new(vec![btreeset! {1,2}], None) } -fn m1234() -> Membership { - Membership::::new(vec![btreeset! {1,2,3,4}], None) +fn m1234() -> Membership { + Membership::::new(vec![btreeset! {1,2,3,4}], None) } fn eng() -> Engine { - let mut eng = Engine::default(); + let mut eng = Engine::testing_default(0); eng.state.enable_validation(false); // Disable validation for incomplete state eng.state.log_ids = LogIdList::new([LogId::new(CommittedLeaderId::new(0, 0), 0)]); diff --git a/openraft/src/engine/tests/initialize_test.rs b/openraft/src/engine/tests/initialize_test.rs index cf580c84d..8f0a2e353 100644 --- a/openraft/src/engine/tests/initialize_test.rs +++ b/openraft/src/engine/tests/initialize_test.rs @@ -24,7 +24,7 @@ use crate::Vote; #[test] fn test_initialize_single_node() -> anyhow::Result<()> { let eng = || { - let mut eng = Engine::::default(); + let mut eng = Engine::::testing_default(0); eng.state.enable_validation(false); // Disable validation for incomplete state eng.state.server_state = eng.calc_server_state(); @@ -36,7 +36,7 @@ fn test_initialize_single_node() -> anyhow::Result<()> { index: 0, }; - let m1 = || Membership::::new(vec![btreeset! {1}], None); + let m1 = || Membership::::new(vec![btreeset! {1}], None); let entry = Entry::::new_membership(LogId::default(), m1()); tracing::info!("--- ok: init empty node 1 with membership(1,2)"); @@ -97,7 +97,7 @@ fn test_initialize_single_node() -> anyhow::Result<()> { #[test] fn test_initialize() -> anyhow::Result<()> { let eng = || { - let mut eng = Engine::::default(); + let mut eng = Engine::::testing_default(0); eng.state.enable_validation(false); // Disable validation for incomplete state eng.state.server_state = eng.calc_server_state(); @@ -109,7 +109,7 @@ fn test_initialize() -> anyhow::Result<()> { index: 0, }; - let m12 = || Membership::::new(vec![btreeset! {1,2}], None); + let m12 = || Membership::::new(vec![btreeset! {1,2}], None); let entry = || Entry::::new_membership(LogId::default(), m12()); tracing::info!("--- ok: init empty node 1 with membership(1,2)"); diff --git a/openraft/src/engine/tests/startup_test.rs b/openraft/src/engine/tests/startup_test.rs index e932e8c65..5547aa4d7 100644 --- a/openraft/src/engine/tests/startup_test.rs +++ b/openraft/src/engine/tests/startup_test.rs @@ -15,20 +15,20 @@ use crate::ServerState; use crate::TokioInstant; use crate::Vote; -fn m_empty() -> Membership { - Membership::::new(vec![btreeset! {}], None) +fn m_empty() -> Membership { + Membership::::new(vec![btreeset! {}], None) } -fn m23() -> Membership { - Membership::::new(vec![btreeset! {2,3}], None) +fn m23() -> Membership { + Membership::::new(vec![btreeset! {2,3}], None) } -fn m34() -> Membership { - Membership::::new(vec![btreeset! {3,4}], None) +fn m34() -> Membership { + Membership::::new(vec![btreeset! {3,4}], None) } fn eng() -> Engine { - let mut eng = Engine::default(); + let mut eng = Engine::testing_default(0); eng.config.id = 2; // This will be overridden eng.state.server_state = ServerState::default(); diff --git a/openraft/src/engine/tests/trigger_purge_log_test.rs b/openraft/src/engine/tests/trigger_purge_log_test.rs index 1ae42bbb5..2f91edb38 100644 --- a/openraft/src/engine/tests/trigger_purge_log_test.rs +++ b/openraft/src/engine/tests/trigger_purge_log_test.rs @@ -17,12 +17,12 @@ use crate::MembershipState; use crate::SnapshotMeta; use crate::StoredMembership; -fn m12() -> Membership { - Membership::::new(vec![btreeset! {1,2}], None) +fn m12() -> Membership { + Membership::::new(vec![btreeset! {1,2}], None) } fn eng() -> Engine { - let mut eng = Engine::default(); + let mut eng = Engine::testing_default(0); eng.state.enable_validation(false); // Disable validation for incomplete state eng.state.membership_state = MembershipState::new( EffectiveMembership::new_arc(Some(log_id(1, 0, 1)), m12()), diff --git a/openraft/src/entry/mod.rs b/openraft/src/entry/mod.rs index b413e5bf7..75a7c1302 100644 --- a/openraft/src/entry/mod.rs +++ b/openraft/src/entry/mod.rs @@ -101,7 +101,7 @@ where C: RaftTypeConfig self.payload.is_blank() } - fn get_membership(&self) -> Option<&Membership> { + fn get_membership(&self) -> Option<&Membership> { self.payload.get_membership() } } @@ -128,7 +128,7 @@ where C: RaftTypeConfig } } - fn new_membership(log_id: LogId, m: Membership) -> Self { + fn new_membership(log_id: LogId, m: Membership) -> Self { Self { log_id, payload: EntryPayload::Membership(m), diff --git a/openraft/src/entry/payload.rs b/openraft/src/entry/payload.rs index f51686423..a8d8277d9 100644 --- a/openraft/src/entry/payload.rs +++ b/openraft/src/entry/payload.rs @@ -8,7 +8,7 @@ use crate::RaftTypeConfig; /// Log entry payload variants. #[derive(PartialEq)] -#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] +#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize), serde(bound = ""))] pub enum EntryPayload { /// An empty payload committed by a new cluster leader. Blank, @@ -16,7 +16,7 @@ pub enum EntryPayload { Normal(C::D), /// A change-membership log entry. - Membership(Membership), + Membership(Membership), } impl Clone for EntryPayload @@ -64,7 +64,7 @@ impl RaftPayload for EntryPayload { matches!(self, EntryPayload::Blank) } - fn get_membership(&self) -> Option<&Membership> { + fn get_membership(&self) -> Option<&Membership> { if let EntryPayload::Membership(m) = self { Some(m) } else { diff --git a/openraft/src/entry/traits.rs b/openraft/src/entry/traits.rs index 627b7dd74..59d24f469 100644 --- a/openraft/src/entry/traits.rs +++ b/openraft/src/entry/traits.rs @@ -17,7 +17,7 @@ where C: RaftTypeConfig fn is_blank(&self) -> bool; /// Return `Some(&Membership)` if the entry payload is a membership payload. - fn get_membership(&self) -> Option<&Membership>; + fn get_membership(&self) -> Option<&Membership>; } /// Defines operations on an entry. @@ -34,7 +34,7 @@ where /// Create a new membership log entry. /// /// The returned instance must return `Some()` for `Self::get_membership()`. - fn new_membership(log_id: LogId, m: Membership) -> Self; + fn new_membership(log_id: LogId, m: Membership) -> Self; } /// Build a raft log entry from app data. diff --git a/openraft/src/error.rs b/openraft/src/error.rs index cf7f2a065..5fb1acc60 100644 --- a/openraft/src/error.rs +++ b/openraft/src/error.rs @@ -14,13 +14,12 @@ use anyerror::AnyError; pub use self::replication_closed::ReplicationClosed; pub use self::streaming_error::StreamingError; use crate::network::RPCTypes; -use crate::node::Node; use crate::raft::AppendEntriesResponse; use crate::raft_types::SnapshotSegmentId; use crate::try_as_ref::TryAsRef; use crate::LogId; use crate::Membership; -use crate::NodeId; +use crate::RaftTypeConfig; use crate::StorageError; use crate::Vote; @@ -30,21 +29,21 @@ use crate::Vote; /// error, or an API error `E`. #[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] -pub enum RaftError -where NID: NodeId +pub enum RaftError +where C: RaftTypeConfig { #[error(transparent)] APIError(E), - // Reset serde trait bound for NID but not for E + // Reset serde trait bound for C but not for E #[cfg_attr(feature = "serde", serde(bound = ""))] #[error(transparent)] - Fatal(#[from] Fatal), + Fatal(#[from] Fatal), } -impl RaftError +impl RaftError where - NID: NodeId, + C: RaftTypeConfig, E: Debug, { /// Return a reference to Self::APIError. @@ -64,7 +63,7 @@ where } /// Return a reference to Self::Fatal. - pub fn fatal(&self) -> Option<&Fatal> { + pub fn fatal(&self) -> Option<&Fatal> { match self { RaftError::APIError(_) => None, RaftError::Fatal(f) => Some(f), @@ -72,7 +71,7 @@ where } /// Try to convert self to Fatal error. - pub fn into_fatal(self) -> Option> { + pub fn into_fatal(self) -> Option> { match self { RaftError::APIError(_) => None, RaftError::Fatal(f) => Some(f), @@ -80,11 +79,8 @@ where } /// Return a reference to ForwardToLeader if Self::APIError contains it. - pub fn forward_to_leader(&self) -> Option<&ForwardToLeader> - where - N: Node, - E: TryAsRef>, - { + pub fn forward_to_leader(&self) -> Option<&ForwardToLeader> + where E: TryAsRef> { match self { RaftError::APIError(api_err) => api_err.try_as_ref(), RaftError::Fatal(_) => None, @@ -92,11 +88,8 @@ where } /// Try to convert self to ForwardToLeader error if APIError is a ForwardToLeader error. - pub fn into_forward_to_leader(self) -> Option> - where - N: Node, - E: TryInto>, - { + pub fn into_forward_to_leader(self) -> Option> + where E: TryInto> { match self { RaftError::APIError(api_err) => api_err.try_into().ok(), RaftError::Fatal(_) => None, @@ -104,10 +97,10 @@ where } } -impl From> for RaftError -where NID: NodeId +impl From> for RaftError +where C: RaftTypeConfig { - fn from(se: StorageError) -> Self { + fn from(se: StorageError) -> Self { RaftError::Fatal(Fatal::from(se)) } } @@ -115,11 +108,11 @@ where NID: NodeId /// Fatal is unrecoverable and shuts down raft at once. #[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize), serde(bound = ""))] -pub enum Fatal -where NID: NodeId +pub enum Fatal +where C: RaftTypeConfig { #[error(transparent)] - StorageError(#[from] StorageError), + StorageError(#[from] StorageError), #[error("panicked")] Panicked, @@ -141,24 +134,20 @@ pub enum InstallSnapshotError { /// An error related to a is_leader request. #[derive(Debug, Clone, thiserror::Error, derive_more::TryInto)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize), serde(bound = ""))] -pub enum CheckIsLeaderError -where - NID: NodeId, - N: Node, +pub enum CheckIsLeaderError +where C: RaftTypeConfig { #[error(transparent)] - ForwardToLeader(#[from] ForwardToLeader), + ForwardToLeader(#[from] ForwardToLeader), #[error(transparent)] - QuorumNotEnough(#[from] QuorumNotEnough), + QuorumNotEnough(#[from] QuorumNotEnough), } -impl TryAsRef> for CheckIsLeaderError -where - NID: NodeId, - N: Node, +impl TryAsRef> for CheckIsLeaderError +where C: RaftTypeConfig { - fn try_as_ref(&self) -> Option<&ForwardToLeader> { + fn try_as_ref(&self) -> Option<&ForwardToLeader> { match self { Self::ForwardToLeader(f) => Some(f), _ => None, @@ -170,25 +159,21 @@ where #[derive(Debug, Clone, thiserror::Error, derive_more::TryInto)] #[derive(PartialEq, Eq)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize), serde(bound = ""))] -pub enum ClientWriteError -where - NID: NodeId, - N: Node, +pub enum ClientWriteError +where C: RaftTypeConfig { #[error(transparent)] - ForwardToLeader(#[from] ForwardToLeader), + ForwardToLeader(#[from] ForwardToLeader), /// When writing a change-membership entry. #[error(transparent)] - ChangeMembershipError(#[from] ChangeMembershipError), + ChangeMembershipError(#[from] ChangeMembershipError), } -impl TryAsRef> for ClientWriteError -where - NID: NodeId, - N: Node, +impl TryAsRef> for ClientWriteError +where C: RaftTypeConfig { - fn try_as_ref(&self) -> Option<&ForwardToLeader> { + fn try_as_ref(&self) -> Option<&ForwardToLeader> { match self { Self::ForwardToLeader(f) => Some(f), _ => None, @@ -199,42 +184,38 @@ where /// The set of errors which may take place when requesting to propose a config change. #[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize), serde(bound = ""))] -pub enum ChangeMembershipError { +pub enum ChangeMembershipError { #[error(transparent)] - InProgress(#[from] InProgress), + InProgress(#[from] InProgress), #[error(transparent)] EmptyMembership(#[from] EmptyMembership), #[error(transparent)] - LearnerNotFound(#[from] LearnerNotFound), + LearnerNotFound(#[from] LearnerNotFound), } /// The set of errors which may take place when initializing a pristine Raft node. #[derive(Debug, Clone, PartialEq, Eq, thiserror::Error, derive_more::TryInto)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize), serde(bound = ""))] -pub enum InitializeError -where - NID: NodeId, - N: Node, +pub enum InitializeError +where C: RaftTypeConfig { #[error(transparent)] - NotAllowed(#[from] NotAllowed), + NotAllowed(#[from] NotAllowed), #[error(transparent)] - NotInMembers(#[from] NotInMembers), + NotInMembers(#[from] NotInMembers), } /// Error variants related to the Replication. #[derive(Debug, thiserror::Error)] #[allow(clippy::large_enum_variant)] -pub(crate) enum ReplicationError -where - NID: NodeId, - N: Node, +pub(crate) enum ReplicationError +where C: RaftTypeConfig { #[error(transparent)] - HigherVote(#[from] HigherVote), + HigherVote(#[from] HigherVote), #[error(transparent)] Closed(#[from] ReplicationClosed), @@ -242,15 +223,15 @@ where // TODO(xp): two sub type: StorageError / TransportError // TODO(xp): a sub error for just append_entries() #[error(transparent)] - StorageError(#[from] StorageError), + StorageError(#[from] StorageError), #[error(transparent)] - RPCError(#[from] RPCError>), + RPCError(#[from] RPCError>), } /// Error occurs when invoking a remote raft API. #[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] -// NID already has serde bound. +// C already has serde bound. // E still needs additional serde bound. // `serde(bound="")` does not work in this case. #[cfg_attr( @@ -259,9 +240,9 @@ where serde(bound(serialize = "E: serde::Serialize")), serde(bound(deserialize = "E: for <'d> serde::Deserialize<'d>")) )] -pub enum RPCError { +pub enum RPCError { #[error(transparent)] - Timeout(#[from] Timeout), + Timeout(#[from] Timeout), /// The node is temporarily unreachable and should backoff before retrying. #[error(transparent)] @@ -276,18 +257,17 @@ pub enum RPCError { Network(#[from] NetworkError), #[error(transparent)] - RemoteError(#[from] RemoteError), + RemoteError(#[from] RemoteError), } -impl RPCError> +impl RPCError> where - NID: NodeId, - N: Node, + C: RaftTypeConfig, E: Error, { /// Return a reference to ForwardToLeader error if Self::RemoteError contains one. - pub fn forward_to_leader(&self) -> Option<&ForwardToLeader> - where E: TryAsRef> { + pub fn forward_to_leader(&self) -> Option<&ForwardToLeader> + where E: TryAsRef> { match self { RPCError::Timeout(_) => None, RPCError::Unreachable(_) => None, @@ -301,23 +281,25 @@ where #[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] #[error("error occur on remote peer {target}: {source}")] -pub struct RemoteError { +pub struct RemoteError +where C: RaftTypeConfig +{ #[cfg_attr(feature = "serde", serde(bound = ""))] - pub target: NID, + pub target: C::NodeId, #[cfg_attr(feature = "serde", serde(bound = ""))] - pub target_node: Option, + pub target_node: Option, pub source: T, } -impl RemoteError { - pub fn new(target: NID, e: T) -> Self { +impl RemoteError { + pub fn new(target: C::NodeId, e: T) -> Self { Self { target, target_node: None, source: e, } } - pub fn new_with_node(target: NID, node: N, e: T) -> Self { + pub fn new_with_node(target: C::NodeId, node: C::Node, e: T) -> Self { Self { target, target_node: Some(node), @@ -326,13 +308,12 @@ impl RemoteError { } } -impl From>> for RemoteError> +impl From>> for RemoteError> where - NID: NodeId, - N: Node, + C: RaftTypeConfig, E: Error, { - fn from(e: RemoteError>) -> Self { + fn from(e: RemoteError>) -> Self { RemoteError { target: e.target, target_node: e.target_node, @@ -344,9 +325,9 @@ where #[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize), serde(bound = ""))] #[error("seen a higher vote: {higher} GT mine: {mine}")] -pub struct HigherVote { - pub higher: Vote, - pub mine: Vote, +pub struct HigherVote { + pub higher: Vote, + pub mine: Vote, } /// Error that indicates a **temporary** network error and when it is returned, Openraft will retry @@ -416,7 +397,7 @@ impl Unreachable { /// fn append_entries(&self, /// rpc: AppendEntriesRequest, /// option: RPCOption -/// ) -> Result<_, RPCError>> { +/// ) -> Result<_, RPCError>> { /// if rpc.entries.len() > 10 { /// return Err(PayloadTooLarge::new_entries_hint(10).into()); /// } @@ -528,37 +509,25 @@ impl PayloadTooLarge { #[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize), serde(bound = ""))] #[error("timeout after {timeout:?} when {action} {id}->{target}")] -pub struct Timeout { +pub struct Timeout { pub action: RPCTypes, - pub id: NID, - pub target: NID, + pub id: C::NodeId, + pub target: C::NodeId, pub timeout: Duration, } -#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] -#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize), serde(bound = ""))] -#[error("store has no log at: {index:?}, last purged: {last_purged_log_id:?}")] -pub struct LackEntry { - pub index: Option, - pub last_purged_log_id: Option>, -} - #[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize), serde(bound = ""))] #[error("has to forward request to: {leader_id:?}, {leader_node:?}")] -pub struct ForwardToLeader -where - NID: NodeId, - N: Node, +pub struct ForwardToLeader +where C: RaftTypeConfig { - pub leader_id: Option, - pub leader_node: Option, + pub leader_id: Option, + pub leader_node: Option, } -impl ForwardToLeader -where - NID: NodeId, - N: Node, +impl ForwardToLeader +where C: RaftTypeConfig { pub const fn empty() -> Self { Self { @@ -567,7 +536,7 @@ where } } - pub fn new(leader_id: NID, node: N) -> Self { + pub fn new(leader_id: C::NodeId, node: C::Node) -> Self { Self { leader_id: Some(leader_id), leader_node: Some(node), @@ -586,44 +555,42 @@ pub struct SnapshotMismatch { #[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize), serde(bound = ""))] #[error("not enough for a quorum, cluster: {cluster}, got: {got:?}")] -pub struct QuorumNotEnough { +pub struct QuorumNotEnough { pub cluster: String, - pub got: BTreeSet, + pub got: BTreeSet, } #[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize), serde(bound = ""))] #[error("the cluster is already undergoing a configuration change at log {membership_log_id:?}, last committed membership log id: {committed:?}")] -pub struct InProgress { - pub committed: Option>, - pub membership_log_id: Option>, +pub struct InProgress { + pub committed: Option>, + pub membership_log_id: Option>, } #[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize), serde(bound = ""))] #[error("Learner {node_id} not found: add it as learner before adding it as a voter")] -pub struct LearnerNotFound { - pub node_id: NID, +pub struct LearnerNotFound { + pub node_id: C::NodeId, } #[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize), serde(bound = ""))] #[error("not allowed to initialize due to current raft state: last_log_id: {last_log_id:?} vote: {vote}")] -pub struct NotAllowed { - pub last_log_id: Option>, - pub vote: Vote, +pub struct NotAllowed { + pub last_log_id: Option>, + pub vote: Vote, } #[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize), serde(bound = ""))] #[error("node {node_id} has to be a member. membership:{membership:?}")] -pub struct NotInMembers -where - NID: NodeId, - N: Node, +pub struct NotInMembers +where C: RaftTypeConfig { - pub node_id: NID, - pub membership: Membership, + pub node_id: C::NodeId, + pub membership: Membership, } #[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] @@ -644,17 +611,19 @@ pub enum NoForward {} #[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize), serde(bound = ""))] -pub(crate) enum RejectVoteRequest { +pub(crate) enum RejectVoteRequest { #[error("reject vote request by a greater vote: {0}")] - ByVote(Vote), + ByVote(Vote), #[allow(dead_code)] #[error("reject vote request by a greater last-log-id: {0:?}")] - ByLastLogId(Option>), + ByLastLogId(Option>), } -impl From> for AppendEntriesResponse { - fn from(r: RejectVoteRequest) -> Self { +impl From> for AppendEntriesResponse +where C: RaftTypeConfig +{ + fn from(r: RejectVoteRequest) -> Self { match r { RejectVoteRequest::ByVote(v) => AppendEntriesResponse::HigherVote(v), RejectVoteRequest::ByLastLogId(_) => { @@ -665,19 +634,21 @@ impl From> for AppendEntriesResponse { } #[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] -pub(crate) enum RejectAppendEntries { +pub(crate) enum RejectAppendEntries { #[error("reject AppendEntries by a greater vote: {0}")] - ByVote(Vote), + ByVote(Vote), #[error("reject AppendEntries because of conflicting log-id: {local:?}; expect to be: {expect:?}")] ByConflictingLogId { - expect: LogId, - local: Option>, + expect: LogId, + local: Option>, }, } -impl From> for RejectAppendEntries { - fn from(r: RejectVoteRequest) -> Self { +impl From> for RejectAppendEntries +where C: RaftTypeConfig +{ + fn from(r: RejectVoteRequest) -> Self { match r { RejectVoteRequest::ByVote(v) => RejectAppendEntries::ByVote(v), RejectVoteRequest::ByLastLogId(_) => { @@ -687,8 +658,10 @@ impl From> for RejectAppendEntries { } } -impl From>> for AppendEntriesResponse { - fn from(r: Result<(), RejectAppendEntries>) -> Self { +impl From>> for AppendEntriesResponse +where C: RaftTypeConfig +{ + fn from(r: Result<(), RejectAppendEntries>) -> Self { match r { Ok(_) => AppendEntriesResponse::Success, Err(e) => match e { diff --git a/openraft/src/error/streaming_error.rs b/openraft/src/error/streaming_error.rs index bf2584a91..990f63e6a 100644 --- a/openraft/src/error/streaming_error.rs +++ b/openraft/src/error/streaming_error.rs @@ -32,7 +32,7 @@ pub enum StreamingError { /// Timeout when streaming data to remote node. #[error(transparent)] - Timeout(#[from] Timeout), + Timeout(#[from] Timeout), /// The node is temporarily unreachable and should backoff before retrying. #[error(transparent)] @@ -44,13 +44,13 @@ pub enum StreamingError { /// Remote node returns an error. #[error(transparent)] - RemoteError(#[from] RemoteError), + RemoteError(#[from] RemoteError), } -impl From> for ReplicationError +impl From> for ReplicationError where E: Error, - RaftError: From, + RaftError: From, { fn from(e: StreamingError) -> Self { match e { diff --git a/openraft/src/membership/bench/is_quorum.rs b/openraft/src/membership/bench/is_quorum.rs index 7a781ecbf..c39fce961 100644 --- a/openraft/src/membership/bench/is_quorum.rs +++ b/openraft/src/membership/bench/is_quorum.rs @@ -4,13 +4,14 @@ use maplit::btreeset; use test::black_box; use test::Bencher; +use crate::engine::testing::UTConfig; use crate::quorum::QuorumSet; use crate::EffectiveMembership; use crate::Membership; #[bench] fn m12345_ids_slice(b: &mut Bencher) { - let m = Membership::::new(vec![btreeset! {1,2,3,4,5}], None); + let m = Membership::::new(vec![btreeset! {1,2,3,4,5}], None); let m = EffectiveMembership::new(None, m); let x = [1, 2, 3, 6, 7]; @@ -19,7 +20,7 @@ fn m12345_ids_slice(b: &mut Bencher) { #[bench] fn m12345_ids_btreeset(b: &mut Bencher) { - let m = Membership::::new(vec![btreeset! {1,2,3,4,5}], None); + let m = Membership::::new(vec![btreeset! {1,2,3,4,5}], None); let m = EffectiveMembership::new(None, m); let x = btreeset! {1, 2, 3, 6, 7}; @@ -28,7 +29,7 @@ fn m12345_ids_btreeset(b: &mut Bencher) { #[bench] fn m12345_678_ids_slice(b: &mut Bencher) { - let m = Membership::::new(vec![btreeset! {1,2,3,4,5}], None); + let m = Membership::::new(vec![btreeset! {1,2,3,4,5}], None); let m = EffectiveMembership::new(None, m); let x = [1, 2, 3, 6, 7]; @@ -37,7 +38,7 @@ fn m12345_678_ids_slice(b: &mut Bencher) { #[bench] fn m12345_678_ids_btreeset(b: &mut Bencher) { - let m = Membership::::new(vec![btreeset! {1,2,3,4,5}], None); + let m = Membership::::new(vec![btreeset! {1,2,3,4,5}], None); let m = EffectiveMembership::new(None, m); let x = btreeset! {1, 2, 3, 6, 7}; diff --git a/openraft/src/membership/effective_membership.rs b/openraft/src/membership/effective_membership.rs index 8b223ef50..06cd4bf0e 100644 --- a/openraft/src/membership/effective_membership.rs +++ b/openraft/src/membership/effective_membership.rs @@ -3,13 +3,12 @@ use std::fmt::Debug; use std::sync::Arc; use crate::log_id::RaftLogId; -use crate::node::Node; use crate::quorum::Joint; use crate::quorum::QuorumSet; use crate::LogId; use crate::Membership; use crate::MessageSummary; -use crate::NodeId; +use crate::RaftTypeConfig; use crate::StoredMembership; /// The currently active membership config. @@ -20,24 +19,20 @@ use crate::StoredMembership; /// /// An active config is just the last seen config in raft spec. #[derive(Clone, Default, Eq)] -pub struct EffectiveMembership -where - N: Node, - NID: NodeId, +pub struct EffectiveMembership +where C: RaftTypeConfig { - stored_membership: Arc>, + stored_membership: Arc>, /// The quorum set built from `membership`. - quorum_set: Joint, Vec>>, + quorum_set: Joint, Vec>>, /// Cache of union of all members - voter_ids: BTreeSet, + voter_ids: BTreeSet, } -impl Debug for EffectiveMembership -where - N: Node, - NID: NodeId, +impl Debug for EffectiveMembership +where C: RaftTypeConfig { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("EffectiveMembership") @@ -48,37 +43,32 @@ where } } -impl PartialEq for EffectiveMembership -where - N: Node, - NID: NodeId, +impl PartialEq for EffectiveMembership +where C: RaftTypeConfig { fn eq(&self, other: &Self) -> bool { self.stored_membership == other.stored_membership && self.voter_ids == other.voter_ids } } -impl From<(&LID, Membership)> for EffectiveMembership +impl From<(&LID, Membership)> for EffectiveMembership where - N: Node, - NID: NodeId, - LID: RaftLogId, + C: RaftTypeConfig, + LID: RaftLogId, { - fn from(v: (&LID, Membership)) -> Self { + fn from(v: (&LID, Membership)) -> Self { EffectiveMembership::new(Some(*v.0.get_log_id()), v.1) } } -impl EffectiveMembership -where - N: Node, - NID: NodeId, +impl EffectiveMembership +where C: RaftTypeConfig { - pub(crate) fn new_arc(log_id: Option>, membership: Membership) -> Arc { + pub(crate) fn new_arc(log_id: Option>, membership: Membership) -> Arc { Arc::new(Self::new(log_id, membership)) } - pub fn new(log_id: Option>, membership: Membership) -> Self { + pub fn new(log_id: Option>, membership: Membership) -> Self { let voter_ids = membership.voter_ids().collect(); let configs = membership.get_joint_config(); @@ -96,51 +86,49 @@ where } } - pub(crate) fn new_from_stored_membership(stored: StoredMembership) -> Self { + pub(crate) fn new_from_stored_membership(stored: StoredMembership) -> Self { Self::new(*stored.log_id(), stored.membership().clone()) } - pub(crate) fn stored_membership(&self) -> &Arc> { + pub(crate) fn stored_membership(&self) -> &Arc> { &self.stored_membership } - pub fn log_id(&self) -> &Option> { + pub fn log_id(&self) -> &Option> { self.stored_membership.log_id() } - pub fn membership(&self) -> &Membership { + pub fn membership(&self) -> &Membership { self.stored_membership.membership() } } /// Membership API -impl EffectiveMembership -where - N: Node, - NID: NodeId, +impl EffectiveMembership +where C: RaftTypeConfig { #[allow(dead_code)] - pub(crate) fn is_voter(&self, nid: &NID) -> bool { + pub(crate) fn is_voter(&self, nid: &C::NodeId) -> bool { self.membership().is_voter(nid) } /// Returns an Iterator of all voter node ids. Learners are not included. - pub fn voter_ids(&self) -> impl Iterator + '_ { + pub fn voter_ids(&self) -> impl Iterator + '_ { self.voter_ids.iter().copied() } /// Returns an Iterator of all learner node ids. Voters are not included. - pub(crate) fn learner_ids(&self) -> impl Iterator + '_ { + pub(crate) fn learner_ids(&self) -> impl Iterator + '_ { self.membership().learner_ids() } /// Get a the node(either voter or learner) by node id. - pub fn get_node(&self, node_id: &NID) -> Option<&N> { + pub fn get_node(&self, node_id: &C::NodeId) -> Option<&C::Node> { self.membership().get_node(node_id) } /// Returns an Iterator of all nodes(voters and learners). - pub fn nodes(&self) -> impl Iterator { + pub fn nodes(&self) -> impl Iterator { self.membership().nodes() } @@ -148,15 +136,13 @@ where /// /// Membership is defined by a joint of multiple configs. /// Each config is a vec of node-id. - pub fn get_joint_config(&self) -> &Vec> { + pub fn get_joint_config(&self) -> &Vec> { self.quorum_set.children() } } -impl MessageSummary> for EffectiveMembership -where - N: Node, - NID: NodeId, +impl MessageSummary> for EffectiveMembership +where C: RaftTypeConfig { fn summary(&self) -> String { format!( @@ -168,14 +154,12 @@ where } /// Implement node-id joint quorum set. -impl QuorumSet for EffectiveMembership -where - N: Node, - NID: NodeId, +impl QuorumSet for EffectiveMembership +where C: RaftTypeConfig { - type Iter = std::collections::btree_set::IntoIter; + type Iter = std::collections::btree_set::IntoIter; - fn is_quorum<'a, I: Iterator + Clone>(&self, ids: I) -> bool { + fn is_quorum<'a, I: Iterator + Clone>(&self, ids: I) -> bool { self.quorum_set.is_quorum(ids) } diff --git a/openraft/src/membership/effective_membership_test.rs b/openraft/src/membership/effective_membership_test.rs index ac81506c5..c2e27b7e2 100644 --- a/openraft/src/membership/effective_membership_test.rs +++ b/openraft/src/membership/effective_membership_test.rs @@ -1,5 +1,6 @@ use maplit::btreeset; +use crate::engine::testing::UTConfig; use crate::quorum::QuorumSet; use crate::EffectiveMembership; use crate::Membership; @@ -7,8 +8,8 @@ use crate::Membership; #[test] fn test_effective_membership_majority() -> anyhow::Result<()> { { - let m12345 = Membership::::new(vec![btreeset! {1,2,3,4,5 }], None); - let m = EffectiveMembership::new(None, m12345); + let m12345 = Membership::::new(vec![btreeset! {1,2,3,4,5 }], None); + let m = EffectiveMembership::::new(None, m12345); assert!(!m.is_quorum([0].iter())); assert!(!m.is_quorum([0, 1, 2].iter())); @@ -19,8 +20,8 @@ fn test_effective_membership_majority() -> anyhow::Result<()> { } { - let m12345_678 = Membership::::new(vec![btreeset! {1,2,3,4,5 }, btreeset! {6,7,8}], None); - let m = EffectiveMembership::new(None, m12345_678); + let m12345_678 = Membership::::new(vec![btreeset! {1,2,3,4,5 }, btreeset! {6,7,8}], None); + let m = EffectiveMembership::::new(None, m12345_678); assert!(!m.is_quorum([0].iter())); assert!(!m.is_quorum([0, 1, 2].iter())); diff --git a/openraft/src/membership/membership.rs b/openraft/src/membership/membership.rs index 766b838c7..ff1c65fc8 100644 --- a/openraft/src/membership/membership.rs +++ b/openraft/src/membership/membership.rs @@ -6,14 +6,13 @@ use crate::error::ChangeMembershipError; use crate::error::EmptyMembership; use crate::error::LearnerNotFound; use crate::membership::IntoNodes; -use crate::node::Node; use crate::quorum::AsJoint; use crate::quorum::FindCoherent; use crate::quorum::Joint; use crate::quorum::QuorumSet; use crate::ChangeMembers; use crate::MessageSummary; -use crate::NodeId; +use crate::RaftTypeConfig; /// The membership configuration of the cluster. /// @@ -21,47 +20,39 @@ use crate::NodeId; /// of a majority of every config. #[derive(Clone, Debug, Default, PartialEq, Eq)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize), serde(bound = ""))] -pub struct Membership -where - N: Node, - NID: NodeId, +pub struct Membership +where C: RaftTypeConfig { /// Multi configs of members. /// /// AKA a joint config in original raft paper. - configs: Vec>, + configs: Vec>, /// Additional info of all nodes, e.g., the connecting host and port. /// /// A node-id key that is in `nodes` but is not in `configs` is a **learner**. - nodes: BTreeMap, + nodes: BTreeMap, } -impl From> for Membership -where - N: Node, - NID: NodeId, +impl From> for Membership +where C: RaftTypeConfig { - fn from(b: BTreeMap) -> Self { - let member_ids = b.keys().cloned().collect::>(); + fn from(b: BTreeMap) -> Self { + let member_ids = b.keys().cloned().collect::>(); Membership::new_unchecked(vec![member_ids], b) } } -impl fmt::Display for Membership -where - N: Node, - NID: NodeId, +impl fmt::Display for Membership +where C: RaftTypeConfig { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.summary()) } } -impl MessageSummary> for Membership -where - N: Node, - NID: NodeId, +impl MessageSummary> for Membership +where C: RaftTypeConfig { fn summary(&self) -> String { let mut res = vec!["voters:[".to_string()]; @@ -104,10 +95,8 @@ where } // Public APIs -impl Membership -where - N: Node, - NID: NodeId, +impl Membership +where C: RaftTypeConfig { /// Create a new Membership from a joint config of voter-ids and a collection of all /// `Node`(voter nodes and learner nodes). @@ -121,8 +110,8 @@ where /// - `BTreeSet` provides learner node ids whose `Node` data are `Node::default()`, /// - `BTreeMap` provides nodes for every node id. Node ids that are not in /// `configs` are learners. - pub fn new(config: Vec>, nodes: T) -> Self - where T: IntoNodes { + pub fn new(config: Vec>, nodes: T) -> Self + where T: IntoNodes { let voter_ids = config.as_joint().ids().collect::>(); let nodes = Self::extend_nodes(nodes.into_nodes(), &voter_ids.into_nodes()); @@ -136,43 +125,41 @@ where /// /// The returned `Vec` contains one or more configs(currently it is two). If there is only one /// config, it is in a uniform config, otherwise, it is in a joint consensus. - pub fn get_joint_config(&self) -> &Vec> { + pub fn get_joint_config(&self) -> &Vec> { &self.configs } /// Returns an Iterator of all nodes(voters and learners). - pub fn nodes(&self) -> impl Iterator { + pub fn nodes(&self) -> impl Iterator { self.nodes.iter() } /// Get a the node(either voter or learner) by node id. - pub fn get_node(&self, node_id: &NID) -> Option<&N> { + pub fn get_node(&self, node_id: &C::NodeId) -> Option<&C::Node> { self.nodes.get(node_id) } /// Returns an Iterator of all voter node ids. Learners are not included. - pub fn voter_ids(&self) -> impl Iterator { + pub fn voter_ids(&self) -> impl Iterator { self.configs.as_joint().ids() } /// Returns an Iterator of all learner node ids. Voters are not included. - pub fn learner_ids(&self) -> impl Iterator + '_ { + pub fn learner_ids(&self) -> impl Iterator + '_ { self.nodes.keys().filter(|x| !self.is_voter(x)).copied() } } -impl Membership -where - N: Node, - NID: NodeId, +impl Membership +where C: RaftTypeConfig { /// Return true if the given node id is an either voter or learner. - pub(crate) fn contains(&self, node_id: &NID) -> bool { + pub(crate) fn contains(&self, node_id: &C::NodeId) -> bool { self.nodes.contains_key(node_id) } /// Check if the given `NodeId` exists and is a voter. - pub(crate) fn is_voter(&self, node_id: &NID) -> bool { + pub(crate) fn is_voter(&self, node_id: &C::NodeId) -> bool { for c in self.configs.iter() { if c.contains(node_id) { return true; @@ -183,8 +170,8 @@ where /// Create a new Membership the same as [`Self::new()`], but does not add default value /// `Node::default()` if a voter id is not in `nodes`. Thus it may create an invalid instance. - pub(crate) fn new_unchecked(configs: Vec>, nodes: T) -> Self - where T: IntoNodes { + pub(crate) fn new_unchecked(configs: Vec>, nodes: T) -> Self + where T: IntoNodes { let nodes = nodes.into_nodes(); Membership { configs, nodes } } @@ -193,7 +180,10 @@ where /// /// Node that present in `old` will **NOT** be replaced because changing the address of a node /// potentially breaks consensus guarantee. - pub(crate) fn extend_nodes(old: BTreeMap, new: &BTreeMap) -> BTreeMap { + pub(crate) fn extend_nodes( + old: BTreeMap, + new: &BTreeMap, + ) -> BTreeMap { let mut res = old; for (k, v) in new.iter() { @@ -209,7 +199,7 @@ where /// Ensure the membership config is valid: /// - No empty sub-config in it. /// - Every voter has a corresponding Node. - pub(crate) fn ensure_valid(&self) -> Result<(), ChangeMembershipError> { + pub(crate) fn ensure_valid(&self) -> Result<(), ChangeMembershipError> { self.ensure_non_empty_config()?; self.ensure_voter_nodes().map_err(|nid| LearnerNotFound { node_id: nid })?; Ok(()) @@ -229,7 +219,7 @@ where /// Ensures that every vote has a corresponding Node. /// /// If a voter is found not having a Node, it returns the voter node id in an `Err()` - pub(crate) fn ensure_voter_nodes(&self) -> Result<(), NID> { + pub(crate) fn ensure_voter_nodes(&self) -> Result<(), C::NodeId> { for voter_id in self.voter_ids() { if !self.nodes.contains_key(&voter_id) { return Err(voter_id); @@ -263,7 +253,7 @@ where /// curr = next; /// } /// ``` - pub(crate) fn next_coherent(&self, goal: BTreeSet, retain: bool) -> Self { + pub(crate) fn next_coherent(&self, goal: BTreeSet, retain: bool) -> Self { let config = Joint::from(self.configs.clone()).find_coherent(goal).children().clone(); let mut nodes = self.nodes.clone(); @@ -288,9 +278,9 @@ where /// continue to receive log replication from the leader. pub(crate) fn change( mut self, - change: ChangeMembers, + change: ChangeMembers, retain: bool, - ) -> Result> { + ) -> Result> { tracing::debug!(change = debug(&change), "{}", func_name!()); let last = self.get_joint_config().last().unwrap().clone(); @@ -346,7 +336,7 @@ where } /// Build a QuorumSet from current joint config - pub(crate) fn to_quorum_set(&self) -> Joint, Vec>> { + pub(crate) fn to_quorum_set(&self) -> Joint, Vec>> { let mut qs = vec![]; for c in self.get_joint_config().iter() { qs.push(c.iter().copied().collect::>()); @@ -360,6 +350,7 @@ mod tests { use maplit::btreemap; use maplit::btreeset; + use crate::engine::testing::UTConfig; use crate::error::ChangeMembershipError; use crate::error::EmptyMembership; use crate::error::LearnerNotFound; @@ -368,7 +359,7 @@ mod tests { #[test] fn test_membership_ensure_voter_nodes() -> anyhow::Result<()> { - let m = Membership:: { + let m = Membership:: { configs: vec![btreeset! {1,2}], nodes: btreemap! {1=>()}, }; @@ -378,7 +369,7 @@ mod tests { #[test] fn test_membership_change() -> anyhow::Result<()> { - let m = || Membership:: { + let m = || Membership:: { configs: vec![btreeset! {1,2}], nodes: btreemap! {1=>(),2=>(),3=>()}, }; @@ -396,7 +387,7 @@ mod tests { { let res = m().change(ChangeMembers::AddVoterIds(btreeset! {3}), true); assert_eq!( - Ok(Membership:: { + Ok(Membership:: { configs: vec![btreeset! {1,2}, btreeset! {1,2,3}], nodes: btreemap! {1=>(),2=>(),3=>()} }), @@ -408,7 +399,7 @@ mod tests { { let res = m().change(ChangeMembers::AddVoters(btreemap! {5=>()}), true); assert_eq!( - Ok(Membership:: { + Ok(Membership:: { configs: vec![btreeset! {1,2}, btreeset! {1,2,5}], nodes: btreemap! {1=>(),2=>(),3=>(),5=>()} }), @@ -420,7 +411,7 @@ mod tests { { let res = m().change(ChangeMembers::RemoveVoters(btreeset! {5}), true); assert_eq!( - Ok(Membership:: { + Ok(Membership:: { configs: vec![btreeset! {1,2}], nodes: btreemap! {1=>(),2=>(),3=>()} }), @@ -438,7 +429,7 @@ mod tests { { let res = m().change(ChangeMembers::RemoveVoters(btreeset! {1}), true); assert_eq!( - Ok(Membership:: { + Ok(Membership:: { configs: vec![btreeset! {1,2}, btreeset! {2}], nodes: btreemap! {1=>(),2=>(),3=>()} }), @@ -450,7 +441,7 @@ mod tests { { let res = m().change(ChangeMembers::RemoveVoters(btreeset! {1}), false); assert_eq!( - Ok(Membership:: { + Ok(Membership:: { configs: vec![btreeset! {1,2}, btreeset! {2}], nodes: btreemap! {1=>(),2=>(),3=>()} }), @@ -460,13 +451,13 @@ mod tests { // Remove: OK, not retain; learner removed { - let mem = Membership:: { + let mem = Membership:: { configs: vec![btreeset! {1,2}, btreeset! {2}], nodes: btreemap! {1=>(),2=>(),3=>()}, }; let res = mem.change(ChangeMembers::RemoveVoters(btreeset! {1}), false); assert_eq!( - Ok(Membership:: { + Ok(Membership:: { configs: vec![btreeset! {2}], nodes: btreemap! {2=>(),3=>()} }), @@ -478,7 +469,7 @@ mod tests { { let res = m().change(ChangeMembers::ReplaceAllVoters(btreeset! {2}), false); assert_eq!( - Ok(Membership:: { + Ok(Membership:: { configs: vec![btreeset! {1,2}, btreeset! {2}], nodes: btreemap! {1=>(),2=>(),3=>()} }), @@ -490,7 +481,7 @@ mod tests { { let res = m().change(ChangeMembers::AddNodes(btreemap! {2=>()}), false); assert_eq!( - Ok(Membership:: { + Ok(Membership:: { configs: vec![btreeset! {1,2}], nodes: btreemap! {1=>(),2=>(),3=>()} }), @@ -502,7 +493,7 @@ mod tests { { let res = m().change(ChangeMembers::AddNodes(btreemap! {3=>()}), false); assert_eq!( - Ok(Membership:: { + Ok(Membership:: { configs: vec![btreeset! {1,2}], nodes: btreemap! {1=>(),2=>(),3=>()} }), @@ -514,7 +505,7 @@ mod tests { { let res = m().change(ChangeMembers::AddNodes(btreemap! {4=>()}), false); assert_eq!( - Ok(Membership:: { + Ok(Membership:: { configs: vec![btreeset! {1,2}], nodes: btreemap! {1=>(),2=>(),3=>(), 4=>()} }), @@ -524,14 +515,14 @@ mod tests { // SetNodes: Ok { - let m = || Membership:: { + let m = || Membership::> { configs: vec![btreeset! {1,2}], nodes: btreemap! {1=>1,2=>2,3=>3}, }; let res = m().change(ChangeMembers::SetNodes(btreemap! {3=>30, 4=>40}), false); assert_eq!( - Ok(Membership:: { + Ok(Membership::> { configs: vec![btreeset! {1,2}], nodes: btreemap! {1=>1,2=>2,3=>30, 4=>40} }), @@ -552,7 +543,7 @@ mod tests { { let res = m().change(ChangeMembers::RemoveNodes(btreeset! {3}), false); assert_eq!( - Ok(Membership:: { + Ok(Membership:: { configs: vec![btreeset! {1,2}], nodes: btreemap! {1=>(),2=>()} }), @@ -564,7 +555,7 @@ mod tests { { let res = m().change(ChangeMembers::ReplaceAllNodes(btreemap! {1=>(),2=>(),4=>()}), false); assert_eq!( - Ok(Membership:: { + Ok(Membership:: { configs: vec![btreeset! {1,2}], nodes: btreemap! {1=>(),2=>(),4=>()} }), diff --git a/openraft/src/membership/membership_test.rs b/openraft/src/membership/membership_test.rs index bfca59a01..eb01af1b0 100644 --- a/openraft/src/membership/membership_test.rs +++ b/openraft/src/membership/membership_test.rs @@ -5,12 +5,13 @@ use std::fmt::Formatter; use maplit::btreemap; use maplit::btreeset; +use crate::engine::testing::UTConfig; use crate::membership::IntoNodes; use crate::ChangeMembers; use crate::Membership; use crate::MessageSummary; -#[derive(Debug, Clone, Default, PartialEq, Eq)] +#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] pub struct TestNode { pub addr: String, @@ -36,13 +37,13 @@ fn test_membership_summary() -> anyhow::Result<()> { data: btreemap! {k.to_string() => k.to_string()}, }; - let m = Membership::::new(vec![btreeset! {1,2}, btreeset! {3}], None); + let m = Membership::::new(vec![btreeset! {1,2}, btreeset! {3}], None); assert_eq!("voters:[{1:{()},2:{()}},{3:{()}}], learners:[]", m.summary()); - let m = Membership::::new(vec![btreeset! {1,2}, btreeset! {3}], Some(btreeset! {4})); + let m = Membership::::new(vec![btreeset! {1,2}, btreeset! {3}], Some(btreeset! {4})); assert_eq!("voters:[{1:{()},2:{()}},{3:{()}}], learners:[4:{()}]", m.summary()); - let m = Membership::::new_unchecked(vec![btreeset! {1,2}, btreeset! {3}], btreemap! { + let m = Membership::>::new_unchecked(vec![btreeset! {1,2}, btreeset! {3}], btreemap! { 1=>node("127.0.0.1", "k1"), 2=>node("127.0.0.2", "k2"), 3=>node("127.0.0.3", "k3"), @@ -59,9 +60,9 @@ fn test_membership_summary() -> anyhow::Result<()> { #[test] fn test_membership() -> anyhow::Result<()> { - let m1 = Membership::::new(vec![btreeset! {1}], None); - let m123 = Membership::::new(vec![btreeset! {1,2,3}], None); - let m123_345 = Membership::::new(vec![btreeset! {1,2,3}, btreeset! {3,4,5}], None); + let m1 = Membership::::new(vec![btreeset! {1}], None); + let m123 = Membership::::new(vec![btreeset! {1,2,3}], None); + let m123_345 = Membership::::new(vec![btreeset! {1,2,3}, btreeset! {3,4,5}], None); assert_eq!(Some(btreeset! {1}), m1.get_joint_config().first().cloned()); assert_eq!(Some(btreeset! {1,2,3}), m123.get_joint_config().first().cloned()); @@ -90,7 +91,7 @@ fn test_membership() -> anyhow::Result<()> { fn test_membership_with_learners() -> anyhow::Result<()> { // test multi membership with learners { - let m1_2 = Membership::::new(vec![btreeset! {1}], Some(btreeset! {2})); + let m1_2 = Membership::::new(vec![btreeset! {1}], Some(btreeset! {2})); let m1_23 = m1_2.clone().change(ChangeMembers::AddNodes(btreemap! {3=>()}), true)?; // test learner and membership @@ -115,7 +116,7 @@ fn test_membership_with_learners() -> anyhow::Result<()> { // overlapping members and learners { - let s1_2 = Membership::::new(vec![btreeset! {1,2,3}, btreeset! {5,6,7}], Some(btreeset! {3,4,5})); + let s1_2 = Membership::::new(vec![btreeset! {1,2,3}, btreeset! {5,6,7}], Some(btreeset! {3,4,5})); let x = s1_2.learner_ids().collect(); assert_eq!(btreeset! {4}, x); } @@ -130,7 +131,7 @@ fn test_membership_add_learner() -> anyhow::Result<()> { data: Default::default(), }; - let m_1_2 = Membership::::new_unchecked( + let m_1_2 = Membership::>::new_unchecked( vec![btreeset! {1}, btreeset! {2}], btreemap! {1=>node("1"), 2=>node("2")}, ); @@ -144,7 +145,7 @@ fn test_membership_add_learner() -> anyhow::Result<()> { let m_1_2_3 = m_1_2.change(ChangeMembers::AddNodes(btreemap! {3=>node("3")}), true)?; assert_eq!( - Membership::::new_unchecked( + Membership::>::new_unchecked( vec![btreeset! {1}, btreeset! {2}], btreemap! {1=>node("1"), 2=>node("2"), 3=>node("3")} ), @@ -161,14 +162,14 @@ fn test_membership_update_nodes() -> anyhow::Result<()> { data: Default::default(), }; - let m_1_2 = Membership::::new_unchecked( + let m_1_2 = Membership::>::new_unchecked( vec![btreeset! {1}, btreeset! {2}], btreemap! {1=>node("1"), 2=>node("2")}, ); let m_1_2_3 = m_1_2.change(ChangeMembers::SetNodes(btreemap! {2=>node("20"), 3=>node("30")}), true)?; assert_eq!( - Membership::::new_unchecked( + Membership::>::new_unchecked( vec![btreeset! {1}, btreeset! {2}], btreemap! {1=>node("1"), 2=>node("20"), 3=>node("30")} ), @@ -185,7 +186,7 @@ fn test_membership_extend_nodes() -> anyhow::Result<()> { data: Default::default(), }; - let ext = |a, b| Membership::::extend_nodes(a, &b); + let ext = |a, b| Membership::>::extend_nodes(a, &b); assert_eq!( btreemap! {1=>node("1")}, @@ -205,7 +206,7 @@ fn test_membership_extend_nodes() -> anyhow::Result<()> { #[test] fn test_membership_with_nodes() -> anyhow::Result<()> { let node = TestNode::default; - let with_nodes = |nodes| Membership::::new_unchecked(vec![btreeset! {1}, btreeset! {2}], nodes); + let with_nodes = |nodes| Membership::>::new_unchecked(vec![btreeset! {1}, btreeset! {2}], nodes); let res = with_nodes(btreemap! {1=>node(), 2=>node()}); assert_eq!( @@ -230,7 +231,7 @@ fn test_membership_next_coherent() -> anyhow::Result<()> { let c3 = || btreeset! {7,8,9}; #[allow(clippy::redundant_closure)] - let new_mem = |voter_ids, ns| Membership::::new(voter_ids, ns); + let new_mem = |voter_ids, ns| Membership::::new(voter_ids, ns); let m1 = new_mem(vec![c1()], nodes()); let m12 = new_mem(vec![c1(), c2()], nodes()); @@ -254,8 +255,8 @@ fn test_membership_next_coherent() -> anyhow::Result<()> { let old_learners = || btreeset! {1, 2}; let learners = || btreeset! {1, 2, 3, 4, 5}; - let m23_with_learners_old = Membership::::new(vec![c2(), c3()], Some(old_learners())); - let m23_with_learners_new = Membership::::new(vec![c3()], Some(learners())); + let m23_with_learners_old = Membership::::new(vec![c2(), c3()], Some(old_learners())); + let m23_with_learners_new = Membership::::new(vec![c3()], Some(learners())); assert_eq!(m23_with_learners_new, m23_with_learners_old.next_coherent(c3(), true)); Ok(()) @@ -271,7 +272,8 @@ fn test_membership_next_coherent_with_nodes() -> anyhow::Result<()> { let c1 = || btreeset! {1}; let c2 = || btreeset! {2}; - let initial = Membership::::new_unchecked(vec![c1(), c2()], btreemap! {1=>node("1"), 2=>node("2")}); + let initial = + Membership::>::new_unchecked(vec![c1(), c2()], btreemap! {1=>node("1"), 2=>node("2")}); // joint [{2}, {1,2}] diff --git a/openraft/src/membership/stored_membership.rs b/openraft/src/membership/stored_membership.rs index 9d580dc70..56317a4b2 100644 --- a/openraft/src/membership/stored_membership.rs +++ b/openraft/src/membership/stored_membership.rs @@ -4,8 +4,7 @@ use crate::display_ext::DisplayOption; use crate::LogId; use crate::Membership; use crate::MessageSummary; -use crate::Node; -use crate::NodeId; +use crate::RaftTypeConfig; /// This struct represents information about a membership config that has already been stored in the /// raft logs. @@ -19,48 +18,42 @@ use crate::NodeId; #[derive(Clone, Debug, Default)] #[derive(PartialEq, Eq)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize), serde(bound = ""))] -pub struct StoredMembership -where - N: Node, - NID: NodeId, +pub struct StoredMembership +where C: RaftTypeConfig { /// The id of the log that stores this membership config - log_id: Option>, + log_id: Option>, /// Membership config - membership: Membership, + membership: Membership, } -impl StoredMembership -where - N: Node, - NID: NodeId, +impl StoredMembership +where C: RaftTypeConfig { - pub fn new(log_id: Option>, membership: Membership) -> Self { + pub fn new(log_id: Option>, membership: Membership) -> Self { Self { log_id, membership } } - pub fn log_id(&self) -> &Option> { + pub fn log_id(&self) -> &Option> { &self.log_id } - pub fn membership(&self) -> &Membership { + pub fn membership(&self) -> &Membership { &self.membership } - pub fn voter_ids(&self) -> impl Iterator { + pub fn voter_ids(&self) -> impl Iterator { self.membership.voter_ids() } - pub fn nodes(&self) -> impl Iterator { + pub fn nodes(&self) -> impl Iterator { self.membership.nodes() } } -impl fmt::Display for StoredMembership -where - N: Node, - NID: NodeId, +impl fmt::Display for StoredMembership +where C: RaftTypeConfig { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( @@ -72,10 +65,8 @@ where } } -impl MessageSummary> for StoredMembership -where - N: Node, - NID: NodeId, +impl MessageSummary> for StoredMembership +where C: RaftTypeConfig { fn summary(&self) -> String { self.to_string() diff --git a/openraft/src/metrics/raft_metrics.rs b/openraft/src/metrics/raft_metrics.rs index c31f7f473..5d8fb43e5 100644 --- a/openraft/src/metrics/raft_metrics.rs +++ b/openraft/src/metrics/raft_metrics.rs @@ -16,7 +16,7 @@ use crate::Vote; #[derive(Clone, Debug, PartialEq, Eq)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize), serde(bound = ""))] pub struct RaftMetrics { - pub running_state: Result<(), Fatal>, + pub running_state: Result<(), Fatal>, /// The ID of the Raft node. pub id: C::NodeId, @@ -71,7 +71,7 @@ pub struct RaftMetrics { pub millis_since_quorum_ack: Option, /// The current membership config of the cluster. - pub membership_config: Arc>, + pub membership_config: Arc>, // --- // --- replication --- @@ -217,7 +217,7 @@ pub struct RaftServerMetrics { pub state: ServerState, pub current_leader: Option, - pub membership_config: Arc>, + pub membership_config: Arc>, } impl fmt::Display for RaftServerMetrics diff --git a/openraft/src/network/network.rs b/openraft/src/network/network.rs index b49de3bef..50ee678f4 100644 --- a/openraft/src/network/network.rs +++ b/openraft/src/network/network.rs @@ -37,7 +37,7 @@ where C: RaftTypeConfig &mut self, rpc: AppendEntriesRequest, option: RPCOption, - ) -> Result, RPCError>>; + ) -> Result, RPCError>>; /// Send an InstallSnapshot RPC to the target. #[cfg(feature = "generic-snapshot-data")] @@ -49,10 +49,8 @@ where C: RaftTypeConfig &mut self, _rpc: crate::raft::InstallSnapshotRequest, _option: RPCOption, - ) -> Result< - crate::raft::InstallSnapshotResponse, - RPCError>, - > { + ) -> Result, RPCError>> + { unimplemented!() } @@ -62,17 +60,14 @@ where C: RaftTypeConfig &mut self, _rpc: crate::raft::InstallSnapshotRequest, _option: RPCOption, - ) -> Result< - crate::raft::InstallSnapshotResponse, - RPCError>, - >; + ) -> Result, RPCError>>; /// Send a RequestVote RPC to the target. async fn vote( &mut self, - rpc: VoteRequest, + rpc: VoteRequest, option: RPCOption, - ) -> Result, RPCError>>; + ) -> Result, RPCError>>; /// Send a complete Snapshot to the target. /// @@ -96,7 +91,7 @@ where C: RaftTypeConfig snapshot: Snapshot, cancel: impl Future + OptionalSend, option: RPCOption, - ) -> Result, StreamingError>>; + ) -> Result, StreamingError>>; /// Send a complete Snapshot to the target. /// @@ -122,7 +117,7 @@ where C: RaftTypeConfig snapshot: Snapshot, cancel: impl Future + OptionalSend, option: RPCOption, - ) -> Result, StreamingError>> { + ) -> Result, StreamingError>> { use crate::network::snapshot_transport::Chunked; use crate::network::snapshot_transport::SnapshotTransport; diff --git a/openraft/src/network/snapshot_transport.rs b/openraft/src/network/snapshot_transport.rs index 491d1aa05..83820247f 100644 --- a/openraft/src/network/snapshot_transport.rs +++ b/openraft/src/network/snapshot_transport.rs @@ -54,7 +54,7 @@ pub trait SnapshotTransport { snapshot: Snapshot, cancel: impl Future + OptionalSend, option: RPCOption, - ) -> Result, StreamingError>> + ) -> Result, StreamingError>> where Net: RaftNetwork + ?Sized; @@ -87,7 +87,7 @@ pub trait SnapshotTransport { streaming: &mut Option>, raft: &Raft, req: InstallSnapshotRequest, - ) -> Result>, RaftError>; + ) -> Result>, RaftError>; } /// Send and Receive snapshot by chunks. @@ -103,7 +103,7 @@ where C::SnapshotData: tokio::io::AsyncRead + tokio::io::AsyncWrite + tokio::io: mut snapshot: Snapshot, mut cancel: impl Future + OptionalSend, option: RPCOption, - ) -> Result, StreamingError>> + ) -> Result, StreamingError>> where Net: RaftNetwork + ?Sized, { @@ -192,7 +192,7 @@ where C::SnapshotData: tokio::io::AsyncRead + tokio::io::AsyncWrite + tokio::io: streaming: &mut Option>, raft: &Raft, req: InstallSnapshotRequest, - ) -> Result>, RaftError> { + ) -> Result>, RaftError> { let snapshot_id = &req.meta.snapshot_id; let snapshot_meta = req.meta.clone(); let done = req.done; diff --git a/openraft/src/raft/core_state.rs b/openraft/src/raft/core_state.rs index c924ca310..b586e6e65 100644 --- a/openraft/src/raft/core_state.rs +++ b/openraft/src/raft/core_state.rs @@ -1,16 +1,14 @@ use crate::error::Fatal; -use crate::AsyncRuntime; -use crate::NodeId; +use crate::type_config::alias::JoinHandleOf; +use crate::RaftTypeConfig; /// The running state of RaftCore -pub(in crate::raft) enum CoreState -where - NID: NodeId, - A: AsyncRuntime, +pub(in crate::raft) enum CoreState +where C: RaftTypeConfig { /// The RaftCore task is still running. - Running(A::JoinHandle>>), + Running(JoinHandleOf>>), /// The RaftCore task has finished. The return value of the task is stored. - Done(Result<(), Fatal>), + Done(Result<(), Fatal>), } diff --git a/openraft/src/raft/message/append_entries.rs b/openraft/src/raft/message/append_entries.rs index 0387ea2ed..07816c6fc 100644 --- a/openraft/src/raft/message/append_entries.rs +++ b/openraft/src/raft/message/append_entries.rs @@ -4,7 +4,6 @@ use crate::display_ext::DisplayOptionExt; use crate::display_ext::DisplaySlice; use crate::LogId; use crate::MessageSummary; -use crate::NodeId; use crate::RaftTypeConfig; use crate::Vote; @@ -61,7 +60,7 @@ impl MessageSummary> for AppendEntrie #[derive(Debug)] #[derive(PartialEq, Eq)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize), serde(bound = ""))] -pub enum AppendEntriesResponse { +pub enum AppendEntriesResponse { /// Successfully replicated all log entries to the target node. Success, @@ -81,7 +80,7 @@ pub enum AppendEntriesResponse { /// /// [`RPCError`]: crate::error::RPCError /// [`RaftNetwork::append_entries`]: crate::network::RaftNetwork::append_entries - PartialSuccess(Option>), + PartialSuccess(Option>), /// The first log id([`AppendEntriesRequest::prev_log_id`]) of the entries to send does not /// match on the remote target node. @@ -90,10 +89,12 @@ pub enum AppendEntriesResponse { /// Seen a vote `v` that does not hold `mine_vote >= v`. /// And a leader's vote(committed vote) must be total order with other vote. /// Therefore it has to be a higher vote: `mine_vote < v` - HigherVote(Vote), + HigherVote(Vote), } -impl AppendEntriesResponse { +impl AppendEntriesResponse +where C: RaftTypeConfig +{ pub fn is_success(&self) -> bool { matches!(*self, AppendEntriesResponse::Success) } @@ -103,7 +104,9 @@ impl AppendEntriesResponse { } } -impl fmt::Display for AppendEntriesResponse { +impl fmt::Display for AppendEntriesResponse +where C: RaftTypeConfig +{ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { AppendEntriesResponse::Success => write!(f, "Success"), diff --git a/openraft/src/raft/message/client_write.rs b/openraft/src/raft/message/client_write.rs index 4cdc708c1..593262ef6 100644 --- a/openraft/src/raft/message/client_write.rs +++ b/openraft/src/raft/message/client_write.rs @@ -19,7 +19,7 @@ pub struct ClientWriteResponse { pub data: C::R, /// If the log entry is a change-membership entry. - pub membership: Option>, + pub membership: Option>, } impl Debug for ClientWriteResponse diff --git a/openraft/src/raft/message/install_snapshot.rs b/openraft/src/raft/message/install_snapshot.rs index f48806db9..501ebb26b 100644 --- a/openraft/src/raft/message/install_snapshot.rs +++ b/openraft/src/raft/message/install_snapshot.rs @@ -1,7 +1,6 @@ use std::fmt; use crate::MessageSummary; -use crate::NodeId; use crate::RaftTypeConfig; use crate::SnapshotMeta; use crate::Vote; @@ -14,7 +13,7 @@ pub struct InstallSnapshotRequest { pub vote: Vote, /// Metadata of a snapshot: snapshot_id, last_log_ed membership etc. - pub meta: SnapshotMeta, + pub meta: SnapshotMeta, /// The byte offset where this chunk of data is positioned in the snapshot file. pub offset: u64, @@ -51,8 +50,8 @@ impl MessageSummary> for InstallSna #[derive(derive_more::Display)] #[display(fmt = "{{vote:{}}}", vote)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize), serde(bound = ""))] -pub struct InstallSnapshotResponse { - pub vote: Vote, +pub struct InstallSnapshotResponse { + pub vote: Vote, } /// The response to `Raft::install_full_snapshot` API. @@ -61,18 +60,20 @@ pub struct InstallSnapshotResponse { #[derive(derive_more::Display)] #[display(fmt = "SnapshotResponse{{vote:{}}}", vote)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize), serde(bound = ""))] -pub struct SnapshotResponse { - pub vote: Vote, +pub struct SnapshotResponse { + pub vote: Vote, } -impl SnapshotResponse { - pub fn new(vote: Vote) -> Self { +impl SnapshotResponse { + pub fn new(vote: Vote) -> Self { Self { vote } } } -impl From> for InstallSnapshotResponse { - fn from(snap_resp: SnapshotResponse) -> Self { +impl From> for InstallSnapshotResponse +where C: RaftTypeConfig +{ + fn from(snap_resp: SnapshotResponse) -> Self { Self { vote: snap_resp.vote } } } diff --git a/openraft/src/raft/message/vote.rs b/openraft/src/raft/message/vote.rs index 7c7336145..ae696f99e 100644 --- a/openraft/src/raft/message/vote.rs +++ b/openraft/src/raft/message/vote.rs @@ -3,31 +3,37 @@ use std::fmt; use crate::display_ext::DisplayOptionExt; use crate::LogId; use crate::MessageSummary; -use crate::NodeId; +use crate::RaftTypeConfig; use crate::Vote; /// An RPC sent by candidates to gather votes (§5.2). #[derive(Debug, Clone, PartialEq, Eq)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize), serde(bound = ""))] -pub struct VoteRequest { - pub vote: Vote, - pub last_log_id: Option>, +pub struct VoteRequest { + pub vote: Vote, + pub last_log_id: Option>, } -impl fmt::Display for VoteRequest { +impl fmt::Display for VoteRequest +where C: RaftTypeConfig +{ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{{vote:{}, last_log:{}}}", self.vote, self.last_log_id.display(),) } } -impl MessageSummary> for VoteRequest { +impl MessageSummary> for VoteRequest +where C: RaftTypeConfig +{ fn summary(&self) -> String { self.to_string() } } -impl VoteRequest { - pub fn new(vote: Vote, last_log_id: Option>) -> Self { +impl VoteRequest +where C: RaftTypeConfig +{ + pub fn new(vote: Vote, last_log_id: Option>) -> Self { Self { vote, last_log_id } } } @@ -35,19 +41,21 @@ impl VoteRequest { /// The response to a `VoteRequest`. #[derive(Debug, Clone, PartialEq, Eq)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize), serde(bound = ""))] -pub struct VoteResponse { +pub struct VoteResponse { /// vote after a node handling vote-request. /// Thus `resp.vote >= req.vote` always holds. - pub vote: Vote, + pub vote: Vote, /// Will be true if the candidate received a vote from the responder. pub vote_granted: bool, /// The last log id stored on the remote voter. - pub last_log_id: Option>, + pub last_log_id: Option>, } -impl MessageSummary> for VoteResponse { +impl MessageSummary> for VoteResponse +where C: RaftTypeConfig +{ fn summary(&self) -> String { format!( "{{granted:{}, {}, last_log:{:?}}}", diff --git a/openraft/src/raft/mod.rs b/openraft/src/raft/mod.rs index ab303bc1e..f910cb1c6 100644 --- a/openraft/src/raft/mod.rs +++ b/openraft/src/raft/mod.rs @@ -171,7 +171,7 @@ where C: RaftTypeConfig network: N, mut log_store: LS, mut state_machine: SM, - ) -> Result> + ) -> Result> where N: RaftNetworkFactory, LS: RaftLogStorage, @@ -200,7 +200,7 @@ where C: RaftTypeConfig cluster = display(&config.cluster_name) ); - let eng_config = EngineConfig::new::(id, config.as_ref()); + let eng_config = EngineConfig::new(id, config.as_ref()); let state = { let mut helper = StorageHelper::new(&mut log_store, &mut state_machine); @@ -297,10 +297,7 @@ where C: RaftTypeConfig /// These RPCs are sent by the cluster leader to replicate log entries (§5.3), and are also /// used as heartbeats (§5.2). #[tracing::instrument(level = "debug", skip(self, rpc))] - pub async fn append_entries( - &self, - rpc: AppendEntriesRequest, - ) -> Result, RaftError> { + pub async fn append_entries(&self, rpc: AppendEntriesRequest) -> Result, RaftError> { tracing::debug!(rpc = display(rpc.summary()), "Raft::append_entries"); let (tx, rx) = C::AsyncRuntime::oneshot(); @@ -312,7 +309,7 @@ where C: RaftTypeConfig /// These RPCs are sent by cluster peers which are in candidate state attempting to gather votes /// (§5.2). #[tracing::instrument(level = "debug", skip(self, rpc))] - pub async fn vote(&self, rpc: VoteRequest) -> Result, RaftError> { + pub async fn vote(&self, rpc: VoteRequest) -> Result, RaftError> { tracing::info!(rpc = display(rpc.summary()), "Raft::vote()"); let (tx, rx) = C::AsyncRuntime::oneshot(); @@ -324,7 +321,7 @@ where C: RaftTypeConfig /// It returns error only when `RaftCore` fails to serve the request, e.g., Encountering a /// storage error or shutting down. #[tracing::instrument(level = "debug", skip_all)] - pub async fn get_snapshot(&self) -> Result>, RaftError> { + pub async fn get_snapshot(&self) -> Result>, RaftError> { tracing::debug!("Raft::get_snapshot()"); let (tx, rx) = C::AsyncRuntime::oneshot(); @@ -334,7 +331,7 @@ where C: RaftTypeConfig /// Get a snapshot data for receiving snapshot from the leader. #[tracing::instrument(level = "debug", skip_all)] - pub async fn begin_receiving_snapshot(&self) -> Result>, RaftError> { + pub async fn begin_receiving_snapshot(&self) -> Result>, RaftError> { tracing::info!("Raft::begin_receiving_snapshot()"); let (tx, rx) = AsyncRuntimeOf::::oneshot(); @@ -352,7 +349,7 @@ where C: RaftTypeConfig &self, vote: Vote, snapshot: Snapshot, - ) -> Result, Fatal> { + ) -> Result, Fatal> { tracing::info!("Raft::install_full_snapshot()"); let (tx, rx) = C::AsyncRuntime::oneshot(); @@ -384,7 +381,7 @@ where C: RaftTypeConfig pub async fn install_snapshot( &self, req: InstallSnapshotRequest, - ) -> Result, RaftError> + ) -> Result, RaftError> where C::SnapshotData: tokio::io::AsyncRead + tokio::io::AsyncWrite + tokio::io::AsyncSeek + Unpin, { @@ -438,7 +435,7 @@ where C: RaftTypeConfig /// the read will not be stale. #[deprecated(since = "0.9.0", note = "use `Raft::ensure_linearizable()` instead")] #[tracing::instrument(level = "debug", skip(self))] - pub async fn is_leader(&self) -> Result<(), RaftError>> { + pub async fn is_leader(&self) -> Result<(), RaftError>> { let (tx, rx) = C::AsyncRuntime::oneshot(); let _ = self.inner.call_core(RaftMsg::CheckIsLeaderRequest { tx }, rx).await?; Ok(()) @@ -468,9 +465,7 @@ where C: RaftTypeConfig /// ``` /// Read more about how it works: [Read Operation](crate::docs::protocol::read) #[tracing::instrument(level = "debug", skip(self))] - pub async fn ensure_linearizable( - &self, - ) -> Result>, RaftError>> { + pub async fn ensure_linearizable(&self) -> Result>, RaftError>> { let (read_log_id, applied) = self.get_read_log_id().await?; if read_log_id.index() > applied.index() { @@ -519,10 +514,7 @@ where C: RaftTypeConfig #[tracing::instrument(level = "debug", skip(self))] pub async fn get_read_log_id( &self, - ) -> Result< - (Option>, Option>), - RaftError>, - > { + ) -> Result<(Option>, Option>), RaftError>> { let (tx, rx) = C::AsyncRuntime::oneshot(); let (read_log_id, applied) = self.inner.call_core(RaftMsg::CheckIsLeaderRequest { tx }, rx).await?; Ok((read_log_id, applied)) @@ -550,7 +542,7 @@ where C: RaftTypeConfig pub async fn client_write( &self, app_data: C::D, - ) -> Result, RaftError>> { + ) -> Result, RaftError>> { let (tx, rx) = C::AsyncRuntime::oneshot(); self.inner.call_core(RaftMsg::ClientWriteRequest { app_data, tx }, rx).await } @@ -577,13 +569,8 @@ where C: RaftTypeConfig /// More than one node performing `initialize()` with the same config is safe, /// with different config will result in split brain condition. #[tracing::instrument(level = "debug", skip(self))] - pub async fn initialize( - &self, - members: T, - ) -> Result<(), RaftError>> - where - T: IntoNodes + Debug, - { + pub async fn initialize(&self, members: T) -> Result<(), RaftError>> + where T: IntoNodes + Debug { let (tx, rx) = C::AsyncRuntime::oneshot(); self.inner .call_core( @@ -618,7 +605,7 @@ where C: RaftTypeConfig id: C::NodeId, node: C::Node, blocking: bool, - ) -> Result, RaftError>> { + ) -> Result, RaftError>> { let (tx, rx) = C::AsyncRuntime::oneshot(); let resp = self .inner @@ -740,7 +727,7 @@ where C: RaftTypeConfig &self, members: impl Into>, retain: bool, - ) -> Result, RaftError>> { + ) -> Result, RaftError>> { let changes: ChangeMembers = members.into(); tracing::info!( @@ -796,7 +783,7 @@ where C: RaftTypeConfig /// Provides read-only access to [`RaftState`] through a user-provided function. /// /// The function `func` is applied to the current [`RaftState`]. The result of this function, - /// of type `V`, is returned wrapped in `Result>`. `Fatal` error will be + /// of type `V`, is returned wrapped in `Result>`. `Fatal` error will be /// returned if failed to receive a reply from `RaftCore`. /// /// A `Fatal` error is returned if: @@ -808,7 +795,7 @@ where C: RaftTypeConfig /// ```ignore /// let committed = my_raft.with_raft_state(|st| st.committed).await?; /// ``` - pub async fn with_raft_state(&self, func: F) -> Result> + pub async fn with_raft_state(&self, func: F) -> Result> where F: FnOnce(&RaftState) -> V + OptionalSend + 'static, V: OptionalSend + 'static, diff --git a/openraft/src/raft/raft_inner.rs b/openraft/src/raft/raft_inner.rs index 7d8146540..c7baa43f6 100644 --- a/openraft/src/raft/raft_inner.rs +++ b/openraft/src/raft/raft_inner.rs @@ -42,7 +42,7 @@ where C: RaftTypeConfig // TODO(xp): it does not need to be a async mutex. #[allow(clippy::type_complexity)] pub(in crate::raft) tx_shutdown: Mutex>>, - pub(in crate::raft) core_state: Mutex>, + pub(in crate::raft) core_state: Mutex>, /// The ongoing snapshot transmission. pub(in crate::raft) snapshot: Mutex>>, @@ -57,7 +57,7 @@ where C: RaftTypeConfig &self, mes: RaftMsg, rx: OneshotReceiverOf>, - ) -> Result> + ) -> Result> where E: Debug + OptionalSend, T: OptionalSend, @@ -95,7 +95,7 @@ where C: RaftTypeConfig &self, cmd: ExternalCommand, cmd_desc: impl fmt::Display + Default, - ) -> Result<(), Fatal> { + ) -> Result<(), Fatal> { let send_res = self.tx_api.send(RaftMsg::ExternalCommand { cmd }); if send_res.is_err() { @@ -110,7 +110,7 @@ where C: RaftTypeConfig &self, when: impl fmt::Display, message_summary: Option, - ) -> Fatal { + ) -> Fatal { // Wait for the core task to finish. self.join_core_task().await; diff --git a/openraft/src/raft/trigger.rs b/openraft/src/raft/trigger.rs index 5fe6e5b20..eda9affce 100644 --- a/openraft/src/raft/trigger.rs +++ b/openraft/src/raft/trigger.rs @@ -28,7 +28,7 @@ where C: RaftTypeConfig /// /// Returns error when RaftCore has [`Fatal`] error, e.g. shut down or having storage error. /// It is not affected by `Raft::enable_elect(false)`. - pub async fn elect(&self) -> Result<(), Fatal> { + pub async fn elect(&self) -> Result<(), Fatal> { self.raft_inner.send_external_command(ExternalCommand::Elect, "trigger_elect").await } @@ -36,14 +36,14 @@ where C: RaftTypeConfig /// /// Returns error when RaftCore has [`Fatal`] error, e.g. shut down or having storage error. /// It is not affected by `Raft::enable_heartbeat(false)`. - pub async fn heartbeat(&self) -> Result<(), Fatal> { + pub async fn heartbeat(&self) -> Result<(), Fatal> { self.raft_inner.send_external_command(ExternalCommand::Heartbeat, "trigger_heartbeat").await } /// Trigger to build a snapshot at once and return at once. /// /// Returns error when RaftCore has [`Fatal`] error, e.g. shut down or having storage error. - pub async fn snapshot(&self) -> Result<(), Fatal> { + pub async fn snapshot(&self) -> Result<(), Fatal> { self.raft_inner.send_external_command(ExternalCommand::Snapshot, "trigger_snapshot").await } @@ -62,7 +62,7 @@ where C: RaftTypeConfig /// can't be purged until the replication task is finished. /// /// [`max_in_snapshot_log_to_keep`]: `crate::Config::max_in_snapshot_log_to_keep` - pub async fn purge_log(&self, upto: u64) -> Result<(), Fatal> { + pub async fn purge_log(&self, upto: u64) -> Result<(), Fatal> { self.raft_inner.send_external_command(ExternalCommand::PurgeLog { upto }, "purge_log").await } } diff --git a/openraft/src/raft_state/membership_state/change_handler.rs b/openraft/src/raft_state/membership_state/change_handler.rs index aa3094beb..8e1fa35fc 100644 --- a/openraft/src/raft_state/membership_state/change_handler.rs +++ b/openraft/src/raft_state/membership_state/change_handler.rs @@ -3,24 +3,19 @@ use crate::error::InProgress; use crate::ChangeMembers; use crate::Membership; use crate::MembershipState; -use crate::Node; -use crate::NodeId; +use crate::RaftTypeConfig; /// This struct handles change-membership requests, validating them and applying the changes if /// the necessary conditions are met. It operates at the `Engine` and `RaftState` level, and /// serves as the outermost API for a consensus engine. -pub(crate) struct ChangeHandler<'m, NID, N> -where - NID: NodeId, - N: Node, +pub(crate) struct ChangeHandler<'m, C> +where C: RaftTypeConfig { - pub(crate) state: &'m MembershipState, + pub(crate) state: &'m MembershipState, } -impl<'m, NID, N> ChangeHandler<'m, NID, N> -where - NID: NodeId, - N: Node, +impl<'m, C> ChangeHandler<'m, C> +where C: RaftTypeConfig { /// Builds a new membership configuration by applying changes to the current configuration. /// @@ -35,9 +30,9 @@ where /// configuration. pub(crate) fn apply( &self, - change: ChangeMembers, + change: ChangeMembers, retain: bool, - ) -> Result, ChangeMembershipError> { + ) -> Result, ChangeMembershipError> { self.ensure_committed()?; let new_membership = self.state.effective().membership().clone().change(change, retain)?; @@ -48,7 +43,7 @@ where /// /// Returns Ok if the last membership is committed, or an InProgress error /// otherwise, to indicate a change-membership request should be rejected. - pub(crate) fn ensure_committed(&self) -> Result<(), InProgress> { + pub(crate) fn ensure_committed(&self) -> Result<(), InProgress> { let effective = self.state.effective(); let committed = self.state.committed(); diff --git a/openraft/src/raft_state/membership_state/change_handler_test.rs b/openraft/src/raft_state/membership_state/change_handler_test.rs index a3a0549a1..ccdaf63cc 100644 --- a/openraft/src/raft_state/membership_state/change_handler_test.rs +++ b/openraft/src/raft_state/membership_state/change_handler_test.rs @@ -3,6 +3,7 @@ use std::sync::Arc; use maplit::btreemap; use maplit::btreeset; +use crate::engine::testing::UTConfig; use crate::error::ChangeMembershipError; use crate::error::EmptyMembership; use crate::error::InProgress; @@ -14,26 +15,26 @@ use crate::Membership; use crate::MembershipState; /// Create an Arc -fn effmem(term: u64, index: u64, m: Membership) -> Arc> { +fn effmem(term: u64, index: u64, m: Membership) -> Arc> { let lid = Some(log_id(term, 1, index)); Arc::new(EffectiveMembership::new(lid, m)) } -fn m1() -> Membership { +fn m1() -> Membership { Membership::new(vec![btreeset! {1}], None) } -fn m12() -> Membership { +fn m12() -> Membership { Membership::new(vec![btreeset! {1,2}], None) } -fn m123_345() -> Membership { +fn m123_345() -> Membership { Membership::new(vec![btreeset! {1,2,3}, btreeset! {3,4,5}], None) } #[test] fn test_apply_not_committed() -> anyhow::Result<()> { - let new = || MembershipState::new(effmem(2, 2, m1()), effmem(3, 4, m123_345())); + let new = || MembershipState::::new(effmem(2, 2, m1()), effmem(3, 4, m123_345())); let res = new().change_handler().apply(ChangeMembers::AddVoterIds(btreeset! {1}), false); assert_eq!( @@ -49,7 +50,7 @@ fn test_apply_not_committed() -> anyhow::Result<()> { #[test] fn test_apply_empty_voters() -> anyhow::Result<()> { - let new = || MembershipState::new(effmem(3, 4, m1()), effmem(3, 4, m1())); + let new = || MembershipState::::new(effmem(3, 4, m1()), effmem(3, 4, m1())); let res = new().change_handler().apply(ChangeMembers::RemoveVoters(btreeset! {1}), false); assert_eq!(Err(ChangeMembershipError::EmptyMembership(EmptyMembership {})), res); @@ -59,7 +60,7 @@ fn test_apply_empty_voters() -> anyhow::Result<()> { #[test] fn test_apply_learner_not_found() -> anyhow::Result<()> { - let new = || MembershipState::new(effmem(3, 4, m1()), effmem(3, 4, m1())); + let new = || MembershipState::::new(effmem(3, 4, m1()), effmem(3, 4, m1())); let res = new().change_handler().apply(ChangeMembers::AddVoterIds(btreeset! {2}), false); assert_eq!( @@ -72,7 +73,7 @@ fn test_apply_learner_not_found() -> anyhow::Result<()> { #[test] fn test_apply_retain_learner() -> anyhow::Result<()> { - let new = || MembershipState::new(effmem(3, 4, m12()), effmem(3, 4, m123_345())); + let new = || MembershipState::::new(effmem(3, 4, m12()), effmem(3, 4, m123_345())); // Do not leave removed voters as learner let res = new().change_handler().apply(ChangeMembers::RemoveVoters(btreeset! {1,2}), false); diff --git a/openraft/src/raft_state/membership_state/membership_state_test.rs b/openraft/src/raft_state/membership_state/membership_state_test.rs index c6c2a5781..13b222f19 100644 --- a/openraft/src/raft_state/membership_state/membership_state_test.rs +++ b/openraft/src/raft_state/membership_state/membership_state_test.rs @@ -2,32 +2,33 @@ use std::sync::Arc; use maplit::btreeset; +use crate::engine::testing::UTConfig; use crate::testing::log_id; use crate::EffectiveMembership; use crate::Membership; use crate::MembershipState; /// Create an Arc -fn effmem(term: u64, index: u64, m: Membership) -> Arc> { +fn effmem(term: u64, index: u64, m: Membership) -> Arc> { let lid = Some(log_id(term, 1, index)); Arc::new(EffectiveMembership::new(lid, m)) } -fn m1() -> Membership { +fn m1() -> Membership { Membership::new(vec![btreeset! {1}], None) } -fn m12() -> Membership { +fn m12() -> Membership { Membership::new(vec![btreeset! {1,2}], None) } -fn m123_345() -> Membership { +fn m123_345() -> Membership { Membership::new(vec![btreeset! {1,2,3}, btreeset! {3,4,5}], None) } #[test] fn test_membership_state_is_member() -> anyhow::Result<()> { - let x = MembershipState::new(effmem(1, 1, m1()), effmem(3, 4, m123_345())); + let x = MembershipState::::new(effmem(1, 1, m1()), effmem(3, 4, m123_345())); assert!(!x.is_voter(&0)); assert!(x.is_voter(&1)); @@ -43,7 +44,7 @@ fn test_membership_state_is_member() -> anyhow::Result<()> { #[test] fn test_membership_state_update_committed() -> anyhow::Result<()> { let new = || { - MembershipState::new( + MembershipState::::new( Arc::new(EffectiveMembership::new(Some(log_id(2, 1, 2)), m1())), Arc::new(EffectiveMembership::new(Some(log_id(3, 1, 4)), m123_345())), ) @@ -93,7 +94,7 @@ fn test_membership_state_update_committed() -> anyhow::Result<()> { #[test] fn test_membership_state_append() -> anyhow::Result<()> { - let new = || MembershipState::new(effmem(2, 2, m1()), effmem(3, 4, m123_345())); + let new = || MembershipState::::new(effmem(2, 2, m1()), effmem(3, 4, m123_345())); let mut ms = new(); ms.append(effmem(4, 5, m12())); @@ -107,7 +108,7 @@ fn test_membership_state_append() -> anyhow::Result<()> { #[test] fn test_membership_state_commit() -> anyhow::Result<()> { - let new = || MembershipState::new(effmem(2, 2, m1()), effmem(3, 4, m123_345())); + let new = || MembershipState::::new(effmem(2, 2, m1()), effmem(3, 4, m123_345())); // Less than committed { @@ -146,7 +147,7 @@ fn test_membership_state_commit() -> anyhow::Result<()> { #[test] fn test_membership_state_truncate() -> anyhow::Result<()> { - let new = || MembershipState::new(effmem(2, 2, m1()), effmem(3, 4, m123_345())); + let new = || MembershipState::::new(effmem(2, 2, m1()), effmem(3, 4, m123_345())); { let mut ms = new(); diff --git a/openraft/src/raft_state/membership_state/mod.rs b/openraft/src/raft_state/membership_state/mod.rs index 8f6faad03..d2a7ea691 100644 --- a/openraft/src/raft_state/membership_state/mod.rs +++ b/openraft/src/raft_state/membership_state/mod.rs @@ -7,8 +7,7 @@ use crate::EffectiveMembership; use crate::LogId; use crate::LogIdOptionExt; use crate::MessageSummary; -use crate::Node; -use crate::NodeId; +use crate::RaftTypeConfig; mod change_handler; #[cfg(test)] mod change_handler_test; @@ -40,21 +39,17 @@ pub(crate) use change_handler::ChangeHandler; /// Thus a raft node will only need to store at most two recent membership logs. #[derive(Debug, Clone, Default)] #[derive(PartialEq, Eq)] -pub struct MembershipState -where - NID: NodeId, - N: Node, +pub struct MembershipState +where C: RaftTypeConfig { - committed: Arc>, + committed: Arc>, // Using `Arc` because the effective membership will be copied to RaftMetrics frequently. - effective: Arc>, + effective: Arc>, } -impl MessageSummary> for MembershipState -where - NID: NodeId, - N: Node, +impl MessageSummary> for MembershipState +where C: RaftTypeConfig { fn summary(&self) -> String { format!( @@ -65,30 +60,25 @@ where } } -impl MembershipState -where - NID: NodeId, - N: Node, +impl MembershipState +where C: RaftTypeConfig { - pub(crate) fn new( - committed: Arc>, - effective: Arc>, - ) -> Self { + pub(crate) fn new(committed: Arc>, effective: Arc>) -> Self { Self { committed, effective } } /// Return true if the given node id is an either voter or learner. - pub(crate) fn contains(&self, id: &NID) -> bool { + pub(crate) fn contains(&self, id: &C::NodeId) -> bool { self.effective.membership().contains(id) } /// Check if the given `NodeId` exists and is a voter. - pub(crate) fn is_voter(&self, id: &NID) -> bool { + pub(crate) fn is_voter(&self, id: &C::NodeId) -> bool { self.effective.membership().is_voter(id) } /// Update membership state if the specified committed_log_id is greater than `self.effective` - pub(crate) fn commit(&mut self, committed_log_id: &Option>) { + pub(crate) fn commit(&mut self, committed_log_id: &Option>) { if committed_log_id >= self.effective().log_id() { debug_assert!(committed_log_id.index() >= self.effective().log_id().index()); self.committed = self.effective.clone(); @@ -100,10 +90,7 @@ where /// /// If `self.effective` changed, it returns a reference to the new one. /// If not, it returns None. - pub(crate) fn update_committed( - &mut self, - c: Arc>, - ) -> Option>> { + pub(crate) fn update_committed(&mut self, c: Arc>) -> Option>> { let mut changed = false; // The local effective membership may conflict with the leader. @@ -145,7 +132,7 @@ where /// - Leader appends a new membership, /// - Or a follower has confirmed preceding logs matches the leaders' and appends membership /// received from the leader. - pub(crate) fn append(&mut self, m: Arc>) { + pub(crate) fn append(&mut self, m: Arc>) { debug_assert!( m.log_id() > self.effective.log_id(), "new membership has to have a greater log_id" @@ -179,7 +166,7 @@ where /// | last membership // before deleting since.. /// last membership // after deleting since.. /// ``` - pub(crate) fn truncate(&mut self, since: u64) -> Option>> { + pub(crate) fn truncate(&mut self, since: u64) -> Option>> { debug_assert!( since >= self.committed().log_id().next_index(), "committed log should never be truncated: committed membership can not conflict with the leader" @@ -200,14 +187,14 @@ where // This method is only used by tests #[cfg(test)] - pub(crate) fn set_effective(&mut self, e: Arc>) { + pub(crate) fn set_effective(&mut self, e: Arc>) { self.effective = e } /// Returns a reference to the last committed membership config. /// /// A committed membership config may or may not be the same as the effective one. - pub fn committed(&self) -> &Arc> { + pub fn committed(&self) -> &Arc> { &self.committed } @@ -217,19 +204,17 @@ where /// one. /// /// A committed membership config may or may not be the same as the effective one. - pub fn effective(&self) -> &Arc> { + pub fn effective(&self) -> &Arc> { &self.effective } - pub(crate) fn change_handler(&self) -> ChangeHandler { + pub(crate) fn change_handler(&self) -> ChangeHandler { ChangeHandler { state: self } } } -impl Validate for MembershipState -where - NID: NodeId, - N: Node, +impl Validate for MembershipState +where C: RaftTypeConfig { fn validate(&self) -> Result<(), Box> { validit::less_equal!(self.committed.log_id(), self.effective.log_id()); diff --git a/openraft/src/raft_state/mod.rs b/openraft/src/raft_state/mod.rs index 7382b941e..bf97480d3 100644 --- a/openraft/src/raft_state/mod.rs +++ b/openraft/src/raft_state/mod.rs @@ -67,10 +67,10 @@ where C: RaftTypeConfig pub log_ids: LogIdList, /// The latest cluster membership configuration found, in log or in state machine. - pub membership_state: MembershipState, + pub membership_state: MembershipState, /// The metadata of the last snapshot. - pub snapshot_meta: SnapshotMeta, + pub snapshot_meta: SnapshotMeta, // -- // -- volatile fields: they are not persisted. @@ -399,7 +399,7 @@ where C: RaftTypeConfig } /// Build a ForwardToLeader error that contains the leader id and node it knows. - pub(crate) fn forward_to_leader(&self) -> ForwardToLeader { + pub(crate) fn forward_to_leader(&self) -> ForwardToLeader { let vote = self.vote_ref(); if vote.is_committed() { diff --git a/openraft/src/raft_state/tests/forward_to_leader_test.rs b/openraft/src/raft_state/tests/forward_to_leader_test.rs index ebd9b8a8f..4fa3822b5 100644 --- a/openraft/src/raft_state/tests/forward_to_leader_test.rs +++ b/openraft/src/raft_state/tests/forward_to_leader_test.rs @@ -22,7 +22,7 @@ fn log_id(term: u64, index: u64) -> LogId { } } -fn m12() -> Membership { +fn m12() -> Membership { Membership::new(vec![btreeset! {1,2}], None) } @@ -56,7 +56,7 @@ fn test_forward_to_leader_not_a_member() { #[test] fn test_forward_to_leader_has_leader() { - let m123 = || Membership::::new(vec![btreeset! {1,2}], btreemap! {1=>4,2=>5,3=>6}); + let m123 = || Membership::>::new(vec![btreeset! {1,2}], btreemap! {1=>4,2=>5,3=>6}); let rs = RaftState::> { vote: UTime::new(TokioInstant::now(), Vote::new_committed(1, 3)), diff --git a/openraft/src/replication/callbacks.rs b/openraft/src/replication/callbacks.rs index 867c7b331..89913ebef 100644 --- a/openraft/src/replication/callbacks.rs +++ b/openraft/src/replication/callbacks.rs @@ -20,17 +20,17 @@ pub(crate) struct SnapshotCallback { pub(crate) start_time: InstantOf, /// Meta data of the snapshot to be replicated. - pub(crate) snapshot_meta: SnapshotMeta, + pub(crate) snapshot_meta: SnapshotMeta, /// The result of the snapshot replication. - pub(crate) result: Result, StreamingError>>, + pub(crate) result: Result, StreamingError>>, } impl SnapshotCallback { pub(in crate::replication) fn new( start_time: InstantOf, - snapshot_meta: SnapshotMeta, - result: Result, StreamingError>>, + snapshot_meta: SnapshotMeta, + result: Result, StreamingError>>, ) -> Self { Self { start_time, diff --git a/openraft/src/replication/mod.rs b/openraft/src/replication/mod.rs index baebfe70f..f9172a1a7 100644 --- a/openraft/src/replication/mod.rs +++ b/openraft/src/replication/mod.rs @@ -360,7 +360,7 @@ where async fn send_log_entries( &mut self, log_ids: DataWithId>, - ) -> Result>, ReplicationError> { + ) -> Result>, ReplicationError> { let request_id = log_ids.request_id(); tracing::debug!( @@ -491,11 +491,7 @@ where /// Send the error result to RaftCore. /// RaftCore will then submit another replication command. - fn send_progress_error( - &mut self, - request_id: RequestId, - err: RPCError>, - ) { + fn send_progress_error(&mut self, request_id: RequestId, err: RPCError>) { let _ = self.tx_raft_core.send(Notify::Network { response: Response::Progress { target: self.target, @@ -722,7 +718,7 @@ where async fn stream_snapshot( &mut self, snapshot_rx: DataWithId>>>, - ) -> Result>, ReplicationError> { + ) -> Result>, ReplicationError> { let request_id = snapshot_rx.request_id(); let rx = snapshot_rx.into_data(); @@ -813,7 +809,7 @@ where fn handle_snapshot_callback( &mut self, callback: DataWithId>, - ) -> Result>, ReplicationError> { + ) -> Result>, ReplicationError> { tracing::debug!( request_id = debug(callback.request_id()), response = display(callback.data()), diff --git a/openraft/src/replication/request.rs b/openraft/src/replication/request.rs index 7f1f15988..df1b6aa13 100644 --- a/openraft/src/replication/request.rs +++ b/openraft/src/replication/request.rs @@ -150,8 +150,8 @@ where C: RaftTypeConfig pub(crate) fn new_snapshot_callback( request_id: RequestId, start_time: InstantOf, - snapshot_meta: SnapshotMeta, - result: Result, StreamingError>>, + snapshot_meta: SnapshotMeta, + result: Result, StreamingError>>, ) -> Self { Self::SnapshotCallback(DataWithId::new( request_id, diff --git a/openraft/src/storage/helper.rs b/openraft/src/storage/helper.rs index a31613f3f..b56eabde3 100644 --- a/openraft/src/storage/helper.rs +++ b/openraft/src/storage/helper.rs @@ -188,7 +188,7 @@ where /// a follower only need to revert at most one membership log. /// /// Thus a raft node will only need to store at most two recent membership logs. - pub async fn get_membership(&mut self) -> Result, StorageError> { + pub async fn get_membership(&mut self) -> Result, StorageError> { let (_, sm_mem) = self.state_machine.applied_state().await?; let sm_mem_next_index = sm_mem.log_id().next_index(); @@ -227,7 +227,7 @@ where pub async fn last_membership_in_log( &mut self, since_index: u64, - ) -> Result>, StorageError> { + ) -> Result>, StorageError> { let st = self.log_store.get_log_state().await?; let mut end = st.last_log_id.next_index(); diff --git a/openraft/src/storage/mod.rs b/openraft/src/storage/mod.rs index 458a6e353..ebeba17c4 100644 --- a/openraft/src/storage/mod.rs +++ b/openraft/src/storage/mod.rs @@ -19,13 +19,11 @@ pub use v2::RaftLogStorageExt; pub use v2::RaftStateMachine; use crate::display_ext::DisplayOption; -use crate::node::Node; use crate::raft_types::SnapshotId; pub use crate::storage::callback::LogApplied; pub use crate::storage::callback::LogFlushed; use crate::LogId; use crate::MessageSummary; -use crate::NodeId; use crate::OptionalSend; use crate::OptionalSync; use crate::RaftTypeConfig; @@ -34,16 +32,14 @@ use crate::StoredMembership; #[derive(Debug, Clone, Default, PartialEq, Eq)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize), serde(bound = ""))] -pub struct SnapshotMeta -where - NID: NodeId, - N: Node, +pub struct SnapshotMeta +where C: RaftTypeConfig { /// Log entries upto which this snapshot includes, inclusive. - pub last_log_id: Option>, + pub last_log_id: Option>, /// The last applied membership config. - pub last_membership: StoredMembership, + pub last_membership: StoredMembership, /// To identify a snapshot when transferring. /// Caveat: even when two snapshot is built with the same `last_log_id`, they still could be @@ -51,10 +47,8 @@ where pub snapshot_id: SnapshotId, } -impl fmt::Display for SnapshotMeta -where - NID: NodeId, - N: Node, +impl fmt::Display for SnapshotMeta +where C: RaftTypeConfig { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( @@ -67,22 +61,18 @@ where } } -impl MessageSummary> for SnapshotMeta -where - NID: NodeId, - N: Node, +impl MessageSummary> for SnapshotMeta +where C: RaftTypeConfig { fn summary(&self) -> String { self.to_string() } } -impl SnapshotMeta -where - NID: NodeId, - N: Node, +impl SnapshotMeta +where C: RaftTypeConfig { - pub fn signature(&self) -> SnapshotSignature { + pub fn signature(&self) -> SnapshotSignature { SnapshotSignature { last_log_id: self.last_log_id, last_membership_log_id: *self.last_membership.log_id(), @@ -90,8 +80,8 @@ where } } - /// Returns a ref to the id of the last log that is included in this snasphot. - pub fn last_log_id(&self) -> Option<&LogId> { + /// Returns a ref to the id of the last log that is included in this snapshot. + pub fn last_log_id(&self) -> Option<&LogId> { self.last_log_id.as_ref() } } @@ -102,7 +92,7 @@ pub struct Snapshot where C: RaftTypeConfig { /// metadata of a snapshot - pub meta: SnapshotMeta, + pub meta: SnapshotMeta, /// A read handle to the associated snapshot. pub snapshot: Box, @@ -112,7 +102,7 @@ impl Snapshot where C: RaftTypeConfig { #[allow(dead_code)] - pub(crate) fn new(meta: SnapshotMeta, snapshot: Box) -> Self { + pub(crate) fn new(meta: SnapshotMeta, snapshot: Box) -> Self { Self { meta, snapshot } } } diff --git a/openraft/src/storage/v2.rs b/openraft/src/storage/v2.rs index 7804ef34a..a4a26e7e2 100644 --- a/openraft/src/storage/v2.rs +++ b/openraft/src/storage/v2.rs @@ -154,7 +154,7 @@ where C: RaftTypeConfig /// last-applied-log-id. async fn applied_state( &mut self, - ) -> Result<(Option>, StoredMembership), StorageError>; + ) -> Result<(Option>, StoredMembership), StorageError>; /// Apply the given payload of entries to the state machine. /// @@ -221,7 +221,7 @@ where C: RaftTypeConfig /// snapshot. async fn install_snapshot( &mut self, - meta: &SnapshotMeta, + meta: &SnapshotMeta, snapshot: Box, ) -> Result<(), StorageError>; diff --git a/stores/memstore/src/lib.rs b/stores/memstore/src/lib.rs index a0d06f4ba..d2109c765 100644 --- a/stores/memstore/src/lib.rs +++ b/stores/memstore/src/lib.rs @@ -89,7 +89,7 @@ openraft::declare_raft_types!( /// The application snapshot type which the `MemStore` works with. #[derive(Debug)] pub struct MemStoreSnapshot { - pub meta: SnapshotMeta, + pub meta: SnapshotMeta, /// The data of the state machine at the time of this snapshot. pub data: Vec, @@ -100,7 +100,7 @@ pub struct MemStoreSnapshot { pub struct MemStoreStateMachine { pub last_applied_log: Option>, - pub last_membership: StoredMembership, + pub last_membership: StoredMembership, /// A mapping of client IDs to their state info. pub client_serial_responses: HashMap)>, @@ -430,7 +430,7 @@ impl RaftStateMachine for Arc { async fn applied_state( &mut self, - ) -> Result<(Option>, StoredMembership), StorageError> { + ) -> Result<(Option>, StoredMembership), StorageError> { let sm = self.sm.read().await; Ok((sm.last_applied_log, sm.last_membership.clone())) } @@ -484,7 +484,7 @@ impl RaftStateMachine for Arc { #[tracing::instrument(level = "trace", skip(self, snapshot))] async fn install_snapshot( &mut self, - meta: &SnapshotMeta, + meta: &SnapshotMeta, snapshot: Box>, ) -> Result<(), StorageError> { tracing::info!( diff --git a/stores/rocksstore/src/lib.rs b/stores/rocksstore/src/lib.rs index 324e6565e..d6ad66546 100644 --- a/stores/rocksstore/src/lib.rs +++ b/stores/rocksstore/src/lib.rs @@ -89,7 +89,7 @@ pub struct RocksResponse { #[derive(Serialize, Deserialize, Debug)] pub struct RocksSnapshot { - pub meta: SnapshotMeta, + pub meta: SnapshotMeta, /// The data of the state machine at the time of this snapshot. pub data: Vec, @@ -101,7 +101,7 @@ pub struct RocksSnapshot { pub struct StateMachine { pub last_applied_log: Option>, - pub last_membership: StoredMembership, + pub last_membership: StoredMembership, /// Application data. pub data: BTreeMap, @@ -414,7 +414,7 @@ impl RaftStateMachine for RocksStateMachine { async fn applied_state( &mut self, - ) -> Result<(Option>, StoredMembership), StorageError> { + ) -> Result<(Option>, StoredMembership), StorageError> { Ok((self.sm.last_applied_log, self.sm.last_membership.clone())) } @@ -459,7 +459,7 @@ impl RaftStateMachine for RocksStateMachine { async fn install_snapshot( &mut self, - meta: &SnapshotMeta, + meta: &SnapshotMeta, snapshot: Box>, ) -> Result<(), StorageError> { tracing::info!( diff --git a/stores/sledstore/src/lib.rs b/stores/sledstore/src/lib.rs index d0129b60f..c4a1c3cd0 100644 --- a/stores/sledstore/src/lib.rs +++ b/stores/sledstore/src/lib.rs @@ -79,7 +79,7 @@ pub struct ExampleResponse { #[derive(Serialize, Deserialize, Debug)] pub struct ExampleSnapshot { - pub meta: SnapshotMeta, + pub meta: SnapshotMeta, /// The data of the state machine at the time of this snapshot. pub data: Vec, @@ -95,7 +95,7 @@ pub struct ExampleSnapshot { pub struct SerializableExampleStateMachine { pub last_applied_log: Option>, - pub last_membership: StoredMembership, + pub last_membership: StoredMembership, /// Application data. pub data: BTreeMap, @@ -164,7 +164,7 @@ fn conflictable_txn_err(e: E) -> sled::transaction::Conflict } impl ExampleStateMachine { - fn get_last_membership(&self) -> StorageResult> { + fn get_last_membership(&self) -> StorageResult> { let state_machine = state_machine(&self.db); let ivec = state_machine.get(b"last_membership").map_err(read_sm_err)?; @@ -176,7 +176,7 @@ impl ExampleStateMachine { Ok(m) } - async fn set_last_membership(&self, membership: StoredMembership) -> StorageResult<()> { + async fn set_last_membership(&self, membership: StoredMembership) -> StorageResult<()> { let value = serde_json::to_vec(&membership).map_err(write_sm_err)?; let state_machine = state_machine(&self.db); state_machine.insert(b"last_membership", value).map_err(write_err)?; @@ -187,7 +187,7 @@ impl ExampleStateMachine { fn set_last_membership_tx( &self, tx_state_machine: &sled::transaction::TransactionalTree, - membership: StoredMembership, + membership: StoredMembership, ) -> Result<(), sled::transaction::ConflictableTransactionError> { let value = serde_json::to_vec(&membership).map_err(conflictable_txn_err)?; tx_state_machine.insert(b"last_membership", value).map_err(conflictable_txn_err)?; @@ -573,8 +573,7 @@ impl RaftStateMachine for Arc { async fn applied_state( &mut self, - ) -> Result<(Option>, StoredMembership), StorageError> - { + ) -> Result<(Option>, StoredMembership), StorageError> { let state_machine = self.state_machine.read().await; Ok(( state_machine.get_last_applied_log()?, @@ -641,7 +640,7 @@ impl RaftStateMachine for Arc { #[tracing::instrument(level = "trace", skip(self, snapshot))] async fn install_snapshot( &mut self, - meta: &SnapshotMeta, + meta: &SnapshotMeta, snapshot: Box>, ) -> Result<(), StorageError> { tracing::info!( diff --git a/tests/tests/fixtures/mod.rs b/tests/tests/fixtures/mod.rs index 0c0d3e8f2..ea6850d92 100644 --- a/tests/tests/fixtures/mod.rs +++ b/tests/tests/fixtures/mod.rs @@ -48,8 +48,6 @@ use openraft::Config; use openraft::LogId; use openraft::LogIdOptionExt; use openraft::MessageSummary; -use openraft::Node; -use openraft::NodeId; use openraft::RPCTypes; use openraft::Raft; use openraft::RaftLogId; @@ -171,10 +169,9 @@ pub enum RPCErrorType { } impl RPCErrorType { - fn make_error(&self, id: NID, dir: Direction) -> RPCError> + fn make_error(&self, id: C::NodeId, dir: Direction) -> RPCError> where - NID: NodeId, - N: Node, + C: RaftTypeConfig, E: std::error::Error, { let msg = format!("error {} id={}", dir, id); @@ -196,13 +193,13 @@ impl RPCErrorType { } /// Pre-hook result, which does not return remote Error. -pub type PreHookResult = Result<(), RPCError>; +pub type PreHookResult = Result<(), RPCError>; #[derive(derive_more::From, derive_more::TryInto)] pub enum RPCRequest { AppendEntries(AppendEntriesRequest), InstallSnapshot(InstallSnapshotRequest), - Vote(VoteRequest), + Vote(VoteRequest), } impl RPCRequest { @@ -535,7 +532,7 @@ impl TypedRaftRouter { request: impl Into>, from: MemNodeId, to: MemNodeId, - ) -> Result<(), RPCError> + ) -> Result<(), RPCError> where E: std::error::Error, { @@ -709,13 +706,13 @@ impl TypedRaftRouter { &self, leader: MemNodeId, target: MemNodeId, - ) -> Result, ClientWriteError> { + ) -> Result, ClientWriteError> { let node = self.get_raft_handle(&leader).unwrap(); node.add_learner(target, (), true).await.map_err(|e| e.into_api_error().unwrap()) } /// Ensure read linearizability. - pub async fn ensure_linearizable(&self, target: MemNodeId) -> Result<(), CheckIsLeaderError> { + pub async fn ensure_linearizable(&self, target: MemNodeId) -> Result<(), CheckIsLeaderError> { let n = self.get_raft_handle(&target).unwrap(); n.ensure_linearizable().await.map_err(|e| e.into_api_error().unwrap())?; Ok(()) @@ -727,7 +724,7 @@ impl TypedRaftRouter { mut target: MemNodeId, client_id: &str, serial: u64, - ) -> Result<(), RaftError>> { + ) -> Result<(), RaftError>> { for ith in 0..3 { let req = ClientRequest::make_request(client_id, serial); if let Err(err) = self.send_client_request(target, req).await { @@ -761,7 +758,7 @@ impl TypedRaftRouter { } /// Send external request to the particular node. - pub async fn with_raft_state(&self, target: MemNodeId, func: F) -> Result> + pub async fn with_raft_state(&self, target: MemNodeId, func: F) -> Result> where F: FnOnce(&RaftState) -> V + Send + 'static, V: Send + 'static, @@ -789,7 +786,7 @@ impl TypedRaftRouter { target: MemNodeId, client_id: &str, count: usize, - ) -> Result>> { + ) -> Result>> { for idx in 0..count { self.client_request(target, client_id, idx as u64).await?; } @@ -801,7 +798,7 @@ impl TypedRaftRouter { &self, target: MemNodeId, req: ClientRequest, - ) -> Result>> { + ) -> Result>> { let node = { let rt = self.nodes.lock().unwrap(); rt.get(&target) @@ -946,7 +943,7 @@ impl TypedRaftRouter { &self, id: MemNodeId, target: MemNodeId, - ) -> Result<(), RPCError>> + ) -> Result<(), RPCError>> where E: std::error::Error, { @@ -984,7 +981,7 @@ impl RaftNetwork for RaftRouterNetwork { &mut self, mut rpc: AppendEntriesRequest, _option: RPCOption, - ) -> Result, RPCError>> { + ) -> Result, RPCError>> { let from_id = rpc.vote.leader_id().voted_for().unwrap(); tracing::debug!("append_entries to id={} {}", self.target, rpc.summary()); @@ -1048,7 +1045,7 @@ impl RaftNetwork for RaftRouterNetwork { &mut self, rpc: InstallSnapshotRequest, _option: RPCOption, - ) -> Result, RPCError>> + ) -> Result, RPCError>> { let from_id = rpc.vote.leader_id().voted_for().unwrap(); @@ -1068,9 +1065,9 @@ impl RaftNetwork for RaftRouterNetwork { /// Send a RequestVote RPC to the target Raft node (§5). async fn vote( &mut self, - rpc: VoteRequest, + rpc: VoteRequest, _option: RPCOption, - ) -> Result, RPCError>> { + ) -> Result, RPCError>> { let from_id = rpc.vote.leader_id().voted_for().unwrap(); self.owner.count_rpc(RPCTypes::Vote);