Skip to content

Commit 74cfecd

Browse files
committed
improvements to ParseConfig and its tests
1 parent ddc8d09 commit 74cfecd

File tree

2 files changed

+43
-54
lines changed

2 files changed

+43
-54
lines changed

balancer/randomsubsetting/randomsubsetting.go

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

19-
// Package randomsubsetting defines a random subsetting balancer.
19+
// Package randomsubsetting implements the random_subsetting LB policy specified
20+
// here: https://github.com/grpc/proposal/blob/master/A68-random-subsetting.md
2021
//
21-
// To install random subsetting balancer, import this package as:
22+
// To install the LB policy, import this package as:
2223
//
2324
// import _ "google.golang.org/grpc/balancer/randomsubsetting"
2425
package randomsubsetting
@@ -30,7 +31,7 @@ import (
3031
"slices"
3132
"time"
3233

33-
"github.com/cespare/xxhash/v2"
34+
xxhash "github.com/cespare/xxhash/v2"
3435
"google.golang.org/grpc/balancer"
3536
"google.golang.org/grpc/grpclog"
3637
"google.golang.org/grpc/internal/balancer/gracefulswitch"
@@ -40,14 +41,10 @@ import (
4041
"google.golang.org/grpc/serviceconfig"
4142
)
4243

43-
const (
44-
// Name is the name of the random subsetting load balancer.
45-
Name = "random_subsetting"
46-
)
44+
// Name is the name of the random subsetting load balancer.
45+
const Name = "random_subsetting"
4746

48-
var (
49-
logger = grpclog.Component(Name)
50-
)
47+
var logger = grpclog.Component(Name)
5148

5249
func prefixLogger(p *subsettingBalancer) *internalgrpclog.PrefixLogger {
5350
return internalgrpclog.NewPrefixLogger(logger, fmt.Sprintf("[random-subsetting-lb %p] ", p))
@@ -64,45 +61,31 @@ func (bb) Build(cc balancer.ClientConn, bOpts balancer.BuildOptions) balancer.Ba
6461
Balancer: gracefulswitch.NewBalancer(cc, bOpts),
6562
hashf: xxhash.NewWithSeed(uint64(time.Now().UnixNano())),
6663
}
67-
// Create a logger with a prefix specific to this balancer instance.
6864
b.logger = prefixLogger(b)
69-
7065
b.logger.Infof("Created")
7166
return b
7267
}
7368

