From 22cd3bb423ddaf21c1f4d70917f689e79a9cacb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=82=8E=E6=B3=BC?= Date: Sat, 24 Feb 2024 10:51:31 +0800 Subject: [PATCH] Change: remove deprecated RaftNetwork methods without `option` argument --- .../src/network.rs | 9 +- .../src/network.rs | 9 +- .../src/network.rs | 13 ++- .../src/network/raft_network_impl.rs | 13 ++- .../src/network/raft_network_impl.rs | 18 ++-- openraft/src/error.rs | 2 +- openraft/src/network/network.rs | 100 ++++-------------- openraft/src/raft/message/append_entries.rs | 10 +- tests/tests/fixtures/mod.rs | 10 +- .../t10_append_entries_partial_success.rs | 2 +- 10 files changed, 78 insertions(+), 108 deletions(-) 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 7d1d59a02..45e459433 100644 --- a/examples/raft-kv-memstore-generic-snapshot-data/src/network.rs +++ b/examples/raft-kv-memstore-generic-snapshot-data/src/network.rs @@ -37,9 +37,10 @@ impl RaftNetworkFactory for Router { } impl RaftNetwork for Connection { - async fn send_append_entries( + async fn append_entries( &mut self, req: AppendEntriesRequest, + _option: RPCOption, ) -> Result, typ::RPCError> { let resp = self .router @@ -65,7 +66,11 @@ impl RaftNetwork for Connection { Ok(resp) } - async fn send_vote(&mut self, req: VoteRequest) -> Result, typ::RPCError> { + async fn vote( + &mut self, + req: VoteRequest, + _option: RPCOption, + ) -> Result, typ::RPCError> { let resp = self .router .send(self.target, "/raft/vote", req) 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 7d1d59a02..45e459433 100644 --- a/examples/raft-kv-memstore-opendal-snapshot-data/src/network.rs +++ b/examples/raft-kv-memstore-opendal-snapshot-data/src/network.rs @@ -37,9 +37,10 @@ impl RaftNetworkFactory for Router { } impl RaftNetwork for Connection { - async fn send_append_entries( + async fn append_entries( &mut self, req: AppendEntriesRequest, + _option: RPCOption, ) -> Result, typ::RPCError> { let resp = self .router @@ -65,7 +66,11 @@ impl RaftNetwork for Connection { Ok(resp) } - async fn send_vote(&mut self, req: VoteRequest) -> Result, typ::RPCError> { + async fn vote( + &mut self, + req: VoteRequest, + _option: RPCOption, + ) -> Result, typ::RPCError> { let resp = self .router .send(self.target, "/raft/vote", req) diff --git a/examples/raft-kv-memstore-singlethreaded/src/network.rs b/examples/raft-kv-memstore-singlethreaded/src/network.rs index 87f653f14..c7c7e39fa 100644 --- a/examples/raft-kv-memstore-singlethreaded/src/network.rs +++ b/examples/raft-kv-memstore-singlethreaded/src/network.rs @@ -1,5 +1,6 @@ use openraft::error::InstallSnapshotError; use openraft::error::RemoteError; +use openraft::network::RPCOption; use openraft::raft::AppendEntriesRequest; use openraft::raft::AppendEntriesResponse; use openraft::raft::InstallSnapshotRequest; @@ -32,9 +33,10 @@ impl RaftNetworkFactory for Router { } impl RaftNetwork for Connection { - async fn send_append_entries( + async fn append_entries( &mut self, req: AppendEntriesRequest, + _option: RPCOption, ) -> Result, typ::RPCError> { let resp = self .router @@ -44,9 +46,10 @@ impl RaftNetwork for Connection { Ok(resp) } - async fn send_install_snapshot( + async fn install_snapshot( &mut self, req: InstallSnapshotRequest, + _option: RPCOption, ) -> Result, typ::RPCError> { let resp = self .router @@ -56,7 +59,11 @@ impl RaftNetwork for Connection { Ok(resp) } - async fn send_vote(&mut self, req: VoteRequest) -> Result, typ::RPCError> { + async fn vote( + &mut self, + req: VoteRequest, + _option: RPCOption, + ) -> Result, typ::RPCError> { let resp = self .router .send(self.target, "/raft/vote", req) 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 cf2ab8707..db76ed109 100644 --- a/examples/raft-kv-memstore/src/network/raft_network_impl.rs +++ b/examples/raft-kv-memstore/src/network/raft_network_impl.rs @@ -1,6 +1,7 @@ use openraft::error::InstallSnapshotError; use openraft::error::NetworkError; use openraft::error::RemoteError; +use openraft::network::RPCOption; use openraft::network::RaftNetwork; use openraft::network::RaftNetworkFactory; use openraft::raft::AppendEntriesRequest; @@ -77,21 +78,27 @@ pub struct NetworkConnection { } impl RaftNetwork for NetworkConnection { - async fn send_append_entries( + async fn append_entries( &mut self, req: AppendEntriesRequest, + _option: RPCOption, ) -> Result, typ::RPCError> { self.owner.send_rpc(self.target, &self.target_node, "raft-append", req).await } - async fn send_install_snapshot( + async fn install_snapshot( &mut self, req: InstallSnapshotRequest, + _option: RPCOption, ) -> Result, typ::RPCError> { self.owner.send_rpc(self.target, &self.target_node, "raft-snapshot", req).await } - async fn send_vote(&mut self, req: VoteRequest) -> Result, typ::RPCError> { + async fn vote( + &mut self, + req: VoteRequest, + _option: RPCOption, + ) -> Result, typ::RPCError> { self.owner.send_rpc(self.target, &self.target_node, "raft-vote", req).await } } 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 a3cc85f55..40feacdcc 100644 --- a/examples/raft-kv-rocksdb/src/network/raft_network_impl.rs +++ b/examples/raft-kv-rocksdb/src/network/raft_network_impl.rs @@ -6,6 +6,7 @@ use openraft::error::NetworkError; use openraft::error::RPCError; use openraft::error::RaftError; use openraft::error::RemoteError; +use openraft::network::RPCOption; use openraft::network::RaftNetwork; use openraft::network::RaftNetworkFactory; use openraft::raft::AppendEntriesRequest; @@ -99,7 +100,7 @@ fn to_error(e: toy_rpc::Error, target: N // 99 | ) -> Result, RPCError>> // { // | ___________________________________________________________________________________________^ -// 100 | | tracing::debug!(req = debug(&req), "send_append_entries"); +// 100 | | tracing::debug!(req = debug(&req), "append_entries"); // 101 | | // 102 | | let c = self.c().await?; // ... | @@ -112,11 +113,12 @@ fn to_error(e: toy_rpc::Error, target: N #[allow(clippy::blocks_in_conditions)] impl RaftNetwork for NetworkConnection { #[tracing::instrument(level = "debug", skip_all, err(Debug))] - async fn send_append_entries( + async fn append_entries( &mut self, req: AppendEntriesRequest, + _option: RPCOption, ) -> Result, RPCError>> { - tracing::debug!(req = debug(&req), "send_append_entries"); + tracing::debug!(req = debug(&req), "append_entries"); let c = self.c().await?; tracing::debug!("got connection"); @@ -128,20 +130,22 @@ impl RaftNetwork for NetworkConnection { } #[tracing::instrument(level = "debug", skip_all, err(Debug))] - async fn send_install_snapshot( + async fn install_snapshot( &mut self, req: InstallSnapshotRequest, + _option: RPCOption, ) -> Result, RPCError>> { - tracing::debug!(req = debug(&req), "send_install_snapshot"); + tracing::debug!(req = debug(&req), "install_snapshot"); self.c().await?.raft().snapshot(req).await.map_err(|e| to_error(e, self.target)) } #[tracing::instrument(level = "debug", skip_all, err(Debug))] - async fn send_vote( + async fn vote( &mut self, req: VoteRequest, + _option: RPCOption, ) -> Result, RPCError>> { - tracing::debug!(req = debug(&req), "send_vote"); + tracing::debug!(req = debug(&req), "vote"); self.c().await?.raft().vote(req).await.map_err(|e| to_error(e, self.target)) } } diff --git a/openraft/src/error.rs b/openraft/src/error.rs index ba3bcba34..cf7f2a065 100644 --- a/openraft/src/error.rs +++ b/openraft/src/error.rs @@ -240,7 +240,7 @@ where Closed(#[from] ReplicationClosed), // TODO(xp): two sub type: StorageError / TransportError - // TODO(xp): a sub error for just send_append_entries() + // TODO(xp): a sub error for just append_entries() #[error(transparent)] StorageError(#[from] StorageError), diff --git a/openraft/src/network/network.rs b/openraft/src/network/network.rs index c019c5e8e..5131db833 100644 --- a/openraft/src/network/network.rs +++ b/openraft/src/network/network.rs @@ -31,18 +31,6 @@ use crate::Vote; /// /// A single network instance is used to connect to a single target node. The network instance is /// constructed by the [`RaftNetworkFactory`](`crate::network::RaftNetworkFactory`). -/// -/// ### 2023-05-03: New API with options -/// -/// - This trait introduced 3 new API `append_entries`, `install_snapshot` and `vote` which accept -/// an additional argument [`RPCOption`], and deprecated the old API `send_append_entries`, -/// `send_install_snapshot` and `send_vote`. -/// -/// - The old API will be **removed** in `0.9`. An application can still implement the old API -/// without any changes. Openraft calls only the new API and the default implementation will -/// delegate to the old API. -/// -/// - Implementing the new APIs will disable the old APIs. #[add_async_trait] pub trait RaftNetwork: OptionalSend + OptionalSync + 'static where C: RaftTypeConfig @@ -52,37 +40,26 @@ where C: RaftTypeConfig &mut self, rpc: AppendEntriesRequest, option: RPCOption, - ) -> Result, RPCError>> { - let _ = option; - #[allow(deprecated)] - self.send_append_entries(rpc).await - } + ) -> Result, RPCError>>; /// Send an InstallSnapshot RPC to the target. + #[cfg(not(feature = "generic-snapshot-data"))] #[deprecated(since = "0.9.0", note = "use `snapshot()` instead for sending a complete snapshot")] async fn install_snapshot( &mut self, - rpc: InstallSnapshotRequest, - option: RPCOption, + _rpc: InstallSnapshotRequest, + _option: RPCOption, ) -> Result< InstallSnapshotResponse, RPCError>, - > { - let _ = option; - #[allow(deprecated)] - self.send_install_snapshot(rpc).await - } + >; /// Send a RequestVote RPC to the target. async fn vote( &mut self, rpc: VoteRequest, option: RPCOption, - ) -> Result, RPCError>> { - let _ = option; - #[allow(deprecated)] - self.send_vote(rpc).await - } + ) -> Result, RPCError>>; /// Send a complete Snapshot to the target. /// @@ -99,67 +76,28 @@ where C: RaftTypeConfig /// with this vote. /// /// `cancel` get `Ready` when the caller decides to cancel this snapshot transmission. + #[cfg(feature = "generic-snapshot-data")] async fn snapshot( &mut self, vote: Vote, snapshot: Snapshot, cancel: impl Future + OptionalSend, option: RPCOption, - ) -> Result, StreamingError>> { - #[cfg(not(feature = "generic-snapshot-data"))] - { - use crate::network::stream_snapshot; - use crate::network::stream_snapshot::SnapshotTransport; - - let resp = stream_snapshot::Chunked::send_snapshot(self, vote, snapshot, cancel, option).await?; - Ok(resp) - } - #[cfg(feature = "generic-snapshot-data")] - { - let _ = (vote, snapshot, cancel, option); - unimplemented!( - "no default implementation for RaftNetwork::snapshot() if `generic-snapshot-data` feature is enabled" - ) - } - } + ) -> Result, StreamingError>>; - /// Send an AppendEntries RPC to the target Raft node (§5). - #[deprecated( - since = "0.8.4", - note = "use `append_entries` instead. This method will be removed in 0.9" - )] - async fn send_append_entries( - &mut self, - rpc: AppendEntriesRequest, - ) -> Result, RPCError>> { - let _ = rpc; - unimplemented!("send_append_entries is deprecated") - } - - /// Send an InstallSnapshot RPC to the target Raft node (§7). - #[deprecated( - since = "0.8.4", - note = "use `install_snapshot` instead. This method will be removed in 0.9" - )] - async fn send_install_snapshot( + #[cfg(not(feature = "generic-snapshot-data"))] + async fn snapshot( &mut self, - rpc: InstallSnapshotRequest, - ) -> Result< - InstallSnapshotResponse, - RPCError>, - > { - let _ = rpc; - unimplemented!("send_install_snapshot is deprecated") - } + vote: Vote, + snapshot: Snapshot, + cancel: impl Future + OptionalSend, + option: RPCOption, + ) -> Result, StreamingError>> { + use crate::network::stream_snapshot; + use crate::network::stream_snapshot::SnapshotTransport; - /// Send a RequestVote RPC to the target Raft node (§5). - #[deprecated(since = "0.8.4", note = "use `vote` instead. This method will be removed in 0.9")] - async fn send_vote( - &mut self, - rpc: VoteRequest, - ) -> Result, RPCError>> { - let _ = rpc; - unimplemented!("send_vote is deprecated") + let resp = stream_snapshot::Chunked::send_snapshot(self, vote, snapshot, cancel, option).await?; + Ok(resp) } /// Build a backoff instance if the target node is temporarily(or permanently) unreachable. diff --git a/openraft/src/raft/message/append_entries.rs b/openraft/src/raft/message/append_entries.rs index 5980110d8..0387ea2ed 100644 --- a/openraft/src/raft/message/append_entries.rs +++ b/openraft/src/raft/message/append_entries.rs @@ -53,11 +53,11 @@ impl MessageSummary> for AppendEntrie /// The response to an `AppendEntriesRequest`. /// -/// [`RaftNetwork::send_append_entries`] returns this type only when received an RPC reply. +/// [`RaftNetwork::append_entries`] returns this type only when received an RPC reply. /// Otherwise it should return [`RPCError`]. /// /// [`RPCError`]: crate::error::RPCError -/// [`RaftNetwork::send_append_entries`]: crate::network::RaftNetwork::send_append_entries +/// [`RaftNetwork::append_entries`]: crate::network::RaftNetwork::append_entries #[derive(Debug)] #[derive(PartialEq, Eq)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize), serde(bound = ""))] @@ -67,7 +67,7 @@ pub enum AppendEntriesResponse { /// Successfully sent the first portion of log entries. /// - /// [`RaftNetwork::send_append_entries`] can return a partial success. + /// [`RaftNetwork::append_entries`] can return a partial success. /// For example, it tries to send log entries `[1-2..3-10]`, the application is allowed to send /// just `[1-2..1-3]` and return `PartialSuccess(1-3)`, /// @@ -75,12 +75,12 @@ pub enum AppendEntriesResponse { /// /// The returned matching log id must be **greater than or equal to** the first log /// id([`AppendEntriesRequest::prev_log_id`]) of the entries to send. If no RPC reply is - /// received, [`RaftNetwork::send_append_entries`] must return an [`RPCError`] to inform + /// received, [`RaftNetwork::append_entries`] must return an [`RPCError`] to inform /// Openraft that the first log id([`AppendEntriesRequest::prev_log_id`]) may not match on /// the remote target node. /// /// [`RPCError`]: crate::error::RPCError - /// [`RaftNetwork::send_append_entries`]: crate::network::RaftNetwork::send_append_entries + /// [`RaftNetwork::append_entries`]: crate::network::RaftNetwork::append_entries PartialSuccess(Option>), /// The first log id([`AppendEntriesRequest::prev_log_id`]) of the entries to send does not diff --git a/tests/tests/fixtures/mod.rs b/tests/tests/fixtures/mod.rs index 1ef43ffa3..58bcae1ad 100644 --- a/tests/tests/fixtures/mod.rs +++ b/tests/tests/fixtures/mod.rs @@ -32,6 +32,7 @@ use openraft::error::RaftError; use openraft::error::RemoteError; use openraft::error::Unreachable; use openraft::metrics::Wait; +use openraft::network::RPCOption; use openraft::network::RaftNetwork; use openraft::network::RaftNetworkFactory; use openraft::raft::AppendEntriesRequest; @@ -985,9 +986,10 @@ pub struct RaftRouterNetwork { impl RaftNetwork for RaftRouterNetwork { /// Send an AppendEntries RPC to the target Raft node (§5). - async fn send_append_entries( + async fn append_entries( &mut self, mut rpc: AppendEntriesRequest, + _option: RPCOption, ) -> Result, RPCError>> { let from_id = rpc.vote.leader_id().voted_for().unwrap(); @@ -1048,9 +1050,10 @@ impl RaftNetwork for RaftRouterNetwork { } /// Send an InstallSnapshot RPC to the target Raft node (§7). - async fn send_install_snapshot( + async fn install_snapshot( &mut self, rpc: InstallSnapshotRequest, + _option: RPCOption, ) -> Result, RPCError>> { let from_id = rpc.vote.leader_id().voted_for().unwrap(); @@ -1069,9 +1072,10 @@ impl RaftNetwork for RaftRouterNetwork { } /// Send a RequestVote RPC to the target Raft node (§5). - async fn send_vote( + async fn vote( &mut self, rpc: VoteRequest, + _option: RPCOption, ) -> Result, RPCError>> { let from_id = rpc.vote.leader_id().voted_for().unwrap(); diff --git a/tests/tests/replication/t10_append_entries_partial_success.rs b/tests/tests/replication/t10_append_entries_partial_success.rs index 6efc46f2a..a5573564f 100644 --- a/tests/tests/replication/t10_append_entries_partial_success.rs +++ b/tests/tests/replication/t10_append_entries_partial_success.rs @@ -8,7 +8,7 @@ use openraft::Config; use crate::fixtures::init_default_ut_tracing; use crate::fixtures::RaftRouter; -/// RaftNetwork::send_append_entries can return a partial success. +/// RaftNetwork::append_entries can return a partial success. /// For example, it tries to send log entries `[1-2..2-10]`, the application is allowed to send just /// `[1-2..1-3]` and return `PartialSuccess(1-3)`. #[async_entry::test(worker_threads = 4, init = "init_default_ut_tracing()", tracing_span = "debug")]