openraft 0.10.0-alpha.30

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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
use crate::LogId;
use crate::RaftState;
use crate::engine::EngineConfig;
use crate::engine::LogIdList;
use crate::engine::testing::UTConfig;
use crate::progress::entry::ProgressEntry;
use crate::progress::inflight::Inflight;
use crate::progress::inflight_id::InflightId;
use crate::progress::stream_id::StreamId;
use crate::type_config::alias::LeaderIdOf;
use crate::type_config::alias::LogIdOf;
use crate::type_config::alias::SnapshotMetaOf;
use crate::type_config::alias::StoredMembershipOf;
use crate::vote::RaftLeaderIdExt;

fn log_id(index: u64) -> LogIdOf<UTConfig> {
    LogId {
        leader_id: LeaderIdOf::<UTConfig>::new_committed(1, 1),
        index,
    }
}

fn inflight_logs(prev_index: u64, last_index: u64) -> Inflight<UTConfig> {
    Inflight::logs(Some(log_id(prev_index)), Some(log_id(last_index)), InflightId::new(0))
}

#[test]
fn test_is_log_range_inflight() -> anyhow::Result<()> {
    let mut pe = ProgressEntry::<UTConfig>::empty(0, StreamId::new(0), 20);
    assert_eq!(false, pe.is_log_range_inflight(&log_id(2)));

    pe.inflight = inflight_logs(2, 4);
    assert_eq!(false, pe.is_log_range_inflight(&log_id(1)));
    assert_eq!(false, pe.is_log_range_inflight(&log_id(2)));
    assert_eq!(true, pe.is_log_range_inflight(&log_id(3)));
    assert_eq!(true, pe.is_log_range_inflight(&log_id(4)));
    assert_eq!(true, pe.is_log_range_inflight(&log_id(5)));

    pe.inflight = Inflight::snapshot(InflightId::new(0));
    assert_eq!(false, pe.is_log_range_inflight(&log_id(5)));

    // LogsSince: all logs after prev are inflight
    pe.inflight = Inflight::logs_since(Some(log_id(2)), InflightId::new(0));
    assert_eq!(false, pe.is_log_range_inflight(&log_id(1)));
    assert_eq!(false, pe.is_log_range_inflight(&log_id(2)));
    assert_eq!(true, pe.is_log_range_inflight(&log_id(3)));
    assert_eq!(true, pe.is_log_range_inflight(&log_id(100)));

    Ok(())
}

#[test]
fn test_update_matching() -> anyhow::Result<()> {
    let engine_config = EngineConfig::new_default(1);

    // Update matching and inflight
    {
        let mut pe = ProgressEntry::<UTConfig>::empty(0, StreamId::new(0), 20);
        pe.inflight = inflight_logs(5, 10);
        pe.new_updater(&engine_config).update_matching(Some(log_id(6)), Some(InflightId::new(0)));
        assert_eq!(inflight_logs(6, 10), pe.inflight);
        assert_eq!(Some(&log_id(6)), pe.matching());
        assert_eq!(20, pe.searching_end);

        pe.new_updater(&engine_config).update_matching(Some(log_id(10)), Some(InflightId::new(0)));
        assert_eq!(Inflight::None, pe.inflight);
        assert_eq!(Some(&log_id(10)), pe.matching());
        assert_eq!(20, pe.searching_end);
    }

    // `searching_end` should be updated
    {
        let mut pe = ProgressEntry::<UTConfig>::empty(0, StreamId::new(0), 20);
        pe.matching = Some(log_id(6));
        pe.inflight = inflight_logs(5, 20);

        pe.new_updater(&engine_config).update_matching(Some(log_id(20)), Some(InflightId::new(0)));
        assert_eq!(21, pe.searching_end);
    }

    Ok(())
}

#[test]
fn test_update_conflicting() -> anyhow::Result<()> {
    let mut pe = ProgressEntry::<UTConfig>::empty(0, StreamId::new(0), 20);
    pe.matching = Some(log_id(3));
    pe.inflight = inflight_logs(5, 10);

    let engine_config = EngineConfig::new_default(1);
    pe.new_updater(&engine_config).update_conflicting(5, Some(InflightId::new(0)));

    assert_eq!(Inflight::None, pe.inflight);
    assert_eq!(Some(&log_id(3)), pe.matching());
    assert_eq!(5, pe.searching_end);

    Ok(())
}

