2021-06-08 19:25:37 +08:00
|
|
|
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed 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.
|
|
|
|
|
2021-06-22 10:42:07 +08:00
|
|
|
package datacoord
|
2021-05-21 18:30:41 +08:00
|
|
|
|
|
|
|
import (
|
2021-06-08 19:25:37 +08:00
|
|
|
"sort"
|
2021-08-19 14:08:10 +08:00
|
|
|
"time"
|
2021-06-08 19:25:37 +08:00
|
|
|
|
2021-06-24 14:20:10 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/commonpb"
|
2021-05-21 18:30:41 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/schemapb"
|
2021-08-20 15:42:12 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/tsoutil"
|
2021-05-21 18:30:41 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/typeutil"
|
|
|
|
)
|
|
|
|
|
2021-07-12 17:24:25 +08:00
|
|
|
type calUpperLimitPolicy func(schema *schemapb.CollectionSchema) (int, error)
|
2021-05-21 18:30:41 +08:00
|
|
|
|
2021-07-12 17:24:25 +08:00
|
|
|
func calBySchemaPolicy(schema *schemapb.CollectionSchema) (int, error) {
|
2021-05-21 18:30:41 +08:00
|
|
|
sizePerRecord, err := typeutil.EstimateSizePerRecord(schema)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2021-06-22 16:18:21 +08:00
|
|
|
threshold := Params.SegmentMaxSize * 1024 * 1024
|
2021-05-21 18:30:41 +08:00
|
|
|
return int(threshold / float64(sizePerRecord)), nil
|
|
|
|
}
|
|
|
|
|
2021-07-23 21:58:33 +08:00
|
|
|
type AllocatePolicy func(segments []*SegmentInfo, count int64,
|
|
|
|
maxCountPerSegment int64) ([]*Allocation, []*Allocation)
|
2021-05-21 18:30:41 +08:00
|
|
|
|
2021-07-23 21:58:33 +08:00
|
|
|
func AllocatePolicyV1(segments []*SegmentInfo, count int64,
|
|
|
|
maxCountPerSegment int64) ([]*Allocation, []*Allocation) {
|
|
|
|
newSegmentAllocations := make([]*Allocation, 0)
|
|
|
|
existedSegmentAllocations := make([]*Allocation, 0)
|
|
|
|
// create new segment if count >= max num
|
|
|
|
for count >= maxCountPerSegment {
|
|
|
|
allocation := &Allocation{
|
|
|
|
NumOfRows: maxCountPerSegment,
|
|
|
|
}
|
|
|
|
newSegmentAllocations = append(newSegmentAllocations, allocation)
|
|
|
|
count -= maxCountPerSegment
|
|
|
|
}
|
|
|
|
|
|
|
|
// allocate space for remaining count
|
|
|
|
if count == 0 {
|
|
|
|
return newSegmentAllocations, existedSegmentAllocations
|
|
|
|
}
|
|
|
|
for _, segment := range segments {
|
|
|
|
var allocSize int64
|
|
|
|
for _, allocation := range segment.allocations {
|
|
|
|
allocSize += allocation.NumOfRows
|
|
|
|
}
|
|
|
|
free := segment.GetMaxRowNum() - segment.GetNumOfRows() - allocSize
|
|
|
|
if free < count {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
allocation := &Allocation{
|
|
|
|
SegmentID: segment.GetID(),
|
|
|
|
NumOfRows: count,
|
|
|
|
}
|
|
|
|
existedSegmentAllocations = append(existedSegmentAllocations, allocation)
|
|
|
|
return newSegmentAllocations, existedSegmentAllocations
|
|
|
|
}
|
|
|
|
|
|
|
|
// allocate new segment for remaining count
|
|
|
|
allocation := &Allocation{
|
|
|
|
NumOfRows: count,
|
2021-07-12 17:24:25 +08:00
|
|
|
}
|
2021-07-23 21:58:33 +08:00
|
|
|
newSegmentAllocations = append(newSegmentAllocations, allocation)
|
|
|
|
return newSegmentAllocations, existedSegmentAllocations
|
2021-05-21 18:30:41 +08:00
|
|
|
}
|
|
|
|
|
2021-07-12 17:24:25 +08:00
|
|
|
type sealPolicy func(maxCount, writtenCount, allocatedCount int64) bool
|
2021-05-21 18:30:41 +08:00
|
|
|
|
2021-06-08 19:25:37 +08:00
|
|
|
// segmentSealPolicy seal policy applies to segment
|
2021-07-12 17:24:25 +08:00
|
|
|
type segmentSealPolicy func(segment *SegmentInfo, ts Timestamp) bool
|
2021-06-08 19:25:37 +08:00
|
|
|
|
|
|
|
// getSegmentCapacityPolicy get segmentSealPolicy with segment size factor policy
|
|
|
|
func getSegmentCapacityPolicy(sizeFactor float64) segmentSealPolicy {
|
2021-07-12 17:24:25 +08:00
|
|
|
return func(segment *SegmentInfo, ts Timestamp) bool {
|
2021-06-08 19:25:37 +08:00
|
|
|
var allocSize int64
|
2021-07-12 17:24:25 +08:00
|
|
|
for _, allocation := range segment.allocations {
|
2021-07-23 21:58:33 +08:00
|
|
|
allocSize += allocation.NumOfRows
|
2021-06-08 19:25:37 +08:00
|
|
|
}
|
2021-07-12 17:24:25 +08:00
|
|
|
return float64(segment.currRows) >= sizeFactor*float64(segment.GetMaxRowNum())
|
2021-06-08 19:25:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// getLastExpiresLifetimePolicy get segmentSealPolicy with lifetime limit compares ts - segment.lastExpireTime
|
2021-08-20 15:42:12 +08:00
|
|
|
func sealByLifetimePolicy(lifetime time.Duration) segmentSealPolicy {
|
2021-07-12 17:24:25 +08:00
|
|
|
return func(segment *SegmentInfo, ts Timestamp) bool {
|
2021-08-20 15:42:12 +08:00
|
|
|
pts, _ := tsoutil.ParseTS(ts)
|
|
|
|
epts, _ := tsoutil.ParseTS(segment.GetLastExpireTime())
|
|
|
|
d := pts.Sub(epts)
|
|
|
|
return d >= lifetime
|
2021-06-08 19:25:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-20 15:42:12 +08:00
|
|
|
// channelSealPolicy seal policy applies to channel
|
|
|
|
type channelSealPolicy func(string, []*SegmentInfo, Timestamp) []*SegmentInfo
|
|
|
|
|
2021-06-08 19:25:37 +08:00
|
|
|
// getChannelCapacityPolicy get channelSealPolicy with channel segment capacity policy
|
|
|
|
func getChannelOpenSegCapacityPolicy(limit int) channelSealPolicy {
|
2021-07-12 17:24:25 +08:00
|
|
|
return func(channel string, segs []*SegmentInfo, ts Timestamp) []*SegmentInfo {
|
2021-06-08 19:25:37 +08:00
|
|
|
if len(segs) <= limit {
|
2021-07-12 17:24:25 +08:00
|
|
|
return []*SegmentInfo{}
|
2021-06-08 19:25:37 +08:00
|
|
|
}
|
2021-06-24 14:20:10 +08:00
|
|
|
sortSegmentsByLastExpires(segs)
|
2021-06-08 19:25:37 +08:00
|
|
|
offLen := len(segs) - limit
|
|
|
|
return segs[0:offLen]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sortSegStatusByLastExpires sort segmentStatus with lastExpireTime ascending order
|
2021-07-12 17:24:25 +08:00
|
|
|
func sortSegmentsByLastExpires(segs []*SegmentInfo) {
|
2021-06-08 19:25:37 +08:00
|
|
|
sort.Slice(segs, func(i, j int) bool {
|
2021-06-24 14:20:10 +08:00
|
|
|
return segs[i].LastExpireTime < segs[j].LastExpireTime
|
2021-06-08 19:25:37 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-07-12 17:24:25 +08:00
|
|
|
type flushPolicy func(segment *SegmentInfo, t Timestamp) bool
|
2021-05-21 18:30:41 +08:00
|
|
|
|
2021-08-19 14:08:10 +08:00
|
|
|
const flushInterval = 2 * time.Second
|
|
|
|
|
2021-07-12 17:24:25 +08:00
|
|
|
func flushPolicyV1(segment *SegmentInfo, t Timestamp) bool {
|
2021-08-19 14:08:10 +08:00
|
|
|
return segment.GetState() == commonpb.SegmentState_Sealed &&
|
|
|
|
segment.GetLastExpireTime() <= t &&
|
|
|
|
time.Since(segment.lastFlushTime) >= flushInterval
|
2021-05-21 18:30:41 +08:00
|
|
|
}
|