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;
}
Aggregations