openraft 0.10.0-alpha.31

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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
//! [`RaftLeaderId`] implementation that enforces standard Raft behavior of at most one leader per
//! term.

use std::cmp::Ordering;
use std::fmt;
use std::ops::Deref;
use std::ops::DerefMut;

use openraft_macros::since;

use crate::NodeId;
use crate::vote::LeaderIdCompare;
use crate::vote::RaftLeaderId;
use crate::vote::RaftTerm;

/// ID of a `leader`, enforcing a single leader per term.
///
/// It includes the `term` and the `node_id`.
///
/// Raft specifies that in a term there is at most one leader, thus Leader ID is partially order as
/// defined below.
#[since(version = "0.10.0", change = "`voted_for` from `Option<NID>` to `NID`")]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize), serde(bound = ""))]
pub struct LeaderId<Term, NID>
where
    Term: RaftTerm,
    NID: NodeId,
{
    /// The term of the leader.
    pub term: Term,

    /// The node ID that was voted for in this term.
    ///
    /// A `LeaderId` always votes for a node, thus this field is not an `Option`. It is still
    /// serialized as `Option<NID>`, to keep the encoding identical to openraft-0.9 and earlier, in
    /// which this field was an `Option`. A `None` is rejected when decoding.
    #[cfg_attr(feature = "serde", serde(with = "serde_voted_for"))]
    pub voted_for: NID,
}

/// Serialize [`LeaderId::voted_for`] as `Option<NID>`, the encoding used by openraft-0.9 and
/// earlier.
///
/// Self-describing formats such as JSON encode `Some(x)` the same as `x`, but tagged formats such
/// as bincode prefix `Option` with a discriminant byte. Keeping the `Option` in the encoding makes
/// the on-disk data identical in either kind of format, so no data migration is required, and a
/// binary of an earlier version still decodes data written by this version.
#[cfg(feature = "serde")]
mod serde_voted_for {
    use serde::Deserialize;
    use serde::Deserializer;
    use serde::Serialize;
    use serde::Serializer;

    pub(super) fn serialize<S, NID>(voted_for: &NID, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
        NID: Serialize,
    {
        Some(voted_for).serialize(serializer)
    }

    pub(super) fn deserialize<'de, D, NID>(deserializer: D) -> Result<NID, D::Error>
    where
        D: Deserializer<'de>,
        NID: Deserialize<'de>,
    {
        Option::<NID>::deserialize(deserializer)?
            .ok_or_else(|| serde::de::Error::custom("LeaderId.voted_for must not be null"))
    }
}

impl<Term, NID> PartialOrd for LeaderId<Term, NID>
where
    Term: RaftTerm,
    NID: NodeId,
{
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        LeaderIdCompare::std(self, other)
    }
}

impl<Term, NID> fmt::Display for LeaderId<Term, NID>
where
    Term: RaftTerm,
    NID: NodeId,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "T{}-N{}", self.term, self.voted_for)
    }
}

impl<Term, NID> PartialEq<CommittedLeaderId<Term>> for LeaderId<Term, NID>
where
    Term: RaftTerm,
    NID: NodeId,
{
    fn eq(&self, _other: &CommittedLeaderId<Term>) -> bool {
        // Committed and non-committed are never equal
        false
    }
}

impl<Term, NID> PartialOrd<CommittedLeaderId<Term>> for LeaderId<Term, NID>
where
    Term: RaftTerm,
    NID: NodeId,
{
    fn partial_cmp(&self, other: &CommittedLeaderId<Term>) -> Option<Ordering> {
        if self.term == other.term {
            // For the same term, committed Leader overrides non-committed
            Some(Ordering::Less)
        } else {
            self.term.partial_cmp(&other.term)
        }
    }
}

/// Reciprocal comparison: `CommittedLeaderId` compared with `LeaderId`.
///
/// Not required by [`RaftLeaderId`] trait bound, but provides symmetric comparison semantics.
impl<Term, NID> PartialEq<LeaderId<Term, NID>> for CommittedLeaderId<Term>
where
    Term: RaftTerm,
    NID: NodeId,
{
    fn eq(&self, _other: &LeaderId<Term, NID>) -> bool {
        false
    }
}

/// Reciprocal comparison: `CommittedLeaderId` compared with `LeaderId`.
///
/// Not required by [`RaftLeaderId`] trait bound, but provides symmetric comparison semantics.
impl<Term, NID> PartialOrd<LeaderId<Term, NID>> for CommittedLeaderId<Term>
where
    Term: RaftTerm,
    NID: NodeId,
{
    fn partial_cmp(&self, other: &LeaderId<Term, NID>) -> Option<Ordering> {
        if self.term == other.term {
            Some(Ordering::Greater)
        } else {
            self.term.partial_cmp(&other.term)
        }
    }
}

impl<Term, NID> RaftLeaderId for LeaderId<Term, NID>
where
    Term: RaftTerm,
    NID: NodeId,
{
    type Term = Term;
    type NodeId = NID;
    type Committed = CommittedLeaderId<Term>;

    fn new(term: Term, node_id: NID) -> Self {
        Self {
            term,
            voted_for: node_id,
        }
    }

    fn term(&self) -> Term {
        self.term
    }

    fn node_id(&self) -> &NID {
        &self.voted_for
    }

    fn to_committed(&self) -> Self::Committed {
        CommittedLeaderId::new(self.term)
    }
}

