Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions pkg/querier/queryrange/results_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,25 +168,19 @@ func (s resultsCache) Do(ctx context.Context, r Request) (Response, error) {
}

// shouldCacheResponse says whether the response should be cached or not.
func shouldCacheResponse(r Response) bool {
func (s resultsCache) shouldCacheResponse(r Response) bool {
if promResp, ok := r.(*PrometheusResponse); ok {
shouldCache := true
outer:
for _, hv := range promResp.Headers {
if hv == nil {
if hv.GetName() != cachecontrolHeader {
continue
}
if hv.Name != cachecontrolHeader {
continue
}
for _, v := range hv.Values {
for _, v := range hv.GetValues() {
if v == noCacheValue {
shouldCache = false
break outer
level.Debug(s.logger).Log("msg", fmt.Sprintf("%s header in response is equal to %s, not caching the response", cachecontrolHeader, noCacheValue))
return false
}
}
}
return shouldCache
}
return true
}
Expand All @@ -197,8 +191,7 @@ func (s resultsCache) handleMiss(ctx context.Context, r Request) (Response, []Ex
return nil, nil, err
}

if !shouldCacheResponse(response) {
level.Debug(s.logger).Log("msg", fmt.Sprintf("%s header in response is equal to %s, not caching the response", cachecontrolHeader, noCacheValue))
if !s.shouldCacheResponse(response) {
return response, []Extent{}, nil
}

Expand Down Expand Up @@ -238,6 +231,9 @@ func (s resultsCache) handleHit(ctx context.Context, r Request, extents []Extent

for _, reqResp := range reqResps {
responses = append(responses, reqResp.Response)
if !s.shouldCacheResponse(reqResp.Response) {
continue
}
extent, err := toExtent(ctx, reqResp.Request, reqResp.Response)
if err != nil {
return nil, nil, err
Expand Down
21 changes: 20 additions & 1 deletion pkg/querier/queryrange/results_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/cortexproject/cortex/pkg/chunk/cache"
"github.com/cortexproject/cortex/pkg/ingester/client"
"github.com/cortexproject/cortex/pkg/util"
"github.com/cortexproject/cortex/pkg/util/flagext"
)

Expand Down Expand Up @@ -96,6 +97,7 @@ func mkExtent(start, end int64) Extent {
}

func TestShouldCache(t *testing.T) {
c := &resultsCache{logger: util.Logger}
for i, tc := range []struct {
input Response
expected bool
Expand Down Expand Up @@ -136,10 +138,27 @@ func TestShouldCache(t *testing.T) {
}),
expected: false,
},
// some broken responses
{
input: Response(&PrometheusResponse{}),
expected: true,
},
{
input: Response(&PrometheusResponse{
Headers: []*PrometheusResponseHeader{nil},
}),
expected: true,
},
{
input: Response(&PrometheusResponse{
Headers: []*PrometheusResponseHeader{{Name: cachecontrolHeader}},
}),
expected: true,
},
} {
{
t.Run(strconv.Itoa(i), func(t *testing.T) {
ret := shouldCacheResponse(tc.input)
ret := c.shouldCacheResponse(tc.input)
require.Equal(t, tc.expected, ret)
})
}
Expand Down