Skip to content

Commit 75f3810

Browse files
committed
Move Pick First Balancer to seperate file
1 parent a639c40 commit 75f3810

File tree

13 files changed

+46
-32
lines changed

13 files changed

+46
-32
lines changed

balancer/grpclb/grpclb_config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ package grpclb
2121
import (
2222
"encoding/json"
2323

24-
"google.golang.org/grpc"
24+
"google.golang.org/grpc/balancer/pickfirst"
2525
"google.golang.org/grpc/balancer/roundrobin"
2626
"google.golang.org/grpc/serviceconfig"
2727
)
2828

2929
const (
3030
roundRobinName = roundrobin.Name
31-
pickFirstName = grpc.PickFirstBalancerName
31+
pickFirstName = pickfirst.Name
3232
)
3333

3434
type grpclbServiceConfig struct {

pickfirst.go renamed to balancer/pickfirst/pickfirst.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
*
1717
*/
1818

19-
package grpc
19+
// Package pickfirst contains the pick_first load balancing policy.
20+
package pickfirst
2021

2122
import (
2223
"encoding/json"
@@ -25,17 +26,24 @@ import (
2526

2627
"google.golang.org/grpc/balancer"
2728
"google.golang.org/grpc/connectivity"
29+
"google.golang.org/grpc/grpclog"
2830
internalgrpclog "google.golang.org/grpc/internal/grpclog"
2931
"google.golang.org/grpc/internal/grpcrand"
3032
"google.golang.org/grpc/internal/pretty"
3133
"google.golang.org/grpc/resolver"
3234
"google.golang.org/grpc/serviceconfig"
3335
)
3436

37+
func init() {
38+
balancer.Register(pickfirstBuilder{})
39+
}
40+
41+
var logger = grpclog.Component("pick-first-lb")
42+
3543
const (
36-
// PickFirstBalancerName is the name of the pick_first balancer.
37-
PickFirstBalancerName = "pick_first"
38-
logPrefix = "[pick-first-lb %p] "
44+
// Name is the name of the pick_first balancer.
45+
Name = "pick_first"
46+
logPrefix = "[pick-first-lb %p] "
3947
)
4048

4149
type pickfirstBuilder struct{}
@@ -47,7 +55,7 @@ func (pickfirstBuilder) Build(cc balancer.ClientConn, opt balancer.BuildOptions)
4755
}
4856

4957
func (pickfirstBuilder) Name() string {
50-
return PickFirstBalancerName
58+
return Name
5159
}
5260

5361
type pfConfig struct {

balancer/rls/balancer_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ import (
3030
"github.com/google/go-cmp/cmp"
3131
"google.golang.org/grpc"
3232
"google.golang.org/grpc/balancer"
33+
"google.golang.org/grpc/balancer/pickfirst"
3334
"google.golang.org/grpc/balancer/rls/internal/test/e2e"
3435
"google.golang.org/grpc/codes"
3536
"google.golang.org/grpc/connectivity"
3637
"google.golang.org/grpc/credentials"
3738
"google.golang.org/grpc/credentials/insecure"
3839
"google.golang.org/grpc/internal"
3940
"google.golang.org/grpc/internal/balancer/stub"
40-
rlspb "google.golang.org/grpc/internal/proto/grpc_lookup_v1"
4141
internalserviceconfig "google.golang.org/grpc/internal/serviceconfig"
4242
"google.golang.org/grpc/internal/testutils"
4343
rlstest "google.golang.org/grpc/internal/testutils/rls"
@@ -46,6 +46,8 @@ import (
4646
"google.golang.org/grpc/resolver/manual"
4747
"google.golang.org/grpc/serviceconfig"
4848
"google.golang.org/grpc/testdata"
49+
50+
rlspb "google.golang.org/grpc/internal/proto/grpc_lookup_v1"
4951
"google.golang.org/protobuf/types/known/durationpb"
5052
)
5153

@@ -919,7 +921,7 @@ func (s) TestUpdateStatePauses(t *testing.T) {
919921
}
920922
stub.Register(childPolicyName, stub.BalancerFuncs{
921923
Init: func(bd *stub.BalancerData) {
922-
bd.Data = balancer.Get(grpc.PickFirstBalancerName).Build(bd.ClientConn, bd.BuildOptions)
924+
bd.Data = balancer.Get(pickfirst.Name).Build(bd.ClientConn, bd.BuildOptions)
923925
},
924926
ParseConfig: func(sc json.RawMessage) (serviceconfig.LoadBalancingConfig, error) {
925927
cfg := &childPolicyConfig{}

balancer/rls/internal/test/e2e/rls_child_policy.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ import (
2323
"errors"
2424
"fmt"
2525

26-
"google.golang.org/grpc"
2726
"google.golang.org/grpc/balancer"
27+
"google.golang.org/grpc/balancer/pickfirst"
2828
"google.golang.org/grpc/internal/grpcsync"
2929
"google.golang.org/grpc/resolver"
3030
"google.golang.org/grpc/serviceconfig"
@@ -68,7 +68,7 @@ type bb struct {
6868
func (bb bb) Name() string { return bb.name }
6969

7070
func (bb bb) Build(cc balancer.ClientConn, opts balancer.BuildOptions) balancer.Balancer {
71-
pf := balancer.Get(grpc.PickFirstBalancerName)
71+
pf := balancer.Get(pickfirst.Name)
7272
b := &bal{
7373
Balancer: pf.Build(cc, opts),
7474
bf: bb.bf,

clientconn.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,6 @@ func (cc *ClientConn) waitForResolvedAddrs(ctx context.Context) error {
693693
var emptyServiceConfig *ServiceConfig
694694

695695
func init() {
696-
balancer.Register(pickfirstBuilder{})
697696
cfg := parseServiceConfig("{}")
698697
if cfg.Err != nil {
699698
panic(fmt.Sprintf("impossible error parsing empty service config: %v", cfg.Err))

internal/balancergroup/balancergroup_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ import (
2323
"testing"
2424
"time"
2525

26-
"google.golang.org/grpc"
2726
"google.golang.org/grpc/balancer"
27+
"google.golang.org/grpc/balancer/pickfirst"
2828
"google.golang.org/grpc/balancer/roundrobin"
2929
"google.golang.org/grpc/balancer/weightedtarget/weightedaggregator"
3030
"google.golang.org/grpc/connectivity"
@@ -602,7 +602,7 @@ func (s) TestBalancerGracefulSwitch(t *testing.T) {
602602
childPolicyName := t.Name()
603603
stub.Register(childPolicyName, stub.BalancerFuncs{
604604
Init: func(bd *stub.BalancerData) {
605-
bd.Data = balancer.Get(grpc.PickFirstBalancerName).Build(bd.ClientConn, bd.BuildOptions)
605+
bd.Data = balancer.Get(pickfirst.Name).Build(bd.ClientConn, bd.BuildOptions)
606606
},
607607
UpdateClientConnState: func(bd *stub.BalancerData, ccs balancer.ClientConnState) error {
608608
ccs.ResolverState.Addresses = ccs.ResolverState.Addresses[1:]

service_config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"time"
2727

2828
"google.golang.org/grpc/balancer"
29+
"google.golang.org/grpc/balancer/pickfirst"
2930
"google.golang.org/grpc/codes"
3031
"google.golang.org/grpc/internal"
3132
"google.golang.org/grpc/internal/balancer/gracefulswitch"
@@ -183,12 +184,12 @@ func parseServiceConfig(js string) *serviceconfig.ParseResult {
183184
}
184185
c := rsc.LoadBalancingConfig
185186
if c == nil {
186-
name := PickFirstBalancerName
187+
name := pickfirst.Name
187188
if rsc.LoadBalancingPolicy != nil {
188189
name = *rsc.LoadBalancingPolicy
189190
}
190191
if balancer.Get(name) == nil {
191-
name = PickFirstBalancerName
192+
name = pickfirst.Name
192193
}
193194
cfg := []map[string]any{{name: struct{}{}}}
194195
strCfg, err := json.Marshal(cfg)

test/balancer_switching_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ import (
2626
"google.golang.org/grpc"
2727
"google.golang.org/grpc/balancer"
2828
grpclbstate "google.golang.org/grpc/balancer/grpclb/state"
29+
pickfirst "google.golang.org/grpc/balancer/pickfirst"
2930
"google.golang.org/grpc/credentials/insecure"
3031
"google.golang.org/grpc/internal"
3132
"google.golang.org/grpc/internal/balancer/stub"
3233
"google.golang.org/grpc/internal/stubserver"
3334
"google.golang.org/grpc/internal/testutils/fakegrpclb"
34-
"google.golang.org/grpc/internal/testutils/pickfirst"
35+
pfutil "google.golang.org/grpc/internal/testutils/pickfirst"
3536
rrutil "google.golang.org/grpc/internal/testutils/roundrobin"
3637
"google.golang.org/grpc/resolver"
3738
"google.golang.org/grpc/resolver/manual"
@@ -127,7 +128,7 @@ func (s) TestBalancerSwitch_Basic(t *testing.T) {
127128
r.UpdateState(resolver.State{Addresses: addrs})
128129
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
129130
defer cancel()
130-
if err := pickfirst.CheckRPCsToBackend(ctx, cc, addrs[0]); err != nil {
131+
if err := pfutil.CheckRPCsToBackend(ctx, cc, addrs[0]); err != nil {
131132
t.Fatal(err)
132133
}
133134

@@ -146,7 +147,7 @@ func (s) TestBalancerSwitch_Basic(t *testing.T) {
146147
Addresses: addrs,
147148
ServiceConfig: parseServiceConfig(t, r, pickFirstServiceConfig),
148149
})
149-
if err := pickfirst.CheckRPCsToBackend(ctx, cc, addrs[0]); err != nil {
150+
if err := pfutil.CheckRPCsToBackend(ctx, cc, addrs[0]); err != nil {
150151
t.Fatal(err)
151152
}
152153
}
@@ -195,7 +196,7 @@ func (s) TestBalancerSwitch_grpclbToPickFirst(t *testing.T) {
195196
// newly configured backends, as part of the balancer switch.
196197
emptyConfig := parseServiceConfig(t, r, `{}`)
197198
r.UpdateState(resolver.State{Addresses: addrs[1:], ServiceConfig: emptyConfig})
198-
if err := pickfirst.CheckRPCsToBackend(ctx, cc, addrs[1]); err != nil {
199+
if err := pfutil.CheckRPCsToBackend(ctx, cc, addrs[1]); err != nil {
199200
t.Fatal(err)
200201
}
201202
}
@@ -220,7 +221,7 @@ func (s) TestBalancerSwitch_pickFirstToGRPCLB(t *testing.T) {
220221
r.UpdateState(resolver.State{Addresses: addrs[1:]})
221222
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
222223
defer cancel()
223-
if err := pickfirst.CheckRPCsToBackend(ctx, cc, addrs[1]); err != nil {
224+
if err := pfutil.CheckRPCsToBackend(ctx, cc, addrs[1]); err != nil {
224225
t.Fatal(err)
225226
}
226227

@@ -245,7 +246,7 @@ func (s) TestBalancerSwitch_pickFirstToGRPCLB(t *testing.T) {
245246
// Switch to "pick_first" again by sending no grpclb server addresses.
246247
emptyConfig := parseServiceConfig(t, r, `{}`)
247248
r.UpdateState(resolver.State{Addresses: addrs[1:], ServiceConfig: emptyConfig})
248-
if err := pickfirst.CheckRPCsToBackend(ctx, cc, addrs[1]); err != nil {
249+
if err := pfutil.CheckRPCsToBackend(ctx, cc, addrs[1]); err != nil {
249250
t.Fatal(err)
250251
}
251252
}
@@ -340,7 +341,7 @@ func (s) TestBalancerSwitch_grpclbNotRegistered(t *testing.T) {
340341
r.UpdateState(grpclbstate.Set(state, &grpclbstate.State{BalancerAddresses: grpclbAddr}))
341342
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
342343
defer cancel()
343-
if err := pickfirst.CheckRPCsToBackend(ctx, cc, addrs[0]); err != nil {
344+
if err := pfutil.CheckRPCsToBackend(ctx, cc, addrs[0]); err != nil {
344345
t.Fatal(err)
345346
}
346347

@@ -468,7 +469,7 @@ func (s) TestBalancerSwitch_Graceful(t *testing.T) {
468469
waitToProceed := make(chan struct{})
469470
stub.Register(t.Name(), stub.BalancerFuncs{
470471
Init: func(bd *stub.BalancerData) {
471-
pf := balancer.Get(grpc.PickFirstBalancerName)
472+
pf := balancer.Get(pickfirst.Name)
472473
bd.Data = pf.Build(bd.ClientConn, bd.BuildOptions)
473474
},
474475
UpdateClientConnState: func(bd *stub.BalancerData, ccs balancer.ClientConnState) error {
@@ -503,7 +504,7 @@ func (s) TestBalancerSwitch_Graceful(t *testing.T) {
503504
// underlying "pick_first" balancer which will result in a healthy picker
504505
// being reported to the channel. RPCs should start using the new balancer.
505506
close(waitToProceed)
506-
if err := pickfirst.CheckRPCsToBackend(ctx, cc, addrs[0]); err != nil {
507+
if err := pfutil.CheckRPCsToBackend(ctx, cc, addrs[0]); err != nil {
507508
t.Fatal(err)
508509
}
509510
}

test/balancer_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"google.golang.org/grpc"
3333
"google.golang.org/grpc/attributes"
3434
"google.golang.org/grpc/balancer"
35+
"google.golang.org/grpc/balancer/pickfirst"
3536
"google.golang.org/grpc/codes"
3637
"google.golang.org/grpc/connectivity"
3738
"google.golang.org/grpc/credentials"
@@ -847,7 +848,7 @@ func (s) TestMetadataInPickResult(t *testing.T) {
847848
stub.Register(t.Name(), stub.BalancerFuncs{
848849
Init: func(bd *stub.BalancerData) {
849850
cc := &testCCWrapper{ClientConn: bd.ClientConn}
850-
bd.Data = balancer.Get(grpc.PickFirstBalancerName).Build(cc, bd.BuildOptions)
851+
bd.Data = balancer.Get(pickfirst.Name).Build(cc, bd.BuildOptions)
851852
},
852853
UpdateClientConnState: func(bd *stub.BalancerData, ccs balancer.ClientConnState) error {
853854
bal := bd.Data.(balancer.Balancer)

test/resolver_update_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/google/go-cmp/cmp"
3030
"google.golang.org/grpc"
3131
"google.golang.org/grpc/balancer"
32+
"google.golang.org/grpc/balancer/pickfirst"
3233
"google.golang.org/grpc/codes"
3334
"google.golang.org/grpc/credentials/insecure"
3435
"google.golang.org/grpc/internal"
@@ -158,7 +159,7 @@ func (s) TestResolverUpdate_InvalidServiceConfigAfterGoodUpdate(t *testing.T) {
158159
ccUpdateCh := testutils.NewChannel()
159160
stub.Register(t.Name(), stub.BalancerFuncs{
160161
Init: func(bd *stub.BalancerData) {
161-
pf := balancer.Get(grpc.PickFirstBalancerName)
162+
pf := balancer.Get(pickfirst.Name)
162163
bd.Data = pf.Build(bd.ClientConn, bd.BuildOptions)
163164
},
164165
ParseConfig: func(lbCfg json.RawMessage) (serviceconfig.LoadBalancingConfig, error) {

0 commit comments

Comments
 (0)