lib/{mergeset,storage}: properly update lastAccessTime in index and data block cache entries

This commit is contained in:
Aliaksandr Valialkin 2020-01-20 14:58:50 +02:00
parent 04363d6511
commit 6665f10e7b
2 changed files with 11 additions and 35 deletions

View file

@ -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(&currentTimestamp)
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(&currentTimestamp),
ib: ib,
}

View file

@ -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(&currentTimestamp)
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(&currentTimestamp),
ib: ib,
}