forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[improve][broker] Make BrokerSelectionStrategy pluggable (apache#22553)
- Loading branch information
1 parent
358c7cc
commit 89b201e
Showing
4 changed files
with
123 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
.../apache/pulsar/broker/loadbalance/extensions/strategy/BrokerSelectionStrategyFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* 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.extensions.strategy; | ||
|
||
import org.apache.pulsar.common.classification.InterfaceStability; | ||
|
||
@InterfaceStability.Stable | ||
public interface BrokerSelectionStrategyFactory { | ||
|
||
BrokerSelectionStrategy createBrokerSelectionStrategy(); | ||
} |
86 changes: 86 additions & 0 deletions
86
...ache/pulsar/broker/loadbalance/extensions/strategy/CustomBrokerSelectionStrategyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* 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.extensions.strategy; | ||
|
||
import java.util.Comparator; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import lombok.Cleanup; | ||
import org.apache.pulsar.broker.MultiBrokerBaseTest; | ||
import org.apache.pulsar.broker.PulsarService; | ||
import org.apache.pulsar.broker.ServiceConfiguration; | ||
import org.apache.pulsar.broker.loadbalance.extensions.ExtensibleLoadManagerImpl; | ||
import org.apache.pulsar.broker.loadbalance.extensions.scheduler.TransferShedder; | ||
import org.apache.pulsar.client.impl.PartitionedProducerImpl; | ||
import org.apache.pulsar.client.impl.ProducerImpl; | ||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
@Test(groups = "broker") | ||
public class CustomBrokerSelectionStrategyTest extends MultiBrokerBaseTest { | ||
|
||
@Override | ||
protected void startBroker() throws Exception { | ||
addCustomConfigs(conf); | ||
super.startBroker(); | ||
} | ||
|
||
@Override | ||
protected ServiceConfiguration createConfForAdditionalBroker(int additionalBrokerIndex) { | ||
return addCustomConfigs(getDefaultConf()); | ||
} | ||
|
||
private static ServiceConfiguration addCustomConfigs(ServiceConfiguration conf) { | ||
conf.setLoadManagerClassName(CustomExtensibleLoadManager.class.getName()); | ||
conf.setLoadBalancerLoadSheddingStrategy(TransferShedder.class.getName()); | ||
conf.setLoadBalancerAutoBundleSplitEnabled(false); | ||
conf.setDefaultNumberOfNamespaceBundles(8); | ||
// Don't consider broker's load so the broker will be selected randomly with the default strategy | ||
conf.setLoadBalancerAverageResourceUsageDifferenceThresholdPercentage(100); | ||
return conf; | ||
} | ||
|
||
@Test | ||
public void testSingleBrokerSelected() throws Exception { | ||
final var topic = "test-single-broker-selected"; | ||
getAllAdmins().get(0).topics().createPartitionedTopic(topic, 16); | ||
@Cleanup final var producer = (PartitionedProducerImpl<byte[]>) getAllClients().get(0).newProducer() | ||
.topic(topic).create(); | ||
Assert.assertNotNull(producer); | ||
final var connections = producer.getProducers().stream().map(ProducerImpl::getClientCnx) | ||
.collect(Collectors.toSet()); | ||
Assert.assertEquals(connections.size(), 1); | ||
final var port = Integer.parseInt(connections.stream().findFirst().orElseThrow().ctx().channel() | ||
.remoteAddress().toString().replaceAll(".*:", "")); | ||
final var expectedPort = Stream.concat(Stream.of(pulsar), additionalBrokers.stream()) | ||
.min(Comparator.comparingInt(o -> o.getListenPortHTTP().orElseThrow())) | ||
.map(PulsarService::getBrokerListenPort) | ||
.orElseThrow().orElseThrow(); | ||
Assert.assertEquals(port, expectedPort); | ||
} | ||
|
||
public static class CustomExtensibleLoadManager extends ExtensibleLoadManagerImpl { | ||
|
||
@Override | ||
public BrokerSelectionStrategy createBrokerSelectionStrategy() { | ||
// The smallest HTTP port will always be selected because the host parts are all "localhost" | ||
return (brokers, __, ___) -> brokers.stream().sorted().findFirst(); | ||
} | ||
} | ||
} |