impl.go 24.8 KB
Newer Older
1 2 3 4 5 6 7 8
package proxynode

import (
	"context"
	"log"
	"strconv"
	"time"

S
sunby 已提交
9 10
	"errors"

11 12
	"github.com/zilliztech/milvus-distributed/internal/msgstream"
	"github.com/zilliztech/milvus-distributed/internal/proto/commonpb"
G
godchen 已提交
13 14
	"github.com/zilliztech/milvus-distributed/internal/proto/datapb"
	"github.com/zilliztech/milvus-distributed/internal/proto/internalpb2"
15 16
	"github.com/zilliztech/milvus-distributed/internal/proto/milvuspb"
	"github.com/zilliztech/milvus-distributed/internal/proto/proxypb"
G
godchen 已提交
17
	"github.com/zilliztech/milvus-distributed/internal/proto/querypb"
18 19 20 21 22 23
)

const (
	reqTimeoutInterval = time.Second * 10
)

Z
zhenshan.cao 已提交
24
func (node *NodeImpl) UpdateStateCode(code internalpb2.StateCode) {
25
	node.stateCode.Store(code)
Z
zhenshan.cao 已提交
26 27
}

28
func (node *NodeImpl) InvalidateCollectionMetaCache(ctx context.Context, request *proxypb.InvalidateCollMetaCacheRequest) (*commonpb.Status, error) {
29
	collectionName := request.CollectionName
G
godchen 已提交
30
	globalMetaCache.RemoveCollection(ctx, collectionName) // no need to return error, though collection may be not cached
31 32 33 34
	return &commonpb.Status{
		ErrorCode: commonpb.ErrorCode_SUCCESS,
		Reason:    "",
	}, nil
35 36
}

G
godchen 已提交
37
func (node *NodeImpl) CreateCollection(ctx context.Context, request *milvuspb.CreateCollectionRequest) (*commonpb.Status, error) {
38
	log.Println("create collection: ", request)
G
godchen 已提交
39
	ctx, cancel := context.WithTimeout(ctx, reqTimeoutInterval)
40 41
	defer cancel()

42
	cct := &CreateCollectionTask{
S
sunby 已提交
43
		ctx:                     ctx,
44 45 46
		Condition:               NewTaskCondition(ctx),
		CreateCollectionRequest: request,
		masterClient:            node.masterClient,
47
		dataServiceClient:       node.dataServiceClient,
48 49
	}

50
	err := node.sched.DdQueue.Enqueue(cct)
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
	if err != nil {
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    err.Error(),
		}, nil
	}

	err = cct.WaitToFinish()
	if err != nil {
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    err.Error(),
		}, nil
	}

	return cct.result, nil
}

G
godchen 已提交
69
func (node *NodeImpl) DropCollection(ctx context.Context, request *milvuspb.DropCollectionRequest) (*commonpb.Status, error) {
70
	log.Println("drop collection: ", request)
G
godchen 已提交
71
	ctx, cancel := context.WithTimeout(ctx, reqTimeoutInterval)
72 73
	defer cancel()

74
	dct := &DropCollectionTask{
S
sunby 已提交
75
		ctx:                   ctx,
76 77 78 79 80
		Condition:             NewTaskCondition(ctx),
		DropCollectionRequest: request,
		masterClient:          node.masterClient,
	}

81
	err := node.sched.DdQueue.Enqueue(dct)
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
	if err != nil {
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    err.Error(),
		}, nil
	}

	err = dct.WaitToFinish()
	if err != nil {
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    err.Error(),
		}, nil
	}

	return dct.result, nil
}

G
godchen 已提交
100
func (node *NodeImpl) HasCollection(ctx context.Context, request *milvuspb.HasCollectionRequest) (*milvuspb.BoolResponse, error) {
101
	log.Println("has collection: ", request)
G
godchen 已提交
102
	ctx, cancel := context.WithTimeout(ctx, reqTimeoutInterval)
103 104
	defer cancel()

105
	hct := &HasCollectionTask{
S
sunby 已提交
106
		ctx:                  ctx,
107 108 109 110 111
		Condition:            NewTaskCondition(ctx),
		HasCollectionRequest: request,
		masterClient:         node.masterClient,
	}

112
	err := node.sched.DdQueue.Enqueue(hct)
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
	if err != nil {
		return &milvuspb.BoolResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    err.Error(),
			},
		}, nil
	}

	err = hct.WaitToFinish()
	if err != nil {
		return &milvuspb.BoolResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    err.Error(),
			},
		}, nil
	}

	return hct.result, nil
}

