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

bugfix: leastConnsBalance #1159 #1160

Closed
wants to merge 7 commits into from
Closed
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
12 changes: 5 additions & 7 deletions bfe_balance/bal_slb/bal_rr.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,12 @@ import (
"math/rand"
"sort"
"sync"
)

import (
"github.com/baidu/go-lib/log"
"github.com/spaolacci/murmur3"
)

import (
"github.com/bfenetworks/bfe/bfe_balance/backend"
"github.com/bfenetworks/bfe/bfe_config/bfe_cluster_conf/cluster_table_conf"
"github.com/bfenetworks/bfe/bfe_debug"
"github.com/spaolacci/murmur3"
)

// implementation versions of weighted round-robin algorithm
Expand Down Expand Up @@ -343,6 +338,7 @@ func leastConnsBalance(backs BackendList) (BackendList, error) {
if ret > 0 {
best = backendRR
singleBackend = true
candidates = candidates[0:0]
} else if ret == 0 {
singleBackend = false
if len(candidates) > 0 {
Expand All @@ -360,7 +356,9 @@ func leastConnsBalance(backs BackendList) (BackendList, error) {

// single backend, return directly
if singleBackend {
return BackendList{best}, nil
candidates = candidates[0:0]
candidates = append(candidates, best)
return candidates, nil
}
// more than one backend have same connections/weight,
// return all the candidates
Expand Down
74 changes: 69 additions & 5 deletions bfe_balance/bal_slb/bal_rr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ import (
"math/rand"
"reflect"
"testing"
)

import (
"github.com/bfenetworks/bfe/bfe_balance/backend"
"github.com/bfenetworks/bfe/bfe_config/bfe_cluster_conf/cluster_table_conf"
"github.com/bfenetworks/bfe/bfe_util/json"
Expand Down Expand Up @@ -131,7 +129,7 @@ func processBalancLoopTwenty(t *testing.T, label string, algor int, key []byte,

func processSimpleBalance(t *testing.T, label string, algor int, key []byte, rr *BalanceRR, result []string) {
var l []string
loopCount := (300+200+100)+4
loopCount := (300 + 200 + 100) + 4

for i := 1; i < loopCount; i++ {
r, err := rr.Balance(algor, key)
Expand All @@ -156,7 +154,7 @@ func processSimpleBalance(t *testing.T, label string, algor int, key []byte, rr

func processSimpleBalance3(t *testing.T, label string, algor int, key []byte, rr *BalanceRR, result []string) {
var l []string
loopCount := (200+100)*3+4
loopCount := (200+100)*3 + 4

for i := 1; i < loopCount; i++ {
r, err := rr.Balance(algor, key)
Expand Down Expand Up @@ -218,7 +216,7 @@ func TestBalance(t *testing.T) {
rr.backends[0].backend.SetAvail(false)
// after scale up 100, the hash result changed
expectResult = []string{"b3", "b3", "b3", "b3", "b3", "b3", "b3", "b3", "b3"}
// expectResult = []string{"b2", "b2", "b2", "b2", "b2", "b2", "b2", "b2", "b2"}
// expectResult = []string{"b2", "b2", "b2", "b2", "b2", "b2", "b2", "b2", "b2"}
processBalance(t, "case 6", WrrSticky, []byte{1}, rr, expectResult)

// case 7, lcw balance
Expand Down Expand Up @@ -363,3 +361,69 @@ func TestSlowStart(t *testing.T) {
rr := prepareBalanceRR()
rr.SetSlowStart(30)
}

func Test_leastConnsBalance(t *testing.T) {
{
backs := BackendList{
{
backend: backend.NewBfeBackend(),
weight: 10,
},
{
backend: backend.NewBfeBackend(),
weight: 10,
},
{
backend: backend.NewBfeBackend(),
weight: 20,
},
{
backend: backend.NewBfeBackend(),
weight: 20,
},
}

for _, back := range backs {
back.backend.IncConnNum()
}

got, err := leastConnsBalance(backs)
if err != nil {
t.Errorf("leastConnsBalance() error = %v", err)
return
}
if len(got) != 2 {
t.Errorf("want length: %d, got: %d", 2, len(got))
}
}

{
backs := BackendList{
{
backend: backend.NewBfeBackend(),
weight: 10,
},
{
backend: backend.NewBfeBackend(),
weight: 20,
},
{
backend: backend.NewBfeBackend(),
weight: 30,
},
}

for _, back := range backs {
back.backend.IncConnNum()
}

got, err := leastConnsBalance(backs)
if err != nil {
t.Errorf("leastConnsBalance() error = %v", err)
return
}
if len(got) != 1 {
t.Errorf("want length: %d, got: %d", 1, len(got))
}
}
}
Loading