2019-05-22 23:16:55 +02:00
|
|
|
package mergeset
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/heap"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2019-09-23 19:40:38 +02:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
2019-05-22 23:16:55 +02:00
|
|
|
)
|
|
|
|
|
2019-09-20 18:46:47 +02:00
|
|
|
// PrepareBlockCallback can transform the passed items allocated at the given data.
|
|
|
|
//
|
|
|
|
// The callback is called during merge before flushing full block of the given items
|
|
|
|
// to persistent storage.
|
|
|
|
//
|
2019-09-23 19:40:38 +02:00
|
|
|
// The callback must return sorted items. The first and the last item must be unchanged.
|
2019-09-20 18:46:47 +02:00
|
|
|
// The callback can re-use data and items for storing the result.
|
2021-02-21 21:06:45 +01:00
|
|
|
type PrepareBlockCallback func(data []byte, items []Item) ([]byte, []Item)
|
2019-09-20 18:46:47 +02:00
|
|
|
|
2019-05-22 23:16:55 +02:00
|
|
|
// mergeBlockStreams merges bsrs and writes result to bsw.
|
|
|
|
//
|
|
|
|
// It also fills ph.
|
|
|
|
//
|
2019-09-20 18:46:47 +02:00
|
|
|
// prepareBlock is optional.
|
|
|
|
//
|
2019-05-22 23:16:55 +02:00
|
|
|
// The function immediately returns when stopCh is closed.
|
|
|
|
//
|
|
|
|
// It also atomically adds the number of items merged to itemsMerged.
|
2020-07-22 23:58:48 +02:00
|
|
|
func mergeBlockStreams(ph *partHeader, bsw *blockStreamWriter, bsrs []*blockStreamReader, prepareBlock PrepareBlockCallback, stopCh <-chan struct{},
|
2020-07-30 18:57:25 +02:00
|
|
|
itemsMerged *uint64) error {
|
2019-05-22 23:16:55 +02:00
|
|
|
bsm := bsmPool.Get().(*blockStreamMerger)
|
2020-07-30 18:57:25 +02:00
|
|
|
if err := bsm.Init(bsrs, prepareBlock); err != nil {
|
2020-06-30 21:58:18 +02:00
|
|
|
return fmt.Errorf("cannot initialize blockStreamMerger: %w", err)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
err := bsm.Merge(bsw, ph, stopCh, itemsMerged)
|
|
|
|
bsm.reset()
|
|
|
|
bsmPool.Put(bsm)
|
|
|
|
bsw.MustClose()
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2020-06-30 21:58:18 +02:00
|
|
|
return fmt.Errorf("cannot merge %d block streams: %s: %w", len(bsrs), bsrs, err)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var bsmPool = &sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
return &blockStreamMerger{}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
type blockStreamMerger struct {
|
2019-09-20 18:46:47 +02:00
|
|
|
prepareBlock PrepareBlockCallback
|
|
|
|
|
2019-05-22 23:16:55 +02:00
|
|
|
bsrHeap bsrHeap
|
|
|
|
|
|
|
|
// ib is a scratch block with pending items.
|
|
|
|
ib inmemoryBlock
|
|
|
|
|
|
|
|
phFirstItemCaught bool
|
2019-09-23 19:40:38 +02:00
|
|
|
|
|
|
|
// This are auxiliary buffers used in flushIB
|
|
|
|
// for consistency checks after prepareBlock call.
|
|
|
|
firstItem []byte
|
|
|
|
lastItem []byte
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (bsm *blockStreamMerger) reset() {
|
2019-09-20 18:46:47 +02:00
|
|
|
bsm.prepareBlock = nil
|
|
|
|
|
2019-05-22 23:16:55 +02:00
|
|
|
for i := range bsm.bsrHeap {
|
|
|
|
bsm.bsrHeap[i] = nil
|
|
|
|
}
|
|
|
|
bsm.bsrHeap = bsm.bsrHeap[:0]
|
|
|
|
bsm.ib.Reset()
|
|
|
|
|
|
|
|
bsm.phFirstItemCaught = false
|
|
|
|
}
|
|
|
|
|
2020-07-30 18:57:25 +02:00
|
|
|
func (bsm *blockStreamMerger) Init(bsrs []*blockStreamReader, prepareBlock PrepareBlockCallback) error {
|
2019-05-22 23:16:55 +02:00
|
|
|
bsm.reset()
|
2019-09-20 18:46:47 +02:00
|
|
|
bsm.prepareBlock = prepareBlock
|
2019-05-22 23:16:55 +02:00
|
|
|
for _, bsr := range bsrs {
|
|
|
|
if bsr.Next() {
|
|
|
|
bsm.bsrHeap = append(bsm.bsrHeap, bsr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := bsr.Error(); err != nil {
|
2020-06-30 21:58:18 +02:00
|
|
|
return fmt.Errorf("cannot obtain the next block from blockStreamReader %q: %w", bsr.path, err)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
heap.Init(&bsm.bsrHeap)
|
|
|
|
|
|
|
|
if len(bsm.bsrHeap) == 0 {
|
|
|
|
return fmt.Errorf("bsrHeap cannot be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var errForciblyStopped = fmt.Errorf("forcibly stopped")
|
|
|
|
|
|
|
|
func (bsm *blockStreamMerger) Merge(bsw *blockStreamWriter, ph *partHeader, stopCh <-chan struct{}, itemsMerged *uint64) error {
|
|
|
|
again:
|
|
|
|
if len(bsm.bsrHeap) == 0 {
|
|
|
|
// Write the last (maybe incomplete) inmemoryBlock to bsw.
|
|
|
|
bsm.flushIB(bsw, ph, itemsMerged)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-stopCh:
|
|
|
|
return errForciblyStopped
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
bsr := heap.Pop(&bsm.bsrHeap).(*blockStreamReader)
|
|
|
|
|
|
|
|
var nextItem []byte
|
|
|
|
hasNextItem := false
|
|
|
|
if len(bsm.bsrHeap) > 0 {
|
|
|
|
nextItem = bsm.bsrHeap[0].bh.firstItem
|
|
|
|
hasNextItem = true
|
|
|
|
}
|
2021-02-21 21:06:45 +01:00
|
|
|
items := bsr.Block.items
|
|
|
|
data := bsr.Block.data
|
2019-09-23 19:40:38 +02:00
|
|
|
for bsr.blockItemIdx < len(bsr.Block.items) {
|
2021-02-21 21:06:45 +01:00
|
|
|
item := items[bsr.blockItemIdx].Bytes(data)
|
2019-09-23 19:40:38 +02:00
|
|
|
if hasNextItem && string(item) > string(nextItem) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if !bsm.ib.Add(item) {
|
|
|
|
// The bsm.ib is full. Flush it to bsw and continue.
|
|
|
|
bsm.flushIB(bsw, ph, itemsMerged)
|
2019-05-22 23:16:55 +02:00
|
|
|
continue
|
|
|
|
}
|
2019-09-23 19:40:38 +02:00
|
|
|
bsr.blockItemIdx++
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
if bsr.blockItemIdx == len(bsr.Block.items) {
|
|
|
|
// bsr.Block is fully read. Proceed to the next block.
|
|
|
|
if bsr.Next() {
|
|
|
|
heap.Push(&bsm.bsrHeap, bsr)
|
|
|
|
goto again
|
|
|
|
}
|
|
|
|
if err := bsr.Error(); err != nil {
|
2020-06-30 21:58:18 +02:00
|
|
|
return fmt.Errorf("cannot read storageBlock: %w", err)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
goto again
|
|
|
|
}
|
|
|
|
|
|
|
|
// The next item in the bsr.Block exceeds nextItem.
|
|
|
|
// Adjust bsr.bh.firstItem and return bsr to heap.
|
2021-02-21 21:06:45 +01:00
|
|
|
bsr.bh.firstItem = append(bsr.bh.firstItem[:0], bsr.Block.items[bsr.blockItemIdx].String(bsr.Block.data)...)
|
2019-05-22 23:16:55 +02:00
|
|
|
heap.Push(&bsm.bsrHeap, bsr)
|
|
|
|
goto again
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bsm *blockStreamMerger) flushIB(bsw *blockStreamWriter, ph *partHeader, itemsMerged *uint64) {
|
2021-02-21 21:06:45 +01:00
|
|
|
items := bsm.ib.items
|
|
|
|
data := bsm.ib.data
|
|
|
|
if len(items) == 0 {
|
2019-05-22 23:16:55 +02:00
|
|
|
// Nothing to flush.
|
|
|
|
return
|
|
|
|
}
|
2021-02-21 21:06:45 +01:00
|
|
|
atomic.AddUint64(itemsMerged, uint64(len(items)))
|
2019-09-20 18:46:47 +02:00
|
|
|
if bsm.prepareBlock != nil {
|
2021-02-21 21:06:45 +01:00
|
|
|
bsm.firstItem = append(bsm.firstItem[:0], items[0].String(data)...)
|
|
|
|
bsm.lastItem = append(bsm.lastItem[:0], items[len(items)-1].String(data)...)
|
|
|
|
data, items = bsm.prepareBlock(data, items)
|
|
|
|
bsm.ib.data = data
|
|
|
|
bsm.ib.items = items
|
|
|
|
if len(items) == 0 {
|
2019-09-23 19:40:38 +02:00
|
|
|
// Nothing to flush
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Consistency checks after prepareBlock call.
|
2021-02-21 21:06:45 +01:00
|
|
|
firstItem := items[0].String(data)
|
|
|
|
if firstItem != string(bsm.firstItem) {
|
2019-09-23 19:40:38 +02:00
|
|
|
logger.Panicf("BUG: prepareBlock must return first item equal to the original first item;\ngot\n%X\nwant\n%X", firstItem, bsm.firstItem)
|
|
|
|
}
|
2021-02-21 21:06:45 +01:00
|
|
|
lastItem := items[len(items)-1].String(data)
|
|
|
|
if lastItem != string(bsm.lastItem) {
|
2019-09-23 19:40:38 +02:00
|
|
|
logger.Panicf("BUG: prepareBlock must return last item equal to the original last item;\ngot\n%X\nwant\n%X", lastItem, bsm.lastItem)
|
|
|
|
}
|
|
|
|
// Verify whether the bsm.ib.items are sorted only in tests, since this
|
|
|
|
// can be expensive check in prod for items with long common prefix.
|
|
|
|
if isInTest && !bsm.ib.isSorted() {
|
|
|
|
logger.Panicf("BUG: prepareBlock must return sorted items;\ngot\n%s", bsm.ib.debugItemsString())
|
|
|
|
}
|
2019-09-20 18:46:47 +02:00
|
|
|
}
|
2021-02-21 21:06:45 +01:00
|
|
|
ph.itemsCount += uint64(len(items))
|
2019-09-19 16:46:32 +02:00
|
|
|
if !bsm.phFirstItemCaught {
|
2021-02-21 21:06:45 +01:00
|
|
|
ph.firstItem = append(ph.firstItem[:0], items[0].String(data)...)
|
2019-09-19 16:46:32 +02:00
|
|
|
bsm.phFirstItemCaught = true
|
|
|
|
}
|
2021-02-21 21:06:45 +01:00
|
|
|
ph.lastItem = append(ph.lastItem[:0], items[len(items)-1].String(data)...)
|
2019-05-22 23:16:55 +02:00
|
|
|
bsw.WriteBlock(&bsm.ib)
|
|
|
|
bsm.ib.Reset()
|
|
|
|
ph.blocksCount++
|
|
|
|
}
|
|
|
|
|
|
|
|
type bsrHeap []*blockStreamReader
|
|
|
|
|
|
|
|
func (bh *bsrHeap) Len() int {
|
|
|
|
return len(*bh)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bh *bsrHeap) Swap(i, j int) {
|
|
|
|
x := *bh
|
|
|
|
x[i], x[j] = x[j], x[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bh *bsrHeap) Less(i, j int) bool {
|
|
|
|
x := *bh
|
|
|
|
return string(x[i].bh.firstItem) < string(x[j].bh.firstItem)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bh *bsrHeap) Pop() interface{} {
|
|
|
|
a := *bh
|
|
|
|
v := a[len(a)-1]
|
|
|
|
*bh = a[:len(a)-1]
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bh *bsrHeap) Push(x interface{}) {
|
|
|
|
v := x.(*blockStreamReader)
|
|
|
|
*bh = append(*bh, v)
|
|
|
|
}
|