-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathbackoff_test.go
More file actions
520 lines (420 loc) · 13.8 KB
/
Copy pathbackoff_test.go
File metadata and controls
520 lines (420 loc) · 13.8 KB
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
package scyllacdc
import (
"context"
"errors"
"fmt"
"sync"
"testing"
"time"
"github.com/gocql/gocql"
)
type emptyIterator struct{}
func (emptyIterator) Next() (cdcChangeBatchCols, *ChangeRow) { return cdcChangeBatchCols{}, nil }
func (emptyIterator) Close() error { return nil }
type noopConsumer struct{}
func (noopConsumer) Consume(context.Context, Change) error { return nil }
func (noopConsumer) End() error { return nil }
type noopConsumerFactory struct{}
func (noopConsumerFactory) CreateChangeConsumer(context.Context, CreateChangeConsumerInput) (ChangeConsumer, error) {
return noopConsumer{}, nil
}
// callTracker records timestamps of calls in a thread-safe manner.
type callTracker struct {
mu sync.Mutex
times []time.Time
}
func (ct *callTracker) record() {
ct.mu.Lock()
ct.times = append(ct.times, time.Now())
ct.mu.Unlock()
}
func (ct *callTracker) getTimes() []time.Time {
ct.mu.Lock()
defer ct.mu.Unlock()
cp := make([]time.Time, len(ct.times))
copy(cp, ct.times)
return cp
}
func (ct *callTracker) count() int {
ct.mu.Lock()
defer ct.mu.Unlock()
return len(ct.times)
}
func newTestStreamBatchReader(
baseDelay, maxDelay time.Duration,
queryFn func(begin, end gocql.UUID) (cdcIterator, error),
) *streamBatchReader {
config := &ReaderConfig{
ChangeConsumerFactory: noopConsumerFactory{},
ProgressManager: noProgressManager{},
Logger: noLogger{},
Advanced: AdvancedReaderConfig{
PostFailedQueryDelay: baseDelay,
MaxPostFailedQueryDelay: maxDelay,
PostEmptyQueryDelay: baseDelay,
PostNonEmptyQueryDelay: baseDelay,
ConfidenceWindowSize: time.Millisecond,
QueryTimeWindowSize: 365 * 24 * time.Hour,
TableMissingRetryLimit: 30,
},
}
startFrom := time.Now().Add(-time.Hour)
streams := []StreamID{[]byte("0123456789abcdef")} // 16 bytes
sbr := newStreamBatchReader(
config,
startFrom,
streams,
"test_ks",
"test_tbl",
gocql.MinTimeUUID(startFrom),
)
sbr.queryRangeFunc = queryFn
return sbr
}
func TestStreamBatchReaderBackoffOnConsecutiveFailures(t *testing.T) {
tracker := &callTracker{}
baseDelay := 5 * time.Millisecond
maxDelay := 80 * time.Millisecond
sbr := newTestStreamBatchReader(baseDelay, maxDelay, func(begin, end gocql.UUID) (cdcIterator, error) {
tracker.record()
return nil, errors.New("connection refused")
})
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
defer cancel()
err := sbr.run(ctx)
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("expected context.DeadlineExceeded, got: %v", err)
}
times := tracker.getTimes()
if len(times) < 4 {
t.Fatalf("expected at least 4 query attempts, got %d", len(times))
}
if len(times) > 50 {
t.Errorf("too many query attempts (%d), backoff not working", len(times))
}
intervals := computeIntervals(times)
earlyAvg := avgDuration(intervals[:3])
lateAvg := avgDuration(intervals[len(intervals)-3:])
if lateAvg <= earlyAvg {
t.Errorf("late average interval (%v) should be larger than early average (%v)", lateAvg, earlyAvg)
}
if lateAvg < maxDelay/4 {
t.Errorf("late average interval (%v) should approach max delay (%v)", lateAvg, maxDelay)
}
}
func TestStreamBatchReaderBackoffResetOnSuccess(t *testing.T) {
tracker := &callTracker{}
baseDelay := 5 * time.Millisecond
maxDelay := 160 * time.Millisecond
const failsBeforeSuccess = 5
var mu sync.Mutex
callCount := 0
queryFn := func(begin, end gocql.UUID) (cdcIterator, error) {
tracker.record()
mu.Lock()
n := callCount
callCount++
mu.Unlock()
if n == failsBeforeSuccess {
return emptyIterator{}, nil
}
return nil, errors.New("connection refused")
}
sbr := newTestStreamBatchReader(baseDelay, maxDelay, queryFn)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
err := sbr.run(ctx)
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("expected context.DeadlineExceeded, got: %v", err)
}
times := tracker.getTimes()
minCalls := failsBeforeSuccess + 3
if len(times) < minCalls {
t.Fatalf("expected at least %d query attempts, got %d", minCalls, len(times))
}
intervals := computeIntervals(times)
successIdx := failsBeforeSuccess
if len(intervals) <= successIdx+1 {
t.Fatalf("need at least %d intervals, got %d", successIdx+2, len(intervals))
}
intervalBeforeSuccess := intervals[successIdx-1]
intervalAfterSuccess := intervals[successIdx]
intervalFirstErrorAfterReset := intervals[successIdx+1]
if intervalBeforeSuccess < baseDelay*2 {
t.Errorf("interval before success (%v) should show backoff (>= %v)", intervalBeforeSuccess, baseDelay*2)
}
if intervalAfterSuccess >= intervalBeforeSuccess {
t.Errorf("interval after success (%v) should be shorter than before (%v) — backoff not resetting",
intervalAfterSuccess, intervalBeforeSuccess)
}
tolerance := baseDelay * 5
if intervalAfterSuccess > tolerance {
t.Errorf("interval after success (%v) should be close to base delay (%v)", intervalAfterSuccess, baseDelay)
}
if intervalFirstErrorAfterReset > tolerance {
t.Errorf("first failure after reset (%v) should be close to base delay (%v)", intervalFirstErrorAfterReset, baseDelay)
}
}
func TestStreamBatchReaderTableMissingGivesUp(t *testing.T) {
tests := []struct {
name string
err error
}{
{
name: "ErrNotFound (original table deleted)",
err: fmt.Errorf("failed to get CDC table columns: %w", gocql.ErrNotFound),
},
{
name: "unconfigured table (CDC table deleted)",
err: fmt.Errorf("unconfigured table test_ks.test_tbl_scylla_cdc_log"),
},
{
name: "no such table",
err: fmt.Errorf("no such table test_ks.test_tbl_scylla_cdc_log"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tracker := &callTracker{}
sbr := newTestStreamBatchReader(time.Millisecond, 5*time.Millisecond,
func(begin, end gocql.UUID) (cdcIterator, error) {
tracker.record()
return nil, tt.err
},
)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err := sbr.run(ctx)
if errors.Is(err, context.DeadlineExceeded) {
t.Fatal("reader should have given up before context timeout")
}
if err == nil {
t.Fatal("expected an error from table-missing retry limit")
}
if !errors.Is(err, tt.err) {
t.Logf("returned error: %v", err)
}
count := tracker.count()
if count != 31 {
t.Errorf("expected exactly 31 attempts (30 retries + 1 final), got %d", count)
}
})
}
}
func TestStreamBatchReaderGenericErrorDoesNotGiveUp(t *testing.T) {
tracker := &callTracker{}
sbr := newTestStreamBatchReader(time.Millisecond, 5*time.Millisecond,
func(begin, end gocql.UUID) (cdcIterator, error) {
tracker.record()
return nil, errors.New("connection refused")
},
)
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
defer cancel()
err := sbr.run(ctx)
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("expected context.DeadlineExceeded (reader should not give up on generic errors), got: %v", err)
}
count := tracker.count()
if count <= 31 {
t.Errorf("expected more than 31 attempts to prove no retry limit was hit, got %d", count)
}
}
func TestStreamBatchReaderTableMissingResetsOnSuccess(t *testing.T) {
tracker := &callTracker{}
var mu sync.Mutex
callCount := 0
// Return table-missing for 25 calls, then success, then table-missing forever.
// After reset, it should survive another 30 retries.
queryFn := func(begin, end gocql.UUID) (cdcIterator, error) {
tracker.record()
mu.Lock()
n := callCount
callCount++
mu.Unlock()
if n == 25 {
return emptyIterator{}, nil
}
return nil, gocql.ErrNotFound
}
sbr := newTestStreamBatchReader(time.Millisecond, 5*time.Millisecond, queryFn)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err := sbr.run(ctx)
if errors.Is(err, context.DeadlineExceeded) {
t.Fatal("reader should have given up before context timeout")
}
if err == nil {
t.Fatal("expected table-missing error")
}
// Total calls: 25 (table missing) + 1 (success) + 31 (table missing, gives up on 31st) = 57
count := tracker.count()
if count != 57 {
t.Errorf("expected 57 attempts (25 + 1 success + 31), got %d", count)
}
}
type mockGenerationSource struct {
mu sync.Mutex
getTimesFn func(gocql.Consistency) ([]time.Time, error)
}
func (m *mockGenerationSource) getGenerationTimes(consistency gocql.Consistency) ([]time.Time, error) {
m.mu.Lock()
fn := m.getTimesFn
m.mu.Unlock()
return fn(consistency)
}
func (m *mockGenerationSource) getGeneration(genTime time.Time, consistency gocql.Consistency) ([][]StreamID, error) {
return nil, nil
}
func (m *mockGenerationSource) maybeUpgrade() (generationSource, error) {
return m, nil
}
func newTestGenerationFetcher(
basePeriod, maxPeriod time.Duration,
source generationSource,
) *generationFetcher {
return &generationFetcher{
logger: noLogger{},
lastTime: time.Now().Add(-time.Hour),
source: source,
generationCh: make(chan *generation, 1),
stopCh: make(chan struct{}),
refreshCh: make(chan struct{}, 1),
fetchPeriod: basePeriod,
maxFetchPeriod: maxPeriod,
clusterSizeFunc: func() (int, error) { return 3, nil },
}
}
func TestGenerationFetcherBackoffOnConsecutiveFailures(t *testing.T) {
tracker := &callTracker{}
basePeriod := 5 * time.Millisecond
maxPeriod := 80 * time.Millisecond
source := &mockGenerationSource{
getTimesFn: func(gocql.Consistency) ([]time.Time, error) {
tracker.record()
return nil, errors.New("connection refused")
},
}
gf := newTestGenerationFetcher(basePeriod, maxPeriod, source)
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
defer cancel()
_ = gf.Run(ctx)
times := tracker.getTimes()
if len(times) < 4 {
t.Fatalf("expected at least 4 fetch attempts, got %d", len(times))
}
if len(times) > 50 {
t.Errorf("too many fetch attempts (%d), backoff not working", len(times))
}
intervals := computeIntervals(times)
earlyAvg := avgDuration(intervals[:3])
lateAvg := avgDuration(intervals[len(intervals)-3:])
if lateAvg <= earlyAvg {
t.Errorf("late average interval (%v) should be larger than early average (%v)", lateAvg, earlyAvg)
}
if lateAvg < maxPeriod/4 {
t.Errorf("late average interval (%v) should approach max period (%v)", lateAvg, maxPeriod)
}
}
func TestGenerationFetcherBackoffResetOnSuccess(t *testing.T) {
tracker := &callTracker{}
basePeriod := 5 * time.Millisecond
maxPeriod := 160 * time.Millisecond
const failsBeforeSuccess = 5
var mu sync.Mutex
callCount := 0
source := &mockGenerationSource{
getTimesFn: func(gocql.Consistency) ([]time.Time, error) {
tracker.record()
mu.Lock()
n := callCount
callCount++
mu.Unlock()
if n == failsBeforeSuccess {
return nil, nil
}
return nil, errors.New("connection refused")
},
}
gf := newTestGenerationFetcher(basePeriod, maxPeriod, source)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
_ = gf.Run(ctx)
times := tracker.getTimes()
minCalls := failsBeforeSuccess + 3
if len(times) < minCalls {
t.Fatalf("expected at least %d fetch attempts, got %d", minCalls, len(times))
}
intervals := computeIntervals(times)
successIdx := failsBeforeSuccess
if len(intervals) <= successIdx+1 {
t.Fatalf("need at least %d intervals, got %d", successIdx+2, len(intervals))
}
intervalBeforeSuccess := intervals[successIdx-1]
intervalAfterSuccess := intervals[successIdx]
intervalFirstErrorAfterReset := intervals[successIdx+1]
if intervalBeforeSuccess < basePeriod*2 {
t.Errorf("interval before success (%v) should show backoff (>= %v)", intervalBeforeSuccess, basePeriod*2)
}
if intervalAfterSuccess >= intervalBeforeSuccess {
t.Errorf("interval after success (%v) should be shorter than before (%v) — backoff not resetting",
intervalAfterSuccess, intervalBeforeSuccess)
}
tolerance := basePeriod * 5
if intervalAfterSuccess > tolerance {
t.Errorf("interval after success (%v) should be close to base period (%v)", intervalAfterSuccess, basePeriod)
}
if intervalFirstErrorAfterReset > tolerance {
t.Errorf("first failure after reset (%v) should be close to base period (%v)", intervalFirstErrorAfterReset, basePeriod)
}
}
func TestGenerationFetcherClusterSizeErrorBackoff(t *testing.T) {
tracker := &callTracker{}
basePeriod := 5 * time.Millisecond
maxPeriod := 80 * time.Millisecond
source := &mockGenerationSource{
getTimesFn: func(gocql.Consistency) ([]time.Time, error) {
// Should not be called because getClusterSize fails first
t.Error("getGenerationTimes should not be called when getClusterSize fails")
return nil, nil
},
}
gf := newTestGenerationFetcher(basePeriod, maxPeriod, source)
gf.clusterSizeFunc = func() (int, error) {
tracker.record()
return 0, errors.New("cannot reach cluster")
}
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
defer cancel()
_ = gf.Run(ctx)
times := tracker.getTimes()
if len(times) < 4 {
t.Fatalf("expected at least 4 attempts, got %d", len(times))
}
if len(times) > 50 {
t.Errorf("too many attempts (%d), backoff not working for cluster size errors", len(times))
}
intervals := computeIntervals(times)
earlyAvg := avgDuration(intervals[:2])
lateAvg := avgDuration(intervals[len(intervals)-2:])
if lateAvg <= earlyAvg {
t.Errorf("late average interval (%v) should be larger than early average (%v)", lateAvg, earlyAvg)
}
}
func computeIntervals(times []time.Time) []time.Duration {
intervals := make([]time.Duration, 0, len(times)-1)
for i := 1; i < len(times); i++ {
intervals = append(intervals, times[i].Sub(times[i-1]))
}
return intervals
}
func avgDuration(ds []time.Duration) time.Duration {
if len(ds) == 0 {
return 0
}
var sum time.Duration
for _, d := range ds {
sum += d
}
return sum / time.Duration(len(ds))
}