Skip to content

Commit bd2fef8

Browse files
authored
Merge pull request #1997 from cloudflare/cache
Fix cache evictions
2 parents a0fcdd6 + c623b50 commit bd2fef8

4 files changed

Lines changed: 14 additions & 44 deletions

File tree

‎cmd/pint/watch.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,9 @@ func startTimer(ctx context.Context, workers int, isOffline bool, gen *config.Pr
250250
slog.LogAttrs(ctx, slog.LevelError, "Got an error when running checks", slog.Any("err", err))
251251
}
252252
checkIterationsTotal.Inc()
253+
for _, server := range gen.Servers() {
254+
server.GC()
255+
}
253256
case <-stop:
254257
ticker.Stop()
255258
slog.LogAttrs(ctx, slog.LevelInfo, "Background worker finished")

‎docs/changelog.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
parsed, instead of waiting for all checks to complete.
99
- The `sampleRateLimit` option was being applied to all requests, instead of only
1010
requests that can load samples. Now it only applies to instant and range queries.
11+
- When running `pint watch` Prometheus query cache was being cleaned prematurely
12+
while cached responses might have been still needed, resulting in unnecessary queries.
13+
Cache evictions now happen only after each `pint watch` iteration.
1114

1215
## v0.89.0
1316

‎internal/promapi/failover.go‎

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,6 @@ func (e *FailoverGroupError) IsStrict() bool {
3434
return e.isStrict
3535
}
3636

37-
func cacheCleaner(cache *queryCache, interval time.Duration, quit chan bool) {
38-
ticker := time.NewTicker(interval)
39-
for {
40-
select {
41-
case <-quit:
42-
return
43-
case <-ticker.C:
44-
cache.gc()
45-
}
46-
}
47-
}
48-
4937
type disabledChecks struct {
5038
// Key is the name of the unsupported API, value is the list of checks disabled because of it.
5139
apis map[string][]string
@@ -77,13 +65,11 @@ type FailoverGroup struct {
7765
servers []*Prometheus
7866
uptimeMetric string
7967
cacheCollector *cacheCollector
80-
quitChan chan bool
81-
82-
pathsInclude []*regexp.Regexp
83-
pathsExclude []*regexp.Regexp
84-
tags []string
85-
started bool
86-
strictErrors bool
68+
pathsInclude []*regexp.Regexp
69+
pathsExclude []*regexp.Regexp
70+
tags []string
71+
started bool
72+
strictErrors bool
8773
}
8874

8975
func NewFailoverGroup(name, uri string, servers []*Prometheus, strictErrors bool, uptimeMetric string, include, exclude []*regexp.Regexp, tags []string) *FailoverGroup {
@@ -188,8 +174,6 @@ func (fg *FailoverGroup) StartWorkers(reg *prometheus.Registry) {
188174
}
189175

190176
queryCache := newQueryCache(time.Hour, time.Now)
191-
fg.quitChan = make(chan bool)
192-
go cacheCleaner(queryCache, time.Minute*2, fg.quitChan)
193177

194178
fg.cacheCollector = newCacheCollector(queryCache, fg.name)
195179
reg.MustRegister(fg.cacheCollector)
@@ -205,10 +189,9 @@ func (fg *FailoverGroup) Close(reg *prometheus.Registry) {
205189
return
206190
}
207191
reg.Unregister(fg.cacheCollector)
208-
fg.quitChan <- true
209192
}
210193

211-
func (fg *FailoverGroup) CleanCache() {
194+
func (fg *FailoverGroup) GC() {
212195
for _, prom := range fg.servers {
213196
if prom.cache != nil {
214197
prom.cache.gc()

‎internal/promapi/failover_test.go‎

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,6 @@ import (
88
"github.com/stretchr/testify/require"
99
)
1010

11-
func TestCacheCleaner(t *testing.T) {
12-
cache := newQueryCache(time.Minute, time.Now)
13-
quit := make(chan bool)
14-
15-
// Add some entries to cache
16-
cache.set(1, nil, 0)
17-
cache.set(2, nil, 0)
18-
require.Len(t, cache.entries, 2)
19-
20-
// Start cache cleaner with very short interval
21-
go cacheCleaner(cache, time.Millisecond*10, quit)
22-
23-
// Wait for at least one gc cycle
24-
time.Sleep(time.Millisecond * 50)
25-
26-
// Stop the cleaner
27-
quit <- true
28-
}
29-
3011
func TestFailoverGroupStartWorkers(t *testing.T) {
3112
type testCaseT struct {
3213
name string
@@ -160,11 +141,11 @@ func TestFailoverGroupCleanCache(t *testing.T) {
160141

161142
if tc.expectPanics {
162143
require.Panics(t, func() {
163-
fg.CleanCache()
144+
fg.GC()
164145
})
165146
} else {
166147
require.NotPanics(t, func() {
167-
fg.CleanCache()
148+
fg.GC()
168149
})
169150
}
170151

0 commit comments

Comments
 (0)