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

## Class PlatformAffinityFunction

- java.lang.Object
- - org.apache.ignite.internal.processors.platform.cache.affinity.PlatformAffinityFunction

All Implemented Interfaces:
  Externalizable, Serializable, AffinityFunction, LifecycleAware

    public class PlatformAffinityFunction
    extends Object
    implements AffinityFunction, Externalizable, LifecycleAware

Platform AffinityFunction.

See Also:
  Serialized Form

### Constructor Summary

Constructors
Constructor and Description
`PlatformAffinityFunction()` Ctor for serialization.
`PlatformAffinityFunction(Object func, int partitions, byte overrideFlags, AffinityFunction baseFunc)` Ctor.

### Method Summary

All Methods Instance Methods Concrete Methods
`List<List<ClusterNode>>` `assignPartitions(AffinityFunctionContext affCtx)` Gets affinity nodes for a partition.
`AffinityFunction` `getBaseFunc()` Gets the base func.
`byte` `getOverrideFlags()` Gets the override flags.
`Object` `getUserFunc()` Gets the user func object.
`int` `partition(Object key)` Gets partition number for a given key starting from `0`.
`int` `partitions()` Gets total number of partitions available.
`void` `readExternal(ObjectInput in)`
`void` `removeNode(UUID nodeId)` Removes node from affinity.
`void` `reset()` Resets cache affinity to its initial state.
`void` `setIgnite(Ignite ignite)` Injects the Ignite.
`void` `start()` Starts grid component, called on grid start.
`void` `stop()` Stops grid component, called on grid shutdown.
`void` `writeExternal(ObjectOutput out)`

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

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

### Constructor Detail

#### PlatformAffinityFunction

    public PlatformAffinityFunction()

Ctor for serialization.

#### PlatformAffinityFunction

    public PlatformAffinityFunction(Object func,
                                    int partitions,
                                    byte overrideFlags,
                                    AffinityFunction baseFunc)

Ctor.

Parameters:
  `func` - User fun object.
  `partitions` - Number of partitions.

### Method Detail

#### getUserFunc

    public Object getUserFunc()

Gets the user func object.

Returns:
  User func object.

#### getBaseFunc

    public AffinityFunction getBaseFunc()

Gets the base func.

Returns:
  Base func.

#### getOverrideFlags

    public byte getOverrideFlags()

Gets the override flags.

Returns:
  The override flags

#### 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 interface `AffinityFunction`

#### 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 from `0` inclusively to `N` exclusively without any gaps.

Specified by:
  `partitions` in interface `AffinityFunction`
Returns:
  Total partition count.

#### partition

    public int partition(Object key)

Gets partition number for a given key starting from `0`. Partitioned caches should make sure that keys are about evenly distributed across all partitions from `0` to `partition 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 interface `AffinityFunction`
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 key `K`, if any other node(s) leave grid and no node joins grid, node `N` will remain primary for key `K`.

Specified by:
  `assignPartitions` in interface `AffinityFunction`
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 interface `AffinityFunction`
Parameters:
  `nodeId` - ID of node to remove.

#### writeExternal

    public void writeExternal(ObjectOutput out)
                       throws IOException

Specified by:
  `writeExternal` in interface `Externalizable`
Throws:
  `IOException`

#### readExternal

    public void readExternal(ObjectInput in)
                      throws IOException,
                             ClassNotFoundException

Specified by:
  `readExternal` in interface `Externalizable`
Throws:
  `IOException`
  `ClassNotFoundException`

#### start

    public void start()
               throws IgniteException

Starts grid component, called on grid start.

Specified by:
  `start` in interface `LifecycleAware`
Throws:
  `IgniteException` - If failed.

#### stop

    public void stop()
              throws IgniteException

Stops grid component, called on grid shutdown.

Specified by:
  `stop` in interface `LifecycleAware`
Throws:
  `IgniteException` - If failed.

#### setIgnite

    @IgniteInstanceResource
    public void setIgnite(Ignite ignite)
                                           throws IgniteCheckedException

Injects the Ignite.

Parameters:
  `ignite` - Ignite.
Throws:
  `IgniteCheckedException`

${current.year} Copyright © Apache Software Foundation

Follow @ApacheIgnite

Ignite Database and Caching Platform | : ver. 2.15.0
Release Date | : April 25 2023