Package org.apache.ignite.internal.util
Class GridConsistentHash<N>
- java.lang.Object
-
- org.apache.ignite.internal.util.GridConsistentHash<N>
-
public class GridConsistentHash<N> extends Object
Controls key to node affinity using consistent hash algorithm. This class is thread-safe and does not have to be externally synchronized.For a good explanation of what consistent hashing is, you can refer to Tom White's Blog.
-
-
Constructor Summary
Constructors Constructor Description GridConsistentHash()Constructs consistent hash using default parameters.GridConsistentHash(@Nullable Object affSeed)Constructs consistent hash using given affinity seed andMD5hasher function.GridConsistentHash(@Nullable Comparator<N> nodesComp, @Nullable Object affSeed)Constructs consistent hash using given affinity seed and hasher function.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanaddNode(N node, int replicas)Adds a node to consistent hash algorithm.voidaddNodes(@Nullable Collection<N> nodes, int replicas)Adds nodes to consistent hash algorithm (if nodes arenullor empty, then no-op).booleanbelongs(@Nullable Object key, int cnt, @Nullable Collection<N> nodes)Checks if node the key is mapped to or the given count of its adjacent nodes are contained in given set of nodes.booleanbelongs(@Nullable Object key, int cnt, N node)Checks if node the key is mapped to or the given count of its adjacent nodes contain the node passed in.booleanbelongs(@Nullable Object key, @Nullable Collection<N> nodes)Checks if given key belongs to any of the given nodes.booleanbelongs(@Nullable Object key, N node)Checks if key belongs to the given node.voidclear()Clears all nodes from consistent hash.intcount()Gets number of distinct nodes, excluding replicas, in consistent hash.protected inthash(Object o)Gets hash code for a given object.booleanisEmpty()Checks if consistent hash has nodes added to it.Nnode(@Nullable Object key)Gets node for a key.Nnode(@Nullable Object key, @Nullable Collection<N> inc)Gets node for a given key.Nnode(@Nullable Object key, @Nullable Collection<N> inc, @Nullable Collection<N> exc)Gets node for a given key.Nnode(@Nullable Object key, @Nullable IgnitePredicate<N>... p)Gets node for a given key.Set<N>nodes()Gets set of all distinct nodes in the consistent hash (in no particular order).List<N>nodes(@Nullable Object key, int cnt)Gets specified count of adjacent nodes for a given key.List<N>nodes(@Nullable Object key, int cnt, @Nullable Collection<N> inc)Gets specified count of adjacent nodes for a given key.List<N>nodes(@Nullable Object key, int cnt, @Nullable Collection<N> inc, @Nullable Collection<N> exc)Gets specified count of adjacent nodes for a given key.List<N>nodes(@Nullable Object key, int cnt, @Nullable IgnitePredicate<N>... p)Gets specified count of adjacent nodes for a given key.Nrandom()Picks a random node from consistent hash.booleanremoveNode(N node)Removes a node and all of its replicas.voidremoveNodes(@Nullable Collection<N> nodes)Removes given nodes and all their replicas from consistent hash algorithm (if nodes arenullor empty, then no-op).intsize()Gets size of all nodes (including replicas) in consistent hash.StringtoString()
-
-
-
Constructor Detail
-
GridConsistentHash
public GridConsistentHash(@Nullable @Nullable Object affSeed)Constructs consistent hash using given affinity seed andMD5hasher function.- Parameters:
affSeed- Affinity seed (will be used as key prefix for hashing).
-
GridConsistentHash
public GridConsistentHash()
Constructs consistent hash using default parameters.
-
GridConsistentHash
public GridConsistentHash(@Nullable @Nullable Comparator<N> nodesComp, @Nullable @Nullable Object affSeed)Constructs consistent hash using given affinity seed and hasher function.- Parameters:
nodesComp- Nodes comparator to resolve hash codes collisions.affSeed- Affinity seed (will be used as key prefix for hashing).
-
-
Method Detail
-
addNodes
public void addNodes(@Nullable @Nullable Collection<N> nodes, int replicas)Adds nodes to consistent hash algorithm (if nodes arenullor empty, then no-op).- Parameters:
nodes- Nodes to add.replicas- Number of replicas for every node.
-
addNode
public boolean addNode(@Nullable N node, int replicas)Adds a node to consistent hash algorithm.- Parameters:
node- New node (ifnullthen no-op).replicas- Number of replicas for the node.- Returns:
Trueif node was added,falseif it isnullor is already contained in the hash.
-
removeNodes
public void removeNodes(@Nullable @Nullable Collection<N> nodes)Removes given nodes and all their replicas from consistent hash algorithm (if nodes arenullor empty, then no-op).- Parameters:
nodes- Nodes to remove.
-
removeNode
public boolean removeNode(@Nullable N node)Removes a node and all of its replicas.- Parameters:
node- Node to remove (ifnull, then no-op).- Returns:
Trueif node was removed,falseif node isnullor not present in hash.
-
clear
public void clear()
Clears all nodes from consistent hash.
-
count
public int count()
Gets number of distinct nodes, excluding replicas, in consistent hash.- Returns:
- Number of distinct nodes, excluding replicas, in consistent hash.
-
size
public int size()
Gets size of all nodes (including replicas) in consistent hash.- Returns:
- Size of all nodes (including replicas) in consistent hash.
-
isEmpty
public boolean isEmpty()
Checks if consistent hash has nodes added to it.- Returns:
Trueif consistent hash is empty,falseotherwise.
-
nodes
public Set<N> nodes()
Gets set of all distinct nodes in the consistent hash (in no particular order).- Returns:
- Set of all distinct nodes in the consistent hash.
-
random
@Nullable public N random()
Picks a random node from consistent hash.- Returns:
- Random node from consistent hash or
nullif there are no nodes.
-
node
@Nullable public N node(@Nullable @Nullable Object key)
Gets node for a key.- Parameters:
key- Key.- Returns:
- Node.
-
node
@Nullable public N node(@Nullable @Nullable Object key, @Nullable @Nullable Collection<N> inc)
Gets node for a given key.- Parameters:
key- Key to get node for.inc- Optional inclusion set. Only nodes contained in this set may be returned. Ifnull, then all nodes may be included.- Returns:
- Node for key, or
nullif node was not found.
-
node
@Nullable public N node(@Nullable @Nullable Object key, @Nullable @Nullable Collection<N> inc, @Nullable @Nullable Collection<N> exc)
Gets node for a given key.- Parameters:
key- Key to get node for.inc- Optional inclusion set. Only nodes contained in this set may be returned. Ifnull, then all nodes may be included.exc- Optional exclusion set. Only nodes not contained in this set may be returned. Ifnull, then all nodes may be returned.- Returns:
- Node for key, or
nullif node was not found.
-
node
@Nullable public N node(@Nullable @Nullable Object key, @Nullable @Nullable IgnitePredicate<N>... p)
Gets node for a given key.- Parameters:
key- Key to get node for.p- Optional predicate for node filtering.- Returns:
- Node for key, or
nullif node was not found.
-
nodes
public List<N> nodes(@Nullable @Nullable Object key, int cnt)
Gets specified count of adjacent nodes for a given key. If number of nodes in consistent hash is less than specified count, then all nodes are returned.- Parameters:
key- Key to get adjacent nodes for.cnt- Number of adjacent nodes to get.- Returns:
- List containing adjacent nodes for given key.
-
nodes
public List<N> nodes(@Nullable @Nullable Object key, int cnt, @Nullable @Nullable Collection<N> inc)
Gets specified count of adjacent nodes for a given key. If number of nodes in consistent hash is less than specified count, then all nodes are returned.- Parameters:
key- Key to get adjacent nodes for.cnt- Number of adjacent nodes to get.inc- Optional inclusion set. Only nodes contained in this set may be returned. Ifnull, then all nodes may be returned.- Returns:
- List containing adjacent nodes for given key.
-
nodes
public List<N> nodes(@Nullable @Nullable Object key, int cnt, @Nullable @Nullable Collection<N> inc, @Nullable @Nullable Collection<N> exc)
Gets specified count of adjacent nodes for a given key. If number of nodes in consistent hash is less than specified count, then all nodes are returned.- Parameters:
key- Key to get adjacent nodes for.cnt- Number of adjacent nodes to get.inc- Optional inclusion set. Only nodes contained in this set may be returned. Ifnull, then all nodes may be included.exc- Optional exclusion set. Only nodes not contained in this set may be returned. Ifnull, then all nodes may be returned.- Returns:
- List containing adjacent nodes for given key.
-
nodes
public List<N> nodes(@Nullable @Nullable Object key, int cnt, @Nullable @Nullable IgnitePredicate<N>... p)
Gets specified count of adjacent nodes for a given key. If number of nodes in consistent hash is less than specified count, then all nodes are returned.- Parameters:
key- Key to get adjacent nodes for.cnt- Number of adjacent nodes to get.p- Optional predicate to filter out nodes. Nodes that don't pass the filter will be skipped.- Returns:
- List containing adjacent nodes for given key.
-
belongs
public boolean belongs(@Nullable @Nullable Object key, N node)Checks if key belongs to the given node.- Parameters:
key- Key to check.node- Node to check.- Returns:
Trueif key belongs to given node.
-
belongs
public boolean belongs(@Nullable @Nullable Object key, @Nullable @Nullable Collection<N> nodes)Checks if given key belongs to any of the given nodes.- Parameters:
key- Key to check.nodes- Nodes to check (ifnullthenfalseis returned).- Returns:
Trueif key belongs to any of the given nodes.
-
belongs
public boolean belongs(@Nullable @Nullable Object key, int cnt, N node)Checks if node the key is mapped to or the given count of its adjacent nodes contain the node passed in.- Parameters:
key- Key to check.cnt- Number of adjacent nodes to check.node- Node to check.- Returns:
Trueif node the key is mapped to or given count of its adjacent neighbors contain the node.
-
belongs
public boolean belongs(@Nullable @Nullable Object key, int cnt, @Nullable @Nullable Collection<N> nodes)Checks if node the key is mapped to or the given count of its adjacent nodes are contained in given set of nodes.- Parameters:
key- Key to check.cnt- Number of adjacent nodes to check.nodes- Nodes to check.- Returns:
Trueif node the key is mapped to or given count of its adjacent neighbors are contained in given set of nodes.
-
hash
protected int hash(Object o)
Gets hash code for a given object.- Parameters:
o- Object to get hash code for.- Returns:
- Hash code.
-
-