2022-07-11 14:38:24 +08:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
2022-10-16 20:49:27 +08:00
|
|
|
"github.com/milvus-io/milvus-proto/go-api/commonpb"
|
|
|
|
"github.com/milvus-io/milvus-proto/go-api/milvuspb"
|
|
|
|
"github.com/milvus-io/milvus-proto/go-api/schemapb"
|
2022-07-11 14:38:24 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/allocator"
|
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
|
|
|
"github.com/milvus-io/milvus/internal/metrics"
|
|
|
|
"github.com/milvus-io/milvus/internal/mq/msgstream"
|
2022-11-04 14:25:38 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/paramtable"
|
2022-07-11 14:38:24 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/timerecord"
|
2023-01-12 16:09:39 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/typeutil"
|
|
|
|
"go.opentelemetry.io/otel"
|
2022-07-11 14:38:24 +08:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type insertTask struct {
|
|
|
|
// req *milvuspb.InsertRequest
|
|
|
|
Condition
|
2022-12-08 18:37:19 +08:00
|
|
|
insertMsg *BaseInsertTask
|
|
|
|
ctx context.Context
|
2022-07-11 14:38:24 +08:00
|
|
|
|
|
|
|
result *milvuspb.MutationResult
|
|
|
|
idAllocator *allocator.IDAllocator
|
|
|
|
segIDAssigner *segIDAssigner
|
|
|
|
chMgr channelsMgr
|
|
|
|
chTicker channelsTimeTicker
|
|
|
|
vChannels []vChan
|
|
|
|
pChannels []pChan
|
|
|
|
schema *schemapb.CollectionSchema
|
|
|
|
}
|
|
|
|
|
|
|
|
// TraceCtx returns insertTask context
|
|
|
|
func (it *insertTask) TraceCtx() context.Context {
|
|
|
|
return it.ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (it *insertTask) ID() UniqueID {
|
2022-12-08 18:37:19 +08:00
|
|
|
return it.insertMsg.Base.MsgID
|
2022-07-11 14:38:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (it *insertTask) SetID(uid UniqueID) {
|
2022-12-08 18:37:19 +08:00
|
|
|
it.insertMsg.Base.MsgID = uid
|
2022-07-11 14:38:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (it *insertTask) Name() string {
|
|
|
|
return InsertTaskName
|
|
|
|
}
|
|
|
|
|
|
|
|
func (it *insertTask) Type() commonpb.MsgType {
|
2022-12-08 18:37:19 +08:00
|
|
|
return it.insertMsg.Base.MsgType
|
2022-07-11 14:38:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (it *insertTask) BeginTs() Timestamp {
|
2022-12-08 18:37:19 +08:00
|
|
|
return it.insertMsg.BeginTimestamp
|
2022-07-11 14:38:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (it *insertTask) SetTs(ts Timestamp) {
|
2022-12-08 18:37:19 +08:00
|
|
|
it.insertMsg.BeginTimestamp = ts
|
|
|
|
it.insertMsg.EndTimestamp = ts
|
2022-07-11 14:38:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (it *insertTask) EndTs() Timestamp {
|
2022-12-08 18:37:19 +08:00
|
|
|
return it.insertMsg.EndTimestamp
|
2022-07-11 14:38:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (it *insertTask) getChannels() ([]pChan, error) {
|
2022-12-08 18:37:19 +08:00
|
|
|
collID, err := globalMetaCache.GetCollectionID(it.ctx, it.insertMsg.CollectionName)
|
2022-07-11 14:38:24 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return it.chMgr.getChannels(collID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (it *insertTask) OnEnqueue() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (it *insertTask) PreExecute(ctx context.Context) error {
|
2023-01-12 16:09:39 +08:00
|
|
|
ctx, sp := otel.Tracer(typeutil.ProxyRole).Start(ctx, "Insert-PreExecute")
|
|
|
|
defer sp.End()
|
2022-07-11 14:38:24 +08:00
|
|
|
|
|
|
|
it.result = &milvuspb.MutationResult{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_Success,
|
|
|
|
},
|
|
|
|
IDs: &schemapb.IDs{
|
|
|
|
IdField: nil,
|
|
|
|
},
|
|
|
|
Timestamp: it.EndTs(),
|
|
|
|
}
|
|
|
|
|
2022-12-08 18:37:19 +08:00
|
|
|
collectionName := it.insertMsg.CollectionName
|
2022-07-11 14:38:24 +08:00
|
|
|
if err := validateCollectionName(collectionName); err != nil {
|
2022-11-14 15:29:06 +08:00
|
|
|
log.Error("valid collection name failed", zap.String("collectionName", collectionName), zap.Error(err))
|
2022-07-11 14:38:24 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-08 18:37:19 +08:00
|
|
|
partitionTag := it.insertMsg.PartitionName
|
2022-07-11 14:38:24 +08:00
|
|
|
if err := validatePartitionTag(partitionTag, true); err != nil {
|
|
|
|
log.Error("valid partition name failed", zap.String("partition name", partitionTag), zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-04 17:21:36 +08:00
|
|
|
schema, err := globalMetaCache.GetCollectionSchema(ctx, collectionName)
|
2022-07-11 14:38:24 +08:00
|
|
|
if err != nil {
|
2022-11-14 15:29:06 +08:00
|
|
|
log.Error("get collection schema from global meta cache failed", zap.String("collectionName", collectionName), zap.Error(err))
|
2022-07-11 14:38:24 +08:00
|
|
|
return err
|
|
|
|
}
|
2023-01-04 17:21:36 +08:00
|
|
|
it.schema = schema
|
2022-07-11 14:38:24 +08:00
|
|
|
|
2022-12-08 18:37:19 +08:00
|
|
|
rowNums := uint32(it.insertMsg.NRows())
|
2022-07-11 14:38:24 +08:00
|
|
|
// set insertTask.rowIDs
|
|
|
|
var rowIDBegin UniqueID
|
|
|
|
var rowIDEnd UniqueID
|
|
|
|
tr := timerecord.NewTimeRecorder("applyPK")
|
|
|
|
rowIDBegin, rowIDEnd, _ = it.idAllocator.Alloc(rowNums)
|
2022-11-29 15:23:14 +08:00
|
|
|
metrics.ProxyApplyPrimaryKeyLatency.WithLabelValues(strconv.FormatInt(paramtable.GetNodeID(), 10)).Observe(float64(tr.ElapseSpan().Milliseconds()))
|
2022-07-11 14:38:24 +08:00
|
|
|
|
2022-12-08 18:37:19 +08:00
|
|
|
it.insertMsg.RowIDs = make([]UniqueID, rowNums)
|
2022-07-11 14:38:24 +08:00
|
|
|
for i := rowIDBegin; i < rowIDEnd; i++ {
|
|
|
|
offset := i - rowIDBegin
|
2022-12-08 18:37:19 +08:00
|
|
|
it.insertMsg.RowIDs[offset] = i
|
2022-07-11 14:38:24 +08:00
|
|
|
}
|
|
|
|
// set insertTask.timeStamps
|
2022-12-08 18:37:19 +08:00
|
|
|
rowNum := it.insertMsg.NRows()
|
|
|
|
it.insertMsg.Timestamps = make([]uint64, rowNum)
|
|
|
|
for index := range it.insertMsg.Timestamps {
|
|
|
|
it.insertMsg.Timestamps[index] = it.insertMsg.BeginTimestamp
|
2022-07-11 14:38:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// set result.SuccIndex
|
|
|
|
sliceIndex := make([]uint32, rowNums)
|
|
|
|
for i := uint32(0); i < rowNums; i++ {
|
|
|
|
sliceIndex[i] = i
|
|
|
|
}
|
|
|
|
it.result.SuccIndex = sliceIndex
|
|
|
|
|
|
|
|
// check primaryFieldData whether autoID is true or not
|
|
|
|
// set rowIDs as primary data if autoID == true
|
2022-12-08 18:37:19 +08:00
|
|
|
// TODO(dragondriver): in fact, NumRows is not trustable, we should check all input fields
|
2023-01-17 17:53:42 +08:00
|
|
|
it.result.IDs, err = checkPrimaryFieldData(it.schema, it.result, it.insertMsg, true)
|
2022-11-14 15:29:06 +08:00
|
|
|
log := log.Ctx(ctx).With(zap.String("collectionName", collectionName))
|
2022-07-11 14:38:24 +08:00
|
|
|
if err != nil {
|
2022-11-14 15:29:06 +08:00
|
|
|
log.Error("check primary field data and hash primary key failed",
|
|
|
|
zap.Error(err))
|
2022-07-11 14:38:24 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// set field ID to insert field data
|
2023-01-04 17:21:36 +08:00
|
|
|
err = fillFieldIDBySchema(it.insertMsg.GetFieldsData(), schema)
|
2022-07-11 14:38:24 +08:00
|
|
|
if err != nil {
|
2022-11-14 15:29:06 +08:00
|
|
|
log.Error("set fieldID to fieldData failed",
|
|
|
|
zap.Error(err))
|
2022-07-11 14:38:24 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-24 17:09:58 +08:00
|
|
|
if err := newValidateUtil(withNANCheck()).Validate(it.insertMsg.GetFieldsData(), schema, it.insertMsg.NRows()); err != nil {
|
2023-03-14 14:03:58 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-14 15:29:06 +08:00
|
|
|
log.Debug("Proxy Insert PreExecute done")
|
2022-07-11 14:38:24 +08:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (it *insertTask) Execute(ctx context.Context) error {
|
2023-01-12 16:09:39 +08:00
|
|
|
ctx, sp := otel.Tracer(typeutil.ProxyRole).Start(ctx, "Proxy-Insert-PreExecute")
|
|
|
|
defer sp.End()
|
2022-07-11 14:38:24 +08:00
|
|
|
|
|
|
|
tr := timerecord.NewTimeRecorder(fmt.Sprintf("proxy execute insert %d", it.ID()))
|
|
|
|
defer tr.Elapse("insert execute done")
|
|
|
|
|
2022-12-08 18:37:19 +08:00
|
|
|
collectionName := it.insertMsg.CollectionName
|
2022-07-11 14:38:24 +08:00
|
|
|
collID, err := globalMetaCache.GetCollectionID(ctx, collectionName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-08 18:37:19 +08:00
|
|
|
it.insertMsg.CollectionID = collID
|
2022-07-11 14:38:24 +08:00
|
|
|
var partitionID UniqueID
|
2022-12-08 18:37:19 +08:00
|
|
|
if len(it.insertMsg.PartitionName) > 0 {
|
|
|
|
partitionID, err = globalMetaCache.GetPartitionID(ctx, collectionName, it.insertMsg.PartitionName)
|
2022-07-11 14:38:24 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2022-12-07 18:01:19 +08:00
|
|
|
partitionID, err = globalMetaCache.GetPartitionID(ctx, collectionName, Params.CommonCfg.DefaultPartitionName.GetValue())
|
2022-07-11 14:38:24 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-12-08 18:37:19 +08:00
|
|
|
it.insertMsg.PartitionID = partitionID
|
2022-07-11 14:38:24 +08:00
|
|
|
tr.Record("get collection id & partition id from cache")
|
|
|
|
|
|
|
|
stream, err := it.chMgr.getOrCreateDmlStream(collID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tr.Record("get used message stream")
|
|
|
|
|
|
|
|
channelNames, err := it.chMgr.getVChannels(collID)
|
|
|
|
if err != nil {
|
2022-11-14 15:29:06 +08:00
|
|
|
log.Ctx(ctx).Error("get vChannels failed",
|
|
|
|
zap.Int64("collectionID", collID),
|
|
|
|
zap.Error(err))
|
2022-07-11 14:38:24 +08:00
|
|
|
it.result.Status.ErrorCode = commonpb.ErrorCode_UnexpectedError
|
|
|
|
it.result.Status.Reason = err.Error()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-09 15:05:08 +08:00
|
|
|
log.Ctx(ctx).Debug("send insert request to virtual channels",
|
2022-12-08 18:37:19 +08:00
|
|
|
zap.String("collection", it.insertMsg.GetCollectionName()),
|
|
|
|
zap.String("partition", it.insertMsg.GetPartitionName()),
|
2022-07-11 14:38:24 +08:00
|
|
|
zap.Int64("collection_id", collID),
|
|
|
|
zap.Int64("partition_id", partitionID),
|
|
|
|
zap.Strings("virtual_channels", channelNames),
|
|
|
|
zap.Int64("task_id", it.ID()))
|
|
|
|
|
|
|
|
// assign segmentID for insert data and repack data by segmentID
|
2022-12-08 18:37:19 +08:00
|
|
|
var msgPack *msgstream.MsgPack
|
|
|
|
msgPack, err = assignSegmentID(it.TraceCtx(), it.insertMsg, it.result, channelNames, it.idAllocator, it.segIDAssigner)
|
2022-07-11 14:38:24 +08:00
|
|
|
if err != nil {
|
2022-11-14 15:29:06 +08:00
|
|
|
log.Error("assign segmentID and repack insert data failed",
|
|
|
|
zap.Int64("collectionID", collID),
|
|
|
|
zap.Error(err))
|
2022-07-11 14:38:24 +08:00
|
|
|
it.result.Status.ErrorCode = commonpb.ErrorCode_UnexpectedError
|
|
|
|
it.result.Status.Reason = err.Error()
|
|
|
|
return err
|
|
|
|
}
|
2022-11-14 15:29:06 +08:00
|
|
|
log.Debug("assign segmentID for insert data success",
|
|
|
|
zap.Int64("collectionID", collID),
|
2022-12-08 18:37:19 +08:00
|
|
|
zap.String("collectionName", it.insertMsg.CollectionName))
|
2022-07-11 14:38:24 +08:00
|
|
|
tr.Record("assign segment id")
|
|
|
|
err = stream.Produce(msgPack)
|
|
|
|
if err != nil {
|
|
|
|
it.result.Status.ErrorCode = commonpb.ErrorCode_UnexpectedError
|
|
|
|
it.result.Status.Reason = err.Error()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sendMsgDur := tr.Record("send insert request to dml channel")
|
2022-11-04 14:25:38 +08:00
|
|
|
metrics.ProxySendMutationReqLatency.WithLabelValues(strconv.FormatInt(paramtable.GetNodeID(), 10), metrics.InsertLabel).Observe(float64(sendMsgDur.Milliseconds()))
|
2022-07-11 14:38:24 +08:00
|
|
|
|
2022-11-14 15:29:06 +08:00
|
|
|
log.Debug("Proxy Insert Execute done",
|
|
|
|
zap.String("collectionName", collectionName))
|
2022-07-11 14:38:24 +08:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (it *insertTask) PostExecute(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|