[improve][broker] Improve the extensibility of the TopicBundleAssignmentStrategy interface class #23773
Open
2 tasks done
Labels
type/enhancement
The enhancements for the existing features or docs. e.g. reduce memory usage of the delayed messages
Search before asking
Motivation
During the lookup process of the pulsar broker, the hash value needs to be calculated based on the topic name. For example:
Bundle obtaining triggered by a lookup request:
Lookup -> NamespaceService#getBrokerServiceUrlAsync -> NamespaceService#getBundleAsync ->
NamespaceBundles#findBundle -> TopicBundleAssignmentStrategy#findBundle -> NamespaceBundles#getBundle(long hash)
When loading a topic, the broker needs to determine whether it owns the topic partition.
PulsarService#loadNamespaceTopics -> NamespaceBundle#includes -> NamespaceBundleFactory#getLongHashCode -> NamespaceBundle.keyRange#contains(long hash)
The current code implementation has the following problems:
The hash algorithm is fixed. When the load balancing algorithm is extended, the bundle to which the partition belongs cannot be adjusted. As a result, other algorithms such as RoundRobin cannot be extended.
The
NamespaceBundleFactory#getLongHashCode
method uses a fixed algorithm to calculate the hash value. Therefore, it is difficult to extend the implementation of theTopicBundleAssignmentStrategy
interface class that uses different hash algorithms without modifying theNamespaceBundleFactory#getLongHashCode
method, which violates the open and closed principles.Bad code smell (shot-like modification): The hash algorithm is implemented in the
findBundle
andgetLongHashCode
methods. The system must ensure that the calculated hash results are the same. Otherwise, split-brain occurs in the cluster. Therefore, if the hash algorithm needs to be modified, the code has a bad smell.Lookup request:
Take the default implementation class
ConsistentHashingTopicBundleAssigner
of theTopicBundleAssignmentStrategy
interface class as an example. During the lookup process, the hash value is calculated inConsistentHashingTopicBundleAssigner#findBundle
.pulsar/pulsar-broker/src/main/java/org/apache/pulsar/common/naming/ConsistentHashingTopicBundleAssigner.java
Lines 25 to 40 in 1967a93
When a topic is loaded, the hash value is calculated in the
NamespaceBundleFactory#getLongHashCode
method to determine whether the current broker owns the topic.pulsar/pulsar-broker/src/main/java/org/apache/pulsar/broker/namespace/NamespaceService.java
Line 204 in 1967a93
pulsar/pulsar-broker/src/main/java/org/apache/pulsar/common/naming/NamespaceBundleFactory.java
Lines 78 to 79 in 1967a93
pulsar/pulsar-broker/src/main/java/org/apache/pulsar/common/naming/NamespaceBundleFactory.java
Lines 294 to 296 in 1967a93
Solution
It is recommended that the implementation of the
NamespaceBundleFactory#getLongHashCode
method be moved to the implementation class of the interfaceTopicBundleAssignmentStrategy
. Therefore, we may add a new methodlong getHashCode(String name)
to theTopicBundleAssignmentStrategy
interface class. The implementation of the hash algorithm is no longer fixed in theNamespaceBundleFactory#getLongHashCode
method. Instead, thegetHashCode
method implemented by different algorithms is invoked.Alternatives
No response
Anything else?
No response
Are you willing to submit a PR?
The text was updated successfully, but these errors were encountered: