From 5d4ec08a1fd65ff84ea16bca22ad3b20b91604f6 Mon Sep 17 00:00:00 2001 From: Sebastian Rabenhorst Date: Fri, 14 Apr 2023 11:59:30 +0200 Subject: [PATCH] Fixed sampleRingIterator for mixed histograms Signed-off-by: Sebastian Rabenhorst Fixed sampleRingIterator for mixed histograms Signed-off-by: Sebastian Rabenhorst Fixed lint --- storage/buffer.go | 2 ++ storage/buffer_test.go | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/storage/buffer.go b/storage/buffer.go index 27ac21661..2229e5259 100644 --- a/storage/buffer.go +++ b/storage/buffer.go @@ -332,9 +332,11 @@ func (it *sampleRingIterator) Next() chunkenc.ValueType { switch s.Type() { case chunkenc.ValHistogram: it.h = s.H() + it.fh = nil return chunkenc.ValHistogram case chunkenc.ValFloatHistogram: it.fh = s.FH() + it.h = nil return chunkenc.ValFloatHistogram default: it.f = s.F() diff --git a/storage/buffer_test.go b/storage/buffer_test.go index ebe24d8df..70cd7f4ff 100644 --- a/storage/buffer_test.go +++ b/storage/buffer_test.go @@ -21,6 +21,7 @@ import ( "github.com/prometheus/prometheus/model/histogram" "github.com/prometheus/prometheus/tsdb/chunkenc" + "github.com/prometheus/prometheus/tsdb/tsdbutil" ) func TestSampleRing(t *testing.T) { @@ -180,6 +181,28 @@ func TestBufferedSeriesIteratorNoBadAt(t *testing.T) { it.Next() } +func TestBufferedSeriesIteratorMixedHistograms(t *testing.T) { + histograms := tsdbutil.GenerateTestHistograms(2) + + it := NewBufferIterator(NewListSeriesIterator(samples{ + fhSample{t: 1, fh: histograms[0].ToFloat()}, + hSample{t: 2, h: histograms[1]}, + }), 2) + + require.Equal(t, chunkenc.ValNone, it.Seek(3)) + require.NoError(t, it.Err()) + + buf := it.Buffer() + + require.Equal(t, chunkenc.ValFloatHistogram, buf.Next()) + _, fh := buf.AtFloatHistogram() + require.Equal(t, histograms[0].ToFloat(), fh) + + require.Equal(t, chunkenc.ValHistogram, buf.Next()) + _, fh = buf.AtFloatHistogram() + require.Equal(t, histograms[1].ToFloat(), fh) +} + func BenchmarkBufferedSeriesIterator(b *testing.B) { // Simulate a 5 minute rate. it := NewBufferIterator(newFakeSeriesIterator(int64(b.N), 30), 5*60)