org.axonframework.commandhandling.distributed
Class ConsistentHash

java.lang.Object
  extended by org.axonframework.commandhandling.distributed.ConsistentHash
All Implemented Interfaces:
Externalizable, Serializable

public class ConsistentHash
extends Object
implements Externalizable

Basic implementation of a Consistent Hashing algorithm, using the MD5 algorithm to build the hash values for given keys and node names. It contains some basic operation to add nodes and remove nodes given a set of known remaining members.

Each node contains a Set of supported Commands (as a set of the fully qualified names of payload types). When performing a lookup for a given command, only nodes that support the payload type of the command are eligible.

Since:
2.0
Author:
Allard Buijze
See Also:
Serialized Form

Nested Class Summary
static class ConsistentHash.Member
          Represents a member in a consistently hashed cluster.
 
Constructor Summary
ConsistentHash()
          Initializes an empty hash.
 
Method Summary
static ConsistentHash emptyRing()
          Returns an instance of an empty Ring, which can be used to add members.
 boolean equals(Object o)
           
 String getMember(String item, String commandType)
          Returns the member for the given item, that supports given commandType.
 Set<ConsistentHash.Member> getMembers()
          Returns the set of members part of this hash ring.
 int hashCode()
           
 void readExternal(ObjectInput in)
           
 String toString()
           
 ConsistentHash withAdditionalNode(String nodeName, int segmentCount, Set<String> supportedCommandTypes)
          Returns a ConsistentHash with the given additional nodeName, which is given segmentCount segments on the ring.
 ConsistentHash withExclusively(Collection<String> nodes)
          Returns a ConsistentHash instance where only segments leading to the given nodes are available.
 void writeExternal(ObjectOutput out)
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ConsistentHash

public ConsistentHash()
Initializes an empty hash.

This constructor is required for serialization. Instead of using this constructor, use emptyRing() to obtain an instance.

Method Detail

emptyRing

public static ConsistentHash emptyRing()
Returns an instance of an empty Ring, which can be used to add members.

Returns:
an empty ConsistentHash ring.

withAdditionalNode

public ConsistentHash withAdditionalNode(String nodeName,
                                         int segmentCount,
                                         Set<String> supportedCommandTypes)
Returns a ConsistentHash with the given additional nodeName, which is given segmentCount segments on the ring. A registration of a node will completely override any previous registration known for that node.

Parameters:
nodeName - The name of the node to add. This will be used to compute the segments
segmentCount - The number of segments to add the given node
supportedCommandTypes - The fully qualified names of command (payload) types this node supports
Returns:
a ConsistentHash with the given additional node

withExclusively

public ConsistentHash withExclusively(Collection<String> nodes)
Returns a ConsistentHash instance where only segments leading to the given nodes are available. Each lookup will always result in one of the given nodes.

Parameters:
nodes - The nodes to keep in the consistent hash
Returns:
a ConsistentHash instance where only segments leading to the given nodes are available

getMember

public String getMember(String item,
                        String commandType)
Returns the member for the given item, that supports given commandType. If no such member is available, this method returns null.

Parameters:
item - The item to find a node name for
commandType - The type of command the member must support
Returns:
The node name for the given item, or null if not found

equals

public boolean equals(Object o)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

toString

public String toString()
Overrides:
toString in class Object

writeExternal

public void writeExternal(ObjectOutput out)
                   throws IOException
Specified by:
writeExternal in interface Externalizable
Throws:
IOException

readExternal

public void readExternal(ObjectInput in)
                  throws IOException,
                         ClassNotFoundException
Specified by:
readExternal in interface Externalizable
Throws:
IOException
ClassNotFoundException

getMembers

public Set<ConsistentHash.Member> getMembers()
Returns the set of members part of this hash ring.

Returns:
the set of members part of this hash ring


Copyright © 2010-2016. All Rights Reserved.