Class SimpleAssignmentStrategy

  • All Implemented Interfaces:
    PartitionAssignmentStrategy, SimpleStrategyMBean
    Direct Known Subclasses:
    MirroringAssignmentStrategy

    public class SimpleAssignmentStrategy
    extends Object
    implements PartitionAssignmentStrategy, SimpleStrategyMBean
    SimpleAssignmentStrategy is a PartitionAssignmentStrategy that attempts to balance the partition distribution based on the number of primary and backup partitions owned. The SimpleAssignmentStrategy will attempt to ensure machine-safety, but only if a balanced "safe" distribution is achievable.

    The SimpleAssignmentStrategy is an extensible implementation of the internal distribution algorithm that was available prior to Coherence 3.7.

    Since:
    Coherence 3.7
    Author:
    rhl 2010.11.08
    • Field Detail

      • MEMBERID_COMPARATOR

        protected static final Comparator MEMBERID_COMPARATOR
        Comparator used to provide arbitrary (equals-compatible) comparisons between members.
      • MSG_NO_RESULT

        protected static final String MSG_NO_RESULT
        The message returned by SimpleStrategyMBean when the distribution coordinator has not done its first analysis yet.
        See Also:
        Constant Field Values
      • MSG_NO_PENDING

        protected static final String MSG_NO_PENDING
        The message returned by SimpleStrategyMBean when all suggested distributions have completed and none are in-progress or scheduled.
        See Also:
        Constant Field Values
      • m_setOwnersLast

        protected Set m_setOwnersLast
        The Set of ownership-enabled members at the time of the last analysis.
      • m_mapSuggestLast

        protected Map<Ownership,​PartitionSet> m_mapSuggestLast
        The Map containing the last distribution suggested by this strategy.
      • m_fRefresh

        protected boolean m_fRefresh
        True if JMXPartitionStats should be updated.
      • m_cPlanCompletionDelay

        protected long m_cPlanCompletionDelay
        The amount of time in ms to delay the analysis after a distribution suggestion has been made and before it is carried out.
    • Constructor Detail

      • SimpleAssignmentStrategy

        public SimpleAssignmentStrategy()
        Default constructor.
    • Method Detail

      • getManager

        public DistributionManager getManager()
        Return the DistributionManager.
        Returns:
        the DistributionManager
      • getLastOwnershipMembers

        public Set getLastOwnershipMembers()
        Return the set of ownership-enabled members present when the analysis was last considered.
        Returns:
        the set of ownership-enabled members when analysis was last considered
      • setLastOwnershipMembers

        protected void setLastOwnershipMembers​(Set setOwners)
        Record the set of ownership-enabled members present when the analysis was last considered.
        Parameters:
        setOwners - the set of ownership-enabled members
      • getMemberJoinDelay

        protected long getMemberJoinDelay()
        Return the amount of time in ms to delay the analysis after a member has joined. This delay could be used to "dampen" the reactivity of the strategy to membership changes.
        Returns:
        the amount of time in ms to delay the analysis after a member joins
      • getSuggestionDelay

        protected long getSuggestionDelay()
        Return the amount of time in ms to delay the analysis.
        Returns:
        the amount of time in ms to delay the analysis
      • getSuggestionCompletionDelay

        protected long getSuggestionCompletionDelay()
        Return the amount of time in ms to delay the analysis after a distribution suggestion has been made and before it is carried out. This delay could be used to "dampen" the volatility of the strategy by allowing sufficient time for in-flight transfers to complete prior to considering further recommendations.
        Returns:
        the amount of time in ms to delay the analysis after a suggestion is made
      • init

        public void init​(DistributionManager manager)
        Initialize the PartitionAssignmentStrategy and bind it to the specified DistributionManager. This method is called only on the distribution coordinator, prior its first distribution analysis.
        Specified by:
        init in interface PartitionAssignmentStrategy
        Parameters:
        manager - the DistributionManager that this strategy is bound to
      • analyzeOrphans

        public void analyzeOrphans​(Map<Member,​PartitionSet> mapConstraints)
        Analyze and suggest the assignment of orphaned partitions (partitions without an active primary or backup owner) subject to the specified constraints. For each partition, the supplied constraints specify the set of members which are eligible to be assigned the ownership.

        The strategy must provide suggestions for all orphaned partitions which are consistent with the supplied constraints. Failure to provide a complete set of valid suggestions may result in the loss of partition data.

        Specified by:
        analyzeOrphans in interface PartitionAssignmentStrategy
        Parameters:
        mapConstraints - the map of assignment constraints associating members with the set of partitions that they could be assigned ownership of
      • analyzeDistribution

        public long analyzeDistribution()
        Analyze the distribution and return the desired time interval before the next distribution analysis. This method may or may not make distribution suggestions through the distribution manager. The strategy can influence (but not guarantee) the frequency with which it is analyzed by returning the desired interval before the next call, or -1 if the strategy has no preference and will rely on the PartitionedService to decide.

        As a result of failover, partitions may become 'endangered', meaning that the necessary number of backups do not exist. Failure to suggest a distribution recovery plan for those partitions may result in the partition remaining in the endangered state. Additionally, ownership-enabled service members that are in the process of shutting down will wait until all owned partitions are transferred out. Failure to suggest a distribution plan may delay the exit of these leaving members.

        The statistics and ownership information exposed by the DistributionManager will not mutate for the duration of this method call.

        Specified by:
        analyzeDistribution in interface PartitionAssignmentStrategy
        Returns:
        the time interval before the next desired analysis, or -1
      • analyzeDistribution

        protected long analyzeDistribution​(SimpleAssignmentStrategy.AnalysisContext ctx)
        Analyze the distribution and suggest the resulting distribution to distribution manager.
        Parameters:
        ctx - the analysis context
        Returns:
        the time interval before the next desired analysis, or -1
      • primeDistribution

        protected void primeDistribution​(SimpleAssignmentStrategy.AnalysisContext ctx)
        Optimize the distribution with minimum backup fan-out by distributing the partitions among first two strong members.
        Parameters:
        ctx - the analysis context
      • analyze

        protected long analyze​(SimpleAssignmentStrategy.AnalysisContext ctx)
        Analyze the distribution using the specified analysis context.
        Parameters:
        ctx - the analysis context
        Returns:
        the time interval before the next desired analysis, or -1
      • checkSimpleDistribution

        protected boolean checkSimpleDistribution​(SimpleAssignmentStrategy.AnalysisContext ctx)
        Analyze the distribution for the special two-server case using the specified analysis context.
        Parameters:
        ctx - the analysis context
        Returns:
        true if the simple distribution is in effect
      • getDescription

        public String getDescription()
        Return a human-readable description of the state of the partition assignment.
        Specified by:
        getDescription in interface PartitionAssignmentStrategy
        Returns:
        a human-readable description
      • checkLeaving

        protected void checkLeaving​(SimpleAssignmentStrategy.AnalysisContext ctx)
        Check for any service members that are leaving, and adjust the distribution plan accordingly.

        Partitions owned by leaving members must be transferred to other members before the departing members are able to shutdown.

        Parameters:
        ctx - the AnalysisContext
      • validateBackups

        protected void validateBackups​(SimpleAssignmentStrategy.AnalysisContext ctx)
        Check if there are enough ownership members to maintain the configured backup count, reducing the number of backups otherwise.
        Parameters:
        ctx - the AnalysisContext
      • checkPrimaryBalance

        protected void checkPrimaryBalance​(SimpleAssignmentStrategy.AnalysisContext ctx)
        Check the distribution to ensure that primary the partition load is balanced.
        Parameters:
        ctx - the analysis context
      • doBalancePrimary

        protected int doBalancePrimary​(SimpleAssignmentStrategy.AnalysisContext ctx,
                                       Member memberFrom,
                                       PartitionSet parts,
                                       Member[] aMembersTarget)
        Do balancing transfers for primary distribution.
        Parameters:
        ctx - the analysis context
        memberFrom - the member to transfer partitions from
        parts - the set of partitions from which to transfer
        aMembersTarget - the (unordered) array of members
        Returns:
        the number of changes (transfers) that were made
      • checkEndangered

        protected void checkEndangered​(SimpleAssignmentStrategy.AnalysisContext ctx)
        Check the distribution to ensure that backups are created for any "endangered" partitions.

        A partition is "endangered" if it is incompletely backed up (e.g. some backup copies do not exist).

        Parameters:
        ctx - the analysis context
      • checkBackupStrong

        protected int checkBackupStrong​(SimpleAssignmentStrategy.AnalysisContext ctx)
        Check that the backups are strong.
        Parameters:
        ctx - the analysis context
        Returns:
        the number of changes (transfers) that were made
      • checkBackupBalance

        protected int checkBackupBalance​(SimpleAssignmentStrategy.AnalysisContext ctx)
        Check that the distribution of backup partitions is balanced.
        Parameters:
        ctx - the analysis context
        Returns:
        the number of changes (transfers) that were made
      • checkBackupOverloaded

        protected int checkBackupOverloaded​(SimpleAssignmentStrategy.AnalysisContext ctx)
        Check if the distribution of backup partitions is balanced. If not, disturb the distribution by moving a partition from the overloaded member to another member that retains partition strength.
        Parameters:
        ctx - the analysis context
        Returns:
        the number of unbalanced partitions that need to be transferred
      • getMember

        protected Member getMember​(int nMemberId)
        Return the PartitionedService Member with the specified mini-id.
        Parameters:
        nMemberId - the mini-id
        Returns:
        the PartitionedService Member with the specified mini-id, or null
      • getVariance

        protected int getVariance​(SimpleAssignmentStrategy.AnalysisContext ctx,
                                  boolean fPrimary)
        Return the maximum load variance in the partition assignments represented by the analysis context. The maximum variance is the difference in load between the 'lightest' and 'heaviest' members.
        Parameters:
        ctx - the analysis context
        fPrimary - true iff the "primary" load variance should be computed
        Returns:
        the maximum variance
      • chainComparators

        protected static Comparator chainComparators​(Comparator comp1,
                                                     Comparator comp2)
        Helper method to return a Comparator chaining the specified comparators.
        Parameters:
        comp1 - the first comparator
        comp2 - the second comparator
        Returns:
        a chained comparator
      • chainComparators

        protected static Comparator chainComparators​(Comparator comp1,
                                                     Comparator comp2,
                                                     Comparator comp3)
        Helper method to return a Comparator chaining the specified comparators.
        Parameters:
        comp1 - the first comparator
        comp2 - the second comparator
        comp3 - the third comparator
        Returns:
        a chained comparator
      • filterSort

        protected static int filterSort​(Object[] ao,
                                        Comparator comparator,
                                        Filter filter)
        Filter the elements in the specified array and sort any matching elements using the specified comparator. All matching results will be compacted to the front of the array. The order of results not matching the filter is undefined.
        Parameters:
        ao - the object array to sort and filter
        comparator - the comparator to order the elements
        filter - the filter to use to filter the results
        Returns:
        the number of elements matching the specified filter
      • filterSort

        protected static int filterSort​(Object[] ao,
                                        int cElems,
                                        Comparator comparator,
                                        Filter filter)
        Filter the specified array elements and sort any matching elements using the specified comparator. All matching results will be compacted to the front of the array. The order of results not matching the filter is undefined.
        Parameters:
        ao - the object array to sort and filter
        cElems - the number of elements to filter and sort
        comparator - the comparator to order the elements
        filter - the filter to use to filter the results
        Returns:
        the number of elements matching the specified filter
      • filterArray

        protected static int filterArray​(Object[] ao,
                                         Filter filter)
        Apply the specified filter to the elements of the specified array. All matching results will be compacted to the front of the array in a "stable" manner. The order of results not matching the filter may not be preserved.
        Parameters:
        ao - the object array to apply the filter to
        filter - the filter to apply
        Returns:
        the number of elements matching the specified filter
      • filterArray

        protected static int filterArray​(Object[] ao,
                                         int cElems,
                                         Filter filter)
        Apply the specified filter to the specified array elements. All matching results will be compacted to the front of the array in a "stable" manner. The order of results not matching the filter may not be preserved.
        Parameters:
        ao - the object array to apply the filter to
        cElems - the number of elements to filter
        filter - the filter to apply
        Returns:
        the number of elements matching the specified filter
      • getPartitionCount

        public int getPartitionCount()
        Get the configured number of partitions for the service.
        Specified by:
        getPartitionCount in interface SimpleStrategyMBean
        Returns:
        the partition count
      • getBackupCount

        public int getBackupCount()
        Get the configured number of backups.
        Specified by:
        getBackupCount in interface SimpleStrategyMBean
        Returns:
        the backup count
      • getServiceNodeCount

        public int getServiceNodeCount()
        Get the number of storage-enabled nodes running this service.
        Specified by:
        getServiceNodeCount in interface SimpleStrategyMBean
        Returns:
        the number of storage-enabled nodes
      • getServiceMachineCount

        public int getServiceMachineCount()
        Get the number of machines that host storage-enabled nodes running this service.
        Specified by:
        getServiceMachineCount in interface SimpleStrategyMBean
        Returns:
        the number of machines with storage-enabled nodes
      • getServiceRackCount

        public int getServiceRackCount()
        Get the number of racks that host storage-enabled nodes running this service.
        Specified by:
        getServiceRackCount in interface SimpleStrategyMBean
        Returns:
        the number of racks with storage-enabled nodes
      • getServiceSiteCount

        public int getServiceSiteCount()
        Get the number of sites that host storage-enabled nodes running this service.
        Specified by:
        getServiceSiteCount in interface SimpleStrategyMBean
        Returns:
        the number of sites with storage-enabled nodes
      • getHAStatus

        public String getHAStatus()
        The High Availability status for the service.
        Specified by:
        getHAStatus in interface SimpleStrategyMBean
        Returns:
        one of the following values: ENDANGERED, NODE-SAFE, MACHINE-SAFE, RACK-SAFE, SITE-SAFE
      • getHAStatusCode

        public int getHAStatusCode()
        The High Availability status for the service as an integer.

        Below is an example of how to map a HA Status integer to a string:

         HAStatus.values()[getHAStatusCode()].name()
         
        Specified by:
        getHAStatusCode in interface SimpleStrategyMBean
        Returns:
        integer representation of SimpleStrategyMBean.HAStatus
      • getHATarget

        public String getHATarget()
        The High Availability status that this strategy attempts to achieve. Values are the same as for HAStatus attribute.
        Specified by:
        getHATarget in interface SimpleStrategyMBean
        Returns:
        the target High Availability status
      • getFairShareBackup

        public int getFairShareBackup()
        Get the number of backup partitions per storage-enabled service member that this strategy will currently attempt to maintain.
        Specified by:
        getFairShareBackup in interface SimpleStrategyMBean
        Returns:
        the number of backup partitions per storage-enabled member
      • getFairSharePrimary

        public int getFairSharePrimary()
        Get the number of primary partitions per storage-enabled service member that this strategy will currently attempt to maintain.
        Specified by:
        getFairSharePrimary in interface SimpleStrategyMBean
        Returns:
        the number of primary partitions per storage-enabled member
      • getStrategyName

        public String getStrategyName()
        Get name of the PartitionAssignmentStrategy in use.
        Specified by:
        getStrategyName in interface SimpleStrategyMBean
        Returns:
        partitioning strategy name
      • getLastAnalysisTime

        public Date getLastAnalysisTime()
        Get the last time a partition distribution analysis was performed.
        Specified by:
        getLastAnalysisTime in interface SimpleStrategyMBean
        Returns:
        the time of the last analysis
      • getCoordinatorId

        public int getCoordinatorId()
        Get the node id of the ownership distribution coordinator.
        Specified by:
        getCoordinatorId in interface SimpleStrategyMBean
        Returns:
        current distribution coordinator node id
      • getRemainingDistributionCount

        public int getRemainingDistributionCount()
        Get the number of distributions (partition transfers) that remain to be completed before the service achieves the goals set by this strategy.
        Specified by:
        getRemainingDistributionCount in interface SimpleStrategyMBean
        Returns:
        the number of remaining partition transfers
      • getAveragePartitionSizeKB

        public long getAveragePartitionSizeKB()
        Get the average partition storage size.
        Specified by:
        getAveragePartitionSizeKB in interface SimpleStrategyMBean
        Returns:
        the average partition storage size in kilobytes
      • getMaxPartitionSizeKB

        public long getMaxPartitionSizeKB()
        Get the maximum partition storage size.
        Specified by:
        getMaxPartitionSizeKB in interface SimpleStrategyMBean
        Returns:
        the maximum partition storage size in kilobytes
      • getMaxStorageSizeKB

        public long getMaxStorageSizeKB()
        Get maximum node storage size.
        Specified by:
        getMaxStorageSizeKB in interface SimpleStrategyMBean
        Returns:
        the maximum node storage size in kilobytes
      • getAverageStorageSizeKB

        public long getAverageStorageSizeKB()
        Get the average node storage size.
        Specified by:
        getAverageStorageSizeKB in interface SimpleStrategyMBean
        Returns:
        the average node storage size in kilobytes
      • getMaxLoadNodeId

        public int getMaxLoadNodeId()
        Get the node id with the maximum storage size.
        Specified by:
        getMaxLoadNodeId in interface SimpleStrategyMBean
        Returns:
        the node id with the maximum storage size
      • updateCompositeStats

        protected SimpleAssignmentStrategy.JMXPartitionStats updateCompositeStats()
        Return a JMXPartitionStats that contains calculated MBean Attributes, updated periodically.
        Returns:
        JMXPartitionStats with calculated MBean Attributes
      • reportScheduledDistributions

        public String reportScheduledDistributions​(boolean fVerbose)
        Report partitions that storage-enabled members are waiting to receive or still need to send in order to achieve distribution goal set by the strategy.
        Specified by:
        reportScheduledDistributions in interface SimpleStrategyMBean
        Parameters:
        fVerbose - if true, the report includes partition numbers for each scheduled transfer
        Returns:
        a description of scheduled distributions for the service
      • registerMBean

        protected void registerMBean()
        Register an MBean representing this SimpleAssignmentStrategy.

        This implementation creates and registers a SimpleStrategyMBean.

      • makeMBeanName

        protected String makeMBeanName​(PartitionedService service)
        Create a name for the MBean representing this strategy.

        The name must be globally unique, but not contain the nodeId property. This allows us to re-bind the same JMX name to the MBean on a different node when the distribution coordinator migrates.

        Parameters:
        service - partitioned service that uses this strategy
        Returns:
        the name for the MBean
      • unregisterMBean

        protected void unregisterMBean()
        Unregister the MBean representing this SimpleAssignmentStrategy.
      • emitLossNotification

        protected void emitLossNotification​(PartitionSet partsLost)
        Emit a partition loss notification for given partitions.
        Parameters:
        partsLost - the partition id that has been lost
      • collectScheduledDistributions

        protected Map<Member,​PartitionSet[]> collectScheduledDistributions()
        Collect the scheduled partition distributions, grouped by the primary owner and storage index. A partition distribution (either primary or backup) is considered to be scheduled if the distribution has been suggested but the ownership change has not yet occurred.
        Returns:
        a Map of partition sets still awaiting distribution keyed by primary owner
      • getUnownedPartitions

        protected PartitionSet getUnownedPartitions​(PartitionSet parts,
                                                    int nMember)
        Calculate a subset of the PartitionSet consisting of partitions that are not owned (primary or backup) by the specified member.
        Parameters:
        parts - set of partitions to check the ownership of
        nMember - the member-id
        Returns:
        the subset of partitions that are not owned by the specified member
      • splitByOwner

        protected SortedMap<Integer,​PartitionSet> splitByOwner​(PartitionSet parts)
        Split the partition set scheduled for distribution by the current primary owner (all transfers originate from the primary owner).
        Parameters:
        parts - a set of partitions not yet transferred
        Returns:
        the partitions scheduled for distribution, associated with their primary owners
      • instantiateLoadCalculator

        public SimpleAssignmentStrategy.LoadCalculator instantiateLoadCalculator​(boolean fPrimary)
        Instantiate the load calculator.
        Parameters:
        fPrimary - true iff the load calculator will be used for primary partition load; backup otherwise
        Returns:
        a load calculator