Search in sources :

Example 6 with Partition

use of com.linkedin.kafka.cruisecontrol.model.Partition in project cruise-control by linkedin.

the class KafkaAssignerEvenRackAwareGoal method optimize.

/**
 * Optimize the given cluster model as needed for this goal.
 *
 * @param clusterModel   The state of the cluster.
 * @param optimizedGoals Goals that have already been optimized. These goals cannot be violated.
 * @param excludedTopics The topics that should be excluded from the optimization action.
 * @return true if the goal is met after the optimization, throws an exceptions if the goal is not met.
 */
@Override
public boolean optimize(ClusterModel clusterModel, Set<Goal> optimizedGoals, Set<String> excludedTopics) throws KafkaCruiseControlException {
    LOG.debug("Starting {} with excluded topics = {}", name(), excludedTopics);
    if (!optimizedGoals.isEmpty()) {
        throw new IllegalArgumentException(String.format("Goals %s cannot be optimized before %s.", optimizedGoals, name()));
    }
    initGoalState(clusterModel, excludedTopics);
    // STEP1: Move leader to the first position in partition replica list.
    for (Map.Entry<String, List<Partition>> entry : _partitionsByTopic.entrySet()) {
        for (Partition partition : entry.getValue()) {
            // Ensure the first replica is the leader.
            if (partition.replicas().get(0) != partition.leader()) {
                partition.swapReplicaPositions(0, partition.replicas().indexOf(partition.leader()));
            }
        }
    }
    // STEP2: Perform optimization.
    int maxReplicationFactor = clusterModel.maxReplicationFactor();
    for (int position = 0; position < maxReplicationFactor; position++) {
        for (Map.Entry<String, List<Partition>> entry : _partitionsByTopic.entrySet()) {
            for (Partition partition : entry.getValue()) {
                if (partition.replicas().size() <= position) {
                    continue;
                }
                if (shouldExclude(partition, position, excludedTopics)) {
                    continue;
                }
                // Apply the necessary move (if needed).
                if (!maybeApplyMove(clusterModel, partition, position)) {
                    throw new OptimizationFailureException(String.format("Unable to apply move for replica %s.", replicaAtPosition(partition, position)));
                }
            }
        }
    }
    ensureRackAware(clusterModel, excludedTopics);
    // Sanity check: No self-healing eligible replica should remain at a decommissioned broker.
    AnalyzerUtils.ensureNoReplicaOnDeadBrokers(clusterModel);
    return true;
}
Also used : Partition(com.linkedin.kafka.cruisecontrol.model.Partition) OptimizationFailureException(com.linkedin.kafka.cruisecontrol.exception.OptimizationFailureException) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Partition (com.linkedin.kafka.cruisecontrol.model.Partition)6 HashMap (java.util.HashMap)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Map (java.util.Map)4 Broker (com.linkedin.kafka.cruisecontrol.model.Broker)3 TreeSet (java.util.TreeSet)3 TopicPartition (org.apache.kafka.common.TopicPartition)3 AggregatedMetricValues (com.linkedin.cruisecontrol.monitor.sampling.aggregator.AggregatedMetricValues)2 Resource (com.linkedin.kafka.cruisecontrol.common.Resource)2 ClusterModel (com.linkedin.kafka.cruisecontrol.model.ClusterModel)2 HashSet (java.util.HashSet)2 MetricRegistry (com.codahale.metrics.MetricRegistry)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 MetricValues (com.linkedin.cruisecontrol.monitor.sampling.aggregator.MetricValues)1 KafkaClusterState (com.linkedin.kafka.cruisecontrol.KafkaClusterState)1 KafkaCruiseControlState (com.linkedin.kafka.cruisecontrol.KafkaCruiseControlState)1 GoalOptimizer (com.linkedin.kafka.cruisecontrol.analyzer.GoalOptimizer)1 Goal (com.linkedin.kafka.cruisecontrol.analyzer.goals.Goal)1