task.go 53.1 KB
Newer Older
1
package proxynode
Z
zhenshan.cao 已提交
2 3

import (
G
godchen 已提交
4
	"context"
5
	"fmt"
Z
zhenshan.cao 已提交
6
	"log"
N
neza2017 已提交
7 8 9
	"math"
	"strconv"

S
sunby 已提交
10 11
	"errors"

N
neza2017 已提交
12
	"github.com/golang/protobuf/proto"
13
	"github.com/zilliztech/milvus-distributed/internal/allocator"
14 15
	"github.com/zilliztech/milvus-distributed/internal/msgstream"
	"github.com/zilliztech/milvus-distributed/internal/proto/commonpb"
16 17
	"github.com/zilliztech/milvus-distributed/internal/proto/datapb"
	"github.com/zilliztech/milvus-distributed/internal/proto/indexpb"
18 19
	"github.com/zilliztech/milvus-distributed/internal/proto/internalpb2"
	"github.com/zilliztech/milvus-distributed/internal/proto/milvuspb"
20
	"github.com/zilliztech/milvus-distributed/internal/proto/querypb"
N
neza2017 已提交
21
	"github.com/zilliztech/milvus-distributed/internal/proto/schemapb"
C
cai.zhang 已提交
22
	"github.com/zilliztech/milvus-distributed/internal/util/typeutil"
Z
zhenshan.cao 已提交
23 24
)

S
sunby 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
const (
	InsertTaskName                  = "InsertTask"
	CreateCollectionTaskName        = "CreateCollectionTask"
	DropCollectionTaskName          = "DropCollectionTask"
	SearchTaskName                  = "SearchTask"
	HasCollectionTaskName           = "HasCollectionTask"
	DescribeCollectionTaskName      = "DescribeCollectionTask"
	GetCollectionStatisticsTaskName = "GetCollectionStatisticsTask"
	ShowCollectionTaskName          = "ShowCollectionTask"
	CreatePartitionTaskName         = "CreatePartitionTask"
	DropPartitionTaskName           = "DropPartitionTask"
	HasPartitionTaskName            = "HasPartitionTask"
	ShowPartitionTaskName           = "ShowPartitionTask"
	CreateIndexTaskName             = "CreateIndexTask"
	DescribeIndexTaskName           = "DescribeIndexTask"
	DropIndexTaskName               = "DropIndexTask"
	GetIndexStateTaskName           = "GetIndexStateTask"
	FlushTaskName                   = "FlushTask"
	LoadCollectionTaskName          = "LoadCollectionTask"
	ReleaseCollectionTaskName       = "ReleaseCollectionTask"
	LoadPartitionTaskName           = "LoadPartitionTask"
	ReleasePartitionTaskName        = "ReleasePartitionTask"
)

Z
zhenshan.cao 已提交
49
type task interface {
S
sunby 已提交
50
	Ctx() context.Context
51 52
	ID() UniqueID       // return ReqID
	SetID(uid UniqueID) // set ReqID
S
sunby 已提交
53
	Name() string
54
	Type() commonpb.MsgType
55 56
	BeginTs() Timestamp
	EndTs() Timestamp
Z
zhenshan.cao 已提交
57
	SetTs(ts Timestamp)
58
	OnEnqueue() error
S
sunby 已提交
59 60 61
	PreExecute(ctx context.Context) error
	Execute(ctx context.Context) error
	PostExecute(ctx context.Context) error
Z
zhenshan.cao 已提交
62
	WaitToFinish() error
63
	Notify(err error)
Z
zhenshan.cao 已提交
64 65
}

66
type BaseInsertTask = msgstream.InsertMsg
67 68

type InsertTask struct {
69
	BaseInsertTask
D
dragondriver 已提交
70
	Condition
S
sunby 已提交
71
	ctx               context.Context
72 73 74
	dataServiceClient DataServiceClient
	result            *milvuspb.InsertResponse
	rowIDAllocator    *allocator.IDAllocator
75 76
}

S
sunby 已提交
77 78 79 80 81 82
func (it *InsertTask) Ctx() context.Context {
	return it.ctx
}

func (it *InsertTask) ID() UniqueID {
	return it.Base.MsgID
83 84
}

85
func (it *InsertTask) SetID(uid UniqueID) {
86
	it.Base.MsgID = uid
87 88
}

S
sunby 已提交
89 90 91 92 93 94 95 96 97 98 99 100
func (it *InsertTask) Name() string {
	return InsertTaskName
}

func (it *InsertTask) Type() commonpb.MsgType {
	return it.Base.MsgType
}

func (it *InsertTask) BeginTs() Timestamp {
	return it.BeginTimestamp
}

101
func (it *InsertTask) SetTs(ts Timestamp) {
N
neza2017 已提交
102 103 104 105 106 107 108
	rowNum := len(it.RowData)
	it.Timestamps = make([]uint64, rowNum)
	for index := range it.Timestamps {
		it.Timestamps[index] = ts
	}
	it.BeginTimestamp = ts
	it.EndTimestamp = ts
109 110 111
}

func (it *InsertTask) EndTs() Timestamp {
N
neza2017 已提交
112
	return it.EndTimestamp
113 114
}

S
sunby 已提交
115 116
func (it *InsertTask) OnEnqueue() error {
	return nil
117 118
}

S
sunby 已提交
119
func (it *InsertTask) PreExecute(ctx context.Context) error {
120
	it.Base.MsgType = commonpb.MsgType_kInsert
121
	it.Base.SourceID = Params.ProxyID
122

N
neza2017 已提交
123 124 125 126
	collectionName := it.BaseInsertTask.CollectionName
	if err := ValidateCollectionName(collectionName); err != nil {
		return err
	}
127
	partitionTag := it.BaseInsertTask.PartitionName
N
neza2017 已提交
128 129 130 131
	if err := ValidatePartitionTag(partitionTag, true); err != nil {
		return err
	}

132 133 134
	return nil
}

S
sunby 已提交
135
func (it *InsertTask) Execute(ctx context.Context) error {
136
	collectionName := it.BaseInsertTask.CollectionName
G
godchen 已提交
137
	collSchema, err := globalMetaCache.GetCollectionSchema(ctx, collectionName)
G
godchen 已提交
138 139
	if err != nil {
		return err
140
	}
G
godchen 已提交
141
	autoID := collSchema.AutoID
G
godchen 已提交
142
	collID, err := globalMetaCache.GetCollectionID(ctx, collectionName)
G
godchen 已提交
143
	if err != nil {
144 145
		return err
	}
G
godchen 已提交
146
	it.CollectionID = collID
147 148
	var partitionID UniqueID
	if len(it.PartitionName) > 0 {
G
godchen 已提交
149
		partitionID, err = globalMetaCache.GetPartitionID(ctx, collectionName, it.PartitionName)
150 151 152 153
		if err != nil {
			return err
		}
	} else {
G
godchen 已提交
154
		partitionID, err = globalMetaCache.GetPartitionID(ctx, collectionName, Params.DefaultPartitionTag)
155 156 157
		if err != nil {
			return err
		}
G
godchen 已提交
158 159
	}
	it.PartitionID = partitionID
160 161
	var rowIDBegin UniqueID
	var rowIDEnd UniqueID
G
godchen 已提交
162 163
	rowNums := len(it.BaseInsertTask.RowData)
	rowIDBegin, rowIDEnd, _ = it.rowIDAllocator.Alloc(uint32(rowNums))
164

G
godchen 已提交
165 166 167 168 169 170 171
	it.BaseInsertTask.RowIDs = make([]UniqueID, rowNums)
	for i := rowIDBegin; i < rowIDEnd; i++ {
		offset := i - rowIDBegin
		it.BaseInsertTask.RowIDs[offset] = i
	}

	if autoID {
N
neza2017 已提交
172 173 174
		if it.HashValues == nil || len(it.HashValues) == 0 {
			it.HashValues = make([]uint32, 0)
		}
G
godchen 已提交
175 176
		for _, rowID := range it.RowIDs {
			hashValue, _ := typeutil.Hash32Int64(rowID)
N
neza2017 已提交
177
			it.HashValues = append(it.HashValues, hashValue)
178 179 180
		}
	}

181
	var tsMsg msgstream.TsMsg = &it.BaseInsertTask
182 183 184
	msgPack := &msgstream.MsgPack{
		BeginTs: it.BeginTs(),
		EndTs:   it.EndTs(),
X
xige-16 已提交
185
		Msgs:    make([]msgstream.TsMsg, 1),
186
	}
G
godchen 已提交
187

188
	it.result = &milvuspb.InsertResponse{
189 190 191
		Status: &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_SUCCESS,
		},
192 193
		RowIDBegin: rowIDBegin,
		RowIDEnd:   rowIDEnd,
194
	}
195 196 197

	msgPack.Msgs[0] = tsMsg

G
godchen 已提交
198
	stream, err := globalInsertChannelsMap.getInsertMsgStream(collID)
199
	if err != nil {
G
godchen 已提交
200
		resp, _ := it.dataServiceClient.GetInsertChannels(ctx, &datapb.InsertChannelRequest{
201 202 203 204 205 206 207
			Base: &commonpb.MsgBase{
				MsgType:   commonpb.MsgType_kInsert, // todo
				MsgID:     it.Base.MsgID,            // todo
				Timestamp: 0,                        // todo
				SourceID:  Params.ProxyID,
			},
			DbID:         0, // todo
G
godchen 已提交
208
			CollectionID: collID,
209
		})
G
godchen 已提交
210 211 212 213 214
		if resp == nil {
			return errors.New("get insert channels resp is nil")
		}
		if resp.Status.ErrorCode != commonpb.ErrorCode_SUCCESS {
			return errors.New(resp.Status.Reason)
215
		}
G
godchen 已提交
216
		err = globalInsertChannelsMap.createInsertMsgStream(collID, resp.Values)
217 218 219 220
		if err != nil {
			return err
		}
	}
G
godchen 已提交
221
	stream, err = globalInsertChannelsMap.getInsertMsgStream(collID)
222 223 224 225 226 227
	if err != nil {
		it.result.Status.ErrorCode = commonpb.ErrorCode_UNEXPECTED_ERROR
		it.result.Status.Reason = err.Error()
		return err
	}

X
XuanYang-cn 已提交
228
	err = stream.Produce(ctx, msgPack)
229 230 231
	if err != nil {
		it.result.Status.ErrorCode = commonpb.ErrorCode_UNEXPECTED_ERROR
		it.result.Status.Reason = err.Error()
232
		return err
233
	}
234

235 236 237
	return nil
}

S
sunby 已提交
238
func (it *InsertTask) PostExecute(ctx context.Context) error {
239 240 241 242
	return nil
}

type CreateCollectionTask struct {
D
dragondriver 已提交
243
	Condition
244
	*milvuspb.CreateCollectionRequest
S
sunby 已提交
245
	ctx               context.Context
246 247 248 249
	masterClient      MasterClient
	dataServiceClient DataServiceClient
	result            *commonpb.Status
	schema            *schemapb.CollectionSchema
250 251
}

S
sunby 已提交
252 253
func (cct *CreateCollectionTask) Ctx() context.Context {
	return cct.ctx
254 255
}