G
godchen 已提交
135
func (node *NodeImpl) LoadCollection(ctx context.Context, request *milvuspb.LoadCollectionRequest) (*commonpb.Status, error) {
136
	log.Println("load collection: ", request)
G
godchen 已提交
137
	ctx, cancel := context.WithTimeout(ctx, reqTimeoutInterval)
138 139 140
	defer cancel()

	lct := &LoadCollectionTask{
S
sunby 已提交
141
		ctx:                   ctx,
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
		Condition:             NewTaskCondition(ctx),
		LoadCollectionRequest: request,
		queryserviceClient:    node.queryServiceClient,
	}

	err := node.sched.DdQueue.Enqueue(lct)
	if err != nil {
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    err.Error(),
		}, nil
	}

	err = lct.WaitToFinish()
	if err != nil {
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    err.Error(),
		}, nil
	}

	return lct.result, nil
164 165
}

G
godchen 已提交
166
func (node *NodeImpl) ReleaseCollection(ctx context.Context, request *milvuspb.ReleaseCollectionRequest) (*commonpb.Status, error) {
167
	log.Println("release collection: ", request)
G
godchen 已提交
168
	ctx, cancel := context.WithTimeout(ctx, reqTimeoutInterval)
169 170 171
	defer cancel()

	rct := &ReleaseCollectionTask{
S
sunby 已提交
172
		ctx:                      ctx,
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
		Condition:                NewTaskCondition(ctx),
		ReleaseCollectionRequest: request,
		queryserviceClient:       node.queryServiceClient,
	}

	err := node.sched.DdQueue.Enqueue(rct)
	if err != nil {
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    err.Error(),
		}, nil
	}

	err = rct.WaitToFinish()
	if err != nil {
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    err.Error(),
		}, nil
	}

	return rct.result, nil
195 196
}

G
godchen 已提交
197
func (node *NodeImpl) DescribeCollection(ctx context.Context, request *milvuspb.DescribeCollectionRequest) (*milvuspb.DescribeCollectionResponse, error) {
198
	log.Println("describe collection: ", request)
G
godchen 已提交
199
	ctx, cancel := context.WithTimeout(ctx, reqTimeoutInterval)
200 201
	defer cancel()

202
	dct := &DescribeCollectionTask{
S
sunby 已提交
203
		ctx:                       ctx,
204 205 206 207 208
		Condition:                 NewTaskCondition(ctx),
		DescribeCollectionRequest: request,
		masterClient:              node.masterClient,
	}

209
	err := node.sched.DdQueue.Enqueue(dct)
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
	if err != nil {
		return &milvuspb.DescribeCollectionResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    err.Error(),
			},
		}, nil
	}

	err = dct.WaitToFinish()
	if err != nil {
		return &milvuspb.DescribeCollectionResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    err.Error(),
			},
		}, nil
	}

	return dct.result, nil
}

G
godchen 已提交
232
func (node *NodeImpl) GetCollectionStatistics(ctx context.Context, request *milvuspb.CollectionStatsRequest) (*milvuspb.CollectionStatsResponse, error) {
233
	log.Println("get collection statistics")
G
godchen 已提交
234
	ctx, cancel := context.WithTimeout(ctx, reqTimeoutInterval)
235
	defer cancel()
236
	g := &GetCollectionsStatisticsTask{
S
sunby 已提交
237
		ctx:                    ctx,
238 239 240 241 242
		Condition:              NewTaskCondition(ctx),
		CollectionStatsRequest: request,
		dataServiceClient:      node.dataServiceClient,
	}

243
	err := node.sched.DdQueue.Enqueue(g)
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
	if err != nil {
		return &milvuspb.CollectionStatsResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    err.Error(),
			},
		}, nil
	}

	err = g.WaitToFinish()
	if err != nil {
		return &milvuspb.CollectionStatsResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    err.Error(),
			},
		}, nil
	}

	return g.result, nil
264 265
}

