最新版本dubbo源码之LoadBalance(一)- RoundRobinLoadBalance

写在开头

      最近想详细了解下dubbo负载均衡,本来想从网上找些资料辅助学习,没想到这些资料都是比较旧的,比如说dubbo负载均衡存在的一些问题,最新版本(2.7.0)的其实已经修复了。所以想着还是得自己详细看下源码,并写一下总结,一方面便于日后的回顾,同时也希望能够对大家有一些帮助。    

正文开始      

      dubbo负载均衡,说白了就是从一堆的服务提供者之中,选择一个来处理消费者的服务请求。我们看下LoadBalance的接口类

@SPI(RandomLoadBalance.NAME)
public interface LoadBalance {

    /**
     * select one invoker in list.
     *
     * @param invokers   invokers.
     * @param url        refer url
     * @param invocation invocation.
     * @return selected invoker.
     */
    @Adaptive("loadbalance")
    <T> Invoker<T> select(List<Invoker<T>> invokers, URL url, Invocation invocation) throws RpcException;

}复制代码

      从代码中看出,负载均衡主要是从服务提供者列表中,选择一个。这个接口有一个抽象类的实现。

public abstract class AbstractLoadBalance implements LoadBalance {

    static int calculateWarmupWeight(int uptime, int warmup, int weight) {
        int ww = (int) ((float) uptime / ((float) warmup / (float) weight));
        return ww < 1 ? 1 : (ww > weight ? weight : ww);
    }

    @Override
    public <T> Invoker<T> select(List<Invoker<T>> invokers, URL url, Invocation invocation) {
        if (invokers == null || invokers.isEmpty()) {
            return null;
        }
        if (invokers.size() == 1) {  //当只有一个invoker时,直接返回
            return invokers.get(0);
        }
        //选择一个invoker
        return doSelect(invokers, url, invocation);
    }
    //具体的负载均衡策略由子类来实现
    protected abstract <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation);

    //权重方法
    protected int getWeight(Invoker<?> invoker, Invocation invocation) {
        //如果有配置,直接读取配置
        int weight = invoker.getUrl().getMethodParameter(invocation.getMethodName(), Constants.WEIGHT_KEY, Constants.DEFAULT_WEIGHT);
        if (weight > 0) {
            long timestamp = invoker.getUrl().getParameter(Constants.REMOTE_TIMESTAMP_KEY, 0L);
            if (timestamp > 0L) {
                int uptime = (int) (System.currentTimeMillis() - timestamp);
                int warmup = invoker.getUrl().getParameter(Constants.WARMUP_KEY, Constants.DEFAULT_WARMUP);
                if (uptime > 0 && uptime < warmup) {
                    weight = calculateWarmupWeight(uptime, warmup, weight);
                }
            }
        }
        return weight;
    }

}复制代码

      这个抽象类很简单,主要就是额外增加了一个获取权重的方法。我们看下具体的实现类。


      从截图中可以看出,dubbo的负载均衡一共有4种。分别为:

      1、一致性hash   ConsistentHashLoadBalance

      2、最小活跃数   LeastActiveLoadBalance

      3、随机   RandomLoadBalance

      4、轮询  RoundRobinLoadBalance

本文讲下轮询,先来看下核心代码(后续用演示图来描述步骤)

public class RoundRobinLoadBalance extends AbstractLoadBalance {
    public static final String NAME = "roundrobin";
    
    private static int RECYCLE_PERIOD = 60000;
    
    //这个类主要是记录每个提供者的权重信息
    protected static class WeightedRoundRobin {
        private int weight;  //权重
        private AtomicLong current = new AtomicLong(0);  //每次获得执行机会后,权重值
        private long lastUpdate;  //暂时先忽略
        public int getWeight() {
            return weight;
        }
        public void setWeight(int weight) {
            this.weight = weight;
            current.set(0);
        }
        public long increaseCurrent() {
            return current.addAndGet(weight);
        }
        public void sel(int total) {
            current.addAndGet(-1 * total);
        }
        public long getLastUpdate() {
            return lastUpdate;
        }
        public void setLastUpdate(long lastUpdate) {
            this.lastUpdate = lastUpdate;
        }
    }

    private ConcurrentMap<String, ConcurrentMap<String, WeightedRoundRobin>> methodWeightMap = new ConcurrentHashMap<String, ConcurrentMap<String, WeightedRoundRobin>>();
    private AtomicBoolean updateLock = new AtomicBoolean();
    
    /**
     * get invoker addr list cached for specified invocation
     * <p>
     * <b>for unit test only</b>
     * 
     * @param invokers
     * @param invocation
     * @return
     */
    protected <T> Collection<String> getInvokerAddrList(List<Invoker<T>> invokers, Invocation invocation) {
        String key = invokers.get(0).getUrl().getServiceKey() + "." + invocation.getMethodName();
        Map<String, WeightedRoundRobin> map = methodWeightMap.get(key);
        if (map != null) {
            return map.keySet();
        }
        return null;
    }
    
