8
8
"net/http"
9
9
"net/http/httptest"
10
10
"net/url"
11
+ "sort"
11
12
"testing"
12
13
"time"
13
14
@@ -92,6 +93,11 @@ http_requests_total`,
92
93
expression : "sum by (pod) (http_requests_total)" ,
93
94
shardingLabels : []string {"pod" },
94
95
},
96
+ {
97
+ name : "aggregation with comparison" ,
98
+ expression : "avg by (Roles,type) (rss_service_message_handling) > 0.5" ,
99
+ shardingLabels : []string {"Roles" , "type" },
100
+ },
95
101
{
96
102
name : "multiple aggregations with grouping" ,
97
103
expression : "max by (pod) (sum by (pod, cluster) (http_requests_total))" ,
@@ -185,23 +191,25 @@ http_requests_total`,
185
191
}
186
192
tests := []testCase {
187
193
{
188
- name : "should shard range query when query is shardable" ,
189
- path : `/api/v1/query_range?end=1&start=0&step=120&query=sum(metric) by (pod,cluster_name)` ,
190
- isShardable : true ,
191
- codec : shardedPrometheusCodec ,
192
- shardSize : 2 ,
194
+ name : "should shard range query when query is shardable" ,
195
+ path : `/api/v1/query_range?end=1&start=0&step=120&query=sum(metric) by (pod,cluster_name)` ,
196
+ isShardable : true ,
197
+ codec : shardedPrometheusCodec ,
198
+ shardingLabels : []string {"pod" , "cluster_name" },
199
+ shardSize : 2 ,
193
200
responses : []string {
194
201
`{"status":"success","data":{"resultType":"matrix","result":[{"metric":{"__name__":"metric","__job__":"a"},"values":[[1,"1"],[2,"2"],[3,"3"]]}],"stats":{"samples":{"totalQueryableSamples":6,"totalQueryableSamplesPerStep":[[1,1],[2,2],[3,3]]}}}}` ,
195
202
`{"status":"success","data":{"resultType":"matrix","result":[{"metric":{"__name__":"metric","__job__":"b"},"values":[[1,"1"],[2,"2"],[3,"3"]]}],"stats":{"samples":{"totalQueryableSamples":6,"totalQueryableSamplesPerStep":[[1,1],[2,2],[3,3]]}}}}` ,
196
203
},
197
204
response : `{"status":"success","data":{"resultType":"matrix","result":[{"metric":{"__job__":"a","__name__":"metric"},"values":[[1,"1"],[2,"2"],[3,"3"]]},{"metric":{"__job__":"b","__name__":"metric"},"values":[[1,"1"],[2,"2"],[3,"3"]]}],"stats":{"samples":{"totalQueryableSamples":12,"totalQueryableSamplesPerStep":[[1,2],[2,4],[3,6]]}}}}` ,
198
205
},
199
206
{
200
- name : "should shard instant query when query is shardable" ,
201
- path : `/api/v1/query?time=120&query=sum(metric) by (pod,cluster_name)` ,
202
- codec : instantQueryCodec ,
203
- shardSize : 2 ,
204
- isShardable : true ,
207
+ name : "should shard instant query when query is shardable" ,
208
+ path : `/api/v1/query?time=120&query=sum(metric) by (pod,cluster_name)` ,
209
+ codec : instantQueryCodec ,
210
+ shardSize : 2 ,
211
+ shardingLabels : []string {"pod" , "cluster_name" },
212
+ isShardable : true ,
205
213
responses : []string {
206
214
`{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"up","job":"foo"},"value":[1,"1"]}],"stats":{"samples":{"totalQueryableSamples":10,"totalQueryableSamplesPerStep":[[1,10]]}}}}` ,
207
215
`{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"up","job":"bar"},"value":[2,"2"]}],"stats":{"samples":{"totalQueryableSamples":10,"totalQueryableSamplesPerStep":[[1,10]]}}}}` ,
@@ -258,11 +266,12 @@ http_requests_total`,
258
266
response : `{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"up","job":"bar"},"value":[2,"2"]},{"metric":{"__name__":"up","job":"foo"},"value":[1,"1"]}],"stats":{"samples":{"totalQueryableSamples":20,"totalQueryableSamplesPerStep":[[1,20]]}}}}` ,
259
267
})
260
268
tests = append (tests , testCase {
261
- name : fmt .Sprintf ("shardable query_range: %s" , query .name ),
262
- path : fmt .Sprintf (`/api/v1/query_range?start=1&end=2&step=1&query=%s` , url .QueryEscape (query .expression )),
263
- codec : shardedPrometheusCodec ,
264
- isShardable : true ,
265
- shardSize : 2 ,
269
+ name : fmt .Sprintf ("shardable query_range: %s" , query .name ),
270
+ path : fmt .Sprintf (`/api/v1/query_range?start=1&end=2&step=1&query=%s` , url .QueryEscape (query .expression )),
271
+ codec : shardedPrometheusCodec ,
272
+ isShardable : true ,
273
+ shardSize : 2 ,
274
+ shardingLabels : query .shardingLabels ,
266
275
responses : []string {
267
276
`{"status":"success","data":{"resultType":"matrix","result":[{"metric":{"__name__":"metric","__job__":"a"},"values":[[1,"1"],[2,"2"],[3,"3"]]}],"stats":{"samples":{"totalQueryableSamples":6,"totalQueryableSamplesPerStep":[[1,1],[2,2],[3,3]]}}}}` ,
268
277
`{"status":"success","data":{"resultType":"matrix","result":[{"metric":{"__name__":"metric","__job__":"b"},"values":[[1,"1"],[2,"2"],[3,"3"]]}],"stats":{"samples":{"totalQueryableSamples":6,"totalQueryableSamplesPerStep":[[1,1],[2,2],[3,3]]}}}}` ,
@@ -273,28 +282,33 @@ http_requests_total`,
273
282
274
283
for _ , tt := range tests {
275
284
t .Run (tt .name , func (t * testing.T ) {
285
+ sort .Strings (tt .shardingLabels )
276
286
s := httptest .NewServer (
277
287
http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
278
288
q := r .FormValue ("query" )
279
289
expr , _ := parser .ParseExpr (q )
280
- shardInfo := storepb. ShardInfo {}
290
+ shardIndex := int64 ( 0 )
281
291
282
292
parser .Inspect (expr , func (n parser.Node , _ []parser.Node ) error {
283
293
if selector , ok := n .(* parser.VectorSelector ); ok {
284
294
for _ , matcher := range selector .LabelMatchers {
285
295
if matcher .Name == querysharding .CortexShardByLabel {
286
296
287
297
decoded , _ := base64 .StdEncoding .DecodeString (matcher .Value )
298
+ shardInfo := storepb.ShardInfo {}
288
299
err := shardInfo .Unmarshal (decoded )
289
300
require .NoError (t , err )
301
+ sort .Strings (shardInfo .Labels )
302
+ require .Equal (t , tt .shardingLabels , shardInfo .Labels )
303
+ require .Equal (t , tt .isShardable , shardInfo .TotalShards > 0 )
304
+ shardIndex = shardInfo .ShardIndex
290
305
}
291
306
}
292
307
}
293
308
return nil
294
309
})
295
310
296
- require .Equal (t , tt .isShardable , shardInfo .TotalShards > 0 )
297
- _ , _ = w .Write ([]byte (tt .responses [shardInfo .ShardIndex ]))
311
+ _ , _ = w .Write ([]byte (tt .responses [shardIndex ]))
298
312
}),
299
313
)
300
314
defer s .Close ()
0 commit comments