|
| 1 | +// +build requires_docker |
| 2 | + |
| 3 | +package integration |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "strconv" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/prometheus/prometheus/pkg/labels" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + |
| 14 | + "github.com/cortexproject/cortex/integration/e2e" |
| 15 | + e2edb "github.com/cortexproject/cortex/integration/e2e/db" |
| 16 | + "github.com/cortexproject/cortex/integration/e2ecortex" |
| 17 | +) |
| 18 | + |
| 19 | +func TestIngesterSharding(t *testing.T) { |
| 20 | + const numSeriesToPush = 1000 |
| 21 | + |
| 22 | + tests := map[string]struct { |
| 23 | + shardingStrategy string |
| 24 | + tenantShardSize int |
| 25 | + expectedIngestersWithSeries int |
| 26 | + }{ |
| 27 | + "default sharding strategy should spread series across all ingesters": { |
| 28 | + shardingStrategy: "default", |
| 29 | + tenantShardSize: 2, // Ignored by default strategy. |
| 30 | + expectedIngestersWithSeries: 3, |
| 31 | + }, |
| 32 | + "shuffle-sharding strategy should spread series across the configured shard size number of ingesters": { |
| 33 | + shardingStrategy: "shuffle-sharding", |
| 34 | + tenantShardSize: 2, |
| 35 | + expectedIngestersWithSeries: 2, |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | + for testName, testData := range tests { |
| 40 | + t.Run(testName, func(t *testing.T) { |
| 41 | + s, err := e2e.NewScenario(networkName) |
| 42 | + require.NoError(t, err) |
| 43 | + defer s.Close() |
| 44 | + |
| 45 | + flags := BlocksStorageFlags |
| 46 | + flags["-distributor.sharding-strategy"] = testData.shardingStrategy |
| 47 | + flags["-distributor.ingestion-tenant-shard-size"] = strconv.Itoa(testData.tenantShardSize) |
| 48 | + |
| 49 | + // Start dependencies. |
| 50 | + consul := e2edb.NewConsul() |
| 51 | + minio := e2edb.NewMinio(9000, flags["-blocks-storage.s3.bucket-name"]) |
| 52 | + require.NoError(t, s.StartAndWaitReady(consul, minio)) |
| 53 | + |
| 54 | + // Start Cortex components. |
| 55 | + distributor := e2ecortex.NewDistributor("distributor", consul.NetworkHTTPEndpoint(), flags, "") |
| 56 | + ingester1 := e2ecortex.NewIngester("ingester-1", consul.NetworkHTTPEndpoint(), flags, "") |
| 57 | + ingester2 := e2ecortex.NewIngester("ingester-2", consul.NetworkHTTPEndpoint(), flags, "") |
| 58 | + ingester3 := e2ecortex.NewIngester("ingester-3", consul.NetworkHTTPEndpoint(), flags, "") |
| 59 | + querier := e2ecortex.NewQuerier("querier", consul.NetworkHTTPEndpoint(), flags, "") |
| 60 | + require.NoError(t, s.StartAndWaitReady(distributor, ingester1, ingester2, ingester3, querier)) |
| 61 | + |
| 62 | + // Wait until distributor and queriers have updated the ring. |
| 63 | + require.NoError(t, distributor.WaitSumMetricsWithOptions(e2e.Equals(3), []string{"cortex_ring_members"}, e2e.WithLabelMatchers( |
| 64 | + labels.MustNewMatcher(labels.MatchEqual, "name", "ingester"), |
| 65 | + labels.MustNewMatcher(labels.MatchEqual, "state", "ACTIVE")))) |
| 66 | + |
| 67 | + require.NoError(t, querier.WaitSumMetricsWithOptions(e2e.Equals(3), []string{"cortex_ring_members"}, e2e.WithLabelMatchers( |
| 68 | + labels.MustNewMatcher(labels.MatchEqual, "name", "ingester"), |
| 69 | + labels.MustNewMatcher(labels.MatchEqual, "state", "ACTIVE")))) |
| 70 | + |
| 71 | + // Push series. |
| 72 | + now := time.Now() |
| 73 | + |
| 74 | + client, err := e2ecortex.NewClient(distributor.HTTPEndpoint(), "", "", "", userID) |
| 75 | + require.NoError(t, err) |
| 76 | + |
| 77 | + for i := 1; i <= numSeriesToPush; i++ { |
| 78 | + series, _ := generateSeries(fmt.Sprintf("series_%d", i), now) |
| 79 | + res, err := client.Push(series) |
| 80 | + require.NoError(t, err) |
| 81 | + require.Equal(t, 200, res.StatusCode) |
| 82 | + } |
| 83 | + |
| 84 | + // Extract metrics from ingesters. |
| 85 | + numIngestersWithSeries := 0 |
| 86 | + totalIngestedSeries := 0 |
| 87 | + |
| 88 | + for _, ing := range []*e2ecortex.CortexService{ingester1, ingester2, ingester3} { |
| 89 | + values, err := ing.SumMetrics([]string{"cortex_ingester_memory_series"}) |
| 90 | + require.NoError(t, err) |
| 91 | + |
| 92 | + numMemorySeries := e2e.SumValues(values) |
| 93 | + totalIngestedSeries += int(numMemorySeries) |
| 94 | + if numMemorySeries > 0 { |
| 95 | + numIngestersWithSeries++ |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + require.Equal(t, testData.expectedIngestersWithSeries, numIngestersWithSeries) |
| 100 | + require.Equal(t, numSeriesToPush, totalIngestedSeries) |
| 101 | + |
| 102 | + // Ensure no service-specific metrics prefix is used by the wrong service. |
| 103 | + assertServiceMetricsPrefixes(t, Distributor, distributor) |
| 104 | + assertServiceMetricsPrefixes(t, Ingester, ingester1) |
| 105 | + assertServiceMetricsPrefixes(t, Ingester, ingester2) |
| 106 | + assertServiceMetricsPrefixes(t, Ingester, ingester3) |
| 107 | + assertServiceMetricsPrefixes(t, Querier, querier) |
| 108 | + }) |
| 109 | + } |
| 110 | +} |
0 commit comments