openraft 0.10.2

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
//! Tests for [`Config`]'s clap-derived CLI parsing surface.
//!
//! The whole module is gated behind `feature = "clap"` in [`super`], so
//! individual tests do not need to repeat the gate.

use core::time::Duration;

use super::error::ConfigError;
use super::parser::parse_bytes_with_unit;
use crate::Config;
use crate::SnapshotPolicy;
use crate::StepDownPolicy;

#[test]
fn test_parse_bytes_with_unit() -> anyhow::Result<()> {
    assert_eq!(204, parse_bytes_with_unit("204")?);
    assert_eq!(3 * 1024 * 1024, parse_bytes_with_unit("3MiB")?);
    assert_eq!(5_300, parse_bytes_with_unit("5.3 KB")?);
    assert_eq!(
        Err(ConfigError::InvalidNumber {
            invalid: "invalid".to_string(),
            reason: "invalid digit found in string".to_string(),
        }),
        parse_bytes_with_unit("invalid")
    );

    Ok(())
}

#[test]
fn test_build() -> anyhow::Result<()> {
    let config = Config::build(&[
        "foo",
        "--cluster-name=bar",
        "--election-timeout-min=10",
        "--election-timeout-max=20",
        "--heartbeat-interval=5",
        "--install-snapshot-timeout=231",
        "--max-payload-entries=201",
        "--snapshot-policy=since_last:202",
        "--replication-lag-threshold=203",
        "--snapshot-max-chunk-size=204",
        "--max-in-snapshot-log-to-keep=205",
        "--purge-batch-size=207",
        "--api-channel-size=208",
        "--notification-channel-size=209",
    ])?;

    assert_eq!("bar", config.cluster_name);
    assert_eq!(10, config.election_timeout_min);
    assert_eq!(20, config.election_timeout_max);
    assert_eq!(5, config.heartbeat_interval);

    assert_eq!(231, config.install_snapshot_timeout);
    assert_eq!(201, config.max_payload_entries);
    assert_eq!(SnapshotPolicy::LogsSinceLast(202), config.snapshot_policy);
    assert_eq!(203, config.replication_lag_threshold);
    assert_eq!(204, config.snapshot_max_chunk_size);
    assert_eq!(205, config.max_in_snapshot_log_to_keep);
    assert_eq!(207, config.purge_batch_size);
    assert_eq!(Some(208), config.api_channel_size);
    assert_eq!(Some(209), config.notification_channel_size);

    // Test config methods. 231 differs from the default of 200, so a builder that ignored the
    // option would fail here.
    let timeout = config.install_snapshot_timeout();
    let expected_timeout = Duration::from_millis(231);
    assert_eq!(expected_timeout, timeout);
    Ok(())
}

#[test]
fn test_config_snapshot_policy() -> anyhow::Result<()> {
    let config = Config::build(&["foo", "--snapshot-policy=never"])?;
    assert_eq!(SnapshotPolicy::Never, config.snapshot_policy);

    let config = Config::build(&["foo", "--snapshot-policy=since_last:3"])?;
    assert_eq!(SnapshotPolicy::LogsSinceLast(3), config.snapshot_policy);

    let zero = Config::build(&["foo", "--snapshot-policy=since_last:0"]);
    assert_eq!(Err(ConfigError::SnapshotLogsSinceLastIs0), zero.map(|_| ()));

    let args = ["foo", "--snapshot-policy=bar:3"];
    let error = Config::build(&args).unwrap_err();
    let ConfigError::ParseError { source, args: returned } = error else {
        panic!("expected ConfigError::ParseError, got: {error:?}");
    };
    assert_eq!(args, returned.as_slice());
    let reason = source.to_string();
    assert!(reason.contains("snapshot policy string is invalid"), "{reason}");

    Ok(())
}

#[test]
fn test_config_removed_leader_step_down() -> anyhow::Result<()> {
    let config = Config::build(&["foo"])?;
    assert_eq!(StepDownPolicy::After(150), config.removed_leader_step_down);

    for never in ["never", "no", "none", "off", "false", "Never", "NO", "OFF"] {
        let config = Config::build(&["foo", &format!("--removed-leader-step-down={}", never)])?;
        assert_eq!(
            StepDownPolicy::Never,
            config.removed_leader_step_down,
            "literal: {}",
            never
        );
    }

    let config = Config::build(&["foo", "--removed-leader-step-down=500"])?;
    assert_eq!(StepDownPolicy::After(500), config.removed_leader_step_down);

    let args = ["foo", "--removed-leader-step-down=bar"];
    let error = Config::build(&args).unwrap_err();
    let ConfigError::ParseError { source, args: returned } = error else {
        panic!("expected ConfigError::ParseError, got: {error:?}");
    };
    assert_eq!(args, returned.as_slice());
    let reason = source.to_string();
    assert!(reason.contains("invalid digit found in string"), "{reason}");

    Ok(())
}