74-
// LBConfig is the config for the random subsetting balancer.
75-
type LBConfig struct {
69+
type lbConfig struct {
7670
serviceconfig.LoadBalancingConfig `json:"-"`
7771

7872
SubsetSize uint64 `json:"subsetSize,omitempty"`
7973
ChildPolicy *iserviceconfig.BalancerConfig `json:"childPolicy,omitempty"`
8074
}
8175

8276
func (bb) ParseConfig(s json.RawMessage) (serviceconfig.LoadBalancingConfig, error) {
83-
lbCfg := &LBConfig{
84-
SubsetSize: 2, // default value
85-
ChildPolicy: &iserviceconfig.BalancerConfig{Name: "round_robin"},
86-
}
77+
lbCfg := &lbConfig{}
8778

88-
if err := json.Unmarshal(s, lbCfg); err != nil { // Validates child config if present as well.
89-
return nil, fmt.Errorf("randomsubsetting: unable to unmarshal LBConfig: %s, error: %v", string(s), err)
79+
// Ensure that the specified child policy is registered and validates its
80+
// config, if present.
81+
if err := json.Unmarshal(s, lbCfg); err != nil {
82+
return nil, fmt.Errorf("randomsubsetting: unmarshaling configuration: %s, failed: %v", string(s), err)
9083
}
91-
92-
// if someone needs SubsetSize == 1, he should use pick_first instead
93-
if lbCfg.SubsetSize < 2 {
94-
return nil, fmt.Errorf("randomsubsetting: SubsetSize must be >= 2")
84+
if lbCfg.SubsetSize == 0 {
85+
return nil, fmt.Errorf("randomsubsetting: SubsetSize must be greater than 0")
9586
}
96-
9787
if lbCfg.ChildPolicy == nil {
98-
return nil, fmt.Errorf("randomsubsetting: child policy field must be set")
99-
}
100-
101-
// Reject whole config if child policy doesn't exist, don't persist it for
102-
// later.
103-
bb := balancer.Get(lbCfg.ChildPolicy.Name)
104-
if bb == nil {
105-
return nil, fmt.Errorf("randomsubsetting: child balancer %q not registered", lbCfg.ChildPolicy.Name)
88+
return nil, fmt.Errorf("randomsubsetting: ChildPolicy must be specified")
10689
}
10790

10891
return lbCfg, nil
@@ -116,12 +99,12 @@ type subsettingBalancer struct {
11699
*gracefulswitch.Balancer
117100

118101
logger *internalgrpclog.PrefixLogger
119-
cfg *LBConfig
102+
cfg *lbConfig
120103
hashf *xxhash.Digest
121104
}
122105

123106
func (b *subsettingBalancer) UpdateClientConnState(s balancer.ClientConnState) error {
124-
lbCfg, ok := s.BalancerConfig.(*LBConfig)
107+
lbCfg, ok := s.BalancerConfig.(*lbConfig)
125108
if !ok {
126109
b.logger.Errorf("Received config with unexpected type %T: %v", s.BalancerConfig, s.BalancerConfig)
127110
return balancer.ErrBadResolverState

balancer/randomsubsetting/randomsubsetting_test.go

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,31 +63,37 @@ func (s) TestParseConfig(t *testing.T) {
6363
wantErr string
6464
}{
6565
{
66-
name: "happy-case-default",
67-
input: `{}`,
68-
wantCfg: &LBConfig{
69-
SubsetSize: 2,
70-
ChildPolicy: &iserviceconfig.BalancerConfig{Name: "round_robin"},
71-
},
66+
name: "invalid-json",
67+
input: "{{invalidjson{{",
68+
wantErr: "invalid character",
7269
},
7370
{
74-
name: "happy-case-subset_size-set",
75-
input: `{ "subsetSize": 3 }`,
76-
wantCfg: &LBConfig{
77-
SubsetSize: 3,
78-
ChildPolicy: &iserviceconfig.BalancerConfig{Name: "round_robin"},
79-
},
71+
name: "empty_config",
72+
input: `{}`,
73+
wantErr: "SubsetSize must be greater than 0",
8074
},
8175
{
82-
name: "subset_size-less-than-2",
83-
input: `{ "subsetSize": 1,
84-
"childPolicy": [{"round_robin": {}}]}`,
85-
wantErr: "randomsubsetting: SubsetSize must be >= 2",
76+
name: "subset_size_zero",
77+
input: `{ "subsetSize": 0 }`,
78+
wantErr: "SubsetSize must be greater than 0",
8679
},
8780
{
88-
name: "invalid-json",
89-
input: "{{invalidjson{{",
90-
wantErr: "invalid character",
81+
name: "child_policy_missing",
82+
input: `{ "subsetSize": 1 }`,
83+
wantErr: "ChildPolicy must be specified",
84+
},
85+
{
86+
name: "child_policy_not_registered",
87+
input: `{ "subsetSize": 1 , "childPolicy": [{"unregistered_lb": {}}] }`,
88+
wantErr: "no supported policies found",
89+
},
90+
{
91+
name: "success",
92+
input: `{ "subsetSize": 3, "childPolicy": [{"round_robin": {}}]}`,
93+
wantCfg: &lbConfig{
94+
SubsetSize: 3,
95+
ChildPolicy: &iserviceconfig.BalancerConfig{Name: "round_robin"},
96+
},
9197
},
9298
}
9399
for _, test := range tests {

0 commit comments

Comments
 (0)