C
cai.zhang 已提交
256
func (cct *CreateCollectionTask) ID() UniqueID {
257
	return cct.Base.MsgID
258 259
}

260
func (cct *CreateCollectionTask) SetID(uid UniqueID) {
261
	cct.Base.MsgID = uid
262 263
}

S
sunby 已提交
264 265 266 267
func (cct *CreateCollectionTask) Name() string {
	return CreateCollectionTaskName
}

268
func (cct *CreateCollectionTask) Type() commonpb.MsgType {
269
	return cct.Base.MsgType
270 271 272
}

func (cct *CreateCollectionTask) BeginTs() Timestamp {
273
	return cct.Base.Timestamp
274 275 276
}

func (cct *CreateCollectionTask) EndTs() Timestamp {
277
	return cct.Base.Timestamp
278 279 280
}

func (cct *CreateCollectionTask) SetTs(ts Timestamp) {
281
	cct.Base.Timestamp = ts
282 283
}

S
sunby 已提交
284 285 286 287 288 289
func (cct *CreateCollectionTask) OnEnqueue() error {
	cct.Base = &commonpb.MsgBase{}
	return nil
}

func (cct *CreateCollectionTask) PreExecute(ctx context.Context) error {
290
	cct.Base.MsgType = commonpb.MsgType_kCreateCollection
291
	cct.Base.SourceID = Params.ProxyID
292 293 294 295 296 297 298

	cct.schema = &schemapb.CollectionSchema{}
	err := proto.Unmarshal(cct.Schema, cct.schema)
	if err != nil {
		return err
	}

299
	if int64(len(cct.schema.Fields)) > Params.MaxFieldNum {
S
sunby 已提交
300
		return fmt.Errorf("maximum field's number should be limited to %d", Params.MaxFieldNum)
N
neza2017 已提交
301 302 303 304 305 306 307
	}

	// validate collection name
	if err := ValidateCollectionName(cct.schema.Name); err != nil {
		return err
	}

N
neza2017 已提交
308 309 310 311 312 313 314 315
	if err := ValidateDuplicatedFieldName(cct.schema.Fields); err != nil {
		return err
	}

	if err := ValidatePrimaryKey(cct.schema); err != nil {
		return err
	}

N
neza2017 已提交
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
	// validate field name
	for _, field := range cct.schema.Fields {
		if err := ValidateFieldName(field.Name); err != nil {
			return err
		}
		if field.DataType == schemapb.DataType_VECTOR_FLOAT || field.DataType == schemapb.DataType_VECTOR_BINARY {
			exist := false
			var dim int64 = 0
			for _, param := range field.TypeParams {
				if param.Key == "dim" {
					exist = true
					tmp, err := strconv.ParseInt(param.Value, 10, 64)
					if err != nil {
						return err
					}
					dim = tmp
					break
				}
			}
			if !exist {
				return errors.New("dimension is not defined in field type params")
			}
			if field.DataType == schemapb.DataType_VECTOR_FLOAT {
				if err := ValidateDimension(dim, false); err != nil {
					return err
				}
			} else {
				if err := ValidateDimension(dim, true); err != nil {
					return err
				}
			}
		}
	}

350
	return nil
Z
zhenshan.cao 已提交
351 352
}

S
sunby 已提交
353
func (cct *CreateCollectionTask) Execute(ctx context.Context) error {
354
	var err error
G
godchen 已提交
355
	cct.result, err = cct.masterClient.CreateCollection(ctx, cct.CreateCollectionRequest)
356 357 358 359
	if err != nil {
		return err
	}
	if cct.result.ErrorCode == commonpb.ErrorCode_SUCCESS {
G
godchen 已提交
360
		collID, err := globalMetaCache.GetCollectionID(ctx, cct.CollectionName)
361 362 363
		if err != nil {
			return err
		}
G
godchen 已提交
364
		resp, _ := cct.dataServiceClient.GetInsertChannels(ctx, &datapb.InsertChannelRequest{
365 366 367 368 369 370 371
			Base: &commonpb.MsgBase{
				MsgType:   commonpb.MsgType_kInsert, // todo
				MsgID:     cct.Base.MsgID,           // todo
				Timestamp: 0,                        // todo
				SourceID:  Params.ProxyID,
			},
			DbID:         0, // todo
G
godchen 已提交
372
			CollectionID: collID,
373
		})
G
godchen 已提交
374 375 376 377 378
		if resp == nil {
			return errors.New("get insert channels resp is nil")
		}
		if resp.Status.ErrorCode != commonpb.ErrorCode_SUCCESS {
			return errors.New(resp.Status.Reason)
379
		}
G
godchen 已提交
380
		err = globalInsertChannelsMap.createInsertMsgStream(collID, resp.Values)
381 382 383 384 385
		if err != nil {
			return err
		}
	}
	return nil
Z
zhenshan.cao 已提交
386 387
}

S
sunby 已提交
388
func (cct *CreateCollectionTask) PostExecute(ctx context.Context) error {
389
	return nil
Z
zhenshan.cao 已提交
390 391
}

392
type DropCollectionTask struct {
D
dragondriver 已提交
393
	Condition
394
	*milvuspb.DropCollectionRequest
S
sunby 已提交
395
	ctx          context.Context
Z
zhenshan.cao 已提交
396 397
	masterClient MasterClient
	result       *commonpb.Status
398 399
}

S
sunby 已提交
400 401
func (dct *DropCollectionTask) Ctx() context.Context {
	return dct.ctx
402 403
}

C
cai.zhang 已提交
404
func (dct *DropCollectionTask) ID() UniqueID {
405
	return dct.Base.MsgID
406 407
}

408
func (dct *DropCollectionTask) SetID(uid UniqueID) {
409
	dct.Base.MsgID = uid
410 411
}

S
sunby 已提交
412 413 414 415
func (dct *DropCollectionTask) Name() string {
	return DropCollectionTaskName
}

416
func (dct *DropCollectionTask) Type() commonpb.MsgType {
417
	return dct.Base.MsgType
418 419 420
}

func (dct *DropCollectionTask) BeginTs() Timestamp {
421
	return dct.Base.Timestamp
422 423 424
}

func (dct *DropCollectionTask) EndTs() Timestamp {
425
	return dct.Base.Timestamp
426 427 428
}

func (dct *DropCollectionTask) SetTs(ts Timestamp) {
429
	dct.Base.Timestamp = ts
430 431
}

S
sunby 已提交
432 433 434 435 436 437
func (dct *DropCollectionTask) OnEnqueue() error {
	dct.Base = &commonpb.MsgBase{}
	return nil
}

func (dct *DropCollectionTask) PreExecute(ctx context.Context) error {
438
	dct.Base.MsgType = commonpb.MsgType_kDropCollection
439
	dct.Base.SourceID = Params.ProxyID
440

441
	if err := ValidateCollectionName(dct.CollectionName); err != nil {
N
neza2017 已提交
442 443
		return err
	}
444 445 446
	return nil
}

S
sunby 已提交
447
func (dct *DropCollectionTask) Execute(ctx context.Context) error {
G
godchen 已提交
448
	collID, err := globalMetaCache.GetCollectionID(ctx, dct.CollectionName)
449 450 451
	if err != nil {
		return err
	}
S
sunby 已提交
452

G
godchen 已提交
453
	dct.result, err = dct.masterClient.DropCollection(ctx, dct.DropCollectionRequest)
S
sunby 已提交
454 455
	if err != nil {
		return err
456
	}
S
sunby 已提交
457

G
godchen 已提交
458 459 460 461
	err = globalInsertChannelsMap.closeInsertMsgStream(collID)
	if err != nil {
		return err
	}
S
sunby 已提交
462

G
godchen 已提交
463
	return nil
464 465
}

S
sunby 已提交
466
func (dct *DropCollectionTask) PostExecute(ctx context.Context) error {
G
godchen 已提交
467
	globalMetaCache.RemoveCollection(ctx, dct.CollectionName)
Z
zhenshan.cao 已提交
468
	return nil
469 470
}

471
type SearchTask struct {
D
dragondriver 已提交
472
	Condition
S
sunby 已提交
473 474
	*internalpb2.SearchRequest
	ctx            context.Context
Z
zhenshan.cao 已提交
475
	queryMsgStream msgstream.MsgStream
476
	resultBuf      chan []*internalpb2.SearchResults
477 478 479 480
	result         *milvuspb.SearchResults
	query          *milvuspb.SearchRequest
}

S
sunby 已提交
481 482
func (st *SearchTask) Ctx() context.Context {
	return st.ctx
483 484
}

485 486
func (st *SearchTask) ID() UniqueID {
	return st.Base.MsgID
487 488
}

489 490
func (st *SearchTask) SetID(uid UniqueID) {
	st.Base.MsgID = uid
491 492
}

S
sunby 已提交
493 494 495 496
func (st *SearchTask) Name() string {
	return SearchTaskName
}

497 498
func (st *SearchTask) Type() commonpb.MsgType {
	return st.Base.MsgType
499 500
}

501 502
func (st *SearchTask) BeginTs() Timestamp {
	return st.Base.Timestamp
503 504
}

505 506
func (st *SearchTask) EndTs() Timestamp {
	return st.Base.Timestamp
507 508
}

509 510
func (st *SearchTask) SetTs(ts Timestamp) {
	st.Base.Timestamp = ts
511 512
}

S
sunby 已提交
513 514 515 516 517
func (st *SearchTask) OnEnqueue() error {
	return nil
}

func (st *SearchTask) PreExecute(ctx context.Context) error {
518
	st.Base.MsgType = commonpb.MsgType_kSearch
519
	st.Base.SourceID = Params.ProxyID
520

521
	collectionName := st.query.CollectionName
G
godchen 已提交
522
	_, err := globalMetaCache.GetCollectionID(ctx, collectionName)
523 524 525 526
	if err != nil { // err is not nil if collection not exists
		return err
	}

527
	if err := ValidateCollectionName(st.query.CollectionName); err != nil {
N
neza2017 已提交
528 529 530
		return err
	}

531
	for _, tag := range st.query.PartitionNames {
N
neza2017 已提交
532 533 534 535
		if err := ValidatePartitionTag(tag, false); err != nil {
			return err
		}
	}
536 537
	st.Base.MsgType = commonpb.MsgType_kSearch
	queryBytes, err := proto.Marshal(st.query)
C
cai.zhang 已提交
538 539 540
	if err != nil {
		return err
	}
541
	st.Query = &commonpb.Blob{
C
cai.zhang 已提交
542 543
		Value: queryBytes,
	}
544 545 546

	st.ResultChannelID = Params.SearchResultChannelNames[0]
	st.DbID = 0 // todo
G
godchen 已提交
547
	collectionID, err := globalMetaCache.GetCollectionID(ctx, collectionName)
548 549 550 551 552 553
	if err != nil { // err is not nil if collection not exists
		return err
	}
	st.CollectionID = collectionID
	st.PartitionIDs = make([]UniqueID, 0)
	for _, partitionName := range st.query.PartitionNames {
G
godchen 已提交
554
		partitionID, err := globalMetaCache.GetPartitionID(ctx, collectionName, partitionName)
555
		if err != nil {
B
bigsheeper 已提交
556
			continue
557 558 559 560 561 562
		}
		st.PartitionIDs = append(st.PartitionIDs, partitionID)
	}
	st.Dsl = st.query.Dsl
	st.PlaceholderGroup = st.query.PlaceholderGroup

563 564 565
	return nil
}

