6.原始碼分析---和dubbo相比SOFARPC是如何實現負載均衡的?

luozhiyun發表於2019-08-06

官方目前建議使用的負載均衡包括以下幾種:

  1. random(隨機演算法)
  2. localPref(本地優先演算法)
  3. roundRobin(輪詢演算法)
  4. consistentHash(一致性hash演算法)

所以我們接下來分析以下以上四種負載均衡的原始碼是怎樣的。

隨機演算法

我們先看一下SOFARPC的原始碼實現:

@Override
public ProviderInfo doSelect(SofaRequest invocation, List<ProviderInfo> providerInfos) {
    ProviderInfo providerInfo = null;
    int size = providerInfos.size(); // 總個數
    int totalWeight = 0; // 總權重
    boolean isWeightSame = true; // 權重是否都一樣
    for (int i = 0; i < size; i++) {
        int weight = getWeight(providerInfos.get(i));
        totalWeight += weight; // 累計總權重
        if (isWeightSame && i > 0 && weight != getWeight(providerInfos.get(i - 1))) {
            isWeightSame = false; // 計算所有權重是否一樣
        }
    }
    if (totalWeight > 0 && !isWeightSame) {
        // 如果權重不相同且權重大於0則按總權重數隨機
        int offset = random.nextInt(totalWeight);
        // 並確定隨機值落在哪個片斷上
        for (int i = 0; i < size; i++) {
            offset -= getWeight(providerInfos.get(i));
            if (offset < 0) {
                providerInfo = providerInfos.get(i);
                break;
            }
        }
    } else {
        // 如果權重相同或權重為0則均等隨機
        providerInfo = providerInfos.get(random.nextInt(size));
    }
    return providerInfo;
}

上面主要做了幾件事:

  1. 獲取所有的provider
  2. 遍歷provier,如果當前的provider的權重和上一個provider的權重不一樣,那麼就做個標記
  3. 如果權重不相同那麼就隨機取一個0到總權重之間的值,遍歷provider去減隨機數,如果減到小於0,那麼就返回那個provider
  4. 如果沒有權重相同,那麼用隨機函式取一個provider

6.原始碼分析---和dubbo相比SOFARPC是如何實現負載均衡的?

我們再來看看dubbo是怎麼實現的:

@Override
protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
    int length = invokers.size(); // Number of invokers
    boolean sameWeight = true; // Every invoker has the same weight?
    int firstWeight = getWeight(invokers.get(0), invocation);
    int totalWeight = firstWeight; // The sum of weights
    for (int i = 1; i < length; i++) {
        int weight = getWeight(invokers.get(i), invocation);
        totalWeight += weight; // Sum
        if (sameWeight && weight != firstWeight) {
            sameWeight = false;
        }
    }
    if (totalWeight > 0 && !sameWeight) {
        // If (not every invoker has the same weight & at least one invoker's weight>0), select randomly based on totalWeight.
        int offset = ThreadLocalRandom.current().nextInt(totalWeight);
        // Return a invoker based on the random value.
        for (int i = 0; i < length; i++) {
            offset -= getWeight(invokers.get(i), invocation);
            if (offset < 0) {
                return invokers.get(i);
            }
        }
    }
    // If all invokers have the same weight value or totalWeight=0, return evenly.
    return invokers.get(ThreadLocalRandom.current().nextInt(length));
}
  1. 獲取invoker的數量
  2. 獲取第一個invoker的權重,並複製給firstWeight
  3. 迴圈invoker集合,把它們的權重全部相加,並複製給totalWeight,如果權重不相等,那麼sameWeight為false
  4. 如果invoker集合的權重並不是全部相等的,那麼獲取一個隨機數在1到totalWeight之間,賦值給offset屬性
  5. 迴圈遍歷invoker集合,獲取權重並與offset相減,當offset減到小於零,那麼就返回這個inovker
  6. 如果權重相等,那麼直接在invoker集合裡面取一個隨機數返回

從上面我們可以看到,基本上SOFARPC和dubbo的負載均衡實現是一致的。

本地優先演算法

在負載均衡時使用保持本機優先。這個相信大家也比較好理解。在所有的可選地址中,找到本機發布的地址,然後進行呼叫。

@Override
public ProviderInfo doSelect(SofaRequest invocation, List<ProviderInfo> providerInfos) {
    String localhost = SystemInfo.getLocalHost();
    if (StringUtils.isEmpty(localhost)) {
        return super.doSelect(invocation, providerInfos);
    }
    List<ProviderInfo> localProviderInfo = new ArrayList<ProviderInfo>();
    for (ProviderInfo providerInfo : providerInfos) { // 解析IP,看是否和本地一致
        if (localhost.equals(providerInfo.getHost())) {
            localProviderInfo.add(providerInfo);
        }
    }
    if (CommonUtils.isNotEmpty(localProviderInfo)) { // 命中本機的服務端
        return super.doSelect(invocation, localProviderInfo);
    } else { // 沒有命中本機上的服務端
        return super.doSelect(invocation, providerInfos);
    }
}
  1. 檢視本機的host,如果為空,那麼直接呼叫父類隨機演算法
  2. 遍歷所有的provider,如果服務提供方的host和服務呼叫方的host一致,那麼儲存到集合裡
  3. 如果存在服務提供方的host和服務呼叫方的host一致,那麼就在這些集合中選取
  4. 如果不一致,那麼就在所有provider中選取

輪詢演算法

我們首先來看看SOFARPC的輪訓是怎麼實現的:

private final ConcurrentMap<String, PositiveAtomicCounter> sequences = new ConcurrentHashMap<String, PositiveAtomicCounter>();

@Override
public ProviderInfo doSelect(SofaRequest request, List<ProviderInfo> providerInfos) {
    String key = getServiceKey(request); // 每個方法級自己輪詢,互不影響
    int length = providerInfos.size(); // 總個數
    PositiveAtomicCounter sequence = sequences.get(key);
    if (sequence == null) {
        sequences.putIfAbsent(key, new PositiveAtomicCounter());
        sequence = sequences.get(key);
    }
    return providerInfos.get(sequence.getAndIncrement() % length);
}

private String getServiceKey(SofaRequest request) {
    StringBuilder builder = new StringBuilder();
    builder.append(request.getTargetAppName()).append("#")
        .append(request.getMethodName());
    return builder.toString();
}

從上面的程式碼我們可以看出,SOFARPC的輪詢做的很直接簡單。就是new了一個map,然後把每個服務的服務名拼上方法名存到map裡面,然後每次value值自增1對provider取模。

我們再看dubbo的實現方式:

protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
    String key = invokers.get(0).getUrl().getServiceKey() + "." + invocation.getMethodName();
    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 (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);
}

dubbo的輪詢的實現裡面還加入了權重在裡面,sofarpc的權重輪詢是放到另外一個類當中去做的,因為效能太差了而被棄用了。

我們舉個例子來簡單看一下dubbo的加權輪詢是怎麼做的:

假定有3臺dubbo provider:

10.0.0.1:20884, weight=2
10.0.0.1:20886, weight=3
10.0.0.1:20888, weight=4

totalWeight=9;

那麼第一次呼叫的時候:
10.0.0.1:20884, weight=2    selectedWRR -> current = 2
10.0.0.1:20886, weight=3    selectedWRR -> current = 3
10.0.0.1:20888, weight=4    selectedWRR -> current = 4
 
selectedInvoker-> 10.0.0.1:20888 
呼叫 selectedWRR.sel(totalWeight); 
10.0.0.1:20888, weight=4    selectedWRR -> current = -5
返回10.0.0.1:20888這個例項

那麼第二次呼叫的時候:
10.0.0.1:20884, weight=2    selectedWRR -> current = 4
10.0.0.1:20886, weight=3    selectedWRR -> current = 6
10.0.0.1:20888, weight=4    selectedWRR -> current = -1

selectedInvoker-> 10.0.0.1:20886 
呼叫 selectedWRR.sel(totalWeight); 
10.0.0.1:20886 , weight=4   selectedWRR -> current = -3
返回10.0.0.1:20886這個例項

那麼第三次呼叫的時候:
10.0.0.1:20884, weight=2    selectedWRR -> current = 6
10.0.0.1:20886, weight=3    selectedWRR -> current = 0
10.0.0.1:20888, weight=4    selectedWRR -> current = 3

selectedInvoker-> 10.0.0.1:20884
呼叫 selectedWRR.sel(totalWeight); 
10.0.0.1:20884, weight=2    selectedWRR -> current = -3
返回10.0.0.1:20884這個例項

一致性hash演算法

在SOFARPC中有兩種方式實現一致性hash演算法,一種是帶權重的一種是不帶權重的,我對比了一下,兩邊的程式碼基本上是一樣的,所以我直接分析帶權重的程式碼就好了。

下面我們來分析一下程式碼:

private final ConcurrentHashMap<String, Selector> selectorCache = new ConcurrentHashMap<String, Selector>();


@Override
public ProviderInfo doSelect(SofaRequest request, List<ProviderInfo> providerInfos) {
    String interfaceId = request.getInterfaceName();
    String method = request.getMethodName();
    String key = interfaceId + "#" + method;
    // 判斷是否同樣的服務列表
    int hashcode = providerInfos.hashCode();
    Selector selector = selectorCache.get(key);
    // 原來沒有
    if (selector == null ||
        // 或者服務列表已經變化
        selector.getHashCode() != hashcode) {
        selector = new Selector(interfaceId, method, providerInfos, hashcode);
        selectorCache.put(key, selector);
    }
    return selector.select(request);
} 

上面的doSelect方法就是獲取到相同服務的Selector,如果沒有就新建一個。Selector是WeightConsistentHashLoadBalancer裡面的內部類,我們接下來看看這個內部類的實現。


public Selector(String interfaceId, String method, List<ProviderInfo> actualNodes, int hashcode) {
    this.interfaceId = interfaceId;
    this.method = method;
    this.hashcode = hashcode;
    // 建立虛擬節點環 (provider建立虛擬節點數 =  真實節點權重 * 32)
    this.virtualNodes = new TreeMap<Long, ProviderInfo>();
    // 設定越大越慢,精度越高
    int num = 32;
    for (ProviderInfo providerInfo : actualNodes) {
        for (int i = 0; i < num * providerInfo.getWeight() / 4; i++) {
            byte[] digest = HashUtils.messageDigest(providerInfo.getHost() + providerInfo.getPort() + i);
            for (int h = 0; h < 4; h++) {
                long m = HashUtils.hash(digest, h);
                virtualNodes.put(m, providerInfo);
            }
        }
    }
}

Selector內部類中就是構建了一個TreeMap例項,然後遍歷所有的provider,每個provider虛擬的節點數是(真實節點權重 * 32)個。

虛擬好節點後,我們直接呼叫Selector#select方法在hash環中得到相應的provider。

public ProviderInfo select(SofaRequest request) {
    String key = buildKeyOfHash(request.getMethodArgs());
    byte[] digest = HashUtils.messageDigest(key);
    return selectForKey(HashUtils.hash(digest, 0));
}

/**
 * 獲取第一引數作為hash的key
 *
 * @param args the args
 * @return the string
 */
private String buildKeyOfHash(Object[] args) {
    if (CommonUtils.isEmpty(args)) {
        return StringUtils.EMPTY;
    } else {
        return StringUtils.toString(args[0]);
    }
}

/**
 * Select for key.
 *
 * @param hash the hash
 * @return the provider
 */
private ProviderInfo selectForKey(long hash) {
    Map.Entry<Long, ProviderInfo> entry = virtualNodes.ceilingEntry(hash);
    if (entry == null) {
        entry = virtualNodes.firstEntry();
    }
    return entry.getValue();
}

這上面主要是獲取第一引數作為hash的key,然後對它進行hash。所以我感覺這裡可能有一個問題就是如果一個某個服務裡面很多個引數一樣的服務,那麼是不是都會打到那同一臺機器上呢?

dubbo的實現方式也和SOFARPC類似,這裡不再贅述。

相關文章