Module chash

A consistent hashing implementation.

References

Description

A consistent hashing implementation. The space described by the ring coincides with SHA-1 hashes, and so any two keys producing the same SHA-1 hash are considered identical within the ring.

Warning: It is not recommended that code outside this module make use of the structure of a chash.

Data Types

chash()

chash() = {num_partitions(), [node_entry()]}

A Node is the unique identifier for the owner of a given partition. An Erlang Pid works well here, but the chash module allows it to be any term.

chash_node()

chash_node() = term()

Indices into the ring, used as keys for object location, are binary representations of 160-bit integers.

index()

index() = <<_:160>>

index_as_int()

index_as_int() = integer()

node_entry()

node_entry() = {index_as_int(), chash_node()}

num_partitions()

num_partitions() = pos_integer()

Function Index

contains_name/2Return true if named Node owns any partitions in the ring, else false.
fresh/2Create a brand new ring.
key_of/1Given any term used to name an object, produce that object's key into the ring.
lookup/2Find the Node that owns the partition identified by IndexAsInt.
members/1Return all Nodes that own any partitions in the ring.
merge_rings/2Return a randomized merge of two rings.
next_index/2Given the integer representation of a chash key, return the next ring index integer value.
nodes/1Return the entire set of NodeEntries in the ring.
predecessors/2Given an object key, return all NodeEntries in reverse order starting at Index.
predecessors/3Given an object key, return the next N NodeEntries in reverse order starting at Index.
ring_increment/1Return increment between ring indexes given the number of ring partitions.
size/1Return the number of partitions in the ring.
successors/2Given an object key, return all NodeEntries in order starting at Index.
successors/3Given an object key, return the next N NodeEntries in order starting at Index.
update/3Make the partition beginning at IndexAsInt owned by Name'd node.

Function Details

contains_name/2

contains_name(Name::chash_node(), CHash::chash()) -> boolean()

Return true if named Node owns any partitions in the ring, else false.

fresh/2

fresh(NumPartitions::num_partitions(), SeedNode::chash_node()) -> chash()

Create a brand new ring. The size and seednode are specified; initially all partitions are owned by the seednode. If NumPartitions is not much larger than the intended eventual number of participating nodes, then performance will suffer.

key_of/1

key_of(ObjectName::term()) -> index()

Given any term used to name an object, produce that object's key into the ring. Two names with the same SHA-1 hash value are considered the same name.

lookup/2

lookup(IndexAsInt::index_as_int(), CHash::chash()) -> chash_node()

Find the Node that owns the partition identified by IndexAsInt.

members/1

members(CHash::chash()) -> [chash_node()]

Return all Nodes that own any partitions in the ring.

merge_rings/2

merge_rings(CHashA::chash(), CHashB::chash()) -> chash()

Return a randomized merge of two rings. If multiple nodes are actively claiming nodes in the same time period, churn will occur. Be prepared to live with it.

next_index/2

next_index(IntegerKey::integer(), CHash::chash()) -> index_as_int()

Given the integer representation of a chash key, return the next ring index integer value.

nodes/1

nodes(CHash::chash()) -> [node_entry()]

Return the entire set of NodeEntries in the ring.

predecessors/2

predecessors(Index::index() | index_as_int(), CHash::chash()) -> [node_entry()]

Given an object key, return all NodeEntries in reverse order starting at Index.

predecessors/3

predecessors(Index::index() | index_as_int(), CHash::chash(), N::integer()) -> [node_entry()]

Given an object key, return the next N NodeEntries in reverse order starting at Index.

ring_increment/1

ring_increment(NumPartitions::pos_integer()) -> pos_integer()

Return increment between ring indexes given the number of ring partitions.

size/1

size(CHash::chash()) -> integer()

Return the number of partitions in the ring.

successors/2

successors(Index::index(), CHash::chash()) -> [node_entry()]

Given an object key, return all NodeEntries in order starting at Index.

successors/3

successors(Index::index(), CHash::chash(), N::integer()) -> [node_entry()]

Given an object key, return the next N NodeEntries in order starting at Index.

update/3

update(IndexAsInt::index_as_int(), Name::chash_node(), CHash::chash()) -> chash()

Make the partition beginning at IndexAsInt owned by Name'd node.


Generated by EDoc