From a362eaa8bd8caf13795aba5ddbdf4edd8cec7bd0 Mon Sep 17 00:00:00 2001 From: houxiaoyu Date: Sat, 29 Apr 2023 01:57:43 +0800 Subject: [PATCH] [improve][broker] Improve knownBrokers update in ModularLoadManagerImpl (#20196) --- .../loadbalance/impl/ModularLoadManagerImpl.java | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/impl/ModularLoadManagerImpl.java b/pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/impl/ModularLoadManagerImpl.java index 30a2ef5cdf250..73b4f318f3a36 100644 --- a/pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/impl/ModularLoadManagerImpl.java +++ b/pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/impl/ModularLoadManagerImpl.java @@ -195,7 +195,7 @@ public class ModularLoadManagerImpl implements ModularLoadManager { private long unloadBundleCount = 0; private final Lock lock = new ReentrantLock(); - private Set knownBrokers = ConcurrentHashMap.newKeySet(); + private final Set knownBrokers = new HashSet<>(); private Map bundleBrokerAffinityMap; /** @@ -480,13 +480,11 @@ public void updateAll() { checkNamespaceBundleSplit(); } - private void cleanupDeadBrokersData() { + private synchronized void cleanupDeadBrokersData() { final Set activeBrokers = getAvailableBrokers(); - final Set knownBrokersCopy = new HashSet<>(this.knownBrokers); - Collection newBrokers = CollectionUtils.subtract(activeBrokers, knownBrokersCopy); - this.knownBrokers.addAll(newBrokers); - Collection deadBrokers = CollectionUtils.subtract(knownBrokersCopy, activeBrokers); - this.knownBrokers.removeAll(deadBrokers); + Collection deadBrokers = CollectionUtils.subtract(knownBrokers, activeBrokers); + this.knownBrokers.clear(); + this.knownBrokers.addAll(activeBrokers); if (pulsar.getLeaderElectionService() != null && pulsar.getLeaderElectionService().isLeader()) { deadBrokers.forEach(this::deleteTimeAverageDataFromMetadataStoreAsync);