grpc_service.go 13.7 KB
Newer Older
1 2 3 4
package proxy

import (
	"context"
Z
zhenshan.cao 已提交
5
	"errors"
D
dragondriver 已提交
6 7
	"log"
	"strconv"
D
dragondriver 已提交
8
	"time"
9

C
cai.zhang 已提交
10 11
	"github.com/golang/protobuf/proto"
	"github.com/zilliztech/milvus-distributed/internal/msgstream"
12 13 14 15 16 17
	"github.com/zilliztech/milvus-distributed/internal/proto/commonpb"
	"github.com/zilliztech/milvus-distributed/internal/proto/internalpb"
	"github.com/zilliztech/milvus-distributed/internal/proto/schemapb"
	"github.com/zilliztech/milvus-distributed/internal/proto/servicepb"
)

D
dragondriver 已提交
18
const (
19
	reqTimeoutInterval = time.Second * 10
D
dragondriver 已提交
20 21
)

22 23
func (p *Proxy) Insert(ctx context.Context, in *servicepb.RowBatch) (*servicepb.IntegerRangeResponse, error) {
	it := &InsertTask{
D
dragondriver 已提交
24
		Condition: NewTaskCondition(ctx),
25
		BaseInsertTask: BaseInsertTask{
26 27 28 29 30 31 32 33 34 35 36
			BaseMsg: msgstream.BaseMsg{
				HashValues: in.HashKeys,
			},
			InsertRequest: internalpb.InsertRequest{
				MsgType:        internalpb.MsgType_kInsert,
				CollectionName: in.CollectionName,
				PartitionTag:   in.PartitionTag,
				RowData:        in.RowData,
			},
		},
		manipulationMsgStream: p.manipulationMsgStream,
37
		rowIDAllocator:        p.idAllocator,
38
	}
39

D
dragondriver 已提交
40 41
	var cancel func()
	it.ctx, cancel = context.WithTimeout(ctx, reqTimeoutInterval)
42

D
dragondriver 已提交
43
	defer cancel()
44

45 46 47 48 49
	fn := func() error {
		select {
		case <-ctx.Done():
			return errors.New("insert timeout")
		default:
Q
quicksilver 已提交
50
			return p.sched.DmQueue.Enqueue(it)
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
		}
	}
	err := fn()

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

	err = it.WaitToFinish()
	if err != nil {
B
bigsheeper 已提交
66 67 68
		return &servicepb.IntegerRangeResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
69
				Reason:    err.Error(),
B
bigsheeper 已提交
70
			},
71
		}, nil
72
	}
73 74

	return it.result, nil
75 76 77
}

func (p *Proxy) CreateCollection(ctx context.Context, req *schemapb.CollectionSchema) (*commonpb.Status, error) {
78
	cct := &CreateCollectionTask{
D
dragondriver 已提交
79
		Condition: NewTaskCondition(ctx),
80 81 82 83 84
		CreateCollectionRequest: internalpb.CreateCollectionRequest{
			MsgType: internalpb.MsgType_kCreateCollection,
			Schema:  &commonpb.Blob{},
		},
		masterClient: p.masterClient,
B
bigsheeper 已提交
85
		schema:       req,
86
	}
D
dragondriver 已提交
87 88 89
	var cancel func()
	cct.ctx, cancel = context.WithTimeout(ctx, reqTimeoutInterval)
	defer cancel()
90

91 92 93 94 95
	fn := func() error {
		select {
		case <-ctx.Done():
			return errors.New("create collection timeout")
		default:
Q
quicksilver 已提交
96
			return p.sched.DdQueue.Enqueue(cct)
97 98 99 100 101 102 103
		}
	}
	err := fn()
	if err != nil {
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    err.Error(),
104
		}, nil
105 106 107 108
	}

	err = cct.WaitToFinish()
	if err != nil {
B
bigsheeper 已提交
109 110
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
111
			Reason:    err.Error(),
112
		}, nil
113
	}
114 115

	return cct.result, nil
116 117 118 119
}

