|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.astraea.common.balancer; |
| 18 | + |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.Collection; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.function.Function; |
| 23 | +import java.util.function.Predicate; |
| 24 | +import java.util.regex.Pattern; |
| 25 | +import java.util.stream.Collectors; |
| 26 | +import java.util.stream.IntStream; |
| 27 | +import java.util.stream.Stream; |
| 28 | +import org.astraea.common.EnumInfo; |
| 29 | +import org.astraea.common.admin.ClusterInfo; |
| 30 | +import org.astraea.common.admin.NodeInfo; |
| 31 | +import org.astraea.common.admin.Replica; |
| 32 | + |
| 33 | +public final class BalancerUtils { |
| 34 | + |
| 35 | + private BalancerUtils() {} |
| 36 | + |
| 37 | + public static Map<Integer, BalancingModes> balancingMode(ClusterInfo cluster, String config) { |
| 38 | + var num = Pattern.compile("[0-9]+"); |
| 39 | + |
| 40 | + var map = |
| 41 | + Arrays.stream(config.split(",")) |
| 42 | + .filter(Predicate.not(String::isEmpty)) |
| 43 | + .map(x -> x.split(":")) |
| 44 | + .collect( |
| 45 | + Collectors.toUnmodifiableMap( |
| 46 | + s -> (Object) (num.matcher(s[0]).find() ? Integer.parseInt(s[0]) : s[0]), |
| 47 | + s -> |
| 48 | + switch (s[1]) { |
| 49 | + case "balancing" -> BalancingModes.BALANCING; |
| 50 | + case "demoted" -> BalancingModes.DEMOTED; |
| 51 | + case "excluded" -> BalancingModes.EXCLUDED; |
| 52 | + default -> throw new IllegalArgumentException( |
| 53 | + "Unsupported balancing mode: " + s[1]); |
| 54 | + })); |
| 55 | + |
| 56 | + Function<Integer, BalancingModes> mode = |
| 57 | + (id) -> map.getOrDefault(id, map.getOrDefault("default", BalancingModes.BALANCING)); |
| 58 | + |
| 59 | + return cluster.brokers().stream() |
| 60 | + .map(NodeInfo::id) |
| 61 | + .collect(Collectors.toUnmodifiableMap(Function.identity(), mode)); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Verify there is no logic conflict between {@link BalancerConfigs#BALANCER_ALLOWED_TOPICS_REGEX} |
| 66 | + * and {@link BalancerConfigs#BALANCER_BROKER_BALANCING_MODE}. It also performs other common |
| 67 | + * validness checks to the cluster. |
| 68 | + */ |
| 69 | + public static void verifyClearBrokerValidness( |
| 70 | + ClusterInfo cluster, Predicate<Integer> isDemoted, Predicate<String> allowedTopics) { |
| 71 | + var disallowedTopicsToClear = |
| 72 | + cluster.topicPartitionReplicas().stream() |
| 73 | + .filter(tpr -> isDemoted.test(tpr.brokerId())) |
| 74 | + .filter(tpr -> !allowedTopics.test(tpr.topic())) |
| 75 | + .collect(Collectors.toUnmodifiableSet()); |
| 76 | + if (!disallowedTopicsToClear.isEmpty()) |
| 77 | + throw new IllegalArgumentException( |
| 78 | + "Attempts to clear some brokers, but some of them contain topics that forbidden from being changed due to \"" |
| 79 | + + BalancerConfigs.BALANCER_ALLOWED_TOPICS_REGEX |
| 80 | + + "\": " |
| 81 | + + disallowedTopicsToClear); |
| 82 | + |
| 83 | + var ongoingEventReplica = |
| 84 | + cluster.replicas().stream() |
| 85 | + .filter(r -> isDemoted.test(r.nodeInfo().id())) |
| 86 | + .filter(r -> r.isAdding() || r.isRemoving() || r.isFuture()) |
| 87 | + .map(Replica::topicPartitionReplica) |
| 88 | + .collect(Collectors.toUnmodifiableSet()); |
| 89 | + if (!ongoingEventReplica.isEmpty()) |
| 90 | + throw new IllegalArgumentException( |
| 91 | + "Attempts to clear broker with ongoing migration event (adding/removing/future replica): " |
| 92 | + + ongoingEventReplica); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Move all the replicas at the demoting broker to other allowed brokers. <b>BE CAREFUL, The |
| 97 | + * implementation made no assumption for MoveCost or ClusterCost of the returned ClusterInfo.</b> |
| 98 | + * Be aware of this limitation before using it as the starting point for a solution search. Some |
| 99 | + * balancer implementation might have trouble finding answer when starting at a state where the |
| 100 | + * MoveCost is already violated. |
| 101 | + */ |
| 102 | + public static ClusterInfo clearedCluster( |
| 103 | + ClusterInfo initial, Predicate<Integer> clearBrokers, Predicate<Integer> allowedBrokers) { |
| 104 | + final var allowed = |
| 105 | + initial.nodes().stream() |
| 106 | + .filter(node -> allowedBrokers.test(node.id())) |
| 107 | + .filter(node -> Predicate.not(clearBrokers).test(node.id())) |
| 108 | + .collect(Collectors.toUnmodifiableSet()); |
| 109 | + final var nextBroker = Stream.generate(() -> allowed).flatMap(Collection::stream).iterator(); |
| 110 | + final var nextBrokerFolder = |
| 111 | + initial.brokerFolders().entrySet().stream() |
| 112 | + .collect( |
| 113 | + Collectors.toUnmodifiableMap( |
| 114 | + Map.Entry::getKey, |
| 115 | + x -> Stream.generate(x::getValue).flatMap(Collection::stream).iterator())); |
| 116 | + |
| 117 | + var trackingReplicaList = |
| 118 | + initial.topicPartitions().stream() |
| 119 | + .collect( |
| 120 | + Collectors.toUnmodifiableMap( |
| 121 | + tp -> tp, |
| 122 | + tp -> |
| 123 | + initial.replicas(tp).stream() |
| 124 | + .map(Replica::nodeInfo) |
| 125 | + .collect(Collectors.toSet()))); |
| 126 | + return ClusterInfo.builder(initial) |
| 127 | + .mapLog( |
| 128 | + replica -> { |
| 129 | + if (!clearBrokers.test(replica.nodeInfo().id())) return replica; |
| 130 | + var currentReplicaList = trackingReplicaList.get(replica.topicPartition()); |
| 131 | + var broker = |
| 132 | + IntStream.range(0, allowed.size()) |
| 133 | + .mapToObj(i -> nextBroker.next()) |
| 134 | + .filter(b -> !currentReplicaList.contains(b)) |
| 135 | + .findFirst() |
| 136 | + .orElseThrow( |
| 137 | + () -> |
| 138 | + new IllegalStateException( |
| 139 | + "Unable to clear replica " |
| 140 | + + replica.topicPartitionReplica() |
| 141 | + + " for broker " |
| 142 | + + replica.nodeInfo().id() |
| 143 | + + ", the allowed destination brokers are " |
| 144 | + + allowed.stream() |
| 145 | + .map(NodeInfo::id) |
| 146 | + .collect(Collectors.toUnmodifiableSet()) |
| 147 | + + " but all of them already hosting a replica for this partition. " |
| 148 | + + "There is no broker can adopt this replica.")); |
| 149 | + var folder = nextBrokerFolder.get(broker.id()).next(); |
| 150 | + |
| 151 | + // update the tracking list. have to do this to avoid putting two replicas from the |
| 152 | + // same tp to one broker. |
| 153 | + currentReplicaList.remove(replica.nodeInfo()); |
| 154 | + currentReplicaList.add(broker); |
| 155 | + |
| 156 | + return Replica.builder(replica).nodeInfo(broker).path(folder).build(); |
| 157 | + }) |
| 158 | + .build(); |
| 159 | + } |
| 160 | + |
| 161 | + public enum BalancingModes implements EnumInfo { |
| 162 | + BALANCING, |
| 163 | + DEMOTED, |
| 164 | + EXCLUDED; |
| 165 | + |
| 166 | + public static BalancingModes ofAlias(String alias) { |
| 167 | + return EnumInfo.ignoreCaseEnum(BalancingModes.class, alias); |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public String alias() { |
| 172 | + return name(); |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public String toString() { |
| 177 | + return alias(); |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments