上一篇講了普通輪詢、加權輪詢的兩種實現方式,重點講了平滑加權輪詢演算法,並在文末留下了懸念:節點出現分配失敗時降低有效權重值;成功時提高有效權重值(但不能大於weight值)。
本文在平滑加權輪詢演算法的基礎上講,還沒弄懂的可以看上一篇文章。
現在來模擬實現:平滑加權輪詢演算法的降權和提權
1.兩個關鍵點
節點當機時,降低有效權重值;
節點正常時,提高有效權重值(但不能大於weight值);
注意:降低或提高權重都是針對有效權重。
2.程式碼實現
2.1.服務節點類
package com.yty.loadbalancingalgorithm.wrr;
/**
* String ip:負載IP
* final Integer weight:權重,儲存配置的權重
* Integer effectiveWeight:有效權重,輪詢的過程權重可能變化
* Integer currentWeight:當前權重,比對該值大小獲取節點
* 第一次加權輪詢時:currentWeight = weight = effectiveWeight
* 後面每次加權輪詢時:currentWeight 的值都會不斷變化,其他權重不變
* Boolean isAvailable:是否存活
*/
public class ServerNode implements Comparable<ServerNode>{
private String ip;
private final Integer weight;
private Integer effectiveWeight;
private Integer currentWeight;
private Boolean isAvailable;
public ServerNode(String ip, Integer weight){
this(ip,weight,true);
}
public ServerNode(String ip, Integer weight,Boolean isAvailable){
this.ip = ip;
this.weight = weight;
this.effectiveWeight = weight;
this.currentWeight = weight;
this.isAvailable = isAvailable;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public Integer getWeight() {
return weight;
}
public Integer getEffectiveWeight() {
return effectiveWeight;
}
public void setEffectiveWeight(Integer effectiveWeight) {
this.effectiveWeight = effectiveWeight;
}
public Integer getCurrentWeight() {
return currentWeight;
}
public void setCurrentWeight(Integer currentWeight) {
this.currentWeight = currentWeight;
}
public Boolean isAvailable() {
return isAvailable;
}
public void setIsAvailable(Boolean isAvailable){
this.isAvailable = isAvailable;
}
// 每成功一次,恢復有效權重1,不超過配置的起始權重
public void onInvokeSuccess(){
if(effectiveWeight < weight) effectiveWeight++;
}
// 每失敗一次,有效權重減少1,無底線的減少
public void onInvokeFault(){
effectiveWeight--;
}
@Override
public int compareTo(ServerNode node) {
return currentWeight > node.currentWeight ? 1 : (currentWeight.equals(node.currentWeight) ? 0 : -1);
}
@Override
public String toString() {
return "{ip='" + ip + "', weight=" + weight + ", effectiveWeight=" + effectiveWeight
+ ", currentWeight=" + currentWeight + ", isAvailable=" + isAvailable + "}";
}
}
2.2.平滑輪詢演算法降權和提權
package com.yty.loadbalancingalgorithm.wrr;
import java.util.ArrayList;
import java.util.List;
/**
* 加權輪詢演算法:加入存活狀態,降權使當機權重降低,從而不會被選中
*/
public class WeightedRoundRobinAvailable {
private static List<ServerNode> serverNodes = new ArrayList<>();
// 準備模擬資料
static {
serverNodes.add(new ServerNode("192.168.1.101",1));// 預設為true
serverNodes.add(new ServerNode("192.168.1.102",3,false));
serverNodes.add(new ServerNode("192.168.1.103",2));
}
/**
* 按照當前權重(currentWeight)最大值獲取IP
* @return ServerNode
*/
public ServerNode selectNode(){
if (serverNodes.size() <= 0) return null;
if (serverNodes.size() == 1)
return (serverNodes.get(0).isAvailable()) ? serverNodes.get(0) : null;
// 權重之和
Integer totalWeight = 0;
ServerNode nodeOfMaxWeight = null; // 儲存輪詢選中的節點資訊
synchronized (serverNodes){
StringBuffer sb1 = new StringBuffer();
StringBuffer sb2 = new StringBuffer();
sb1.append(Thread.currentThread().getName()+"==加權輪詢--[當前權重]值的變化:"+printCurrentWeight(serverNodes));
// 有限權重總和可能發生變化
for(ServerNode serverNode : serverNodes){
totalWeight += serverNode.getEffectiveWeight();
}
// 選出當前權重最大的節點
ServerNode tempNodeOfMaxWeight = serverNodes.get(0);
for (ServerNode serverNode : serverNodes) {
if (serverNode.isAvailable()) {
serverNode.onInvokeSuccess();//提權
sb2.append(Thread.currentThread().getName()+"==[正常節點]:"+serverNode+"\n");
} else {
serverNode.onInvokeFault();//降權
sb2.append(Thread.currentThread().getName()+"==[當機節點]:"+serverNode+"\n");
}
tempNodeOfMaxWeight = tempNodeOfMaxWeight.compareTo(serverNode) > 0 ? tempNodeOfMaxWeight : serverNode;
}
// 必須new個新的節點例項來儲存資訊,否則引用指向同一個堆例項,後面的set操作將會修改節點資訊
nodeOfMaxWeight = new ServerNode(tempNodeOfMaxWeight.getIp(),tempNodeOfMaxWeight.getWeight(),tempNodeOfMaxWeight.isAvailable());
nodeOfMaxWeight.setEffectiveWeight(tempNodeOfMaxWeight.getEffectiveWeight());
nodeOfMaxWeight.setCurrentWeight(tempNodeOfMaxWeight.getCurrentWeight());
// 調整當前權重比:按權重(effectiveWeight)的比例進行調整,確保請求分發合理。
tempNodeOfMaxWeight.setCurrentWeight(tempNodeOfMaxWeight.getCurrentWeight() - totalWeight);
sb1.append(" -> "+printCurrentWeight(serverNodes));
serverNodes.forEach(serverNode -> serverNode.setCurrentWeight(serverNode.getCurrentWeight()+serverNode.getEffectiveWeight()));
sb1.append(" -> "+printCurrentWeight(serverNodes));
System.out.print(sb2); //所有節點的當前資訊
System.out.println(sb1); //列印當前權重變化過程
}
return nodeOfMaxWeight;
}
// 格式化列印資訊
private String printCurrentWeight(List<ServerNode> serverNodes){
StringBuffer stringBuffer = new StringBuffer("[");
serverNodes.forEach(node -> stringBuffer.append(node.getCurrentWeight()+",") );
return stringBuffer.substring(0, stringBuffer.length() - 1) + "]";
}
// 併發測試:兩個執行緒迴圈獲取節點
public static void main(String[] args) throws InterruptedException {
// 迴圈次數
int loop = 18;
new Thread(() -> {
WeightedRoundRobinAvailable weightedRoundRobin1 = new WeightedRoundRobinAvailable();
for(int i=1;i<=loop;i++){
ServerNode serverNode = weightedRoundRobin1.selectNode();
System.out.println(Thread.currentThread().getName()+"==第"+i+"次輪詢選中[當前權重最大]的節點:" + serverNode + "\n");
}
}).start();
//
new Thread(() -> {
WeightedRoundRobinAvailable weightedRoundRobin2 = new WeightedRoundRobinAvailable();
for(int i=1;i<=loop;i++){
ServerNode serverNode = weightedRoundRobin2.selectNode();
System.out.println(Thread.currentThread().getName()+"==第"+i+"次輪詢選中[當前權重最大]的節點:" + serverNode + "\n");
}
}).start();
//main 執行緒睡了一下,再偷偷把 所有當機 拉起來:模擬伺服器恢復正常
Thread.sleep(5);
for (ServerNode serverNode:serverNodes){
if(!serverNode.isAvailable())
serverNode.setIsAvailable(true);
}
}
}
3.分析結果
執行結果:將執行結果的前中後四次抽出來分析
Thread-0==[正常節點]:{ip='192.168.1.101', weight=1, effectiveWeight=1, currentWeight=1, isAvailable=true}
Thread-0==[當機節點]:{ip='192.168.1.102', weight=3, effectiveWeight=2, currentWeight=3, isAvailable=false}
Thread-0==[正常節點]:{ip='192.168.1.103', weight=2, effectiveWeight=2, currentWeight=2, isAvailable=true}
Thread-0==加權輪詢--[當前權重]值的變化:[1,3,2] -> [1,-3,2] -> [2,-1,4]
Thread-0==第1次輪詢選中[當前權重最大]的節點:{ip='192.168.1.102', weight=3, effectiveWeight=2, currentWeight=3, isAvailable=false}
……
Thread-1==[正常節點]:{ip='192.168.1.101', weight=1, effectiveWeight=1, currentWeight=6, isAvailable=true}
Thread-1==[當機節點]:{ip='192.168.1.102', weight=3, effectiveWeight=-7, currentWeight=-21, isAvailable=false}
Thread-1==[正常節點]:{ip='192.168.1.103', weight=2, effectiveWeight=2, currentWeight=12, isAvailable=true}
Thread-1==加權輪詢--[當前權重]值的變化:[6,-21,12] -> [6,-21,15] -> [7,-28,17]
Thread-1==第5次輪詢選中[當前權重最大]的節點:{ip='192.168.1.103', weight=2, effectiveWeight=2, currentWeight=12, isAvailable=true}
……
Thread-0==[正常節點]:{ip='192.168.1.101', weight=1, effectiveWeight=1, currentWeight=13, isAvailable=true}
Thread-0==[正常節點]:{ip='192.168.1.102', weight=3, effectiveWeight=3, currentWeight=-19, isAvailable=true}
Thread-0==[正常節點]:{ip='192.168.1.103', weight=2, effectiveWeight=2, currentWeight=12, isAvailable=true}
Thread-0==加權輪詢--[當前權重]值的變化:[13,-19,12] -> [7,-19,12] -> [8,-16,14]
Thread-0==第15次輪詢選中[當前權重最大]的節點:{ip='192.168.1.101', weight=1, effectiveWeight=1, currentWeight=13, isAvailable=true}
……
Thread-1==[正常節點]:{ip='192.168.1.101', weight=1, effectiveWeight=1, currentWeight=2, isAvailable=true}
Thread-1==[正常節點]:{ip='192.168.1.102', weight=3, effectiveWeight=3, currentWeight=2, isAvailable=true}
Thread-1==[正常節點]:{ip='192.168.1.103', weight=2, effectiveWeight=2, currentWeight=2, isAvailable=true}
Thread-1==加權輪詢--[當前權重]值的變化:[2,2,2] -> [2,2,-4] -> [3,5,-2]
Thread-1==第18次輪詢選中[當前權重最大]的節點:{ip='192.168.1.103', weight=2, effectiveWeight=2, currentWeight=2, isAvailable=true}
分析
一開始權重最高的節點雖然是當機了,但是還是會被選中並返回;
“有效權重總和” 和 “當前權重總和”都減少了1,因為設定輪詢到失敗節點,都會自減1;
到第5次輪詢時,當前權重已經變成了[7,-28,17],可以看出當機節點越往後當前權重越小,所以後面根本不會再選中當機節點,雖然沒剔除故障節點,但卻起到不分配當機節點;
到第15次輪詢時,有效權重已經恢復起始值,當前權重變為[8,-16,14],當前權重只能慢慢恢復,並不是節點一正常就立即恢復當機過的節點,起到對故障節點的緩衝恢復(故障過的節點可能還存在問題);
最後1次輪詢時,因為沒有當機節點,所以有效權重不變,當前權重已經恢復[3,5,-2],如果再輪詢一次,那就會訪問到一開始故障的節點了。
4.結論
降權起到緩慢“剔除”當機節點的效果;提權起到緩衝恢復當機節點的效果。
對比上一篇文章可以看到:
當前權重(currentWeight):針對的是節點的選擇,受有效權重影響,起到緩慢“剔除”當機節點和緩衝恢復當機節點的效果,當前權重最高就會被選擇;
有效權重(effectiveWeight):針對的是權重的變化,也即是降權和提權,降權/提權只會直接操作有效權重;
權重(weight):針對的是儲存起始配置,限定有效權重的提權。