Class RendezvousAffinityFunction
- java.lang.Object
-
- org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction
-
- All Implemented Interfaces:
Serializable
,AffinityFunction
public class RendezvousAffinityFunction extends Object implements AffinityFunction, Serializable
Affinity function for partitioned cache based on Highest Random Weight algorithm. This function supports the following configuration:-
partitions
- Number of partitions to spread across nodes. -
excludeNeighbors
- If set totrue
, will exclude same-host-neighbors from being backups of each other. This flag can be ignored in cases when topology has no enough nodes for assign backups. Note thatbackupFilter
is ignored ifexcludeNeighbors
is set totrue
. -
backupFilter
- Optional filter for back up nodes. If provided, then only nodes that pass this filter will be selected as backup nodes. If not provided, then primary and backup nodes will be selected out of all nodes available for this cache.
Cache affinity can be configured for individual caches via
CacheConfiguration.getAffinity()
method.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static int
DFLT_PARTITION_COUNT
Default number of partitions.
-
Constructor Summary
Constructors Constructor Description RendezvousAffinityFunction()
Empty constructor with all defaults.RendezvousAffinityFunction(boolean exclNeighbors)
Initializes affinity with flag to exclude same-host-neighbors from being backups of each other and specified number of backups.RendezvousAffinityFunction(boolean exclNeighbors, int parts)
Initializes affinity with flag to exclude same-host-neighbors from being backups of each other, and specified number of backups and partitions.RendezvousAffinityFunction(int parts, @Nullable IgniteBiPredicate<ClusterNode,ClusterNode> backupFilter)
Initializes optional counts for replicas and backups.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description List<ClusterNode>
assignPartition(int part, List<ClusterNode> nodes, int backups, @Nullable Map<UUID,Collection<ClusterNode>> neighborhoodCache)
Returns collection of nodes (primary first) for specified partition.List<List<ClusterNode>>
assignPartitions(AffinityFunctionContext affCtx)
Gets affinity nodes for a partition.static int
calculateMask(int parts)
Helper method to calculates mask.static int
calculatePartition(Object key, int mask, int parts)
Helper method to calculate partition.@Nullable IgniteBiPredicate<ClusterNode,List<ClusterNode>>
getAffinityBackupFilter()
Gets optional backup filter.@Nullable IgniteBiPredicate<ClusterNode,ClusterNode>
getBackupFilter()
Gets optional backup filter.int
getPartitions()
Gets total number of key partitions.boolean
isExcludeNeighbors()
Checks flag to exclude same-host-neighbors from being backups of each other (default isfalse
).int
partition(Object key)
Gets partition number for a given key starting from0
.int
partitions()
Gets total number of partitions available.void
removeNode(UUID nodeId)
Removes node from affinity.void
reset()
Resets cache affinity to its initial state.Object
resolveNodeHash(ClusterNode node)
Resolves node hash.RendezvousAffinityFunction
setAffinityBackupFilter(@Nullable IgniteBiPredicate<ClusterNode,List<ClusterNode>> affinityBackupFilter)
Sets optional backup filter.RendezvousAffinityFunction
setBackupFilter(@Nullable IgniteBiPredicate<ClusterNode,ClusterNode> backupFilter)
Deprecated.UseaffinityBackupFilter
instead.RendezvousAffinityFunction
setExcludeNeighbors(boolean exclNeighbors)
Sets flag to exclude same-host-neighbors from being backups of each other (default isfalse
).RendezvousAffinityFunction
setPartitions(int parts)
Sets total number of partitions.If the number of partitions is a power of two, the PowerOfTwo hashing method will be used.String
toString()
-
-
-
Field Detail
-
DFLT_PARTITION_COUNT
public static final int DFLT_PARTITION_COUNT
Default number of partitions.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
RendezvousAffinityFunction
public RendezvousAffinityFunction()
Empty constructor with all defaults.
-
RendezvousAffinityFunction
public RendezvousAffinityFunction(boolean exclNeighbors)
Initializes affinity with flag to exclude same-host-neighbors from being backups of each other and specified number of backups.Note that
backupFilter
is ignored ifexcludeNeighbors
is set totrue
.- Parameters:
exclNeighbors
-True
if nodes residing on the same host may not act as backups of each other.
-
RendezvousAffinityFunction
public RendezvousAffinityFunction(boolean exclNeighbors, int parts)
Initializes affinity with flag to exclude same-host-neighbors from being backups of each other, and specified number of backups and partitions.Note that
backupFilter
is ignored ifexcludeNeighbors
is set totrue
.- Parameters:
exclNeighbors
-True
if nodes residing on the same host may not act as backups of each other.parts
- Total number of partitions.
-
RendezvousAffinityFunction
public RendezvousAffinityFunction(int parts, @Nullable @Nullable IgniteBiPredicate<ClusterNode,ClusterNode> backupFilter)
Initializes optional counts for replicas and backups.Note that
backupFilter
is ignored ifexcludeNeighbors
is set totrue
.- Parameters:
parts
- Total number of partitions.backupFilter
- Optional back up filter for nodes. If provided, backups will be selected from all nodes that pass this filter. First argument for this filter is primary node, and second argument is node being tested.Note that
backupFilter
is ignored ifexcludeNeighbors
is set totrue
.
-
-
Method Detail
-
calculateMask
public static int calculateMask(int parts)
Helper method to calculates mask.- Parameters:
parts
- Number of partitions.- Returns:
- Mask to use in calculation when partitions count is power of 2.
-
calculatePartition
public static int calculatePartition(Object key, int mask, int parts)
Helper method to calculate partition.- Parameters:
key
- – Key to get partition for.mask
- Mask to use in calculation when partitions count is power of 2.parts
- Number of partitions.- Returns:
- Partition number for a given key.
-
getPartitions
public int getPartitions()
Gets total number of key partitions. To ensure that all partitions are equally distributed across all nodes, please make sure that this number is significantly larger than a number of nodes. Also, partition size should be relatively small. Try to avoid having partitions with more than quarter million keys.For fully replicated caches this method works the same way as a partitioned cache.
- Returns:
- Total partition count.
-
setPartitions
public RendezvousAffinityFunction setPartitions(int parts)
Sets total number of partitions.If the number of partitions is a power of two, the PowerOfTwo hashing method will be used. Otherwise the Standard hashing method will be applied.- Parameters:
parts
- Total number of partitions.- Returns:
this
for chaining.
-
getBackupFilter
@Nullable public @Nullable IgniteBiPredicate<ClusterNode,ClusterNode> getBackupFilter()
Gets optional backup filter. If notnull
, backups will be selected from all nodes that pass this filter. First node passed to this filter is primary node, and second node is a node being tested.Note that
backupFilter
is ignored ifexcludeNeighbors
is set totrue
.- Returns:
- Optional backup filter.
-
setBackupFilter
@Deprecated public RendezvousAffinityFunction setBackupFilter(@Nullable @Nullable IgniteBiPredicate<ClusterNode,ClusterNode> backupFilter)
Deprecated.UseaffinityBackupFilter
instead.Sets optional backup filter. If provided, then backups will be selected from all nodes that pass this filter. First node being passed to this filter is primary node, and second node is a node being tested.Note that
backupFilter
is ignored ifexcludeNeighbors
is set totrue
.- Parameters:
backupFilter
- Optional backup filter.- Returns:
this
for chaining.
-
getAffinityBackupFilter
@Nullable public @Nullable IgniteBiPredicate<ClusterNode,List<ClusterNode>> getAffinityBackupFilter()
Gets optional backup filter. If notnull
, backups will be selected from all nodes that pass this filter. First node passed to this filter is a node being tested, and the second parameter is a list of nodes that are already assigned for a given partition (primary node is the first in the list).Note that
affinityBackupFilter
is ignored ifexcludeNeighbors
is set totrue
.- Returns:
- Optional backup filter.
-
setAffinityBackupFilter
public RendezvousAffinityFunction setAffinityBackupFilter(@Nullable @Nullable IgniteBiPredicate<ClusterNode,List<ClusterNode>> affinityBackupFilter)
Sets optional backup filter. If provided, then backups will be selected from all nodes that pass this filter. First node being passed to this filter is a node being tested, and the second parameter is a list of nodes that are already assigned for a given partition (primary node is the first in the list).Note that
affinityBackupFilter
is ignored ifexcludeNeighbors
is set totrue
.For an example filter, see
ClusterNodeAttributeAffinityBackupFilter
.- Parameters:
affinityBackupFilter
- Optional backup filter.- Returns:
this
for chaining.
-
isExcludeNeighbors
public boolean isExcludeNeighbors()
Checks flag to exclude same-host-neighbors from being backups of each other (default isfalse
).Note that
backupFilter
is ignored ifexcludeNeighbors
is set totrue
.- Returns:
True
if nodes residing on the same host may not act as backups of each other.
-
setExcludeNeighbors
public RendezvousAffinityFunction setExcludeNeighbors(boolean exclNeighbors)
Sets flag to exclude same-host-neighbors from being backups of each other (default isfalse
).Note that
backupFilter
is ignored ifexcludeNeighbors
is set totrue
.- Parameters:
exclNeighbors
-True
if nodes residing on the same host may not act as backups of each other.- Returns:
this
for chaining.
-
resolveNodeHash
public Object resolveNodeHash(ClusterNode node)
Resolves node hash.- Parameters:
node
- Cluster node;- Returns:
- Node hash.
-
assignPartition
public List<ClusterNode> assignPartition(int part, List<ClusterNode> nodes, int backups, @Nullable @Nullable Map<UUID,Collection<ClusterNode>> neighborhoodCache)
Returns collection of nodes (primary first) for specified partition.- Parameters:
part
- Partition.nodes
- Nodes.backups
- Number of backups.neighborhoodCache
- Neighborhood.- Returns:
- Assignment.
-
reset
public void reset()
Resets cache affinity to its initial state. This method will be called by the system any time the affinity has been sent to remote node where it has to be reinitialized. If your implementation of affinity function has no initialization logic, leave this method empty.- Specified by:
reset
in interfaceAffinityFunction
-
partitions
public int partitions()
Gets total number of partitions available. All caches should always provide correct partition count which should be the same on all participating nodes. Note that partitions should always be numbered from0
inclusively toN
exclusively without any gaps.- Specified by:
partitions
in interfaceAffinityFunction
- Returns:
- Total partition count.
-
partition
public int partition(Object key)
Gets partition number for a given key starting from0
. Partitioned caches should make sure that keys are about evenly distributed across all partitions from0
topartition count
for best performance.Note that for fully replicated caches it is possible to segment key sets among different grid node groups. In that case each node group should return a unique partition number. However, unlike partitioned cache, mappings of keys to nodes in replicated caches are constant and a node cannot migrate from one partition to another.
- Specified by:
partition
in interfaceAffinityFunction
- Parameters:
key
- Key to get partition for.- Returns:
- Partition number for a given key.
-
assignPartitions
public List<List<ClusterNode>> assignPartitions(AffinityFunctionContext affCtx)
Gets affinity nodes for a partition. In case of replicated cache, all returned nodes are updated in the same manner. In case of partitioned cache, the returned list should contain only the primary and back up nodes with primary node being always first.Note that partitioned affinity must obey the following contract: given that node
N
is primary for some keyK
, if any other node(s) leave grid and no node joins grid, nodeN
will remain primary for keyK
.- Specified by:
assignPartitions
in interfaceAffinityFunction
- Parameters:
affCtx
- Affinity function context. Will provide all required information to calculate new partition assignments.- Returns:
- Unmodifiable list indexed by partition number. Each element of array is a collection in which first node is a primary node and other nodes are backup nodes.
-
removeNode
public void removeNode(UUID nodeId)
Removes node from affinity. This method is called when it is safe to remove left node from affinity mapping.- Specified by:
removeNode
in interfaceAffinityFunction
- Parameters:
nodeId
- ID of node to remove.
-
-