Class GridClientConsistentHash<N>
- java.lang.Object
-
- org.apache.ignite.internal.client.util.GridClientConsistentHash<N>
-
public class GridClientConsistentHash<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 GridClientConsistentHash()Constructs consistent hash using empty affinity seed andMD5hasher function.GridClientConsistentHash(Object affSeed)Constructs consistent hash using given affinity seed andMD5hasher function.GridClientConsistentHash(Comparator<N> nodesComp, Object affSeed)Constructs consistent hash using given affinity seed and hasher function.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanaddNode(N node, int replicas)Adds a node to consistent hash algorithm.voidaddNodes(Collection<N> nodes, int replicas)Adds nodes to consistent hash algorithm (if nodes arenullor empty, then no-op).intcount()Gets number of distinct nodes, excluding replicas, in consistent hash.static inthash(Object o)Gets hash code for a given object.booleanisEmpty()Checks if consistent hash has nodes added to it.Nnode(Object key)Gets node for a key.Nnode(Object key, @Nullable Collection<N> inc, @Nullable Collection<N> exc)Gets node for a given key.Nnode(Object key, Collection<N> inc)Gets node for a given key.Nnode(Object key, GridClientPredicate<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).Nrandom()Picks a random node from consistent hash.booleanremoveNode(N node)Removes a node and all of its replicas.intsize()Gets size of all nodes (including replicas) in consistent hash.StringtoString()
-
-
-
Constructor Detail
-
GridClientConsistentHash
public GridClientConsistentHash()
Constructs consistent hash using empty affinity seed andMD5hasher function.
-
GridClientConsistentHash
public GridClientConsistentHash(Object affSeed)
Constructs consistent hash using given affinity seed andMD5hasher function.- Parameters:
affSeed- Affinity seed (will be used as key prefix for hashing).
-
GridClientConsistentHash
public GridClientConsistentHash(Comparator<N> nodesComp, Object affSeed)
Constructs consistent hash using given affinity seed and hasher function.- Parameters:
nodesComp- Nodes comparator to resolve hash codes collisions. Ifnullnatural order will be used.affSeed- Affinity seed (will be used as key prefix for hashing).
-
-
Method Detail
-
addNodes
public void addNodes(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(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.
-
removeNode
public boolean removeNode(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.
-
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
public N random()
Picks a random node from consistent hash.- Returns:
- Random node from consistent hash or
nullif there are no nodes.
-
node
public N node(Object key, 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
public N node(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
public N node(Object key, GridClientPredicate<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.
-
hash
public static int hash(Object o)
Gets hash code for a given object.- Parameters:
o- Object to get hash code for.- Returns:
- Hash code.
-
-