Fix interjections even more

Signed-off-by: beorn7 <beorn@grafana.com>
This commit is contained in:
beorn7 2021-07-05 23:59:33 +02:00
parent dc1c744169
commit 01957eee2b
4 changed files with 36 additions and 21 deletions

View file

@ -387,7 +387,6 @@ func (a *HistoAppender) Recode(posInterjections, negInterjections []Interjection
}
app.AppendHistogram(tOld, hOld)
}
return hc, app
}

View file

@ -159,41 +159,36 @@ func compareSpans(a, b []histogram.Span) ([]Interjection, bool) {
av, aok := ai.Next()
bv, bok := bi.Next()
loop:
for {
if aok && bok {
if av == bv { // both have an identical value. move on!
// finish WIP interjection and reset
switch {
case aok && bok:
switch {
case av == bv: // Both have an identical value. move on!
// Finish WIP interjection and reset.
if inter.num > 0 {
interjections = append(interjections, inter)
}
inter.num = 0
av, aok = ai.Next()
bv, bok = bi.Next()
if aok {
inter.pos++
}
continue
}
if av < bv { // b misses a value that is in a.
inter.pos++
case av < bv: // b misses a value that is in a.
return interjections, false
}
if av > bv { // a misses a value that is in b. forward b and recompare
case av > bv: // a misses a value that is in b. Forward b and recompare.
inter.num++
bv, bok = bi.Next()
continue
}
} else if aok && !bok { // b misses a value that is in a.
case aok && !bok: // b misses a value that is in a.
return interjections, false
} else if !aok && bok { // a misses a value that is in b. forward b and recompare
case !aok && bok: // a misses a value that is in b. Forward b and recompare.
inter.num++
inter.pos++
bv, bok = bi.Next()
continue
} else { // both iterators ran out. we're done
default: // Both iterators ran out. We're done.
if inter.num > 0 {
interjections = append(interjections, inter)
}
break
break loop
}
}

View file

@ -187,6 +187,28 @@ func TestInterjection(t *testing.T) {
bucketsIn: []int64{6, -3, 0},
bucketsOut: []int64{6, -3, 0, -3, 0},
},
{
description: "double prepond at the beginning and double append at the end",
spansA: []histogram.Span{
{Offset: -10, Length: 3},
},
spansB: []histogram.Span{
{Offset: -12, Length: 7},
},
valid: true,
interjections: []Interjection{
{
pos: 0,
num: 2,
},
{
pos: 3,
num: 2,
},
},
bucketsIn: []int64{6, -3, 0},
bucketsOut: []int64{0, 0, 6, -3, 0, -3, 0},
},
{
description: "single removal of bucket at the start",
spansA: []histogram.Span{
@ -218,7 +240,6 @@ func TestInterjection(t *testing.T) {
},
valid: false,
},
// TODO(beorn7): Add more scenarios.
{
description: "as described in doc comment",
spansA: []histogram.Span{

View file

@ -16,7 +16,6 @@ package tsdb
import (
"context"
"fmt"
"github.com/prometheus/prometheus/pkg/histogram"
"math"
"path/filepath"
"runtime"
@ -32,6 +31,7 @@ import (
"go.uber.org/atomic"
"github.com/prometheus/prometheus/pkg/exemplar"
"github.com/prometheus/prometheus/pkg/histogram"
"github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/storage"
"github.com/prometheus/prometheus/tsdb/chunkenc"