From 4259f296cce1a3a1e4050c7a7ea86c035b31a74f Mon Sep 17 00:00:00 2001 From: qupeng Date: Tue, 17 Dec 2019 15:10:41 +0800 Subject: [PATCH] fixes and improvements (#5) * some improvements Signed-off-by: qupeng * fix a little bug Signed-off-by: qupeng * remove useless code Signed-off-by: qupeng --- src/progress/progress_set.rs | 29 ++++++++++++++--------------- src/raft.rs | 4 ++-- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/progress/progress_set.rs b/src/progress/progress_set.rs index fae73f64f..d42e0c0e2 100644 --- a/src/progress/progress_set.rs +++ b/src/progress/progress_set.rs @@ -55,11 +55,6 @@ impl Configuration { } } - /// Take learners - pub fn take_learners(&mut self) -> HashSet { - std::mem::replace(&mut self.learners, HashSet::default()) - } - /// Create a new `ConfState` from the configuration itself. pub fn to_conf_state(&self) -> ConfState { let mut state = ConfState::default(); @@ -432,22 +427,26 @@ impl ProgressSet { F: FnMut(u64, bool), { let mut active = HashSet::default(); - let learners = self.configuration.take_learners(); - for (&id, pr) in self.iter_mut() { - if id == perspective_of { - active.insert(id); - continue; - } - f(id, pr.recent_active); - if learners.contains(&id) { + for id in &self.configuration.voters { + if *id == perspective_of { + active.insert(*id); continue; } + let pr = self.progress.get_mut(id).unwrap(); + f(*id, pr.recent_active); if pr.recent_active { - active.insert(id); + active.insert(*id); + } + pr.recent_active = false; + } + for id in &self.configuration.learners { + if *id == perspective_of { + continue; } + let pr = self.progress.get_mut(id).unwrap(); + f(*id, pr.recent_active); pr.recent_active = false; } - self.configuration.set_learners(learners); self.configuration.has_quorum(&active) } diff --git a/src/raft.rs b/src/raft.rs index c40720422..cf8b95a73 100644 --- a/src/raft.rs +++ b/src/raft.rs @@ -940,8 +940,8 @@ impl Raft { /// message from a peer. #[allow(clippy::collapsible_if)] pub fn step(&mut self, m: Message) -> Result<()> { - if m.term != 0 && m.get_group_id() != INVALID_ID { - if self.groups.update_group_id(m.from, m.get_group_id()) { + if m.term != 0 && m.group_id != INVALID_ID && self.is_leader() { + if self.groups.update_group_id(m.from, m.group_id) { let prs = self.take_prs(); self.groups.resolve_delegates(&prs); self.set_prs(prs);