rayluoluo commented on code in PR #23774:
URL: https://github.com/apache/pulsar/pull/23774#discussion_r1904870755


##########
pulsar-broker/src/main/java/org/apache/pulsar/common/naming/ConsistentHashingTopicBundleAssigner.java:
##########
@@ -23,18 +23,26 @@
 import org.apache.pulsar.broker.PulsarService;
 
 public class ConsistentHashingTopicBundleAssigner implements 
TopicBundleAssignmentStrategy {
+    private PulsarService pulsar;
     @Override
     public NamespaceBundle findBundle(TopicName topicName, NamespaceBundles 
namespaceBundles) {
-        long hashCode = Hashing.crc32().hashString(topicName.toString(), 
StandardCharsets.UTF_8).padToLong();
-        NamespaceBundle bundle = namespaceBundles.getBundle(hashCode);
+        NamespaceBundle bundle = 
namespaceBundles.getBundle(getHashCode(topicName.toString()));
         if (topicName.getDomain().equals(TopicDomain.non_persistent)) {
             bundle.setHasNonPersistentTopic(true);
         }
         return bundle;
     }
 
+    @Override
+    public long getHashCode(String name) {
+        return 
pulsar.getNamespaceService().getNamespaceBundleFactory().getHashFunc()
+                .hashString(name, StandardCharsets.UTF_8)
+                .padToLong();
+    }
+
     @Override
     public void init(PulsarService pulsarService) {
+        this.pulsar = pulsarService;

Review Comment:
   HashFunc is initialized in the NamespaceBundleFactory. When the 
ConsistentHashingTopicBundleAssigner#init() method needs to be called to 
initialize the bundle allocation algorithm instance, 
pulsar.getNamespaceService() is not ready, 
pulsar.getNamespaceService().getNamespaceBundleFactory() will cause NPE. 
Therefore, HashFunc is more suitable for lazy acquisition.



-- 
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