Back to LVS series article: http://www.cnblogs.com/f-ck-need-u/p/7576137.html
The weighted scheduling algorithm is a very common scheduling algorithm. If there are only two back-ends, the order of dispatch is easy, but if the back end is more than 2, it may not be dispatched in the order it was imagined.
So, the paper discusses how to dispatch the weighted scheduling algorithm in the end.
1. Weighted Scheduling algorithm formula
First, give an official LVS manual for the weighted scheduling algorithm formula:
Suppose there is a set of servers S = {S0, S1, ..., Sn-1},w (SI) represents the weight of the server Si, a
Indicates that the variable i represents the last server selected, indicating that the variable CW represents the right value for the current dispatch, Max (S)
Represents the maximum weight of all servers in the collection S, gcd (s) represents the maximum of all server weights in the collection s
Number of conventions. The variable i is initialized to -1,CW initialized to zero.
while (true) { i = (i + 1) mod n; if (i == 0) { cw = cw - gcd(S); if (cw <= 0) { cw = max(S); if (cw == 0) return NULL; } } if (W(Si) >= cw) return Si;}
For example, a, B, c three back-end weight ratio is 2:3:4
, then a dispatch loop within the scheduling sequence is cbcabcabc.
If you don't want to find the rules in algorithmic formulas, look below.
2. Weighted scheduling Popular law
Remember three weight scheduling rules:
1. First Numerator
2. Start scheduling from Maximum weight
3. The back end of the same weight, dispatched from front to back
For example, three back ends A:B:C=2:3:4
. I can't numerator here.
- Dispatch C
After scheduling, the ratios become A:B:C=2:3:3
, B and C weights are the same, starting with B scheduling
- Dispatch b
After scheduling, the ratio becomes A:B:C=2:2:3
, so the next time you schedule C
- Dispatch C
After the dispatch, the ratio becomes A:B:C=2:2:2
, the next time starting from a
When the weights are all adjusted to the same value, they are continuously looped in order until all weights are dispatched
- Dispatch A, after scheduling, the ratio becomes
A:B:C=1:2:2
- Dispatch B, after dispatch, the ratio becomes
A:B:C=1:1:2
- Dispatch C, after dispatch, the ratio becomes
A:B:C=1:1:1
- Dispatch A, after scheduling, the ratio becomes
A:B:C=0:1:1
- Dispatch B, after dispatch, the ratio becomes
A:B:C=0:0:1
- Dispatch C, after dispatch, the ratio becomes
A:B:C=0:0:0
- Enter the next scheduling loop, in order: CBCABCABC
Therefore, the scheduling order for each scheduling cycle is: CBCABCABC
The scheduling process is as follows:
To give an example,A:B:C:D=2:4:6:8
First numerator, getA:B:C:D=1:2:3:4
- Dispatch D
- Dispatch C
- Dispatch D
- Dispatch b
- Dispatch C
- Dispatch D
- Dispatch a
- Dispatch b
- Dispatch C
- Dispatch D
Therefore, the scheduling sequence is DCDBCDABCD.
Application of load-balanced LVs (v): The law of weighted scheduling algorithm