Skip to content

Commit 1d6062f

Browse files
arjan-baljanardhankrishna-sai
authored andcommitted
endpointsharding: Export parsed pickfirst config instead of json string (grpc#8007)
1 parent fc69301 commit 1d6062f

File tree

4 files changed

+18
-27
lines changed

4 files changed

+18
-27
lines changed

balancer/endpointsharding/endpointsharding.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,24 @@ import (
3737
"google.golang.org/grpc/balancer/base"
3838
"google.golang.org/grpc/balancer/pickfirst/pickfirstleaf"
3939
"google.golang.org/grpc/connectivity"
40+
"google.golang.org/grpc/grpclog"
4041
"google.golang.org/grpc/internal/balancer/gracefulswitch"
4142
"google.golang.org/grpc/resolver"
4243
"google.golang.org/grpc/serviceconfig"
4344
)
4445

45-
// PickFirstConfig is a pick first config without shuffling enabled.
46-
var PickFirstConfig string
46+
var (
47+
// PickFirstConfig is a pick first config without shuffling enabled.
48+
PickFirstConfig serviceconfig.LoadBalancingConfig
49+
logger = grpclog.Component("endpoint-sharding")
50+
)
4751

4852
func init() {
49-
PickFirstConfig = fmt.Sprintf("[{%q: {}}]", pickfirstleaf.Name)
53+
var err error
54+
PickFirstConfig, err = ParseConfig(json.RawMessage(fmt.Sprintf("[{%q: {}}]", pickfirstleaf.Name)))
55+
if err != nil {
56+
logger.Fatal(err)
57+
}
5058
}
5159

5260
// ChildState is the balancer state of a child along with the endpoint which

balancer/endpointsharding/endpointsharding_test.go

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

19-
package endpointsharding
19+
package endpointsharding_test
2020

2121
import (
2222
"context"
@@ -28,6 +28,7 @@ import (
2828

2929
"google.golang.org/grpc"
3030
"google.golang.org/grpc/balancer"
31+
"google.golang.org/grpc/balancer/endpointsharding"
3132
"google.golang.org/grpc/credentials/insecure"
3233
"google.golang.org/grpc/grpclog"
3334
"google.golang.org/grpc/internal"
@@ -49,16 +50,11 @@ func Test(t *testing.T) {
4950
grpctest.RunSubTests(t, s{})
5051
}
5152

52-
var gracefulSwitchPickFirst serviceconfig.LoadBalancingConfig
53+
var gracefulSwitchPickFirst = endpointsharding.PickFirstConfig
5354

5455
var logger = grpclog.Component("endpoint-sharding-test")
5556

5657
func init() {
57-
var err error
58-
gracefulSwitchPickFirst, err = ParseConfig(json.RawMessage(PickFirstConfig))
59-
if err != nil {
60-
logger.Fatal(err)
61-
}
6258
balancer.Register(fakePetioleBuilder{})
6359
}
6460

@@ -75,7 +71,7 @@ func (fakePetioleBuilder) Build(cc balancer.ClientConn, opts balancer.BuildOptio
7571
ClientConn: cc,
7672
bOpts: opts,
7773
}
78-
fp.Balancer = NewBalancer(fp, opts)
74+
fp.Balancer = endpointsharding.NewBalancer(fp, opts)
7975
return fp
8076
}
8177

@@ -105,7 +101,7 @@ func (fp *fakePetiole) UpdateClientConnState(state balancer.ClientConnState) err
105101
}
106102

107103
func (fp *fakePetiole) UpdateState(state balancer.State) {
108-
childStates := ChildStatesFromPicker(state.Picker)
104+
childStates := endpointsharding.ChildStatesFromPicker(state.Picker)
109105
// Both child states should be present in the child picker. States and
110106
// picker change over the lifecycle of test, but there should always be two.
111107
if len(childStates) != 2 {

balancer/weightedroundrobin/balancer.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,10 @@ var (
8585
)
8686

8787
// endpointSharding which specifies pick first children.
88-
var endpointShardingLBConfig serviceconfig.LoadBalancingConfig
88+
var endpointShardingLBConfig = endpointsharding.PickFirstConfig
8989

9090
func init() {
9191
balancer.Register(bb{})
92-
var err error
93-
endpointShardingLBConfig, err = endpointsharding.ParseConfig(json.RawMessage(endpointsharding.PickFirstConfig))
94-
if err != nil {
95-
logger.Fatal(err)
96-
}
9792
}
9893

9994
type bb struct{}

examples/features/customloadbalancer/client/customroundrobin/customroundrobin.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,13 @@ import (
2828
"google.golang.org/grpc/balancer"
2929
"google.golang.org/grpc/balancer/endpointsharding"
3030
"google.golang.org/grpc/connectivity"
31-
"google.golang.org/grpc/grpclog"
3231
"google.golang.org/grpc/serviceconfig"
3332
)
3433

35-
var gracefulSwitchPickFirst serviceconfig.LoadBalancingConfig
34+
var gracefulSwitchPickFirst = endpointsharding.PickFirstConfig
3635

3736
func init() {
3837
balancer.Register(customRoundRobinBuilder{})
39-
var err error
40-
gracefulSwitchPickFirst, err = endpointsharding.ParseConfig(json.RawMessage(endpointsharding.PickFirstConfig))
41-
if err != nil {
42-
logger.Fatal(err)
43-
}
4438
}
4539

4640
const customRRName = "custom_round_robin"
@@ -79,8 +73,6 @@ func (customRoundRobinBuilder) Build(cc balancer.ClientConn, bOpts balancer.Buil
7973
return crr
8074
}
8175

82-
var logger = grpclog.Component("example")
83-
8476
type customRoundRobin struct {
8577
// All state and operations on this balancer are either initialized at build
8678
// time and read only after, or are only accessed as part of its

0 commit comments

Comments
 (0)