lib/storage: reset perKeyMisses stats less frequently

This should reduce CPU usage for queries executed with intervals higher than 30 seconds
This commit is contained in:
Aliaksandr Valialkin 2021-07-12 14:33:39 +03:00
parent 3f705fe8d7
commit 51cd19d2e3

View file

@ -196,10 +196,16 @@ func (idxbc *indexBlockCache) MustClose() {
func (idxbc *indexBlockCache) cleaner() { func (idxbc *indexBlockCache) cleaner() {
ticker := time.NewTicker(30 * time.Second) ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop() defer ticker.Stop()
perKeyMissesTicker := time.NewTicker(2 * time.Minute)
defer perKeyMissesTicker.Stop()
for { for {
select { select {
case <-ticker.C: case <-ticker.C:
idxbc.cleanByTimeout() idxbc.cleanByTimeout()
case <-perKeyMissesTicker.C:
idxbc.perKeyMissesLock.Lock()
idxbc.perKeyMisses = make(map[uint64]int, len(idxbc.perKeyMisses))
idxbc.perKeyMissesLock.Unlock()
case <-idxbc.cleanerStopCh: case <-idxbc.cleanerStopCh:
return return
} }
@ -217,10 +223,6 @@ func (idxbc *indexBlockCache) cleanByTimeout() {
} }
} }
idxbc.mu.Unlock() idxbc.mu.Unlock()
idxbc.perKeyMissesLock.Lock()
idxbc.perKeyMisses = make(map[uint64]int, len(idxbc.perKeyMisses))
idxbc.perKeyMissesLock.Unlock()
} }
func (idxbc *indexBlockCache) Get(k uint64) *indexBlock { func (idxbc *indexBlockCache) Get(k uint64) *indexBlock {
@ -360,10 +362,16 @@ func (ibc *inmemoryBlockCache) MustClose() {
func (ibc *inmemoryBlockCache) cleaner() { func (ibc *inmemoryBlockCache) cleaner() {
ticker := time.NewTicker(30 * time.Second) ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop() defer ticker.Stop()
perKeyMissesTicker := time.NewTicker(2 * time.Minute)
defer perKeyMissesTicker.Stop()
for { for {
select { select {
case <-ticker.C: case <-ticker.C:
ibc.cleanByTimeout() ibc.cleanByTimeout()
case <-perKeyMissesTicker.C:
ibc.perKeyMissesLock.Lock()
ibc.perKeyMisses = make(map[inmemoryBlockCacheKey]int, len(ibc.perKeyMisses))
ibc.perKeyMissesLock.Unlock()
case <-ibc.cleanerStopCh: case <-ibc.cleanerStopCh:
return return
} }
@ -381,10 +389,6 @@ func (ibc *inmemoryBlockCache) cleanByTimeout() {
} }
} }
ibc.mu.Unlock() ibc.mu.Unlock()
ibc.perKeyMissesLock.Lock()
ibc.perKeyMisses = make(map[inmemoryBlockCacheKey]int, len(ibc.perKeyMisses))
ibc.perKeyMissesLock.Unlock()
} }
func (ibc *inmemoryBlockCache) Get(k inmemoryBlockCacheKey) *inmemoryBlock { func (ibc *inmemoryBlockCache) Get(k inmemoryBlockCacheKey) *inmemoryBlock {