From cb75747bce6c58954017e5e70300d57a09a44af2 Mon Sep 17 00:00:00 2001 From: beorn7 Date: Tue, 6 Jul 2021 00:20:35 +0200 Subject: [PATCH] Fix re-encoding Signed-off-by: beorn7 --- tsdb/chunkenc/histo.go | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/tsdb/chunkenc/histo.go b/tsdb/chunkenc/histo.go index f588278f0..a3a4abcf8 100644 --- a/tsdb/chunkenc/histo.go +++ b/tsdb/chunkenc/histo.go @@ -361,10 +361,14 @@ func (a *HistoAppender) AppendHistogram(t int64, h histogram.SparseHistogram) { } // Recode converts the current chunk to accommodate an expansion of the set of -// (positive and/or negative) buckets used, according to the provided interjections, resulting in -// the honoring of the provided new posSpans and negSpans -// note: the decode-recode can probably be done more efficiently, but that's for a future optimization +// (positive and/or negative) buckets used, according to the provided +// interjections, resulting in the honoring of the provided new posSpans and +// negSpans. func (a *HistoAppender) Recode(posInterjections, negInterjections []Interjection, posSpans, negSpans []histogram.Span) (Chunk, Appender) { + // TODO(beorn7): This currently just decodes everything and then encodes + // it again with the new span layout. This can probably be done in-place + // by editing the chunk. But let's first see how expensive it is in the + // big picture. it := newHistoIterator(a.b.bytes()) hc := NewHistoChunk() app, err := hc.Appender() @@ -372,18 +376,24 @@ func (a *HistoAppender) Recode(posInterjections, negInterjections []Interjection panic(err) } numPosBuckets, numNegBuckets := countSpans(posSpans), countSpans(negSpans) - posbuckets := make([]int64, numPosBuckets) // new (modified) histogram buckets - negbuckets := make([]int64, numNegBuckets) // new (modified) histogram buckets for it.Next() { tOld, hOld := it.AtHistogram() - // save the modified histogram to the new chunk + + // We have to newly allocate slices for the modified buckets + // here because they are kept by the appender until the next + // append. + // TODO(beorn7): We might be able to optimize this. + posBuckets := make([]int64, numPosBuckets) + negBuckets := make([]int64, numNegBuckets) + + // Save the modified histogram to the new chunk. hOld.PositiveSpans, hOld.NegativeSpans = posSpans, negSpans if len(posInterjections) > 0 { - hOld.PositiveBuckets = interject(hOld.PositiveBuckets, posbuckets, posInterjections) + hOld.PositiveBuckets = interject(hOld.PositiveBuckets, posBuckets, posInterjections) } if len(negInterjections) > 0 { - hOld.NegativeBuckets = interject(hOld.NegativeBuckets, negbuckets, negInterjections) + hOld.NegativeBuckets = interject(hOld.NegativeBuckets, negBuckets, negInterjections) } app.AppendHistogram(tOld, hOld) }