hrw-hash
A minimalistic implementation of the Highest Random Weight (HRW) aka Rendezvous hashing algorithm as described in the "A name-based mapping scheme for Rendezvous", by Thaler and Ravishankar (1996).
The weighted variant of the HRW algorithm is implemented using Logarithmic Method as described in "Weighted distributed hash tables", by Schindelhauer and Schomaker (2005).
To constrain the number of hashing operations, the implementation hashes nodes and keys only once
(instead of nodes * keys hashes). This optimization idea is well presented in the
"Rendezvous Hashing: The Path to Faster Hashes Calculation"
blog.
Features
- Absolutely minimalistic implementation with sane defaults.
- Allow weighted nodes.
- Optimized for performance and memory usage. No wasted re-hashing.
Motivation
Given list of nodes (as IntoIterator<Item = Node>) the aim is to produce sorted list of
references to these nodes (Iterator<Item = &Node>) for any given key.
This sorted list serves as priority-based set of destination nodes for the key. With the first node being the primary replica, the second one being the secondary replica, and so on.
Both weighted and non-weighted nodes are supported.
Usage
For non-weighted nodes:
use ;
// Anything that implements `Hash + Eq` can be used as node.
;
// Mark the node as eligible for HRW hashing.
// Create a new HRW instance with the list of nodes.
let nodes: = .map.collect;
let hrw = new;
// For a given key, get list of nodes sorted by their priority.
let key = 42;
let replicas: = hrw.sorted.take.collect;
assert_eq!;
For weighted nodes, which can have different capacities, the only difference is that you have to
implement the capacity() method of HrwNode trait:
use ;
// Anything that implements `Hash + Eq` can be used as node.
// Mark the node as eligible for HRW hashing.
// The `capacity()` method returns the capacity of the node.
let mut nodes = Vecnew;
for i in 0..100
// Add some nodes with higher capacities.
nodes.push;
nodes.push;
let hrw = new;
// Nodes `100` and `101` have higher capacity, so they will be
// selected more often -- even though there are many other nodes.
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Note that primitive types can be used as nodes too. This is done to allow passing node indexes or IDs directly.
use ;
// String as node
let nodes: = .map.collect;
let hrw = new;
let replicas = hrw.sorted.take.;
assert_eq!;
// u16 as node
let nodes: = .map.collect;
let hrw = new;
let replicas = hrw.sorted.take.;
assert_eq!;
License
MIT