Skip to content

Add dynamic interval cache splitter #6592

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

Merged
merged 3 commits into from
Feb 17, 2025
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
5 changes: 5 additions & 0 deletions pkg/querier/tripperware/queryrange/query_range_middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,12 @@ func Middlewares(
}
return false
}

queryCacheMiddleware, cache, err := NewResultsCacheMiddleware(log, cfg.ResultsCacheConfig, constSplitter(cfg.SplitQueriesByInterval), limits, prometheusCodec, cacheExtractor, shouldCache, registerer)
if cfg.DynamicQuerySplitsConfig.MaxShardsPerQuery > 0 || cfg.DynamicQuerySplitsConfig.MaxFetchedDataDurationPerQuery > 0 {
queryCacheMiddleware, cache, err = NewResultsCacheMiddleware(log, cfg.ResultsCacheConfig, dynamicIntervalSplitter{cfg, limits, queryAnalyzer, lookbackDelta}, limits, prometheusCodec, cacheExtractor, shouldCache, registerer)
}

if err != nil {
return nil, nil, err
}
Expand Down
12 changes: 8 additions & 4 deletions pkg/querier/tripperware/queryrange/results_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,16 @@ func (PrometheusResponseExtractor) ResponseWithoutStats(resp tripperware.Respons
// CacheSplitter generates cache keys. This is a useful interface for downstream
// consumers who wish to implement their own strategies.
type CacheSplitter interface {
GenerateCacheKey(userID string, r tripperware.Request) string
GenerateCacheKey(userID string, ctx context.Context, r tripperware.Request) (string, error)
}

// constSplitter is a utility for using a constant split interval when determining cache keys
type constSplitter time.Duration

// GenerateCacheKey generates a cache key based on the userID, Request and interval.
func (t constSplitter) GenerateCacheKey(userID string, r tripperware.Request) string {
func (t constSplitter) GenerateCacheKey(userID string, ctx context.Context, r tripperware.Request) (string, error) {
currentInterval := r.GetStart() / int64(time.Duration(t)/time.Millisecond)
return fmt.Sprintf("%s:%s:%d:%d", userID, r.GetQuery(), r.GetStep(), currentInterval)
return fmt.Sprintf("%s:%s:%d:%d", userID, r.GetQuery(), r.GetStep(), currentInterval), nil
}

// ShouldCacheFn checks whether the current request should go to cache
Expand Down Expand Up @@ -232,8 +232,12 @@ func (s resultsCache) Do(ctx context.Context, r tripperware.Request) (tripperwar
return s.next.Do(ctx, r)
}

key, err := s.splitter.GenerateCacheKey(tenant.JoinTenantIDs(tenantIDs), ctx, r)
if err != nil {
return nil, httpgrpc.Errorf(http.StatusBadRequest, err.Error())
}

var (
key = s.splitter.GenerateCacheKey(tenant.JoinTenantIDs(tenantIDs), r)
extents []tripperware.Extent
response tripperware.Response
)
Expand Down
15 changes: 12 additions & 3 deletions pkg/querier/tripperware/queryrange/results_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1381,7 +1381,9 @@ func TestResultsCacheMaxFreshness(t *testing.T) {
req := parsedRequest.WithStartEnd(int64(modelNow)-(50*1e3), int64(modelNow)-(10*1e3))

// fill cache
key := constSplitter(day).GenerateCacheKey("1", req)
key, err := constSplitter(day).GenerateCacheKey("1", ctx, req)
require.NoError(t, err)

rc.(*resultsCache).put(ctx, key, []tripperware.Extent{mkExtent(int64(modelNow)-(600*1e3), int64(modelNow))})

resp, err := rc.Do(ctx, req)
Expand Down Expand Up @@ -1460,7 +1462,11 @@ func TestConstSplitter_generateCacheKey(t *testing.T) {
tt := tt
t.Run(fmt.Sprintf("%s - %s", tt.name, tt.interval), func(t *testing.T) {
t.Parallel()
if got := constSplitter(tt.interval).GenerateCacheKey("fake", tt.r); got != tt.want {
ctx := user.InjectOrgID(context.Background(), "1")
got, err := constSplitter(tt.interval).GenerateCacheKey("fake", ctx, tt.r)
require.NoError(t, err)

if got != tt.want {
t.Errorf("generateKey() = %v, want %v", got, tt.want)
}
})
Expand Down Expand Up @@ -1563,7 +1569,10 @@ func TestResultsCacheFillCompatibility(t *testing.T) {
// Check cache and make sure we write response in old format even though the response is new format.
tenantIDs, err := tenant.TenantIDs(ctx)
require.NoError(t, err)
cacheKey := cache.HashKey(constSplitter(day).GenerateCacheKey(tenant.JoinTenantIDs(tenantIDs), parsedRequest))
key, err := constSplitter(day).GenerateCacheKey(tenant.JoinTenantIDs(tenantIDs), ctx, parsedRequest)
require.NoError(t, err)

cacheKey := cache.HashKey(key)
found, bufs, _ := c.Fetch(ctx, []string{cacheKey})
require.Equal(t, []string{cacheKey}, found)
require.Len(t, bufs, 1)
Expand Down
19 changes: 19 additions & 0 deletions pkg/querier/tripperware/queryrange/split_by_interval.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package queryrange

import (
"context"
"fmt"
"net/http"
"time"

Expand Down Expand Up @@ -364,6 +365,24 @@ func analyzeDurationFetchedByQueryExpr(expr parser.Expr, queryStart int64, query
return time.Duration(durationFetchedByRangeCount) * baseInterval, time.Duration(durationFetchedBySelectorsCount) * baseInterval, time.Duration(durationFetchedByLookbackDeltaCount) * baseInterval
}

// dynamicIntervalSplitter is a utility for using a dynamic split interval when determining cache keys
type dynamicIntervalSplitter struct {
cfg Config
limits tripperware.Limits
queryAnalyzer querysharding.Analyzer
lookbackDelta time.Duration
}

// GenerateCacheKey generates a cache key based on the userID, Request and interval.
func (c dynamicIntervalSplitter) GenerateCacheKey(userID string, ctx context.Context, r tripperware.Request) (string, error) {
interval, err := dynamicIntervalFn(c.cfg, c.limits, c.queryAnalyzer, c.lookbackDelta)(ctx, r)
if err != nil {
return "", err
}
currentInterval := r.GetStart() / int64(interval/time.Millisecond)
return fmt.Sprintf("%s:%s:%d:%d", userID, r.GetQuery(), r.GetStep(), currentInterval), nil
}

func floorDiv(a, b int64) int64 {
if a < 0 && a%b != 0 {
return a/b - 1
Expand Down
Loading