From 2c214596ead4b083f8483d2c61b3c94f710ab6cf Mon Sep 17 00:00:00 2001 From: Ben Ye Date: Fri, 7 Aug 2020 16:17:17 -0400 Subject: [PATCH] add config flags for Cortex fifo cache Signed-off-by: Ben Ye --- cmd/thanos/query-frontend.go | 24 +++++++++++++++++++++++- pkg/queryfrontend/cache.go | 25 +++++++++++++++++++++++++ pkg/queryfrontend/roundtrip.go | 14 +------------- pkg/queryfrontend/roundtrip_test.go | 6 ++++-- test/e2e/e2ethanos/services.go | 2 ++ 5 files changed, 55 insertions(+), 16 deletions(-) create mode 100644 pkg/queryfrontend/cache.go diff --git a/cmd/thanos/query-frontend.go b/cmd/thanos/query-frontend.go index 501839c3dd9..8c96bba301f 100644 --- a/cmd/thanos/query-frontend.go +++ b/cmd/thanos/query-frontend.go @@ -6,6 +6,7 @@ package main import ( "time" + "github.com/alecthomas/units" "github.com/cortexproject/cortex/pkg/querier/frontend" "github.com/cortexproject/cortex/pkg/querier/queryrange" "github.com/go-kit/kit/log" @@ -46,13 +47,28 @@ type queryRangeConfig struct { type responseCacheConfig struct { cacheMaxFreshness time.Duration + fifoCache fifoCacheConfig } func (c *responseCacheConfig) registerFlag(cmd *kingpin.CmdClause) { - cmd.Flag("query-range.response-cache-max-freshness", "Most recent allowed cacheable result per-tenant, to prevent caching very recent results that might still be in flux."). + c.fifoCache.registerFlag(cmd) + cmd.Flag("query-range.response-cache-max-freshness", "Most recent allowed cacheable result, to prevent caching very recent results that might still be in flux."). Default("1m").DurationVar(&c.cacheMaxFreshness) } +// fifoCacheConfig defines configurations for Cortex fifo cache. +type fifoCacheConfig struct { + maxSizeBytes units.Base2Bytes + maxSizeItems int + ttl time.Duration +} + +func (c *fifoCacheConfig) registerFlag(cmd *kingpin.CmdClause) { + cmd.Flag("fifocache.max-size-bytes", "Maximum memory size of the cache in bytes. A unit suffix (KB, MB, GB) may be applied.").BytesVar(&c.maxSizeBytes) + cmd.Flag("fifocache.max-size-items", "Maximum number of entries in the cache.").Default("0").IntVar(&c.maxSizeItems) + cmd.Flag("fifocache.ttl", "The expiry duration for the cache.").Default("0").DurationVar(&c.ttl) +} + func (c *queryRangeConfig) registerFlag(cmd *kingpin.CmdClause) { c.respCacheConfig.registerFlag(cmd) @@ -132,8 +148,14 @@ func runQueryFrontend( conf.queryRangeConfig.respCacheConfig.cacheMaxFreshness, ) + // TODO(yeya24): support other cache when available. + // Using Cortex fifo cache temporarily. + fifoCache := conf.queryRangeConfig.respCacheConfig.fifoCache + cacheConfig := queryfrontend.NewFifoCacheConfig(fifoCache.maxSizeBytes.String(), fifoCache.maxSizeItems, fifoCache.ttl) + tripperWare, err := queryfrontend.NewTripperWare( limits, + cacheConfig, queryrange.PrometheusCodec, queryrange.PrometheusResponseExtractor{}, conf.queryRangeConfig.cacheResults, diff --git a/pkg/queryfrontend/cache.go b/pkg/queryfrontend/cache.go new file mode 100644 index 00000000000..a25af558db3 --- /dev/null +++ b/pkg/queryfrontend/cache.go @@ -0,0 +1,25 @@ +// Copyright (c) The Thanos Authors. +// Licensed under the Apache License 2.0. + +package queryfrontend + +import ( + "time" + + cortexcache "github.com/cortexproject/cortex/pkg/chunk/cache" + "github.com/cortexproject/cortex/pkg/querier/queryrange" +) + +// NewFifoCacheConfig creates Cortex fifo cache config. +func NewFifoCacheConfig(maxSizeBytes string, maxSizeItems int, ttl time.Duration) queryrange.ResultsCacheConfig { + return queryrange.ResultsCacheConfig{ + CacheConfig: cortexcache.Config{ + EnableFifoCache: true, + Fifocache: cortexcache.FifoCacheConfig{ + MaxSizeBytes: maxSizeBytes, + MaxSizeItems: maxSizeItems, + Validity: ttl, + }, + }, + } +} diff --git a/pkg/queryfrontend/roundtrip.go b/pkg/queryfrontend/roundtrip.go index eb60b314981..86008ec5844 100644 --- a/pkg/queryfrontend/roundtrip.go +++ b/pkg/queryfrontend/roundtrip.go @@ -9,7 +9,6 @@ import ( "strings" "time" - cortexcache "github.com/cortexproject/cortex/pkg/chunk/cache" "github.com/cortexproject/cortex/pkg/querier/frontend" "github.com/cortexproject/cortex/pkg/querier/queryrange" @@ -27,6 +26,7 @@ const ( func NewTripperWare( limits queryrange.Limits, + cacheConfig queryrange.ResultsCacheConfig, codec queryrange.Codec, cacheExtractor queryrange.Extractor, cacheResults bool, @@ -63,18 +63,6 @@ func NewTripperWare( } if cacheResults { - - // TODO(yeya24): use actual cache config after it is implemented - cacheConfig := queryrange.ResultsCacheConfig{ - CacheConfig: cortexcache.Config{ - EnableFifoCache: true, - Fifocache: cortexcache.FifoCacheConfig{ - MaxSizeBytes: "10000", - MaxSizeItems: 10000, - }, - }, - } - queryCacheMiddleware, _, err := queryrange.NewResultsCacheMiddleware( logger, cacheConfig, diff --git a/pkg/queryfrontend/roundtrip_test.go b/pkg/queryfrontend/roundtrip_test.go index a36c8f20a43..daaa2d55ee6 100644 --- a/pkg/queryfrontend/roundtrip_test.go +++ b/pkg/queryfrontend/roundtrip_test.go @@ -104,7 +104,8 @@ func TestRoundTripRetryMiddleware(t *testing.T) { } { t.Run(tc.name, func(t *testing.T) { - tpw, err := NewTripperWare(&fakeLimits{}, queryrange.PrometheusCodec, nil, false, + cache := NewFifoCacheConfig("1MB", 1000, time.Minute) + tpw, err := NewTripperWare(&fakeLimits{}, cache, queryrange.PrometheusCodec, nil, false, time.Hour, tc.maxRetries, nil, log.NewNopLogger()) testutil.Ok(t, err) @@ -187,7 +188,8 @@ func TestRoundTripSplitIntervalMiddleware(t *testing.T) { } { t.Run(tc.name, func(t *testing.T) { - tpw, err := NewTripperWare(&fakeLimits{}, queryrange.PrometheusCodec, nil, false, + cache := NewFifoCacheConfig("1MB", 1000, time.Minute) + tpw, err := NewTripperWare(&fakeLimits{}, cache, queryrange.PrometheusCodec, nil, false, tc.splitInterval, 0, nil, log.NewNopLogger()) testutil.Ok(t, err) diff --git a/test/e2e/e2ethanos/services.go b/test/e2e/e2ethanos/services.go index 017324690bc..1707dd31c32 100644 --- a/test/e2e/e2ethanos/services.go +++ b/test/e2e/e2ethanos/services.go @@ -412,6 +412,8 @@ func NewQueryFrontend(sharedDir string, name string, downstreamURL string) (*e2e "--query-frontend.downstream-url": downstreamURL, "--query-range.cache-results": "", "--log.level": logLevel, + "--fifocache.max-size-bytes": "2KB", + "--fifocache.ttl": "6h", }) queryFrontend := e2e.NewHTTPService(