Skip to content

Commit ef090e1

Browse files
committed
docs: refine docs for feature-flag adapt-network-v1
Complete the pseudo code to produce the conflict. Add doc in the feature-flags section in `docs` crate. Add link to this feature-flags for `RaftNetworkV2` doc.
1 parent 4f49f02 commit ef090e1

File tree

5 files changed

+78
-19
lines changed

5 files changed

+78
-19
lines changed

openraft/Cargo.toml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,17 @@ type-alias = []
7474
# Provide basic compatible types
7575
compat = []
7676

77-
# Enable this feature to automatically implement `RaftNetworkV2` for `RaftNetworkV1` implementations.
77+
# Enable this feature to automatically implement `RaftNetworkV2` for `RaftNetwork` implementations.
7878
# This helps to migrate to `RaftNetworkV2` without changing your existing implementation.
7979
# However, if this is enabled, the blanket implementation of `RaftNetworkV2` may result in
80-
# conflicting implementations in certain cases.
80+
# conflicting if the application tries to implement `RaftNetworkV2<T>` where `T`
81+
# is a generic type. For example a conflict in application with this feature on:
82+
# ```
83+
# pub trait RaftTypeConfigExt: openraft::RaftTypeConfig {}
84+
# pub struct NetworkConnection2 {}
85+
# impl<T> RaftNetworkV2<T> for NetworkConnection2
86+
# where T: RaftTypeConfigExt + Send + Sync + 'static {/*...*/}
87+
# ```
8188
adapt-network-v1 = []
8289

