org.apache.ignite.internal.processors.cache.affinity

## Class GridCacheAffinityImpl<K,V>

- java.lang.Object
- - org.apache.ignite.internal.processors.cache.affinity.GridCacheAffinityImpl<K,V>

All Implemented Interfaces:
  Affinity<K>

    public class GridCacheAffinityImpl<K,V>
    extends Object
    implements Affinity<K>

Affinity interface implementation.

### Field Summary

Fields
`static String` `FAILED_TO_FIND_CACHE_ERR_MSG`

### Constructor Summary

Constructors
Constructor and Description
`GridCacheAffinityImpl(GridCacheContext<K,V> cctx)`

### Method Summary

All Methods Instance Methods Concrete Methods
`Object` `affinityKey(K key)` Maps passed in key to a key which will be used for node affinity.
`int[]` `allPartitions(ClusterNode n)` Gets partition ids for which given cluster node has any ownership (either primary or backup).
`int[]` `backupPartitions(ClusterNode n)` Gets partition ids for which given cluster node has backup ownership.
`boolean` `isBackup(ClusterNode n, K key)` Returns `true` if local node is one of the backup nodes for given key.
`boolean` `isPrimary(ClusterNode n, K key)` Returns `true` if given node is the primary node for given key.
`boolean` `isPrimaryOrBackup(ClusterNode n, K key)` Returns `true` if local node is primary or one of the backup nodes This method is essentially equivalent to calling "`Affinity.isPrimary(ClusterNode, Object)` || `Affinity.isBackup(ClusterNode, Object)`)", however it is more efficient as it makes both checks at once.
`Map<ClusterNode,Collection<K>>` `mapKeysToNodes(Collection<? extends K> keys)` This method provides ability to detect which keys are mapped to which nodes.
`ClusterNode` `mapKeyToNode(K key)` This method provides ability to detect to which primary node the given key is mapped.
`Collection<ClusterNode>` `mapKeyToPrimaryAndBackups(K key)` Gets primary and backup nodes for the key.
`Map<Integer,ClusterNode>` `mapPartitionsToNodes(Collection<Integer> parts)` Gets primary nodes for the given partitions.
`ClusterNode` `mapPartitionToNode(int part)` Gets primary node for the given partition.
`Collection<ClusterNode>` `mapPartitionToPrimaryAndBackups(int part)` Gets primary and backup nodes for partition.
`int` `partition(K key)` Gets partition id for the given key.
`int` `partitions()` Gets number of partitions in cache according to configured affinity function.
`int[]` `primaryPartitions(ClusterNode n)` Gets partition ids for which the given cluster node has primary ownership.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Field Detail

#### FAILED_TO_FIND_CACHE_ERR_MSG

    public static final String FAILED_TO_FIND_CACHE_ERR_MSG

See Also:
  Constant Field Values

### Constructor Detail

#### GridCacheAffinityImpl

    public GridCacheAffinityImpl(GridCacheContext<K,V> cctx)

Parameters:
  `cctx` - Context.

### Method Detail

#### partitions

    public int partitions()

Gets number of partitions in cache according to configured affinity function.

Specified by:
  `partitions` in interface `Affinity<K>`
Returns:
  Number of cache partitions.
See Also:
  `AffinityFunction`, `CacheConfiguration.getAffinity()`, `CacheConfiguration.setAffinity(AffinityFunction)`

#### partition

    public int partition(K key)

Gets partition id for the given key.

Specified by:
  `partition` in interface `Affinity<K>`
Parameters:
  `key` - Key to get partition id for.
Returns:
  Partition id.
See Also:
  `AffinityFunction`, `CacheConfiguration.getAffinity()`, `CacheConfiguration.setAffinity(AffinityFunction)`

#### isPrimary

    public boolean isPrimary(ClusterNode n,
                             K key)

Returns `true` if given node is the primary node for given key.

Specified by:
  `isPrimary` in interface `Affinity<K>`
Parameters:
  `n` - Node to check.
  `key` - Key to check.
Returns:
  `True` if local node is the primary node for given key.

#### isBackup

    public boolean isBackup(ClusterNode n,
                            K key)

Returns `true` if local node is one of the backup nodes for given key.

Specified by:
  `isBackup` in interface `Affinity<K>`
Parameters:
  `n` - Node to check.
  `key` - Key to check.
Returns:
  `True` if local node is one of the backup nodes for given key.

#### isPrimaryOrBackup

    public boolean isPrimaryOrBackup(ClusterNode n,
                                     K key)

Returns `true` if local node is primary or one of the backup nodes

This method is essentially equivalent to calling "`Affinity.isPrimary(ClusterNode, Object)` || `Affinity.isBackup(ClusterNode, Object)`)", however it is more efficient as it makes both checks at once.

Specified by:
  `isPrimaryOrBackup` in interface `Affinity<K>`
Parameters:
  `n` - Node to check.
  `key` - Key to check.
Returns:
  `True` if local node is primary or backup for given key.

