2019-05-22 23:16:55 +02:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
2021-02-08 23:34:18 +01:00
|
|
|
"unsafe"
|
2019-05-22 23:16:55 +02:00
|
|
|
|
2022-01-20 17:34:59 +01:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/blockcache"
|
2019-05-22 23:16:55 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/filestream"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fs"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/memory"
|
|
|
|
)
|
|
|
|
|
2022-01-20 17:34:59 +01:00
|
|
|
var ibCache = blockcache.NewCache(getMaxIndexBlocksCacheSize)
|
|
|
|
|
|
|
|
func getMaxIndexBlocksCacheSize() int {
|
|
|
|
maxIndexBlockCacheSizeOnce.Do(func() {
|
2022-01-21 11:45:29 +01:00
|
|
|
maxIndexBlockCacheSize = int(0.1 * float64(memory.Allowed()))
|
2019-05-22 23:16:55 +02:00
|
|
|
})
|
2022-01-20 17:34:59 +01:00
|
|
|
return maxIndexBlockCacheSize
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2022-01-20 17:34:59 +01:00
|
|
|
maxIndexBlockCacheSize int
|
|
|
|
maxIndexBlockCacheSizeOnce sync.Once
|
2019-05-22 23:16:55 +02:00
|
|
|
)
|
|
|
|
|
2020-01-14 21:47:04 +01:00
|
|
|
// part represents a searchable part containing time series data.
|
|
|
|
type part struct {
|
2019-05-22 23:16:55 +02:00
|
|
|
ph partHeader
|
|
|
|
|
|
|
|
// Filesystem path to the part.
|
|
|
|
//
|
|
|
|
// Empty for in-memory part.
|
|
|
|
path string
|
|
|
|
|
2019-07-04 18:09:40 +02:00
|
|
|
// Total size in bytes of part data.
|
|
|
|
size uint64
|
|
|
|
|
2020-01-30 12:22:15 +01:00
|
|
|
timestampsFile fs.MustReadAtCloser
|
|
|
|
valuesFile fs.MustReadAtCloser
|
|
|
|
indexFile fs.MustReadAtCloser
|
2019-05-22 23:16:55 +02:00
|
|
|
|
|
|
|
metaindex []metaindexRow
|
|
|
|
}
|
|
|
|
|
|
|
|
// openFilePart opens file-based part from the given path.
|
|
|
|
func openFilePart(path string) (*part, error) {
|
|
|
|
path = filepath.Clean(path)
|
|
|
|
|
|
|
|
var ph partHeader
|
2023-03-19 09:36:05 +01:00
|
|
|
if err := ph.ReadMetadata(path); err != nil {
|
2020-06-30 21:58:18 +02:00
|
|
|
return nil, fmt.Errorf("cannot parse path to part: %w", err)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
timestampsPath := path + "/timestamps.bin"
|
2020-11-23 08:55:38 +01:00
|
|
|
timestampsFile := fs.MustOpenReaderAt(timestampsPath)
|
2019-07-04 18:09:40 +02:00
|
|
|
timestampsSize := fs.MustFileSize(timestampsPath)
|
2019-05-22 23:16:55 +02:00
|
|
|
|
|
|
|
valuesPath := path + "/values.bin"
|
2020-11-23 08:55:38 +01:00
|
|
|
valuesFile := fs.MustOpenReaderAt(valuesPath)
|
2019-07-04 18:09:40 +02:00
|
|
|
valuesSize := fs.MustFileSize(valuesPath)
|
2019-05-22 23:16:55 +02:00
|
|
|
|
|
|
|
indexPath := path + "/index.bin"
|
2020-11-23 08:55:38 +01:00
|
|
|
indexFile := fs.MustOpenReaderAt(indexPath)
|
2019-07-04 18:09:40 +02:00
|
|
|
indexSize := fs.MustFileSize(indexPath)
|
2019-05-22 23:16:55 +02:00
|
|
|
|
|
|
|
metaindexPath := path + "/metaindex.bin"
|
|
|
|
metaindexFile, err := filestream.Open(metaindexPath, true)
|
|
|
|
if err != nil {
|
|
|
|
timestampsFile.MustClose()
|
|
|
|
valuesFile.MustClose()
|
|
|
|
indexFile.MustClose()
|
2020-06-30 21:58:18 +02:00
|
|
|
return nil, fmt.Errorf("cannot open metaindex file: %w", err)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
2019-07-04 18:09:40 +02:00
|
|
|
metaindexSize := fs.MustFileSize(metaindexPath)
|
2019-05-22 23:16:55 +02:00
|
|
|
|
2019-07-04 18:09:40 +02:00
|
|
|
size := timestampsSize + valuesSize + indexSize + metaindexSize
|
|
|
|
return newPart(&ph, path, size, metaindexFile, timestampsFile, valuesFile, indexFile)
|
2019-05-22 23:16:55 +02: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 12:22:15 +01:00
|
|
|
func newPart(ph *partHeader, path string, size uint64, metaindexReader filestream.ReadCloser, timestampsFile, valuesFile, indexFile fs.MustReadAtCloser) (*part, error) {
|
2019-05-22 23:16:55 +02:00
|
|
|
var errors []error
|
|
|
|
metaindex, err := unmarshalMetaindexRows(nil, metaindexReader)
|
|
|
|
if err != nil {
|
2020-06-30 21:58:18 +02:00
|
|
|
errors = append(errors, fmt.Errorf("cannot unmarshal metaindex data: %w", err))
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
metaindexReader.MustClose()
|
|
|
|
|
2019-10-17 17:22:56 +02:00
|
|
|
var p part
|
|
|
|
p.ph = *ph
|
|
|
|
p.path = path
|
|
|
|
p.size = size
|
|
|
|
p.timestampsFile = timestampsFile
|
|
|
|
p.valuesFile = valuesFile
|
|
|
|
p.indexFile = indexFile
|
|
|
|
p.metaindex = metaindex
|
2019-05-22 23:16:55 +02:00
|
|
|
|
|
|
|
if len(errors) > 0 {
|
|
|
|
// Return only the first error, since it has no sense in returning all errors.
|
2020-06-30 21:58:18 +02:00
|
|
|
err = fmt.Errorf("cannot initialize part %q: %w", &p, errors[0])
|
2019-05-22 23:16:55 +02:00
|
|
|
p.MustClose()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-10-17 17:22:56 +02:00
|
|
|
return &p, nil
|
2019-05-22 23:16:55 +02: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()
|
|
|
|
|
2022-01-20 17:34:59 +01:00
|
|
|
ibCache.RemoveBlocksForPart(p)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type indexBlock struct {
|
|
|
|
bhs []blockHeader
|
|
|
|
}
|
|
|
|
|
2021-02-08 23:34:18 +01:00
|
|
|
func (idxb *indexBlock) SizeBytes() int {
|
|
|
|
return cap(idxb.bhs) * int(unsafe.Sizeof(blockHeader{}))
|
|
|
|
}
|