Skip to content

Commit

Permalink
[pulsar-broker] add uniform load shedder strategy to distribute traff…
Browse files Browse the repository at this point in the history
…ic uniformly across brokers
  • Loading branch information
rdhabalia committed Nov 20, 2021
1 parent ec715f2 commit 4f9fcf8
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 0 deletions.
12 changes: 12 additions & 0 deletions conf/broker.conf
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,18 @@ loadBalancerLoadSheddingStrategy=org.apache.pulsar.broker.loadbalance.impl.Overl
# It only takes effect in the ThresholdShedder strategy.
loadBalancerBrokerThresholdShedderPercentage=10

# Message-rate percentage threshold between highest and least loaded brokers for
# uniform load shedding. (eg: broker1 with 50K msgRate and broker2 with 30K msgRate
# will have 66% msgRate difference and load balancer can unload bundles from broker-1
# to broker-2)
loadBalancerMsgRateDifferenceShedderThreshold=50

# Message-throughput threshold between highest and least loaded brokers for
# uniform load shedding. (eg: broker1 with 450MB msgRate and broker2 with 100MB msgRate
# will have 4.5 times msgThroughout difference and load balancer can unload bundles
# from broker-1 to broker-2)
loadBalancerMsgThroughputMultiplierDifferenceShedderThreshold=4

# When calculating new resource usage, the history usage accounts for.
# It only takes effect in the ThresholdShedder strategy.
loadBalancerHistoryResourcePercentage=0.9
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1773,6 +1773,25 @@ public class ServiceConfiguration implements PulsarConfiguration {
)
private int loadBalancerBrokerThresholdShedderPercentage = 10;

@FieldContext(
dynamic = true,
category = CATEGORY_LOAD_BALANCER,
doc = "Message-rate percentage threshold between highest and least loaded brokers for "
+ "uniform load shedding. (eg: broker1 with 50K msgRate and broker2 with 30K msgRate "
+ "will have 66% msgRate difference and load balancer can unload bundles from broker-1 "
+ "to broker-2)"
)
private int loadBalancerMsgRateDifferenceShedderThreshold = 50;
@FieldContext(
dynamic = true,
category = CATEGORY_LOAD_BALANCER,
doc = "Message-throughput threshold between highest and least loaded brokers for "
+ "uniform load shedding. (eg: broker1 with 450MB msgRate and broker2 with 100MB msgRate "
+ "will have 4.5 times msgThroughout difference and load balancer can unload bundles "
+ "from broker-1 to broker-2)"
)
private int loadBalancerMsgThroughputMultiplierDifferenceShedderThreshold = 4;

