VictoriaMetrics/lib/storage/partition_search_test.go

273 lines
7.2 KiB
Go
Raw Permalink Normal View History

2019-05-22 21:16:55 +00:00
package storage
import (
"fmt"
"math/rand"
"os"
"sort"
"testing"
"time"
)
func TestPartitionSearch(t *testing.T) {
ptt := timestampFromTime(time.Now())
var ptr TimeRange
ptr.fromPartitionTimestamp(ptt)
t.Run("SinglePart", func(t *testing.T) {
tr := TimeRange{
MinTimestamp: ptr.MinTimestamp + 4e3,
MaxTimestamp: ptr.MaxTimestamp - 4e3,
}
testPartitionSearchEx(t, ptt, tr, 1, 10000, 10)
})
t.Run("SingleRowPerPart", func(t *testing.T) {
tr := TimeRange{
MinTimestamp: ptr.MinTimestamp + 4e3,
MaxTimestamp: ptr.MaxTimestamp - 4e3,
}
testPartitionSearchEx(t, ptt, tr, 1000, 1, 10)
})
t.Run("SingleTSID", func(t *testing.T) {
tr := TimeRange{
MinTimestamp: ptr.MinTimestamp + 4e3,
MaxTimestamp: ptr.MaxTimestamp - 4e3,
}
testPartitionSearchEx(t, ptt, tr, 5, 10000, 1)
})
t.Run("ManyParts", func(t *testing.T) {
tr := TimeRange{
MinTimestamp: ptr.MinTimestamp + 4e3,
MaxTimestamp: ptr.MaxTimestamp - 4e3,
}
testPartitionSearchEx(t, ptt, tr, 300, 300, 20)
})
t.Run("ManyTSIDs", func(t *testing.T) {
tr := TimeRange{
MinTimestamp: ptr.MinTimestamp + 4e3,
MaxTimestamp: ptr.MaxTimestamp - 4e3,
}
testPartitionSearchEx(t, ptt, tr, 5, 10000, 1000)
})
t.Run("ExactTimeRange", func(t *testing.T) {
tr := TimeRange{
MinTimestamp: ptr.MinTimestamp,
MaxTimestamp: ptr.MaxTimestamp,
}
testPartitionSearchEx(t, ptt, tr, 5, 1000, 10)
})
t.Run("InnerTimeRange", func(t *testing.T) {
tr := TimeRange{
MinTimestamp: ptr.MinTimestamp + 4e3,
MaxTimestamp: ptr.MaxTimestamp - 4e3,
}
testPartitionSearchEx(t, ptt, tr, 5, 1000, 10)
})
t.Run("OuterTimeRange", func(t *testing.T) {
tr := TimeRange{
MinTimestamp: ptr.MinTimestamp - 1e6,
MaxTimestamp: ptr.MaxTimestamp + 1e6,
}
testPartitionSearchEx(t, ptt, tr, 5, 1000, 10)
})
t.Run("LowTimeRange", func(t *testing.T) {
tr := TimeRange{
MinTimestamp: ptr.MinTimestamp - 2e6,
MaxTimestamp: ptr.MinTimestamp - 1e6,
}
testPartitionSearchEx(t, ptt, tr, 5, 1000, 10)
})
t.Run("HighTimeRange", func(t *testing.T) {
tr := TimeRange{
MinTimestamp: ptr.MaxTimestamp + 1e6,
MaxTimestamp: ptr.MaxTimestamp + 2e6,
}
testPartitionSearchEx(t, ptt, tr, 5, 1000, 10)
})
t.Run("LowerEndTimeRange", func(t *testing.T) {
tr := TimeRange{
MinTimestamp: ptr.MinTimestamp - 1e6,
MaxTimestamp: ptr.MaxTimestamp - 4e3,
}
testPartitionSearchEx(t, ptt, tr, 5, 1000, 10)
})
t.Run("HigherEndTimeRange", func(t *testing.T) {
tr := TimeRange{
MinTimestamp: ptr.MinTimestamp + 4e3,
MaxTimestamp: ptr.MaxTimestamp + 1e6,
}
testPartitionSearchEx(t, ptt, tr, 5, 1000, 10)
})
}
func testPartitionSearchEx(t *testing.T, ptt int64, tr TimeRange, partsCount, maxRowsPerPart, tsidsCount int) {
t.Helper()
rng := rand.New(rand.NewSource(1))
2019-05-22 21:16:55 +00:00
// Generate tsids to search.
var tsids []TSID
var tsid TSID
for i := 0; i < 25; i++ {
tsid.MetricID = uint64(rng.Intn(tsidsCount * 2))
2019-05-22 21:16:55 +00:00
tsids = append(tsids, tsid)
}
sort.Slice(tsids, func(i, j int) bool { return tsids[i].Less(&tsids[j]) })
// Generate the expected blocks.
rowsCountExpected := int64(0)
rbsExpected := []rawBlock{}
var ptr TimeRange
ptr.fromPartitionTimestamp(ptt)
var rowss [][]rawRow
for i := 0; i < partsCount; i++ {
var rows []rawRow
var r rawRow
r.PrecisionBits = 30
timestamp := ptr.MinTimestamp
rowsCount := 1 + rng.Intn(maxRowsPerPart)
2019-05-22 21:16:55 +00:00
for j := 0; j < rowsCount; j++ {
r.TSID.MetricID = uint64(rng.Intn(tsidsCount))
2019-05-22 21:16:55 +00:00
r.Timestamp = timestamp
r.Value = float64(int(rng.NormFloat64() * 1e5))
2019-05-22 21:16:55 +00:00
timestamp += int64(rng.Intn(1e4))
2019-05-22 21:16:55 +00:00
if timestamp > ptr.MaxTimestamp {
break
}
rows = append(rows, r)
rowsCountExpected++
}
rbs := getTestExpectedRawBlocks(rows, tsids, tr)
rbsExpected = append(rbsExpected, rbs...)
rowss = append(rowss, rows)
}
sort.Slice(rbsExpected, func(i, j int) bool {
a, b := rbsExpected[i], rbsExpected[j]
if a.TSID.Less(&b.TSID) {
return true
}
if b.TSID.Less(&a.TSID) {
return false
}
return a.Timestamps[0] < b.Timestamps[0]
})
// Create partition from rowss and test search on it.
strg := newTestStorage()
strg.retentionMsecs = timestampFromTime(time.Now()) - ptr.MinTimestamp + 3600*1000
pt := mustCreatePartition(ptt, "small-table", "big-table", strg)
2019-05-22 21:16:55 +00:00
smallPartsPath := pt.smallPartsPath
bigPartsPath := pt.bigPartsPath
for _, rows := range rowss {
pt.AddRows(rows)
// Flush just added rows to a separate partitions.
pt.flushPendingRows(true)
2019-05-22 21:16:55 +00:00
}
testPartitionSearch(t, pt, tsids, tr, rbsExpected, -1)
pt.MustClose()
// Open the created partition and test search on it.
pt = mustOpenPartition(smallPartsPath, bigPartsPath, strg)
2019-05-22 21:16:55 +00:00
testPartitionSearch(t, pt, tsids, tr, rbsExpected, rowsCountExpected)
pt.MustClose()
stopTestStorage(strg)
lib/storage: switch from global to per-day index for `MetricName -> TSID` mapping Previously all the newly ingested time series were registered in global `MetricName -> TSID` index. This index was used during data ingestion for locating the TSID (internal series id) for the given canonical metric name (the canonical metric name consists of metric name plus all its labels sorted by label names). The `MetricName -> TSID` index is stored on disk in order to make sure that the data isn't lost on VictoriaMetrics restart or unclean shutdown. The lookup in this index is relatively slow, since VictoriaMetrics needs to read the corresponding data block from disk, unpack it, put the unpacked block into `indexdb/dataBlocks` cache, and then search for the given `MetricName -> TSID` entry there. So VictoriaMetrics uses in-memory cache for speeding up the lookup for active time series. This cache is named `storage/tsid`. If this cache capacity is enough for all the currently ingested active time series, then VictoriaMetrics works fast, since it doesn't need to read the data from disk. VictoriaMetrics starts reading data from `MetricName -> TSID` on-disk index in the following cases: - If `storage/tsid` cache capacity isn't enough for active time series. Then just increase available memory for VictoriaMetrics or reduce the number of active time series ingested into VictoriaMetrics. - If new time series is ingested into VictoriaMetrics. In this case it cannot find the needed entry in the `storage/tsid` cache, so it needs to consult on-disk `MetricName -> TSID` index, since it doesn't know that the index has no the corresponding entry too. This is a typical event under high churn rate, when old time series are constantly substituted with new time series. Reading the data from `MetricName -> TSID` index is slow, so inserts, which lead to reading this index, are counted as slow inserts, and they can be monitored via `vm_slow_row_inserts_total` metric exposed by VictoriaMetrics. Prior to this commit the `MetricName -> TSID` index was global, e.g. it contained entries sorted by `MetricName` for all the time series ever ingested into VictoriaMetrics during the configured -retentionPeriod. This index can become very large under high churn rate and long retention. VictoriaMetrics caches data from this index in `indexdb/dataBlocks` in-memory cache for speeding up index lookups. The `indexdb/dataBlocks` cache may occupy significant share of available memory for storing recently accessed blocks at `MetricName -> TSID` index when searching for newly ingested time series. This commit switches from global `MetricName -> TSID` index to per-day index. This allows significantly reducing the amounts of data, which needs to be cached in `indexdb/dataBlocks`, since now VictoriaMetrics consults only the index for the current day when new time series is ingested into it. The downside of this change is increased indexdb size on disk for workloads without high churn rate, e.g. with static time series, which do no change over time, since now VictoriaMetrics needs to store identical `MetricName -> TSID` entries for static time series for every day. This change removes an optimization for reducing CPU and disk IO spikes at indexdb rotation, since it didn't work correctly - see https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1401 . At the same time the change fixes the issue, which could result in lost access to time series, which stop receving new samples during the first hour after indexdb rotation - see https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2698 The issue with the increased CPU and disk IO usage during indexdb rotation will be addressed in a separate commit according to https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1401#issuecomment-1553488685 This is a follow-up for 1f28b46ae9350795af41cbfc3ca0e8a5af084fce
2023-07-13 22:33:41 +00:00
if err := os.RemoveAll("small-table"); err != nil {
t.Fatalf("cannot remove small parts directory: %s", err)
}
if err := os.RemoveAll("big-table"); err != nil {
t.Fatalf("cannot remove big parts directory: %s", err)
}
2019-05-22 21:16:55 +00:00
}
func testPartitionSearch(t *testing.T, pt *partition, tsids []TSID, tr TimeRange, rbsExpected []rawBlock, rowsCountExpected int64) {
t.Helper()
if err := testPartitionSearchSerial(pt, tsids, tr, rbsExpected, rowsCountExpected); err != nil {
t.Fatalf("unexpected error in serial partition search: %s", err)
}
ch := make(chan error, 5)
for i := 0; i < cap(ch); i++ {
go func() {
ch <- testPartitionSearchSerial(pt, tsids, tr, rbsExpected, rowsCountExpected)
}()
}
for i := 0; i < cap(ch); i++ {
select {
case err := <-ch:
if err != nil {
t.Fatalf("unexpected error in concurrent partition search: %s", err)
}
case <-time.After(time.Second):
t.Fatalf("timeout in concurrent partition search")
}
}
}
func testPartitionSearchSerial(pt *partition, tsids []TSID, tr TimeRange, rbsExpected []rawBlock, rowsCountExpected int64) error {
if rowsCountExpected >= 0 {
// Verify rows count only on partition opened from files.
//
// Online created partition may contain incomplete number of rows
// due to the race with raw rows flusher.
var m partitionMetrics
pt.UpdateMetrics(&m)
if rowsCount := m.TotalRowsCount(); rowsCount != uint64(rowsCountExpected) {
2019-05-22 21:16:55 +00:00
return fmt.Errorf("unexpected rows count; got %d; want %d", rowsCount, rowsCountExpected)
}
}
bs := []Block{}
var pts partitionSearch
pts.Init(pt, tsids, tr)
2019-05-22 21:16:55 +00:00
for pts.NextBlock() {
var b Block
pts.BlockRef.MustReadBlock(&b)
2019-05-22 21:16:55 +00:00
bs = append(bs, b)
}
if err := pts.Error(); err != nil {
return fmt.Errorf("unexpected error: %w", err)
2019-05-22 21:16:55 +00:00
}
pts.MustClose()
rbs := newTestRawBlocks(bs, tr)
if err := testEqualRawBlocks(rbs, rbsExpected); err != nil {
return fmt.Errorf("unequal blocks: %w", err)
2019-05-22 21:16:55 +00:00
}
if rowsCountExpected >= 0 {
var m partitionMetrics
pt.UpdateMetrics(&m)
if rowsCount := m.TotalRowsCount(); rowsCount != uint64(rowsCountExpected) {
2019-05-22 21:16:55 +00:00
return fmt.Errorf("unexpected rows count after search; got %d; want %d", rowsCount, rowsCountExpected)
}
}
// verify that empty tsids returns empty result
pts.Init(pt, []TSID{}, tr)
2019-05-22 21:16:55 +00:00
if pts.NextBlock() {
return fmt.Errorf("unexpected block got for an empty tsids list: %+v", pts.BlockRef)
}
if err := pts.Error(); err != nil {
return fmt.Errorf("unexpected error on empty tsids list: %w", err)
}
pts.MustClose()
2019-05-22 21:16:55 +00:00
return nil
}