openraft 0.10.1

Advanced Raft consensus
Documentation
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
use std::error::Error;
use std::fmt;
use std::sync::Arc;

use openraft_macros::since;
use validit::Validate;

use crate::LogIdOptionExt;
use crate::errors::ChangeMembershipError;
use crate::errors::ErrorSource;
use crate::errors::InProgress;
use crate::errors::MembershipError;
use crate::errors::UnsupportedMembershipTransition;

#[cfg(test)]
mod change_membership_test;
#[cfg(test)]
mod membership_state_test;

use crate::log_id::LogId;
use crate::membership::RaftMembership;
use crate::membership::RaftMembershipEdit;
use crate::membership::RaftMembershipExt;
use crate::membership::StoredMembership;
use crate::vote::RaftCommittedLeaderId;

/// The state of membership configs a raft node needs to know.
///
/// `MembershipState` is a minimal store of the membership log and the membership state machine:
/// - `committed` is the membership state machine, i.e., the last committed membership;
/// - `effective` is the last membership log, and thus plays the role of a log entry: the last seen
///   membership, committed or not.
///
/// A raft node needs to store at most 2 membership config logs:
/// - The first(committed) one must have been committed, because (1): raft allows proposing new
///   membership only when the previous one is committed.
/// - The second(effective) may be committed or not.
///
/// From (1) we have:
/// (2) there is at most one outstanding, uncommitted membership log. On
/// either leader or follower, the second last one must have been committed.
/// A committed log must be consistent with the leader.
///
/// (3) By raft design, the last membership takes effect.
///
/// When handling append-entries RPC:
/// (4) a raft follower will delete logs that are inconsistent with the leader.
///
/// From (3) and (4), a follower needs to revert the effective membership to the previous one.
///
/// From (2), a follower only needs to revert at most one membership log.
///
/// Thus, a raft node will only need to store at most two recent membership logs.
#[since(
    version = "0.10.0",
    change = "from `MembershipState<C>` to `MembershipState<CLID, Mem>`"
)]
#[derive(Debug, Clone)]
#[derive(PartialEq, Eq)]
pub struct MembershipState<CLID, Mem>
where
    CLID: RaftCommittedLeaderId,
    Mem: RaftMembership,
{
    committed: Arc<StoredMembership<CLID, Mem>>,

    // Using `Arc` because the effective membership will be copied to RaftMetrics frequently.
    effective: Arc<StoredMembership<CLID, Mem>>,
}

impl<CLID, Mem> Default for MembershipState<CLID, Mem>
where
    CLID: RaftCommittedLeaderId,
    Mem: RaftMembership,
{
    fn default() -> Self {
        Self {
            committed: Arc::new(StoredMembership::default()),
            effective: Arc::new(StoredMembership::default()),
        }
    }
}

impl<CLID, Mem> fmt::Display for MembershipState<CLID, Mem>
where
    CLID: RaftCommittedLeaderId,
    Mem: RaftMembership,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "MembershipState{{committed: {}, effective: {}}}",
            self.committed, self.effective
        )
    }
}

