Skip to content

Commit fc90bde

Browse files
Small optimization to avoid unnecessary Extents merge when growing cache entry (#4026)
* Small optimization to avoid unnecessary Extents merge when growing cache entry Signed-off-by: Alvin Lin <[email protected]> * Update CHANGELOG.md Signed-off-by: Alvin Lin <[email protected]> * Remove unintended whitespace modification Signed-off-by: Alvin Lin <[email protected]> * Add test to indirectly test the existence of the optimization Signed-off-by: Alvin Lin <[email protected]> * Update CHANGELOG.md Signed-off-by: Marco Pracucci <[email protected]> Co-authored-by: Marco Pracucci <[email protected]>
1 parent 774518a commit fc90bde

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* [ENHANCEMENT] Distributor: reduce CPU and memory when an high number of errors are returned by the distributor on the write path. #3990
2626
* [ENHANCEMENT] Put metric before label value in the "label value too long" error message. #4018
2727
* [ENHANCEMENT] Allow use of `y|w|d` suffixes for duration related limits and per-tenant limits. #4044
28+
* [ENHANCEMENT] Query-frontend: Small optimization on top of PR #3968 to avoid unnecessary Extents merging. #4026
2829
* [BUGFIX] Ruler-API: fix bug where `/api/v1/rules/<namespace>/<group_name>` endpoint return `400` instead of `404`. #4013
2930
* [BUGFIX] Distributor: reverted changes done to rate limiting in #3825. #3948
3031
* [BUGFIX] Ingester: Fix race condition when opening and closing tsdb concurrently. #3959

pkg/querier/queryrange/results_cache.go

+7
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,13 @@ func (s resultsCache) handleHit(ctx context.Context, r Request, extents []Extent
385385
extents = append(extents, extent)
386386
}
387387
sort.Slice(extents, func(i, j int) bool {
388+
if extents[i].Start == extents[j].Start {
389+
// as an optimization, for two extents starts at the same time, we
390+
// put bigger extent at the front of the slice, which helps
391+
// to reduce the amount of merge we have to do later.
392+
return extents[i].End > extents[j].End
393+
}
394+
388395
return extents[i].Start < extents[j].Start
389396
})
390397

pkg/querier/queryrange/results_cache_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,31 @@ func TestHandleHit(t *testing.T) {
690690
mkExtentWithStep(60, 180, 20),
691691
},
692692
},
693+
{
694+
name: "Should not throw error if complete-overlapped smaller Extent is erroneous",
695+
input: &PrometheusRequest{
696+
// This request is carefully crated such that cachedEntry is not used to fulfill
697+
// the request.
698+
Start: 160,
699+
End: 180,
700+
Step: 20,
701+
},
702+
cachedEntry: []Extent{
703+
{
704+
Start: 60,
705+
End: 80,
706+
707+
// if the optimization of "sorting by End when Start of 2 Extents are equal" is not there, this nil
708+
// response would cause error during Extents merge phase. With the optimization
709+
// this bad Extent should be dropped. The good Extent below can be used instead.
710+
Response: nil,
711+
},
712+
mkExtentWithStep(60, 160, 20),
713+
},
714+
expectedUpdatedCachedEntry: []Extent{
715+
mkExtentWithStep(60, 180, 20),
716+
},
717+
},
693718
} {
694719
t.Run(tc.name, func(t *testing.T) {
695720
sut := resultsCache{

0 commit comments

Comments
 (0)