-
Notifications
You must be signed in to change notification settings - Fork 831
/
Copy pathpolicy.go
278 lines (244 loc) · 7.71 KB
/
policy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* Copyright 2021 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retry
import (
"context"
"fmt"
"github.com/cloudwego/kitex/pkg/klog"
"github.com/cloudwego/kitex/pkg/rpcinfo"
)
// Type is retry type include FailureType, BackupType
type Type int
// retry types
const (
FailureType Type = iota
BackupType
MixedType
)
// String prints human readable information.
func (t Type) String() string {
switch t {
case FailureType:
return "Failure"
case BackupType:
return "Backup"
case MixedType:
return "Mixed"
}
return ""
}
// BuildFailurePolicy is used to build Policy with *FailurePolicy
func BuildFailurePolicy(p *FailurePolicy) Policy {
if p == nil {
return Policy{}
}
return Policy{Enable: true, Type: FailureType, FailurePolicy: p}
}
// BuildBackupRequest is used to build Policy with *BackupPolicy
func BuildBackupRequest(p *BackupPolicy) Policy {
if p == nil {
return Policy{}
}
return Policy{Enable: true, Type: BackupType, BackupPolicy: p}
}
// BuildMixedPolicy is used to build Policy with *MixedPolicy
func BuildMixedPolicy(p *MixedPolicy) Policy {
if p == nil {
return Policy{}
}
return Policy{Enable: true, Type: MixedType, MixedPolicy: p}
}
// Policy contains all retry policies
// DON'T FORGET to update Equals() and DeepCopy() if you add new fields
type Policy struct {
Enable bool `json:"enable"`
// 0 is failure retry, 1 is backup
Type Type `json:"type"`
// notice: only one retry policy can be enabled, which one depend on Policy.Type
FailurePolicy *FailurePolicy `json:"failure_policy,omitempty"`
BackupPolicy *BackupPolicy `json:"backup_policy,omitempty"`
MixedPolicy *MixedPolicy `json:"mixed_policy,omitempty"`
}
func (p *Policy) DeepCopy() *Policy {
if p == nil {
return nil
}
return &Policy{
Enable: p.Enable,
Type: p.Type,
FailurePolicy: p.FailurePolicy.DeepCopy(),
BackupPolicy: p.BackupPolicy.DeepCopy(),
MixedPolicy: p.MixedPolicy.DeepCopy(),
}
}
// FailurePolicy for failure retry
// DON'T FORGET to update Equals() and DeepCopy() if you add new fields
type FailurePolicy struct {
StopPolicy StopPolicy `json:"stop_policy"`
BackOffPolicy *BackOffPolicy `json:"backoff_policy,omitempty"`
RetrySameNode bool `json:"retry_same_node"`
ShouldResultRetry *ShouldResultRetry `json:"-"`
// Extra is not used directly by kitex. It's used for better integrating your own config source.
// After loading FailurePolicy from your config source, `Extra` can be decoded into a user-defined schema,
// with which, more complex strategies can be implemented, such as modifying the `ShouldResultRetry`.
Extra string `json:"extra"`
}
// BackupPolicy for backup request
// DON'T FORGET to update Equals() and DeepCopy() if you add new fields
type BackupPolicy struct {
RetryDelayMS uint32 `json:"retry_delay_ms"`
StopPolicy StopPolicy `json:"stop_policy"`
RetrySameNode bool `json:"retry_same_node"`
}
// MixedPolicy for failure retry
// DON'T FORGET to update Equals() and DeepCopy() if you add new fields
type MixedPolicy struct {
RetryDelayMS uint32 `json:"retry_delay_ms"`
FailurePolicy
}
// StopPolicy is a group policies to decide when stop retry
type StopPolicy struct {
MaxRetryTimes int `json:"max_retry_times"`
MaxDurationMS uint32 `json:"max_duration_ms"`
DisableChainStop bool `json:"disable_chain_stop"`
DDLStop bool `json:"ddl_stop"`
CBPolicy CBPolicy `json:"cb_policy"`
}
const (
defaultCBErrRate = 0.1
cbMinSample = 10
)
// CBPolicy is the circuit breaker policy
type CBPolicy struct {
ErrorRate float64 `json:"error_rate"`
}
// BackOffPolicy is the BackOff policy.
// DON'T FORGET to update Equals() and DeepCopy() if you add new fields
type BackOffPolicy struct {
BackOffType BackOffType `json:"backoff_type"`
CfgItems map[BackOffCfgKey]float64 `json:"cfg_items,omitempty"`
}
// BackOffType means the BackOff type.
type BackOffType string
// all back off types
const (
NoneBackOffType BackOffType = "none"
FixedBackOffType BackOffType = "fixed"
RandomBackOffType BackOffType = "random"
)
// BackOffCfgKey represents the keys for BackOff.
type BackOffCfgKey string
// the keys of all back off configs
const (
FixMSBackOffCfgKey BackOffCfgKey = "fix_ms"
MinMSBackOffCfgKey BackOffCfgKey = "min_ms"
MaxMSBackOffCfgKey BackOffCfgKey = "max_ms"
InitialMSBackOffCfgKey BackOffCfgKey = "initial_ms"
MultiplierBackOffCfgKey BackOffCfgKey = "multiplier"
)
// ShouldResultRetry is used for specifying which error or resp need to be retried
type ShouldResultRetry struct {
// ErrorRetryWithCtx is added in v0.10.0, passing ctx is more convenient for user
ErrorRetryWithCtx func(ctx context.Context, err error, ri rpcinfo.RPCInfo) bool
// RespRetryWithCtx is added in v0.10.0, passing ctx is more convenient for user
RespRetryWithCtx func(ctx context.Context, resp interface{}, ri rpcinfo.RPCInfo) bool
// Deprecated: please use ErrorRetryWithCtx instead of ErrorRetry
ErrorRetry func(err error, ri rpcinfo.RPCInfo) bool
// Deprecated: please use RespRetryWithCtx instead of RespRetry
RespRetry func(resp interface{}, ri rpcinfo.RPCInfo) bool
// disable the default timeout retry in specific scenarios (e.g. the requests are not non-idempotent)
NotRetryForTimeout bool
}
// Equals to check if policy is equal
func (p Policy) Equals(np Policy) bool {
if p.Enable != np.Enable {
return false
}
if p.Type != np.Type {
return false
}
if !p.FailurePolicy.Equals(np.FailurePolicy) {
return false
}
if !p.BackupPolicy.Equals(np.BackupPolicy) {
return false
}
if !p.MixedPolicy.Equals(np.MixedPolicy) {
return false
}
return true
}
// Equals to check if BackOffPolicy is equal.
func (p *BackOffPolicy) Equals(np *BackOffPolicy) bool {
if p == nil {
return np == nil
}
if np == nil {
return false
}
if p.BackOffType != np.BackOffType {
return false
}
if len(p.CfgItems) != len(np.CfgItems) {
return false
}
for k := range p.CfgItems {
if p.CfgItems[k] != np.CfgItems[k] {
return false
}
}
return true
}
func (p *BackOffPolicy) DeepCopy() *BackOffPolicy {
if p == nil {
return nil
}
return &BackOffPolicy{
BackOffType: p.BackOffType,
CfgItems: p.copyCfgItems(),
}
}
func (p *BackOffPolicy) copyCfgItems() map[BackOffCfgKey]float64 {
if p.CfgItems == nil {
return nil
}
cfgItems := make(map[BackOffCfgKey]float64, len(p.CfgItems))
for k, v := range p.CfgItems {
cfgItems[k] = v
}
return cfgItems
}
func (rr *ShouldResultRetry) IsValid() bool {
return rr.ErrorRetryWithCtx != nil || rr.RespRetryWithCtx != nil || rr.RespRetry != nil || rr.ErrorRetry != nil
}
func checkCBErrorRate(p *CBPolicy) error {
if p.ErrorRate <= 0 || p.ErrorRate > 0.3 {
return fmt.Errorf("invalid retry circuit breaker rate, errRate=%0.2f", p.ErrorRate)
}
return nil
}
func checkStopPolicy(sp *StopPolicy, maxRetryTimes int, retryer Retryer) error {
rt := sp.MaxRetryTimes
// 0 is valid, it means stop retry
if rt < 0 || rt > maxRetryTimes {
return fmt.Errorf("invalid MaxRetryTimes[%d]", rt)
}
if e := checkCBErrorRate(&sp.CBPolicy); e != nil {
sp.CBPolicy.ErrorRate = defaultCBErrRate
klog.Warnf("KITEX: %s retry - %s, use default %0.2f", retryer.Type(), e.Error(), defaultCBErrRate)
}
return nil
}