From d47d3c9cfe9a2f34959b12639e0c6debff8bcf7e Mon Sep 17 00:00:00 2001 From: yanshushuang Date: Thu, 11 Jan 2024 11:02:53 +0800 Subject: [PATCH 1/6] fix WeightedICMPSelector --- client/ping_utils.go | 19 ++++++++----------- client/selector.go | 1 + 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/client/ping_utils.go b/client/ping_utils.go index 1e93512e..5aa198da 100644 --- a/client/ping_utils.go +++ b/client/ping_utils.go @@ -11,24 +11,21 @@ import ( func newWeightedICMPSelector(servers map[string]string) Selector { ss := createICMPWeighted(servers) - return &weightedICMPSelector{servers: ss} + wicmps := &weightedICMPSelector{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 { diff --git a/client/selector.go b/client/selector.go index 0cefb8c3..00ecfc5f 100644 --- a/client/selector.go +++ b/client/selector.go @@ -322,4 +322,5 @@ func (s *consistentHashSelector) UpdateServer(servers map[string]string) { // weightedICMPSelector selects servers with ping result. type weightedICMPSelector struct { servers []*Weighted + wrs weightedRoundRobinSelector } From aa6615709478e0602ae9bb43078f1e8ed1f9268d Mon Sep 17 00:00:00 2001 From: yanshushuang Date: Thu, 11 Jan 2024 11:08:06 +0800 Subject: [PATCH 2/6] fix WeightedICMPSelector --- client/ping_utils.go | 10 ++++++++-- client/selector.go | 6 ------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/client/ping_utils.go b/client/ping_utils.go index 5aa198da..8b510b68 100644 --- a/client/ping_utils.go +++ b/client/ping_utils.go @@ -9,12 +9,18 @@ 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) - wicmps := &weightedICMPSelector{servers: ss} + wicmps := weightedICMPSelector{servers: ss} wicmps.wrs.servers = ss wicmps.wrs.buildRing() - return wicmps + return &wicmps } func (s *weightedICMPSelector) Select(ctx context.Context, servicePath, serviceMethod string, args interface{}) string { diff --git a/client/selector.go b/client/selector.go index 00ecfc5f..3b665554 100644 --- a/client/selector.go +++ b/client/selector.go @@ -318,9 +318,3 @@ func (s *consistentHashSelector) UpdateServer(servers map[string]string) { } s.servers = ss } - -// weightedICMPSelector selects servers with ping result. -type weightedICMPSelector struct { - servers []*Weighted - wrs weightedRoundRobinSelector -} From f0e86a540e17cdab5ddd4450472e598aea1fa6be Mon Sep 17 00:00:00 2001 From: yanshushuang Date: Thu, 11 Jan 2024 11:08:16 +0800 Subject: [PATCH 3/6] clean code --- client/smooth_weighted_round_robin.go | 39 --------------------------- 1 file changed, 39 deletions(-) diff --git a/client/smooth_weighted_round_robin.go b/client/smooth_weighted_round_robin.go index fa36b3a0..5bd59478 100644 --- a/client/smooth_weighted_round_robin.go +++ b/client/smooth_weighted_round_robin.go @@ -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 -} From 59e7d7518354f712e356f76554ea8295909dde35 Mon Sep 17 00:00:00 2001 From: yanshushuang Date: Thu, 11 Jan 2024 11:11:19 +0800 Subject: [PATCH 4/6] fix WeightedICMPSelector --- client/ping_utils.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/client/ping_utils.go b/client/ping_utils.go index 8b510b68..9f3c6a95 100644 --- a/client/ping_utils.go +++ b/client/ping_utils.go @@ -12,12 +12,15 @@ import ( // weightedICMPSelector selects servers with ping result. type weightedICMPSelector struct { servers []*Weighted - wrs weightedRoundRobinSelector + wrs *weightedRoundRobinSelector } func newWeightedICMPSelector(servers map[string]string) Selector { ss := createICMPWeighted(servers) - wicmps := weightedICMPSelector{servers: ss} + wicmps := weightedICMPSelector{ + servers: ss, + wrs: &weightedRoundRobinSelector{servers: ss}, + } wicmps.wrs.servers = ss wicmps.wrs.buildRing() return &wicmps From 8e8c28b2125b74cca31cc4670b51e11528425e7a Mon Sep 17 00:00:00 2001 From: yanshushuang Date: Thu, 11 Jan 2024 11:40:08 +0800 Subject: [PATCH 5/6] add test --- client/selector_test.go | 43 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/client/selector_test.go b/client/selector_test.go index 3a961caa..f4e077ab 100644 --- a/client/selector_test.go +++ b/client/selector_test.go @@ -105,3 +105,46 @@ 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)) + } +} From 81531dfec1f68e5b1023f2c667449a4377dd3090 Mon Sep 17 00:00:00 2001 From: yanshushuang Date: Thu, 11 Jan 2024 11:50:13 +0800 Subject: [PATCH 6/6] del test --- client/selector_test.go | 85 +++++++++++++++++++++-------------------- 1 file changed, 43 insertions(+), 42 deletions(-) diff --git a/client/selector_test.go b/client/selector_test.go index f4e077ab..a004bafb 100644 --- a/client/selector_test.go +++ b/client/selector_test.go @@ -106,45 +106,46 @@ func BenchmarkWeightedRoundRobinSelector_Select(b *testing.B) { } } -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)) - } -} +// +//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)) +// } +//}