Skip to content

Commit 357d901

Browse files
authored
Merge pull request #1699 from gouthamve/caching-fix-1
Simplify long-term caching
2 parents 8bab3df + ee7d70a commit 357d901

File tree

2 files changed

+33
-62
lines changed

2 files changed

+33
-62
lines changed

pkg/chunk/schema_caching.go

Lines changed: 32 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -14,81 +14,52 @@ type schemaCaching struct {
1414
}
1515

1616
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
2022
}
2123

2224
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
2630
}
2731

2832
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
3238
}
3339

3440
// If the query resulted in series IDs, use this method to find chunks.
3541
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
6945
}
70-
71-
return mergeCacheableAndActiveQueries(cacheableQueries, activeQueries), nil
46+
return s.setImmutability(from, through, queries), nil
7247
}
7348

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
8561
}
86-
87-
cq.Immutable = true
88-
finalQueries = append(finalQueries, cq)
8962
}
9063

91-
finalQueries = append(finalQueries, activeQueries...)
92-
93-
return finalQueries
64+
return queries
9465
}

pkg/chunk/schema_caching_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func TestCachingSchema(t *testing.T) {
5555
// Mix of both.
5656
baseTime.Add(-50 * time.Hour),
5757
baseTime.Add(-2 * time.Hour),
58-
0,
58+
-1,
5959
},
6060
} {
6161
t.Run(strconv.Itoa(i), func(t *testing.T) {

0 commit comments

Comments
 (0)