2019-05-22 21:16:55 +00:00
|
|
|
package mergeset
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2020-01-14 19:20:18 +00:00
|
|
|
"time"
|
2019-05-22 21:16:55 +00:00
|
|
|
|
2020-05-14 19:01:51 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fasttime"
|
2019-05-22 21:16:55 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/filestream"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fs"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/memory"
|
|
|
|
)
|
|
|
|
|
|
|
|
func getMaxCachedIndexBlocksPerPart() int {
|
|
|
|
maxCachedIndexBlocksPerPartOnce.Do(func() {
|
2019-09-30 09:25:34 +00:00
|
|
|
n := memory.Allowed() / 1024 / 1024 / 4
|
2019-05-22 21:16:55 +00:00
|
|
|
if n == 0 {
|
|
|
|
n = 10
|
|
|
|
}
|
|
|
|
maxCachedIndexBlocksPerPart = n
|
|
|
|
})
|
|
|
|
return maxCachedIndexBlocksPerPart
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
maxCachedIndexBlocksPerPart int
|
|
|
|
maxCachedIndexBlocksPerPartOnce sync.Once
|
|
|
|
)
|
|
|
|
|
|
|
|
func getMaxCachedInmemoryBlocksPerPart() int {
|
|
|
|
maxCachedInmemoryBlocksPerPartOnce.Do(func() {
|
2019-09-30 09:25:34 +00:00
|
|
|
n := memory.Allowed() / 1024 / 1024 / 4
|
2019-05-22 21:16:55 +00:00
|
|
|
if n == 0 {
|
|
|
|
n = 10
|
|
|
|
}
|
|
|
|
maxCachedInmemoryBlocksPerPart = n
|
|
|
|
})
|
|
|
|
return maxCachedInmemoryBlocksPerPart
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
maxCachedInmemoryBlocksPerPart int
|
|
|
|
maxCachedInmemoryBlocksPerPartOnce sync.Once
|
|
|
|
)
|
|
|
|
|
2020-01-14 20:47:04 +00:00
|
|
|
type part struct {
|
2019-05-22 21:16:55 +00:00
|
|
|
ph partHeader
|
|
|
|
|
|
|
|
path string
|
|
|
|
|
2019-07-04 16:09:40 +00:00
|
|
|
size uint64
|
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
mrs []metaindexRow
|
|
|
|
|
2020-01-30 11:22:15 +00:00
|
|
|
indexFile fs.MustReadAtCloser
|
|
|
|
itemsFile fs.MustReadAtCloser
|
|
|
|
lensFile fs.MustReadAtCloser
|
2019-05-22 21:16:55 +00:00
|
|
|
|
2020-01-14 20:47:04 +00:00
|
|
|
idxbCache *indexBlockCache
|
|
|
|
ibCache *inmemoryBlockCache
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func openFilePart(path string) (*part, error) {
|
|
|
|
path = filepath.Clean(path)
|
|
|
|
|
|
|
|
var ph partHeader
|
|
|
|
if err := ph.ParseFromPath(path); err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("cannot parse path to part: %w", err)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
metaindexPath := path + "/metaindex.bin"
|
|
|
|
metaindexFile, err := filestream.Open(metaindexPath, true)
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("cannot open %q: %w", metaindexPath, err)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2019-07-04 16:09:40 +00:00
|
|
|
metaindexSize := fs.MustFileSize(metaindexPath)
|
2019-05-22 21:16:55 +00:00
|
|
|
|
|
|
|
indexPath := path + "/index.bin"
|
|
|
|
indexFile, err := fs.OpenReaderAt(indexPath)
|
|
|
|
if err != nil {
|
|
|
|
metaindexFile.MustClose()
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("cannot open %q: %w", indexPath, err)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2019-07-04 16:09:40 +00:00
|
|
|
indexSize := fs.MustFileSize(indexPath)
|
2019-05-22 21:16:55 +00:00
|
|
|
|
|
|
|
itemsPath := path + "/items.bin"
|
|
|
|
itemsFile, err := fs.OpenReaderAt(itemsPath)
|
|
|
|
if err != nil {
|
|
|
|
metaindexFile.MustClose()
|
|
|
|
indexFile.MustClose()
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("cannot open %q: %w", itemsPath, err)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2019-07-04 16:09:40 +00:00
|
|
|
itemsSize := fs.MustFileSize(itemsPath)
|
2019-05-22 21:16:55 +00:00
|
|
|
|
|
|
|
lensPath := path + "/lens.bin"
|
|
|
|
lensFile, err := fs.OpenReaderAt(lensPath)
|
|
|
|
if err != nil {
|
|
|
|
metaindexFile.MustClose()
|
|
|
|
indexFile.MustClose()
|
|
|
|
itemsFile.MustClose()
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("cannot open %q: %w", lensPath, err)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2019-07-04 16:09:40 +00:00
|
|
|
lensSize := fs.MustFileSize(lensPath)
|
2019-05-22 21:16:55 +00:00
|
|
|
|
2019-07-04 16:09:40 +00:00
|
|
|
size := metaindexSize + indexSize + itemsSize + lensSize
|
|
|
|
return newPart(&ph, path, size, metaindexFile, indexFile, itemsFile, lensFile)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
2020-01-30 11:22:15 +00:00
|
|
|
func newPart(ph *partHeader, path string, size uint64, metaindexReader filestream.ReadCloser, indexFile, itemsFile, lensFile fs.MustReadAtCloser) (*part, error) {
|
2019-05-22 21:16:55 +00:00
|
|
|
var errors []error
|
|
|
|
mrs, err := unmarshalMetaindexRows(nil, metaindexReader)
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
errors = append(errors, fmt.Errorf("cannot unmarshal metaindexRows: %w", err))
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
metaindexReader.MustClose()
|
|
|
|
|
2019-10-17 15:22:56 +00:00
|
|
|
var p part
|
|
|
|
p.path = path
|
|
|
|
p.size = size
|
|
|
|
p.mrs = mrs
|
|
|
|
|
|
|
|
p.indexFile = indexFile
|
|
|
|
p.itemsFile = itemsFile
|
|
|
|
p.lensFile = lensFile
|
2019-05-22 21:16:55 +00:00
|
|
|
|
|
|
|
p.ph.CopyFrom(ph)
|
2020-01-14 20:47:04 +00:00
|
|
|
p.idxbCache = newIndexBlockCache()
|
|
|
|
p.ibCache = newInmemoryBlockCache()
|
2019-05-22 21:16:55 +00:00
|
|
|
|
|
|
|
if len(errors) > 0 {
|
|
|
|
// Return only the first error, since it has no sense in returning all errors.
|
2020-06-30 19:58:18 +00:00
|
|
|
err := fmt.Errorf("error opening part %s: %w", p.path, errors[0])
|
2019-05-22 21:16:55 +00:00
|
|
|
p.MustClose()
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-17 15:22:56 +00:00
|
|
|
return &p, nil
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *part) MustClose() {
|
|
|
|
p.indexFile.MustClose()
|
|
|
|
p.itemsFile.MustClose()
|
|
|
|
p.lensFile.MustClose()
|
|
|
|
|
2020-01-14 19:20:18 +00:00
|
|
|
p.idxbCache.MustClose()
|
|
|
|
p.ibCache.MustClose()
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type indexBlock struct {
|
|
|
|
bhs []blockHeader
|
|
|
|
}
|
|
|
|
|
|
|
|
func getIndexBlock() *indexBlock {
|
|
|
|
v := indexBlockPool.Get()
|
|
|
|
if v == nil {
|
|
|
|
return &indexBlock{}
|
|
|
|
}
|
|
|
|
return v.(*indexBlock)
|
|
|
|
}
|
|
|
|
|
|
|
|
func putIndexBlock(idxb *indexBlock) {
|
|
|
|
idxb.bhs = idxb.bhs[:0]
|
|
|
|
indexBlockPool.Put(idxb)
|
|
|
|
}
|
|
|
|
|
|
|
|
var indexBlockPool sync.Pool
|
|
|
|
|
|
|
|
type indexBlockCache struct {
|
2019-10-17 15:22:56 +00:00
|
|
|
// Atomically updated counters must go first in the struct, so they are properly
|
|
|
|
// aligned to 8 bytes on 32-bit architectures.
|
|
|
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/212
|
|
|
|
requests uint64
|
|
|
|
misses uint64
|
|
|
|
|
2020-01-29 19:19:17 +00:00
|
|
|
m map[uint64]*indexBlockCacheEntry
|
2020-01-14 19:20:18 +00:00
|
|
|
mu sync.RWMutex
|
|
|
|
|
|
|
|
cleanerStopCh chan struct{}
|
|
|
|
cleanerWG sync.WaitGroup
|
|
|
|
}
|
|
|
|
|
|
|
|
type indexBlockCacheEntry struct {
|
2020-01-14 20:47:04 +00:00
|
|
|
// Atomically updated counters must go first in the struct, so they are properly
|
|
|
|
// aligned to 8 bytes on 32-bit architectures.
|
|
|
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/212
|
2020-01-14 19:20:18 +00:00
|
|
|
lastAccessTime uint64
|
2020-01-14 20:47:04 +00:00
|
|
|
|
|
|
|
idxb *indexBlock
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 20:47:04 +00:00
|
|
|
func newIndexBlockCache() *indexBlockCache {
|
|
|
|
var idxbc indexBlockCache
|
2020-01-29 19:19:17 +00:00
|
|
|
idxbc.m = make(map[uint64]*indexBlockCacheEntry)
|
2020-01-14 19:20:18 +00:00
|
|
|
idxbc.cleanerStopCh = make(chan struct{})
|
|
|
|
idxbc.cleanerWG.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer idxbc.cleanerWG.Done()
|
|
|
|
idxbc.cleaner()
|
|
|
|
}()
|
2020-01-14 20:47:04 +00:00
|
|
|
return &idxbc
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 19:20:18 +00:00
|
|
|
func (idxbc *indexBlockCache) MustClose() {
|
|
|
|
close(idxbc.cleanerStopCh)
|
|
|
|
idxbc.cleanerWG.Wait()
|
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
// It is safe returning idxbc.m to pool, since the Reset must be called
|
|
|
|
// when the idxbc entries are no longer accessed by concurrent goroutines.
|
2020-01-14 19:20:18 +00:00
|
|
|
for _, idxbe := range idxbc.m {
|
|
|
|
putIndexBlock(idxbe.idxb)
|
|
|
|
}
|
|
|
|
idxbc.m = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleaner periodically cleans least recently used items.
|
|
|
|
func (idxbc *indexBlockCache) cleaner() {
|
2020-02-13 10:55:58 +00:00
|
|
|
ticker := time.NewTicker(5 * time.Second)
|
|
|
|
defer ticker.Stop()
|
2020-01-14 19:20:18 +00:00
|
|
|
for {
|
|
|
|
select {
|
2020-02-13 10:55:58 +00:00
|
|
|
case <-ticker.C:
|
2020-01-14 19:20:18 +00:00
|
|
|
idxbc.cleanByTimeout()
|
|
|
|
case <-idxbc.cleanerStopCh:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (idxbc *indexBlockCache) cleanByTimeout() {
|
2020-05-14 19:01:51 +00:00
|
|
|
currentTime := fasttime.UnixTimestamp()
|
2020-01-14 19:20:18 +00:00
|
|
|
idxbc.mu.Lock()
|
|
|
|
for k, idxbe := range idxbc.m {
|
|
|
|
// Delete items accessed more than 10 minutes ago.
|
|
|
|
if currentTime-atomic.LoadUint64(&idxbe.lastAccessTime) > 10*60 {
|
|
|
|
delete(idxbc.m, k)
|
|
|
|
}
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2020-01-14 19:20:18 +00:00
|
|
|
idxbc.mu.Unlock()
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (idxbc *indexBlockCache) Get(k uint64) *indexBlock {
|
|
|
|
atomic.AddUint64(&idxbc.requests, 1)
|
|
|
|
idxbc.mu.RLock()
|
2020-01-29 19:19:17 +00:00
|
|
|
idxbe := idxbc.m[k]
|
2019-05-22 21:16:55 +00:00
|
|
|
idxbc.mu.RUnlock()
|
|
|
|
|
2020-01-29 19:19:17 +00:00
|
|
|
if idxbe != nil {
|
2020-05-14 19:01:51 +00:00
|
|
|
currentTime := fasttime.UnixTimestamp()
|
2020-01-14 19:20:18 +00:00
|
|
|
if atomic.LoadUint64(&idxbe.lastAccessTime) != currentTime {
|
|
|
|
atomic.StoreUint64(&idxbe.lastAccessTime, currentTime)
|
|
|
|
}
|
|
|
|
return idxbe.idxb
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
atomic.AddUint64(&idxbc.misses, 1)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put puts idxb under the key k into idxbc.
|
2020-05-14 11:02:52 +00:00
|
|
|
func (idxbc *indexBlockCache) Put(k uint64, idxb *indexBlock) {
|
2019-05-22 21:16:55 +00:00
|
|
|
idxbc.mu.Lock()
|
|
|
|
|
|
|
|
// Remove superflouos entries.
|
|
|
|
if overflow := len(idxbc.m) - getMaxCachedIndexBlocksPerPart(); overflow > 0 {
|
|
|
|
// Remove 10% of items from the cache.
|
|
|
|
overflow = int(float64(len(idxbc.m)) * 0.1)
|
|
|
|
for k := range idxbc.m {
|
|
|
|
// Do not return idxb to pool, since these entries may be used
|
|
|
|
// by concurrent goroutines.
|
|
|
|
delete(idxbc.m, k)
|
|
|
|
overflow--
|
|
|
|
if overflow == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-14 19:20:18 +00:00
|
|
|
// Store idxb in the cache.
|
2020-01-29 19:19:17 +00:00
|
|
|
idxbe := &indexBlockCacheEntry{
|
2020-05-14 19:01:51 +00:00
|
|
|
lastAccessTime: fasttime.UnixTimestamp(),
|
2020-01-14 20:47:04 +00:00
|
|
|
idxb: idxb,
|
2020-01-14 19:20:18 +00:00
|
|
|
}
|
|
|
|
idxbc.m[k] = idxbe
|
2019-05-22 21:16:55 +00:00
|
|
|
idxbc.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (idxbc *indexBlockCache) Len() uint64 {
|
|
|
|
idxbc.mu.RLock()
|
|
|
|
n := len(idxbc.m)
|
|
|
|
idxbc.mu.RUnlock()
|
|
|
|
return uint64(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (idxbc *indexBlockCache) Requests() uint64 {
|
|
|
|
return atomic.LoadUint64(&idxbc.requests)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (idxbc *indexBlockCache) Misses() uint64 {
|
|
|
|
return atomic.LoadUint64(&idxbc.misses)
|
|
|
|
}
|
|
|
|
|
|
|
|
type inmemoryBlockCache struct {
|
2019-10-17 15:22:56 +00:00
|
|
|
// Atomically updated counters must go first in the struct, so they are properly
|
|
|
|
// aligned to 8 bytes on 32-bit architectures.
|
|
|
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/212
|
|
|
|
requests uint64
|
|
|
|
misses uint64
|
|
|
|
|
2020-01-20 12:58:50 +00:00
|
|
|
m map[inmemoryBlockCacheKey]*inmemoryBlockCacheEntry
|
2020-01-14 19:20:18 +00:00
|
|
|
mu sync.RWMutex
|
|
|
|
|
|
|
|
cleanerStopCh chan struct{}
|
|
|
|
cleanerWG sync.WaitGroup
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type inmemoryBlockCacheKey struct {
|
|
|
|
firstItem string
|
|
|
|
itemsBlockOffset uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ibck *inmemoryBlockCacheKey) Init(bh *blockHeader) {
|
|
|
|
ibck.firstItem = ""
|
|
|
|
if bh.itemsBlockSize == 0 {
|
|
|
|
ibck.firstItem = string(bh.firstItem)
|
|
|
|
}
|
|
|
|
ibck.itemsBlockOffset = bh.itemsBlockOffset
|
|
|
|
}
|
|
|
|
|
2020-01-14 19:20:18 +00:00
|
|
|
type inmemoryBlockCacheEntry struct {
|
2020-01-14 20:47:04 +00:00
|
|
|
// Atomically updated counters must go first in the struct, so they are properly
|
|
|
|
// aligned to 8 bytes on 32-bit architectures.
|
|
|
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/212
|
2020-01-14 19:20:18 +00:00
|
|
|
lastAccessTime uint64
|
2020-01-14 20:47:04 +00:00
|
|
|
|
|
|
|
ib *inmemoryBlock
|
2020-01-14 19:20:18 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 20:47:04 +00:00
|
|
|
func newInmemoryBlockCache() *inmemoryBlockCache {
|
|
|
|
var ibc inmemoryBlockCache
|
2020-01-20 12:58:50 +00:00
|
|
|
ibc.m = make(map[inmemoryBlockCacheKey]*inmemoryBlockCacheEntry)
|
2020-01-14 19:20:18 +00:00
|
|
|
|
|
|
|
ibc.cleanerStopCh = make(chan struct{})
|
|
|
|
ibc.cleanerWG.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer ibc.cleanerWG.Done()
|
|
|
|
ibc.cleaner()
|
|
|
|
}()
|
2020-01-14 20:47:04 +00:00
|
|
|
return &ibc
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 19:20:18 +00:00
|
|
|
func (ibc *inmemoryBlockCache) MustClose() {
|
|
|
|
close(ibc.cleanerStopCh)
|
|
|
|
ibc.cleanerWG.Wait()
|
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
// It is safe returning ibc.m entries to pool, since the Reset function may be called
|
|
|
|
// only if no other goroutines access ibc entries.
|
2020-01-14 19:20:18 +00:00
|
|
|
for _, ibe := range ibc.m {
|
|
|
|
putInmemoryBlock(ibe.ib)
|
|
|
|
}
|
|
|
|
ibc.m = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleaner periodically cleans least recently used items.
|
|
|
|
func (ibc *inmemoryBlockCache) cleaner() {
|
2020-02-13 10:55:58 +00:00
|
|
|
ticker := time.NewTicker(5 * time.Second)
|
|
|
|
defer ticker.Stop()
|
2020-01-14 19:20:18 +00:00
|
|
|
for {
|
|
|
|
select {
|
2020-02-13 10:55:58 +00:00
|
|
|
case <-ticker.C:
|
2020-01-14 19:20:18 +00:00
|
|
|
ibc.cleanByTimeout()
|
|
|
|
case <-ibc.cleanerStopCh:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ibc *inmemoryBlockCache) cleanByTimeout() {
|
2020-05-14 19:01:51 +00:00
|
|
|
currentTime := fasttime.UnixTimestamp()
|
2020-01-14 19:20:18 +00:00
|
|
|
ibc.mu.Lock()
|
|
|
|
for k, ibe := range ibc.m {
|
|
|
|
// Delete items accessed more than 10 minutes ago.
|
|
|
|
if currentTime-atomic.LoadUint64(&ibe.lastAccessTime) > 10*60 {
|
|
|
|
delete(ibc.m, k)
|
|
|
|
}
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2020-01-14 19:20:18 +00:00
|
|
|
ibc.mu.Unlock()
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ibc *inmemoryBlockCache) Get(k inmemoryBlockCacheKey) *inmemoryBlock {
|
|
|
|
atomic.AddUint64(&ibc.requests, 1)
|
|
|
|
|
|
|
|
ibc.mu.RLock()
|
2020-01-20 12:58:50 +00:00
|
|
|
ibe := ibc.m[k]
|
2019-05-22 21:16:55 +00:00
|
|
|
ibc.mu.RUnlock()
|
|
|
|
|
2020-01-20 12:58:50 +00:00
|
|
|
if ibe != nil {
|
2020-05-14 19:01:51 +00:00
|
|
|
currentTime := fasttime.UnixTimestamp()
|
2020-01-14 19:20:18 +00:00
|
|
|
if atomic.LoadUint64(&ibe.lastAccessTime) != currentTime {
|
|
|
|
atomic.StoreUint64(&ibe.lastAccessTime, currentTime)
|
|
|
|
}
|
|
|
|
return ibe.ib
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
atomic.AddUint64(&ibc.misses, 1)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put puts ib under key k into ibc.
|
2020-05-14 11:02:52 +00:00
|
|
|
func (ibc *inmemoryBlockCache) Put(k inmemoryBlockCacheKey, ib *inmemoryBlock) {
|
2019-05-22 21:16:55 +00:00
|
|
|
ibc.mu.Lock()
|
|
|
|
|
2020-01-14 19:20:18 +00:00
|
|
|
// Clean superflouos entries in cache.
|
2019-05-22 21:16:55 +00:00
|
|
|
if overflow := len(ibc.m) - getMaxCachedInmemoryBlocksPerPart(); overflow > 0 {
|
|
|
|
// Remove 10% of items from the cache.
|
|
|
|
overflow = int(float64(len(ibc.m)) * 0.1)
|
|
|
|
for k := range ibc.m {
|
|
|
|
// do not call putInmemoryBlock(ib), since the ib
|
|
|
|
// may be used by concurrent goroutines.
|
|
|
|
delete(ibc.m, k)
|
|
|
|
overflow--
|
|
|
|
if overflow == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-14 19:20:18 +00:00
|
|
|
// Store ib in the cache.
|
2020-01-20 12:58:50 +00:00
|
|
|
ibe := &inmemoryBlockCacheEntry{
|
2020-05-14 19:01:51 +00:00
|
|
|
lastAccessTime: fasttime.UnixTimestamp(),
|
2020-01-14 20:47:04 +00:00
|
|
|
ib: ib,
|
2020-01-14 19:20:18 +00:00
|
|
|
}
|
|
|
|
ibc.m[k] = ibe
|
2019-05-22 21:16:55 +00:00
|
|
|
ibc.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ibc *inmemoryBlockCache) Len() uint64 {
|
|
|
|
ibc.mu.RLock()
|
|
|
|
n := len(ibc.m)
|
|
|
|
ibc.mu.RUnlock()
|
|
|
|
return uint64(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ibc *inmemoryBlockCache) Requests() uint64 {
|
|
|
|
return atomic.LoadUint64(&ibc.requests)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ibc *inmemoryBlockCache) Misses() uint64 {
|
|
|
|
return atomic.LoadUint64(&ibc.misses)
|
|
|
|
}
|