From 0876d57aea9636898835c7a179760a6cd72a290d Mon Sep 17 00:00:00 2001 From: beorn7 Date: Mon, 18 Oct 2021 19:37:24 +0200 Subject: [PATCH] chunkenc: Add test for chunk layout encoding And fix a bug exposed by it... Signed-off-by: beorn7 --- tsdb/chunkenc/histogram_meta.go | 2 +- tsdb/chunkenc/histogram_meta_test.go | 75 ++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/tsdb/chunkenc/histogram_meta.go b/tsdb/chunkenc/histogram_meta.go index 730da44d6..dbf212241 100644 --- a/tsdb/chunkenc/histogram_meta.go +++ b/tsdb/chunkenc/histogram_meta.go @@ -136,7 +136,7 @@ func readZeroThreshold(br *bstreamReader) (float64, error) { } return math.Float64frombits(v), nil default: - return math.Ldexp(0.5, int(b-243)), nil + return math.Ldexp(0.5, int(b)-243), nil } } diff --git a/tsdb/chunkenc/histogram_meta_test.go b/tsdb/chunkenc/histogram_meta_test.go index 9c98b7295..2b9de71ec 100644 --- a/tsdb/chunkenc/histogram_meta_test.go +++ b/tsdb/chunkenc/histogram_meta_test.go @@ -19,6 +19,7 @@ package chunkenc import ( + "math" "testing" "github.com/prometheus/prometheus/model/histogram" @@ -294,3 +295,77 @@ func TestInterjection(t *testing.T) { }) } } + +func TestWriteReadHistogramChunkLayout(t *testing.T) { + layouts := []struct { + schema int32 + zeroThreshold float64 + positiveSpans, negativeSpans []histogram.Span + }{ + { + schema: 3, + zeroThreshold: 0, + positiveSpans: []histogram.Span{{Offset: -4, Length: 3}, {Offset: 2, Length: 42}}, + negativeSpans: nil, + }, + { + schema: -2, + zeroThreshold: 2.938735877055719e-39, // Default value in client_golang. + positiveSpans: nil, + negativeSpans: []histogram.Span{{Offset: 2, Length: 5}, {Offset: 1, Length: 34}}, + }, + { + schema: 6, + zeroThreshold: 1024, // The largest power of two we can encode in one byte. + positiveSpans: nil, + negativeSpans: nil, + }, + { + schema: 6, + zeroThreshold: 1025, + positiveSpans: []histogram.Span{{Offset: 2, Length: 5}, {Offset: 1, Length: 34}, {Offset: 0, Length: 0}}, // Weird span. + negativeSpans: []histogram.Span{{Offset: -345, Length: 4545}, {Offset: 53645665, Length: 345}, {Offset: 945995, Length: 85848}}, + }, + { + schema: 6, + zeroThreshold: 2048, + positiveSpans: nil, + negativeSpans: nil, + }, + { + schema: 0, + zeroThreshold: math.Ldexp(0.5, -242), // The smallest power of two we can encode in one byte. + positiveSpans: []histogram.Span{{Offset: -4, Length: 3}}, + negativeSpans: []histogram.Span{{Offset: 2, Length: 5}, {Offset: 1, Length: 34}}, + }, + { + schema: 0, + zeroThreshold: math.Ldexp(0.5, -243), + positiveSpans: []histogram.Span{{Offset: -4, Length: 3}}, + negativeSpans: []histogram.Span{{Offset: 2, Length: 5}, {Offset: 1, Length: 34}}, + }, + { + schema: 4, + zeroThreshold: 42, // Not a power of two. + positiveSpans: nil, + negativeSpans: nil, + }, + } + + bs := bstream{} + + for _, l := range layouts { + writeHistogramChunkLayout(&bs, l.schema, l.zeroThreshold, l.positiveSpans, l.negativeSpans) + } + + bsr := newBReader(bs.bytes()) + + for _, want := range layouts { + gotSchema, gotZeroThreshold, gotPositiveSpans, gotNegativeSpans, err := readHistogramChunkLayout(&bsr) + require.NoError(t, err) + require.Equal(t, want.schema, gotSchema) + require.Equal(t, want.zeroThreshold, gotZeroThreshold) + require.Equal(t, want.positiveSpans, gotPositiveSpans) + require.Equal(t, want.negativeSpans, gotNegativeSpans) + } +}