G
godchen 已提交
266
func (node *NodeImpl) ShowCollections(ctx context.Context, request *milvuspb.ShowCollectionRequest) (*milvuspb.ShowCollectionResponse, error) {
267
	log.Println("show collections")
G
godchen 已提交
268
	ctx, cancel := context.WithTimeout(ctx, reqTimeoutInterval)
269
	defer cancel()
270
	sct := &ShowCollectionsTask{
S
sunby 已提交
271
		ctx:                   ctx,
272 273 274 275 276
		Condition:             NewTaskCondition(ctx),
		ShowCollectionRequest: request,
		masterClient:          node.masterClient,
	}

277
	err := node.sched.DdQueue.Enqueue(sct)
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
	if err != nil {
		return &milvuspb.ShowCollectionResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    err.Error(),
			},
		}, nil
	}

	err = sct.WaitToFinish()
	if err != nil {
		return &milvuspb.ShowCollectionResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    err.Error(),
			},
		}, nil
	}

	return sct.result, nil
}

G
godchen 已提交
300
func (node *NodeImpl) CreatePartition(ctx context.Context, request *milvuspb.CreatePartitionRequest) (*commonpb.Status, error) {
301
	log.Println("create partition", request)
G
godchen 已提交
302
	ctx, cancel := context.WithTimeout(ctx, reqTimeoutInterval)
303
	defer cancel()
304
	cpt := &CreatePartitionTask{
S
sunby 已提交
305
		ctx:                    ctx,
306 307 308 309 310 311
		Condition:              NewTaskCondition(ctx),
		CreatePartitionRequest: request,
		masterClient:           node.masterClient,
		result:                 nil,
	}

312
	err := node.sched.DdQueue.Enqueue(cpt)
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
	if err != nil {
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    err.Error(),
		}, nil
	}
	err = cpt.WaitToFinish()
	if err != nil {
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    err.Error(),
		}, nil
	}
	return cpt.result, nil
}

G
godchen 已提交
329
func (node *NodeImpl) DropPartition(ctx context.Context, request *milvuspb.DropPartitionRequest) (*commonpb.Status, error) {
330
	log.Println("drop partition: ", request)
G
godchen 已提交
331
	ctx, cancel := context.WithTimeout(ctx, reqTimeoutInterval)
332
	defer cancel()
333
	dpt := &DropPartitionTask{
S
sunby 已提交
334
		ctx:                  ctx,
335 336 337 338 339 340
		Condition:            NewTaskCondition(ctx),
		DropPartitionRequest: request,
		masterClient:         node.masterClient,
		result:               nil,
	}

341
	err := node.sched.DdQueue.Enqueue(dpt)
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358

	if err != nil {
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    err.Error(),
		}, nil
	}
	err = dpt.WaitToFinish()
	if err != nil {
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    err.Error(),
		}, nil
	}
	return dpt.result, nil
}

G
godchen 已提交
359
func (node *NodeImpl) HasPartition(ctx context.Context, request *milvuspb.HasPartitionRequest) (*milvuspb.BoolResponse, error) {
360
	log.Println("has partition: ", request)
G
godchen 已提交
361
	ctx, cancel := context.WithTimeout(ctx, reqTimeoutInterval)
362
	defer cancel()
363
	hpt := &HasPartitionTask{
S
sunby 已提交
364
		ctx:                 ctx,
365 366 367 368 369 370
		Condition:           NewTaskCondition(ctx),
		HasPartitionRequest: request,
		masterClient:        node.masterClient,
		result:              nil,
	}

371
	err := node.sched.DdQueue.Enqueue(hpt)
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394

	if err != nil {
		return &milvuspb.BoolResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    err.Error(),
			},
			Value: false,
		}, nil
	}
	err = hpt.WaitToFinish()
	if err != nil {
		return &milvuspb.BoolResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    err.Error(),
			},
			Value: false,
		}, nil
	}
	return hpt.result, nil
}

G
godchen 已提交
395
func (node *NodeImpl) LoadPartitions(ctx context.Context, request *milvuspb.LoadPartitonRequest) (*commonpb.Status, error) {
396
	log.Println("load partitions: ", request)
G
godchen 已提交
397
	ctx, cancel := context.WithTimeout(ctx, reqTimeoutInterval)
398 399 400
	defer cancel()

	lpt := &LoadPartitionTask{
S
sunby 已提交
401
		ctx:                 ctx,
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
		Condition:           NewTaskCondition(ctx),
		LoadPartitonRequest: request,
		queryserviceClient:  node.queryServiceClient,
	}

	err := node.sched.DdQueue.Enqueue(lpt)
	if err != nil {
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    err.Error(),
		}, nil
	}

	err = lpt.WaitToFinish()
	if err != nil {
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    err.Error(),
		}, nil
	}

	return lpt.result, nil
