forked from snowflakedb/gosnowflake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
retry.go
319 lines (279 loc) · 7.9 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
// Copyright (c) 2017-2019 Snowflake Computing Inc. All right reserved.
package gosnowflake
import (
"bytes"
"crypto/x509"
"errors"
"fmt"
"github.com/google/uuid"
"io"
"math/rand"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"context"
"sync"
)
var random *rand.Rand
func init() {
random = rand.New(rand.NewSource(time.Now().UnixNano()))
}
// requestGUIDKey is attached to every request against Snowflake
const requestGUIDKey string = "request_guid"
// retryCounterKey is attached to query-request from the second time
const retryCounterKey string = "retryCounter"
// requestIDKey is attached to all requests to Snowflake
const requestIDKey string = "requestId"
// This class takes in an url during construction and replace the
// value of request_guid every time the replace() is called
// When 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, uuid.New().String())
replacer.urlPtr.RawQuery = replacer.urlValues.Encode()
return replacer.urlPtr
}
type retryCounterUpdater interface {
replaceOrAdd(retry int) *url.URL
}
type retryCounterUpdate struct {
urlPtr *url.URL
urlValues url.Values
}
// this replacer does nothing but replace the url
type transientReplaceOrAdd struct {
urlPtr *url.URL
}
func (replaceOrAdder *transientReplaceOrAdd) replaceOrAdd(retry int) *url.URL {
return replaceOrAdder.urlPtr
}
func (replacer *retryCounterUpdate) replaceOrAdd(retry int) *url.URL {
replacer.urlValues.Del(retryCounterKey)
replacer.urlValues.Add(retryCounterKey, strconv.Itoa(retry))
replacer.urlPtr.RawQuery = replacer.urlValues.Encode()
return replacer.urlPtr
}
func newRetryUpdate(urlPtr *url.URL) retryCounterUpdater {
if !strings.HasPrefix(urlPtr.Path, queryRequestPath) {
// nop if not query-request
return &transientReplaceOrAdd{urlPtr}
}
values, err := url.ParseQuery(urlPtr.RawQuery)
if err != nil {
// nop if the URL is not valid
return &transientReplaceOrAdd{urlPtr}
}
return &retryCounterUpdate{urlPtr, values}
}
type waitAlgo struct {
mutex *sync.Mutex // required for random.Int63n
base time.Duration // base wait time
cap time.Duration // maximum wait time
}
func randSecondDuration(n time.Duration) time.Duration {
return time.Duration(random.Int63n(int64(n/time.Second))) * time.Second
}
// decorrelated jitter backoff
func (w *waitAlgo) decorr(attempt int, sleep time.Duration) time.Duration {
w.mutex.Lock()
defer w.mutex.Unlock()
t := 3*sleep - w.base
switch {
case t > 0:
return durationMin(w.cap, randSecondDuration(t)+w.base)
case t < 0:
return durationMin(w.cap, randSecondDuration(-t)+3*sleep)
}
return w.base
}
var defaultWaitAlgo = &waitAlgo{
mutex: &sync.Mutex{},
base: 5 * time.Second,
cap: 160 * time.Second,
}
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
body []byte
timeout time.Duration
raise4XX bool
}
func newRetryHTTP(ctx context.Context,
client clientInterface,
req requestFunc,
fullURL *url.URL,
headers map[string]string,
timeout time.Duration) *retryHTTP {
instance := retryHTTP{}
instance.ctx = ctx
instance.client = client
instance.req = req
instance.method = "GET"
instance.fullURL = fullURL
instance.headers = headers
instance.body = nil
instance.timeout = timeout
instance.raise4XX = false
return &instance
}
func (r *retryHTTP) doRaise4XX(raise4XX bool) *retryHTTP {
r.raise4XX = raise4XX
return r
}
func (r *retryHTTP) doPost() *retryHTTP {
r.method = "POST"
return r
}
func (r *retryHTTP) setBody(body []byte) *retryHTTP {
r.body = body
return r
}
func (r *retryHTTP) execute() (res *http.Response, err error) {
totalTimeout := r.timeout
glog.V(2).Infof("retryHTTP.totalTimeout: %v", totalTimeout)
retryCounter := 0
sleepTime := time.Duration(0)
var rIDReplacer requestGUIDReplacer
var rUpdater retryCounterUpdater
for {
req, err := r.req(r.method, r.fullURL.String(), bytes.NewReader(r.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)
if err != nil {
// check if it can retry.
doExit, err := r.isRetryableError(err)
if doExit {
return res, err
}
// cannot just return 4xx and 5xx status as the error can be sporadic. run often helps.
glog.V(2).Infof(
"failed http connection. no response is returned. err: %v. retrying...\n", err)
} else {
if res.StatusCode == http.StatusOK || r.raise4XX && res != nil && res.StatusCode >= 400 && res.StatusCode < 500 {
// exit if success
// or
// abort connection if raise4XX flag is enabled and the range of HTTP status code are 4XX.
// This is currently used for Snowflake login. The caller must generate an error object based on HTTP status.
break
}
glog.V(2).Infof(
"failed http connection. HTTP Status: %v. retrying...\n", res.StatusCode)
res.Body.Close()
}
// uses decorrelated jitter backoff
sleepTime = defaultWaitAlgo.decorr(retryCounter, sleepTime)
if totalTimeout > 0 {
glog.V(2).Infof("to timeout: %v", totalTimeout)
// if any timeout is set
totalTimeout -= sleepTime
if totalTimeout <= 0 {
if err != nil {
return nil, err
}
if res != nil {
return nil, fmt.Errorf("timeout. HTTP Status: %v. Hanging?", res.StatusCode)
}
return nil, errors.New("timeout. Hanging?")
}
}
retryCounter++
if rIDReplacer == nil {
rIDReplacer = newRequestGUIDReplace(r.fullURL)
}
r.fullURL = rIDReplacer.replace()
if rUpdater == nil {
rUpdater = newRetryUpdate(r.fullURL)
}
r.fullURL = rUpdater.replaceOrAdd(retryCounter)
glog.V(2).Infof("sleeping %v. to timeout: %v. retrying", sleepTime, totalTimeout)
await := time.NewTimer(sleepTime)
select {
case <-await.C:
// retry the request
case <-r.ctx.Done():
await.Stop()
return res, r.ctx.Err()
}
}
return res, err
}
func (r *retryHTTP) isRetryableError(err error) (bool, error) {
urlError, isURLError := err.(*url.Error)
if isURLError {
// context cancel or timeout
if urlError.Err == context.DeadlineExceeded || urlError.Err == context.Canceled {
return true, urlError.Err
}
if driverError, ok := urlError.Err.(*SnowflakeError); ok {
// Certificate Revoked
if driverError.Number == ErrOCSPStatusRevoked {
return true, err
}
}
if _, ok := urlError.Err.(x509.CertificateInvalidError); ok {
// Certificate is invalid
return true, err
}
if _, ok := urlError.Err.(x509.UnknownAuthorityError); ok {
// Certificate is self-signed
return true, err
}
}
return false, err
}