Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support configuring election timeout range #63

Merged
merged 6 commits into from
May 20, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
address comment
  • Loading branch information
BusyJay committed May 20, 2018
commit 7b781aa96a85fb7cafff10c69cd962537fccf004
21 changes: 14 additions & 7 deletions src/raft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 127,10 @@ pub struct Config {
/// The range of election timeout. In some cases, we hope some nodes has less possibility
/// to become leader. This configuration ensures that the randomized election_timeout
/// will always be suit in [min_election_tick, max_election_tick).
/// If it is None, then election_tick will be chosen.
pub min_election_tick: Option<usize>,
/// If it is None, then 2 * election_tick will be chosen.
pub max_election_tick: Option<usize>,
/// If it is 0, then election_tick will be chosen.
pub min_election_tick: usize,
/// If it is 0, then 2 * election_tick will be chosen.
pub max_election_tick: usize,

/// read_only_option specifies how the read only request is processed.
pub read_only_option: ReadOnlyOption,
Expand All @@ -147,13 147,20 @@ pub struct Config {
impl Config {
#[inline]
pub fn min_election_tick(&self) -> usize {
self.min_election_tick.unwrap_or(self.election_tick)
if self.min_election_tick == 0 {
self.election_tick
} else {
self.min_election_tick
}
}

#[inline]
pub fn max_election_tick(&self) -> usize {
self.max_election_tick
.unwrap_or_else(|| 2 * self.election_tick)
if self.max_election_tick == 0 {
2 * self.election_tick
} else {
self.max_election_tick
}
}

pub fn validate(&self) -> Result<()> {
Expand Down
10 changes: 5 additions & 5 deletions tests/cases/test_raft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4098,19 4098,19 @@ fn test_election_tick_range() {
);
}

cfg.min_election_tick = Some(cfg.election_tick);
cfg.min_election_tick = cfg.election_tick;
cfg.validate().unwrap();

// Too small election tick.
cfg.min_election_tick = Some(cfg.election_tick - 1);
cfg.min_election_tick = cfg.election_tick - 1;
cfg.validate().unwrap_err();

// max_election_tick should be larger than min_election_tick
cfg.min_election_tick = Some(cfg.election_tick);
cfg.max_election_tick = Some(cfg.election_tick);
cfg.min_election_tick = cfg.election_tick;
cfg.max_election_tick = cfg.election_tick;
cfg.validate().unwrap_err();

cfg.max_election_tick = Some(cfg.election_tick 1);
cfg.max_election_tick = cfg.election_tick 1;
raft = Raft::new(&cfg, new_storage());
for _ in 0..100 {
raft.reset_randomized_election_timeout();
Expand Down