2021-04-19 13:47:10 +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-16 10:12:14 +08:00
|
|
|
package querynode
|
2020-12-10 16:31:09 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/golang/protobuf/proto"
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/trace"
|
2021-03-25 14:41:46 +08:00
|
|
|
"github.com/opentracing/opentracing-go"
|
2021-03-05 09:21:35 +08:00
|
|
|
"go.uber.org/zap"
|
2020-12-10 16:31:09 +08:00
|
|
|
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
|
|
|
"github.com/milvus-io/milvus/internal/msgstream"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/schemapb"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/flowgraph"
|
2020-12-10 16:31:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type ddNode struct {
|
2021-01-15 15:28:54 +08:00
|
|
|
baseNode
|
2020-12-10 16:31:09 +08:00
|
|
|
ddMsg *ddMsg
|
2021-03-05 16:52:45 +08:00
|
|
|
replica ReplicaInterface
|
2020-12-10 16:31:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ddNode *ddNode) Name() string {
|
|
|
|
return "ddNode"
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:41:46 +08:00
|
|
|
func (ddNode *ddNode) Operate(in []flowgraph.Msg) []flowgraph.Msg {
|
2021-03-05 09:21:35 +08:00
|
|
|
//log.Debug("Do filterDmNode operation")
|
2020-12-10 16:31:09 +08:00
|
|
|
|
|
|
|
if len(in) != 1 {
|
2021-03-05 09:21:35 +08:00
|
|
|
log.Error("Invalid operate message input in ddNode", zap.Int("input length", len(in)))
|
2020-12-10 16:31:09 +08:00
|
|
|
// TODO: add error handling
|
|
|
|
}
|
|
|
|
|
2021-02-25 17:35:36 +08:00
|
|
|
msMsg, ok := in[0].(*MsgStreamMsg)
|
2020-12-10 16:31:09 +08:00
|
|
|
if !ok {
|
2021-03-05 09:21:35 +08:00
|
|
|
log.Error("type assertion failed for MsgStreamMsg")
|
2020-12-10 16:31:09 +08:00
|
|
|
// TODO: add error handling
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:41:46 +08:00
|
|
|
var spans []opentracing.Span
|
|
|
|
for _, msg := range msMsg.TsMessages() {
|
|
|
|
sp, ctx := trace.StartSpanFromContext(msg.TraceCtx())
|
|
|
|
spans = append(spans, sp)
|
|
|
|
msg.SetTraceCtx(ctx)
|
|
|
|
}
|
|
|
|
|
2020-12-10 16:31:09 +08:00
|
|
|
var ddMsg = ddMsg{
|
2021-02-03 11:52:19 +08:00
|
|
|
collectionRecords: make(map[UniqueID][]metaOperateRecord),
|
2021-02-03 18:12:48 +08:00
|
|
|
partitionRecords: make(map[UniqueID][]metaOperateRecord),
|
2020-12-10 16:31:09 +08:00
|
|
|
timeRange: TimeRange{
|
|
|
|
timestampMin: msMsg.TimestampMin(),
|
|
|
|
timestampMax: msMsg.TimestampMax(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ddNode.ddMsg = &ddMsg
|
2021-01-06 16:44:12 +08:00
|
|
|
gcRecord := gcRecord{
|
|
|
|
collections: make([]UniqueID, 0),
|
|
|
|
partitions: make([]partitionWithID, 0),
|
|
|
|
}
|
|
|
|
ddNode.ddMsg.gcRecord = &gcRecord
|
2020-12-10 16:31:09 +08:00
|
|
|
|
|
|
|
// sort tsMessages
|
2021-02-07 09:30:48 +08:00
|
|
|
//tsMessages := msMsg.TsMessages()
|
|
|
|
//sort.Slice(tsMessages,
|
|
|
|
// func(i, j int) bool {
|
|
|
|
// return tsMessages[i].BeginTs() < tsMessages[j].BeginTs()
|
|
|
|
// })
|
2020-12-10 16:31:09 +08:00
|
|
|
|
|
|
|
// do dd tasks
|
2021-02-07 09:30:48 +08:00
|
|
|
//for _, msg := range tsMessages {
|
|
|
|
// switch msg.Type() {
|
|
|
|
// case commonpb.MsgType_kCreateCollection:
|
|
|
|
// ddNode.createCollection(msg.(*msgstream.CreateCollectionMsg))
|
|
|
|
// case commonpb.MsgType_kDropCollection:
|
|
|
|
// ddNode.dropCollection(msg.(*msgstream.DropCollectionMsg))
|
|
|
|
// case commonpb.MsgType_kCreatePartition:
|
|
|
|
// ddNode.createPartition(msg.(*msgstream.CreatePartitionMsg))
|
|
|
|
// case commonpb.MsgType_kDropPartition:
|
|
|
|
// ddNode.dropPartition(msg.(*msgstream.DropPartitionMsg))
|
|
|
|
// default:
|
|
|
|
// log.Println("Non supporting message type:", msg.Type())
|
|
|
|
// }
|
|
|
|
//}
|
2020-12-10 16:31:09 +08:00
|
|
|
|
|
|
|
var res Msg = ddNode.ddMsg
|
2021-03-25 14:41:46 +08:00
|
|
|
for _, span := range spans {
|
|
|
|
span.Finish()
|
|
|
|
}
|
|
|
|
return []Msg{res}
|
2020-12-10 16:31:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ddNode *ddNode) createCollection(msg *msgstream.CreateCollectionMsg) {
|
|
|
|
collectionID := msg.CollectionID
|
|
|
|
|
|
|
|
hasCollection := ddNode.replica.hasCollection(collectionID)
|
|
|
|
if hasCollection {
|
2021-03-05 09:21:35 +08:00
|
|
|
log.Debug("collection already exists", zap.Int64("collectionID", collectionID))
|
2020-12-10 16:31:09 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var schema schemapb.CollectionSchema
|
2021-01-18 19:32:08 +08:00
|
|
|
err := proto.Unmarshal(msg.Schema, &schema)
|
2020-12-10 16:31:09 +08:00
|
|
|
if err != nil {
|
2021-03-05 09:21:35 +08:00
|
|
|
log.Error(err.Error())
|
2020-12-10 16:31:09 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// add collection
|
2021-01-18 10:38:41 +08:00
|
|
|
err = ddNode.replica.addCollection(collectionID, &schema)
|
2020-12-10 16:31:09 +08:00
|
|
|
if err != nil {
|
2021-03-05 09:21:35 +08:00
|
|
|
log.Error(err.Error())
|
2020-12-10 16:31:09 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// add default partition
|
2021-02-03 18:12:48 +08:00
|
|
|
// TODO: allocate default partition id in master
|
|
|
|
err = ddNode.replica.addPartition(collectionID, UniqueID(2021))
|
2020-12-10 16:31:09 +08:00
|
|
|
if err != nil {
|
2021-03-05 09:21:35 +08:00
|
|
|
log.Error(err.Error())
|
2020-12-10 16:31:09 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-03 11:52:19 +08:00
|
|
|
ddNode.ddMsg.collectionRecords[collectionID] = append(ddNode.ddMsg.collectionRecords[collectionID],
|
2020-12-10 16:31:09 +08:00
|
|
|
metaOperateRecord{
|
|
|
|
createOrDrop: true,
|
2021-01-18 19:32:08 +08:00
|
|
|
timestamp: msg.Base.Timestamp,
|
2020-12-10 16:31:09 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ddNode *ddNode) dropCollection(msg *msgstream.DropCollectionMsg) {
|
|
|
|
collectionID := msg.CollectionID
|
|
|
|
|
2021-02-03 11:52:19 +08:00
|
|
|
ddNode.ddMsg.collectionRecords[collectionID] = append(ddNode.ddMsg.collectionRecords[collectionID],
|
2020-12-10 16:31:09 +08:00
|
|
|
metaOperateRecord{
|
|
|
|
createOrDrop: false,
|
2021-01-18 19:32:08 +08:00
|
|
|
timestamp: msg.Base.Timestamp,
|
2020-12-10 16:31:09 +08:00
|
|
|
})
|
2021-01-06 16:44:12 +08:00
|
|
|
|
|
|
|
ddNode.ddMsg.gcRecord.collections = append(ddNode.ddMsg.gcRecord.collections, collectionID)
|
2020-12-10 16:31:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ddNode *ddNode) createPartition(msg *msgstream.CreatePartitionMsg) {
|
|
|
|
collectionID := msg.CollectionID
|
2021-02-03 18:12:48 +08:00
|
|
|
partitionID := msg.PartitionID
|
2020-12-10 16:31:09 +08:00
|
|
|
|
2021-02-03 18:12:48 +08:00
|
|
|
err := ddNode.replica.addPartition(collectionID, partitionID)
|
2020-12-10 16:31:09 +08:00
|
|
|
if err != nil {
|
2021-03-05 09:21:35 +08:00
|
|
|
log.Error(err.Error())
|
2020-12-10 16:31:09 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-03 18:12:48 +08:00
|
|
|
ddNode.ddMsg.partitionRecords[partitionID] = append(ddNode.ddMsg.partitionRecords[partitionID],
|
2020-12-10 16:31:09 +08:00
|
|
|
metaOperateRecord{
|
|
|
|
createOrDrop: true,
|
2021-01-18 19:32:08 +08:00
|
|
|
timestamp: msg.Base.Timestamp,
|
2020-12-10 16:31:09 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ddNode *ddNode) dropPartition(msg *msgstream.DropPartitionMsg) {
|
|
|
|
collectionID := msg.CollectionID
|
2021-02-03 18:12:48 +08:00
|
|
|
partitionID := msg.PartitionID
|
2020-12-10 16:31:09 +08:00
|
|
|
|
2021-02-03 18:12:48 +08:00
|
|
|
ddNode.ddMsg.partitionRecords[partitionID] = append(ddNode.ddMsg.partitionRecords[partitionID],
|
2020-12-10 16:31:09 +08:00
|
|
|
metaOperateRecord{
|
|
|
|
createOrDrop: false,
|
2021-01-18 19:32:08 +08:00
|
|
|
timestamp: msg.Base.Timestamp,
|
2020-12-10 16:31:09 +08:00
|
|
|
})
|
2021-01-06 16:44:12 +08:00
|
|
|
|
|
|
|
ddNode.ddMsg.gcRecord.partitions = append(ddNode.ddMsg.gcRecord.partitions, partitionWithID{
|
2021-02-03 18:12:48 +08:00
|
|
|
partitionID: partitionID,
|
2021-01-06 16:44:12 +08:00
|
|
|
collectionID: collectionID,
|
|
|
|
})
|
2020-12-10 16:31:09 +08:00
|
|
|
}
|
|
|
|
|
2021-03-05 16:52:45 +08:00
|
|
|
func newDDNode(replica ReplicaInterface) *ddNode {
|
2020-12-10 16:31:09 +08:00
|
|
|
maxQueueLength := Params.FlowGraphMaxQueueLength
|
|
|
|
maxParallelism := Params.FlowGraphMaxParallelism
|
|
|
|
|
2021-01-15 15:28:54 +08:00
|
|
|
baseNode := baseNode{}
|
2020-12-10 16:31:09 +08:00
|
|
|
baseNode.SetMaxQueueLength(maxQueueLength)
|
|
|
|
baseNode.SetMaxParallelism(maxParallelism)
|
|
|
|
|
|
|
|
return &ddNode{
|
2021-01-15 15:28:54 +08:00
|
|
|
baseNode: baseNode,
|
2020-12-10 16:31:09 +08:00
|
|
|
replica: replica,
|
|
|
|
}
|
|
|
|
}
|