-
Notifications
You must be signed in to change notification settings - Fork 410
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
Small API Improvements #102
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,10 +25,10 @@ | |
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use errors::Error; | ||
use fxhash::FxHashMap; | ||
use std::cmp; | ||
use std::collections::hash_map::{HashMap, Iter, IterMut}; | ||
use std::iter::Chain; | ||
use std::collections::hash_map::HashMap; | ||
|
||
/// The state of the progress. | ||
#[derive(Debug, PartialEq, Clone, Copy)] | ||
|
@@ -105,41 +105,37 @@ impl ProgressSet { | |
} | ||
|
||
/// Returns an iterator across all the nodes and their progress. | ||
pub fn iter(&self) -> Chain<Iter<u64, Progress>, Iter<u64, Progress>> { | ||
pub fn iter(&self) -> impl Iterator<Item = (&u64, &Progress)> { | ||
self.voters.iter().chain(&self.learners) | ||
} | ||
|
||
/// Returns a mutable iterator across all the nodes and their progress. | ||
pub fn iter_mut(&mut self) -> Chain<IterMut<u64, Progress>, IterMut<u64, Progress>> { | ||
pub fn iter_mut(&mut self) -> impl Iterator<Item = (&u64, &mut Progress)> { | ||
self.voters.iter_mut().chain(&mut self.learners) | ||
} | ||
|
||
/// 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"))?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any different to panic outside? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The error message will be different now. Otherwise this is just moving the error up a level so these functions can be called without worrying about panic. |
||
} | ||
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. | ||
|
@@ -151,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"))?; | ||
} | ||
// We don't want to remove it unless it's there. | ||
if self.learners.contains_key(&id) { | ||
let mut learner = self.learners.remove(&id).unwrap(); // We just checked! | ||
learner.is_learner = false; | ||
self.voters.insert(id, learner); | ||
Ok(()) | ||
} else { | ||
Err(Error::NotExists(id, "learners")) | ||
} | ||
panic!("promote not exists learner: {}", id); | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can these two errors be merged?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, maybe? I think
Exists(u64, &'static str, bool)
could work? I worry that there might be some semantic issues. I was already not sure about combining things likeVoterCannotBecomeLearner
,VoterExists
,LearnerExists
, andLearnerAlreadyVoter
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think distinguishing them may be more clear.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so. People usually don't care about what the exact case are they, what they do is just panic. More errors lead to longer match or repeated duplicated
error1 | error 2
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@BusyJay In the case of this particular error there are definitely recoverable cases, and they are different between the errors.
If the user is lazy and wants to panic, that's fine, but we should allow our users to write good code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am fine with this, like Rust IO error, it has NotFound or AlreadyExists error for a file.
LGTM