mirror of
https://github.com/prometheus/prometheus.git
synced 2025-03-05 20:59:13 -08:00
Add OOO head tests
Signed-off-by: Carrie Edwards <edwrdscarrie@gmail.com>
This commit is contained in:
parent
6382cc3b31
commit
456d5f71d5
|
@ -14,6 +14,7 @@
|
||||||
package tsdb
|
package tsdb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/prometheus/prometheus/tsdb/tsdbutil"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
@ -22,25 +23,48 @@ import (
|
||||||
const testMaxSize int = 32
|
const testMaxSize int = 32
|
||||||
|
|
||||||
// Formulas chosen to make testing easy.
|
// Formulas chosen to make testing easy.
|
||||||
func valEven(pos int) int { return pos*2 + 2 } // s[0]=2, s[1]=4, s[2]=6, ..., s[31]=64 - Predictable pre-existing values
|
// Formulas chosen to make testing easy.
|
||||||
func valOdd(pos int) int { return pos*2 + 1 } // s[0]=1, s[1]=3, s[2]=5, ..., s[31]=63 - New values will interject at chosen position because they sort before the pre-existing vals.
|
func valEven(pos int) int64 { return int64(pos*2 + 2) } // s[0]=2, s[1]=4, s[2]=6, ..., s[31]=64 - Predictable pre-existing values
|
||||||
|
func valOdd(pos int) int64 { return int64(pos*2 + 1) } // s[0]=1, s[1]=3, s[2]=5, ..., s[31]=63 - New values will interject at chosen position because they sort before the pre-existing vals.
|
||||||
|
|
||||||
func samplify(v int) sample { return sample{int64(v), float64(v), nil, nil} }
|
func makeEvenSampleSlice(n int, sampleFunc func(ts int64) sample) []sample {
|
||||||
|
|
||||||
func makeEvenSampleSlice(n int) []sample {
|
|
||||||
s := make([]sample, n)
|
s := make([]sample, n)
|
||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
s[i] = samplify(valEven(i))
|
s[i] = sampleFunc(valEven(i))
|
||||||
}
|
}
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestOOOInsert tests the following cases:
|
|
||||||
// - Number of pre-existing samples anywhere from 0 to testMaxSize-1.
|
|
||||||
// - Insert new sample before first pre-existing samples, after the last, and anywhere in between.
|
|
||||||
// - With a chunk initial capacity of testMaxSize/8 and testMaxSize, which lets us test non-full and full chunks, and chunks that need to expand themselves.
|
|
||||||
// Note: In all samples used, t always equals v in numeric value. when we talk about 'value' we just refer to a value that will be used for both sample.t and sample.v.
|
|
||||||
func TestOOOInsert(t *testing.T) {
|
func TestOOOInsert(t *testing.T) {
|
||||||
|
scenarios := map[string]struct {
|
||||||
|
sampleFunc func(ts int64) sample
|
||||||
|
}{
|
||||||
|
"float": {
|
||||||
|
sampleFunc: func(ts int64) sample {
|
||||||
|
return sample{t: ts, f: float64(ts)}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"integer histogram": {
|
||||||
|
sampleFunc: func(ts int64) sample {
|
||||||
|
return sample{t: ts, h: tsdbutil.GenerateTestHistogram(int(ts))}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"float histogram": {
|
||||||
|
sampleFunc: func(ts int64) sample {
|
||||||
|
return sample{t: ts, fh: tsdbutil.GenerateTestFloatHistogram(int(ts))}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for name, scenario := range scenarios {
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
testOOOInsert(t, scenario.sampleFunc)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func testOOOInsert(t *testing.T,
|
||||||
|
sampleFunc func(ts int64) sample,
|
||||||
|
) {
|
||||||
for numPreExisting := 0; numPreExisting <= testMaxSize; numPreExisting++ {
|
for numPreExisting := 0; numPreExisting <= testMaxSize; numPreExisting++ {
|
||||||
// For example, if we have numPreExisting 2, then:
|
// For example, if we have numPreExisting 2, then:
|
||||||
// chunk.samples indexes filled 0 1
|
// chunk.samples indexes filled 0 1
|
||||||
|
@ -50,20 +74,21 @@ func TestOOOInsert(t *testing.T) {
|
||||||
|
|
||||||
for insertPos := 0; insertPos <= numPreExisting; insertPos++ {
|
for insertPos := 0; insertPos <= numPreExisting; insertPos++ {
|
||||||
chunk := NewOOOChunk()
|
chunk := NewOOOChunk()
|
||||||
chunk.samples = makeEvenSampleSlice(numPreExisting)
|
chunk.samples = make([]sample, numPreExisting)
|
||||||
newSample := samplify(valOdd(insertPos))
|
chunk.samples = makeEvenSampleSlice(numPreExisting, sampleFunc)
|
||||||
chunk.Insert(newSample.t, newSample.f)
|
newSample := sampleFunc(valOdd(insertPos))
|
||||||
|
chunk.Insert(newSample.t, newSample.f, newSample.h, newSample.fh)
|
||||||
|
|
||||||
var expSamples []sample
|
var expSamples []sample
|
||||||
// Our expected new samples slice, will be first the original samples.
|
// Our expected new samples slice, will be first the original samples.
|
||||||
for i := 0; i < insertPos; i++ {
|
for i := 0; i < insertPos; i++ {
|
||||||
expSamples = append(expSamples, samplify(valEven(i)))
|
expSamples = append(expSamples, sampleFunc(valEven(i)))
|
||||||
}
|
}
|
||||||
// Then the new sample.
|
// Then the new sample.
|
||||||
expSamples = append(expSamples, newSample)
|
expSamples = append(expSamples, newSample)
|
||||||
// Followed by any original samples that were pushed back by the new one.
|
// Followed by any original samples that were pushed back by the new one.
|
||||||
for i := insertPos; i < numPreExisting; i++ {
|
for i := insertPos; i < numPreExisting; i++ {
|
||||||
expSamples = append(expSamples, samplify(valEven(i)))
|
expSamples = append(expSamples, sampleFunc(valEven(i)))
|
||||||
}
|
}
|
||||||
|
|
||||||
require.Equal(t, expSamples, chunk.samples, "numPreExisting %d, insertPos %d", numPreExisting, insertPos)
|
require.Equal(t, expSamples, chunk.samples, "numPreExisting %d, insertPos %d", numPreExisting, insertPos)
|
||||||
|
@ -71,21 +96,49 @@ func TestOOOInsert(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestOOOInsertDuplicate tests the correct behavior when inserting a sample that is a duplicate of any
|
|
||||||
// pre-existing samples, with between 1 and testMaxSize pre-existing samples and
|
// pre-existing samples, with between 1 and testMaxSize pre-existing samples and
|
||||||
// with a chunk initial capacity of testMaxSize/8 and testMaxSize, which lets us test non-full and full chunks, and chunks that need to expand themselves.
|
// with a chunk initial capacity of testMaxSize/8 and testMaxSize, which lets us test non-full and full chunks, and chunks that need to expand themselves.
|
||||||
func TestOOOInsertDuplicate(t *testing.T) {
|
func TestOOOInsertDuplicate(t *testing.T) {
|
||||||
|
scenarios := map[string]struct {
|
||||||
|
sampleFunc func(ts int64) sample
|
||||||
|
}{
|
||||||
|
"float": {
|
||||||
|
sampleFunc: func(ts int64) sample {
|
||||||
|
return sample{t: ts, f: float64(ts)}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"integer histogram": {
|
||||||
|
sampleFunc: func(ts int64) sample {
|
||||||
|
return sample{t: ts, h: tsdbutil.GenerateTestHistogram(int(ts))}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"float histogram": {
|
||||||
|
sampleFunc: func(ts int64) sample {
|
||||||
|
return sample{t: ts, fh: tsdbutil.GenerateTestFloatHistogram(int(ts))}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for name, scenario := range scenarios {
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
testOOOInsertDuplicate(t, scenario.sampleFunc)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func testOOOInsertDuplicate(t *testing.T,
|
||||||
|
sampleFunc func(ts int64) sample,
|
||||||
|
) {
|
||||||
for num := 1; num <= testMaxSize; num++ {
|
for num := 1; num <= testMaxSize; num++ {
|
||||||
for dupPos := 0; dupPos < num; dupPos++ {
|
for dupPos := 0; dupPos < num; dupPos++ {
|
||||||
chunk := NewOOOChunk()
|
chunk := NewOOOChunk()
|
||||||
chunk.samples = makeEvenSampleSlice(num)
|
chunk.samples = makeEvenSampleSlice(num, sampleFunc)
|
||||||
|
|
||||||
dupSample := chunk.samples[dupPos]
|
dupSample := chunk.samples[dupPos]
|
||||||
dupSample.f = 0.123
|
dupSample.f = 0.123
|
||||||
|
|
||||||
ok := chunk.Insert(dupSample.t, dupSample.f)
|
ok := chunk.Insert(dupSample.t, dupSample.f, dupSample.h, dupSample.fh)
|
||||||
|
|
||||||
expSamples := makeEvenSampleSlice(num) // We expect no change.
|
expSamples := makeEvenSampleSlice(num, sampleFunc) // We expect no change.
|
||||||
require.False(t, ok)
|
require.False(t, ok)
|
||||||
require.Equal(t, expSamples, chunk.samples, "num %d, dupPos %d", num, dupPos)
|
require.Equal(t, expSamples, chunk.samples, "num %d, dupPos %d", num, dupPos)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue