Skip to content

Commit 9665ffb

Browse files
authored
feat(auto_balancer): allow change broker rack on register (AutoMQ#1771) (AutoMQ#1776)
Signed-off-by: Shichao Nie <niesc@automq.com>
1 parent bbd95f4 commit 9665ffb

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

core/src/main/java/kafka/autobalancer/model/BrokerUpdater.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
public class BrokerUpdater extends AbstractInstanceUpdater {
2626
private final int brokerId;
27-
private final String rack;
27+
private String rack;
2828
private boolean active;
2929

3030
public BrokerUpdater(int brokerId, String rack, boolean active) {
@@ -41,6 +41,10 @@ public String rack() {
4141
return this.rack;
4242
}
4343

44+
public void setRack(String rack) {
45+
this.rack = rack;
46+
}
47+
4448
public boolean isActive() {
4549
lock.lock();
4650
try {

core/src/main/java/kafka/autobalancer/model/ClusterModel.java

+10-6
Original file line numberDiff line numberDiff line change
@@ -237,15 +237,19 @@ public boolean updateTopicPartitionMetrics(int brokerId, TopicPartition tp, Iter
237237
public void registerBroker(int brokerId, String rackId, boolean active) {
238238
clusterLock.lock();
239239
try {
240-
if (brokerMap.containsKey(brokerId)) {
241-
return;
242-
}
243240
if (Utils.isBlank(rackId)) {
244241
rackId = DEFAULT_RACK_ID;
245242
}
246-
BrokerUpdater brokerUpdater = createBrokerUpdater(brokerId, rackId, active);
247-
brokerMap.putIfAbsent(brokerId, brokerUpdater);
248-
brokerReplicaMap.put(brokerId, new HashMap<>());
243+
String finalRackId = rackId;
244+
brokerMap.compute(brokerId, (id, brokerUpdater) -> {
245+
if (brokerUpdater == null) {
246+
brokerReplicaMap.put(brokerId, new HashMap<>());
247+
return createBrokerUpdater(brokerId, finalRackId, active);
248+
}
249+
brokerUpdater.setRack(finalRackId);
250+
brokerUpdater.setActive(active);
251+
return brokerUpdater;
252+
});
249253
} finally {
250254
clusterLock.unlock();
251255
}

0 commit comments

Comments
 (0)