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

raft: Avoid scanning raft log in become_leader #15

Merged

Conversation

csmoe
Copy link
Contributor

@csmoe csmoe commented Jan 22, 2018

should resolve issue #11
modifications based on pull request etcd-io/etcd#9073

@BusyJay BusyJay changed the title raft: Avoid scanning raft log in become_leader. (#11) raft: Avoid scanning raft log in become_leader Jan 23, 2018
@siddontang
Copy link
Contributor

Great work @csmoe

PTAL @Hoverbear @BusyJay

src/raft.rs Outdated
// 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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use ents.last().map_or(0, |e| e.get_index()).

src/raft.rs Outdated
@@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be self.pending_conf_index > self.raft_log.applied.

src/raft.rs Outdated
// 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 {
Copy link
Member

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.

@@ -1819,7 +1826,7 @@ impl<T: Storage> Raft<T> {
}

pub fn should_bcast_commit(&self) -> bool {
!self.skip_bcast_commit || self.pending_conf
!self.skip_bcast_commit || (self.pending_conf_index > self.raft_log.applied)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any tests to cover this?

@siddontang
Copy link
Contributor

any update @csmoe

@csmoe
Copy link
Contributor Author

csmoe commented Jan 26, 2018

@siddontang sorry for the delay. I'm trying to figure out the tests for self.pending_conf_index > self.raft_log.applied in pub fn should_bcast_commit(&self) -> bool

@@ -429,6 +429,11 @@ fn test_skip_bcast_commit() {
// elect r1 as leader
nt.send(vec![new_message(1, 1, MessageType::MsgHup, 0)]);

// Skip bcast commit when pending_conf_index <= applied
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we check the result of should_bcast_commit here?

@csmoe csmoe force-pushed the avoid-scanning-raft-log-in-become_leader branch from f98ab5f to 0f0b42e Compare January 28, 2018 04:15
@siddontang
Copy link
Contributor

LGTM

PTAL @BusyJay @hicqu

@siddontang
Copy link
Contributor

PTAL @BusyJay @hicqu

@siddontang siddontang merged commit 9668b1b into tikv:master Feb 2, 2018
@csmoe csmoe deleted the avoid-scanning-raft-log-in-become_leader branch February 3, 2018 05:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants