Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make some optimization for leastConnsBalance, Benchmark increases 43% #1062

Merged
merged 1 commit into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions bfe_balance/bal_slb/bal_rr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

}
}

Expand All @@ -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
}

Expand Down
64 changes: 64 additions & 0 deletions bfe_balance/bal_slb/bal_rr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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}
Expand Down