<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I have searched the <a href="https://github.com/apache/incubator-dubbo/issues">issues</a> of this repository and believe that this is not a duplicate.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I have checked the <a href="https://github.com/apache/incubator-dubbo/blob/master/FAQ.md">FAQ</a> of this repository and believe that this is not a duplicate.</li>
</ul>
<h3 dir="auto">Environment</h3>
<ul dir="auto">
<li>Dubbo version: newest master and 2.5.3</li>
<li>Operating System version: mac</li>
<li>Java version: 1.8</li>
</ul>
<h3 dir="auto">Steps to reproduce this issue</h3>
<p dir="auto">Our company currently uses dubbo-2.5.3, LoadBalance chooses roundrobin. After the weight adjustment is made, the distribution of traffic is not in line with expectations. So I saw the implementation of RoundRobinLoadBalance of 2.5.3, which logically led to the distribution of traffic. Not in line with expectations, so I read the latest dubbo version of the implementation, found that the latest version of RoundRobinLoadBalance implementation traffic distribution is in line with expectations, but the performance is not satisfactory.</p>
<p dir="auto">Test Code：</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="package com.manzhizhen.study.loadbalance;

import com.alibaba.dubbo.common.utils.AtomicPositiveInteger;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ThreadLocalRandom;

/**
 * Created by yizhenqiang on 18/9/26.
 */
public class DubboRoundRobinLoadBalance {

    /**
     * 假设该接口有10个可用的Invoker
     */
    private static final int INVOKER_SIZE = 10;
    private static final int[] INVOKER_WEIGHT_ARRAY = new int[]{100, 100, 200, 200, 300, 300, 400, 400, 500, 500};

    private static final String SERVICE_KEY = &quot;com.test.Test.testMethod&quot;;

    private static final ConcurrentMap&lt;String, AtomicPositiveInteger&gt; sequences = new ConcurrentHashMap&lt;String, AtomicPositiveInteger&gt;();

    private static final ConcurrentMap&lt;String, AtomicPositiveInteger&gt; sequences1 = new ConcurrentHashMap&lt;String, AtomicPositiveInteger&gt;();
    private static final ConcurrentMap&lt;String, AtomicPositiveInteger&gt; weightSequences = new ConcurrentHashMap&lt;String, AtomicPositiveInteger&gt;();


    public static void main(String[] args) {
        int times = 1000000;
        int[] selectArray = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        long start = System.nanoTime();
        while (times-- &gt; 0) {
            int select = currentSelect();
            selectArray[select]++;
        }

        System.out.println(&quot;最新dubbo的RoundRobinLoadBalance耗时：&quot; + (System.nanoTime() - start) / 1000000);
        System.out.println(&quot;最新dubbo的RoundRobinLoadBalance流量分布：&quot; + JSON.toJSONString(selectArray));

        times = 1000000;
        selectArray = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        start = System.nanoTime();
        while (times-- &gt; 0) {
            int select = oldSelect();
            selectArray[select]++;
        }

        System.out.println(&quot;dubbo-2.5.3的RoundRobinLoadBalance耗时：&quot; + (System.nanoTime() - start) / 1000000);
        System.out.println(&quot;dubbo-2.5.3的RoundRobinLoadBalance流量分布：&quot; + JSON.toJSONString(selectArray));

        times = 1000000;
        selectArray = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        start = System.nanoTime();
        while (times-- &gt; 0) {
            int select = oldRandomSelect();
            selectArray[select]++;
        }

        System.out.println(&quot;dubbo-2.5.3的RandomLoadBalance耗时：&quot; + (System.nanoTime() - start) / 1000000);
        System.out.println(&quot;dubbo-2.5.3的RandomLoadBalance流量分布：&quot; + JSON.toJSONString(selectArray));

    }