func (p *Proxy) Search(ctx context.Context, req *servicepb.Query) (*servicepb.QueryResult, error) {
	qt := &QueryTask{
D
dragondriver 已提交
120
		Condition: NewTaskCondition(ctx),
121 122 123 124 125 126
		SearchRequest: internalpb.SearchRequest{
			MsgType: internalpb.MsgType_kSearch,
			Query:   &commonpb.Blob{},
		},
		queryMsgStream: p.queryMsgStream,
		resultBuf:      make(chan []*internalpb.SearchResult),
B
bigsheeper 已提交
127
		query:          req,
128
	}
D
dragondriver 已提交
129 130
	var cancel func()
	qt.ctx, cancel = context.WithTimeout(ctx, reqTimeoutInterval)
D
dragondriver 已提交
131 132 133
	// Hack with test, shit here but no other ways
	reqID, _ := strconv.Atoi(req.CollectionName[len(req.CollectionName)-1:])
	qt.ReqID = int64(reqID)
134 135
	queryBytes, _ := proto.Marshal(req)
	qt.SearchRequest.Query.Value = queryBytes
D
dragondriver 已提交
136
	log.Printf("grpc address of query task: %p", qt)
D
dragondriver 已提交
137
	defer cancel()
138

139 140 141 142 143
	fn := func() error {
		select {
		case <-ctx.Done():
			return errors.New("create collection timeout")
		default:
Q
quicksilver 已提交
144
			return p.sched.DqQueue.Enqueue(qt)
145 146 147 148 149 150 151 152 153
		}
	}
	err := fn()
	if err != nil {
		return &servicepb.QueryResult{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    err.Error(),
			},
D
dragondriver 已提交
154
		}, nil
155 156 157 158
	}

	err = qt.WaitToFinish()
	if err != nil {
B
bigsheeper 已提交
159 160 161
		return &servicepb.QueryResult{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
162
				Reason:    err.Error(),
B
bigsheeper 已提交
163
			},
D
dragondriver 已提交
164
		}, nil
165
	}
166 167

	return qt.result, nil
168 169 170
}

func (p *Proxy) DropCollection(ctx context.Context, req *servicepb.CollectionName) (*commonpb.Status, error) {
171
	dct := &DropCollectionTask{
D
dragondriver 已提交
172
		Condition: NewTaskCondition(ctx),
173
		DropCollectionRequest: internalpb.DropCollectionRequest{
174
			MsgType:        internalpb.MsgType_kDropCollection,
175 176 177 178
			CollectionName: req,
		},
		masterClient: p.masterClient,
	}
D
dragondriver 已提交
179 180 181
	var cancel func()
	dct.ctx, cancel = context.WithTimeout(ctx, reqTimeoutInterval)
	defer cancel()
182

183 184 185 186 187
	fn := func() error {
		select {
		case <-ctx.Done():
			return errors.New("create collection timeout")
		default:
Q
quicksilver 已提交
188
			return p.sched.DdQueue.Enqueue(dct)
189 190 191 192
		}
	}
	err := fn()
	if err != nil {
B
bigsheeper 已提交
193 194
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
195
			Reason:    err.Error(),
196
		}, nil
197
	}
198 199 200 201 202 203

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

	return dct.result, nil
208 209 210
}

func (p *Proxy) HasCollection(ctx context.Context, req *servicepb.CollectionName) (*servicepb.BoolResponse, error) {
211
	hct := &HasCollectionTask{
D
dragondriver 已提交
212
		Condition: NewTaskCondition(ctx),
213
		HasCollectionRequest: internalpb.HasCollectionRequest{
214
			MsgType:        internalpb.MsgType_kHasCollection,
215
			CollectionName: req,
216
		},
217 218
		masterClient: p.masterClient,
	}
D
dragondriver 已提交
219 220 221
	var cancel func()
	hct.ctx, cancel = context.WithTimeout(ctx, reqTimeoutInterval)
	defer cancel()
222

223 224 225 226 227
	fn := func() error {
		select {
		case <-ctx.Done():
			return errors.New("create collection timeout")
		default:
Q
quicksilver 已提交
228
			return p.sched.DdQueue.Enqueue(hct)
229 230 231 232 233 234 235 236 237
		}
	}
	err := fn()
	if err != nil {
		return &servicepb.BoolResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    err.Error(),
			},
238
		}, nil
239 240 241 242
	}

	err = hct.WaitToFinish()
	if err != nil {
B
bigsheeper 已提交
243 244 245
		return &servicepb.BoolResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
246
				Reason:    err.Error(),
B
bigsheeper 已提交
247
			},
248
		}, nil
249
	}
250 251

	return hct.result, nil
252 253 254
}

