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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use std::collections::BTreeSet;
use crate::errors::EmptyMembership;
use crate::errors::NodeMetadataChanged;
use crate::errors::NodeNotFound;
use crate::errors::Operation;
use crate::membership::NodeRole;
use crate::membership::RaftMembership;
use crate::membership::errors::UnprovenReadWriteIntersection;
use crate::membership::errors::WriteVoterWithoutReceiveLogs;
use crate::quorum::QuorumSet;
/// Operations derived from the required methods of [`RaftMembership`].
pub(crate) trait RaftMembershipExt: RaftMembership {
/// Returns whether `node_id` is a known node of this configuration.
fn contains(&self, node_id: &Self::NodeId) -> bool {
let node = self.get_node(node_id);
node.is_some()
}
/// Returns whether `node_id` may start an election under this configuration.
fn is_campaigner(&self, node_id: &Self::NodeId) -> bool {
let Some(node) = self.get_node(node_id) else {
return false;
};
node.role.contains(NodeRole::Campaigner)
}
/// Returns whether `node_id` may receive replicated logs.
fn receives_logs(&self, node_id: &Self::NodeId) -> bool {
let Some(node) = self.get_node(node_id) else {
return false;
};
node.role.contains(NodeRole::ReceiveLogs)
}
/// Returns whether `node_id` is in the write quorum set.
fn is_write_voter(&self, node_id: &Self::NodeId) -> bool {
let Some(node) = self.get_node(node_id) else {
return false;
};
node.is_write_voter
}
/// Returns whether `node_id` is a known node that receives logs and has no campaign or voter
/// role.
fn is_learner(&self, node_id: &Self::NodeId) -> bool {
let Some(node) = self.get_node(node_id) else {
return false;
};
node.is_learner()
}
/// Returns the IDs in the read or the write quorum set, in ascending order.
fn voter_ids(&self) -> impl Iterator<Item = Self::NodeId> {
let read_ids = self.read_quorum_set().ids();
let write_ids = self.write_quorum_set().ids();
let ids: BTreeSet<_> = read_ids.chain(write_ids).collect();
ids.into_iter()
}
/// Returns the known node IDs outside the write quorum set.
fn non_write_voter_ids(&self) -> impl Iterator<Item = Self::NodeId> {
self.node_ids().filter(|id| !self.is_write_voter(id))
}
/// Returns [`NodeMetadataChanged`] for the first node ID that both configurations know but
/// describe with a different node.
///
/// A node ID that only one of the two configurations knows is skipped: adding a node and
/// removing a node are not metadata changes.
fn ensure_node_metadata_unchanged(&self, other: &Self) -> Result<(), NodeMetadataChanged<Self::NodeId>> {
for node_id in self.node_ids() {
let Some(node) = self.get_node(&node_id) else {
continue;
};
let Some(other_node) = other.get_node(&node_id) else {
continue;
};
if other_node.node != node.node {
return Err(NodeMetadataChanged { node_id });
}
}
Ok(())
}
/// Returns an error if the read or the write quorum set has no usable quorum.
///
/// A quorum set is usable when the empty set is not a quorum and the set of all its IDs is a
/// quorum. [`QuorumSet`] rules are upward-closed, so the second test passes exactly when some
/// quorum exists. A joint quorum set with no component, or with an empty component, fails the
/// second test, because it accepts no set.
///
/// OpenRaft calls it before it uses the quorums of a membership that an application supplies.
fn ensure_non_empty_config(&self) -> Result<(), EmptyMembership> {
let quorum_sets = [self.read_quorum_set(), self.write_quorum_set()];
for quorum_set in quorum_sets {
let empty_is_quorum = quorum_set.is_quorum(std::iter::empty());
if empty_is_quorum {
return Err(EmptyMembership {});
}
let ids: Vec<_> = quorum_set.ids().collect();
let all_is_quorum = quorum_set.is_quorum(ids.iter());
if !all_is_quorum {
return Err(EmptyMembership {});
}
}
Ok(())
}
/// Returns [`NodeNotFound`] for the first quorum ID that has no node.
///
/// OpenRaft calls it before it uses the quorums of a membership that an application supplies.
fn ensure_voters_have_nodes(&self) -> Result<(), NodeNotFound<Self::NodeId>> {
for voter_id in self.voter_ids() {
if !self.contains(&voter_id) {
return Err(NodeNotFound::new(voter_id, Operation::None));
}
}
Ok(())
}
/// Returns [`WriteVoterWithoutReceiveLogs`] for the first write voter without
/// [`NodeRole::ReceiveLogs`].
///
/// OpenRaft replicates logs only to nodes with [`NodeRole::ReceiveLogs`], so such a voter never
/// acknowledges a log. OpenRaft calls it before it uses the quorums of a membership that an
/// application supplies.
fn ensure_write_voters_receive_logs(&self) -> Result<(), WriteVoterWithoutReceiveLogs<Self::NodeId>> {
let write_voter_ids = self.write_quorum_set().ids();
for voter_id in write_voter_ids {
if !self.receives_logs(&voter_id) {
return Err(WriteVoterWithoutReceiveLogs { node_id: voter_id });
}
}
Ok(())
}
/// Returns [`UnprovenReadWriteIntersection`] unless
/// [`read_write_quorums_intersect()`](RaftMembership::read_write_quorums_intersect) returns
/// `Some(true)`.
///
/// OpenRaft calls it before it uses the quorums of a membership that an application supplies.
fn ensure_read_write_quorums_intersect(&self) -> Result<(), UnprovenReadWriteIntersection> {
let intersects = self.read_write_quorums_intersect();
if intersects == Some(true) {
return Ok(());
}
Err(UnprovenReadWriteIntersection {
membership: self.to_string(),
})
}
/// Returns whether `other` can be appended directly after `self`, as one log entry without an
/// intermediate joint configuration.
///
/// Both configurations must pass [`ensure_non_empty_config()`](Self::ensure_non_empty_config).
/// Then [`RaftMembership::supports_direct_transition_to()`] decides.
fn allows_direct_transition_to(&self, other: &Self) -> bool {
if self.ensure_non_empty_config().is_err() {
return false;
}
if other.ensure_non_empty_config().is_err() {
return false;
}
self.supports_direct_transition_to(other)
}
}
impl<T> RaftMembershipExt for T where T: RaftMembership {}
#[cfg(test)]
mod raft_membership_ext_test;