Skip to content

Commit f57f9e0

Browse files
committed
Avoid cache only if the warning message contains partial data error
1 parent 5ae9973 commit f57f9e0

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

pkg/querier/partialdata/partia_data.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ import (
88
type partialDataCtxKey struct{}
99

1010
var (
11-
ctxKey = &partialDataCtxKey{}
12-
partialDataErrorMsg = "Query result may contain partial data."
11+
ctxKey = &partialDataCtxKey{}
1312
)
1413

14+
const ErrorMsg string = "Query result may contain partial data."
15+
1516
type Error struct{}
1617

1718
func (e Error) Error() string {
18-
return partialDataErrorMsg
19+
return ErrorMsg
1920
}
2021

2122
func ContextWithPartialData(ctx context.Context, isEnabled bool) context.Context {

pkg/querier/tripperware/queryrange/results_cache.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/cortexproject/cortex/pkg/chunk/cache"
2828
"github.com/cortexproject/cortex/pkg/cortexpb"
2929
"github.com/cortexproject/cortex/pkg/querier"
30+
"github.com/cortexproject/cortex/pkg/querier/partialdata"
3031
"github.com/cortexproject/cortex/pkg/querier/tripperware"
3132
"github.com/cortexproject/cortex/pkg/tenant"
3233
"github.com/cortexproject/cortex/pkg/util/flagext"
@@ -296,8 +297,11 @@ func (s resultsCache) shouldCacheResponse(ctx context.Context, req tripperware.R
296297
return false
297298
}
298299
if res, ok := r.(*tripperware.PrometheusResponse); ok {
299-
if len(res.Warnings) > 0 {
300-
return false
300+
partialDataErr := partialdata.Error{}
301+
for _, warning := range res.Warnings {
302+
if warning == partialDataErr.Error() {
303+
return false
304+
}
301305
}
302306
}
303307

pkg/querier/tripperware/queryrange/results_cache_test.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818

1919
"github.com/cortexproject/cortex/pkg/chunk/cache"
2020
"github.com/cortexproject/cortex/pkg/cortexpb"
21+
"github.com/cortexproject/cortex/pkg/querier/partialdata"
2122
"github.com/cortexproject/cortex/pkg/querier/tripperware"
2223
"github.com/cortexproject/cortex/pkg/tenant"
2324
"github.com/cortexproject/cortex/pkg/util/flagext"
@@ -555,7 +556,7 @@ func TestShouldCache(t *testing.T) {
555556
expected: false,
556557
},
557558
{
558-
name: "contains warning",
559+
name: "contains partial data warning",
559560
request: &tripperware.PrometheusRequest{Query: "metric"},
560561
input: tripperware.Response(&tripperware.PrometheusResponse{
561562
Headers: []*tripperware.PrometheusResponseHeader{
@@ -564,10 +565,24 @@ func TestShouldCache(t *testing.T) {
564565
Values: []string{},
565566
},
566567
},
567-
Warnings: []string{"some warning"},
568+
Warnings: []string{partialdata.ErrorMsg},
568569
}),
569570
expected: false,
570571
},
572+
{
573+
name: "contains other warning",
574+
request: &tripperware.PrometheusRequest{Query: "metric"},
575+
input: tripperware.Response(&tripperware.PrometheusResponse{
576+
Headers: []*tripperware.PrometheusResponseHeader{
577+
{
578+
Name: "meaninglessheader",
579+
Values: []string{},
580+
},
581+
},
582+
Warnings: []string{"other warning"},
583+
}),
584+
expected: true,
585+
},
571586
} {
572587
{
573588
t.Run(tc.name, func(t *testing.T) {

0 commit comments

Comments
 (0)