From 176e05714e061311e2d786c5731b24f81f930b71 Mon Sep 17 00:00:00 2001 From: Alex Grintsvayg Date: Fri, 20 Nov 2020 15:01:33 -0500 Subject: [PATCH] rename cahces --- cmd/reflector.go | 8 +++++--- store/lru.go | 18 ++++++++++-------- store/singleflight.go | 10 +++++----- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/cmd/reflector.go b/cmd/reflector.go index cff66f5..a047b85 100644 --- a/cmd/reflector.go +++ b/cmd/reflector.go @@ -159,9 +159,10 @@ func wrapWithCache(s store.BlobStore) store.BlobStore { wrapped = store.NewCachingStore( "reflector", wrapped, - store.NewLRUStore("peer_server", store.NewDiskStore(diskCachePath, 2), diskCacheMaxSize), + store.NewLRUStore("hdd", store.NewDiskStore(diskCachePath, 2), diskCacheMaxSize), ) } + diskCacheMaxSize, diskCachePath = diskCacheParams(bufferReflectorCmdDiskCache) if diskCacheMaxSize > 0 { err := os.MkdirAll(diskCachePath, os.ModePerm) @@ -171,14 +172,15 @@ func wrapWithCache(s store.BlobStore) store.BlobStore { wrapped = store.NewCachingStore( "reflector", wrapped, - store.NewLRUStore("peer_server", store.NewDiskStore(diskCachePath, 2), diskCacheMaxSize), + store.NewLRUStore("nvme", store.NewDiskStore(diskCachePath, 2), diskCacheMaxSize), ) } + if reflectorCmdMemCache > 0 { wrapped = store.NewCachingStore( "reflector", wrapped, - store.NewLRUStore("peer_server", store.NewMemStore(), reflectorCmdMemCache), + store.NewLRUStore("mem", store.NewMemStore(), reflectorCmdMemCache), ) } diff --git a/store/lru.go b/store/lru.go index 6d06aa6..edbe3a5 100644 --- a/store/lru.go +++ b/store/lru.go @@ -18,18 +18,20 @@ type LRUStore struct { // NewLRUStore initialize a new LRUStore func NewLRUStore(component string, store BlobStore, maxItems int) *LRUStore { + l := &LRUStore{ + store: store, + } + lru, err := golru.NewWithEvict(maxItems, func(key interface{}, value interface{}) { - metrics.CacheLRUEvictCount.With(metrics.CacheLabels(store.Name(), component)).Inc() + metrics.CacheLRUEvictCount.With(metrics.CacheLabels(l.Name(), component)).Inc() _ = store.Delete(key.(string)) // TODO: log this error. may happen if underlying entry is gone but cache entry still there }) if err != nil { panic(err) } - l := &LRUStore{ - store: store, - lru: lru, - } + l.lru = lru + go func() { if lstr, ok := store.(lister); ok { err = l.loadExisting(lstr, maxItems) @@ -42,10 +44,10 @@ func NewLRUStore(component string, store BlobStore, maxItems int) *LRUStore { return l } -const nameLRU = "lru" - // Name is the cache type name -func (l *LRUStore) Name() string { return nameLRU } +func (l *LRUStore) Name() string { + return "lru_" + l.store.Name() +} // Has returns whether the blob is in the store, without updating the recent-ness. func (l *LRUStore) Has(hash string) (bool, error) { diff --git a/store/singleflight.go b/store/singleflight.go index fbe314f..cca1e2f 100644 --- a/store/singleflight.go +++ b/store/singleflight.go @@ -32,8 +32,8 @@ func (s *singleflightStore) Name() string { // Get ensures that only one request per hash is sent to the origin at a time, // thereby protecting against https://en.wikipedia.org/wiki/Thundering_herd_problem func (s *singleflightStore) Get(hash string) (stream.Blob, error) { - metrics.CacheWaitingRequestsCount.With(metrics.CacheLabels(s.BlobStore.Name(), s.component)).Inc() - defer metrics.CacheWaitingRequestsCount.With(metrics.CacheLabels(s.BlobStore.Name(), s.component)).Dec() + metrics.CacheWaitingRequestsCount.With(metrics.CacheLabels(s.Name(), s.component)).Inc() + defer metrics.CacheWaitingRequestsCount.With(metrics.CacheLabels(s.Name(), s.component)).Dec() blob, err, _ := s.sf.Do(hash, s.getter(hash)) if err != nil { @@ -46,8 +46,8 @@ func (s *singleflightStore) Get(hash string) (stream.Blob, error) { // only one getter per hash will be executing at a time func (s *singleflightStore) getter(hash string) func() (interface{}, error) { return func() (interface{}, error) { - metrics.CacheOriginRequestsCount.With(metrics.CacheLabels(s.BlobStore.Name(), s.component)).Inc() - defer metrics.CacheOriginRequestsCount.With(metrics.CacheLabels(s.BlobStore.Name(), s.component)).Dec() + metrics.CacheOriginRequestsCount.With(metrics.CacheLabels(s.Name(), s.component)).Inc() + defer metrics.CacheOriginRequestsCount.With(metrics.CacheLabels(s.Name(), s.component)).Dec() start := time.Now() blob, err := s.BlobStore.Get(hash) @@ -57,7 +57,7 @@ func (s *singleflightStore) getter(hash string) func() (interface{}, error) { rate := float64(len(blob)) / 1024 / 1024 / time.Since(start).Seconds() metrics.CacheRetrievalSpeed.With(map[string]string{ - metrics.LabelCacheType: s.BlobStore.Name(), + metrics.LabelCacheType: s.Name(), metrics.LabelComponent: s.component, metrics.LabelSource: "origin", }).Set(rate)