app/vmselect/netstorage: optimize mergeSortBlocks() for the worst case when blocks contain interleaved samples

This commit is contained in:
Aliaksandr Valialkin 2022-07-12 12:30:24 +03:00
parent 8429d4af5a
commit 159c2e15e3
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1

View file

@ -507,14 +507,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
@ -528,8 +527,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)
}
}
@ -597,6 +597,21 @@ func (sb *sortBlock) unpackFrom(tmpBlock *storage.Block, tbf *tmpBlocksFile, br
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)
}