2021-10-15 18:09: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
|
2021-06-21 16:00:22 +08:00
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
2021-10-15 18:09:00 +08:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2021-06-21 16:00:22 +08:00
|
|
|
//
|
2021-10-15 18:09:00 +08:00
|
|
|
// 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-21 16:00:22 +08:00
|
|
|
|
|
|
|
package datanode
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-08-25 10:29:52 +08:00
|
|
|
"math"
|
2021-06-21 16:00:22 +08:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2021-08-25 10:29:52 +08:00
|
|
|
"github.com/bits-and-blooms/bloom/v3"
|
2021-10-25 18:49:20 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/common"
|
2021-10-14 10:24:33 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/kv"
|
|
|
|
miniokv "github.com/milvus-io/milvus/internal/kv/minio"
|
2021-06-21 16:00:22 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/datapb"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/internalpb"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/schemapb"
|
2021-10-14 10:24:33 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/storage"
|
2021-06-21 16:00:22 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/types"
|
|
|
|
)
|
|
|
|
|
2021-08-25 10:29:52 +08:00
|
|
|
const (
|
|
|
|
// TODO silverxia maybe need set from config
|
|
|
|
bloomFilterSize uint = 100000
|
|
|
|
maxBloomFalsePositive float64 = 0.005
|
|
|
|
)
|
|
|
|
|
2021-09-26 20:51:58 +08:00
|
|
|
// Replica is DataNode unique replication
|
2021-06-21 16:00:22 +08:00
|
|
|
type Replica interface {
|
|
|
|
getCollectionID() UniqueID
|
|
|
|
getCollectionSchema(collectionID UniqueID, ts Timestamp) (*schemapb.CollectionSchema, error)
|
|
|
|
getCollectionAndPartitionID(segID UniqueID) (collID, partitionID UniqueID, err error)
|
|
|
|
|
|
|
|
addNewSegment(segID, collID, partitionID UniqueID, channelName string, startPos, endPos *internalpb.MsgPosition) error
|
2021-10-22 14:31:13 +08:00
|
|
|
addNormalSegment(segID, collID, partitionID UniqueID, channelName string, numOfRows int64, statsBinlog []*datapb.FieldBinlog, cp *segmentCheckPoint) error
|
2021-09-29 10:27:58 +08:00
|
|
|
filterSegments(channelName string, partitionID UniqueID) []*Segment
|
2021-10-22 14:31:13 +08:00
|
|
|
addFlushedSegment(segID, collID, partitionID UniqueID, channelName string, numOfRows int64, statsBinlog []*datapb.FieldBinlog) error
|
2021-06-21 16:00:22 +08:00
|
|
|
listNewSegmentsStartPositions() []*datapb.SegmentStartPosition
|
|
|
|
listSegmentsCheckPoints() map[UniqueID]segmentCheckPoint
|
|
|
|
updateSegmentEndPosition(segID UniqueID, endPos *internalpb.MsgPosition)
|
|
|
|
updateSegmentCheckPoint(segID UniqueID)
|
2021-08-25 10:29:52 +08:00
|
|
|
updateSegmentPKRange(segID UniqueID, rowIDs []int64)
|
2021-10-27 17:14:28 +08:00
|
|
|
refreshFlushedSegmentPKRange(segID UniqueID, rowIDs []int64)
|
|
|
|
addFlushedSegmentWithPKs(segID, collID, partID UniqueID, channelName string, numOfRow int64, rowIDs []int64)
|
2021-08-11 14:24:09 +08:00
|
|
|
hasSegment(segID UniqueID, countFlushed bool) bool
|
2021-10-27 17:14:28 +08:00
|
|
|
removeSegment(segID UniqueID)
|
2021-06-21 16:00:22 +08:00
|
|
|
|
2021-09-17 16:27:56 +08:00
|
|
|
updateStatistics(segID UniqueID, numRows int64)
|
2021-06-21 16:00:22 +08:00
|
|
|
getSegmentStatisticsUpdates(segID UniqueID) (*internalpb.SegmentStatisticsUpdates, error)
|
|
|
|
segmentFlushed(segID UniqueID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Segment is the data structure of segments in data node replica.
|
|
|
|
type Segment struct {
|
|
|
|
collectionID UniqueID
|
|
|
|
partitionID UniqueID
|
|
|
|
segmentID UniqueID
|
|
|
|
numRows int64
|
|
|
|
memorySize int64
|
|
|
|
isNew atomic.Value // bool
|
|
|
|
isFlushed atomic.Value // bool
|
|
|
|
channelName string
|
|
|
|
|
|
|
|
checkPoint segmentCheckPoint
|
|
|
|
startPos *internalpb.MsgPosition // TODO readonly
|
|
|
|
endPos *internalpb.MsgPosition
|
2021-08-25 10:29:52 +08:00
|
|
|
|
|
|
|
pkFilter *bloom.BloomFilter // bloom filter of pk inside a segment
|
|
|
|
// TODO silverxia, needs to change to interface to support `string` type PK
|
|
|
|
minPK int64 // minimal pk value, shortcut for checking whether a pk is inside this segment
|
|
|
|
maxPK int64 // maximal pk value, same above
|
2021-06-21 16:00:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// SegmentReplica is the data replication of persistent data in datanode.
|
|
|
|
// It implements `Replica` interface.
|
|
|
|
type SegmentReplica struct {
|
|
|
|
collectionID UniqueID
|
|
|
|
collSchema *schemapb.CollectionSchema
|
|
|
|
|
|
|
|
segMu sync.RWMutex
|
|
|
|
newSegments map[UniqueID]*Segment
|
|
|
|
normalSegments map[UniqueID]*Segment
|
|
|
|
flushedSegments map[UniqueID]*Segment
|
|
|
|
|
|
|
|
metaService *metaService
|
2021-10-14 10:24:33 +08:00
|
|
|
minIOKV kv.BaseKV
|
2021-06-21 16:00:22 +08:00
|
|
|
}
|
|
|
|
|
2021-10-19 20:18:47 +08:00
|
|
|
func (s *Segment) updatePKRange(pks []int64) {
|
2021-08-25 10:29:52 +08:00
|
|
|
buf := make([]byte, 8)
|
2021-10-19 20:18:47 +08:00
|
|
|
for _, pk := range pks {
|
2021-11-02 18:16:32 +08:00
|
|
|
common.Endian.PutUint64(buf, uint64(pk))
|
2021-08-25 10:29:52 +08:00
|
|
|
s.pkFilter.Add(buf)
|
2021-10-19 20:18:47 +08:00
|
|
|
if pk > s.maxPK {
|
|
|
|
s.maxPK = pk
|
2021-08-25 10:29:52 +08:00
|
|
|
}
|
2021-10-19 20:18:47 +08:00
|
|
|
if pk < s.minPK {
|
|
|
|
s.minPK = pk
|
2021-08-25 10:29:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-21 16:00:22 +08:00
|
|
|
var _ Replica = &SegmentReplica{}
|
|
|
|
|
2021-10-14 10:24:33 +08:00
|
|
|
func newReplica(ctx context.Context, rc types.RootCoord, collID UniqueID) (*SegmentReplica, error) {
|
|
|
|
// MinIO
|
|
|
|
option := &miniokv.Option{
|
|
|
|
Address: Params.MinioAddress,
|
|
|
|
AccessKeyID: Params.MinioAccessKeyID,
|
|
|
|
SecretAccessKeyID: Params.MinioSecretAccessKey,
|
|
|
|
UseSSL: Params.MinioUseSSL,
|
|
|
|
CreateBucket: true,
|
|
|
|
BucketName: Params.MinioBucketName,
|
|
|
|
}
|
|
|
|
|
|
|
|
minIOKV, err := miniokv.NewMinIOKV(ctx, option)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-21 17:28:03 +08:00
|
|
|
metaService := newMetaService(rc, collID)
|
2021-06-21 16:00:22 +08:00
|
|
|
|
2021-10-14 10:24:33 +08:00
|
|
|
replica := &SegmentReplica{
|
2021-06-21 16:00:22 +08:00
|
|
|
collectionID: collID,
|
|
|
|
|
|
|
|
newSegments: make(map[UniqueID]*Segment),
|
|
|
|
normalSegments: make(map[UniqueID]*Segment),
|
|
|
|
flushedSegments: make(map[UniqueID]*Segment),
|
|
|
|
|
|
|
|
metaService: metaService,
|
2021-10-14 10:24:33 +08:00
|
|
|
minIOKV: minIOKV,
|
2021-06-21 16:00:22 +08:00
|
|
|
}
|
2021-10-14 10:24:33 +08:00
|
|
|
|
|
|
|
return replica, nil
|
2021-06-21 16:00:22 +08:00
|
|
|
}
|
|
|
|
|
2021-06-30 10:26:12 +08:00
|
|
|
// segmentFlushed transfers a segment from *New* or *Normal* into *Flushed*.
|
2021-06-21 16:00:22 +08:00
|
|
|
func (replica *SegmentReplica) segmentFlushed(segID UniqueID) {
|
|
|
|
replica.segMu.Lock()
|
|
|
|
defer replica.segMu.Unlock()
|
|
|
|
|
|
|
|
if _, ok := replica.newSegments[segID]; ok {
|
|
|
|
replica.new2FlushedSegment(segID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := replica.normalSegments[segID]; ok {
|
|
|
|
replica.normal2FlushedSegment(segID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (replica *SegmentReplica) new2NormalSegment(segID UniqueID) {
|
|
|
|
var seg Segment = *replica.newSegments[segID]
|
|
|
|
|
|
|
|
seg.isNew.Store(false)
|
|
|
|
replica.normalSegments[segID] = &seg
|
|
|
|
|
|
|
|
delete(replica.newSegments, segID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (replica *SegmentReplica) new2FlushedSegment(segID UniqueID) {
|
|
|
|
var seg Segment = *replica.newSegments[segID]
|
|
|
|
|
|
|
|
seg.isNew.Store(false)
|
2021-06-30 10:26:12 +08:00
|
|
|
seg.isFlushed.Store(true)
|
2021-06-21 16:00:22 +08:00
|
|
|
replica.flushedSegments[segID] = &seg
|
|
|
|
|
|
|
|
delete(replica.newSegments, segID)
|
|
|
|
}
|
|
|
|
|
2021-10-07 23:46:37 +08:00
|
|
|
// normal2FlushedSegment transfers a segment from *normal* to *flushed* by changing *isFlushed*
|
|
|
|
// flag into true, and mv the segment from normalSegments map to flushedSegments map.
|
2021-06-21 16:00:22 +08:00
|
|
|
func (replica *SegmentReplica) normal2FlushedSegment(segID UniqueID) {
|
|
|
|
var seg Segment = *replica.normalSegments[segID]
|
|
|
|
|
2021-06-30 10:26:12 +08:00
|
|
|
seg.isFlushed.Store(true)
|
2021-06-21 16:00:22 +08:00
|
|
|
replica.flushedSegments[segID] = &seg
|
|
|
|
|
|
|
|
delete(replica.normalSegments, segID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (replica *SegmentReplica) getCollectionAndPartitionID(segID UniqueID) (collID, partitionID UniqueID, err error) {
|
|
|
|
replica.segMu.RLock()
|
|
|
|
defer replica.segMu.RUnlock()
|
|
|
|
|
|
|
|
if seg, ok := replica.newSegments[segID]; ok {
|
|
|
|
return seg.collectionID, seg.partitionID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if seg, ok := replica.normalSegments[segID]; ok {
|
|
|
|
return seg.collectionID, seg.partitionID, nil
|
|
|
|
}
|
|
|
|
|
2021-10-11 16:31:44 +08:00
|
|
|
if seg, ok := replica.flushedSegments[segID]; ok {
|
|
|
|
return seg.collectionID, seg.partitionID, nil
|
|
|
|
}
|
|
|
|
|
2021-06-21 16:00:22 +08:00
|
|
|
return 0, 0, fmt.Errorf("Cannot find segment, id = %v", segID)
|
|
|
|
}
|
|
|
|
|
2021-09-09 10:14:00 +08:00
|
|
|
// addNewSegment adds a *New* and *NotFlushed* new segment. Before add, please make sure there's no
|
|
|
|
// such segment by `hasSegment`
|
2021-06-21 16:00:22 +08:00
|
|
|
func (replica *SegmentReplica) addNewSegment(segID, collID, partitionID UniqueID, channelName string,
|
|
|
|
startPos, endPos *internalpb.MsgPosition) error {
|
|
|
|
|
|
|
|
replica.segMu.Lock()
|
|
|
|
defer replica.segMu.Unlock()
|
|
|
|
|
|
|
|
if collID != replica.collectionID {
|
2021-09-09 10:14:00 +08:00
|
|
|
log.Warn("Mismatch collection",
|
|
|
|
zap.Int64("input ID", collID),
|
|
|
|
zap.Int64("expected ID", replica.collectionID))
|
2021-06-21 16:00:22 +08:00
|
|
|
return fmt.Errorf("Mismatch collection, ID=%d", collID)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug("Add new segment",
|
|
|
|
zap.Int64("segment ID", segID),
|
|
|
|
zap.Int64("collection ID", collID),
|
|
|
|
zap.Int64("partition ID", partitionID),
|
|
|
|
zap.String("channel name", channelName),
|
|
|
|
)
|
|
|
|
|
|
|
|
seg := &Segment{
|
|
|
|
collectionID: collID,
|
|
|
|
partitionID: partitionID,
|
|
|
|
segmentID: segID,
|
|
|
|
channelName: channelName,
|
|
|
|
|
|
|
|
checkPoint: segmentCheckPoint{0, *startPos},
|
|
|
|
startPos: startPos,
|
|
|
|
endPos: endPos,
|
2021-08-25 10:29:52 +08:00
|
|
|
|
|
|
|
pkFilter: bloom.NewWithEstimates(bloomFilterSize, maxBloomFalsePositive),
|
|
|
|
minPK: math.MaxInt64, // use max value, represents no value
|
|
|
|
maxPK: math.MinInt64, // use min value represents no value
|
2021-06-21 16:00:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
seg.isNew.Store(true)
|
|
|
|
seg.isFlushed.Store(false)
|
|
|
|
|
|
|
|
replica.newSegments[segID] = seg
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-29 10:27:58 +08:00
|
|
|
// filterSegments return segments with same channelName and partition ID
|
|
|
|
func (replica *SegmentReplica) filterSegments(channelName string, partitionID UniqueID) []*Segment {
|
2021-09-27 14:38:00 +08:00
|
|
|
replica.segMu.Lock()
|
|
|
|
defer replica.segMu.Unlock()
|
|
|
|
results := make([]*Segment, 0)
|
2021-09-29 10:27:58 +08:00
|
|
|
|
|
|
|
isMatched := func(segment *Segment, chanName string, partID UniqueID) bool {
|
2021-10-25 18:49:20 +08:00
|
|
|
return segment.channelName == chanName && (partID == common.InvalidFieldID || segment.partitionID == partID)
|
2021-09-29 10:27:58 +08:00
|
|
|
}
|
|
|
|
for _, seg := range replica.newSegments {
|
|
|
|
if isMatched(seg, channelName, partitionID) {
|
|
|
|
results = append(results, seg)
|
2021-09-27 14:38:00 +08:00
|
|
|
}
|
|
|
|
}
|
2021-09-29 10:27:58 +08:00
|
|
|
for _, seg := range replica.normalSegments {
|
|
|
|
if isMatched(seg, channelName, partitionID) {
|
|
|
|
results = append(results, seg)
|
2021-09-27 14:38:00 +08:00
|
|
|
}
|
|
|
|
}
|
2021-09-29 10:27:58 +08:00
|
|
|
for _, seg := range replica.flushedSegments {
|
|
|
|
if isMatched(seg, channelName, partitionID) {
|
|
|
|
results = append(results, seg)
|
2021-09-27 14:38:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
2021-09-09 10:14:00 +08:00
|
|
|
// addNormalSegment adds a *NotNew* and *NotFlushed* segment. Before add, please make sure there's no
|
|
|
|
// such segment by `hasSegment`
|
2021-10-22 14:31:13 +08:00
|
|
|
func (replica *SegmentReplica) addNormalSegment(segID, collID, partitionID UniqueID, channelName string, numOfRows int64, statsBinlogs []*datapb.FieldBinlog, cp *segmentCheckPoint) error {
|
2021-06-21 16:00:22 +08:00
|
|
|
if collID != replica.collectionID {
|
2021-09-09 10:14:00 +08:00
|
|
|
log.Warn("Mismatch collection",
|
|
|
|
zap.Int64("input ID", collID),
|
|
|
|
zap.Int64("expected ID", replica.collectionID))
|
2021-06-21 16:00:22 +08:00
|
|
|
return fmt.Errorf("Mismatch collection, ID=%d", collID)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug("Add Normal segment",
|
|
|
|
zap.Int64("segment ID", segID),
|
|
|
|
zap.Int64("collection ID", collID),
|
|
|
|
zap.Int64("partition ID", partitionID),
|
|
|
|
zap.String("channel name", channelName),
|
|
|
|
)
|
|
|
|
|
|
|
|
seg := &Segment{
|
|
|
|
collectionID: collID,
|
|
|
|
partitionID: partitionID,
|
|
|
|
segmentID: segID,
|
|
|
|
channelName: channelName,
|
|
|
|
numRows: numOfRows,
|
|
|
|
|
|
|
|
checkPoint: *cp,
|
|
|
|
endPos: &cp.pos,
|
2021-08-25 10:29:52 +08:00
|
|
|
|
|
|
|
pkFilter: bloom.NewWithEstimates(bloomFilterSize, maxBloomFalsePositive),
|
|
|
|
minPK: math.MaxInt64, // use max value, represents no value
|
|
|
|
maxPK: math.MinInt64, // use min value represents no value
|
2021-06-21 16:00:22 +08:00
|
|
|
}
|
2021-10-22 14:31:13 +08:00
|
|
|
err := replica.initPKBloomFilter(seg, statsBinlogs)
|
2021-10-14 10:24:33 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-21 16:00:22 +08:00
|
|
|
seg.isNew.Store(false)
|
|
|
|
seg.isFlushed.Store(false)
|
|
|
|
|
2021-10-19 20:18:47 +08:00
|
|
|
replica.segMu.Lock()
|
2021-06-21 16:00:22 +08:00
|
|
|
replica.normalSegments[segID] = seg
|
2021-10-19 20:18:47 +08:00
|
|
|
replica.segMu.Unlock()
|
|
|
|
|
2021-06-21 16:00:22 +08:00
|
|
|
return nil
|
2021-09-27 14:38:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// addFlushedSegment adds a *Flushed* segment. Before add, please make sure there's no
|
|
|
|
// such segment by `hasSegment`
|
2021-10-22 14:31:13 +08:00
|
|
|
func (replica *SegmentReplica) addFlushedSegment(segID, collID, partitionID UniqueID, channelName string, numOfRows int64, statsBinlogs []*datapb.FieldBinlog) error {
|
2021-09-27 14:38:00 +08:00
|
|
|
|
|
|
|
if collID != replica.collectionID {
|
|
|
|
log.Warn("Mismatch collection",
|
|
|
|
zap.Int64("input ID", collID),
|
|
|
|
zap.Int64("expected ID", replica.collectionID))
|
|
|
|
return fmt.Errorf("Mismatch collection, ID=%d", collID)
|
|
|
|
}
|
|
|
|
|
2021-10-11 21:12:44 +08:00
|
|
|
log.Debug("Add Flushed segment",
|
2021-09-27 14:38:00 +08:00
|
|
|
zap.Int64("segment ID", segID),
|
|
|
|
zap.Int64("collection ID", collID),
|
|
|
|
zap.Int64("partition ID", partitionID),
|
|
|
|
zap.String("channel name", channelName),
|
|
|
|
)
|
|
|
|
|
|
|
|
seg := &Segment{
|
|
|
|
collectionID: collID,
|
|
|
|
partitionID: partitionID,
|
|
|
|
segmentID: segID,
|
|
|
|
channelName: channelName,
|
|
|
|
numRows: numOfRows,
|
|
|
|
|
|
|
|
//TODO silverxia, normal segments bloom filter and pk range should be loaded from serialized files
|
|
|
|
pkFilter: bloom.NewWithEstimates(bloomFilterSize, maxBloomFalsePositive),
|
|
|
|
minPK: math.MaxInt64, // use max value, represents no value
|
|
|
|
maxPK: math.MinInt64, // use min value represents no value
|
|
|
|
}
|
|
|
|
|
2021-10-22 14:31:13 +08:00
|
|
|
err := replica.initPKBloomFilter(seg, statsBinlogs)
|
2021-10-19 20:18:47 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
seg.isNew.Store(false)
|
|
|
|
seg.isFlushed.Store(true)
|
|
|
|
|
|
|
|
replica.segMu.Lock()
|
|
|
|
replica.flushedSegments[segID] = seg
|
|
|
|
replica.segMu.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-22 14:31:13 +08:00
|
|
|
func (replica *SegmentReplica) initPKBloomFilter(s *Segment, statsBinlogs []*datapb.FieldBinlog) error {
|
|
|
|
if len(statsBinlogs) == 0 {
|
|
|
|
log.Info("statsBinlogs is empty")
|
|
|
|
}
|
2021-10-19 20:18:47 +08:00
|
|
|
schema, err := replica.getCollectionSchema(s.collectionID, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-22 14:31:13 +08:00
|
|
|
// get pkfield id
|
2021-10-19 20:18:47 +08:00
|
|
|
pkField := int64(-1)
|
|
|
|
for _, field := range schema.Fields {
|
|
|
|
if field.IsPrimaryKey {
|
|
|
|
pkField = field.FieldID
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-22 14:31:13 +08:00
|
|
|
// filter stats binlog files which is pk field stats log
|
|
|
|
bloomFilterFiles := make([]string, 0)
|
|
|
|
for _, binlog := range statsBinlogs {
|
|
|
|
if binlog.FieldID != pkField {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
bloomFilterFiles = append(bloomFilterFiles, binlog.Binlogs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
values, err := replica.minIOKV.MultiLoad(bloomFilterFiles)
|
2021-10-14 10:24:33 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
blobs := make([]*Blob, 0)
|
2021-10-22 14:31:13 +08:00
|
|
|
for i := 0; i < len(values); i++ {
|
|
|
|
blobs = append(blobs, &Blob{Value: []byte(values[i])})
|
2021-10-14 10:24:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
stats, err := storage.DeserializeStats(blobs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, stat := range stats {
|
2021-10-19 20:18:47 +08:00
|
|
|
err = s.pkFilter.Merge(stat.BF)
|
2021-10-15 16:58:42 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-10-19 20:18:47 +08:00
|
|
|
if s.minPK > stat.Min {
|
|
|
|
s.minPK = stat.Min
|
2021-10-14 10:24:33 +08:00
|
|
|
}
|
|
|
|
|
2021-10-19 20:18:47 +08:00
|
|
|
if s.maxPK < stat.Max {
|
|
|
|
s.maxPK = stat.Max
|
2021-10-14 10:24:33 +08:00
|
|
|
}
|
|
|
|
}
|
2021-09-27 14:38:00 +08:00
|
|
|
return nil
|
2021-06-21 16:00:22 +08:00
|
|
|
}
|
2021-06-30 10:26:12 +08:00
|
|
|
|
|
|
|
// listNewSegmentsStartPositions gets all *New Segments* start positions and
|
|
|
|
// transfer segments states from *New* to *Normal*.
|
2021-06-21 16:00:22 +08:00
|
|
|
func (replica *SegmentReplica) listNewSegmentsStartPositions() []*datapb.SegmentStartPosition {
|
2021-10-25 18:03:42 +08:00
|
|
|
replica.segMu.Lock()
|
|
|
|
defer replica.segMu.Unlock()
|
2021-06-21 16:00:22 +08:00
|
|
|
|
|
|
|
result := make([]*datapb.SegmentStartPosition, 0, len(replica.newSegments))
|
|
|
|
for id, seg := range replica.newSegments {
|
|
|
|
|
|
|
|
result = append(result, &datapb.SegmentStartPosition{
|
|
|
|
SegmentID: id,
|
|
|
|
StartPosition: seg.startPos,
|
|
|
|
})
|
|
|
|
|
2021-06-30 10:26:12 +08:00
|
|
|
// transfer states
|
2021-06-21 16:00:22 +08:00
|
|
|
replica.new2NormalSegment(id)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2021-06-30 10:26:12 +08:00
|
|
|
// listSegmentsCheckPoints gets check points from both *New* and *Normal* segments.
|
2021-06-21 16:00:22 +08:00
|
|
|
func (replica *SegmentReplica) listSegmentsCheckPoints() map[UniqueID]segmentCheckPoint {
|
|
|
|
replica.segMu.RLock()
|
|
|
|
defer replica.segMu.RUnlock()
|
|
|
|
|
|
|
|
result := make(map[UniqueID]segmentCheckPoint)
|
|
|
|
|
|
|
|
for id, seg := range replica.newSegments {
|
|
|
|
result[id] = seg.checkPoint
|
|
|
|
}
|
|
|
|
|
|
|
|
for id, seg := range replica.normalSegments {
|
|
|
|
result[id] = seg.checkPoint
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2021-06-30 10:26:12 +08:00
|
|
|
// updateSegmentEndPosition updates *New* or *Normal* segment's end position.
|
2021-06-21 16:00:22 +08:00
|
|
|
func (replica *SegmentReplica) updateSegmentEndPosition(segID UniqueID, endPos *internalpb.MsgPosition) {
|
|
|
|
replica.segMu.RLock()
|
|
|
|
defer replica.segMu.RUnlock()
|
|
|
|
|
|
|
|
seg, ok := replica.newSegments[segID]
|
|
|
|
if ok {
|
|
|
|
seg.endPos = endPos
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
seg, ok = replica.normalSegments[segID]
|
|
|
|
if ok {
|
|
|
|
seg.endPos = endPos
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Warn("No match segment", zap.Int64("ID", segID))
|
|
|
|
}
|
|
|
|
|
2021-10-19 20:18:47 +08:00
|
|
|
func (replica *SegmentReplica) updateSegmentPKRange(segID UniqueID, pks []int64) {
|
2021-08-25 10:29:52 +08:00
|
|
|
replica.segMu.Lock()
|
|
|
|
defer replica.segMu.Unlock()
|
|
|
|
|
|
|
|
seg, ok := replica.newSegments[segID]
|
|
|
|
if ok {
|
2021-10-19 20:18:47 +08:00
|
|
|
seg.updatePKRange(pks)
|
2021-08-25 10:29:52 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
seg, ok = replica.normalSegments[segID]
|
|
|
|
if ok {
|
2021-10-19 20:18:47 +08:00
|
|
|
seg.updatePKRange(pks)
|
2021-08-25 10:29:52 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-25 18:49:20 +08:00
|
|
|
seg, ok = replica.flushedSegments[segID]
|
|
|
|
if ok {
|
|
|
|
seg.updatePKRange(pks)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-25 10:29:52 +08:00
|
|
|
log.Warn("No match segment to update PK range", zap.Int64("ID", segID))
|
|
|
|
}
|
|
|
|
|
2021-10-27 17:14:28 +08:00
|
|
|
func (replica *SegmentReplica) removeSegment(segID UniqueID) {
|
|
|
|
replica.segMu.Lock()
|
|
|
|
defer replica.segMu.Unlock()
|
|
|
|
|
|
|
|
delete(replica.newSegments, segID)
|
|
|
|
delete(replica.normalSegments, segID)
|
|
|
|
delete(replica.flushedSegments, segID)
|
2021-06-21 16:00:22 +08:00
|
|
|
}
|
|
|
|
|
2021-06-30 10:26:12 +08:00
|
|
|
// hasSegment checks whether this replica has a segment according to segment ID.
|
2021-08-11 14:24:09 +08:00
|
|
|
func (replica *SegmentReplica) hasSegment(segID UniqueID, countFlushed bool) bool {
|
2021-06-21 16:00:22 +08:00
|
|
|
replica.segMu.RLock()
|
|
|
|
defer replica.segMu.RUnlock()
|
|
|
|
|
|
|
|
_, inNew := replica.newSegments[segID]
|
|
|
|
_, inNormal := replica.normalSegments[segID]
|
2021-08-11 14:24:09 +08:00
|
|
|
|
|
|
|
inFlush := false
|
|
|
|
if countFlushed {
|
|
|
|
_, inFlush = replica.flushedSegments[segID]
|
|
|
|
}
|
2021-06-21 16:00:22 +08:00
|
|
|
|
|
|
|
return inNew || inNormal || inFlush
|
|
|
|
}
|
|
|
|
|
2021-06-30 10:26:12 +08:00
|
|
|
// updateStatistics updates the number of rows of a segment in replica.
|
2021-09-17 16:27:56 +08:00
|
|
|
func (replica *SegmentReplica) updateStatistics(segID UniqueID, numRows int64) {
|
2021-06-21 16:00:22 +08:00
|
|
|
replica.segMu.Lock()
|
|
|
|
defer replica.segMu.Unlock()
|
|
|
|
|
|
|
|
log.Debug("updating segment", zap.Int64("Segment ID", segID), zap.Int64("numRows", numRows))
|
|
|
|
if seg, ok := replica.newSegments[segID]; ok {
|
|
|
|
seg.memorySize = 0
|
|
|
|
seg.numRows += numRows
|
2021-09-17 16:27:56 +08:00
|
|
|
return
|
2021-06-21 16:00:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if seg, ok := replica.normalSegments[segID]; ok {
|
|
|
|
seg.memorySize = 0
|
|
|
|
seg.numRows += numRows
|
2021-09-17 16:27:56 +08:00
|
|
|
return
|
2021-06-21 16:00:22 +08:00
|
|
|
}
|
|
|
|
|
2021-09-17 16:27:56 +08:00
|
|
|
log.Warn("update segment num row not exist", zap.Int64("segID", segID))
|
2021-06-21 16:00:22 +08:00
|
|
|
}
|
|
|
|
|
2021-06-30 10:26:12 +08:00
|
|
|
// getSegmentStatisticsUpdates gives current segment's statistics updates.
|
2021-06-21 16:00:22 +08:00
|
|
|
func (replica *SegmentReplica) getSegmentStatisticsUpdates(segID UniqueID) (*internalpb.SegmentStatisticsUpdates, error) {
|
|
|
|
replica.segMu.Lock()
|
|
|
|
defer replica.segMu.Unlock()
|
|
|
|
updates := &internalpb.SegmentStatisticsUpdates{
|
|
|
|
SegmentID: segID,
|
|
|
|
}
|
|
|
|
|
|
|
|
if seg, ok := replica.newSegments[segID]; ok {
|
|
|
|
updates.NumRows = seg.numRows
|
|
|
|
return updates, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if seg, ok := replica.normalSegments[segID]; ok {
|
|
|
|
updates.NumRows = seg.numRows
|
|
|
|
return updates, nil
|
|
|
|
}
|
|
|
|
|
2021-10-19 11:04:34 +08:00
|
|
|
if seg, ok := replica.flushedSegments[segID]; ok {
|
|
|
|
updates.NumRows = seg.numRows
|
|
|
|
return updates, nil
|
|
|
|
}
|
|
|
|
|
2021-06-21 16:00:22 +08:00
|
|
|
return nil, fmt.Errorf("Error, there's no segment %v", segID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- collection ---
|
|
|
|
func (replica *SegmentReplica) getCollectionID() UniqueID {
|
|
|
|
return replica.collectionID
|
|
|
|
}
|
|
|
|
|
2021-06-30 10:26:12 +08:00
|
|
|
// getCollectionSchema gets collection schema from rootcoord for a certain timestamp.
|
|
|
|
// If you want the latest collection schema, ts should be 0.
|
2021-06-21 16:00:22 +08:00
|
|
|
func (replica *SegmentReplica) getCollectionSchema(collID UniqueID, ts Timestamp) (*schemapb.CollectionSchema, error) {
|
|
|
|
if !replica.validCollection(collID) {
|
2021-09-09 10:14:00 +08:00
|
|
|
log.Warn("Mismatch collection for the replica",
|
2021-06-21 16:00:22 +08:00
|
|
|
zap.Int64("Want", replica.collectionID),
|
|
|
|
zap.Int64("Actual", collID),
|
|
|
|
)
|
|
|
|
return nil, fmt.Errorf("Not supported collection %v", collID)
|
|
|
|
}
|
|
|
|
|
|
|
|
sch, err := replica.metaService.getCollectionSchema(context.Background(), collID, ts)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Grpc error", zap.Error(err))
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return sch, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (replica *SegmentReplica) validCollection(collID UniqueID) bool {
|
|
|
|
return collID == replica.collectionID
|
|
|
|
}
|
|
|
|
|
2021-06-30 10:26:12 +08:00
|
|
|
// updateSegmentCheckPoint is called when auto flush or mannul flush is done.
|
2021-06-21 16:00:22 +08:00
|
|
|
func (replica *SegmentReplica) updateSegmentCheckPoint(segID UniqueID) {
|
|
|
|
replica.segMu.Lock()
|
|
|
|
defer replica.segMu.Unlock()
|
|
|
|
|
|
|
|
if seg, ok := replica.newSegments[segID]; ok {
|
|
|
|
seg.checkPoint = segmentCheckPoint{seg.numRows, *seg.endPos}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if seg, ok := replica.normalSegments[segID]; ok {
|
|
|
|
seg.checkPoint = segmentCheckPoint{seg.numRows, *seg.endPos}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Warn("There's no segment", zap.Int64("ID", segID))
|
|
|
|
}
|
2021-10-27 17:14:28 +08:00
|
|
|
|
|
|
|
// please call hasSegment first
|
|
|
|
func (replica *SegmentReplica) refreshFlushedSegmentPKRange(segID UniqueID, rowIDs []int64) {
|
|
|
|
replica.segMu.Lock()
|
|
|
|
defer replica.segMu.Unlock()
|
|
|
|
|
|
|
|
seg, ok := replica.flushedSegments[segID]
|
|
|
|
if ok {
|
|
|
|
seg.pkFilter.ClearAll()
|
|
|
|
seg.updatePKRange(rowIDs)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Warn("No match segment to update PK range", zap.Int64("ID", segID))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (replica *SegmentReplica) addFlushedSegmentWithPKs(segID, collID, partID UniqueID, channelName string, numOfRows int64, rowIDs []int64) {
|
|
|
|
if collID != replica.collectionID {
|
|
|
|
log.Warn("Mismatch collection",
|
|
|
|
zap.Int64("input ID", collID),
|
|
|
|
zap.Int64("expected ID", replica.collectionID))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug("Add Flushed segment",
|
|
|
|
zap.Int64("segment ID", segID),
|
|
|
|
zap.Int64("collection ID", collID),
|
|
|
|
zap.Int64("partition ID", partID),
|
|
|
|
zap.String("channel name", channelName),
|
|
|
|
)
|
|
|
|
|
|
|
|
seg := &Segment{
|
|
|
|
collectionID: collID,
|
|
|
|
partitionID: partID,
|
|
|
|
segmentID: segID,
|
|
|
|
channelName: channelName,
|
|
|
|
numRows: numOfRows,
|
|
|
|
|
|
|
|
pkFilter: bloom.NewWithEstimates(bloomFilterSize, maxBloomFalsePositive),
|
|
|
|
minPK: math.MaxInt64, // use max value, represents no value
|
|
|
|
maxPK: math.MinInt64, // use min value represents no value
|
|
|
|
}
|
|
|
|
|
|
|
|
seg.updatePKRange(rowIDs)
|
|
|
|
|
|
|
|
seg.isNew.Store(false)
|
|
|
|
seg.isFlushed.Store(true)
|
|
|
|
|
|
|
|
replica.segMu.Lock()
|
|
|
|
replica.flushedSegments[segID] = seg
|
|
|
|
replica.segMu.Unlock()
|
|
|
|
}
|