S
sunby 已提交
566
func (st *SearchTask) Execute(ctx context.Context) error {
567
	var tsMsg msgstream.TsMsg = &msgstream.SearchMsg{
S
sunby 已提交
568
		SearchRequest: *st.SearchRequest,
569
		BaseMsg: msgstream.BaseMsg{
570
			HashValues:     []uint32{uint32(Params.ProxyID)},
571 572
			BeginTimestamp: st.Base.Timestamp,
			EndTimestamp:   st.Base.Timestamp,
573 574 575
		},
	}
	msgPack := &msgstream.MsgPack{
576 577
		BeginTs: st.Base.Timestamp,
		EndTs:   st.Base.Timestamp,
X
xige-16 已提交
578
		Msgs:    make([]msgstream.TsMsg, 1),
579
	}
X
xige-16 已提交
580
	msgPack.Msgs[0] = tsMsg
X
XuanYang-cn 已提交
581
	err := st.queryMsgStream.Produce(ctx, msgPack)
582
	log.Printf("[ProxyNode] length of searchMsg: %v", len(msgPack.Msgs))
C
cai.zhang 已提交
583
	if err != nil {
584
		log.Printf("[ProxyNode] send search request failed: %v", err)
C
cai.zhang 已提交
585 586
	}
	return err
587 588
}

S
sunby 已提交
589
func (st *SearchTask) PostExecute(ctx context.Context) error {
590 591
	for {
		select {
592
		case <-st.Ctx().Done():
593
			log.Print("SearchTask: wait to finish failed, timeout!, taskID:", st.ID())
S
sunby 已提交
594
			return fmt.Errorf("SearchTask:wait to finish failed, timeout: %d", st.ID())
595
		case searchResults := <-st.resultBuf:
596
			// fmt.Println("searchResults: ", searchResults)
597
			filterSearchResult := make([]*internalpb2.SearchResults, 0)
Z
zhenshan.cao 已提交
598
			var filterReason string
599 600 601
			for _, partialSearchResult := range searchResults {
				if partialSearchResult.Status.ErrorCode == commonpb.ErrorCode_SUCCESS {
					filterSearchResult = append(filterSearchResult, partialSearchResult)
602 603 604 605 606 607 608 609 610 611
					// For debugging, please don't delete.
					//for i := 0; i < len(partialSearchResult.Hits); i++ {
					//	testHits := milvuspb.Hits{}
					//	err := proto.Unmarshal(partialSearchResult.Hits[i], &testHits)
					//	if err != nil {
					//		panic(err)
					//	}
					//	fmt.Println(testHits.IDs)
					//	fmt.Println(testHits.Scores)
					//}
Z
zhenshan.cao 已提交
612 613
				} else {
					filterReason += partialSearchResult.Status.Reason + "\n"
614 615 616
				}
			}

617 618
			availableQueryNodeNum := len(filterSearchResult)
			if availableQueryNodeNum <= 0 {
619
				st.result = &milvuspb.SearchResults{
Z
zhenshan.cao 已提交
620 621 622 623 624 625
					Status: &commonpb.Status{
						ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
						Reason:    filterReason,
					},
				}
				return errors.New(filterReason)
626
			}
C
cai.zhang 已提交
627

628
			hits := make([][]*milvuspb.Hits, 0)
629
			for _, partialSearchResult := range filterSearchResult {
B
bigsheeper 已提交
630
				if partialSearchResult.Hits == nil || len(partialSearchResult.Hits) <= 0 {
631 632 633
					filterReason += "nq is zero\n"
					continue
				}
634
				partialHits := make([]*milvuspb.Hits, 0)
635
				for _, bs := range partialSearchResult.Hits {
636
					partialHit := &milvuspb.Hits{}
637
					err := proto.Unmarshal(bs, partialHit)
N
neza2017 已提交
638
					if err != nil {
B
bigsheeper 已提交
639
						log.Println("unmarshal error")
N
neza2017 已提交
640 641
						return err
					}
642 643 644 645 646 647 648
					partialHits = append(partialHits, partialHit)
				}
				hits = append(hits, partialHits)
			}

			availableQueryNodeNum = len(hits)
			if availableQueryNodeNum <= 0 {
649
				st.result = &milvuspb.SearchResults{
650 651 652 653 654 655 656 657 658 659
					Status: &commonpb.Status{
						ErrorCode: commonpb.ErrorCode_SUCCESS,
						Reason:    filterReason,
					},
				}
				return nil
			}

			nq := len(hits[0])
			if nq <= 0 {
660
				st.result = &milvuspb.SearchResults{
661 662 663 664
					Status: &commonpb.Status{
						ErrorCode: commonpb.ErrorCode_SUCCESS,
						Reason:    filterReason,
					},
N
neza2017 已提交
665
				}
666
				return nil
N
neza2017 已提交
667
			}
C
cai.zhang 已提交
668

B
bigsheeper 已提交
669 670 671 672 673 674 675 676 677 678
			topk := 0
			getMax := func(a, b int) int {
				if a > b {
					return a
				}
				return b
			}
			for _, hit := range hits {
				topk = getMax(topk, len(hit[0].IDs))
			}
679
			st.result = &milvuspb.SearchResults{
680 681 682
				Status: &commonpb.Status{
					ErrorCode: 0,
				},
C
cai.zhang 已提交
683
				Hits: make([][]byte, 0),
684
			}
C
cai.zhang 已提交
685

G
GuoRentong 已提交
686
			const minFloat32 = -1 * float32(math.MaxFloat32)
687 688
			for i := 0; i < nq; i++ {
				locs := make([]int, availableQueryNodeNum)
689
				reducedHits := &milvuspb.Hits{
C
cai.zhang 已提交
690 691 692 693 694
					IDs:     make([]int64, 0),
					RowData: make([][]byte, 0),
					Scores:  make([]float32, 0),
				}

695
				for j := 0; j < topk; j++ {
B
bigsheeper 已提交
696
					valid := false
G
GuoRentong 已提交
697
					choice, maxDistance := 0, minFloat32
698
					for q, loc := range locs { // query num, the number of ways to merge
B
bigsheeper 已提交
699 700 701
						if loc >= len(hits[q][i].IDs) {
							continue
						}
N
neza2017 已提交
702
						distance := hits[q][i].Scores[loc]
C
cai.zhang 已提交
703
						if distance > maxDistance || (math.Abs(float64(distance-maxDistance)) < math.SmallestNonzeroFloat32 && choice != q) {
704
							choice = q
G
GuoRentong 已提交
705
							maxDistance = distance
B
bigsheeper 已提交
706
							valid = true
707 708
						}
					}
B
bigsheeper 已提交
709 710 711
					if !valid {
						break
					}
712
					choiceOffset := locs[choice]
713 714
					// check if distance is valid, `invalid` here means very very big,
					// in this process, distance here is the smallest, so the rest of distance are all invalid
G
GuoRentong 已提交
715
					if hits[choice][i].Scores[choiceOffset] <= minFloat32 {
716
						break
717
					}
N
neza2017 已提交
718
					reducedHits.IDs = append(reducedHits.IDs, hits[choice][i].IDs[choiceOffset])
C
cai.zhang 已提交
719 720 721
					if hits[choice][i].RowData != nil && len(hits[choice][i].RowData) > 0 {
						reducedHits.RowData = append(reducedHits.RowData, hits[choice][i].RowData[choiceOffset])
					}
N
neza2017 已提交
722
					reducedHits.Scores = append(reducedHits.Scores, hits[choice][i].Scores[choiceOffset])
723 724
					locs[choice]++
				}
G
GuoRentong 已提交
725 726 727 728 729
				if searchResults[0].MetricType != "IP" {
					for k := range reducedHits.Scores {
						reducedHits.Scores[k] *= -1
					}
				}
N
neza2017 已提交
730 731
				reducedHitsBs, err := proto.Marshal(reducedHits)
				if err != nil {
B
bigsheeper 已提交
732
					log.Println("marshal error")
N
neza2017 已提交
733 734
					return err
				}
735
				st.result.Hits = append(st.result.Hits, reducedHitsBs)
736
			}
C
cai.zhang 已提交
737
			return nil
738 739
		}
	}
D
dragondriver 已提交
740 741
}

742
type HasCollectionTask struct {
D
dragondriver 已提交
743
	Condition
744
	*milvuspb.HasCollectionRequest
S
sunby 已提交
745
	ctx          context.Context
Z
zhenshan.cao 已提交
746
	masterClient MasterClient
747
	result       *milvuspb.BoolResponse
748 749
}

S
sunby 已提交
750 751
func (hct *HasCollectionTask) Ctx() context.Context {
	return hct.ctx
752 753
}

C
cai.zhang 已提交
754
func (hct *HasCollectionTask) ID() UniqueID {
755
	return hct.Base.MsgID
756 757
}

758
func (hct *HasCollectionTask) SetID(uid UniqueID) {
759
	hct.Base.MsgID = uid
760 761
}

S
sunby 已提交
762 763 764 765
func (hct *HasCollectionTask) Name() string {
	return HasCollectionTaskName
}

766
func (hct *HasCollectionTask) Type() commonpb.MsgType {
767
	return hct.Base.MsgType
768 769 770
}

func (hct *HasCollectionTask) BeginTs() Timestamp {
771
	return hct.Base.Timestamp
772 773 774
}

func (hct *HasCollectionTask) EndTs() Timestamp {
775
	return hct.Base.Timestamp
776 777 778
}

func (hct *HasCollectionTask) SetTs(ts Timestamp) {
779
	hct.Base.Timestamp = ts
780 781
}

S
sunby 已提交
782 783 784 785 786 787
func (hct *HasCollectionTask) OnEnqueue() error {
	hct.Base = &commonpb.MsgBase{}
	return nil
}

func (hct *HasCollectionTask) PreExecute(ctx context.Context) error {
788
	hct.Base.MsgType = commonpb.MsgType_kHasCollection
789
	hct.Base.SourceID = Params.ProxyID
790

791
	if err := ValidateCollectionName(hct.CollectionName); err != nil {
N
neza2017 已提交
792 793
		return err
	}
794 795 796
	return nil
}

S
sunby 已提交
797
func (hct *HasCollectionTask) Execute(ctx context.Context) error {
798
	var err error
G
godchen 已提交
799
	hct.result, err = hct.masterClient.HasCollection(ctx, hct.HasCollectionRequest)
G
godchen 已提交
800 801 802 803 804 805
	if hct.result == nil {
		return errors.New("has collection resp is nil")
	}
	if hct.result.Status.ErrorCode != commonpb.ErrorCode_SUCCESS {
		return errors.New(hct.result.Status.Reason)
	}
806 807 808
	return err
}

S
sunby 已提交
809
func (hct *HasCollectionTask) PostExecute(ctx context.Context) error {
810 811 812 813
	return nil
}

type DescribeCollectionTask struct {
D
dragondriver 已提交
814
	Condition
815
	*milvuspb.DescribeCollectionRequest
S
sunby 已提交
816
	ctx          context.Context
Z
zhenshan.cao 已提交
817
	masterClient MasterClient
818
	result       *milvuspb.DescribeCollectionResponse
819 820
}

