|
22 | 22 | package roundrobin
|
23 | 23 |
|
24 | 24 | import (
|
25 |
| - rand "math/rand/v2" |
26 |
| - "sync/atomic" |
| 25 | + "encoding/json" |
| 26 | + "fmt" |
27 | 27 |
|
28 | 28 | "google.golang.org/grpc/balancer"
|
29 |
| - "google.golang.org/grpc/balancer/base" |
| 29 | + "google.golang.org/grpc/balancer/endpointsharding" |
| 30 | + "google.golang.org/grpc/balancer/pickfirst/pickfirstleaf" |
30 | 31 | "google.golang.org/grpc/grpclog"
|
| 32 | + internalgrpclog "google.golang.org/grpc/internal/grpclog" |
| 33 | + "google.golang.org/grpc/serviceconfig" |
31 | 34 | )
|
32 | 35 |
|
33 | 36 | // Name is the name of round_robin balancer.
|
34 | 37 | const Name = "round_robin"
|
35 | 38 |
|
36 |
| -var logger = grpclog.Component("roundrobin") |
37 |
| - |
38 |
| -// newBuilder creates a new roundrobin balancer builder. |
39 |
| -func newBuilder() balancer.Builder { |
40 |
| - return base.NewBalancerBuilder(Name, &rrPickerBuilder{}, base.Config{HealthCheck: true}) |
41 |
| -} |
| 39 | +var ( |
| 40 | + logger = grpclog.Component("roundrobin") |
| 41 | + // endpointSharding which specifies pick first children. |
| 42 | + endpointShardingLBConfig serviceconfig.LoadBalancingConfig |
| 43 | +) |
42 | 44 |
|
43 | 45 | func init() {
|
44 |
| - balancer.Register(newBuilder()) |
| 46 | + var err error |
| 47 | + endpointShardingLBConfig, err = endpointsharding.ParseConfig(json.RawMessage(endpointsharding.PickFirstConfig)) |
| 48 | + if err != nil { |
| 49 | + logger.Fatal(err) |
| 50 | + } |
| 51 | + balancer.Register(builder{}) |
45 | 52 | }
|
46 | 53 |
|
47 |
| -type rrPickerBuilder struct{} |
| 54 | +type builder struct{} |
48 | 55 |
|
49 |
| -func (*rrPickerBuilder) Build(info base.PickerBuildInfo) balancer.Picker { |
50 |
| - logger.Infof("roundrobinPicker: Build called with info: %v", info) |
51 |
| - if len(info.ReadySCs) == 0 { |
52 |
| - return base.NewErrPicker(balancer.ErrNoSubConnAvailable) |
53 |
| - } |
54 |
| - scs := make([]balancer.SubConn, 0, len(info.ReadySCs)) |
55 |
| - for sc := range info.ReadySCs { |
56 |
| - scs = append(scs, sc) |
| 56 | +func (bb builder) Name() string { |
| 57 | + return Name |
| 58 | +} |
| 59 | + |
| 60 | +func (bb builder) Build(cc balancer.ClientConn, opts balancer.BuildOptions) balancer.Balancer { |
| 61 | + bal := &rrBalancer{ |
| 62 | + cc: cc, |
| 63 | + child: endpointsharding.NewBalancer(cc, opts), |
57 | 64 | }
|
58 |
| - return &rrPicker{ |
59 |
| - subConns: scs, |
60 |
| - // Start at a random index, as the same RR balancer rebuilds a new |
61 |
| - // picker when SubConn states change, and we don't want to apply excess |
62 |
| - // load to the first server in the list. |
63 |
| - next: uint32(rand.IntN(len(scs))), |
| 65 | + bal.logger = internalgrpclog.NewPrefixLogger(logger, fmt.Sprintf("[%p] ", bal)) |
| 66 | + bal.logger.Infof("Created") |
| 67 | + return bal |
| 68 | +} |
| 69 | + |
| 70 | +type rrBalancer struct { |
| 71 | + cc balancer.ClientConn |
| 72 | + child balancer.Balancer |
| 73 | + logger *internalgrpclog.PrefixLogger |
| 74 | +} |
| 75 | + |
| 76 | +func (b *rrBalancer) Close() { |
| 77 | + b.child.Close() |
| 78 | +} |
| 79 | + |
| 80 | +func (b *rrBalancer) ExitIdle() { |
| 81 | + // Should always be ok, as child is endpoint sharding. |
| 82 | + if ei, ok := b.child.(balancer.ExitIdler); ok { |
| 83 | + ei.ExitIdle() |
64 | 84 | }
|
65 | 85 | }
|
66 | 86 |
|
67 |
| -type rrPicker struct { |
68 |
| - // subConns is the snapshot of the roundrobin balancer when this picker was |
69 |
| - // created. The slice is immutable. Each Get() will do a round robin |
70 |
| - // selection from it and return the selected SubConn. |
71 |
| - subConns []balancer.SubConn |
72 |
| - next uint32 |
| 87 | +func (b *rrBalancer) ResolverError(err error) { |
| 88 | + // Will cause inline picker update from endpoint sharding. |
| 89 | + b.child.ResolverError(err) |
73 | 90 | }
|
74 | 91 |
|
75 |
| -func (p *rrPicker) Pick(balancer.PickInfo) (balancer.PickResult, error) { |
76 |
| - subConnsLen := uint32(len(p.subConns)) |
77 |
| - nextIndex := atomic.AddUint32(&p.next, 1) |
| 92 | +func (b *rrBalancer) UpdateClientConnState(ccs balancer.ClientConnState) error { |
| 93 | + // Enable the health listener in pickfirst children for client side health |
| 94 | + // checks and outlier detection, if configured. |
| 95 | + ccs.ResolverState = pickfirstleaf.EnableHealthListener(ccs.ResolverState) |
| 96 | + ccs.BalancerConfig = endpointShardingLBConfig |
| 97 | + return b.child.UpdateClientConnState(ccs) |
| 98 | +} |
78 | 99 |
|
79 |
| - sc := p.subConns[nextIndex%subConnsLen] |
80 |
| - return balancer.PickResult{SubConn: sc}, nil |
| 100 | +func (b *rrBalancer) UpdateSubConnState(sc balancer.SubConn, state balancer.SubConnState) { |
| 101 | + b.logger.Errorf("UpdateSubConnState(%v, %+v) called unexpectedly", sc, state) |
81 | 102 | }
|
0 commit comments