forked from cortexproject/cortex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ha_tracker_test.go
813 lines (671 loc) · 29.7 KB
/
ha_tracker_test.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
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
package distributor
import (
"context"
"fmt"
"strings"
"testing"
"time"
"github.com/go-kit/log"
"github.com/grafana/dskit/flagext"
"github.com/grafana/dskit/services"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/timestamp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/weaveworks/common/user"
"github.com/cortexproject/cortex/pkg/cortexpb"
"github.com/cortexproject/cortex/pkg/ring"
"github.com/cortexproject/cortex/pkg/ring/kv"
"github.com/cortexproject/cortex/pkg/ring/kv/consul"
"github.com/cortexproject/cortex/pkg/util"
util_log "github.com/cortexproject/cortex/pkg/util/log"
"github.com/cortexproject/cortex/pkg/util/test"
)
func checkReplicaTimestamp(t *testing.T, duration time.Duration, c *haTracker, user, cluster, replica string, expected time.Time) {
key := fmt.Sprintf("%s/%s", user, cluster)
// Round the expected timestamp with milliseconds precision
// to match "received at" precision
expected = expected.Truncate(time.Millisecond)
test.Poll(t, duration, nil, func() interface{} {
c.electedLock.RLock()
r := c.elected[key]
c.electedLock.RUnlock()
if r.GetReplica() != replica {
return fmt.Errorf("replicas did not match: %s != %s", r.GetReplica(), replica)
}
if r.GetDeletedAt() > 0 {
return fmt.Errorf("replica is marked for deletion")
}
if !timestamp.Time(r.GetReceivedAt()).Equal(expected) {
return fmt.Errorf("timestamps did not match: %+v != %+v", timestamp.Time(r.GetReceivedAt()), expected)
}
return nil
})
}
func TestHATrackerConfig_Validate(t *testing.T) {
t.Parallel()
tests := map[string]struct {
cfg HATrackerConfig
expectedErr error
}{
"should pass with default config": {
cfg: func() HATrackerConfig {
cfg := HATrackerConfig{}
flagext.DefaultValues(&cfg)
return cfg
}(),
expectedErr: nil,
},
"should fail if max update timeout jitter is negative": {
cfg: func() HATrackerConfig {
cfg := HATrackerConfig{}
flagext.DefaultValues(&cfg)
cfg.UpdateTimeoutJitterMax = -1
return cfg
}(),
expectedErr: errNegativeUpdateTimeoutJitterMax,
},
"should fail if failover timeout is < update timeout + jitter + 1 sec": {
cfg: func() HATrackerConfig {
cfg := HATrackerConfig{}
flagext.DefaultValues(&cfg)
cfg.FailoverTimeout = 5 * time.Second
cfg.UpdateTimeout = 4 * time.Second
cfg.UpdateTimeoutJitterMax = 2 * time.Second
return cfg
}(),
expectedErr: fmt.Errorf(errInvalidFailoverTimeout, 5*time.Second, 7*time.Second),
},
"should pass if failover timeout is >= update timeout + jitter + 1 sec": {
cfg: func() HATrackerConfig {
cfg := HATrackerConfig{}
flagext.DefaultValues(&cfg)
cfg.FailoverTimeout = 7 * time.Second
cfg.UpdateTimeout = 4 * time.Second
cfg.UpdateTimeoutJitterMax = 2 * time.Second
return cfg
}(),
expectedErr: nil,
},
}
for testName, testData := range tests {
t.Run(testName, func(t *testing.T) {
assert.Equal(t, testData.expectedErr, testData.cfg.Validate())
})
}
}
// Test that values are set in the HATracker after WatchPrefix has found it in the KVStore.
func TestWatchPrefixAssignment(t *testing.T) {
cluster := "c1"
replica := "r1"
codec := GetReplicaDescCodec()
kvStore, closer := consul.NewInMemoryClient(codec, log.NewNopLogger(), nil)
t.Cleanup(func() { assert.NoError(t, closer.Close()) })
mock := kv.PrefixClient(kvStore, "prefix")
c, err := newHATracker(HATrackerConfig{
EnableHATracker: true,
KVStore: kv.Config{Mock: mock},
UpdateTimeout: time.Millisecond,
UpdateTimeoutJitterMax: 0,
FailoverTimeout: time.Millisecond * 2,
}, trackerLimits{maxClusters: 100}, nil, log.NewNopLogger())
require.NoError(t, err)
require.NoError(t, services.StartAndAwaitRunning(context.Background(), c))
defer services.StopAndAwaitTerminated(context.Background(), c) //nolint:errcheck
// Write the first time.
now := time.Now()
err = c.checkReplica(context.Background(), "user", cluster, replica, now)
assert.NoError(t, err)
// Check to see if the value in the trackers cache is correct.
checkReplicaTimestamp(t, time.Second, c, "user", cluster, replica, now)
}
func TestCheckReplicaOverwriteTimeout(t *testing.T) {
replica1 := "replica1"
replica2 := "replica2"
c, err := newHATracker(HATrackerConfig{
EnableHATracker: true,
KVStore: kv.Config{Store: "inmemory"},
UpdateTimeout: 100 * time.Millisecond,
UpdateTimeoutJitterMax: 0,
FailoverTimeout: time.Second,
}, trackerLimits{maxClusters: 100}, nil, log.NewNopLogger())
require.NoError(t, err)
require.NoError(t, services.StartAndAwaitRunning(context.Background(), c))
defer services.StopAndAwaitTerminated(context.Background(), c) //nolint:errcheck
now := time.Now()
// Write the first time.
err = c.checkReplica(context.Background(), "user", "test", replica1, now)
assert.NoError(t, err)
// Throw away a sample from replica2.
err = c.checkReplica(context.Background(), "user", "test", replica2, now)
assert.Error(t, err)
// Wait more than the overwrite timeout.
now = now.Add(1100 * time.Millisecond)
// Accept from replica 2, this should overwrite the saved replica of replica 1.
err = c.checkReplica(context.Background(), "user", "test", replica2, now)
assert.NoError(t, err)
// We timed out accepting samples from replica 1 and should now reject them.
err = c.checkReplica(context.Background(), "user", "test", replica1, now)
assert.Error(t, err)
}
func TestCheckReplicaMultiCluster(t *testing.T) {
replica1 := "replica1"
replica2 := "replica2"
reg := prometheus.NewPedanticRegistry()
c, err := newHATracker(HATrackerConfig{
EnableHATracker: true,
KVStore: kv.Config{Store: "inmemory"},
UpdateTimeout: 100 * time.Millisecond,
UpdateTimeoutJitterMax: 0,
FailoverTimeout: time.Second,
}, trackerLimits{maxClusters: 100}, reg, log.NewNopLogger())
require.NoError(t, err)
require.NoError(t, services.StartAndAwaitRunning(context.Background(), c))
defer services.StopAndAwaitTerminated(context.Background(), c) //nolint:errcheck
now := time.Now()
// Write the first time.
err = c.checkReplica(context.Background(), "user", "c1", replica1, now)
assert.NoError(t, err)
err = c.checkReplica(context.Background(), "user", "c2", replica1, now)
assert.NoError(t, err)
// Reject samples from replica 2 in each cluster.
err = c.checkReplica(context.Background(), "user", "c1", replica2, now)
assert.Error(t, err)
err = c.checkReplica(context.Background(), "user", "c2", replica2, now)
assert.Error(t, err)
// We should still accept from replica 1.
err = c.checkReplica(context.Background(), "user", "c1", replica1, now)
assert.NoError(t, err)
err = c.checkReplica(context.Background(), "user", "c2", replica1, now)
assert.NoError(t, err)
// We expect no CAS operation failures.
metrics, err := reg.Gather()
require.NoError(t, err)
assert.Equal(t, uint64(0), util.GetSumOfHistogramSampleCount(metrics, "cortex_kv_request_duration_seconds", labels.Selector{
labels.MustNewMatcher(labels.MatchEqual, "operation", "CAS"),
labels.MustNewMatcher(labels.MatchRegexp, "status_code", "5.*"),
}))
assert.Greater(t, util.GetSumOfHistogramSampleCount(metrics, "cortex_kv_request_duration_seconds", labels.Selector{
labels.MustNewMatcher(labels.MatchEqual, "operation", "CAS"),
labels.MustNewMatcher(labels.MatchRegexp, "status_code", "2.*"),
}), uint64(0))
}
func TestCheckReplicaMultiClusterTimeout(t *testing.T) {
replica1 := "replica1"
replica2 := "replica2"
reg := prometheus.NewPedanticRegistry()
c, err := newHATracker(HATrackerConfig{
EnableHATracker: true,
KVStore: kv.Config{Store: "inmemory"},
UpdateTimeout: 100 * time.Millisecond,
UpdateTimeoutJitterMax: 0,
FailoverTimeout: time.Second,
}, trackerLimits{maxClusters: 100}, reg, log.NewNopLogger())
require.NoError(t, err)
require.NoError(t, services.StartAndAwaitRunning(context.Background(), c))
defer services.StopAndAwaitTerminated(context.Background(), c) //nolint:errcheck
now := time.Now()
// Write the first time.
err = c.checkReplica(context.Background(), "user", "c1", replica1, now)
assert.NoError(t, err)
err = c.checkReplica(context.Background(), "user", "c2", replica1, now)
assert.NoError(t, err)
// Reject samples from replica 2 in each cluster.
err = c.checkReplica(context.Background(), "user", "c1", replica2, now)
assert.Error(t, err)
err = c.checkReplica(context.Background(), "user", "c2", replica2, now)
assert.Error(t, err)
// Accept a sample for replica1 in C2.
now = now.Add(500 * time.Millisecond)
err = c.checkReplica(context.Background(), "user", "c2", replica1, now)
assert.NoError(t, err)
// Reject samples from replica 2 in each cluster.
err = c.checkReplica(context.Background(), "user", "c1", replica2, now)
assert.Error(t, err)
err = c.checkReplica(context.Background(), "user", "c2", replica2, now)
assert.Error(t, err)
// Wait more than the failover timeout.
now = now.Add(1100 * time.Millisecond)
// Accept a sample from c1/replica2.
err = c.checkReplica(context.Background(), "user", "c1", replica2, now)
assert.NoError(t, err)
// We should still accept from c2/replica1 but reject from c1/replica1.
err = c.checkReplica(context.Background(), "user", "c1", replica1, now)
assert.Error(t, err)
err = c.checkReplica(context.Background(), "user", "c2", replica1, now)
assert.NoError(t, err)
// We expect no CAS operation failures.
metrics, err := reg.Gather()
require.NoError(t, err)
assert.Equal(t, uint64(0), util.GetSumOfHistogramSampleCount(metrics, "cortex_kv_request_duration_seconds", labels.Selector{
labels.MustNewMatcher(labels.MatchEqual, "operation", "CAS"),
labels.MustNewMatcher(labels.MatchRegexp, "status_code", "5.*"),
}))
assert.Greater(t, util.GetSumOfHistogramSampleCount(metrics, "cortex_kv_request_duration_seconds", labels.Selector{
labels.MustNewMatcher(labels.MatchEqual, "operation", "CAS"),
labels.MustNewMatcher(labels.MatchRegexp, "status_code", "2.*"),
}), uint64(0))
}
// Test that writes only happen every update timeout.
func TestCheckReplicaUpdateTimeout(t *testing.T) {
replica := "r1"
cluster := "c1"
user := "user"
codec := GetReplicaDescCodec()
kvStore, closer := consul.NewInMemoryClient(codec, log.NewNopLogger(), nil)
t.Cleanup(func() { assert.NoError(t, closer.Close()) })
mock := kv.PrefixClient(kvStore, "prefix")
c, err := newHATracker(HATrackerConfig{
EnableHATracker: true,
KVStore: kv.Config{Mock: mock},
UpdateTimeout: time.Second,
UpdateTimeoutJitterMax: 0,
FailoverTimeout: time.Second,
}, trackerLimits{maxClusters: 100}, nil, log.NewNopLogger())
require.NoError(t, err)
require.NoError(t, services.StartAndAwaitRunning(context.Background(), c))
defer services.StopAndAwaitTerminated(context.Background(), c) //nolint:errcheck
// Write the first time.
startTime := time.Now()
err = c.checkReplica(context.Background(), user, cluster, replica, startTime)
assert.NoError(t, err)
checkReplicaTimestamp(t, time.Second, c, user, cluster, replica, startTime)
// Timestamp should not update here, since time has not advanced.
err = c.checkReplica(context.Background(), user, cluster, replica, startTime)
assert.NoError(t, err)
checkReplicaTimestamp(t, time.Second, c, user, cluster, replica, startTime)
// Wait 500ms and the timestamp should still not update.
updateTime := time.Unix(0, startTime.UnixNano()).Add(500 * time.Millisecond)
err = c.checkReplica(context.Background(), user, cluster, replica, updateTime)
assert.NoError(t, err)
checkReplicaTimestamp(t, time.Second, c, user, cluster, replica, startTime)
// Now we've waited > 1s, so the timestamp should update.
updateTime = time.Unix(0, startTime.UnixNano()).Add(1100 * time.Millisecond)
err = c.checkReplica(context.Background(), user, cluster, replica, updateTime)
assert.NoError(t, err)
checkReplicaTimestamp(t, time.Second, c, user, cluster, replica, updateTime)
}
// Test that writes only happen every write timeout.
func TestCheckReplicaMultiUser(t *testing.T) {
replica := "r1"
cluster := "c1"
codec := GetReplicaDescCodec()
kvStore, closer := consul.NewInMemoryClient(codec, log.NewNopLogger(), nil)
t.Cleanup(func() { assert.NoError(t, closer.Close()) })
mock := kv.PrefixClient(kvStore, "prefix")
c, err := newHATracker(HATrackerConfig{
EnableHATracker: true,
KVStore: kv.Config{Mock: mock},
UpdateTimeout: 100 * time.Millisecond,
UpdateTimeoutJitterMax: 0,
FailoverTimeout: time.Second,
}, trackerLimits{maxClusters: 100}, nil, log.NewNopLogger())
require.NoError(t, err)
require.NoError(t, services.StartAndAwaitRunning(context.Background(), c))
defer services.StopAndAwaitTerminated(context.Background(), c) //nolint:errcheck
now := time.Now()
// Write the first time for user 1.
err = c.checkReplica(context.Background(), "user1", cluster, replica, now)
assert.NoError(t, err)
checkReplicaTimestamp(t, time.Second, c, "user1", cluster, replica, now)
// Write the first time for user 2.
err = c.checkReplica(context.Background(), "user2", cluster, replica, now)
assert.NoError(t, err)
checkReplicaTimestamp(t, time.Second, c, "user2", cluster, replica, now)
// Now we've waited > 1s, so the timestamp should update.
updated := now.Add(1100 * time.Millisecond)
err = c.checkReplica(context.Background(), "user1", cluster, replica, updated)
assert.NoError(t, err)
checkReplicaTimestamp(t, time.Second, c, "user1", cluster, replica, updated)
// No update for user2.
checkReplicaTimestamp(t, time.Second, c, "user2", cluster, replica, now)
}
func TestCheckReplicaUpdateTimeoutJitter(t *testing.T) {
t.Parallel()
tests := map[string]struct {
updateTimeout time.Duration
updateJitter time.Duration
startTime time.Time
updateTime time.Time
expectedTimestamp time.Time
}{
"should not refresh the replica if the update timeout is not expired yet (without jitter)": {
updateTimeout: 10 * time.Second,
updateJitter: 0,
startTime: time.Unix(5, 0),
updateTime: time.Unix(14, 0),
expectedTimestamp: time.Unix(5, 0),
},
"should refresh the replica if the update timeout is expired (without jitter)": {
updateTimeout: 10 * time.Second,
updateJitter: 0,
startTime: time.Unix(5, 0),
updateTime: time.Unix(15, 0),
expectedTimestamp: time.Unix(15, 0),
},
"should not refresh the replica if the update timeout is not expired yet (with jitter)": {
updateTimeout: 10 * time.Second,
updateJitter: 2 * time.Second,
startTime: time.Unix(5, 0),
updateTime: time.Unix(16, 0),
expectedTimestamp: time.Unix(5, 0),
},
"should refresh the replica if the update timeout is expired (with jitter)": {
updateTimeout: 10 * time.Second,
updateJitter: 2 * time.Second,
startTime: time.Unix(5, 0),
updateTime: time.Unix(17, 0),
expectedTimestamp: time.Unix(17, 0),
},
}
for testName, testData := range tests {
t.Run(testName, func(t *testing.T) {
// Init HA tracker
codec := GetReplicaDescCodec()
kvStore, closer := consul.NewInMemoryClient(codec, log.NewNopLogger(), nil)
t.Cleanup(func() { assert.NoError(t, closer.Close()) })
mock := kv.PrefixClient(kvStore, "prefix")
c, err := newHATracker(HATrackerConfig{
EnableHATracker: true,
KVStore: kv.Config{Mock: mock},
UpdateTimeout: testData.updateTimeout,
UpdateTimeoutJitterMax: 0,
FailoverTimeout: time.Second,
}, trackerLimits{maxClusters: 100}, nil, log.NewNopLogger())
require.NoError(t, err)
require.NoError(t, services.StartAndAwaitRunning(context.Background(), c))
defer services.StopAndAwaitTerminated(context.Background(), c) //nolint:errcheck
// Init context used by the test
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
// Override the jitter so that it's not based on a random value
// we can't control in tests
c.updateTimeoutJitter = testData.updateJitter
// Init the replica in the KV Store
err = c.checkReplica(ctx, "user1", "cluster", "replica-1", testData.startTime)
require.NoError(t, err)
checkReplicaTimestamp(t, time.Second, c, "user1", "cluster", "replica-1", testData.startTime)
// Refresh the replica in the KV Store
err = c.checkReplica(ctx, "user1", "cluster", "replica-1", testData.updateTime)
require.NoError(t, err)
// Assert on the the received timestamp
checkReplicaTimestamp(t, time.Second, c, "user1", "cluster", "replica-1", testData.expectedTimestamp)
})
}
}
func TestFindHALabels(t *testing.T) {
replicaLabel, clusterLabel := "replica", "cluster"
type expectedOutput struct {
cluster string
replica string
}
cases := []struct {
labelsIn []cortexpb.LabelAdapter
expected expectedOutput
}{
{
[]cortexpb.LabelAdapter{
{Name: "__name__", Value: "foo"},
{Name: "bar", Value: "baz"},
{Name: "sample", Value: "1"},
{Name: replicaLabel, Value: "1"},
},
expectedOutput{cluster: "", replica: "1"},
},
{
[]cortexpb.LabelAdapter{
{Name: "__name__", Value: "foo"},
{Name: "bar", Value: "baz"},
{Name: "sample", Value: "1"},
{Name: clusterLabel, Value: "cluster-2"},
},
expectedOutput{cluster: "cluster-2", replica: ""},
},
{
[]cortexpb.LabelAdapter{
{Name: "__name__", Value: "foo"},
{Name: "bar", Value: "baz"},
{Name: "sample", Value: "1"},
{Name: replicaLabel, Value: "3"},
{Name: clusterLabel, Value: "cluster-3"},
},
expectedOutput{cluster: "cluster-3", replica: "3"},
},
}
for _, c := range cases {
cluster, replica := findHALabels(replicaLabel, clusterLabel, c.labelsIn)
assert.Equal(t, c.expected.cluster, cluster)
assert.Equal(t, c.expected.replica, replica)
}
}
func TestHATrackerConfig_ShouldCustomizePrefixDefaultValue(t *testing.T) {
haConfig := HATrackerConfig{}
ringConfig := ring.Config{}
flagext.DefaultValues(&haConfig)
flagext.DefaultValues(&ringConfig)
assert.Equal(t, "ha-tracker/", haConfig.KVStore.Prefix)
assert.NotEqual(t, haConfig.KVStore.Prefix, ringConfig.KVStore.Prefix)
}
func TestHAClustersLimit(t *testing.T) {
const userID = "user"
codec := GetReplicaDescCodec()
kvStore, closer := consul.NewInMemoryClient(codec, log.NewNopLogger(), nil)
t.Cleanup(func() { assert.NoError(t, closer.Close()) })
mock := kv.PrefixClient(kvStore, "prefix")
limits := trackerLimits{maxClusters: 2}
t1, err := newHATracker(HATrackerConfig{
EnableHATracker: true,
KVStore: kv.Config{Mock: mock},
UpdateTimeout: time.Second,
UpdateTimeoutJitterMax: 0,
FailoverTimeout: time.Second,
}, limits, nil, log.NewNopLogger())
require.NoError(t, err)
require.NoError(t, services.StartAndAwaitRunning(context.Background(), t1))
defer services.StopAndAwaitTerminated(context.Background(), t1) //nolint:errcheck
now := time.Now()
assert.NoError(t, t1.checkReplica(context.Background(), userID, "a", "a1", now))
waitForClustersUpdate(t, 1, t1, userID)
assert.NoError(t, t1.checkReplica(context.Background(), userID, "b", "b1", now))
waitForClustersUpdate(t, 2, t1, userID)
assert.EqualError(t, t1.checkReplica(context.Background(), userID, "c", "c1", now), "too many HA clusters (limit: 2)")
// Move time forward, and make sure that checkReplica for existing cluster works fine.
now = now.Add(5 * time.Second) // higher than "update timeout"
assert.NoError(t, t1.checkReplica(context.Background(), userID, "b", "b2", now))
waitForClustersUpdate(t, 2, t1, userID)
// Mark cluster "a" for deletion (it was last updated 5 seconds ago)
// We use seconds timestamp resolution here, to avoid cleaning up 'b'. (In KV store, we only store seconds).
t1.cleanupOldReplicas(context.Background(), time.Unix(now.Unix(), 0))
waitForClustersUpdate(t, 1, t1, userID)
// Now adding cluster "c" works.
assert.NoError(t, t1.checkReplica(context.Background(), userID, "c", "c1", now))
waitForClustersUpdate(t, 2, t1, userID)
// But yet another cluster doesn't.
assert.EqualError(t, t1.checkReplica(context.Background(), userID, "a", "a2", now), "too many HA clusters (limit: 2)")
now = now.Add(5 * time.Second)
// clean all replicas
t1.cleanupOldReplicas(context.Background(), now)
waitForClustersUpdate(t, 0, t1, userID)
// Now "a" works again.
assert.NoError(t, t1.checkReplica(context.Background(), userID, "a", "a1", now))
waitForClustersUpdate(t, 1, t1, userID)
}
func waitForClustersUpdate(t *testing.T, expected int, tr *haTracker, userID string) {
t.Helper()
test.Poll(t, 2*time.Second, expected, func() interface{} {
tr.electedLock.RLock()
defer tr.electedLock.RUnlock()
return len(tr.clusters[userID])
})
}
func TestTooManyClustersError(t *testing.T) {
var err error = tooManyClustersError{limit: 10}
assert.True(t, errors.Is(err, tooManyClustersError{}))
assert.True(t, errors.Is(err, &tooManyClustersError{}))
err = &tooManyClustersError{limit: 20}
assert.True(t, errors.Is(err, tooManyClustersError{}))
assert.True(t, errors.Is(err, &tooManyClustersError{}))
err = replicasNotMatchError{replica: "a", elected: "b"}
assert.False(t, errors.Is(err, tooManyClustersError{}))
assert.False(t, errors.Is(err, &tooManyClustersError{}))
}
func TestReplicasNotMatchError(t *testing.T) {
var err error = replicasNotMatchError{replica: "a", elected: "b"}
assert.True(t, errors.Is(err, replicasNotMatchError{}))
assert.True(t, errors.Is(err, &replicasNotMatchError{}))
err = &replicasNotMatchError{replica: "a", elected: "b"}
assert.True(t, errors.Is(err, replicasNotMatchError{}))
assert.True(t, errors.Is(err, &replicasNotMatchError{}))
err = tooManyClustersError{limit: 10}
assert.False(t, errors.Is(err, replicasNotMatchError{}))
assert.False(t, errors.Is(err, &replicasNotMatchError{}))
}
type trackerLimits struct {
maxClusters int
}
func (l trackerLimits) MaxHAClusters(_ string) int {
return l.maxClusters
}
func TestHATracker_MetricsCleanup(t *testing.T) {
reg := prometheus.NewPedanticRegistry()
tr, err := newHATracker(HATrackerConfig{EnableHATracker: false}, nil, reg, log.NewNopLogger())
require.NoError(t, err)
metrics := []string{
"cortex_ha_tracker_elected_replica_changes_total",
"cortex_ha_tracker_elected_replica_timestamp_seconds",
"cortex_ha_tracker_kv_store_cas_total",
}
tr.electedReplicaChanges.WithLabelValues("userA", "cluster1").Add(5)
tr.electedReplicaChanges.WithLabelValues("userA", "cluster2").Add(8)
tr.electedReplicaChanges.WithLabelValues("userB", "cluster").Add(10)
tr.electedReplicaTimestamp.WithLabelValues("userA", "cluster1").Add(5)
tr.electedReplicaTimestamp.WithLabelValues("userA", "cluster2").Add(8)
tr.electedReplicaTimestamp.WithLabelValues("userB", "cluster").Add(10)
tr.kvCASCalls.WithLabelValues("userA", "cluster1").Add(5)
tr.kvCASCalls.WithLabelValues("userA", "cluster2").Add(8)
tr.kvCASCalls.WithLabelValues("userB", "cluster").Add(10)
require.NoError(t, testutil.GatherAndCompare(reg, strings.NewReader(`
# HELP cortex_ha_tracker_elected_replica_changes_total The total number of times the elected replica has changed for a user ID/cluster.
# TYPE cortex_ha_tracker_elected_replica_changes_total counter
cortex_ha_tracker_elected_replica_changes_total{cluster="cluster",user="userB"} 10
cortex_ha_tracker_elected_replica_changes_total{cluster="cluster1",user="userA"} 5
cortex_ha_tracker_elected_replica_changes_total{cluster="cluster2",user="userA"} 8
# HELP cortex_ha_tracker_elected_replica_timestamp_seconds The timestamp stored for the currently elected replica, from the KVStore.
# TYPE cortex_ha_tracker_elected_replica_timestamp_seconds gauge
cortex_ha_tracker_elected_replica_timestamp_seconds{cluster="cluster",user="userB"} 10
cortex_ha_tracker_elected_replica_timestamp_seconds{cluster="cluster1",user="userA"} 5
cortex_ha_tracker_elected_replica_timestamp_seconds{cluster="cluster2",user="userA"} 8
# HELP cortex_ha_tracker_kv_store_cas_total The total number of CAS calls to the KV store for a user ID/cluster.
# TYPE cortex_ha_tracker_kv_store_cas_total counter
cortex_ha_tracker_kv_store_cas_total{cluster="cluster",user="userB"} 10
cortex_ha_tracker_kv_store_cas_total{cluster="cluster1",user="userA"} 5
cortex_ha_tracker_kv_store_cas_total{cluster="cluster2",user="userA"} 8
`), metrics...))
tr.cleanupHATrackerMetricsForUser("userA")
require.NoError(t, testutil.GatherAndCompare(reg, strings.NewReader(`
# HELP cortex_ha_tracker_elected_replica_changes_total The total number of times the elected replica has changed for a user ID/cluster.
# TYPE cortex_ha_tracker_elected_replica_changes_total counter
cortex_ha_tracker_elected_replica_changes_total{cluster="cluster",user="userB"} 10
# HELP cortex_ha_tracker_elected_replica_timestamp_seconds The timestamp stored for the currently elected replica, from the KVStore.
# TYPE cortex_ha_tracker_elected_replica_timestamp_seconds gauge
cortex_ha_tracker_elected_replica_timestamp_seconds{cluster="cluster",user="userB"} 10
# HELP cortex_ha_tracker_kv_store_cas_total The total number of CAS calls to the KV store for a user ID/cluster.
# TYPE cortex_ha_tracker_kv_store_cas_total counter
cortex_ha_tracker_kv_store_cas_total{cluster="cluster",user="userB"} 10
`), metrics...))
}
func TestCheckReplicaCleanup(t *testing.T) {
replica := "r1"
cluster := "c1"
userID := "user"
ctx := user.InjectOrgID(context.Background(), userID)
reg := prometheus.NewPedanticRegistry()
kvStore, closer := consul.NewInMemoryClient(GetReplicaDescCodec(), log.NewNopLogger(), nil)
t.Cleanup(func() { assert.NoError(t, closer.Close()) })
mock := kv.PrefixClient(kvStore, "prefix")
c, err := newHATracker(HATrackerConfig{
EnableHATracker: true,
KVStore: kv.Config{Mock: mock},
UpdateTimeout: 1 * time.Second,
UpdateTimeoutJitterMax: 0,
FailoverTimeout: time.Second,
}, trackerLimits{maxClusters: 100}, reg, util_log.Logger)
require.NoError(t, err)
require.NoError(t, services.StartAndAwaitRunning(context.Background(), c))
defer services.StopAndAwaitTerminated(context.Background(), c) //nolint:errcheck
now := time.Now()
err = c.checkReplica(context.Background(), userID, cluster, replica, now)
assert.NoError(t, err)
checkReplicaTimestamp(t, time.Second, c, userID, cluster, replica, now)
// Replica is not marked for deletion yet.
checkReplicaDeletionState(t, time.Second, c, userID, cluster, true, true, false)
checkUserClusters(t, time.Second, c, userID, 1)
// This will mark replica for deletion (with time.Now())
c.cleanupOldReplicas(ctx, now.Add(1*time.Second))
// Verify marking for deletion.
checkReplicaDeletionState(t, time.Second, c, userID, cluster, false, true, true)
checkUserClusters(t, time.Second, c, userID, 0)
// This will "revive" the replica.
now = time.Now()
err = c.checkReplica(context.Background(), userID, cluster, replica, now)
assert.NoError(t, err)
checkReplicaTimestamp(t, time.Second, c, userID, cluster, replica, now) // This also checks that entry is not marked for deletion.
checkUserClusters(t, time.Second, c, userID, 1)
// This will mark replica for deletion again (with new time.Now())
c.cleanupOldReplicas(ctx, now.Add(1*time.Second))
checkReplicaDeletionState(t, time.Second, c, userID, cluster, false, true, true)
checkUserClusters(t, time.Second, c, userID, 0)
// Delete entry marked for deletion completely.
c.cleanupOldReplicas(ctx, time.Now().Add(5*time.Second))
checkReplicaDeletionState(t, time.Second, c, userID, cluster, false, false, false)
checkUserClusters(t, time.Second, c, userID, 0)
require.NoError(t, testutil.GatherAndCompare(reg, strings.NewReader(`
# HELP cortex_ha_tracker_replicas_cleanup_marked_for_deletion_total Number of elected replicas marked for deletion.
# TYPE cortex_ha_tracker_replicas_cleanup_marked_for_deletion_total counter
cortex_ha_tracker_replicas_cleanup_marked_for_deletion_total 2
# HELP cortex_ha_tracker_replicas_cleanup_deleted_total Number of elected replicas deleted from KV store.
# TYPE cortex_ha_tracker_replicas_cleanup_deleted_total counter
cortex_ha_tracker_replicas_cleanup_deleted_total 1
# HELP cortex_ha_tracker_replicas_cleanup_delete_failed_total Number of elected replicas that failed to be marked for deletion, or deleted.
# TYPE cortex_ha_tracker_replicas_cleanup_delete_failed_total counter
cortex_ha_tracker_replicas_cleanup_delete_failed_total 0
`), "cortex_ha_tracker_replicas_cleanup_marked_for_deletion_total",
"cortex_ha_tracker_replicas_cleanup_deleted_total",
"cortex_ha_tracker_replicas_cleanup_delete_failed_total",
))
}
func checkUserClusters(t *testing.T, duration time.Duration, c *haTracker, user string, expectedClusters int) {
t.Helper()
test.Poll(t, duration, nil, func() interface{} {
c.electedLock.RLock()
cl := len(c.clusters[user])
c.electedLock.RUnlock()
if cl != expectedClusters {
return fmt.Errorf("expected clusters: %d, got %d", expectedClusters, cl)
}
return nil
})
}
func checkReplicaDeletionState(t *testing.T, duration time.Duration, c *haTracker, user, cluster string, expectedExistsInMemory, expectedExistsInKV, expectedMarkedForDeletion bool) {
key := fmt.Sprintf("%s/%s", user, cluster)
test.Poll(t, duration, nil, func() interface{} {
c.electedLock.RLock()
_, exists := c.elected[key]
c.electedLock.RUnlock()
if exists != expectedExistsInMemory {
return fmt.Errorf("exists in memory: expected=%v, got=%v", expectedExistsInMemory, exists)
}
return nil
})
val, err := c.client.Get(context.Background(), key)
require.NoError(t, err)
existsInKV := val != nil
require.Equal(t, expectedExistsInKV, existsInKV, "exists in KV")
if val != nil {
markedForDeletion := val.(*ReplicaDesc).DeletedAt > 0
require.Equal(t, expectedMarkedForDeletion, markedForDeletion, "KV entry marked for deletion")
}
}