S
sunby 已提交
821 822
func (dct *DescribeCollectionTask) Ctx() context.Context {
	return dct.ctx
823 824
}

C
cai.zhang 已提交
825
func (dct *DescribeCollectionTask) ID() UniqueID {
826
	return dct.Base.MsgID
827 828
}

829
func (dct *DescribeCollectionTask) SetID(uid UniqueID) {
830
	dct.Base.MsgID = uid
831 832
}

S
sunby 已提交
833 834 835 836
func (dct *DescribeCollectionTask) Name() string {
	return DescribeCollectionTaskName
}

837
func (dct *DescribeCollectionTask) Type() commonpb.MsgType {
838
	return dct.Base.MsgType
839 840 841
}

func (dct *DescribeCollectionTask) BeginTs() Timestamp {
842
	return dct.Base.Timestamp
843 844 845
}

func (dct *DescribeCollectionTask) EndTs() Timestamp {
846
	return dct.Base.Timestamp
847 848 849
}

func (dct *DescribeCollectionTask) SetTs(ts Timestamp) {
850
	dct.Base.Timestamp = ts
851 852
}

S
sunby 已提交
853 854 855 856 857 858
func (dct *DescribeCollectionTask) OnEnqueue() error {
	dct.Base = &commonpb.MsgBase{}
	return nil
}

func (dct *DescribeCollectionTask) PreExecute(ctx context.Context) error {
859
	dct.Base.MsgType = commonpb.MsgType_kDescribeCollection
860
	dct.Base.SourceID = Params.ProxyID
861

862
	if err := ValidateCollectionName(dct.CollectionName); err != nil {
N
neza2017 已提交
863 864
		return err
	}
865 866 867
	return nil
}

S
sunby 已提交
868
func (dct *DescribeCollectionTask) Execute(ctx context.Context) error {
869
	var err error
G
godchen 已提交
870
	dct.result, err = dct.masterClient.DescribeCollection(ctx, dct.DescribeCollectionRequest)
G
godchen 已提交
871 872
	if dct.result == nil {
		return errors.New("has collection resp is nil")
873
	}
G
godchen 已提交
874 875 876 877
	if dct.result.Status.ErrorCode != commonpb.ErrorCode_SUCCESS {
		return errors.New(dct.result.Status.Reason)
	}
	return err
878 879
}

S
sunby 已提交
880
func (dct *DescribeCollectionTask) PostExecute(ctx context.Context) error {
881 882 883 884 885 886
	return nil
}

type GetCollectionsStatisticsTask struct {
	Condition
	*milvuspb.CollectionStatsRequest
S
sunby 已提交
887
	ctx               context.Context
888 889 890 891
	dataServiceClient DataServiceClient
	result            *milvuspb.CollectionStatsResponse
}

S
sunby 已提交
892 893 894 895
func (g *GetCollectionsStatisticsTask) Ctx() context.Context {
	return g.ctx
}

896 897 898 899 900 901 902 903
func (g *GetCollectionsStatisticsTask) ID() UniqueID {
	return g.Base.MsgID
}

func (g *GetCollectionsStatisticsTask) SetID(uid UniqueID) {
	g.Base.MsgID = uid
}

S
sunby 已提交
904 905 906 907
func (g *GetCollectionsStatisticsTask) Name() string {
	return GetCollectionStatisticsTaskName
}

908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
func (g *GetCollectionsStatisticsTask) Type() commonpb.MsgType {
	return g.Base.MsgType
}

func (g *GetCollectionsStatisticsTask) BeginTs() Timestamp {
	return g.Base.Timestamp
}

func (g *GetCollectionsStatisticsTask) EndTs() Timestamp {
	return g.Base.Timestamp
}

func (g *GetCollectionsStatisticsTask) SetTs(ts Timestamp) {
	g.Base.Timestamp = ts
}

func (g *GetCollectionsStatisticsTask) OnEnqueue() error {
	g.Base = &commonpb.MsgBase{}
	return nil
}

S
sunby 已提交
929
func (g *GetCollectionsStatisticsTask) PreExecute(ctx context.Context) error {
930 931 932 933 934
	g.Base.MsgType = commonpb.MsgType_kGetCollectionStatistics
	g.Base.SourceID = Params.ProxyID
	return nil
}

S
sunby 已提交
935
func (g *GetCollectionsStatisticsTask) Execute(ctx context.Context) error {
G
godchen 已提交
936
	collID, err := globalMetaCache.GetCollectionID(ctx, g.CollectionName)
937 938 939 940 941 942 943 944 945 946 947 948 949
	if err != nil {
		return err
	}
	req := &datapb.CollectionStatsRequest{
		Base: &commonpb.MsgBase{
			MsgType:   commonpb.MsgType_kGetCollectionStatistics,
			MsgID:     g.Base.MsgID,
			Timestamp: g.Base.Timestamp,
			SourceID:  g.Base.SourceID,
		},
		CollectionID: collID,
	}

G
godchen 已提交
950
	result, _ := g.dataServiceClient.GetCollectionStatistics(ctx, req)
G
godchen 已提交
951 952 953 954 955
	if result == nil {
		return errors.New("get collection statistics resp is nil")
	}
	if result.Status.ErrorCode != commonpb.ErrorCode_SUCCESS {
		return errors.New(result.Status.Reason)
956 957 958 959 960 961 962 963 964 965 966
	}
	g.result = &milvuspb.CollectionStatsResponse{
		Status: &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_SUCCESS,
			Reason:    "",
		},
		Stats: result.Stats,
	}
	return nil
}

S
sunby 已提交
967
func (g *GetCollectionsStatisticsTask) PostExecute(ctx context.Context) error {
968 969 970 971
	return nil
}

type ShowCollectionsTask struct {
D
dragondriver 已提交
972
	Condition
973
	*milvuspb.ShowCollectionRequest
S
sunby 已提交
974
	ctx          context.Context
Z
zhenshan.cao 已提交
975
	masterClient MasterClient
976
	result       *milvuspb.ShowCollectionResponse
977 978
}

S
sunby 已提交
979 980
func (sct *ShowCollectionsTask) Ctx() context.Context {
	return sct.ctx
981 982
}

C
cai.zhang 已提交
983
func (sct *ShowCollectionsTask) ID() UniqueID {
984
	return sct.Base.MsgID
985 986
}

987
func (sct *ShowCollectionsTask) SetID(uid UniqueID) {
988
	sct.Base.MsgID = uid
989 990
}

S
sunby 已提交
991 992 993 994
func (sct *ShowCollectionsTask) Name() string {
	return ShowCollectionTaskName
}

995
func (sct *ShowCollectionsTask) Type() commonpb.MsgType {
996
	return sct.Base.MsgType
997 998 999
}

func (sct *ShowCollectionsTask) BeginTs() Timestamp {
1000
	return sct.Base.Timestamp
1001 1002 1003
}

func (sct *ShowCollectionsTask) EndTs() Timestamp {
1004
	return sct.Base.Timestamp
1005 1006 1007
}

func (sct *ShowCollectionsTask) SetTs(ts Timestamp) {
1008
	sct.Base.Timestamp = ts
1009 1010
}

S
sunby 已提交
1011 1012 1013 1014 1015 1016
func (sct *ShowCollectionsTask) OnEnqueue() error {
	sct.Base = &commonpb.MsgBase{}
	return nil
}

func (sct *ShowCollectionsTask) PreExecute(ctx context.Context) error {
1017
	sct.Base.MsgType = commonpb.MsgType_kShowCollections
1018
	sct.Base.SourceID = Params.ProxyID
1019

1020 1021 1022
	return nil
}

S
sunby 已提交
1023
func (sct *ShowCollectionsTask) Execute(ctx context.Context) error {
1024
	var err error
G
godchen 已提交
1025
	sct.result, err = sct.masterClient.ShowCollections(ctx, sct.ShowCollectionRequest)
G
godchen 已提交
1026 1027 1028 1029 1030 1031
	if sct.result == nil {
		return errors.New("get collection statistics resp is nil")
	}
	if sct.result.Status.ErrorCode != commonpb.ErrorCode_SUCCESS {
		return errors.New(sct.result.Status.Reason)
	}
1032 1033 1034
	return err
}

S
sunby 已提交
1035
func (sct *ShowCollectionsTask) PostExecute(ctx context.Context) error {
1036 1037
	return nil
}
N
neza2017 已提交
1038 1039 1040

type CreatePartitionTask struct {
	Condition
1041
	*milvuspb.CreatePartitionRequest
S
sunby 已提交
1042
	ctx          context.Context
Z
zhenshan.cao 已提交
1043
	masterClient MasterClient
N
neza2017 已提交
1044 1045 1046
	result       *commonpb.Status
}

S
sunby 已提交
1047 1048
func (cpt *CreatePartitionTask) Ctx() context.Context {
	return cpt.ctx
1049 1050
}

N
neza2017 已提交
1051
func (cpt *CreatePartitionTask) ID() UniqueID {
1052
	return cpt.Base.MsgID
N
neza2017 已提交
1053 1054
}

1055
func (cpt *CreatePartitionTask) SetID(uid UniqueID) {
1056
	cpt.Base.MsgID = uid
1057 1058
}

S
sunby 已提交
1059 1060 1061 1062
func (cpt *CreatePartitionTask) Name() string {
	return CreatePartitionTaskName
}

1063
func (cpt *CreatePartitionTask) Type() commonpb.MsgType {
1064
	return cpt.Base.MsgType
N
neza2017 已提交
1065 1066 1067
}

func (cpt *CreatePartitionTask) BeginTs() Timestamp {
1068
	return cpt.Base.Timestamp
N
neza2017 已提交
1069 1070 1071
}

func (cpt *CreatePartitionTask) EndTs() Timestamp {
1072
	return cpt.Base.Timestamp
N
neza2017 已提交
1073 1074 1075
}

func (cpt *CreatePartitionTask) SetTs(ts Timestamp) {
1076
	cpt.Base.Timestamp = ts
N
neza2017 已提交
1077 1078
}

S
sunby 已提交
1079 1080 1081 1082 1083 1084
func (cpt *CreatePartitionTask) OnEnqueue() error {
	cpt.Base = &commonpb.MsgBase{}
	return nil
}

func (cpt *CreatePartitionTask) PreExecute(ctx context.Context) error {
1085
	cpt.Base.MsgType = commonpb.MsgType_kCreatePartition
1086
	cpt.Base.SourceID = Params.ProxyID
1087

1088
	collName, partitionTag := cpt.CollectionName, cpt.PartitionName
N
neza2017 已提交
1089 1090 1091 1092 1093 1094 1095 1096 1097

	if err := ValidateCollectionName(collName); err != nil {
		return err
	}

	if err := ValidatePartitionTag(partitionTag, true); err != nil {
		return err
	}

N
neza2017 已提交
1098 1099 1100
	return nil
}

S
sunby 已提交
1101
func (cpt *CreatePartitionTask) Execute(ctx context.Context) (err error) {
G
godchen 已提交
1102
	cpt.result, err = cpt.masterClient.CreatePartition(ctx, cpt.CreatePartitionRequest)
G
godchen 已提交
1103 1104 1105 1106 1107 1108
	if cpt.result == nil {
		return errors.New("get collection statistics resp is nil")
	}
	if cpt.result.ErrorCode != commonpb.ErrorCode_SUCCESS {
		return errors.New(cpt.result.Reason)
	}
N
neza2017 已提交
1109 1110 1111
	return err
}