#[test]
fn test_config_quorum_loss() -> anyhow::Result<()> {
    let config = Config::build(&["foo"])?;
    assert_eq!(None, config.quorum_loss_probe_interval);

    let config = Config::build(&["foo", "--quorum-loss-probe-interval=600"])?;
    assert_eq!(Some(600), config.quorum_loss_probe_interval);

    let result = Config::build(&["foo", "--quorum-loss-probe-interval=599"]);
    let error = result.unwrap_err();
    assert_eq!(
        ConfigError::QuorumLossProbeIntervalTooSmall {
            election_timeout_max: 300,
            probe_interval: 599,
        },
        error
    );

    Ok(())
}

#[test]
fn test_config_enable_tick() -> anyhow::Result<()> {
    let config = Config::build(&["foo", "--enable-tick=false"])?;
    assert_eq!(false, config.enable_tick);

    let config = Config::build(&["foo", "--enable-tick=true"])?;
    assert_eq!(true, config.enable_tick);

    let config = Config::build(&["foo", "--enable-tick"])?;
    assert_eq!(true, config.enable_tick);

    let config = Config::build(&["foo"])?;
    assert_eq!(true, config.enable_tick);

    Ok(())
}

#[test]
fn test_config_broadcast_submitted_on_append() -> anyhow::Result<()> {
    let config = Config::build(&["foo"])?;
    assert_eq!(None, config.broadcast_submitted_on_append);

    let config = Config::build(&["foo", "--broadcast-submitted-on-append"])?;
    assert_eq!(Some(true), config.broadcast_submitted_on_append);

    let config = Config::build(&["foo", "--broadcast-submitted-on-append=false"])?;
    assert_eq!(Some(false), config.broadcast_submitted_on_append);

    Ok(())
}

#[test]
fn test_config_enable_heartbeat() -> anyhow::Result<()> {
    let config = Config::build(&["foo", "--enable-heartbeat=false"])?;
    assert_eq!(false, config.enable_heartbeat);

    let config = Config::build(&["foo", "--enable-heartbeat=true"])?;
    assert_eq!(true, config.enable_heartbeat);

    let config = Config::build(&["foo", "--enable-heartbeat"])?;
    assert_eq!(true, config.enable_heartbeat);

    let config = Config::build(&["foo"])?;
    assert_eq!(true, config.enable_heartbeat);

    Ok(())
}

#[test]
fn test_config_enable_elect() -> anyhow::Result<()> {
    let config = Config::build(&["foo", "--enable-elect=false"])?;
    assert_eq!(false, config.enable_elect);

    let config = Config::build(&["foo", "--enable-elect=true"])?;
    assert_eq!(true, config.enable_elect);

    let config = Config::build(&["foo", "--enable-elect"])?;
    assert_eq!(true, config.enable_elect);

    let config = Config::build(&["foo"])?;
    assert_eq!(true, config.enable_elect);

    Ok(())
}

#[test]
fn test_config_enable_pre_vote() -> anyhow::Result<()> {
    let config = Config::build(&["foo", "--enable-pre-vote=false"])?;
    assert_eq!(Some(false), config.enable_pre_vote);
    assert_eq!(false, config.get_enable_pre_vote());

    let config = Config::build(&["foo", "--enable-pre-vote=true"])?;
    assert_eq!(Some(true), config.enable_pre_vote);
    assert_eq!(true, config.get_enable_pre_vote());

    let config = Config::build(&["foo", "--enable-pre-vote"])?;
    assert_eq!(Some(true), config.enable_pre_vote);
    assert_eq!(true, config.get_enable_pre_vote());

    // Omitted: the field defaults to `None`, which evaluates to disabled (`false`).
    let config = Config::build(&["foo"])?;
    assert_eq!(None, config.enable_pre_vote);
    assert_eq!(false, config.get_enable_pre_vote());

    Ok(())
}