    /**
     * 当前最新版本dubbo master分支中实现方式
     *
     * @return 选择的invoker的index
     */
    private static int currentSelect() {
        // 为了测试方便，key默认写死
        String key = SERVICE_KEY;
        // invoker默认是10个
        int length = INVOKER_SIZE; // Number of invokers

        int maxWeight = 0; // The maximum weight
        int minWeight = Integer.MAX_VALUE; // The minimum weight
        final LinkedHashMap&lt;Integer, IntegerWrapper&gt; invokerToWeightMap = new LinkedHashMap&lt;Integer, IntegerWrapper&gt;();
        int weightSum = 0;
        for (int i = 0; i &lt; length; i++) {
            int weight = getWeight(i);
            maxWeight = Math.max(maxWeight, weight); // Choose the maximum weight
            minWeight = Math.min(minWeight, weight); // Choose the minimum weight
            if (weight &gt; 0) {
                invokerToWeightMap.put(i, new IntegerWrapper(weight));
                weightSum += weight;
            }
        }
        AtomicPositiveInteger sequence = sequences.get(key);
        if (sequence == null) {
            sequences.putIfAbsent(key, new AtomicPositiveInteger());
            sequence = sequences.get(key);
        }
        int currentSequence = sequence.getAndIncrement();
        if (maxWeight &gt; 0 &amp;&amp; minWeight &lt; maxWeight) {
            int mod = currentSequence % weightSum;
            for (int i = 0; i &lt; maxWeight; i++) {
                for (Map.Entry&lt;Integer, IntegerWrapper&gt; each : invokerToWeightMap.entrySet()) {
                    final Integer k = each.getKey();
                    final IntegerWrapper v = each.getValue();
                    if (mod == 0 &amp;&amp; v.getValue() &gt; 0) {
                        return k;
                    }
                    if (v.getValue() &gt; 0) {
                        v.decrement();
                        mod--;
                    }
                }
            }
        }
        // Round robin
        return currentSequence % length;
    }

    /**
     * 2.5.3版本的roundrobin方式
     *
     * @return
     */
    private static int oldSelect() {
        // 为了测试方便，key默认写死
        String key = SERVICE_KEY;
        // invoker默认是10个
        int length = INVOKER_SIZE; // Number of invokers

        List&lt;Integer&gt; invokers = Lists.newArrayList();

        int maxWeight = 0; // 最大权重
        int minWeight = Integer.MAX_VALUE; // 最小权重
        for (int i = 0; i &lt; length; i++) {
            int weight = getWeight(i);
            maxWeight = Math.max(maxWeight, weight); // 累计最大权重
            minWeight = Math.min(minWeight, weight); // 累计最小权重
        }
        if (maxWeight &gt; 0 &amp;&amp; minWeight &lt; maxWeight) { // 权重不一样
            AtomicPositiveInteger weightSequence = weightSequences.get(key);
            if (weightSequence == null) {
                weightSequences.putIfAbsent(key, new AtomicPositiveInteger());
                weightSequence = weightSequences.get(key);
            }
            int currentWeight = weightSequence.getAndIncrement() % maxWeight;
            List&lt;Integer&gt; weightInvokers = new ArrayList&lt;Integer&gt;();
            for (int i = 0; i &lt; INVOKER_SIZE; i++) { // 筛选权重大于当前权重基数的Invoker
                if (getWeight(i) &gt; currentWeight) {
                    weightInvokers.add(i);
                }
            }
            int weightLength = weightInvokers.size();
            if (weightLength == 1) {
                return weightInvokers.get(0);
            } else if (weightLength &gt; 1) {
                invokers = weightInvokers;
                length = invokers.size();
            }
        }
        AtomicPositiveInteger sequence = sequences1.get(key);
        if (sequence == null) {
            sequences1.putIfAbsent(key, new AtomicPositiveInteger());
            sequence = sequences1.get(key);
        }
        // 取模轮循
        return invokers.get(sequence.getAndIncrement() % length);
    }

    /**
     * 2.5.3版本的random方式
     *
     * @return
     */
    private static int oldRandomSelect() {
        // 为了测试方便，key默认写死
        String key = SERVICE_KEY;
        // invoker默认是10个
        int length = INVOKER_SIZE; // Number of invokers
        int totalWeight = 0; // 总权重

        boolean sameWeight = true; // 权重是否都一样
        for (int i = 0; i &lt; length; i++) {
            int weight = getWeight(i);
            totalWeight += weight; // 累计总权重
            if (sameWeight &amp;&amp; i &gt; 0
                    &amp;&amp; weight != getWeight(i - 1)) {
                sameWeight = false; // 计算所有权重是否一样
            }
        }
        if (totalWeight &gt; 0 &amp;&amp; !sameWeight) {
            // 如果权重不相同且权重大于0则按总权重数随机
            int offset = ThreadLocalRandom.current().nextInt(totalWeight);
            // 并确定随机值落在哪个片断上
            for (int i = 0; i &lt; length; i++) {
                offset -= getWeight(i);
                if (offset &lt; 0) {
                    return i;
                }
            }
        }
        // 如果权重相同或权重为0则均等随机
        return ThreadLocalRandom.current().nextInt(length);
    }

