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

reslove WeightedICMPSelector #839

Merged
merged 6 commits into from
Jan 11, 2024
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
28 changes: 17 additions & 11 deletions client/ping_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,32 @@ import (
ping "github.com/go-ping/ping"
)

// weightedICMPSelector selects servers with ping result.
type weightedICMPSelector struct {
servers []*Weighted
wrs *weightedRoundRobinSelector
}

func newWeightedICMPSelector(servers map[string]string) Selector {
ss := createICMPWeighted(servers)
return &weightedICMPSelector{servers: ss}
wicmps := weightedICMPSelector{
servers: ss,
wrs: &weightedRoundRobinSelector{servers: ss},
}
wicmps.wrs.servers = ss
wicmps.wrs.buildRing()
return &wicmps
}

func (s weightedICMPSelector) Select(ctx context.Context, servicePath, serviceMethod string, args interface{}) string {
ss := s.servers
if len(ss) == 0 {
return ""
}
w := nextWeighted(ss)
if w == nil {
return ""
}
return w.Server
func (s *weightedICMPSelector) Select(ctx context.Context, servicePath, serviceMethod string, args interface{}) string {
return s.wrs.Select(ctx, servicePath, serviceMethod, args)
}

func (s *weightedICMPSelector) UpdateServer(servers map[string]string) {
ss := createICMPWeighted(servers)
s.wrs.servers = ss
s.servers = ss
s.wrs.buildRing()
}

func createICMPWeighted(servers map[string]string) []*Weighted {
Expand Down
5 changes: 0 additions & 5 deletions client/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,3 @@ func (s *consistentHashSelector) UpdateServer(servers map[string]string) {
}
s.servers = ss
}

// weightedICMPSelector selects servers with ping result.
type weightedICMPSelector struct {
servers []*Weighted
}
44 changes: 44 additions & 0 deletions client/selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,47 @@ func BenchmarkWeightedRoundRobinSelector_Select(b *testing.B) {
weightSelector.Select(ctx, "", "", nil)
}
}

//
//func TestWeightedICMPSelector(t *testing.T) {
// calc := make(map[string]int)
// servers := make(map[string]string)
// servers["@localhost:3333"] = ""
// servers["@www.baidu.com:3334"] = ""
// servers["@xxxx.xxxx:333"] = ""
// s := newWeightedICMPSelector(servers)
// ctx := context.Background()
// for i := 0; i < 10; i++ {
// host := s.Select(ctx, "", "", nil)
// if _, ok := calc[host]; ok {
// calc[host]++
// } else {
// calc[host] = 0
// }
// }
// if len(calc) != 2 {
// t.Errorf("expected %d but got %d", 2, len(servers))
// }
//}
//func TestWeightedICMPSelector_UpdateServer(t *testing.T) {
// calc := make(map[string]int)
// servers := make(map[string]string)
// servers["@localhost:3333"] = ""
// servers["@www.baidu.com:3334"] = ""
// servers["@xxxx.xxxx:333"] = ""
// s := newWeightedICMPSelector(servers)
// ctx := context.Background()
// servers["@www.sina.com:3333"] = ""
// s.UpdateServer(servers)
// for i := 0; i < 10; i++ {
// host := s.Select(ctx, "", "", nil)
// if _, ok := calc[host]; ok {
// calc[host]++
// } else {
// calc[host] = 0
// }
// }
// if len(calc) != 3 {
// t.Errorf("expected %d but got %d", 3, len(servers))
// }
//}
39 changes: 0 additions & 39 deletions client/smooth_weighted_round_robin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,3 @@ type Weighted struct {
CurrentWeight int
EffectiveWeight int
}

// func (w *Weighted) fail() {
// w.EffectiveWeight -= w.Weight
// if w.EffectiveWeight < 0 {
// w.EffectiveWeight = 0
// }
// }

// https://github.com/phusion/nginx/commit/27e94984486058d73157038f7950a0a36ecc6e35
func nextWeighted(servers []*Weighted) (best *Weighted) {
total := 0

for i := 0; i < len(servers); i++ {
w := servers[i]

if w == nil {
continue
}
// if w is down, continue

w.CurrentWeight += w.EffectiveWeight
total += w.EffectiveWeight
if w.EffectiveWeight < w.Weight {
w.EffectiveWeight++
}

if best == nil || w.CurrentWeight > best.CurrentWeight {
best = w
}

}

if best == nil {
return nil
}

best.CurrentWeight -= total
return best
}
Loading