Skip to content

Commit 0590b91

Browse files
arjan-baljanardhankrishna-sai
authored andcommitted
roundrobin: Delegate subchannel creation to pickfirst (grpc#7966)
1 parent 7f07821 commit 0590b91

File tree

10 files changed

+523
-353
lines changed

10 files changed

+523
-353
lines changed

balancer/roundrobin/roundrobin.go

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -22,60 +22,58 @@
2222
package roundrobin
2323

2424
import (
25-
rand "math/rand/v2"
26-
"sync/atomic"
25+
"fmt"
2726

2827
"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"
3030
"google.golang.org/grpc/grpclog"
31+
internalgrpclog "google.golang.org/grpc/internal/grpclog"
3132
)
3233

3334
// Name is the name of round_robin balancer.
3435
const Name = "round_robin"
3536

3637
var logger = grpclog.Component("roundrobin")
3738

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-
4339
func init() {
44-
balancer.Register(newBuilder())
40+
balancer.Register(builder{})
4541
}
4642

47-
type rrPickerBuilder struct{}
43+
type builder struct{}
4844

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{}),
6454
}
55+
bal.logger = internalgrpclog.NewPrefixLogger(logger, fmt.Sprintf("[%p] ", bal))
56+
bal.logger.Infof("Created")
57+
return bal
6558
}
6659

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
7364
}
7465

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+
}
7873

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+
}
8179
}

balancer/weightedroundrobin/balancer.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ func (b *wrrBalancer) Close() {
395395
ew.stopORCAListener()
396396
}
397397
}
398+
b.child.Close()
398399
}
399400

400401
func (b *wrrBalancer) ExitIdle() {

0 commit comments

Comments
 (0)