    private static int getWeight(int invokerIndex) {
        return INVOKER_WEIGHT_ARRAY[invokerIndex];
    }

    private static final class IntegerWrapper {
        private int value;

        public IntegerWrapper(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }

        public void decrement() {
            this.value--;
        }
    }

}
"><pre class="notranslate"><code class="notranslate">package com.manzhizhen.study.loadbalance;

import com.alibaba.dubbo.common.utils.AtomicPositiveInteger;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ThreadLocalRandom;

/**
 * Created by yizhenqiang on 18/9/26.
 */
public class DubboRoundRobinLoadBalance {

    /**
     * 假设该接口有10个可用的Invoker
     */
    private static final int INVOKER_SIZE = 10;
    private static final int[] INVOKER_WEIGHT_ARRAY = new int[]{100, 100, 200, 200, 300, 300, 400, 400, 500, 500};

    private static final String SERVICE_KEY = "com.test.Test.testMethod";

    private static final ConcurrentMap&lt;String, AtomicPositiveInteger&gt; sequences = new ConcurrentHashMap&lt;String, AtomicPositiveInteger&gt;();

    private static final ConcurrentMap&lt;String, AtomicPositiveInteger&gt; sequences1 = new ConcurrentHashMap&lt;String, AtomicPositiveInteger&gt;();
    private static final ConcurrentMap&lt;String, AtomicPositiveInteger&gt; weightSequences = new ConcurrentHashMap&lt;String, AtomicPositiveInteger&gt;();


    public static void main(String[] args) {
        int times = 1000000;
        int[] selectArray = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        long start = System.nanoTime();
        while (times-- &gt; 0) {
            int select = currentSelect();
            selectArray[select]++;
        }

        System.out.println("最新dubbo的RoundRobinLoadBalance耗时：" + (System.nanoTime() - start) / 1000000);
        System.out.println("最新dubbo的RoundRobinLoadBalance流量分布：" + JSON.toJSONString(selectArray));

        times = 1000000;
        selectArray = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        start = System.nanoTime();
        while (times-- &gt; 0) {
            int select = oldSelect();
            selectArray[select]++;
        }

        System.out.println("dubbo-2.5.3的RoundRobinLoadBalance耗时：" + (System.nanoTime() - start) / 1000000);
        System.out.println("dubbo-2.5.3的RoundRobinLoadBalance流量分布：" + JSON.toJSONString(selectArray));

        times = 1000000;
        selectArray = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        start = System.nanoTime();
        while (times-- &gt; 0) {
            int select = oldRandomSelect();
            selectArray[select]++;
        }

        System.out.println("dubbo-2.5.3的RandomLoadBalance耗时：" + (System.nanoTime() - start) / 1000000);
        System.out.println("dubbo-2.5.3的RandomLoadBalance流量分布：" + JSON.toJSONString(selectArray));

    }

