Histgram: Fix Compact for spans of only empty buckets

Signed-off-by: beorn7 <beorn@grafana.com>
This commit is contained in:
beorn7 2022-04-12 00:37:50 +02:00
parent 106e20cde5
commit 56db51c826
2 changed files with 35 additions and 3 deletions

View File

@ -430,7 +430,11 @@ func compactBuckets(buckets []float64, spans []Span, maxEmptyBuckets int) ([]flo
// Start of span. // Start of span.
if nEmpty == int(spans[iSpan].Length) { if nEmpty == int(spans[iSpan].Length) {
// The whole span is empty. // The whole span is empty.
offset := spans[iSpan].Offset
spans = append(spans[:iSpan], spans[iSpan+1:]...) spans = append(spans[:iSpan], spans[iSpan+1:]...)
if len(spans) > iSpan {
spans[iSpan].Offset += offset + int32(nEmpty)
}
continue continue
} }
spans[iSpan].Length -= uint32(nEmpty) spans[iSpan].Length -= uint32(nEmpty)

View File

@ -794,7 +794,7 @@ func TestFloatHistogramCompact(t *testing.T) {
}, },
}, },
{ {
"cut empty buckets at start or end of chunks, even in the middle", "cut empty buckets at start or end of spans, even in the middle",
&FloatHistogram{ &FloatHistogram{
PositiveSpans: []Span{{-4, 6}, {3, 6}}, PositiveSpans: []Span{{-4, 6}, {3, 6}},
PositiveBuckets: []float64{0, 0, 1, 3.3, 0, 0, 4.2, 0.1, 3.3, 0, 0, 0}, PositiveBuckets: []float64{0, 0, 1, 3.3, 0, 0, 4.2, 0.1, 3.3, 0, 0, 0},
@ -826,7 +826,7 @@ func TestFloatHistogramCompact(t *testing.T) {
}, },
}, },
{ {
"cut empty buckets from the middle of a chunk", "cut empty buckets from the middle of a span",
&FloatHistogram{ &FloatHistogram{
PositiveSpans: []Span{{-4, 6}, {3, 3}}, PositiveSpans: []Span{{-4, 6}, {3, 3}},
PositiveBuckets: []float64{0, 0, 1, 0, 0, 3.3, 4.2, 0.1, 3.3}, PositiveBuckets: []float64{0, 0, 1, 0, 0, 3.3, 4.2, 0.1, 3.3},
@ -842,7 +842,19 @@ func TestFloatHistogramCompact(t *testing.T) {
}, },
}, },
{ {
"cut empty buckets from the middle of a chunk, avoiding some due to maxEmptyBuckets", "cut out a span containing only empty buckets",
&FloatHistogram{
PositiveSpans: []Span{{-4, 3}, {2, 2}, {3, 4}},
PositiveBuckets: []float64{0, 0, 1, 0, 0, 3.3, 4.2, 0.1, 3.3},
},
0,
&FloatHistogram{
PositiveSpans: []Span{{-2, 1}, {7, 4}},
PositiveBuckets: []float64{1, 3.3, 4.2, 0.1, 3.3},
},
},
{
"cut empty buckets from the middle of a span, avoiding some due to maxEmptyBuckets",
&FloatHistogram{ &FloatHistogram{
PositiveSpans: []Span{{-4, 6}, {3, 3}}, PositiveSpans: []Span{{-4, 6}, {3, 3}},
PositiveBuckets: []float64{0, 0, 1, 0, 0, 3.3, 4.2, 0.1, 3.3}, PositiveBuckets: []float64{0, 0, 1, 0, 0, 3.3, 4.2, 0.1, 3.3},
@ -905,6 +917,22 @@ func TestFloatHistogramCompact(t *testing.T) {
NegativeBuckets: []float64{}, NegativeBuckets: []float64{},
}, },
}, },
{
"multiple spans of only empty buckets",
&FloatHistogram{
PositiveSpans: []Span{{-10, 2}, {2, 1}, {3, 3}},
PositiveBuckets: []float64{0, 0, 0, 0, 2, 3},
NegativeSpans: []Span{{-10, 2}, {2, 1}, {3, 3}},
NegativeBuckets: []float64{2, 3, 0, 0, 0, 0},
},
0,
&FloatHistogram{
PositiveSpans: []Span{{-1, 2}},
PositiveBuckets: []float64{2, 3},
NegativeSpans: []Span{{-10, 2}},
NegativeBuckets: []float64{2, 3},
},
},
} }
for _, c := range cases { for _, c := range cases {