2019-05-22 21:16:55 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2021-08-25 06:35:03 +00:00
|
|
|
func TestPartitionGetMaxOutBytes(t *testing.T) {
|
|
|
|
n := getMaxOutBytes(".", 1)
|
2019-05-22 21:16:55 +00:00
|
|
|
if n < 1e3 {
|
2021-08-25 06:35:03 +00:00
|
|
|
t.Fatalf("too small free space remained in the current directory: %d", n)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAppendPartsToMerge(t *testing.T) {
|
2019-10-17 15:22:56 +00:00
|
|
|
testAppendPartsToMerge(t, 2, []uint64{}, nil)
|
|
|
|
testAppendPartsToMerge(t, 2, []uint64{123}, nil)
|
|
|
|
testAppendPartsToMerge(t, 2, []uint64{4, 2}, nil)
|
|
|
|
testAppendPartsToMerge(t, 2, []uint64{128, 64, 32, 16, 8, 4, 2, 1}, nil)
|
2020-12-18 18:00:06 +00:00
|
|
|
testAppendPartsToMerge(t, 4, []uint64{128, 64, 32, 10, 9, 7, 3, 1}, []uint64{3, 7, 9, 10})
|
2019-10-17 15:22:56 +00:00
|
|
|
testAppendPartsToMerge(t, 2, []uint64{128, 64, 32, 16, 8, 4, 2, 2}, []uint64{2, 2})
|
|
|
|
testAppendPartsToMerge(t, 4, []uint64{128, 64, 32, 16, 8, 4, 2, 2}, []uint64{2, 2, 4, 8})
|
|
|
|
testAppendPartsToMerge(t, 2, []uint64{1, 1}, []uint64{1, 1})
|
|
|
|
testAppendPartsToMerge(t, 2, []uint64{2, 2, 2}, []uint64{2, 2})
|
|
|
|
testAppendPartsToMerge(t, 2, []uint64{4, 2, 4}, []uint64{4, 4})
|
|
|
|
testAppendPartsToMerge(t, 2, []uint64{1, 3, 7, 2}, nil)
|
|
|
|
testAppendPartsToMerge(t, 3, []uint64{1, 3, 7, 2}, []uint64{1, 2, 3})
|
2021-02-21 19:25:58 +00:00
|
|
|
testAppendPartsToMerge(t, 4, []uint64{1, 3, 7, 2}, []uint64{1, 2, 3})
|
|
|
|
testAppendPartsToMerge(t, 5, []uint64{1, 3, 7, 2}, nil)
|
2020-12-18 18:00:06 +00:00
|
|
|
testAppendPartsToMerge(t, 4, []uint64{1e6, 3e6, 7e6, 2e6}, []uint64{1e6, 2e6, 3e6})
|
2021-02-21 19:25:58 +00:00
|
|
|
testAppendPartsToMerge(t, 4, []uint64{2, 3, 7, 2}, []uint64{2, 2, 3})
|
|
|
|
testAppendPartsToMerge(t, 5, []uint64{2, 3, 7, 2}, nil)
|
2019-10-17 15:22:56 +00:00
|
|
|
testAppendPartsToMerge(t, 3, []uint64{11, 1, 10, 100, 10}, []uint64{10, 10, 11})
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAppendPartsToMergeManyParts(t *testing.T) {
|
|
|
|
// Verify that big number of parts are merged into minimal number of parts
|
|
|
|
// using minimum merges.
|
2021-08-25 06:35:03 +00:00
|
|
|
var sizes []uint64
|
|
|
|
maxOutSize := uint64(0)
|
2020-12-18 18:00:06 +00:00
|
|
|
r := rand.New(rand.NewSource(1))
|
2019-05-22 21:16:55 +00:00
|
|
|
for i := 0; i < 1024; i++ {
|
2020-12-18 18:00:06 +00:00
|
|
|
n := uint64(uint32(r.NormFloat64() * 1e9))
|
2019-05-22 21:16:55 +00:00
|
|
|
n++
|
2021-08-25 06:35:03 +00:00
|
|
|
maxOutSize += n
|
|
|
|
sizes = append(sizes, n)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2021-08-25 06:35:03 +00:00
|
|
|
pws := newTestPartWrappersForSizes(sizes)
|
2019-05-22 21:16:55 +00:00
|
|
|
|
|
|
|
iterationsCount := 0
|
2021-08-25 06:35:03 +00:00
|
|
|
sizeMergedTotal := uint64(0)
|
2019-05-22 21:16:55 +00:00
|
|
|
for {
|
2023-09-25 14:52:37 +00:00
|
|
|
pms := appendPartsToMerge(nil, pws, defaultPartsToMerge, maxOutSize)
|
2019-05-22 21:16:55 +00:00
|
|
|
if len(pms) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
m := make(map[*partWrapper]bool)
|
|
|
|
for _, pw := range pms {
|
|
|
|
m[pw] = true
|
|
|
|
}
|
|
|
|
var pwsNew []*partWrapper
|
2021-08-25 06:35:03 +00:00
|
|
|
size := uint64(0)
|
2019-05-22 21:16:55 +00:00
|
|
|
for _, pw := range pws {
|
|
|
|
if m[pw] {
|
2021-08-25 06:35:03 +00:00
|
|
|
size += pw.p.size
|
2019-05-22 21:16:55 +00:00
|
|
|
} else {
|
|
|
|
pwsNew = append(pwsNew, pw)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pw := &partWrapper{
|
2021-08-25 06:35:03 +00:00
|
|
|
p: &part{
|
|
|
|
size: size,
|
|
|
|
},
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2021-08-25 06:35:03 +00:00
|
|
|
sizeMergedTotal += size
|
2019-05-22 21:16:55 +00:00
|
|
|
pwsNew = append(pwsNew, pw)
|
|
|
|
pws = pwsNew
|
|
|
|
iterationsCount++
|
|
|
|
}
|
2021-08-25 06:35:03 +00:00
|
|
|
sizes = newTestSizesFromPartWrappers(pws)
|
|
|
|
sizeTotal := uint64(0)
|
|
|
|
for _, size := range sizes {
|
|
|
|
sizeTotal += uint64(size)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2021-08-25 06:35:03 +00:00
|
|
|
overhead := float64(sizeMergedTotal) / float64(sizeTotal)
|
2020-12-18 18:00:06 +00:00
|
|
|
if overhead > 2.1 {
|
2021-08-25 06:35:03 +00:00
|
|
|
t.Fatalf("too big overhead; sizes=%d, iterationsCount=%d, sizeTotal=%d, sizeMergedTotal=%d, overhead=%f",
|
|
|
|
sizes, iterationsCount, sizeTotal, sizeMergedTotal, overhead)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2021-08-25 06:35:03 +00:00
|
|
|
if len(sizes) > 18 {
|
|
|
|
t.Fatalf("too many sizes %d; sizes=%d, iterationsCount=%d, sizeTotal=%d, sizeMergedTotal=%d, overhead=%f",
|
|
|
|
len(sizes), sizes, iterationsCount, sizeTotal, sizeMergedTotal, overhead)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-25 06:35:03 +00:00
|
|
|
func testAppendPartsToMerge(t *testing.T, maxPartsToMerge int, initialSizes, expectedSizes []uint64) {
|
2019-05-22 21:16:55 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2021-08-25 06:35:03 +00:00
|
|
|
pws := newTestPartWrappersForSizes(initialSizes)
|
2019-05-22 21:16:55 +00:00
|
|
|
|
|
|
|
// Verify appending to nil.
|
2023-09-25 14:52:37 +00:00
|
|
|
pms := appendPartsToMerge(nil, pws, maxPartsToMerge, 1e9)
|
2021-08-25 06:35:03 +00:00
|
|
|
sizes := newTestSizesFromPartWrappers(pms)
|
|
|
|
if !reflect.DeepEqual(sizes, expectedSizes) {
|
|
|
|
t.Fatalf("unexpected size for maxPartsToMerge=%d, initialSizes=%d; got\n%d; want\n%d",
|
|
|
|
maxPartsToMerge, initialSizes, sizes, expectedSizes)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify appending to prefix
|
|
|
|
prefix := []*partWrapper{
|
2019-05-25 18:43:35 +00:00
|
|
|
{
|
2019-05-22 21:16:55 +00:00
|
|
|
p: &part{
|
2021-08-25 06:35:03 +00:00
|
|
|
size: 1234,
|
2019-05-22 21:16:55 +00:00
|
|
|
},
|
|
|
|
},
|
2019-05-25 18:43:35 +00:00
|
|
|
{},
|
|
|
|
{},
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2023-09-25 14:52:37 +00:00
|
|
|
pms = appendPartsToMerge(prefix, pws, maxPartsToMerge, 1e9)
|
2019-05-22 21:16:55 +00:00
|
|
|
if !reflect.DeepEqual(pms[:len(prefix)], prefix) {
|
2021-08-25 06:35:03 +00:00
|
|
|
t.Fatalf("unexpected prefix for maxPartsToMerge=%d, initialSizes=%d; got\n%+v; want\n%+v",
|
|
|
|
maxPartsToMerge, initialSizes, pms[:len(prefix)], prefix)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 06:35:03 +00:00
|
|
|
sizes = newTestSizesFromPartWrappers(pms[len(prefix):])
|
|
|
|
if !reflect.DeepEqual(sizes, expectedSizes) {
|
|
|
|
t.Fatalf("unexpected prefixed sizes for maxPartsToMerge=%d, initialSizes=%d; got\n%d; want\n%d",
|
|
|
|
maxPartsToMerge, initialSizes, sizes, expectedSizes)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-25 06:35:03 +00:00
|
|
|
func newTestSizesFromPartWrappers(pws []*partWrapper) []uint64 {
|
|
|
|
var sizes []uint64
|
2019-05-22 21:16:55 +00:00
|
|
|
for _, pw := range pws {
|
2021-08-25 06:35:03 +00:00
|
|
|
sizes = append(sizes, pw.p.size)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2021-08-25 06:35:03 +00:00
|
|
|
return sizes
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 06:35:03 +00:00
|
|
|
func newTestPartWrappersForSizes(sizes []uint64) []*partWrapper {
|
2019-05-22 21:16:55 +00:00
|
|
|
var pws []*partWrapper
|
2021-08-25 06:35:03 +00:00
|
|
|
for _, size := range sizes {
|
2019-05-22 21:16:55 +00:00
|
|
|
pw := &partWrapper{
|
|
|
|
p: &part{
|
2021-08-25 06:35:03 +00:00
|
|
|
size: size,
|
2019-05-22 21:16:55 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
pws = append(pws, pw)
|
|
|
|
}
|
|
|
|
return pws
|
|
|
|
}
|
2024-10-27 19:40:13 +00:00
|
|
|
|
|
|
|
func TestMergeInMemoryPartsEmptyResult(t *testing.T) {
|
|
|
|
pt := &partition{}
|
|
|
|
s := newTestStorage()
|
|
|
|
s.retentionMsecs = 1000
|
|
|
|
defer stopTestStorage(s)
|
|
|
|
pt.s = s
|
|
|
|
var pws []*partWrapper
|
|
|
|
|
|
|
|
const (
|
|
|
|
inMemoryPartsCount = 5
|
|
|
|
rowsCount = 10
|
|
|
|
)
|
|
|
|
|
|
|
|
for range inMemoryPartsCount {
|
|
|
|
rows := make([]rawRow, rowsCount)
|
|
|
|
for i := range rowsCount {
|
|
|
|
rows[i].TSID = TSID{
|
|
|
|
MetricID: uint64(i),
|
|
|
|
}
|
|
|
|
rows[i].Value = float64(i)
|
|
|
|
rows[i].Timestamp = int64(i)
|
|
|
|
rows[i].PrecisionBits = 64
|
|
|
|
}
|
|
|
|
|
|
|
|
pws = append(pws, &partWrapper{
|
|
|
|
mp: newTestInmemoryPart(rows),
|
|
|
|
p: &part{},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pwsNew := pt.mustMergeInmemoryParts(pws)
|
|
|
|
if len(pwsNew) != 0 {
|
|
|
|
t.Fatalf("unexpected non-empty pwsNew: %d", len(pwsNew))
|
|
|
|
}
|
|
|
|
}
|