|
| 1 | +//go:build integration_querier |
| 2 | + |
| 3 | +package integration |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + "math/rand" |
| 9 | + "path/filepath" |
| 10 | + "slices" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/prometheus/common/model" |
| 15 | + "github.com/prometheus/prometheus/model/labels" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | + "github.com/thanos-io/thanos/pkg/block" |
| 18 | + "github.com/thanos-io/thanos/pkg/block/metadata" |
| 19 | + |
| 20 | + cortex_testutil "github.com/cortexproject/cortex/pkg/util/test" |
| 21 | + |
| 22 | + "github.com/cortexproject/cortex/integration/e2e" |
| 23 | + e2ecache "github.com/cortexproject/cortex/integration/e2e/cache" |
| 24 | + e2edb "github.com/cortexproject/cortex/integration/e2e/db" |
| 25 | + "github.com/cortexproject/cortex/integration/e2ecortex" |
| 26 | + "github.com/cortexproject/cortex/pkg/storage/bucket" |
| 27 | + "github.com/cortexproject/cortex/pkg/util/log" |
| 28 | +) |
| 29 | + |
| 30 | +func TestParquetBucketStore_ProjectionHint(t *testing.T) { |
| 31 | + s, err := e2e.NewScenario(networkName) |
| 32 | + require.NoError(t, err) |
| 33 | + defer s.Close() |
| 34 | + |
| 35 | + consul := e2edb.NewConsulWithName("consul") |
| 36 | + minio := e2edb.NewMinio(9000, bucketName) |
| 37 | + memcached := e2ecache.NewMemcached() |
| 38 | + require.NoError(t, s.StartAndWaitReady(consul, minio, memcached)) |
| 39 | + |
| 40 | + // Define configuration flags. |
| 41 | + flags := BlocksStorageFlags() |
| 42 | + flags = mergeFlags(flags, map[string]string{ |
| 43 | + // Enable Thanos engine and projection optimization. |
| 44 | + "-querier.thanos-engine": "true", |
| 45 | + "-querier.optimizers": "projection", |
| 46 | + |
| 47 | + // enable honor-projection-hints querier and store gateway |
| 48 | + "-querier.honor-projection-hints": "true", |
| 49 | + "-blocks-storage.bucket-store.honor-projection-hints": "true", |
| 50 | + // enable Store Gateway Parquet mode |
| 51 | + "-blocks-storage.bucket-store.bucket-store-type": "parquet", |
| 52 | + |
| 53 | + // Set query-ingesters-within to 1h so queries older than 1h don't hit ingesters |
| 54 | + "-querier.query-ingesters-within": "1h", |
| 55 | + |
| 56 | + // Configure Parquet Converter |
| 57 | + "-parquet-converter.enabled": "true", |
| 58 | + "-parquet-converter.conversion-interval": "1s", |
| 59 | + "-parquet-converter.ring.consul.hostname": consul.NetworkHTTPEndpoint(), |
| 60 | + "-compactor.block-ranges": "1ms,12h", |
| 61 | + // Enable cache |
| 62 | + "-blocks-storage.bucket-store.parquet-labels-cache.backend": "inmemory,memcached", |
| 63 | + "-blocks-storage.bucket-store.parquet-labels-cache.memcached.addresses": "dns+" + memcached.NetworkEndpoint(e2ecache.MemcachedPort), |
| 64 | + }) |
| 65 | + |
| 66 | + // Store Gateway |
| 67 | + storeGateway := e2ecortex.NewStoreGateway("store-gateway", e2ecortex.RingStoreConsul, consul.NetworkHTTPEndpoint(), flags, "") |
| 68 | + require.NoError(t, s.StartAndWaitReady(storeGateway)) |
| 69 | + |
| 70 | + // Parquet Converter |
| 71 | + parquetConverter := e2ecortex.NewParquetConverter("parquet-converter", e2ecortex.RingStoreConsul, consul.NetworkHTTPEndpoint(), flags, "") |
| 72 | + require.NoError(t, s.StartAndWaitReady(parquetConverter)) |
| 73 | + |
| 74 | + // Querier |
| 75 | + querier := e2ecortex.NewQuerier("querier", e2ecortex.RingStoreConsul, consul.NetworkHTTPEndpoint(), mergeFlags(flags, map[string]string{ |
| 76 | + "-querier.store-gateway-addresses": storeGateway.NetworkGRPCEndpoint(), |
| 77 | + }), "") |
| 78 | + require.NoError(t, s.StartAndWaitReady(querier)) |
| 79 | + |
| 80 | + require.NoError(t, querier.WaitSumMetrics(e2e.Equals(512), "cortex_ring_tokens_total")) |
| 81 | + require.NoError(t, storeGateway.WaitSumMetrics(e2e.Equals(512), "cortex_ring_tokens_total")) |
| 82 | + |
| 83 | + // Create block |
| 84 | + now := time.Now() |
| 85 | + // Time range: [Now - 24h] to [Now - 20h] |
| 86 | + start := now.Add(-24 * time.Hour) |
| 87 | + end := now.Add(-20 * time.Hour) |
| 88 | + |
| 89 | + ctx := context.Background() |
| 90 | + |
| 91 | + rnd := rand.New(rand.NewSource(time.Now().Unix())) |
| 92 | + dir := filepath.Join(s.SharedDir(), "data") |
| 93 | + scrapeInterval := time.Minute |
| 94 | + statusCodes := []string{"200", "400", "404", "500", "502"} |
| 95 | + methods := []string{"GET", "POST", "PUT", "DELETE"} |
| 96 | + |
| 97 | + numSeries := 10 |
| 98 | + numSamples := 100 |
| 99 | + |
| 100 | + lbls := make([]labels.Labels, 0, numSeries) |
| 101 | + for i := 0; i < numSeries; i++ { |
| 102 | + lbls = append(lbls, labels.FromStrings( |
| 103 | + labels.MetricName, "http_requests_total", |
| 104 | + "job", "api-server", |
| 105 | + "instance", fmt.Sprintf("instance-%d", i), |
| 106 | + "status_code", statusCodes[i%len(statusCodes)], |
| 107 | + "method", methods[i%len(methods)], |
| 108 | + "path", fmt.Sprintf("/api/v1/endpoint%d", i%3), |
| 109 | + "cluster", "test-cluster", |
| 110 | + )) |
| 111 | + } |
| 112 | + |
| 113 | + id, err := e2e.CreateBlock(ctx, rnd, dir, lbls, numSamples, start.UnixMilli(), end.UnixMilli(), scrapeInterval.Milliseconds(), 10) |
| 114 | + require.NoError(t, err) |
| 115 | + |
| 116 | + storage, err := e2ecortex.NewS3ClientForMinio(minio, bucketName) |
| 117 | + require.NoError(t, err) |
| 118 | + bkt := bucket.NewUserBucketClient("user-1", storage.GetBucket(), nil) |
| 119 | + |
| 120 | + // Upload TSDB Block |
| 121 | + require.NoError(t, block.Upload(ctx, log.Logger, bkt, filepath.Join(dir, id.String()), metadata.NoneFunc)) |
| 122 | + |
| 123 | + // Wait until parquet converter convert block |
| 124 | + require.NoError(t, parquetConverter.WaitSumMetricsWithOptions(e2e.Equals(float64(1)), []string{"cortex_parquet_converter_blocks_converted_total"}, e2e.WaitMissingMetrics)) |
| 125 | + |
| 126 | + // Create client |
| 127 | + c, err := e2ecortex.NewClient("", querier.HTTPEndpoint(), "", "", "user-1") |
| 128 | + require.NoError(t, err) |
| 129 | + |
| 130 | + cortex_testutil.Poll(t, 60*time.Second, true, func() interface{} { |
| 131 | + labelSets, err := c.Series([]string{`{job="api-server"}`}, start, end) |
| 132 | + if err != nil { |
| 133 | + t.Logf("Series query failed: %v", err) |
| 134 | + return false |
| 135 | + } |
| 136 | + return len(labelSets) > 0 |
| 137 | + }) |
| 138 | + |
| 139 | + testCases := []struct { |
| 140 | + name string |
| 141 | + query string |
| 142 | + expectedLabels []string // query result should contain these labels |
| 143 | + }{ |
| 144 | + { |
| 145 | + name: "vector selector query", |
| 146 | + query: `http_requests_total`, |
| 147 | + expectedLabels: []string{ |
| 148 | + "__name__", "job", "instance", "status_code", "method", "path", "cluster", |
| 149 | + }, |
| 150 | + }, |
| 151 | + { |
| 152 | + name: "simple_sum_by_job", |
| 153 | + query: `sum by (job) (http_requests_total)`, |
| 154 | + expectedLabels: []string{"job"}, |
| 155 | + }, |
| 156 | + { |
| 157 | + name: "rate_with_aggregation", |
| 158 | + query: `sum by (method) (rate(http_requests_total[5m]))`, |
| 159 | + expectedLabels: []string{"method"}, |
| 160 | + }, |
| 161 | + { |
| 162 | + name: "multiple_grouping_labels", |
| 163 | + query: `sum by (job, status_code) (http_requests_total)`, |
| 164 | + expectedLabels: []string{"job", "status_code"}, |
| 165 | + }, |
| 166 | + { |
| 167 | + name: "aggregation without query", |
| 168 | + query: `sum without (instance, method) (http_requests_total)`, |
| 169 | + expectedLabels: []string{"job", "status_code", "path", "cluster"}, |
| 170 | + }, |
| 171 | + } |
| 172 | + for _, tc := range testCases { |
| 173 | + t.Run(tc.name, func(t *testing.T) { |
| 174 | + t.Logf("Testing: %s", tc.query) |
| 175 | + |
| 176 | + // Execute instant query |
| 177 | + result, err := c.Query(tc.query, end) |
| 178 | + require.NoError(t, err) |
| 179 | + require.NotNil(t, result) |
| 180 | + |
| 181 | + // Verify we got results |
| 182 | + vector, ok := result.(model.Vector) |
| 183 | + require.True(t, ok, "result should be a vector") |
| 184 | + require.NotEmpty(t, vector, "query should return results") |
| 185 | + |
| 186 | + for _, sample := range vector { |
| 187 | + actualLabels := make(map[string]struct{}) |
| 188 | + for label := range sample.Metric { |
| 189 | + actualLabels[string(label)] = struct{}{} |
| 190 | + } |
| 191 | + |
| 192 | + // Check that all expected labels are present |
| 193 | + for _, expectedLabel := range tc.expectedLabels { |
| 194 | + _, ok := actualLabels[expectedLabel] |
| 195 | + require.True(t, ok, |
| 196 | + "series should have %s label", expectedLabel) |
| 197 | + } |
| 198 | + |
| 199 | + // Check that no unexpected labels are present |
| 200 | + for lbl := range actualLabels { |
| 201 | + if !slices.Contains(tc.expectedLabels, lbl) { |
| 202 | + require.Fail(t, "series should not have unexpected label: %s", lbl) |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | + }) |
| 207 | + } |
| 208 | +} |
0 commit comments