Skip to content

Commit

Permalink
make cache stats metrics work again (#107)
Browse files Browse the repository at this point in the history
* fix: make cache stats metrics work again
* fix: lint issues
* update docs
  • Loading branch information
emmanuelm41 authored Apr 3, 2024
1 parent 4b5d400 commit 21eb7a3
Show file tree
Hide file tree
Showing 9 changed files with 223 additions and 222 deletions.
54 changes: 19 additions & 35 deletions pkg/zcache/combined_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package zcache

import (
"context"
"fmt"
"github.com/allegro/bigcache/v3"
"github.com/go-redis/redis/v8"
"github.com/zondax/golem/pkg/logger"
"github.com/zondax/golem/pkg/metrics"
"go.uber.org/zap"
"time"
)

Expand All @@ -17,78 +16,77 @@ type CombinedCache interface {
type combinedCache struct {
localCache LocalCache
remoteCache RemoteCache
logger *zap.Logger
logger *logger.Logger
isRemoteBestEffort bool
metricsServer metrics.TaskMetrics
appName string
}

func (c *combinedCache) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error {
c.logger.Sugar().Debugf("set key on combined cache, key: [%s]", key)
c.logger.Debugf("set key on combined cache, key: [%s]", key)

if err := c.remoteCache.Set(ctx, key, value, ttl); err != nil {
c.logger.Sugar().Errorf("error setting key on combined/remote cache, key: [%s], err: %s", key, err)
c.logger.Errorf("error setting key on combined/remote cache, key: [%s], err: %s", key, err)
if !c.isRemoteBestEffort {
c.logger.Sugar().Debugf("emitting error as remote best effort is false, key: [%s]", key)
c.logger.Debugf("emitting error as remote best effort is false, key: [%s]", key)
return err
}
}

if err := c.localCache.Set(ctx, key, value, ttl); err != nil {
c.logger.Sugar().Errorf("error setting key on combined/local cache, key: [%s], err: %s", key, err)
c.logger.Errorf("error setting key on combined/local cache, key: [%s], err: %s", key, err)
return err
}
return nil
}

func (c *combinedCache) Get(ctx context.Context, key string, data interface{}) error {
c.logger.Sugar().Debugf("get key on combined cache, key: [%s]", key)
c.logger.Debugf("get key on combined cache, key: [%s]", key)

err := c.localCache.Get(ctx, key, data)
if err != nil {
if c.localCache.IsNotFoundError(err) {
c.logger.Sugar().Debugf("key not found on combined/local cache, key: [%s]", key)
c.logger.Debugf("key not found on combined/local cache, key: [%s]", key)
} else {
c.logger.Sugar().Debugf("error getting key on combined/local cache, key: [%s], err: %s", key, err)
c.logger.Debugf("error getting key on combined/local cache, key: [%s], err: %s", key, err)
}

if err := c.remoteCache.Get(ctx, key, data); err != nil {
if c.remoteCache.IsNotFoundError(err) {
c.logger.Sugar().Debugf("key not found on combined/remote cache, key: [%s]", key)
c.logger.Debugf("key not found on combined/remote cache, key: [%s]", key)
} else {
c.logger.Sugar().Debugf("error getting key on combined/remote cache, key: [%s], err: %s", key, err)
c.logger.Debugf("error getting key on combined/remote cache, key: [%s], err: %s", key, err)
}

return err
}

c.logger.Sugar().Debugf("set value found on remote cache in the local cache, key: [%s]", key)
c.logger.Debugf("set value found on remote cache in the local cache, key: [%s]", key)
ttl, ttlErr := c.remoteCache.TTL(ctx, key)

// Refresh data TTL on both caches
if ttlErr == nil {
_ = c.localCache.Set(ctx, key, data, ttl)
} else {
c.logger.Sugar().Errorf("error getting TTL for key [%s] from remote cache, err: %s", key, ttlErr)
c.logger.Errorf("error getting TTL for key [%s] from remote cache, err: %s", key, ttlErr)
}
}

return nil
}

func (c *combinedCache) Delete(ctx context.Context, key string) error {
c.logger.Sugar().Debugf("delete key on combined cache, key: [%s]", key)
c.logger.Debugf("delete key on combined cache, key: [%s]", key)
err2 := c.remoteCache.Delete(ctx, key)
if err2 != nil {
c.logger.Sugar().Errorf("error deleting key on combined/remote cache, key: [%s], err: %s", key, err2)
c.logger.Errorf("error deleting key on combined/remote cache, key: [%s], err: %s", key, err2)
if !c.isRemoteBestEffort {
c.logger.Sugar().Debugf("emitting error as remote best effort is false, key: [%s]")
c.logger.Debugf("emitting error as remote best effort is false, key: [%s]")
return err2
}
}

if err1 := c.localCache.Delete(ctx, key); err1 != nil {
c.logger.Sugar().Errorf("error deleting key on combined/local cache, key: [%s], err: %s", key, err1)
c.logger.Errorf("error deleting key on combined/local cache, key: [%s], err: %s", key, err1)
return err1
}

Expand All @@ -110,20 +108,6 @@ func (c *combinedCache) IsNotFoundError(err error) bool {
return c.remoteCache.IsNotFoundError(err) || c.localCache.IsNotFoundError(err)
}

func (c *combinedCache) SetupAndMonitorMetrics(appName string, metricsServer metrics.TaskMetrics, updateInterval time.Duration) []error {
c.metricsServer = metricsServer
c.appName = appName

errs := setupAndMonitorCacheMetrics(appName, metricsServer, c, updateInterval)
errs = append(errs, c.registerInternalCacheMetrics()...)

return errs
}

func (c *combinedCache) registerInternalCacheMetrics() []error {
if c.metricsServer == nil {
return []error{fmt.Errorf("metrics server not available")}
}

return []error{}
func (c *combinedCache) setupAndMonitorMetrics(updateInterval time.Duration) {
setupAndMonitorCacheMetrics(c.metricsServer, c, c.logger, updateInterval)
}
5 changes: 2 additions & 3 deletions pkg/zcache/combined_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package zcache
import (
"context"
"github.com/stretchr/testify/suite"
logger2 "github.com/zondax/golem/pkg/logger"
"github.com/zondax/golem/pkg/metrics"
"go.uber.org/zap"
"os"
"testing"
"time"
Expand All @@ -31,8 +31,7 @@ func (suite *CombinedCacheTestSuite) SetupSuite() {
suite.Require().NoError(err)
suite.mr = mr
suite.ms = metrics.NewTaskMetrics("", "", "appname")
logger, err := zap.NewDevelopment()
suite.Require().NoError(err)
logger := logger2.NewLogger()

prefix := os.Getenv("PREFIX")
suite.cacheRemoteBrokenBestEffort, err = NewCombinedCache(
Expand Down
30 changes: 21 additions & 9 deletions pkg/zcache/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,22 @@ package zcache
import (
"github.com/allegro/bigcache/v3"
"github.com/go-redis/redis/v8"
"github.com/zondax/golem/pkg/logger"
"github.com/zondax/golem/pkg/metrics"
"go.uber.org/zap"
"time"
)

type StatsMetrics struct {
Enable bool
UpdateInterval time.Duration
}

type CleanupProcess struct {
Interval time.Duration
BatchSize int
ThrottleTime time.Duration
}

type RemoteConfig struct {
Network string
Addr string
Expand All @@ -23,16 +34,16 @@ type RemoteConfig struct {
IdleTimeout time.Duration
IdleCheckFrequency time.Duration
Prefix string
Logger *zap.Logger
Logger *logger.Logger
StatsMetrics StatsMetrics
}

type LocalConfig struct {
Prefix string
Logger *zap.Logger
MetricServer metrics.TaskMetrics
CleanupInterval time.Duration
BatchSize int
ThrottleTime time.Duration
Prefix string
Logger *logger.Logger
MetricServer metrics.TaskMetrics
StatsMetrics StatsMetrics
CleanupProcess CleanupProcess
}

func (c *RemoteConfig) ToRedisConfig() *redis.Options {
Expand Down Expand Up @@ -61,7 +72,8 @@ func (c *LocalConfig) ToBigCacheConfig() bigcache.Config {
type CombinedConfig struct {
Local *LocalConfig
Remote *RemoteConfig
GlobalLogger *zap.Logger
GlobalLogger *logger.Logger
GlobalPrefix string
GlobalStatsMetrics StatsMetrics
IsRemoteBestEffort bool
}
Loading

0 comments on commit 21eb7a3

Please sign in to comment.