#[test]
fn test_config_reset_backoff_on_transfer_leader() -> anyhow::Result<()> {
    let config = Config::build(&["foo"])?;
    assert_eq!(None, config.reset_backoff_on_transfer_leader);
    let enabled = config.get_reset_backoff_on_transfer_leader();
    assert!(enabled);

    let config = Config::build(&["foo", "--reset-backoff-on-transfer-leader=false"])?;
    assert_eq!(Some(false), config.reset_backoff_on_transfer_leader);
    let enabled = config.get_reset_backoff_on_transfer_leader();
    assert!(!enabled);

    let config = Config::build(&["foo", "--reset-backoff-on-transfer-leader=true"])?;
    assert_eq!(Some(true), config.reset_backoff_on_transfer_leader);
    let enabled = config.get_reset_backoff_on_transfer_leader();
    assert!(enabled);

    let config = Config::build(&["foo", "--reset-backoff-on-transfer-leader"])?;
    assert_eq!(Some(true), config.reset_backoff_on_transfer_leader);

    Ok(())
}

#[test]
fn test_config_allow_log_reversion() -> anyhow::Result<()> {
    let config = Config::build(&["foo", "--allow-log-reversion=false"])?;
    assert_eq!(Some(false), config.allow_log_reversion);

    let config = Config::build(&["foo", "--allow-log-reversion=true"])?;
    assert_eq!(Some(true), config.allow_log_reversion);

    let config = Config::build(&["foo", "--allow-log-reversion"])?;
    assert_eq!(Some(true), config.allow_log_reversion);

    let mut config = Config::build(&["foo"])?;
    assert_eq!(None, config.allow_log_reversion);

    // test allow_log_reversion method

    config.allow_log_reversion = None;
    assert_eq!(false, config.get_allow_log_reversion());

    config.allow_log_reversion = Some(true);
    assert_eq!(true, config.get_allow_log_reversion());

    config.allow_log_reversion = Some(false);
    assert_eq!(false, config.get_allow_log_reversion());

    Ok(())
}

#[test]
fn test_config_enable_leader_restore() -> anyhow::Result<()> {
    let config = Config::build(&["foo", "--enable-leader-restore=false"])?;
    assert_eq!(Some(false), config.enable_leader_restore);

    let config = Config::build(&["foo", "--enable-leader-restore=true"])?;
    assert_eq!(Some(true), config.enable_leader_restore);

    let config = Config::build(&["foo", "--enable-leader-restore"])?;
    assert_eq!(Some(true), config.enable_leader_restore);

    let mut config = Config::build(&["foo"])?;
    assert_eq!(None, config.enable_leader_restore);

    // test enable_leader_restore method

    config.enable_leader_restore = None;
    assert_eq!(true, config.enable_leader_restore());

    config.enable_leader_restore = Some(true);
    assert_eq!(true, config.enable_leader_restore());

    config.enable_leader_restore = Some(false);
    assert_eq!(false, config.enable_leader_restore());

    Ok(())
}

#[test]
fn test_config_api_channel_size() -> anyhow::Result<()> {
    // Test default value
    let config = Config::build(&["foo"])?;
    assert_eq!(Some(65536), config.api_channel_size);
    assert_eq!(65536, config.api_channel_size());

    // Test custom value
    let config = Config::build(&["foo", "--api-channel-size=10000"])?;
    assert_eq!(Some(10000), config.api_channel_size);
    assert_eq!(10000, config.api_channel_size());

    // Test api_channel_size() method with None
    let mut config = Config::build(&["foo"])?;
    config.api_channel_size = None;
    assert_eq!(65536, config.api_channel_size());

    // Test api_channel_size() method with Some
    config.api_channel_size = Some(50000);
    assert_eq!(50000, config.api_channel_size());

    Ok(())
}

#[test]
fn test_config_notification_channel_size() -> anyhow::Result<()> {
    // Test default value
    let config = Config::build(&["foo"])?;
    assert_eq!(Some(65536), config.notification_channel_size);
    assert_eq!(65536, config.notification_channel_size());

    // Test custom value
    let config = Config::build(&["foo", "--notification-channel-size=2048"])?;
    assert_eq!(Some(2048), config.notification_channel_size);
    assert_eq!(2048, config.notification_channel_size());

    // Test notification_channel_size() method with None
    let mut config = Config::build(&["foo"])?;
    config.notification_channel_size = None;
    assert_eq!(65536, config.notification_channel_size());

    // Test notification_channel_size() method with Some
    config.notification_channel_size = Some(512);
    assert_eq!(512, config.notification_channel_size());

    Ok(())
}

