Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat][broker]PIP-255 Part-2: Make the partition assignment strategy pluggable #20537

Merged
merged 30 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions conf/broker.conf
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,9 @@ loadBalancerOverrideBrokerNicSpeedGbps=
# Name of load manager to use
loadManagerClassName=org.apache.pulsar.broker.loadbalance.impl.ModularLoadManagerImpl

#Name of topic bundle assignment strategy to use
BewareMyPower marked this conversation as resolved.
Show resolved Hide resolved
topicBundleAssignmentStrategy=org.apache.pulsar.common.naming.ConsistentHashingTopicBundleAssigner

# Supported algorithms name for namespace bundle split.
# "range_equally_divide" divides the bundle into two parts with the same hash range size.
# "topic_count_equally_divide" divides the bundle into two parts with the same topics count.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2477,6 +2477,11 @@ The delayed message index time step(in seconds) in per bucket snapshot segment,
doc = "Name of load manager to use"
)
private String loadManagerClassName = "org.apache.pulsar.broker.loadbalance.impl.ModularLoadManagerImpl";


315157973 marked this conversation as resolved.
Show resolved Hide resolved
@FieldContext(category = CATEGORY_LOAD_BALANCER, doc = "Name of topic bundle assignment strategy to use")
private String topicBundleAssignmentStrategy =
"org.apache.pulsar.common.naming.ConsistentHashingTopicBundleAssigner";
@FieldContext(
dynamic = true,
category = CATEGORY_LOAD_BALANCER,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.common.naming;

import static com.google.common.base.Preconditions.checkArgument;
import org.apache.pulsar.broker.ServiceConfiguration;
import org.apache.pulsar.broker.namespace.NamespaceService;
import org.apache.pulsar.client.admin.PulsarAdmin;

public class ConsistentHashingTopicBundleAssigner implements TopicBundleAssignmentStrategy {
private NamespaceService namespaceService;
@Override
public NamespaceBundle findBundle(TopicName topicName, NamespaceBundles namespaceBundles) {
long hashCode = namespaceService.getNamespaceBundleFactory().getLongHashCode(topicName.toString());
NamespaceBundle bundle = namespaceBundles.getBundle(hashCode);
checkArgument(bundle.getNamespaceObject().equals(topicName.getNamespaceObject()));
315157973 marked this conversation as resolved.
Show resolved Hide resolved
if (topicName.getDomain().equals(TopicDomain.non_persistent)) {
bundle.setHasNonPersistentTopic(true);
}
return bundle;
}

@Override
public void init(NamespaceService namespaceService, PulsarAdmin pulsarAdmin, ServiceConfiguration configuration) {
this.namespaceService = namespaceService;
}
}
BewareMyPower marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,10 @@ public boolean canSplitBundle(NamespaceBundle bundle) {
return range.upperEndpoint() - range.lowerEndpoint() > 1;
}

public PulsarService getPulsar() {
BewareMyPower marked this conversation as resolved.
Show resolved Hide resolved
return pulsar;
}

public static void validateFullRange(SortedSet<String> partitions) {
checkArgument(partitions.first().equals(FIRST_BOUNDARY) && partitions.last().equals(LAST_BOUNDARY));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class NamespaceBundles {
public static final Long FULL_LOWER_BOUND = 0x00000000L;
public static final Long FULL_UPPER_BOUND = 0xffffffffL;
private final NamespaceBundle fullBundle;

private final TopicBundleAssignmentStrategy topicBundleAssignmentStrategy;
private final Optional<Pair<LocalPolicies, Long>> localPolicies;

public NamespaceBundles(NamespaceName nsname, NamespaceBundleFactory factory,
Expand All @@ -66,7 +66,7 @@ public NamespaceBundles(NamespaceName nsname, NamespaceBundleFactory factory,
this.factory = Objects.requireNonNull(factory);
this.localPolicies = localPolicies;
checkArgument(partitions.length > 0, "Can't create bundles w/o partition boundaries");

this.topicBundleAssignmentStrategy = TopicBundleAssignmentFactory.create(factory.getPulsar());
315157973 marked this conversation as resolved.
Show resolved Hide resolved
// calculate bundles based on partition boundaries
this.bundles = new ArrayList<>();
fullBundle = new NamespaceBundle(nsname,
Expand Down Expand Up @@ -94,13 +94,7 @@ public NamespaceBundles(NamespaceName nsname, NamespaceBundleFactory factory,
}

public NamespaceBundle findBundle(TopicName topicName) {
checkArgument(this.nsname.equals(topicName.getNamespaceObject()));
long hashCode = factory.getLongHashCode(topicName.toString());
NamespaceBundle bundle = getBundle(hashCode);
if (topicName.getDomain().equals(TopicDomain.non_persistent)) {
bundle.setHasNonPersistentTopic(true);
}
return bundle;
return topicBundleAssignmentStrategy.findBundle(topicName, this);
}

public List<NamespaceBundle> getBundles() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.common.naming;

import org.apache.pulsar.broker.PulsarService;
import org.apache.pulsar.broker.ServiceConfiguration;
import org.apache.pulsar.broker.namespace.NamespaceService;
import org.apache.pulsar.common.util.Reflections;

public class TopicBundleAssignmentFactory {

public static TopicBundleAssignmentStrategy create(PulsarService pulsar) {
ServiceConfiguration conf = pulsar.getConfiguration();
NamespaceService namespaceService = pulsar.getNamespaceService();
try {
TopicBundleAssignmentStrategy strategy = Reflections.createInstance(conf.getTopicBundleAssignmentStrategy(),
TopicBundleAssignmentStrategy.class, Thread.currentThread().getContextClassLoader());
strategy.init(namespaceService, pulsar.getAdminClient(), pulsar.getConfiguration());
return strategy;
} catch (Exception e) {
throw new RuntimeException(
"Could not load TopicBundleAssignmentStrategy:" + conf.getTopicBundleAssignmentStrategy(), e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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.common.naming;

import org.apache.pulsar.broker.ServiceConfiguration;
import org.apache.pulsar.broker.namespace.NamespaceService;
import org.apache.pulsar.client.admin.PulsarAdmin;

public interface TopicBundleAssignmentStrategy {
NamespaceBundle findBundle(TopicName topicName, NamespaceBundles namespaceBundles);
315157973 marked this conversation as resolved.
Show resolved Hide resolved

void init(NamespaceService namespaceService, PulsarAdmin pulsarAdmin, ServiceConfiguration configuration);
315157973 marked this conversation as resolved.
Show resolved Hide resolved
}
BewareMyPower marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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.common.naming;

import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;

import com.google.common.collect.BoundType;
import com.google.common.collect.Range;

import org.apache.pulsar.broker.PulsarService;
import org.apache.pulsar.broker.ServiceConfiguration;
import org.apache.pulsar.broker.namespace.NamespaceService;
import org.apache.pulsar.client.admin.PulsarAdmin;
import org.testng.Assert;
import org.testng.annotations.Test;

@Test(groups = "broker-naming")
public class TopicBundleAssignmentStrategyTest {
@Test
public void testStrategyFactory() {
ServiceConfiguration conf = new ServiceConfiguration();
conf.setTopicBundleAssignmentStrategy(
"org.apache.pulsar.common.naming.TopicBundleAssignmentStrategyTest$TestStrategy");
PulsarService pulsarService = mock(PulsarService.class);
doReturn(conf).when(pulsarService).getConfiguration();
TopicBundleAssignmentStrategy strategy = TopicBundleAssignmentFactory.create(pulsarService);
NamespaceBundle bundle = strategy.findBundle(null, null);
Range<Long> keyRange = Range.range(0L, BoundType.CLOSED, 0xffffffffL, BoundType.CLOSED);
String range = String.format("0x%08x_0x%08x", keyRange.lowerEndpoint(), keyRange.upperEndpoint());
Assert.assertEquals(bundle.getBundleRange(), range);
Assert.assertEquals(bundle.getNamespaceObject(), NamespaceName.get("my/test"));
}

public static class TestStrategy implements TopicBundleAssignmentStrategy {
@Override
public NamespaceBundle findBundle(TopicName topicName, NamespaceBundles namespaceBundles) {
Range<Long> range = Range.range(0L, BoundType.CLOSED, 0xffffffffL, BoundType.CLOSED);
return new NamespaceBundle(NamespaceName.get("my/test"), range,
mock(NamespaceBundleFactory.class));
}

@Override
public void init(NamespaceService namespaceService, PulsarAdmin pulsarAdmin,
ServiceConfiguration configuration) {
}
}

}
BewareMyPower marked this conversation as resolved.
Show resolved Hide resolved