Skip to content

Commit a509d72

Browse files
authored
Added round_robin balancer as an option to gRPC client settings (#1353)
* Added round_robin balancer as an option to gRPC client settings * Added documentation changes` * Changed the balancerName setting from bool to string to accomodate new balancers in future Setting invalid balancer is panicking. Hence validated the same & thrown an error * Fixed test * Fixed tests * Replaced grpc.WithBalancerName with grpc.WithDefaultServiceConfig * Validated the balancerName using a var string array instead of error control flow * Fixed lint errors * Fixed lint errors * typo fix in documentation
1 parent b964ca4 commit a509d72

File tree

11 files changed

+65
-3
lines changed

11 files changed

+65
-3
lines changed

config/configgrpc/configgrpc.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"time"
2323

2424
"google.golang.org/grpc"
25+
"google.golang.org/grpc/balancer/roundrobin"
2526
"google.golang.org/grpc/credentials"
2627
"google.golang.org/grpc/encoding/gzip"
2728
"google.golang.org/grpc/keepalive"
@@ -45,6 +46,9 @@ var (
4546
}
4647
)
4748

49+
// Allowed balancer names to be set in grpclb_policy to discover the servers
50+
var allowedBalancerNames = []string{roundrobin.Name, grpc.PickFirstBalancerName}
51+
4852
// KeepaliveClientConfig exposes the keepalive.ClientParameters to be used by the exporter.
4953
// Refer to the original data-structure for the meaning of each parameter:
5054
// https://godoc.org/google.golang.org/grpc/keepalive#ClientParameters
@@ -89,6 +93,10 @@ type GRPCClientSettings struct {
8993

9094
// PerRPCAuth parameter configures the client to send authentication data on a per-RPC basis.
9195
PerRPCAuth *PerRPCAuthConfig `mapstructure:"per_rpc_auth"`
96+
97+
// Sets the balancer in grpclb_policy to discover the servers. Default is pick_first
98+
// https://github.com/grpc/grpc-go/blob/master/examples/features/load_balancing/README.md
99+
BalancerName string `mapstructure:"balancer_name"`
92100
}
93101

94102
type KeepaliveServerConfig struct {
@@ -154,7 +162,6 @@ type GRPCServerSettings struct {
154162
// ToServerOption maps configgrpc.GRPCClientSettings to a slice of dial options for gRPC
155163
func (gcs *GRPCClientSettings) ToDialOptions() ([]grpc.DialOption, error) {
156164
opts := []grpc.DialOption{}
157-
158165
if gcs.Compression != "" {
159166
if compressionKey := GetGRPCCompressionKey(gcs.Compression); compressionKey != CompressionUnsupported {
160167
opts = append(opts, grpc.WithDefaultCallOptions(grpc.UseCompressor(compressionKey)))
@@ -200,9 +207,26 @@ func (gcs *GRPCClientSettings) ToDialOptions() ([]grpc.DialOption, error) {
200207
}
201208
}
202209

210+
if gcs.BalancerName != "" {
211+
valid := validateBalancerName(gcs.BalancerName)
212+
if !valid {
213+
return nil, fmt.Errorf("invalid balancer_name: %s", gcs.BalancerName)
214+
}
215+
opts = append(opts, grpc.WithDefaultServiceConfig(fmt.Sprintf(`{"loadBalancingPolicy":"%s"}`, gcs.BalancerName)))
216+
}
217+
203218
return opts, nil
204219
}
205220

221+
func validateBalancerName(balancerName string) bool {
222+
for _, item := range allowedBalancerNames {
223+
if item == balancerName {
224+
return true
225+
}
226+
}
227+
return false
228+
}
229+
206230
func (gss *GRPCServerSettings) ToListener() (net.Listener, error) {
207231
return gss.NetAddr.Listen()
208232
}

config/configgrpc/configgrpc_test.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,11 @@ func TestAllGrpcClientSettings(t *testing.T) {
6060
WriteBufferSize: 1024,
6161
WaitForReady: true,
6262
PerRPCAuth: nil,
63+
BalancerName: "round_robin",
6364
}
6465
opts, err := gcs.ToDialOptions()
6566
assert.NoError(t, err)
66-
assert.Len(t, opts, 5)
67+
assert.Len(t, opts, 6)
6768
}
6869

6970
func TestDefaultGrpcServerSettings(t *testing.T) {
@@ -143,10 +144,34 @@ func TestGRPCClientSettingsError(t *testing.T) {
143144
Keepalive: nil,
144145
},
145146
},
147+
{
148+
err: "invalid balancer_name: test",
149+
settings: GRPCClientSettings{
150+
Headers: map[string]string{
151+
"test": "test",
152+
},
153+
Endpoint: "localhost:1234",
154+
Compression: "gzip",
155+
TLSSetting: configtls.TLSClientSetting{
156+
Insecure: false,
157+
},
158+
Keepalive: &KeepaliveClientConfig{
159+
Time: time.Second,
160+
Timeout: time.Second,
161+
PermitWithoutStream: true,
162+
},
163+
ReadBufferSize: 1024,
164+
WriteBufferSize: 1024,
165+
WaitForReady: true,
166+
BalancerName: "test",
167+
},
168+
},
146169
}
147170
for _, test := range tests {
148171
t.Run(test.err, func(t *testing.T) {
149-
_, err := test.settings.ToDialOptions()
172+
opts, err := test.settings.ToDialOptions()
173+
assert.Nil(t, opts)
174+
assert.Error(t, err)
150175
assert.Regexp(t, test.err, err)
151176
})
152177
}

exporter/jaegerexporter/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ connection. See [grpc.WithInsecure()](https://godoc.org/google.golang.org/grpc#W
1818
[grpc.WithKeepaliveParams()](https://godoc.org/google.golang.org/grpc#WithKeepaliveParams).
1919
- `server_name_override`: If set to a non empty string, it will override the virtual host name
2020
of authority (e.g. :authority header field) in requests (typically used for testing).
21+
- `balancer_name`(default = pick_first): Sets the balancer in grpclb_policy to discover the servers.
22+
See [grpc loadbalancing example](https://github.com/grpc/grpc-go/blob/master/examples/features/load_balancing/README.md).
2123

2224
Example:
2325

exporter/jaegerexporter/config_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func TestLoadConfig(t *testing.T) {
5353
e1 := cfg.Exporters["jaeger/2"]
5454
assert.Equal(t, "jaeger/2", e1.(*Config).Name())
5555
assert.Equal(t, "a.new.target:1234", e1.(*Config).Endpoint)
56+
assert.Equal(t, "round_robin", e1.(*Config).GRPCClientSettings.BalancerName)
5657
params := component.ExporterCreateParams{Logger: zap.NewNop()}
5758
te, err := factory.CreateTraceExporter(context.Background(), params, e1)
5859
require.NoError(t, err)

exporter/jaegerexporter/testdata/config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ exporters:
1010
insecure: true
1111
jaeger/2:
1212
endpoint: "a.new.target:1234"
13+
balancer_name: "round_robin"
14+
1315

1416
service:
1517
pipelines:

exporter/opencensusexporter/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ The following settings can be optionally configured:
2525
Optional.
2626
- `reconnection_delay` (default = unset): time period between each reconnection
2727
performed by the exporter.
28+
- `balancer_name`(default = pick_first): Sets the balancer in grpclb_policy to discover the servers.
29+
See [grpc loadbalancing example](https://github.com/grpc/grpc-go/blob/master/examples/features/load_balancing/README.md).
2830

2931
Example:
3032

exporter/opencensusexporter/config_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ func TestLoadConfig(t *testing.T) {
6868
Timeout: 30,
6969
},
7070
WriteBufferSize: 512 * 1024,
71+
BalancerName: "round_robin",
7172
},
7273
NumWorkers: 123,
7374
ReconnectionDelay: 15,

exporter/opencensusexporter/testdata/config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ exporters:
1616
header1: 234
1717
another: "somevalue"
1818
reconnection_delay: 15
19+
balancer_name: "round_robin"
1920
keepalive:
2021
time: 20
2122
timeout: 30

exporter/otlpexporter/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ The following settings can be optionally configured:
2626
- `insecure`: whether to enable client transport security for the exporter's
2727
gRPC connection. See
2828
[grpc.WithInsecure()](https://godoc.org/google.golang.org/grpc#WithInsecure).
29+
- `balancer_name`(default = pick_first): Sets the balancer in grpclb_policy to discover the servers.
30+
See [grpc loadbalancing example](https://github.com/grpc/grpc-go/blob/master/examples/features/load_balancing/README.md).
2931

3032
Example:
3133

exporter/otlpexporter/config_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func TestLoadConfig(t *testing.T) {
7373
AuthType: "bearer",
7474
BearerToken: "some-token",
7575
},
76+
BalancerName: "round_robin",
7677
},
7778
})
7879
}

0 commit comments

Comments
 (0)