Class ConsistentHash
Member that should handle a command.
Each member claims a number of positions on the ring proportional to its
load factor. Resolving a routing key hashes it onto the ring and walks
clockwise until a member is found that handles the command
in question. The same routing key therefore always resolves to the same member for as long as the memberships do
not change, which is what keeps commands for one entity on one node.
Instances are immutable: with(Member, MemberCapabilities) and without(Member) return a new ring
rather than mutating this one, so a ring can be published to readers without further synchronisation. Every change
increases the version().
A member with a load factor of 0 — including one whose capabilities could not be retrieved, reported as
MemberCapabilities.INCAPABLE — claims no positions and is never resolved to, but remains part of
members().
- Since:
- 5.4.0
- Author:
- Allard Buijze
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs an emptyConsistentHashhashing routing keys and ring positions with an MD5 digest.ConsistentHash(UnaryOperator<String> hashFunction) Constructs an emptyConsistentHashusing the givenhashFunctionto hash both routing keys and the ring positions claimed by each member. -
Method Summary
Modifier and TypeMethodDescriptioncapabilitiesOf(Member member) Returns theMemberCapabilitiesthe givenmemberis registered under.booleaninthashCode()memberFor(String routingKey, QualifiedName commandName) Resolves theMemberthat should handle a command of the givencommandNamecarrying the givenroutingKey.members()Returns everyMemberregistered with this ring, including those claiming no ring positions.queryHandlers(QualifiedName queryName) toString()intversion()Returns the version of this ring, increased by one on every change in memberships.with(Member member, MemberCapabilities capabilities) Returns aConsistentHashwith the givenmemberregistered under the givencapabilities, replacing any earlier registration of a member with the samename.withOnly(Map<Member, MemberCapabilities> memberships) Returns aConsistentHashholding exactly the givenmemberships, hashed with this ring's hash function.Returns aConsistentHashwithout the givenmember.
-
Constructor Details
-
ConsistentHash
public ConsistentHash()Constructs an emptyConsistentHashhashing routing keys and ring positions with an MD5 digest.Register members through
with(Member, MemberCapabilities). -
ConsistentHash
Constructs an emptyConsistentHashusing the givenhashFunctionto hash both routing keys and the ring positions claimed by each member.Every member of the cluster must use the same hash function, or they will disagree on where commands belong. Register members through
with(Member, MemberCapabilities).- Parameters:
hashFunction- the function hashing a routing key, or a member's ring position, onto the ring. Must be deterministic, and must order its output consistently for all members of the cluster.
-
-
Method Details
-
withOnly
Returns aConsistentHashholding exactly the givenmemberships, hashed with this ring's hash function.Intended for rebuilding a ring from scratch, as a discovery round does. Registering members one by one through
with(Member, MemberCapabilities)would recompute every member's ring positions on each call, making a rebuild quadratic in the number of members; this assigns the positions once.A member this ring already holds under the same capabilities keeps the positions it has, rather than having them computed again, which makes rebuilding a ring in which little changed cheap.
Members this ring holds that are absent from
membershipsare not carried over — the result describes the cluster asmembershipsdescribes it, which is what makes it a rebuild rather than an update.- Parameters:
memberships- the members to register, and the messages each of them handles- Returns:
- a
ConsistentHashholding exactly the givenmemberships, orthiswhen it already holds exactly those memberships
-
with
Returns aConsistentHashwith the givenmemberregistered under the givencapabilities, replacing any earlier registration of a member with the samename.- Parameters:
member- the member to registercapabilities- the messages the givenmemberhandles, and the command load it asks for- Returns:
- a
ConsistentHashincluding the givenmember, orthiswhen the givenmemberwas already registered with identicalcapabilities
-
without
Returns aConsistentHashwithout the givenmember.- Parameters:
member- the member to deregister- Returns:
- a
ConsistentHashexcluding the givenmember, orthiswhen the givenmemberwas not registered
-
memberFor
Resolves theMemberthat should handle a command of the givencommandNamecarrying the givenroutingKey.- Parameters:
routingKey- the routing key of the command to resolve a member forcommandName- theQualifiedNameof the command to resolve a member for. Only members that subscribed to this name are considered.- Returns:
- the member that should handle the command, or
Optional.empty()when no registered member handles commands of the givencommandName
-
queryHandlers
Returns everyMemberregistered with this ring thathandlesqueries of the givenqueryName, ordered byname.The order is stable for as long as the memberships do not change, so that a caller rotating over the result rotates over the same sequence on every call. The result is computed once per query name and ring.
- Parameters:
queryName- theQualifiedNameof the query to resolve the handling members of- Returns:
- every registered member handling the given
queryName, empty when none does
-
members
Returns everyMemberregistered with this ring, including those claiming no ring positions.- Returns:
- every registered member, in no particular order
-
capabilitiesOf
Returns theMemberCapabilitiesthe givenmemberis registered under.- Parameters:
member- the member to look up the capabilities of- Returns:
- the capabilities of the given
member, orOptional.empty()when it is not registered
-
version
public int version()Returns the version of this ring, increased by one on every change in memberships.Two rings with the same version are not necessarily equal — the version counts changes made along one lineage of rings, and says nothing about rings built independently.
- Returns:
- the number of changes made to reach this ring
-
equals
-
hashCode
public int hashCode() -
toString
-