You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In OpenRaft, whether a term supports multiple or single leaders is
determined by the ordering implementation of `LeaderId`. This behavior
is independent of compilation and can be configured at the application
level, removing the need for a compilation flag `single-term-leader`.
### Changes:
- The `single-term-leader` feature flag is removed.
- Leader mode (multi-leader or single-leader per term) is now controlled
by the `LeaderId` associated type in the application configuration.
### Configuration Guide:
To enable **standard Raft mode (single leader per term)**:
- Set `LeaderId` to `openraft::impls::leader_id_std::LeaderId` in the
`declare_raft_types!` statement, or within the `RaftTypeConfig`
implementation.
Example:
```rust
openraft::declare_raft_types!(
pub TypeConfig:
D = String,
LeaderId = openraft::impls::leader_id_std::LeaderId<TypeConfig>,
);
```
To use the **default multi-leader per term mode**, leave `LeaderId`
unset or explicitly set it to
`openraft::impls::leader_id_adv::LeaderId`.
Example:
```rust
openraft::declare_raft_types!(
pub TypeConfig:
D = String,
);
// Or:
openraft::declare_raft_types!(
pub TypeConfig:
D = String,
LeaderId = openraft::impls::leader_id_adv::LeaderId<TypeConfig>,
);
```
Upgrade tip:
If the `single-term-leader` flag was previously used, remove it and
configure `LeaderId` as follows:
```rust
openraft::declare_raft_types!(
pub TypeConfig:
LeaderId = openraft::impls::leader_id_std::LeaderId<TypeConfig>,
);
```
Copy file name to clipboardExpand all lines: openraft/src/docs/data/leader_id.md
+15-10Lines changed: 15 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,13 @@
1
1
## Leader-id in Advanced mode and Standard mode
2
2
3
-
Openraft provides two `LeaderId` types to switch between these two modes with feature [`single-term-leader`] :
3
+
Openraft provides two `LeaderId` types to switch between these two modes:
4
+
- the default mode: every term may have more than one leader
5
+
(enabled by default, or explicitly by setting [`RaftTypeConfig::LeaderId`] to [`leader_id_adv::LeaderId`]).
6
+
- and the standard Raft mode: every term has only one leader
7
+
(enabled by setting [`RaftTypeConfig::LeaderId`] to [`leader_id_std::LeaderId`]).
4
8
5
-
The feature [`single-term-leader`] affects the `PartialOrd` implementation of `LeaderId`, where `LeaderId` is defined as a tuple `(term, node_id)`.
9
+
[`leader_id_adv::LeaderId`] is totally ordered.
10
+
[`leader_id_std::LeaderId`] is `PartialOrd`.
6
11
7
12
### Definition of `LeaderId`
8
13
@@ -15,27 +20,26 @@ Within Openraft, and also implicitly in the standard Raft, `LeaderId` is utilize
15
20
`A.term > B.term ↔ A > B`.
16
21
<br/><br/>
17
22
18
-
- Conversely, in Openraft, with the [`single-term-leader`] feature disabled by default, `LeaderId` follows a `total order` based on lexicographical comparison:
23
+
- Conversely, in Openraft, with [`leader_id_adv::LeaderId`], `LeaderId` follows a `total order` based on lexicographical comparison:
Activating the `single-term-leader` feature makes `LeaderId` conform to the `partial order` seen in standard Raft.
27
+
Using [`leader_id_std::LeaderId`] makes `LeaderId` conform to the `partial order` seen in standard Raft.
23
28
24
29
### Usage of `LeaderId`
25
30
26
31
When handling `VoteRequest`, both Openraft and standard Raft (though not explicitly detailed) rely on the ordering of `LeaderId` to decide whether to grant a vote:
27
32
**a node will grant a vote with a `LeaderId` that is greater than any it has previously granted**.
28
33
29
-
Consequently, by default in Openraft (with `single-term-leader` disabled), it is possible to elect multiple `Leader`s within the same term, with the last elected `Leader` being recognized as valid. In contrast, under standard Raft protocol, only a single `Leader` is elected per `term`.
34
+
Consequently, by default in Openraft (with [`leader_id_adv::LeaderId`]), it is possible to elect multiple `Leader`s within the same term, with the last elected `Leader` being recognized as valid. In contrast, under standard Raft protocol, only a single `Leader` is elected per `term`.
30
35
31
36
### Default: advanced mode
32
37
33
-
`cargo build` without[`single-term-leader`][], is the advanced mode, the default mode:
38
+
Use `openraft::impls::leader_id_adv::LeaderId` for[`RaftTypeConfig::LeaderId`] or leave it to default to switch to advanced mode.
34
39
`LeaderId` is defined as the following, and it is a **totally ordered** value(two or more leaders can be granted in the same term):
35
40
36
41
```ignore
37
42
// Advanced mode(default):
38
-
#[cfg(not(feature = "single-term-leader"))]
39
43
#[derive(PartialOrd, Ord)]
40
44
pub struct LeaderId<NID: NodeId>
41
45
{
@@ -57,12 +61,11 @@ elected(although only the last is valid and can commit logs).
57
61
58
62
#### Standard mode
59
63
60
-
`cargo build --features "single-term-leader"` builds openraft in standard raft mode.
64
+
Use `openraft::impls::leader_id_std::LeaderId` for [`RaftTypeConfig::LeaderId`] to switch to standard mode.
61
65
In the standard mode, `LeaderId` is defined as the following, and it is a **partially ordered** value(no two leaders can be granted in the same term):
62
66
63
67
```ignore
64
68
// Standard raft mode:
65
-
#[cfg(feature = "single-term-leader")]
66
69
pub struct LeaderId<NID: NodeId>
67
70
{
68
71
pub term: u64,
@@ -125,4 +128,6 @@ So let a committed `Vote` override a incomparable non-committed is safe.
0 commit comments