From c1df877acb8700f9e724f99151007278b860004c Mon Sep 17 00:00:00 2001 From: Benjamin Delmas Date: Fri, 21 Dec 2018 15:57:41 +0100 Subject: [PATCH 1/3] Update fxhash Hashes to hashbrown Hashes --- Cargo.toml | 2 +- src/lib.rs | 2 +- src/progress.rs | 23 ++++++++++++----------- src/raft.rs | 8 ++++---- src/read_only.rs | 14 +++++++------- src/status.rs | 6 +++--- tests/integration_cases/test_raft.rs | 8 ++++---- tests/tests.rs | 2 +- 8 files changed, 33 insertions(+), 32 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a22f58afd..7ed554f7d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,7 @@ log = ">0.2" protobuf = "2.0.4" quick-error = "1.2.2" rand = "0.5.4" -fxhash = "0.2.1" +hashbrown = "0.1" fail = { version = "0.2", optional = true } [dev-dependencies] diff --git a/src/lib.rs b/src/lib.rs index df01a2b18..0b2d8c6c8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -266,7 +266,7 @@ For more information, check out an [example](examples/single_mem_node/main.rs#L1 #[cfg(feature = "failpoint")] #[macro_use] extern crate fail; -extern crate fxhash; +extern crate hashbrown; #[macro_use] extern crate log; extern crate protobuf; diff --git a/src/progress.rs b/src/progress.rs index 723c5a0fa..afe24ec7b 100644 --- a/src/progress.rs +++ b/src/progress.rs @@ -26,10 +26,11 @@ // limitations under the License. use errors::Error; -use fxhash::{FxBuildHasher, FxHashMap, FxHashSet}; +use hashbrown::{HashMap, HashSet}; +type DefaultHashMapBuilder = ::hashbrown::hash_map::DefaultHashBuilder; +type DefaultHashSetBuilder = ::hashbrown::hash_map::DefaultHashBuilder; use std::cell::RefCell; use std::cmp; -use std::collections::{HashMap, HashSet}; // Since it's an integer, it rounds for us. #[inline] @@ -56,8 +57,8 @@ impl Default for ProgressState { #[derive(Clone, Debug, Default)] struct Configuration { - voters: FxHashSet, - learners: FxHashSet, + voters: HashSet, + learners: HashSet, } /// The status of an election according to a Candidate node. @@ -77,7 +78,7 @@ pub enum CandidacyStatus { /// which could be `Leader`, `Follower` and `Learner`. #[derive(Default, Clone)] pub struct ProgressSet { - progress: FxHashMap, + progress: HashMap, configuration: Configuration, // A preallocated buffer for sorting in the minimally_commited_index function. // You should not depend on these values unless you just set them. @@ -100,11 +101,11 @@ impl ProgressSet { ProgressSet { progress: HashMap::with_capacity_and_hasher( voters + learners, - FxBuildHasher::default(), + DefaultHashMapBuilder::default(), ), configuration: Configuration { - voters: HashSet::with_capacity_and_hasher(voters, FxBuildHasher::default()), - learners: HashSet::with_capacity_and_hasher(learners, FxBuildHasher::default()), + voters: HashSet::with_capacity_and_hasher(voters, DefaultHashSetBuilder::default()), + learners: HashSet::with_capacity_and_hasher(learners, DefaultHashSetBuilder::default()), }, sort_buffer: Default::default(), } @@ -144,13 +145,13 @@ impl ProgressSet { /// Returns the ids of all known voters. #[inline] - pub fn voter_ids(&self) -> &FxHashSet { + pub fn voter_ids(&self) -> &HashSet { &self.configuration.voters } /// Returns the ids of all known learners. #[inline] - pub fn learner_ids(&self) -> &FxHashSet { + pub fn learner_ids(&self) -> &HashSet { &self.configuration.learners } @@ -328,7 +329,7 @@ impl ProgressSet { } /// Determine if a quorum is formed from the given set of nodes. - pub fn has_quorum(&self, potential_quorum: &FxHashSet) -> bool { + pub fn has_quorum(&self, potential_quorum: &HashSet) -> bool { potential_quorum.len() >= majority(self.voter_ids().len()) } } diff --git a/src/raft.rs b/src/raft.rs index b85bc6e56..5a3a0016e 100644 --- a/src/raft.rs +++ b/src/raft.rs @@ -28,7 +28,7 @@ use std::cmp; use eraftpb::{Entry, EntryType, HardState, Message, MessageType, Snapshot}; -use fxhash::{FxHashMap, FxHashSet}; +use hashbrown::{HashMap, HashSet}; use protobuf::RepeatedField; use rand::{self, Rng}; @@ -121,7 +121,7 @@ pub struct Raft { /// The current votes for this node in an election. /// /// Reset when changing role. - pub votes: FxHashMap, + pub votes: HashMap, /// The list of messages. pub msgs: Vec, @@ -719,7 +719,7 @@ impl Raft { // but doesn't change anything else. In particular it does not increase // self.term or change self.vote. self.state = StateRole::PreCandidate; - self.votes = FxHashMap::default(); + self.votes = HashMap::default(); // If a network partition happens, and leader is in minority partition, // it will step down, and become follower without notifying others. self.leader_id = INVALID_ID; @@ -1392,7 +1392,7 @@ impl Raft { return Ok(()); } - let mut self_set = FxHashSet::default(); + let mut self_set = HashSet::default(); self_set.insert(self.id); if !self.prs().has_quorum(&self_set) { // thinking: use an interally defined context instead of the user given context. diff --git a/src/read_only.rs b/src/read_only.rs index 26f045882..a51a3475a 100644 --- a/src/read_only.rs +++ b/src/read_only.rs @@ -29,7 +29,7 @@ use std::collections::VecDeque; use eraftpb::Message; -use fxhash::{FxHashMap, FxHashSet}; +use hashbrown::{HashMap, HashSet}; /// Determines the relative safety of and consistency of read only requests. #[derive(Debug, PartialEq, Clone, Copy)] @@ -68,13 +68,13 @@ pub struct ReadState { pub struct ReadIndexStatus { pub req: Message, pub index: u64, - pub acks: FxHashSet, + pub acks: HashSet, } #[derive(Default, Debug, Clone)] pub struct ReadOnly { pub option: ReadOnlyOption, - pub pending_read_index: FxHashMap, ReadIndexStatus>, + pub pending_read_index: HashMap, ReadIndexStatus>, pub read_index_queue: VecDeque>, } @@ -82,7 +82,7 @@ impl ReadOnly { pub fn new(option: ReadOnlyOption) -> ReadOnly { ReadOnly { option, - pending_read_index: FxHashMap::default(), + pending_read_index: HashMap::default(), read_index_queue: VecDeque::new(), } } @@ -104,7 +104,7 @@ impl ReadOnly { let status = ReadIndexStatus { req: m, index, - acks: FxHashSet::default(), + acks: HashSet::default(), }; self.pending_read_index.insert(ctx.clone(), status); self.read_index_queue.push_back(ctx); @@ -113,13 +113,13 @@ impl ReadOnly { /// Notifies the ReadOnly struct that the raft state machine received /// an acknowledgment of the heartbeat that attached with the read only request /// context. - pub fn recv_ack(&mut self, m: &Message) -> FxHashSet { + pub fn recv_ack(&mut self, m: &Message) -> HashSet { match self.pending_read_index.get_mut(m.get_context()) { None => Default::default(), Some(rs) => { rs.acks.insert(m.get_from()); // add one to include an ack from local node - let mut set_with_self = FxHashSet::default(); + let mut set_with_self = HashSet::default(); set_with_self.insert(m.get_to()); rs.acks.union(&set_with_self).cloned().collect() } diff --git a/src/status.rs b/src/status.rs index b17ff1a8c..ce9c1bd1d 100644 --- a/src/status.rs +++ b/src/status.rs @@ -26,7 +26,7 @@ // limitations under the License. use eraftpb::HardState; -use fxhash::FxHashMap; +use hashbrown::HashMap; use progress::Progress; use raft::{Raft, SoftState, StateRole}; @@ -44,9 +44,9 @@ pub struct Status { /// The index of the last entry to have been applied. pub applied: u64, /// The progress towards catching up and applying logs. - pub progress: FxHashMap, + pub progress: HashMap, /// The progress of learners in catching up and applying logs. - pub learner_progress: FxHashMap, + pub learner_progress: HashMap, } impl Status { diff --git a/tests/integration_cases/test_raft.rs b/tests/integration_cases/test_raft.rs index bf97647f3..0e4ca12eb 100644 --- a/tests/integration_cases/test_raft.rs +++ b/tests/integration_cases/test_raft.rs @@ -29,7 +29,7 @@ use std::cmp; use std::collections::HashMap; use std::panic::{self, AssertUnwindSafe}; -use fxhash::FxHashSet; +use hashbrown::HashSet; use protobuf::{self, RepeatedField}; use raft::eraftpb::{ ConfChange, ConfChangeType, ConfState, Entry, EntryType, HardState, Message, MessageType, @@ -2746,7 +2746,7 @@ fn test_restore() { .get_nodes() .iter() .cloned() - .collect::>(), + .collect::>(), ); assert!(!sm.restore(s)); } @@ -2956,7 +2956,7 @@ fn test_add_node() { r.add_node(2); assert_eq!( r.prs().voter_ids(), - &vec![1, 2].into_iter().collect::>() + &vec![1, 2].into_iter().collect::>() ); } @@ -3032,7 +3032,7 @@ fn test_raft_nodes() { for (i, (ids, wids)) in tests.drain(..).enumerate() { let r = new_test_raft(1, ids, 10, 1, new_storage()); let voter_ids = r.prs().voter_ids(); - let wids = wids.into_iter().collect::>(); + let wids = wids.into_iter().collect::>(); if voter_ids != &wids { panic!("#{}: nodes = {:?}, want {:?}", i, voter_ids, wids); } diff --git a/tests/tests.rs b/tests/tests.rs index 016d1edf6..969aa711f 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -25,7 +25,7 @@ extern crate rand; extern crate lazy_static; #[cfg(feature = "failpoint")] extern crate fail; -extern crate fxhash; +extern crate hashbrown; /// Get the count of macro's arguments. /// From 38672e2db4c44c4be9fb78a5633cce9db9821391 Mon Sep 17 00:00:00 2001 From: Hoverbear Date: Fri, 4 Jan 2019 10:06:00 -0800 Subject: [PATCH 2/3] fmt --- src/progress.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/progress.rs b/src/progress.rs index 947bc5a66..86b1ca82d 100644 --- a/src/progress.rs +++ b/src/progress.rs @@ -105,7 +105,10 @@ impl ProgressSet { ), configuration: Configuration { voters: HashSet::with_capacity_and_hasher(voters, DefaultHashSetBuilder::default()), - learners: HashSet::with_capacity_and_hasher(learners, DefaultHashSetBuilder::default()), + learners: HashSet::with_capacity_and_hasher( + learners, + DefaultHashSetBuilder::default(), + ), }, sort_buffer: Default::default(), } From 48be5a8c1f70c561a3cd0348a2f9d4cfb2b32083 Mon Sep 17 00:00:00 2001 From: Benjamin Delmas Date: Tue, 15 Jan 2019 14:32:09 +0100 Subject: [PATCH 3/3] Update imports from hashbrown --- src/progress.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/progress.rs b/src/progress.rs index 86b1ca82d..16cd8259a 100644 --- a/src/progress.rs +++ b/src/progress.rs @@ -26,9 +26,8 @@ // limitations under the License. use errors::Error; +use hashbrown::hash_map::DefaultHashBuilder; use hashbrown::{HashMap, HashSet}; -type DefaultHashMapBuilder = ::hashbrown::hash_map::DefaultHashBuilder; -type DefaultHashSetBuilder = ::hashbrown::hash_map::DefaultHashBuilder; use std::cell::RefCell; use std::cmp; @@ -101,13 +100,13 @@ impl ProgressSet { ProgressSet { progress: HashMap::with_capacity_and_hasher( voters + learners, - DefaultHashMapBuilder::default(), + DefaultHashBuilder::default(), ), configuration: Configuration { - voters: HashSet::with_capacity_and_hasher(voters, DefaultHashSetBuilder::default()), + voters: HashSet::with_capacity_and_hasher(voters, DefaultHashBuilder::default()), learners: HashSet::with_capacity_and_hasher( learners, - DefaultHashSetBuilder::default(), + DefaultHashBuilder::default(), ), }, sort_buffer: Default::default(),