-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
[pulsar-broker] add uniform load shedder strategy to distribute traffic uniformly across brokers #12902
Merged
Merged
[pulsar-broker] add uniform load shedder strategy to distribute traffic uniformly across brokers #12902
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just wondering whether there would there need to be support for fractions like 1.5 ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, value is double and it supports fractions. |
||
|
||
@FieldContext( | ||
dynamic = true, | ||
category = CATEGORY_LOAD_BALANCER, | ||
|
161 changes: 161 additions & 0 deletions
161
...ar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/impl/UniformLoadShedder.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,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; | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO, It's better to add a notice about how to enable uniform load shedding, like set
loadBalancerLoadSheddingStrategy="org.apache.pulsar.broker.loadbalance.impl.UniformLoadShedder"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added into documentation