|
22 | 22 | package roundrobin |
23 | 23 |
|
24 | 24 | import ( |
25 | | - rand "math/rand/v2" |
26 | | - "sync/atomic" |
| 25 | + "fmt" |
27 | 26 |
|
28 | 27 | "google.golang.org/grpc/balancer" |
29 | | - "google.golang.org/grpc/balancer/base" |
| 28 | + "google.golang.org/grpc/balancer/endpointsharding" |
| 29 | + "google.golang.org/grpc/balancer/pickfirst/pickfirstleaf" |
30 | 30 | "google.golang.org/grpc/grpclog" |
| 31 | + internalgrpclog "google.golang.org/grpc/internal/grpclog" |
31 | 32 | ) |
32 | 33 |
|
33 | 34 | // Name is the name of round_robin balancer. |
34 | 35 | const Name = "round_robin" |
35 | 36 |
|
36 | 37 | var logger = grpclog.Component("roundrobin") |
37 | 38 |
|
38 | | -// newBuilder creates a new roundrobin balancer builder. |
39 | | -func newBuilder() balancer.Builder { |
40 | | - return base.NewBalancerBuilder(Name, &rrPickerBuilder{}, base.Config{HealthCheck: true}) |
41 | | -} |
42 | | - |
43 | 39 | func init() { |
44 | | - balancer.Register(newBuilder()) |
| 40 | + balancer.Register(builder{}) |
45 | 41 | } |
46 | 42 |
|
47 | | -type rrPickerBuilder struct{} |
| 43 | +type builder struct{} |
48 | 44 |
|
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) |
57 | | - } |
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))), |
| 45 | +func (bb builder) Name() string { |
| 46 | + return Name |
| 47 | +} |
| 48 | + |
| 49 | +func (bb builder) Build(cc balancer.ClientConn, opts balancer.BuildOptions) balancer.Balancer { |
| 50 | + childBuilder := balancer.Get(pickfirstleaf.Name).Build |
| 51 | + bal := &rrBalancer{ |
| 52 | + cc: cc, |
| 53 | + Balancer: endpointsharding.NewBalancer(cc, opts, childBuilder, endpointsharding.Options{}), |
64 | 54 | } |
| 55 | + bal.logger = internalgrpclog.NewPrefixLogger(logger, fmt.Sprintf("[%p] ", bal)) |
| 56 | + bal.logger.Infof("Created") |
| 57 | + return bal |
65 | 58 | } |
66 | 59 |
|
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 |
| 60 | +type rrBalancer struct { |
| 61 | + balancer.Balancer |
| 62 | + cc balancer.ClientConn |
| 63 | + logger *internalgrpclog.PrefixLogger |
73 | 64 | } |
74 | 65 |
|
75 | | -func (p *rrPicker) Pick(balancer.PickInfo) (balancer.PickResult, error) { |
76 | | - subConnsLen := uint32(len(p.subConns)) |
77 | | - nextIndex := atomic.AddUint32(&p.next, 1) |
| 66 | +func (b *rrBalancer) UpdateClientConnState(ccs balancer.ClientConnState) error { |
| 67 | + return b.Balancer.UpdateClientConnState(balancer.ClientConnState{ |
| 68 | + // Enable the health listener in pickfirst children for client side health |
| 69 | + // checks and outlier detection, if configured. |
| 70 | + ResolverState: pickfirstleaf.EnableHealthListener(ccs.ResolverState), |
| 71 | + }) |
| 72 | +} |
78 | 73 |
|
79 | | - sc := p.subConns[nextIndex%subConnsLen] |
80 | | - return balancer.PickResult{SubConn: sc}, nil |
| 74 | +func (b *rrBalancer) ExitIdle() { |
| 75 | + // Should always be ok, as child is endpoint sharding. |
| 76 | + if ei, ok := b.Balancer.(balancer.ExitIdler); ok { |
| 77 | + ei.ExitIdle() |
| 78 | + } |
81 | 79 | } |
0 commit comments