2021-04-19 15:16:33 +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-01-19 11:37:16 +08:00
|
|
|
package datanode
|
|
|
|
|
|
|
|
import (
|
2021-03-05 10:15:27 +08:00
|
|
|
"fmt"
|
2021-01-19 11:37:16 +08:00
|
|
|
"sync"
|
2021-03-16 17:55:42 +08:00
|
|
|
"sync/atomic"
|
2021-01-19 11:37:16 +08:00
|
|
|
|
2021-02-26 10:13:36 +08:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/internalpb"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/schemapb"
|
2021-01-19 11:37:16 +08:00
|
|
|
)
|
|
|
|
|
2021-02-04 20:31:23 +08:00
|
|
|
type Replica interface {
|
2021-01-19 11:37:16 +08:00
|
|
|
|
|
|
|
// collection
|
|
|
|
getCollectionNum() int
|
2021-01-25 18:33:10 +08:00
|
|
|
addCollection(collectionID UniqueID, schema *schemapb.CollectionSchema) error
|
2021-01-19 11:37:16 +08:00
|
|
|
removeCollection(collectionID UniqueID) error
|
|
|
|
getCollectionByID(collectionID UniqueID) (*Collection, error)
|
|
|
|
hasCollection(collectionID UniqueID) bool
|
|
|
|
|
2021-01-21 09:55:25 +08:00
|
|
|
// segment
|
2021-02-04 11:19:48 +08:00
|
|
|
addSegment(segmentID UniqueID, collID UniqueID, partitionID UniqueID, channelName string) error
|
2021-01-21 09:55:25 +08:00
|
|
|
removeSegment(segmentID UniqueID) error
|
|
|
|
hasSegment(segmentID UniqueID) bool
|
2021-03-16 17:55:42 +08:00
|
|
|
setIsFlushed(segmentID UniqueID) error
|
|
|
|
setStartPosition(segmentID UniqueID, startPos *internalpb.MsgPosition) error
|
|
|
|
setEndPosition(segmentID UniqueID, endPos *internalpb.MsgPosition) error
|
2021-01-26 09:44:39 +08:00
|
|
|
updateStatistics(segmentID UniqueID, numRows int64) error
|
2021-03-12 14:22:09 +08:00
|
|
|
getSegmentStatisticsUpdates(segmentID UniqueID) (*internalpb.SegmentStatisticsUpdates, error)
|
2021-01-22 09:36:40 +08:00
|
|
|
getSegmentByID(segmentID UniqueID) (*Segment, error)
|
2021-05-18 16:33:05 +08:00
|
|
|
bufferAutoFlushBinlogPaths(segmentID UniqueID, field2Path map[UniqueID]string) error
|
|
|
|
getBufferPaths(segID UniqueID) (map[UniqueID][]string, error)
|
2021-05-25 15:35:37 +08:00
|
|
|
getChannelName(segID UniqueID) (string, error)
|
2021-01-19 11:37:16 +08:00
|
|
|
}
|
|
|
|
|
2021-05-08 15:24:12 +08:00
|
|
|
// Segment is the data structure of segments in data node replica.
|
2021-03-05 16:52:45 +08:00
|
|
|
type Segment struct {
|
2021-03-16 17:55:42 +08:00
|
|
|
segmentID UniqueID
|
|
|
|
collectionID UniqueID
|
|
|
|
partitionID UniqueID
|
|
|
|
numRows int64
|
|
|
|
memorySize int64
|
|
|
|
isNew atomic.Value // bool
|
|
|
|
isFlushed bool
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
createTime Timestamp // not using
|
|
|
|
endTime Timestamp // not using
|
2021-03-12 14:22:09 +08:00
|
|
|
startPosition *internalpb.MsgPosition
|
|
|
|
endPosition *internalpb.MsgPosition // not using
|
2021-03-22 16:36:10 +08:00
|
|
|
channelName string
|
2021-05-18 16:33:05 +08:00
|
|
|
field2Paths map[UniqueID][]string // fieldID to binlog paths, only auto-flushed paths will be buffered.
|
2021-03-05 16:52:45 +08:00
|
|
|
}
|
2021-01-21 09:55:25 +08:00
|
|
|
|
2021-05-08 15:24:12 +08:00
|
|
|
// CollectionSegmentReplica is the data replication of persistent data in datanode.
|
|
|
|
// It implements `Replica` interface.
|
2021-03-05 16:52:45 +08:00
|
|
|
type CollectionSegmentReplica struct {
|
|
|
|
mu sync.RWMutex
|
2021-03-16 17:55:42 +08:00
|
|
|
segments map[UniqueID]*Segment
|
2021-03-05 16:52:45 +08:00
|
|
|
collections map[UniqueID]*Collection
|
|
|
|
}
|
2021-01-21 09:55:25 +08:00
|
|
|
|
2021-05-18 16:33:05 +08:00
|
|
|
var _ Replica = &CollectionSegmentReplica{}
|
|
|
|
|
2021-02-04 20:31:23 +08:00
|
|
|
func newReplica() Replica {
|
2021-03-16 17:55:42 +08:00
|
|
|
segments := make(map[UniqueID]*Segment)
|
2021-02-04 20:31:23 +08:00
|
|
|
collections := make(map[UniqueID]*Collection)
|
2021-01-28 15:20:24 +08:00
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
var replica Replica = &CollectionSegmentReplica{
|
2021-01-28 15:20:24 +08:00
|
|
|
segments: segments,
|
2021-02-04 20:31:23 +08:00
|
|
|
collections: collections,
|
2021-01-28 15:20:24 +08:00
|
|
|
}
|
|
|
|
return replica
|
|
|
|
}
|
|
|
|
|
2021-05-25 15:35:37 +08:00
|
|
|
func (replica *CollectionSegmentReplica) getChannelName(segID UniqueID) (string, error) {
|
|
|
|
replica.mu.RLock()
|
|
|
|
defer replica.mu.RUnlock()
|
|
|
|
|
|
|
|
seg, ok := replica.segments[segID]
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("Cannot find segment, id = %v", segID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return seg.channelName, nil
|
|
|
|
}
|
|
|
|
|
2021-05-18 16:33:05 +08:00
|
|
|
// bufferAutoFlushBinlogPaths buffers binlog paths generated by auto-flush
|
|
|
|
func (replica *CollectionSegmentReplica) bufferAutoFlushBinlogPaths(segID UniqueID, field2Path map[UniqueID]string) error {
|
|
|
|
replica.mu.RLock()
|
|
|
|
defer replica.mu.RUnlock()
|
|
|
|
|
|
|
|
seg, ok := replica.segments[segID]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Cannot find segment, id = %v", segID)
|
|
|
|
}
|
|
|
|
|
|
|
|
for fieldID, path := range field2Path {
|
|
|
|
buffpaths, ok := seg.field2Paths[fieldID]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
buffpaths = make([]string, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
buffpaths = append(buffpaths, path)
|
|
|
|
seg.field2Paths[fieldID] = buffpaths
|
|
|
|
}
|
|
|
|
log.Info("Buffer auto flush binlog paths", zap.Int64("segment ID", segID))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (replica *CollectionSegmentReplica) getBufferPaths(segID UniqueID) (map[UniqueID][]string, error) {
|
|
|
|
replica.mu.RLock()
|
|
|
|
defer replica.mu.RUnlock()
|
|
|
|
|
|
|
|
if seg, ok := replica.segments[segID]; ok {
|
|
|
|
return seg.field2Paths, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("Cannot find segment, id = %v", segID)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
func (replica *CollectionSegmentReplica) getSegmentByID(segmentID UniqueID) (*Segment, error) {
|
2021-02-04 20:31:23 +08:00
|
|
|
replica.mu.RLock()
|
|
|
|
defer replica.mu.RUnlock()
|
2021-01-22 09:36:40 +08:00
|
|
|
|
2021-03-16 17:55:42 +08:00
|
|
|
if seg, ok := replica.segments[segmentID]; ok {
|
|
|
|
return seg, nil
|
2021-01-22 09:36:40 +08:00
|
|
|
}
|
2021-03-05 10:15:27 +08:00
|
|
|
return nil, fmt.Errorf("Cannot find segment, id = %v", segmentID)
|
2021-03-16 17:55:42 +08:00
|
|
|
|
2021-01-22 09:36:40 +08:00
|
|
|
}
|
|
|
|
|
2021-05-08 15:24:12 +08:00
|
|
|
// `addSegment` add a new segment into replica when data node see the segment
|
|
|
|
// for the first time in insert channels. It sets the startPosition of a segment, and
|
|
|
|
// flags `isNew=true`
|
2021-03-05 16:52:45 +08:00
|
|
|
func (replica *CollectionSegmentReplica) addSegment(
|
2021-02-04 11:19:48 +08:00
|
|
|
segmentID UniqueID,
|
|
|
|
collID UniqueID,
|
|
|
|
partitionID UniqueID,
|
|
|
|
channelName string) error {
|
2021-01-25 13:36:40 +08:00
|
|
|
|
2021-02-04 20:31:23 +08:00
|
|
|
replica.mu.Lock()
|
|
|
|
defer replica.mu.Unlock()
|
2021-02-26 10:13:36 +08:00
|
|
|
log.Debug("Add Segment", zap.Int64("Segment ID", segmentID))
|
2021-01-22 09:36:40 +08:00
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
position := &internalpb.MsgPosition{
|
2021-02-04 11:19:48 +08:00
|
|
|
ChannelName: channelName,
|
|
|
|
}
|
|
|
|
|
2021-01-21 09:55:25 +08:00
|
|
|
seg := &Segment{
|
2021-02-04 11:19:48 +08:00
|
|
|
segmentID: segmentID,
|
|
|
|
collectionID: collID,
|
|
|
|
partitionID: partitionID,
|
2021-03-16 17:55:42 +08:00
|
|
|
isFlushed: false,
|
2021-02-04 11:19:48 +08:00
|
|
|
createTime: 0,
|
|
|
|
startPosition: position,
|
2021-03-12 14:22:09 +08:00
|
|
|
endPosition: new(internalpb.MsgPosition),
|
2021-03-22 16:36:10 +08:00
|
|
|
channelName: channelName,
|
2021-05-18 16:33:05 +08:00
|
|
|
field2Paths: make(map[UniqueID][]string),
|
2021-01-21 09:55:25 +08:00
|
|
|
}
|
2021-03-16 17:55:42 +08:00
|
|
|
|
|
|
|
seg.isNew.Store(true)
|
|
|
|
|
|
|
|
replica.segments[segmentID] = seg
|
2021-01-21 09:55:25 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
func (replica *CollectionSegmentReplica) removeSegment(segmentID UniqueID) error {
|
2021-02-04 20:31:23 +08:00
|
|
|
replica.mu.Lock()
|
|
|
|
defer replica.mu.Unlock()
|
2021-01-21 09:55:25 +08:00
|
|
|
|
2021-03-16 17:55:42 +08:00
|
|
|
delete(replica.segments, segmentID)
|
|
|
|
|
|
|
|
return nil
|
2021-01-21 09:55:25 +08:00
|
|
|
}
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
func (replica *CollectionSegmentReplica) hasSegment(segmentID UniqueID) bool {
|
2021-02-04 20:31:23 +08:00
|
|
|
replica.mu.RLock()
|
|
|
|
defer replica.mu.RUnlock()
|
2021-01-21 09:55:25 +08:00
|
|
|
|
2021-03-16 17:55:42 +08:00
|
|
|
_, ok := replica.segments[segmentID]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (replica *CollectionSegmentReplica) setIsFlushed(segmentID UniqueID) error {
|
|
|
|
replica.mu.RLock()
|
|
|
|
defer replica.mu.RUnlock()
|
|
|
|
|
|
|
|
if seg, ok := replica.segments[segmentID]; ok {
|
|
|
|
seg.isFlushed = true
|
|
|
|
return nil
|
2021-01-21 09:55:25 +08:00
|
|
|
}
|
2021-03-16 17:55:42 +08:00
|
|
|
|
|
|
|
return fmt.Errorf("There's no segment %v", segmentID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (replica *CollectionSegmentReplica) setStartPosition(segmentID UniqueID, startPos *internalpb.MsgPosition) error {
|
|
|
|
replica.mu.RLock()
|
|
|
|
defer replica.mu.RUnlock()
|
|
|
|
|
|
|
|
if startPos == nil {
|
|
|
|
return fmt.Errorf("Nil MsgPosition")
|
|
|
|
}
|
|
|
|
|
|
|
|
if seg, ok := replica.segments[segmentID]; ok {
|
|
|
|
seg.startPosition = startPos
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("There's no segment %v", segmentID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (replica *CollectionSegmentReplica) setEndPosition(segmentID UniqueID, endPos *internalpb.MsgPosition) error {
|
|
|
|
replica.mu.RLock()
|
|
|
|
defer replica.mu.RUnlock()
|
|
|
|
|
|
|
|
if endPos == nil {
|
|
|
|
return fmt.Errorf("Nil MsgPosition")
|
|
|
|
}
|
|
|
|
|
|
|
|
if seg, ok := replica.segments[segmentID]; ok {
|
|
|
|
seg.endPosition = endPos
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("There's no segment %v", segmentID)
|
2021-01-21 09:55:25 +08:00
|
|
|
}
|
|
|
|
|
2021-05-08 15:24:12 +08:00
|
|
|
// `updateStatistics` updates the number of rows of a segment in replica.
|
2021-03-05 16:52:45 +08:00
|
|
|
func (replica *CollectionSegmentReplica) updateStatistics(segmentID UniqueID, numRows int64) error {
|
2021-02-04 20:31:23 +08:00
|
|
|
replica.mu.Lock()
|
|
|
|
defer replica.mu.Unlock()
|
2021-01-21 09:55:25 +08:00
|
|
|
|
2021-03-16 17:55:42 +08:00
|
|
|
if seg, ok := replica.segments[segmentID]; ok {
|
|
|
|
log.Debug("updating segment", zap.Int64("Segment ID", segmentID), zap.Int64("numRows", numRows))
|
|
|
|
seg.memorySize = 0
|
|
|
|
seg.numRows += numRows
|
|
|
|
return nil
|
2021-01-21 09:55:25 +08:00
|
|
|
}
|
2021-03-16 17:55:42 +08:00
|
|
|
|
|
|
|
return fmt.Errorf("There's no segment %v", segmentID)
|
2021-01-21 09:55:25 +08:00
|
|
|
}
|
|
|
|
|
2021-05-08 15:24:12 +08:00
|
|
|
// `getSegmentStatisticsUpdates` gives current segment's statistics updates.
|
|
|
|
// if the segment's flag `isNew` is true, updates will contain a valid start position.
|
|
|
|
// if the segment's flag `isFlushed` is true, updates will contain a valid end position.
|
2021-03-12 14:22:09 +08:00
|
|
|
func (replica *CollectionSegmentReplica) getSegmentStatisticsUpdates(segmentID UniqueID) (*internalpb.SegmentStatisticsUpdates, error) {
|
2021-02-04 20:31:23 +08:00
|
|
|
replica.mu.Lock()
|
|
|
|
defer replica.mu.Unlock()
|
2021-01-21 09:55:25 +08:00
|
|
|
|
2021-03-16 17:55:42 +08:00
|
|
|
if seg, ok := replica.segments[segmentID]; ok {
|
|
|
|
updates := &internalpb.SegmentStatisticsUpdates{
|
|
|
|
SegmentID: segmentID,
|
|
|
|
MemorySize: seg.memorySize,
|
|
|
|
NumRows: seg.numRows,
|
2021-01-21 09:55:25 +08:00
|
|
|
}
|
2021-03-16 17:55:42 +08:00
|
|
|
|
|
|
|
if seg.isNew.Load() == true {
|
|
|
|
updates.StartPosition = seg.startPosition
|
|
|
|
seg.isNew.Store(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
if seg.isFlushed {
|
|
|
|
updates.EndPosition = seg.endPosition
|
|
|
|
}
|
|
|
|
|
|
|
|
return updates, nil
|
2021-01-21 09:55:25 +08:00
|
|
|
}
|
2021-03-05 10:15:27 +08:00
|
|
|
return nil, fmt.Errorf("Error, there's no segment %v", segmentID)
|
2021-01-19 11:37:16 +08:00
|
|
|
}
|
|
|
|
|
2021-02-04 20:31:23 +08:00
|
|
|
// --- collection ---
|
2021-03-05 16:52:45 +08:00
|
|
|
func (replica *CollectionSegmentReplica) getCollectionNum() int {
|
2021-02-04 20:31:23 +08:00
|
|
|
replica.mu.RLock()
|
|
|
|
defer replica.mu.RUnlock()
|
2021-01-19 11:37:16 +08:00
|
|
|
|
2021-02-04 20:31:23 +08:00
|
|
|
return len(replica.collections)
|
2021-01-19 11:37:16 +08:00
|
|
|
}
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
func (replica *CollectionSegmentReplica) addCollection(collectionID UniqueID, schema *schemapb.CollectionSchema) error {
|
2021-02-04 20:31:23 +08:00
|
|
|
replica.mu.Lock()
|
|
|
|
defer replica.mu.Unlock()
|
2021-01-22 09:36:40 +08:00
|
|
|
|
2021-02-04 20:31:23 +08:00
|
|
|
if _, ok := replica.collections[collectionID]; ok {
|
2021-03-05 10:15:27 +08:00
|
|
|
return fmt.Errorf("Create an existing collection=%s", schema.GetName())
|
2021-01-22 09:36:40 +08:00
|
|
|
}
|
2021-01-19 11:37:16 +08:00
|
|
|
|
2021-02-04 20:31:23 +08:00
|
|
|
newCollection, err := newCollection(collectionID, schema)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-01-19 11:37:16 +08:00
|
|
|
}
|
2021-01-28 15:20:24 +08:00
|
|
|
|
2021-02-04 20:31:23 +08:00
|
|
|
replica.collections[collectionID] = newCollection
|
2021-02-26 10:13:36 +08:00
|
|
|
log.Debug("Create collection", zap.String("collection name", newCollection.GetName()))
|
2021-02-04 20:31:23 +08:00
|
|
|
|
|
|
|
return nil
|
2021-01-19 11:37:16 +08:00
|
|
|
}
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
func (replica *CollectionSegmentReplica) removeCollection(collectionID UniqueID) error {
|
2021-02-04 20:31:23 +08:00
|
|
|
replica.mu.Lock()
|
|
|
|
defer replica.mu.Unlock()
|
2021-01-19 11:37:16 +08:00
|
|
|
|
2021-02-04 20:31:23 +08:00
|
|
|
delete(replica.collections, collectionID)
|
|
|
|
|
|
|
|
return nil
|
2021-01-19 11:37:16 +08:00
|
|
|
}
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
func (replica *CollectionSegmentReplica) getCollectionByID(collectionID UniqueID) (*Collection, error) {
|
2021-02-04 20:31:23 +08:00
|
|
|
replica.mu.RLock()
|
|
|
|
defer replica.mu.RUnlock()
|
2021-01-19 11:37:16 +08:00
|
|
|
|
2021-02-04 20:31:23 +08:00
|
|
|
coll, ok := replica.collections[collectionID]
|
|
|
|
if !ok {
|
2021-03-05 10:15:27 +08:00
|
|
|
return nil, fmt.Errorf("Cannot get collection %d by ID: not exist", collectionID)
|
2021-01-19 11:37:16 +08:00
|
|
|
}
|
|
|
|
|
2021-02-04 20:31:23 +08:00
|
|
|
return coll, nil
|
2021-01-19 11:37:16 +08:00
|
|
|
}
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
func (replica *CollectionSegmentReplica) hasCollection(collectionID UniqueID) bool {
|
2021-02-04 20:31:23 +08:00
|
|
|
replica.mu.RLock()
|
|
|
|
defer replica.mu.RUnlock()
|
2021-01-19 11:37:16 +08:00
|
|
|
|
2021-02-04 20:31:23 +08:00
|
|
|
_, ok := replica.collections[collectionID]
|
|
|
|
return ok
|
2021-01-19 11:37:16 +08:00
|
|
|
}
|