thetumbled commented on code in PR #23217:
URL: https://github.com/apache/pulsar/pull/23217#discussion_r1732087910


##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/impl/ModularLoadManagerImpl.java:
##########
@@ -873,17 +857,7 @@ private void preallocateBundle(String bundle, String 
broker) {
 
         final String namespaceName = 
LoadManagerShared.getNamespaceNameFromBundleName(bundle);
         final String bundleRange = 
LoadManagerShared.getBundleRangeFromBundleName(bundle);
-        final ConcurrentOpenHashMap<String, ConcurrentOpenHashSet<String>> 
namespaceToBundleRange =
-                brokerToNamespaceToBundleRange
-                        .computeIfAbsent(broker,
-                                k -> ConcurrentOpenHashMap.<String,
-                                        
ConcurrentOpenHashSet<String>>newBuilder()
-                                        .build());
-        synchronized (namespaceToBundleRange) {
-            namespaceToBundleRange.computeIfAbsent(namespaceName,
-                    k -> ConcurrentOpenHashSet.<String>newBuilder().build())
-                    .add(bundleRange);
-        }
+        brokerToNamespaceToBundleRange.addBundleRange(broker, namespaceName, 
bundleRange);
     }

Review Comment:
   Can we guarantee thread safety if we remove the synchronize lock?



##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/impl/BundleRangeCache.java:
##########
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.broker.loadbalance.impl;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.function.BiFunction;
+import java.util.stream.Stream;
+
+public class BundleRangeCache {
+
+    // Map from brokers to namespaces to the bundle ranges in that namespace 
assigned to that broker.
+    // Used to distribute bundles within a namespace evenly across brokers.
+    private final Map<String, Map<String, Set<String>>> data = new 
ConcurrentHashMap<>();
+
+    public void reloadFromBundles(String broker, Stream<String> bundles) {
+        final var namespaceToBundleRange = new ConcurrentHashMap<String, 
Set<String>>();
+        bundles.forEach(bundleName -> {
+            final String namespace = 
LoadManagerShared.getNamespaceNameFromBundleName(bundleName);
+            final String bundleRange = 
LoadManagerShared.getBundleRangeFromBundleName(bundleName);
+            initConcurrentHashSet(namespaceToBundleRange, 
namespace).add(bundleRange);
+        });
+        data.put(broker, namespaceToBundleRange);
+    }
+
+    public void addBundleRange(String broker, String namespace, String 
bundleRange) {
+        getBundleRangeSet(broker, namespace).add(bundleRange);
+    }
+
+    public int getBundles(String broker, String namespace) {

Review Comment:
   rename to `getBundleCount`.



##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/impl/BundleRangeCache.java:
##########
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.broker.loadbalance.impl;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.function.BiFunction;
+import java.util.stream.Stream;
+
+public class BundleRangeCache {
+
+    // Map from brokers to namespaces to the bundle ranges in that namespace 
assigned to that broker.
+    // Used to distribute bundles within a namespace evenly across brokers.
+    private final Map<String, Map<String, Set<String>>> data = new 
ConcurrentHashMap<>();
+
+    public void reloadFromBundles(String broker, Stream<String> bundles) {
+        final var namespaceToBundleRange = new ConcurrentHashMap<String, 
Set<String>>();
+        bundles.forEach(bundleName -> {
+            final String namespace = 
LoadManagerShared.getNamespaceNameFromBundleName(bundleName);
+            final String bundleRange = 
LoadManagerShared.getBundleRangeFromBundleName(bundleName);
+            initConcurrentHashSet(namespaceToBundleRange, 
namespace).add(bundleRange);
+        });
+        data.put(broker, namespaceToBundleRange);
+    }
+
+    public void addBundleRange(String broker, String namespace, String 
bundleRange) {
+        getBundleRangeSet(broker, namespace).add(bundleRange);
+    }

Review Comment:
   may be we need to add some concurrent control in BundleRangeCache.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to