@FieldContext(
dynamic = true,
category = CATEGORY_LOAD_BALANCER,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/**
* 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.impl;

import static org.apache.pulsar.broker.namespace.NamespaceService.HEARTBEAT_NAMESPACE_PATTERN;
import static org.apache.pulsar.broker.namespace.NamespaceService.HEARTBEAT_NAMESPACE_PATTERN_V2;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import java.util.Map;
import org.apache.commons.lang3.mutable.MutableDouble;
import org.apache.commons.lang3.mutable.MutableInt;
import org.apache.commons.lang3.mutable.MutableObject;
import org.apache.commons.lang3.tuple.Triple;
import org.apache.pulsar.broker.BrokerData;
import org.apache.pulsar.broker.BundleData;
import org.apache.pulsar.broker.ServiceConfiguration;
import org.apache.pulsar.broker.TimeAverageMessageData;
import org.apache.pulsar.broker.loadbalance.LoadData;
import org.apache.pulsar.broker.loadbalance.LoadSheddingStrategy;
import org.apache.pulsar.policies.data.loadbalancer.LocalBrokerData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class UniformLoadShedder implements LoadSheddingStrategy {

private static final Logger log = LoggerFactory.getLogger(UniformLoadShedder.class);

private final Multimap<String, String> selectedBundlesCache = ArrayListMultimap.create();

/**
* Attempt to shed some bundles off every broker which is overloaded.
*
* @param loadData
* The load data to used to make the unloading decision.
* @param conf
* The service configuration.
* @return A map from bundles to unload to the brokers on which they are loaded.
*/
@Override
public Multimap<String, String> findBundlesForUnloading(final LoadData loadData, final ServiceConfiguration conf) {
selectedBundlesCache.clear();
Map<String, BrokerData> brokersData = loadData.getBrokerData();
Map<String, BundleData> loadBundleData = loadData.getBundleData();
Map<String, Long> recentlyUnloadedBundles = loadData.getRecentlyUnloadedBundles();

MutableObject<String> overloadedBroker = new MutableObject<>();
MutableObject<String> underloadedBroker = new MutableObject<>();
MutableDouble maxMsgRate = new MutableDouble(-1);
MutableDouble maxThroughputRate = new MutableDouble(-1);
MutableDouble minMsgRate = new MutableDouble(Integer.MAX_VALUE);
MutableDouble minThroughputgRate = new MutableDouble(Integer.MAX_VALUE);
brokersData.forEach((broker, data) -> {
double msgRate = data.getLocalData().getMsgRateIn() + data.getLocalData().getMsgRateOut();
double throughputRate = data.getLocalData().getMsgThroughputIn()
+ data.getLocalData().getMsgThroughputOut();
if (data.getLocalData().getBundles().size() > 1 // broker with one bundle can't be considered for
// bundle unloading
&& (msgRate > maxMsgRate.getValue() || throughputRate > maxThroughputRate.getValue())) {
overloadedBroker.setValue(broker);
maxMsgRate.setValue(msgRate);
maxThroughputRate.setValue(throughputRate);
}
if (msgRate < minMsgRate.getValue() || throughputRate < minThroughputgRate.getValue()) {
underloadedBroker.setValue(broker);
minMsgRate.setValue(msgRate);
minThroughputgRate.setValue(throughputRate);
}
});

// find the difference between two brokers based on msgRate and throughout and check if the load distribution
// discrepancy is higher than threshold. if that matches then try to unload bundle from overloaded brokers to
// give chance of uniform load distribution.
double msgRateDifferencePercentage = ((maxMsgRate.getValue() - minMsgRate.getValue()) * 100)
/ (minMsgRate.getValue());
double msgThroughputDifferenceRate = maxThroughputRate.getValue() / minThroughputgRate.getValue();

// if the threshold matches then find out how much load needs to be unloaded by considering number of msgRate
// and throughput.
boolean isMsgRateThresholdExceeded = conf.getLoadBalancerMsgRateDifferenceShedderThreshold() > 0
&& msgRateDifferencePercentage > conf.getLoadBalancerMsgRateDifferenceShedderThreshold();
boolean isMsgThroughputThresholdExceeded = conf
.getLoadBalancerMsgThroughputMultiplierDifferenceShedderThreshold() > 0
&& msgThroughputDifferenceRate > conf
.getLoadBalancerMsgThroughputMultiplierDifferenceShedderThreshold();

if (isMsgRateThresholdExceeded || isMsgThroughputThresholdExceeded) {
if (log.isDebugEnabled()) {
log.debug(
"Found bundles for uniform load balancing. "
+ "overloaded broker {} with (msgRate,throughput)= ({},{}) "
+ "and underloaded broker {} with (msgRate,throughput)= ({},{})",
overloadedBroker.getValue(), maxMsgRate.getValue(), maxThroughputRate.getValue(),
underloadedBroker.getValue(), minMsgRate.getValue(), minThroughputgRate.getValue());
}
MutableInt msgRateRequiredFromUnloadedBundles = new MutableInt(
(int) ((maxMsgRate.getValue() - minMsgRate.getValue()) / 2));
MutableInt msgThroughtputRequiredFromUnloadedBundles = new MutableInt(
(int) ((maxThroughputRate.getValue() - minThroughputgRate.getValue()) / 2));
LocalBrokerData overloadedBrokerData = brokersData.get(overloadedBroker.getValue()).getLocalData();

if (overloadedBrokerData.getBundles().size() > 1) {
// Sort bundles by throughput, then pick the bundle which can help to reduce load uniformly with
// under-loaded broker
loadBundleData.entrySet().stream()
.filter(e -> !HEARTBEAT_NAMESPACE_PATTERN.matcher(e.getKey()).matches()
&& !HEARTBEAT_NAMESPACE_PATTERN_V2.matcher(e.getKey()).matches()
&& overloadedBrokerData.getBundles().contains(e.getKey()))
.map((e) -> {
String bundle = e.getKey();
BundleData bundleData = e.getValue();
TimeAverageMessageData shortTermData = bundleData.getShortTermData();
double throughput = isMsgRateThresholdExceeded
? shortTermData.getMsgRateIn() + shortTermData.getMsgRateOut()
: shortTermData.getMsgThroughputIn() + shortTermData.getMsgThroughputOut();
return Triple.of(bundle, bundleData, throughput);
}).filter(e -> !recentlyUnloadedBundles.containsKey(e.getLeft()))
.filter(e -> overloadedBrokerData.getBundles().contains(e.getLeft()))
.sorted((e1, e2) -> Double.compare(e2.getRight(), e1.getRight())).forEach((e) -> {
String bundle = e.getLeft();
BundleData bundleData = e.getMiddle();
TimeAverageMessageData shortTermData = bundleData.getShortTermData();
double throughput = shortTermData.getMsgThroughputIn()
+ shortTermData.getMsgThroughputOut();
double bundleMsgRate = shortTermData.getMsgRateIn() + shortTermData.getMsgRateOut();
if (isMsgRateThresholdExceeded) {
if (bundleMsgRate <= (msgRateRequiredFromUnloadedBundles.getValue()
+ 1000/* delta */)) {
log.info("Found bundle to unload with msgRate {}", bundleMsgRate);
msgRateRequiredFromUnloadedBundles.add(-bundleMsgRate);
selectedBundlesCache.put(overloadedBroker.getValue(), bundle);
}
} else {
if (throughput <= (msgThroughtputRequiredFromUnloadedBundles.getValue())) {
log.info("Found bundle to unload with throughput {}", throughput);
msgThroughtputRequiredFromUnloadedBundles.add(-throughput);
selectedBundlesCache.put(overloadedBroker.getValue(), bundle);
}
}
});
}
}

return selectedBundlesCache;
}
}

0 comments on commit 4f9fcf8

Please sign in to comment.