424 425
}

G
godchen 已提交
426
func (node *NodeImpl) ReleasePartitions(ctx context.Context, request *milvuspb.ReleasePartitionRequest) (*commonpb.Status, error) {
427
	log.Println("load partitions: ", request)
G
godchen 已提交
428
	ctx, cancel := context.WithTimeout(ctx, reqTimeoutInterval)
429 430 431
	defer cancel()

	rpt := &ReleasePartitionTask{
S
sunby 已提交
432
		ctx:                     ctx,
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
		Condition:               NewTaskCondition(ctx),
		ReleasePartitionRequest: request,
		queryserviceClient:      node.queryServiceClient,
	}

	err := node.sched.DdQueue.Enqueue(rpt)
	if err != nil {
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    err.Error(),
		}, nil
	}

	err = rpt.WaitToFinish()
	if err != nil {
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    err.Error(),
		}, nil
	}

	return rpt.result, nil
455 456
}

G
godchen 已提交
457
func (node *NodeImpl) GetPartitionStatistics(ctx context.Context, request *milvuspb.PartitionStatsRequest) (*milvuspb.PartitionStatsResponse, error) {
458 459 460
	panic("implement me")
}

G
godchen 已提交
461
func (node *NodeImpl) ShowPartitions(ctx context.Context, request *milvuspb.ShowPartitionRequest) (*milvuspb.ShowPartitionResponse, error) {
462
	log.Println("show partitions: ", request)
G
godchen 已提交
463
	ctx, cancel := context.WithTimeout(ctx, reqTimeoutInterval)
464
	defer cancel()
465
	spt := &ShowPartitionsTask{
S
sunby 已提交
466
		ctx:                  ctx,
467 468 469 470 471 472
		Condition:            NewTaskCondition(ctx),
		ShowPartitionRequest: request,
		masterClient:         node.masterClient,
		result:               nil,
	}

473
	err := node.sched.DdQueue.Enqueue(spt)
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495

	if err != nil {
		return &milvuspb.ShowPartitionResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    err.Error(),
			},
		}, nil
	}

	err = spt.WaitToFinish()
	if err != nil {
		return &milvuspb.ShowPartitionResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    err.Error(),
			},
		}, nil
	}
	return spt.result, nil
}

G
godchen 已提交
496
func (node *NodeImpl) CreateIndex(ctx context.Context, request *milvuspb.CreateIndexRequest) (*commonpb.Status, error) {
497
	log.Println("create index for: ", request)
G
godchen 已提交
498
	ctx, cancel := context.WithTimeout(ctx, reqTimeoutInterval)
499
	defer cancel()
500
	cit := &CreateIndexTask{
S
sunby 已提交
501
		ctx:                ctx,
502 503 504 505 506
		Condition:          NewTaskCondition(ctx),
		CreateIndexRequest: request,
		masterClient:       node.masterClient,
	}

507
	err := node.sched.DdQueue.Enqueue(cit)
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
	if err != nil {
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    err.Error(),
		}, nil
	}

	err = cit.WaitToFinish()
	if err != nil {
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    err.Error(),
		}, nil
	}

	return cit.result, nil
}

G
godchen 已提交
526
func (node *NodeImpl) DescribeIndex(ctx context.Context, request *milvuspb.DescribeIndexRequest) (*milvuspb.DescribeIndexResponse, error) {
527
	log.Println("Describe index for: ", request)
G
godchen 已提交
528
	ctx, cancel := context.WithTimeout(ctx, reqTimeoutInterval)
529
	defer cancel()
530
	dit := &DescribeIndexTask{
S
sunby 已提交
531
		ctx:                  ctx,
532 533 534 535 536
		Condition:            NewTaskCondition(ctx),
		DescribeIndexRequest: request,
		masterClient:         node.masterClient,
	}

537
	err := node.sched.DdQueue.Enqueue(dit)
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
	if err != nil {
		return &milvuspb.DescribeIndexResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    err.Error(),
			},
		}, nil
	}

	err = dit.WaitToFinish()
	if err != nil {
		return &milvuspb.DescribeIndexResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    err.Error(),
			},
		}, nil
	}

	return dit.result, nil
}