impl<CLID, Mem> MembershipState<CLID, Mem>
where
    CLID: RaftCommittedLeaderId,
    Mem: RaftMembership,
{
    pub(crate) fn new(
        committed: Arc<StoredMembership<CLID, Mem>>,
        effective: Arc<StoredMembership<CLID, Mem>>,
    ) -> Self {
        Self { committed, effective }
    }

    /// Returns whether `id` is a voter or learner in the effective membership.
    pub(crate) fn contains(&self, id: &Mem::NodeId) -> bool {
        self.effective.membership().contains(id)
    }

    /// Returns whether `id` may campaign under the effective membership.
    ///
    /// This uses the latest membership even before its log entry commits.
    /// Election triggers use [`Self::is_campaigner_in_effective_or_committed`] instead.
    pub(crate) fn is_campaigner(&self, id: &Mem::NodeId) -> bool {
        self.effective.membership().is_campaigner(id)
    }

    /// Whether a node may campaign according to its effective or committed membership.
    ///
    /// A removed campaigner may need to regain leadership to finish the membership change.
    /// It remains eligible until the membership change commits, even if retained as a learner.
    /// Voting and quorum checks use only the effective membership.
    pub(crate) fn is_campaigner_in_effective_or_committed(&self, id: &Mem::NodeId) -> bool {
        self.effective.is_campaigner(id) || self.committed.is_campaigner(id)
    }

    /// Builds a new membership configuration by applying changes to the current configuration.
    ///
    /// It returns the target of `change` when `supports_direct_transition_to()` holds from the
    /// effective membership to the target. Otherwise it returns the `joint_with()` of the two,
    /// which needs no check: every read quorum of the joint is a read quorum of both memberships,
    /// and the same holds for write quorums. Both memberships passed
    /// `ensure_read_write_quorums_intersect()`, so on each step to and from the joint, every read
    /// quorum of one side intersects every write quorum of the other side.
    ///
    /// E.g.(`cicj` is a joint membership of `ci` and `cj`, and for any two of `c1`, `c2`, `c3`, a
    /// quorum of one can miss a quorum of the other):
    /// - from `c1` to `c1`, it returns `c1`
    /// - from `c1` to `c2`, it returns `c1c2`
    /// - from `c1c2` to `c2`, it returns `c2`
    /// - from `c1c2` to `c1`, it returns `c1`
    /// - from `c1c2` to `c3`, it returns `c1c2c3`
    ///
    /// * `changes`: The changes to apply to the current membership configuration.
    /// * `retain` specifies whether to retain the removed voters as learners, i.e., nodes that
    ///   continue to receive log replication from the leader.
    ///
    /// Returns the next membership and, for a joint step, the exact target to append afterward.
    ///
    /// This function ensures that the cluster will have at least one voter in the new membership
    /// configuration.
    pub(crate) fn next_membership<Edit, E>(
        &self,
        change: Edit,
        retain: bool,
    ) -> Result<(Mem, Option<Mem>), ChangeMembershipError<CLID, Mem::NodeId, E>>
    where
        Edit: RaftMembershipEdit<Mem>,
        E: ErrorSource,
    {
        self.ensure_committed()?;

        let effective = self.effective().membership();
        let wrap = |error| E::from_error(&error);
        let goal = change.apply(effective, retain);
        let goal = goal.map_err(|error| error.map_user_defined(wrap))?;
        goal.ensure_non_empty_config()?;
        goal.ensure_voters_have_nodes().map_err(MembershipError::<_, E>::from)?;
        goal.ensure_write_voters_receive_logs()?;
        goal.ensure_read_write_quorums_intersect()?;
        if effective.allows_direct_transition_to(&goal) {
            return Ok((goal, None));
        }

        let joint = effective.joint_with(&goal);
        Ok((joint, Some(goal)))
    }

    /// Validates a membership a caller wants appended as one log entry, without an intermediate
    /// joint membership.
    ///
    /// It rejects the request unless every one of these holds:
    ///
    /// - The last membership log is committed, so the effective membership is also the last
    ///   committed one, the parent this transition starts from.
    /// - `proposed` is a valid membership on its own, and it defines a quorum. A public constructor
    ///   is not enough of a guarantee: deserialization, [`Membership::new()`],
    ///   [`Membership::default()`] and [`Membership::new_with_defaults()`] can all produce a value
    ///   with no voter set at all, or with an empty one.
    /// - Every write voter of `proposed` has [`NodeRole::ReceiveLogs`], so it can acknowledge logs.
    /// - `read_write_quorums_intersect()` returns `Some(true)` for `proposed`, so every read quorum
    ///   of it intersects every write quorum.
    /// - `proposed` keeps the [`Node`] of every node id the effective membership already knows.
    /// - The transition from the effective membership to `proposed` is one that a direct append
    ///   supports.
    ///
    /// [`Membership::new()`]: crate::Membership::new
    /// [`Membership::default()`]: crate::Membership::default
    /// [`Membership::new_with_defaults()`]: crate::Membership::new_with_defaults
    /// [`Node`]: crate::Node
    /// [`NodeRole::ReceiveLogs`]: crate::membership::NodeRole::ReceiveLogs
    pub(crate) fn validate_append_membership<E>(
        &self,
        proposed: &Mem,
    ) -> Result<(), ChangeMembershipError<CLID, Mem::NodeId, E>>
    where
        E: ErrorSource,
    {
        self.ensure_committed()?;
        proposed.ensure_non_empty_config()?;
        proposed.ensure_voters_have_nodes().map_err(MembershipError::<_, E>::from)?;
        proposed.ensure_write_voters_receive_logs()?;
        proposed.ensure_read_write_quorums_intersect()?;

        let effective = self.effective().membership();

        effective.ensure_node_metadata_unchanged(proposed)?;

        let compatible = effective.allows_direct_transition_to(proposed);
        if !compatible {
            let err = UnsupportedMembershipTransition {
                previous: effective.to_string(),
                proposed: proposed.to_string(),
            };
            return Err(err.into());
        }

        Ok(())
    }

    /// Ensures that the latest membership has been committed.
    ///
    /// Returns Ok if the last membership is committed, or an InProgress error
    /// otherwise, to indicate a change-membership request should be rejected.
    fn ensure_committed(&self) -> Result<(), InProgress<CLID>> {
        let effective = self.effective();
        let committed = self.committed();

        if effective.log_id() == committed.log_id() {
            return Ok(());
        }

        Err(InProgress {
            committed: committed.log_id().clone(),
            membership_log_id: effective.log_id().clone(),
        })
    }

    /// Commit the effective membership config if `committed_log_id` is greater than or equal to
    /// its log id.
    ///
    /// Committing replaces `self.committed`(the membership state machine) with
    /// `self.effective`(the last membership log).
    pub(crate) fn commit(&mut self, committed_log_id: &Option<LogId<CLID>>) {
        let current = self.committed.log_id().clone();
        let last = self.effective().log_id().clone();

        // `current == last` means the effective membership is already committed.
        if committed_log_id >= &last && current < last {
            debug_assert!(committed_log_id.index() >= last.index());
            self.committed = self.effective.clone();
        }
    }

    /// Install the membership config carried by a snapshot.
    ///
    /// A snapshot stores the last membership applied to its state machine, so this membership is
    /// committed. It updates `self.committed`(the membership state machine), and may also update
    /// `self.effective`(the last membership log), each only when the snapshot is newer than the
    /// corresponding local membership.
    pub(crate) fn install_membership_snapshot(
        &mut self,
        membership_snapshot: Arc<StoredMembership<CLID, Mem>>,
        snapshot_last_log_index: u64,
    ) {
        // Snapshot install purges every log entry up to snapshot_last_log_index.
        let effective_is_purged = self.effective.log_id().index() <= Some(snapshot_last_log_index);

        if effective_is_purged {
            self.committed = membership_snapshot.clone();
            self.effective = membership_snapshot;
            return;
        }

        if membership_snapshot.log_id().index() >= self.effective.log_id().index() {
            self.effective = membership_snapshot.clone()
        }

        #[allow(clippy::collapsible_if)]
        if cfg!(debug_assertions) {
            if membership_snapshot.log_id() == self.committed.log_id() {
                debug_assert_eq!(
                    membership_snapshot.membership(),
                    self.committed.membership(),
                    "the same log id implies the same membership"
                );
            }
        }

        // same log id implies the same membership,
        // so it only needs to compare log id.
        if membership_snapshot.log_id() > self.committed.log_id() {
            self.committed = membership_snapshot
        }
    }

    /// Append a membership config `m`.
    ///
    /// It assumes `self.effective` does not conflict with the leader's log, i.e.:
    /// - Leader appends a new membership,
    /// - Or a follower has confirmed preceding logs matches the leaders' and appends membership
    ///   received from the leader.
    pub(crate) fn append(&mut self, m: Arc<StoredMembership<CLID, Mem>>) {
        debug_assert!(
            m.log_id() > self.effective.log_id(),
            "new membership has to have a greater log_id"
        );
        debug_assert!(
            m.log_id().index() > self.effective.log_id().index(),
            "new membership has to have a greater index"
        );

        // Openraft allows at most only one non-committed membership config.
        // If there is another new config, self.effective must have been committed.
        self.committed = self.effective.clone();
        self.effective = m;
    }

    /// Truncate membership state if the log is truncated since `since`(inclusive).
    ///
    /// It returns the updated effective membership config if it is changed.
    ///
    /// It will reset `self.effective` to `self.committed`. Only the effective could be truncated
    /// when a new leader tries to truncate follower logs that the leader does not have.
    ///
    /// If the effective membership is from a conflicting log,
    /// the membership state has to revert to the last committed membership config.
    /// See: [Effective-membership](crate::docs::data::effective_membership)
    ///
    /// ```text
    /// committed_membership, ... since, ... effective_membership // log
    /// ^                                    ^
    /// |                                    |
    /// |                                    last membership      // before deleting since..
    /// last membership                                           // after  deleting since..
    /// ```
    pub(crate) fn truncate(&mut self, since: u64) -> Option<Arc<StoredMembership<CLID, Mem>>> {
        debug_assert!(
            since >= self.committed().log_id().next_index(),
            "committed log should never be truncated: committed membership cannot conflict with the leader"
        );

        if Some(since) <= self.effective().log_id().index() {
            tracing::debug!(
                "effective membership is in conflicting logs, revert it to last committed: effective: {}, committed: {}",
                self.effective(),
                self.committed()
            );

            self.effective = self.committed.clone();
            return Some(self.effective.clone());
        }
        None
    }

    // This method is only used by tests
    #[cfg(test)]
    pub(crate) fn set_effective(&mut self, e: Arc<StoredMembership<CLID, Mem>>) {
        self.effective = e
    }

    /// Returns a reference to the last committed membership config.
    ///
    /// A committed membership config may or may not be the same as the effective one.
    pub fn committed(&self) -> &Arc<StoredMembership<CLID, Mem>> {
        &self.committed
    }

    /// Returns a reference to the presently effective membership config.
    ///
    /// In openraft the last seen membership config, whether committed or not, is the effective
    /// one.
    ///
    /// A committed membership config may or may not be the same as the effective one.
    pub fn effective(&self) -> &Arc<StoredMembership<CLID, Mem>> {
        &self.effective
    }
}

impl<CLID, Mem> Validate for MembershipState<CLID, Mem>
where
    CLID: RaftCommittedLeaderId,
    Mem: RaftMembership,
{
    fn validate(&self) -> Result<(), Box<dyn Error>> {
        validit::less_equal!(self.committed.log_id(), self.effective.log_id());
        validit::less_equal!(self.committed.log_id().index(), self.effective.log_id().index());
        Ok(())
    }
}