fn new_raft_state(purge_upto: u64, snap_last: u64, last: u64) -> RaftState<UTConfig> {
    let mut st = RaftState::new(0);

    // TODO: setup log_ids

    st.purge_upto = Some(log_id(purge_upto));
    st.snapshot_meta = SnapshotMetaOf::<UTConfig> {
        last_log_id: Some(log_id(snap_last)),
        last_membership: StoredMembershipOf::<UTConfig>::default(),
        snapshot_id: "".to_string(),
    };

    st.log_ids = LogIdList::new(None, [log_id(purge_upto - 1), log_id(last)]);

    st
}

#[test]
fn test_next_send_inflight_busy() -> anyhow::Result<()> {
    // There is already inflight data, return it in an Error
    let mut pe = ProgressEntry::<UTConfig>::empty(0, StreamId::new(0), 20);
    pe.inflight = inflight_logs(10, 11);

    let res = pe.next_send(&mut new_raft_state(6, 10, 20), 100);
    assert_eq!(Err(&inflight_logs(10, 11)), res);

    Ok(())
}

#[test]
fn test_next_send_snapshot_matching_purged() -> anyhow::Result<()> {
    //    matching,end
    //    4,5
    //    v
    // -----+------+-----+--->
    //      purged snap  last
    //      6      10    20
    //
    // Pipeline regime (matching.next_index() == searching_end): the matching point is
    // known to be 4, but the logs the follower needs next (5..=6) are purged.
    // Snapshot condition 1.
    let mut pe = ProgressEntry::<UTConfig>::empty(0, StreamId::new(0), 5);
    pe.matching = Some(log_id(4));

    let res = pe.next_send(&mut new_raft_state(6, 10, 20), 100);
    assert_eq!(Ok(&Inflight::snapshot(InflightId::new(1))), res);

    Ok(())
}

#[test]
fn test_next_send_snapshot_search_range_purged() -> anyhow::Result<()> {
    //    matching,end
    //    4 6
    //    v-v
    // -----+------+-----+--->
    //      purged snap  last
    //      6      10    20
    //
    // Probing regime: every candidate matching position (4, 5) lies strictly below
    // the purge boundary (6). Snapshot condition 1.
    let mut pe = ProgressEntry::<UTConfig>::empty(0, StreamId::new(0), 6);
    pe.matching = Some(log_id(4));

    let res = pe.next_send(&mut new_raft_state(6, 10, 20), 100);
    assert_eq!(Ok(&Inflight::snapshot(InflightId::new(1))), res);

    Ok(())
}

#[test]
fn test_next_send_probe_at_purge_boundary() -> anyhow::Result<()> {
    //    matching,end
    //    4  7
    //    v--v
    // -----+------+-----+--->
    //      purged snap  last
    //      6      10    20
    //
    // searching_end == purge_upto_next: the purge boundary (6) is itself still a
    // candidate matching position, and its log id is known as the snapshot's last
    // log id. Probe with prev at the boundary instead of falling back to a snapshot.
    let mut pe = ProgressEntry::<UTConfig>::empty(0, StreamId::new(0), 7);
    pe.matching = Some(log_id(4));

    let res = pe.next_send(&mut new_raft_state(6, 10, 20), 100);
    assert_eq!(Ok(&inflight_logs(6, 20).with_id(1)), res);

    Ok(())
}

#[test]
fn test_next_send_probe_clamped_to_purge_boundary() -> anyhow::Result<()> {
    //    matching,end
    //    4              20
    //    v--------------v
    // -----+------+-----+--->
    //      purged snap  last
    //      6      10    20
    //
    // The binary-search midpoint, calc_mid(5, 20) = 5, is below the purge boundary;
    // the probe start is clamped up to the boundary: prev = log id 6.
    let mut pe = ProgressEntry::<UTConfig>::empty(0, StreamId::new(0), 20);
    pe.matching = Some(log_id(4));

    let res = pe.next_send(&mut new_raft_state(6, 10, 20), 100);
    assert_eq!(Ok(&inflight_logs(6, 20).with_id(1)), res);

    Ok(())
}

#[test]
fn test_next_send_pipeline_at_purge_boundary() -> anyhow::Result<()> {
    //      matching,end
    //      6,7
    //      v
    // -----+------+-----+--->
    //      purged snap  last
    //      6      10    20
    //
    // matching.next_index() == searching_end, enter pipeline mode
    // (the matching point is exactly the purge boundary: nothing needed is purged)
    let mut pe = ProgressEntry::<UTConfig>::empty(0, StreamId::new(0), 7);
    pe.matching = Some(log_id(6));

    let res = pe.next_send(&mut new_raft_state(6, 10, 20), 100);
    assert_eq!(
        Ok(&Inflight::logs_since(Some(log_id(6)), InflightId::new(1))),
        res,
        "pipeline mode: matching.next == searching_end"
    );

    Ok(())
}