/// The unique identifier of a leader that is already granted by a quorum in phase-1(voting).
///
/// [`CommittedLeaderId`] contains less information than [`LeaderId`], because it implies the
/// constraint that **a quorum has granted it**.
///
/// For a partial order `LeaderId`, we know that all the committed leader-id must be a total order
/// set. Therefore, once it is granted by a quorum, it only keeps the information that makes
/// leader-ids a correct total order set, e.g., in standard raft, `voted_for: Option<node_id>` can
/// be removed from `(term, voted_for)` once it is granted. This is why standard Raft stores just a
/// `term` in log entry to identify the Leader proposing the log entry.
///
/// In std mode, `CommittedLeaderId` is just a wrapper of `Term`, which is an integer in most
/// cases (u64, u32, u16, u8, i64, i32, i16, i8).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(PartialOrd, Ord)]
#[derive(derive_more::Display)]
#[display("{}", term)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize), serde(bound = ""))]
#[cfg_attr(feature = "serde", serde(transparent))]
#[repr(transparent)]
pub struct CommittedLeaderId<Term>
where Term: RaftTerm
{
    /// The term of the committed leader.
    pub term: Term,
}

impl<Term> CommittedLeaderId<Term>
where Term: RaftTerm
{
    /// Create a new committed leader ID for the given term.
    pub fn new(term: Term) -> Self {
        Self { term }
    }
}

impl<Term> Deref for CommittedLeaderId<Term>
where Term: RaftTerm
{
    type Target = Term;

    fn deref(&self) -> &Self::Target {
        &self.term
    }
}

impl<Term> DerefMut for CommittedLeaderId<Term>
where Term: RaftTerm
{
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.term
    }
}

#[rustfmt::skip]
mod impl_from_int {
    use crate::vote::leader_id_std::CommittedLeaderId;

    impl From<u64> for CommittedLeaderId<u64> {fn from(term: u64) -> Self {Self {term}}}
    impl From<u32> for CommittedLeaderId<u32> {fn from(term: u32) -> Self {Self {term}}}
    impl From<u16> for CommittedLeaderId<u16> {fn from(term: u16) -> Self {Self {term}}}
    impl From<u8>  for CommittedLeaderId<u8>  {fn from(term: u8)  -> Self {Self {term}}}
    impl From<i64> for CommittedLeaderId<i64> {fn from(term: i64) -> Self {Self {term}}}
    impl From<i32> for CommittedLeaderId<i32> {fn from(term: i32) -> Self {Self {term}}}
    impl From<i16> for CommittedLeaderId<i16> {fn from(term: i16) -> Self {Self {term}}}
    impl From<i8>  for CommittedLeaderId<i8>  {fn from(term: i8)  -> Self {Self {term}}}
}

#[cfg(test)]
#[allow(clippy::nonminimal_bool)]
mod tests {
    use super::LeaderId;
    use crate::vote::RaftLeaderId;