    /**
     * 当前最新版本dubbo master分支中实现方式
     *
     * @return 选择的invoker的index
     */
    private static int currentSelect() {
        // 为了测试方便，key默认写死
        String key = SERVICE_KEY;
        // invoker默认是10个
        int length = INVOKER_SIZE; // Number of invokers

        int maxWeight = 0; // The maximum weight
        int minWeight = Integer.MAX_VALUE; // The minimum weight
        final LinkedHashMap&lt;Integer, IntegerWrapper&gt; invokerToWeightMap = new LinkedHashMap&lt;Integer, IntegerWrapper&gt;();
        int weightSum = 0;
        for (int i = 0; i &lt; length; i++) {
            int weight = getWeight(i);
            maxWeight = Math.max(maxWeight, weight); // Choose the maximum weight
            minWeight = Math.min(minWeight, weight); // Choose the minimum weight
            if (weight &gt; 0) {
                invokerToWeightMap.put(i, new IntegerWrapper(weight));
                weightSum += weight;
            }
        }
        AtomicPositiveInteger sequence = sequences.get(key);
        if (sequence == null) {
            sequences.putIfAbsent(key, new AtomicPositiveInteger());
            sequence = sequences.get(key);
        }
        int currentSequence = sequence.getAndIncrement();
        if (maxWeight &gt; 0 &amp;&amp; minWeight &lt; maxWeight) {
            int mod = currentSequence % weightSum;
            for (int i = 0; i &lt; maxWeight; i++) {
                for (Map.Entry&lt;Integer, IntegerWrapper&gt; each : invokerToWeightMap.entrySet()) {
                    final Integer k = each.getKey();
                    final IntegerWrapper v = each.getValue();
                    if (mod == 0 &amp;&amp; v.getValue() &gt; 0) {
                        return k;
                    }
                    if (v.getValue() &gt; 0) {
                        v.decrement();
                        mod--;
                    }
                }
            }
        }
        // Round robin
        return currentSequence % length;
    }

    /**
     * 2.5.3版本的roundrobin方式
     *
     * @return
     */
    private static int oldSelect() {
        // 为了测试方便，key默认写死
        String key = SERVICE_KEY;
        // invoker默认是10个
        int length = INVOKER_SIZE; // Number of invokers

        List&lt;Integer&gt; invokers = Lists.newArrayList();

        int maxWeight = 0; // 最大权重
        int minWeight = Integer.MAX_VALUE; // 最小权重
        for (int i = 0; i &lt; length; i++) {
            int weight = getWeight(i);
            maxWeight = Math.max(maxWeight, weight); // 累计最大权重
            minWeight = Math.min(minWeight, weight); // 累计最小权重
        }
        if (maxWeight &gt; 0 &amp;&amp; minWeight &lt; maxWeight) { // 权重不一样
            AtomicPositiveInteger weightSequence = weightSequences.get(key);
            if (weightSequence == null) {
                weightSequences.putIfAbsent(key, new AtomicPositiveInteger());
                weightSequence = weightSequences.get(key);
            }
            int currentWeight = weightSequence.getAndIncrement() % maxWeight;
            List&lt;Integer&gt; weightInvokers = new ArrayList&lt;Integer&gt;();
            for (int i = 0; i &lt; INVOKER_SIZE; i++) { // 筛选权重大于当前权重基数的Invoker
                if (getWeight(i) &gt; currentWeight) {
                    weightInvokers.add(i);
                }
            }
            int weightLength = weightInvokers.size();
            if (weightLength == 1) {
                return weightInvokers.get(0);
            } else if (weightLength &gt; 1) {
                invokers = weightInvokers;
                length = invokers.size();
            }
        }
        AtomicPositiveInteger sequence = sequences1.get(key);
        if (sequence == null) {
            sequences1.putIfAbsent(key, new AtomicPositiveInteger());
            sequence = sequences1.get(key);
        }
        // 取模轮循
        return invokers.get(sequence.getAndIncrement() % length);
    }

    /**
     * 2.5.3版本的random方式
     *
     * @return
     */
    private static int oldRandomSelect() {
        // 为了测试方便，key默认写死
        String key = SERVICE_KEY;
        // invoker默认是10个
        int length = INVOKER_SIZE; // Number of invokers
        int totalWeight = 0; // 总权重

        boolean sameWeight = true; // 权重是否都一样
        for (int i = 0; i &lt; length; i++) {
            int weight = getWeight(i);
            totalWeight += weight; // 累计总权重
            if (sameWeight &amp;&amp; i &gt; 0
                    &amp;&amp; weight != getWeight(i - 1)) {
                sameWeight = false; // 计算所有权重是否一样
            }
        }
        if (totalWeight &gt; 0 &amp;&amp; !sameWeight) {
            // 如果权重不相同且权重大于0则按总权重数随机
            int offset = ThreadLocalRandom.current().nextInt(totalWeight);
            // 并确定随机值落在哪个片断上
            for (int i = 0; i &lt; length; i++) {
                offset -= getWeight(i);
                if (offset &lt; 0) {
                    return i;
                }
            }
        }
        // 如果权重相同或权重为0则均等随机
        return ThreadLocalRandom.current().nextInt(length);
    }

