-
Notifications
You must be signed in to change notification settings - Fork 12
/
view.go
436 lines (372 loc) · 11.8 KB
/
view.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package hcat
import (
"context"
"fmt"
"math/rand"
"net/http"
"reflect"
"regexp"
"strconv"
"strings"
"sync"
"time"
"github.com/hashicorp/hcat/dep"
"github.com/hashicorp/hcat/events"
idep "github.com/hashicorp/hcat/internal/dependency"
)
var regexUnexpectedResponseCode = regexp.MustCompile("Unexpected response code: ([0-9]{3})")
// Temporarily raise these types to the top level via aliasing.
// This is to address a bug in the short term and this should be refactored
// when thinking of how to modularlize the dependencies.
type QueryOptionsSetter = idep.QueryOptionsSetter
type QueryOptions = idep.QueryOptions
// view is a representation of a Dependency and the most recent data it has
// received from Consul.
type view struct {
// dependency is the dependency that is associated with this view
dependency dep.Dependency
// clients is the list of clients to communicate upstream. This is passed
// directly to the dependency.
clients Looker
// event holds the callback for event processing
event events.EventHandler
// data is the most-recently-received data from Consul for this view. It is
// accompanied by a series of locks and booleans to ensure consistency.
dataLock sync.RWMutex
data interface{}
receivedData bool
lastIndex uint64
// flag to denote that polling is active
isPolling bool
// blockWaitTime is amount of time in seconds to do a blocking query for
blockWaitTime time.Duration
// maxStale is the maximum amount of time to allow a query to be stale.
maxStale time.Duration
// defaultLease is used for non-renewable leases when secret has no lease
defaultLease time.Duration
// retryFunc is the function to invoke on failure to determine if a retry
// should be attempted.
retryFunc RetryFunc
// stopCh is used to stop polling on this view
stopCh chan struct{}
// Each view has a context used to cancel an in-flight HTTP request. This is
// a no-op if there is not an active request. Canceling is required to release
// the underlying TCP connections used by Consul blocking queries that are
// waiting for changes from the server. It allows for the http.Transport to
// change the TCP connection status from active to idle, to then be reaped.
ctx context.Context
ctxCancel context.CancelFunc
}
// NewViewInput is used as input to the NewView function.
type newViewInput struct {
// Dependency is the dependency to associate with the new view.
Dependency dep.Dependency
// Clients is the list of clients to communicate upstream. This is passed
// directly to the dependency.
Clients Looker
// EventHandler takes the callback for event processing
EventHandler events.EventHandler
// BlockWaitTime is amount of time in seconds to do a blocking query for
BlockWaitTime time.Duration
// MaxStale is the maximum amount a time a query response is allowed to be
// stale before forcing a read from the leader.
MaxStale time.Duration
// RetryFunc is a function which dictates how this view should retry on
// upstream errors.
RetryFunc RetryFunc
// Default non-renewable secret duration
VaultDefaultLease time.Duration
}
// NewView constructs a new view with the given inputs.
func newView(i *newViewInput) *view {
ctx, cancel := context.WithCancel(context.Background())
eventHandler := i.EventHandler
if eventHandler == nil {
eventHandler = func(events.Event) {}
}
return &view{
dependency: i.Dependency,
clients: i.Clients,
event: eventHandler,
blockWaitTime: i.BlockWaitTime,
maxStale: i.MaxStale,
retryFunc: i.RetryFunc,
stopCh: make(chan struct{}, 1),
ctx: ctx,
ctxCancel: cancel,
defaultLease: i.VaultDefaultLease,
}
}
// Dependency returns the dependency attached to this view.
func (v *view) Dependency() dep.Dependency {
return v.dependency
}
// Data returns the most-recently-received data from Consul for this view.
func (v *view) Data() interface{} {
v.dataLock.RLock()
defer v.dataLock.RUnlock()
return v.data
}
// DataAndLastIndex returns the most-recently-received data from Consul for
// this view, along with the last index. This is atomic so you will get the
// index that goes with the data you are fetching.
func (v *view) DataAndLastIndex() (interface{}, uint64) {
v.dataLock.RLock()
defer v.dataLock.RUnlock()
return v.data, v.lastIndex
}
// ID outputs a unique string identifier for the view
// It is identical to it's contained Dependency ID.
func (v *view) ID() string {
return v.dependency.ID()
}
// pollingFlag handles setting and clearing the flag to indicate active polling
// Returned function needs to be called (usually w/ defer) to clear the flag.
func (v *view) pollingFlag() (alreadyPolling bool, unflag func()) {
v.dataLock.Lock()
defer v.dataLock.Unlock()
if v.isPolling {
return true, func() {}
}
v.isPolling = true
return false, func() {
v.dataLock.Lock()
defer v.dataLock.Unlock()
v.isPolling = false
}
}
// poll queries the Consul instance for data using the fetch function, but also
// accounts for interrupts on the interrupt channel. This allows the poll
// function to be fired in a goroutine, but then halted even if the fetch
// function is in the middle of a blocking query.
func (v *view) poll(viewCh chan<- *view, errCh chan<- error) {
var retries int
v.event(events.TrackStart{ID: v.ID()})
alreadyPolling, stoppedPolling := v.pollingFlag()
if alreadyPolling {
return
}
defer func() {
stoppedPolling()
v.event(events.TrackStop{ID: v.ID()})
}()
for {
doneCh := make(chan struct{}, 1)
successCh := make(chan struct{}, 1)
fetchErrCh := make(chan error, 1)
go v.fetch(doneCh, successCh, fetchErrCh)
WAIT:
select {
case <-doneCh:
// Reset the retry to avoid exponentially incrementing retries when we
// have some successful requests
retries = 0
select {
case <-v.stopCh:
return
case viewCh <- v:
}
case <-successCh:
// We successfully received a non-error response from the server.
// This does not mean we have data (that's dataCh's job), but
// rather this just resets the counter indicating we communicated
// successfully. For example, Consul make have an outage, but when
// it returns, the view is unchanged. We have to reset the counter
// retries, but not update the actual template.
v.event(events.ServerContacted{ID: v.ID()})
retries = 0
goto WAIT
case err := <-fetchErrCh:
v.event(events.ServerError{ID: v.ID(), Error: err})
var skipRetry bool
if strings.Contains(err.Error(), "Unexpected response code: 400") {
// 400 is not useful to retry
skipRetry = true
}
if getResponseCodeFromError(err) == http.StatusInternalServerError {
// This indicates that Consul may have restarted. If Consul
// restarted, the current lastIndex will be stale and cause the
// next blocking query to hang until the wait time expires. To
// be safe, reset the lastIndex=0 so that the next query will not
// block and retrieve the latest lastIndex
v.dataLock.Lock()
v.lastIndex = 0
v.dataLock.Unlock()
}
if v.retryFunc != nil && !skipRetry {
retry, sleep := v.retryFunc(retries)
if retry {
v.event(events.RetryAttempt{
ID: v.ID(),
Attempt: retries + 1,
Sleep: sleep,
Error: err,
})
select {
case <-time.After(sleep):
retries++
continue
case <-v.stopCh:
return
}
}
v.event(events.MaxRetries{ID: v.ID(), Count: retries})
}
// Push the error back up to the watcher
select {
case <-v.stopCh:
return
case errCh <- err:
return
}
case <-v.stopCh:
return
}
}
}
// fetch queries the Consul instance for the attached dependency. This API
// promises that either data will be written to doneCh or an error will be
// written to errCh. It is designed to be run in a goroutine that selects the
// result of doneCh and errCh. It is assumed that only one instance of fetch
// is running per view and therefore no locking or mutexes are used.
func (v *view) fetch(doneCh, successCh chan<- struct{}, errCh chan<- error) {
v.event(events.Trace{ID: v.ID(), Message: "starting fetch"})
var allowStale bool
if v.maxStale != 0 {
allowStale = true
}
for {
// If the view was stopped, short-circuit this loop. This prevents a bug
// where a view can get "lost" in the event Consul Template is reloaded.
select {
case <-v.stopCh:
return
case <-v.ctx.Done():
return
default:
}
start := time.Now() // for rateLimiter below
if d, ok := v.dependency.(QueryOptionsSetter); ok {
opts := QueryOptions{
AllowStale: allowStale,
WaitTime: v.blockWaitTime,
WaitIndex: v.lastIndex,
DefaultLease: v.defaultLease,
}
opts = opts.SetContext(v.ctx)
d.SetOptions(opts)
}
v.event(events.Trace{ID: v.ID(), Message: "fetching value"})
data, rm, err := v.dependency.Fetch(v.clients)
if err != nil {
switch {
case err == dep.ErrStopped:
v.event(events.Trace{ID: v.ID(), Message: err.Error()})
case strings.Contains(err.Error(), context.Canceled.Error()):
// This is a wrapped error so relying on string matching
v.event(events.Trace{ID: v.ID(), Message: err.Error()})
default:
errCh <- err
}
return
}
if rm == nil {
errCh <- fmt.Errorf("received nil response metadata - this is a bug " +
"and should be reported")
return
}
// If we got this far, we received data successfully. That data might not
// trigger a data update (because we could continue below), but we need to
// inform the poller to reset the retry count.
v.event(events.Trace{ID: v.ID(), Message: "successful data response"})
select {
case successCh <- struct{}{}:
default:
}
if allowStale && rm.LastContact > v.maxStale {
allowStale = false
v.event(events.StaleData{ID: v.ID(), LastContant: rm.LastContact})
continue
}
if v.maxStale != 0 {
allowStale = true
}
if rm.LastIndex == v.lastIndex {
v.event(events.Trace{ID: v.ID(), Message: "same index, no new data"})
continue
}
if v.receivedData { // ratelimit after first
if dur := rateLimiter(start); dur > 1 {
time.Sleep(dur)
}
}
v.dataLock.Lock()
if rm.LastIndex < v.lastIndex {
v.event(events.Trace{ID: v.ID(),
Message: "wrong index order, resetting"})
v.lastIndex = 0
v.dataLock.Unlock()
continue
}
v.lastIndex = rm.LastIndex
if v.receivedData && reflect.DeepEqual(data, v.data) {
v.event(events.NoNewData{ID: v.ID()})
v.dataLock.Unlock()
continue
}
if _, ok := v.dependency.(idep.BlockingQuery); ok && data == nil {
v.event(events.BlockingWait{ID: v.ID()})
v.dataLock.Unlock()
continue
}
v.dataLock.Unlock()
v.event(events.NewData{ID: v.ID(), Data: data})
v.store(data)
close(doneCh)
return
}
}
// Store-s the data and marks that it was received
// Returns the view to make test setup easier.
func (v *view) store(data interface{}) *view {
v.dataLock.Lock()
defer v.dataLock.Unlock()
v.data = data
if !v.receivedData {
v.receivedData = true
}
return v
}
const minDelayBetweenUpdates = time.Millisecond * 100
// return a duration to sleep to limit the frequency of upstream calls
func rateLimiter(start time.Time) time.Duration {
remaining := minDelayBetweenUpdates - time.Since(start)
if remaining > 0 {
dither := time.Duration(rand.Int63n(20000000)) // 0-20ms
return remaining + dither
}
return 0
}
// stop halts polling of this view.
func (v *view) stop() {
v.dependency.Stop()
close(v.stopCh)
v.ctxCancel()
}
func getResponseCodeFromError(err error) int {
// Extract the unexpected response substring
s := regexUnexpectedResponseCode.FindString(err.Error())
if s == "" {
return 0
}
// Extract the response code substring from the unexpected response substring
s = s[len(s)-3:]
// Convert the response code to an integer
i, err := strconv.Atoi(s)
if err != nil {
return 0
}
return i
}