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 skip bcast commit at run time #115

Merged
merged 2 commits into from
Sep 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions src/raft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,12 @@ impl<T: Storage> Raft<T> {
self.randomized_election_timeout
}

/// Set whether skip broadcast empty commit messages at runtime.
#[inline]
pub fn skip_bcast_commit(&mut self, skip: bool) {
self.skip_bcast_commit = skip;
}

// send persists state to stable storage and then sends to its mailbox.
fn send(&mut self, mut m: Message) {
m.set_from(self.id);
Expand Down
6 changes: 6 additions & 0 deletions src/raw_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,12 @@ impl<T: Storage> RawNode<T> {
pub fn mut_store(&mut self) -> &mut T {
self.raft.mut_store()
}

/// Set whether skip broadcast empty commit messages at runtime.
#[inline]
pub fn skip_bcast_commit(&mut self, skip: bool) {
self.raft.skip_bcast_commit(skip)
}
}

#[cfg(test)]
Expand Down
21 changes: 15 additions & 6 deletions tests/cases/test_raw_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,13 +497,22 @@ fn test_skip_bcast_commit() {
assert_eq!(nt.peers[&2].raft_log.committed, 2);
assert_eq!(nt.peers[&3].raft_log.committed, 2);

// Later proposal should commit former proposal.
// The feature should be able to be adjusted at run time.
nt.peers.get_mut(&1).unwrap().skip_bcast_commit(false);
nt.send(vec![msg.clone()]);
nt.send(vec![msg]);
assert_eq!(nt.peers[&1].raft_log.committed, 4);
assert_eq!(nt.peers[&1].raft_log.committed, 3);
assert_eq!(nt.peers[&2].raft_log.committed, 3);
assert_eq!(nt.peers[&3].raft_log.committed, 3);

nt.peers.get_mut(&1).unwrap().skip_bcast_commit(true);

// Later proposal should commit former proposal.
nt.send(vec![msg.clone()]);
nt.send(vec![msg]);
assert_eq!(nt.peers[&1].raft_log.committed, 5);
assert_eq!(nt.peers[&2].raft_log.committed, 4);
assert_eq!(nt.peers[&3].raft_log.committed, 4);

// When committing conf change, leader should always bcast commit.
let mut cc_entry = Entry::new();
cc_entry.set_entry_type(EntryType::EntryConfChange);
Expand All @@ -517,7 +526,7 @@ fn test_skip_bcast_commit() {
assert!(nt.peers[&2].should_bcast_commit());
assert!(nt.peers[&3].should_bcast_commit());

assert_eq!(nt.peers[&1].raft_log.committed, 5);
assert_eq!(nt.peers[&2].raft_log.committed, 5);
assert_eq!(nt.peers[&3].raft_log.committed, 5);
assert_eq!(nt.peers[&1].raft_log.committed, 6);
assert_eq!(nt.peers[&2].raft_log.committed, 6);
assert_eq!(nt.peers[&3].raft_log.committed, 6);
}