Skip to content

Commit 4f49f02

Browse files
maackledrmingdrmer
authored andcommitted
feat: Put RaftNetworkV2 impl for RaftNetwork behind a feature
Make the default implementation `impl<T: RaftNetwork> RaftNetworkV2 for T` optional via the new feature flag `adapt-network-v1`. This prevents conflicts when applications implement `RaftNetworkV2` generically, while still providing an upgrade path for applications transitioning from `RaftNetwork` to `RaftNetworkV2`.
1 parent 3fb0d95 commit 4f49f02

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

openraft/Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ serde_json = { workspace = true }
4242

4343

4444
[features]
45-
default = ["tokio-rt"]
45+
default = ["tokio-rt", "adapt-network-v1"]
4646

4747
# Enable the default Tokio runtime
4848
tokio-rt = ["dep:tokio"]
@@ -74,6 +74,12 @@ type-alias = []
7474
# Provide basic compatible types
7575
compat = []
7676

77+
# Enable this feature to automatically implement `RaftNetworkV2` for `RaftNetworkV1` implementations.
78+
# This helps to migrate to `RaftNetworkV2` without changing your existing implementation.
79+
# However, if this is enabled, the blanket implementation of `RaftNetworkV2` may result in
80+
# conflicting implementations in certain cases.
81+
adapt-network-v1 = []
82+
7783
# Disallows applications to share a raft instance with multiple threads.
7884
singlethreaded = ["openraft-macros/singlethreaded"]
7985

openraft/src/docs/getting_started/getting-started.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,19 @@ When the server receives a Raft RPC, it simply passes it to its `raft` instance
233233
For a real-world implementation, you may want to use [Tonic gRPC](https://github.com/hyperium/tonic) to handle gRPC-based communication between Raft nodes. The [databend-meta](https://github.com/databendlabs/databend/blob/6603392a958ba8593b1f4b01410bebedd484c6a9/metasrv/src/network.rs#L89) project provides an excellent real-world example of a Tonic gRPC-based Raft network implementation.
234234

235235

236+
Note: when implementing `RaftNetworkV2<T>` where `T` is a supertype of `RaftTypeConfig` (for instance, `impl<T: MySuperType> RaftNetworkV2<T> for YourNetworkType<T>`), the compiler may complain with the following error:
237+
238+
```text
239+
conflicting implementations of trait `RaftNetworkV2<_>` for type `YourNetworkType<_>`
240+
conflicting implementation in crate `openraft`:
241+
- impl<C, V1> RaftNetworkV2<C> for V1
242+
where C: RaftTypeConfig, V1: RaftNetwork<C>, <C as RaftTypeConfig>::SnapshotData: tokio::io::async_read::AsyncRead, <C as RaftTypeConfig>::SnapshotData: tokio::io::async_write::AsyncWrite, <C as RaftTypeConfig>::SnapshotData: tokio::io::async_seek::AsyncSeek, <C as RaftTypeConfig>::SnapshotData: Unpin;
243+
downstream crates may implement trait `openraft::RaftNetwork<_>` for type `YourNetworkType<_>`
244+
```
245+
246+
If so, you will want to disable the feature `adapt-network-v1`, which will remove forward compatibility for V1 implementations, but will also fix this error.
247+
248+
236249
### Implement [`RaftNetworkFactory`].
237250

238251
[`RaftNetworkFactory`] is a singleton responsible for creating [`RaftNetworkV2`] instances for each replication target node.

openraft/src/network/v2/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#[cfg(feature = "tokio-rt")]
1+
#[cfg(all(feature = "tokio-rt", feature = "adapt-network-v1"))]
22
mod adapt_v1;
33
mod network;
44

0 commit comments

Comments
 (0)