Search in sources :

Example 1 with INTER_BROKER_REPLICA_MOVEMENT

use of com.linkedin.kafka.cruisecontrol.analyzer.ActionType.INTER_BROKER_REPLICA_MOVEMENT in project cruise-control by linkedin.

the class MinTopicLeadersPerBrokerGoal method maybeMoveLeaderOfTopicToBroker.

private void maybeMoveLeaderOfTopicToBroker(String topicMustHaveLeaderPerBroker, Broker broker, ClusterModel clusterModel, Set<Goal> optimizedGoals, OptimizationOptions optimizationOptions) throws OptimizationFailureException {
    int topicLeaderCountOnReceiverBroker = broker.numLeadersFor(topicMustHaveLeaderPerBroker);
    if (topicLeaderCountOnReceiverBroker >= minTopicLeadersPerBroker(topicMustHaveLeaderPerBroker)) {
        // This broker has enough leader replica(s) for the given topic
        return;
    }
    // Try to elect follower replica(s) of the interested topic on this broker to be leader
    List<Replica> followerReplicas = broker.trackedSortedReplicas(_replicaSortName).sortedReplicas(false).stream().filter(replica -> !replica.isLeader() && replica.topicPartition().topic().equals(topicMustHaveLeaderPerBroker)).collect(Collectors.toList());
    for (Replica followerReplica : followerReplicas) {
        Replica leader = clusterModel.partition(followerReplica.topicPartition()).leader();
        if (leader.broker().numLeadersFor(topicMustHaveLeaderPerBroker) > minTopicLeadersPerBroker(topicMustHaveLeaderPerBroker)) {
            if (maybeApplyBalancingAction(clusterModel, leader, Collections.singleton(broker), LEADERSHIP_MOVEMENT, optimizedGoals, optimizationOptions) != null) {
                topicLeaderCountOnReceiverBroker++;
                if (topicLeaderCountOnReceiverBroker >= minTopicLeadersPerBroker(topicMustHaveLeaderPerBroker)) {
                    // This broker satisfies this goal for the given topic
                    return;
                }
            }
        }
    }
    // Try to move leader replica(s) of the interested topic from other brokers to this broker
    PriorityQueue<Broker> brokersWithExcessiveLeaderToMove = getBrokersWithExcessiveLeaderToMove(topicMustHaveLeaderPerBroker, clusterModel, broker);
    while (!brokersWithExcessiveLeaderToMove.isEmpty()) {
        Broker brokerWithExcessiveLeaderToMove = brokersWithExcessiveLeaderToMove.poll();
        List<Replica> leadersOfTopic = brokerWithExcessiveLeaderToMove.trackedSortedReplicas(_replicaSortName).sortedReplicas(false).stream().filter(replica -> replica.isLeader() && replica.topicPartition().topic().equals(topicMustHaveLeaderPerBroker)).collect(Collectors.toList());
        boolean leaderMoved = false;
        int topicLeaderCountOnGiverBroker = leadersOfTopic.size();
        for (Replica leaderOfTopic : leadersOfTopic) {
            Broker destinationBroker = maybeApplyBalancingAction(clusterModel, leaderOfTopic, Collections.singleton(broker), INTER_BROKER_REPLICA_MOVEMENT, optimizedGoals, optimizationOptions);
            if (destinationBroker != null) {
                leaderMoved = true;
                // Successfully move one leader replica
                break;
            }
        }
        if (leaderMoved) {
            topicLeaderCountOnReceiverBroker++;
            if (topicLeaderCountOnReceiverBroker >= minTopicLeadersPerBroker(topicMustHaveLeaderPerBroker)) {
                // This broker satisfies this goal for the given topic
                return;
            }
            topicLeaderCountOnGiverBroker--;
            if (topicLeaderCountOnGiverBroker > minTopicLeadersPerBroker(topicMustHaveLeaderPerBroker)) {
                // Still have excessive topic leader to give
                brokersWithExcessiveLeaderToMove.add(brokerWithExcessiveLeaderToMove);
            }
        }
    }
    throw new OptimizationFailureException(String.format("[%s] Cannot make broker %d have at least %d leaders from topic %s.", name(), broker.id(), minTopicLeadersPerBroker(topicMustHaveLeaderPerBroker), topicMustHaveLeaderPerBroker));
}
Also used : Replica(com.linkedin.kafka.cruisecontrol.model.Replica) SortedReplicasHelper(com.linkedin.kafka.cruisecontrol.model.SortedReplicasHelper) OptimizationOptions(com.linkedin.kafka.cruisecontrol.analyzer.OptimizationOptions) SortedSet(java.util.SortedSet) REPLICA_REJECT(com.linkedin.kafka.cruisecontrol.analyzer.ActionAcceptance.REPLICA_REJECT) PriorityQueue(java.util.PriorityQueue) ClusterModel(com.linkedin.kafka.cruisecontrol.model.ClusterModel) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) LEADERSHIP_MOVEMENT(com.linkedin.kafka.cruisecontrol.analyzer.ActionType.LEADERSHIP_MOVEMENT) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) OptimizationFailureException(com.linkedin.kafka.cruisecontrol.exception.OptimizationFailureException) Map(java.util.Map) GoalUtils.replicaSortName(com.linkedin.kafka.cruisecontrol.analyzer.goals.GoalUtils.replicaSortName) ActionAcceptance(com.linkedin.kafka.cruisecontrol.analyzer.ActionAcceptance) Logger(org.slf4j.Logger) BalancingConstraint(com.linkedin.kafka.cruisecontrol.analyzer.BalancingConstraint) Set(java.util.Set) ACCEPT(com.linkedin.kafka.cruisecontrol.analyzer.ActionAcceptance.ACCEPT) ActionType(com.linkedin.kafka.cruisecontrol.analyzer.ActionType) Collectors(java.util.stream.Collectors) Broker(com.linkedin.kafka.cruisecontrol.model.Broker) Objects(java.util.Objects) List(java.util.List) BalancingAction(com.linkedin.kafka.cruisecontrol.analyzer.BalancingAction) ProvisionRecommendation(com.linkedin.kafka.cruisecontrol.analyzer.ProvisionRecommendation) INTER_BROKER_REPLICA_MOVEMENT(com.linkedin.kafka.cruisecontrol.analyzer.ActionType.INTER_BROKER_REPLICA_MOVEMENT) AnalyzerConfig(com.linkedin.kafka.cruisecontrol.config.constants.AnalyzerConfig) ReplicaSortFunctionFactory(com.linkedin.kafka.cruisecontrol.model.ReplicaSortFunctionFactory) Pattern(java.util.regex.Pattern) Comparator(java.util.Comparator) ProvisionStatus(com.linkedin.kafka.cruisecontrol.analyzer.ProvisionStatus) Utils(com.linkedin.kafka.cruisecontrol.common.Utils) Collections(java.util.Collections) ModelCompletenessRequirements(com.linkedin.kafka.cruisecontrol.monitor.ModelCompletenessRequirements) Broker(com.linkedin.kafka.cruisecontrol.model.Broker) OptimizationFailureException(com.linkedin.kafka.cruisecontrol.exception.OptimizationFailureException) Replica(com.linkedin.kafka.cruisecontrol.model.Replica) BalancingConstraint(com.linkedin.kafka.cruisecontrol.analyzer.BalancingConstraint)

