Class GridClientPartitionAffinity

  • All Implemented Interfaces:
    GridClientDataAffinity, GridClientTopologyListener

    public class GridClientPartitionAffinity
    extends Object
    implements GridClientDataAffinity, GridClientTopologyListener
    Affinity function for partitioned cache. This function supports the following configuration:
    • backupFilter - Optional filter for back up nodes. If provided, then only nodes that pass this filter will be selected as backup nodes and only nodes that don't pass this filter will be selected as primary nodes. If not provided, then primary and backup nodes will be selected out of all nodes available for this cache.

      NOTE: In situations where there are no primary nodes at all, i.e. no nodes for which backup filter returns false, first backup node for the key will be considered primary.

    • Field Detail

      • DFLT_PARTITION_CNT

        public static final int DFLT_PARTITION_CNT
        Default number of partitions.
        See Also:
        Constant Field Values
    • Constructor Detail

      • GridClientPartitionAffinity

        public GridClientPartitionAffinity()
        Empty constructor with all defaults.
      • GridClientPartitionAffinity

        public GridClientPartitionAffinity​(Integer parts,
                                           GridClientPredicate<UUID> backupFilter)
        Initializes optional counts for replicas and backups.

        Note that excludeNeighbors parameter is ignored if backupFilter is set.

        Parameters:
        parts - Total number of partitions. If null than DFLT_PARTITION_CNT will be used.
        backupFilter - Optional back up filter for nodes. If provided, then primary nodes will be selected from all nodes outside of this filter, and backups will be selected from all nodes inside it.
    • Method Detail

      • 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.

        Note that for fully replicated caches this method should always return 1.

        Returns:
        Total partition count.
      • setPartitions

        public GridClientPartitionAffinity setPartitions​(int parts)
        Sets total number of partitions.
        Parameters:
        parts - Total number of partitions.
        Returns:
        this for chaining.
      • getHashIdResolver

        public GridClientPartitionAffinity.HashIdResolver getHashIdResolver()
        Gets hash ID resolver for nodes. This resolver is used to provide alternate hash ID, other than node ID.

        Node IDs constantly change when nodes get restarted, which causes them to be placed on different locations in the hash ring, and hence causing repartitioning. Providing an alternate hash ID, which survives node restarts, puts node on the same location on the hash ring, hence minimizing required repartitioning.

        Returns:
        Hash ID resolver.
      • setHashIdResolver

        public GridClientPartitionAffinity setHashIdResolver​(GridClientPartitionAffinity.HashIdResolver hashIdRslvr)
        Sets hash ID resolver for nodes. This resolver is used to provide alternate hash ID, other than node ID.

        Node IDs constantly change when nodes get restarted, which causes them to be placed on different locations in the hash ring, and hence causing repartitioning. Providing an alternate hash ID, which survives node restarts, puts node on the same location on the hash ring, hence minimizing required repartitioning.

        Parameters:
        hashIdRslvr - Hash ID resolver.
        Returns:
        this for chaining.
      • getBackupFilter

        public GridClientPredicate<UUID> getBackupFilter()
        Gets optional backup filter. If not null, then primary nodes will be selected from all nodes outside of this filter, and backups will be selected from all nodes inside it.

        Note that excludeNeighbors parameter is ignored if backupFilter is set.

        Returns:
        Optional backup filter.
      • setBackupFilter

        public GridClientPartitionAffinity setBackupFilter​(GridClientPredicate<UUID> backupFilter)
        Sets optional backup filter. If provided, then primary nodes will be selected from all nodes outside of this filter, and backups will be selected from all nodes inside it.

        Note that excludeNeighbors parameter is ignored if backupFilter is set.

        Parameters:
        backupFilter - Optional backup filter.
        Returns:
        this for chaining.
      • node

        public GridClientNode node​(Object key,
                                   Collection<? extends GridClientNode> nodes)
        Gets primary affinity node for a key. In case of replicated cache all nodes are equal and can be considered primary, so it may return any node. In case of partitioned cache primary node is returned.
        Specified by:
        node in interface GridClientDataAffinity
        Parameters:
        key - Key to get affinity for.
        nodes - Nodes to choose from.
        Returns:
        Affinity nodes for the given partition.