-
Notifications
You must be signed in to change notification settings - Fork 406
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
raft: Avoid scanning raft log in become_leader #15
Merged
siddontang
merged 6 commits into
tikv:master
from
csmoe:avoid-scanning-raft-log-in-become_leader
Feb 2, 2018
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3f5da1c
raft: Avoid scanning raft log in become_leader. (#11)
csmoe ea456a7
fix
csmoe c33cf9d
remove unnecessary check
csmoe bb6f851
test whether pending_conf_index <= applied when bcasting is skipped
csmoe 0f0b42e
fix test_should_bcast_commit
csmoe 454dbee
Merge branch 'master' into avoid-scanning-raft-log-in-become_leader
siddontang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -203,8 +203,13 @@ pub struct Raft<T: Storage> { | |
/// Follow the procedure defined in raft thesis 3.10. | ||
pub lead_transferee: Option<u64>, | ||
|
||
/// New configuration is ignored if there exists unapplied configuration. | ||
pub pending_conf: bool, | ||
/// Only one conf change may be pending (in the log, but not yet | ||
/// applied) at a time. This is enforced via pending_conf_index, which | ||
/// is set to a value >= the log index of the latest pending | ||
/// configuration change (if any). Config changes are only allowed to | ||
/// be proposed if the leader's applied index is greater than this | ||
/// value. | ||
pub pending_conf_index: u64, | ||
|
||
pub read_only: ReadOnly, | ||
|
||
|
@@ -311,7 +316,7 @@ impl<T: Storage> Raft<T> { | |
lead_transferee: None, | ||
term: Default::default(), | ||
election_elapsed: Default::default(), | ||
pending_conf: Default::default(), | ||
pending_conf_index: Default::default(), | ||
before_step_state: None, | ||
vote: Default::default(), | ||
heartbeat_elapsed: Default::default(), | ||
|
@@ -639,7 +644,7 @@ impl<T: Storage> Raft<T> { | |
|
||
self.votes = FlatMap::default(); | ||
|
||
self.pending_conf = false; | ||
self.pending_conf_index = 0; | ||
self.read_only = ReadOnly::new(self.read_only.option); | ||
|
||
let (last_index, max_inflight) = (self.raft_log.last_index(), self.max_inflight); | ||
|
@@ -780,13 +785,13 @@ impl<T: Storage> Raft<T> { | |
let ents = self.raft_log | ||
.entries(begin, raft_log::NO_LIMIT) | ||
.expect("unexpected error getting uncommitted entries"); | ||
let nconf = self.num_pending_conf(&ents); | ||
if nconf > 1 { | ||
panic!("{} unexpected double uncommitted config entry", self.tag); | ||
} | ||
|
||
if nconf == 1 { | ||
self.pending_conf = true; | ||
// Conservatively set the pending_conf_index to the last index in the | ||
// log. There may or may not be a pending config change, but it's | ||
// safe to delay any future proposals until we commit all our | ||
// pending log entries, and scanning the entire tail of the log | ||
// could be expensive. | ||
if ents.len() > 0 { | ||
self.pending_conf_index = ents[0].get_index() + ents.len() as u64 - 1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use |
||
} | ||
self.append_entry(&mut [Entry::new()]); | ||
info!("{} became leader at term {}", self.tag, self.term); | ||
|
@@ -1387,18 +1392,20 @@ impl<T: Storage> Raft<T> { | |
return; | ||
} | ||
|
||
for e in m.mut_entries().iter_mut() { | ||
if e.get_entry_type() == EntryType::EntryConfChange { | ||
if self.pending_conf { | ||
for (i, e) in m.mut_entries().iter_mut().enumerate() { | ||
|
||
if e.get_entry_type() == EntryType::EntryConfChange { | ||
if self.pending_conf_index > self.raft_log.applied { | ||
info!( | ||
"propose conf {:?} ignored since pending unapplied \ | ||
configuration", | ||
e | ||
configuration [index {}, applied {}]", | ||
e, self.pending_conf_index, self.raft_log.applied | ||
); | ||
*e = Entry::new(); | ||
e.set_entry_type(EntryType::EntryNormal); | ||
} else { | ||
self.pending_conf_index = self.raft_log.last_index() + i as u64 + 1; | ||
} | ||
self.pending_conf = true; | ||
} | ||
} | ||
self.append_entry(&mut m.mut_entries()); | ||
|
@@ -1819,7 +1826,8 @@ impl<T: Storage> Raft<T> { | |
} | ||
|
||
pub fn should_bcast_commit(&self) -> bool { | ||
!self.skip_bcast_commit || self.pending_conf | ||
let last_index = self.raft_log.last_index(); | ||
!self.skip_bcast_commit || (self.pending_conf_index == last_index) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it should be |
||
} | ||
|
||
// promotable indicates whether state machine can be promoted to leader, | ||
|
@@ -1829,7 +1837,6 @@ impl<T: Storage> Raft<T> { | |
} | ||
|
||
fn add_voter_or_learner(&mut self, id: u64, is_learner: bool) { | ||
self.pending_conf = false; | ||
if self.prs().voters().contains_key(&id) { | ||
if is_learner { | ||
info!( | ||
|
@@ -1870,7 +1877,6 @@ impl<T: Storage> Raft<T> { | |
|
||
pub fn remove_node(&mut self, id: u64) { | ||
self.mut_prs().remove(id); | ||
self.pending_conf = false; | ||
|
||
// do not try to commit or abort transferring if there are no nodes in the cluster. | ||
if self.prs().voters().is_empty() && self.prs().learners().is_empty() { | ||
|
@@ -1888,10 +1894,6 @@ impl<T: Storage> Raft<T> { | |
} | ||
} | ||
|
||
pub fn reset_pending_conf(&mut self) { | ||
self.pending_conf = false; | ||
} | ||
|
||
pub fn set_progress(&mut self, id: u64, matched: u64, next_idx: u64, is_learner: bool) { | ||
let mut p = new_progress(next_idx, self.max_inflight); | ||
p.matched = matched; | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The check is unnecessary anymore.