Skip to content

Commit a918423

Browse files
committed
Add more robust cache invalidation to CachingBucket
This change makes use of `add` operations for special "lock" cache entries to ensure that when items in object storage are mutated, stale results are not immediately stored to cache again. It does this by `set`ing "lock" cache entries with a short TTL when an item in object storage is about to be mutated. This prevents reads of the item from caching the results afterwards. After the item is mutated in object storage, its cache entries (excluding the lock entries) are deleted. After the lock entries expire, reads of the item are allowed to store results in the cache again. Part of #9386
1 parent 9907ea8 commit a918423

File tree

11 files changed

+297
-92
lines changed

11 files changed

+297
-92
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ require (
2222
github.com/golang/snappy v0.0.4
2323
github.com/google/gopacket v1.1.19
2424
github.com/gorilla/mux v1.8.1
25-
github.com/grafana/dskit v0.0.0-20241007163720-de20fd2fe818
25+
github.com/grafana/dskit v0.0.0-20241009141103-2e104a8053fa
2626
github.com/grafana/e2e v0.1.2-0.20240118170847-db90b84177fc
2727
github.com/hashicorp/golang-lru v1.0.2 // indirect
2828
github.com/json-iterator/go v1.1.12
@@ -205,7 +205,7 @@ require (
205205
github.com/googleapis/enterprise-certificate-proxy v0.3.4 // indirect
206206
github.com/googleapis/gax-go/v2 v2.13.0 // indirect
207207
github.com/gosimple/slug v1.1.1 // indirect
208-
github.com/grafana/gomemcache v0.0.0-20240229205252-cd6a66d6fb56 // indirect
208+
github.com/grafana/gomemcache v0.0.0-20240229205252-cd6a66d6fb56
209209
github.com/hashicorp/consul/api v1.29.4 // indirect
210210
github.com/hashicorp/errwrap v1.1.0 // indirect
211211
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,8 +1256,8 @@ github.com/grafana-tools/sdk v0.0.0-20220919052116-6562121319fc h1:PXZQA2WCxe85T
12561256
github.com/grafana-tools/sdk v0.0.0-20220919052116-6562121319fc/go.mod h1:AHHlOEv1+GGQ3ktHMlhuTUwo3zljV3QJbC0+8o2kn+4=
12571257
github.com/grafana/alerting v0.0.0-20240926144415-27f4e81b4b6b h1:UO4mv94pG1kzKCgBKh20TXdACBCAK2vYjV3Q2MlcpEQ=
12581258
github.com/grafana/alerting v0.0.0-20240926144415-27f4e81b4b6b/go.mod h1:GMLi6d09Xqo96fCVUjNk//rcjP5NKEdjOzfWIffD5r4=
1259-
github.com/grafana/dskit v0.0.0-20241007163720-de20fd2fe818 h1:VPMurXtl+ONN13d9ge1TGTJDBsJNr9Vb1pNQHpQ/aro=
1260-
github.com/grafana/dskit v0.0.0-20241007163720-de20fd2fe818/go.mod h1:SPLNCARd4xdjCkue0O6hvuoveuS1dGJjDnfxYe405YQ=
1259+
github.com/grafana/dskit v0.0.0-20241009141103-2e104a8053fa h1:jfSQq+jfs1q0cZr9n4u9g6Wz3423VJVE9grnLpx+eS4=
1260+
github.com/grafana/dskit v0.0.0-20241009141103-2e104a8053fa/go.mod h1:SPLNCARd4xdjCkue0O6hvuoveuS1dGJjDnfxYe405YQ=
12611261
github.com/grafana/e2e v0.1.2-0.20240118170847-db90b84177fc h1:BW+LjKJDz0So5LI8UZfW5neWeKpSkWqhmGjQFzcFfLM=
12621262
github.com/grafana/e2e v0.1.2-0.20240118170847-db90b84177fc/go.mod h1:JVmqPBe8A/pZWwRoJW5ZjyALeY5OXMzPl7LrVXOdZAI=
12631263
github.com/grafana/franz-go v0.0.0-20241009101240-fa97d35e871f h1:nsrRsQHfpqs6dWxErIOS3gD6R20H/9XM0ItykNtBFW8=

pkg/ruler/rulestore/bucketclient/bucket_client_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ func TestCachingAndInvalidation(t *testing.T) {
462462

463463
cacheCfg := bucketcache.NewCachingBucketConfig()
464464
cacheCfg.CacheIter("rule-iter", mockCache, matchAll, time.Minute, iterCodec)
465-
cacheCfg.CacheGet("rule-groups", mockCache, matchAll, 1024^2, time.Minute, time.Minute, time.Minute, true)
465+
cacheCfg.CacheGet("rule-groups", mockCache, matchAll, 1024^2, time.Minute, time.Minute, time.Minute)
466466
cacheClient, err := bucketcache.NewCachingBucket("rule-store", baseClient, cacheCfg, log.NewNopLogger(), prometheus.NewPedanticRegistry())
467467
require.NoError(t, err)
468468

pkg/ruler/store_mock_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func newInMemoryRuleStore(t *testing.T) (*cache.InstrumentedMockCache, *bucketcl
5555
mockCache := cache.NewInstrumentedMockCache()
5656
cfg := bucketcache.NewCachingBucketConfig()
5757
cfg.CacheIter("iter", mockCache, isNotTenantsDir, time.Minute, &bucketcache.JSONIterCodec{})
58-
cfg.CacheGet("rules", mockCache, isRuleGroup, 1024^2, time.Minute, time.Minute, time.Minute, true)
58+
cfg.CacheGet("rules", mockCache, isRuleGroup, 1024^2, time.Minute, time.Minute, time.Minute)
5959

6060
cachingBkt, err := bucketcache.NewCachingBucket("rules", bkt, cfg, log.NewNopLogger(), prometheus.NewPedanticRegistry())
6161
require.NoError(t, err)

0 commit comments

Comments
 (0)