#[test]
fn test_next_send_probe_from_matching_next_short_range() -> anyhow::Result<()> {
    //      matching,end
    //      6, 8
    //      v--v
    // -----+------+-----+--->
    //      purged snap  last
    //      6      10    20
    //
    // Probing: the search range [7, 8) is too narrow for a non-zero binary-search
    // offset, so the probe starts right after matching: prev = log id 6.
    let mut pe = ProgressEntry::<UTConfig>::empty(0, StreamId::new(0), 8);
    pe.matching = Some(log_id(6));

    let res = pe.next_send(&mut new_raft_state(6, 10, 20), 100);
    assert_eq!(Ok(&inflight_logs(6, 20).with_id(1)), res);

    Ok(())
}

#[test]
fn test_next_send_probe_from_matching_next_wide_range() -> anyhow::Result<()> {
    //      matching,end
    //      6,           20
    //      v------------v
    // -----+------+-----+--->
    //      purged snap  last
    //      6      10    20
    //
    // Probing: calc_mid(7, 20) = 7 (offset (20-7)/16*8 = 0), so the probe still
    // starts right after matching: prev = log id 6.
    let mut pe = ProgressEntry::<UTConfig>::empty(0, StreamId::new(0), 20);
    pe.matching = Some(log_id(6));

    let res = pe.next_send(&mut new_raft_state(6, 10, 20), 100);
    assert_eq!(Ok(&inflight_logs(6, 20).with_id(1)), res);

    Ok(())
}

#[test]
fn test_next_send_probe_from_matching_next_above_boundary() -> anyhow::Result<()> {
    //       matching,end
    //       7,          20
    //       v-----------v
    // -----+------+-----+--->
    //      purged snap  last
    //      6      10    20
    //
    // Probing with matching above the purge boundary: no clamping is involved;
    // the probe starts right after matching: prev = log id 7.
    let mut pe = ProgressEntry::<UTConfig>::empty(0, StreamId::new(0), 20);
    pe.matching = Some(log_id(7));

    let res = pe.next_send(&mut new_raft_state(6, 10, 20), 100);
    assert_eq!(Ok(&inflight_logs(7, 20).with_id(1)), res);

    Ok(())
}

#[test]
fn test_next_send_pipeline_above_purge_boundary() -> anyhow::Result<()> {
    //          matching,end
    //          7,8
    //          v
    // -----+------+-----+--->
    //      purged snap  last
    //      6      10    20
    //
    // matching.next_index() == searching_end, enter pipeline mode
    let mut pe = ProgressEntry::<UTConfig>::empty(0, StreamId::new(0), 8);
    pe.matching = Some(log_id(7));

    let res = pe.next_send(&mut new_raft_state(6, 10, 20), 100);
    assert_eq!(
        Ok(&Inflight::logs_since(Some(log_id(7)), InflightId::new(1))),
        res,
        "pipeline mode: matching.next == searching_end"
    );

    Ok(())
}

#[test]
fn test_next_send_pipeline_caught_up() -> anyhow::Result<()> {
    //                   matching,end
    //                   20,21
    //                   v
    // -----+------+-----+--->
    //      purged snap  last
    //      6      10    20
    //
    // matching.next_index() == searching_end, enter pipeline mode
    // (even though follower is fully caught up: an empty open-ended stream is valid)
    let mut pe = ProgressEntry::<UTConfig>::empty(0, StreamId::new(0), 21);
    pe.matching = Some(log_id(20));

    let res = pe.next_send(&mut new_raft_state(6, 10, 20), 100);
    assert_eq!(
        Ok(&Inflight::logs_since(Some(log_id(20)), InflightId::new(1))),
        res,
        "pipeline mode: fully caught up"
    );

    Ok(())
}

#[test]
fn test_next_send_probe_capped_by_max_entries() -> anyhow::Result<()> {
    //       matching,end
    //       7,          20
    //       v-----------v
    // -----+------+-----+--->
    //      purged snap  last
    //      6      10    20
    //
    // max_entries = 5 caps the probe range: entries 8..=12 are sent.
    let mut pe = ProgressEntry::<UTConfig>::empty(0, StreamId::new(0), 20);
    pe.matching = Some(log_id(7));

    let res = pe.next_send(&mut new_raft_state(6, 10, 20), 5);
    assert_eq!(Ok(&inflight_logs(7, 12).with_id(1)), res);

    Ok(())
}

