-
Notifications
You must be signed in to change notification settings - Fork 795
/
failure_retryer.go
306 lines (287 loc) · 8.96 KB
/
failure_retryer.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
* 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"
"errors"
"fmt"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/cloudwego/kitex/pkg/circuitbreak"
"github.com/cloudwego/kitex/pkg/kerrors"
"github.com/cloudwego/kitex/pkg/klog"
"github.com/cloudwego/kitex/pkg/rpcinfo"
)
func newFailureRetryer(policy Policy, r *ShouldResultRetry, cbC *cbContainer) (Retryer, error) {
fr := &failureRetryer{specifiedResultRetry: r, cbContainer: cbC}
if err := fr.UpdatePolicy(policy); err != nil {
return nil, fmt.Errorf("newfailureRetryer failed, err=%w", err)
}
return fr, nil
}
type failureRetryer struct {
enable bool
policy *FailurePolicy
backOff BackOff
cbContainer *cbContainer
specifiedResultRetry *ShouldResultRetry
sync.RWMutex
errMsg string
}
// ShouldRetry implements the Retryer interface.
func (r *failureRetryer) ShouldRetry(ctx context.Context, err error, callTimes int, req interface{}, cbKey string) (string, bool) {
r.RLock()
defer r.RUnlock()
if !r.enable {
return "", false
}
if stop, msg := circuitBreakerStop(ctx, r.policy.StopPolicy, r.cbContainer, req, cbKey); stop {
return msg, false
}
if stop, msg := ddlStop(ctx, r.policy.StopPolicy); stop {
return msg, false
}
r.backOff.Wait(callTimes)
return "", true
}
// AllowRetry implements the Retryer interface.
func (r *failureRetryer) AllowRetry(ctx context.Context) (string, bool) {
r.RLock()
defer r.RUnlock()
if !r.enable || r.policy.StopPolicy.MaxRetryTimes == 0 {
return "", false
}
if stop, msg := chainStop(ctx, r.policy.StopPolicy); stop {
return msg, false
}
if stop, msg := ddlStop(ctx, r.policy.StopPolicy); stop {
return msg, false
}
return "", true
}
// Do implement the Retryer interface.
func (r *failureRetryer) Do(ctx context.Context, rpcCall RPCCallFunc, firstRI rpcinfo.RPCInfo, req interface{}) (lastRI rpcinfo.RPCInfo, recycleRI bool, err error) {
r.RLock()
var maxDuration time.Duration
if r.policy.StopPolicy.MaxDurationMS > 0 {
maxDuration = time.Duration(r.policy.StopPolicy.MaxDurationMS) * time.Millisecond
}
retryTimes := r.policy.StopPolicy.MaxRetryTimes
r.RUnlock()
var callTimes int32
var callCosts strings.Builder
var cRI rpcinfo.RPCInfo
cbKey, _ := r.cbContainer.cbCtl.GetKey(ctx, req)
defer func() {
if panicInfo := recover(); panicInfo != nil {
err = panicToErr(ctx, panicInfo, firstRI)
}
}()
startTime := time.Now()
for i := 0; i <= retryTimes; i++ {
var resp interface{}
var callStart time.Time
if i == 0 {
callStart = startTime
} else if i > 0 {
if maxDuration > 0 && time.Since(startTime) > maxDuration {
err = makeRetryErr(ctx, "exceed max duration", callTimes)
break
}
if msg, ok := r.ShouldRetry(ctx, err, i, req, cbKey); !ok {
if msg != "" {
appendMsg := fmt.Sprintf("retried %d, %s", i-1, msg)
appendErrMsg(err, appendMsg)
}
break
}
callStart = time.Now()
callCosts.WriteByte(',')
if respOp, ok := ctx.Value(CtxRespOp).(*int32); ok {
atomic.StoreInt32(respOp, OpNo)
}
}
callTimes++
if r.cbContainer.enablePercentageLimit {
// record stat before call since requests may be slow, making the limiter more accurate
recordRetryStat(cbKey, r.cbContainer.cbPanel, callTimes)
}
cRI, resp, err = rpcCall(ctx, r)
callCosts.WriteString(strconv.FormatInt(time.Since(callStart).Microseconds(), 10))
if !r.cbContainer.enablePercentageLimit && r.cbContainer.cbStat {
circuitbreak.RecordStat(ctx, req, nil, err, cbKey, r.cbContainer.cbCtl, r.cbContainer.cbPanel)
}
if err == nil {
if r.policy.IsRespRetryNonNil() && r.policy.ShouldResultRetry.RespRetry(resp, cRI) {
// user specified resp to do retry
continue
}
break
} else {
if i == retryTimes {
// stop retry then wrap error
err = kerrors.ErrRetry.WithCause(err)
} else if !r.isRetryErr(err, cRI) {
// not timeout or user specified error won't do retry
break
}
}
}
recordRetryInfo(cRI, callTimes, callCosts.String())
if err == nil && callTimes == 1 {
return cRI, true, nil
}
return cRI, false, err
}
// UpdatePolicy implements the Retryer interface.
func (r *failureRetryer) UpdatePolicy(rp Policy) (err error) {
if !rp.Enable {
r.Lock()
r.enable = rp.Enable
r.Unlock()
return nil
}
var errMsg string
if rp.FailurePolicy == nil || rp.Type != FailureType {
errMsg = "FailurePolicy is nil or retry type not match, cannot do update in failureRetryer"
err = errors.New(errMsg)
}
rt := rp.FailurePolicy.StopPolicy.MaxRetryTimes
if errMsg == "" && (rt < 0 || rt > maxFailureRetryTimes) {
errMsg = fmt.Sprintf("invalid failure MaxRetryTimes[%d]", rt)
err = errors.New(errMsg)
}
if errMsg == "" {
if e := checkCBErrorRate(&rp.FailurePolicy.StopPolicy.CBPolicy); e != nil {
rp.FailurePolicy.StopPolicy.CBPolicy.ErrorRate = defaultCBErrRate
errMsg = fmt.Sprintf("failureRetryer %s, use default %0.2f", e.Error(), defaultCBErrRate)
klog.Warnf(errMsg)
}
}
r.Lock()
defer r.Unlock()
r.enable = rp.Enable
if err != nil {
r.errMsg = errMsg
return err
}
r.policy = rp.FailurePolicy
r.setSpecifiedResultRetryIfNeeded(r.specifiedResultRetry)
if bo, e := initBackOff(rp.FailurePolicy.BackOffPolicy); e != nil {
r.errMsg = fmt.Sprintf("failureRetryer update BackOffPolicy failed, err=%s", e.Error())
klog.Warnf(r.errMsg)
} else {
r.backOff = bo
}
return nil
}
// AppendErrMsgIfNeeded implements the Retryer interface.
func (r *failureRetryer) AppendErrMsgIfNeeded(err error, ri rpcinfo.RPCInfo, msg string) {
if r.isRetryErr(err, ri) {
// Add additional reason when retry is not applied.
appendErrMsg(err, msg)
}
}
// Prepare implements the Retryer interface.
func (r *failureRetryer) Prepare(ctx context.Context, prevRI, retryRI rpcinfo.RPCInfo) {
handleRetryInstance(r.policy.RetrySameNode, prevRI, retryRI)
}
func (r *failureRetryer) isRetryErr(err error, ri rpcinfo.RPCInfo) bool {
if err == nil {
return false
}
// Logic Notice:
// some kinds of error cannot be retried, eg: ServiceCircuitBreak.
// But CircuitBreak has been checked in ShouldRetry, it doesn't need to filter ServiceCircuitBreak.
// If there are some other specified errors that cannot be retried, it should be filtered here.
if r.policy.IsRetryForTimeout() && kerrors.IsTimeoutError(err) {
return true
}
if r.policy.IsErrorRetryNonNil() && r.policy.ShouldResultRetry.ErrorRetry(err, ri) {
return true
}
return false
}
func initBackOff(policy *BackOffPolicy) (bo BackOff, err error) {
bo = NoneBackOff
if policy == nil {
return
}
switch policy.BackOffType {
case NoneBackOffType:
case FixedBackOffType:
if policy.CfgItems == nil {
return bo, errors.New("invalid FixedBackOff, CfgItems is nil")
}
fixMS := policy.CfgItems[FixMSBackOffCfgKey]
fixMSInt, _ := strconv.Atoi(fmt.Sprintf("%1.0f", fixMS))
if err = checkFixedBackOff(fixMSInt); err != nil {
return
}
bo = newFixedBackOff(fixMSInt)
case RandomBackOffType:
if policy.CfgItems == nil {
return bo, errors.New("invalid FixedBackOff, CfgItems is nil")
}
minMS := policy.CfgItems[MinMSBackOffCfgKey]
maxMS := policy.CfgItems[MaxMSBackOffCfgKey]
minMSInt, _ := strconv.Atoi(fmt.Sprintf("%1.0f", minMS))
maxMSInt, _ := strconv.Atoi(fmt.Sprintf("%1.0f", maxMS))
if err = checkRandomBackOff(minMSInt, maxMSInt); err != nil {
return
}
bo = newRandomBackOff(minMSInt, maxMSInt)
default:
return bo, fmt.Errorf("invalid backoffType=%v", policy.BackOffType)
}
return
}
// Type implements the Retryer interface.
func (r *failureRetryer) Type() Type {
return FailureType
}
// Dump implements the Retryer interface.
func (r *failureRetryer) Dump() map[string]interface{} {
r.RLock()
defer r.RUnlock()
dm := make(map[string]interface{})
dm["enable"] = r.enable
dm["failure_retry"] = r.policy
if r.policy != nil {
dm["specified_result_retry"] = map[string]bool{
"error_retry": r.policy.IsErrorRetryNonNil(),
"resp_retry": r.policy.IsRespRetryNonNil(),
}
}
if r.errMsg != "" {
dm["errMsg"] = r.errMsg
}
return dm
}
func (r *failureRetryer) setSpecifiedResultRetryIfNeeded(rr *ShouldResultRetry) {
if rr != nil {
// save the object specified by client.WithSpecifiedResultRetry(..)
r.specifiedResultRetry = rr
}
if r.policy != nil && r.specifiedResultRetry != nil {
// The priority of client.WithSpecifiedResultRetry(..) is higher, so always update it
// NOTE: client.WithSpecifiedResultRetry(..) will always reject a nil object
r.policy.ShouldResultRetry = r.specifiedResultRetry
}
}