-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #889 from drmingdrmer/44-raft-ctl
Change: move runtime config API to dedicated RuntimeConfigHandle
- Loading branch information
Showing
6 changed files
with
79 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
//! RuntimeConfigHandle is an interface to change Raft runtime config. | ||
|
||
use std::sync::atomic::Ordering; | ||
|
||
use crate::raft::RaftInner; | ||
use crate::storage::RaftLogStorage; | ||
use crate::RaftNetworkFactory; | ||
use crate::RaftTypeConfig; | ||
|
||
/// RuntimeConfigHandle is an interface to update runtime config. | ||
/// | ||
/// These config are mainly designed for testing purpose and special use cases. | ||
/// Usually you don't need to change runtime config. | ||
pub struct RuntimeConfigHandle<'r, C, N, LS> | ||
where | ||
C: RaftTypeConfig, | ||
N: RaftNetworkFactory<C>, | ||
LS: RaftLogStorage<C>, | ||
{ | ||
raft_inner: &'r RaftInner<C, N, LS>, | ||
} | ||
|
||
impl<'r, C, N, LS> RuntimeConfigHandle<'r, C, N, LS> | ||
where | ||
C: RaftTypeConfig, | ||
N: RaftNetworkFactory<C>, | ||
LS: RaftLogStorage<C>, | ||
{ | ||
pub(in crate::raft) fn new(raft_inner: &'r RaftInner<C, N, LS>) -> Self { | ||
Self { raft_inner } | ||
} | ||
|
||
/// Enable or disable raft internal ticker. | ||
/// | ||
/// Disabling tick will disable election and heartbeat. | ||
pub fn tick(&self, enabled: bool) { | ||
self.raft_inner.tick_handle.enable(enabled); | ||
} | ||
|
||
/// Enable or disable heartbeat message when a leader has no more log to replicate. | ||
/// | ||
/// Note that the follower's leader-lease will not be renewed if it does receive message 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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters