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