From 06300fe9b824b2b19f9054c36fefce675dc65df9 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Wed, 16 Nov 2022 12:06:14 +0200 Subject: [PATCH] lib/mergeset: retain the buffer with the data used by indexBlock.bhs, inside indexBlock.buf Previously indexBlock.bhs pointed to the buffer, which could be changed over time. This could result in incorrect time series search over time. This is a follow-up for 58b40f514ca2148749635e09c288d62ef150514c Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3343 --- lib/mergeset/part.go | 3 +++ lib/mergeset/part_search.go | 6 ++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/mergeset/part.go b/lib/mergeset/part.go index 4554e10828..f8f45f1b88 100644 --- a/lib/mergeset/part.go +++ b/lib/mergeset/part.go @@ -136,6 +136,9 @@ func (p *part) MustClose() { type indexBlock struct { bhs []blockHeader + + // The buffer for holding the data referrred by bhs + buf []byte } func (idxb *indexBlock) SizeBytes() int { diff --git a/lib/mergeset/part_search.go b/lib/mergeset/part_search.go index a2f2c181dd..65504ec156 100644 --- a/lib/mergeset/part_search.go +++ b/lib/mergeset/part_search.go @@ -285,8 +285,10 @@ func (ps *partSearch) readIndexBlock(mr *metaindexRow) (*indexBlock, error) { if err != nil { return nil, fmt.Errorf("cannot decompress index block: %w", err) } - idxb := &indexBlock{} - idxb.bhs, err = unmarshalBlockHeadersNoCopy(idxb.bhs[:0], ps.indexBuf, int(mr.blockHeadersCount)) + idxb := &indexBlock{ + buf: append([]byte{}, ps.indexBuf...), + } + idxb.bhs, err = unmarshalBlockHeadersNoCopy(idxb.bhs[:0], idxb.buf, int(mr.blockHeadersCount)) if err != nil { return nil, fmt.Errorf("cannot unmarshal block headers from index block (offset=%d, size=%d): %w", mr.indexBlockOffset, mr.indexBlockSize, err) }