Skip to content

Commit

Permalink
Fix global zcache stats (#111)
Browse files Browse the repository at this point in the history
* fix: global cache stats

* chore: small refactor

* fix: tests
  • Loading branch information
emmanuelm41 committed Apr 3, 2024
1 parent 8402d57 commit c1442cf
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 21 deletions.
12 changes: 4 additions & 8 deletions pkg/zcache/combined_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package zcache

import (
"context"
"github.com/allegro/bigcache/v3"
"github.com/go-redis/redis/v8"
"github.com/zondax/golem/pkg/logger"
"github.com/zondax/golem/pkg/metrics"
"time"
Expand Down Expand Up @@ -94,13 +92,11 @@ func (c *combinedCache) Delete(ctx context.Context, key string) error {
}

func (c *combinedCache) GetStats() ZCacheStats {
localStats := c.localCache.(interface{}).(*bigcache.BigCache).Stats()
remotePoolStats := c.remoteCache.(interface{}).(*redis.Client).PoolStats()
localStats := c.localCache.GetStats()
remotePoolStats := c.remoteCache.GetStats()
return ZCacheStats{
Local: &localStats,
Remote: &RedisStats{
Pool: remotePoolStats,
},
Local: localStats.Local,
Remote: remotePoolStats.Remote,
}
}

Expand Down
15 changes: 6 additions & 9 deletions pkg/zcache/combined_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,40 +36,37 @@ func (suite *CombinedCacheTestSuite) SetupSuite() {
prefix := os.Getenv("PREFIX")
suite.cacheRemoteBrokenBestEffort, err = NewCombinedCache(
&CombinedConfig{
Local: &LocalConfig{
MetricServer: suite.ms,
},
Local: &LocalConfig{},
Remote: &RemoteConfig{
Addr: "0.0.0.0",
},
IsRemoteBestEffort: true,
GlobalMetricServer: suite.ms,
GlobalPrefix: prefix,
GlobalLogger: logger,
})
suite.Nil(err)

suite.cacheOkNotBestEffort, err = NewCombinedCache(&CombinedConfig{
Local: &LocalConfig{
MetricServer: suite.ms,
},
Local: &LocalConfig{},
Remote: &RemoteConfig{
Addr: mr.Addr(),
},
IsRemoteBestEffort: false,
GlobalMetricServer: suite.ms,
GlobalPrefix: prefix,
GlobalLogger: logger,
})
suite.Nil(err)

suite.cacheRemoteBrokenNotBestEffort, err = NewCombinedCache(
&CombinedConfig{
Local: &LocalConfig{
MetricServer: suite.ms,
},
Local: &LocalConfig{},
Remote: &RemoteConfig{
Addr: "0.0.0.0",
},
IsRemoteBestEffort: false,
GlobalMetricServer: suite.ms,
GlobalPrefix: prefix,
GlobalLogger: logger,
})
Expand Down
2 changes: 2 additions & 0 deletions pkg/zcache/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type RemoteConfig struct {
IdleCheckFrequency time.Duration
Prefix string
Logger *logger.Logger
MetricServer metrics.TaskMetrics
StatsMetrics StatsMetrics
}

Expand Down Expand Up @@ -74,6 +75,7 @@ type CombinedConfig struct {
Remote *RemoteConfig
GlobalLogger *logger.Logger
GlobalPrefix string
GlobalMetricServer metrics.TaskMetrics
GlobalStatsMetrics StatsMetrics
IsRemoteBestEffort bool
}
4 changes: 2 additions & 2 deletions pkg/zcache/local_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ func (c *localCache) setupAndMonitorMetrics(updateInterval time.Duration) {
setupAndMonitorCacheMetrics(c.metricsServer, c, c.logger, updateInterval)
}

func (c *localCache) startCleanupProcess(interval time.Duration) {
ticker := time.NewTicker(interval)
func (c *localCache) startCleanupProcess() {
ticker := time.NewTicker(c.cleanupProcess.Interval)
go func() {
for range ticker.C {
c.cleanupExpiredKeys()
Expand Down
12 changes: 10 additions & 2 deletions pkg/zcache/zcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func NewLocalCache(config *LocalConfig) (LocalCache, error) {
}

lc.registerCleanupMetrics()
lc.startCleanupProcess(config.CleanupProcess.Interval)
lc.startCleanupProcess()

if config.StatsMetrics.Enable {
if config.MetricServer == nil {
Expand All @@ -86,7 +86,12 @@ func NewRemoteCache(config *RemoteConfig) (RemoteCache, error) {
loggerInst = logger.NewLogger()
}

rc := &redisCache{client: client, prefix: config.Prefix, logger: loggerInst}
rc := &redisCache{
client: client,
prefix: config.Prefix,
logger: loggerInst,
metricsServer: config.MetricServer,
}

if config.StatsMetrics.Enable {
rc.setupAndMonitorMetrics(config.StatsMetrics.UpdateInterval)
Expand Down Expand Up @@ -115,6 +120,7 @@ func NewCombinedCache(combinedConfig *CombinedConfig) (CombinedCache, error) {
// Set global configs on remote cache config
remoteCacheConfig.Prefix = combinedConfig.GlobalPrefix
remoteCacheConfig.Logger = combinedConfig.GlobalLogger
remoteCacheConfig.MetricServer = combinedConfig.GlobalMetricServer

remoteClient, err := NewRemoteCache(remoteCacheConfig)
if err != nil {
Expand All @@ -125,6 +131,7 @@ func NewCombinedCache(combinedConfig *CombinedConfig) (CombinedCache, error) {
// Set global configs on local cache config
localCacheConfig.Prefix = combinedConfig.GlobalPrefix
localCacheConfig.Logger = combinedConfig.GlobalLogger
localCacheConfig.MetricServer = combinedConfig.GlobalMetricServer

localClient, err := NewLocalCache(localCacheConfig)
if err != nil {
Expand All @@ -136,6 +143,7 @@ func NewCombinedCache(combinedConfig *CombinedConfig) (CombinedCache, error) {
remoteCache: remoteClient,
localCache: localClient,
isRemoteBestEffort: combinedConfig.IsRemoteBestEffort,
metricsServer: combinedConfig.GlobalMetricServer,
logger: combinedConfig.GlobalLogger,
}

Expand Down

0 comments on commit c1442cf

Please sign in to comment.