G
godchen 已提交
560
func (node *NodeImpl) DropIndex(ctx context.Context, request *milvuspb.DropIndexRequest) (*commonpb.Status, error) {
B
BossZou 已提交
561
	log.Println("Drop index for: ", request)
G
godchen 已提交
562
	ctx, cancel := context.WithTimeout(ctx, reqTimeoutInterval)
B
BossZou 已提交
563 564
	defer cancel()
	dit := &DropIndexTask{
S
sunby 已提交
565
		ctx:              ctx,
B
BossZou 已提交
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
		Condition:        NewTaskCondition(ctx),
		DropIndexRequest: request,
		masterClient:     node.masterClient,
	}
	err := node.sched.DdQueue.Enqueue(dit)
	if err != nil {
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    err.Error(),
		}, nil
	}
	err = dit.WaitToFinish()
	if err != nil {
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    err.Error(),
		}, nil
	}
	return dit.result, nil
}

G
godchen 已提交
587
func (node *NodeImpl) GetIndexState(ctx context.Context, request *milvuspb.IndexStateRequest) (*milvuspb.IndexStateResponse, error) {
588
	// log.Println("Describe index progress for: ", request)
G
godchen 已提交
589
	ctx, cancel := context.WithTimeout(ctx, reqTimeoutInterval)
590
	defer cancel()
591
	dipt := &GetIndexStateTask{
S
sunby 已提交
592
		ctx:                ctx,
593 594 595 596
		Condition:          NewTaskCondition(ctx),
		IndexStateRequest:  request,
		indexServiceClient: node.indexServiceClient,
		masterClient:       node.masterClient,
597 598
	}

599
	err := node.sched.DdQueue.Enqueue(dipt)
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
	if err != nil {
		return &milvuspb.IndexStateResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    err.Error(),
			},
		}, nil
	}

	err = dipt.WaitToFinish()
	if err != nil {
		return &milvuspb.IndexStateResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    err.Error(),
			},
		}, nil
	}

	return dipt.result, nil
}

G
godchen 已提交
622 623
func (node *NodeImpl) Insert(ctx context.Context, request *milvuspb.InsertRequest) (*milvuspb.InsertResponse, error) {
	ctx, cancel := context.WithTimeout(ctx, reqTimeoutInterval)
624 625
	defer cancel()

626
	it := &InsertTask{
S
sunby 已提交
627
		ctx:               ctx,
628 629
		Condition:         NewTaskCondition(ctx),
		dataServiceClient: node.dataServiceClient,
630 631 632 633 634 635 636 637 638 639 640 641 642 643
		BaseInsertTask: BaseInsertTask{
			BaseMsg: msgstream.BaseMsg{
				HashValues: request.HashKeys,
			},
			InsertRequest: internalpb2.InsertRequest{
				Base: &commonpb.MsgBase{
					MsgType: commonpb.MsgType_kInsert,
					MsgID:   0,
				},
				CollectionName: request.CollectionName,
				PartitionName:  request.PartitionName,
				RowData:        request.RowData,
			},
		},
644
		rowIDAllocator: node.idAllocator,
645 646
	}
	if len(it.PartitionName) <= 0 {
647
		it.PartitionName = Params.DefaultPartitionTag
648 649
	}

650
	err := node.sched.DmQueue.Enqueue(it)
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673

	if err != nil {
		return &milvuspb.InsertResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    err.Error(),
			},
		}, nil
	}

	err = it.WaitToFinish()
	if err != nil {
		return &milvuspb.InsertResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    err.Error(),
			},
		}, nil
	}

	return it.result, nil
}

G
godchen 已提交
674 675
func (node *NodeImpl) Search(ctx context.Context, request *milvuspb.SearchRequest) (*milvuspb.SearchResults, error) {
	ctx, cancel := context.WithTimeout(ctx, reqTimeoutInterval)
676 677
	defer cancel()

678
	qt := &SearchTask{
S
sunby 已提交
679
		ctx:       ctx,
680
		Condition: NewTaskCondition(ctx),
S
sunby 已提交
681
		SearchRequest: &internalpb2.SearchRequest{
682 683
			Base: &commonpb.MsgBase{
				MsgType:  commonpb.MsgType_kSearch,
684
				SourceID: Params.ProxyID,
685
			},
686
			ResultChannelID: strconv.FormatInt(Params.ProxyID, 10),
687 688 689 690 691 692
		},
		queryMsgStream: node.queryMsgStream,
		resultBuf:      make(chan []*internalpb2.SearchResults),
		query:          request,
	}

693
	err := node.sched.DqQueue.Enqueue(qt)
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
	if err != nil {
		return &milvuspb.SearchResults{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    err.Error(),
			},
		}, nil
	}

	err = qt.WaitToFinish()
	if err != nil {
		return &milvuspb.SearchResults{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    err.Error(),
			},
		}, nil
	}

	return qt.result, nil
}

