2023-11-23 17:30:25 +08:00
|
|
|
package datacoord
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/samber/lo"
|
|
|
|
|
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/msgpb"
|
2024-02-01 20:39:03 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/paramtable"
|
2023-11-23 17:30:25 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// The LevelZeroSegments keeps the min group
|
|
|
|
type LevelZeroSegmentsView struct {
|
|
|
|
label *CompactionGroupLabel
|
|
|
|
segments []*SegmentView
|
|
|
|
earliestGrowingSegmentPos *msgpb.MsgPosition
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ CompactionView = (*LevelZeroSegmentsView)(nil)
|
|
|
|
|
|
|
|
func (v *LevelZeroSegmentsView) String() string {
|
|
|
|
l0strings := lo.Map(v.segments, func(v *SegmentView, _ int) string {
|
|
|
|
return v.LevelZeroString()
|
|
|
|
})
|
2024-06-20 17:46:01 +08:00
|
|
|
|
|
|
|
count := lo.SumBy(v.segments, func(v *SegmentView) int {
|
|
|
|
return v.DeltaRowCount
|
|
|
|
})
|
|
|
|
return fmt.Sprintf("L0SegCount=%d, DeltaRowCount=%d, label=<%s>, posT=<%v>, L0 segments=%v",
|
|
|
|
len(v.segments),
|
|
|
|
count,
|
2023-11-23 17:30:25 +08:00
|
|
|
v.label.String(),
|
|
|
|
v.earliestGrowingSegmentPos.GetTimestamp(),
|
|
|
|
l0strings)
|
|
|
|
}
|
|
|
|
|
2023-12-05 18:44:37 +08:00
|
|
|
func (v *LevelZeroSegmentsView) Append(segments ...*SegmentView) {
|
|
|
|
if v.segments == nil {
|
|
|
|
v.segments = segments
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
v.segments = append(v.segments, segments...)
|
|
|
|
}
|
|
|
|
|
2023-11-23 17:30:25 +08:00
|
|
|
func (v *LevelZeroSegmentsView) GetGroupLabel() *CompactionGroupLabel {
|
|
|
|
if v == nil {
|
|
|
|
return &CompactionGroupLabel{}
|
|
|
|
}
|
|
|
|
return v.label
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *LevelZeroSegmentsView) GetSegmentsView() []*SegmentView {
|
|
|
|
if v == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return v.segments
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *LevelZeroSegmentsView) Equal(others []*SegmentView) bool {
|
|
|
|
if len(v.segments) != len(others) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
IDSelector := func(v *SegmentView, _ int) int64 {
|
|
|
|
return v.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
diffLeft, diffRight := lo.Difference(lo.Map(others, IDSelector), lo.Map(v.segments, IDSelector))
|
|
|
|
|
|
|
|
diffCount := len(diffLeft) + len(diffRight)
|
|
|
|
return diffCount == 0
|
|
|
|
}
|
|
|
|
|
2024-03-05 16:37:00 +08:00
|
|
|
// ForceTrigger triggers all qualified LevelZeroSegments according to views
|
|
|
|
func (v *LevelZeroSegmentsView) ForceTrigger() (CompactionView, string) {
|
|
|
|
// Only choose segments with position less than the earliest growing segment position
|
|
|
|
validSegments := lo.Filter(v.segments, func(view *SegmentView, _ int) bool {
|
|
|
|
return view.dmlPos.GetTimestamp() < v.earliestGrowingSegmentPos.GetTimestamp()
|
|
|
|
})
|
|
|
|
|
|
|
|
targetViews, reason := v.forceTrigger(validSegments)
|
|
|
|
if len(targetViews) > 0 {
|
|
|
|
return &LevelZeroSegmentsView{
|
|
|
|
label: v.label,
|
|
|
|
segments: targetViews,
|
|
|
|
earliestGrowingSegmentPos: v.earliestGrowingSegmentPos,
|
|
|
|
}, reason
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, ""
|
|
|
|
}
|
|
|
|
|
2023-11-23 17:30:25 +08:00
|
|
|
// Trigger triggers all qualified LevelZeroSegments according to views
|
2023-12-13 18:48:39 +08:00
|
|
|
func (v *LevelZeroSegmentsView) Trigger() (CompactionView, string) {
|
2023-11-23 17:30:25 +08:00
|
|
|
// Only choose segments with position less than the earliest growing segment position
|
|
|
|
validSegments := lo.Filter(v.segments, func(view *SegmentView, _ int) bool {
|
|
|
|
return view.dmlPos.GetTimestamp() < v.earliestGrowingSegmentPos.GetTimestamp()
|
|
|
|
})
|
|
|
|
|
2024-02-19 18:40:50 +08:00
|
|
|
targetViews, reason := v.minCountSizeTrigger(validSegments)
|
|
|
|
if len(targetViews) > 0 {
|
2024-02-01 20:39:03 +08:00
|
|
|
return &LevelZeroSegmentsView{
|
|
|
|
label: v.label,
|
|
|
|
segments: targetViews,
|
|
|
|
earliestGrowingSegmentPos: v.earliestGrowingSegmentPos,
|
|
|
|
}, reason
|
2023-11-23 17:30:25 +08:00
|
|
|
}
|
|
|
|
|
2024-02-01 20:39:03 +08:00
|
|
|
return nil, ""
|
|
|
|
}
|
|
|
|
|
2024-02-19 18:40:50 +08:00
|
|
|
// minCountSizeTrigger tries to trigger LevelZeroCompaction when segmentViews reaches minimum trigger conditions:
|
|
|
|
// 1. count >= minDeltaCount, OR
|
|
|
|
// 2. size >= minDeltaSize
|
|
|
|
func (v *LevelZeroSegmentsView) minCountSizeTrigger(segments []*SegmentView) (picked []*SegmentView, reason string) {
|
|
|
|
var (
|
|
|
|
minDeltaSize = paramtable.Get().DataCoordCfg.LevelZeroCompactionTriggerMinSize.GetAsFloat()
|
|
|
|
maxDeltaSize = paramtable.Get().DataCoordCfg.LevelZeroCompactionTriggerMaxSize.GetAsFloat()
|
|
|
|
minDeltaCount = paramtable.Get().DataCoordCfg.LevelZeroCompactionTriggerDeltalogMinNum.GetAsInt()
|
|
|
|
maxDeltaCount = paramtable.Get().DataCoordCfg.LevelZeroCompactionTriggerDeltalogMaxNum.GetAsInt()
|
|
|
|
)
|
2024-02-01 20:39:03 +08:00
|
|
|
|
2024-06-20 17:46:01 +08:00
|
|
|
pickedSize := float64(0)
|
|
|
|
pickedCount := 0
|
2024-02-19 18:40:50 +08:00
|
|
|
|
|
|
|
// count >= minDeltaCount
|
|
|
|
if lo.SumBy(segments, func(view *SegmentView) int { return view.DeltalogCount }) >= minDeltaCount {
|
2024-06-20 17:46:01 +08:00
|
|
|
picked, pickedSize, pickedCount = pickByMaxCountSize(segments, maxDeltaSize, maxDeltaCount)
|
|
|
|
reason = fmt.Sprintf("level zero segments count reaches minForceTriggerCountLimit=%d, pickedSize=%.2fB, pickedCount=%d", minDeltaCount, pickedSize, pickedCount)
|
2024-02-19 18:40:50 +08:00
|
|
|
return
|
2023-12-13 18:48:39 +08:00
|
|
|
}
|
|
|
|
|
2024-02-19 18:40:50 +08:00
|
|
|
// size >= minDeltaSize
|
|
|
|
if lo.SumBy(segments, func(view *SegmentView) float64 { return view.DeltaSize }) >= minDeltaSize {
|
2024-06-20 17:46:01 +08:00
|
|
|
picked, pickedSize, pickedCount = pickByMaxCountSize(segments, maxDeltaSize, maxDeltaCount)
|
|
|
|
reason = fmt.Sprintf("level zero segments size reaches minForceTriggerSizeLimit=%.2fB, pickedSize=%.2fB, pickedCount=%d", minDeltaSize, pickedSize, pickedCount)
|
2024-02-19 18:40:50 +08:00
|
|
|
return
|
2024-02-01 20:39:03 +08:00
|
|
|
}
|
|
|
|
|
2024-02-19 18:40:50 +08:00
|
|
|
return
|
2024-02-01 20:39:03 +08:00
|
|
|
}
|
|
|
|
|
2024-02-19 18:40:50 +08:00
|
|
|
// forceTrigger tries to trigger LevelZeroCompaction even when segmentsViews don't meet the minimum condition,
|
|
|
|
// the picked plan is still satisfied with the maximum condition
|
|
|
|
func (v *LevelZeroSegmentsView) forceTrigger(segments []*SegmentView) (picked []*SegmentView, reason string) {
|
|
|
|
var (
|
|
|
|
maxDeltaSize = paramtable.Get().DataCoordCfg.LevelZeroCompactionTriggerMaxSize.GetAsFloat()
|
|
|
|
maxDeltaCount = paramtable.Get().DataCoordCfg.LevelZeroCompactionTriggerDeltalogMaxNum.GetAsInt()
|
|
|
|
)
|
|
|
|
|
2024-06-20 17:46:01 +08:00
|
|
|
picked, pickedSize, pickedCount := pickByMaxCountSize(segments, maxDeltaSize, maxDeltaCount)
|
|
|
|
reason = fmt.Sprintf("level zero views force to trigger, pickedSize=%.2fB, pickedCount=%d", pickedSize, pickedCount)
|
|
|
|
return picked, reason
|
2024-02-19 18:40:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// pickByMaxCountSize picks segments that count <= maxCount or size <= maxSize
|
2024-06-20 17:46:01 +08:00
|
|
|
func pickByMaxCountSize(segments []*SegmentView, maxSize float64, maxCount int) (picked []*SegmentView, pickedSize float64, pickedCount int) {
|
2024-02-01 20:39:03 +08:00
|
|
|
idx := 0
|
|
|
|
for _, view := range segments {
|
2024-06-20 17:46:01 +08:00
|
|
|
targetCount := view.DeltalogCount + pickedCount
|
|
|
|
targetSize := view.DeltaSize + pickedSize
|
2024-02-19 18:40:50 +08:00
|
|
|
|
2024-06-20 17:46:01 +08:00
|
|
|
if (pickedCount != 0 && pickedSize != float64(0)) && (targetSize > maxSize || targetCount > maxCount) {
|
2024-02-01 20:39:03 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2024-06-20 17:46:01 +08:00
|
|
|
pickedCount = targetCount
|
|
|
|
pickedSize = targetSize
|
2024-02-19 18:40:50 +08:00
|
|
|
idx += 1
|
2023-12-13 18:48:39 +08:00
|
|
|
}
|
2024-06-20 17:46:01 +08:00
|
|
|
return segments[:idx], pickedSize, pickedCount
|
2023-11-23 17:30:25 +08:00
|
|
|
}
|