    @Override
    protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
        //每一个方法对应自己的一套缓存(一个map)
        String key = invokers.get(0).getUrl().getServiceKey() + "." + invocation.getMethodName();
        //每个map中缓存着每个提供者对应的权重状态
        ConcurrentMap<String, WeightedRoundRobin> map = methodWeightMap.get(key);
        if (map == null) {
            methodWeightMap.putIfAbsent(key, new ConcurrentHashMap<String, WeightedRoundRobin>());
            map = methodWeightMap.get(key);
        }
        int totalWeight = 0;
        long maxCurrent = Long.MIN_VALUE;
        long now = System.currentTimeMillis();
        Invoker<T> selectedInvoker = null;
        WeightedRoundRobin selectedWRR = null;
        //循环服务提供者列表
        for (Invoker<T> invoker : invokers) {
            String identifyString = invoker.getUrl().toIdentityString();
            //获取服务提供者对应的权重状态
            WeightedRoundRobin weightedRoundRobin = map.get(identifyString);
            int weight = getWeight(invoker, invocation);  //权重从配置中读取。没有配置默认相等
            if (weight < 0) {
                weight = 0;
            }
            //权重状态为空,则需要新建
            if (weightedRoundRobin == null) {
                weightedRoundRobin = new WeightedRoundRobin();
                weightedRoundRobin.setWeight(weight);
                map.putIfAbsent(identifyString, weightedRoundRobin);
                weightedRoundRobin = map.get(identifyString);
            }
            if (weight != weightedRoundRobin.getWeight()) {
                //weight changed 权重变更
                weightedRoundRobin.setWeight(weight);
            }
            //每次循环,服务提供者的当前权重值都会加上权重值
            long cur = weightedRoundRobin.increaseCurrent();
            weightedRoundRobin.setLastUpdate(now);
            //这个if代码主要是为了找到这组服务提供者列表中当前权重值最大的
            if (cur > maxCurrent) {
                maxCurrent = cur;
                selectedInvoker = invoker;
                selectedWRR = weightedRoundRobin;
            }
            totalWeight += weight;  //一个循环结束,服务列表不变,这个总权重不会变
        }
        //这块是缓存数据的,大家细看下,不啰嗦了
        if (!updateLock.get() && invokers.size() != map.size()) {
            if (updateLock.compareAndSet(false, true)) {
                try {
                    // copy -> modify -> update reference
                    ConcurrentMap<String, WeightedRoundRobin> newMap = new ConcurrentHashMap<String, WeightedRoundRobin>();
                    newMap.putAll(map);
                    Iterator<Entry<String, WeightedRoundRobin>> it = newMap.entrySet().iterator();
                    while (it.hasNext()) {
                        Entry<String, WeightedRoundRobin> item = it.next();
                        if (now - item.getValue().getLastUpdate() > RECYCLE_PERIOD) {
                            it.remove();
                        }
                    }
                    methodWeightMap.put(key, newMap);
                } finally {
                    updateLock.set(false);
                }
            }
        }
        //这块也很关键,每次选定服务提供者后,会把这个服务提供者的当前权重值减去总权重
        if (selectedInvoker != null) {
            selectedWRR.sel(totalWeight);
            return selectedInvoker;
        }
        // should not happen here
        return invokers.get(0);
    }

}复制代码

      看代码还是不清晰?没关系,下面我画张图来表述下,还看不懂,你来打我。

假设有三个服务提供者A,B,C,权重分别为1:3:5 (相同权重不讲了,按列表顺序从前往后循环就行了)。


下面附上我的测试代码,结果和上图演示结果一致。

package loadbalance;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @author <a href="mailto:wujia@2dfire.com">linxiaohui</a>
 * @version 1.0 2018/12/17
 * @since 1.0
 */
public class RoundRobinLoadBalance {
    public static final String NAME = "roundrobin";

    private static int RECYCLE_PERIOD = 60000;

    protected static class WeightedRoundRobin {
        private int weight;
        private AtomicLong current = new AtomicLong(0);
        private long lastUpdate;

        public int getWeight() {
            return weight;
        }

        public void setWeight(int weight) {
            this.weight = weight;
            current.set(0);
        }

        public long increaseCurrent() {
            return current.addAndGet(weight);
        }

        public void sel(int total) {
            current.addAndGet(-1 * total);
        }

        public long getLastUpdate() {
            return lastUpdate;
        }

        public void setLastUpdate(long lastUpdate) {
            this.lastUpdate = lastUpdate;
        }
    }

    private ConcurrentMap<String, ConcurrentMap<String, WeightedRoundRobin>> methodWeightMap = new ConcurrentHashMap<String, ConcurrentMap<String, WeightedRoundRobin>>();
    private AtomicBoolean                                                    updateLock      = new AtomicBoolean();

