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
//! RuntimeConfigHandle is an interface to change Raft runtime config.
use std::sync::atomic::Ordering;
use crate::RaftTypeConfig;
use crate::async_runtime::watch::WatchSender;
use crate::raft::RaftInner;
/// RuntimeConfigHandle is an interface to update runtime config.
///
/// These configs are mainly designed for testing purposes and special use cases.
/// Usually you don't need to change runtime config.
pub struct RuntimeConfigHandle<'r, C>
where C: RaftTypeConfig
{
raft_inner: &'r RaftInner<C>,
}
impl<'r, C> RuntimeConfigHandle<'r, C>
where C: RaftTypeConfig
{
pub(in crate::raft) fn new(raft_inner: &'r RaftInner<C>) -> Self {
Self { raft_inner }
}
/// Enable or disable raft internal ticker.
///
/// Disabling tick will disable election and heartbeat. While disabled, the internal ticker
/// parks and causes no wakeups; re-enabling it resumes ticking, with the first tick at most
/// one tick period later.
pub fn tick(&self, enabled: bool) {
let enable_tick = self.raft_inner.runtime_config.enable_tick.lock().unwrap();
enable_tick.send_if_different(enabled);
}
/// Enable or disable heartbeat messages when a leader has no more log to replicate.
///
/// Note that the follower's leader-lease will not be renewed if it does receive messages from
/// the leader, and it will start election (if `Self::elect()` is enabled) when the lease timed
/// out.
pub fn heartbeat(&self, enabled: bool) {
self.raft_inner.runtime_config.enable_heartbeat.store(enabled, Ordering::Relaxed);
}
/// Enable or disable election for a follower when its leader lease timed out.
pub fn elect(&self, enabled: bool) {
self.raft_inner.runtime_config.enable_elect.store(enabled, Ordering::Relaxed);
}
/// Enable or disable the Pre-Vote round that precedes a real election.
///
/// When enabled, a follower asks peers whether they would grant it a vote before incrementing
/// its term, avoiding term inflation by a node that cannot currently win. See
/// [`Config::enable_pre_vote`](crate::Config::enable_pre_vote).
pub fn pre_vote(&self, enabled: bool) {
self.raft_inner.runtime_config.enable_pre_vote.store(enabled, Ordering::Relaxed);
}
}