S
sunby 已提交
1112
func (cpt *CreatePartitionTask) PostExecute(ctx context.Context) error {
N
neza2017 已提交
1113 1114 1115 1116 1117
	return nil
}

type DropPartitionTask struct {
	Condition
1118
	*milvuspb.DropPartitionRequest
S
sunby 已提交
1119
	ctx          context.Context
Z
zhenshan.cao 已提交
1120
	masterClient MasterClient
N
neza2017 已提交
1121 1122 1123
	result       *commonpb.Status
}

S
sunby 已提交
1124 1125
func (dpt *DropPartitionTask) Ctx() context.Context {
	return dpt.ctx
1126 1127
}

N
neza2017 已提交
1128
func (dpt *DropPartitionTask) ID() UniqueID {
1129
	return dpt.Base.MsgID
N
neza2017 已提交
1130 1131
}

1132
func (dpt *DropPartitionTask) SetID(uid UniqueID) {
1133
	dpt.Base.MsgID = uid
1134 1135
}

S
sunby 已提交
1136 1137 1138 1139
func (dpt *DropPartitionTask) Name() string {
	return DropPartitionTaskName
}

1140
func (dpt *DropPartitionTask) Type() commonpb.MsgType {
1141
	return dpt.Base.MsgType
N
neza2017 已提交
1142 1143 1144
}

func (dpt *DropPartitionTask) BeginTs() Timestamp {
1145
	return dpt.Base.Timestamp
N
neza2017 已提交
1146 1147 1148
}

func (dpt *DropPartitionTask) EndTs() Timestamp {
1149
	return dpt.Base.Timestamp
N
neza2017 已提交
1150 1151 1152
}

func (dpt *DropPartitionTask) SetTs(ts Timestamp) {
1153
	dpt.Base.Timestamp = ts
N
neza2017 已提交
1154 1155
}

S
sunby 已提交
1156 1157 1158 1159 1160 1161
func (dpt *DropPartitionTask) OnEnqueue() error {
	dpt.Base = &commonpb.MsgBase{}
	return nil
}

func (dpt *DropPartitionTask) PreExecute(ctx context.Context) error {
1162
	dpt.Base.MsgType = commonpb.MsgType_kDropPartition
1163
	dpt.Base.SourceID = Params.ProxyID
1164

1165
	collName, partitionTag := dpt.CollectionName, dpt.PartitionName
N
neza2017 已提交
1166 1167 1168 1169 1170 1171 1172 1173 1174

	if err := ValidateCollectionName(collName); err != nil {
		return err
	}

	if err := ValidatePartitionTag(partitionTag, true); err != nil {
		return err
	}

N
neza2017 已提交
1175 1176 1177
	return nil
}

S
sunby 已提交
1178
func (dpt *DropPartitionTask) Execute(ctx context.Context) (err error) {
G
godchen 已提交
1179
	dpt.result, err = dpt.masterClient.DropPartition(ctx, dpt.DropPartitionRequest)
G
godchen 已提交
1180 1181 1182 1183 1184 1185
	if dpt.result == nil {
		return errors.New("get collection statistics resp is nil")
	}
	if dpt.result.ErrorCode != commonpb.ErrorCode_SUCCESS {
		return errors.New(dpt.result.Reason)
	}
N
neza2017 已提交
1186 1187 1188
	return err
}

S
sunby 已提交
1189
func (dpt *DropPartitionTask) PostExecute(ctx context.Context) error {
N
neza2017 已提交
1190 1191 1192 1193 1194
	return nil
}

type HasPartitionTask struct {
	Condition
1195
	*milvuspb.HasPartitionRequest
S
sunby 已提交
1196
	ctx          context.Context
Z
zhenshan.cao 已提交
1197
	masterClient MasterClient
1198
	result       *milvuspb.BoolResponse
N
neza2017 已提交
1199 1200
}

S
sunby 已提交
1201 1202
func (hpt *HasPartitionTask) Ctx() context.Context {
	return hpt.ctx
1203 1204
}

N
neza2017 已提交
1205
func (hpt *HasPartitionTask) ID() UniqueID {
1206
	return hpt.Base.MsgID
N
neza2017 已提交
1207 1208
}

1209
func (hpt *HasPartitionTask) SetID(uid UniqueID) {
1210
	hpt.Base.MsgID = uid
1211 1212
}

S
sunby 已提交
1213 1214 1215 1216
func (hpt *HasPartitionTask) Name() string {
	return HasPartitionTaskName
}

1217
func (hpt *HasPartitionTask) Type() commonpb.MsgType {
1218
	return hpt.Base.MsgType
N
neza2017 已提交
1219 1220 1221
}

func (hpt *HasPartitionTask) BeginTs() Timestamp {
1222
	return hpt.Base.Timestamp
N
neza2017 已提交
1223 1224 1225
}

func (hpt *HasPartitionTask) EndTs() Timestamp {
1226
	return hpt.Base.Timestamp
N
neza2017 已提交
1227 1228 1229
}

func (hpt *HasPartitionTask) SetTs(ts Timestamp) {
1230
	hpt.Base.Timestamp = ts
N
neza2017 已提交
1231 1232
}

S
sunby 已提交
1233 1234 1235 1236 1237 1238
func (hpt *HasPartitionTask) OnEnqueue() error {
	hpt.Base = &commonpb.MsgBase{}
	return nil
}

func (hpt *HasPartitionTask) PreExecute(ctx context.Context) error {
1239
	hpt.Base.MsgType = commonpb.MsgType_kHasPartition
1240
	hpt.Base.SourceID = Params.ProxyID
1241

1242
	collName, partitionTag := hpt.CollectionName, hpt.PartitionName
N
neza2017 已提交
1243 1244 1245 1246 1247 1248 1249 1250

	if err := ValidateCollectionName(collName); err != nil {
		return err
	}

	if err := ValidatePartitionTag(partitionTag, true); err != nil {
		return err
	}
N
neza2017 已提交
1251 1252 1253
	return nil
}

S
sunby 已提交
1254
func (hpt *HasPartitionTask) Execute(ctx context.Context) (err error) {
G
godchen 已提交
1255
	hpt.result, err = hpt.masterClient.HasPartition(ctx, hpt.HasPartitionRequest)
G
godchen 已提交
1256 1257 1258 1259 1260 1261
	if hpt.result == nil {
		return errors.New("get collection statistics resp is nil")
	}
	if hpt.result.Status.ErrorCode != commonpb.ErrorCode_SUCCESS {
		return errors.New(hpt.result.Status.Reason)
	}
N
neza2017 已提交
1262 1263 1264
	return err
}

S
sunby 已提交
1265
func (hpt *HasPartitionTask) PostExecute(ctx context.Context) error {
N
neza2017 已提交
1266 1267 1268 1269 1270
	return nil
}

type ShowPartitionsTask struct {
	Condition
1271
	*milvuspb.ShowPartitionRequest
S
sunby 已提交
1272
	ctx          context.Context
Z
zhenshan.cao 已提交
1273
	masterClient MasterClient
1274
	result       *milvuspb.ShowPartitionResponse
N
neza2017 已提交
1275 1276
}

S
sunby 已提交
1277 1278
func (spt *ShowPartitionsTask) Ctx() context.Context {
	return spt.ctx
1279 1280
}

N
neza2017 已提交
1281
func (spt *ShowPartitionsTask) ID() UniqueID {
1282
	return spt.Base.MsgID
N
neza2017 已提交
1283 1284
}

1285
func (spt *ShowPartitionsTask) SetID(uid UniqueID) {
1286
	spt.Base.MsgID = uid
1287 1288
}

S
sunby 已提交
1289 1290 1291 1292
func (spt *ShowPartitionsTask) Name() string {
	return ShowPartitionTaskName
}

1293
func (spt *ShowPartitionsTask) Type() commonpb.MsgType {
1294
	return spt.Base.MsgType
N
neza2017 已提交
1295 1296 1297
}

func (spt *ShowPartitionsTask) BeginTs() Timestamp {
1298
	return spt.Base.Timestamp
N
neza2017 已提交
1299 1300 1301
}

func (spt *ShowPartitionsTask) EndTs() Timestamp {
1302
	return spt.Base.Timestamp
N
neza2017 已提交
1303 1304 1305
}

func (spt *ShowPartitionsTask) SetTs(ts Timestamp) {
1306
	spt.Base.Timestamp = ts
N
neza2017 已提交
1307 1308
}

S
sunby 已提交
1309 1310 1311 1312 1313 1314
func (spt *ShowPartitionsTask) OnEnqueue() error {
	spt.Base = &commonpb.MsgBase{}
	return nil
}

func (spt *ShowPartitionsTask) PreExecute(ctx context.Context) error {
1315
	spt.Base.MsgType = commonpb.MsgType_kShowPartitions
1316
	spt.Base.SourceID = Params.ProxyID
1317

1318
	if err := ValidateCollectionName(spt.CollectionName); err != nil {
N
neza2017 已提交
1319 1320
		return err
	}
N
neza2017 已提交
1321 1322 1323
	return nil
}

S
sunby 已提交
1324
func (spt *ShowPartitionsTask) Execute(ctx context.Context) error {
1325
	var err error
G
godchen 已提交
1326
	spt.result, err = spt.masterClient.ShowPartitions(ctx, spt.ShowPartitionRequest)
G
godchen 已提交
1327 1328
	if spt.result == nil {
		return errors.New("get collection statistics resp is nil")
G
godchen 已提交
1329
	}
G
godchen 已提交
1330 1331 1332 1333
	if spt.result.Status.ErrorCode != commonpb.ErrorCode_SUCCESS {
		return errors.New(spt.result.Status.Reason)
	}
	return err
N
neza2017 已提交
1334 1335
}

S
sunby 已提交
1336
func (spt *ShowPartitionsTask) PostExecute(ctx context.Context) error {
N
neza2017 已提交
1337 1338
	return nil
}
1339 1340 1341

type CreateIndexTask struct {
	Condition
1342
	*milvuspb.CreateIndexRequest
S
sunby 已提交
1343
	ctx          context.Context
Z
zhenshan.cao 已提交
1344
	masterClient MasterClient
1345 1346 1347
	result       *commonpb.Status
}

S
sunby 已提交
1348 1349
func (cit *CreateIndexTask) Ctx() context.Context {
	return cit.ctx
1350 1351
}

1352
func (cit *CreateIndexTask) ID() UniqueID {
1353
	return cit.Base.MsgID
1354 1355 1356
}

func (cit *CreateIndexTask) SetID(uid UniqueID) {
1357
	cit.Base.MsgID = uid
1358 1359
}

S
sunby 已提交
1360 1361 1362 1363
func (cit *CreateIndexTask) Name() string {
	return CreateIndexTaskName
}

1364
func (cit *CreateIndexTask) Type() commonpb.MsgType {
1365
	return cit.Base.MsgType
1366 1367 1368
}

func (cit *CreateIndexTask) BeginTs() Timestamp {
1369
	return cit.Base.Timestamp
1370 1371 1372
}

