2023-03-27 00:42:00 +08:00
|
|
|
// Licensed to the LF AI & Data foundation under one
|
|
|
|
// or more contributor license agreements. See the NOTICE file
|
|
|
|
// distributed with this work for additional information
|
|
|
|
// regarding copyright ownership. The ASF licenses this file
|
|
|
|
// to you under the Apache License, Version 2.0 (the
|
|
|
|
// "License"); you may not use this file except in compliance
|
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package segments
|
|
|
|
|
|
|
|
/*
|
|
|
|
#cgo pkg-config: milvus_segcore
|
|
|
|
|
|
|
|
#include "segcore/collection_c.h"
|
|
|
|
#include "segcore/segment_c.h"
|
|
|
|
*/
|
|
|
|
import "C"
|
2023-04-06 19:14:32 +08:00
|
|
|
|
2023-03-27 00:42:00 +08:00
|
|
|
import (
|
2023-08-29 15:46:27 +08:00
|
|
|
"context"
|
2023-03-27 00:42:00 +08:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
2023-09-21 09:45:27 +08:00
|
|
|
"go.uber.org/zap"
|
2024-02-27 20:58:40 +08:00
|
|
|
"golang.org/x/sync/singleflight"
|
2023-09-21 09:45:27 +08:00
|
|
|
|
2023-11-07 01:44:18 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/datapb"
|
2023-03-27 00:42:00 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/querypb"
|
2023-07-25 17:23:01 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/eventlog"
|
2023-08-11 11:21:32 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/log"
|
2023-04-06 19:14:32 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/metrics"
|
2024-02-27 20:58:40 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/cache"
|
2023-08-16 18:38:17 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/merr"
|
2023-04-06 19:14:32 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/paramtable"
|
|
|
|
. "github.com/milvus-io/milvus/pkg/util/typeutil"
|
2023-03-27 00:42:00 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type SegmentFilter func(segment Segment) bool
|
|
|
|
|
2023-10-15 16:10:04 +08:00
|
|
|
func WithSkipEmpty() SegmentFilter {
|
|
|
|
return func(segment Segment) bool {
|
|
|
|
return segment.InsertCount() > 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-27 00:42:00 +08:00
|
|
|
func WithPartition(partitionID UniqueID) SegmentFilter {
|
|
|
|
return func(segment Segment) bool {
|
|
|
|
return segment.Partition() == partitionID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithChannel(channel string) SegmentFilter {
|
|
|
|
return func(segment Segment) bool {
|
|
|
|
return segment.Shard() == channel
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithType(typ SegmentType) SegmentFilter {
|
|
|
|
return func(segment Segment) bool {
|
|
|
|
return segment.Type() == typ
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithID(id int64) SegmentFilter {
|
|
|
|
return func(segment Segment) bool {
|
|
|
|
return segment.ID() == id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-21 18:24:22 +08:00
|
|
|
func WithLevel(level datapb.SegmentLevel) SegmentFilter {
|
|
|
|
return func(segment Segment) bool {
|
|
|
|
return segment.Level() == level
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-29 15:46:27 +08:00
|
|
|
type SegmentAction func(segment Segment) bool
|
|
|
|
|
|
|
|
func IncreaseVersion(version int64) SegmentAction {
|
|
|
|
return func(segment Segment) bool {
|
|
|
|
log := log.Ctx(context.Background()).With(
|
|
|
|
zap.Int64("segmentID", segment.ID()),
|
|
|
|
zap.String("type", segment.Type().String()),
|
|
|
|
zap.Int64("segmentVersion", segment.Version()),
|
|
|
|
zap.Int64("updateVersion", version),
|
|
|
|
)
|
|
|
|
for oldVersion := segment.Version(); oldVersion < version; {
|
|
|
|
if segment.CASVersion(oldVersion, version) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Warn("segment version cannot go backwards, skip update")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-27 00:42:00 +08:00
|
|
|
type actionType int32
|
|
|
|
|
|
|
|
const (
|
|
|
|
removeAction actionType = iota
|
|
|
|
addAction
|
|
|
|
)
|
|
|
|
|
|
|
|
type Manager struct {
|
|
|
|
Collection CollectionManager
|
|
|
|
Segment SegmentManager
|
2024-02-27 20:58:40 +08:00
|
|
|
DiskCache cache.Cache[int64, Segment]
|
2023-03-27 00:42:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewManager() *Manager {
|
2024-02-27 20:58:40 +08:00
|
|
|
diskCap := paramtable.Get().QueryNodeCfg.DiskCapacityLimit.GetAsInt64()
|
|
|
|
segmentMaxSIze := paramtable.Get().DataCoordCfg.SegmentMaxSize.GetAsInt64()
|
|
|
|
cacheMaxItemNum := diskCap / segmentMaxSIze
|
|
|
|
|
|
|
|
segMgr := NewSegmentManager()
|
|
|
|
sf := singleflight.Group{}
|
2023-03-27 00:42:00 +08:00
|
|
|
return &Manager{
|
|
|
|
Collection: NewCollectionManager(),
|
2024-02-27 20:58:40 +08:00
|
|
|
Segment: segMgr,
|
|
|
|
DiskCache: cache.NewLRUCache[int64, Segment](
|
|
|
|
int32(cacheMaxItemNum),
|
|
|
|
func(key int64) (Segment, bool) {
|
|
|
|
log.Debug("cache missed segment", zap.Int64("segmentID", key))
|
|
|
|
segMgr.mu.RLock()
|
|
|
|
defer segMgr.mu.RUnlock()
|
|
|
|
|
|
|
|
segment, ok := segMgr.sealedSegments[key]
|
|
|
|
if !ok {
|
|
|
|
// the segment has been released, just ignore it
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
info := segment.LoadInfo()
|
|
|
|
_, err, _ := sf.Do(fmt.Sprint(segment.ID()), func() (interface{}, error) {
|
|
|
|
err := loadSealedSegmentFields(context.Background(), segment.(*LocalSegment), info.BinlogPaths, info.GetNumOfRows(), WithLoadStatus(LoadStatusMapped))
|
|
|
|
return nil, err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("cache sealed segment failed", zap.Error(err))
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return segment, true
|
|
|
|
},
|
|
|
|
func(key int64, segment Segment) {
|
|
|
|
log.Debug("evict segment from cache", zap.Int64("segmentID", key))
|
|
|
|
segment.Release(WithReleaseScope(ReleaseScopeData))
|
|
|
|
}),
|
2023-03-27 00:42:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type SegmentManager interface {
|
|
|
|
// Put puts the given segments in,
|
|
|
|
// and increases the ref count of the corresponding collection,
|
|
|
|
// dup segments will not increase the ref count
|
|
|
|
Put(segmentType SegmentType, segments ...Segment)
|
2023-11-07 01:44:18 +08:00
|
|
|
UpdateBy(action SegmentAction, filters ...SegmentFilter) int
|
2023-03-27 00:42:00 +08:00
|
|
|
Get(segmentID UniqueID) Segment
|
|
|
|
GetWithType(segmentID UniqueID, typ SegmentType) Segment
|
|
|
|
GetBy(filters ...SegmentFilter) []Segment
|
2023-08-16 18:38:17 +08:00
|
|
|
// Get segments and acquire the read locks
|
|
|
|
GetAndPinBy(filters ...SegmentFilter) ([]Segment, error)
|
|
|
|
GetAndPin(segments []int64, filters ...SegmentFilter) ([]Segment, error)
|
|
|
|
Unpin(segments []Segment)
|
2023-08-29 15:46:27 +08:00
|
|
|
|
2023-03-27 00:42:00 +08:00
|
|
|
GetSealed(segmentID UniqueID) Segment
|
|
|
|
GetGrowing(segmentID UniqueID) Segment
|
2023-06-30 17:28:23 +08:00
|
|
|
Empty() bool
|
|
|
|
|
2023-03-27 00:42:00 +08:00
|
|
|
// Remove removes the given segment,
|
|
|
|
// and decreases the ref count of the corresponding collection,
|
|
|
|
// will not decrease the ref count if the given segment not exists
|
2023-07-14 10:28:30 +08:00
|
|
|
Remove(segmentID UniqueID, scope querypb.DataScope) (int, int)
|
|
|
|
RemoveBy(filters ...SegmentFilter) (int, int)
|
2023-06-30 17:28:23 +08:00
|
|
|
Clear()
|
2023-03-27 00:42:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ SegmentManager = (*segmentManager)(nil)
|
|
|
|
|
|
|
|
// Manager manages all collections and segments
|
|
|
|
type segmentManager struct {
|
|
|
|
mu sync.RWMutex // guards all
|
|
|
|
|
|
|
|
growingSegments map[UniqueID]Segment
|
|
|
|
sealedSegments map[UniqueID]Segment
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSegmentManager() *segmentManager {
|
2024-02-27 20:58:40 +08:00
|
|
|
mgr := &segmentManager{
|
2023-03-27 00:42:00 +08:00
|
|
|
growingSegments: make(map[int64]Segment),
|
|
|
|
sealedSegments: make(map[int64]Segment),
|
|
|
|
}
|
2024-02-27 20:58:40 +08:00
|
|
|
return mgr
|
2023-03-27 00:42:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mgr *segmentManager) Put(segmentType SegmentType, segments ...Segment) {
|
2023-08-30 11:37:00 +08:00
|
|
|
var replacedSegment []Segment
|
2023-03-27 00:42:00 +08:00
|
|
|
mgr.mu.Lock()
|
|
|
|
defer mgr.mu.Unlock()
|
2023-09-19 10:05:22 +08:00
|
|
|
var targetMap map[int64]Segment
|
2023-03-27 00:42:00 +08:00
|
|
|
switch segmentType {
|
|
|
|
case SegmentTypeGrowing:
|
|
|
|
targetMap = mgr.growingSegments
|
|
|
|
case SegmentTypeSealed:
|
|
|
|
targetMap = mgr.sealedSegments
|
|
|
|
default:
|
|
|
|
panic("unexpected segment type")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, segment := range segments {
|
2023-08-11 11:21:32 +08:00
|
|
|
oldSegment, ok := targetMap[segment.ID()]
|
|
|
|
|
2023-08-30 11:37:00 +08:00
|
|
|
if ok {
|
|
|
|
if oldSegment.Version() >= segment.Version() {
|
|
|
|
log.Warn("Invalid segment distribution changed, skip it",
|
|
|
|
zap.Int64("segmentID", segment.ID()),
|
|
|
|
zap.Int64("oldVersion", oldSegment.Version()),
|
|
|
|
zap.Int64("newVersion", segment.Version()),
|
|
|
|
)
|
|
|
|
// delete redundant segment
|
2023-09-09 10:35:16 +08:00
|
|
|
segment.Release()
|
2023-08-30 11:37:00 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
replacedSegment = append(replacedSegment, oldSegment)
|
2023-03-27 00:42:00 +08:00
|
|
|
}
|
|
|
|
targetMap[segment.ID()] = segment
|
2023-08-11 11:21:32 +08:00
|
|
|
|
2023-08-30 11:37:00 +08:00
|
|
|
eventlog.Record(eventlog.NewRawEvt(eventlog.Level_Info, fmt.Sprintf("Segment %d[%d] loaded", segment.ID(), segment.Collection())))
|
|
|
|
metrics.QueryNodeNumSegments.WithLabelValues(
|
|
|
|
fmt.Sprint(paramtable.GetNodeID()),
|
|
|
|
fmt.Sprint(segment.Collection()),
|
|
|
|
fmt.Sprint(segment.Partition()),
|
|
|
|
segment.Type().String(),
|
|
|
|
fmt.Sprint(len(segment.Indexes())),
|
2023-12-07 14:34:35 +08:00
|
|
|
segment.Level().String(),
|
2023-08-30 11:37:00 +08:00
|
|
|
).Inc()
|
2023-03-27 00:42:00 +08:00
|
|
|
}
|
|
|
|
mgr.updateMetric()
|
2023-08-30 11:37:00 +08:00
|
|
|
|
|
|
|
// release replaced segment
|
|
|
|
if len(replacedSegment) > 0 {
|
|
|
|
go func() {
|
|
|
|
for _, segment := range replacedSegment {
|
2023-09-09 10:35:16 +08:00
|
|
|
remove(segment)
|
2023-08-30 11:37:00 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2023-03-27 00:42:00 +08:00
|
|
|
}
|
|
|
|
|
2023-11-07 01:44:18 +08:00
|
|
|
func (mgr *segmentManager) UpdateBy(action SegmentAction, filters ...SegmentFilter) int {
|
2023-08-29 15:46:27 +08:00
|
|
|
mgr.mu.RLock()
|
|
|
|
defer mgr.mu.RUnlock()
|
2023-08-11 11:21:32 +08:00
|
|
|
|
2023-08-29 15:46:27 +08:00
|
|
|
updated := 0
|
|
|
|
for _, segment := range mgr.growingSegments {
|
|
|
|
if filter(segment, filters...) {
|
|
|
|
if action(segment) {
|
|
|
|
updated++
|
|
|
|
}
|
|
|
|
}
|
2023-08-11 11:21:32 +08:00
|
|
|
}
|
|
|
|
|
2023-08-29 15:46:27 +08:00
|
|
|
for _, segment := range mgr.sealedSegments {
|
|
|
|
if filter(segment, filters...) {
|
|
|
|
if action(segment) {
|
|
|
|
updated++
|
|
|
|
}
|
|
|
|
}
|
2023-08-11 11:21:32 +08:00
|
|
|
}
|
2023-08-29 15:46:27 +08:00
|
|
|
return updated
|
2023-08-11 11:21:32 +08:00
|
|
|
}
|
|
|
|
|
2023-03-27 00:42:00 +08:00
|
|
|
func (mgr *segmentManager) Get(segmentID UniqueID) Segment {
|
|
|
|
mgr.mu.RLock()
|
|
|
|
defer mgr.mu.RUnlock()
|
|
|
|
|
|
|
|
if segment, ok := mgr.growingSegments[segmentID]; ok {
|
|
|
|
return segment
|
|
|
|
} else if segment, ok = mgr.sealedSegments[segmentID]; ok {
|
|
|
|
return segment
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mgr *segmentManager) GetWithType(segmentID UniqueID, typ SegmentType) Segment {
|
|
|
|
mgr.mu.RLock()
|
|
|
|
defer mgr.mu.RUnlock()
|
|
|
|
|
|
|
|
switch typ {
|
|
|
|
case SegmentTypeSealed:
|
|
|
|
return mgr.sealedSegments[segmentID]
|
|
|
|
case SegmentTypeGrowing:
|
|
|
|
return mgr.growingSegments[segmentID]
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mgr *segmentManager) GetBy(filters ...SegmentFilter) []Segment {
|
|
|
|
mgr.mu.RLock()
|
|
|
|
defer mgr.mu.RUnlock()
|
|
|
|
|
|
|
|
ret := make([]Segment, 0)
|
|
|
|
for _, segment := range mgr.growingSegments {
|
|
|
|
if filter(segment, filters...) {
|
|
|
|
ret = append(ret, segment)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, segment := range mgr.sealedSegments {
|
|
|
|
if filter(segment, filters...) {
|
|
|
|
ret = append(ret, segment)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2023-08-16 18:38:17 +08:00
|
|
|
func (mgr *segmentManager) GetAndPinBy(filters ...SegmentFilter) ([]Segment, error) {
|
|
|
|
mgr.mu.RLock()
|
|
|
|
defer mgr.mu.RUnlock()
|
|
|
|
|
|
|
|
ret := make([]Segment, 0)
|
|
|
|
var err error
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
for _, segment := range ret {
|
|
|
|
segment.RUnlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for _, segment := range mgr.growingSegments {
|
|
|
|
if filter(segment, filters...) {
|
|
|
|
err = segment.RLock()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ret = append(ret, segment)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, segment := range mgr.sealedSegments {
|
2023-11-21 18:24:22 +08:00
|
|
|
if segment.Level() != datapb.SegmentLevel_L0 && filter(segment, filters...) {
|
2023-08-16 18:38:17 +08:00
|
|
|
err = segment.RLock()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ret = append(ret, segment)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mgr *segmentManager) GetAndPin(segments []int64, filters ...SegmentFilter) ([]Segment, error) {
|
|
|
|
mgr.mu.RLock()
|
|
|
|
defer mgr.mu.RUnlock()
|
|
|
|
|
|
|
|
lockedSegments := make([]Segment, 0, len(segments))
|
|
|
|
var err error
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
for _, segment := range lockedSegments {
|
|
|
|
segment.RUnlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for _, id := range segments {
|
|
|
|
growing, growingExist := mgr.growingSegments[id]
|
|
|
|
sealed, sealedExist := mgr.sealedSegments[id]
|
|
|
|
|
2023-11-20 17:26:23 +08:00
|
|
|
// L0 Segment should not be queryable.
|
|
|
|
if sealedExist && sealed.Level() == datapb.SegmentLevel_L0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-08-16 18:38:17 +08:00
|
|
|
growingExist = growingExist && filter(growing, filters...)
|
|
|
|
sealedExist = sealedExist && filter(sealed, filters...)
|
|
|
|
|
|
|
|
if growingExist {
|
|
|
|
err = growing.RLock()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
lockedSegments = append(lockedSegments, growing)
|
|
|
|
}
|
|
|
|
if sealedExist {
|
|
|
|
err = sealed.RLock()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
lockedSegments = append(lockedSegments, sealed)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !growingExist && !sealedExist {
|
|
|
|
err = merr.WrapErrSegmentNotLoaded(id, "segment not found")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2024-02-27 20:58:40 +08:00
|
|
|
|
2023-08-16 18:38:17 +08:00
|
|
|
return lockedSegments, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mgr *segmentManager) Unpin(segments []Segment) {
|
|
|
|
for _, segment := range segments {
|
|
|
|
segment.RUnlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-27 00:42:00 +08:00
|
|
|
func filter(segment Segment, filters ...SegmentFilter) bool {
|
|
|
|
for _, filter := range filters {
|
|
|
|
if !filter(segment) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mgr *segmentManager) GetSealed(segmentID UniqueID) Segment {
|
|
|
|
mgr.mu.RLock()
|
|
|
|
defer mgr.mu.RUnlock()
|
|
|
|
|
|
|
|
if segment, ok := mgr.sealedSegments[segmentID]; ok {
|
|
|
|
return segment
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mgr *segmentManager) GetGrowing(segmentID UniqueID) Segment {
|
|
|
|
mgr.mu.RLock()
|
|
|
|
defer mgr.mu.RUnlock()
|
|
|
|
|
|
|
|
if segment, ok := mgr.growingSegments[segmentID]; ok {
|
|
|
|
return segment
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-30 17:28:23 +08:00
|
|
|
func (mgr *segmentManager) Empty() bool {
|
|
|
|
mgr.mu.RLock()
|
|
|
|
defer mgr.mu.RUnlock()
|
|
|
|
|
|
|
|
return len(mgr.growingSegments)+len(mgr.sealedSegments) == 0
|
|
|
|
}
|
|
|
|
|
2023-07-14 10:28:30 +08:00
|
|
|
// returns true if the segment exists,
|
|
|
|
// false otherwise
|
|
|
|
func (mgr *segmentManager) Remove(segmentID UniqueID, scope querypb.DataScope) (int, int) {
|
2023-03-27 00:42:00 +08:00
|
|
|
mgr.mu.Lock()
|
|
|
|
|
2023-07-14 10:28:30 +08:00
|
|
|
var removeGrowing, removeSealed int
|
2023-09-09 10:35:16 +08:00
|
|
|
var growing, sealed Segment
|
2023-03-27 00:42:00 +08:00
|
|
|
switch scope {
|
|
|
|
case querypb.DataScope_Streaming:
|
2023-08-18 15:28:18 +08:00
|
|
|
growing = mgr.removeSegmentWithType(SegmentTypeGrowing, segmentID)
|
|
|
|
if growing != nil {
|
2023-07-14 10:28:30 +08:00
|
|
|
removeGrowing = 1
|
|
|
|
}
|
2023-03-27 00:42:00 +08:00
|
|
|
|
|
|
|
case querypb.DataScope_Historical:
|
2023-08-18 15:28:18 +08:00
|
|
|
sealed = mgr.removeSegmentWithType(SegmentTypeSealed, segmentID)
|
|
|
|
if sealed != nil {
|
2023-07-14 10:28:30 +08:00
|
|
|
removeSealed = 1
|
|
|
|
}
|
2023-03-27 00:42:00 +08:00
|
|
|
|
|
|
|
case querypb.DataScope_All:
|
2023-08-18 15:28:18 +08:00
|
|
|
growing = mgr.removeSegmentWithType(SegmentTypeGrowing, segmentID)
|
|
|
|
if growing != nil {
|
2023-07-14 10:28:30 +08:00
|
|
|
removeGrowing = 1
|
|
|
|
}
|
2023-08-18 15:28:18 +08:00
|
|
|
|
|
|
|
sealed = mgr.removeSegmentWithType(SegmentTypeSealed, segmentID)
|
|
|
|
if sealed != nil {
|
2023-07-14 10:28:30 +08:00
|
|
|
removeSealed = 1
|
|
|
|
}
|
2023-03-27 00:42:00 +08:00
|
|
|
}
|
|
|
|
mgr.updateMetric()
|
2023-08-18 15:28:18 +08:00
|
|
|
mgr.mu.Unlock()
|
|
|
|
|
|
|
|
if growing != nil {
|
|
|
|
remove(growing)
|
|
|
|
}
|
|
|
|
|
|
|
|
if sealed != nil {
|
|
|
|
remove(sealed)
|
|
|
|
}
|
|
|
|
|
2023-07-14 10:28:30 +08:00
|
|
|
return removeGrowing, removeSealed
|
2023-03-27 00:42:00 +08:00
|
|
|
}
|
|
|
|
|
2023-09-09 10:35:16 +08:00
|
|
|
func (mgr *segmentManager) removeSegmentWithType(typ SegmentType, segmentID UniqueID) Segment {
|
2023-08-18 15:28:18 +08:00
|
|
|
switch typ {
|
|
|
|
case SegmentTypeGrowing:
|
|
|
|
s, ok := mgr.growingSegments[segmentID]
|
|
|
|
if ok {
|
|
|
|
delete(mgr.growingSegments, segmentID)
|
2023-09-09 10:35:16 +08:00
|
|
|
return s
|
2023-08-18 15:28:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case SegmentTypeSealed:
|
|
|
|
s, ok := mgr.sealedSegments[segmentID]
|
|
|
|
if ok {
|
|
|
|
delete(mgr.sealedSegments, segmentID)
|
2023-09-09 10:35:16 +08:00
|
|
|
return s
|
2023-08-18 15:28:18 +08:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-14 10:28:30 +08:00
|
|
|
func (mgr *segmentManager) RemoveBy(filters ...SegmentFilter) (int, int) {
|
2023-03-27 00:42:00 +08:00
|
|
|
mgr.mu.Lock()
|
|
|
|
|
2023-09-09 10:35:16 +08:00
|
|
|
var removeGrowing, removeSealed []Segment
|
2023-03-27 00:42:00 +08:00
|
|
|
for id, segment := range mgr.growingSegments {
|
2023-08-18 15:28:18 +08:00
|
|
|
if filter(segment, filters...) {
|
|
|
|
s := mgr.removeSegmentWithType(SegmentTypeGrowing, id)
|
|
|
|
if s != nil {
|
|
|
|
removeGrowing = append(removeGrowing, s)
|
|
|
|
}
|
2023-03-27 00:42:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for id, segment := range mgr.sealedSegments {
|
2023-08-18 15:28:18 +08:00
|
|
|
if filter(segment, filters...) {
|
|
|
|
s := mgr.removeSegmentWithType(SegmentTypeSealed, id)
|
|
|
|
if s != nil {
|
|
|
|
removeSealed = append(removeSealed, s)
|
|
|
|
}
|
2023-03-27 00:42:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
mgr.updateMetric()
|
2023-08-18 15:28:18 +08:00
|
|
|
mgr.mu.Unlock()
|
|
|
|
|
|
|
|
for _, s := range removeGrowing {
|
|
|
|
remove(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range removeSealed {
|
|
|
|
remove(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
return len(removeGrowing), len(removeSealed)
|
2023-03-27 00:42:00 +08:00
|
|
|
}
|
|
|
|
|
2023-06-30 17:28:23 +08:00
|
|
|
func (mgr *segmentManager) Clear() {
|
|
|
|
mgr.mu.Lock()
|
|
|
|
defer mgr.mu.Unlock()
|
|
|
|
|
2023-08-18 15:28:18 +08:00
|
|
|
for id, segment := range mgr.growingSegments {
|
|
|
|
delete(mgr.growingSegments, id)
|
2023-09-09 10:35:16 +08:00
|
|
|
remove(segment)
|
2023-06-30 17:28:23 +08:00
|
|
|
}
|
|
|
|
|
2023-08-18 15:28:18 +08:00
|
|
|
for id, segment := range mgr.sealedSegments {
|
|
|
|
delete(mgr.sealedSegments, id)
|
2023-09-09 10:35:16 +08:00
|
|
|
remove(segment)
|
2023-06-30 17:28:23 +08:00
|
|
|
}
|
|
|
|
mgr.updateMetric()
|
|
|
|
}
|
|
|
|
|
2023-03-27 00:42:00 +08:00
|
|
|
func (mgr *segmentManager) updateMetric() {
|
|
|
|
// update collection and partiation metric
|
2023-09-21 09:45:27 +08:00
|
|
|
collections, partiations := make(Set[int64]), make(Set[int64])
|
2023-03-27 00:42:00 +08:00
|
|
|
for _, seg := range mgr.growingSegments {
|
|
|
|
collections.Insert(seg.Collection())
|
|
|
|
partiations.Insert(seg.Partition())
|
|
|
|
}
|
|
|
|
for _, seg := range mgr.sealedSegments {
|
|
|
|
collections.Insert(seg.Collection())
|
|
|
|
partiations.Insert(seg.Partition())
|
|
|
|
}
|
|
|
|
metrics.QueryNodeNumCollections.WithLabelValues(fmt.Sprint(paramtable.GetNodeID())).Set(float64(collections.Len()))
|
|
|
|
metrics.QueryNodeNumPartitions.WithLabelValues(fmt.Sprint(paramtable.GetNodeID())).Set(float64(partiations.Len()))
|
|
|
|
}
|
|
|
|
|
2023-09-09 10:35:16 +08:00
|
|
|
func remove(segment Segment) bool {
|
|
|
|
segment.Release()
|
2023-03-27 00:42:00 +08:00
|
|
|
|
|
|
|
metrics.QueryNodeNumSegments.WithLabelValues(
|
|
|
|
fmt.Sprint(paramtable.GetNodeID()),
|
|
|
|
fmt.Sprint(segment.Collection()),
|
|
|
|
fmt.Sprint(segment.Partition()),
|
|
|
|
segment.Type().String(),
|
|
|
|
fmt.Sprint(len(segment.Indexes())),
|
2023-12-07 14:34:35 +08:00
|
|
|
segment.Level().String(),
|
2023-03-27 00:42:00 +08:00
|
|
|
).Dec()
|
2024-01-05 18:24:47 +08:00
|
|
|
|
2023-07-14 10:28:30 +08:00
|
|
|
return true
|
2023-03-27 00:42:00 +08:00
|
|
|
}
|