func (p *Proxy) DescribeCollection(ctx context.Context, req *servicepb.CollectionName) (*servicepb.CollectionDescription, error) {
255
	dct := &DescribeCollectionTask{
D
dragondriver 已提交
256
		Condition: NewTaskCondition(ctx),
257
		DescribeCollectionRequest: internalpb.DescribeCollectionRequest{
258
			MsgType:        internalpb.MsgType_kDescribeCollection,
259
			CollectionName: req,
260
		},
261 262
		masterClient: p.masterClient,
	}
D
dragondriver 已提交
263 264 265
	var cancel func()
	dct.ctx, cancel = context.WithTimeout(ctx, reqTimeoutInterval)
	defer cancel()
266

267 268 269 270 271
	fn := func() error {
		select {
		case <-ctx.Done():
			return errors.New("create collection timeout")
		default:
Q
quicksilver 已提交
272
			return p.sched.DdQueue.Enqueue(dct)
273 274 275 276
		}
	}
	err := fn()
	if err != nil {
B
bigsheeper 已提交
277 278 279
		return &servicepb.CollectionDescription{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
280
				Reason:    err.Error(),
B
bigsheeper 已提交
281
			},
282
		}, nil
283
	}
284 285 286 287 288 289 290 291

	err = dct.WaitToFinish()
	if err != nil {
		return &servicepb.CollectionDescription{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    err.Error(),
			},
292
		}, nil
293 294 295
	}

	return dct.result, nil
296 297 298
}

func (p *Proxy) ShowCollections(ctx context.Context, req *commonpb.Empty) (*servicepb.StringListResponse, error) {
299
	sct := &ShowCollectionsTask{
D
dragondriver 已提交
300
		Condition: NewTaskCondition(ctx),
301 302
		ShowCollectionRequest: internalpb.ShowCollectionRequest{
			MsgType: internalpb.MsgType_kDescribeCollection,
303
		},
304 305
		masterClient: p.masterClient,
	}
D
dragondriver 已提交
306 307 308
	var cancel func()
	sct.ctx, cancel = context.WithTimeout(ctx, reqTimeoutInterval)
	defer cancel()
309

310 311 312 313 314
	fn := func() error {
		select {
		case <-ctx.Done():
			return errors.New("create collection timeout")
		default:
Q
quicksilver 已提交
315
			return p.sched.DdQueue.Enqueue(sct)
316 317 318 319
		}
	}
	err := fn()
	if err != nil {
B
bigsheeper 已提交
320 321 322
		return &servicepb.StringListResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
323
				Reason:    err.Error(),
B
bigsheeper 已提交
324
			},
325
		}, nil
326
	}
327 328 329 330 331 332 333 334

	err = sct.WaitToFinish()
	if err != nil {
		return &servicepb.StringListResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    err.Error(),
			},
335
		}, nil
336 337 338
	}

	return sct.result, nil
339 340 341
}

func (p *Proxy) CreatePartition(ctx context.Context, in *servicepb.PartitionName) (*commonpb.Status, error) {
N
neza2017 已提交
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
	cpt := &CreatePartitionTask{
		Condition: NewTaskCondition(ctx),
		CreatePartitionRequest: internalpb.CreatePartitionRequest{
			MsgType:       internalpb.MsgType_kCreatePartition,
			ReqID:         0,
			Timestamp:     0,
			ProxyID:       0,
			PartitionName: in,
			//TODO, ReqID,Timestamp,ProxyID
		},
		masterClient: p.masterClient,
		result:       nil,
		ctx:          nil,
	}
	var cancel func()
	cpt.ctx, cancel = context.WithTimeout(ctx, reqTimeoutInterval)
	defer cancel()

	err := func() error {
		select {
		case <-ctx.Done():
			return errors.New("create partition timeout")
		default:
Q
quicksilver 已提交
365
			return p.sched.DdQueue.Enqueue(cpt)
N
neza2017 已提交
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
		}
	}()

	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

384 385 386
}

func (p *Proxy) DropPartition(ctx context.Context, in *servicepb.PartitionName) (*commonpb.Status, error) {
N
neza2017 已提交
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
	dpt := &DropPartitionTask{
		Condition: NewTaskCondition(ctx),
		DropPartitionRequest: internalpb.DropPartitionRequest{
			MsgType:       internalpb.MsgType_kDropPartition,
			ReqID:         0,
			Timestamp:     0,
			ProxyID:       0,
			PartitionName: in,
			//TODO, ReqID,Timestamp,ProxyID
		},
		masterClient: p.masterClient,
		result:       nil,
		ctx:          nil,
	}

	var cancel func()
	dpt.ctx, cancel = context.WithTimeout(ctx, reqTimeoutInterval)
	defer cancel()

	err := func() error {
		select {
		case <-ctx.Done():
			return errors.New("drop partition timeout")
		default:
Q
quicksilver 已提交
411
			return p.sched.DdQueue.Enqueue(dpt)
N
neza2017 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
		}
	}()

	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

430 431 432
}