    public Invoker doSelect(List<Invoker> invokers) {
        String key = invokers.get(0).getMethodKey();
        ConcurrentMap<String, WeightedRoundRobin> map = methodWeightMap.get(key);
        if (map == null) {
            methodWeightMap.putIfAbsent(key, new ConcurrentHashMap<String, WeightedRoundRobin>());
            map = methodWeightMap.get(key);
        }
        int totalWeight = 0;
        long maxCurrent = Long.MIN_VALUE;
        long now = System.currentTimeMillis();
        Invoker selectedInvoker = null;
        WeightedRoundRobin selectedWRR = null;
        for (Invoker invoker : invokers) {
            String identifyString = invoker.getUrlKey();
            WeightedRoundRobin weightedRoundRobin = map.get(identifyString);
            int weight = invoker.getWeight();
            if (weight < 0) {
                weight = 0;
            }
            if (weightedRoundRobin == null) {
                weightedRoundRobin = new WeightedRoundRobin();
                weightedRoundRobin.setWeight(weight);
                map.putIfAbsent(identifyString, weightedRoundRobin);
                weightedRoundRobin = map.get(identifyString);
            }
            if (weight != weightedRoundRobin.getWeight()) {
                //weight changed
                weightedRoundRobin.setWeight(weight);
            }
            long cur = weightedRoundRobin.increaseCurrent();
            weightedRoundRobin.setLastUpdate(now);
            if (cur > maxCurrent) {
                maxCurrent = cur;
                selectedInvoker = invoker;
                selectedWRR = weightedRoundRobin;
            }
            totalWeight += weight;
        }
        if (!updateLock.get() && invokers.size() != map.size()) {
            if (updateLock.compareAndSet(false, true)) {
                try {
                    // copy -> modify -> update reference
                    ConcurrentMap<String, WeightedRoundRobin> newMap = new ConcurrentHashMap<String, WeightedRoundRobin>();
                    newMap.putAll(map);
                    Iterator<Map.Entry<String, WeightedRoundRobin>> it = newMap.entrySet().iterator();
                    while (it.hasNext()) {
                        Map.Entry<String, WeightedRoundRobin> item = it.next();
                        if (now - item.getValue().getLastUpdate() > RECYCLE_PERIOD) {
                            it.remove();
                        }
                    }
                    methodWeightMap.put(key, newMap);
                } finally {
                    updateLock.set(false);
                }
            }
        }
        if (selectedInvoker != null) {
            selectedWRR.sel(totalWeight);
            return selectedInvoker;
        }
        // should not happen here
        return invokers.get(0);
    }

    public static void main(String[] args) {
        List<Invoker> invokers = new LinkedList<>();
        Invoker invoker = new Invoker("A", "dubbo://10.121.135.25:20880/DemoService/sayHello", "dubbo://10.121.135.25:20880/DemoService", 1);
        Invoker invoker2 = new Invoker("B", "dubbo://10.121.135.26:20880/DemoService/sayHello", "dubbo://10.121.135.26:20880/DemoService", 3);
        Invoker invoker3 = new Invoker("C", "dubbo://10.121.135.27:20880/DemoService/sayHello", "dubbo://10.121.135.27:20880/DemoService", 5);
        invokers.add(invoker);
        invokers.add(invoker2);
        invokers.add(invoker3);

        RoundRobinLoadBalance balance = new RoundRobinLoadBalance();
        for (int i = 0; i < 10; i++) {
            Invoker selectedInvoker = balance.doSelect(invokers);
            System.out.println(selectedInvoker.getName() + "  方法权重:" + selectedInvoker.getWeight());
        }

    }
}复制代码

附上我模拟的服务提供者

package loadbalance;

/**
 * @author <a href="mailto:wujia@2dfire.com">linxiaohui</a>
 * @version 1.0 2018/12/17
 * @since 1.0
 */
public class Invoker {
    private String methodKey;

    private Integer weight;

    private String urlKey;

    private String name;

    public Invoker(String name, String methodKey, String urlKey, Integer weight) {
        this.name = name;
        this.methodKey = methodKey;
        this.urlKey = urlKey;
        this.weight = weight;
    }

    public String getMethodKey() {
        return methodKey;
    }

    public void setMethodKey(String methodKey) {
        this.methodKey = methodKey;
    }

    public Integer getWeight() {
        return weight;
    }

    public void setWeight(Integer weight) {
        this.weight = weight;
    }

    public String getUrlKey() {
        return urlKey;
    }

    public void setUrlKey(String urlKey) {
        this.urlKey = urlKey;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}复制代码

测试结果图:


小结

      目前网上的一些dubbo的源码分析文章都是基于比较旧的版本,最新版本的已经发生了变化。鉴于这个原因,我会在后续抽空把dubbo的一些源码阅读逐步分享给大家。最近项目忙成狗?,先写到这吧。



转载于:https://juejin.im/post/5c176da8f265da6167203b0d

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值