2022-10-11 11:39:22 +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.
|
|
|
|
|
2022-09-15 18:48:32 +08:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
2024-04-24 16:53:24 +08:00
|
|
|
"github.com/samber/lo"
|
2023-04-06 19:14:32 +08:00
|
|
|
|
2022-09-15 18:48:32 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/datapb"
|
2023-07-11 11:22:29 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/querypb"
|
2024-04-24 16:53:24 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/typeutil"
|
2022-09-15 18:48:32 +08:00
|
|
|
)
|
|
|
|
|
2024-04-25 11:13:25 +08:00
|
|
|
type segDistCriterion struct {
|
|
|
|
nodes []int64
|
|
|
|
collectionID int64
|
|
|
|
channel string
|
|
|
|
hasOtherFilter bool
|
|
|
|
}
|
|
|
|
|
2024-04-24 16:53:24 +08:00
|
|
|
type SegmentDistFilter interface {
|
|
|
|
Match(s *Segment) bool
|
2024-04-25 11:13:25 +08:00
|
|
|
AddFilter(*segDistCriterion)
|
2024-04-24 16:53:24 +08:00
|
|
|
}
|
2024-03-08 16:29:01 +08:00
|
|
|
|
2024-04-24 16:53:24 +08:00
|
|
|
type SegmentDistFilterFunc func(s *Segment) bool
|
|
|
|
|
|
|
|
func (f SegmentDistFilterFunc) Match(s *Segment) bool {
|
|
|
|
return f(s)
|
|
|
|
}
|
|
|
|
|
2024-04-25 11:13:25 +08:00
|
|
|
func (f SegmentDistFilterFunc) AddFilter(filter *segDistCriterion) {
|
|
|
|
filter.hasOtherFilter = true
|
2024-04-24 16:53:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type ReplicaSegDistFilter struct {
|
|
|
|
*Replica
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *ReplicaSegDistFilter) Match(s *Segment) bool {
|
|
|
|
return f.GetCollectionID() == s.GetCollectionID() && f.Contains(s.Node)
|
|
|
|
}
|
|
|
|
|
2024-04-25 11:13:25 +08:00
|
|
|
func (f ReplicaSegDistFilter) AddFilter(filter *segDistCriterion) {
|
|
|
|
filter.nodes = f.GetNodes()
|
|
|
|
filter.collectionID = f.GetCollectionID()
|
2024-03-08 16:29:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func WithReplica(replica *Replica) SegmentDistFilter {
|
2024-04-24 16:53:24 +08:00
|
|
|
return &ReplicaSegDistFilter{
|
|
|
|
Replica: replica,
|
2024-03-08 16:29:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-24 16:53:24 +08:00
|
|
|
type NodeSegDistFilter int64
|
|
|
|
|
|
|
|
func (f NodeSegDistFilter) Match(s *Segment) bool {
|
|
|
|
return s.Node == int64(f)
|
|
|
|
}
|
|
|
|
|
2024-04-25 11:13:25 +08:00
|
|
|
func (f NodeSegDistFilter) AddFilter(filter *segDistCriterion) {
|
|
|
|
filter.nodes = []int64{int64(f)}
|
2024-04-24 16:53:24 +08:00
|
|
|
}
|
|
|
|
|
2024-03-08 16:29:01 +08:00
|
|
|
func WithNodeID(nodeID int64) SegmentDistFilter {
|
2024-04-24 16:53:24 +08:00
|
|
|
return NodeSegDistFilter(nodeID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithSegmentID(segmentID int64) SegmentDistFilter {
|
|
|
|
return SegmentDistFilterFunc(func(s *Segment) bool {
|
|
|
|
return s.GetID() == segmentID
|
|
|
|
})
|
2024-03-08 16:29:01 +08:00
|
|
|
}
|
|
|
|
|
2024-04-25 11:13:25 +08:00
|
|
|
type CollectionSegDistFilter int64
|
|
|
|
|
|
|
|
func (f CollectionSegDistFilter) Match(s *Segment) bool {
|
|
|
|
return s.GetCollectionID() == int64(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f CollectionSegDistFilter) AddFilter(filter *segDistCriterion) {
|
|
|
|
filter.collectionID = int64(f)
|
|
|
|
}
|
|
|
|
|
2024-04-24 16:53:24 +08:00
|
|
|
func WithCollectionID(collectionID typeutil.UniqueID) SegmentDistFilter {
|
2024-04-25 11:13:25 +08:00
|
|
|
return CollectionSegDistFilter(collectionID)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ChannelSegDistFilter string
|
|
|
|
|
|
|
|
func (f ChannelSegDistFilter) Match(s *Segment) bool {
|
|
|
|
return s.GetInsertChannel() == string(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f ChannelSegDistFilter) AddFilter(filter *segDistCriterion) {
|
|
|
|
filter.channel = string(f)
|
2024-03-08 16:29:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func WithChannel(channelName string) SegmentDistFilter {
|
2024-04-25 11:13:25 +08:00
|
|
|
return ChannelSegDistFilter(channelName)
|
2024-03-08 16:29:01 +08:00
|
|
|
}
|
|
|
|
|
2022-09-15 18:48:32 +08:00
|
|
|
type Segment struct {
|
|
|
|
*datapb.SegmentInfo
|
2023-07-11 11:22:29 +08:00
|
|
|
Node int64 // Node the segment is in
|
|
|
|
Version int64 // Version is the timestamp of loading segment
|
|
|
|
LastDeltaTimestamp uint64 // The timestamp of the last delta record
|
|
|
|
IndexInfo map[int64]*querypb.FieldIndexInfo // index info of loaded segment
|
2022-09-15 18:48:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func SegmentFromInfo(info *datapb.SegmentInfo) *Segment {
|
|
|
|
return &Segment{
|
|
|
|
SegmentInfo: info,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (segment *Segment) Clone() *Segment {
|
|
|
|
return &Segment{
|
|
|
|
SegmentInfo: proto.Clone(segment.SegmentInfo).(*datapb.SegmentInfo),
|
|
|
|
Node: segment.Node,
|
|
|
|
Version: segment.Version,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type SegmentDistManager struct {
|
|
|
|
rwmutex sync.RWMutex
|
|
|
|
|
|
|
|
// nodeID -> []*Segment
|
2024-04-25 11:13:25 +08:00
|
|
|
segments map[typeutil.UniqueID]nodeSegments
|
|
|
|
}
|
|
|
|
|
|
|
|
type nodeSegments struct {
|
|
|
|
segments []*Segment
|
|
|
|
collSegments map[int64][]*Segment
|
|
|
|
channelSegments map[string][]*Segment
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s nodeSegments) Filter(criterion *segDistCriterion, filter func(*Segment) bool) []*Segment {
|
|
|
|
var segments []*Segment
|
|
|
|
switch {
|
|
|
|
case criterion.channel != "":
|
|
|
|
segments = s.channelSegments[criterion.channel]
|
|
|
|
case criterion.collectionID != 0:
|
|
|
|
segments = s.collSegments[criterion.collectionID]
|
|
|
|
default:
|
|
|
|
segments = s.segments
|
|
|
|
}
|
|
|
|
if criterion.hasOtherFilter {
|
|
|
|
segments = lo.Filter(segments, func(segment *Segment, _ int) bool {
|
|
|
|
return filter(segment)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return segments
|
|
|
|
}
|
|
|
|
|
|
|
|
func composeNodeSegments(segments []*Segment) nodeSegments {
|
|
|
|
return nodeSegments{
|
|
|
|
segments: segments,
|
|
|
|
collSegments: lo.GroupBy(segments, func(segment *Segment) int64 { return segment.GetCollectionID() }),
|
|
|
|
channelSegments: lo.GroupBy(segments, func(segment *Segment) string { return segment.GetInsertChannel() }),
|
|
|
|
}
|
2022-09-15 18:48:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewSegmentDistManager() *SegmentDistManager {
|
|
|
|
return &SegmentDistManager{
|
2024-04-25 11:13:25 +08:00
|
|
|
segments: make(map[typeutil.UniqueID]nodeSegments),
|
2022-09-15 18:48:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-24 16:53:24 +08:00
|
|
|
func (m *SegmentDistManager) Update(nodeID typeutil.UniqueID, segments ...*Segment) {
|
2022-09-15 18:48:32 +08:00
|
|
|
m.rwmutex.Lock()
|
|
|
|
defer m.rwmutex.Unlock()
|
|
|
|
|
|
|
|
for _, segment := range segments {
|
|
|
|
segment.Node = nodeID
|
|
|
|
}
|
2024-04-25 11:13:25 +08:00
|
|
|
m.segments[nodeID] = composeNodeSegments(segments)
|
2022-09-15 18:48:32 +08:00
|
|
|
}
|
|
|
|
|
2024-03-08 16:29:01 +08:00
|
|
|
// GetByFilter return segment list which match all given filters
|
|
|
|
func (m *SegmentDistManager) GetByFilter(filters ...SegmentDistFilter) []*Segment {
|
2022-09-15 18:48:32 +08:00
|
|
|
m.rwmutex.RLock()
|
|
|
|
defer m.rwmutex.RUnlock()
|
|
|
|
|
2024-04-25 11:13:25 +08:00
|
|
|
criterion := &segDistCriterion{}
|
2024-04-24 16:53:24 +08:00
|
|
|
for _, filter := range filters {
|
2024-04-25 11:13:25 +08:00
|
|
|
filter.AddFilter(criterion)
|
2024-04-24 16:53:24 +08:00
|
|
|
}
|
|
|
|
|
2024-04-02 10:23:14 +08:00
|
|
|
mergedFilters := func(s *Segment) bool {
|
|
|
|
for _, f := range filters {
|
2024-04-24 16:53:24 +08:00
|
|
|
if f != nil && !f.Match(s) {
|
2024-04-02 10:23:14 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-04-25 11:13:25 +08:00
|
|
|
var candidates []nodeSegments
|
|
|
|
if criterion.nodes != nil {
|
|
|
|
candidates = lo.Map(criterion.nodes, func(nodeID int64, _ int) nodeSegments {
|
2024-04-24 16:53:24 +08:00
|
|
|
return m.segments[nodeID]
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
candidates = lo.Values(m.segments)
|
|
|
|
}
|
|
|
|
|
2024-04-25 11:13:25 +08:00
|
|
|
var ret []*Segment
|
|
|
|
for _, nodeSegments := range candidates {
|
|
|
|
ret = append(ret, nodeSegments.Filter(criterion, mergedFilters)...)
|
2022-09-23 15:16:51 +08:00
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|