blob: 686ad7ba616024ed554684dc57a8df0807cb26f5 [file] [log] [blame]
Scott Bakereee8dd82019-09-24 12:52:34 -07001/*
2 *
3 * Copyright 2017 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19package grpc
20
21import (
22 "encoding/json"
23 "fmt"
24 "strconv"
25 "strings"
26 "time"
27
Scott Baker611f6bd2019-10-18 13:45:19 -070028 "google.golang.org/grpc/balancer"
Scott Bakereee8dd82019-09-24 12:52:34 -070029 "google.golang.org/grpc/codes"
30 "google.golang.org/grpc/grpclog"
Scott Baker611f6bd2019-10-18 13:45:19 -070031 "google.golang.org/grpc/internal"
32 "google.golang.org/grpc/serviceconfig"
Scott Bakereee8dd82019-09-24 12:52:34 -070033)
34
35const maxInt = int(^uint(0) >> 1)
36
37// MethodConfig defines the configuration recommended by the service providers for a
38// particular method.
39//
40// Deprecated: Users should not use this struct. Service config should be received
41// through name resolver, as specified here
42// https://github.com/grpc/grpc/blob/master/doc/service_config.md
43type MethodConfig struct {
44 // WaitForReady indicates whether RPCs sent to this method should wait until
45 // the connection is ready by default (!failfast). The value specified via the
46 // gRPC client API will override the value set here.
47 WaitForReady *bool
48 // Timeout is the default timeout for RPCs sent to this method. The actual
49 // deadline used will be the minimum of the value specified here and the value
50 // set by the application via the gRPC client API. If either one is not set,
51 // then the other will be used. If neither is set, then the RPC has no deadline.
52 Timeout *time.Duration
53 // MaxReqSize is the maximum allowed payload size for an individual request in a
54 // stream (client->server) in bytes. The size which is measured is the serialized
55 // payload after per-message compression (but before stream compression) in bytes.
56 // The actual value used is the minimum of the value specified here and the value set
57 // by the application via the gRPC client API. If either one is not set, then the other
58 // will be used. If neither is set, then the built-in default is used.
59 MaxReqSize *int
60 // MaxRespSize is the maximum allowed payload size for an individual response in a
61 // stream (server->client) in bytes.
62 MaxRespSize *int
63 // RetryPolicy configures retry options for the method.
64 retryPolicy *retryPolicy
65}
66
Scott Baker611f6bd2019-10-18 13:45:19 -070067type lbConfig struct {
68 name string
69 cfg serviceconfig.LoadBalancingConfig
70}
71
Scott Bakereee8dd82019-09-24 12:52:34 -070072// ServiceConfig is provided by the service provider and contains parameters for how
73// clients that connect to the service should behave.
74//
75// Deprecated: Users should not use this struct. Service config should be received
76// through name resolver, as specified here
77// https://github.com/grpc/grpc/blob/master/doc/service_config.md
78type ServiceConfig struct {
Scott Baker611f6bd2019-10-18 13:45:19 -070079 serviceconfig.Config
80
81 // LB is the load balancer the service providers recommends. The balancer
82 // specified via grpc.WithBalancer will override this. This is deprecated;
83 // lbConfigs is preferred. If lbConfig and LB are both present, lbConfig
84 // will be used.
Scott Bakereee8dd82019-09-24 12:52:34 -070085 LB *string
86
Scott Baker611f6bd2019-10-18 13:45:19 -070087 // lbConfig is the service config's load balancing configuration. If
88 // lbConfig and LB are both present, lbConfig will be used.
89 lbConfig *lbConfig
90
Scott Bakereee8dd82019-09-24 12:52:34 -070091 // Methods contains a map for the methods in this service. If there is an
92 // exact match for a method (i.e. /service/method) in the map, use the
93 // corresponding MethodConfig. If there's no exact match, look for the
94 // default config for the service (/service/) and use the corresponding
95 // MethodConfig if it exists. Otherwise, the method has no MethodConfig to
96 // use.
97 Methods map[string]MethodConfig
98
99 // If a retryThrottlingPolicy is provided, gRPC will automatically throttle
100 // retry attempts and hedged RPCs when the client’s ratio of failures to
101 // successes exceeds a threshold.
102 //
103 // For each server name, the gRPC client will maintain a token_count which is
104 // initially set to maxTokens, and can take values between 0 and maxTokens.
105 //
106 // Every outgoing RPC (regardless of service or method invoked) will change
107 // token_count as follows:
108 //
109 // - Every failed RPC will decrement the token_count by 1.
110 // - Every successful RPC will increment the token_count by tokenRatio.
111 //
112 // If token_count is less than or equal to maxTokens / 2, then RPCs will not
113 // be retried and hedged RPCs will not be sent.
114 retryThrottling *retryThrottlingPolicy
115 // healthCheckConfig must be set as one of the requirement to enable LB channel
116 // health check.
117 healthCheckConfig *healthCheckConfig
Scott Baker611f6bd2019-10-18 13:45:19 -0700118 // rawJSONString stores service config json string that get parsed into
119 // this service config struct.
120 rawJSONString string
Scott Bakereee8dd82019-09-24 12:52:34 -0700121}
122
123// healthCheckConfig defines the go-native version of the LB channel health check config.
124type healthCheckConfig struct {
125 // serviceName is the service name to use in the health-checking request.
126 ServiceName string
127}
128
129// retryPolicy defines the go-native version of the retry policy defined by the
130// service config here:
131// https://github.com/grpc/proposal/blob/master/A6-client-retries.md#integration-with-service-config
132type retryPolicy struct {
133 // MaxAttempts is the maximum number of attempts, including the original RPC.
134 //
135 // This field is required and must be two or greater.
136 maxAttempts int
137
138 // Exponential backoff parameters. The initial retry attempt will occur at
139 // random(0, initialBackoffMS). In general, the nth attempt will occur at
140 // random(0,
141 // min(initialBackoffMS*backoffMultiplier**(n-1), maxBackoffMS)).
142 //
143 // These fields are required and must be greater than zero.
144 initialBackoff time.Duration
145 maxBackoff time.Duration
146 backoffMultiplier float64
147
148 // The set of status codes which may be retried.
149 //
150 // Status codes are specified as strings, e.g., "UNAVAILABLE".
151 //
152 // This field is required and must be non-empty.
153 // Note: a set is used to store this for easy lookup.
154 retryableStatusCodes map[codes.Code]bool
155}
156
157type jsonRetryPolicy struct {
158 MaxAttempts int
159 InitialBackoff string
160 MaxBackoff string
161 BackoffMultiplier float64
162 RetryableStatusCodes []codes.Code
163}
164
165// retryThrottlingPolicy defines the go-native version of the retry throttling
166// policy defined by the service config here:
167// https://github.com/grpc/proposal/blob/master/A6-client-retries.md#integration-with-service-config
168type retryThrottlingPolicy struct {
169 // The number of tokens starts at maxTokens. The token_count will always be
170 // between 0 and maxTokens.
171 //
172 // This field is required and must be greater than zero.
173 MaxTokens float64
174 // The amount of tokens to add on each successful RPC. Typically this will
175 // be some number between 0 and 1, e.g., 0.1.
176 //
177 // This field is required and must be greater than zero. Up to 3 decimal
178 // places are supported.
179 TokenRatio float64
180}
181
182func parseDuration(s *string) (*time.Duration, error) {
183 if s == nil {
184 return nil, nil
185 }
186 if !strings.HasSuffix(*s, "s") {
187 return nil, fmt.Errorf("malformed duration %q", *s)
188 }
189 ss := strings.SplitN((*s)[:len(*s)-1], ".", 3)
190 if len(ss) > 2 {
191 return nil, fmt.Errorf("malformed duration %q", *s)
192 }
193 // hasDigits is set if either the whole or fractional part of the number is
194 // present, since both are optional but one is required.
195 hasDigits := false
196 var d time.Duration
197 if len(ss[0]) > 0 {
198 i, err := strconv.ParseInt(ss[0], 10, 32)
199 if err != nil {
200 return nil, fmt.Errorf("malformed duration %q: %v", *s, err)
201 }
202 d = time.Duration(i) * time.Second
203 hasDigits = true
204 }
205 if len(ss) == 2 && len(ss[1]) > 0 {
206 if len(ss[1]) > 9 {
207 return nil, fmt.Errorf("malformed duration %q", *s)
208 }
209 f, err := strconv.ParseInt(ss[1], 10, 64)
210 if err != nil {
211 return nil, fmt.Errorf("malformed duration %q: %v", *s, err)
212 }
213 for i := 9; i > len(ss[1]); i-- {
214 f *= 10
215 }
216 d += time.Duration(f)
217 hasDigits = true
218 }
219 if !hasDigits {
220 return nil, fmt.Errorf("malformed duration %q", *s)
221 }
222
223 return &d, nil
224}
225
226type jsonName struct {
227 Service *string
228 Method *string
229}
230
231func (j jsonName) generatePath() (string, bool) {
232 if j.Service == nil {
233 return "", false
234 }
235 res := "/" + *j.Service + "/"
236 if j.Method != nil {
237 res += *j.Method
238 }
239 return res, true
240}
241
242// TODO(lyuxuan): delete this struct after cleaning up old service config implementation.
243type jsonMC struct {
244 Name *[]jsonName
245 WaitForReady *bool
246 Timeout *string
247 MaxRequestMessageBytes *int64
248 MaxResponseMessageBytes *int64
249 RetryPolicy *jsonRetryPolicy
250}
251
Scott Baker611f6bd2019-10-18 13:45:19 -0700252type loadBalancingConfig map[string]json.RawMessage
253
Scott Bakereee8dd82019-09-24 12:52:34 -0700254// TODO(lyuxuan): delete this struct after cleaning up old service config implementation.
255type jsonSC struct {
256 LoadBalancingPolicy *string
Scott Baker611f6bd2019-10-18 13:45:19 -0700257 LoadBalancingConfig *[]loadBalancingConfig
Scott Bakereee8dd82019-09-24 12:52:34 -0700258 MethodConfig *[]jsonMC
259 RetryThrottling *retryThrottlingPolicy
260 HealthCheckConfig *healthCheckConfig
261}
262
Scott Baker611f6bd2019-10-18 13:45:19 -0700263func init() {
264 internal.ParseServiceConfig = func(sc string) (interface{}, error) {
265 return parseServiceConfig(sc)
266 }
267}
268
269func parseServiceConfig(js string) (*ServiceConfig, error) {
Scott Bakereee8dd82019-09-24 12:52:34 -0700270 if len(js) == 0 {
Scott Baker611f6bd2019-10-18 13:45:19 -0700271 return nil, fmt.Errorf("no JSON service config provided")
Scott Bakereee8dd82019-09-24 12:52:34 -0700272 }
273 var rsc jsonSC
274 err := json.Unmarshal([]byte(js), &rsc)
275 if err != nil {
276 grpclog.Warningf("grpc: parseServiceConfig error unmarshaling %s due to %v", js, err)
Scott Baker611f6bd2019-10-18 13:45:19 -0700277 return nil, err
Scott Bakereee8dd82019-09-24 12:52:34 -0700278 }
279 sc := ServiceConfig{
280 LB: rsc.LoadBalancingPolicy,
281 Methods: make(map[string]MethodConfig),
282 retryThrottling: rsc.RetryThrottling,
283 healthCheckConfig: rsc.HealthCheckConfig,
Scott Baker611f6bd2019-10-18 13:45:19 -0700284 rawJSONString: js,
Scott Bakereee8dd82019-09-24 12:52:34 -0700285 }
Scott Baker611f6bd2019-10-18 13:45:19 -0700286 if rsc.LoadBalancingConfig != nil {
287 for i, lbcfg := range *rsc.LoadBalancingConfig {
288 if len(lbcfg) != 1 {
289 err := fmt.Errorf("invalid loadBalancingConfig: entry %v does not contain exactly 1 policy/config pair: %q", i, lbcfg)
290 grpclog.Warningf(err.Error())
291 return nil, err
292 }
293 var name string
294 var jsonCfg json.RawMessage
295 for name, jsonCfg = range lbcfg {
296 }
297 builder := balancer.Get(name)
298 if builder == nil {
299 continue
300 }
301 sc.lbConfig = &lbConfig{name: name}
302 if parser, ok := builder.(balancer.ConfigParser); ok {
303 var err error
304 sc.lbConfig.cfg, err = parser.ParseConfig(jsonCfg)
305 if err != nil {
306 return nil, fmt.Errorf("error parsing loadBalancingConfig for policy %q: %v", name, err)
307 }
308 } else if string(jsonCfg) != "{}" {
309 grpclog.Warningf("non-empty balancer configuration %q, but balancer does not implement ParseConfig", string(jsonCfg))
310 }
311 break
312 }
313 if sc.lbConfig == nil {
314 // We had a loadBalancingConfig field but did not encounter a
315 // supported policy. The config is considered invalid in this
316 // case.
317 err := fmt.Errorf("invalid loadBalancingConfig: no supported policies found")
318 grpclog.Warningf(err.Error())
319 return nil, err
320 }
Scott Bakereee8dd82019-09-24 12:52:34 -0700321 }
322
Scott Baker611f6bd2019-10-18 13:45:19 -0700323 if rsc.MethodConfig == nil {
324 return &sc, nil
325 }
Scott Bakereee8dd82019-09-24 12:52:34 -0700326 for _, m := range *rsc.MethodConfig {
327 if m.Name == nil {
328 continue
329 }
330 d, err := parseDuration(m.Timeout)
331 if err != nil {
332 grpclog.Warningf("grpc: parseServiceConfig error unmarshaling %s due to %v", js, err)
Scott Baker611f6bd2019-10-18 13:45:19 -0700333 return nil, err
Scott Bakereee8dd82019-09-24 12:52:34 -0700334 }
335
336 mc := MethodConfig{
337 WaitForReady: m.WaitForReady,
338 Timeout: d,
339 }
340 if mc.retryPolicy, err = convertRetryPolicy(m.RetryPolicy); err != nil {
341 grpclog.Warningf("grpc: parseServiceConfig error unmarshaling %s due to %v", js, err)
Scott Baker611f6bd2019-10-18 13:45:19 -0700342 return nil, err
Scott Bakereee8dd82019-09-24 12:52:34 -0700343 }
344 if m.MaxRequestMessageBytes != nil {
345 if *m.MaxRequestMessageBytes > int64(maxInt) {
346 mc.MaxReqSize = newInt(maxInt)
347 } else {
348 mc.MaxReqSize = newInt(int(*m.MaxRequestMessageBytes))
349 }
350 }
351 if m.MaxResponseMessageBytes != nil {
352 if *m.MaxResponseMessageBytes > int64(maxInt) {
353 mc.MaxRespSize = newInt(maxInt)
354 } else {
355 mc.MaxRespSize = newInt(int(*m.MaxResponseMessageBytes))
356 }
357 }
358 for _, n := range *m.Name {
359 if path, valid := n.generatePath(); valid {
360 sc.Methods[path] = mc
361 }
362 }
363 }
364
365 if sc.retryThrottling != nil {
Scott Baker611f6bd2019-10-18 13:45:19 -0700366 if mt := sc.retryThrottling.MaxTokens; mt <= 0 || mt > 1000 {
367 return nil, fmt.Errorf("invalid retry throttling config: maxTokens (%v) out of range (0, 1000]", mt)
368 }
369 if tr := sc.retryThrottling.TokenRatio; tr <= 0 {
370 return nil, fmt.Errorf("invalid retry throttling config: tokenRatio (%v) may not be negative", tr)
Scott Bakereee8dd82019-09-24 12:52:34 -0700371 }
372 }
Scott Baker611f6bd2019-10-18 13:45:19 -0700373 return &sc, nil
Scott Bakereee8dd82019-09-24 12:52:34 -0700374}
375
376func convertRetryPolicy(jrp *jsonRetryPolicy) (p *retryPolicy, err error) {
377 if jrp == nil {
378 return nil, nil
379 }
380 ib, err := parseDuration(&jrp.InitialBackoff)
381 if err != nil {
382 return nil, err
383 }
384 mb, err := parseDuration(&jrp.MaxBackoff)
385 if err != nil {
386 return nil, err
387 }
388
389 if jrp.MaxAttempts <= 1 ||
390 *ib <= 0 ||
391 *mb <= 0 ||
392 jrp.BackoffMultiplier <= 0 ||
393 len(jrp.RetryableStatusCodes) == 0 {
394 grpclog.Warningf("grpc: ignoring retry policy %v due to illegal configuration", jrp)
395 return nil, nil
396 }
397
398 rp := &retryPolicy{
399 maxAttempts: jrp.MaxAttempts,
400 initialBackoff: *ib,
401 maxBackoff: *mb,
402 backoffMultiplier: jrp.BackoffMultiplier,
403 retryableStatusCodes: make(map[codes.Code]bool),
404 }
405 if rp.maxAttempts > 5 {
406 // TODO(retry): Make the max maxAttempts configurable.
407 rp.maxAttempts = 5
408 }
409 for _, code := range jrp.RetryableStatusCodes {
410 rp.retryableStatusCodes[code] = true
411 }
412 return rp, nil
413}
414
415func min(a, b *int) *int {
416 if *a < *b {
417 return a
418 }
419 return b
420}
421
422func getMaxSize(mcMax, doptMax *int, defaultVal int) *int {
423 if mcMax == nil && doptMax == nil {
424 return &defaultVal
425 }
426 if mcMax != nil && doptMax != nil {
427 return min(mcMax, doptMax)
428 }
429 if mcMax != nil {
430 return mcMax
431 }
432 return doptMax
433}
434
435func newInt(b int) *int {
436 return &b
437}