From 1df5ab431b33a99a434506c0e4962e3277bf7546 Mon Sep 17 00:00:00 2001 From: Jay Lee Date: Thu, 6 Sep 2018 16:57:39 +0800 Subject: [PATCH] support skip bcast commit at run time Only users know whether a message needs to be commit eagerly. So provide a way to configure the feature at run time. --- src/raft.rs | 6 ++++++ src/raw_node.rs | 6 ++++++ tests/cases/test_raw_node.rs | 21 +++++++++++++++------ 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/raft.rs b/src/raft.rs index 0d59a7631..11dd57065 100644 --- a/src/raft.rs +++ b/src/raft.rs @@ -389,6 +389,12 @@ impl Raft { 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); diff --git a/src/raw_node.rs b/src/raw_node.rs index 42f1e4af2..d2e699c78 100644 --- a/src/raw_node.rs +++ b/src/raw_node.rs @@ -474,6 +474,12 @@ impl RawNode { 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)] diff --git a/tests/cases/test_raw_node.rs b/tests/cases/test_raw_node.rs index 80e4279c2..1e82a8985 100644 --- a/tests/cases/test_raw_node.rs +++ b/tests/cases/test_raw_node.rs @@ -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); @@ -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); }