cubefs

Форк
0
162 строки · 5.8 Кб
1
/*
2
 *
3
 * Copyright 2020 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

19
// Package serviceconfig contains utility functions to parse service config.
20
package serviceconfig
21

22
import (
23
	"encoding/json"
24
	"fmt"
25
	"time"
26

27
	"google.golang.org/grpc/balancer"
28
	"google.golang.org/grpc/codes"
29
	"google.golang.org/grpc/grpclog"
30
	externalserviceconfig "google.golang.org/grpc/serviceconfig"
31
)
32

33
var logger = grpclog.Component("core")
34

35
// BalancerConfig wraps the name and config associated with one load balancing
36
// policy. It corresponds to a single entry of the loadBalancingConfig field
37
// from ServiceConfig.
38
//
39
// It implements the json.Unmarshaler interface.
40
//
41
// https://github.com/grpc/grpc-proto/blob/54713b1e8bc6ed2d4f25fb4dff527842150b91b2/grpc/service_config/service_config.proto#L247
42
type BalancerConfig struct {
43
	Name   string
44
	Config externalserviceconfig.LoadBalancingConfig
45
}
46

47
type intermediateBalancerConfig []map[string]json.RawMessage
48

49
// UnmarshalJSON implements the json.Unmarshaler interface.
50
//
51
// ServiceConfig contains a list of loadBalancingConfigs, each with a name and
52
// config. This method iterates through that list in order, and stops at the
53
// first policy that is supported.
54
// - If the config for the first supported policy is invalid, the whole service
55
//   config is invalid.
56
// - If the list doesn't contain any supported policy, the whole service config
57
//   is invalid.
58
func (bc *BalancerConfig) UnmarshalJSON(b []byte) error {
59
	var ir intermediateBalancerConfig
60
	err := json.Unmarshal(b, &ir)
61
	if err != nil {
62
		return err
63
	}
64

65
	for i, lbcfg := range ir {
66
		if len(lbcfg) != 1 {
67
			return fmt.Errorf("invalid loadBalancingConfig: entry %v does not contain exactly 1 policy/config pair: %q", i, lbcfg)
68
		}
69

70
		var (
71
			name    string
72
			jsonCfg json.RawMessage
73
		)
74
		// Get the key:value pair from the map. We have already made sure that
75
		// the map contains a single entry.
76
		for name, jsonCfg = range lbcfg {
77
		}
78

79
		builder := balancer.Get(name)
80
		if builder == nil {
81
			// If the balancer is not registered, move on to the next config.
82
			// This is not an error.
83
			continue
84
		}
85
		bc.Name = name
86

87
		parser, ok := builder.(balancer.ConfigParser)
88
		if !ok {
89
			if string(jsonCfg) != "{}" {
90
				logger.Warningf("non-empty balancer configuration %q, but balancer does not implement ParseConfig", string(jsonCfg))
91
			}
92
			// Stop at this, though the builder doesn't support parsing config.
93
			return nil
94
		}
95

96
		cfg, err := parser.ParseConfig(jsonCfg)
97
		if err != nil {
98
			return fmt.Errorf("error parsing loadBalancingConfig for policy %q: %v", name, err)
99
		}
100
		bc.Config = cfg
101
		return nil
102
	}
103
	// This is reached when the for loop iterates over all entries, but didn't
104
	// return. This means we had a loadBalancingConfig slice but did not
105
	// encounter a registered policy. The config is considered invalid in this
106
	// case.
107
	return fmt.Errorf("invalid loadBalancingConfig: no supported policies found")
108
}
109

110
// MethodConfig defines the configuration recommended by the service providers for a
111
// particular method.
112
type MethodConfig struct {
113
	// WaitForReady indicates whether RPCs sent to this method should wait until
114
	// the connection is ready by default (!failfast). The value specified via the
115
	// gRPC client API will override the value set here.
116
	WaitForReady *bool
117
	// Timeout is the default timeout for RPCs sent to this method. The actual
118
	// deadline used will be the minimum of the value specified here and the value
119
	// set by the application via the gRPC client API.  If either one is not set,
120
	// then the other will be used.  If neither is set, then the RPC has no deadline.
121
	Timeout *time.Duration
122
	// MaxReqSize is the maximum allowed payload size for an individual request in a
123
	// stream (client->server) in bytes. The size which is measured is the serialized
124
	// payload after per-message compression (but before stream compression) in bytes.
125
	// The actual value used is the minimum of the value specified here and the value set
126
	// by the application via the gRPC client API. If either one is not set, then the other
127
	// will be used.  If neither is set, then the built-in default is used.
128
	MaxReqSize *int
129
	// MaxRespSize is the maximum allowed payload size for an individual response in a
130
	// stream (server->client) in bytes.
131
	MaxRespSize *int
132
	// RetryPolicy configures retry options for the method.
133
	RetryPolicy *RetryPolicy
134
}
135

136
// RetryPolicy defines the go-native version of the retry policy defined by the
137
// service config here:
138
// https://github.com/grpc/proposal/blob/master/A6-client-retries.md#integration-with-service-config
139
type RetryPolicy struct {
140
	// MaxAttempts is the maximum number of attempts, including the original RPC.
141
	//
142
	// This field is required and must be two or greater.
143
	MaxAttempts int
144

145
	// Exponential backoff parameters. The initial retry attempt will occur at
146
	// random(0, initialBackoff). In general, the nth attempt will occur at
147
	// random(0,
148
	//   min(initialBackoff*backoffMultiplier**(n-1), maxBackoff)).
149
	//
150
	// These fields are required and must be greater than zero.
151
	InitialBackoff    time.Duration
152
	MaxBackoff        time.Duration
153
	BackoffMultiplier float64
154

155
	// The set of status codes which may be retried.
156
	//
157
	// Status codes are specified as strings, e.g., "UNAVAILABLE".
158
	//
159
	// This field is required and must be non-empty.
160
	// Note: a set is used to store this for easy lookup.
161
	RetryableStatusCodes map[codes.Code]bool
162
}
163

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.