Skip to content
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

Update fxhash Hashes to hashbrown Hashes #160

Merged
merged 6 commits into from
Jan 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
25 changes: 14 additions & 11 deletions src/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
// limitations under the License.

use errors::Error;
use fxhash::{FxBuildHasher, FxHashMap, FxHashSet};
use hashbrown::hash_map::DefaultHashBuilder;
use hashbrown::{HashMap, HashSet};
use std::cell::RefCell;
use std::cmp;
use std::collections::{HashMap, HashSet};

// Since it's an integer, it rounds for us.
#[inline]
Expand All @@ -56,8 +56,8 @@ impl Default for ProgressState {

#[derive(Clone, Debug, Default)]
struct Configuration {
voters: FxHashSet<u64>,
learners: FxHashSet<u64>,
voters: HashSet<u64>,
learners: HashSet<u64>,
}

/// The status of an election according to a Candidate node.
Expand All @@ -77,7 +77,7 @@ pub enum CandidacyStatus {
/// which could be `Leader`, `Follower` and `Learner`.
#[derive(Default, Clone)]
pub struct ProgressSet {
progress: FxHashMap<u64, Progress>,
progress: HashMap<u64, Progress>,
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.
Expand All @@ -100,11 +100,14 @@ impl ProgressSet {
ProgressSet {
progress: HashMap::with_capacity_and_hasher(
voters + learners,
FxBuildHasher::default(),
DefaultHashBuilder::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, DefaultHashBuilder::default()),
learners: HashSet::with_capacity_and_hasher(
learners,
DefaultHashBuilder::default(),
),
},
sort_buffer: Default::default(),
}
Expand Down Expand Up @@ -144,13 +147,13 @@ impl ProgressSet {

/// Returns the ids of all known voters.
#[inline]
pub fn voter_ids(&self) -> &FxHashSet<u64> {
pub fn voter_ids(&self) -> &HashSet<u64> {
&self.configuration.voters
}

/// Returns the ids of all known learners.
#[inline]
pub fn learner_ids(&self) -> &FxHashSet<u64> {
pub fn learner_ids(&self) -> &HashSet<u64> {
&self.configuration.learners
}

Expand Down Expand Up @@ -326,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<u64>) -> bool {
pub fn has_quorum(&self, potential_quorum: &HashSet<u64>) -> bool {
potential_quorum.len() >= majority(self.voter_ids().len())
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/raft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -121,7 +121,7 @@ pub struct Raft<T: Storage> {
/// The current votes for this node in an election.
///
/// Reset when changing role.
pub votes: FxHashMap<u64, bool>,
pub votes: HashMap<u64, bool>,

/// The list of messages.
pub msgs: Vec<Message>,
Expand Down Expand Up @@ -720,7 +720,7 @@ impl<T: Storage> Raft<T> {
// 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;
Expand Down Expand Up @@ -1396,7 +1396,7 @@ impl<T: Storage> Raft<T> {
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.
Expand Down
14 changes: 7 additions & 7 deletions src/read_only.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -68,21 +68,21 @@ pub struct ReadState {
pub struct ReadIndexStatus {
pub req: Message,
pub index: u64,
pub acks: FxHashSet<u64>,
pub acks: HashSet<u64>,
}

#[derive(Default, Debug, Clone)]
pub struct ReadOnly {
pub option: ReadOnlyOption,
pub pending_read_index: FxHashMap<Vec<u8>, ReadIndexStatus>,
pub pending_read_index: HashMap<Vec<u8>, ReadIndexStatus>,
pub read_index_queue: VecDeque<Vec<u8>>,
}

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(),
}
}
Expand All @@ -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);
Expand All @@ -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<u64> {
pub fn recv_ack(&mut self, m: &Message) -> HashSet<u64> {
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()
}
Expand Down
6 changes: 3 additions & 3 deletions src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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<u64, Progress>,
pub progress: HashMap<u64, Progress>,
/// The progress of learners in catching up and applying logs.
pub learner_progress: FxHashMap<u64, Progress>,
pub learner_progress: HashMap<u64, Progress>,
}

impl Status {
Expand Down
8 changes: 4 additions & 4 deletions tests/integration_cases/test_raft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -2746,7 +2746,7 @@ fn test_restore() {
.get_nodes()
.iter()
.cloned()
.collect::<FxHashSet<_>>(),
.collect::<HashSet<_>>(),
);
assert!(!sm.restore(s));
}
Expand Down Expand Up @@ -2956,7 +2956,7 @@ fn test_add_node() {
r.add_node(2);
assert_eq!(
r.prs().voter_ids(),
&vec![1, 2].into_iter().collect::<FxHashSet<_>>()
&vec![1, 2].into_iter().collect::<HashSet<_>>()
);
}

Expand Down Expand Up @@ -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::<FxHashSet<_>>();
let wids = wids.into_iter().collect::<HashSet<_>>();
if voter_ids != &wids {
panic!("#{}: nodes = {:?}, want {:?}", i, voter_ids, wids);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down