func (cit *CreateIndexTask) EndTs() Timestamp {
1373
	return cit.Base.Timestamp
1374 1375 1376
}

func (cit *CreateIndexTask) SetTs(ts Timestamp) {
1377
	cit.Base.Timestamp = ts
1378 1379
}

S
sunby 已提交
1380 1381 1382 1383 1384 1385
func (cit *CreateIndexTask) OnEnqueue() error {
	cit.Base = &commonpb.MsgBase{}
	return nil
}

func (cit *CreateIndexTask) PreExecute(ctx context.Context) error {
1386
	cit.Base.MsgType = commonpb.MsgType_kCreateIndex
1387
	cit.Base.SourceID = Params.ProxyID
1388

1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
	collName, fieldName := cit.CollectionName, cit.FieldName

	if err := ValidateCollectionName(collName); err != nil {
		return err
	}

	if err := ValidateFieldName(fieldName); err != nil {
		return err
	}

	return nil
}

S
sunby 已提交
1402
func (cit *CreateIndexTask) Execute(ctx context.Context) error {
G
godchen 已提交
1403
	var err error
G
godchen 已提交
1404
	cit.result, err = cit.masterClient.CreateIndex(ctx, cit.CreateIndexRequest)
G
godchen 已提交
1405 1406 1407 1408 1409 1410
	if cit.result == nil {
		return errors.New("get collection statistics resp is nil")
	}
	if cit.result.ErrorCode != commonpb.ErrorCode_SUCCESS {
		return errors.New(cit.result.Reason)
	}
1411 1412 1413
	return err
}

S
sunby 已提交
1414
func (cit *CreateIndexTask) PostExecute(ctx context.Context) error {
1415 1416 1417 1418 1419
	return nil
}

type DescribeIndexTask struct {
	Condition
1420
	*milvuspb.DescribeIndexRequest
S
sunby 已提交
1421
	ctx          context.Context
Z
zhenshan.cao 已提交
1422
	masterClient MasterClient
1423
	result       *milvuspb.DescribeIndexResponse
1424 1425
}

S
sunby 已提交
1426 1427
func (dit *DescribeIndexTask) Ctx() context.Context {
	return dit.ctx
1428 1429
}

1430
func (dit *DescribeIndexTask) ID() UniqueID {
1431
	return dit.Base.MsgID
1432 1433 1434
}

func (dit *DescribeIndexTask) SetID(uid UniqueID) {
1435
	dit.Base.MsgID = uid
1436 1437
}

S
sunby 已提交
1438 1439 1440 1441
func (dit *DescribeIndexTask) Name() string {
	return DescribeIndexTaskName
}

1442
func (dit *DescribeIndexTask) Type() commonpb.MsgType {
1443
	return dit.Base.MsgType
1444 1445 1446
}

func (dit *DescribeIndexTask) BeginTs() Timestamp {
1447
	return dit.Base.Timestamp
1448 1449 1450
}

func (dit *DescribeIndexTask) EndTs() Timestamp {
1451
	return dit.Base.Timestamp
1452 1453 1454
}

func (dit *DescribeIndexTask) SetTs(ts Timestamp) {
1455
	dit.Base.Timestamp = ts
1456 1457
}

S
sunby 已提交
1458 1459 1460 1461 1462 1463
func (dit *DescribeIndexTask) OnEnqueue() error {
	dit.Base = &commonpb.MsgBase{}
	return nil
}

func (dit *DescribeIndexTask) PreExecute(ctx context.Context) error {
1464
	dit.Base.MsgType = commonpb.MsgType_kDescribeIndex
1465
	dit.Base.SourceID = Params.ProxyID
1466

1467 1468 1469 1470 1471 1472 1473 1474 1475 1476
	collName, fieldName := dit.CollectionName, dit.FieldName

	if err := ValidateCollectionName(collName); err != nil {
		return err
	}

	if err := ValidateFieldName(fieldName); err != nil {
		return err
	}

Z
zhenshan.cao 已提交
1477 1478 1479 1480 1481
	// only support default index name for now. @2021.02.18
	if dit.IndexName == "" {
		dit.IndexName = Params.DefaultIndexName
	}

1482 1483 1484
	return nil
}

S
sunby 已提交
1485
func (dit *DescribeIndexTask) Execute(ctx context.Context) error {
1486
	var err error
G
godchen 已提交
1487
	dit.result, err = dit.masterClient.DescribeIndex(ctx, dit.DescribeIndexRequest)
Z
zhenshan.cao 已提交
1488
	log.Println("YYYYY:", dit.result)
G
godchen 已提交
1489 1490 1491 1492 1493 1494
	if dit.result == nil {
		return errors.New("get collection statistics resp is nil")
	}
	if dit.result.Status.ErrorCode != commonpb.ErrorCode_SUCCESS {
		return errors.New(dit.result.Status.Reason)
	}
1495 1496 1497
	return err
}

S
sunby 已提交
1498
func (dit *DescribeIndexTask) PostExecute(ctx context.Context) error {
1499 1500 1501
	return nil
}

B
BossZou 已提交
1502 1503
type DropIndexTask struct {
	Condition
S
sunby 已提交
1504
	ctx context.Context
B
BossZou 已提交
1505 1506 1507 1508 1509
	*milvuspb.DropIndexRequest
	masterClient MasterClient
	result       *commonpb.Status
}

S
sunby 已提交
1510 1511
func (dit *DropIndexTask) Ctx() context.Context {
	return dit.ctx
B
BossZou 已提交
1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
}

func (dit *DropIndexTask) ID() UniqueID {
	return dit.Base.MsgID
}

func (dit *DropIndexTask) SetID(uid UniqueID) {
	dit.Base.MsgID = uid
}

S
sunby 已提交
1522 1523 1524 1525
func (dit *DropIndexTask) Name() string {
	return DropIndexTaskName
}

B
BossZou 已提交
1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
func (dit *DropIndexTask) Type() commonpb.MsgType {
	return dit.Base.MsgType
}

func (dit *DropIndexTask) BeginTs() Timestamp {
	return dit.Base.Timestamp
}

func (dit *DropIndexTask) EndTs() Timestamp {
	return dit.Base.Timestamp
}

func (dit *DropIndexTask) SetTs(ts Timestamp) {
	dit.Base.Timestamp = ts
}

S
sunby 已提交
1542 1543 1544 1545 1546 1547
func (dit *DropIndexTask) OnEnqueue() error {
	dit.Base = &commonpb.MsgBase{}
	return nil
}

func (dit *DropIndexTask) PreExecute(ctx context.Context) error {
B
BossZou 已提交
1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563
	dit.Base.MsgType = commonpb.MsgType_kDropIndex
	dit.Base.SourceID = Params.ProxyID

	collName, fieldName := dit.CollectionName, dit.FieldName

	if err := ValidateCollectionName(collName); err != nil {
		return err
	}

	if err := ValidateFieldName(fieldName); err != nil {
		return err
	}

	return nil
}

S
sunby 已提交
1564
func (dit *DropIndexTask) Execute(ctx context.Context) error {
B
BossZou 已提交
1565
	var err error
G
godchen 已提交
1566
	dit.result, err = dit.masterClient.DropIndex(ctx, dit.DropIndexRequest)
B
BossZou 已提交
1567 1568 1569 1570 1571 1572 1573 1574 1575
	if dit.result == nil {
		return errors.New("drop index resp is nil")
	}
	if dit.result.ErrorCode != commonpb.ErrorCode_SUCCESS {
		return errors.New(dit.result.Reason)
	}
	return err
}

S
sunby 已提交
1576
func (dit *DropIndexTask) PostExecute(ctx context.Context) error {
B
BossZou 已提交
1577 1578 1579
	return nil
}

1580
type GetIndexStateTask struct {
1581
	Condition
1582
	*milvuspb.IndexStateRequest
S
sunby 已提交
1583
	ctx                context.Context
1584 1585 1586
	indexServiceClient IndexServiceClient
	masterClient       MasterClient
	result             *milvuspb.IndexStateResponse
1587 1588
}

S
sunby 已提交
1589 1590
func (gist *GetIndexStateTask) Ctx() context.Context {
	return gist.ctx
1591 1592
}

S
sunby 已提交
1593 1594
func (gist *GetIndexStateTask) ID() UniqueID {
	return gist.Base.MsgID
1595 1596
}

S
sunby 已提交
1597 1598
func (gist *GetIndexStateTask) SetID(uid UniqueID) {
	gist.Base.MsgID = uid
1599 1600
}

S
sunby 已提交
1601 1602
func (gist *GetIndexStateTask) Name() string {
	return GetIndexStateTaskName
1603 1604
}

S
sunby 已提交
1605 1606
func (gist *GetIndexStateTask) Type() commonpb.MsgType {
	return gist.Base.MsgType
1607 1608
}

S
sunby 已提交
1609 1610
func (gist *GetIndexStateTask) BeginTs() Timestamp {
	return gist.Base.Timestamp
1611 1612
}

S
sunby 已提交
1613 1614
func (gist *GetIndexStateTask) EndTs() Timestamp {
	return gist.Base.Timestamp
1615 1616
}

S
sunby 已提交
1617 1618 1619 1620 1621 1622 1623 1624
func (gist *GetIndexStateTask) SetTs(ts Timestamp) {
	gist.Base.Timestamp = ts
}

func (gist *GetIndexStateTask) OnEnqueue() error {
	gist.Base = &commonpb.MsgBase{}
	return nil
}
1625

S
sunby 已提交
1626 1627 1628 1629 1630
func (gist *GetIndexStateTask) PreExecute(ctx context.Context) error {
	gist.Base.MsgType = commonpb.MsgType_kGetIndexState
	gist.Base.SourceID = Params.ProxyID

	collName, fieldName := gist.CollectionName, gist.FieldName
1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642

	if err := ValidateCollectionName(collName); err != nil {
		return err
	}

	if err := ValidateFieldName(fieldName); err != nil {
		return err
	}

	return nil
}