#### primaryPartitions

    public int[] primaryPartitions(ClusterNode n)

Gets partition ids for which the given cluster node has primary ownership.

Specified by:
  `primaryPartitions` in interface `Affinity<K>`
Parameters:
  `n` - Cluster node.
Returns:
  Partition ids for which given cluster node has primary ownership.
See Also:
  `AffinityFunction`, `CacheConfiguration.getAffinity()`, `CacheConfiguration.setAffinity(AffinityFunction)`

#### backupPartitions

    public int[] backupPartitions(ClusterNode n)

Gets partition ids for which given cluster node has backup ownership.

Specified by:
  `backupPartitions` in interface `Affinity<K>`
Parameters:
  `n` - Cluster node.
Returns:
  Partition ids for which given cluster node has backup ownership.
See Also:
  `AffinityFunction`, `CacheConfiguration.getAffinity()`, `CacheConfiguration.setAffinity(AffinityFunction)`

#### allPartitions

    public int[] allPartitions(ClusterNode n)

Gets partition ids for which given cluster node has any ownership (either primary or backup).

Specified by:
  `allPartitions` in interface `Affinity<K>`
Parameters:
  `n` - Cluster node.
Returns:
  Partition ids for which given cluster node has any ownership, primary or backup.
See Also:
  `AffinityFunction`, `CacheConfiguration.getAffinity()`, `CacheConfiguration.setAffinity(AffinityFunction)`

#### mapPartitionToNode

    public ClusterNode mapPartitionToNode(int part)

Gets primary node for the given partition.

Specified by:
  `mapPartitionToNode` in interface `Affinity<K>`
Parameters:
  `part` - Partition id.
Returns:
  Primary node for the given partition.
See Also:
  `AffinityFunction`, `CacheConfiguration.getAffinity()`, `CacheConfiguration.setAffinity(AffinityFunction)`

#### mapPartitionsToNodes

    public Map<Integer,ClusterNode> mapPartitionsToNodes(Collection<Integer> parts)

Gets primary nodes for the given partitions.

Specified by:
  `mapPartitionsToNodes` in interface `Affinity<K>`
Parameters:
  `parts` - Partition ids.
Returns:
  Mapping of given partitions to their primary nodes.
See Also:
  `AffinityFunction`, `CacheConfiguration.getAffinity()`, `CacheConfiguration.setAffinity(AffinityFunction)`

#### affinityKey

    public Object affinityKey(K key)

Maps passed in key to a key which will be used for node affinity. The affinity key may be different from actual key if some field in the actual key was designated for affinity mapping via `AffinityKeyMapped` annotation or if a custom `AffinityKeyMapper` was configured.

Specified by:
  `affinityKey` in interface `Affinity<K>`
Parameters:
  `key` - Key to map.
Returns:
  Key to be used for node-to-affinity mapping (may be the same key as passed in).

#### mapKeyToNode

    @Nullable
    public ClusterNode mapKeyToNode(K key)

This method provides ability to detect to which primary node the given key is mapped. Use it to determine which nodes are storing which keys prior to sending jobs that access these keys.

This method works as following:

- For local caches it returns only local node ID.
- For other caches, primary node for the given key is returned.

Specified by:
  `mapKeyToNode` in interface `Affinity<K>`
Parameters:
  `key` - Keys to map to a node.
Returns:
  Primary node for the key.

#### mapKeysToNodes

    public Map<ClusterNode,Collection<K>> mapKeysToNodes(@Nullable
                                                         Collection<? extends K> keys)

This method provides ability to detect which keys are mapped to which nodes. Use it to determine which nodes are storing which keys prior to sending jobs that access these keys.

This method works as following:

- For local caches it returns only local node mapped to all keys.
- For other caches, the returned map represents node-to-key affinity.

Specified by:
  `mapKeysToNodes` in interface `Affinity<K>`
Parameters:
  `keys` - Keys to map to nodes.
Returns:
  Map of nodes to keys.

#### mapKeyToPrimaryAndBackups

    public Collection<ClusterNode> mapKeyToPrimaryAndBackups(K key)

Gets primary and backup nodes for the key. Note that primary node is always first in the returned collection.

Specified by:
  `mapKeyToPrimaryAndBackups` in interface `Affinity<K>`
Parameters:
  `key` - Key to get affinity nodes for.
Returns:
  Collection of primary and backup nodes for the key with primary node always first.

#### mapPartitionToPrimaryAndBackups

    public Collection<ClusterNode> mapPartitionToPrimaryAndBackups(int part)

Gets primary and backup nodes for partition. Note that primary node is always first in the returned collection.

Specified by:
  `mapPartitionToPrimaryAndBackups` in interface `Affinity<K>`
Parameters:
  `part` - Partition to get affinity nodes for.
Returns:
  Collection of primary and backup nodes for partition with primary node always first.

2018 Copyright © Apache Software Foundation

Follow @ApacheIgnite

Ignite Fabric | : ver. 2.6.0
Release Date | : July 10 2018