Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cache: Enable embedded cache if no other cache is configured. #10620

Merged
merged 15 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* [10395](https://github.com/grafana/loki/pull/10395/) **shantanualshi** Remove deprecated `split_queries_by_interval` and `forward_headers_list` configuration options in the `query_range` section
* [10456](https://github.com/grafana/loki/pull/10456) **dannykopping** Add `loki_distributor_ingester_append_timeouts_total` metric, remove `loki_distributor_ingester_append_failures_total` metric
* [10534](https://github.com/grafana/loki/pull/10534) **chaudum** Remove configuration `use_boltdb_shipper_as_backup`
* [10620](https://github.com/grafana/loki/pull/10620) **ashwanthgoli** Enable embedded cache if no other cache is explicitly enabled.

##### Fixes

Expand Down
6 changes: 2 additions & 4 deletions cmd/migrate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,15 @@ func main() {

// This is a little brittle, if we add a new cache it may easily get missed here but it's important to disable
// any of the chunk caches to save on memory because we write chunks to the cache when we call Put operations on the store.
sourceConfig.ChunkStoreConfig.ChunkCacheConfig.EnableFifoCache = false
sourceConfig.ChunkStoreConfig.ChunkCacheConfig.EmbeddedCache.Enabled = false
sourceConfig.ChunkStoreConfig.ChunkCacheConfig.MemcacheClient = defaultsConfig.ChunkStoreConfig.ChunkCacheConfig.MemcacheClient
sourceConfig.ChunkStoreConfig.ChunkCacheConfig.Redis = defaultsConfig.ChunkStoreConfig.ChunkCacheConfig.Redis
sourceConfig.ChunkStoreConfig.WriteDedupeCacheConfig.EnableFifoCache = false
sourceConfig.ChunkStoreConfig.WriteDedupeCacheConfig.MemcacheClient = defaultsConfig.ChunkStoreConfig.WriteDedupeCacheConfig.MemcacheClient
sourceConfig.ChunkStoreConfig.WriteDedupeCacheConfig.Redis = defaultsConfig.ChunkStoreConfig.WriteDedupeCacheConfig.Redis

destConfig.ChunkStoreConfig.ChunkCacheConfig.EnableFifoCache = false
destConfig.ChunkStoreConfig.ChunkCacheConfig.EmbeddedCache.Enabled = false
destConfig.ChunkStoreConfig.ChunkCacheConfig.MemcacheClient = defaultsConfig.ChunkStoreConfig.ChunkCacheConfig.MemcacheClient
destConfig.ChunkStoreConfig.ChunkCacheConfig.Redis = defaultsConfig.ChunkStoreConfig.ChunkCacheConfig.Redis
destConfig.ChunkStoreConfig.WriteDedupeCacheConfig.EnableFifoCache = false
destConfig.ChunkStoreConfig.WriteDedupeCacheConfig.MemcacheClient = defaultsConfig.ChunkStoreConfig.WriteDedupeCacheConfig.MemcacheClient
destConfig.ChunkStoreConfig.WriteDedupeCacheConfig.Redis = defaultsConfig.ChunkStoreConfig.WriteDedupeCacheConfig.Redis

Expand Down
1 change: 1 addition & 0 deletions docs/sources/setup/upgrade/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ The previous default value `false` is applied.
5. `experimental.ruler.enable-api` is removed. Use `ruler.enable-api` instead.
6. `split_queries_by_interval` is removed from `query_range` YAML section. You can instead configure it in [Limits Config](/docs/loki/latest/configuration/#limits_config).
7. `frontend.forward-headers-list` CLI flag and its corresponding YAML setting are removed.
8. `frontend.cache-split-interval` CLI flag is removed. Results caching interval is now determined by `querier.split-queries-by-interval`.

#### Distributor metric changes

Expand Down
19 changes: 7 additions & 12 deletions pkg/loki/config_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (c *ConfigWrapper) ApplyDynamicConfig() cfg.Source {
betterTSDBShipperDefaults(r, &defaults, r.SchemaConfig.Configs[i])
}

applyFIFOCacheConfig(r)
applyEmbeddedCacheConfig(r)
applyIngesterFinalSleep(r)
applyIngesterReplicationFactor(r)
applyChunkRetain(r, &defaults)
Expand Down Expand Up @@ -557,23 +557,18 @@ func betterTSDBShipperDefaults(cfg, defaults *ConfigWrapper, period config.Perio
}
}

// applyFIFOCacheConfig turns on FIFO cache for the chunk store, for the query range results,
// and for the index stats results, but only if no other cache storage is configured (redis or memcache).
// This behavior is only applied for the chunk store cache, for the query range results cache, and for
// the index stats results (i.e: not applicable for the index queries cache or for the write dedupe cache).
func applyFIFOCacheConfig(r *ConfigWrapper) {
// applyEmbeddedCacheConfig turns on Embedded cache for the chunk store, query range results,
// index stats and volume results only if no other cache storage is configured (redis or memcache).
// Not applicable for the index queries cache or for the write dedupe cache.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

write dedupe cache

Possibly not for this PR, but I think we can remove this cache; it's been deprecated for ages.

func applyEmbeddedCacheConfig(r *ConfigWrapper) {
chunkCacheConfig := r.ChunkStoreConfig.ChunkCacheConfig
if !cache.IsCacheConfigured(chunkCacheConfig) {
r.ChunkStoreConfig.ChunkCacheConfig.EnableFifoCache = true
r.ChunkStoreConfig.ChunkCacheConfig.EmbeddedCache.Enabled = true
}

resultsCacheConfig := r.QueryRange.ResultsCacheConfig.CacheConfig
if !cache.IsCacheConfigured(resultsCacheConfig) {
r.QueryRange.ResultsCacheConfig.CacheConfig.EnableFifoCache = true
// The query results fifocache is still in Cortex so we couldn't change the flag defaults
// so instead we will override them here.
r.QueryRange.ResultsCacheConfig.CacheConfig.Fifocache.MaxSizeBytes = "1GB"
r.QueryRange.ResultsCacheConfig.CacheConfig.Fifocache.TTL = 1 * time.Hour
dannykopping marked this conversation as resolved.
Show resolved Hide resolved
r.QueryRange.ResultsCacheConfig.CacheConfig.EmbeddedCache.Enabled = true
}

indexStatsCacheConfig := r.QueryRange.StatsCacheConfig.CacheConfig
Expand Down
85 changes: 40 additions & 45 deletions pkg/loki/config_wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -839,19 +839,14 @@ ingester:
})

t.Run("embedded-cache setting is applied to result caches", func(t *testing.T) {
// ensure they are all false by default
config, _, _ := configWrapperFromYAML(t, minimalConfig, nil)
assert.False(t, config.QueryRange.ResultsCacheConfig.CacheConfig.EmbeddedCache.Enabled)

configFileString := `---
query_range:
results_cache:
cache:
embedded_cache:
enabled: true`

config, _ = testContext(configFileString, nil)

config, _ := testContext(configFileString, nil)
assert.True(t, config.QueryRange.ResultsCacheConfig.CacheConfig.EmbeddedCache.Enabled)
})
}
Expand All @@ -863,9 +858,9 @@ query_range:
memcached_client:
host: memcached.host.org`

func TestDefaultFIFOCacheBehavior(t *testing.T) {
func TestDefaultEmbeddedCacheBehavior(t *testing.T) {
t.Run("for the chunk cache config", func(t *testing.T) {
t.Run("no FIFO cache enabled by default if Redis is set", func(t *testing.T) {
t.Run("no embedded cache enabled by default if Redis is set", func(t *testing.T) {
configFileString := `---
chunk_store_config:
chunk_cache_config:
Expand All @@ -874,10 +869,10 @@ chunk_store_config:

config, _, _ := configWrapperFromYAML(t, configFileString, nil)
assert.EqualValues(t, "endpoint.redis.org", config.ChunkStoreConfig.ChunkCacheConfig.Redis.Endpoint)
assert.False(t, config.ChunkStoreConfig.ChunkCacheConfig.EnableFifoCache)
assert.False(t, config.ChunkStoreConfig.ChunkCacheConfig.EmbeddedCache.Enabled)
})

t.Run("no FIFO cache enabled by default if Memcache is set", func(t *testing.T) {
t.Run("no embedded cache enabled by default if Memcache is set", func(t *testing.T) {
configFileString := `---
chunk_store_config:
chunk_cache_config:
Expand All @@ -886,17 +881,17 @@ chunk_store_config:

config, _, _ := configWrapperFromYAML(t, configFileString, nil)
assert.EqualValues(t, "host.memcached.org", config.ChunkStoreConfig.ChunkCacheConfig.MemcacheClient.Host)
assert.False(t, config.ChunkStoreConfig.ChunkCacheConfig.EnableFifoCache)
assert.False(t, config.ChunkStoreConfig.ChunkCacheConfig.EmbeddedCache.Enabled)
})

t.Run("FIFO cache is enabled by default if no other cache is set", func(t *testing.T) {
t.Run("embedded cache is enabled by default if no other cache is set", func(t *testing.T) {
config, _, _ := configWrapperFromYAML(t, minimalConfig, nil)
assert.True(t, config.ChunkStoreConfig.ChunkCacheConfig.EnableFifoCache)
assert.True(t, config.ChunkStoreConfig.ChunkCacheConfig.EmbeddedCache.Enabled)
})
})

t.Run("for the write dedupe cache config", func(t *testing.T) {
t.Run("no FIFO cache enabled by default if Redis is set", func(t *testing.T) {
t.Run("no embedded cache enabled by default if Redis is set", func(t *testing.T) {
configFileString := `---
chunk_store_config:
write_dedupe_cache_config:
Expand All @@ -905,10 +900,10 @@ chunk_store_config:

config, _, _ := configWrapperFromYAML(t, configFileString, nil)
assert.EqualValues(t, "endpoint.redis.org", config.ChunkStoreConfig.WriteDedupeCacheConfig.Redis.Endpoint)
assert.False(t, config.ChunkStoreConfig.WriteDedupeCacheConfig.EnableFifoCache)
assert.False(t, config.ChunkStoreConfig.WriteDedupeCacheConfig.EmbeddedCache.Enabled)
})

t.Run("no FIFO cache enabled by default if Memcache is set", func(t *testing.T) {
t.Run("no embedded cache enabled by default if Memcache is set", func(t *testing.T) {
configFileString := `---
chunk_store_config:
write_dedupe_cache_config:
Expand All @@ -917,17 +912,17 @@ chunk_store_config:

config, _, _ := configWrapperFromYAML(t, configFileString, nil)
assert.EqualValues(t, "host.memcached.org", config.ChunkStoreConfig.WriteDedupeCacheConfig.MemcacheClient.Host)
assert.False(t, config.ChunkStoreConfig.WriteDedupeCacheConfig.EnableFifoCache)
assert.False(t, config.ChunkStoreConfig.WriteDedupeCacheConfig.EmbeddedCache.Enabled)
})

t.Run("no FIFO cache is enabled by default even if no other cache is set", func(t *testing.T) {
t.Run("no embedded cache is enabled by default even if no other cache is set", func(t *testing.T) {
config, _, _ := configWrapperFromYAML(t, minimalConfig, nil)
assert.False(t, config.ChunkStoreConfig.WriteDedupeCacheConfig.EnableFifoCache)
assert.False(t, config.ChunkStoreConfig.WriteDedupeCacheConfig.EmbeddedCache.Enabled)
})
})

t.Run("for the index queries cache config", func(t *testing.T) {
t.Run("no FIFO cache enabled by default if Redis is set", func(t *testing.T) {
t.Run("no embedded cache enabled by default if Redis is set", func(t *testing.T) {
configFileString := `---
storage_config:
index_queries_cache_config:
Expand All @@ -936,10 +931,10 @@ storage_config:

config, _, _ := configWrapperFromYAML(t, configFileString, nil)
assert.EqualValues(t, "endpoint.redis.org", config.StorageConfig.IndexQueriesCacheConfig.Redis.Endpoint)
assert.False(t, config.StorageConfig.IndexQueriesCacheConfig.EnableFifoCache)
assert.False(t, config.StorageConfig.IndexQueriesCacheConfig.EmbeddedCache.Enabled)
})

t.Run("no FIFO cache enabled by default if Memcache is set", func(t *testing.T) {
t.Run("no embedded cache enabled by default if Memcache is set", func(t *testing.T) {
configFileString := `---
storage_config:
index_queries_cache_config:
Expand All @@ -949,17 +944,17 @@ storage_config:
config, _, _ := configWrapperFromYAML(t, configFileString, nil)

assert.EqualValues(t, "host.memcached.org", config.StorageConfig.IndexQueriesCacheConfig.MemcacheClient.Host)
assert.False(t, config.StorageConfig.IndexQueriesCacheConfig.EnableFifoCache)
assert.False(t, config.StorageConfig.IndexQueriesCacheConfig.EmbeddedCache.Enabled)
})

t.Run("no FIFO cache is enabled by default even if no other cache is set", func(t *testing.T) {
t.Run("no embedded cache is enabled by default even if no other cache is set", func(t *testing.T) {
config, _, _ := configWrapperFromYAML(t, minimalConfig, nil)
assert.False(t, config.StorageConfig.IndexQueriesCacheConfig.EnableFifoCache)
assert.False(t, config.StorageConfig.IndexQueriesCacheConfig.EmbeddedCache.Enabled)
})
})

t.Run("for the query range results cache config", func(t *testing.T) {
t.Run("no FIFO cache enabled by default if Redis is set", func(t *testing.T) {
t.Run("no embedded cache enabled by default if Redis is set", func(t *testing.T) {
configFileString := `---
query_range:
results_cache:
Expand All @@ -969,23 +964,23 @@ query_range:

config, _, _ := configWrapperFromYAML(t, configFileString, nil)
assert.EqualValues(t, config.QueryRange.ResultsCacheConfig.CacheConfig.Redis.Endpoint, "endpoint.redis.org")
assert.False(t, config.QueryRange.ResultsCacheConfig.CacheConfig.EnableFifoCache)
assert.False(t, config.QueryRange.ResultsCacheConfig.CacheConfig.EmbeddedCache.Enabled)
})

t.Run("no FIFO cache enabled by default if Memcache is set", func(t *testing.T) {
t.Run("no embedded cache enabled by default if Memcache is set", func(t *testing.T) {
config, _, _ := configWrapperFromYAML(t, defaultResulsCacheString, nil)
assert.EqualValues(t, "memcached.host.org", config.QueryRange.ResultsCacheConfig.CacheConfig.MemcacheClient.Host)
assert.False(t, config.QueryRange.ResultsCacheConfig.CacheConfig.EnableFifoCache)
assert.False(t, config.QueryRange.ResultsCacheConfig.CacheConfig.EmbeddedCache.Enabled)
})

t.Run("FIFO cache is enabled by default if no other cache is set", func(t *testing.T) {
t.Run("embedded cache is enabled by default if no other cache is set", func(t *testing.T) {
config, _, _ := configWrapperFromYAML(t, minimalConfig, nil)
assert.True(t, config.QueryRange.ResultsCacheConfig.CacheConfig.EnableFifoCache)
assert.True(t, config.QueryRange.ResultsCacheConfig.CacheConfig.EmbeddedCache.Enabled)
})
})

t.Run("for the index stats results cache config", func(t *testing.T) {
t.Run("no FIFO cache enabled by default if Redis is set", func(t *testing.T) {
t.Run("no embedded cache enabled by default if Redis is set", func(t *testing.T) {
configFileString := `---
query_range:
index_stats_results_cache:
Expand All @@ -995,10 +990,10 @@ query_range:

config, _, _ := configWrapperFromYAML(t, configFileString, nil)
assert.EqualValues(t, config.QueryRange.StatsCacheConfig.CacheConfig.Redis.Endpoint, "endpoint.redis.org")
assert.False(t, config.QueryRange.StatsCacheConfig.CacheConfig.EnableFifoCache)
assert.False(t, config.QueryRange.StatsCacheConfig.CacheConfig.EmbeddedCache.Enabled)
})

t.Run("no FIFO cache enabled by default if Memcache is set", func(t *testing.T) {
t.Run("no embedded cache enabled by default if Memcache is set", func(t *testing.T) {
configFileString := `---
query_range:
index_stats_results_cache:
Expand All @@ -1008,23 +1003,23 @@ query_range:

config, _, _ := configWrapperFromYAML(t, configFileString, nil)
assert.EqualValues(t, "memcached.host.org", config.QueryRange.StatsCacheConfig.CacheConfig.MemcacheClient.Host)
assert.False(t, config.QueryRange.StatsCacheConfig.CacheConfig.EnableFifoCache)
assert.False(t, config.QueryRange.StatsCacheConfig.CacheConfig.EmbeddedCache.Enabled)
})

t.Run("FIFO cache is enabled by default if no other cache is set", func(t *testing.T) {
t.Run("embedded cache is enabled by default if no other cache is set", func(t *testing.T) {
config, _, _ := configWrapperFromYAML(t, minimalConfig, nil)
assert.True(t, config.QueryRange.StatsCacheConfig.CacheConfig.EnableFifoCache)
assert.True(t, config.QueryRange.StatsCacheConfig.CacheConfig.EmbeddedCache.Enabled)
})

t.Run("gets results cache config if not configured directly", func(t *testing.T) {
config, _, _ := configWrapperFromYAML(t, defaultResulsCacheString, nil)
assert.EqualValues(t, "memcached.host.org", config.QueryRange.StatsCacheConfig.CacheConfig.MemcacheClient.Host)
assert.False(t, config.QueryRange.StatsCacheConfig.CacheConfig.EnableFifoCache)
assert.False(t, config.QueryRange.StatsCacheConfig.CacheConfig.EmbeddedCache.Enabled)
})
})

t.Run("for the volume results cache config", func(t *testing.T) {
t.Run("no FIFO cache enabled by default if Redis is set", func(t *testing.T) {
t.Run("no embedded cache enabled by default if Redis is set", func(t *testing.T) {
configFileString := `---
query_range:
volume_results_cache:
Expand All @@ -1034,10 +1029,10 @@ query_range:

config, _, _ := configWrapperFromYAML(t, configFileString, nil)
assert.EqualValues(t, config.QueryRange.VolumeCacheConfig.CacheConfig.Redis.Endpoint, "endpoint.redis.org")
assert.False(t, config.QueryRange.VolumeCacheConfig.CacheConfig.EnableFifoCache)
assert.False(t, config.QueryRange.VolumeCacheConfig.CacheConfig.EmbeddedCache.Enabled)
})

t.Run("no FIFO cache enabled by default if Memcache is set", func(t *testing.T) {
t.Run("no embedded cache enabled by default if Memcache is set", func(t *testing.T) {
configFileString := `---
query_range:
volume_results_cache:
Expand All @@ -1047,18 +1042,18 @@ query_range:

config, _, _ := configWrapperFromYAML(t, configFileString, nil)
assert.EqualValues(t, "memcached.host.org", config.QueryRange.VolumeCacheConfig.CacheConfig.MemcacheClient.Host)
assert.False(t, config.QueryRange.VolumeCacheConfig.CacheConfig.EnableFifoCache)
assert.False(t, config.QueryRange.VolumeCacheConfig.CacheConfig.EmbeddedCache.Enabled)
})

t.Run("FIFO cache is enabled by default if no other cache is set", func(t *testing.T) {
t.Run("embedded cache is enabled by default if no other cache is set", func(t *testing.T) {
config, _, _ := configWrapperFromYAML(t, minimalConfig, nil)
assert.True(t, config.QueryRange.VolumeCacheConfig.CacheConfig.EnableFifoCache)
assert.True(t, config.QueryRange.VolumeCacheConfig.CacheConfig.EmbeddedCache.Enabled)
})

t.Run("gets results cache config if not configured directly", func(t *testing.T) {
config, _, _ := configWrapperFromYAML(t, defaultResulsCacheString, nil)
assert.EqualValues(t, "memcached.host.org", config.QueryRange.VolumeCacheConfig.CacheConfig.MemcacheClient.Host)
assert.False(t, config.QueryRange.VolumeCacheConfig.CacheConfig.EnableFifoCache)
assert.False(t, config.QueryRange.VolumeCacheConfig.CacheConfig.EmbeddedCache.Enabled)
})
})
}
Expand Down
7 changes: 1 addition & 6 deletions pkg/querier/queryrange/queryrangebase/results_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/go-kit/log/level"
"github.com/gogo/protobuf/proto"
"github.com/gogo/protobuf/types"
"github.com/grafana/dskit/flagext"
"github.com/grafana/dskit/httpgrpc"
"github.com/grafana/dskit/user"
"github.com/opentracing/opentracing-go"
Expand All @@ -31,7 +30,6 @@ import (

"github.com/grafana/loki/pkg/logproto"
"github.com/grafana/loki/pkg/storage/chunk/cache"
util_log "github.com/grafana/loki/pkg/util/log"
"github.com/grafana/loki/pkg/util/math"
"github.com/grafana/loki/pkg/util/spanlogger"
"github.com/grafana/loki/pkg/util/validation"
Expand Down Expand Up @@ -80,10 +78,7 @@ type ResultsCacheConfig struct {

func (cfg *ResultsCacheConfig) RegisterFlagsWithPrefix(f *flag.FlagSet, prefix string) {
cfg.CacheConfig.RegisterFlagsWithPrefix(prefix, "", f)

f.StringVar(&cfg.Compression, prefix+"compression", "", "Use compression in cache. The default is an empty value '', which disables compression. Supported values are: 'snappy' and ''.")
//lint:ignore faillint Need to pass the global logger like this for warning on deprecated methods
flagext.DeprecatedFlag(f, prefix+"cache-split-interval", "Deprecated: The maximum interval expected for each request, results will be cached per single interval. This behavior is now determined by querier.split-queries-by-interval.", util_log.Logger)
}

// RegisterFlags registers flags.
Expand All @@ -99,7 +94,7 @@ func (cfg *ResultsCacheConfig) Validate() error {
return errors.Errorf("unsupported compression type: %s", cfg.Compression)
}

return cfg.CacheConfig.Validate()
return nil
}

// Extractor is used by the cache to extract a subset of a response from a cache entry.
Expand Down
Loading