-
Notifications
You must be signed in to change notification settings - Fork 12
/
changes_follower.go
470 lines (424 loc) · 14.4 KB
/
changes_follower.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/**
* © Copyright IBM Corporation 2022, 2024. All Rights Reserved.
*
* 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 features
import (
"context"
"errors"
"fmt"
"math"
"math/rand"
"net/http"
"strings"
"sync"
"time"
"github.com/IBM/cloudant-go-sdk/cloudantv1"
"github.com/IBM/cloudant-go-sdk/common"
"github.com/IBM/go-sdk-core/v5/core"
)
// Max timedelta is limited at max int64. A compiler will stop attempts to
// exceed it.
const forever time.Duration = math.MaxInt64
// Minimal client timeout set to 1 minute.
const minClientTimeout time.Duration = time.Minute
// LongpollTimeout is set to give the changes request a chance to be answered
// before the client timeout it is set to 3 seconds less.
const LongpollTimeout time.Duration = minClientTimeout - 3*time.Second
// BatchSize is the default number of document to pull in a single request.
const BatchSize int = 10000
// Base delay between unsuccessful attempts to pull changes feed
// in presence of transient errors
const baseDelay time.Duration = 100 * time.Millisecond
// Once we reach this number of retries we'll be capping the backoff
var expRetryGate int = int(math.Log(float64(LongpollTimeout/baseDelay)) / math.Log(2))
// Mode are enums for changes follower's operation mode.
type Mode int
const (
// Finite mode is an alias for StartOneOff()
Finite Mode = iota
// Listen mode is an alias for Start()
Listen
)
// TransientErrorSuppression are enums for changes follower's
// transient errors suppression mode.
type TransientErrorSuppression int
const (
// Always suppress transient errors
Always TransientErrorSuppression = iota
// Never suppress transient errors
Never
// Timer specifies a time duration for suppressing transient errors
Timer
)
// ChangesFollower is a helper for using the changes feed.
//
// There are two modes of operation:
//
// StartOneOff() to fetch the changes from the supplied since sequence
// until there are no further pending changes.
// Start() to fetch the changes from the supplied since sequence
// and then continuing to listen indefinitely for further new changes.
//
// The starting sequence ID can be changed for either mode by using "Since".
// By default when using:
//
// StartOneOff() the feed will start from the beginning.
// Start() the feed will start from "now".
//
// In either mode the response stream can be terminated early by calling Stop().
// By default ChangesFollower will suppress transient errors indefinitely
// and endeavour to run to completion or listen forever. For applications
// where that behaviour is not desirable an alternate option is
// available where a duration may be specified to limit the time since the
// last successful response that transient errors will be suppressed.
// It should be noted that some errors considered terminal, for example, the
// database not existing or invalid credentials are never suppressed and will
// return an Error immediately.
// The named attributes for "PostChangesOptions" are used to configure the behaviour of the ChangesFollower.
// However, a subset of the attributes are invalid as they are configured
// internally by the implementation and will cause an Error to be returned
// if supplied.
// These invalid options are:
// - Descending
// - Feed
// - Heartbeat
// - LastEventID
// - Timeout
//
// Only the value of "_selector" is permitted for
// the PostChangesOptions's "Filter" option.
// Selector based filters perform better than JS based filters and using one
// of the alternative JS based filter types will cause ChangesFollower
// to return an Error.
// It should also be noted that the "Limit" parameter will truncate the
// response at the given number of changes in either operating mode.
// The ChangesFollower requires the Cloudant client to have HTTP timeout
// of at least 1 minute.
// The default client configuration has a sufficiently long timeout.
type ChangesFollower struct {
client *cloudantv1.CloudantV1
options *cloudantv1.PostChangesOptions
mode Mode
since string
limit int
retry int
errorTolerance time.Duration
suppression TransientErrorSuppression
successTimestamp time.Time
ctx context.Context
cancel context.CancelFunc
running bool
runLock sync.Mutex
logger core.Logger
}
// ChangesItem is a wrapper structure around cloudantv1.ChangesResultItem
// with addtitional attribute Error for errors received during the run.
type ChangesItem struct {
item cloudantv1.ChangesResultItem
error error
}
// Item is a ChangesItem's getter for cloudantv1.ChangesResultItem
// that either returns an acquired item or an error received
// during its fetch.
func (ci ChangesItem) Item() (cloudantv1.ChangesResultItem, error) {
if ci.error == nil && ci.item.ID == nil {
err := core.SDKErrorf(nil, "can't read from a closed channel", "changes-follower-closed-channel", common.GetComponentInfo())
return ci.item, err
}
return ci.item, ci.error
}
// NewChangesFollower returns a new ChangesFollower or an error if provided
// configuration is invalid.
func NewChangesFollower(c *cloudantv1.CloudantV1, o *cloudantv1.PostChangesOptions) (*ChangesFollower, error) {
ctx := context.Background()
return NewChangesFollowerWithContext(ctx, c, o)
}
// NewChangesFollowerWithContext returns a new ChangesFollower initiated
// with a given context or an error if provided configuration is invalid.
func NewChangesFollowerWithContext(ctx context.Context, c *cloudantv1.CloudantV1, o *cloudantv1.PostChangesOptions) (*ChangesFollower, error) {
err := validateOptions(o)
if err != nil {
return nil, err
}
client := c.Service.GetHTTPClient()
if client.Timeout > 0 && client.Timeout < minClientTimeout {
err := fmt.Errorf("to use ChangesFollower the client timeout must be at least %d ms. The client timeout is %d ms", minClientTimeout/time.Millisecond, client.Timeout/time.Millisecond)
return nil, core.SDKErrorf(err, "", "changes-follower-invalid-timeout", common.GetComponentInfo())
}
cf := &ChangesFollower{
client: c,
options: o,
limit: -1,
// this is default, since we are setting error tolerance separately
suppression: Always,
errorTolerance: forever,
logger: core.GetLogger(),
}
if o.Limit != nil {
cf.limit = int(*o.Limit)
}
cf.setOptionsDefaults()
cf.ctx, cf.cancel = context.WithCancel(ctx)
return cf, nil
}
// SetErrorTolerance sets the duration to suppress errors, measured
// from the previous successful request.
func (cf *ChangesFollower) SetErrorTolerance(d time.Duration) error {
if d < 0 {
return core.SDKErrorf(nil, "error tolerance duration must not be negative", "changes-follower-invalid-tolerance", common.GetComponentInfo())
} else if d == 0 {
cf.suppression = Never
} else if d < forever {
cf.suppression = Timer
}
cf.errorTolerance = d
return nil
}
// Start returns a channel that will stream all available changes
// and keep listening for new changes until reaching an end condition.
//
// The end conditions are:
// - a terminal error (e.g. unauthorized client).
// - transient errors occur for longer than the error
// suppression duration.
// - the number of changes received reaches the limit specified
// in the "PostChangesOptions" args used to instantiate
// this ChangesFollower.
// - ChangesFollower's Stop() is called.
//
// The same change may be received more than once.
//
// Returns a channel of ChangesItem structs or an error
// if ChangesFollower's Start() or StartOneOff() was already called
// or terminal error is recevied from the service during setup.
func (cf *ChangesFollower) Start() (<-chan ChangesItem, error) {
return cf.run(Listen)
}
// StartOneOff returns a channel that will stream all available changes
// until there are no further changes pending or reaching an end condition.
//
// The end conditions are:
// - a terminal error (e.g. unauthorized client).
// - transient errors occur for longer than the error
// suppression duration.
// - the number of changes received reaches the limit specified
// in the "PostChangesOptions" used to instantiate this ChangesFollower.
// - ChangesFollower's Stop() is called.
//
// The same change may be received more than once.
//
// Returns a channel of ChangesItem structs or an error
// if ChangesFollower's Start() or StartOneOff() was already called
// or terminal error is recevied from the service during setup.
func (cf *ChangesFollower) StartOneOff() (<-chan ChangesItem, error) {
return cf.run(Finite)
}
// Stop this ChangesFollower.
func (cf *ChangesFollower) Stop() {
cf.cancel()
}
func validateOptions(o *cloudantv1.PostChangesOptions) error {
// this validates that database was properly set
err := core.ValidateStruct(o, "postChangesOptions")
if err != nil {
return err
}
errAttrs := make([]string, 0)
if o.Descending != nil {
errAttrs = append(errAttrs, "descending")
}
if o.Feed != nil {
errAttrs = append(errAttrs, "feed")
}
if o.Heartbeat != nil {
errAttrs = append(errAttrs, "heartbeat")
}
if o.LastEventID != nil {
errAttrs = append(errAttrs, "lastEventId")
}
if o.Timeout != nil {
errAttrs = append(errAttrs, "timeout")
}
if o.Filter != nil && *o.Filter != "_selector" {
errAttrs = append(errAttrs, fmt.Sprintf("filter=%s", *o.Filter))
}
if len(errAttrs) == 1 {
err := fmt.Errorf("the option '%s' is invalid when using ChangesFollower", errAttrs[0])
return core.SDKErrorf(err, "", "changes-follower-validation-failed", common.GetComponentInfo())
}
if len(errAttrs) > 0 {
err := fmt.Errorf("the options %s are invalid when using ChangesFollower", strings.Join(errAttrs, ", "))
return core.SDKErrorf(err, "", "changes-follower-validation-failed", common.GetComponentInfo())
}
return nil
}
func (cf *ChangesFollower) setOptionsDefaults() *ChangesFollower {
cf.options.SetFeed(cloudantv1.PostChangesOptionsFeedLongpollConst)
cf.options.SetTimeout(LongpollTimeout.Milliseconds())
return cf
}
func (cf *ChangesFollower) withLimit(limit int) *ChangesFollower {
cf.logger.Debug("Applying changes limit %d", limit)
cf.options.SetLimit(int64(limit))
return cf
}
func (cf *ChangesFollower) run(m Mode) (<-chan ChangesItem, error) {
defer cf.runLock.Unlock()
cf.runLock.Lock()
if cf.running {
return nil, core.SDKErrorf(nil, "cannot start a feed that has already started", "changes-follower-feed-started", common.GetComponentInfo())
}
cf.running = true
switch m {
case Finite:
cf.mode = Finite
cf.since = "0"
case Listen:
cf.mode = Listen
cf.since = "now"
}
if cf.options.Since != nil {
cf.since = *cf.options.Since
}
batchSize := BatchSize
if cf.options.IncludeDocs != nil && *cf.options.IncludeDocs {
o := cf.client.NewGetDatabaseInformationOptions(*cf.options.Db)
result, _, err := cf.client.GetDatabaseInformation(o)
if err != nil {
return nil, err
}
docs := 0
externalSize := 0
if result.DocCount != nil {
docs = int(*result.DocCount)
}
if result.Sizes != nil && (*result.Sizes).External != nil {
externalSize = int(*result.Sizes.External)
}
if externalSize > 0 && docs > 0 {
batchSize = 5 * 1024 * 1024 / (externalSize/docs + 500)
if batchSize < 1 {
batchSize = 1
}
}
}
if cf.limit > 0 && cf.limit < batchSize {
batchSize = cf.limit
}
cf.setOptionsDefaults().withLimit(batchSize)
cf.successTimestamp = time.Now()
changes := make(chan ChangesItem)
go func() {
defer close(changes)
for batch := range cf.getChangesBatch() {
if errors.Is(cf.ctx.Err(), context.Canceled) || errors.Is(cf.ctx.Err(), context.DeadlineExceeded) {
return
} else if batch.error != nil {
select {
case changes <- ChangesItem{error: batch.error}:
return
case <-cf.ctx.Done():
return
}
}
for _, item := range batch.items {
if cf.limit == 0 {
cf.Stop()
return
}
select {
case changes <- ChangesItem{item: item}:
case <-cf.ctx.Done():
return
}
if cf.limit > 0 {
cf.limit--
}
}
}
}()
return changes, nil
}
type changesItems struct {
items []cloudantv1.ChangesResultItem
error error
}
func (cf *ChangesFollower) getChangesBatch() chan changesItems {
changes := make(chan changesItems, 1)
go func() {
defer close(changes)
for {
select {
case <-cf.ctx.Done():
return
default:
}
cf.options.SetSince(cf.since)
result, resp, err := cf.client.PostChangesWithContext(cf.ctx, cf.options)
if err != nil {
cf.logger.Debug("Error getting changes: %s", err)
if resp == nil || isTerminalError(resp.GetStatusCode()) {
cf.logger.Debug("Terminal error.")
changes <- changesItems{error: err}
return
}
if cf.suppression == Never || (cf.suppression == Timer && cf.successTimestamp.Add(cf.errorTolerance).Before(time.Now())) {
cf.logger.Debug("Error tolerance deadline exceeded.")
changes <- changesItems{error: err}
return
}
cf.retryDelay()
continue
}
cf.since = *result.LastSeq
cf.retry = 0
if cf.suppression == Timer {
cf.successTimestamp = time.Now()
}
changes <- changesItems{items: result.Results}
if cf.mode == Finite && *result.Pending == 0 {
return
}
}
}()
return changes
}
func isTerminalError(code int) bool {
switch code {
case http.StatusBadRequest, http.StatusUnauthorized, http.StatusForbidden, http.StatusNotFound:
return true
default:
return false
}
}
// retryDelay implements full jitter delay algorithm.
//
// This is an exponential capped backoff with added jitter to spread
// retry calls in case of multiple followers started simultaneously
// for different feeds on the same account.
//
// The base delay is set to 100 ms and cap is set to the changes
// feed pull's timeout.
//
// Algorithm reference: https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/
func (cf *ChangesFollower) retryDelay() {
expDelay := int(LongpollTimeout)
if cf.retry < expRetryGate {
expDelay = int(math.Pow(2, float64(cf.retry)) * float64(baseDelay))
}
jitterDelay := rand.Intn(expDelay)
time.Sleep(time.Duration(jitterDelay))
cf.retry++
}