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 (
|
|
|
|
"context"
|
2021-06-04 19:20:34 +08:00
|
|
|
"fmt"
|
2021-01-19 11:37:16 +08:00
|
|
|
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
|
|
|
"github.com/milvus-io/milvus/internal/msgstream"
|
2021-06-04 19:20:34 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/commonpb"
|
2021-05-25 15:35:37 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/datapb"
|
2021-06-18 16:02:05 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/milvuspb"
|
2021-06-04 19:20:34 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/types"
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/flowgraph"
|
2021-02-26 10:13:36 +08:00
|
|
|
|
|
|
|
"go.uber.org/zap"
|
2021-01-19 11:37:16 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type dataSyncService struct {
|
2021-06-18 16:02:05 +08:00
|
|
|
ctx context.Context
|
|
|
|
cancelFn context.CancelFunc
|
|
|
|
fg *flowgraph.TimeTickedFlowGraph
|
|
|
|
flushChan <-chan *flushMsg
|
|
|
|
replica Replica
|
|
|
|
idAllocator allocatorInterface
|
|
|
|
msFactory msgstream.Factory
|
|
|
|
collectionID UniqueID
|
|
|
|
dataService types.DataService
|
|
|
|
masterService types.MasterService
|
|
|
|
clearSignal chan<- UniqueID
|
2021-01-19 11:37:16 +08:00
|
|
|
}
|
|
|
|
|
2021-05-25 15:35:37 +08:00
|
|
|
func newDataSyncService(ctx context.Context,
|
|
|
|
flushChan <-chan *flushMsg,
|
|
|
|
replica Replica,
|
|
|
|
alloc allocatorInterface,
|
|
|
|
factory msgstream.Factory,
|
2021-06-07 11:25:37 +08:00
|
|
|
vchan *datapb.VchannelInfo,
|
|
|
|
clearSignal chan<- UniqueID,
|
2021-06-08 19:25:37 +08:00
|
|
|
dataService types.DataService,
|
2021-06-18 16:02:05 +08:00
|
|
|
masterService types.MasterService,
|
2021-06-07 11:25:37 +08:00
|
|
|
|
|
|
|
) *dataSyncService {
|
|
|
|
|
|
|
|
ctx1, cancel := context.WithCancel(ctx)
|
2021-05-25 15:35:37 +08:00
|
|
|
|
2021-02-03 17:30:10 +08:00
|
|
|
service := &dataSyncService{
|
2021-06-18 16:02:05 +08:00
|
|
|
ctx: ctx1,
|
|
|
|
cancelFn: cancel,
|
|
|
|
fg: nil,
|
|
|
|
flushChan: flushChan,
|
|
|
|
replica: replica,
|
|
|
|
idAllocator: alloc,
|
|
|
|
msFactory: factory,
|
|
|
|
collectionID: vchan.GetCollectionID(),
|
|
|
|
dataService: dataService,
|
|
|
|
clearSignal: clearSignal,
|
|
|
|
masterService: masterService,
|
2021-01-19 11:37:16 +08:00
|
|
|
}
|
2021-05-25 15:35:37 +08:00
|
|
|
|
2021-06-04 09:57:54 +08:00
|
|
|
service.initNodes(vchan)
|
2021-02-03 17:30:10 +08:00
|
|
|
return service
|
2021-02-03 15:18:05 +08:00
|
|
|
}
|
|
|
|
|
2021-02-03 17:30:10 +08:00
|
|
|
func (dsService *dataSyncService) start() {
|
2021-05-08 14:07:04 +08:00
|
|
|
if dsService.fg != nil {
|
2021-06-07 11:25:37 +08:00
|
|
|
log.Debug("Data Sync Service starting flowgraph")
|
2021-05-08 14:07:04 +08:00
|
|
|
dsService.fg.Start()
|
|
|
|
} else {
|
|
|
|
log.Debug("Data Sync Service flowgraph nil")
|
|
|
|
}
|
2021-01-19 11:37:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (dsService *dataSyncService) close() {
|
|
|
|
if dsService.fg != nil {
|
2021-06-07 11:25:37 +08:00
|
|
|
log.Debug("Data Sync Service closing flowgraph")
|
2021-01-19 11:37:16 +08:00
|
|
|
dsService.fg.Close()
|
|
|
|
}
|
2021-06-07 11:25:37 +08:00
|
|
|
|
|
|
|
dsService.cancelFn()
|
2021-01-19 11:37:16 +08:00
|
|
|
}
|
|
|
|
|
2021-06-18 16:02:05 +08:00
|
|
|
func (dsService *dataSyncService) getPChannel(collectionID UniqueID, vchan string) (string, error) {
|
|
|
|
req := &milvuspb.DescribeCollectionRequest{
|
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
MsgType: commonpb.MsgType_DescribeCollection,
|
|
|
|
MsgID: 0,
|
|
|
|
Timestamp: 0,
|
|
|
|
SourceID: Params.NodeID,
|
|
|
|
},
|
|
|
|
DbName: "",
|
|
|
|
CollectionName: "",
|
|
|
|
CollectionID: collectionID,
|
|
|
|
TimeStamp: 0,
|
|
|
|
}
|
|
|
|
resp, err := dsService.masterService.DescribeCollection(dsService.ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to describe collection", zap.Int64("collectionID", collectionID))
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Status.ErrorCode != commonpb.ErrorCode_Success {
|
|
|
|
log.Error("Failed to describe collection", zap.Int64("collectionID", collectionID),
|
|
|
|
zap.String("Reason", resp.Status.GetReason()))
|
|
|
|
return "", fmt.Errorf("Failed to describe collection, resp.Reason: %s", resp.Status.GetReason())
|
|
|
|
}
|
|
|
|
|
|
|
|
vchans := resp.GetVirtualChannelNames()
|
|
|
|
pchans := resp.GetPhysicalChannelNames()
|
|
|
|
for i, v := range vchans {
|
|
|
|
if vchan == v {
|
|
|
|
return pchans[i], nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", fmt.Errorf("Can not find physical channel of %s", vchan)
|
|
|
|
}
|
|
|
|
|
2021-06-07 11:25:37 +08:00
|
|
|
func (dsService *dataSyncService) initNodes(vchanInfo *datapb.VchannelInfo) {
|
2021-01-19 11:37:16 +08:00
|
|
|
// TODO: add delete pipeline support
|
|
|
|
dsService.fg = flowgraph.NewTimeTickedFlowGraph(dsService.ctx)
|
|
|
|
|
2021-02-08 14:30:54 +08:00
|
|
|
m := map[string]interface{}{
|
|
|
|
"PulsarAddress": Params.PulsarAddress,
|
|
|
|
"ReceiveBufSize": 1024,
|
2021-05-25 15:35:37 +08:00
|
|
|
"PulsarBufSize": 1024,
|
|
|
|
}
|
|
|
|
|
2021-05-28 14:54:31 +08:00
|
|
|
err := dsService.msFactory.SetParams(m)
|
2021-02-08 14:30:54 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-06-06 13:21:37 +08:00
|
|
|
saveBinlog := func(fu *segmentFlushUnit) error {
|
2021-06-04 19:20:34 +08:00
|
|
|
id2path := []*datapb.ID2PathList{}
|
|
|
|
checkPoints := []*datapb.CheckPoint{}
|
|
|
|
for k, v := range fu.field2Path {
|
|
|
|
id2path = append(id2path, &datapb.ID2PathList{ID: k, Paths: []string{v}})
|
|
|
|
}
|
2021-06-06 13:21:37 +08:00
|
|
|
for k, v := range fu.checkPoint {
|
2021-06-04 19:20:34 +08:00
|
|
|
v := v
|
|
|
|
checkPoints = append(checkPoints, &datapb.CheckPoint{
|
|
|
|
SegmentID: k,
|
2021-06-06 13:21:37 +08:00
|
|
|
NumOfRows: v.numRows,
|
|
|
|
Position: &v.pos,
|
2021-06-04 19:20:34 +08:00
|
|
|
})
|
|
|
|
}
|
2021-06-08 19:25:37 +08:00
|
|
|
log.Debug("SaveBinlogPath",
|
|
|
|
zap.Int64("SegmentID", fu.segID),
|
|
|
|
zap.Int64("CollectionID", fu.collID),
|
|
|
|
zap.Int("Length of Field2BinlogPaths", len(id2path)),
|
2021-06-11 19:15:48 +08:00
|
|
|
zap.Any("Start Positions", fu.startPositions),
|
2021-06-08 19:25:37 +08:00
|
|
|
)
|
2021-06-04 19:20:34 +08:00
|
|
|
|
|
|
|
req := &datapb.SaveBinlogPathsRequest{
|
|
|
|
Base: &commonpb.MsgBase{
|
2021-06-08 19:25:37 +08:00
|
|
|
MsgType: 0, //TODO msg type
|
|
|
|
MsgID: 0, //TODO msg id
|
|
|
|
Timestamp: 0, //TODO time stamp
|
2021-06-04 19:20:34 +08:00
|
|
|
SourceID: Params.NodeID,
|
|
|
|
},
|
|
|
|
SegmentID: fu.segID,
|
2021-06-08 19:25:37 +08:00
|
|
|
CollectionID: fu.collID,
|
2021-06-04 19:20:34 +08:00
|
|
|
Field2BinlogPaths: id2path,
|
|
|
|
CheckPoints: checkPoints,
|
2021-06-11 19:15:48 +08:00
|
|
|
StartPositions: fu.startPositions,
|
2021-06-04 19:20:34 +08:00
|
|
|
Flushed: fu.flushed,
|
|
|
|
}
|
|
|
|
rsp, err := dsService.dataService.SaveBinlogPaths(dsService.ctx, req)
|
|
|
|
if err != nil {
|
2021-06-08 19:25:37 +08:00
|
|
|
return fmt.Errorf(err.Error())
|
2021-06-04 19:20:34 +08:00
|
|
|
}
|
|
|
|
if rsp.ErrorCode != commonpb.ErrorCode_Success {
|
|
|
|
return fmt.Errorf("data service save bin log path failed, reason = %s", rsp.Reason)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-06-18 16:02:05 +08:00
|
|
|
|
|
|
|
pchan, err := dsService.getPChannel(vchanInfo.GetCollectionID(), vchanInfo.GetChannelName())
|
|
|
|
if err != nil {
|
|
|
|
//FIXME dont panic
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-06-07 13:58:37 +08:00
|
|
|
var dmStreamNode Node = newDmInputNode(
|
|
|
|
dsService.ctx,
|
|
|
|
dsService.msFactory,
|
2021-06-18 16:02:05 +08:00
|
|
|
pchan,
|
2021-06-07 13:58:37 +08:00
|
|
|
vchanInfo.GetSeekPosition(),
|
|
|
|
)
|
|
|
|
var ddNode Node = newDDNode(dsService.clearSignal, dsService.collectionID, vchanInfo)
|
2021-06-04 16:31:34 +08:00
|
|
|
var insertBufferNode Node = newInsertBufferNode(
|
|
|
|
dsService.ctx,
|
|
|
|
dsService.replica,
|
|
|
|
dsService.msFactory,
|
|
|
|
dsService.idAllocator,
|
|
|
|
dsService.flushChan,
|
2021-06-04 19:20:34 +08:00
|
|
|
saveBinlog,
|
2021-06-08 19:25:37 +08:00
|
|
|
vchanInfo.GetChannelName(),
|
2021-06-04 16:31:34 +08:00
|
|
|
)
|
2021-01-19 11:37:16 +08:00
|
|
|
|
2021-06-08 19:25:37 +08:00
|
|
|
// recover segment checkpoints
|
|
|
|
for _, us := range vchanInfo.GetUnflushedSegments() {
|
|
|
|
if us.CollectionID != dsService.collectionID ||
|
|
|
|
us.GetInsertChannel() != vchanInfo.ChannelName {
|
2021-06-11 09:24:52 +08:00
|
|
|
log.Warn("Collection ID or ChannelName not compact",
|
|
|
|
zap.Int64("Wanted ID", dsService.collectionID),
|
|
|
|
zap.Int64("Actual ID", us.CollectionID),
|
|
|
|
zap.String("Wanted Channel Name", vchanInfo.ChannelName),
|
|
|
|
zap.String("Actual Channel Name", us.GetInsertChannel()),
|
|
|
|
)
|
2021-06-08 19:25:37 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-06-11 09:24:52 +08:00
|
|
|
log.Info("Recover Segment NumOfRows form checkpoints",
|
|
|
|
zap.String("InsertChannel", us.GetInsertChannel()),
|
|
|
|
zap.Int64("SegmentID", us.GetID()),
|
|
|
|
zap.Int64("NumOfRows", us.GetNumOfRows()),
|
|
|
|
)
|
2021-06-08 19:25:37 +08:00
|
|
|
dsService.replica.addSegment(us.GetID(), us.CollectionID, us.PartitionID, us.GetInsertChannel())
|
|
|
|
dsService.replica.updateStatistics(us.GetID(), us.GetNumOfRows())
|
|
|
|
}
|
|
|
|
|
2021-02-25 17:35:36 +08:00
|
|
|
dsService.fg.AddNode(dmStreamNode)
|
|
|
|
dsService.fg.AddNode(ddNode)
|
|
|
|
dsService.fg.AddNode(insertBufferNode)
|
2021-01-19 11:37:16 +08:00
|
|
|
|
|
|
|
// ddStreamNode
|
2021-06-02 15:58:33 +08:00
|
|
|
err = dsService.fg.SetEdges(dmStreamNode.Name(),
|
2021-01-19 11:37:16 +08:00
|
|
|
[]string{},
|
|
|
|
[]string{ddNode.Name()},
|
|
|
|
)
|
|
|
|
if err != nil {
|
2021-06-02 15:58:33 +08:00
|
|
|
log.Error("set edges failed in node", zap.String("name", dmStreamNode.Name()), zap.Error(err))
|
2021-02-26 10:13:36 +08:00
|
|
|
panic("set edges faild in the node")
|
2021-01-19 11:37:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ddNode
|
|
|
|
err = dsService.fg.SetEdges(ddNode.Name(),
|
2021-06-02 15:58:33 +08:00
|
|
|
[]string{dmStreamNode.Name()},
|
|
|
|
[]string{insertBufferNode.Name()},
|
2021-01-19 11:37:16 +08:00
|
|
|
)
|
|
|
|
if err != nil {
|
2021-02-26 10:13:36 +08:00
|
|
|
log.Error("set edges failed in node", zap.String("name", ddNode.Name()), zap.Error(err))
|
|
|
|
panic("set edges faild in the node")
|
2021-01-19 11:37:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// insertBufferNode
|
|
|
|
err = dsService.fg.SetEdges(insertBufferNode.Name(),
|
2021-06-02 15:58:33 +08:00
|
|
|
[]string{ddNode.Name()},
|
|
|
|
[]string{},
|
2021-01-19 11:37:16 +08:00
|
|
|
)
|
|
|
|
if err != nil {
|
2021-02-26 10:13:36 +08:00
|
|
|
log.Error("set edges failed in node", zap.String("name", insertBufferNode.Name()), zap.Error(err))
|
|
|
|
panic("set edges faild in the node")
|
2021-01-19 11:37:16 +08:00
|
|
|
}
|
|
|
|
}
|