Skip to content

Commit

Permalink
support skip bcast commit at run time (#115)
Browse files Browse the repository at this point in the history
Only users know whether a message needs to be commit eagerly. So provide a way
to configure the feature at run time.
  • Loading branch information
BusyJay authored and A. Hobden committed Sep 7, 2018
1 parent 50be823 commit 326d04c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
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);
}

0 comments on commit 326d04c

Please sign in to comment.