Skip to content

Commit

Permalink
Fixed scheduler handling backends update bug; removed redundant list …
Browse files Browse the repository at this point in the history
…of backends
  • Loading branch information
illarion committed Aug 21, 2018
1 parent 8898001 commit 21a2279
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
13 changes: 10 additions & 3 deletions src/balance/weight.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package balance
import (
"errors"
"math/rand"
"sort"

"../core"
)
Expand All @@ -20,16 +21,22 @@ type WeightBalancer struct{}

/**
* Elect backend based on weight strategy
* TODO: Ensure backends are sorted in the same way (not it's not bacause of map in scheduler)
*/
func (b *WeightBalancer) Elect(context core.Context, backends []*core.Backend) (*core.Backend, error) {

if len(backends) == 0 {
return nil, errors.New("Can't elect backend, Backends empty")
}

sorted := make([]*core.Backend, len(backends))
copy(sorted, backends)

sort.SliceStable(sorted, func(i, j int) bool {
return sorted[i].Target.String() < sorted[j].Target.String()
})

totalWeight := 0
for _, backend := range backends {
for _, backend := range sorted {
if backend.Weight <= 0 {
return nil, errors.New("Invalid backend weight 0")
}
Expand All @@ -39,7 +46,7 @@ func (b *WeightBalancer) Elect(context core.Context, backends []*core.Backend) (
r := rand.Intn(totalWeight)
pos := 0

for _, backend := range backends {
for _, backend := range sorted {
pos += backend.Weight
if r >= pos {
continue
Expand Down
17 changes: 6 additions & 11 deletions src/server/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@ type Scheduler struct {
/* Current cached backends map */
backends map[core.Target]*core.Backend

/* Current cached backends list (same as backends.list) but preserving order */
backendsList []*core.Backend

/* Stats */
StatsHandler *stats.Handler

Expand Down Expand Up @@ -225,24 +222,22 @@ func (this *Scheduler) HandleBackendLiveChange(target core.Target, live bool) {
func (this *Scheduler) HandleBackendsUpdate(backends []core.Backend) {

updated := map[core.Target]*core.Backend{}
updatedList := make([]*core.Backend, len(backends))

for i, b := range backends {
for _, b := range backends {
oldB, ok := this.backends[b.Target]

if ok {
// if we have this backend, update it's discovery properties
updatedB := oldB.MergeFrom(b)
updated[oldB.Target] = updatedB
updatedList[i] = updatedB
} else {
updated[b.Target] = &b
updatedList[i] = &b
continue
}

b := b // b has to be local variable in order to make unique pointers
updated[b.Target] = &b
}

this.backends = updated
this.backendsList = updatedList
}

/**
Expand All @@ -252,7 +247,7 @@ func (this *Scheduler) HandleBackendElect(req ElectRequest) {

// Filter only live backends
var backends []*core.Backend
for _, b := range this.backendsList {
for _, b := range this.backends {

if !b.Stats.Live {
continue
Expand Down

0 comments on commit 21a2279

Please sign in to comment.