func (p *Proxy) HasPartition(ctx context.Context, in *servicepb.PartitionName) (*servicepb.BoolResponse, error) {
N
neza2017 已提交
433 434 435 436 437 438 439 440 441
	hpt := &HasPartitionTask{
		Condition: NewTaskCondition(ctx),
		HasPartitionRequest: internalpb.HasPartitionRequest{
			MsgType:       internalpb.MsgType_kHasPartition,
			ReqID:         0,
			Timestamp:     0,
			ProxyID:       0,
			PartitionName: in,
			//TODO, ReqID,Timestamp,ProxyID
442
		},
N
neza2017 已提交
443 444 445 446 447 448 449 450 451 452 453 454 455 456
		masterClient: p.masterClient,
		result:       nil,
		ctx:          nil,
	}

	var cancel func()
	hpt.ctx, cancel = context.WithTimeout(ctx, reqTimeoutInterval)
	defer cancel()

	err := func() error {
		select {
		case <-ctx.Done():
			return errors.New("has partition timeout")
		default:
Q
quicksilver 已提交
457
			return p.sched.DdQueue.Enqueue(hpt)
N
neza2017 已提交
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
		}
	}()

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

482 483 484
}

func (p *Proxy) DescribePartition(ctx context.Context, in *servicepb.PartitionName) (*servicepb.PartitionDescription, error) {
N
neza2017 已提交
485 486 487 488 489 490 491 492 493
	dpt := &DescribePartitionTask{
		Condition: NewTaskCondition(ctx),
		DescribePartitionRequest: internalpb.DescribePartitionRequest{
			MsgType:       internalpb.MsgType_kDescribePartition,
			ReqID:         0,
			Timestamp:     0,
			ProxyID:       0,
			PartitionName: in,
			//TODO, ReqID,Timestamp,ProxyID
494
		},
N
neza2017 已提交
495 496 497 498 499 500 501 502 503 504 505 506 507 508
		masterClient: p.masterClient,
		result:       nil,
		ctx:          nil,
	}

	var cancel func()
	dpt.ctx, cancel = context.WithTimeout(ctx, reqTimeoutInterval)
	defer cancel()

	err := func() error {
		select {
		case <-ctx.Done():
			return errors.New("describe partion timeout")
		default:
Q
quicksilver 已提交
509
			return p.sched.DdQueue.Enqueue(dpt)
N
neza2017 已提交
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
		}
	}()

	if err != nil {
		return &servicepb.PartitionDescription{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    err.Error(),
			},
			Name:       in,
			Statistics: nil,
		}, nil
	}

	err = dpt.WaitToFinish()
	if err != nil {
		return &servicepb.PartitionDescription{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    err.Error(),
			},
			Name:       in,
			Statistics: nil,
		}, nil
	}
	return dpt.result, nil
536 537 538
}

func (p *Proxy) ShowPartitions(ctx context.Context, req *servicepb.CollectionName) (*servicepb.StringListResponse, error) {
N
neza2017 已提交
539 540 541 542 543 544 545 546
	spt := &ShowPartitionsTask{
		Condition: NewTaskCondition(ctx),
		ShowPartitionRequest: internalpb.ShowPartitionRequest{
			MsgType:        internalpb.MsgType_kShowPartitions,
			ReqID:          0,
			Timestamp:      0,
			ProxyID:        0,
			CollectionName: req,
547
		},
N
neza2017 已提交
548 549 550 551 552 553 554 555 556 557 558 559 560 561
		masterClient: p.masterClient,
		result:       nil,
		ctx:          nil,
	}

	var cancel func()
	spt.ctx, cancel = context.WithTimeout(ctx, reqTimeoutInterval)
	defer cancel()

	err := func() error {
		select {
		case <-ctx.Done():
			return errors.New("show partition timeout")
		default:
Q
quicksilver 已提交
562
			return p.sched.DdQueue.Enqueue(spt)
N
neza2017 已提交
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
		}
	}()

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

	err = spt.WaitToFinish()
	if err != nil {
		return &servicepb.StringListResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    err.Error(),
			},
			Values: nil,
		}, nil
	}
	return spt.result, nil
587
}