2021-10-15 18:07:09 +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-04-19 15:16:33 +08:00
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
2021-10-15 18:07:09 +08:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2021-04-19 15:16:33 +08:00
|
|
|
//
|
2021-10-15 18:07:09 +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-04-19 15:16:33 +08:00
|
|
|
|
2021-01-19 11:37:16 +08:00
|
|
|
package datanode
|
|
|
|
|
|
|
|
import (
|
2021-11-05 11:59:02 +08:00
|
|
|
"context"
|
2021-06-07 13:58:37 +08:00
|
|
|
"sync"
|
2021-11-11 20:56:49 +08:00
|
|
|
"sync/atomic"
|
2021-06-07 13:58:37 +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/msgstream"
|
2021-06-02 15:58:33 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/commonpb"
|
2021-06-07 13:58:37 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/datapb"
|
2021-06-02 15:58:33 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/internalpb"
|
2021-11-05 11:59:02 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/rootcoord"
|
2021-10-07 19:54:56 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/flowgraph"
|
2021-06-30 16:18:13 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/trace"
|
|
|
|
"github.com/opentracing/opentracing-go"
|
2021-01-19 11:37:16 +08:00
|
|
|
)
|
|
|
|
|
2021-10-07 19:54:56 +08:00
|
|
|
// make sure ddNode implements flowgraph.Node
|
|
|
|
var _ flowgraph.Node = (*ddNode)(nil)
|
|
|
|
|
2021-10-06 22:51:58 +08:00
|
|
|
// ddNode filter messages from message streams.
|
|
|
|
//
|
|
|
|
// ddNode recives all the messages from message stream dml channels, including insert messages,
|
|
|
|
// delete messages and ddl messages like CreateCollectionMsg.
|
|
|
|
//
|
|
|
|
// ddNode filters insert messages according to the `flushedSegment` and `FilterThreshold`.
|
|
|
|
// If the timestamp of the insert message is earlier than `FilterThreshold`, ddNode will
|
|
|
|
// filter out the insert message for those who belong to `flushedSegment`
|
|
|
|
//
|
|
|
|
// When receiving a `DropCollection` message, ddNode will send a signal to DataNode `BackgroundGC`
|
|
|
|
// goroutinue, telling DataNode to release the resources of this perticular flow graph.
|
|
|
|
//
|
|
|
|
// After the filtering process, ddNode passes all the valid insert messages and delete message
|
|
|
|
// to the following flow graph node, which in DataNode is `insertBufferNode`
|
2021-01-19 11:37:16 +08:00
|
|
|
type ddNode struct {
|
|
|
|
BaseNode
|
2021-06-07 11:25:37 +08:00
|
|
|
|
|
|
|
collectionID UniqueID
|
2021-06-07 13:58:37 +08:00
|
|
|
|
2021-09-07 15:41:59 +08:00
|
|
|
segID2SegInfo sync.Map // segment ID to *SegmentInfo
|
2021-10-08 19:09:12 +08:00
|
|
|
flushedSegments []*datapb.SegmentInfo
|
2021-11-05 11:59:02 +08:00
|
|
|
|
|
|
|
deltaMsgStream msgstream.MsgStream
|
2021-11-11 20:56:49 +08:00
|
|
|
dropMode atomic.Value
|
2021-01-19 11:37:16 +08:00
|
|
|
}
|
|
|
|
|
2021-10-07 19:54:56 +08:00
|
|
|
// Name returns node name, implementing flowgraph.Node
|
2021-06-02 15:58:33 +08:00
|
|
|
func (ddn *ddNode) Name() string {
|
2021-01-19 11:37:16 +08:00
|
|
|
return "ddNode"
|
|
|
|
}
|
|
|
|
|
2021-10-07 19:54:56 +08:00
|
|
|
// Operate handles input messages, implementing flowgrpah.Node
|
2021-09-07 15:41:59 +08:00
|
|
|
func (ddn *ddNode) Operate(in []Msg) []Msg {
|
2021-06-08 19:25:37 +08:00
|
|
|
// log.Debug("DDNode Operating")
|
|
|
|
|
2021-01-19 11:37:16 +08:00
|
|
|
if len(in) != 1 {
|
2021-09-07 15:41:59 +08:00
|
|
|
log.Warn("Invalid operate message input in ddNode", zap.Int("input length", len(in)))
|
|
|
|
return []Msg{}
|
2021-05-31 15:28:30 +08:00
|
|
|
}
|
|
|
|
|
2021-02-25 17:35:36 +08:00
|
|
|
msMsg, ok := in[0].(*MsgStreamMsg)
|
2021-01-19 11:37:16 +08:00
|
|
|
if !ok {
|
2021-09-07 15:41:59 +08:00
|
|
|
log.Warn("Type assertion failed for MsgStreamMsg")
|
|
|
|
return []Msg{}
|
2021-01-19 11:37:16 +08:00
|
|
|
}
|
|
|
|
|
2021-06-30 16:18:13 +08:00
|
|
|
var spans []opentracing.Span
|
|
|
|
for _, msg := range msMsg.TsMessages() {
|
|
|
|
sp, ctx := trace.StartSpanFromContext(msg.TraceCtx())
|
|
|
|
spans = append(spans, sp)
|
|
|
|
msg.SetTraceCtx(ctx)
|
|
|
|
}
|
|
|
|
|
2021-11-11 20:56:49 +08:00
|
|
|
if load := ddn.dropMode.Load(); load != nil && load.(bool) {
|
|
|
|
log.Debug("ddNode in dropMode")
|
|
|
|
return []Msg{}
|
|
|
|
}
|
|
|
|
|
2021-09-26 10:43:57 +08:00
|
|
|
var fgMsg = flowGraphMsg{
|
2021-06-02 15:58:33 +08:00
|
|
|
insertMessages: make([]*msgstream.InsertMsg, 0),
|
2021-01-19 11:37:16 +08:00
|
|
|
timeRange: TimeRange{
|
|
|
|
timestampMin: msMsg.TimestampMin(),
|
|
|
|
timestampMax: msMsg.TimestampMax(),
|
|
|
|
},
|
2021-06-02 15:58:33 +08:00
|
|
|
startPositions: make([]*internalpb.MsgPosition, 0),
|
|
|
|
endPositions: make([]*internalpb.MsgPosition, 0),
|
2021-11-11 20:56:49 +08:00
|
|
|
dropCollection: false,
|
2021-01-19 11:37:16 +08:00
|
|
|
}
|
|
|
|
|
2021-11-05 11:59:02 +08:00
|
|
|
forwardMsgs := make([]msgstream.TsMsg, 0)
|
2021-06-02 15:58:33 +08:00
|
|
|
for _, msg := range msMsg.TsMessages() {
|
|
|
|
switch msg.Type() {
|
|
|
|
case commonpb.MsgType_DropCollection:
|
2021-06-07 11:25:37 +08:00
|
|
|
if msg.(*msgstream.DropCollectionMsg).GetCollectionID() == ddn.collectionID {
|
2021-11-11 20:56:49 +08:00
|
|
|
log.Info("Receiving DropCollection msg", zap.Any("collectionID", ddn.collectionID))
|
|
|
|
ddn.dropMode.Store(true)
|
|
|
|
fgMsg.dropCollection = true
|
2021-06-07 11:25:37 +08:00
|
|
|
}
|
2021-06-02 15:58:33 +08:00
|
|
|
case commonpb.MsgType_Insert:
|
2021-10-11 16:31:44 +08:00
|
|
|
log.Debug("DDNode receive insert messages")
|
2021-08-31 18:35:58 +08:00
|
|
|
imsg := msg.(*msgstream.InsertMsg)
|
2021-09-07 15:41:59 +08:00
|
|
|
if imsg.CollectionID != ddn.collectionID {
|
|
|
|
//log.Debug("filter invalid InsertMsg, collection mis-match",
|
|
|
|
// zap.Int64("Get msg collID", imsg.CollectionID),
|
|
|
|
// zap.Int64("Expected collID", ddn.collectionID))
|
|
|
|
continue
|
|
|
|
}
|
2021-06-07 13:58:37 +08:00
|
|
|
if msg.EndTs() < FilterThreshold {
|
2021-06-08 19:25:37 +08:00
|
|
|
log.Info("Filtering Insert Messages",
|
|
|
|
zap.Uint64("Message endts", msg.EndTs()),
|
|
|
|
zap.Uint64("FilterThreshold", FilterThreshold),
|
|
|
|
)
|
2021-09-18 09:13:50 +08:00
|
|
|
if ddn.filterFlushedSegmentInsertMessages(imsg) {
|
2021-06-11 09:24:52 +08:00
|
|
|
continue
|
2021-06-07 13:58:37 +08:00
|
|
|
}
|
2021-05-31 15:28:30 +08:00
|
|
|
}
|
2021-09-26 10:43:57 +08:00
|
|
|
fgMsg.insertMessages = append(fgMsg.insertMessages, imsg)
|
2021-10-11 16:31:44 +08:00
|
|
|
case commonpb.MsgType_Delete:
|
|
|
|
log.Debug("DDNode receive delete messages")
|
|
|
|
dmsg := msg.(*msgstream.DeleteMsg)
|
2021-11-09 09:27:04 +08:00
|
|
|
for i := 0; i < len(dmsg.PrimaryKeys); i++ {
|
|
|
|
dmsg.HashValues = append(dmsg.HashValues, uint32(0))
|
|
|
|
}
|
|
|
|
forwardMsgs = append(forwardMsgs, dmsg)
|
2021-10-11 16:31:44 +08:00
|
|
|
if dmsg.CollectionID != ddn.collectionID {
|
|
|
|
//log.Debug("filter invalid DeleteMsg, collection mis-match",
|
|
|
|
// zap.Int64("Get msg collID", dmsg.CollectionID),
|
|
|
|
// zap.Int64("Expected collID", ddn.collectionID))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fgMsg.deleteMessages = append(fgMsg.deleteMessages, dmsg)
|
2021-01-19 11:37:16 +08:00
|
|
|
}
|
|
|
|
}
|
2021-11-05 11:59:02 +08:00
|
|
|
err := ddn.forwardDeleteMsg(forwardMsgs, msMsg.TimestampMin(), msMsg.TimestampMax())
|
|
|
|
if err != nil {
|
|
|
|
// TODO: proper deal with error
|
|
|
|
log.Warn("DDNode forward delete msg failed", zap.Error(err))
|
|
|
|
}
|
2021-01-19 11:37:16 +08:00
|
|
|
|
2021-09-26 10:43:57 +08:00
|
|
|
fgMsg.startPositions = append(fgMsg.startPositions, msMsg.StartPositions()...)
|
|
|
|
fgMsg.endPositions = append(fgMsg.endPositions, msMsg.EndPositions()...)
|
2021-01-22 19:36:09 +08:00
|
|
|
|
2021-06-30 16:18:13 +08:00
|
|
|
for _, sp := range spans {
|
|
|
|
sp.Finish()
|
|
|
|
}
|
|
|
|
|
2021-10-11 16:31:44 +08:00
|
|
|
return []Msg{&fgMsg}
|
2021-01-19 11:37:16 +08:00
|
|
|
}
|
|
|
|
|
2021-06-11 09:24:52 +08:00
|
|
|
func (ddn *ddNode) filterFlushedSegmentInsertMessages(msg *msgstream.InsertMsg) bool {
|
2021-06-07 13:58:37 +08:00
|
|
|
if ddn.isFlushed(msg.GetSegmentID()) {
|
2021-06-11 09:24:52 +08:00
|
|
|
return true
|
2021-06-07 13:58:37 +08:00
|
|
|
}
|
|
|
|
|
2021-09-07 15:41:59 +08:00
|
|
|
if si, ok := ddn.segID2SegInfo.Load(msg.GetSegmentID()); ok {
|
|
|
|
if msg.EndTs() <= si.(*datapb.SegmentInfo).GetDmlPosition().GetTimestamp() {
|
2021-06-11 09:24:52 +08:00
|
|
|
return true
|
2021-06-07 13:58:37 +08:00
|
|
|
}
|
|
|
|
|
2021-09-07 15:41:59 +08:00
|
|
|
ddn.segID2SegInfo.Delete(msg.GetSegmentID())
|
|
|
|
}
|
2021-06-11 09:24:52 +08:00
|
|
|
return false
|
2021-05-19 19:42:07 +08:00
|
|
|
}
|
|
|
|
|
2021-06-07 13:58:37 +08:00
|
|
|
func (ddn *ddNode) isFlushed(segmentID UniqueID) bool {
|
2021-10-08 19:09:12 +08:00
|
|
|
for _, s := range ddn.flushedSegments {
|
|
|
|
if s.ID == segmentID {
|
2021-06-07 13:58:37 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-11-05 11:59:02 +08:00
|
|
|
func (ddn *ddNode) forwardDeleteMsg(msgs []msgstream.TsMsg, minTs Timestamp, maxTs Timestamp) error {
|
|
|
|
if len(msgs) != 0 {
|
|
|
|
var msgPack = msgstream.MsgPack{
|
|
|
|
Msgs: msgs,
|
|
|
|
BeginTs: minTs,
|
|
|
|
EndTs: maxTs,
|
|
|
|
}
|
|
|
|
if err := ddn.deltaMsgStream.Produce(&msgPack); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := ddn.sendDeltaTimeTick(maxTs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ddn *ddNode) sendDeltaTimeTick(ts Timestamp) error {
|
|
|
|
msgPack := msgstream.MsgPack{}
|
|
|
|
baseMsg := msgstream.BaseMsg{
|
|
|
|
BeginTimestamp: ts,
|
|
|
|
EndTimestamp: ts,
|
|
|
|
HashValues: []uint32{0},
|
|
|
|
}
|
|
|
|
timeTickResult := internalpb.TimeTickMsg{
|
|
|
|
Base: &commonpb.MsgBase{
|
|
|
|
MsgType: commonpb.MsgType_TimeTick,
|
|
|
|
MsgID: 0,
|
|
|
|
Timestamp: ts,
|
|
|
|
SourceID: Params.NodeID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
timeTickMsg := &msgstream.TimeTickMsg{
|
|
|
|
BaseMsg: baseMsg,
|
|
|
|
TimeTickMsg: timeTickResult,
|
|
|
|
}
|
|
|
|
msgPack.Msgs = append(msgPack.Msgs, timeTickMsg)
|
|
|
|
|
|
|
|
if err := ddn.deltaMsgStream.Produce(&msgPack); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ddn *ddNode) Close() {
|
|
|
|
if ddn.deltaMsgStream != nil {
|
|
|
|
ddn.deltaMsgStream.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-11 20:56:49 +08:00
|
|
|
func newDDNode(ctx context.Context, collID UniqueID, vchanInfo *datapb.VchannelInfo, msFactory msgstream.Factory) *ddNode {
|
2021-01-19 11:37:16 +08:00
|
|
|
baseNode := BaseNode{}
|
2021-10-13 11:16:32 +08:00
|
|
|
baseNode.SetMaxQueueLength(Params.FlowGraphMaxQueueLength)
|
|
|
|
baseNode.SetMaxParallelism(Params.FlowGraphMaxParallelism)
|
2021-01-19 11:37:16 +08:00
|
|
|
|
2021-10-08 19:09:12 +08:00
|
|
|
fs := make([]*datapb.SegmentInfo, 0, len(vchanInfo.GetFlushedSegments()))
|
2021-09-07 15:41:59 +08:00
|
|
|
fs = append(fs, vchanInfo.GetFlushedSegments()...)
|
|
|
|
log.Debug("ddNode add flushed segment",
|
|
|
|
zap.Int64("collectionID", vchanInfo.GetCollectionID()),
|
|
|
|
zap.Int("No. Segment", len(vchanInfo.GetFlushedSegments())),
|
|
|
|
)
|
|
|
|
|
2021-11-05 11:59:02 +08:00
|
|
|
deltaStream, err := msFactory.NewMsgStream(ctx)
|
|
|
|
if err != nil {
|
2021-11-11 20:56:49 +08:00
|
|
|
log.Error(err.Error())
|
2021-11-05 11:59:02 +08:00
|
|
|
return nil
|
|
|
|
}
|
2021-11-09 09:27:04 +08:00
|
|
|
pChannelName := rootcoord.ToPhysicalChannel(vchanInfo.ChannelName)
|
|
|
|
deltaChannelName, err := rootcoord.ConvertChannelName(pChannelName, Params.DmlChannelName, Params.DeltaChannelName)
|
2021-11-05 11:59:02 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err.Error())
|
|
|
|
return nil
|
|
|
|
}
|
2021-11-11 20:56:49 +08:00
|
|
|
|
2021-11-09 09:27:04 +08:00
|
|
|
deltaStream.SetRepackFunc(msgstream.DefaultRepackFunc)
|
2021-11-05 11:59:02 +08:00
|
|
|
deltaStream.AsProducer([]string{deltaChannelName})
|
2021-11-09 09:27:04 +08:00
|
|
|
log.Debug("datanode AsProducer", zap.String("DeltaChannelName", deltaChannelName))
|
2021-11-05 11:59:02 +08:00
|
|
|
var deltaMsgStream msgstream.MsgStream = deltaStream
|
|
|
|
deltaMsgStream.Start()
|
|
|
|
|
2021-09-07 15:41:59 +08:00
|
|
|
dd := &ddNode{
|
|
|
|
BaseNode: baseNode,
|
|
|
|
collectionID: collID,
|
|
|
|
flushedSegments: fs,
|
2021-11-05 11:59:02 +08:00
|
|
|
deltaMsgStream: deltaMsgStream,
|
2021-06-07 13:58:37 +08:00
|
|
|
}
|
|
|
|
|
2021-11-11 20:56:49 +08:00
|
|
|
dd.dropMode.Store(false)
|
|
|
|
|
2021-09-07 15:41:59 +08:00
|
|
|
for _, us := range vchanInfo.GetUnflushedSegments() {
|
|
|
|
dd.segID2SegInfo.Store(us.GetID(), us)
|
2021-01-19 11:37:16 +08:00
|
|
|
}
|
2021-09-07 15:41:59 +08:00
|
|
|
|
|
|
|
log.Debug("ddNode add unflushed segment",
|
|
|
|
zap.Int64("collectionID", collID),
|
|
|
|
zap.Int("No. Segment", len(vchanInfo.GetUnflushedSegments())),
|
|
|
|
)
|
|
|
|
|
|
|
|
return dd
|
2021-01-19 11:37:16 +08:00
|
|
|
}
|