mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-11-21 14:44:00 +00:00
3346576a3a
It has been appeared that VictoriaLogs is frequently used for collecting logs with tens of fields. For example, standard Kuberntes setup on top of Filebeat generates more than 20 fields per each log. Such logs are also known as "wide events". The previous storage format was optimized for logs with a few fields. When at least a single field was referenced in the query, then the all the meta-information about all the log fields was unpacked and parsed per each scanned block during the query. This could require a lot of additional disk IO and CPU time when logs contain many fields. Resolve this issue by providing an (field -> metainfo_offset) index per each field in every data block. This index allows reading and extracting only the needed metainfo for fields used in the query. This index is stored in columnsHeaderIndexFilename ( columns_header_index.bin ). This allows increasing performance for queries over wide events by 10x and more. Another issue was that the data for bloom filters and field values across all the log fields except of _msg was intermixed in two files - fieldBloomFilename ( field_bloom.bin ) and fieldValuesFilename ( field_values.bin ). This could result in huge disk read IO overhead when some small field was referred in the query, since the Operating System usually reads more data than requested. It reads the data from disk in at least 4KiB blocks (usually the block size is much bigger in the range 64KiB - 512KiB). So, if 512-byte bloom filter or values' block is read from the file, then the Operating System reads up to 512KiB of data from disk, which results in 1000x disk read IO overhead. This overhead isn't visible for recently accessed data, since this data is usually stored in RAM (aka Operating System page cache), but this overhead may become very annoying when performing the query over large volumes of data which isn't present in OS page cache. The solution for this issue is to split bloom filters and field values across multiple shards. This reduces the worst-case disk read IO overhead by at least Nx where N is the number of shards, while the disk read IO overhead is completely removed in best case when the number of columns doesn't exceed N. Currently the number of shards is 8 - see bloomValuesShardsCount . This solution increases performance for queries over large volumes of newly ingested data by up to 1000x. The new storage format is versioned as v1, while the old storage format is version as v0. It is stored in the partHeader.FormatVersion. Parts with the old storage format are converted into parts with the new storage format during background merge. It is possible to force merge by querying /internal/force_merge HTTP endpoint - see https://docs.victoriametrics.com/victorialogs/#forced-merge .
191 lines
4.7 KiB
Go
191 lines
4.7 KiB
Go
package logstorage
|
|
|
|
import (
|
|
"path/filepath"
|
|
"sort"
|
|
"sync"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fs"
|
|
)
|
|
|
|
// inmemoryPart is an in-memory part.
|
|
type inmemoryPart struct {
|
|
// ph contains partHeader information for the given in-memory part.
|
|
ph partHeader
|
|
|
|
columnNames bytesutil.ByteBuffer
|
|
metaindex bytesutil.ByteBuffer
|
|
index bytesutil.ByteBuffer
|
|
columnsHeaderIndex bytesutil.ByteBuffer
|
|
columnsHeader bytesutil.ByteBuffer
|
|
timestamps bytesutil.ByteBuffer
|
|
|
|
messageBloomValues bloomValuesBuffer
|
|
bloomValuesShards [bloomValuesShardsCount]bloomValuesBuffer
|
|
}
|
|
|
|
type bloomValuesBuffer struct {
|
|
bloom bytesutil.ByteBuffer
|
|
values bytesutil.ByteBuffer
|
|
}
|
|
|
|
func (b *bloomValuesBuffer) reset() {
|
|
b.bloom.Reset()
|
|
b.values.Reset()
|
|
}
|
|
|
|
func (b *bloomValuesBuffer) NewStreamReader() bloomValuesStreamReader {
|
|
return bloomValuesStreamReader{
|
|
bloom: b.bloom.NewReader(),
|
|
values: b.values.NewReader(),
|
|
}
|
|
}
|
|
|
|
func (b *bloomValuesBuffer) NewStreamWriter() bloomValuesStreamWriter {
|
|
return bloomValuesStreamWriter{
|
|
bloom: &b.bloom,
|
|
values: &b.values,
|
|
}
|
|
}
|
|
|
|
// reset resets mp, so it can be re-used
|
|
func (mp *inmemoryPart) reset() {
|
|
mp.ph.reset()
|
|
|
|
mp.columnNames.Reset()
|
|
mp.metaindex.Reset()
|
|
mp.index.Reset()
|
|
mp.columnsHeaderIndex.Reset()
|
|
mp.columnsHeader.Reset()
|
|
mp.timestamps.Reset()
|
|
|
|
mp.messageBloomValues.reset()
|
|
for i := range mp.bloomValuesShards[:] {
|
|
mp.bloomValuesShards[i].reset()
|
|
}
|
|
}
|
|
|
|
// mustInitFromRows initializes mp from lr.
|
|
func (mp *inmemoryPart) mustInitFromRows(lr *LogRows) {
|
|
mp.reset()
|
|
|
|
sort.Sort(lr)
|
|
|
|
bsw := getBlockStreamWriter()
|
|
bsw.MustInitForInmemoryPart(mp)
|
|
trs := getTmpRows()
|
|
var sidPrev *streamID
|
|
uncompressedBlockSizeBytes := uint64(0)
|
|
timestamps := lr.timestamps
|
|
rows := lr.rows
|
|
streamIDs := lr.streamIDs
|
|
for i := range timestamps {
|
|
streamID := &streamIDs[i]
|
|
if sidPrev == nil {
|
|
sidPrev = streamID
|
|
}
|
|
|
|
if uncompressedBlockSizeBytes >= maxUncompressedBlockSize || !streamID.equal(sidPrev) {
|
|
bsw.MustWriteRows(sidPrev, trs.timestamps, trs.rows)
|
|
trs.reset()
|
|
sidPrev = streamID
|
|
uncompressedBlockSizeBytes = 0
|
|
}
|
|
fields := rows[i]
|
|
trs.timestamps = append(trs.timestamps, timestamps[i])
|
|
trs.rows = append(trs.rows, fields)
|
|
uncompressedBlockSizeBytes += uncompressedRowSizeBytes(fields)
|
|
}
|
|
bsw.MustWriteRows(sidPrev, trs.timestamps, trs.rows)
|
|
putTmpRows(trs)
|
|
|
|
bsw.Finalize(&mp.ph)
|
|
putBlockStreamWriter(bsw)
|
|
}
|
|
|
|
// MustStoreToDisk stores mp to disk at the given path.
|
|
func (mp *inmemoryPart) MustStoreToDisk(path string) {
|
|
fs.MustMkdirFailIfExist(path)
|
|
|
|
columnNamesPath := filepath.Join(path, columnNamesFilename)
|
|
metaindexPath := filepath.Join(path, metaindexFilename)
|
|
indexPath := filepath.Join(path, indexFilename)
|
|
columnsHeaderIndexPath := filepath.Join(path, columnsHeaderIndexFilename)
|
|
columnsHeaderPath := filepath.Join(path, columnsHeaderFilename)
|
|
timestampsPath := filepath.Join(path, timestampsFilename)
|
|
messageValuesPath := filepath.Join(path, messageValuesFilename)
|
|
messageBloomFilterPath := filepath.Join(path, messageBloomFilename)
|
|
|
|
fs.MustWriteSync(columnNamesPath, mp.columnNames.B)
|
|
fs.MustWriteSync(metaindexPath, mp.metaindex.B)
|
|
fs.MustWriteSync(indexPath, mp.index.B)
|
|
fs.MustWriteSync(columnsHeaderIndexPath, mp.columnsHeaderIndex.B)
|
|
fs.MustWriteSync(columnsHeaderPath, mp.columnsHeader.B)
|
|
fs.MustWriteSync(timestampsPath, mp.timestamps.B)
|
|
|
|
fs.MustWriteSync(messageBloomFilterPath, mp.messageBloomValues.bloom.B)
|
|
fs.MustWriteSync(messageValuesPath, mp.messageBloomValues.values.B)
|
|
|
|
for i := range mp.bloomValuesShards[:] {
|
|
shard := &mp.bloomValuesShards[i]
|
|
|
|
bloomPath := getBloomFilePath(path, uint64(i))
|
|
fs.MustWriteSync(bloomPath, shard.bloom.B)
|
|
|
|
valuesPath := getValuesFilePath(path, uint64(i))
|
|
fs.MustWriteSync(valuesPath, shard.values.B)
|
|
}
|
|
|
|
mp.ph.mustWriteMetadata(path)
|
|
|
|
fs.MustSyncPath(path)
|
|
// Do not sync parent directory - it must be synced by the caller.
|
|
}
|
|
|
|
// tmpRows is used as a helper for inmemoryPart.mustInitFromRows()
|
|
type tmpRows struct {
|
|
timestamps []int64
|
|
|
|
rows [][]Field
|
|
}
|
|
|
|
func (trs *tmpRows) reset() {
|
|
trs.timestamps = trs.timestamps[:0]
|
|
|
|
rows := trs.rows
|
|
for i := range rows {
|
|
rows[i] = nil
|
|
}
|
|
trs.rows = rows[:0]
|
|
}
|
|
|
|
func getTmpRows() *tmpRows {
|
|
v := tmpRowsPool.Get()
|
|
if v == nil {
|
|
return &tmpRows{}
|
|
}
|
|
return v.(*tmpRows)
|
|
}
|
|
|
|
func putTmpRows(trs *tmpRows) {
|
|
trs.reset()
|
|
tmpRowsPool.Put(trs)
|
|
}
|
|
|
|
var tmpRowsPool sync.Pool
|
|
|
|
func getInmemoryPart() *inmemoryPart {
|
|
v := inmemoryPartPool.Get()
|
|
if v == nil {
|
|
return &inmemoryPart{}
|
|
}
|
|
return v.(*inmemoryPart)
|
|
}
|
|
|
|
func putInmemoryPart(mp *inmemoryPart) {
|
|
mp.reset()
|
|
inmemoryPartPool.Put(mp)
|
|
}
|
|
|
|
var inmemoryPartPool sync.Pool
|