This is an automated email from the ASF dual-hosted git repository.
iluo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-dubbo.git
The following commit(s) were added to refs/heads/master by this push:
new 63bf28d feat:Improve the annotation of LeastActiveLoadBalance (#2893)
63bf28d is described below
commit 63bf28da8c173f474a5643ca716876db2d3aae9a
Author: zhuzi <[email protected]>
AuthorDate: Thu Dec 6 19:32:11 2018 +0800
feat:Improve the annotation of LeastActiveLoadBalance (#2893)
---
.../loadbalance/LeastActiveLoadBalance.java | 69 +++++++++++++++-------
1 file changed, 48 insertions(+), 21 deletions(-)
diff --git
a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/LeastActiveLoadBalance.java
b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/LeastActiveLoadBalance.java
index 1516317..860dd3d 100644
---
a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/LeastActiveLoadBalance.java
+++
b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/LeastActiveLoadBalance.java
@@ -26,6 +26,11 @@ import java.util.concurrent.ThreadLocalRandom;
/**
* LeastActiveLoadBalance
+ * <p>
+ * Filter the number of invokers with the least number of active calls and
count the weights and quantities of these invokers.
+ * If there is only one invoker, use the invoker directly;
+ * if there are multiple invokers and the weights are not the same, then
random according to the total weight;
+ * if there are multiple invokers and the same weight, then randomly called.
*/
public class LeastActiveLoadBalance extends AbstractLoadBalance {
@@ -33,27 +38,48 @@ public class LeastActiveLoadBalance extends
AbstractLoadBalance {
@Override
protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url,
Invocation invocation) {
- int length = invokers.size(); // Number of invokers
- int leastActive = -1; // The least active value of all invokers
- int leastCount = 0; // The number of invokers having the same least
active value (leastActive)
- int[] leastIndexes = new int[length]; // The index of invokers having
the same least active value (leastActive)
- int totalWeight = 0; // The sum of with warmup weights
- int firstWeight = 0; // Initial value, used for comparision
- boolean sameWeight = true; // Every invoker has the same weight value?
+ // Number of invokers
+ int length = invokers.size();
+ // The least active value of all invokers
+ int leastActive = -1;
+ // The number of invokers having the same least active value
(leastActive)
+ int leastCount = 0;
+ // The index of invokers having the same least active value
(leastActive)
+ int[] leastIndexes = new int[length];
+ // The sum of the warmup weights of all the least active invokes
+ int totalWeight = 0;
+ // The weight of the first least active invoke
+ int firstWeight = 0;
+ // Every least active invoker has the same weight value?
+ boolean sameWeight = true;
+
+ // Filter out all the least active invokers
for (int i = 0; i < length; i++) {
Invoker<T> invoker = invokers.get(i);
- int active = RpcStatus.getStatus(invoker.getUrl(),
invocation.getMethodName()).getActive(); // Active number
+ // Get the active number of the invoke
+ int active = RpcStatus.getStatus(invoker.getUrl(),
invocation.getMethodName()).getActive();
+ // Get the weight of the invoke configuration. The default value
is 100.
int afterWarmup = getWeight(invoker, invocation);
- if (leastActive == -1 || active < leastActive) { // Restart, when
find a invoker having smaller least active value.
- leastActive = active; // Record the current least active value
- leastCount = 1; // Reset leastCount, count again based on
current leastCount
- leastIndexes[0] = i; // Reset
- totalWeight = afterWarmup; // Reset
- firstWeight = afterWarmup; // Record the weight the first
invoker
- sameWeight = true; // Reset, every invoker has the same weight
value?
- } else if (active == leastActive) { // If current invoker's active
value equals with leaseActive, then accumulating.
- leastIndexes[leastCount++] = i; // Record index number of this
invoker
- totalWeight += afterWarmup; // Add this invoker's with warmup
weight to totalWeight.
+ // If it is the first invoker or the active number of the invoker
is less than the current least active number
+ if (leastActive == -1 || active < leastActive) {
+ // Reset the active number of the current invoker to the least
active number
+ leastActive = active;
+ // Reset the number of least active invokers
+ leastCount = 1;
+ // Put the first least active invoker first in leastIndexs
+ leastIndexes[0] = i;
+ // Reset totalWeight
+ totalWeight = afterWarmup;
+ // Record the weight the first least active invoker
+ firstWeight = afterWarmup;
+ // Each invoke has the same weight (only one invoker here)
+ sameWeight = true;
+ // If current invoker's active value equals with leaseActive,
then accumulating.
+ } else if (active == leastActive) {
+ // Record the index of the least active invoker in leastIndexs
order
+ leastIndexes[leastCount++] = i;
+ // Accumulate the total weight of the least active invoker
+ totalWeight += afterWarmup;
// If every invoker has the same weight?
if (sameWeight && i > 0
&& afterWarmup != firstWeight) {
@@ -61,13 +87,14 @@ public class LeastActiveLoadBalance extends
AbstractLoadBalance {
}
}
}
- // assert(leastCount > 0)
+ // Choose an invoker from all the least active invokers
if (leastCount == 1) {
// If we got exactly one invoker having the least active value,
return this invoker directly.
return invokers.get(leastIndexes[0]);
}
if (!sameWeight && totalWeight > 0) {
- // If (not every invoker has the same weight & at least one
invoker's weight>0), select randomly based on totalWeight.
+ // If (not every invoker has the same weight & at least one
invoker's weight>0), select randomly based on
+ // totalWeight.
int offsetWeight =
ThreadLocalRandom.current().nextInt(totalWeight) + 1;
// Return a invoker based on the random value.
for (int i = 0; i < leastCount; i++) {
@@ -81,4 +108,4 @@ public class LeastActiveLoadBalance extends
AbstractLoadBalance {
// If all invokers have the same weight value or totalWeight=0, return
evenly.
return
invokers.get(leastIndexes[ThreadLocalRandom.current().nextInt(leastCount)]);
}
-}
+}
\ No newline at end of file