/
backup_retryer.go
261 lines (244 loc) · 7.82 KB
/
backup_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
/*
* 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"
"runtime"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/cloudwego/kitex/pkg/circuitbreak"
"github.com/cloudwego/kitex/pkg/gofunc"
"github.com/cloudwego/kitex/pkg/kerrors"
"github.com/cloudwego/kitex/pkg/klog"
"github.com/cloudwego/kitex/pkg/rpcinfo"
"github.com/cloudwego/kitex/pkg/utils"
)
var errUnexpectedFinish = errors.New("backup request: all retries finished unexpectedly, " +
"please submit an issue to https://github.com/cloudwego/kitex/issues")
func newBackupRetryer(policy Policy, cbC *cbContainer) (Retryer, error) {
br := &backupRetryer{cbContainer: cbC}
if err := br.UpdatePolicy(policy); err != nil {
return nil, fmt.Errorf("newBackupRetryer failed, err=%w", err)
}
return br, nil
}
type backupRetryer struct {
enable bool
policy *BackupPolicy
cbContainer *cbContainer
retryDelay time.Duration
sync.RWMutex
errMsg string
}
type resultWrapper struct {
ri rpcinfo.RPCInfo
err error
}
// ShouldRetry implements the Retryer interface.
func (r *backupRetryer) 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
}
return "", true
}
// AllowRetry implements the Retryer interface.
func (r *backupRetryer) 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
}
return "", true
}
// Do implement the Retryer interface.
func (r *backupRetryer) Do(ctx context.Context, rpcCall RPCCallFunc, firstRI rpcinfo.RPCInfo, req interface{}) (lastRI rpcinfo.RPCInfo, recycleRI bool, err error) {
r.RLock()
retryTimes := r.policy.StopPolicy.MaxRetryTimes
retryDelay := r.retryDelay
r.RUnlock()
var callTimes int32 = 0
var callCosts utils.StringBuilder
callCosts.RawStringBuilder().Grow(32)
var recordCostDoing int32 = 0
var abort int32 = 0
finishedCount := 0
// notice: buff num of chan is very important here, it cannot less than call times, or the below chan receive will block
done := make(chan *resultWrapper, retryTimes+1)
cbKey, _ := r.cbContainer.cbCtl.GetKey(ctx, req)
timer := time.NewTimer(retryDelay)
defer func() {
if panicInfo := recover(); panicInfo != nil {
err = panicToErr(ctx, panicInfo, firstRI)
}
timer.Stop()
}()
// include first call, max loop is retryTimes + 1
doCall := true
for i := 0; ; {
if doCall {
doCall = false
i++
gofunc.GoFunc(ctx, func() {
if atomic.LoadInt32(&abort) == 1 {
return
}
var (
e error
cRI rpcinfo.RPCInfo
)
defer func() {
if panicInfo := recover(); panicInfo != nil {
e = panicToErr(ctx, panicInfo, firstRI)
}
done <- &resultWrapper{cRI, e}
}()
ct := atomic.AddInt32(&callTimes, 1)
callStart := time.Now()
if r.cbContainer.enablePercentageLimit {
// record stat before call since requests may be slow, making the limiter more accurate
recordRetryStat(cbKey, r.cbContainer.cbPanel, ct)
}
cRI, _, e = rpcCall(ctx, r)
recordCost(ct, callStart, &recordCostDoing, &callCosts, &abort, e)
if !r.cbContainer.enablePercentageLimit && r.cbContainer.cbStat {
circuitbreak.RecordStat(ctx, req, nil, e, cbKey, r.cbContainer.cbCtl, r.cbContainer.cbPanel)
}
})
}
select {
case <-timer.C:
if _, ok := r.ShouldRetry(ctx, nil, i, req, cbKey); ok && i <= retryTimes {
doCall = true
timer.Reset(retryDelay)
}
case res := <-done:
if res.err != nil && errors.Is(res.err, kerrors.ErrRPCFinish) {
// There will be only one request (goroutine) pass the `checkRPCState`, others will skip decoding
// and return `ErrRPCFinish`, to avoid concurrent write to response and save the cost of decoding.
// We can safely ignore this error and wait for the response of the passed goroutine.
if finishedCount++; finishedCount >= retryTimes+1 {
// But if all requests return this error, it must be a bug, preventive panic to avoid dead loop
panic(errUnexpectedFinish)
}
continue
}
atomic.StoreInt32(&abort, 1)
recordRetryInfo(res.ri, atomic.LoadInt32(&callTimes), callCosts.String())
return res.ri, false, res.err
}
}
}
// Prepare implements the Retryer interface.
func (r *backupRetryer) Prepare(ctx context.Context, prevRI, retryRI rpcinfo.RPCInfo) {
handleRetryInstance(r.policy.RetrySameNode, prevRI, retryRI)
}
// UpdatePolicy implements the Retryer interface.
func (r *backupRetryer) UpdatePolicy(rp Policy) (err error) {
if !rp.Enable {
r.Lock()
r.enable = rp.Enable
r.Unlock()
return nil
}
var errMsg string
if rp.BackupPolicy == nil || rp.Type != BackupType {
errMsg = "BackupPolicy is nil or retry type not match, cannot do update in backupRetryer"
err = errors.New(errMsg)
}
if errMsg == "" && (rp.BackupPolicy.RetryDelayMS == 0 || rp.BackupPolicy.StopPolicy.MaxRetryTimes < 0 ||
rp.BackupPolicy.StopPolicy.MaxRetryTimes > maxBackupRetryTimes) {
errMsg = "invalid backup request delay duration or retryTimes"
err = errors.New(errMsg)
}
if errMsg == "" {
if e := checkCBErrorRate(&rp.BackupPolicy.StopPolicy.CBPolicy); e != nil {
rp.BackupPolicy.StopPolicy.CBPolicy.ErrorRate = defaultCBErrRate
errMsg = fmt.Sprintf("backupRetryer %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.BackupPolicy
r.retryDelay = time.Duration(rp.BackupPolicy.RetryDelayMS) * time.Millisecond
return nil
}
// AppendErrMsgIfNeeded implements the Retryer interface.
func (r *backupRetryer) AppendErrMsgIfNeeded(err error, ri rpcinfo.RPCInfo, msg string) {
if kerrors.IsTimeoutError(err) {
// Add additional reason to the error message when timeout occurs but the backup request is not sent.
appendErrMsg(err, msg)
}
}
// Dump implements the Retryer interface.
func (r *backupRetryer) Dump() map[string]interface{} {
r.RLock()
defer r.RUnlock()
if r.errMsg != "" {
return map[string]interface{}{
"enable": r.enable,
"backupRequest": r.policy,
"errMsg": r.errMsg,
}
}
return map[string]interface{}{"enable": r.enable, "backupRequest": r.policy}
}
// Type implements the Retryer interface.
func (r *backupRetryer) Type() Type {
return BackupType
}
// record request cost, it may execute concurrent
func recordCost(ct int32, start time.Time, recordCostDoing *int32, sb *utils.StringBuilder, abort *int32, err error) {
if atomic.LoadInt32(abort) == 1 {
return
}
for !atomic.CompareAndSwapInt32(recordCostDoing, 0, 1) {
runtime.Gosched()
}
sb.WithLocked(func(b *strings.Builder) error {
if b.Len() > 0 {
b.WriteByte(',')
}
b.WriteString(strconv.Itoa(int(ct)))
b.WriteByte('-')
b.WriteString(strconv.FormatInt(time.Since(start).Microseconds(), 10))
if err != nil && errors.Is(err, kerrors.ErrRPCFinish) {
// ErrRPCFinish means previous call returns first but is decoding.
// Add ignore to distinguish.
b.WriteString("(ignore)")
}
return nil
})
atomic.StoreInt32(recordCostDoing, 0)
}