1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use openraft_macros::since;
use crate::RaftTypeConfig;
use crate::type_config::alias::CommittedLeaderIdOf;
/// Optional behavior of a client write.
///
/// The default option proposes the write without conditions. Use [`expect_leader()`] to create an
/// option that makes the write conditional on the current leader.
///
/// [`expect_leader()`]: WriteOption::expect_leader
#[since(version = "0.10.1")]
#[derive(Debug, Clone, Default)]
#[derive(PartialEq, Eq)]
pub struct WriteOption<C>
where C: RaftTypeConfig
{
/// The committed leader that must still be the established leader when the write is
/// proposed; `None` accepts any leader.
pub(crate) expected_leader: Option<CommittedLeaderIdOf<C>>,
}
impl<C> WriteOption<C>
where C: RaftTypeConfig
{
/// Create an option that proposes the write only if the current committed leader is
/// `expected_leader`.
///
/// The same as `WriteOption::default().with_leader(expected_leader)`; see
/// [`with_leader()`](Self::with_leader).
///
/// ```ignore
/// let term = raft.as_leader()?.term();
/// raft.client_write_many([data1, data2], WriteOption::expect_leader(term)).await?;
/// ```
#[since(version = "0.10.1")]
pub fn expect_leader(expected_leader: impl Into<CommittedLeaderIdOf<C>>) -> Self {
Self::default().with_leader(expected_leader)
}
/// Propose the write only if the current committed leader is `expected_leader`.
///
/// If it is not, the write fails with [`ClientWriteError::ForwardToLeader`] and nothing is
/// appended. See [`WriteRequest::with_leader()`] for what a leader id contains under each
/// leader id type.
///
/// [`ClientWriteError::ForwardToLeader`]: crate::errors::ClientWriteError::ForwardToLeader
/// [`WriteRequest::with_leader()`]: crate::raft::WriteRequest::with_leader
#[since(version = "0.10.1")]
pub fn with_leader(mut self, expected_leader: impl Into<CommittedLeaderIdOf<C>>) -> Self {
self.expected_leader = Some(expected_leader.into());
self
}
/// Merge this option with `other` into the option for a batch holding writes of both.
///
/// RaftCore merges consecutive client writes into one batch and proposes the batch with a
/// single option. Returns that option, or `None` if the writes cannot share one: a merged
/// option must make each write behave as its own option asks.
///
/// Two expected leaders merge only when they are equal. Merging a write that accepts any leader
/// with one that expects a specific leader would add or drop that condition for one of them.
pub(crate) fn merge(&self, other: &Self) -> Option<Self> {
if self.expected_leader != other.expected_leader {
return None;
}
Some(Self {
expected_leader: self.expected_leader.clone(),
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::engine::testing::UTConfig;
use crate::engine::testing::log_id;
type C = UTConfig<()>;
fn committed_leader_id(term: u64, node_id: u64) -> CommittedLeaderIdOf<C> {
*log_id(term, node_id, 0).committed_leader_id()
}
#[test]
fn test_merge() {
let any = WriteOption::<C>::default();
let leader1 = WriteOption::<C>::default().with_leader(committed_leader_id(1, 1));
let leader2 = WriteOption::<C>::default().with_leader(committed_leader_id(2, 1));
assert_eq!(Some(any.clone()), any.merge(&WriteOption::default()));
assert_eq!(
Some(leader1.clone()),
leader1.merge(&WriteOption::default().with_leader(committed_leader_id(1, 1)))
);
assert_eq!(None, any.merge(&leader1));
assert_eq!(None, leader1.merge(&any));
assert_eq!(None, leader1.merge(&leader2));
}
#[test]
fn test_expect_leader() {
let leader_id = committed_leader_id(1, 1);
assert_eq!(
WriteOption::<C>::default().with_leader(leader_id),
WriteOption::<C>::expect_leader(leader_id)
);
assert_eq!(
Some(leader_id),
WriteOption::<C>::expect_leader(leader_id).expected_leader
);
}
}