|
| 1 | +//go:build !race |
| 2 | + |
| 3 | +package ingester |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + "math" |
| 9 | + "strconv" |
| 10 | + "sync" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/prometheus/client_golang/prometheus" |
| 15 | + "github.com/prometheus/prometheus/model/labels" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | + "github.com/weaveworks/common/user" |
| 18 | + |
| 19 | + "github.com/cortexproject/cortex/pkg/cortexpb" |
| 20 | + "github.com/cortexproject/cortex/pkg/ingester/client" |
| 21 | + "github.com/cortexproject/cortex/pkg/ring" |
| 22 | + "github.com/cortexproject/cortex/pkg/util/services" |
| 23 | + "github.com/cortexproject/cortex/pkg/util/test" |
| 24 | +) |
| 25 | + |
| 26 | +// Running this test without race check as there is a known prometheus race condition. |
| 27 | +// See https://github.com/prometheus/prometheus/pull/15141 and https://github.com/prometheus/prometheus/pull/15316 |
| 28 | +func TestExpandedCachePostings_Race(t *testing.T) { |
| 29 | + cfg := defaultIngesterTestConfig(t) |
| 30 | + cfg.BlocksStorageConfig.TSDB.BlockRanges = []time.Duration{2 * time.Hour} |
| 31 | + cfg.LifecyclerConfig.JoinAfter = 0 |
| 32 | + cfg.BlocksStorageConfig.TSDB.PostingsCache.Head.Enabled = true |
| 33 | + |
| 34 | + r := prometheus.NewRegistry() |
| 35 | + i, err := prepareIngesterWithBlocksStorage(t, cfg, r) |
| 36 | + require.NoError(t, err) |
| 37 | + require.NoError(t, services.StartAndAwaitRunning(context.Background(), i)) |
| 38 | + defer services.StopAndAwaitTerminated(context.Background(), i) //nolint:errcheck |
| 39 | + |
| 40 | + // Wait until the ingester is ACTIVE |
| 41 | + test.Poll(t, 100*time.Millisecond, ring.ACTIVE, func() interface{} { |
| 42 | + return i.lifecycler.GetState() |
| 43 | + }) |
| 44 | + |
| 45 | + ctx := user.InjectOrgID(context.Background(), "test") |
| 46 | + |
| 47 | + wg := sync.WaitGroup{} |
| 48 | + labelNames := 100 |
| 49 | + seriesPerLabelName := 200 |
| 50 | + |
| 51 | + for j := 0; j < labelNames; j++ { |
| 52 | + metricName := fmt.Sprintf("test_metric_%d", j) |
| 53 | + wg.Add(seriesPerLabelName * 2) |
| 54 | + for k := 0; k < seriesPerLabelName; k++ { |
| 55 | + go func() { |
| 56 | + defer wg.Done() |
| 57 | + _, err := i.Push(ctx, cortexpb.ToWriteRequest( |
| 58 | + []labels.Labels{labels.FromStrings(labels.MetricName, metricName, "k", strconv.Itoa(k))}, |
| 59 | + []cortexpb.Sample{{Value: 1, TimestampMs: 9}}, nil, nil, cortexpb.API)) |
| 60 | + require.NoError(t, err) |
| 61 | + }() |
| 62 | + |
| 63 | + go func() { |
| 64 | + defer wg.Done() |
| 65 | + err := i.QueryStream(&client.QueryRequest{ |
| 66 | + StartTimestampMs: 0, |
| 67 | + EndTimestampMs: math.MaxInt64, |
| 68 | + Matchers: []*client.LabelMatcher{{Type: client.EQUAL, Name: labels.MetricName, Value: metricName}}, |
| 69 | + }, &mockQueryStreamServer{ctx: ctx}) |
| 70 | + require.NoError(t, err) |
| 71 | + }() |
| 72 | + } |
| 73 | + |
| 74 | + wg.Wait() |
| 75 | + |
| 76 | + s := &mockQueryStreamServer{ctx: ctx} |
| 77 | + err = i.QueryStream(&client.QueryRequest{ |
| 78 | + StartTimestampMs: 0, |
| 79 | + EndTimestampMs: math.MaxInt64, |
| 80 | + Matchers: []*client.LabelMatcher{{Type: client.EQUAL, Name: labels.MetricName, Value: metricName}}, |
| 81 | + }, s) |
| 82 | + require.NoError(t, err) |
| 83 | + |
| 84 | + set, err := seriesSetFromResponseStream(s) |
| 85 | + require.NoError(t, err) |
| 86 | + res, err := client.MatrixFromSeriesSet(set) |
| 87 | + require.NoError(t, err) |
| 88 | + require.Equal(t, seriesPerLabelName, res.Len()) |
| 89 | + } |
| 90 | +} |
0 commit comments