Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OOO Native Histogram counter reset query tests #570

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 173 additions & 0 deletions tsdb/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5565,6 +5565,179 @@ func testChunkQuerierOOOQuery(t *testing.T,
}
}

// This test verifies the counter reset headers for in-order and out-of-order samples upon ingestion.
// Note that when the counter reset(s) occur in OOO samples, the header is set to UnknownCounterReset
// rather than CounterReset. This is because with OOO native histogram samples, it cannot be definitely
// determined if a counter reset occurred because the samples are not consecutive, and another sample
// could potentially come in that would change the status of the header. In this case, the UnknownCounterReset
// headers would be re-checked at query time and updated as needed. However, this test is checking the counter
// reset headers at the time of storage.
func TestOOONativeHistogramsWithCounterResets(t *testing.T) {
for name, scenario := range sampleTypeScenarios {
t.Run(name, func(t *testing.T) {
if name == intHistogram || name == floatHistogram {
testOOONativeHistogramsWithCounterResets(t, scenario)
}
})
}
}

func testOOONativeHistogramsWithCounterResets(t *testing.T, scenario sampleTypeScenario) {
opts := DefaultOptions()
opts.OutOfOrderCapMax = 30
opts.OutOfOrderTimeWindow = 24 * time.Hour.Milliseconds()

type resetFunc func(v int64) bool
defaultResetFunc := func(v int64) bool { return false }

lbls := labels.FromStrings("foo", "bar1")
minutes := func(m int64) int64 { return m * time.Minute.Milliseconds() }

type sampleBatch struct {
from int64
until int64
shouldReset resetFunc
expCounterResetHints []histogram.CounterResetHint
}

tests := []struct {
name string
queryMin int64
queryMax int64
batches []sampleBatch
expectedSamples []chunks.Sample
}{
{
name: "Counter reset within in-order samples",
queryMin: minutes(40),
queryMax: minutes(55),
batches: []sampleBatch{
// In-order samples
{
from: 40,
until: 50,
shouldReset: func(v int64) bool {
return v == 45
},
expCounterResetHints: []histogram.CounterResetHint{histogram.UnknownCounterReset, histogram.NotCounterReset, histogram.NotCounterReset, histogram.NotCounterReset, histogram.NotCounterReset, histogram.CounterReset, histogram.NotCounterReset, histogram.NotCounterReset, histogram.NotCounterReset, histogram.NotCounterReset},
},
},
},
{
name: "Counter reset right at beginning of OOO samples",
queryMin: minutes(40),
queryMax: minutes(55),
batches: []sampleBatch{
// In-order samples
{
from: 40,
until: 45,
shouldReset: defaultResetFunc,
expCounterResetHints: []histogram.CounterResetHint{histogram.UnknownCounterReset, histogram.NotCounterReset, histogram.NotCounterReset, histogram.NotCounterReset, histogram.NotCounterReset},
},
{
from: 50,
until: 55,
shouldReset: defaultResetFunc,
expCounterResetHints: []histogram.CounterResetHint{histogram.UnknownCounterReset, histogram.NotCounterReset, histogram.NotCounterReset, histogram.NotCounterReset, histogram.NotCounterReset},
},
// OOO samples
{
from: 45,
until: 50,
shouldReset: func(v int64) bool {
return v == 45
},
expCounterResetHints: []histogram.CounterResetHint{histogram.UnknownCounterReset, histogram.NotCounterReset, histogram.NotCounterReset, histogram.NotCounterReset, histogram.NotCounterReset},
},
},
},
{
name: "Counter resets in both in-order and OOO samples",
queryMin: minutes(40),
queryMax: minutes(55),
batches: []sampleBatch{
// In-order samples
{
from: 40,
until: 45,
shouldReset: func(v int64) bool {
return v == 44
},
expCounterResetHints: []histogram.CounterResetHint{histogram.UnknownCounterReset, histogram.NotCounterReset, histogram.NotCounterReset, histogram.NotCounterReset, histogram.CounterReset},
},
{
from: 50,
until: 55,
shouldReset: defaultResetFunc,
expCounterResetHints: []histogram.CounterResetHint{histogram.UnknownCounterReset, histogram.NotCounterReset, histogram.NotCounterReset, histogram.NotCounterReset, histogram.NotCounterReset},
},
// OOO samples
{
from: 45,
until: 50,
shouldReset: func(v int64) bool {
return v == 49
},
expCounterResetHints: []histogram.CounterResetHint{histogram.UnknownCounterReset, histogram.NotCounterReset, histogram.NotCounterReset, histogram.NotCounterReset, histogram.UnknownCounterReset},
},
},
},
}
for _, tc := range tests {
t.Run(fmt.Sprintf("name=%s", tc.name), func(t *testing.T) {
db := openTestDB(t, opts, nil)
db.DisableCompactions()
defer func() {
require.NoError(t, db.Close())
}()

app := db.Appender(context.Background())

expSamples := make(map[string][]chunks.Sample)

for _, batch := range tc.batches {
j := batch.from
smplIdx := 0
for i := batch.from; i < batch.until; i++ {
resetCount := batch.shouldReset(i)
if resetCount {
j = 0
}
_, err, s := scenario.appendFunc(app, lbls, minutes(i), j)
require.NoError(t, err)
if s.Type() == chunkenc.ValHistogram {
s.H().CounterResetHint = batch.expCounterResetHints[smplIdx]
} else if s.Type() == chunkenc.ValFloatHistogram {
s.FH().CounterResetHint = batch.expCounterResetHints[smplIdx]
}
expSamples[lbls.String()] = append(expSamples[lbls.String()], s)
j++
smplIdx++
}
}

require.NoError(t, app.Commit())

for k, v := range expSamples {
sort.Slice(v, func(i, j int) bool {
return v[i].T() < v[j].T()
})
expSamples[k] = v
}

querier, err := db.Querier(tc.queryMin, tc.queryMax)
require.NoError(t, err)
defer querier.Close()

seriesSet := query(t, querier, labels.MustNewMatcher(labels.MatchEqual, "foo", "bar1"))
require.NotNil(t, seriesSet[lbls.String()])
require.Len(t, seriesSet, 1)
requireEqualSamples(t, expSamples, seriesSet, false)
})
}
}