S
sunby 已提交
1643 1644
func (gist *GetIndexStateTask) Execute(ctx context.Context) error {
	collectionName := gist.CollectionName
G
godchen 已提交
1645
	collectionID, err := globalMetaCache.GetCollectionID(ctx, collectionName)
Z
zhenshan.cao 已提交
1646 1647 1648 1649 1650 1651 1652
	if err != nil { // err is not nil if collection not exists
		return err
	}

	showPartitionRequest := &milvuspb.ShowPartitionRequest{
		Base: &commonpb.MsgBase{
			MsgType:   commonpb.MsgType_kShowPartitions,
S
sunby 已提交
1653 1654
			MsgID:     gist.Base.MsgID,
			Timestamp: gist.Base.Timestamp,
Z
zhenshan.cao 已提交
1655 1656
			SourceID:  Params.ProxyID,
		},
S
sunby 已提交
1657
		DbName:         gist.DbName,
Z
zhenshan.cao 已提交
1658 1659 1660
		CollectionName: collectionName,
		CollectionID:   collectionID,
	}
G
godchen 已提交
1661
	partitions, err := gist.masterClient.ShowPartitions(ctx, showPartitionRequest)
Z
zhenshan.cao 已提交
1662 1663 1664 1665
	if err != nil {
		return err
	}

S
sunby 已提交
1666 1667
	if gist.IndexName == "" {
		gist.IndexName = Params.DefaultIndexName
1668 1669 1670 1671 1672
	}

	describeIndexReq := milvuspb.DescribeIndexRequest{
		Base: &commonpb.MsgBase{
			MsgType:   commonpb.MsgType_kDescribeIndex,
S
sunby 已提交
1673 1674
			MsgID:     gist.Base.MsgID,
			Timestamp: gist.Base.Timestamp,
1675 1676
			SourceID:  Params.ProxyID,
		},
S
sunby 已提交
1677 1678 1679 1680
		DbName:         gist.DbName,
		CollectionName: gist.CollectionName,
		FieldName:      gist.FieldName,
		IndexName:      gist.IndexName,
1681 1682
	}

G
godchen 已提交
1683
	indexDescriptionResp, err2 := gist.masterClient.DescribeIndex(ctx, &describeIndexReq)
1684 1685 1686 1687 1688 1689 1690
	if err2 != nil {
		return err2
	}

	matchIndexID := int64(-1)
	foundIndexID := false
	for _, desc := range indexDescriptionResp.IndexDescriptions {
S
sunby 已提交
1691
		if desc.IndexName == gist.IndexName {
1692 1693 1694 1695 1696 1697
			matchIndexID = desc.IndexID
			foundIndexID = true
			break
		}
	}
	if !foundIndexID {
S
sunby 已提交
1698
		return errors.New(fmt.Sprint("Can't found IndexID for indexName", gist.IndexName))
1699 1700
	}

Z
zhenshan.cao 已提交
1701
	var allSegmentIDs []UniqueID
Z
zhenshan.cao 已提交
1702 1703 1704 1705
	for _, partitionID := range partitions.PartitionIDs {
		showSegmentsRequest := &milvuspb.ShowSegmentRequest{
			Base: &commonpb.MsgBase{
				MsgType:   commonpb.MsgType_kShowSegment,
S
sunby 已提交
1706 1707
				MsgID:     gist.Base.MsgID,
				Timestamp: gist.Base.Timestamp,
Z
zhenshan.cao 已提交
1708 1709 1710 1711 1712
				SourceID:  Params.ProxyID,
			},
			CollectionID: collectionID,
			PartitionID:  partitionID,
		}
G
godchen 已提交
1713
		segments, err := gist.masterClient.ShowSegments(ctx, showSegmentsRequest)
Z
zhenshan.cao 已提交
1714 1715 1716
		if err != nil {
			return err
		}
Z
zhenshan.cao 已提交
1717 1718
		if segments.Status.ErrorCode != commonpb.ErrorCode_SUCCESS {
			return errors.New(segments.Status.Reason)
Z
zhenshan.cao 已提交
1719
		}
Z
zhenshan.cao 已提交
1720 1721 1722 1723 1724 1725
		allSegmentIDs = append(allSegmentIDs, segments.SegmentIDs...)
	}

	getIndexStatesRequest := &indexpb.IndexStatesRequest{
		IndexBuildIDs: make([]UniqueID, 0),
	}
Z
zhenshan.cao 已提交
1726

Z
zhenshan.cao 已提交
1727 1728 1729 1730
	for _, segmentID := range allSegmentIDs {
		describeSegmentRequest := &milvuspb.DescribeSegmentRequest{
			Base: &commonpb.MsgBase{
				MsgType:   commonpb.MsgType_kDescribeSegment,
S
sunby 已提交
1731 1732
				MsgID:     gist.Base.MsgID,
				Timestamp: gist.Base.Timestamp,
Z
zhenshan.cao 已提交
1733 1734 1735 1736 1737
				SourceID:  Params.ProxyID,
			},
			CollectionID: collectionID,
			SegmentID:    segmentID,
		}
G
godchen 已提交
1738
		segmentDesc, err := gist.masterClient.DescribeSegment(ctx, describeSegmentRequest)
Z
zhenshan.cao 已提交
1739 1740 1741
		if err != nil {
			return err
		}
Z
zhenshan.cao 已提交
1742 1743 1744 1745
		if segmentDesc.IndexID == matchIndexID {
			getIndexStatesRequest.IndexBuildIDs = append(getIndexStatesRequest.IndexBuildIDs, segmentDesc.BuildID)
		}
	}
Z
zhenshan.cao 已提交
1746

Z
zhenshan.cao 已提交
1747 1748
	log.Println("GetIndexState:: len of allSegmentIDs:", len(allSegmentIDs), " len of IndexBuildIDs", len(getIndexStatesRequest.IndexBuildIDs))
	if len(allSegmentIDs) != len(getIndexStatesRequest.IndexBuildIDs) {
S
sunby 已提交
1749
		gist.result = &milvuspb.IndexStateResponse{
Z
zhenshan.cao 已提交
1750 1751 1752 1753 1754 1755 1756 1757 1758
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_SUCCESS,
				Reason:    "",
			},
			State: commonpb.IndexState_INPROGRESS,
		}
		return err
	}

G
godchen 已提交
1759
	states, err := gist.indexServiceClient.GetIndexStates(ctx, getIndexStatesRequest)
Z
zhenshan.cao 已提交
1760 1761 1762 1763 1764
	if err != nil {
		return err
	}

	if states.Status.ErrorCode != commonpb.ErrorCode_SUCCESS {
S
sunby 已提交
1765
		gist.result = &milvuspb.IndexStateResponse{
Z
zhenshan.cao 已提交
1766 1767 1768 1769 1770 1771 1772 1773 1774
			Status: states.Status,
			State:  commonpb.IndexState_FAILED,
		}

		return nil
	}

	for _, state := range states.States {
		if state.State != commonpb.IndexState_FINISHED {
S
sunby 已提交
1775
			gist.result = &milvuspb.IndexStateResponse{
Z
zhenshan.cao 已提交
1776
				Status: states.Status,
Z
zhenshan.cao 已提交
1777
				State:  state.State,
Z
zhenshan.cao 已提交
1778 1779
			}

Z
zhenshan.cao 已提交
1780
			return nil
Z
zhenshan.cao 已提交
1781 1782 1783
		}
	}

S
sunby 已提交
1784
	gist.result = &milvuspb.IndexStateResponse{
1785
		Status: &commonpb.Status{
Z
zhenshan.cao 已提交
1786
			ErrorCode: commonpb.ErrorCode_SUCCESS,
1787 1788 1789 1790
			Reason:    "",
		},
		State: commonpb.IndexState_FINISHED,
	}
Z
zhenshan.cao 已提交
1791

1792
	return nil
1793 1794
}

S
sunby 已提交
1795
func (gist *GetIndexStateTask) PostExecute(ctx context.Context) error {
1796 1797
	return nil
}
Z
zhenshan.cao 已提交
1798 1799 1800 1801

type FlushTask struct {
	Condition
	*milvuspb.FlushRequest
S
sunby 已提交
1802
	ctx               context.Context
Z
zhenshan.cao 已提交
1803 1804 1805 1806
	dataServiceClient DataServiceClient
	result            *commonpb.Status
}

S
sunby 已提交
1807 1808
func (ft *FlushTask) Ctx() context.Context {
	return ft.ctx
Z
zhenshan.cao 已提交
1809 1810 1811 1812 1813 1814 1815 1816 1817 1818
}

func (ft *FlushTask) ID() UniqueID {
	return ft.Base.MsgID
}

func (ft *FlushTask) SetID(uid UniqueID) {
	ft.Base.MsgID = uid
}

S
sunby 已提交
1819 1820 1821 1822
func (ft *FlushTask) Name() string {
	return FlushTaskName
}

Z
zhenshan.cao 已提交
1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838
func (ft *FlushTask) Type() commonpb.MsgType {
	return ft.Base.MsgType
}

func (ft *FlushTask) BeginTs() Timestamp {
	return ft.Base.Timestamp
}

func (ft *FlushTask) EndTs() Timestamp {
	return ft.Base.Timestamp
}

func (ft *FlushTask) SetTs(ts Timestamp) {
	ft.Base.Timestamp = ts
}

S
sunby 已提交
1839 1840 1841 1842 1843 1844
func (ft *FlushTask) OnEnqueue() error {
	ft.Base = &commonpb.MsgBase{}
	return nil
}

func (ft *FlushTask) PreExecute(ctx context.Context) error {
Z
zhenshan.cao 已提交
1845 1846 1847 1848 1849
	ft.Base.MsgType = commonpb.MsgType_kFlush
	ft.Base.SourceID = Params.ProxyID
	return nil
}

S
sunby 已提交
1850
func (ft *FlushTask) Execute(ctx context.Context) error {
1851
	for _, collName := range ft.CollectionNames {
G
godchen 已提交
1852
		collID, err := globalMetaCache.GetCollectionID(ctx, collName)
1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866
		if err != nil {
			return err
		}
		flushReq := &datapb.FlushRequest{
			Base: &commonpb.MsgBase{
				MsgType:   commonpb.MsgType_kFlush,
				MsgID:     ft.Base.MsgID,
				Timestamp: ft.Base.Timestamp,
				SourceID:  ft.Base.SourceID,
			},
			DbID:         0,
			CollectionID: collID,
		}
		var status *commonpb.Status
G
godchen 已提交
1867
		status, _ = ft.dataServiceClient.Flush(ctx, flushReq)
G
godchen 已提交
1868 1869
		if status == nil {
			return errors.New("flush resp is nil")
1870 1871 1872 1873
		}
		if status.ErrorCode != commonpb.ErrorCode_SUCCESS {
			return errors.New(status.Reason)
		}
Z
zhenshan.cao 已提交
1874
	}
1875 1876
	ft.result = &commonpb.Status{
		ErrorCode: commonpb.ErrorCode_SUCCESS,
Z
zhenshan.cao 已提交
1877
	}
1878
	return nil
Z
zhenshan.cao 已提交
1879 1880
}

S
sunby 已提交
1881
func (ft *FlushTask) PostExecute(ctx context.Context) error {
Z
zhenshan.cao 已提交
1882 1883
	return nil
}
1884 1885 1886 1887

type LoadCollectionTask struct {
	Condition
	*milvuspb.LoadCollectionRequest
S
sunby 已提交
1888
	ctx                context.Context
1889 1890 1891 1892
	queryserviceClient QueryServiceClient
	result             *commonpb.Status
}

S
sunby 已提交
1893 1894
func (lct *LoadCollectionTask) Ctx() context.Context {
	return lct.ctx
1895 1896 1897 1898 1899 1900 1901 1902 1903 1904
}

func (lct *LoadCollectionTask) ID() UniqueID {
	return lct.Base.MsgID
}

func (lct *LoadCollectionTask) SetID(uid UniqueID) {
	lct.Base.MsgID = uid
}

S
sunby 已提交
1905 1906 1907 1908
func (lct *LoadCollectionTask) Name() string {
	return LoadCollectionTaskName
}

1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924
func (lct *LoadCollectionTask) Type() commonpb.MsgType {
	return lct.Base.MsgType
}

func (lct *LoadCollectionTask) BeginTs() Timestamp {
	return lct.Base.Timestamp
}

func (lct *LoadCollectionTask) EndTs() Timestamp {
	return lct.Base.Timestamp
}

