-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Heading toward consistent hash implementation.
- Loading branch information
1 parent
216c8ce
commit 9dcfb1b
Showing
4 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ authors = ["Robert Collins <[email protected]>"] | |
[dependencies] | ||
libc="*" | ||
pnetlink= { version="*", git = "https://github.com/rbtcollins/pnetlink" } | ||
siphasher = "~0.1" | ||
|
||
[dependencies.netmap] | ||
git = "https://github.com/rbtcollins/netmap-rs" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright (c) 2017 Robert Collins. Licensed under the Apache-2.0 license. | ||
// | ||
// Consistent hashing for selecting backends. | ||
|
||
use std::hash::{Hash, Hasher}; | ||
|
||
use siphasher::sip::{SipHasher}; | ||
|
||
use super::primes; | ||
|
||
/// Generate permutations for a given offset, skip, pool | ||
/// | ||
/// ``` | ||
/// use rusty_rail::consistenthash::permutations; | ||
/// | ||
/// assert_eq!(permutations(3, 4, 7), vec![3, 0, 4, 1, 5, 2, 6]); | ||
/// assert_eq!(permutations(0, 2, 7), vec![0, 2, 4, 6, 1, 3, 5]); | ||
/// assert_eq!(permutations(3, 1, 7), vec![3, 4, 5, 6, 0, 1, 2]); | ||
/// ``` | ||
pub fn permutations(offset: u32, skip: u32, pool_size: u32) -> Vec<u32> { | ||
let mut res: Vec<u32> = Vec::with_capacity(pool_size as usize); | ||
for pos in 0..pool_size { | ||
res.push((offset + pos * skip) % pool_size) | ||
} | ||
res | ||
} | ||
|
||
/// Generate permutations for a given name and pool size. | ||
/// | ||
/// ``` | ||
/// use rusty_rail::consistenthash::permute_backend; | ||
/// | ||
/// assert_eq!(permute_backend("fred".to_string(), 7), vec![1, 0, 6, 5, 4, 3, 2]); | ||
/// assert_eq!(permute_backend("ralph".to_string(), 7), vec![3, 2, 1, 0, 6, 5, 4]); | ||
/// assert_eq!(permute_backend("larry".to_string(), 7), vec![4, 0, 3, 6, 2, 5, 1]); | ||
/// ``` | ||
pub fn permute_backend(name: String, pool_size: u32) -> Vec<u32> { | ||
let mut s = SipHasher::new(); | ||
name.hash(&mut s); | ||
let offset = (s.finish() % pool_size as u64) as u32; | ||
"differenthash".hash(&mut s); | ||
let skip = (s.finish() % (pool_size as u64 - 1) + 1) as u32; | ||
permutations(offset, skip, pool_size) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright (c) 2017 Robert Collins. Licensed under the Apache-2.0 license. | ||
// | ||
// Used in implementing the maglev consistent hash. | ||
extern crate netmap; | ||
|
||
#[test] | ||
fn examples() { | ||
assert_eq!(primes(0), vec![]); | ||
assert_eq!(primes(1), vec![]); | ||
assert_eq!(primes(2), vec![2]); | ||
assert_eq!(primes(3), vec![2, 3]); | ||
assert_eq!(primes(4), vec![2, 3]); | ||
assert_eq!(primes(5), vec![2, 3, 5]); | ||
} | ||
|
||
/// Returns all primes less than or equal to limit in a vector | ||
fn primes(limit: usize) -> Vec<u32> { | ||
let sqrt = (limit as f64).sqrt().ceil() as usize; | ||
let mut res: Vec<u32> = Vec::with_capacity(sqrt); | ||
let mut sieve = vec![false; limit+1]; | ||
if limit < 2 { | ||
return res; | ||
} | ||
for candidate in 2..limit + 1 { | ||
if sieve[candidate] { | ||
continue; | ||
} | ||
res.push(candidate as u32); | ||
let mut composite = 2 * candidate; | ||
while composite <= limit { | ||
sieve[composite] = true; | ||
composite += candidate | ||
} | ||
} | ||
res | ||
} |