8390
# Disallows applications to share a raft instance with multiple threads.
@@ -122,4 +129,4 @@ no-default-features = false
122129
#
123130
# Sort modules by appearance order for crate `docs`.
124131
# https://doc.rust-lang.org/rustdoc/unstable-features.html#--sort-modules-by-appearance-control-how-items-on-module-pages-are-sorted
125-
# rustdoc-args = ["-Z", "unstable-options", "--sort-modules-by-appearance"]
132+
# rustdoc-args = ["-Z", "unstable-options", "--sort-modules-by-appearance"]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
- [feature-flag `adapt-network-v1`](#feature-flag-adapt-network-v1)
12
- [feature-flag `bench`](#feature-flag-bench)
23
- [feature-flag `bt`](#feature-flag-bt)
34
- [feature-flag `compat`](#feature-flag-compat)
45
- [feature-flag `serde`](#feature-flag-serde)
56
- [feature-flag `single-term-leader`](#feature-flag-single-term-leader)
67
- [feature-flag `singlethreaded`](#feature-flag-singlethreaded)
8+
- [feature-flag `tokio-rt`](#feature-flag-tokio-rt)
79
- [feature-flag `tracing-log`](#feature-flag-tracing-log)
810
- [feature-flag `type-alias`](#feature-flag-type-alias)

openraft/src/docs/feature_flags/feature-flags.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,33 @@
11

2-
By default openraft enables no features.
2+
By default openraft enables `["tokio-rt", "adapt-network-v1"]`.
3+
4+
5+
## feature-flag `adapt-network-v1`
6+
7+
When implementing [`RaftNetworkV2<T>`][`RaftNetworkV2`] for a generic type parameter `T`, you might
8+
encounter a compiler error about conflicting implementations. This happens
9+
because Openraft provides a blanket implementation that adapts `RaftNetwork`
10+
implementations to [`RaftNetworkV2`]. For example:
11+
12+
```rust,ignore
13+
pub trait RaftTypeConfigExt: openraft::RaftTypeConfig {}
14+
pub struct YourNetworkType {}
15+
impl<T: RaftTypeConfigExt> RaftNetworkV2<T> for YourNetworkType {}
16+
```
17+
18+
You might encounter the following error:
19+
20+
```text
21+
conflicting implementations of trait `RaftNetworkV2<_>` for type `YourNetworkType`
22+
conflicting implementation in crate `openraft`:
23+
- impl<C, V1> RaftNetworkV2<C> for V1
24+
```
25+
26+
If you encounter this error, you can disable the feature `adapt-network-v1` to
27+
remvoe the default implementation for [`RaftNetworkV2`].
28+
29+
[`RaftNetworkV2`]: crate::network::v2::RaftNetworkV2
30+
331

432
## feature-flag `bench`
533

@@ -35,6 +63,7 @@ Read more about how it is implemented in:
3563
[`leader_id`](crate::docs::data::leader_id)
3664
and [`vote`](crate::docs::data::vote).
3765

66+
3867
## feature-flag `singlethreaded`
3968

4069
Removes `Send` and `Sync` bounds from `AppData`, `AppDataResponse`, `RaftEntry`, `SnapshotData`
@@ -45,6 +74,13 @@ If the feature is enabled, affected asynchronous trait methods will not require
4574
In order to use the feature, `AsyncRuntime::spawn` should invoke `tokio::task::spawn_local` or equivalents.
4675

4776

77+
## feature-flag `tokio-rt`
78+
79+
Using `tokio` as the default runtime implementation.
80+
With this feature disabled, application should implement and set the
81+
async-runtime to `AsyncRuntime` mannually in [`RaftTypeconfig`] implementation.
82+
83+
4884
## feature-flag `tracing-log`
4985

5086
Enables "log" feature in `tracing` crate, to let tracing events

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

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub struct TypeConfig {}
8080
impl openraft::RaftTypeConfig for TypeConfig {
8181
type D = Request;
8282
type R = Response;
83-
83+
8484
// Following are absent in `declare_raft_types` and filled with default values:
8585
type NodeId = u64;
8686
type Node = openraft::impls::BasicNode;
@@ -91,7 +91,7 @@ impl openraft::RaftTypeConfig for TypeConfig {
9191
}
9292
```
9393

94-
> In the above `TypeConfig` declaration,
94+
> In the above `TypeConfig` declaration,
9595
> - `NodeId` is the identifier of a node in the cluster, which implements [`NodeId`] trait.
9696
> - `Node` is the node type that contains the node's address, etc., which implements [`Node`] trait.
9797
> - `Entry` is the log entry type that will be stored in the raft log,
@@ -233,17 +233,29 @@ 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.
236+
> ### Trouble shooting: implementation conflicts
237+
>
238+
> When implementing `RaftNetworkV2<T>` for a generic type parameter `T`, you might
239+
> encounter a compiler error about conflicting implementations. This happens
240+
> because Openraft provides a blanket implementation that adapts `RaftNetwork`
241+
> implementations to `RaftNetworkV2`. For example:
242+
>
243+
> ```rust,ignore
244+
> pub trait RaftTypeConfigExt: openraft::RaftTypeConfig {}
245+
> pub struct YourNetworkType {}
246+
> impl<T: RaftTypeConfigExt> RaftNetworkV2<T> for YourNetworkType {}
247+
> ```
248+
>
249+
> You might encounter the following error:
250+
>
251+
> ```text
252+
> conflicting implementations of trait `RaftNetworkV2<_>` for type `YourNetworkType`
253+
> conflicting implementation in crate `openraft`:
254+
> - impl<C, V1> RaftNetworkV2<C> for V1
255+
> ```
256+
>
257+
> If you encounter this error, you can disable the feature `adapt-network-v1` to
258+
> remvoe the default implementation for `RaftNetworkV2`.
247259
248260
249261
### Implement [`RaftNetworkFactory`].
@@ -416,7 +428,7 @@ Additionally, two test scripts for setting up a cluster are available:
416428
[`Entry`]: `crate::entry::Entry`
417429
[`docs::Vote`]: `crate::docs::data::Vote`
418430
[`Vote`]: `crate::vote::Vote`
419-
[`LogState`]: `crate::storage::LogState`
431+
[`LogState`]: `crate::storage::LogState`
420432

421433
[`RaftLogReader`]: `crate::storage::RaftLogReader`
422434
[`try_get_log_entries()`]: `crate::storage::RaftLogReader::try_get_log_entries`

openraft/src/network/v2/network.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@ use crate::RaftTypeConfig;
3535
/// and introduces `full_snapshot()` method that let application fully customize snapshot transmit.
3636
///
3737
/// Compatibility: [`RaftNetworkV2`] is automatically implemented for [`RaftNetwork`]
38-
/// implementations.
38+
/// implementations. If it conflicts with your application, disable [`adapt-network-v1`] feature
39+
/// flag.
3940
///
4041
/// [Ensure connection to correct node][correct-node]
4142
///
4243
/// [`RaftNetwork`]: crate::network::v1::RaftNetwork
4344
/// [correct-node]: `crate::docs::cluster_control::dynamic_membership#ensure-connection-to-the-correct-node`
45+
/// [`adapt-network-v1`]: `crate::docs::feature_flags#adapt-network-v1`
4446
#[since(version = "0.10.0")]
4547
#[add_async_trait]
4648
pub trait RaftNetworkV2<C>: OptionalSend + OptionalSync + 'static

0 commit comments

Comments
 (0)