Skip to content

Commit

Permalink
fix: update fastcache metrics with go routine (#206)
Browse files Browse the repository at this point in the history
* chore: update metric with go routine

* chore: update metric with go routine

* fix: missing for loop

* chore: fix comment
  • Loading branch information
Woosang Son authored May 26, 2021
1 parent eb8569c commit 4bf9d52
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 29 deletions.
23 changes: 18 additions & 5 deletions store/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cache

import (
"sync"
"time"

"github.com/VictoriaMetrics/fastcache"
"github.com/line/lbm-sdk/v2/store/cachekv"
Expand Down Expand Up @@ -63,13 +64,29 @@ func NewCommitKVStoreCacheManager(cacheSize int, provider MetricsProvider) *Comm
// This function was called because it intended to use the inter block cache, creating a cache of minimal size.
cacheSize = DefaultCommitKVStoreCacheSize
}
return &CommitKVStoreCacheManager{
cm := &CommitKVStoreCacheManager{
cache: fastcache.New(cacheSize),
caches: make(map[string]types.CommitKVStore),
metrics: provider(),
prefixMap: make(map[string][]byte),
prefixOrder: 0,
}
startCacheMetricUpdator(cm.cache, cm.metrics)
return cm
}

func startCacheMetricUpdator(cache *fastcache.Cache, metrics *Metrics) {
// Execution time of `fastcache.UpdateStats()` can increase linearly as cache entries grows
// So we update the metrics with a separate go route.
go func() {
for {
stats := fastcache.Stats{}
cache.UpdateStats(&stats)
metrics.InterBlockCacheEntries.Set(float64(stats.EntriesCount))
metrics.InterBlockCacheBytes.Set(float64(stats.BytesSize))
time.Sleep(1 * time.Minute)
}
}()
}

// GetStoreCache returns a Cache from the CommitStoreCacheManager for a given
Expand Down Expand Up @@ -135,10 +152,6 @@ func (ckv *CommitKVStoreCache) Get(key []byte) []byte {
ckv.metrics.InterBlockCacheMisses.Add(1)
value := ckv.CommitKVStore.Get(key)
ckv.cache.Set(prefixedKey, value)
stats := fastcache.Stats{}
ckv.cache.UpdateStats(&stats)
ckv.metrics.InterBlockCacheEntries.Set(float64(stats.EntriesCount))
ckv.metrics.InterBlockCacheBytes.Set(float64(stats.BytesSize))
return value
}

Expand Down
43 changes: 19 additions & 24 deletions store/iavl/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ var (
_ types.StoreWithInitialVersion = (*Store)(nil)

_ types.CacheManager = (*CacheManagerSingleton)(nil)
_ types.CacheManager = (*CacheManagerOwnCache)(nil)
)

// Store Implements types.KVStore and CommitKVStore.
Expand All @@ -55,24 +54,28 @@ func (cms *CacheManagerSingleton) GetCache() *fastcache.Cache {
}

func NewCacheManagerSingleton(cacheSize int, provider MetricsProvider) types.CacheManager {
return &CacheManagerSingleton{
cm := &CacheManagerSingleton{
cache: fastcache.New(cacheSize),
metrics: provider(),
}
}

type CacheManagerOwnCache struct {
cacheSize int
}

func (cmo *CacheManagerOwnCache) GetCache() *fastcache.Cache {
return fastcache.New(cmo.cacheSize)
}

func NewCacheManagerOwnCache(cacheSize int) types.CacheManager {
return &CacheManagerOwnCache{
cacheSize: cacheSize,
}
startCacheMetricUpdator(cm.cache, cm.metrics)
return cm
}

func startCacheMetricUpdator(cache *fastcache.Cache, metrics *Metrics) {
// Execution time of `fastcache.UpdateStats()` can increase linearly as cache entries grows
// So we update the metrics with a separate go route.
go func() {
for {
stats := fastcache.Stats{}
cache.UpdateStats(&stats)
metrics.IAVLCacheHits.Set(float64(stats.GetCalls - stats.Misses))
metrics.IAVLCacheMisses.Set(float64(stats.Misses))
metrics.IAVLCacheEntries.Set(float64(stats.EntriesCount))
metrics.IAVLCacheBytes.Set(float64(stats.BytesSize))
time.Sleep(1 * time.Minute)
}
}()
}

type CacheManagerNoCache struct{}
Expand Down Expand Up @@ -230,14 +233,6 @@ func (st *Store) Set(key, value []byte) {
func (st *Store) Get(key []byte) []byte {
defer telemetry.MeasureSince(time.Now(), "store", "iavl", "get")
_, value := st.tree.Get(key)
if st.cache != nil {
stats := fastcache.Stats{}
st.cache.UpdateStats(&stats)
st.metrics.IAVLCacheHits.Set(float64(stats.GetCalls - stats.Misses))
st.metrics.IAVLCacheMisses.Set(float64(stats.Misses))
st.metrics.IAVLCacheEntries.Set(float64(stats.EntriesCount))
st.metrics.IAVLCacheBytes.Set(float64(stats.BytesSize))
}
return value
}

Expand Down

0 comments on commit 4bf9d52

Please sign in to comment.