G
godchen 已提交
716
func (node *NodeImpl) Flush(ctx context.Context, request *milvuspb.FlushRequest) (*commonpb.Status, error) {
717
	log.Println("AA Flush collections: ", request.CollectionNames)
G
godchen 已提交
718
	ctx, cancel := context.WithTimeout(ctx, reqTimeoutInterval)
719 720
	defer cancel()
	ft := &FlushTask{
S
sunby 已提交
721
		ctx:               ctx,
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
		Condition:         NewTaskCondition(ctx),
		FlushRequest:      request,
		dataServiceClient: node.dataServiceClient,
	}

	err := node.sched.DdQueue.Enqueue(ft)
	if err != nil {
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    err.Error(),
		}, nil
	}

	err = ft.WaitToFinish()
	if err != nil {
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    err.Error(),
		}, nil
	}

	return ft.result, nil
744 745
}

G
godchen 已提交
746
func (node *NodeImpl) GetDdChannel(ctx context.Context, request *commonpb.Empty) (*milvuspb.StringResponse, error) {
747 748
	panic("implement me")
}
X
XuanYang-cn 已提交
749

G
godchen 已提交
750
func (node *NodeImpl) GetPersistentSegmentInfo(ctx context.Context, req *milvuspb.PersistentSegmentInfoRequest) (*milvuspb.PersistentSegmentInfoResponse, error) {
X
XuanYang-cn 已提交
751 752 753 754 755
	resp := &milvuspb.PersistentSegmentInfoResponse{
		Status: &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
		},
	}
G
godchen 已提交
756
	segments, err := node.getSegmentsOfCollection(ctx, req.DbName, req.CollectionName)
X
XuanYang-cn 已提交
757 758 759 760
	if err != nil {
		resp.Status.Reason = err.Error()
		return resp, nil
	}
G
godchen 已提交
761
	infoResp, err := node.dataServiceClient.GetSegmentInfo(ctx, &datapb.SegmentInfoRequest{
X
XuanYang-cn 已提交
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
		Base: &commonpb.MsgBase{
			MsgType:   commonpb.MsgType_kSegmentInfo,
			MsgID:     0,
			Timestamp: 0,
			SourceID:  Params.ProxyID,
		},
		SegmentIDs: segments,
	})
	if err != nil {
		resp.Status.Reason = err.Error()
		return resp, nil
	}
	if infoResp.Status.ErrorCode != commonpb.ErrorCode_SUCCESS {
		resp.Status.Reason = infoResp.Status.Reason
		return resp, nil
	}
	persistentInfos := make([]*milvuspb.PersistentSegmentInfo, len(infoResp.Infos))
	for i, info := range infoResp.Infos {
		persistentInfos[i] = &milvuspb.PersistentSegmentInfo{
			SegmentID:    info.SegmentID,
			CollectionID: info.CollectionID,
			PartitionID:  info.PartitionID,
			OpenTime:     info.OpenTime,
			SealedTime:   info.SealedTime,
			FlushedTime:  info.FlushedTime,
			NumRows:      info.NumRows,
			MemSize:      info.MemSize,
			State:        info.State,
		}
	}
	resp.Status.ErrorCode = commonpb.ErrorCode_SUCCESS
	resp.Infos = persistentInfos
	return resp, nil
}

G
godchen 已提交
797
func (node *NodeImpl) GetQuerySegmentInfo(ctx context.Context, req *milvuspb.QuerySegmentInfoRequest) (*milvuspb.QuerySegmentInfoResponse, error) {
Z
zhenshan.cao 已提交
798 799 800 801 802
	resp := &milvuspb.QuerySegmentInfoResponse{
		Status: &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
		},
	}
G
godchen 已提交
803
	segments, err := node.getSegmentsOfCollection(ctx, req.DbName, req.CollectionName)
Z
zhenshan.cao 已提交
804 805 806 807
	if err != nil {
		resp.Status.Reason = err.Error()
		return resp, nil
	}
