最新版本dubbo原始碼之LoadBalance(一)- RoundRobinLoadBalance

林學習發表於2018-12-17

寫在開頭

      最近想詳細瞭解下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原始碼之LoadBalance(一)- RoundRobinLoadBalance

      從截圖中可以看出,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 (相同權重不講了,按列表順序從前往後迴圈就行了)。

最新版本dubbo原始碼之LoadBalance(一)- RoundRobinLoadBalance

下面附上我的測試程式碼,結果和上圖演示結果一致。

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原始碼之LoadBalance(一)- RoundRobinLoadBalance

小結

      目前網上的一些dubbo的原始碼分析文章都是基於比較舊的版本,最新版本的已經發生了變化。鑑於這個原因,我會在後續抽空把dubbo的一些原始碼閱讀逐步分享給大家。最近專案忙成狗?,先寫到這吧。



相關文章