Skip to content

Commit

Permalink
Add a new cache for caching backend responses before rendering.
Browse files Browse the repository at this point in the history
Remove traces of find cache from code, as it was effectively removed.
  • Loading branch information
jaroslawr committed Jun 16, 2020
1 parent 8c27ce1 commit dfd7122
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 81 deletions.
2 changes: 2 additions & 0 deletions carbonapipb/carbonapi.pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ type AccessLogDetails struct {
SendGlobs bool `json:"send_globs,omitempty"`
From int64 `json:"from,omitempty"`
Until int64 `json:"until,omitempty"`
MaxDataPoints int64 `json:"max_data_points,omitempty"`
Tz string `json:"tz,omitempty"`
FromRaw string `json:"from_raw,omitempty"`
UntilRaw string `json:"until_raw,omitempty"`
URI string `json:"uri,omitempty"`
FromCache bool `json:"from_cache"`
UsedBackendCache bool `json:"used_backend_cache"`
ZipperRequests int64 `json:"zipper_requests,omitempty"`
TotalMetricsCount int64 `json:"total_metrics_count,omitempty"`
RequestHeaders map[string]string `json:"request_headers"`
Expand Down
17 changes: 11 additions & 6 deletions cmd/carbonapi/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ type ConfigType struct {
Listen string `mapstructure:"listen"`
Buckets int `mapstructure:"buckets"`
Concurency int `mapstructure:"concurency"`
Cache CacheConfig `mapstructure:"cache"`
ResponseCacheConfig CacheConfig `mapstructure:"cache"`
BackendCacheConfig CacheConfig `mapstructure:"backendCache"`
Cpus int `mapstructure:"cpus"`
TimezoneString string `mapstructure:"tz"`
UnicodeRangeTables []string `mapstructure:"unicodeRangeTables"`
Expand All @@ -78,8 +79,8 @@ type ConfigType struct {
Expvar ExpvarConfig `mapstructure:"expvar"`
NotFoundStatusCode int `mapstructure:"notFoundStatusCode"`

QueryCache cache.BytesCache `mapstructure:"-" json:"-"`
FindCache cache.BytesCache `mapstructure:"-" json:"-"`
ResponseCache cache.BytesCache `mapstructure:"-" json:"-"`
BackendCache cache.BytesCache `mapstructure:"-" json:"-"`

DefaultTimeZone *time.Location `mapstructure:"-" json:"-"`

Expand All @@ -106,10 +107,14 @@ var Config = ConfigType{
Buckets: 10,
Concurency: 1000,
MaxBatchSize: 100,
Cache: CacheConfig{
ResponseCacheConfig: CacheConfig{
Type: "mem",
DefaultTimeoutSec: 60,
},
BackendCacheConfig: CacheConfig{
Type: "null",
DefaultTimeoutSec: 0,
},
TimezoneString: "",
Graphite: GraphiteConfig{
Pattern: "{prefix}.{fqdn}",
Expand All @@ -121,8 +126,8 @@ var Config = ConfigType{
IdleConnections: 10,
PidFile: "",

QueryCache: cache.NullCache{},
FindCache: cache.NullCache{},
ResponseCache: cache.NullCache{},
BackendCache: cache.NullCache{},

DefaultTimeZone: time.Local,
Logger: []zapwriter.Config{DefaultLoggerConfig},
Expand Down
53 changes: 30 additions & 23 deletions cmd/carbonapi/config/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import (
var graphTemplates map[string]png.PictureParams

func SetUpConfig(logger *zap.Logger, BuildVersion string) {
Config.Cache.MemcachedServers = viper.GetStringSlice("cache.memcachedServers")
Config.ResponseCacheConfig.MemcachedServers = viper.GetStringSlice("cache.memcachedServers")
Config.BackendCacheConfig.MemcachedServers = viper.GetStringSlice("backendCache.memcachedServers")
if n := viper.GetString("logger.logger"); n != "" {
Config.Logger[0].Logger = n
}
Expand Down Expand Up @@ -155,28 +156,8 @@ func SetUpConfig(logger *zap.Logger, BuildVersion string) {

Config.Limiter = limiter.NewSimpleLimiter(Config.Concurency)

switch Config.Cache.Type {
case "memcache":
if len(Config.Cache.MemcachedServers) == 0 {
logger.Fatal("memcache cache requested but no memcache servers provided")
}

logger.Info("memcached configured",
zap.Strings("servers", Config.Cache.MemcachedServers),
)
Config.QueryCache = cache.NewMemcached("capi", Config.Cache.MemcachedServers...)
case "mem":
Config.QueryCache = cache.NewExpireCache(uint64(Config.Cache.Size * 1024 * 1024))
case "null":
// defaults
Config.QueryCache = cache.NullCache{}
Config.FindCache = cache.NullCache{}
default:
logger.Error("unknown cache type",
zap.String("cache_type", Config.Cache.Type),
zap.Strings("known_cache_types", []string{"null", "mem", "memcache"}),
)
}
Config.ResponseCache = createCache(logger, "cache", Config.ResponseCacheConfig)
Config.BackendCache = createCache(logger, "backendCache", Config.BackendCacheConfig)

if Config.TimezoneString != "" {
fields := strings.Split(Config.TimezoneString, ",")
Expand Down Expand Up @@ -266,6 +247,32 @@ func SetUpConfig(logger *zap.Logger, BuildVersion string) {
}
}

func createCache(logger *zap.Logger, cacheName string, cacheConfig CacheConfig) cache.BytesCache {
switch cacheConfig.Type {
case "memcache":
if len(cacheConfig.MemcachedServers) == 0 {
logger.Fatal(cacheName + ": memcache cache requested but no memcache servers provided")
}

logger.Info(cacheName+": memcached configured",
zap.Strings("servers", cacheConfig.MemcachedServers),
)
return cache.NewMemcached("capi-"+cacheName, cacheConfig.MemcachedServers...)
case "mem":
logger.Info(cacheName + ": in-memory cache configured")
return cache.NewExpireCache(uint64(cacheConfig.Size * 1024 * 1024))
case "null":
// defaults
return cache.NullCache{}
default:
logger.Error(cacheName+": unknown cache type",
zap.String("cache_type", cacheConfig.Type),
zap.Strings("known_cache_types", []string{"null", "mem", "memcache"}),
)
return nil
}
}

func SetUpViper(logger *zap.Logger, configPath *string, viperPrefix string) {
if *configPath != "" {
b, err := ioutil.ReadFile(*configPath)
Expand Down
6 changes: 2 additions & 4 deletions cmd/carbonapi/graphite_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,14 @@ func setupGraphiteMetrics(logger *zap.Logger) {
graphite.Register(fmt.Sprintf("%s.request_cache_hits", pattern), http.ApiMetrics.RequestCacheHits)
graphite.Register(fmt.Sprintf("%s.request_cache_misses", pattern), http.ApiMetrics.RequestCacheMisses)
graphite.Register(fmt.Sprintf("%s.request_cache_overhead_ns", pattern), http.ApiMetrics.RenderCacheOverheadNS)
graphite.Register(fmt.Sprintf("%s.backend_cache_hits", pattern), http.ApiMetrics.BackendCacheHits)
graphite.Register(fmt.Sprintf("%s.backend_cache_misses", pattern), http.ApiMetrics.BackendCacheMisses)

for i := 0; i <= config.Config.Buckets; i++ {
graphite.Register(fmt.Sprintf("%s.requests_in_%dms_to_%dms", pattern, i*100, (i+1)*100), http.BucketEntry(i))
}

graphite.Register(fmt.Sprintf("%s.find_requests", pattern), http.ApiMetrics.FindRequests)
graphite.Register(fmt.Sprintf("%s.find_cache_hits", pattern), http.ApiMetrics.FindCacheHits)
graphite.Register(fmt.Sprintf("%s.find_cache_misses", pattern), http.ApiMetrics.FindCacheMisses)
graphite.Register(fmt.Sprintf("%s.find_cache_overhead_ns", pattern), http.ApiMetrics.FindCacheOverheadNS)

graphite.Register(fmt.Sprintf("%s.render_requests", pattern), http.ApiMetrics.RenderRequests)

if http.ApiMetrics.MemcacheTimeouts != nil {
Expand Down
19 changes: 8 additions & 11 deletions cmd/carbonapi/http/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ var ApiMetrics = struct {
RenderRequests *expvar.Int
RequestCacheHits *expvar.Int
RequestCacheMisses *expvar.Int
BackendCacheHits *expvar.Int
BackendCacheMisses *expvar.Int
RenderCacheOverheadNS *expvar.Int
RequestBuckets expvar.Func

FindRequests *expvar.Int
FindCacheHits *expvar.Int
FindCacheMisses *expvar.Int
FindCacheOverheadNS *expvar.Int
FindRequests *expvar.Int

MemcacheTimeouts expvar.Func

Expand All @@ -34,13 +33,11 @@ var ApiMetrics = struct {
RenderRequests: expvar.NewInt("render_requests"),
RequestCacheHits: expvar.NewInt("request_cache_hits"),
RequestCacheMisses: expvar.NewInt("request_cache_misses"),
BackendCacheHits: expvar.NewInt("backend_cache_hits"),
BackendCacheMisses: expvar.NewInt("backend_cache_misses"),
RenderCacheOverheadNS: expvar.NewInt("render_cache_overhead_ns"),

FindRequests: expvar.NewInt("find_requests"),

FindCacheHits: expvar.NewInt("find_cache_hits"),
FindCacheMisses: expvar.NewInt("find_cache_misses"),
FindCacheOverheadNS: expvar.NewInt("find_cache_overhead_ns"),
}

var ZipperMetrics = struct {
Expand Down Expand Up @@ -117,17 +114,17 @@ func RenderTimeBuckets() interface{} {
}

func SetupMetrics(logger *zap.Logger) {
switch config.Config.Cache.Type {
switch config.Config.ResponseCacheConfig.Type {
case "memcache":
mcache := config.Config.QueryCache.(*cache.MemcachedCache)
mcache := config.Config.ResponseCache.(*cache.MemcachedCache)

ApiMetrics.MemcacheTimeouts = expvar.Func(func() interface{} {
return mcache.Timeouts()
})
expvar.Publish("memcache_timeouts", ApiMetrics.MemcacheTimeouts)

case "mem":
qcache := config.Config.QueryCache.(*cache.ExpireCache)
qcache := config.Config.ResponseCache.(*cache.ExpireCache)

ApiMetrics.CacheSize = expvar.Func(func() interface{} {
return qcache.Size()
Expand Down
Loading

0 comments on commit dfd7122

Please sign in to comment.