Skip to content

Commit

Permalink
Make ProgressSet not panic.
Browse files Browse the repository at this point in the history
Moves failable functions to returning `Result<(), Error>` and adds
useful error types for determining the problem.

Adds an `panic` to all calling code to preserve existing behavior.
  • Loading branch information
Hoverbear committed Aug 8, 2018
1 parent cabe8ac commit 2b7977a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 35 deletions.
9 changes: 8 additions & 1 deletion src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@ quick_error! {
description(err.description())
display("protobuf error {:?}", err)
}

/// The node exists, but should not.
Exists(id: u64, set: &'static str) {
display("The node {} aleady exists in the {} set.", id, set)
}
/// The node does not exist, but should.
NotExists(id: u64, set: &'static str) {
display("The node {} is not in the {} set.", id, set)
}
}
}

Expand Down
53 changes: 26 additions & 27 deletions src/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use fxhash::FxHashMap;
use std::cmp;
use std::collections::hash_map::HashMap;
use errors::Error;

/// The state of the progress.
#[derive(Debug, PartialEq, Clone, Copy)]
Expand Down Expand Up @@ -114,31 +115,27 @@ impl ProgressSet {
}

/// Adds a voter node
///
/// # Panics
///
/// Panics if the node already has been added.
pub fn insert_voter(&mut self, id: u64, pr: Progress) {
if self.learners.contains_key(&id) {
panic!("insert voter {} but already in learners", id);
pub fn insert_voter(&mut self, id: u64, pr: Progress) -> Result<(), Error> {
if self.voters.contains_key(&id) {
Err(Error::Exists(id, "voters"))?
}
if self.voters.insert(id, pr).is_some() {
panic!("insert voter {} twice", id);
if self.learners.contains_key(&id) {
Err(Error::Exists(id, "learners"))?;
}
self.voters.insert(id, pr);
Ok(())
}

/// Adds a learner to the cluster
///
/// # Panics
///
/// Panics if the node already has been added.
pub fn insert_learner(&mut self, id: u64, pr: Progress) {
pub fn insert_learner(&mut self, id: u64, pr: Progress) -> Result<(), Error> {
if self.voters.contains_key(&id) {
panic!("insert learner {} but already in voters", id);
Err(Error::Exists(id, "voters"))?
}
if self.learners.insert(id, pr).is_some() {
panic!("insert learner {} twice", id);
if self.learners.contains_key(&id) {
Err(Error::Exists(id, "learners"))?
}
self.learners.insert(id, pr);
Ok(())
}

/// Removes the peer from the set of voters or learners.
Expand All @@ -150,17 +147,19 @@ impl ProgressSet {
}

/// Promote a learner to a peer.
///
/// # Panics
///
/// Panics if the node doesn't exist.
pub fn promote_learner(&mut self, id: u64) {
if let Some(mut pr) = self.learners.remove(&id) {
pr.is_learner = false;
self.voters.insert(id, pr);
return;
pub fn promote_learner(&mut self, id: u64) -> Result<(), Error> {
if self.voters.contains_key(&id) {
Err(Error::Exists(id, "voters"))?;
}
if !self.learners.contains_key(&id) {
Err(Error::NotExists(id, "learners"))?
}
panic!("promote not exists learner: {}", id);

self.learners.remove(&id).map(|mut learner| {
learner.is_learner = false;
self.voters.insert(id, learner);
});
Ok(())
}
}

Expand Down
20 changes: 15 additions & 5 deletions src/raft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,16 @@ impl<T: Storage> Raft<T> {
};
for p in peers {
let pr = new_progress(1, r.max_inflight);
r.mut_prs().insert_voter(*p, pr);
if let Err(e) = r.mut_prs().insert_voter(*p, pr) {
panic!("{}", e);
}
}
for p in learners {
let mut pr = new_progress(1, r.max_inflight);
pr.is_learner = true;
r.mut_prs().insert_learner(*p, pr);
if let Err(e) = r.mut_prs().insert_learner(*p, pr) {
panic!("{}", e);
};
if *p == r.id {
r.is_learner = true;
}
Expand Down Expand Up @@ -1854,7 +1858,9 @@ impl<T: Storage> Raft<T> {
// Ignore redundant add learner.
return;
}
self.mut_prs().promote_learner(id);
if let Err(e) = self.mut_prs().promote_learner(id) {
panic!("{}", e)
}
if id == self.id {
self.is_learner = false;
}
Expand Down Expand Up @@ -1905,9 +1911,13 @@ impl<T: Storage> Raft<T> {
p.matched = matched;
p.is_learner = is_learner;
if is_learner {
self.mut_prs().insert_learner(id, p);
if let Err(e) = self.mut_prs().insert_learner(id, p) {
panic!("{}", e);
}
} else {
self.mut_prs().insert_voter(id, p);
if let Err(e) = self.mut_prs().insert_voter(id, p) {
panic!("{}", e);
}
}
}

Expand Down
8 changes: 6 additions & 2 deletions tests/cases/test_raft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,16 @@ impl Interface {
is_learner: true,
..Default::default()
};
self.mut_prs().insert_learner(*id, progress);
if let Err(e) = self.mut_prs().insert_learner(*id, progress) {
panic!("{}", e);
}
} else {
let progress = Progress {
..Default::default()
};
self.mut_prs().insert_voter(*id, progress);
if let Err(e) = self.mut_prs().insert_voter(*id, progress) {
panic!("{}", e);
}
}
}
let term = self.term;
Expand Down

0 comments on commit 2b7977a

Please sign in to comment.