Skip to content

Commit

Permalink
Set UCR when NCR for some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fionaliao committed Nov 25, 2024
1 parent d9c34a7 commit 956651f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 17 deletions.
57 changes: 47 additions & 10 deletions pkg/querier/batch/chunk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package batch

import (
"slices"
"strconv"
"testing"
"time"
Expand Down Expand Up @@ -82,7 +83,15 @@ func mkGenericChunk(t require.TestingT, from model.Time, points int, encoding ch
return NewGenericChunk(int64(ck.From), int64(ck.Through), ck.Data.NewIterator)
}

func testIter(t require.TestingT, points int, iter chunkenc.Iterator, encoding chunk.Encoding) {
type testIterOptions uint

const (
// setNotCounterResetHintsAsUnknown can be used in cases where it's onerous to generate all the expected counter
// reset hints (e.g. merging lots of chunks together).
setNotCounterResetHintsAsUnknown testIterOptions = iota
)

func testIter(t require.TestingT, points int, iter chunkenc.Iterator, encoding chunk.Encoding, opts ...testIterOptions) {
nextExpectedTS := model.TimeFromUnix(0)
var assertPoint func(i int)
switch encoding {
Expand All @@ -100,8 +109,15 @@ func testIter(t require.TestingT, points int, iter chunkenc.Iterator, encoding c
ts, h := iter.AtHistogram(nil)
require.EqualValues(t, int64(nextExpectedTS), ts, strconv.Itoa(i))
expH := test.GenerateTestHistogram(int(nextExpectedTS))
if nextExpectedTS > 0 {
expH.CounterResetHint = histogram.NotCounterReset
if slices.Contains(opts, setNotCounterResetHintsAsUnknown) {
if h.CounterResetHint == histogram.NotCounterReset {
h.CounterResetHint = histogram.UnknownCounterReset
expH.CounterResetHint = histogram.UnknownCounterReset
}
} else {
if nextExpectedTS > 0 {
expH.CounterResetHint = histogram.NotCounterReset
}
}
test.RequireHistogramEqual(t, expH, h, strconv.Itoa(i))
nextExpectedTS = nextExpectedTS.Add(step)
Expand All @@ -112,8 +128,15 @@ func testIter(t require.TestingT, points int, iter chunkenc.Iterator, encoding c
ts, fh := iter.AtFloatHistogram(nil)
require.EqualValues(t, int64(nextExpectedTS), ts, strconv.Itoa(i))
expFH := test.GenerateTestFloatHistogram(int(nextExpectedTS))
if nextExpectedTS > 0 {
expFH.CounterResetHint = histogram.NotCounterReset
if slices.Contains(opts, setNotCounterResetHintsAsUnknown) {
if fh.CounterResetHint == histogram.NotCounterReset {
fh.CounterResetHint = histogram.UnknownCounterReset
expFH.CounterResetHint = histogram.UnknownCounterReset
}
} else {
if nextExpectedTS > 0 {
expFH.CounterResetHint = histogram.NotCounterReset
}
}
test.RequireFloatHistogramEqual(t, expFH, fh, strconv.Itoa(i))
nextExpectedTS = nextExpectedTS.Add(step)
Expand All @@ -127,7 +150,7 @@ func testIter(t require.TestingT, points int, iter chunkenc.Iterator, encoding c
require.Equal(t, chunkenc.ValNone, iter.Next())
}

func testSeek(t require.TestingT, points int, iter chunkenc.Iterator, encoding chunk.Encoding) {
func testSeek(t require.TestingT, points int, iter chunkenc.Iterator, encoding chunk.Encoding, opts ...testIterOptions) {
var assertPoint func(expectedTS int64, valType chunkenc.ValueType)
switch encoding {
case chunk.PrometheusXorChunk:
Expand All @@ -144,8 +167,15 @@ func testSeek(t require.TestingT, points int, iter chunkenc.Iterator, encoding c
ts, h := iter.AtHistogram(nil)
require.EqualValues(t, expectedTS, ts)
expH := test.GenerateTestHistogram(int(expectedTS))
if expectedTS > 0 {
expH.CounterResetHint = histogram.NotCounterReset
if slices.Contains(opts, setNotCounterResetHintsAsUnknown) {
if h.CounterResetHint == histogram.NotCounterReset {
h.CounterResetHint = histogram.UnknownCounterReset
expH.CounterResetHint = histogram.UnknownCounterReset
}
} else {
if expectedTS > 0 {
expH.CounterResetHint = histogram.NotCounterReset
}
}
test.RequireHistogramEqual(t, expH, h)
require.NoError(t, iter.Err())
Expand All @@ -156,8 +186,15 @@ func testSeek(t require.TestingT, points int, iter chunkenc.Iterator, encoding c
ts, fh := iter.AtFloatHistogram(nil)
require.EqualValues(t, expectedTS, ts)
expFH := test.GenerateTestFloatHistogram(int(expectedTS))
if expectedTS > 0 {
expFH.CounterResetHint = histogram.NotCounterReset
if slices.Contains(opts, setNotCounterResetHintsAsUnknown) {
if fh.CounterResetHint == histogram.NotCounterReset {
fh.CounterResetHint = histogram.UnknownCounterReset
expFH.CounterResetHint = histogram.UnknownCounterReset
}
} else {
if expectedTS > 0 {
expFH.CounterResetHint = histogram.NotCounterReset
}
}
test.RequireFloatHistogramEqual(t, expFH, fh)
require.NoError(t, iter.Err())
Expand Down
14 changes: 7 additions & 7 deletions pkg/querier/batch/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

func TestMergeIter(t *testing.T) {
for _, enc := range []chunk.Encoding{/*chunk.PrometheusXorChunk,*/ chunk.PrometheusHistogramChunk/*, chunk.PrometheusFloatHistogramChunk*/} {
for _, enc := range []chunk.Encoding{chunk.PrometheusXorChunk, chunk.PrometheusHistogramChunk, chunk.PrometheusFloatHistogramChunk} {
t.Run(enc.String(), func(t *testing.T) {
chunk1 := mkGenericChunk(t, 0, 100, enc)
chunk2 := mkGenericChunk(t, model.TimeFromUnix(25), 100, enc)
Expand All @@ -27,15 +27,15 @@ func TestMergeIter(t *testing.T) {
chunk5 := mkGenericChunk(t, model.TimeFromUnix(100), 100, enc)

iter := NewGenericChunkMergeIterator(nil, labels.EmptyLabels(), []GenericChunk{chunk1, chunk2, chunk3, chunk4, chunk5})
testIter(t, 200, iter, enc)
testIter(t, 200, iter, enc, setNotCounterResetHintsAsUnknown)
iter = NewGenericChunkMergeIterator(nil, labels.EmptyLabels(), []GenericChunk{chunk1, chunk2, chunk3, chunk4, chunk5})
testSeek(t, 200, iter, enc)
testSeek(t, 200, iter, enc, setNotCounterResetHintsAsUnknown)

// Re-use iterator.
iter = NewGenericChunkMergeIterator(iter, labels.EmptyLabels(), []GenericChunk{chunk1, chunk2, chunk3, chunk4, chunk5})
testIter(t, 200, iter, enc)
testIter(t, 200, iter, enc, setNotCounterResetHintsAsUnknown)
iter = NewGenericChunkMergeIterator(iter, labels.EmptyLabels(), []GenericChunk{chunk1, chunk2, chunk3, chunk4, chunk5})
testSeek(t, 200, iter, enc)
testSeek(t, 200, iter, enc, setNotCounterResetHintsAsUnknown)
})
}
}
Expand All @@ -56,10 +56,10 @@ func TestMergeHarder(t *testing.T) {
from = from.Add(time.Duration(offset) * time.Second)
}
iter := newMergeIterator(nil, chunks)
testIter(t, offset*numChunks+samples-offset, newIteratorAdapter(nil, iter, labels.EmptyLabels()), enc)
testIter(t, offset*numChunks+samples-offset, newIteratorAdapter(nil, iter, labels.EmptyLabels()), enc, setNotCounterResetHintsAsUnknown)

iter = newMergeIterator(nil, chunks)
testSeek(t, offset*numChunks+samples-offset, newIteratorAdapter(nil, iter, labels.EmptyLabels()), enc)
testSeek(t, offset*numChunks+samples-offset, newIteratorAdapter(nil, iter, labels.EmptyLabels()), enc, setNotCounterResetHintsAsUnknown)
})
}
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/util/test/histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,14 @@ func RequireHistogramEqual(t require.TestingT, expected, actual *histogram.Histo
func RequireFloatHistogramEqual(t require.TestingT, expected, actual *histogram.FloatHistogram, msgAndArgs ...interface{}) {
require.EqualValues(t, expected, actual, msgAndArgs)
}

// RequireHistogramEqualNoCounterResets requires the two histograms to be equal apart from their counter reset hint.
// The hint cannot be CounterReset either
func RequireHistogramEqualNoCounterResets(t require.TestingT, expected, actual *histogram.Histogram, msgAndArgs ...interface{}) {
require.NotEqual(t, histogram.CounterReset, actual.CounterResetHint)
if expected.CounterResetHint != histogram.GaugeType {
expected.CounterResetHint = histogram.UnknownCounterReset
actual.CounterResetHint = histogram.UnknownCounterReset
}
require.EqualValues(t, expected, actual, msgAndArgs)
}

0 comments on commit 956651f

Please sign in to comment.