-
Notifications
You must be signed in to change notification settings - Fork 116
/
retry.go
408 lines (360 loc) · 11.4 KB
/
retry.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// Copyright (c) 2017-2022 Snowflake Computing Inc. All rights reserved.
package gosnowflake
import (
"bytes"
"context"
"fmt"
"io"
"math"
"math/rand"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"time"
)
type waitAlgo struct {
mutex *sync.Mutex // required for *rand.Rand usage
random *rand.Rand
base time.Duration // base wait time
cap time.Duration // maximum wait time
}
var random *rand.Rand
var defaultWaitAlgo *waitAlgo
var authEndpoints = []string{
loginRequestPath,
tokenRequestPath,
authenticatorRequestPath,
}
var clientErrorsStatusCodesEligibleForRetry = []int{
http.StatusTooManyRequests,
http.StatusRequestTimeout,
}
func init() {
random = rand.New(rand.NewSource(time.Now().UnixNano()))
// sleep time before retrying starts from 1s and the max sleep time is 16s
defaultWaitAlgo = &waitAlgo{mutex: &sync.Mutex{}, random: random, base: 1 * time.Second, cap: 16 * time.Second}
}
const (
// requestGUIDKey is attached to every request against Snowflake
requestGUIDKey string = "request_guid"
// retryCountKey is attached to query-request from the second time
retryCountKey string = "retryCount"
// retryReasonKey contains last HTTP status or 0 if timeout
retryReasonKey string = "retryReason"
// clientStartTime contains a time when client started request (first request, not retries)
clientStartTimeKey string = "clientStartTime"
// requestIDKey is attached to all requests to Snowflake
requestIDKey string = "requestId"
)
// This class takes in an url during construction and replaces the value of
// request_guid every time replace() is called. If the url does not contain
// request_guid, just return the original url
type requestGUIDReplacer interface {
// replace the url with new ID
replace() *url.URL
}
// Make requestGUIDReplacer given a url string
func newRequestGUIDReplace(urlPtr *url.URL) requestGUIDReplacer {
values, err := url.ParseQuery(urlPtr.RawQuery)
if err != nil {
// nop if invalid query parameters
return &transientReplace{urlPtr}
}
if len(values.Get(requestGUIDKey)) == 0 {
// nop if no request_guid is included.
return &transientReplace{urlPtr}
}
return &requestGUIDReplace{urlPtr, values}
}
// this replacer does nothing but replace the url
type transientReplace struct {
urlPtr *url.URL
}
func (replacer *transientReplace) replace() *url.URL {
return replacer.urlPtr
}
/*
requestGUIDReplacer is a one-shot object that is created out of the retry loop and
called with replace to change the retry_guid's value upon every retry
*/
type requestGUIDReplace struct {
urlPtr *url.URL
urlValues url.Values
}
/*
*
This function would replace they value of the requestGUIDKey in a url with a newly
generated UUID
*/
func (replacer *requestGUIDReplace) replace() *url.URL {
replacer.urlValues.Del(requestGUIDKey)
replacer.urlValues.Add(requestGUIDKey, NewUUID().String())
replacer.urlPtr.RawQuery = replacer.urlValues.Encode()
return replacer.urlPtr
}
type retryCountUpdater interface {
replaceOrAdd(retry int) *url.URL
}
type retryCountUpdate struct {
urlPtr *url.URL
urlValues url.Values
}
// this replacer does nothing but replace the url
type transientRetryCountUpdater struct {
urlPtr *url.URL
}
func (replaceOrAdder *transientRetryCountUpdater) replaceOrAdd(retry int) *url.URL {
return replaceOrAdder.urlPtr
}
func (replacer *retryCountUpdate) replaceOrAdd(retry int) *url.URL {
replacer.urlValues.Del(retryCountKey)
replacer.urlValues.Add(retryCountKey, strconv.Itoa(retry))
replacer.urlPtr.RawQuery = replacer.urlValues.Encode()
return replacer.urlPtr
}
func newRetryCountUpdater(urlPtr *url.URL) retryCountUpdater {
if !isQueryRequest(urlPtr) {
// nop if not query-request
return &transientRetryCountUpdater{urlPtr}
}
values, err := url.ParseQuery(urlPtr.RawQuery)
if err != nil {
// nop if the URL is not valid
return &transientRetryCountUpdater{urlPtr}
}
return &retryCountUpdate{urlPtr, values}
}
type retryReasonUpdater interface {
replaceOrAdd(reason int) *url.URL
}
type retryReasonUpdate struct {
url *url.URL
}
func (retryReasonUpdater *retryReasonUpdate) replaceOrAdd(reason int) *url.URL {
query := retryReasonUpdater.url.Query()
query.Del(retryReasonKey)
query.Add(retryReasonKey, strconv.Itoa(reason))
retryReasonUpdater.url.RawQuery = query.Encode()
return retryReasonUpdater.url
}
type transientRetryReasonUpdater struct {
url *url.URL
}
func (retryReasonUpdater *transientRetryReasonUpdater) replaceOrAdd(_ int) *url.URL {
return retryReasonUpdater.url
}
func newRetryReasonUpdater(url *url.URL, cfg *Config) retryReasonUpdater {
// not a query request
if !isQueryRequest(url) {
return &transientRetryReasonUpdater{url}
}
// implicitly disabled retry reason
if cfg != nil && cfg.IncludeRetryReason == ConfigBoolFalse {
return &transientRetryReasonUpdater{url}
}
return &retryReasonUpdate{url}
}
func ensureClientStartTimeIsSet(url *url.URL, clientStartTime string) *url.URL {
if !isQueryRequest(url) {
// nop if not query-request
return url
}
query := url.Query()
if query.Has(clientStartTimeKey) {
return url
}
query.Add(clientStartTimeKey, clientStartTime)
url.RawQuery = query.Encode()
return url
}
func isQueryRequest(url *url.URL) bool {
return strings.HasPrefix(url.Path, queryRequestPath)
}
// jitter backoff in seconds
func (w *waitAlgo) calculateWaitBeforeRetryForAuthRequest(attempt int, currWaitTimeDuration time.Duration) time.Duration {
w.mutex.Lock()
defer w.mutex.Unlock()
currWaitTimeInSeconds := currWaitTimeDuration.Seconds()
jitterAmount := w.getJitter(currWaitTimeInSeconds)
jitteredSleepTime := chooseRandomFromRange(currWaitTimeInSeconds+jitterAmount, math.Pow(2, float64(attempt))+jitterAmount)
return time.Duration(jitteredSleepTime * float64(time.Second))
}
func (w *waitAlgo) calculateWaitBeforeRetry(sleep time.Duration) time.Duration {
w.mutex.Lock()
defer w.mutex.Unlock()
// use decorrelated jitter in retry time
randDuration := randMilliSecondDuration(w.base, sleep*3)
return durationMin(w.cap, randDuration)
}
func randMilliSecondDuration(base time.Duration, bound time.Duration) time.Duration {
baseNumber := int64(base / time.Millisecond)
boundNumber := int64(bound / time.Millisecond)
randomDuration := random.Int63n(boundNumber-baseNumber) + baseNumber
return time.Duration(randomDuration) * time.Millisecond
}
func (w *waitAlgo) getJitter(currWaitTime float64) float64 {
multiplicationFactor := chooseRandomFromRange(-1, 1)
jitterAmount := 0.5 * currWaitTime * multiplicationFactor
return jitterAmount
}
type requestFunc func(method, urlStr string, body io.Reader) (*http.Request, error)
type clientInterface interface {
Do(req *http.Request) (*http.Response, error)
}
type retryHTTP struct {
ctx context.Context
client clientInterface
req requestFunc
method string
fullURL *url.URL
headers map[string]string
bodyCreator bodyCreatorType
timeout time.Duration
maxRetryCount int
currentTimeProvider currentTimeProvider
cfg *Config
}
func newRetryHTTP(ctx context.Context,
client clientInterface,
req requestFunc,
fullURL *url.URL,
headers map[string]string,
timeout time.Duration,
maxRetryCount int,
currentTimeProvider currentTimeProvider,
cfg *Config) *retryHTTP {
instance := retryHTTP{}
instance.ctx = ctx
instance.client = client
instance.req = req
instance.method = "GET"
instance.fullURL = fullURL
instance.headers = headers
instance.timeout = timeout
instance.maxRetryCount = maxRetryCount
instance.bodyCreator = emptyBodyCreator
instance.currentTimeProvider = currentTimeProvider
instance.cfg = cfg
return &instance
}
func (r *retryHTTP) doPost() *retryHTTP {
r.method = "POST"
return r
}
func (r *retryHTTP) setBody(body []byte) *retryHTTP {
r.bodyCreator = func() ([]byte, error) {
return body, nil
}
return r
}
func (r *retryHTTP) setBodyCreator(bodyCreator bodyCreatorType) *retryHTTP {
r.bodyCreator = bodyCreator
return r
}
func (r *retryHTTP) execute() (res *http.Response, err error) {
totalTimeout := r.timeout
logger.WithContext(r.ctx).Infof("retryHTTP.totalTimeout: %v", totalTimeout)
retryCounter := 0
sleepTime := time.Duration(time.Second)
clientStartTime := strconv.FormatInt(r.currentTimeProvider.currentTime(), 10)
var requestGUIDReplacer requestGUIDReplacer
var retryCountUpdater retryCountUpdater
var retryReasonUpdater retryReasonUpdater
for {
logger.WithContext(r.ctx).Debugf("retry count: %v", retryCounter)
body, err := r.bodyCreator()
if err != nil {
return nil, err
}
req, err := r.req(r.method, r.fullURL.String(), bytes.NewReader(body))
if err != nil {
return nil, err
}
if req != nil {
// req can be nil in tests
req = req.WithContext(r.ctx)
}
for k, v := range r.headers {
req.Header.Set(k, v)
}
res, err = r.client.Do(req)
// check if it can retry.
retryable, err := isRetryableError(req, res, err)
if !retryable {
return res, err
}
if err != nil {
logger.WithContext(r.ctx).Warningf(
"failed http connection. err: %v. retrying...\n", err)
} else {
logger.WithContext(r.ctx).Warningf(
"failed http connection. HTTP Status: %v. retrying...\n", res.StatusCode)
res.Body.Close()
}
// uses exponential jitter backoff
retryCounter++
if isLoginRequest(req) {
sleepTime = defaultWaitAlgo.calculateWaitBeforeRetryForAuthRequest(retryCounter, sleepTime)
} else {
sleepTime = defaultWaitAlgo.calculateWaitBeforeRetry(sleepTime)
}
if totalTimeout > 0 {
logger.WithContext(r.ctx).Infof("to timeout: %v", totalTimeout)
// if any timeout is set
totalTimeout -= sleepTime
if totalTimeout <= 0 || retryCounter > r.maxRetryCount {
if err != nil {
return nil, err
}
if res != nil {
return nil, fmt.Errorf("timeout after %s and %v retries. HTTP Status: %v. Hanging?", r.timeout, retryCounter, res.StatusCode)
}
return nil, fmt.Errorf("timeout after %s and %v retries. Hanging?", r.timeout, retryCounter)
}
}
if requestGUIDReplacer == nil {
requestGUIDReplacer = newRequestGUIDReplace(r.fullURL)
}
r.fullURL = requestGUIDReplacer.replace()
if retryCountUpdater == nil {
retryCountUpdater = newRetryCountUpdater(r.fullURL)
}
r.fullURL = retryCountUpdater.replaceOrAdd(retryCounter)
if retryReasonUpdater == nil {
retryReasonUpdater = newRetryReasonUpdater(r.fullURL, r.cfg)
}
retryReason := 0
if res != nil {
retryReason = res.StatusCode
}
r.fullURL = retryReasonUpdater.replaceOrAdd(retryReason)
r.fullURL = ensureClientStartTimeIsSet(r.fullURL, clientStartTime)
logger.WithContext(r.ctx).Infof("sleeping %v. to timeout: %v. retrying", sleepTime, totalTimeout)
logger.WithContext(r.ctx).Infof("retry count: %v, retry reason: %v", retryCounter, retryReason)
await := time.NewTimer(sleepTime)
select {
case <-await.C:
// retry the request
case <-r.ctx.Done():
await.Stop()
return res, r.ctx.Err()
}
}
}
func isRetryableError(req *http.Request, res *http.Response, err error) (bool, error) {
if err != nil && res == nil { // Failed http connection. Most probably client timeout.
return true, err
}
if res == nil || req == nil {
return false, err
}
return isRetryableStatus(res.StatusCode), err
}
func isRetryableStatus(statusCode int) bool {
return (statusCode >= 500 && statusCode < 600) || contains(clientErrorsStatusCodesEligibleForRetry, statusCode)
}
func isLoginRequest(req *http.Request) bool {
return contains(authEndpoints, req.URL.Path)
}