From e6abcae560480c5fea9f83dbfaabacd1ec5efca7 Mon Sep 17 00:00:00 2001 From: likepeng Date: Sat, 9 Jul 2022 23:54:13 +0800 Subject: [PATCH] make some optimization for leastConnsBalance, Benchmark increases 43% Signed-off-by: likepeng Signed-off-by: likepeng --- bfe_balance/bal_slb/bal_rr.go | 20 ++++------ bfe_balance/bal_slb/bal_rr_test.go | 64 ++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 13 deletions(-) diff --git a/bfe_balance/bal_slb/bal_rr.go b/bfe_balance/bal_slb/bal_rr.go index fa41b3bc9..507df0d46 100644 --- a/bfe_balance/bal_slb/bal_rr.go +++ b/bfe_balance/bal_slb/bal_rr.go @@ -345,6 +345,12 @@ func leastConnsBalance(backs BackendList) (BackendList, error) { singleBackend = true } else if ret == 0 { singleBackend = false + if len(candidates) > 0 { + candidates = append(candidates, backendRR) + } else { + candidates = append(candidates, best, backendRR) + } + } } @@ -354,22 +360,10 @@ func leastConnsBalance(backs BackendList) (BackendList, error) { // single backend, return directly if singleBackend { - candidates = append(candidates, best) - return candidates, nil + return BackendList{best}, nil } - // more than one backend have same connections/weight, // return all the candidates - for _, backendRR := range backs { - if !backendRR.backend.Avail() || backendRR.weight <= 0 { - continue - } - - if ret := compLCWeight(best, backendRR); ret == 0 { - candidates = append(candidates, backendRR) - } - } - return candidates, nil } diff --git a/bfe_balance/bal_slb/bal_rr_test.go b/bfe_balance/bal_slb/bal_rr_test.go index 2d478a577..db288ba3d 100644 --- a/bfe_balance/bal_slb/bal_rr_test.go +++ b/bfe_balance/bal_slb/bal_rr_test.go @@ -64,6 +64,39 @@ func prepareBalanceRR() *BalanceRR { return rr } +func prepareBalanceRRLcw() *BalanceRR { + b1 := populateBackend("b1", "127.0.0.1", 80, true) + b2 := populateBackend("b2", "127.0.0.1", 81, true) + b3 := populateBackend("b3", "127.0.0.1", 82, true) + b4 := populateBackend("b4", "127.0.0.1", 83, true) + + rr := &BalanceRR{ + backends: []*BackendRR{ + { + weight: 300, + current: 300, + backend: b1, + }, + { + weight: 200, + current: 200, + backend: b2, + }, + { + weight: 100, + current: 100, + backend: b3, + }, + { + weight: 50, + current: 50, + backend: b4, + }, + }, + } + return rr +} + func processBalance(t *testing.T, label string, algor int, key []byte, rr *BalanceRR, result []string) { var l []string for i := 1; i < 10; i++ { @@ -80,6 +113,22 @@ func processBalance(t *testing.T, label string, algor int, key []byte, rr *Balan } } +func processBalancLoopTwenty(t *testing.T, label string, algor int, key []byte, rr *BalanceRR, result []string) { + var l []string + for i := 1; i < 20; i++ { + r, err := rr.Balance(algor, key) + if err != nil { + t.Errorf("should not error") + } + r.IncConnNum() + l = append(l, r.Name) + } + + if !reflect.DeepEqual(l, result) { + t.Errorf("balance error [%s] %v, expect %v", label, l, result) + } +} + func processSimpleBalance(t *testing.T, label string, algor int, key []byte, rr *BalanceRR, result []string) { var l []string loopCount := (300+200+100)+4 @@ -176,6 +225,12 @@ func TestBalance(t *testing.T) { rr = prepareBalanceRR() expectResult = []string{"b1", "b2", "b3", "b1", "b2", "b1", "b3", "b1", "b2"} processBalance(t, "case 7", WlcSmooth, []byte{1}, rr, expectResult) + + // case 8, lcw balance same weight + rr = prepareBalanceRRLcw() + expectResult = []string{"b1", "b2", "b3", "b4", "b1", "b2", "b1", "b1", "b2", "b3", "b1", "b2", "b1", + "b1", "b2", "b3", "b4", "b1", "b2"} + processBalancLoopTwenty(t, "case 8", WlcSmooth, []byte{1}, rr, expectResult) } func TestUpdate(t *testing.T) { @@ -284,6 +339,15 @@ func BenchmarkSimpleBalance(b *testing.B) { } } +func BenchmarkWlcBalance(b *testing.B) { + rr := prepareBalanceRRForBench() + + b.ResetTimer() + for i := 0; i < b.N; i++ { + rr.leastConnsSmoothBalance() + } +} + func BenchmarkStickyBalance(b *testing.B) { rr := prepareBalanceRRForBench() key := []byte{100}