-
Notifications
You must be signed in to change notification settings - Fork 810
/
Copy pathgateway_test.go
1132 lines (966 loc) · 41.1 KB
/
gateway_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
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package storegateway
import (
"context"
"fmt"
"io/ioutil"
"math"
"math/rand"
"net/http"
"os"
"path"
"path/filepath"
"sort"
"strconv"
"strings"
"testing"
"time"
"github.com/go-kit/log"
"github.com/grafana/dskit/flagext"
"github.com/grafana/dskit/kv/consul"
"github.com/grafana/dskit/ring"
"github.com/grafana/dskit/services"
"github.com/oklog/ulid"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/tsdb"
"github.com/prometheus/prometheus/tsdb/chunkenc"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/thanos-io/thanos/pkg/block"
"github.com/thanos-io/thanos/pkg/block/metadata"
"github.com/thanos-io/thanos/pkg/extprom"
"github.com/thanos-io/thanos/pkg/objstore"
"github.com/thanos-io/thanos/pkg/store/labelpb"
"github.com/thanos-io/thanos/pkg/store/storepb"
"google.golang.org/grpc/status"
"github.com/cortexproject/cortex/pkg/storage/bucket"
"github.com/cortexproject/cortex/pkg/storage/bucket/filesystem"
cortex_tsdb "github.com/cortexproject/cortex/pkg/storage/tsdb"
"github.com/cortexproject/cortex/pkg/storage/tsdb/bucketindex"
cortex_testutil "github.com/cortexproject/cortex/pkg/storage/tsdb/testutil"
"github.com/cortexproject/cortex/pkg/util"
"github.com/cortexproject/cortex/pkg/util/test"
"github.com/cortexproject/cortex/pkg/util/validation"
)
func TestConfig_Validate(t *testing.T) {
tests := map[string]struct {
setup func(cfg *Config, limits *validation.Limits)
expected error
}{
"should pass by default": {
setup: func(cfg *Config, limits *validation.Limits) {},
expected: nil,
},
"should fail if the sharding strategy is invalid": {
setup: func(cfg *Config, limits *validation.Limits) {
cfg.ShardingEnabled = true
cfg.ShardingStrategy = "xxx"
},
expected: errInvalidShardingStrategy,
},
"should fail if the sharding strategy is shuffle-sharding and shard size has not been set": {
setup: func(cfg *Config, limits *validation.Limits) {
cfg.ShardingEnabled = true
cfg.ShardingStrategy = util.ShardingStrategyShuffle
},
expected: errInvalidTenantShardSize,
},
"should pass if the sharding strategy is shuffle-sharding and shard size has been set": {
setup: func(cfg *Config, limits *validation.Limits) {
cfg.ShardingEnabled = true
cfg.ShardingStrategy = util.ShardingStrategyShuffle
limits.StoreGatewayTenantShardSize = 3
},
expected: nil,
},
}
for testName, testData := range tests {
t.Run(testName, func(t *testing.T) {
cfg := &Config{}
limits := &validation.Limits{}
flagext.DefaultValues(cfg, limits)
testData.setup(cfg, limits)
assert.Equal(t, testData.expected, cfg.Validate(*limits))
})
}
}
func TestStoreGateway_InitialSyncWithDefaultShardingEnabled(t *testing.T) {
tests := map[string]struct {
initialExists bool
initialState ring.InstanceState
initialTokens ring.Tokens
}{
"instance not in the ring": {
initialExists: false,
},
"instance already in the ring with PENDING state and has no tokens": {
initialExists: true,
initialState: ring.PENDING,
initialTokens: ring.Tokens{},
},
"instance already in the ring with JOINING state and has some tokens": {
initialExists: true,
initialState: ring.JOINING,
initialTokens: ring.Tokens{1, 2, 3, 4, 5, 6, 7, 8, 9},
},
"instance already in the ring with ACTIVE state and has all tokens": {
initialExists: true,
initialState: ring.ACTIVE,
initialTokens: generateSortedTokens(RingNumTokens),
},
"instance already in the ring with LEAVING state and has all tokens": {
initialExists: true,
initialState: ring.LEAVING,
initialTokens: generateSortedTokens(RingNumTokens),
},
}
for testName, testData := range tests {
t.Run(testName, func(t *testing.T) {
ctx := context.Background()
gatewayCfg := mockGatewayConfig()
gatewayCfg.ShardingEnabled = true
storageCfg := mockStorageConfig(t)
ringStore, closer := consul.NewInMemoryClient(ring.GetCodec(), log.NewNopLogger(), nil)
t.Cleanup(func() { assert.NoError(t, closer.Close()) })
bucketClient := &bucket.ClientMock{}
// Setup the initial instance state in the ring.
if testData.initialExists {
require.NoError(t, ringStore.CAS(ctx, RingKey, func(in interface{}) (interface{}, bool, error) {
ringDesc := ring.GetOrCreateRingDesc(in)
ringDesc.AddIngester(gatewayCfg.ShardingRing.InstanceID, gatewayCfg.ShardingRing.InstanceAddr, "", testData.initialTokens, testData.initialState, time.Now())
return ringDesc, true, nil
}))
}
g, err := newStoreGateway(gatewayCfg, storageCfg, bucketClient, ringStore, defaultLimitsOverrides(t), mockLoggingLevel(), log.NewNopLogger(), nil)
require.NoError(t, err)
defer services.StopAndAwaitTerminated(ctx, g) //nolint:errcheck
assert.False(t, g.ringLifecycler.IsRegistered())
bucketClient.MockIterWithCallback("", []string{"user-1", "user-2"}, nil, func() {
// During the initial sync, we expect the instance to always be in the JOINING
// state within the ring.
assert.True(t, g.ringLifecycler.IsRegistered())
assert.Equal(t, ring.JOINING, g.ringLifecycler.GetState())
assert.Equal(t, RingNumTokens, len(g.ringLifecycler.GetTokens()))
assert.Subset(t, g.ringLifecycler.GetTokens(), testData.initialTokens)
})
bucketClient.MockIter("user-1/", []string{}, nil)
bucketClient.MockIter("user-2/", []string{}, nil)
// Once successfully started, the instance should be ACTIVE in the ring.
require.NoError(t, services.StartAndAwaitRunning(ctx, g))
assert.True(t, g.ringLifecycler.IsRegistered())
assert.Equal(t, ring.ACTIVE, g.ringLifecycler.GetState())
assert.Equal(t, RingNumTokens, len(g.ringLifecycler.GetTokens()))
assert.Subset(t, g.ringLifecycler.GetTokens(), testData.initialTokens)
assert.NotNil(t, g.stores.getStore("user-1"))
assert.NotNil(t, g.stores.getStore("user-2"))
assert.Nil(t, g.stores.getStore("user-unknown"))
})
}
}
func TestStoreGateway_InitialSyncWithShardingDisabled(t *testing.T) {
ctx := context.Background()
gatewayCfg := mockGatewayConfig()
gatewayCfg.ShardingEnabled = false
storageCfg := mockStorageConfig(t)
bucketClient := &bucket.ClientMock{}
g, err := newStoreGateway(gatewayCfg, storageCfg, bucketClient, nil, defaultLimitsOverrides(t), mockLoggingLevel(), log.NewNopLogger(), nil)
require.NoError(t, err)
defer services.StopAndAwaitTerminated(ctx, g) //nolint:errcheck
bucketClient.MockIter("", []string{"user-1", "user-2"}, nil)
bucketClient.MockIter("user-1/", []string{}, nil)
bucketClient.MockIter("user-2/", []string{}, nil)
require.NoError(t, services.StartAndAwaitRunning(ctx, g))
assert.NotNil(t, g.stores.getStore("user-1"))
assert.NotNil(t, g.stores.getStore("user-2"))
assert.Nil(t, g.stores.getStore("user-unknown"))
}
func TestStoreGateway_InitialSyncFailure(t *testing.T) {
ctx := context.Background()
gatewayCfg := mockGatewayConfig()
gatewayCfg.ShardingEnabled = true
storageCfg := mockStorageConfig(t)
ringStore, closer := consul.NewInMemoryClient(ring.GetCodec(), log.NewNopLogger(), nil)
t.Cleanup(func() { assert.NoError(t, closer.Close()) })
bucketClient := &bucket.ClientMock{}
g, err := newStoreGateway(gatewayCfg, storageCfg, bucketClient, ringStore, defaultLimitsOverrides(t), mockLoggingLevel(), log.NewNopLogger(), nil)
require.NoError(t, err)
bucketClient.MockIter("", []string{}, errors.New("network error"))
require.NoError(t, g.StartAsync(ctx))
err = g.AwaitRunning(ctx)
assert.Error(t, err)
assert.Equal(t, services.Failed, g.State())
// We expect a clean shutdown, including unregistering the instance from the ring.
assert.False(t, g.ringLifecycler.IsRegistered())
}
// TestStoreGateway_InitialSyncWithWaitRingStability tests the store-gateway cold start case.
// When several store-gateways start up at once, we expect each store-gateway to only load
// their own blocks, regardless which store-gateway joined the ring first or last (even if starting
// at the same time, they will join the ring at a slightly different time).
func TestStoreGateway_InitialSyncWithWaitRingStability(t *testing.T) {
bucketClient, storageDir := cortex_testutil.PrepareFilesystemBucket(t)
// This tests uses real TSDB blocks. 24h time range, 2h block range period,
// 2 users = total (24 / 12) * 2 = 24 blocks.
numUsers := 2
numBlocks := numUsers * 12
now := time.Now()
mockTSDB(t, path.Join(storageDir, "user-1"), 24, 12, now.Add(-24*time.Hour).Unix()*1000, now.Unix()*1000)
mockTSDB(t, path.Join(storageDir, "user-2"), 24, 12, now.Add(-24*time.Hour).Unix()*1000, now.Unix()*1000)
// Write the bucket index.
for _, userID := range []string{"user-1", "user-2"} {
createBucketIndex(t, bucketClient, userID)
}
tests := map[string]struct {
shardingStrategy string
tenantShardSize int // Used only when the sharding strategy is shuffle-sharding.
replicationFactor int
numGateways int
expectedBlocksLoaded int
}{
"default sharding strategy, 1 gateway, RF = 1": {
shardingStrategy: util.ShardingStrategyDefault,
replicationFactor: 1,
numGateways: 1,
expectedBlocksLoaded: numBlocks,
},
"default sharding strategy, 2 gateways, RF = 1": {
shardingStrategy: util.ShardingStrategyDefault,
replicationFactor: 1,
numGateways: 2,
expectedBlocksLoaded: numBlocks, // blocks are sharded across gateways
},
"default sharding strategy, 3 gateways, RF = 2": {
shardingStrategy: util.ShardingStrategyDefault,
replicationFactor: 2,
numGateways: 3,
expectedBlocksLoaded: 2 * numBlocks, // blocks are replicated 2 times
},
"default sharding strategy, 5 gateways, RF = 3": {
shardingStrategy: util.ShardingStrategyDefault,
replicationFactor: 3,
numGateways: 5,
expectedBlocksLoaded: 3 * numBlocks, // blocks are replicated 3 times
},
"shuffle sharding strategy, 1 gateway, RF = 1, SS = 1": {
shardingStrategy: util.ShardingStrategyShuffle,
tenantShardSize: 1,
replicationFactor: 1,
numGateways: 1,
expectedBlocksLoaded: numBlocks,
},
"shuffle sharding strategy, 5 gateways, RF = 2, SS = 3": {
shardingStrategy: util.ShardingStrategyShuffle,
tenantShardSize: 3,
replicationFactor: 2,
numGateways: 5,
expectedBlocksLoaded: 2 * numBlocks, // blocks are replicated 2 times
},
"shuffle sharding strategy, 20 gateways, RF = 3, SS = 3": {
shardingStrategy: util.ShardingStrategyShuffle,
tenantShardSize: 3,
replicationFactor: 3,
numGateways: 20,
expectedBlocksLoaded: 3 * numBlocks, // blocks are replicated 3 times
},
}
for testName, testData := range tests {
for _, bucketIndexEnabled := range []bool{true, false} {
t.Run(fmt.Sprintf("%s (bucket index enabled = %v)", testName, bucketIndexEnabled), func(t *testing.T) {
// Randomise the seed but log it in case we need to reproduce the test on failure.
seed := time.Now().UnixNano()
rand.Seed(seed)
t.Log("random generator seed:", seed)
ctx := context.Background()
ringStore, closer := consul.NewInMemoryClientWithConfig(ring.GetCodec(), consul.Config{
MaxCasRetries: 20,
CasRetryDelay: 500 * time.Millisecond,
}, log.NewNopLogger(), nil)
t.Cleanup(func() { assert.NoError(t, closer.Close()) })
// Create the configured number of gateways.
var gateways []*StoreGateway
registries := util.NewUserRegistries()
for i := 1; i <= testData.numGateways; i++ {
instanceID := fmt.Sprintf("gateway-%d", i)
storageCfg := mockStorageConfig(t)
storageCfg.BucketStore.SyncInterval = time.Hour // Do not trigger the periodic sync in this test. We want the initial sync only.
storageCfg.BucketStore.BucketIndex.Enabled = bucketIndexEnabled
limits := defaultLimitsConfig()
gatewayCfg := mockGatewayConfig()
gatewayCfg.ShardingRing.ReplicationFactor = testData.replicationFactor
gatewayCfg.ShardingRing.InstanceID = instanceID
gatewayCfg.ShardingRing.InstanceAddr = fmt.Sprintf("127.0.0.%d", i)
gatewayCfg.ShardingRing.RingCheckPeriod = time.Hour // Do not check the ring topology changes in this test. We want the initial sync only.
gatewayCfg.ShardingRing.WaitStabilityMinDuration = 2 * time.Second
gatewayCfg.ShardingRing.WaitStabilityMaxDuration = 30 * time.Second
gatewayCfg.ShardingEnabled = true
gatewayCfg.ShardingStrategy = testData.shardingStrategy
limits.StoreGatewayTenantShardSize = testData.tenantShardSize
overrides, err := validation.NewOverrides(limits, nil)
require.NoError(t, err)
reg := prometheus.NewPedanticRegistry()
g, err := newStoreGateway(gatewayCfg, storageCfg, bucketClient, ringStore, overrides, mockLoggingLevel(), log.NewNopLogger(), reg)
require.NoError(t, err)
defer services.StopAndAwaitTerminated(ctx, g) //nolint:errcheck
gateways = append(gateways, g)
registries.AddUserRegistry(instanceID, reg)
}
// Start all gateways concurrently.
for _, g := range gateways {
require.NoError(t, g.StartAsync(ctx))
}
// Wait until all gateways are running.
for _, g := range gateways {
require.NoError(t, g.AwaitRunning(ctx))
}
// At this point we expect that all gateways have done the initial sync and
// they have synched only their own blocks, because they waited for a stable
// ring before starting the initial sync.
metrics := registries.BuildMetricFamiliesPerUser()
assert.Equal(t, float64(testData.expectedBlocksLoaded), metrics.GetSumOfGauges("cortex_bucket_store_blocks_loaded"))
assert.Equal(t, float64(2*testData.numGateways), metrics.GetSumOfGauges("cortex_bucket_stores_tenants_discovered"))
if testData.shardingStrategy == util.ShardingStrategyShuffle {
assert.Equal(t, float64(testData.tenantShardSize*numBlocks), metrics.GetSumOfGauges("cortex_blocks_meta_synced"))
assert.Equal(t, float64(testData.tenantShardSize*numUsers), metrics.GetSumOfGauges("cortex_bucket_stores_tenants_synced"))
} else {
assert.Equal(t, float64(testData.numGateways*numBlocks), metrics.GetSumOfGauges("cortex_blocks_meta_synced"))
assert.Equal(t, float64(testData.numGateways*numUsers), metrics.GetSumOfGauges("cortex_bucket_stores_tenants_synced"))
}
// We expect that all gateways have only run the initial sync and not the periodic one.
assert.Equal(t, float64(testData.numGateways), metrics.GetSumOfCounters("cortex_storegateway_bucket_sync_total"))
})
}
}
}
func TestStoreGateway_BlocksSyncWithDefaultSharding_RingTopologyChangedAfterScaleUp(t *testing.T) {
const (
numUsers = 2
numBlocks = numUsers * 12
shardingStrategy = util.ShardingStrategyDefault
replicationFactor = 3
numInitialGateways = 4
numScaleUpGateways = 6
expectedBlocksLoaded = 3 * numBlocks // blocks are replicated 3 times
)
bucketClient, storageDir := cortex_testutil.PrepareFilesystemBucket(t)
// This tests uses real TSDB blocks. 24h time range, 2h block range period,
// 2 users = total (24 / 12) * 2 = 24 blocks.
now := time.Now()
mockTSDB(t, path.Join(storageDir, "user-1"), 24, 12, now.Add(-24*time.Hour).Unix()*1000, now.Unix()*1000)
mockTSDB(t, path.Join(storageDir, "user-2"), 24, 12, now.Add(-24*time.Hour).Unix()*1000, now.Unix()*1000)
// Write the bucket index.
for _, userID := range []string{"user-1", "user-2"} {
createBucketIndex(t, bucketClient, userID)
}
// Randomise the seed but log it in case we need to reproduce the test on failure.
seed := time.Now().UnixNano()
rand.Seed(seed)
t.Log("random generator seed:", seed)
ctx := context.Background()
ringStore, closer := consul.NewInMemoryClient(ring.GetCodec(), log.NewNopLogger(), nil)
t.Cleanup(func() { assert.NoError(t, closer.Close()) })
// Create the configured number of gateways.
var initialGateways []*StoreGateway
initialRegistries := util.NewUserRegistries()
allRegistries := util.NewUserRegistries()
createStoreGateway := func(id int, waitStabilityMin time.Duration) (*StoreGateway, string, *prometheus.Registry) {
instanceID := fmt.Sprintf("gateway-%d", id)
storageCfg := mockStorageConfig(t)
storageCfg.BucketStore.SyncInterval = time.Hour // Do not trigger the periodic sync in this test. We want it to be triggered by ring topology changed.
storageCfg.BucketStore.BucketIndex.Enabled = true
limits := defaultLimitsConfig()
gatewayCfg := mockGatewayConfig()
gatewayCfg.ShardingRing.ReplicationFactor = replicationFactor
gatewayCfg.ShardingRing.InstanceID = instanceID
gatewayCfg.ShardingRing.InstanceAddr = fmt.Sprintf("127.0.0.%d", id)
gatewayCfg.ShardingRing.RingCheckPeriod = 100 * time.Millisecond // Check it continuously. Topology will change on scale up.
gatewayCfg.ShardingRing.WaitStabilityMinDuration = waitStabilityMin
gatewayCfg.ShardingRing.WaitStabilityMaxDuration = 30 * time.Second
gatewayCfg.ShardingEnabled = true
gatewayCfg.ShardingStrategy = shardingStrategy
overrides, err := validation.NewOverrides(limits, nil)
require.NoError(t, err)
reg := prometheus.NewPedanticRegistry()
g, err := newStoreGateway(gatewayCfg, storageCfg, bucketClient, ringStore, overrides, mockLoggingLevel(), log.NewNopLogger(), reg)
require.NoError(t, err)
return g, instanceID, reg
}
for i := 1; i <= numInitialGateways; i++ {
g, instanceID, reg := createStoreGateway(i, 2*time.Second)
initialGateways = append(initialGateways, g)
initialRegistries.AddUserRegistry(instanceID, reg)
allRegistries.AddUserRegistry(instanceID, reg)
}
// Start all gateways concurrently.
for _, g := range initialGateways {
require.NoError(t, g.StartAsync(ctx))
defer services.StopAndAwaitTerminated(ctx, g) //nolint:errcheck
}
// Wait until all gateways are running.
for _, g := range initialGateways {
require.NoError(t, g.AwaitRunning(ctx))
}
// At this point we expect that all gateways have done the initial sync and
// they have synched only their own blocks.
metrics := initialRegistries.BuildMetricFamiliesPerUser()
assert.Equal(t, float64(expectedBlocksLoaded), metrics.GetSumOfGauges("cortex_bucket_store_blocks_loaded"))
assert.Equal(t, float64(2*numInitialGateways), metrics.GetSumOfGauges("cortex_bucket_stores_tenants_discovered"))
assert.Equal(t, float64(numInitialGateways*numBlocks), metrics.GetSumOfGauges("cortex_blocks_meta_synced"))
assert.Equal(t, float64(numInitialGateways*numUsers), metrics.GetSumOfGauges("cortex_bucket_stores_tenants_synced"))
// Scale up store-gateways.
var scaleUpGateways []*StoreGateway
scaleUpRegistries := util.NewUserRegistries()
numAllGateways := numInitialGateways + numScaleUpGateways
for i := numInitialGateways + 1; i <= numAllGateways; i++ {
g, instanceID, reg := createStoreGateway(i, 10*time.Second) // Intentionally high "wait stability min duration".
scaleUpGateways = append(scaleUpGateways, g)
scaleUpRegistries.AddUserRegistry(instanceID, reg)
allRegistries.AddUserRegistry(instanceID, reg)
}
// Start all new gateways concurrently.
for _, g := range scaleUpGateways {
require.NoError(t, g.StartAsync(ctx))
defer services.StopAndAwaitTerminated(ctx, g) //nolint:errcheck
}
// Since we configured the new store-gateways with an high "wait stability min duration", we expect
// them to join the ring at start up (with JOINING state) but then wait at least the min duration
// before syncing blocks and becoming ACTIVE. This give us enough time to check how the initial
// store-gateways behaves with regards to blocks syncing while other replicas are JOINING.
// Wait until all the initial store-gateways sees all new store-gateways too.
test.Poll(t, 5*time.Second, float64(numAllGateways*numInitialGateways), func() interface{} {
metrics := initialRegistries.BuildMetricFamiliesPerUser()
return metrics.GetSumOfGauges("cortex_ring_members")
})
// We expect each block to be available for querying on at least 1 initial store-gateway.
for _, userID := range []string{"user-1", "user-2"} {
idx, err := bucketindex.ReadIndex(ctx, bucketClient, userID, nil, log.NewNopLogger())
require.NoError(t, err)
for _, block := range idx.Blocks {
queried := false
for _, g := range initialGateways {
req := &storepb.SeriesRequest{MinTime: math.MinInt64, MaxTime: math.MaxInt64}
srv := newBucketStoreSeriesServer(setUserIDToGRPCContext(ctx, userID))
require.NoError(t, g.Series(req, srv))
for _, b := range srv.Hints.QueriedBlocks {
if b.Id == block.ID.String() {
queried = true
}
}
}
assert.True(t, queried, "block %s has been successfully queried on initial store-gateways", block.ID.String())
}
}
// Wait until all new gateways are running.
for _, g := range scaleUpGateways {
require.NoError(t, g.AwaitRunning(ctx))
}
// At this point the new store-gateways are expected to be ACTIVE in the ring and all the initial
// store-gateways should unload blocks they don't own anymore.
test.Poll(t, 5*time.Second, float64(expectedBlocksLoaded), func() interface{} {
metrics := allRegistries.BuildMetricFamiliesPerUser()
return metrics.GetSumOfGauges("cortex_bucket_store_blocks_loaded")
})
}
func TestStoreGateway_ShouldSupportLoadRingTokensFromFile(t *testing.T) {
tests := map[string]struct {
storedTokens ring.Tokens
expectedNumTokens int
}{
"stored tokens are less than the configured ones": {
storedTokens: generateSortedTokens(RingNumTokens - 10),
expectedNumTokens: RingNumTokens,
},
"stored tokens are equal to the configured ones": {
storedTokens: generateSortedTokens(RingNumTokens),
expectedNumTokens: RingNumTokens,
},
"stored tokens are more then the configured ones": {
storedTokens: generateSortedTokens(RingNumTokens + 10),
expectedNumTokens: RingNumTokens + 10,
},
}
for testName, testData := range tests {
t.Run(testName, func(t *testing.T) {
tokensFile, err := ioutil.TempFile(os.TempDir(), "tokens-*")
require.NoError(t, err)
defer os.Remove(tokensFile.Name()) //nolint:errcheck
// Store some tokens to the file.
require.NoError(t, testData.storedTokens.StoreToFile(tokensFile.Name()))
ctx := context.Background()
gatewayCfg := mockGatewayConfig()
gatewayCfg.ShardingEnabled = true
gatewayCfg.ShardingRing.TokensFilePath = tokensFile.Name()
storageCfg := mockStorageConfig(t)
ringStore, closer := consul.NewInMemoryClient(ring.GetCodec(), log.NewNopLogger(), nil)
t.Cleanup(func() { assert.NoError(t, closer.Close()) })
bucketClient := &bucket.ClientMock{}
bucketClient.MockIter("", []string{}, nil)
g, err := newStoreGateway(gatewayCfg, storageCfg, bucketClient, ringStore, defaultLimitsOverrides(t), mockLoggingLevel(), log.NewNopLogger(), nil)
require.NoError(t, err)
defer services.StopAndAwaitTerminated(ctx, g) //nolint:errcheck
assert.False(t, g.ringLifecycler.IsRegistered())
require.NoError(t, services.StartAndAwaitRunning(ctx, g))
assert.True(t, g.ringLifecycler.IsRegistered())
assert.Equal(t, ring.ACTIVE, g.ringLifecycler.GetState())
assert.Len(t, g.ringLifecycler.GetTokens(), testData.expectedNumTokens)
assert.Subset(t, g.ringLifecycler.GetTokens(), testData.storedTokens)
})
}
}
func TestStoreGateway_SyncOnRingTopologyChanged(t *testing.T) {
registeredAt := time.Now()
tests := map[string]struct {
setupRing func(desc *ring.Desc)
updateRing func(desc *ring.Desc)
expectedSync bool
}{
"should sync when an instance is added to the ring": {
setupRing: func(desc *ring.Desc) {
desc.AddIngester("instance-1", "127.0.0.1", "", ring.Tokens{1, 2, 3}, ring.ACTIVE, registeredAt)
},
updateRing: func(desc *ring.Desc) {
desc.AddIngester("instance-2", "127.0.0.2", "", ring.Tokens{4, 5, 6}, ring.ACTIVE, registeredAt)
},
expectedSync: true,
},
"should sync when an instance is removed from the ring": {
setupRing: func(desc *ring.Desc) {
desc.AddIngester("instance-1", "127.0.0.1", "", ring.Tokens{1, 2, 3}, ring.ACTIVE, registeredAt)
desc.AddIngester("instance-2", "127.0.0.2", "", ring.Tokens{4, 5, 6}, ring.ACTIVE, registeredAt)
},
updateRing: func(desc *ring.Desc) {
desc.RemoveIngester("instance-1")
},
expectedSync: true,
},
"should sync when an instance changes state": {
setupRing: func(desc *ring.Desc) {
desc.AddIngester("instance-1", "127.0.0.1", "", ring.Tokens{1, 2, 3}, ring.ACTIVE, registeredAt)
desc.AddIngester("instance-2", "127.0.0.2", "", ring.Tokens{4, 5, 6}, ring.JOINING, registeredAt)
},
updateRing: func(desc *ring.Desc) {
instance := desc.Ingesters["instance-2"]
instance.State = ring.ACTIVE
desc.Ingesters["instance-2"] = instance
},
expectedSync: true,
},
"should sync when an healthy instance becomes unhealthy": {
setupRing: func(desc *ring.Desc) {
desc.AddIngester("instance-1", "127.0.0.1", "", ring.Tokens{1, 2, 3}, ring.ACTIVE, registeredAt)
desc.AddIngester("instance-2", "127.0.0.2", "", ring.Tokens{4, 5, 6}, ring.ACTIVE, registeredAt)
},
updateRing: func(desc *ring.Desc) {
instance := desc.Ingesters["instance-2"]
instance.Timestamp = time.Now().Add(-time.Hour).Unix()
desc.Ingesters["instance-2"] = instance
},
expectedSync: true,
},
"should sync when an unhealthy instance becomes healthy": {
setupRing: func(desc *ring.Desc) {
desc.AddIngester("instance-1", "127.0.0.1", "", ring.Tokens{1, 2, 3}, ring.ACTIVE, registeredAt)
instance := desc.AddIngester("instance-2", "127.0.0.2", "", ring.Tokens{4, 5, 6}, ring.ACTIVE, registeredAt)
instance.Timestamp = time.Now().Add(-time.Hour).Unix()
desc.Ingesters["instance-2"] = instance
},
updateRing: func(desc *ring.Desc) {
instance := desc.Ingesters["instance-2"]
instance.Timestamp = time.Now().Unix()
desc.Ingesters["instance-2"] = instance
},
expectedSync: true,
},
"should NOT sync when an instance updates the heartbeat": {
setupRing: func(desc *ring.Desc) {
desc.AddIngester("instance-1", "127.0.0.1", "", ring.Tokens{1, 2, 3}, ring.ACTIVE, registeredAt)
desc.AddIngester("instance-2", "127.0.0.2", "", ring.Tokens{4, 5, 6}, ring.ACTIVE, registeredAt)
},
updateRing: func(desc *ring.Desc) {
instance := desc.Ingesters["instance-2"]
instance.Timestamp = time.Now().Add(time.Second).Unix()
desc.Ingesters["instance-2"] = instance
},
expectedSync: false,
},
"should NOT sync when an instance is auto-forgotten in the ring but was already unhealthy in the previous state": {
setupRing: func(desc *ring.Desc) {
desc.AddIngester("instance-1", "127.0.0.1", "", ring.Tokens{1, 2, 3}, ring.ACTIVE, registeredAt)
desc.AddIngester("instance-2", "127.0.0.2", "", ring.Tokens{4, 5, 6}, ring.ACTIVE, registeredAt)
// Set it already unhealthy.
instance := desc.Ingesters["instance-2"]
instance.Timestamp = time.Now().Add(-time.Hour).Unix()
desc.Ingesters["instance-2"] = instance
},
updateRing: func(desc *ring.Desc) {
// Remove the unhealthy instance from the ring.
desc.RemoveIngester("instance-2")
},
expectedSync: false,
},
}
for testName, testData := range tests {
t.Run(testName, func(t *testing.T) {
ctx := context.Background()
gatewayCfg := mockGatewayConfig()
gatewayCfg.ShardingEnabled = true
gatewayCfg.ShardingRing.RingCheckPeriod = 100 * time.Millisecond
storageCfg := mockStorageConfig(t)
storageCfg.BucketStore.SyncInterval = time.Hour // Do not trigger the periodic sync in this test.
reg := prometheus.NewPedanticRegistry()
ringStore, closer := consul.NewInMemoryClient(ring.GetCodec(), log.NewNopLogger(), nil)
t.Cleanup(func() { assert.NoError(t, closer.Close()) })
bucketClient := &bucket.ClientMock{}
bucketClient.MockIter("", []string{}, nil)
g, err := newStoreGateway(gatewayCfg, storageCfg, bucketClient, ringStore, defaultLimitsOverrides(t), mockLoggingLevel(), log.NewNopLogger(), reg)
require.NoError(t, err)
// Store the initial ring state before starting the gateway.
require.NoError(t, ringStore.CAS(ctx, RingKey, func(in interface{}) (interface{}, bool, error) {
ringDesc := ring.GetOrCreateRingDesc(in)
testData.setupRing(ringDesc)
return ringDesc, true, nil
}))
require.NoError(t, services.StartAndAwaitRunning(ctx, g))
defer services.StopAndAwaitTerminated(ctx, g) //nolint:errcheck
// Assert on the initial state.
regs := util.NewUserRegistries()
regs.AddUserRegistry("test", reg)
metrics := regs.BuildMetricFamiliesPerUser()
assert.Equal(t, float64(1), metrics.GetSumOfCounters("cortex_storegateway_bucket_sync_total"))
// Change the ring topology.
require.NoError(t, ringStore.CAS(ctx, RingKey, func(in interface{}) (interface{}, bool, error) {
ringDesc := ring.GetOrCreateRingDesc(in)
testData.updateRing(ringDesc)
return ringDesc, true, nil
}))
// Assert whether the sync triggered or not.
if testData.expectedSync {
test.Poll(t, time.Second, float64(2), func() interface{} {
metrics := regs.BuildMetricFamiliesPerUser()
return metrics.GetSumOfCounters("cortex_storegateway_bucket_sync_total")
})
} else {
// Give some time to the store-gateway to trigger the sync (if any).
time.Sleep(250 * time.Millisecond)
metrics := regs.BuildMetricFamiliesPerUser()
assert.Equal(t, float64(1), metrics.GetSumOfCounters("cortex_storegateway_bucket_sync_total"))
}
})
}
}
func TestStoreGateway_RingLifecyclerShouldAutoForgetUnhealthyInstances(t *testing.T) {
const unhealthyInstanceID = "unhealthy-id"
const heartbeatTimeout = time.Minute
ctx := context.Background()
gatewayCfg := mockGatewayConfig()
gatewayCfg.ShardingEnabled = true
gatewayCfg.ShardingRing.HeartbeatPeriod = 100 * time.Millisecond
gatewayCfg.ShardingRing.HeartbeatTimeout = heartbeatTimeout
storageCfg := mockStorageConfig(t)
ringStore, closer := consul.NewInMemoryClient(ring.GetCodec(), log.NewNopLogger(), nil)
t.Cleanup(func() { assert.NoError(t, closer.Close()) })
bucketClient := &bucket.ClientMock{}
bucketClient.MockIter("", []string{}, nil)
g, err := newStoreGateway(gatewayCfg, storageCfg, bucketClient, ringStore, defaultLimitsOverrides(t), mockLoggingLevel(), log.NewNopLogger(), nil)
require.NoError(t, err)
require.NoError(t, services.StartAndAwaitRunning(ctx, g))
defer services.StopAndAwaitTerminated(ctx, g) //nolint:errcheck
// Add an unhealthy instance to the ring.
require.NoError(t, ringStore.CAS(ctx, RingKey, func(in interface{}) (interface{}, bool, error) {
ringDesc := ring.GetOrCreateRingDesc(in)
instance := ringDesc.AddIngester(unhealthyInstanceID, "1.1.1.1", "", generateSortedTokens(RingNumTokens), ring.ACTIVE, time.Now())
instance.Timestamp = time.Now().Add(-(ringAutoForgetUnhealthyPeriods + 1) * heartbeatTimeout).Unix()
ringDesc.Ingesters[unhealthyInstanceID] = instance
return ringDesc, true, nil
}))
// Ensure the unhealthy instance is removed from the ring.
test.Poll(t, time.Second, false, func() interface{} {
d, err := ringStore.Get(ctx, RingKey)
if err != nil {
return err
}
_, ok := ring.GetOrCreateRingDesc(d).Ingesters[unhealthyInstanceID]
return ok
})
}
func TestStoreGateway_SeriesQueryingShouldRemoveExternalLabels(t *testing.T) {
ctx := context.Background()
logger := log.NewNopLogger()
userID := "user-1"
storageDir, err := ioutil.TempDir(os.TempDir(), "")
require.NoError(t, err)
defer os.RemoveAll(storageDir) //nolint:errcheck
// Generate 2 TSDB blocks with the same exact series (and data points).
numSeries := 2
now := time.Now()
minT := now.Add(-1*time.Hour).Unix() * 1000
maxT := now.Unix() * 1000
step := (maxT - minT) / int64(numSeries)
mockTSDB(t, path.Join(storageDir, userID), numSeries, 0, minT, maxT)
mockTSDB(t, path.Join(storageDir, userID), numSeries, 0, minT, maxT)
bucketClient, err := filesystem.NewBucketClient(filesystem.Config{Directory: storageDir})
require.NoError(t, err)
createBucketIndex(t, bucketClient, userID)
// Find the created blocks (we expect 2).
var blockIDs []string
require.NoError(t, bucketClient.Iter(ctx, "user-1/", func(key string) error {
if _, ok := block.IsBlockDir(key); ok {
blockIDs = append(blockIDs, strings.TrimSuffix(strings.TrimPrefix(key, userID+"/"), "/"))
}
return nil
}))
require.Len(t, blockIDs, 2)
// Inject different external labels for each block.
for idx, blockID := range blockIDs {
meta := metadata.Thanos{
Labels: map[string]string{
cortex_tsdb.TenantIDExternalLabel: userID,
cortex_tsdb.IngesterIDExternalLabel: fmt.Sprintf("ingester-%d", idx),
cortex_tsdb.ShardIDExternalLabel: fmt.Sprintf("shard-%d", idx),
},
Source: metadata.TestSource,
}
_, err := metadata.InjectThanos(logger, filepath.Join(storageDir, userID, blockID), meta, nil)
require.NoError(t, err)
}
for _, bucketIndexEnabled := range []bool{true, false} {
t.Run(fmt.Sprintf("bucket index enabled = %v", bucketIndexEnabled), func(t *testing.T) {
// Create a store-gateway used to query back the series from the blocks.
gatewayCfg := mockGatewayConfig()
gatewayCfg.ShardingEnabled = false
storageCfg := mockStorageConfig(t)
storageCfg.BucketStore.BucketIndex.Enabled = bucketIndexEnabled
g, err := newStoreGateway(gatewayCfg, storageCfg, bucketClient, nil, defaultLimitsOverrides(t), mockLoggingLevel(), logger, nil)
require.NoError(t, err)
require.NoError(t, services.StartAndAwaitRunning(ctx, g))
defer services.StopAndAwaitTerminated(ctx, g) //nolint:errcheck
// Query back all series.
req := &storepb.SeriesRequest{
MinTime: minT,
MaxTime: maxT,
Matchers: []storepb.LabelMatcher{
{Type: storepb.LabelMatcher_RE, Name: "__name__", Value: ".*"},
},
}
srv := newBucketStoreSeriesServer(setUserIDToGRPCContext(ctx, userID))
err = g.Series(req, srv)
require.NoError(t, err)
assert.Empty(t, srv.Warnings)
assert.Len(t, srv.SeriesSet, numSeries)
for seriesID := 0; seriesID < numSeries; seriesID++ {
actual := srv.SeriesSet[seriesID]
// Ensure Cortex external labels have been removed.
assert.Equal(t, []labelpb.ZLabel{{Name: "series_id", Value: strconv.Itoa(seriesID)}}, actual.Labels)
// Ensure samples have been correctly queried. The Thanos store also deduplicate samples
// in most cases, but it's not strictly required guaranteeing deduplication at this stage.
samples, err := readSamplesFromChunks(actual.Chunks)
require.NoError(t, err)
assert.Equal(t, []sample{
{ts: minT + (step * int64(seriesID)), value: float64(seriesID)},
}, samples)
}
})
}
}
func TestStoreGateway_SeriesQueryingShouldEnforceMaxChunksPerQueryLimit(t *testing.T) {
const chunksQueried = 10
tests := map[string]struct {
limit int
expectedErr error
}{
"no limit enforced if zero": {
limit: 0,
expectedErr: nil,
},
"should return NO error if the actual number of queried chunks is <= limit": {
limit: chunksQueried,
expectedErr: nil,
},
"should return error if the actual number of queried chunks is > limit": {
limit: chunksQueried - 1,
expectedErr: status.Error(http.StatusUnprocessableEntity, fmt.Sprintf("exceeded chunks limit: rpc error: code = Code(422) desc = limit %d violated (got %d)", chunksQueried-1, chunksQueried)),
},
}
ctx := context.Background()
logger := log.NewNopLogger()
userID := "user-1"
storageDir, err := ioutil.TempDir(os.TempDir(), "")
require.NoError(t, err)
defer os.RemoveAll(storageDir) //nolint:errcheck
// Generate 1 TSDB block with chunksQueried series. Since each mocked series contains only 1 sample,
// it will also only have 1 chunk.
now := time.Now()
minT := now.Add(-1*time.Hour).Unix() * 1000
maxT := now.Unix() * 1000
mockTSDB(t, path.Join(storageDir, userID), chunksQueried, 0, minT, maxT)
bucketClient, err := filesystem.NewBucketClient(filesystem.Config{Directory: storageDir})
require.NoError(t, err)
// Prepare the request to query back all series (1 chunk per series in this test).
req := &storepb.SeriesRequest{
MinTime: minT,
MaxTime: maxT,
Matchers: []storepb.LabelMatcher{
{Type: storepb.LabelMatcher_RE, Name: "__name__", Value: ".*"},
},
}
for testName, testData := range tests {
t.Run(testName, func(t *testing.T) {
// Customise the limits.
limits := defaultLimitsConfig()
limits.MaxChunksPerQueryFromStore = testData.limit
overrides, err := validation.NewOverrides(limits, nil)
require.NoError(t, err)
// Create a store-gateway used to query back the series from the blocks.
gatewayCfg := mockGatewayConfig()
gatewayCfg.ShardingEnabled = false
storageCfg := mockStorageConfig(t)
g, err := newStoreGateway(gatewayCfg, storageCfg, bucketClient, nil, overrides, mockLoggingLevel(), logger, nil)
require.NoError(t, err)
require.NoError(t, services.StartAndAwaitRunning(ctx, g))
defer services.StopAndAwaitTerminated(ctx, g) //nolint:errcheck
// Query back all the series (1 chunk per series in this test).
srv := newBucketStoreSeriesServer(setUserIDToGRPCContext(ctx, userID))
err = g.Series(req, srv)
if testData.expectedErr != nil {
require.Error(t, err)
assert.IsType(t, testData.expectedErr, err)
s1, ok := status.FromError(errors.Cause(err))
assert.True(t, ok)
s2, ok := status.FromError(errors.Cause(testData.expectedErr))
assert.True(t, ok)
assert.True(t, strings.Contains(s1.Message(), s2.Message()))
assert.Equal(t, s1.Code(), s2.Code())
} else {
require.NoError(t, err)
assert.Empty(t, srv.Warnings)
assert.Len(t, srv.SeriesSet, chunksQueried)
}
})
}
}
func mockGatewayConfig() Config {
cfg := Config{}
flagext.DefaultValues(&cfg)
cfg.ShardingRing.InstanceID = "test"
cfg.ShardingRing.InstanceAddr = "127.0.0.1"
cfg.ShardingRing.WaitStabilityMinDuration = 0
cfg.ShardingRing.WaitStabilityMaxDuration = 0
return cfg
}
func mockStorageConfig(t *testing.T) cortex_tsdb.BlocksStorageConfig {
tmpDir, err := ioutil.TempDir(os.TempDir(), "store-gateway-test-*")
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, os.RemoveAll(tmpDir))
})
cfg := cortex_tsdb.BlocksStorageConfig{}
flagext.DefaultValues(&cfg)
cfg.BucketStore.ConsistencyDelay = 0
cfg.BucketStore.SyncDir = tmpDir