|
| 1 | +//go:build requires_docker |
| 2 | +// +build requires_docker |
| 3 | + |
| 4 | +package integration |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "math/rand" |
| 9 | + "strconv" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/prometheus/prometheus/model/labels" |
| 14 | + "github.com/prometheus/prometheus/prompb" |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | + |
| 18 | + "github.com/cortexproject/cortex/integration/e2e" |
| 19 | + e2edb "github.com/cortexproject/cortex/integration/e2e/db" |
| 20 | + "github.com/cortexproject/cortex/integration/e2ecortex" |
| 21 | +) |
| 22 | + |
| 23 | +func TestIngesterStreamPushConnection(t *testing.T) { |
| 24 | + |
| 25 | + s, err := e2e.NewScenario(networkName) |
| 26 | + require.NoError(t, err) |
| 27 | + defer s.Close() |
| 28 | + |
| 29 | + maxGlobalSeriesPerMetric := 300 |
| 30 | + maxGlobalSeriesPerTenant := 1000 |
| 31 | + |
| 32 | + flags := BlocksStorageFlags() |
| 33 | + flags["-distributor.use-stream-push"] = "true" |
| 34 | + flags["-distributor.replication-factor"] = "1" |
| 35 | + flags["-distributor.shard-by-all-labels"] = "true" |
| 36 | + flags["-distributor.sharding-strategy"] = "shuffle-sharding" |
| 37 | + flags["-distributor.ingestion-tenant-shard-size"] = "1" |
| 38 | + flags["-ingester.max-series-per-user"] = "0" |
| 39 | + flags["-ingester.max-series-per-metric"] = "0" |
| 40 | + flags["-ingester.max-global-series-per-user"] = strconv.Itoa(maxGlobalSeriesPerTenant) |
| 41 | + flags["-ingester.max-global-series-per-metric"] = strconv.Itoa(maxGlobalSeriesPerMetric) |
| 42 | + flags["-ingester.heartbeat-period"] = "1s" |
| 43 | + |
| 44 | + // Start dependencies. |
| 45 | + consul := e2edb.NewConsul() |
| 46 | + minio := e2edb.NewMinio(9000, flags["-blocks-storage.s3.bucket-name"]) |
| 47 | + require.NoError(t, s.StartAndWaitReady(consul, minio)) |
| 48 | + |
| 49 | + // Start Cortex components. |
| 50 | + distributor := e2ecortex.NewDistributor("distributor", e2ecortex.RingStoreConsul, consul.NetworkHTTPEndpoint(), flags, "") |
| 51 | + ingester1 := e2ecortex.NewIngester("ingester-1", e2ecortex.RingStoreConsul, consul.NetworkHTTPEndpoint(), flags, "") |
| 52 | + ingester2 := e2ecortex.NewIngester("ingester-2", e2ecortex.RingStoreConsul, consul.NetworkHTTPEndpoint(), flags, "") |
| 53 | + ingester3 := e2ecortex.NewIngester("ingester-3", e2ecortex.RingStoreConsul, consul.NetworkHTTPEndpoint(), flags, "") |
| 54 | + require.NoError(t, s.StartAndWaitReady(distributor, ingester1, ingester2, ingester3)) |
| 55 | + |
| 56 | + // Wait until distributor has updated the ring. |
| 57 | + require.NoError(t, distributor.WaitSumMetricsWithOptions(e2e.Equals(3), []string{"cortex_ring_members"}, e2e.WithLabelMatchers( |
| 58 | + labels.MustNewMatcher(labels.MatchEqual, "name", "ingester"), |
| 59 | + labels.MustNewMatcher(labels.MatchEqual, "state", "ACTIVE")))) |
| 60 | + |
| 61 | + // Wait until ingesters have heartbeated the ring after all ingesters were active, |
| 62 | + // in order to update the number of instances. Since we have no metric, we have to |
| 63 | + // rely on a ugly sleep. |
| 64 | + time.Sleep(2 * time.Second) |
| 65 | + |
| 66 | + now := time.Now() |
| 67 | + client, err := e2ecortex.NewClient(distributor.HTTPEndpoint(), "", "", "", userID) |
| 68 | + require.NoError(t, err) |
| 69 | + |
| 70 | + numSeriesWithSameMetricName := 0 |
| 71 | + numSeriesTotal := 0 |
| 72 | + maxErrorsBeforeStop := 100 |
| 73 | + |
| 74 | + // Try to push as many series with the same metric name as we can. |
| 75 | + for i, errs := 0, 0; i < 10000; i++ { |
| 76 | + series, _ := generateSeries("test_limit_per_metric", now, prompb.Label{ |
| 77 | + Name: "cardinality", |
| 78 | + Value: strconv.Itoa(rand.Int()), |
| 79 | + }) |
| 80 | + |
| 81 | + res, err := client.Push(series) |
| 82 | + require.NoError(t, err) |
| 83 | + |
| 84 | + if res.StatusCode == 200 { |
| 85 | + numSeriesTotal++ |
| 86 | + numSeriesWithSameMetricName++ |
| 87 | + } else if errs++; errs >= maxErrorsBeforeStop { |
| 88 | + break |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // Try to push as many series with the different metric name as we can. |
| 93 | + for i, errs := 0, 0; i < 10000; i++ { |
| 94 | + series, _ := generateSeries(fmt.Sprintf("test_limit_per_tenant_%d", rand.Int()), now) |
| 95 | + res, err := client.Push(series) |
| 96 | + require.NoError(t, err) |
| 97 | + |
| 98 | + if res.StatusCode == 200 { |
| 99 | + numSeriesTotal++ |
| 100 | + } else if errs++; errs >= maxErrorsBeforeStop { |
| 101 | + break |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + // We expect the number of series we've been successfully pushed to be around |
| 106 | + // the limit. Due to how the global limit implementation works (lack of centralised |
| 107 | + // coordination) the actual number of written series could be slightly different |
| 108 | + // than the global limit, so we allow a 10% difference. |
| 109 | + delta := 0.1 |
| 110 | + assert.InDelta(t, maxGlobalSeriesPerMetric, numSeriesWithSameMetricName, float64(maxGlobalSeriesPerMetric)*delta) |
| 111 | + assert.InDelta(t, maxGlobalSeriesPerTenant, numSeriesTotal, float64(maxGlobalSeriesPerTenant)*delta) |
| 112 | + |
| 113 | + // Ensure no service-specific metrics prefix is used by the wrong service. |
| 114 | + assertServiceMetricsPrefixes(t, Distributor, distributor) |
| 115 | + assertServiceMetricsPrefixes(t, Ingester, ingester1) |
| 116 | + assertServiceMetricsPrefixes(t, Ingester, ingester2) |
| 117 | + assertServiceMetricsPrefixes(t, Ingester, ingester3) |
| 118 | +} |
0 commit comments