public class FairStrategy extends Object implements LoadBalanceStrategy
Algorithm overview: The fair load balancing policy dynamically adjusts the weight of each server and divides it by weight. Rpc client maintains a latency window record rpc time for each server, after processing a traffic, the server updates its own window. If the mean value of the service in the window > the overall mean of the service, reduce its own weight. On the contrary, increase its own weight. When the window is not full, the polling does not adjust the weight.
LOAD_BALANCE_FAIR, LOAD_BALANCE_RANDOM, LOAD_BALANCE_ROUND_ROBIN, LOAD_BALANCE_WEIGHT| 构造器和说明 |
|---|
FairStrategy() |
| 限定符和类型 | 方法和说明 |
|---|---|
protected int |
calculateWeight(BrpcChannel group,
int timeOut)
Calculate the weight of a rpc server
|
void |
destroy() |
protected BrpcChannel |
fairSelect(com.baidu.brpc.client.loadbalance.FairStrategy.Node root) |
protected com.baidu.brpc.client.loadbalance.FairStrategy.Node |
generateWeightTreeByLeafNodes(Queue<com.baidu.brpc.client.loadbalance.FairStrategy.Node> leafNodes)
generate the tree by leaf nodes
the leaf nodes are the real rpc servers
the parent nodes used to calculate the sum of it's children's weight
|
protected long |
getRandomLong() |
void |
init(RpcClient rpcClient) |
void |
markInvalidInstance(List<BrpcChannel> instances)
Since the weight tree will update by a period of time, so if there's any invalid instance,
the business should notify the fair strategy.
|
protected BrpcChannel |
randomSelect(List<BrpcChannel> instances) |
protected com.baidu.brpc.client.loadbalance.FairStrategy.Node |
searchNode(com.baidu.brpc.client.loadbalance.FairStrategy.Node parent,
int weight) |
BrpcChannel |
selectInstance(Request request,
List<BrpcChannel> instances,
Set<BrpcChannel> selectedInstances)
select instance channel from total instances
|
protected void |
updateWeightTree()
Update weight of each node of the tree.
|
public void init(RpcClient rpcClient)
init 在接口中 LoadBalanceStrategypublic BrpcChannel selectInstance(Request request, List<BrpcChannel> instances, Set<BrpcChannel> selectedInstances)
LoadBalanceStrategyselectInstance 在接口中 LoadBalanceStrategyrequest - request infoinstances - total instances, often are all healthy instancesselectedInstances - instances which have been selected.public void destroy()
destroy 在接口中 LoadBalanceStrategypublic void markInvalidInstance(List<BrpcChannel> instances)
protected BrpcChannel randomSelect(List<BrpcChannel> instances)
protected long getRandomLong()
protected BrpcChannel fairSelect(com.baidu.brpc.client.loadbalance.FairStrategy.Node root)
protected com.baidu.brpc.client.loadbalance.FairStrategy.Node searchNode(com.baidu.brpc.client.loadbalance.FairStrategy.Node parent,
int weight)
protected void updateWeightTree()
treeContainerprotected int calculateWeight(BrpcChannel group, int timeOut)
group - The BrpcChannel instance of a rpc servertimeOut - Read timeout in millisprotected com.baidu.brpc.client.loadbalance.FairStrategy.Node generateWeightTreeByLeafNodes(Queue<com.baidu.brpc.client.loadbalance.FairStrategy.Node> leafNodes)
leafNodes - leaf nodes listCopyright © 2019 Baidu, Inc.. All rights reserved.