diff --git a/tsdb/chunkenc/histo.go b/tsdb/chunkenc/histo.go index 780ed05971..f588278f02 100644 --- a/tsdb/chunkenc/histo.go +++ b/tsdb/chunkenc/histo.go @@ -387,7 +387,6 @@ func (a *HistoAppender) Recode(posInterjections, negInterjections []Interjection } app.AppendHistogram(tOld, hOld) } - return hc, app } diff --git a/tsdb/chunkenc/histo_meta.go b/tsdb/chunkenc/histo_meta.go index 91506743bb..3010cfa394 100644 --- a/tsdb/chunkenc/histo_meta.go +++ b/tsdb/chunkenc/histo_meta.go @@ -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 } } diff --git a/tsdb/chunkenc/histo_meta_test.go b/tsdb/chunkenc/histo_meta_test.go index 657c783b96..a8339ca2f2 100644 --- a/tsdb/chunkenc/histo_meta_test.go +++ b/tsdb/chunkenc/histo_meta_test.go @@ -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{ diff --git a/tsdb/head.go b/tsdb/head.go index 940d6af83e..1475635e72 100644 --- a/tsdb/head.go +++ b/tsdb/head.go @@ -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"