2019-05-22 21:16:55 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/heap"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
2024-05-12 09:24:48 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/slicesutil"
|
2019-05-22 21:16:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// partitionSearch represents a search in the partition.
|
|
|
|
type partitionSearch struct {
|
2020-04-27 05:13:41 +00:00
|
|
|
// BlockRef is the block found after NextBlock call.
|
|
|
|
BlockRef *BlockRef
|
2019-05-22 21:16:55 +00:00
|
|
|
|
|
|
|
// pt is a partition to search.
|
|
|
|
pt *partition
|
|
|
|
|
|
|
|
// pws hold parts snapshot for the given partition during Init call.
|
|
|
|
// This snapshot is used for calling Part.PutParts on partitionSearch.MustClose.
|
|
|
|
pws []*partWrapper
|
|
|
|
|
|
|
|
psPool []partSearch
|
|
|
|
psHeap partSearchHeap
|
|
|
|
|
|
|
|
err error
|
|
|
|
|
|
|
|
nextBlockNoop bool
|
|
|
|
needClosing bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pts *partitionSearch) reset() {
|
2020-04-27 05:13:41 +00:00
|
|
|
pts.BlockRef = nil
|
2019-05-22 21:16:55 +00:00
|
|
|
pts.pt = nil
|
|
|
|
|
|
|
|
for i := range pts.pws {
|
|
|
|
pts.pws[i] = nil
|
|
|
|
}
|
|
|
|
pts.pws = pts.pws[:0]
|
|
|
|
|
|
|
|
for i := range pts.psPool {
|
|
|
|
pts.psPool[i].reset()
|
|
|
|
}
|
|
|
|
pts.psPool = pts.psPool[:0]
|
|
|
|
|
|
|
|
for i := range pts.psHeap {
|
|
|
|
pts.psHeap[i] = nil
|
|
|
|
}
|
|
|
|
pts.psHeap = pts.psHeap[:0]
|
|
|
|
|
|
|
|
pts.err = nil
|
|
|
|
pts.nextBlockNoop = false
|
|
|
|
pts.needClosing = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init initializes the search in the given partition for the given tsid and tr.
|
|
|
|
//
|
2019-09-23 19:34:04 +00:00
|
|
|
// tsids must be sorted.
|
|
|
|
// tsids cannot be modified after the Init call, since it is owned by pts.
|
|
|
|
//
|
2022-07-11 16:21:59 +00:00
|
|
|
// MustClose must be called when partition search is done.
|
2020-04-27 05:13:41 +00:00
|
|
|
func (pts *partitionSearch) Init(pt *partition, tsids []TSID, tr TimeRange) {
|
2019-05-22 21:16:55 +00:00
|
|
|
if pts.needClosing {
|
|
|
|
logger.Panicf("BUG: missing partitionSearch.MustClose call before the next call to Init")
|
|
|
|
}
|
|
|
|
|
|
|
|
pts.reset()
|
|
|
|
pts.pt = pt
|
|
|
|
pts.needClosing = true
|
|
|
|
|
|
|
|
if len(tsids) == 0 {
|
|
|
|
// Fast path - zero tsids.
|
|
|
|
pts.err = io.EOF
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if pt.tr.MinTimestamp > tr.MaxTimestamp || pt.tr.MaxTimestamp < tr.MinTimestamp {
|
|
|
|
// Fast path - the partition doesn't contain rows for the given time range.
|
|
|
|
pts.err = io.EOF
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-01 17:54:21 +00:00
|
|
|
pts.pws = pt.GetParts(pts.pws[:0], true)
|
2019-05-22 21:16:55 +00:00
|
|
|
|
|
|
|
// Initialize psPool.
|
2024-05-12 09:24:48 +00:00
|
|
|
pts.psPool = slicesutil.SetLength(pts.psPool, len(pts.pws))
|
2019-05-22 21:16:55 +00:00
|
|
|
for i, pw := range pts.pws {
|
2020-04-27 05:13:41 +00:00
|
|
|
pts.psPool[i].Init(pw.p, tsids, tr)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the psHeap.
|
|
|
|
pts.psHeap = pts.psHeap[:0]
|
|
|
|
for i := range pts.psPool {
|
|
|
|
ps := &pts.psPool[i]
|
|
|
|
if !ps.NextBlock() {
|
|
|
|
if err := ps.Error(); err != nil {
|
2022-01-28 10:10:47 +00:00
|
|
|
// Return only the first error, since it has no sense in returning all errors.
|
|
|
|
pts.err = fmt.Errorf("cannot initialize partition search: %w", err)
|
|
|
|
return
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
pts.psHeap = append(pts.psHeap, ps)
|
|
|
|
}
|
|
|
|
if len(pts.psHeap) == 0 {
|
|
|
|
pts.err = io.EOF
|
|
|
|
return
|
|
|
|
}
|
|
|
|
heap.Init(&pts.psHeap)
|
2020-04-27 05:13:41 +00:00
|
|
|
pts.BlockRef = &pts.psHeap[0].BlockRef
|
2019-05-22 21:16:55 +00:00
|
|
|
pts.nextBlockNoop = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// NextBlock advances to the next block.
|
|
|
|
//
|
|
|
|
// The blocks are sorted by (TDIS, MinTimestamp). Two subsequent blocks
|
|
|
|
// for the same TSID may contain overlapped time ranges.
|
|
|
|
func (pts *partitionSearch) NextBlock() bool {
|
|
|
|
if pts.err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if pts.nextBlockNoop {
|
|
|
|
pts.nextBlockNoop = false
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
pts.err = pts.nextBlock()
|
|
|
|
if pts.err != nil {
|
|
|
|
if pts.err != io.EOF {
|
2020-06-30 19:58:18 +00:00
|
|
|
pts.err = fmt.Errorf("cannot obtain the next block to search in the partition: %w", pts.err)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pts *partitionSearch) nextBlock() error {
|
|
|
|
psMin := pts.psHeap[0]
|
|
|
|
if psMin.NextBlock() {
|
|
|
|
heap.Fix(&pts.psHeap, 0)
|
2020-04-27 05:13:41 +00:00
|
|
|
pts.BlockRef = &pts.psHeap[0].BlockRef
|
2019-05-22 21:16:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := psMin.Error(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
heap.Pop(&pts.psHeap)
|
|
|
|
|
|
|
|
if len(pts.psHeap) == 0 {
|
|
|
|
return io.EOF
|
|
|
|
}
|
|
|
|
|
2020-04-27 05:13:41 +00:00
|
|
|
pts.BlockRef = &pts.psHeap[0].BlockRef
|
2019-05-22 21:16:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pts *partitionSearch) Error() error {
|
|
|
|
if pts.err == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return pts.err
|
|
|
|
}
|
|
|
|
|
|
|
|
// MustClose closes the pts.
|
|
|
|
func (pts *partitionSearch) MustClose() {
|
|
|
|
if !pts.needClosing {
|
|
|
|
logger.Panicf("BUG: missing Init call before the MustClose call")
|
|
|
|
}
|
|
|
|
|
|
|
|
pts.pt.PutParts(pts.pws)
|
|
|
|
pts.reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
type partSearchHeap []*partSearch
|
|
|
|
|
|
|
|
func (psh *partSearchHeap) Len() int {
|
|
|
|
return len(*psh)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (psh *partSearchHeap) Less(i, j int) bool {
|
|
|
|
x := *psh
|
2020-04-27 05:13:41 +00:00
|
|
|
return x[i].BlockRef.bh.Less(&x[j].BlockRef.bh)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (psh *partSearchHeap) Swap(i, j int) {
|
|
|
|
x := *psh
|
|
|
|
x[i], x[j] = x[j], x[i]
|
|
|
|
}
|
|
|
|
|
2024-07-09 22:14:15 +00:00
|
|
|
func (psh *partSearchHeap) Push(x any) {
|
2019-05-22 21:16:55 +00:00
|
|
|
*psh = append(*psh, x.(*partSearch))
|
|
|
|
}
|
|
|
|
|
2024-07-09 22:14:15 +00:00
|
|
|
func (psh *partSearchHeap) Pop() any {
|
2019-05-22 21:16:55 +00:00
|
|
|
a := *psh
|
|
|
|
v := a[len(a)-1]
|
|
|
|
*psh = a[:len(a)-1]
|
|
|
|
return v
|
|
|
|
}
|