@@ -14,81 +14,52 @@ type schemaCaching struct {
14
14
}
15
15
16
16
func (s * schemaCaching ) GetReadQueriesForMetric (from , through model.Time , userID string , metricName string ) ([]IndexQuery , error ) {
17
- return s .splitTimesByCacheability (from , through , func (from , through model.Time ) ([]IndexQuery , error ) {
18
- return s .Schema .GetReadQueriesForMetric (from , through , userID , metricName )
19
- })
17
+ queries , err := s .Schema .GetReadQueriesForMetric (from , through , userID , metricName )
18
+ if err != nil {
19
+ return nil , err
20
+ }
21
+ return s .setImmutability (from , through , queries ), nil
20
22
}
21
23
22
24
func (s * schemaCaching ) GetReadQueriesForMetricLabel (from , through model.Time , userID string , metricName string , labelName string ) ([]IndexQuery , error ) {
23
- return s .splitTimesByCacheability (from , through , func (from , through model.Time ) ([]IndexQuery , error ) {
24
- return s .Schema .GetReadQueriesForMetricLabel (from , through , userID , metricName , labelName )
25
- })
25
+ queries , err := s .Schema .GetReadQueriesForMetricLabel (from , through , userID , metricName , labelName )
26
+ if err != nil {
27
+ return nil , err
28
+ }
29
+ return s .setImmutability (from , through , queries ), nil
26
30
}
27
31
28
32
func (s * schemaCaching ) GetReadQueriesForMetricLabelValue (from , through model.Time , userID string , metricName string , labelName string , labelValue string ) ([]IndexQuery , error ) {
29
- return s .splitTimesByCacheability (from , through , func (from , through model.Time ) ([]IndexQuery , error ) {
30
- return s .Schema .GetReadQueriesForMetricLabelValue (from , through , userID , metricName , labelName , labelValue )
31
- })
33
+ queries , err := s .Schema .GetReadQueriesForMetricLabelValue (from , through , userID , metricName , labelName , labelValue )
34
+ if err != nil {
35
+ return nil , err
36
+ }
37
+ return s .setImmutability (from , through , queries ), nil
32
38
}
33
39
34
40
// If the query resulted in series IDs, use this method to find chunks.
35
41
func (s * schemaCaching ) GetChunksForSeries (from , through model.Time , userID string , seriesID []byte ) ([]IndexQuery , error ) {
36
- return s .splitTimesByCacheability (from , through , func (from , through model.Time ) ([]IndexQuery , error ) {
37
- return s .Schema .GetChunksForSeries (from , through , userID , seriesID )
38
- })
39
- }
40
-
41
- func (s * schemaCaching ) splitTimesByCacheability (from , through model.Time , f func (from , through model.Time ) ([]IndexQuery , error )) ([]IndexQuery , error ) {
42
- var (
43
- cacheableQueries []IndexQuery
44
- activeQueries []IndexQuery
45
- err error
46
- cacheBefore = model .TimeFromUnix (mtime .Now ().Add (- s .cacheOlderThan ).Unix ())
47
- )
48
-
49
- if from .After (cacheBefore ) {
50
- activeQueries , err = f (from , through )
51
- if err != nil {
52
- return nil , err
53
- }
54
- } else if through .Before (cacheBefore ) {
55
- cacheableQueries , err = f (from , through )
56
- if err != nil {
57
- return nil , err
58
- }
59
- } else {
60
- cacheableQueries , err = f (from , cacheBefore )
61
- if err != nil {
62
- return nil , err
63
- }
64
-
65
- activeQueries , err = f (cacheBefore , through )
66
- if err != nil {
67
- return nil , err
68
- }
42
+ queries , err := s .Schema .GetChunksForSeries (from , through , userID , seriesID )
43
+ if err != nil {
44
+ return nil , err
69
45
}
70
-
71
- return mergeCacheableAndActiveQueries (cacheableQueries , activeQueries ), nil
46
+ return s .setImmutability (from , through , queries ), nil
72
47
}
73
48
74
- func mergeCacheableAndActiveQueries (cacheableQueries []IndexQuery , activeQueries []IndexQuery ) []IndexQuery {
75
- finalQueries := make ([]IndexQuery , 0 , len (cacheableQueries )+ len (activeQueries ))
76
-
77
- Outer:
78
- for _ , cq := range cacheableQueries {
79
- for _ , aq := range activeQueries {
80
- // When deduping, the bucket values only influence TableName and HashValue
81
- // and just checking those is enough.
82
- if cq .TableName == aq .TableName && cq .HashValue == aq .HashValue {
83
- continue Outer
84
- }
49
+ func (s * schemaCaching ) setImmutability (from , through model.Time , queries []IndexQuery ) []IndexQuery {
50
+ cacheBefore := model .TimeFromUnix (mtime .Now ().Add (- s .cacheOlderThan ).Unix ())
51
+
52
+ // If the entire query is cacheable then cache it.
53
+ // While not super effective stand-alone, when combined with query-frontend and splitting,
54
+ // old queries will mostly be all behind boundary.
55
+ // To cleanly split cacheable and non-cacheable ranges, we'd need bucket start and end times
56
+ // which we don't know.
57
+ // See: https://github.com/cortexproject/cortex/issues/1698
58
+ if through .Before (cacheBefore ) {
59
+ for i := range queries {
60
+ queries [i ].Immutable = true
85
61
}
86
-
87
- cq .Immutable = true
88
- finalQueries = append (finalQueries , cq )
89
62
}
90
63
91
- finalQueries = append (finalQueries , activeQueries ... )
92
-
93
- return finalQueries
64
+ return queries
94
65
}
0 commit comments