#[test]
fn test_next_send_fully_purged_unknown_follower() -> anyhow::Result<()> {
    //                     end
    //                     21
    //                     v
    // ----------------+--->
    //     purged/snap/last
    //                20
    //
    // matching = None; probing, but the leader log is fully purged
    // (purge_upto == last_log == 20): there is no entry for a probe to carry.
    // Snapshot condition 2.
    let mut pe = ProgressEntry::<UTConfig>::empty(0, StreamId::new(0), 21);

    let res = pe.next_send(&mut new_raft_state(20, 20, 20), 100);
    assert_eq!(Ok(&Inflight::snapshot(InflightId::new(1))), res);

    Ok(())
}

#[test]
fn test_next_send_not_pipeline_when_gap_exists() -> anyhow::Result<()> {
    //       matching  searching_end
    //       7         20
    //       v---------v
    // -----+------+-----+--->
    //      purged snap  last
    //      6      10    20
    //
    // NOT pipeline mode: matching.next_index() != searching_end — the exact matching
    // point is not determined yet, so this is a probing Logs, not a LogsSince stream.
    let mut pe = ProgressEntry::<UTConfig>::empty(0, StreamId::new(0), 20);
    pe.matching = Some(log_id(7));

    let res = pe.next_send(&mut new_raft_state(6, 10, 20), 100);
    assert_eq!(Ok(&inflight_logs(7, 20).with_id(1)), res, "not pipeline: gap exists");

    Ok(())
}

#[test]
fn test_next_send_not_pipeline_without_matching() -> anyhow::Result<()> {
    //  matching=None  searching_end
    //                 20
    //                 v
    // -----+------+-----+--->
    //      purged snap  last
    //      6      10    20
    //
    // NOT pipeline mode: no matching yet.
    // calc_mid(0, 20) = 0 + 20/16*8 = 8, so the probe starts at 8: prev = log id 7.
    let mut pe = ProgressEntry::<UTConfig>::empty(0, StreamId::new(0), 20);

    let res = pe.next_send(&mut new_raft_state(6, 10, 20), 100);
    assert_eq!(Ok(&inflight_logs(7, 20).with_id(1)), res, "not pipeline: no matching");

    Ok(())
}

#[test]
fn test_next_send_probe_from_mid_of_search_range() -> anyhow::Result<()> {
    //    matching,end
    //    4               21
    //    v---------------v
    // -----+------+-----+--->
    //      purged snap  last
    //      6      10    20
    //
    // The search range [5, 21) is wide enough for a non-zero binary-search offset:
    // calc_mid(5, 21) = 5 + 16/16*8 = 13. The probe starts from the midpoint,
    // not right after matching: prev = log id 12.
    let mut pe = ProgressEntry::<UTConfig>::empty(0, StreamId::new(0), 21);
    pe.matching = Some(log_id(4));

    let res = pe.next_send(&mut new_raft_state(6, 10, 20), 100);
    assert_eq!(Ok(&inflight_logs(12, 20).with_id(1)), res);

    Ok(())
}

#[test]
fn test_next_send_snapshot_fully_purged_matching_behind() -> anyhow::Result<()> {
    //          matching   end
    //          15         21
    //          v----------v
    // ----------------+--->
    //     purged/snap/last
    //                20
    //
    // Fully purged leader with a known lower bound (matching = 15): still probing
    // (matching.next_index() < searching_end) and still no entry to probe with.
    // Snapshot condition 2.
    let mut pe = ProgressEntry::<UTConfig>::empty(0, StreamId::new(0), 21);
    pe.matching = Some(log_id(15));

    let res = pe.next_send(&mut new_raft_state(20, 20, 20), 100);
    assert_eq!(Ok(&Inflight::snapshot(InflightId::new(1))), res);

    Ok(())
}

#[test]
fn test_next_send_pipeline_fully_purged_caught_up() -> anyhow::Result<()> {
    //             matching,end
    //             20,21
    //             v
    // ----------------+--->
    //     purged/snap/last
    //                20
    //
    // Fully purged leader with a fully caught-up follower: the matching point is
    // known, so this is pipeline, not probing — snapshot condition 2 must not fire.
    // An empty open-ended stream is valid.
    let mut pe = ProgressEntry::<UTConfig>::empty(0, StreamId::new(0), 21);
    pe.matching = Some(log_id(20));

    let res = pe.next_send(&mut new_raft_state(20, 20, 20), 100);
    assert_eq!(Ok(&Inflight::logs_since(Some(log_id(20)), InflightId::new(1))), res);

    Ok(())
}