|
| 1 | +/* |
| 2 | + * Copyright 2022 LinkedIn Corp. Licensed under the BSD 2-Clause License (the "License"). See License in the project root for license information. |
| 3 | + */ |
| 4 | + |
| 5 | +package com.linkedin.kafka.cruisecontrol.analyzer.goals; |
| 6 | + |
| 7 | +import com.linkedin.kafka.cruisecontrol.analyzer.OptimizationOptions; |
| 8 | +import com.linkedin.kafka.cruisecontrol.analyzer.ActionAcceptance; |
| 9 | +import com.linkedin.kafka.cruisecontrol.analyzer.BalancingAction; |
| 10 | +import com.linkedin.kafka.cruisecontrol.analyzer.ProvisionResponse; |
| 11 | +import com.linkedin.kafka.cruisecontrol.analyzer.ProvisionStatus; |
| 12 | +import com.linkedin.kafka.cruisecontrol.common.Resource; |
| 13 | +import com.linkedin.kafka.cruisecontrol.model.Broker; |
| 14 | +import com.linkedin.kafka.cruisecontrol.model.ClusterModel; |
| 15 | +import com.linkedin.kafka.cruisecontrol.model.ClusterModelStats; |
| 16 | +import com.linkedin.kafka.cruisecontrol.model.Disk; |
| 17 | +import com.linkedin.kafka.cruisecontrol.model.Replica; |
| 18 | +import com.linkedin.kafka.cruisecontrol.monitor.ModelCompletenessRequirements; |
| 19 | +import java.util.Set; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.List; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.Comparator; |
| 24 | +import org.slf4j.Logger; |
| 25 | +import org.slf4j.LoggerFactory; |
| 26 | + |
| 27 | +import static com.linkedin.kafka.cruisecontrol.analyzer.ActionAcceptance.ACCEPT; |
| 28 | +import static com.linkedin.kafka.cruisecontrol.analyzer.goals.GoalUtils.MIN_NUM_VALID_WINDOWS_FOR_SELF_HEALING; |
| 29 | + |
| 30 | + |
| 31 | +/** |
| 32 | + * Soft goal to move the replicas to different log dir. |
| 33 | + */ |
| 34 | +public class DiskRemovalGoal implements Goal { |
| 35 | + private static final Logger LOG = LoggerFactory.getLogger(DiskRemovalGoal.class); |
| 36 | + private static final Double EPSILON = 0.0001; |
| 37 | + |
| 38 | + private final ProvisionResponse _provisionResponse; |
| 39 | + protected final Map<Integer, Set<String>> _brokerIdAndLogdirs; |
| 40 | + protected final double _errorMargin; |
| 41 | + |
| 42 | + public DiskRemovalGoal(Map<Integer, Set<String>> brokerIdAndLogdirs, double errorMargin) { |
| 43 | + _provisionResponse = new ProvisionResponse(ProvisionStatus.UNDECIDED); |
| 44 | + _brokerIdAndLogdirs = brokerIdAndLogdirs; |
| 45 | + _errorMargin = errorMargin; |
| 46 | + } |
| 47 | + |
| 48 | + private void sanityCheckOptimizationOptions(OptimizationOptions optimizationOptions) { |
| 49 | + if (optimizationOptions.isTriggeredByGoalViolation()) { |
| 50 | + throw new IllegalArgumentException(String.format("%s goal does not support use by goal violation detector.", name())); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public boolean optimize(ClusterModel clusterModel, Set<Goal> optimizedGoals, OptimizationOptions optimizationOptions) { |
| 56 | + sanityCheckOptimizationOptions(optimizationOptions); |
| 57 | + |
| 58 | + for (Map.Entry<Integer, Set<String>> brokerIdLogDirs : _brokerIdAndLogdirs.entrySet()) { |
| 59 | + Integer brokerId = brokerIdLogDirs.getKey(); |
| 60 | + Set<String> logDirsToRemove = brokerIdLogDirs.getValue(); |
| 61 | + relocateBrokerLogDirs(clusterModel, brokerId, logDirsToRemove); |
| 62 | + } |
| 63 | + |
| 64 | + return true; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * This method relocates the replicas on the provided log dirs to other log dirs of the same broker. |
| 69 | + * |
| 70 | + * @param clusterModel the cluster model |
| 71 | + * @param brokerId the id of the broker where the movement will take place |
| 72 | + * @param logDirsToRemove the set of log dirs to be removed from the broker |
| 73 | + */ |
| 74 | + private void relocateBrokerLogDirs(ClusterModel clusterModel, Integer brokerId, Set<String> logDirsToRemove) { |
| 75 | + Broker currentBroker = clusterModel.broker(brokerId); |
| 76 | + List<Disk> remainingDisks = new ArrayList<>(); |
| 77 | + currentBroker.disks().stream().filter(disk -> !logDirsToRemove.contains(disk.logDir())).forEach(remainingDisks::add); |
| 78 | + remainingDisks.sort(Comparator.comparing(Disk::logDir)); |
| 79 | + List<Replica> replicasToMove = getReplicasToMoveAsListSortedBySizeDesc(currentBroker, logDirsToRemove); |
| 80 | + |
| 81 | + int usedDiskIdx = -1; |
| 82 | + for (Replica replicaToMove : replicasToMove) { |
| 83 | + usedDiskIdx = relocateReplicaIfPossible(clusterModel, brokerId, remainingDisks, replicaToMove, usedDiskIdx); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * This method provides the list of replicas to be moved sorted in descending order by the disk utilization. |
| 89 | + * |
| 90 | + * @param broker the broker where the replicas are |
| 91 | + * @param logDirs the log dirs where the replicas are |
| 92 | + * @return the sorted list of replicas to be moved |
| 93 | + */ |
| 94 | + private List<Replica> getReplicasToMoveAsListSortedBySizeDesc(Broker broker, Set<String> logDirs) { |
| 95 | + List<Replica> replicasToMove = new ArrayList<>(); |
| 96 | + for (String logDir : logDirs) { |
| 97 | + Set<Replica> logDirReplicas = broker.disk(logDir).replicas(); |
| 98 | + replicasToMove.addAll(logDirReplicas); |
| 99 | + } |
| 100 | + |
| 101 | + replicasToMove.sort(Comparator.comparingDouble(o -> ((Replica) o).load().expectedUtilizationFor(Resource.DISK)).reversed()); |
| 102 | + return replicasToMove; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * This method relocates the given replica on one of the candidate disks in a round-robin manner if there is enough space |
| 107 | + * |
| 108 | + * @param clusterModel the cluster model |
| 109 | + * @param brokerId the broker id where the replica movement occurs |
| 110 | + * @param remainingDisks the candidate disks on which to move the replica |
| 111 | + * @param replica the replica to move |
| 112 | + * @param usedDiskIdx the index of the last disk used to relocate replicas |
| 113 | + * @return the index of the disk used to relocate the replica to |
| 114 | + */ |
| 115 | + private int relocateReplicaIfPossible(ClusterModel clusterModel, Integer brokerId, List<Disk> remainingDisks, Replica replica, int usedDiskIdx) { |
| 116 | + int remainingDisksNumber = remainingDisks.size(); |
| 117 | + int diskIndex = (usedDiskIdx + 1) % remainingDisksNumber; |
| 118 | + for (int i = 0; i < remainingDisksNumber; i++) { |
| 119 | + Disk destinationDisk = remainingDisks.get(diskIndex); |
| 120 | + if (isEnoughSpace(destinationDisk, replica)) { |
| 121 | + clusterModel.relocateReplica(replica.topicPartition(), brokerId, destinationDisk.logDir()); |
| 122 | + return diskIndex; |
| 123 | + } |
| 124 | + diskIndex = (diskIndex + 1) % remainingDisksNumber; |
| 125 | + } |
| 126 | + LOG.info("Could not move replica {} to any of the remaining disks.", replica); |
| 127 | + return usedDiskIdx; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * This method checks if the usage on the disk that the replica will be moved to is lower than the disk capacity |
| 132 | + * including the error margin. |
| 133 | + * |
| 134 | + * @param disk the disk on which the replica can be moved |
| 135 | + * @param replica the replica to move |
| 136 | + * @return boolean which reflects if there is enough disk space to move the replica |
| 137 | + */ |
| 138 | + private boolean isEnoughSpace(Disk disk, Replica replica) { |
| 139 | + double futureUsage = disk.utilization() + replica.load().expectedUtilizationFor(Resource.DISK); |
| 140 | + double remainingSpacePercentage = (1 - (futureUsage / disk.capacity())); |
| 141 | + return remainingSpacePercentage > _errorMargin |
| 142 | + || (remainingSpacePercentage > 0 && Math.abs(remainingSpacePercentage - _errorMargin) < EPSILON); |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public ActionAcceptance actionAcceptance(BalancingAction action, ClusterModel clusterModel) { |
| 147 | + return ACCEPT; |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public ClusterModelStatsComparator clusterModelStatsComparator() { |
| 152 | + return new ClusterModelStatsComparator() { |
| 153 | + @Override |
| 154 | + public int compare(ClusterModelStats stats1, ClusterModelStats stats2) { |
| 155 | + return 0; |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public String explainLastComparison() { |
| 160 | + return String.format("Comparison for the %s is irrelevant.", name()); |
| 161 | + } |
| 162 | + }; |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public ModelCompletenessRequirements clusterModelCompletenessRequirements() { |
| 167 | + return new ModelCompletenessRequirements(MIN_NUM_VALID_WINDOWS_FOR_SELF_HEALING, 0, true); |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public String name() { |
| 172 | + return DiskRemovalGoal.class.getSimpleName(); |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public void finish() { |
| 177 | + |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public boolean isHardGoal() { |
| 182 | + return false; |
| 183 | + } |
| 184 | + |
| 185 | + @Override |
| 186 | + public ProvisionStatus provisionStatus() { |
| 187 | + // Provision status computation is not relevant to PLE goal. |
| 188 | + return provisionResponse().status(); |
| 189 | + } |
| 190 | + |
| 191 | + @Override |
| 192 | + public ProvisionResponse provisionResponse() { |
| 193 | + return _provisionResponse; |
| 194 | + } |
| 195 | + |
| 196 | + @Override |
| 197 | + public void configure(Map<String, ?> configs) { |
| 198 | + |
| 199 | + } |
| 200 | +} |
0 commit comments