Skip to content

Commit c1442cf

Browse files
authored
Fix global zcache stats (#111)
* fix: global cache stats * chore: small refactor * fix: tests
1 parent 8402d57 commit c1442cf

File tree

5 files changed

+24
-21
lines changed

5 files changed

+24
-21
lines changed

pkg/zcache/combined_cache.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package zcache
22

33
import (
44
"context"
5-
"github.com/allegro/bigcache/v3"
6-
"github.com/go-redis/redis/v8"
75
"github.com/zondax/golem/pkg/logger"
86
"github.com/zondax/golem/pkg/metrics"
97
"time"
@@ -94,13 +92,11 @@ func (c *combinedCache) Delete(ctx context.Context, key string) error {
9492
}
9593

9694
func (c *combinedCache) GetStats() ZCacheStats {
97-
localStats := c.localCache.(interface{}).(*bigcache.BigCache).Stats()
98-
remotePoolStats := c.remoteCache.(interface{}).(*redis.Client).PoolStats()
95+
localStats := c.localCache.GetStats()
96+
remotePoolStats := c.remoteCache.GetStats()
9997
return ZCacheStats{
100-
Local: &localStats,
101-
Remote: &RedisStats{
102-
Pool: remotePoolStats,
103-
},
98+
Local: localStats.Local,
99+
Remote: remotePoolStats.Remote,
104100
}
105101
}
106102

pkg/zcache/combined_cache_test.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,40 +36,37 @@ func (suite *CombinedCacheTestSuite) SetupSuite() {
3636
prefix := os.Getenv("PREFIX")
3737
suite.cacheRemoteBrokenBestEffort, err = NewCombinedCache(
3838
&CombinedConfig{
39-
Local: &LocalConfig{
40-
MetricServer: suite.ms,
41-
},
39+
Local: &LocalConfig{},
4240
Remote: &RemoteConfig{
4341
Addr: "0.0.0.0",
4442
},
4543
IsRemoteBestEffort: true,
44+
GlobalMetricServer: suite.ms,
4645
GlobalPrefix: prefix,
4746
GlobalLogger: logger,
4847
})
4948
suite.Nil(err)
5049

5150
suite.cacheOkNotBestEffort, err = NewCombinedCache(&CombinedConfig{
52-
Local: &LocalConfig{
53-
MetricServer: suite.ms,
54-
},
51+
Local: &LocalConfig{},
5552
Remote: &RemoteConfig{
5653
Addr: mr.Addr(),
5754
},
5855
IsRemoteBestEffort: false,
56+
GlobalMetricServer: suite.ms,
5957
GlobalPrefix: prefix,
6058
GlobalLogger: logger,
6159
})
6260
suite.Nil(err)
6361

6462
suite.cacheRemoteBrokenNotBestEffort, err = NewCombinedCache(
6563
&CombinedConfig{
66-
Local: &LocalConfig{
67-
MetricServer: suite.ms,
68-
},
64+
Local: &LocalConfig{},
6965
Remote: &RemoteConfig{
7066
Addr: "0.0.0.0",
7167
},
7268
IsRemoteBestEffort: false,
69+
GlobalMetricServer: suite.ms,
7370
GlobalPrefix: prefix,
7471
GlobalLogger: logger,
7572
})

pkg/zcache/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type RemoteConfig struct {
3535
IdleCheckFrequency time.Duration
3636
Prefix string
3737
Logger *logger.Logger
38+
MetricServer metrics.TaskMetrics
3839
StatsMetrics StatsMetrics
3940
}
4041

@@ -74,6 +75,7 @@ type CombinedConfig struct {
7475
Remote *RemoteConfig
7576
GlobalLogger *logger.Logger
7677
GlobalPrefix string
78+
GlobalMetricServer metrics.TaskMetrics
7779
GlobalStatsMetrics StatsMetrics
7880
IsRemoteBestEffort bool
7981
}

pkg/zcache/local_cache.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ func (c *localCache) setupAndMonitorMetrics(updateInterval time.Duration) {
142142
setupAndMonitorCacheMetrics(c.metricsServer, c, c.logger, updateInterval)
143143
}
144144

145-
func (c *localCache) startCleanupProcess(interval time.Duration) {
146-
ticker := time.NewTicker(interval)
145+
func (c *localCache) startCleanupProcess() {
146+
ticker := time.NewTicker(c.cleanupProcess.Interval)
147147
go func() {
148148
for range ticker.C {
149149
c.cleanupExpiredKeys()

pkg/zcache/zcache.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func NewLocalCache(config *LocalConfig) (LocalCache, error) {
6464
}
6565

6666
lc.registerCleanupMetrics()
67-
lc.startCleanupProcess(config.CleanupProcess.Interval)
67+
lc.startCleanupProcess()
6868

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

89-
rc := &redisCache{client: client, prefix: config.Prefix, logger: loggerInst}
89+
rc := &redisCache{
90+
client: client,
91+
prefix: config.Prefix,
92+
logger: loggerInst,
93+
metricsServer: config.MetricServer,
94+
}
9095

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

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

129136
localClient, err := NewLocalCache(localCacheConfig)
130137
if err != nil {
@@ -136,6 +143,7 @@ func NewCombinedCache(combinedConfig *CombinedConfig) (CombinedCache, error) {
136143
remoteCache: remoteClient,
137144
localCache: localClient,
138145
isRemoteBestEffort: combinedConfig.IsRemoteBestEffort,
146+
metricsServer: combinedConfig.GlobalMetricServer,
139147
logger: combinedConfig.GlobalLogger,
140148
}
141149

0 commit comments

Comments
 (0)