Aggregations

ActionAcceptance (com.linkedin.kafka.cruisecontrol.analyzer.ActionAcceptance)1 ACCEPT (com.linkedin.kafka.cruisecontrol.analyzer.ActionAcceptance.ACCEPT)1 REPLICA_REJECT (com.linkedin.kafka.cruisecontrol.analyzer.ActionAcceptance.REPLICA_REJECT)1 ActionType (com.linkedin.kafka.cruisecontrol.analyzer.ActionType)1 INTER_BROKER_REPLICA_MOVEMENT (com.linkedin.kafka.cruisecontrol.analyzer.ActionType.INTER_BROKER_REPLICA_MOVEMENT)1 LEADERSHIP_MOVEMENT (com.linkedin.kafka.cruisecontrol.analyzer.ActionType.LEADERSHIP_MOVEMENT)1 BalancingAction (com.linkedin.kafka.cruisecontrol.analyzer.BalancingAction)1 BalancingConstraint (com.linkedin.kafka.cruisecontrol.analyzer.BalancingConstraint)1 OptimizationOptions (com.linkedin.kafka.cruisecontrol.analyzer.OptimizationOptions)1 ProvisionRecommendation (com.linkedin.kafka.cruisecontrol.analyzer.ProvisionRecommendation)1 ProvisionStatus (com.linkedin.kafka.cruisecontrol.analyzer.ProvisionStatus)1 GoalUtils.replicaSortName (com.linkedin.kafka.cruisecontrol.analyzer.goals.GoalUtils.replicaSortName)1 Utils (com.linkedin.kafka.cruisecontrol.common.Utils)1 AnalyzerConfig (com.linkedin.kafka.cruisecontrol.config.constants.AnalyzerConfig)1 OptimizationFailureException (com.linkedin.kafka.cruisecontrol.exception.OptimizationFailureException)1 Broker (com.linkedin.kafka.cruisecontrol.model.Broker)1 ClusterModel (com.linkedin.kafka.cruisecontrol.model.ClusterModel)1 Replica (com.linkedin.kafka.cruisecontrol.model.Replica)1 ReplicaSortFunctionFactory (com.linkedin.kafka.cruisecontrol.model.ReplicaSortFunctionFactory)1 SortedReplicasHelper (com.linkedin.kafka.cruisecontrol.model.SortedReplicasHelper)1