Skip to content

Commit

Permalink
add config flags for Cortex fifo cache
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Ye <yb532204897@gmail.com>
  • Loading branch information
yeya24 committed Aug 7, 2020
1 parent 32458f5 commit 2c21459
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 16 deletions.
24 changes: 23 additions & 1 deletion cmd/thanos/query-frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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,
Expand Down
25 changes: 25 additions & 0 deletions pkg/queryfrontend/cache.go
Original file line number Diff line number Diff line change
@@ -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,
},
},
}
}
14 changes: 1 addition & 13 deletions pkg/queryfrontend/roundtrip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -27,6 +26,7 @@ const (

func NewTripperWare(
limits queryrange.Limits,
cacheConfig queryrange.ResultsCacheConfig,
codec queryrange.Codec,
cacheExtractor queryrange.Extractor,
cacheResults bool,
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions pkg/queryfrontend/roundtrip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
2 changes: 2 additions & 0 deletions test/e2e/e2ethanos/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 2c21459

Please sign in to comment.