github.com/thanos-io/thanos@v0.32.5/pkg/queryfrontend/cache.go (about) 1 // Copyright (c) The Thanos Authors. 2 // Licensed under the Apache License 2.0. 3 4 package queryfrontend 5 6 import ( 7 "fmt" 8 9 "github.com/thanos-io/thanos/internal/cortex/querier/queryrange" 10 "github.com/thanos-io/thanos/pkg/compact/downsample" 11 ) 12 13 // thanosCacheKeyGenerator is a utility for using split interval when determining cache keys. 14 type thanosCacheKeyGenerator struct { 15 interval queryrange.IntervalFn 16 resolutions []int64 17 } 18 19 func newThanosCacheKeyGenerator(intervalFn queryrange.IntervalFn) thanosCacheKeyGenerator { 20 return thanosCacheKeyGenerator{ 21 interval: intervalFn, 22 resolutions: []int64{downsample.ResLevel2, downsample.ResLevel1, downsample.ResLevel0}, 23 } 24 } 25 26 // GenerateCacheKey generates a cache key based on the Request and interval. 27 // TODO(yeya24): Add other request params as request key. 28 func (t thanosCacheKeyGenerator) GenerateCacheKey(userID string, r queryrange.Request) string { 29 currentInterval := r.GetStart() / t.interval(r).Milliseconds() 30 switch tr := r.(type) { 31 case *ThanosQueryRangeRequest: 32 i := 0 33 for ; i < len(t.resolutions) && t.resolutions[i] > tr.MaxSourceResolution; i++ { 34 } 35 shardInfoKey := generateShardInfoKey(tr) 36 return fmt.Sprintf("fe:%s:%s:%d:%d:%d:%s:%d:%s", userID, tr.Query, tr.Step, currentInterval, i, shardInfoKey, tr.LookbackDelta, tr.Engine) 37 case *ThanosLabelsRequest: 38 return fmt.Sprintf("fe:%s:%s:%s:%d", userID, tr.Label, tr.Matchers, currentInterval) 39 case *ThanosSeriesRequest: 40 return fmt.Sprintf("fe:%s:%s:%d", userID, tr.Matchers, currentInterval) 41 } 42 return fmt.Sprintf("fe:%s:%s:%d:%d", userID, r.GetQuery(), r.GetStep(), currentInterval) 43 } 44 45 func generateShardInfoKey(r *ThanosQueryRangeRequest) string { 46 if r.ShardInfo == nil { 47 return "-" 48 } 49 return fmt.Sprintf("%d:%d", r.ShardInfo.TotalShards, r.ShardInfo.ShardIndex) 50 }