func TestOOOAppendAndQuery(t *testing.T) {
for name, scenario := range sampleTypeScenarios {
t.Run(name, func(t *testing.T) {
Expand Down
18 changes: 13 additions & 5 deletions tsdb/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ import (
"github.com/prometheus/prometheus/tsdb/tsdbutil"
)

const (
float = "float"
intHistogram = "integer histogram"
floatHistogram = "float histogram"
gaugeIntHistogram = "gauge int histogram"
gaugeFloatHistogram = "gauge float histogram"
)

type tsValue struct {
Ts int64
V int64
Expand All @@ -25,7 +33,7 @@ type sampleTypeScenario struct {
}

var sampleTypeScenarios = map[string]sampleTypeScenario{
"float": {
float: {
sampleType: sampleMetricTypeFloat,
appendFunc: func(appender storage.Appender, lbls labels.Labels, ts, value int64) (storage.SeriesRef, error, sample) {
s := sample{t: ts, f: float64(value)}
Expand All @@ -36,7 +44,7 @@ var sampleTypeScenarios = map[string]sampleTypeScenario{
return sample{t: ts, f: float64(value)}
},
},
"integer histogram": {
intHistogram: {
sampleType: sampleMetricTypeHistogram,
appendFunc: func(appender storage.Appender, lbls labels.Labels, ts, value int64) (storage.SeriesRef, error, sample) {
s := sample{t: ts, h: tsdbutil.GenerateTestHistogram(int(value))}
Expand All @@ -47,7 +55,7 @@ var sampleTypeScenarios = map[string]sampleTypeScenario{
return sample{t: ts, h: tsdbutil.GenerateTestHistogram(int(value))}
},
},
"float histogram": {
floatHistogram: {
sampleType: sampleMetricTypeHistogram,
appendFunc: func(appender storage.Appender, lbls labels.Labels, ts, value int64) (storage.SeriesRef, error, sample) {
s := sample{t: ts, fh: tsdbutil.GenerateTestFloatHistogram(int(value))}
Expand All @@ -58,7 +66,7 @@ var sampleTypeScenarios = map[string]sampleTypeScenario{
return sample{t: ts, fh: tsdbutil.GenerateTestFloatHistogram(int(value))}
},
},
"gauge int histogram": {
gaugeIntHistogram: {
sampleType: sampleMetricTypeHistogram,
appendFunc: func(appender storage.Appender, lbls labels.Labels, ts, value int64) (storage.SeriesRef, error, sample) {
s := sample{t: ts, h: tsdbutil.GenerateTestGaugeHistogram(int(value))}
Expand All @@ -69,7 +77,7 @@ var sampleTypeScenarios = map[string]sampleTypeScenario{
return sample{t: ts, h: tsdbutil.GenerateTestGaugeHistogram(int(value))}
},
},
"gauge float histogram": {
gaugeFloatHistogram: {
sampleType: sampleMetricTypeHistogram,
appendFunc: func(appender storage.Appender, lbls labels.Labels, ts, value int64) (storage.SeriesRef, error, sample) {
s := sample{t: ts, fh: tsdbutil.GenerateTestGaugeFloatHistogram(int(value))}
Expand Down
Loading