Skip to content

Commit a631a5f

Browse files
gouthamvekhaines
authored andcommitted
Handle the case of large step causing single pt extents (cortexproject#3818)
* Handle the case of large step causing single pt extents We noticed that we were ignoring samples if the step is 24h and this fixes it. Signed-off-by: Goutham Veeramachaneni <[email protected]> * Handle the case of start == end better Signed-off-by: Goutham Veeramachaneni <[email protected]> * Address feedback Signed-off-by: Goutham Veeramachaneni <[email protected]> (cherry picked from commit e10ccda)
1 parent 9bfd413 commit a631a5f

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

pkg/querier/queryrange/results_cache.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,13 @@ func (s resultsCache) partition(req Request, extents []Extent) ([]Request, []Res
424424
if extent.GetEnd() < start || extent.Start > req.GetEnd() {
425425
continue
426426
}
427-
// If this extent is tiny, discard it: more efficient to do a few larger queries
428-
if extent.End-extent.Start < s.minCacheExtent {
427+
428+
// If this extent is tiny, discard it: more efficient to do a few larger queries.
429+
430+
// However if the step is large enough, the split_query_by_interval middleware would generate a query with same start and end.
431+
// For example, if the step size is more than 12h and the interval is 24h.
432+
// This means the extent's start and end time would be same, even if the timerange covers several hours.
433+
if (req.GetStart() != req.GetEnd()) && (extent.End-extent.Start < s.minCacheExtent) {
429434
continue
430435
}
431436

@@ -449,6 +454,12 @@ func (s resultsCache) partition(req Request, extents []Extent) ([]Request, []Res
449454
requests = append(requests, r)
450455
}
451456

457+
// If start and end are the same (valid in promql), start == req.GetEnd() and we won't do the query.
458+
// But we should only do the request if we don't have a valid cached response for it.
459+
if req.GetStart() == req.GetEnd() && len(cachedResponses) == 0 {
460+
requests = append(requests, req)
461+
}
462+
452463
return requests, cachedResponses, nil
453464
}
454465

pkg/querier/queryrange/results_cache_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,36 @@ func TestPartition(t *testing.T) {
361361
mkAPIResponse(100, 120, 10),
362362
},
363363
},
364+
// Extent is outside the range and the request has a single step (same start and end).
365+
{
366+
input: &PrometheusRequest{
367+
Start: 100,
368+
End: 100,
369+
},
370+
prevCachedResponse: []Extent{
371+
mkExtent(50, 90),
372+
},
373+
expectedRequests: []Request{
374+
&PrometheusRequest{
375+
Start: 100,
376+
End: 100,
377+
},
378+
},
379+
},
380+
// Test when hit has a large step and only a single sample extent.
381+
{
382+
// If there is a only a single sample in the split interval, start and end will be the same.
383+
input: &PrometheusRequest{
384+
Start: 100,
385+
End: 100,
386+
},
387+
prevCachedResponse: []Extent{
388+
mkExtent(100, 100),
389+
},
390+
expectedCachedResponse: []Response{
391+
mkAPIResponse(100, 105, 10),
392+
},
393+
},
364394
} {
365395
t.Run(strconv.Itoa(i), func(t *testing.T) {
366396
s := resultsCache{

0 commit comments

Comments
 (0)