1/*
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
28	"google.golang.org/grpc/balancer"
29	"google.golang.org/grpc/codes"
30	"google.golang.org/grpc/grpclog"
31	"google.golang.org/grpc/internal"
32	"google.golang.org/grpc/serviceconfig"
33)
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
67type lbConfig struct {
68	name string
69	cfg  serviceconfig.LoadBalancingConfig
70}
71
72// 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 {
79	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.
85	LB *string
86
87	// 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
91	// 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
118	// rawJSONString stores service config json string that get parsed into
119	// this service config struct.
120	rawJSONString string
121}
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
252type loadBalancingConfig map[string]json.RawMessage
253
254// TODO(lyuxuan): delete this struct after cleaning up old service config implementation.
255type jsonSC struct {
256	LoadBalancingPolicy *string
257	LoadBalancingConfig *[]loadBalancingConfig
258	MethodConfig        *[]jsonMC
259	RetryThrottling     *retryThrottlingPolicy
260	HealthCheckConfig   *healthCheckConfig
261}
262
263func init() {
264	internal.ParseServiceConfig = func(sc string) (interface{}, error) {
265		return parseServiceConfig(sc)
266	}
267}
268
269func parseServiceConfig(js string) (*ServiceConfig, error) {
270	if len(js) == 0 {
271		return nil, fmt.Errorf("no JSON service config provided")
272	}
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)
277		return nil, err
278	}
279	sc := ServiceConfig{
280		LB:                rsc.LoadBalancingPolicy,
281		Methods:           make(map[string]MethodConfig),
282		retryThrottling:   rsc.RetryThrottling,
283		healthCheckConfig: rsc.HealthCheckConfig,
284		rawJSONString:     js,
285	}
286	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	}
314
315	if rsc.MethodConfig == nil {
316		return &sc, nil
317	}
318	for _, m := range *rsc.MethodConfig {
319		if m.Name == nil {
320			continue
321		}
322		d, err := parseDuration(m.Timeout)
323		if err != nil {
324			grpclog.Warningf("grpc: parseServiceConfig error unmarshaling %s due to %v", js, err)
325			return nil, err
326		}
327
328		mc := MethodConfig{
329			WaitForReady: m.WaitForReady,
330			Timeout:      d,
331		}
332		if mc.retryPolicy, err = convertRetryPolicy(m.RetryPolicy); err != nil {
333			grpclog.Warningf("grpc: parseServiceConfig error unmarshaling %s due to %v", js, err)
334			return nil, err
335		}
336		if m.MaxRequestMessageBytes != nil {
337			if *m.MaxRequestMessageBytes > int64(maxInt) {
338				mc.MaxReqSize = newInt(maxInt)
339			} else {
340				mc.MaxReqSize = newInt(int(*m.MaxRequestMessageBytes))
341			}
342		}
343		if m.MaxResponseMessageBytes != nil {
344			if *m.MaxResponseMessageBytes > int64(maxInt) {
345				mc.MaxRespSize = newInt(maxInt)
346			} else {
347				mc.MaxRespSize = newInt(int(*m.MaxResponseMessageBytes))
348			}
349		}
350		for _, n := range *m.Name {
351			if path, valid := n.generatePath(); valid {
352				sc.Methods[path] = mc
353			}
354		}
355	}
356
357	if sc.retryThrottling != nil {
358		if mt := sc.retryThrottling.MaxTokens; mt <= 0 || mt > 1000 {
359			return nil, fmt.Errorf("invalid retry throttling config: maxTokens (%v) out of range (0, 1000]", mt)
360		}
361		if tr := sc.retryThrottling.TokenRatio; tr <= 0 {
362			return nil, fmt.Errorf("invalid retry throttling config: tokenRatio (%v) may not be negative", tr)
363		}
364	}
365	return &sc, nil
366}
367
368func convertRetryPolicy(jrp *jsonRetryPolicy) (p *retryPolicy, err error) {
369	if jrp == nil {
370		return nil, nil
371	}
372	ib, err := parseDuration(&jrp.InitialBackoff)
373	if err != nil {
374		return nil, err
375	}
376	mb, err := parseDuration(&jrp.MaxBackoff)
377	if err != nil {
378		return nil, err
379	}
380
381	if jrp.MaxAttempts <= 1 ||
382		*ib <= 0 ||
383		*mb <= 0 ||
384		jrp.BackoffMultiplier <= 0 ||
385		len(jrp.RetryableStatusCodes) == 0 {
386		grpclog.Warningf("grpc: ignoring retry policy %v due to illegal configuration", jrp)
387		return nil, nil
388	}
389
390	rp := &retryPolicy{
391		maxAttempts:          jrp.MaxAttempts,
392		initialBackoff:       *ib,
393		maxBackoff:           *mb,
394		backoffMultiplier:    jrp.BackoffMultiplier,
395		retryableStatusCodes: make(map[codes.Code]bool),
396	}
397	if rp.maxAttempts > 5 {
398		// TODO(retry): Make the max maxAttempts configurable.
399		rp.maxAttempts = 5
400	}
401	for _, code := range jrp.RetryableStatusCodes {
402		rp.retryableStatusCodes[code] = true
403	}
404	return rp, nil
405}
406
407func min(a, b *int) *int {
408	if *a < *b {
409		return a
410	}
411	return b
412}
413
414func getMaxSize(mcMax, doptMax *int, defaultVal int) *int {
415	if mcMax == nil && doptMax == nil {
416		return &defaultVal
417	}
418	if mcMax != nil && doptMax != nil {
419		return min(mcMax, doptMax)
420	}
421	if mcMax != nil {
422		return mcMax
423	}
424	return doptMax
425}
426
427func newInt(b int) *int {
428	return &b
429}
430