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.
| Modifier and Type | Class and Description |
|---|---|
static class |
FairStrategy.Node
The weight tree node
|
| Modifier and Type | Field and Description |
|---|---|
protected CopyOnWriteArrayList<FairStrategy.Node> |
treeContainer
The binary tree used to save weight number for each node.
|
LOAD_BALANCE_FAIR, LOAD_BALANCE_RANDOM, LOAD_BALANCE_ROUND_ROBIN, LOAD_BALANCE_WEIGHT| Constructor and Description |
|---|
FairStrategy() |
| Modifier and Type | Method and Description |
|---|---|
protected int |
calculateWeight(CommunicationClient instance,
int timeout)
Calculate the weight of a rpc server
|
void |
destroy() |
protected CommunicationClient |
fairSelect(FairStrategy.Node root) |
protected FairStrategy.Node |
generateWeightTreeByLeafNodes(Queue<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<CommunicationClient> 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 FairStrategy.Node |
searchNode(FairStrategy.Node parent,
int weight) |
CommunicationClient |
selectInstance(Request request,
List<CommunicationClient> instances,
Set<CommunicationClient> selectedInstances)
select instance channel from total instances
|
protected void |
updateWeightTree()
Update weight of each node of the tree.
|
protected CopyOnWriteArrayList<FairStrategy.Node> treeContainer
The weight tree can achieve time complexity at the O(logN) level, and 1000 servers require only 11 memory accesses.
public void init(RpcClient rpcClient)
init in interface LoadBalanceStrategypublic CommunicationClient selectInstance(Request request, List<CommunicationClient> instances, Set<CommunicationClient> selectedInstances)
LoadBalanceStrategyselectInstance in interface LoadBalanceStrategyrequest - request infoinstances - total instances, often are all healthy instancesselectedInstances - instances which have been selected.public void destroy()
destroy in interface LoadBalanceStrategypublic void markInvalidInstance(List<CommunicationClient> instances)
protected long getRandomLong()
protected CommunicationClient fairSelect(FairStrategy.Node root)
protected FairStrategy.Node searchNode(FairStrategy.Node parent, int weight)
protected void updateWeightTree()
treeContainerprotected int calculateWeight(CommunicationClient instance, int timeout)
instance - The instance of a rpc servertimeout - Read timeout in millisprotected FairStrategy.Node generateWeightTreeByLeafNodes(Queue<FairStrategy.Node> leafNodes)
leafNodes - leaf nodes listCopyright © 2022 Baidu, Inc.. All rights reserved.