Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle cache-control headers also during a partial cache hit. #2403

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 6 additions & 3 deletions pkg/querier/queryrange/results_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ 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:
Expand All @@ -182,6 +182,7 @@ func shouldCacheResponse(r Response) bool {
for _, v := range hv.Values {
if v == noCacheValue {
shouldCache = false
level.Debug(s.logger).Log("msg", fmt.Sprintf("%s header in response is equal to %s, not caching the response", cachecontrolHeader, noCacheValue))
break outer
}
}
Expand All @@ -197,8 +198,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 +238,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
4 changes: 3 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 @@ -139,7 +141,7 @@ func TestShouldCache(t *testing.T) {
} {
{
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