2019-05-22 21:16:55 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2020-09-17 00:02:35 +00:00
|
|
|
"errors"
|
2019-05-22 21:16:55 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
2020-12-08 18:49:32 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/cgroup"
|
2019-05-22 21:16:55 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/encoding"
|
2020-05-14 19:01:51 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fasttime"
|
2019-05-22 21:16:55 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fs"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/memory"
|
2022-12-05 23:27:57 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/mergeset"
|
2019-12-04 19:35:51 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/syncwg"
|
2019-05-22 21:16:55 +00:00
|
|
|
)
|
|
|
|
|
2021-08-25 06:35:03 +00:00
|
|
|
// The maximum size of big part.
|
2019-05-22 21:16:55 +00:00
|
|
|
//
|
|
|
|
// This number limits the maximum time required for building big part.
|
|
|
|
// This time shouldn't exceed a few days.
|
2021-08-25 06:35:03 +00:00
|
|
|
const maxBigPartSize = 1e12
|
2019-05-22 21:16:55 +00:00
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
// The maximum number of inmemory parts in the partition.
|
2022-12-13 00:49:21 +00:00
|
|
|
//
|
|
|
|
// If the number of inmemory parts reaches this value, then assisted merge runs during data ingestion.
|
2022-12-28 22:32:18 +00:00
|
|
|
const maxInmemoryPartsPerPartition = 20
|
2019-05-22 21:16:55 +00:00
|
|
|
|
2022-12-13 00:49:21 +00:00
|
|
|
// The maximum number of small parts in the partition.
|
|
|
|
//
|
|
|
|
// If the number of small parts reaches this value, then assisted merge runs during data ingestion.
|
2022-12-28 22:32:18 +00:00
|
|
|
const maxSmallPartsPerPartition = 30
|
2022-12-13 00:49:21 +00:00
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
// Default number of parts to merge at once.
|
|
|
|
//
|
|
|
|
// This number has been obtained empirically - it gives the lowest possible overhead.
|
|
|
|
// See appendPartsToMerge tests for details.
|
|
|
|
const defaultPartsToMerge = 15
|
|
|
|
|
|
|
|
// The final number of parts to merge at once.
|
|
|
|
//
|
|
|
|
// It must be smaller than defaultPartsToMerge.
|
|
|
|
// Lower value improves select performance at the cost of increased
|
|
|
|
// write amplification.
|
2019-11-05 15:26:13 +00:00
|
|
|
const finalPartsToMerge = 3
|
2019-05-22 21:16:55 +00:00
|
|
|
|
2019-12-19 16:12:02 +00:00
|
|
|
// The number of shards for rawRow entries per partition.
|
|
|
|
//
|
|
|
|
// Higher number of shards reduces CPU contention and increases the max bandwidth on multi-core systems.
|
2022-10-17 15:17:42 +00:00
|
|
|
var rawRowsShardsPerPartition = (cgroup.AvailableCPUs() + 1) / 2
|
2019-12-19 16:12:02 +00:00
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
// The interval for flushing bufferred rows into parts, so they become visible to search.
|
|
|
|
const pendingRowsFlushInterval = time.Second
|
|
|
|
|
|
|
|
// The interval for guaranteed flush of recently ingested data from memory to on-disk parts,
|
|
|
|
// so they survive process crash.
|
|
|
|
var dataFlushInterval = 5 * time.Second
|
|
|
|
|
|
|
|
// SetDataFlushInterval sets the interval for guaranteed flush of recently ingested data from memory to disk.
|
|
|
|
//
|
|
|
|
// The data can be flushed from memory to disk more frequently if it doesn't fit the memory limit.
|
|
|
|
//
|
|
|
|
// This function must be called before initializing the storage.
|
|
|
|
func SetDataFlushInterval(d time.Duration) {
|
|
|
|
if d > pendingRowsFlushInterval {
|
|
|
|
dataFlushInterval = d
|
|
|
|
mergeset.SetDataFlushInterval(d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-11 07:52:14 +00:00
|
|
|
// getMaxRawRowsPerShard returns the maximum number of rows that haven't been converted into parts yet.
|
|
|
|
func getMaxRawRowsPerShard() int {
|
2019-05-22 21:16:55 +00:00
|
|
|
maxRawRowsPerPartitionOnce.Do(func() {
|
2021-06-11 07:49:02 +00:00
|
|
|
n := memory.Allowed() / rawRowsShardsPerPartition / 256 / int(unsafe.Sizeof(rawRow{}))
|
2019-05-22 21:16:55 +00:00
|
|
|
if n < 1e4 {
|
|
|
|
n = 1e4
|
|
|
|
}
|
|
|
|
if n > 500e3 {
|
|
|
|
n = 500e3
|
|
|
|
}
|
2020-01-04 17:49:30 +00:00
|
|
|
maxRawRowsPerPartition = n
|
2019-05-22 21:16:55 +00:00
|
|
|
})
|
|
|
|
return maxRawRowsPerPartition
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
maxRawRowsPerPartition int
|
|
|
|
maxRawRowsPerPartitionOnce sync.Once
|
|
|
|
)
|
|
|
|
|
|
|
|
// partition represents a partition.
|
|
|
|
type partition struct {
|
2019-10-17 15:22:56 +00:00
|
|
|
// Put atomic counters to the top of struct, so they are aligned to 8 bytes on 32-bit arch.
|
|
|
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/212
|
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
activeInmemoryMerges uint64
|
|
|
|
activeSmallMerges uint64
|
|
|
|
activeBigMerges uint64
|
|
|
|
|
|
|
|
inmemoryMergesCount uint64
|
|
|
|
smallMergesCount uint64
|
|
|
|
bigMergesCount uint64
|
2019-10-17 15:22:56 +00:00
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
inmemoryRowsMerged uint64
|
|
|
|
smallRowsMerged uint64
|
|
|
|
bigRowsMerged uint64
|
2019-10-17 15:22:56 +00:00
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
inmemoryRowsDeleted uint64
|
|
|
|
smallRowsDeleted uint64
|
|
|
|
bigRowsDeleted uint64
|
|
|
|
|
|
|
|
inmemoryAssistedMerges uint64
|
2022-12-13 00:49:21 +00:00
|
|
|
smallAssistedMerges uint64
|
2022-12-05 23:27:57 +00:00
|
|
|
|
|
|
|
mergeNeedFreeDiskSpace uint64
|
2020-09-29 19:51:43 +00:00
|
|
|
|
2019-10-17 15:22:56 +00:00
|
|
|
mergeIdx uint64
|
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
smallPartsPath string
|
|
|
|
bigPartsPath string
|
|
|
|
|
2022-10-23 13:08:54 +00:00
|
|
|
// The parent storage.
|
|
|
|
s *Storage
|
2019-05-22 21:16:55 +00:00
|
|
|
|
|
|
|
// Name is the name of the partition in the form YYYY_MM.
|
|
|
|
name string
|
|
|
|
|
|
|
|
// The time range for the partition. Usually this is a whole month.
|
|
|
|
tr TimeRange
|
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
// rawRows contains recently added rows that haven't been converted into parts yet.
|
|
|
|
// rawRows are periodically converted into inmemroyParts.
|
|
|
|
// rawRows aren't used in search for performance reasons.
|
|
|
|
rawRows rawRowsShards
|
|
|
|
|
|
|
|
// partsLock protects inmemoryParts, smallParts and bigParts.
|
2019-05-22 21:16:55 +00:00
|
|
|
partsLock sync.Mutex
|
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
// Contains inmemory parts with recently ingested data.
|
|
|
|
// It must be merged into either smallParts or bigParts to become visible to search.
|
|
|
|
inmemoryParts []*partWrapper
|
|
|
|
|
|
|
|
// Contains file-based parts with small number of items.
|
2019-05-22 21:16:55 +00:00
|
|
|
smallParts []*partWrapper
|
|
|
|
|
|
|
|
// Contains file-based parts with big number of items.
|
|
|
|
bigParts []*partWrapper
|
|
|
|
|
2023-01-18 09:09:03 +00:00
|
|
|
// This channel is used for signaling the background mergers that there are parts,
|
|
|
|
// which may need to be merged.
|
|
|
|
needMergeCh chan struct{}
|
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
snapshotLock sync.RWMutex
|
|
|
|
|
|
|
|
stopCh chan struct{}
|
|
|
|
|
2022-12-04 07:03:05 +00:00
|
|
|
wg sync.WaitGroup
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// partWrapper is a wrapper for the part.
|
|
|
|
type partWrapper struct {
|
2019-10-17 15:22:56 +00:00
|
|
|
// Put atomic counters to the top of struct, so they are aligned to 8 bytes on 32-bit arch.
|
|
|
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/212
|
|
|
|
|
|
|
|
// The number of references to the part.
|
|
|
|
refCount uint64
|
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
// The part itself.
|
|
|
|
p *part
|
|
|
|
|
|
|
|
// non-nil if the part is inmemoryPart.
|
|
|
|
mp *inmemoryPart
|
|
|
|
|
|
|
|
// Whether the part is in merge now.
|
|
|
|
isInMerge bool
|
2022-12-05 23:27:57 +00:00
|
|
|
|
|
|
|
// The deadline when in-memory part must be flushed to disk.
|
|
|
|
flushToDiskDeadline time.Time
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pw *partWrapper) incRef() {
|
|
|
|
atomic.AddUint64(&pw.refCount, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pw *partWrapper) decRef() {
|
|
|
|
n := atomic.AddUint64(&pw.refCount, ^uint64(0))
|
|
|
|
if int64(n) < 0 {
|
|
|
|
logger.Panicf("BUG: pw.refCount must be bigger than 0; got %d", int64(n))
|
|
|
|
}
|
|
|
|
if n > 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if pw.mp != nil {
|
|
|
|
putInmemoryPart(pw.mp)
|
|
|
|
pw.mp = nil
|
|
|
|
}
|
|
|
|
pw.p.MustClose()
|
|
|
|
pw.p = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// createPartition creates new partition for the given timestamp and the given paths
|
|
|
|
// to small and big partitions.
|
2022-10-23 22:30:50 +00:00
|
|
|
func createPartition(timestamp int64, smallPartitionsPath, bigPartitionsPath string, s *Storage) (*partition, error) {
|
2019-05-22 21:16:55 +00:00
|
|
|
name := timestampToPartitionName(timestamp)
|
|
|
|
smallPartsPath := filepath.Clean(smallPartitionsPath) + "/" + name
|
|
|
|
bigPartsPath := filepath.Clean(bigPartitionsPath) + "/" + name
|
|
|
|
logger.Infof("creating a partition %q with smallPartsPath=%q, bigPartsPath=%q", name, smallPartsPath, bigPartsPath)
|
|
|
|
|
|
|
|
if err := createPartitionDirs(smallPartsPath); err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("cannot create directories for small parts %q: %w", smallPartsPath, err)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
if err := createPartitionDirs(bigPartsPath); err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("cannot create directories for big parts %q: %w", bigPartsPath, err)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
2022-10-23 22:30:50 +00:00
|
|
|
pt := newPartition(name, smallPartsPath, bigPartsPath, s)
|
2019-05-22 21:16:55 +00:00
|
|
|
pt.tr.fromPartitionTimestamp(timestamp)
|
2022-12-04 08:01:04 +00:00
|
|
|
pt.startBackgroundWorkers()
|
2019-05-22 21:16:55 +00:00
|
|
|
|
|
|
|
logger.Infof("partition %q has been created", name)
|
|
|
|
|
|
|
|
return pt, nil
|
|
|
|
}
|
|
|
|
|
2022-12-04 08:01:04 +00:00
|
|
|
func (pt *partition) startBackgroundWorkers() {
|
|
|
|
pt.startMergeWorkers()
|
|
|
|
pt.startInmemoryPartsFlusher()
|
2022-12-05 23:27:57 +00:00
|
|
|
pt.startPendingRowsFlusher()
|
2022-12-04 08:01:04 +00:00
|
|
|
pt.startStalePartsRemover()
|
|
|
|
}
|
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
// Drop drops all the data on the storage for the given pt.
|
|
|
|
//
|
|
|
|
// The pt must be detached from table before calling pt.Drop.
|
|
|
|
func (pt *partition) Drop() {
|
|
|
|
logger.Infof("dropping partition %q at smallPartsPath=%q, bigPartsPath=%q", pt.name, pt.smallPartsPath, pt.bigPartsPath)
|
2020-12-25 09:45:47 +00:00
|
|
|
// Wait until all the pending transaction deletions are finished before removing partition directories.
|
|
|
|
pendingTxnDeletionsWG.Wait()
|
|
|
|
|
2022-09-13 10:37:34 +00:00
|
|
|
fs.MustRemoveDirAtomic(pt.smallPartsPath)
|
|
|
|
fs.MustRemoveDirAtomic(pt.bigPartsPath)
|
2019-05-22 21:16:55 +00:00
|
|
|
logger.Infof("partition %q has been dropped", pt.name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// openPartition opens the existing partition from the given paths.
|
2022-10-23 22:30:50 +00:00
|
|
|
func openPartition(smallPartsPath, bigPartsPath string, s *Storage) (*partition, error) {
|
2019-05-22 21:16:55 +00:00
|
|
|
smallPartsPath = filepath.Clean(smallPartsPath)
|
|
|
|
bigPartsPath = filepath.Clean(bigPartsPath)
|
|
|
|
|
|
|
|
n := strings.LastIndexByte(smallPartsPath, '/')
|
|
|
|
if n < 0 {
|
|
|
|
return nil, fmt.Errorf("cannot find partition name from smallPartsPath %q; must be in the form /path/to/smallparts/YYYY_MM", smallPartsPath)
|
|
|
|
}
|
|
|
|
name := smallPartsPath[n+1:]
|
|
|
|
|
|
|
|
if !strings.HasSuffix(bigPartsPath, "/"+name) {
|
|
|
|
return nil, fmt.Errorf("patititon name in bigPartsPath %q doesn't match smallPartsPath %q; want %q", bigPartsPath, smallPartsPath, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
smallParts, err := openParts(smallPartsPath, bigPartsPath, smallPartsPath)
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("cannot open small parts from %q: %w", smallPartsPath, err)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
bigParts, err := openParts(smallPartsPath, bigPartsPath, bigPartsPath)
|
|
|
|
if err != nil {
|
|
|
|
mustCloseParts(smallParts)
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("cannot open big parts from %q: %w", bigPartsPath, err)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
2022-10-23 22:30:50 +00:00
|
|
|
pt := newPartition(name, smallPartsPath, bigPartsPath, s)
|
2019-05-22 21:16:55 +00:00
|
|
|
pt.smallParts = smallParts
|
|
|
|
pt.bigParts = bigParts
|
|
|
|
if err := pt.tr.fromPartitionName(name); err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("cannot obtain partition time range from smallPartsPath %q: %w", smallPartsPath, err)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2022-12-04 08:01:04 +00:00
|
|
|
pt.startBackgroundWorkers()
|
2019-05-22 21:16:55 +00:00
|
|
|
|
2023-01-18 09:09:03 +00:00
|
|
|
// Wake up a single background merger, so it could start merging parts if needed.
|
|
|
|
pt.notifyBackgroundMergers()
|
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
return pt, nil
|
|
|
|
}
|
|
|
|
|
2022-10-23 22:30:50 +00:00
|
|
|
func newPartition(name, smallPartsPath, bigPartsPath string, s *Storage) *partition {
|
2019-12-19 16:12:02 +00:00
|
|
|
p := &partition{
|
2019-05-22 21:16:55 +00:00
|
|
|
name: name,
|
|
|
|
smallPartsPath: smallPartsPath,
|
|
|
|
bigPartsPath: bigPartsPath,
|
|
|
|
|
2022-10-23 22:30:50 +00:00
|
|
|
s: s,
|
2019-05-22 21:16:55 +00:00
|
|
|
|
2023-01-18 09:09:03 +00:00
|
|
|
mergeIdx: uint64(time.Now().UnixNano()),
|
|
|
|
needMergeCh: make(chan struct{}, cgroup.AvailableCPUs()),
|
|
|
|
|
|
|
|
stopCh: make(chan struct{}),
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2019-12-19 16:12:02 +00:00
|
|
|
p.rawRows.init()
|
|
|
|
return p
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// partitionMetrics contains essential metrics for the partition.
|
|
|
|
type partitionMetrics struct {
|
|
|
|
PendingRows uint64
|
|
|
|
|
2022-01-20 16:34:59 +00:00
|
|
|
IndexBlocksCacheSize uint64
|
|
|
|
IndexBlocksCacheSizeBytes uint64
|
|
|
|
IndexBlocksCacheSizeMaxBytes uint64
|
|
|
|
IndexBlocksCacheRequests uint64
|
|
|
|
IndexBlocksCacheMisses uint64
|
2019-05-22 21:16:55 +00:00
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
InmemorySizeBytes uint64
|
|
|
|
SmallSizeBytes uint64
|
|
|
|
BigSizeBytes uint64
|
|
|
|
|
|
|
|
InmemoryRowsCount uint64
|
|
|
|
SmallRowsCount uint64
|
|
|
|
BigRowsCount uint64
|
2019-07-04 16:09:40 +00:00
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
InmemoryBlocksCount uint64
|
|
|
|
SmallBlocksCount uint64
|
|
|
|
BigBlocksCount uint64
|
2019-05-22 21:16:55 +00:00
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
InmemoryPartsCount uint64
|
|
|
|
SmallPartsCount uint64
|
|
|
|
BigPartsCount uint64
|
2019-05-22 21:16:55 +00:00
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
ActiveInmemoryMerges uint64
|
|
|
|
ActiveSmallMerges uint64
|
|
|
|
ActiveBigMerges uint64
|
2019-05-22 21:16:55 +00:00
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
InmemoryMergesCount uint64
|
|
|
|
SmallMergesCount uint64
|
|
|
|
BigMergesCount uint64
|
2019-05-22 21:16:55 +00:00
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
InmemoryRowsMerged uint64
|
|
|
|
SmallRowsMerged uint64
|
|
|
|
BigRowsMerged uint64
|
2019-05-22 21:16:55 +00:00
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
InmemoryRowsDeleted uint64
|
|
|
|
SmallRowsDeleted uint64
|
|
|
|
BigRowsDeleted uint64
|
2019-05-22 21:16:55 +00:00
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
InmemoryPartsRefCount uint64
|
|
|
|
SmallPartsRefCount uint64
|
|
|
|
BigPartsRefCount uint64
|
2019-05-22 21:16:55 +00:00
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
InmemoryAssistedMerges uint64
|
2022-12-13 00:49:21 +00:00
|
|
|
SmallAssistedMerges uint64
|
2019-05-22 21:16:55 +00:00
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
MergeNeedFreeDiskSpace uint64
|
|
|
|
}
|
2020-09-29 18:47:40 +00:00
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
// TotalRowsCount returns total number of rows in tm.
|
|
|
|
func (pm *partitionMetrics) TotalRowsCount() uint64 {
|
|
|
|
return pm.PendingRows + pm.InmemoryRowsCount + pm.SmallRowsCount + pm.BigRowsCount
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateMetrics updates m with metrics from pt.
|
|
|
|
func (pt *partition) UpdateMetrics(m *partitionMetrics) {
|
2022-12-05 23:27:57 +00:00
|
|
|
m.PendingRows += uint64(pt.rawRows.Len())
|
2019-05-22 21:16:55 +00:00
|
|
|
|
|
|
|
pt.partsLock.Lock()
|
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
for _, pw := range pt.inmemoryParts {
|
2019-05-22 21:16:55 +00:00
|
|
|
p := pw.p
|
2022-12-05 23:27:57 +00:00
|
|
|
m.InmemoryRowsCount += p.ph.RowsCount
|
|
|
|
m.InmemoryBlocksCount += p.ph.BlocksCount
|
|
|
|
m.InmemorySizeBytes += p.size
|
|
|
|
m.InmemoryPartsRefCount += atomic.LoadUint64(&pw.refCount)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
for _, pw := range pt.smallParts {
|
|
|
|
p := pw.p
|
|
|
|
m.SmallRowsCount += p.ph.RowsCount
|
|
|
|
m.SmallBlocksCount += p.ph.BlocksCount
|
2019-07-04 16:09:40 +00:00
|
|
|
m.SmallSizeBytes += p.size
|
2019-05-22 21:16:55 +00:00
|
|
|
m.SmallPartsRefCount += atomic.LoadUint64(&pw.refCount)
|
|
|
|
}
|
2022-12-05 23:27:57 +00:00
|
|
|
for _, pw := range pt.bigParts {
|
|
|
|
p := pw.p
|
|
|
|
m.BigRowsCount += p.ph.RowsCount
|
|
|
|
m.BigBlocksCount += p.ph.BlocksCount
|
|
|
|
m.BigSizeBytes += p.size
|
|
|
|
m.BigPartsRefCount += atomic.LoadUint64(&pw.refCount)
|
|
|
|
}
|
2019-05-22 21:16:55 +00:00
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
m.InmemoryPartsCount += uint64(len(pt.inmemoryParts))
|
2019-05-22 21:16:55 +00:00
|
|
|
m.SmallPartsCount += uint64(len(pt.smallParts))
|
2022-12-05 23:27:57 +00:00
|
|
|
m.BigPartsCount += uint64(len(pt.bigParts))
|
2019-05-22 21:16:55 +00:00
|
|
|
|
|
|
|
pt.partsLock.Unlock()
|
|
|
|
|
2022-01-20 16:34:59 +00:00
|
|
|
m.IndexBlocksCacheSize = uint64(ibCache.Len())
|
|
|
|
m.IndexBlocksCacheSizeBytes = uint64(ibCache.SizeBytes())
|
|
|
|
m.IndexBlocksCacheSizeMaxBytes = uint64(ibCache.SizeMaxBytes())
|
|
|
|
m.IndexBlocksCacheRequests = ibCache.Requests()
|
|
|
|
m.IndexBlocksCacheMisses = ibCache.Misses()
|
2019-05-22 21:16:55 +00:00
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
m.ActiveInmemoryMerges += atomic.LoadUint64(&pt.activeInmemoryMerges)
|
2019-05-22 21:16:55 +00:00
|
|
|
m.ActiveSmallMerges += atomic.LoadUint64(&pt.activeSmallMerges)
|
2022-12-05 23:27:57 +00:00
|
|
|
m.ActiveBigMerges += atomic.LoadUint64(&pt.activeBigMerges)
|
2019-05-22 21:16:55 +00:00
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
m.InmemoryMergesCount += atomic.LoadUint64(&pt.inmemoryMergesCount)
|
2019-05-22 21:16:55 +00:00
|
|
|
m.SmallMergesCount += atomic.LoadUint64(&pt.smallMergesCount)
|
2022-12-05 23:27:57 +00:00
|
|
|
m.BigMergesCount += atomic.LoadUint64(&pt.bigMergesCount)
|
2019-05-22 21:16:55 +00:00
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
m.InmemoryRowsMerged += atomic.LoadUint64(&pt.inmemoryRowsMerged)
|
2019-05-22 21:16:55 +00:00
|
|
|
m.SmallRowsMerged += atomic.LoadUint64(&pt.smallRowsMerged)
|
2022-12-05 23:27:57 +00:00
|
|
|
m.BigRowsMerged += atomic.LoadUint64(&pt.bigRowsMerged)
|
2019-05-22 21:16:55 +00:00
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
m.InmemoryRowsDeleted += atomic.LoadUint64(&pt.inmemoryRowsDeleted)
|
2019-05-22 21:16:55 +00:00
|
|
|
m.SmallRowsDeleted += atomic.LoadUint64(&pt.smallRowsDeleted)
|
2022-12-05 23:27:57 +00:00
|
|
|
m.BigRowsDeleted += atomic.LoadUint64(&pt.bigRowsDeleted)
|
2019-05-22 21:16:55 +00:00
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
m.InmemoryAssistedMerges += atomic.LoadUint64(&pt.inmemoryAssistedMerges)
|
2022-12-13 00:49:21 +00:00
|
|
|
m.SmallAssistedMerges += atomic.LoadUint64(&pt.smallAssistedMerges)
|
2020-09-29 18:47:40 +00:00
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
m.MergeNeedFreeDiskSpace += atomic.LoadUint64(&pt.mergeNeedFreeDiskSpace)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddRows adds the given rows to the partition pt.
|
|
|
|
//
|
|
|
|
// All the rows must fit the partition by timestamp range
|
|
|
|
// and must have valid PrecisionBits.
|
|
|
|
func (pt *partition) AddRows(rows []rawRow) {
|
|
|
|
if len(rows) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate all the rows.
|
|
|
|
for i := range rows {
|
|
|
|
r := &rows[i]
|
|
|
|
if !pt.HasTimestamp(r.Timestamp) {
|
|
|
|
logger.Panicf("BUG: row %+v has Timestamp outside partition %q range %+v", r, pt.smallPartsPath, &pt.tr)
|
|
|
|
}
|
|
|
|
if err := encoding.CheckPrecisionBits(r.PrecisionBits); err != nil {
|
|
|
|
logger.Panicf("BUG: row %+v has invalid PrecisionBits: %s", r, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-19 16:12:02 +00:00
|
|
|
pt.rawRows.addRows(pt, rows)
|
|
|
|
}
|
|
|
|
|
|
|
|
type rawRowsShards struct {
|
2021-04-27 13:41:22 +00:00
|
|
|
shardIdx uint32
|
2019-12-19 16:12:02 +00:00
|
|
|
|
|
|
|
// Shards reduce lock contention when adding rows on multi-CPU systems.
|
|
|
|
shards []rawRowsShard
|
|
|
|
}
|
|
|
|
|
2021-04-27 12:36:31 +00:00
|
|
|
func (rrss *rawRowsShards) init() {
|
|
|
|
rrss.shards = make([]rawRowsShard, rawRowsShardsPerPartition)
|
2019-12-19 16:12:02 +00:00
|
|
|
}
|
|
|
|
|
2021-04-27 12:36:31 +00:00
|
|
|
func (rrss *rawRowsShards) addRows(pt *partition, rows []rawRow) {
|
|
|
|
shards := rrss.shards
|
2022-12-05 23:27:57 +00:00
|
|
|
shardsLen := uint32(len(shards))
|
|
|
|
for len(rows) > 0 {
|
|
|
|
n := atomic.AddUint32(&rrss.shardIdx, 1)
|
|
|
|
idx := n % shardsLen
|
|
|
|
rows = shards[idx].addRows(pt, rows)
|
|
|
|
}
|
2019-12-19 16:12:02 +00:00
|
|
|
}
|
|
|
|
|
2021-04-27 12:36:31 +00:00
|
|
|
func (rrss *rawRowsShards) Len() int {
|
2019-12-19 16:12:02 +00:00
|
|
|
n := 0
|
2021-04-27 12:36:31 +00:00
|
|
|
for i := range rrss.shards[:] {
|
|
|
|
n += rrss.shards[i].Len()
|
2019-12-19 16:12:02 +00:00
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2022-10-20 13:17:09 +00:00
|
|
|
type rawRowsShardNopad struct {
|
|
|
|
// Put lastFlushTime to the top in order to avoid unaligned memory access on 32-bit architectures
|
2020-05-14 19:01:51 +00:00
|
|
|
lastFlushTime uint64
|
2022-10-20 13:17:09 +00:00
|
|
|
|
|
|
|
mu sync.Mutex
|
|
|
|
rows []rawRow
|
|
|
|
}
|
|
|
|
|
|
|
|
type rawRowsShard struct {
|
|
|
|
rawRowsShardNopad
|
|
|
|
|
|
|
|
// The padding prevents false sharing on widespread platforms with
|
|
|
|
// 128 mod (cache line size) = 0 .
|
|
|
|
_ [128 - unsafe.Sizeof(rawRowsShardNopad{})%128]byte
|
2019-12-19 16:12:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rrs *rawRowsShard) Len() int {
|
2021-04-27 12:36:31 +00:00
|
|
|
rrs.mu.Lock()
|
2019-12-19 16:12:02 +00:00
|
|
|
n := len(rrs.rows)
|
2021-04-27 12:36:31 +00:00
|
|
|
rrs.mu.Unlock()
|
2019-12-19 16:12:02 +00:00
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
func (rrs *rawRowsShard) addRows(pt *partition, rows []rawRow) []rawRow {
|
|
|
|
var rrb *rawRowsBlock
|
2019-12-19 16:12:02 +00:00
|
|
|
|
2021-04-27 12:36:31 +00:00
|
|
|
rrs.mu.Lock()
|
2019-12-19 16:12:02 +00:00
|
|
|
if cap(rrs.rows) == 0 {
|
2023-01-18 08:01:03 +00:00
|
|
|
rrs.rows = newRawRows()
|
2019-12-19 16:12:02 +00:00
|
|
|
}
|
2022-10-21 11:39:27 +00:00
|
|
|
n := copy(rrs.rows[len(rrs.rows):cap(rrs.rows)], rows)
|
|
|
|
rrs.rows = rrs.rows[:len(rrs.rows)+n]
|
|
|
|
rows = rows[n:]
|
|
|
|
if len(rows) > 0 {
|
2022-12-05 23:27:57 +00:00
|
|
|
rrb = getRawRowsBlock()
|
|
|
|
rrb.rows, rrs.rows = rrs.rows, rrb.rows
|
|
|
|
n = copy(rrs.rows[:cap(rrs.rows)], rows)
|
|
|
|
rrs.rows = rrs.rows[:n]
|
|
|
|
rows = rows[n:]
|
2022-10-17 15:01:26 +00:00
|
|
|
atomic.StoreUint64(&rrs.lastFlushTime, fasttime.UnixTimestamp())
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2021-04-27 12:36:31 +00:00
|
|
|
rrs.mu.Unlock()
|
2019-05-22 21:16:55 +00:00
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
if rrb != nil {
|
|
|
|
pt.flushRowsToParts(rrb.rows)
|
|
|
|
putRawRowsBlock(rrb)
|
2023-01-18 08:20:56 +00:00
|
|
|
|
|
|
|
// Run assisted merges if needed.
|
|
|
|
flushConcurrencyCh <- struct{}{}
|
|
|
|
pt.assistedMergeForInmemoryParts()
|
|
|
|
pt.assistedMergeForSmallParts()
|
|
|
|
// There is no need in assisted merges for big parts,
|
|
|
|
// since the bottleneck is possible only at inmemory and small parts.
|
|
|
|
<-flushConcurrencyCh
|
2022-12-05 23:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rows
|
|
|
|
}
|
|
|
|
|
|
|
|
type rawRowsBlock struct {
|
|
|
|
rows []rawRow
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
2023-01-18 08:01:03 +00:00
|
|
|
func newRawRows() []rawRow {
|
2022-10-21 11:46:06 +00:00
|
|
|
n := getMaxRawRowsPerShard()
|
|
|
|
return make([]rawRow, 0, n)
|
|
|
|
}
|
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
func getRawRowsBlock() *rawRowsBlock {
|
|
|
|
v := rawRowsBlockPool.Get()
|
|
|
|
if v == nil {
|
|
|
|
return &rawRowsBlock{
|
2023-01-18 08:01:03 +00:00
|
|
|
rows: newRawRows(),
|
2022-12-05 23:27:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return v.(*rawRowsBlock)
|
|
|
|
}
|
|
|
|
|
|
|
|
func putRawRowsBlock(rrb *rawRowsBlock) {
|
|
|
|
rrb.rows = rrb.rows[:0]
|
|
|
|
rawRowsBlockPool.Put(rrb)
|
|
|
|
}
|
|
|
|
|
|
|
|
var rawRowsBlockPool sync.Pool
|
|
|
|
|
2021-06-17 10:42:32 +00:00
|
|
|
func (pt *partition) flushRowsToParts(rows []rawRow) {
|
2022-12-05 23:27:57 +00:00
|
|
|
if len(rows) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2021-06-17 10:42:32 +00:00
|
|
|
maxRows := getMaxRawRowsPerShard()
|
2022-12-05 23:27:57 +00:00
|
|
|
var pwsLock sync.Mutex
|
|
|
|
pws := make([]*partWrapper, 0, (len(rows)+maxRows-1)/maxRows)
|
2022-04-06 10:34:00 +00:00
|
|
|
wg := getWaitGroup()
|
2021-06-17 10:42:32 +00:00
|
|
|
for len(rows) > 0 {
|
|
|
|
n := maxRows
|
|
|
|
if n > len(rows) {
|
|
|
|
n = len(rows)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2021-06-17 10:42:32 +00:00
|
|
|
wg.Add(1)
|
2022-12-05 23:27:57 +00:00
|
|
|
flushConcurrencyCh <- struct{}{}
|
|
|
|
go func(rowsChunk []rawRow) {
|
|
|
|
defer func() {
|
|
|
|
<-flushConcurrencyCh
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
pw := pt.createInmemoryPart(rowsChunk)
|
|
|
|
if pw == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pwsLock.Lock()
|
|
|
|
pws = append(pws, pw)
|
|
|
|
pwsLock.Unlock()
|
2021-06-17 10:42:32 +00:00
|
|
|
}(rows[:n])
|
|
|
|
rows = rows[n:]
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2021-06-17 10:42:32 +00:00
|
|
|
wg.Wait()
|
2022-04-06 10:34:00 +00:00
|
|
|
putWaitGroup(wg)
|
2022-12-05 23:27:57 +00:00
|
|
|
|
|
|
|
pt.partsLock.Lock()
|
|
|
|
pt.inmemoryParts = append(pt.inmemoryParts, pws...)
|
2023-01-18 09:09:03 +00:00
|
|
|
for range pws {
|
|
|
|
if !pt.notifyBackgroundMergers() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2022-12-05 23:27:57 +00:00
|
|
|
pt.partsLock.Unlock()
|
|
|
|
}
|
|
|
|
|
2023-01-18 09:09:03 +00:00
|
|
|
func (pt *partition) notifyBackgroundMergers() bool {
|
|
|
|
select {
|
|
|
|
case pt.needMergeCh <- struct{}{}:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-11 20:06:18 +00:00
|
|
|
var flushConcurrencyLimit = func() int {
|
|
|
|
n := cgroup.AvailableCPUs()
|
|
|
|
if n < 3 {
|
|
|
|
// Allow at least 3 concurrent flushers on systems with a single CPU core
|
|
|
|
// in order to guarantee that in-memory data flushes and background merges can be continued
|
|
|
|
// when a single flusher is busy with the long merge of big parts,
|
|
|
|
// while another flusher is busy with the long merge of small parts.
|
|
|
|
n = 3
|
|
|
|
}
|
|
|
|
return n
|
|
|
|
}()
|
|
|
|
|
|
|
|
var flushConcurrencyCh = make(chan struct{}, flushConcurrencyLimit)
|
2022-12-05 23:27:57 +00:00
|
|
|
|
2022-12-28 22:32:18 +00:00
|
|
|
func needAssistedMerge(pws []*partWrapper, maxParts int) bool {
|
|
|
|
if len(pws) < maxParts {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return getNotInMergePartsCount(pws) >= defaultPartsToMerge
|
|
|
|
}
|
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
func (pt *partition) assistedMergeForInmemoryParts() {
|
|
|
|
for {
|
|
|
|
pt.partsLock.Lock()
|
2022-12-28 22:32:18 +00:00
|
|
|
needMerge := needAssistedMerge(pt.inmemoryParts, maxInmemoryPartsPerPartition)
|
2022-12-05 23:27:57 +00:00
|
|
|
pt.partsLock.Unlock()
|
2022-12-28 22:32:18 +00:00
|
|
|
if !needMerge {
|
2022-12-05 23:27:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-13 00:49:21 +00:00
|
|
|
atomic.AddUint64(&pt.inmemoryAssistedMerges, 1)
|
2022-12-05 23:27:57 +00:00
|
|
|
err := pt.mergeInmemoryParts()
|
|
|
|
if err == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if errors.Is(err, errNothingToMerge) || errors.Is(err, errForciblyStopped) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
logger.Panicf("FATAL: cannot merge inmemory parts: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-13 00:49:21 +00:00
|
|
|
func (pt *partition) assistedMergeForSmallParts() {
|
|
|
|
for {
|
|
|
|
pt.partsLock.Lock()
|
2022-12-28 22:32:18 +00:00
|
|
|
needMerge := needAssistedMerge(pt.smallParts, maxSmallPartsPerPartition)
|
2022-12-13 00:49:21 +00:00
|
|
|
pt.partsLock.Unlock()
|
2022-12-28 22:32:18 +00:00
|
|
|
if !needMerge {
|
2022-12-13 00:49:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
atomic.AddUint64(&pt.smallAssistedMerges, 1)
|
|
|
|
err := pt.mergeExistingParts(false)
|
|
|
|
if err == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if errors.Is(err, errNothingToMerge) || errors.Is(err, errForciblyStopped) || errors.Is(err, errReadOnlyMode) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
logger.Panicf("FATAL: cannot merge small parts: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
func getNotInMergePartsCount(pws []*partWrapper) int {
|
|
|
|
n := 0
|
|
|
|
for _, pw := range pws {
|
|
|
|
if !pw.isInMerge {
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
2022-04-06 10:34:00 +00:00
|
|
|
func getWaitGroup() *sync.WaitGroup {
|
|
|
|
v := wgPool.Get()
|
|
|
|
if v == nil {
|
|
|
|
return &sync.WaitGroup{}
|
|
|
|
}
|
|
|
|
return v.(*sync.WaitGroup)
|
|
|
|
}
|
|
|
|
|
|
|
|
func putWaitGroup(wg *sync.WaitGroup) {
|
|
|
|
wgPool.Put(wg)
|
|
|
|
}
|
|
|
|
|
|
|
|
var wgPool sync.Pool
|
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
func (pt *partition) createInmemoryPart(rows []rawRow) *partWrapper {
|
2019-05-22 21:16:55 +00:00
|
|
|
if len(rows) == 0 {
|
2022-12-05 23:27:57 +00:00
|
|
|
return nil
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
mp := getInmemoryPart()
|
|
|
|
mp.InitFromRows(rows)
|
|
|
|
|
|
|
|
// Make sure the part may be added.
|
|
|
|
if mp.ph.MinTimestamp > mp.ph.MaxTimestamp {
|
|
|
|
logger.Panicf("BUG: the part %q cannot be added to partition %q because its MinTimestamp exceeds MaxTimestamp; %d vs %d",
|
|
|
|
&mp.ph, pt.smallPartsPath, mp.ph.MinTimestamp, mp.ph.MaxTimestamp)
|
|
|
|
}
|
|
|
|
if mp.ph.MinTimestamp < pt.tr.MinTimestamp {
|
|
|
|
logger.Panicf("BUG: the part %q cannot be added to partition %q because of too small MinTimestamp; got %d; want at least %d",
|
|
|
|
&mp.ph, pt.smallPartsPath, mp.ph.MinTimestamp, pt.tr.MinTimestamp)
|
|
|
|
}
|
|
|
|
if mp.ph.MaxTimestamp > pt.tr.MaxTimestamp {
|
|
|
|
logger.Panicf("BUG: the part %q cannot be added to partition %q because of too big MaxTimestamp; got %d; want at least %d",
|
|
|
|
&mp.ph, pt.smallPartsPath, mp.ph.MaxTimestamp, pt.tr.MaxTimestamp)
|
|
|
|
}
|
2022-12-05 23:27:57 +00:00
|
|
|
flushToDiskDeadline := time.Now().Add(dataFlushInterval)
|
|
|
|
return newPartWrapperFromInmemoryPart(mp, flushToDiskDeadline)
|
|
|
|
}
|
2019-05-22 21:16:55 +00:00
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
func newPartWrapperFromInmemoryPart(mp *inmemoryPart, flushToDiskDeadline time.Time) *partWrapper {
|
2019-05-22 21:16:55 +00:00
|
|
|
p, err := mp.NewPart()
|
|
|
|
if err != nil {
|
|
|
|
logger.Panicf("BUG: cannot create part from %q: %s", &mp.ph, err)
|
|
|
|
}
|
|
|
|
pw := &partWrapper{
|
2022-12-05 23:27:57 +00:00
|
|
|
p: p,
|
|
|
|
mp: mp,
|
|
|
|
refCount: 1,
|
|
|
|
flushToDiskDeadline: flushToDiskDeadline,
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2022-12-05 23:27:57 +00:00
|
|
|
return pw
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HasTimestamp returns true if the pt contains the given timestamp.
|
|
|
|
func (pt *partition) HasTimestamp(timestamp int64) bool {
|
|
|
|
return timestamp >= pt.tr.MinTimestamp && timestamp <= pt.tr.MaxTimestamp
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetParts appends parts snapshot to dst and returns it.
|
|
|
|
//
|
|
|
|
// The appended parts must be released with PutParts.
|
2023-02-01 17:54:21 +00:00
|
|
|
func (pt *partition) GetParts(dst []*partWrapper, addInMemory bool) []*partWrapper {
|
2019-05-22 21:16:55 +00:00
|
|
|
pt.partsLock.Lock()
|
2023-02-01 17:54:21 +00:00
|
|
|
if addInMemory {
|
|
|
|
for _, pw := range pt.inmemoryParts {
|
|
|
|
pw.incRef()
|
|
|
|
}
|
|
|
|
dst = append(dst, pt.inmemoryParts...)
|
2022-12-05 23:27:57 +00:00
|
|
|
}
|
2023-02-01 17:54:21 +00:00
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
for _, pw := range pt.smallParts {
|
|
|
|
pw.incRef()
|
|
|
|
}
|
|
|
|
dst = append(dst, pt.smallParts...)
|
|
|
|
for _, pw := range pt.bigParts {
|
|
|
|
pw.incRef()
|
|
|
|
}
|
|
|
|
dst = append(dst, pt.bigParts...)
|
|
|
|
pt.partsLock.Unlock()
|
|
|
|
|
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutParts releases the given pws obtained via GetParts.
|
|
|
|
func (pt *partition) PutParts(pws []*partWrapper) {
|
|
|
|
for _, pw := range pws {
|
|
|
|
pw.decRef()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MustClose closes the pt, so the app may safely exit.
|
|
|
|
//
|
|
|
|
// The pt must be detached from table before calling pt.MustClose.
|
|
|
|
func (pt *partition) MustClose() {
|
|
|
|
close(pt.stopCh)
|
|
|
|
|
2020-12-25 09:45:47 +00:00
|
|
|
// Wait until all the pending transaction deletions are finished.
|
|
|
|
pendingTxnDeletionsWG.Wait()
|
|
|
|
|
2022-12-04 07:03:05 +00:00
|
|
|
logger.Infof("waiting for service workers to stop on %q...", pt.smallPartsPath)
|
2019-05-22 21:16:55 +00:00
|
|
|
startTime := time.Now()
|
2022-12-04 07:03:05 +00:00
|
|
|
pt.wg.Wait()
|
|
|
|
logger.Infof("service workers stopped in %.3f seconds on %q", time.Since(startTime).Seconds(), pt.smallPartsPath)
|
2019-05-22 21:16:55 +00:00
|
|
|
|
|
|
|
logger.Infof("flushing inmemory parts to files on %q...", pt.smallPartsPath)
|
|
|
|
startTime = time.Now()
|
2022-12-05 23:27:57 +00:00
|
|
|
pt.flushInmemoryRows()
|
2022-12-06 05:30:48 +00:00
|
|
|
logger.Infof("inmemory parts have been flushed to files in %.3f seconds on %q", time.Since(startTime).Seconds(), pt.smallPartsPath)
|
2019-05-22 21:16:55 +00:00
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
// Remove references from inmemoryParts, smallParts and bigParts, so they may be eventually closed
|
2019-05-25 18:51:11 +00:00
|
|
|
// after all the searches are done.
|
2019-05-22 21:16:55 +00:00
|
|
|
pt.partsLock.Lock()
|
2022-12-05 23:27:57 +00:00
|
|
|
inmemoryParts := pt.inmemoryParts
|
2019-05-22 21:16:55 +00:00
|
|
|
smallParts := pt.smallParts
|
2022-12-05 23:27:57 +00:00
|
|
|
bigParts := pt.bigParts
|
|
|
|
pt.inmemoryParts = nil
|
2019-05-22 21:16:55 +00:00
|
|
|
pt.smallParts = nil
|
2022-12-05 23:27:57 +00:00
|
|
|
pt.bigParts = nil
|
2019-05-22 21:16:55 +00:00
|
|
|
pt.partsLock.Unlock()
|
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
for _, pw := range inmemoryParts {
|
|
|
|
pw.decRef()
|
|
|
|
}
|
2019-05-22 21:16:55 +00:00
|
|
|
for _, pw := range smallParts {
|
|
|
|
pw.decRef()
|
|
|
|
}
|
|
|
|
for _, pw := range bigParts {
|
|
|
|
pw.decRef()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
func (pt *partition) startInmemoryPartsFlusher() {
|
|
|
|
pt.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
pt.inmemoryPartsFlusher()
|
|
|
|
pt.wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pt *partition) startPendingRowsFlusher() {
|
2022-12-04 07:03:05 +00:00
|
|
|
pt.wg.Add(1)
|
2019-05-22 21:16:55 +00:00
|
|
|
go func() {
|
2022-12-05 23:27:57 +00:00
|
|
|
pt.pendingRowsFlusher()
|
2022-12-04 07:03:05 +00:00
|
|
|
pt.wg.Done()
|
2019-05-22 21:16:55 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
func (pt *partition) inmemoryPartsFlusher() {
|
|
|
|
ticker := time.NewTicker(dataFlushInterval)
|
|
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-pt.stopCh:
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
pt.flushInmemoryParts(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pt *partition) pendingRowsFlusher() {
|
|
|
|
ticker := time.NewTicker(pendingRowsFlushInterval)
|
2020-02-13 10:55:58 +00:00
|
|
|
defer ticker.Stop()
|
2022-12-05 23:27:57 +00:00
|
|
|
var rows []rawRow
|
2019-05-22 21:16:55 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-pt.stopCh:
|
|
|
|
return
|
2020-02-13 10:55:58 +00:00
|
|
|
case <-ticker.C:
|
2022-12-05 23:27:57 +00:00
|
|
|
rows = pt.flushPendingRows(rows[:0], false)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2019-12-19 16:12:02 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-22 21:16:55 +00:00
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
func (pt *partition) flushPendingRows(dst []rawRow, isFinal bool) []rawRow {
|
|
|
|
return pt.rawRows.flush(pt, dst, isFinal)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pt *partition) flushInmemoryRows() {
|
|
|
|
pt.rawRows.flush(pt, nil, true)
|
|
|
|
pt.flushInmemoryParts(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pt *partition) flushInmemoryParts(isFinal bool) {
|
2023-03-19 07:10:24 +00:00
|
|
|
currentTime := time.Now()
|
|
|
|
var pws []*partWrapper
|
2022-12-05 23:27:57 +00:00
|
|
|
|
2023-03-19 07:10:24 +00:00
|
|
|
pt.partsLock.Lock()
|
|
|
|
for _, pw := range pt.inmemoryParts {
|
|
|
|
if !pw.isInMerge && (isFinal || pw.flushToDiskDeadline.Before(currentTime)) {
|
|
|
|
pw.isInMerge = true
|
|
|
|
pws = append(pws, pw)
|
2022-12-05 23:27:57 +00:00
|
|
|
}
|
2023-03-19 07:10:24 +00:00
|
|
|
}
|
|
|
|
pt.partsLock.Unlock()
|
2022-12-05 23:27:57 +00:00
|
|
|
|
2023-03-19 07:10:24 +00:00
|
|
|
if err := pt.mergePartsOptimal(pws, nil); err != nil {
|
|
|
|
logger.Panicf("FATAL: cannot merge in-memory parts: %s", err)
|
2022-12-05 23:27:57 +00:00
|
|
|
}
|
2019-12-19 16:12:02 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
func (rrss *rawRowsShards) flush(pt *partition, dst []rawRow, isFinal bool) []rawRow {
|
2021-04-27 12:36:31 +00:00
|
|
|
for i := range rrss.shards {
|
2022-12-05 23:27:57 +00:00
|
|
|
dst = rrss.shards[i].appendRawRowsToFlush(dst, pt, isFinal)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2022-12-05 23:27:57 +00:00
|
|
|
pt.flushRowsToParts(dst)
|
|
|
|
return dst
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
2021-06-17 10:42:32 +00:00
|
|
|
func (rrs *rawRowsShard) appendRawRowsToFlush(dst []rawRow, pt *partition, isFinal bool) []rawRow {
|
2020-05-14 19:01:51 +00:00
|
|
|
currentTime := fasttime.UnixTimestamp()
|
2022-12-05 23:27:57 +00:00
|
|
|
flushSeconds := int64(pendingRowsFlushInterval.Seconds())
|
2020-05-14 19:01:51 +00:00
|
|
|
if flushSeconds <= 0 {
|
|
|
|
flushSeconds = 1
|
|
|
|
}
|
2022-10-17 15:01:26 +00:00
|
|
|
lastFlushTime := atomic.LoadUint64(&rrs.lastFlushTime)
|
2022-12-05 23:27:57 +00:00
|
|
|
if !isFinal && currentTime < lastFlushTime+uint64(flushSeconds) {
|
2022-10-21 11:33:03 +00:00
|
|
|
// Fast path - nothing to flush
|
|
|
|
return dst
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2022-10-21 11:33:03 +00:00
|
|
|
// Slow path - move rrs.rows to dst.
|
|
|
|
rrs.mu.Lock()
|
|
|
|
dst = append(dst, rrs.rows...)
|
|
|
|
rrs.rows = rrs.rows[:0]
|
|
|
|
atomic.StoreUint64(&rrs.lastFlushTime, currentTime)
|
|
|
|
rrs.mu.Unlock()
|
2021-06-17 10:42:32 +00:00
|
|
|
return dst
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
func (pt *partition) mergePartsOptimal(pws []*partWrapper, stopCh <-chan struct{}) error {
|
|
|
|
sortPartsForOptimalMerge(pws)
|
|
|
|
for len(pws) > 0 {
|
|
|
|
n := defaultPartsToMerge
|
|
|
|
if n > len(pws) {
|
|
|
|
n = len(pws)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2022-12-05 23:27:57 +00:00
|
|
|
pwsChunk := pws[:n]
|
|
|
|
pws = pws[n:]
|
|
|
|
err := pt.mergeParts(pwsChunk, stopCh, true)
|
|
|
|
if err == nil {
|
2019-05-22 21:16:55 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-12-05 23:27:57 +00:00
|
|
|
pt.releasePartsToMerge(pws)
|
|
|
|
if errors.Is(err, errForciblyStopped) {
|
|
|
|
return nil
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2022-12-05 23:27:57 +00:00
|
|
|
return fmt.Errorf("cannot merge parts optimally: %w", err)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
// ForceMergeAllParts runs merge for all the parts in pt.
|
2020-09-17 09:01:53 +00:00
|
|
|
func (pt *partition) ForceMergeAllParts() error {
|
2022-12-05 23:27:57 +00:00
|
|
|
pws := pt.getAllPartsForMerge()
|
2020-09-17 09:01:53 +00:00
|
|
|
if len(pws) == 0 {
|
|
|
|
// Nothing to merge.
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-15 13:58:27 +00:00
|
|
|
|
2023-09-15 17:04:54 +00:00
|
|
|
// Check whether there is enough disk space for merging pws.
|
|
|
|
newPartSize := getPartsSize(pws)
|
|
|
|
maxOutBytes := fs.MustGetFreeSpace(pt.bigPartsPath)
|
|
|
|
if newPartSize > maxOutBytes {
|
|
|
|
freeSpaceNeededBytes := newPartSize - maxOutBytes
|
|
|
|
forceMergeLogger.Warnf("cannot initiate force merge for the partition %s; additional space needed: %d bytes", pt.name, freeSpaceNeededBytes)
|
|
|
|
pt.releasePartsToMerge(pws)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If len(pws) == 1, then the merge must run anyway.
|
|
|
|
// This allows applying the configured retention, removing the deleted series
|
|
|
|
// and performing de-duplication if needed.
|
|
|
|
if err := pt.mergePartsOptimal(pws, pt.stopCh); err != nil {
|
|
|
|
return fmt.Errorf("cannot force merge %d parts from partition %q: %w", len(pws), pt.name, err)
|
2020-09-17 09:01:53 +00:00
|
|
|
}
|
2023-09-15 17:04:54 +00:00
|
|
|
|
|
|
|
return nil
|
2020-09-17 09:01:53 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 09:31:16 +00:00
|
|
|
var forceMergeLogger = logger.WithThrottler("forceMerge", time.Minute)
|
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
func (pt *partition) getAllPartsForMerge() []*partWrapper {
|
|
|
|
var pws []*partWrapper
|
|
|
|
pt.partsLock.Lock()
|
|
|
|
if !hasActiveMerges(pt.inmemoryParts) && !hasActiveMerges(pt.smallParts) && !hasActiveMerges(pt.bigParts) {
|
|
|
|
pws = appendAllPartsForMerge(pws, pt.inmemoryParts)
|
|
|
|
pws = appendAllPartsForMerge(pws, pt.smallParts)
|
|
|
|
pws = appendAllPartsForMerge(pws, pt.bigParts)
|
|
|
|
}
|
|
|
|
pt.partsLock.Unlock()
|
|
|
|
return pws
|
|
|
|
}
|
|
|
|
|
|
|
|
func appendAllPartsForMerge(dst, src []*partWrapper) []*partWrapper {
|
2020-09-17 09:01:53 +00:00
|
|
|
for _, pw := range src {
|
|
|
|
if pw.isInMerge {
|
|
|
|
logger.Panicf("BUG: part %q is already in merge", pw.p.path)
|
|
|
|
}
|
|
|
|
pw.isInMerge = true
|
|
|
|
dst = append(dst, pw)
|
|
|
|
}
|
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasActiveMerges(pws []*partWrapper) bool {
|
|
|
|
for _, pw := range pws {
|
|
|
|
if pw.isInMerge {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-04-14 06:36:06 +00:00
|
|
|
var mergeWorkersLimitCh = make(chan struct{}, getDefaultMergeConcurrency(16))
|
2022-12-05 23:27:57 +00:00
|
|
|
|
2022-06-07 11:55:09 +00:00
|
|
|
func getDefaultMergeConcurrency(max int) int {
|
|
|
|
v := (cgroup.AvailableCPUs() + 1) / 2
|
|
|
|
if v > max {
|
|
|
|
v = max
|
|
|
|
}
|
2023-04-14 06:36:06 +00:00
|
|
|
return adjustMergeWorkersLimit(v)
|
2022-06-07 11:55:09 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
// SetMergeWorkersCount sets the maximum number of concurrent mergers for parts.
|
2019-10-31 14:16:53 +00:00
|
|
|
//
|
|
|
|
// The function must be called before opening or creating any storage.
|
2022-12-05 23:27:57 +00:00
|
|
|
func SetMergeWorkersCount(n int) {
|
2019-10-31 14:16:53 +00:00
|
|
|
if n <= 0 {
|
|
|
|
// Do nothing
|
|
|
|
return
|
|
|
|
}
|
2023-02-11 20:06:18 +00:00
|
|
|
n = adjustMergeWorkersLimit(n)
|
2022-12-05 23:27:57 +00:00
|
|
|
mergeWorkersLimitCh = make(chan struct{}, n)
|
2019-10-31 14:16:53 +00:00
|
|
|
}
|
2019-10-29 10:45:19 +00:00
|
|
|
|
2023-02-11 20:06:18 +00:00
|
|
|
func adjustMergeWorkersLimit(n int) int {
|
2023-04-14 06:36:06 +00:00
|
|
|
if n < 4 {
|
|
|
|
// Allow at least 4 merge workers on systems with small CPUs count
|
2023-02-11 20:06:18 +00:00
|
|
|
// in order to guarantee that background merges can be continued
|
2023-04-14 06:36:06 +00:00
|
|
|
// when multiple workers are busy with big merges.
|
|
|
|
n = 4
|
2023-02-11 20:06:18 +00:00
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
func (pt *partition) startMergeWorkers() {
|
2022-12-13 00:49:21 +00:00
|
|
|
// The actual number of concurrent merges is limited inside mergeWorker() below.
|
2023-04-14 06:36:06 +00:00
|
|
|
for i := 0; i < cap(mergeWorkersLimitCh); i++ {
|
2022-12-04 07:03:05 +00:00
|
|
|
pt.wg.Add(1)
|
2019-05-22 21:16:55 +00:00
|
|
|
go func() {
|
2022-12-05 23:27:57 +00:00
|
|
|
pt.mergeWorker()
|
2022-12-04 07:03:05 +00:00
|
|
|
pt.wg.Done()
|
2019-05-22 21:16:55 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
func (pt *partition) mergeWorker() {
|
2020-05-14 19:01:51 +00:00
|
|
|
var lastMergeTime uint64
|
2019-05-22 21:16:55 +00:00
|
|
|
isFinal := false
|
|
|
|
for {
|
2022-12-13 00:49:21 +00:00
|
|
|
// Limit the number of concurrent calls to mergeExistingParts, since the total number of merge workers
|
|
|
|
// across partitions may exceed the the cap(mergeWorkersLimitCh).
|
2022-12-05 23:27:57 +00:00
|
|
|
mergeWorkersLimitCh <- struct{}{}
|
|
|
|
err := pt.mergeExistingParts(isFinal)
|
|
|
|
<-mergeWorkersLimitCh
|
2019-05-22 21:16:55 +00:00
|
|
|
if err == nil {
|
|
|
|
// Try merging additional parts.
|
2020-05-14 19:01:51 +00:00
|
|
|
lastMergeTime = fasttime.UnixTimestamp()
|
2019-05-22 21:16:55 +00:00
|
|
|
isFinal = false
|
|
|
|
continue
|
|
|
|
}
|
2020-09-17 00:02:35 +00:00
|
|
|
if errors.Is(err, errForciblyStopped) {
|
2019-05-22 21:16:55 +00:00
|
|
|
// The merger has been stopped.
|
2022-12-05 23:27:57 +00:00
|
|
|
return
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2022-08-09 09:17:00 +00:00
|
|
|
if !errors.Is(err, errNothingToMerge) && !errors.Is(err, errReadOnlyMode) {
|
2022-12-05 23:27:57 +00:00
|
|
|
// Unexpected error.
|
|
|
|
logger.Panicf("FATAL: unrecoverable error when merging parts in the partition (%q, %q): %s", pt.smallPartsPath, pt.bigPartsPath, err)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2021-01-07 22:09:00 +00:00
|
|
|
if finalMergeDelaySeconds > 0 && fasttime.UnixTimestamp()-lastMergeTime > finalMergeDelaySeconds {
|
2019-05-22 21:16:55 +00:00
|
|
|
// We have free time for merging into bigger parts.
|
|
|
|
// This should improve select performance.
|
2020-05-14 19:01:51 +00:00
|
|
|
lastMergeTime = fasttime.UnixTimestamp()
|
2019-05-22 21:16:55 +00:00
|
|
|
isFinal = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-01-18 09:09:03 +00:00
|
|
|
// Nothing to merge. Wait for the notification of new merge.
|
2019-05-22 21:16:55 +00:00
|
|
|
select {
|
|
|
|
case <-pt.stopCh:
|
2022-12-05 23:27:57 +00:00
|
|
|
return
|
2023-01-18 09:09:03 +00:00
|
|
|
case <-pt.needMergeCh:
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-07 22:09:00 +00:00
|
|
|
// Disable final merge by default, since it may lead to high disk IO and CPU usage
|
|
|
|
// at the beginning of every month when merging data for the previous month.
|
|
|
|
var finalMergeDelaySeconds = uint64(0)
|
2020-10-07 14:35:42 +00:00
|
|
|
|
|
|
|
// SetFinalMergeDelay sets the delay before doing final merge for partitions without newly ingested data.
|
|
|
|
//
|
|
|
|
// This function may be called only before Storage initialization.
|
|
|
|
func SetFinalMergeDelay(delay time.Duration) {
|
2021-01-07 22:09:00 +00:00
|
|
|
if delay <= 0 {
|
2020-10-07 14:35:42 +00:00
|
|
|
return
|
|
|
|
}
|
2021-01-07 22:09:00 +00:00
|
|
|
finalMergeDelaySeconds = uint64(delay.Seconds() + 1)
|
2022-12-05 23:27:57 +00:00
|
|
|
mergeset.SetFinalMergeDelay(delay)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getMaxInmemoryPartSize() uint64 {
|
|
|
|
// Allocate 10% of allowed memory for in-memory parts.
|
|
|
|
n := uint64(0.1 * float64(memory.Allowed()) / maxInmemoryPartsPerPartition)
|
|
|
|
if n < 1e6 {
|
|
|
|
n = 1e6
|
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pt *partition) getMaxSmallPartSize() uint64 {
|
|
|
|
// Small parts are cached in the OS page cache,
|
|
|
|
// so limit their size by the remaining free RAM.
|
|
|
|
mem := memory.Remaining()
|
|
|
|
// It is expected no more than defaultPartsToMerge/2 parts exist
|
|
|
|
// in the OS page cache before they are merged into bigger part.
|
|
|
|
// Half of the remaining RAM must be left for lib/mergeset parts,
|
|
|
|
// so the maxItems is calculated using the below code:
|
|
|
|
n := uint64(mem) / defaultPartsToMerge
|
|
|
|
if n < 10e6 {
|
|
|
|
n = 10e6
|
|
|
|
}
|
|
|
|
// Make sure the output part fits available disk space for small parts.
|
|
|
|
sizeLimit := getMaxOutBytes(pt.smallPartsPath, cap(mergeWorkersLimitCh))
|
|
|
|
if n > sizeLimit {
|
|
|
|
n = sizeLimit
|
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pt *partition) getMaxBigPartSize() uint64 {
|
2023-04-14 03:33:33 +00:00
|
|
|
workersCount := getDefaultMergeConcurrency(4)
|
|
|
|
return getMaxOutBytes(pt.bigPartsPath, workersCount)
|
2020-10-07 14:35:42 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 06:35:03 +00:00
|
|
|
func getMaxOutBytes(path string, workersCount int) uint64 {
|
|
|
|
n := fs.MustGetFreeSpace(path)
|
2022-12-13 00:49:21 +00:00
|
|
|
// Do not subtract freeDiskSpaceLimitBytes from n before calculating the maxOutBytes,
|
2021-12-01 08:56:21 +00:00
|
|
|
// since this will result in sub-optimal merges - e.g. many small parts will be left unmerged.
|
|
|
|
|
2022-12-13 00:49:21 +00:00
|
|
|
// Divide free space by the max number of concurrent merges.
|
2021-08-25 06:35:03 +00:00
|
|
|
maxOutBytes := n / uint64(workersCount)
|
|
|
|
if maxOutBytes > maxBigPartSize {
|
|
|
|
maxOutBytes = maxBigPartSize
|
2019-08-25 11:10:43 +00:00
|
|
|
}
|
2021-08-25 06:35:03 +00:00
|
|
|
return maxOutBytes
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
2022-06-01 11:21:12 +00:00
|
|
|
func (pt *partition) canBackgroundMerge() bool {
|
2022-10-23 22:30:50 +00:00
|
|
|
return atomic.LoadUint32(&pt.s.isReadOnly) == 0
|
2022-06-01 11:21:12 +00:00
|
|
|
}
|
|
|
|
|
2022-08-09 09:17:00 +00:00
|
|
|
var errReadOnlyMode = fmt.Errorf("storage is in readonly mode")
|
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
func (pt *partition) mergeInmemoryParts() error {
|
|
|
|
maxOutBytes := pt.getMaxBigPartSize()
|
2019-05-22 21:16:55 +00:00
|
|
|
|
|
|
|
pt.partsLock.Lock()
|
2022-12-05 23:27:57 +00:00
|
|
|
pws, needFreeSpace := getPartsToMerge(pt.inmemoryParts, maxOutBytes, false)
|
2019-05-22 21:16:55 +00:00
|
|
|
pt.partsLock.Unlock()
|
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
atomicSetBool(&pt.mergeNeedFreeDiskSpace, needFreeSpace)
|
|
|
|
return pt.mergeParts(pws, pt.stopCh, false)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
func (pt *partition) mergeExistingParts(isFinal bool) error {
|
2022-06-01 11:21:12 +00:00
|
|
|
if !pt.canBackgroundMerge() {
|
|
|
|
// Do not perform merge in read-only mode, since this may result in disk space shortage.
|
|
|
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2603
|
2022-08-09 09:17:00 +00:00
|
|
|
return errReadOnlyMode
|
2022-06-01 11:21:12 +00:00
|
|
|
}
|
2022-12-05 23:27:57 +00:00
|
|
|
maxOutBytes := pt.getMaxBigPartSize()
|
2020-12-18 21:14:35 +00:00
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
pt.partsLock.Lock()
|
2022-12-05 23:27:57 +00:00
|
|
|
dst := make([]*partWrapper, 0, len(pt.inmemoryParts)+len(pt.smallParts)+len(pt.bigParts))
|
|
|
|
dst = append(dst, pt.inmemoryParts...)
|
|
|
|
dst = append(dst, pt.smallParts...)
|
|
|
|
dst = append(dst, pt.bigParts...)
|
|
|
|
pws, needFreeSpace := getPartsToMerge(dst, maxOutBytes, isFinal)
|
2019-05-22 21:16:55 +00:00
|
|
|
pt.partsLock.Unlock()
|
2020-12-18 21:14:35 +00:00
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
atomicSetBool(&pt.mergeNeedFreeDiskSpace, needFreeSpace)
|
|
|
|
return pt.mergeParts(pws, pt.stopCh, isFinal)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 21:14:35 +00:00
|
|
|
func (pt *partition) releasePartsToMerge(pws []*partWrapper) {
|
|
|
|
pt.partsLock.Lock()
|
|
|
|
for _, pw := range pws {
|
|
|
|
if !pw.isInMerge {
|
|
|
|
logger.Panicf("BUG: missing isInMerge flag on the part %q", pw.p.path)
|
|
|
|
}
|
|
|
|
pw.isInMerge = false
|
|
|
|
}
|
|
|
|
pt.partsLock.Unlock()
|
|
|
|
}
|
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
var errNothingToMerge = fmt.Errorf("nothing to merge")
|
|
|
|
|
2020-09-29 18:47:40 +00:00
|
|
|
func atomicSetBool(p *uint64, b bool) {
|
|
|
|
v := uint64(0)
|
|
|
|
if b {
|
|
|
|
v = 1
|
|
|
|
}
|
|
|
|
atomic.StoreUint64(p, v)
|
|
|
|
}
|
|
|
|
|
2021-12-15 13:58:27 +00:00
|
|
|
func (pt *partition) runFinalDedup() error {
|
2021-12-17 18:11:15 +00:00
|
|
|
requiredDedupInterval, actualDedupInterval := pt.getRequiredDedupInterval()
|
2021-12-15 13:58:27 +00:00
|
|
|
t := time.Now()
|
2021-12-17 18:11:15 +00:00
|
|
|
logger.Infof("starting final dedup for partition %s using requiredDedupInterval=%d ms, since the partition has smaller actualDedupInterval=%d ms",
|
|
|
|
pt.bigPartsPath, requiredDedupInterval, actualDedupInterval)
|
2021-12-15 13:58:27 +00:00
|
|
|
if err := pt.ForceMergeAllParts(); err != nil {
|
2021-12-17 18:11:15 +00:00
|
|
|
return fmt.Errorf("cannot perform final dedup for partition %s: %w", pt.bigPartsPath, err)
|
2021-12-15 13:58:27 +00:00
|
|
|
}
|
2021-12-17 18:11:15 +00:00
|
|
|
logger.Infof("final dedup for partition %s has been finished in %.3f seconds", pt.bigPartsPath, time.Since(t).Seconds())
|
2021-12-15 13:58:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-20 18:11:38 +00:00
|
|
|
func (pt *partition) isFinalDedupNeeded() bool {
|
|
|
|
requiredDedupInterval, actualDedupInterval := pt.getRequiredDedupInterval()
|
|
|
|
return requiredDedupInterval > actualDedupInterval
|
|
|
|
}
|
|
|
|
|
2021-12-17 18:11:15 +00:00
|
|
|
func (pt *partition) getRequiredDedupInterval() (int64, int64) {
|
2023-02-01 17:54:21 +00:00
|
|
|
pws := pt.GetParts(nil, false)
|
2021-12-15 13:58:27 +00:00
|
|
|
defer pt.PutParts(pws)
|
|
|
|
dedupInterval := GetDedupInterval()
|
|
|
|
minDedupInterval := getMinDedupInterval(pws)
|
2021-12-17 18:11:15 +00:00
|
|
|
return dedupInterval, minDedupInterval
|
|
|
|
}
|
|
|
|
|
|
|
|
func getMinDedupInterval(pws []*partWrapper) int64 {
|
|
|
|
if len(pws) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
dMin := pws[0].p.ph.MinDedupInterval
|
|
|
|
for _, pw := range pws[1:] {
|
|
|
|
d := pw.p.ph.MinDedupInterval
|
|
|
|
if d < dMin {
|
|
|
|
dMin = d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dMin
|
2021-12-15 13:58:27 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
// mergeParts merges pws to a single resulting part.
|
2020-09-16 23:05:54 +00:00
|
|
|
//
|
|
|
|
// Merging is immediately stopped if stopCh is closed.
|
|
|
|
//
|
2022-12-05 23:27:57 +00:00
|
|
|
// if isFinal is set, then the resulting part will be saved to disk.
|
|
|
|
//
|
2020-09-16 23:05:54 +00:00
|
|
|
// All the parts inside pws must have isInMerge field set to true.
|
2022-12-05 23:27:57 +00:00
|
|
|
func (pt *partition) mergeParts(pws []*partWrapper, stopCh <-chan struct{}, isFinal bool) error {
|
2019-05-22 21:16:55 +00:00
|
|
|
if len(pws) == 0 {
|
|
|
|
// Nothing to merge.
|
|
|
|
return errNothingToMerge
|
|
|
|
}
|
2020-12-18 21:14:35 +00:00
|
|
|
defer pt.releasePartsToMerge(pws)
|
2019-05-22 21:16:55 +00:00
|
|
|
|
|
|
|
startTime := time.Now()
|
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
// Initialize destination paths.
|
|
|
|
dstPartType := pt.getDstPartType(pws, isFinal)
|
|
|
|
ptPath, tmpPartPath, mergeIdx := pt.getDstPartPaths(dstPartType)
|
|
|
|
|
2023-02-01 17:54:21 +00:00
|
|
|
if !isDedupEnabled() && isFinal && len(pws) == 1 && pws[0].mp != nil {
|
2022-12-05 23:27:57 +00:00
|
|
|
// Fast path: flush a single in-memory part to disk.
|
|
|
|
mp := pws[0].mp
|
|
|
|
if tmpPartPath == "" {
|
|
|
|
logger.Panicf("BUG: tmpPartPath must be non-empty")
|
|
|
|
}
|
2023-02-01 17:54:21 +00:00
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
if err := mp.StoreToDisk(tmpPartPath); err != nil {
|
|
|
|
return fmt.Errorf("cannot store in-memory part to %q: %w", tmpPartPath, err)
|
|
|
|
}
|
|
|
|
pwNew, err := pt.openCreatedPart(&mp.ph, pws, nil, ptPath, tmpPartPath, mergeIdx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot atomically register the created part: %w", err)
|
|
|
|
}
|
|
|
|
pt.swapSrcWithDstParts(pws, pwNew, dstPartType)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
// Prepare BlockStreamReaders for source parts.
|
2022-12-05 23:27:57 +00:00
|
|
|
bsrs, err := openBlockStreamReaders(pws)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
closeBlockStreamReaders := func() {
|
2019-05-22 21:16:55 +00:00
|
|
|
for _, bsr := range bsrs {
|
|
|
|
putBlockStreamReader(bsr)
|
|
|
|
}
|
2022-12-05 23:27:57 +00:00
|
|
|
bsrs = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare BlockStreamWriter for destination part.
|
|
|
|
srcSize := uint64(0)
|
|
|
|
srcRowsCount := uint64(0)
|
|
|
|
srcBlocksCount := uint64(0)
|
2019-05-22 21:16:55 +00:00
|
|
|
for _, pw := range pws {
|
2022-12-05 23:27:57 +00:00
|
|
|
srcSize += pw.p.size
|
|
|
|
srcRowsCount += pw.p.ph.RowsCount
|
|
|
|
srcBlocksCount += pw.p.ph.BlocksCount
|
|
|
|
}
|
|
|
|
rowsPerBlock := float64(srcRowsCount) / float64(srcBlocksCount)
|
|
|
|
compressLevel := getCompressLevel(rowsPerBlock)
|
|
|
|
bsw := getBlockStreamWriter()
|
|
|
|
var mpNew *inmemoryPart
|
|
|
|
if dstPartType == partInmemory {
|
|
|
|
mpNew = getInmemoryPart()
|
|
|
|
bsw.InitFromInmemoryPart(mpNew, compressLevel)
|
|
|
|
} else {
|
|
|
|
if tmpPartPath == "" {
|
|
|
|
logger.Panicf("BUG: tmpPartPath must be non-empty")
|
|
|
|
}
|
|
|
|
nocache := dstPartType == partBig
|
|
|
|
if err := bsw.InitFromFilePart(tmpPartPath, nocache, compressLevel); err != nil {
|
|
|
|
closeBlockStreamReaders()
|
|
|
|
return fmt.Errorf("cannot create destination part at %q: %w", tmpPartPath, err)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
// Merge source parts to destination part.
|
|
|
|
ph, err := pt.mergePartsInternal(tmpPartPath, bsw, bsrs, dstPartType, stopCh)
|
|
|
|
putBlockStreamWriter(bsw)
|
|
|
|
closeBlockStreamReaders()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot merge %d parts: %w", len(pws), err)
|
|
|
|
}
|
|
|
|
if mpNew != nil {
|
|
|
|
// Update partHeader for destination inmemory part after the merge.
|
|
|
|
mpNew.ph = *ph
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
// Atomically move the created part from tmpPartPath to its destination
|
|
|
|
// and swap the source parts with the newly created part.
|
|
|
|
pwNew, err := pt.openCreatedPart(ph, pws, mpNew, ptPath, tmpPartPath, mergeIdx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot atomically register the created part: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dstRowsCount := uint64(0)
|
|
|
|
dstBlocksCount := uint64(0)
|
|
|
|
dstSize := uint64(0)
|
|
|
|
dstPartPath := ""
|
|
|
|
if pwNew != nil {
|
|
|
|
pDst := pwNew.p
|
|
|
|
dstRowsCount = pDst.ph.RowsCount
|
|
|
|
dstBlocksCount = pDst.ph.BlocksCount
|
|
|
|
dstSize = pDst.size
|
|
|
|
dstPartPath = pDst.String()
|
|
|
|
}
|
2023-03-03 11:33:42 +00:00
|
|
|
|
|
|
|
pt.swapSrcWithDstParts(pws, pwNew, dstPartType)
|
|
|
|
|
|
|
|
d := time.Since(startTime)
|
|
|
|
if d <= 30*time.Second {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Log stats for long merges.
|
2022-12-05 23:27:57 +00:00
|
|
|
durationSecs := d.Seconds()
|
|
|
|
rowsPerSec := int(float64(srcRowsCount) / durationSecs)
|
|
|
|
logger.Infof("merged (%d parts, %d rows, %d blocks, %d bytes) into (1 part, %d rows, %d blocks, %d bytes) in %.3f seconds at %d rows/sec to %q",
|
|
|
|
len(pws), srcRowsCount, srcBlocksCount, srcSize, dstRowsCount, dstBlocksCount, dstSize, durationSecs, rowsPerSec, dstPartPath)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getFlushToDiskDeadline(pws []*partWrapper) time.Time {
|
|
|
|
d := pws[0].flushToDiskDeadline
|
|
|
|
for _, pw := range pws[1:] {
|
|
|
|
if pw.flushToDiskDeadline.Before(d) {
|
|
|
|
d = pw.flushToDiskDeadline
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
type partType int
|
|
|
|
|
|
|
|
var (
|
|
|
|
partInmemory = partType(0)
|
|
|
|
partSmall = partType(1)
|
|
|
|
partBig = partType(2)
|
|
|
|
)
|
|
|
|
|
|
|
|
func (pt *partition) getDstPartType(pws []*partWrapper, isFinal bool) partType {
|
|
|
|
dstPartSize := getPartsSize(pws)
|
|
|
|
if dstPartSize > pt.getMaxSmallPartSize() {
|
|
|
|
return partBig
|
|
|
|
}
|
|
|
|
if isFinal || dstPartSize > getMaxInmemoryPartSize() {
|
|
|
|
return partSmall
|
|
|
|
}
|
|
|
|
if !areAllInmemoryParts(pws) {
|
|
|
|
// If at least a single source part is located in file,
|
|
|
|
// then the destination part must be in file for durability reasons.
|
|
|
|
return partSmall
|
|
|
|
}
|
|
|
|
return partInmemory
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pt *partition) getDstPartPaths(dstPartType partType) (string, string, uint64) {
|
|
|
|
ptPath := ""
|
|
|
|
switch dstPartType {
|
|
|
|
case partSmall:
|
|
|
|
ptPath = pt.smallPartsPath
|
|
|
|
case partBig:
|
2019-05-22 21:16:55 +00:00
|
|
|
ptPath = pt.bigPartsPath
|
2022-12-05 23:27:57 +00:00
|
|
|
case partInmemory:
|
|
|
|
ptPath = pt.smallPartsPath
|
|
|
|
default:
|
|
|
|
logger.Panicf("BUG: unknown partType=%d", dstPartType)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
ptPath = filepath.Clean(ptPath)
|
|
|
|
mergeIdx := pt.nextMergeIdx()
|
2022-12-05 23:27:57 +00:00
|
|
|
tmpPartPath := ""
|
|
|
|
if dstPartType != partInmemory {
|
|
|
|
tmpPartPath = fmt.Sprintf("%s/tmp/%016X", ptPath, mergeIdx)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2022-12-05 23:27:57 +00:00
|
|
|
return ptPath, tmpPartPath, mergeIdx
|
|
|
|
}
|
2019-05-22 21:16:55 +00:00
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
func openBlockStreamReaders(pws []*partWrapper) ([]*blockStreamReader, error) {
|
|
|
|
bsrs := make([]*blockStreamReader, 0, len(pws))
|
|
|
|
for _, pw := range pws {
|
|
|
|
bsr := getBlockStreamReader()
|
|
|
|
if pw.mp != nil {
|
|
|
|
bsr.InitFromInmemoryPart(pw.mp)
|
|
|
|
} else {
|
|
|
|
if err := bsr.InitFromFilePart(pw.p.path); err != nil {
|
|
|
|
for _, bsr := range bsrs {
|
|
|
|
putBlockStreamReader(bsr)
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("cannot open source part for merging: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bsrs = append(bsrs, bsr)
|
|
|
|
}
|
|
|
|
return bsrs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pt *partition) mergePartsInternal(tmpPartPath string, bsw *blockStreamWriter, bsrs []*blockStreamReader, dstPartType partType, stopCh <-chan struct{}) (*partHeader, error) {
|
2019-05-22 21:16:55 +00:00
|
|
|
var ph partHeader
|
2022-12-05 23:27:57 +00:00
|
|
|
var rowsMerged *uint64
|
|
|
|
var rowsDeleted *uint64
|
|
|
|
var mergesCount *uint64
|
|
|
|
var activeMerges *uint64
|
|
|
|
switch dstPartType {
|
|
|
|
case partInmemory:
|
|
|
|
rowsMerged = &pt.inmemoryRowsMerged
|
|
|
|
rowsDeleted = &pt.inmemoryRowsDeleted
|
|
|
|
mergesCount = &pt.inmemoryMergesCount
|
|
|
|
activeMerges = &pt.activeInmemoryMerges
|
|
|
|
case partSmall:
|
|
|
|
rowsMerged = &pt.smallRowsMerged
|
|
|
|
rowsDeleted = &pt.smallRowsDeleted
|
|
|
|
mergesCount = &pt.smallMergesCount
|
|
|
|
activeMerges = &pt.activeSmallMerges
|
|
|
|
case partBig:
|
2019-05-22 21:16:55 +00:00
|
|
|
rowsMerged = &pt.bigRowsMerged
|
|
|
|
rowsDeleted = &pt.bigRowsDeleted
|
2022-12-05 23:27:57 +00:00
|
|
|
mergesCount = &pt.bigMergesCount
|
|
|
|
activeMerges = &pt.activeBigMerges
|
|
|
|
default:
|
|
|
|
logger.Panicf("BUG: unknown partType=%d", dstPartType)
|
2020-07-22 21:58:48 +00:00
|
|
|
}
|
2022-12-05 23:27:57 +00:00
|
|
|
retentionDeadline := timestampFromTime(time.Now()) - pt.s.retentionMsecs
|
|
|
|
atomic.AddUint64(activeMerges, 1)
|
2022-10-23 13:08:54 +00:00
|
|
|
err := mergeBlockStreams(&ph, bsw, bsrs, stopCh, pt.s, retentionDeadline, rowsMerged, rowsDeleted)
|
2022-12-05 23:27:57 +00:00
|
|
|
atomic.AddUint64(activeMerges, ^uint64(0))
|
|
|
|
atomic.AddUint64(mergesCount, 1)
|
2019-05-22 21:16:55 +00:00
|
|
|
if err != nil {
|
2022-12-05 23:27:57 +00:00
|
|
|
return nil, fmt.Errorf("cannot merge parts to %q: %w", tmpPartPath, err)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2022-12-05 23:27:57 +00:00
|
|
|
if tmpPartPath != "" {
|
|
|
|
ph.MinDedupInterval = GetDedupInterval()
|
|
|
|
if err := ph.writeMinDedupInterval(tmpPartPath); err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot store min dedup interval: %w", err)
|
|
|
|
}
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2022-12-05 23:27:57 +00:00
|
|
|
return &ph, nil
|
|
|
|
}
|
2019-05-22 21:16:55 +00:00
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
func (pt *partition) openCreatedPart(ph *partHeader, pws []*partWrapper, mpNew *inmemoryPart, ptPath, tmpPartPath string, mergeIdx uint64) (*partWrapper, error) {
|
|
|
|
dstPartPath := ""
|
|
|
|
if mpNew == nil || !areAllInmemoryParts(pws) {
|
|
|
|
// Either source or destination parts are located on disk.
|
|
|
|
// Create a transaction for atomic deleting of old parts and moving new part to its destination on disk.
|
|
|
|
var bb bytesutil.ByteBuffer
|
|
|
|
for _, pw := range pws {
|
|
|
|
if pw.mp == nil {
|
|
|
|
fmt.Fprintf(&bb, "%s\n", pw.p.path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ph.RowsCount > 0 {
|
|
|
|
// The destination part may have no rows if they are deleted during the merge.
|
|
|
|
dstPartPath = ph.Path(ptPath, mergeIdx)
|
|
|
|
}
|
|
|
|
fmt.Fprintf(&bb, "%s -> %s\n", tmpPartPath, dstPartPath)
|
|
|
|
txnPath := fmt.Sprintf("%s/txn/%016X", ptPath, mergeIdx)
|
|
|
|
if err := fs.WriteFileAtomically(txnPath, bb.B, false); err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot create transaction file %q: %w", txnPath, err)
|
|
|
|
}
|
2021-12-15 13:58:27 +00:00
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
// Run the created transaction.
|
|
|
|
if err := runTransaction(&pt.snapshotLock, pt.smallPartsPath, pt.bigPartsPath, txnPath); err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot execute transaction %q: %w", txnPath, err)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-05 23:27:57 +00:00
|
|
|
// Open the created part.
|
|
|
|
if ph.RowsCount == 0 {
|
|
|
|
// The created part is empty.
|
|
|
|
return nil, nil
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2022-12-05 23:27:57 +00:00
|
|
|
if mpNew != nil {
|
|
|
|
// Open the created part from memory.
|
|
|
|
flushToDiskDeadline := getFlushToDiskDeadline(pws)
|
|
|
|
pwNew := newPartWrapperFromInmemoryPart(mpNew, flushToDiskDeadline)
|
|
|
|
return pwNew, nil
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2022-12-05 23:27:57 +00:00
|
|
|
// Open the created part from disk.
|
|
|
|
pNew, err := openFilePart(dstPartPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot open merged part %q: %w", dstPartPath, err)
|
|
|
|
}
|
|
|
|
pwNew := &partWrapper{
|
|
|
|
p: pNew,
|
|
|
|
refCount: 1,
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2022-12-05 23:27:57 +00:00
|
|
|
return pwNew, nil
|
|
|
|
}
|
2019-05-22 21:16:55 +00:00
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
func areAllInmemoryParts(pws []*partWrapper) bool {
|
|
|
|
for _, pw := range pws {
|
|
|
|
if pw.mp == nil {
|
|
|
|
return false
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-05 23:27:57 +00:00
|
|
|
return true
|
|
|
|
}
|
2019-05-22 21:16:55 +00:00
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
func (pt *partition) swapSrcWithDstParts(pws []*partWrapper, pwNew *partWrapper, dstPartType partType) {
|
|
|
|
// Atomically unregister old parts and add new part to pt.
|
2019-05-22 21:16:55 +00:00
|
|
|
m := make(map[*partWrapper]bool, len(pws))
|
|
|
|
for _, pw := range pws {
|
|
|
|
m[pw] = true
|
|
|
|
}
|
|
|
|
if len(m) != len(pws) {
|
2022-12-05 23:27:57 +00:00
|
|
|
logger.Panicf("BUG: %d duplicate parts found when merging %d parts", len(pws)-len(m), len(pws))
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2022-12-05 23:27:57 +00:00
|
|
|
removedInmemoryParts := 0
|
2019-05-22 21:16:55 +00:00
|
|
|
removedSmallParts := 0
|
|
|
|
removedBigParts := 0
|
2022-12-05 23:27:57 +00:00
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
pt.partsLock.Lock()
|
2022-12-05 23:27:57 +00:00
|
|
|
pt.inmemoryParts, removedInmemoryParts = removeParts(pt.inmemoryParts, m)
|
|
|
|
pt.smallParts, removedSmallParts = removeParts(pt.smallParts, m)
|
|
|
|
pt.bigParts, removedBigParts = removeParts(pt.bigParts, m)
|
|
|
|
if pwNew != nil {
|
|
|
|
switch dstPartType {
|
|
|
|
case partInmemory:
|
|
|
|
pt.inmemoryParts = append(pt.inmemoryParts, pwNew)
|
|
|
|
case partSmall:
|
|
|
|
pt.smallParts = append(pt.smallParts, pwNew)
|
|
|
|
case partBig:
|
|
|
|
pt.bigParts = append(pt.bigParts, pwNew)
|
|
|
|
default:
|
|
|
|
logger.Panicf("BUG: unknown partType=%d", dstPartType)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2023-01-18 09:09:03 +00:00
|
|
|
pt.notifyBackgroundMergers()
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
pt.partsLock.Unlock()
|
2022-12-05 23:27:57 +00:00
|
|
|
|
|
|
|
removedParts := removedInmemoryParts + removedSmallParts + removedBigParts
|
|
|
|
if removedParts != len(m) {
|
|
|
|
logger.Panicf("BUG: unexpected number of parts removed; got %d, want %d", removedParts, len(m))
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove partition references from old parts.
|
|
|
|
for _, pw := range pws {
|
|
|
|
pw.decRef()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 07:10:16 +00:00
|
|
|
func getCompressLevel(rowsPerBlock float64) int {
|
2022-02-25 13:32:27 +00:00
|
|
|
// See https://github.com/facebook/zstd/releases/tag/v1.3.4 about negative compression levels.
|
2022-12-04 07:10:16 +00:00
|
|
|
if rowsPerBlock <= 10 {
|
2022-02-25 13:32:27 +00:00
|
|
|
return -5
|
|
|
|
}
|
2022-12-04 07:10:16 +00:00
|
|
|
if rowsPerBlock <= 50 {
|
2022-02-25 13:32:27 +00:00
|
|
|
return -2
|
|
|
|
}
|
2022-12-04 07:10:16 +00:00
|
|
|
if rowsPerBlock <= 200 {
|
2020-05-15 10:11:30 +00:00
|
|
|
return -1
|
|
|
|
}
|
2022-12-04 07:10:16 +00:00
|
|
|
if rowsPerBlock <= 500 {
|
2019-05-22 21:16:55 +00:00
|
|
|
return 1
|
|
|
|
}
|
2022-12-04 07:10:16 +00:00
|
|
|
if rowsPerBlock <= 1000 {
|
2019-05-22 21:16:55 +00:00
|
|
|
return 2
|
|
|
|
}
|
2022-12-04 07:10:16 +00:00
|
|
|
if rowsPerBlock <= 2000 {
|
2019-05-22 21:16:55 +00:00
|
|
|
return 3
|
|
|
|
}
|
2022-12-04 07:10:16 +00:00
|
|
|
if rowsPerBlock <= 4000 {
|
2019-05-22 21:16:55 +00:00
|
|
|
return 4
|
|
|
|
}
|
|
|
|
return 5
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pt *partition) nextMergeIdx() uint64 {
|
|
|
|
return atomic.AddUint64(&pt.mergeIdx, 1)
|
|
|
|
}
|
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
func removeParts(pws []*partWrapper, partsToRemove map[*partWrapper]bool) ([]*partWrapper, int) {
|
2019-05-22 21:16:55 +00:00
|
|
|
dst := pws[:0]
|
|
|
|
for _, pw := range pws {
|
2020-09-16 23:05:54 +00:00
|
|
|
if !partsToRemove[pw] {
|
|
|
|
dst = append(dst, pw)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-05 23:27:57 +00:00
|
|
|
for i := len(dst); i < len(pws); i++ {
|
|
|
|
pws[i] = nil
|
|
|
|
}
|
|
|
|
return dst, len(pws) - len(dst)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 17:48:27 +00:00
|
|
|
func (pt *partition) startStalePartsRemover() {
|
2022-12-04 07:03:05 +00:00
|
|
|
pt.wg.Add(1)
|
2020-12-22 17:48:27 +00:00
|
|
|
go func() {
|
|
|
|
pt.stalePartsRemover()
|
2022-12-04 07:03:05 +00:00
|
|
|
pt.wg.Done()
|
2020-12-22 17:48:27 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pt *partition) stalePartsRemover() {
|
|
|
|
ticker := time.NewTicker(7 * time.Minute)
|
|
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-pt.stopCh:
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
pt.removeStaleParts()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pt *partition) removeStaleParts() {
|
|
|
|
m := make(map[*partWrapper]bool)
|
|
|
|
startTime := time.Now()
|
2022-10-23 22:30:50 +00:00
|
|
|
retentionDeadline := timestampFromTime(startTime) - pt.s.retentionMsecs
|
2020-12-22 17:48:27 +00:00
|
|
|
|
|
|
|
pt.partsLock.Lock()
|
2022-12-05 23:27:57 +00:00
|
|
|
for _, pw := range pt.inmemoryParts {
|
2020-12-24 06:50:10 +00:00
|
|
|
if !pw.isInMerge && pw.p.ph.MaxTimestamp < retentionDeadline {
|
2022-12-05 23:27:57 +00:00
|
|
|
atomic.AddUint64(&pt.inmemoryRowsDeleted, pw.p.ph.RowsCount)
|
2020-12-22 17:48:27 +00:00
|
|
|
m[pw] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, pw := range pt.smallParts {
|
2020-12-24 06:50:10 +00:00
|
|
|
if !pw.isInMerge && pw.p.ph.MaxTimestamp < retentionDeadline {
|
2020-12-22 17:48:27 +00:00
|
|
|
atomic.AddUint64(&pt.smallRowsDeleted, pw.p.ph.RowsCount)
|
|
|
|
m[pw] = true
|
|
|
|
}
|
|
|
|
}
|
2022-12-05 23:27:57 +00:00
|
|
|
for _, pw := range pt.bigParts {
|
|
|
|
if !pw.isInMerge && pw.p.ph.MaxTimestamp < retentionDeadline {
|
|
|
|
atomic.AddUint64(&pt.bigRowsDeleted, pw.p.ph.RowsCount)
|
|
|
|
m[pw] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
removedInmemoryParts := 0
|
2020-12-22 17:48:27 +00:00
|
|
|
removedSmallParts := 0
|
|
|
|
removedBigParts := 0
|
|
|
|
if len(m) > 0 {
|
2022-12-05 23:27:57 +00:00
|
|
|
pt.inmemoryParts, removedInmemoryParts = removeParts(pt.inmemoryParts, m)
|
|
|
|
pt.smallParts, removedSmallParts = removeParts(pt.smallParts, m)
|
|
|
|
pt.bigParts, removedBigParts = removeParts(pt.bigParts, m)
|
2020-12-22 17:48:27 +00:00
|
|
|
}
|
|
|
|
pt.partsLock.Unlock()
|
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
removedParts := removedInmemoryParts + removedSmallParts + removedBigParts
|
|
|
|
if removedParts != len(m) {
|
|
|
|
logger.Panicf("BUG: unexpected number of stale parts removed; got %d, want %d", removedParts, len(m))
|
2020-12-22 17:48:27 +00:00
|
|
|
}
|
|
|
|
|
2020-12-24 14:50:55 +00:00
|
|
|
// Physically remove stale parts under snapshotLock in order to provide
|
2022-09-13 12:28:01 +00:00
|
|
|
// consistent snapshots with table.CreateSnapshot().
|
2020-12-24 14:50:55 +00:00
|
|
|
pt.snapshotLock.RLock()
|
2020-12-22 17:48:27 +00:00
|
|
|
for pw := range m {
|
2022-12-05 23:27:57 +00:00
|
|
|
if pw.mp == nil {
|
|
|
|
logger.Infof("removing part %q, since its data is out of the configured retention (%d secs)", pw.p.path, pt.s.retentionMsecs/1000)
|
|
|
|
fs.MustRemoveDirAtomic(pw.p.path)
|
|
|
|
}
|
2020-12-24 14:50:55 +00:00
|
|
|
}
|
2020-12-25 09:45:47 +00:00
|
|
|
// There is no need in calling fs.MustSyncPath() on pt.smallPartsPath and pt.bigPartsPath,
|
2022-09-13 12:28:01 +00:00
|
|
|
// since they should be automatically called inside fs.MustRemoveDirAtomic().
|
2020-12-24 14:50:55 +00:00
|
|
|
pt.snapshotLock.RUnlock()
|
|
|
|
|
|
|
|
// Remove partition references from removed parts.
|
|
|
|
for pw := range m {
|
2020-12-22 17:48:27 +00:00
|
|
|
pw.decRef()
|
|
|
|
}
|
2020-12-24 14:50:55 +00:00
|
|
|
|
2020-12-22 17:48:27 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
// getPartsToMerge returns optimal parts to merge from pws.
|
|
|
|
//
|
2021-08-25 06:35:03 +00:00
|
|
|
// The summary size of the returned parts must be smaller than maxOutBytes.
|
|
|
|
// The function returns true if pws contains parts, which cannot be merged because of maxOutBytes limit.
|
|
|
|
func getPartsToMerge(pws []*partWrapper, maxOutBytes uint64, isFinal bool) ([]*partWrapper, bool) {
|
2019-05-22 21:16:55 +00:00
|
|
|
pwsRemaining := make([]*partWrapper, 0, len(pws))
|
|
|
|
for _, pw := range pws {
|
|
|
|
if !pw.isInMerge {
|
|
|
|
pwsRemaining = append(pwsRemaining, pw)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
maxPartsToMerge := defaultPartsToMerge
|
|
|
|
var pms []*partWrapper
|
2020-09-29 18:47:40 +00:00
|
|
|
needFreeSpace := false
|
2019-05-22 21:16:55 +00:00
|
|
|
if isFinal {
|
|
|
|
for len(pms) == 0 && maxPartsToMerge >= finalPartsToMerge {
|
2021-08-25 06:35:03 +00:00
|
|
|
pms, needFreeSpace = appendPartsToMerge(pms[:0], pwsRemaining, maxPartsToMerge, maxOutBytes)
|
2019-05-22 21:16:55 +00:00
|
|
|
maxPartsToMerge--
|
|
|
|
}
|
|
|
|
} else {
|
2021-08-25 06:35:03 +00:00
|
|
|
pms, needFreeSpace = appendPartsToMerge(pms[:0], pwsRemaining, maxPartsToMerge, maxOutBytes)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
for _, pw := range pms {
|
|
|
|
if pw.isInMerge {
|
|
|
|
logger.Panicf("BUG: partWrapper.isInMerge cannot be set")
|
|
|
|
}
|
|
|
|
pw.isInMerge = true
|
|
|
|
}
|
2020-09-29 18:47:40 +00:00
|
|
|
return pms, needFreeSpace
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 06:35:03 +00:00
|
|
|
// minMergeMultiplier is the minimum multiplier for the size of the output part
|
|
|
|
// compared to the size of the maximum input part for the merge.
|
|
|
|
//
|
|
|
|
// Higher value reduces write amplification (disk write IO induced by the merge),
|
|
|
|
// while increases the number of unmerged parts.
|
|
|
|
// The 1.7 is good enough for production workloads.
|
|
|
|
const minMergeMultiplier = 1.7
|
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
// appendPartsToMerge finds optimal parts to merge from src, appends
|
|
|
|
// them to dst and returns the result.
|
2021-08-25 06:35:03 +00:00
|
|
|
// The function returns true if src contains parts, which cannot be merged because of maxOutBytes limit.
|
|
|
|
func appendPartsToMerge(dst, src []*partWrapper, maxPartsToMerge int, maxOutBytes uint64) ([]*partWrapper, bool) {
|
2019-05-22 21:16:55 +00:00
|
|
|
if len(src) < 2 {
|
|
|
|
// There is no need in merging zero or one part :)
|
2020-09-29 18:47:40 +00:00
|
|
|
return dst, false
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
if maxPartsToMerge < 2 {
|
|
|
|
logger.Panicf("BUG: maxPartsToMerge cannot be smaller than 2; got %d", maxPartsToMerge)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter out too big parts.
|
2020-07-31 10:48:35 +00:00
|
|
|
// This should reduce N for O(N^2) algorithm below.
|
2021-07-02 14:24:14 +00:00
|
|
|
skippedBigParts := 0
|
2021-08-25 06:35:03 +00:00
|
|
|
maxInPartBytes := uint64(float64(maxOutBytes) / minMergeMultiplier)
|
2019-05-22 21:16:55 +00:00
|
|
|
tmp := make([]*partWrapper, 0, len(src))
|
|
|
|
for _, pw := range src {
|
2021-08-25 06:35:03 +00:00
|
|
|
if pw.p.size > maxInPartBytes {
|
2021-07-02 14:24:14 +00:00
|
|
|
skippedBigParts++
|
2019-05-22 21:16:55 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
tmp = append(tmp, pw)
|
|
|
|
}
|
|
|
|
src = tmp
|
2021-07-02 14:24:14 +00:00
|
|
|
needFreeSpace := skippedBigParts > 1
|
2019-05-22 21:16:55 +00:00
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
sortPartsForOptimalMerge(src)
|
2019-05-22 21:16:55 +00:00
|
|
|
|
2020-12-18 18:00:06 +00:00
|
|
|
maxSrcParts := maxPartsToMerge
|
2021-07-02 14:24:14 +00:00
|
|
|
if maxSrcParts > len(src) {
|
2020-12-18 18:00:06 +00:00
|
|
|
maxSrcParts = len(src)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2021-07-02 14:24:14 +00:00
|
|
|
minSrcParts := (maxSrcParts + 1) / 2
|
|
|
|
if minSrcParts < 2 {
|
|
|
|
minSrcParts = 2
|
|
|
|
}
|
2019-05-22 21:16:55 +00:00
|
|
|
|
2020-12-18 18:00:06 +00:00
|
|
|
// Exhaustive search for parts giving the lowest write amplification when merged.
|
2019-05-22 21:16:55 +00:00
|
|
|
var pws []*partWrapper
|
|
|
|
maxM := float64(0)
|
2020-12-18 18:00:06 +00:00
|
|
|
for i := minSrcParts; i <= maxSrcParts; i++ {
|
2019-05-22 21:16:55 +00:00
|
|
|
for j := 0; j <= len(src)-i; j++ {
|
2019-10-29 10:45:19 +00:00
|
|
|
a := src[j : j+i]
|
2021-08-25 06:35:03 +00:00
|
|
|
outSize := getPartsSize(a)
|
|
|
|
if outSize > maxOutBytes {
|
2021-07-27 09:02:52 +00:00
|
|
|
needFreeSpace = true
|
|
|
|
}
|
2021-08-25 06:35:03 +00:00
|
|
|
if a[0].p.size*uint64(len(a)) < a[len(a)-1].p.size {
|
|
|
|
// Do not merge parts with too big difference in size,
|
2020-12-18 18:00:06 +00:00
|
|
|
// since this results in unbalanced merges.
|
|
|
|
continue
|
|
|
|
}
|
2021-08-25 06:35:03 +00:00
|
|
|
if outSize > maxOutBytes {
|
|
|
|
// There is no need in verifying remaining parts with bigger sizes.
|
2019-10-29 10:45:19 +00:00
|
|
|
break
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2021-08-25 06:35:03 +00:00
|
|
|
m := float64(outSize) / float64(a[len(a)-1].p.size)
|
2019-05-22 21:16:55 +00:00
|
|
|
if m < maxM {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
maxM = m
|
2019-10-29 10:45:19 +00:00
|
|
|
pws = a
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-29 10:45:19 +00:00
|
|
|
minM := float64(maxPartsToMerge) / 2
|
2021-08-25 06:35:03 +00:00
|
|
|
if minM < minMergeMultiplier {
|
|
|
|
minM = minMergeMultiplier
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
if maxM < minM {
|
2021-08-25 06:35:03 +00:00
|
|
|
// There is no sense in merging parts with too small m,
|
|
|
|
// since this leads to high disk write IO.
|
2021-07-02 14:24:14 +00:00
|
|
|
return dst, needFreeSpace
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2020-09-29 18:47:40 +00:00
|
|
|
return append(dst, pws...), needFreeSpace
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 23:27:57 +00:00
|
|
|
func sortPartsForOptimalMerge(pws []*partWrapper) {
|
|
|
|
// Sort src parts by size and backwards timestamp.
|
|
|
|
// This should improve adjanced points' locality in the merged parts.
|
|
|
|
sort.Slice(pws, func(i, j int) bool {
|
|
|
|
a := pws[i].p
|
|
|
|
b := pws[j].p
|
|
|
|
if a.size == b.size {
|
|
|
|
return a.ph.MinTimestamp > b.ph.MinTimestamp
|
|
|
|
}
|
|
|
|
return a.size < b.size
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-25 06:35:03 +00:00
|
|
|
func getPartsSize(pws []*partWrapper) uint64 {
|
2020-12-18 21:14:35 +00:00
|
|
|
n := uint64(0)
|
|
|
|
for _, pw := range pws {
|
2021-08-25 06:35:03 +00:00
|
|
|
n += pw.p.size
|
2020-12-18 21:14:35 +00:00
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
func openParts(pathPrefix1, pathPrefix2, path string) ([]*partWrapper, error) {
|
2019-11-02 00:26:02 +00:00
|
|
|
// The path can be missing after restoring from backup, so create it if needed.
|
|
|
|
if err := fs.MkdirAllIfNotExist(path); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-13 12:28:01 +00:00
|
|
|
fs.MustRemoveTemporaryDirs(path)
|
2019-05-22 21:16:55 +00:00
|
|
|
d, err := os.Open(path)
|
|
|
|
if err != nil {
|
2022-12-05 23:27:57 +00:00
|
|
|
return nil, fmt.Errorf("cannot open partition directory: %w", err)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
defer fs.MustClose(d)
|
|
|
|
|
|
|
|
// Run remaining transactions and cleanup /txn and /tmp directories.
|
|
|
|
// Snapshots cannot be created yet, so use fakeSnapshotLock.
|
|
|
|
var fakeSnapshotLock sync.RWMutex
|
|
|
|
if err := runTransactions(&fakeSnapshotLock, pathPrefix1, pathPrefix2, path); err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("cannot run transactions from %q: %w", path, err)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
txnDir := path + "/txn"
|
2022-09-13 12:28:01 +00:00
|
|
|
fs.MustRemoveDirAtomic(txnDir)
|
2019-05-22 21:16:55 +00:00
|
|
|
tmpDir := path + "/tmp"
|
2022-09-13 12:28:01 +00:00
|
|
|
fs.MustRemoveDirAtomic(tmpDir)
|
2019-05-22 21:16:55 +00:00
|
|
|
if err := createPartitionDirs(path); err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("cannot create directories for partition %q: %w", path, err)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Open parts.
|
|
|
|
fis, err := d.Readdir(-1)
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("cannot read directory %q: %w", d.Name(), err)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
var pws []*partWrapper
|
|
|
|
for _, fi := range fis {
|
|
|
|
if !fs.IsDirOrSymlink(fi) {
|
|
|
|
// Skip non-directories.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fn := fi.Name()
|
2022-10-23 22:16:23 +00:00
|
|
|
if fn == "snapshots" {
|
2019-05-22 21:16:55 +00:00
|
|
|
// "snapshots" dir is skipped for backwards compatibility. Now it is unused.
|
2022-10-23 22:16:23 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if fn == "tmp" || fn == "txn" {
|
2019-05-22 21:16:55 +00:00
|
|
|
// Skip special dirs.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
partPath := path + "/" + fn
|
2021-04-22 09:58:53 +00:00
|
|
|
if fs.IsEmptyDir(partPath) {
|
|
|
|
// Remove empty directory, which can be left after unclean shutdown on NFS.
|
|
|
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1142
|
2022-09-13 12:28:01 +00:00
|
|
|
fs.MustRemoveDirAtomic(partPath)
|
2021-04-22 09:58:53 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-05-22 21:16:55 +00:00
|
|
|
startTime := time.Now()
|
|
|
|
p, err := openFilePart(partPath)
|
|
|
|
if err != nil {
|
|
|
|
mustCloseParts(pws)
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("cannot open part %q: %w", partPath, err)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2020-01-22 16:27:44 +00:00
|
|
|
logger.Infof("opened part %q in %.3f seconds", partPath, time.Since(startTime).Seconds())
|
2019-05-22 21:16:55 +00:00
|
|
|
|
|
|
|
pw := &partWrapper{
|
|
|
|
p: p,
|
|
|
|
refCount: 1,
|
|
|
|
}
|
|
|
|
pws = append(pws, pw)
|
|
|
|
}
|
|
|
|
|
|
|
|
return pws, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func mustCloseParts(pws []*partWrapper) {
|
|
|
|
for _, pw := range pws {
|
|
|
|
if pw.refCount != 1 {
|
|
|
|
logger.Panicf("BUG: unexpected refCount when closing part %q: %d; want 1", &pw.p.ph, pw.refCount)
|
|
|
|
}
|
|
|
|
pw.p.MustClose()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateSnapshotAt creates pt snapshot at the given smallPath and bigPath dirs.
|
|
|
|
//
|
|
|
|
// Snapshot is created using linux hard links, so it is usually created
|
|
|
|
// very quickly.
|
|
|
|
func (pt *partition) CreateSnapshotAt(smallPath, bigPath string) error {
|
|
|
|
logger.Infof("creating partition snapshot of %q and %q...", pt.smallPartsPath, pt.bigPartsPath)
|
|
|
|
startTime := time.Now()
|
|
|
|
|
|
|
|
// Flush inmemory data to disk.
|
2022-12-05 23:27:57 +00:00
|
|
|
pt.flushInmemoryRows()
|
2019-05-22 21:16:55 +00:00
|
|
|
|
|
|
|
// The snapshot must be created under the lock in order to prevent from
|
|
|
|
// concurrent modifications via runTransaction.
|
|
|
|
pt.snapshotLock.Lock()
|
|
|
|
defer pt.snapshotLock.Unlock()
|
|
|
|
|
|
|
|
if err := pt.createSnapshot(pt.smallPartsPath, smallPath); err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return fmt.Errorf("cannot create snapshot for %q: %w", pt.smallPartsPath, err)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
if err := pt.createSnapshot(pt.bigPartsPath, bigPath); err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return fmt.Errorf("cannot create snapshot for %q: %w", pt.bigPartsPath, err)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 16:27:44 +00:00
|
|
|
logger.Infof("created partition snapshot of %q and %q at %q and %q in %.3f seconds",
|
|
|
|
pt.smallPartsPath, pt.bigPartsPath, smallPath, bigPath, time.Since(startTime).Seconds())
|
2019-05-22 21:16:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pt *partition) createSnapshot(srcDir, dstDir string) error {
|
|
|
|
if err := fs.MkdirAllFailIfExist(dstDir); err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return fmt.Errorf("cannot create snapshot dir %q: %w", dstDir, err)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
d, err := os.Open(srcDir)
|
|
|
|
if err != nil {
|
2022-12-05 23:27:57 +00:00
|
|
|
return fmt.Errorf("cannot open partition difrectory: %w", err)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
defer fs.MustClose(d)
|
|
|
|
|
|
|
|
fis, err := d.Readdir(-1)
|
|
|
|
if err != nil {
|
2022-12-05 23:27:57 +00:00
|
|
|
return fmt.Errorf("cannot read partition directory: %w", err)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
for _, fi := range fis {
|
2022-10-24 00:02:14 +00:00
|
|
|
fn := fi.Name()
|
2019-05-22 21:16:55 +00:00
|
|
|
if !fs.IsDirOrSymlink(fi) {
|
2022-10-24 00:02:14 +00:00
|
|
|
if fn == "appliedRetention.txt" {
|
|
|
|
// Copy the appliedRetention.txt file to dstDir.
|
|
|
|
// This file can be created by VictoriaMetrics enterprise.
|
|
|
|
// See https://docs.victoriametrics.com/#retention-filters .
|
|
|
|
// Do not make hard link to this file, since it can be modified over time.
|
|
|
|
srcPath := srcDir + "/" + fn
|
|
|
|
dstPath := dstDir + "/" + fn
|
|
|
|
if err := fs.CopyFile(srcPath, dstPath); err != nil {
|
|
|
|
return fmt.Errorf("cannot copy %q to %q: %w", srcPath, dstPath, err)
|
|
|
|
}
|
|
|
|
}
|
2019-05-22 21:16:55 +00:00
|
|
|
// Skip non-directories.
|
|
|
|
continue
|
|
|
|
}
|
2023-02-24 20:38:42 +00:00
|
|
|
if fn == "tmp" || fn == "txn" || fs.IsScheduledForRemoval(fn) {
|
2019-05-22 21:16:55 +00:00
|
|
|
// Skip special dirs.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
srcPartPath := srcDir + "/" + fn
|
|
|
|
dstPartPath := dstDir + "/" + fn
|
|
|
|
if err := fs.HardLinkFiles(srcPartPath, dstPartPath); err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return fmt.Errorf("cannot create hard links from %q to %q: %w", srcPartPath, dstPartPath, err)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 20:13:04 +00:00
|
|
|
fs.MustSyncPath(dstDir)
|
|
|
|
fs.MustSyncPath(filepath.Dir(dstDir))
|
2019-05-22 21:16:55 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func runTransactions(txnLock *sync.RWMutex, pathPrefix1, pathPrefix2, path string) error {
|
2019-12-04 19:35:51 +00:00
|
|
|
// Wait until all the previous pending transaction deletions are finished.
|
|
|
|
pendingTxnDeletionsWG.Wait()
|
|
|
|
|
|
|
|
// Make sure all the current transaction deletions are finished before exiting.
|
|
|
|
defer pendingTxnDeletionsWG.Wait()
|
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
txnDir := path + "/txn"
|
|
|
|
d, err := os.Open(txnDir)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
2022-12-05 23:27:57 +00:00
|
|
|
return fmt.Errorf("cannot open transaction directory: %w", err)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
defer fs.MustClose(d)
|
|
|
|
|
|
|
|
fis, err := d.Readdir(-1)
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return fmt.Errorf("cannot read directory %q: %w", d.Name(), err)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sort transaction files by id.
|
|
|
|
sort.Slice(fis, func(i, j int) bool {
|
|
|
|
return fis[i].Name() < fis[j].Name()
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, fi := range fis {
|
2019-08-12 11:44:24 +00:00
|
|
|
fn := fi.Name()
|
|
|
|
if fs.IsTemporaryFileName(fn) {
|
|
|
|
// Skip temporary files, which could be left after unclean shutdown.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
txnPath := txnDir + "/" + fn
|
2019-05-22 21:16:55 +00:00
|
|
|
if err := runTransaction(txnLock, pathPrefix1, pathPrefix2, txnPath); err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return fmt.Errorf("cannot run transaction from %q: %w", txnPath, err)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func runTransaction(txnLock *sync.RWMutex, pathPrefix1, pathPrefix2, txnPath string) error {
|
2020-09-16 23:05:54 +00:00
|
|
|
// The transaction must run under read lock in order to provide
|
2019-05-22 21:16:55 +00:00
|
|
|
// consistent snapshots with partition.CreateSnapshot().
|
|
|
|
txnLock.RLock()
|
|
|
|
defer txnLock.RUnlock()
|
|
|
|
|
2022-08-21 20:51:13 +00:00
|
|
|
data, err := os.ReadFile(txnPath)
|
2019-05-22 21:16:55 +00:00
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return fmt.Errorf("cannot read transaction file: %w", err)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
if len(data) > 0 && data[len(data)-1] == '\n' {
|
|
|
|
data = data[:len(data)-1]
|
|
|
|
}
|
|
|
|
paths := strings.Split(string(data), "\n")
|
|
|
|
|
|
|
|
if len(paths) == 0 {
|
|
|
|
return fmt.Errorf("empty transaction")
|
|
|
|
}
|
|
|
|
rmPaths := paths[:len(paths)-1]
|
|
|
|
mvPaths := strings.Split(paths[len(paths)-1], " -> ")
|
|
|
|
if len(mvPaths) != 2 {
|
|
|
|
return fmt.Errorf("invalid last line in the transaction file: got %q; must contain `srcPath -> dstPath`", paths[len(paths)-1])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove old paths. It is OK if certain paths don't exist.
|
|
|
|
for _, path := range rmPaths {
|
|
|
|
path, err := validatePath(pathPrefix1, pathPrefix2, path)
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return fmt.Errorf("invalid path to remove: %w", err)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2022-09-13 12:28:01 +00:00
|
|
|
fs.MustRemoveDirAtomic(path)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Move the new part to new directory.
|
|
|
|
srcPath := mvPaths[0]
|
|
|
|
dstPath := mvPaths[1]
|
2022-12-05 23:27:57 +00:00
|
|
|
if len(srcPath) > 0 {
|
|
|
|
srcPath, err = validatePath(pathPrefix1, pathPrefix2, srcPath)
|
2019-05-22 21:16:55 +00:00
|
|
|
if err != nil {
|
2022-12-05 23:27:57 +00:00
|
|
|
return fmt.Errorf("invalid source path to rename: %w", err)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2022-12-05 23:27:57 +00:00
|
|
|
if len(dstPath) > 0 {
|
|
|
|
// Move srcPath to dstPath.
|
|
|
|
dstPath, err = validatePath(pathPrefix1, pathPrefix2, dstPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid destination path to rename: %w", err)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2022-12-05 23:27:57 +00:00
|
|
|
if fs.IsPathExist(srcPath) {
|
|
|
|
if err := os.Rename(srcPath, dstPath); err != nil {
|
|
|
|
return fmt.Errorf("cannot rename %q to %q: %w", srcPath, dstPath, err)
|
|
|
|
}
|
|
|
|
} else if !fs.IsPathExist(dstPath) {
|
|
|
|
// Emit info message for the expected condition after unclean shutdown on NFS disk.
|
|
|
|
// The dstPath part may be missing because it could be already merged into bigger part
|
|
|
|
// while old source parts for the current txn weren't still deleted due to NFS locks.
|
|
|
|
logger.Infof("cannot find both source and destination paths: %q -> %q; this may be the case after unclean shutdown "+
|
|
|
|
"(OOM, `kill -9`, hard reset) on NFS disk", srcPath, dstPath)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Just remove srcPath.
|
|
|
|
fs.MustRemoveDirAtomic(srcPath)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-25 09:45:47 +00:00
|
|
|
// Flush pathPrefix* directory metadata to the underying storage,
|
|
|
|
// so the moved files become visible there.
|
|
|
|
fs.MustSyncPath(pathPrefix1)
|
|
|
|
fs.MustSyncPath(pathPrefix2)
|
|
|
|
|
2019-12-04 19:35:51 +00:00
|
|
|
pendingTxnDeletionsWG.Add(1)
|
2019-12-02 19:34:35 +00:00
|
|
|
go func() {
|
2019-12-04 19:35:51 +00:00
|
|
|
defer pendingTxnDeletionsWG.Done()
|
2020-12-24 14:50:55 +00:00
|
|
|
|
2020-12-25 09:45:47 +00:00
|
|
|
// There is no need in calling fs.MustSyncPath for pathPrefix* after parts' removal,
|
2022-09-13 12:28:01 +00:00
|
|
|
// since it is already called by fs.MustRemoveDirAtomic.
|
2020-12-25 09:45:47 +00:00
|
|
|
|
2019-12-02 19:34:35 +00:00
|
|
|
if err := os.Remove(txnPath); err != nil {
|
|
|
|
logger.Errorf("cannot remove transaction file %q: %s", txnPath, err)
|
|
|
|
}
|
|
|
|
}()
|
2019-05-22 21:16:55 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-04 19:35:51 +00:00
|
|
|
var pendingTxnDeletionsWG syncwg.WaitGroup
|
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
func validatePath(pathPrefix1, pathPrefix2, path string) (string, error) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
pathPrefix1, err = filepath.Abs(pathPrefix1)
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return path, fmt.Errorf("cannot determine absolute path for pathPrefix1=%q: %w", pathPrefix1, err)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
pathPrefix2, err = filepath.Abs(pathPrefix2)
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return path, fmt.Errorf("cannot determine absolute path for pathPrefix2=%q: %w", pathPrefix2, err)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
path, err = filepath.Abs(path)
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return path, fmt.Errorf("cannot determine absolute path for %q: %w", path, err)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
if !strings.HasPrefix(path, pathPrefix1+"/") && !strings.HasPrefix(path, pathPrefix2+"/") {
|
|
|
|
return path, fmt.Errorf("invalid path %q; must start with either %q or %q", path, pathPrefix1+"/", pathPrefix2+"/")
|
|
|
|
}
|
|
|
|
return path, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createPartitionDirs(path string) error {
|
|
|
|
path = filepath.Clean(path)
|
|
|
|
txnPath := path + "/txn"
|
|
|
|
if err := fs.MkdirAllFailIfExist(txnPath); err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return fmt.Errorf("cannot create txn directory %q: %w", txnPath, err)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
tmpPath := path + "/tmp"
|
|
|
|
if err := fs.MkdirAllFailIfExist(tmpPath); err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return fmt.Errorf("cannot create tmp directory %q: %w", tmpPath, err)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2019-06-11 20:13:04 +00:00
|
|
|
fs.MustSyncPath(path)
|
2019-05-22 21:16:55 +00:00
|
|
|
return nil
|
|
|
|
}
|