diff --git a/app/vmselect/netstorage/netstorage.go b/app/vmselect/netstorage/netstorage.go index 0b45386e56..32bd5e1b1b 100644 --- a/app/vmselect/netstorage/netstorage.go +++ b/app/vmselect/netstorage/netstorage.go @@ -514,14 +514,13 @@ func mergeSortBlocks(dst *Result, sbh sortBlocksHeap, dedupInterval int64) { heap.Init(&sbh) for { top := sbh[0] - heap.Pop(&sbh) - if len(sbh) == 0 { + if len(sbh) == 1 { dst.Timestamps = append(dst.Timestamps, top.Timestamps[top.NextIdx:]...) dst.Values = append(dst.Values, top.Values[top.NextIdx:]...) putSortBlock(top) break } - sbNext := sbh[0] + sbNext := sbh.getNextBlock() tsNext := sbNext.Timestamps[sbNext.NextIdx] topTimestamps := top.Timestamps topNextIdx := top.NextIdx @@ -535,8 +534,9 @@ func mergeSortBlocks(dst *Result, sbh sortBlocksHeap, dedupInterval int64) { dst.Values = append(dst.Values, top.Values[topNextIdx:top.NextIdx]...) } if top.NextIdx < len(topTimestamps) { - heap.Push(&sbh, top) + heap.Fix(&sbh, 0) } else { + heap.Pop(&sbh) putSortBlock(top) } } @@ -603,6 +603,21 @@ func (sb *sortBlock) unpackFrom(tmpBlock *storage.Block, tbf *tmpBlocksFile, add type sortBlocksHeap []*sortBlock +func (sbh sortBlocksHeap) getNextBlock() *sortBlock { + if len(sbh) < 2 { + return nil + } + if len(sbh) < 3 { + return sbh[1] + } + a := sbh[1] + b := sbh[2] + if a.Timestamps[a.NextIdx] <= b.Timestamps[b.NextIdx] { + return a + } + return b +} + func (sbh sortBlocksHeap) Len() int { return len(sbh) }