Class SyncConsistentHashFactory
- All Implemented Interfaces:
ConsistentHashFactory<DefaultConsistentHash>
- Direct Known Subclasses:
TopologyAwareSyncConsistentHashFactory
ConsistentHashFactory implementation
that guarantees that multiple caches with the same members will
have the same consistent hash (unlike DefaultConsistentHashFactory).
It has a drawback compared to DefaultConsistentHashFactory though:
it can potentially move a lot more segments during a rebalance than
strictly necessary.
E.g. {0:AB, 1:BA, 2:CD, 3:DA} could turn into {0:BC, 1:CA, 2:CB, 3:AB} when D leaves,
even though {0:AB, 1:BA, 2:CB, 3:AC} would require fewer segment ownership changes.
It may also reorder the owners of a segments, e.g. AB -> BA (same as DefaultConsistentHashFactory).
- Since:
- 5.2
- Author:
- Dan Berindei
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionCreate a new consistent hash instance.fromPersistentState(ScopedPersistentState state, Function<UUID, Address> addressMapper) Recreates aConsistentHashfrom a previously stored persistent state.static SyncConsistentHashFactoryrebalance(DefaultConsistentHash baseCH) Create a new consistent hash instance, based on an existing instance, but balanced according to the implementation's rules.union(DefaultConsistentHash ch1, DefaultConsistentHash ch2) Creates a union of two compatible ConsistentHashes (use the same hashing function and have the same configuration parameters).updateMembers(DefaultConsistentHash baseCH, List<Address> newMembers, Map<Address, Float> actualCapacityFactors) Updates an existing consistent hash instance to remove owners that are not in thenewMemberslist.
-
Constructor Details
-
SyncConsistentHashFactory
protected SyncConsistentHashFactory()
-
-
Method Details
-
getInstance
-
create
public DefaultConsistentHash create(int numOwners, int numSegments, List<Address> members, Map<Address, Float> capacityFactors) Description copied from interface:ConsistentHashFactoryCreate a new consistent hash instance. The consistent hash will be balanced.- Specified by:
createin interfaceConsistentHashFactory<DefaultConsistentHash>- Parameters:
numOwners- The ideal number of owners for each key. The created consistent hash can have more or less owners, but each key will have at least one owner.numSegments- Number of hash-space segments. The implementation may round up the number of segments for performance, or may ignore the parameter altogether.members- A list of addresses representing the new cache members.capacityFactors- The capacity factor of each member. Determines the relative capacity of each node compared to the others. The implementation may ignore this parameter. Ifnull, all the members are assumed to have a capacity factor of 1.
-
fromPersistentState
public PersistedConsistentHash<DefaultConsistentHash> fromPersistentState(ScopedPersistentState state, Function<UUID, Address> addressMapper) Description copied from interface:ConsistentHashFactoryRecreates aConsistentHashfrom a previously stored persistent state.The stored state typically contains a collection of
UUIDs representing the members. If a member is not present when reading from the state, itsUUIDmust be added to thePersistedConsistentHash.missingUuids()list. ThePersistedConsistentHash.consistentHash()may have incomplete ownership when there are missing UUIDs.- Specified by:
fromPersistentStatein interfaceConsistentHashFactory<DefaultConsistentHash>- Parameters:
state- the state to restore, containing memberUUIDs.addressMapper- A function to map theUUIDtoAddress.- Returns:
- A
PersistedConsistentHashwith theConsistentHashand with the missingUUIDif any.
-
updateMembers
public DefaultConsistentHash updateMembers(DefaultConsistentHash baseCH, List<Address> newMembers, Map<Address, Float> actualCapacityFactors) Description copied from interface:ConsistentHashFactoryUpdates an existing consistent hash instance to remove owners that are not in thenewMemberslist.If a segment has at least one owner in
newMembers, this method will not add another owner. This guarantees that the new consistent hash can be used immediately, without transferring any state.If a segment has no owners in
newMembersand theConsistentHashimplementation (e.g.DefaultConsistentHash) requires at least one owner for each segment, this method may add one or more owners for that segment. Since the data in that segment was lost, the new consistent hash can still be used without transferring state.- Specified by:
updateMembersin interfaceConsistentHashFactory<DefaultConsistentHash>- Parameters:
baseCH- An existing consistent hash instance, should not benullnewMembers- A list of addresses representing the new cache members.actualCapacityFactors- The capacity factor of each member. Determines the relative capacity of each node compared to the others. The implementation may ignore this parameter. Ifnull, all the members are assumed to have a capacity factor of 1.- Returns:
- A new
ConsistentHashinstance, orbaseCHif the existing instance does not need any changes.
-
rebalance
Description copied from interface:ConsistentHashFactoryCreate a new consistent hash instance, based on an existing instance, but balanced according to the implementation's rules.- Specified by:
rebalancein interfaceConsistentHashFactory<DefaultConsistentHash>- Parameters:
baseCH- An existing consistent hash instance, should not benull- Returns:
- A new
ConsistentHashinstance, orbaseCHif the existing instance does not need any changes.
-
union
Description copied from interface:ConsistentHashFactoryCreates a union of two compatible ConsistentHashes (use the same hashing function and have the same configuration parameters).The owners of a segment
sinunion(ch1, ch2)will include both the owners ofsinch1and the owners ofsinch2, so a cache can switch from usingunion(ch1, ch2)to usingch2without transferring any state.- Specified by:
unionin interfaceConsistentHashFactory<DefaultConsistentHash>
-