    #[cfg(feature = "serde")]
    #[test]
    fn test_committed_leader_id_serde() -> anyhow::Result<()> {
        use super::CommittedLeaderId;

        let c = CommittedLeaderId::<u64>::new(5);
        let s = serde_json::to_string(&c)?;
        assert_eq!(r#"5"#, s);

        let c2: CommittedLeaderId<u64> = serde_json::from_str(&s)?;
        assert_eq!(CommittedLeaderId::new(5), c2);

        Ok(())
    }

    /// `voted_for` must be encoded as `Option<NID>`, the shape used by openraft-0.9 and earlier.
    ///
    /// A self-describing format such as JSON encodes `Some(x)` the same as `x`, but a tagged
    /// format such as bincode prefixes an `Option` with a discriminant byte.
    #[cfg(feature = "serde")]
    #[test]
    fn test_leader_id_serde_compat_with_option() -> anyhow::Result<()> {
        /// `LeaderId` as defined by openraft-0.9 and earlier.
        #[derive(serde::Deserialize, serde::Serialize)]
        struct LegacyLeaderId {
            term: u64,
            voted_for: Option<u64>,
        }

        let legacy = LegacyLeaderId {
            term: 5,
            voted_for: Some(3),
        };
        let lid = LeaderId::<u64, u64>::new(5, 3);

        let legacy_json = serde_json::to_string(&legacy)?;
        assert_eq!(legacy_json, serde_json::to_string(&lid)?);
        let decoded: LeaderId<u64, u64> = serde_json::from_str(&legacy_json)?;
        assert_eq!(lid, decoded);

        let legacy_bin = bincode::serialize(&legacy)?;
        assert_eq!(legacy_bin, bincode::serialize(&lid)?);
        let decoded: LeaderId<u64, u64> = bincode::deserialize(&legacy_bin)?;
        assert_eq!(lid, decoded);

        Ok(())
    }

    /// A `None` written by openraft-0.9 or earlier is rejected when decoding, instead of building a
    /// `LeaderId` without a node id.
    #[cfg(feature = "serde")]
    #[test]
    fn test_leader_id_serde_reject_none() -> anyhow::Result<()> {
        let res = serde_json::from_str::<LeaderId<u64, u64>>(r#"{"term":5,"voted_for":null}"#);
        assert_eq!(
            "LeaderId.voted_for must not be null at line 1 column 27",
            res.unwrap_err().to_string()
        );

        let legacy_none = bincode::serialize(&(5u64, None::<u64>))?;
        let res = bincode::deserialize::<LeaderId<u64, u64>>(&legacy_none);
        assert_eq!("LeaderId.voted_for must not be null", res.unwrap_err().to_string());

        Ok(())
    }

    #[test]
    #[allow(clippy::neg_cmp_op_on_partial_ord)]
    fn test_std_leader_id_partial_order() -> anyhow::Result<()> {
        #[allow(clippy::redundant_closure)]
        let lid = |term, node_id| LeaderId::<u64, u64>::new(term, node_id);

        // Compare term first
        assert!(lid(2, 2) > lid(1, 2));
        assert!(lid(1, 2) < lid(2, 2));

        // Equal
        assert!(lid(2, 2) == lid(2, 2));
        assert!(lid(2, 2) >= lid(2, 2));
        assert!(lid(2, 2) <= lid(2, 2));

        // Incomparable
        assert!(!(lid(2, 2) > lid(2, 3)));
        assert!(!(lid(2, 2) < lid(2, 3)));
        assert!(!(lid(2, 2) == lid(2, 3)));

        Ok(())
    }

    #[test]
    #[allow(clippy::neg_cmp_op_on_partial_ord)]
    fn test_leader_id_vs_committed_partial_order() -> anyhow::Result<()> {
        use super::CommittedLeaderId;

        let lid = |term, node_id| LeaderId::<u64, u64>::new(term, node_id);
        let clid = |term| CommittedLeaderId::<u64>::new(term);

        // PartialEq: LeaderId and CommittedLeaderId are never equal
        assert!(lid(2, 2) != clid(2));

        // Same term: CommittedLeaderId > LeaderId
        assert!(lid(2, 2) < clid(2));
        assert!(!(lid(2, 2) > clid(2)));

        // Different terms: compare by term
        assert!(lid(1, 2) < clid(2));
        assert!(lid(3, 2) > clid(2));

        Ok(())
    }

    #[test]
    #[allow(clippy::neg_cmp_op_on_partial_ord)]
    fn test_committed_vs_leader_id_partial_order() -> anyhow::Result<()> {
        use super::CommittedLeaderId;

        let lid = |term, node_id| LeaderId::<u64, u64>::new(term, node_id);
        let clid = |term| CommittedLeaderId::<u64>::new(term);

        // PartialEq: CommittedLeaderId and LeaderId are never equal (symmetric)
        assert!(clid(2) != lid(2, 2));
        assert!(clid(2) != lid(2, 5));

        // Same term: CommittedLeaderId > LeaderId (symmetric)
        assert!(clid(2) > lid(2, 2));
        assert!(!(clid(2) < lid(2, 2)));
        assert!(!(clid(2) == lid(2, 2)));

        // Different terms: compare by term (symmetric)
        assert!(clid(2) > lid(1, 2));
        assert!(clid(2) < lid(3, 2));
        assert!(!(clid(2) > lid(3, 2)));
        assert!(!(clid(2) == lid(1, 2)));

        Ok(())
    }

    #[test]
    fn test_committed_leader_id_deref() -> anyhow::Result<()> {
        use super::CommittedLeaderId;

        let clid = CommittedLeaderId::<u64>::new(5);
        assert_eq!(5, *clid);

        let term_ref: &u64 = &clid;
        assert_eq!(&5, term_ref);

        Ok(())
    }

    #[test]
    fn test_committed_leader_id_deref_mut() -> anyhow::Result<()> {
        use super::CommittedLeaderId;

        let mut clid = CommittedLeaderId::<u64>::new(5);
        *clid = 10;
        assert_eq!(10, *clid);
        assert_eq!(CommittedLeaderId::new(10), clid);

        Ok(())
    }

    #[test]
    fn test_committed_leader_id_from_int() -> anyhow::Result<()> {
        use super::CommittedLeaderId;

        macro_rules! test_from {
            ($term_type:ty, $value:expr) => {{
                let clid: CommittedLeaderId<$term_type> = $value.into();
                assert_eq!(CommittedLeaderId::new($value), clid);

                let clid = CommittedLeaderId::<$term_type>::from($value);
                assert_eq!(CommittedLeaderId::new($value), clid);
            }};
        }

        test_from!(u64, 5u64);
        test_from!(u32, 5u32);
        test_from!(u16, 5u16);
        test_from!(u8, 5u8);
        test_from!(i64, 5i64);
        test_from!(i32, 5i32);
        test_from!(i16, 5i16);
        test_from!(i8, 5i8);

        Ok(())
    }
}