func (lct *LoadCollectionTask) SetTs(ts Timestamp) {
	lct.Base.Timestamp = ts
}

S
sunby 已提交
1925 1926 1927 1928 1929 1930
func (lct *LoadCollectionTask) OnEnqueue() error {
	lct.Base = &commonpb.MsgBase{}
	return nil
}

func (lct *LoadCollectionTask) PreExecute(ctx context.Context) error {
1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942
	lct.Base.MsgType = commonpb.MsgType_kLoadCollection
	lct.Base.SourceID = Params.ProxyID

	collName := lct.CollectionName

	if err := ValidateCollectionName(collName); err != nil {
		return err
	}

	return nil
}

S
sunby 已提交
1943
func (lct *LoadCollectionTask) Execute(ctx context.Context) (err error) {
G
godchen 已提交
1944
	collID, err := globalMetaCache.GetCollectionID(ctx, lct.CollectionName)
1945 1946 1947
	if err != nil {
		return err
	}
G
godchen 已提交
1948
	collSchema, err := globalMetaCache.GetCollectionSchema(ctx, lct.CollectionName)
1949 1950 1951 1952
	if err != nil {
		return err
	}

1953 1954 1955 1956 1957 1958 1959 1960 1961
	request := &querypb.LoadCollectionRequest{
		Base: &commonpb.MsgBase{
			MsgType:   commonpb.MsgType_kLoadCollection,
			MsgID:     lct.Base.MsgID,
			Timestamp: lct.Base.Timestamp,
			SourceID:  lct.Base.SourceID,
		},
		DbID:         0,
		CollectionID: collID,
1962
		Schema:       collSchema,
1963
	}
G
godchen 已提交
1964
	lct.result, err = lct.queryserviceClient.LoadCollection(ctx, request)
1965 1966 1967
	return err
}

S
sunby 已提交
1968
func (lct *LoadCollectionTask) PostExecute(ctx context.Context) error {
1969 1970 1971 1972 1973 1974
	return nil
}

type ReleaseCollectionTask struct {
	Condition
	*milvuspb.ReleaseCollectionRequest
S
sunby 已提交
1975
	ctx                context.Context
1976 1977 1978 1979
	queryserviceClient QueryServiceClient
	result             *commonpb.Status
}

S
sunby 已提交
1980 1981
func (rct *ReleaseCollectionTask) Ctx() context.Context {
	return rct.ctx
1982 1983 1984 1985 1986 1987 1988 1989 1990 1991
}

func (rct *ReleaseCollectionTask) ID() UniqueID {
	return rct.Base.MsgID
}

func (rct *ReleaseCollectionTask) SetID(uid UniqueID) {
	rct.Base.MsgID = uid
}

S
sunby 已提交
1992 1993 1994 1995
func (rct *ReleaseCollectionTask) Name() string {
	return ReleaseCollectionTaskName
}

1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011
func (rct *ReleaseCollectionTask) Type() commonpb.MsgType {
	return rct.Base.MsgType
}

func (rct *ReleaseCollectionTask) BeginTs() Timestamp {
	return rct.Base.Timestamp
}

func (rct *ReleaseCollectionTask) EndTs() Timestamp {
	return rct.Base.Timestamp
}

func (rct *ReleaseCollectionTask) SetTs(ts Timestamp) {
	rct.Base.Timestamp = ts
}

S
sunby 已提交
2012 2013 2014 2015 2016 2017
func (rct *ReleaseCollectionTask) OnEnqueue() error {
	rct.Base = &commonpb.MsgBase{}
	return nil
}

func (rct *ReleaseCollectionTask) PreExecute(ctx context.Context) error {
2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029
	rct.Base.MsgType = commonpb.MsgType_kReleaseCollection
	rct.Base.SourceID = Params.ProxyID

	collName := rct.CollectionName

	if err := ValidateCollectionName(collName); err != nil {
		return err
	}

	return nil
}

S
sunby 已提交
2030
func (rct *ReleaseCollectionTask) Execute(ctx context.Context) (err error) {
G
godchen 已提交
2031
	collID, err := globalMetaCache.GetCollectionID(ctx, rct.CollectionName)
2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044
	if err != nil {
		return err
	}
	request := &querypb.ReleaseCollectionRequest{
		Base: &commonpb.MsgBase{
			MsgType:   commonpb.MsgType_kReleaseCollection,
			MsgID:     rct.Base.MsgID,
			Timestamp: rct.Base.Timestamp,
			SourceID:  rct.Base.SourceID,
		},
		DbID:         0,
		CollectionID: collID,
	}
G
godchen 已提交
2045
	rct.result, err = rct.queryserviceClient.ReleaseCollection(ctx, request)
2046 2047 2048
	return err
}

S
sunby 已提交
2049
func (rct *ReleaseCollectionTask) PostExecute(ctx context.Context) error {
2050 2051 2052 2053 2054 2055
	return nil
}

type LoadPartitionTask struct {
	Condition
	*milvuspb.LoadPartitonRequest
S
sunby 已提交
2056
	ctx                context.Context
2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068
	queryserviceClient QueryServiceClient
	result             *commonpb.Status
}

func (lpt *LoadPartitionTask) ID() UniqueID {
	return lpt.Base.MsgID
}

func (lpt *LoadPartitionTask) SetID(uid UniqueID) {
	lpt.Base.MsgID = uid
}

S
sunby 已提交
2069 2070 2071 2072
func (lpt *LoadPartitionTask) Name() string {
	return LoadPartitionTaskName
}

2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088
func (lpt *LoadPartitionTask) Type() commonpb.MsgType {
	return lpt.Base.MsgType
}

func (lpt *LoadPartitionTask) BeginTs() Timestamp {
	return lpt.Base.Timestamp
}

func (lpt *LoadPartitionTask) EndTs() Timestamp {
	return lpt.Base.Timestamp
}

func (lpt *LoadPartitionTask) SetTs(ts Timestamp) {
	lpt.Base.Timestamp = ts
}

S
sunby 已提交
2089 2090 2091 2092 2093 2094
func (lpt *LoadPartitionTask) OnEnqueue() error {
	lpt.Base = &commonpb.MsgBase{}
	return nil
}

func (lpt *LoadPartitionTask) PreExecute(ctx context.Context) error {
2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106
	lpt.Base.MsgType = commonpb.MsgType_kLoadPartition
	lpt.Base.SourceID = Params.ProxyID

	collName := lpt.CollectionName

	if err := ValidateCollectionName(collName); err != nil {
		return err
	}

	return nil
}

S
sunby 已提交
2107
func (lpt *LoadPartitionTask) Execute(ctx context.Context) error {
2108
	var partitionIDs []int64
G
godchen 已提交
2109
	collID, err := globalMetaCache.GetCollectionID(ctx, lpt.CollectionName)
2110 2111 2112
	if err != nil {
		return err
	}
G
godchen 已提交
2113
	collSchema, err := globalMetaCache.GetCollectionSchema(ctx, lpt.CollectionName)
2114 2115 2116
	if err != nil {
		return err
	}
2117
	for _, partitionName := range lpt.PartitionNames {
G
godchen 已提交
2118
		partitionID, err := globalMetaCache.GetPartitionID(ctx, lpt.CollectionName, partitionName)
2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133
		if err != nil {
			return err
		}
		partitionIDs = append(partitionIDs, partitionID)
	}
	request := &querypb.LoadPartitionRequest{
		Base: &commonpb.MsgBase{
			MsgType:   commonpb.MsgType_kLoadPartition,
			MsgID:     lpt.Base.MsgID,
			Timestamp: lpt.Base.Timestamp,
			SourceID:  lpt.Base.SourceID,
		},
		DbID:         0,
		CollectionID: collID,
		PartitionIDs: partitionIDs,
2134
		Schema:       collSchema,
2135
	}
G
godchen 已提交
2136
	lpt.result, err = lpt.queryserviceClient.LoadPartitions(ctx, request)
2137 2138 2139
	return err
}

S
sunby 已提交
2140
func (lpt *LoadPartitionTask) PostExecute(ctx context.Context) error {
2141 2142 2143 2144 2145 2146
	return nil
}

type ReleasePartitionTask struct {
	Condition
	*milvuspb.ReleasePartitionRequest
S
sunby 已提交
2147
	ctx                context.Context
2148 2149 2150 2151
	queryserviceClient QueryServiceClient
	result             *commonpb.Status
}

S
sunby 已提交
2152 2153
func (rpt *ReleasePartitionTask) Ctx() context.Context {
	return rpt.ctx
2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167
}

func (rpt *ReleasePartitionTask) ID() UniqueID {
	return rpt.Base.MsgID
}

func (rpt *ReleasePartitionTask) SetID(uid UniqueID) {
	rpt.Base.MsgID = uid
}

func (rpt *ReleasePartitionTask) Type() commonpb.MsgType {
	return rpt.Base.MsgType
}

S
sunby 已提交
2168 2169 2170 2171
func (rpt *ReleasePartitionTask) Name() string {
	return ReleasePartitionTaskName
}

2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183
func (rpt *ReleasePartitionTask) BeginTs() Timestamp {
	return rpt.Base.Timestamp
}

func (rpt *ReleasePartitionTask) EndTs() Timestamp {
	return rpt.Base.Timestamp
}

func (rpt *ReleasePartitionTask) SetTs(ts Timestamp) {
	rpt.Base.Timestamp = ts
}

S
sunby 已提交
2184 2185 2186 2187 2188 2189
func (rpt *ReleasePartitionTask) OnEnqueue() error {
	rpt.Base = &commonpb.MsgBase{}
	return nil
}

func (rpt *ReleasePartitionTask) PreExecute(ctx context.Context) error {
2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201
	rpt.Base.MsgType = commonpb.MsgType_kReleasePartition
	rpt.Base.SourceID = Params.ProxyID

	collName := rpt.CollectionName

	if err := ValidateCollectionName(collName); err != nil {
		return err
	}

	return nil
}

S
sunby 已提交
2202
func (rpt *ReleasePartitionTask) Execute(ctx context.Context) (err error) {
2203
	var partitionIDs []int64
G
godchen 已提交
2204
	collID, err := globalMetaCache.GetCollectionID(ctx, rpt.CollectionName)
2205 2206 2207 2208
	if err != nil {
		return err
	}
	for _, partitionName := range rpt.PartitionNames {
G
godchen 已提交
2209
		partitionID, err := globalMetaCache.GetPartitionID(ctx, rpt.CollectionName, partitionName)
2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225
		if err != nil {
			return err
		}
		partitionIDs = append(partitionIDs, partitionID)
	}
	request := &querypb.ReleasePartitionRequest{
		Base: &commonpb.MsgBase{
			MsgType:   commonpb.MsgType_kReleasePartition,
			MsgID:     rpt.Base.MsgID,
			Timestamp: rpt.Base.Timestamp,
			SourceID:  rpt.Base.SourceID,
		},
		DbID:         0,
		CollectionID: collID,
		PartitionIDs: partitionIDs,
	}
G
godchen 已提交
2226
	rpt.result, err = rpt.queryserviceClient.ReleasePartitions(ctx, request)
2227 2228 2229
	return err
}

S
sunby 已提交
2230
func (rpt *ReleasePartitionTask) PostExecute(ctx context.Context) error {
2231 2232
	return nil
}