2019-05-22 21:16:55 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2020-01-14 19:20:18 +00:00
|
|
|
"time"
|
2021-02-08 22:34:18 +00:00
|
|
|
"unsafe"
|
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() {
|
|
|
|
n := memory.Allowed() / 1024 / 1024 / 8
|
|
|
|
if n < 16 {
|
|
|
|
n = 16
|
|
|
|
}
|
|
|
|
maxCachedIndexBlocksPerPart = n
|
|
|
|
})
|
|
|
|
return maxCachedIndexBlocksPerPart
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
maxCachedIndexBlocksPerPart int
|
|
|
|
maxCachedIndexBlocksPerPartOnce sync.Once
|
|
|
|
)
|
|
|
|
|
2020-01-14 20:47:04 +00:00
|
|
|
// part represents a searchable part containing time series data.
|
|
|
|
type part struct {
|
2019-05-22 21:16:55 +00:00
|
|
|
ph partHeader
|
|
|
|
|
|
|
|
// Filesystem path to the part.
|
|
|
|
//
|
|
|
|
// Empty for in-memory part.
|
|
|
|
path string
|
|
|
|
|
2019-07-04 16:09:40 +00:00
|
|
|
// Total size in bytes of part data.
|
|
|
|
size uint64
|
|
|
|
|
2020-01-30 11:22:15 +00:00
|
|
|
timestampsFile fs.MustReadAtCloser
|
|
|
|
valuesFile fs.MustReadAtCloser
|
|
|
|
indexFile fs.MustReadAtCloser
|
2019-05-22 21:16:55 +00:00
|
|
|
|
|
|
|
metaindex []metaindexRow
|
2019-10-17 15:22:56 +00:00
|
|
|
|
2020-01-14 20:47:04 +00:00
|
|
|
ibCache *indexBlockCache
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// openFilePart opens file-based part from the given path.
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
timestampsPath := path + "/timestamps.bin"
|
2020-11-23 07:55:38 +00:00
|
|
|
timestampsFile := fs.MustOpenReaderAt(timestampsPath)
|
2019-07-04 16:09:40 +00:00
|
|
|
timestampsSize := fs.MustFileSize(timestampsPath)
|
2019-05-22 21:16:55 +00:00
|
|
|
|
|
|
|
valuesPath := path + "/values.bin"
|
2020-11-23 07:55:38 +00:00
|
|
|
valuesFile := fs.MustOpenReaderAt(valuesPath)
|
2019-07-04 16:09:40 +00:00
|
|
|
valuesSize := fs.MustFileSize(valuesPath)
|
2019-05-22 21:16:55 +00:00
|
|
|
|
|
|
|
indexPath := path + "/index.bin"
|
2020-11-23 07:55:38 +00:00
|
|
|
indexFile := fs.MustOpenReaderAt(indexPath)
|
2019-07-04 16:09:40 +00:00
|
|
|
indexSize := fs.MustFileSize(indexPath)
|
2019-05-22 21:16:55 +00:00
|
|
|
|
|
|
|
metaindexPath := path + "/metaindex.bin"
|
|
|
|
metaindexFile, err := filestream.Open(metaindexPath, true)
|
|
|
|
if err != nil {
|
|
|
|
timestampsFile.MustClose()
|
|
|
|
valuesFile.MustClose()
|
|
|
|
indexFile.MustClose()
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("cannot open metaindex file: %w", 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
|
|
|
|
2019-07-04 16:09:40 +00:00
|
|
|
size := timestampsSize + valuesSize + indexSize + metaindexSize
|
|
|
|
return newPart(&ph, path, size, metaindexFile, timestampsFile, valuesFile, indexFile)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// newPart returns new part initialized with the given arguments.
|
|
|
|
//
|
|
|
|
// The returned part calls MustClose on all the files passed to newPart
|
|
|
|
// when calling part.MustClose.
|
2020-01-30 11:22:15 +00:00
|
|
|
func newPart(ph *partHeader, path string, size uint64, metaindexReader filestream.ReadCloser, timestampsFile, valuesFile, indexFile fs.MustReadAtCloser) (*part, error) {
|
2019-05-22 21:16:55 +00:00
|
|
|
var errors []error
|
|
|
|
metaindex, err := unmarshalMetaindexRows(nil, metaindexReader)
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
errors = append(errors, fmt.Errorf("cannot unmarshal metaindex data: %w", err))
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
metaindexReader.MustClose()
|
|
|
|
|
2019-10-17 15:22:56 +00:00
|
|
|
var p part
|
|
|
|
p.ph = *ph
|
|
|
|
p.path = path
|
|
|
|
p.size = size
|
|
|
|
p.timestampsFile = timestampsFile
|
|
|
|
p.valuesFile = valuesFile
|
|
|
|
p.indexFile = indexFile
|
2019-05-22 21:16:55 +00:00
|
|
|
|
2019-10-17 15:22:56 +00:00
|
|
|
p.metaindex = metaindex
|
2020-05-12 11:53:15 +00:00
|
|
|
p.ibCache = newIndexBlockCache()
|
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("cannot initialize part %q: %w", &p, 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
|
|
|
}
|
|
|
|
|
|
|
|
// String returns human-readable representation of p.
|
|
|
|
func (p *part) String() string {
|
|
|
|
if len(p.path) > 0 {
|
|
|
|
return p.path
|
|
|
|
}
|
|
|
|
return p.ph.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// MustClose closes all the part files.
|
|
|
|
func (p *part) MustClose() {
|
|
|
|
p.timestampsFile.MustClose()
|
|
|
|
p.valuesFile.MustClose()
|
|
|
|
p.indexFile.MustClose()
|
|
|
|
|
2019-08-25 11:39:39 +00:00
|
|
|
isBig := p.ph.RowsCount > maxRowsPerSmallPart()
|
2020-01-14 19:20:18 +00:00
|
|
|
p.ibCache.MustClose(isBig)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type indexBlock struct {
|
|
|
|
bhs []blockHeader
|
|
|
|
}
|
|
|
|
|
2021-02-08 22:34:18 +00:00
|
|
|
func (idxb *indexBlock) SizeBytes() int {
|
|
|
|
return cap(idxb.bhs) * int(unsafe.Sizeof(blockHeader{}))
|
|
|
|
}
|
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
type indexBlockCache struct {
|
2019-10-17 15:22:56 +00:00
|
|
|
// Put atomic counters to the top of struct in order to align them 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[uint64]*indexBlockCacheEntry
|
|
|
|
mu sync.RWMutex
|
2020-01-14 19:20:18 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
ib *indexBlock
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 20:47:04 +00:00
|
|
|
func newIndexBlockCache() *indexBlockCache {
|
|
|
|
var ibc indexBlockCache
|
2020-01-20 12:58:50 +00:00
|
|
|
ibc.m = make(map[uint64]*indexBlockCacheEntry)
|
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 *indexBlockCache) MustClose(isBig bool) {
|
|
|
|
close(ibc.cleanerStopCh)
|
|
|
|
ibc.cleanerWG.Wait()
|
|
|
|
ibc.m = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleaner periodically cleans least recently used items.
|
|
|
|
func (ibc *indexBlockCache) cleaner() {
|
2021-02-08 22:34:18 +00:00
|
|
|
ticker := time.NewTicker(30 * time.Second)
|
2020-02-13 10:55:58 +00:00
|
|
|
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 *indexBlockCache) 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 {
|
2021-02-17 18:40:22 +00:00
|
|
|
// Delete items accessed more than two minutes ago.
|
|
|
|
if currentTime-atomic.LoadUint64(&ibe.lastAccessTime) > 2*60 {
|
2020-01-14 19:20:18 +00:00
|
|
|
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 *indexBlockCache) Get(k uint64) *indexBlock {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-14 11:02:52 +00:00
|
|
|
func (ibc *indexBlockCache) Put(k uint64, ib *indexBlock) {
|
2019-05-22 21:16:55 +00:00
|
|
|
ibc.mu.Lock()
|
|
|
|
|
2020-11-24 10:41:34 +00:00
|
|
|
// Clean superfluous cache entries.
|
2019-05-22 21:16:55 +00:00
|
|
|
if overflow := len(ibc.m) - getMaxCachedIndexBlocksPerPart(); overflow > 0 {
|
|
|
|
// Remove 10% of items from the cache.
|
|
|
|
overflow = int(float64(len(ibc.m)) * 0.1)
|
|
|
|
for k := range ibc.m {
|
|
|
|
delete(ibc.m, k)
|
|
|
|
overflow--
|
|
|
|
if overflow == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store frequently requested ib in the cache.
|
2020-01-20 12:58:50 +00:00
|
|
|
ibe := &indexBlockCacheEntry{
|
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 *indexBlockCache) Requests() uint64 {
|
|
|
|
return atomic.LoadUint64(&ibc.requests)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ibc *indexBlockCache) Misses() uint64 {
|
|
|
|
return atomic.LoadUint64(&ibc.misses)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ibc *indexBlockCache) Len() uint64 {
|
|
|
|
ibc.mu.Lock()
|
|
|
|
n := uint64(len(ibc.m))
|
|
|
|
ibc.mu.Unlock()
|
|
|
|
return n
|
|
|
|
}
|
2021-02-08 22:34:18 +00:00
|
|
|
|
|
|
|
func (ibc *indexBlockCache) SizeBytes() uint64 {
|
|
|
|
n := 0
|
|
|
|
ibc.mu.Lock()
|
|
|
|
for _, e := range ibc.m {
|
|
|
|
n += e.ib.SizeBytes()
|
|
|
|
}
|
|
|
|
ibc.mu.Unlock()
|
|
|
|
return uint64(n)
|
|
|
|
}
|