Skip to content

Commit ab3ca0a

Browse files
authored
use caching bucket for compactor, except cleaner (#5682)
* use caching bucket for compactor, except cleaner Signed-off-by: Wen Xu <[email protected]> * disable chunks cache for compactor, do not cache block deletion marker and tenant deletion marker for compactor Signed-off-by: Wen Xu <[email protected]> * turn on cachingbucketenabled in compactor test and add more descript to the feature flag Signed-off-by: Wen Xu <[email protected]> * remove cachingBucketEnabled flag in unit test Signed-off-by: Wen Xu <[email protected]> * properly disable chunkscache Signed-off-by: Wen Xu <[email protected]> * fix lint Signed-off-by: Wen Xu <[email protected]> --------- Signed-off-by: Wen Xu <[email protected]>
1 parent a85f3c1 commit ab3ca0a

File tree

6 files changed

+112
-12
lines changed

6 files changed

+112
-12
lines changed

docs/blocks-storage/compactor.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,4 +298,9 @@ compactor:
298298
# When enabled, index verification will ignore out of order label names.
299299
# CLI flag: -compactor.accept-malformed-index
300300
[accept_malformed_index: <boolean> | default = false]
301+
302+
# When enabled, caching bucket will be used for compactor, except cleaner
303+
# service, which serves as the source of truth for block status
304+
# CLI flag: -compactor.caching-bucket-enabled
305+
[caching_bucket_enabled: <boolean> | default = false]
301306
```

docs/configuration/config-file-reference.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2021,6 +2021,11 @@ sharding_ring:
20212021
# When enabled, index verification will ignore out of order label names.
20222022
# CLI flag: -compactor.accept-malformed-index
20232023
[accept_malformed_index: <boolean> | default = false]
2024+
2025+
# When enabled, caching bucket will be used for compactor, except cleaner
2026+
# service, which serves as the source of truth for block status
2027+
# CLI flag: -compactor.caching-bucket-enabled
2028+
[caching_bucket_enabled: <boolean> | default = false]
20242029
```
20252030

20262031
### `configs_config`

pkg/compactor/compactor.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/thanos-io/thanos/pkg/block/metadata"
2323
"github.com/thanos-io/thanos/pkg/compact"
2424
"github.com/thanos-io/thanos/pkg/compact/downsample"
25+
"github.com/thanos-io/thanos/pkg/extprom"
2526

2627
"github.com/cortexproject/cortex/pkg/ring"
2728
"github.com/cortexproject/cortex/pkg/storage/bucket"
@@ -209,6 +210,7 @@ type Config struct {
209210
BlockVisitMarkerFileUpdateInterval time.Duration `yaml:"block_visit_marker_file_update_interval"`
210211

211212
AcceptMalformedIndex bool `yaml:"accept_malformed_index"`
213+
CachingBucketEnabled bool `yaml:"caching_bucket_enabled"`
212214
}
213215

214216
// RegisterFlags registers the Compactor flags.
@@ -247,6 +249,7 @@ func (cfg *Config) RegisterFlags(f *flag.FlagSet) {
247249
f.DurationVar(&cfg.BlockVisitMarkerFileUpdateInterval, "compactor.block-visit-marker-file-update-interval", 1*time.Minute, "How frequently block visit marker file should be updated duration compaction.")
248250

249251
f.BoolVar(&cfg.AcceptMalformedIndex, "compactor.accept-malformed-index", false, "When enabled, index verification will ignore out of order label names.")
252+
f.BoolVar(&cfg.CachingBucketEnabled, "compactor.caching-bucket-enabled", false, "When enabled, caching bucket will be used for compactor, except cleaner service, which serves as the source of truth for block status")
250253
}
251254

252255
func (cfg *Config) Validate(limits validation.Limits) error {
@@ -588,6 +591,17 @@ func (c *Compactor) starting(ctx context.Context) error {
588591
return errors.Wrap(err, "failed to start the blocks cleaner")
589592
}
590593

594+
if c.compactorCfg.CachingBucketEnabled {
595+
matchers := cortex_tsdb.NewMatchers()
596+
// Do not cache tenant deletion marker and block deletion marker for compactor
597+
matchers.SetMetaFileMatcher(func(name string) bool {
598+
return strings.HasSuffix(name, "/"+metadata.MetaFilename)
599+
})
600+
c.bucketClient, err = cortex_tsdb.CreateCachingBucket(cortex_tsdb.ChunksCacheConfig{}, c.storageCfg.BucketStore.MetadataCache, matchers, c.bucketClient, c.logger, extprom.WrapRegistererWith(prometheus.Labels{"component": "compactor"}, c.registerer))
601+
if err != nil {
602+
return errors.Wrap(err, "create caching bucket")
603+
}
604+
}
591605
return nil
592606
}
593607

pkg/querier/blocks_store_queryable.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ func NewBlocksStoreQueryableFromConfig(querierCfg Config, gatewayCfg storegatewa
183183
}
184184

185185
// Blocks finder doesn't use chunks, but we pass config for consistency.
186-
cachingBucket, err := cortex_tsdb.CreateCachingBucket(storageCfg.BucketStore.ChunksCache, storageCfg.BucketStore.MetadataCache, bucketClient, logger, extprom.WrapRegistererWith(prometheus.Labels{"component": "querier"}, reg))
186+
matchers := cortex_tsdb.NewMatchers()
187+
cachingBucket, err := cortex_tsdb.CreateCachingBucket(storageCfg.BucketStore.ChunksCache, storageCfg.BucketStore.MetadataCache, matchers, bucketClient, logger, extprom.WrapRegistererWith(prometheus.Labels{"component": "querier"}, reg))
187188
if err != nil {
188189
return nil, errors.Wrap(err, "create caching bucket")
189190
}

pkg/storage/tsdb/caching_bucket.go

Lines changed: 84 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func (cfg *MetadataCacheConfig) Validate() error {
110110
return cfg.CacheBackend.Validate()
111111
}
112112

113-
func CreateCachingBucket(chunksConfig ChunksCacheConfig, metadataConfig MetadataCacheConfig, bkt objstore.InstrumentedBucket, logger log.Logger, reg prometheus.Registerer) (objstore.InstrumentedBucket, error) {
113+
func CreateCachingBucket(chunksConfig ChunksCacheConfig, metadataConfig MetadataCacheConfig, matchers Matchers, bkt objstore.InstrumentedBucket, logger log.Logger, reg prometheus.Registerer) (objstore.InstrumentedBucket, error) {
114114
cfg := cache.NewCachingBucketConfig()
115115
cachingConfigured := false
116116

@@ -121,7 +121,7 @@ func CreateCachingBucket(chunksConfig ChunksCacheConfig, metadataConfig Metadata
121121
if chunksCache != nil {
122122
cachingConfigured = true
123123
chunksCache = cache.NewTracingCache(chunksCache)
124-
cfg.CacheGetRange("chunks", chunksCache, isTSDBChunkFile, chunksConfig.SubrangeSize, chunksConfig.AttributesTTL, chunksConfig.SubrangeTTL, chunksConfig.MaxGetRangeRequests)
124+
cfg.CacheGetRange("chunks", chunksCache, matchers.GetChunksMatcher(), chunksConfig.SubrangeSize, chunksConfig.AttributesTTL, chunksConfig.SubrangeTTL, chunksConfig.MaxGetRangeRequests)
125125
}
126126

127127
metadataCache, err := createCache("metadata-cache", &metadataConfig.CacheBackend, logger, reg)
@@ -132,16 +132,16 @@ func CreateCachingBucket(chunksConfig ChunksCacheConfig, metadataConfig Metadata
132132
cachingConfigured = true
133133
metadataCache = cache.NewTracingCache(metadataCache)
134134

135-
cfg.CacheExists("metafile", metadataCache, isMetaFile, metadataConfig.MetafileExistsTTL, metadataConfig.MetafileDoesntExistTTL)
136-
cfg.CacheGet("metafile", metadataCache, isMetaFile, metadataConfig.MetafileMaxSize, metadataConfig.MetafileContentTTL, metadataConfig.MetafileExistsTTL, metadataConfig.MetafileDoesntExistTTL)
137-
cfg.CacheAttributes("metafile", metadataCache, isMetaFile, metadataConfig.MetafileAttributesTTL)
138-
cfg.CacheAttributes("block-index", metadataCache, isBlockIndexFile, metadataConfig.BlockIndexAttributesTTL)
139-
cfg.CacheGet("bucket-index", metadataCache, isBucketIndexFiles, metadataConfig.BucketIndexMaxSize, metadataConfig.BucketIndexContentTTL /* do not cache exist / not exist: */, 0, 0)
135+
cfg.CacheExists("metafile", metadataCache, matchers.GetMetafileMatcher(), metadataConfig.MetafileExistsTTL, metadataConfig.MetafileDoesntExistTTL)
136+
cfg.CacheGet("metafile", metadataCache, matchers.GetMetafileMatcher(), metadataConfig.MetafileMaxSize, metadataConfig.MetafileContentTTL, metadataConfig.MetafileExistsTTL, metadataConfig.MetafileDoesntExistTTL)
137+
cfg.CacheAttributes("metafile", metadataCache, matchers.GetMetafileMatcher(), metadataConfig.MetafileAttributesTTL)
138+
cfg.CacheAttributes("block-index", metadataCache, matchers.GetBlockIndexMatcher(), metadataConfig.BlockIndexAttributesTTL)
139+
cfg.CacheGet("bucket-index", metadataCache, matchers.GetBucketIndexMatcher(), metadataConfig.BucketIndexMaxSize, metadataConfig.BucketIndexContentTTL /* do not cache exist / not exist: */, 0, 0)
140140

141141
codec := snappyIterCodec{storecache.JSONIterCodec{}}
142-
cfg.CacheIter("tenants-iter", metadataCache, isTenantsDir, metadataConfig.TenantsListTTL, codec)
143-
cfg.CacheIter("tenant-blocks-iter", metadataCache, isTenantBlocksDir, metadataConfig.TenantBlocksListTTL, codec)
144-
cfg.CacheIter("chunks-iter", metadataCache, isChunksDir, metadataConfig.ChunksListTTL, codec)
142+
cfg.CacheIter("tenants-iter", metadataCache, matchers.GetTenantsIterMatcher(), metadataConfig.TenantsListTTL, codec)
143+
cfg.CacheIter("tenant-blocks-iter", metadataCache, matchers.GetTenantBlocksIterMatcher(), metadataConfig.TenantBlocksListTTL, codec)
144+
cfg.CacheIter("chunks-iter", metadataCache, matchers.GetChunksIterMatcher(), metadataConfig.ChunksListTTL, codec)
145145
}
146146

147147
if !cachingConfigured {
@@ -178,6 +178,80 @@ func createCache(cacheName string, cacheBackend *CacheBackend, logger log.Logger
178178
}
179179
}
180180

181+
type Matchers struct {
182+
matcherMap map[string]func(string) bool
183+
}
184+
185+
func NewMatchers() Matchers {
186+
matcherMap := make(map[string]func(string) bool)
187+
matcherMap["chunks"] = isTSDBChunkFile
188+
matcherMap["metafile"] = isMetaFile
189+
matcherMap["block-index"] = isBlockIndexFile
190+
matcherMap["bucket-index"] = isBucketIndexFiles
191+
matcherMap["tenants-iter"] = isTenantsDir
192+
matcherMap["tenant-blocks-iter"] = isTenantBlocksDir
193+
matcherMap["chunks-iter"] = isChunksDir
194+
return Matchers{
195+
matcherMap: matcherMap,
196+
}
197+
}
198+
199+
func (m *Matchers) SetMetaFileMatcher(f func(string) bool) {
200+
m.matcherMap["metafile"] = f
201+
}
202+
203+
func (m *Matchers) SetChunksMatcher(f func(string) bool) {
204+
m.matcherMap["chunks"] = f
205+
}
206+
207+
func (m *Matchers) SetBlockIndexMatcher(f func(string) bool) {
208+
m.matcherMap["block-index"] = f
209+
}
210+
211+
func (m *Matchers) SetBucketIndexMatcher(f func(string) bool) {
212+
m.matcherMap["bucket-index"] = f
213+
}
214+
215+
func (m *Matchers) SetTenantsIterMatcher(f func(string) bool) {
216+
m.matcherMap["tenants-iter"] = f
217+
}
218+
219+
func (m *Matchers) SetTenantBlocksIterMatcher(f func(string) bool) {
220+
m.matcherMap["tenant-blocks-iter"] = f
221+
}
222+
223+
func (m *Matchers) SetChunksIterMatcher(f func(string) bool) {
224+
m.matcherMap["chunks-iter"] = f
225+
}
226+
227+
func (m *Matchers) GetChunksMatcher() func(string) bool {
228+
return m.matcherMap["chunks"]
229+
}
230+
231+
func (m *Matchers) GetMetafileMatcher() func(string) bool {
232+
return m.matcherMap["metafile"]
233+
}
234+
235+
func (m *Matchers) GetBlockIndexMatcher() func(string) bool {
236+
return m.matcherMap["block-index"]
237+
}
238+
239+
func (m *Matchers) GetBucketIndexMatcher() func(string) bool {
240+
return m.matcherMap["bucket-index"]
241+
}
242+
243+
func (m *Matchers) GetTenantsIterMatcher() func(string) bool {
244+
return m.matcherMap["tenants-iter"]
245+
}
246+
247+
func (m *Matchers) GetTenantBlocksIterMatcher() func(string) bool {
248+
return m.matcherMap["tenant-blocks-iter"]
249+
}
250+
251+
func (m *Matchers) GetChunksIterMatcher() func(string) bool {
252+
return m.matcherMap["chunks-iter"]
253+
}
254+
181255
var chunksMatcher = regexp.MustCompile(`^.*/chunks/\d+$`)
182256

183257
func isTSDBChunkFile(name string) bool { return chunksMatcher.MatchString(name) }

pkg/storegateway/bucket_stores.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ var ErrTooManyInflightRequests = status.Error(codes.ResourceExhausted, "too many
8888

8989
// NewBucketStores makes a new BucketStores.
9090
func NewBucketStores(cfg tsdb.BlocksStorageConfig, shardingStrategy ShardingStrategy, bucketClient objstore.InstrumentedBucket, limits *validation.Overrides, logLevel logging.Level, logger log.Logger, reg prometheus.Registerer) (*BucketStores, error) {
91-
cachingBucket, err := tsdb.CreateCachingBucket(cfg.BucketStore.ChunksCache, cfg.BucketStore.MetadataCache, bucketClient, logger, reg)
91+
matchers := tsdb.NewMatchers()
92+
cachingBucket, err := tsdb.CreateCachingBucket(cfg.BucketStore.ChunksCache, cfg.BucketStore.MetadataCache, matchers, bucketClient, logger, reg)
9293
if err != nil {
9394
return nil, errors.Wrapf(err, "create caching bucket")
9495
}

0 commit comments

Comments
 (0)