    private static int getWeight(int invokerIndex) {
        return INVOKER_WEIGHT_ARRAY[invokerIndex];
    }

    private static final class IntegerWrapper {
        private int value;

        public IntegerWrapper(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }

        public void decrement() {
            this.value--;
        }
    }

}

</code></pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="最新dubbo的RoundRobinLoadBalance耗时：9291
最新dubbo的RoundRobinLoadBalance流量分布：[33400,33400,66700,66700,100000,100000,133300,133300,166600,166600]
dubbo-2.5.3的RoundRobinLoadBalance耗时：158
dubbo-2.5.3的RoundRobinLoadBalance流量分布：[20000,20000,45000,45000,78333,78333,128333,128333,228334,228334]
dubbo-2.5.3的RandomLoadBalance耗时：39
dubbo-2.5.3的RandomLoadBalance流量分布：[33261,33660,66804,66524,99586,99774,133574,133397,166703,166717]"><pre class="notranslate"><code class="notranslate">最新dubbo的RoundRobinLoadBalance耗时：9291
最新dubbo的RoundRobinLoadBalance流量分布：[33400,33400,66700,66700,100000,100000,133300,133300,166600,166600]
dubbo-2.5.3的RoundRobinLoadBalance耗时：158
dubbo-2.5.3的RoundRobinLoadBalance流量分布：[20000,20000,45000,45000,78333,78333,128333,128333,228334,228334]
dubbo-2.5.3的RandomLoadBalance耗时：39
dubbo-2.5.3的RandomLoadBalance流量分布：[33261,33660,66804,66524,99586,99774,133574,133397,166703,166717]
</code></pre></div>
<p dir="auto">In the latest version of the RoundRobinLoadBalance implementation, a large number of for loops are used to do the minus one operation. This is not optimistic when the weight distribution is relatively large (hundreds of thousands of weights).</p>
<p dir="auto">Come up with some design and implementation for dubbo util support. The purpose of this enhancement is to provide utility methods which can help dubbo user to use and achieve the better API experience without enforcing service implementation to provide those support while they write service.</p>
<p dir="auto">e.g.<br>
If consumer of service needs to make a <strong>Future</strong> type of call then instead of service implementation can add those as feature, using Dubbo provided utility they can transfer Rpc call to future callable using something like</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="RpcFuture future=DubboRpcUti.toFuture(someService.someMethod(msg),someNotifyMethod);

DubboRpcUril.call(someUserService.getUser(org)).process(businessLogicMethod).onException(UserNotAvailable.class,handleUserNotAvailable)"><pre class="notranslate"><code class="notranslate">RpcFuture future=DubboRpcUti.toFuture(someService.someMethod(msg),someNotifyMethod);

DubboRpcUril.call(someUserService.getUser(org)).process(businessLogicMethod).onException(UserNotAvailable.class,handleUserNotAvailable)
</code></pre></div>
<p dir="auto">Above are not exhaustive list or final, but the idea is this is an place holder for coming up with some approach, design and implementation.</p>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> I have searched the <a href="https://github.com/apache/incubator-dubbo/issues">issues</a> of this repository and believe that this is not a duplicate.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> I have checked the <a href="https://github.com/apache/incubator-dubbo/blob/master/FAQ.md">FAQ</a> of this repository and believe that this is not a duplicate.</li>
</ul>
<h3 dir="auto">Environment</h3>
<ul dir="auto">
<li>Dubbo version: xxx</li>
<li>Operating System version: xxx</li>
<li>Java version: xxx</li>
</ul>
<h3 dir="auto">Steps to reproduce this issue</h3>
<ol dir="auto">
<li>xxx</li>
<li>xxx</li>
<li>xxx</li>
</ol>
<p dir="auto">Pls. provide [GitHub address] to reproduce this issue.</p>
<h3 dir="auto">Expected Result</h3>
<p dir="auto">What do you expected from the above steps？</p>
<h3 dir="auto">Actual Result</h3>
<p dir="auto">What actually happens?</p>
<p dir="auto">If there is an exception, please attach the exception trace:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Just put your stack trace here!"><pre class="notranslate"><code class="notranslate">Just put your stack trace here!
</code></pre></div>