#[test]
fn test_config_state_machine_buffer() -> anyhow::Result<()> {
    let config = Config::build(&["foo"])?;
    assert_eq!(None, config.enable_unbounded_state_machine_buffer);
    let buffered = config.enable_unbounded_state_machine_buffer();
    assert!(!buffered);
    let config = Config::build(&["foo", "--enable-unbounded-state-machine-buffer"])?;
    assert_eq!(Some(true), config.enable_unbounded_state_machine_buffer);
    let buffered = config.enable_unbounded_state_machine_buffer();
    assert!(buffered);
    let config = Config::build(&["foo", "--enable-unbounded-state-machine-buffer=false"])?;
    assert_eq!(Some(false), config.enable_unbounded_state_machine_buffer);
    let buffered = config.enable_unbounded_state_machine_buffer();
    assert!(!buffered);
    Ok(())
}

#[test]
fn test_config_relaxed_read() -> anyhow::Result<()> {
    let cases = [
        (vec!["foo"], None),
        (vec!["foo", "--enable-relaxed-read"], Some(true)),
        (vec!["foo", "--enable-relaxed-read=true"], Some(true)),
        (vec!["foo", "--enable-relaxed-read=false"], Some(false)),
    ];
    for (args, expected) in cases {
        let config = Config::build(&args)?;
        assert_eq!(expected, config.enable_relaxed_read);
        let enabled = config.get_enable_relaxed_read();
        assert_eq!(expected.unwrap_or(false), enabled);
    }
    Ok(())
}

#[test]
fn test_config_local_lease_read() -> anyhow::Result<()> {
    let cases = [
        (vec!["foo"], None),
        (vec!["foo", "--enable-local-lease-read"], Some(true)),
        (vec!["foo", "--enable-local-lease-read=true"], Some(true)),
        (vec!["foo", "--enable-local-lease-read=false"], Some(false)),
    ];
    for (args, expected) in cases {
        let config = Config::build(&args)?;
        assert_eq!(expected, config.enable_local_lease_read);
        let enabled = config.get_enable_local_lease_read();
        let expected_enabled = expected.unwrap_or(false);
        assert_eq!(expected_enabled, enabled);
    }
    Ok(())
}

#[test]
fn test_config_lifecycle_latency_capacity() -> anyhow::Result<()> {
    // Default: None (no clap default_value)
    let config = Config::build(&["foo"])?;
    assert_eq!(None, config.log_stage_capacity);
    assert_eq!(1024, config.log_stage_capacity());

    // Custom value via CLI
    let config = Config::build(&["foo", "--log-stage-capacity=512"])?;
    assert_eq!(Some(512), config.log_stage_capacity);
    assert_eq!(512, config.log_stage_capacity());

    // Method with None returns default
    let mut config = Config::build(&["foo"])?;
    config.log_stage_capacity = None;
    assert_eq!(1024, config.log_stage_capacity());

    // Method with Some returns the value
    config.log_stage_capacity = Some(2048);
    assert_eq!(2048, config.log_stage_capacity());

    // Zero is rejected
    let result = Config::build(&["foo", "--log-stage-capacity=0"]);
    assert_eq!(ConfigError::LogStageCapacityIs0, result.unwrap_err());

    Ok(())
}

#[test]
fn test_config_snapshot_max_chunk_size() -> anyhow::Result<()> {
    let config = Config::build(&["foo", "--snapshot-max-chunk-size=1"])?;
    assert_eq!(1, config.snapshot_max_chunk_size);

    let zero = Config::build(&["foo", "--snapshot-max-chunk-size=0"]);
    assert_eq!(Err(ConfigError::SnapshotMaxChunkSizeIs0), zero.map(|_| ()));

    let max = u64::try_from(isize::MAX)?;
    let too_large = format!("--snapshot-max-chunk-size={}", max + 1);
    let too_large = Config::build(&["foo", &too_large]);
    let expected = ConfigError::TooLarge {
        field: "snapshot_max_chunk_size",
        value: max + 1,
        max,
    };
    assert_eq!(Err(expected), too_large.map(|_| ()));

    Ok(())
}

#[test]
fn test_config_heartbeat_interval() -> anyhow::Result<()> {
    let config = Config::build(&["foo", "--heartbeat-interval=1"])?;
    assert_eq!(1, config.heartbeat_interval);

    let zero = Config::build(&["foo", "--heartbeat-interval=0"]);
    assert_eq!(Err(ConfigError::HeartbeatIntervalIs0), zero.map(|_| ()));

    Ok(())
}