From 62b041e90a6973fbd2af126fa50bee74cbc06688 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Mon, 20 Jan 2020 14:58:50 +0200 Subject: [PATCH] lib/{mergeset,storage}: properly update `lastAccessTime` in index and data block cache entries --- lib/mergeset/part.go | 10 +++++----- lib/storage/part.go | 36 ++++++------------------------------ 2 files changed, 11 insertions(+), 35 deletions(-) diff --git a/lib/mergeset/part.go b/lib/mergeset/part.go index 6936839f2..e03e8977c 100644 --- a/lib/mergeset/part.go +++ b/lib/mergeset/part.go @@ -315,7 +315,7 @@ type inmemoryBlockCache struct { requests uint64 misses uint64 - m map[inmemoryBlockCacheKey]inmemoryBlockCacheEntry + m map[inmemoryBlockCacheKey]*inmemoryBlockCacheEntry mu sync.RWMutex cleanerStopCh chan struct{} @@ -346,7 +346,7 @@ type inmemoryBlockCacheEntry struct { func newInmemoryBlockCache() *inmemoryBlockCache { var ibc inmemoryBlockCache - ibc.m = make(map[inmemoryBlockCacheKey]inmemoryBlockCacheEntry) + ibc.m = make(map[inmemoryBlockCacheKey]*inmemoryBlockCacheEntry) ibc.cleanerStopCh = make(chan struct{}) ibc.cleanerWG.Add(1) @@ -406,10 +406,10 @@ func (ibc *inmemoryBlockCache) Get(k inmemoryBlockCacheKey) *inmemoryBlock { atomic.AddUint64(&ibc.requests, 1) ibc.mu.RLock() - ibe, ok := ibc.m[k] + ibe := ibc.m[k] ibc.mu.RUnlock() - if ok { + if ibe != nil { currentTime := atomic.LoadUint64(¤tTimestamp) if atomic.LoadUint64(&ibe.lastAccessTime) != currentTime { atomic.StoreUint64(&ibe.lastAccessTime, currentTime) @@ -442,7 +442,7 @@ func (ibc *inmemoryBlockCache) Put(k inmemoryBlockCacheKey, ib *inmemoryBlock) b } // Store ib in the cache. - ibe := inmemoryBlockCacheEntry{ + ibe := &inmemoryBlockCacheEntry{ lastAccessTime: atomic.LoadUint64(¤tTimestamp), ib: ib, } diff --git a/lib/storage/part.go b/lib/storage/part.go index bc1c3212e..986846cb7 100644 --- a/lib/storage/part.go +++ b/lib/storage/part.go @@ -173,9 +173,8 @@ type indexBlockCache struct { requests uint64 misses uint64 - m map[uint64]indexBlockCacheEntry - missesMap map[uint64]uint64 - mu sync.RWMutex + m map[uint64]*indexBlockCacheEntry + mu sync.RWMutex cleanerStopCh chan struct{} cleanerWG sync.WaitGroup @@ -192,8 +191,7 @@ type indexBlockCacheEntry struct { func newIndexBlockCache() *indexBlockCache { var ibc indexBlockCache - ibc.m = make(map[uint64]indexBlockCacheEntry) - ibc.missesMap = make(map[uint64]uint64) + ibc.m = make(map[uint64]*indexBlockCacheEntry) ibc.cleanerStopCh = make(chan struct{}) ibc.cleanerWG.Add(1) @@ -261,10 +259,10 @@ func (ibc *indexBlockCache) Get(k uint64) *indexBlock { atomic.AddUint64(&ibc.requests, 1) ibc.mu.RLock() - ibe, ok := ibc.m[k] + ibe := ibc.m[k] ibc.mu.RUnlock() - if ok { + if ibe != nil { currentTime := atomic.LoadUint64(¤tTimestamp) if atomic.LoadUint64(&ibe.lastAccessTime) != currentTime { atomic.StoreUint64(&ibe.lastAccessTime, currentTime) @@ -272,22 +270,12 @@ func (ibc *indexBlockCache) Get(k uint64) *indexBlock { return ibe.ib } atomic.AddUint64(&ibc.misses, 1) - ibc.mu.Lock() - ibc.missesMap[k]++ - ibc.mu.Unlock() return nil } func (ibc *indexBlockCache) Put(k uint64, ib *indexBlock) bool { ibc.mu.Lock() - if ibc.missesMap[k] < 2 { - // Do not store infrequently accessed ib in the cache, - // so it don't evict frequently accessed items. - ibc.mu.Unlock() - return false - } - // Clean superflouos cache entries. if overflow := len(ibc.m) - getMaxCachedIndexBlocksPerPart(); overflow > 0 { // Remove 10% of items from the cache. @@ -301,21 +289,9 @@ func (ibc *indexBlockCache) Put(k uint64, ib *indexBlock) bool { } } } - if overflow := len(ibc.missesMap) - 8*getMaxCachedIndexBlocksPerPart(); overflow > 0 { - // Remove 10% of items from the cache. - overflow = int(float64(len(ibc.missesMap)) * 0.1) - for k := range ibc.missesMap { - delete(ibc.missesMap, k) - overflow-- - if overflow == 0 { - break - } - } - } // Store frequently requested ib in the cache. - delete(ibc.missesMap, k) - ibe := indexBlockCacheEntry{ + ibe := &indexBlockCacheEntry{ lastAccessTime: atomic.LoadUint64(¤tTimestamp), ib: ib, }