public class ConsistentHash extends Object
A ConsistentHash is used to route commands targeting the same aggregate to the same member. In that case the aggregate identifier is the routing key.
Modifier and Type | Class and Description |
---|---|
static class |
ConsistentHash.ConsistentHashMember
Member implementation used by a
ConsistentHash registry. |
Constructor and Description |
---|
ConsistentHash()
Initializes a new
ConsistentHash . |
ConsistentHash(Function<String,String> hashFunction)
Initializes a new
ConsistentHash using the given hashFunction to calculate positions for each
member on the ring. |
Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object o) |
Collection<ConsistentHash.ConsistentHashMember> |
getEligibleMembers(String routingKey)
Returns the collection of nodes, represented as
ConsistentHash.ConsistentHashMember , in the order they would be
considered for the given routing key. |
Optional<Member> |
getMember(String routingKey,
CommandMessage<?> commandMessage)
Returns the member instance to which the given
message should be routed. |
Set<Member> |
getMembers()
Returns the set of members registered with this consistent hash instance.
|
protected static String |
hash(String routingKey)
Returns the hash of the given
routingKey . |
int |
hashCode() |
String |
toString() |
int |
version()
Returns the version of this consistent hash instance.
|
ConsistentHash |
with(Member member,
int loadFactor,
CommandMessageFilter commandFilter)
Registers the given
member with given loadFactor and commandFilter if it is not
already contained in the ConsistentHash . |
ConsistentHash |
without(Member member)
Deregisters the given
member and returns a new ConsistentHash with updated memberships. |
public ConsistentHash()
ConsistentHash
. To register members use with(Member, int, CommandMessageFilter)
.public ConsistentHash(Function<String,String> hashFunction)
ConsistentHash
using the given hashFunction
to calculate positions for each
member on the ring. To register members use with(Member, int, CommandMessageFilter)
.hashFunction
- The hash function to use to calculate each member's positions on the ringprotected static String hash(String routingKey)
routingKey
. By default this creates a MD5 hash with hex encoding.routingKey
- the routing key to hashpublic Collection<ConsistentHash.ConsistentHashMember> getEligibleMembers(String routingKey)
ConsistentHash.ConsistentHashMember
, in the order they would be
considered for the given routing key. Whether a CommandMessage would be forwarded to each of the candidates,
depends on the Command Filter of each node.routingKey
- The routing key to select orderingpublic Optional<Member> getMember(String routingKey, CommandMessage<?> commandMessage)
message
should be routed. If no suitable member could be
found an empty Optional is returned.routingKey
- the routing that should be used to select a membercommandMessage
- the command message to find a member forpublic Set<Member> getMembers()
public ConsistentHash with(Member member, int loadFactor, CommandMessageFilter commandFilter)
member
with given loadFactor
and commandFilter
if it is not
already contained in the ConsistentHash
. It will return the current ConsistentHash if the addition is
a duplicate and returns a new ConsistentHash with updated memberships if it is not.
The relative loadFactor of the member determines the likelihood of being selected as a destination for a command.
member
- the member to registerloadFactor
- the load factor of the new membercommandFilter
- filter describing which commands can be handled by the given memberConsistentHash
instance with updated membershipspublic ConsistentHash without(Member member)
member
and returns a new ConsistentHash
with updated memberships.member
- the member to remove from the consistent hashConsistentHash
instance with updated membershipspublic int version()
Copyright © 2010–2022. All rights reserved.