G
godchen 已提交
808
	infoResp, err := node.queryServiceClient.GetSegmentInfo(ctx, &querypb.SegmentInfoRequest{
Z
zhenshan.cao 已提交
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
		Base: &commonpb.MsgBase{
			MsgType:   commonpb.MsgType_kSegmentInfo,
			MsgID:     0,
			Timestamp: 0,
			SourceID:  Params.ProxyID,
		},
		SegmentIDs: segments,
	})
	if err != nil {
		resp.Status.Reason = err.Error()
		return resp, nil
	}
	if infoResp.Status.ErrorCode != commonpb.ErrorCode_SUCCESS {
		resp.Status.Reason = infoResp.Status.Reason
		return resp, nil
	}
	queryInfos := make([]*milvuspb.QuerySegmentInfo, len(infoResp.Infos))
	for i, info := range infoResp.Infos {
		queryInfos[i] = &milvuspb.QuerySegmentInfo{
			SegmentID:    info.SegmentID,
			CollectionID: info.CollectionID,
			PartitionID:  info.PartitionID,
			NumRows:      info.NumRows,
			MemSize:      info.MemSize,
			IndexName:    info.IndexName,
			IndexID:      info.IndexID,
		}
	}
	resp.Status.ErrorCode = commonpb.ErrorCode_SUCCESS
	resp.Infos = queryInfos
	return resp, nil
}

G
godchen 已提交
842 843
func (node *NodeImpl) getSegmentsOfCollection(ctx context.Context, dbName string, collectionName string) ([]UniqueID, error) {
	describeCollectionResponse, err := node.masterClient.DescribeCollection(ctx, &milvuspb.DescribeCollectionRequest{
X
XuanYang-cn 已提交
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
		Base: &commonpb.MsgBase{
			MsgType:   commonpb.MsgType_kDescribeCollection,
			MsgID:     0,
			Timestamp: 0,
			SourceID:  Params.ProxyID,
		},
		DbName:         dbName,
		CollectionName: collectionName,
	})
	if err != nil {
		return nil, err
	}
	if describeCollectionResponse.Status.ErrorCode != commonpb.ErrorCode_SUCCESS {
		return nil, errors.New(describeCollectionResponse.Status.Reason)
	}
	collectionID := describeCollectionResponse.CollectionID
G
godchen 已提交
860
	showPartitionsResp, err := node.masterClient.ShowPartitions(ctx, &milvuspb.ShowPartitionRequest{
X
XuanYang-cn 已提交
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
		Base: &commonpb.MsgBase{
			MsgType:   commonpb.MsgType_kShowPartitions,
			MsgID:     0,
			Timestamp: 0,
			SourceID:  Params.ProxyID,
		},
		DbName:         dbName,
		CollectionName: collectionName,
		CollectionID:   collectionID,
	})
	if err != nil {
		return nil, err
	}
	if showPartitionsResp.Status.ErrorCode != commonpb.ErrorCode_SUCCESS {
		return nil, errors.New(showPartitionsResp.Status.Reason)
	}

	ret := make([]UniqueID, 0)
	for _, partitionID := range showPartitionsResp.PartitionIDs {
G
godchen 已提交
880
		showSegmentResponse, err := node.masterClient.ShowSegments(ctx, &milvuspb.ShowSegmentRequest{
X
XuanYang-cn 已提交
881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
			Base: &commonpb.MsgBase{
				MsgType:   commonpb.MsgType_kShowSegment,
				MsgID:     0,
				Timestamp: 0,
				SourceID:  Params.ProxyID,
			},
			CollectionID: collectionID,
			PartitionID:  partitionID,
		})
		if err != nil {
			return nil, err
		}
		if showSegmentResponse.Status.ErrorCode != commonpb.ErrorCode_SUCCESS {
			return nil, errors.New(showSegmentResponse.Status.Reason)
		}
		ret = append(ret, showSegmentResponse.SegmentIDs...)
	}
	return ret, nil
}
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919

func (node *NodeImpl) RegisterLink(request *commonpb.Empty) (*milvuspb.RegisterLinkResponse, error) {
	code := node.stateCode.Load().(internalpb2.StateCode)
	if code != internalpb2.StateCode_HEALTHY {
		return &milvuspb.RegisterLinkResponse{
			Address: nil,
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    "proxy node not healthy",
			},
		}, nil
	}
	return &milvuspb.RegisterLinkResponse{
		Address: nil,
		Status: &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_SUCCESS,
			Reason:    "",
		},
	}, nil
}