Skip to content

Commit

Permalink
[improve][broker] Improve the extensibility of the TopicBundleAssignm…
Browse files Browse the repository at this point in the history
…entStrategy interface class (#23773)
  • Loading branch information
rayluoluo committed Jan 7, 2025
1 parent 817e419 commit b8efc53
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import org.apache.pulsar.broker.PulsarService;
import org.apache.pulsar.broker.ServiceConfiguration;
import org.apache.pulsar.broker.namespace.NamespaceService;

public class ConsistentHashingTopicBundleAssigner implements TopicBundleAssignmentStrategy {
private PulsarService pulsar;

private volatile HashFunction hashFunction;

@Override
public NamespaceBundle findBundle(TopicName topicName, NamespaceBundles namespaceBundles) {
NamespaceBundle bundle = namespaceBundles.getBundle(calculateBundleHashCode(topicName));
Expand All @@ -36,7 +41,10 @@ public NamespaceBundle findBundle(TopicName topicName, NamespaceBundles namespac

@Override
public long calculateBundleHashCode(TopicName topicName) {
return getBundleHashFunc().hashString(topicName.toString(), StandardCharsets.UTF_8).padToLong();
if (hashFunction == null) {
hashFunction = getBundleHashFunc();
}
return hashFunction.hashString(topicName.toString(), StandardCharsets.UTF_8).padToLong();
}

@Override
Expand All @@ -45,6 +53,7 @@ public void init(PulsarService pulsarService) {
}

private HashFunction getBundleHashFunc() {
return Optional.of(pulsar.getNamespaceService().getNamespaceBundleFactory().getHashFunc()).get();
return Optional.ofNullable(pulsar.getNamespaceService()).map(NamespaceService::getNamespaceBundleFactory)
.map(NamespaceBundleFactory::getHashFunc).orElseThrow(() -> new RuntimeException("HashFunc not specified"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

import com.google.common.hash.Hashing;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.util.HashSet;
import java.util.Optional;
Expand Down

0 comments on commit b8efc53

Please sign in to comment.