2019-05-22 23:16:55 +02:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2022-12-06 00:27:57 +01:00
|
|
|
"path/filepath"
|
|
|
|
|
2019-05-22 23:16:55 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
2021-07-06 15:28:39 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/cgroup"
|
2020-05-14 21:01:51 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fasttime"
|
2022-12-06 00:27:57 +01:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fs"
|
2019-05-22 23:16:55 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
|
|
)
|
|
|
|
|
|
|
|
// inmemoryPart represents in-memory partition.
|
|
|
|
type inmemoryPart struct {
|
|
|
|
ph partHeader
|
|
|
|
|
|
|
|
timestampsData bytesutil.ByteBuffer
|
|
|
|
valuesData bytesutil.ByteBuffer
|
|
|
|
indexData bytesutil.ByteBuffer
|
|
|
|
metaindexData bytesutil.ByteBuffer
|
|
|
|
|
2020-05-14 21:01:51 +02:00
|
|
|
creationTime uint64
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reset resets mp.
|
|
|
|
func (mp *inmemoryPart) Reset() {
|
|
|
|
mp.ph.Reset()
|
|
|
|
|
|
|
|
mp.timestampsData.Reset()
|
|
|
|
mp.valuesData.Reset()
|
|
|
|
mp.indexData.Reset()
|
|
|
|
mp.metaindexData.Reset()
|
|
|
|
|
2020-05-14 21:01:51 +02:00
|
|
|
mp.creationTime = 0
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
|
2023-04-14 07:11:56 +02:00
|
|
|
// MustStoreToDisk stores the mp to the given path on disk.
|
|
|
|
func (mp *inmemoryPart) MustStoreToDisk(path string) {
|
|
|
|
fs.MustMkdirFailIfExist(path)
|
|
|
|
|
2023-03-25 22:33:54 +01:00
|
|
|
timestampsPath := filepath.Join(path, timestampsFilename)
|
2023-04-14 06:42:11 +02:00
|
|
|
fs.MustWriteSync(timestampsPath, mp.timestampsData.B)
|
2023-04-14 06:33:15 +02:00
|
|
|
|
2023-03-25 22:33:54 +01:00
|
|
|
valuesPath := filepath.Join(path, valuesFilename)
|
2023-04-14 06:42:11 +02:00
|
|
|
fs.MustWriteSync(valuesPath, mp.valuesData.B)
|
2023-04-14 06:33:15 +02:00
|
|
|
|
2023-03-25 22:33:54 +01:00
|
|
|
indexPath := filepath.Join(path, indexFilename)
|
2023-04-14 06:42:11 +02:00
|
|
|
fs.MustWriteSync(indexPath, mp.indexData.B)
|
2023-04-14 06:33:15 +02:00
|
|
|
|
2023-03-25 22:33:54 +01:00
|
|
|
metaindexPath := filepath.Join(path, metaindexFilename)
|
2023-04-14 06:42:11 +02:00
|
|
|
fs.MustWriteSync(metaindexPath, mp.metaindexData.B)
|
2023-04-14 06:33:15 +02:00
|
|
|
|
|
|
|
mp.ph.MustWriteMetadata(path)
|
|
|
|
|
2023-04-14 06:18:39 +02:00
|
|
|
fs.MustSyncPath(path)
|
|
|
|
// Do not sync parent directory - it must be synced by the caller.
|
2022-12-06 00:27:57 +01:00
|
|
|
}
|
|
|
|
|
2019-05-22 23:16:55 +02:00
|
|
|
// InitFromRows initializes mp from the given rows.
|
|
|
|
func (mp *inmemoryPart) InitFromRows(rows []rawRow) {
|
|
|
|
if len(rows) == 0 {
|
|
|
|
logger.Panicf("BUG: Inmemory.InitFromRows must accept at least one row")
|
|
|
|
}
|
|
|
|
|
|
|
|
mp.Reset()
|
|
|
|
rrm := getRawRowsMarshaler()
|
|
|
|
rrm.marshalToInmemoryPart(mp, rows)
|
|
|
|
putRawRowsMarshaler(rrm)
|
2020-05-14 21:01:51 +02:00
|
|
|
mp.creationTime = fasttime.UnixTimestamp()
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPart creates new part from mp.
|
|
|
|
//
|
|
|
|
// It is safe calling NewPart multiple times.
|
|
|
|
// It is unsafe re-using mp while the returned part is in use.
|
2023-04-15 00:46:09 +02:00
|
|
|
func (mp *inmemoryPart) NewPart() *part {
|
2022-12-04 07:30:30 +01:00
|
|
|
size := mp.size()
|
|
|
|
return newPart(&mp.ph, "", size, mp.metaindexData.NewReader(), &mp.timestampsData, &mp.valuesData, &mp.indexData)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mp *inmemoryPart) size() uint64 {
|
|
|
|
return uint64(cap(mp.timestampsData.B) + cap(mp.valuesData.B) + cap(mp.indexData.B) + cap(mp.metaindexData.B))
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func getInmemoryPart() *inmemoryPart {
|
2021-07-06 15:28:39 +02:00
|
|
|
select {
|
|
|
|
case mp := <-mpPool:
|
|
|
|
return mp
|
|
|
|
default:
|
2019-05-22 23:16:55 +02:00
|
|
|
return &inmemoryPart{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func putInmemoryPart(mp *inmemoryPart) {
|
|
|
|
mp.Reset()
|
2021-07-06 15:28:39 +02:00
|
|
|
select {
|
|
|
|
case mpPool <- mp:
|
|
|
|
default:
|
|
|
|
// Drop mp in order to reduce memory usage.
|
|
|
|
}
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
|
2021-07-06 15:28:39 +02:00
|
|
|
// Use chan instead of sync.Pool in order to reduce memory usage on systems with big number of CPU cores,
|
|
|
|
// since sync.Pool maintains per-CPU pool of inmemoryPart objects.
|
|
|
|
//
|
|
|
|
// The inmemoryPart object size can exceed 64KB, so it is better to use chan instead of sync.Pool for reducing memory usage.
|
|
|
|
var mpPool = make(chan *inmemoryPart, cgroup.AvailableCPUs())
|