grpc_service.go 14.0 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 43
	// TODO: req_id, segment_id, channel_id, proxy_id, timestamps, row_ids

D
dragondriver 已提交
44
	defer cancel()
45

46 47 48 49 50
	fn := func() error {
		select {
		case <-ctx.Done():
			return errors.New("insert timeout")
		default:
Q
quicksilver 已提交
51
			return p.sched.DmQueue.Enqueue(it)
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
		}
	}
	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 已提交
67 68 69
		return &servicepb.IntegerRangeResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
70
				Reason:    err.Error(),
B
bigsheeper 已提交
71
			},
72
		}, nil
73
	}
74 75

	return it.result, nil
76 77 78
}

func (p *Proxy) CreateCollection(ctx context.Context, req *schemapb.CollectionSchema) (*commonpb.Status, error) {
79
	cct := &CreateCollectionTask{
D
dragondriver 已提交
80
		Condition: NewTaskCondition(ctx),
81 82 83 84 85 86 87 88 89
		CreateCollectionRequest: internalpb.CreateCollectionRequest{
			MsgType: internalpb.MsgType_kCreateCollection,
			Schema:  &commonpb.Blob{},
			// TODO: req_id, timestamp, proxy_id
		},
		masterClient: p.masterClient,
	}
	schemaBytes, _ := proto.Marshal(req)
	cct.CreateCollectionRequest.Schema.Value = schemaBytes
D
dragondriver 已提交
90 91 92
	var cancel func()
	cct.ctx, cancel = context.WithTimeout(ctx, reqTimeoutInterval)
	defer cancel()
93

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

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

	return cct.result, nil
119 120 121 122
}

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

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

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

	return qt.result, nil
171 172 173
}

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

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

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

	return dct.result, nil
212 213 214
}

func (p *Proxy) HasCollection(ctx context.Context, req *servicepb.CollectionName) (*servicepb.BoolResponse, error) {
215
	hct := &HasCollectionTask{
D
dragondriver 已提交
216
		Condition: NewTaskCondition(ctx),
217 218 219 220
		HasCollectionRequest: internalpb.HasCollectionRequest{
			MsgType: internalpb.MsgType_kHasCollection,
			// TODO: req_id, timestamp, proxy_id
			CollectionName: req,
221
		},
222 223
		masterClient: p.masterClient,
	}
D
dragondriver 已提交
224 225 226
	var cancel func()
	hct.ctx, cancel = context.WithTimeout(ctx, reqTimeoutInterval)
	defer cancel()
227

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

	err = hct.WaitToFinish()
	if err != nil {
B
bigsheeper 已提交
248 249 250
		return &servicepb.BoolResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
251
				Reason:    err.Error(),
B
bigsheeper 已提交
252
			},
253
		}, nil
254
	}
255 256

	return hct.result, nil
257 258 259
}

func (p *Proxy) DescribeCollection(ctx context.Context, req *servicepb.CollectionName) (*servicepb.CollectionDescription, error) {
260
	dct := &DescribeCollectionTask{
D
dragondriver 已提交
261
		Condition: NewTaskCondition(ctx),
262 263 264 265
		DescribeCollectionRequest: internalpb.DescribeCollectionRequest{
			MsgType: internalpb.MsgType_kDescribeCollection,
			// TODO: req_id, timestamp, proxy_id
			CollectionName: req,
266
		},
267 268
		masterClient: p.masterClient,
	}
D
dragondriver 已提交
269 270 271
	var cancel func()
	dct.ctx, cancel = context.WithTimeout(ctx, reqTimeoutInterval)
	defer cancel()
272

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

	err = dct.WaitToFinish()
	if err != nil {
		return &servicepb.CollectionDescription{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    err.Error(),
			},
298
		}, nil
299 300 301
	}

	return dct.result, nil
302 303 304
}

func (p *Proxy) ShowCollections(ctx context.Context, req *commonpb.Empty) (*servicepb.StringListResponse, error) {
305
	sct := &ShowCollectionsTask{
D
dragondriver 已提交
306
		Condition: NewTaskCondition(ctx),
307 308 309
		ShowCollectionRequest: internalpb.ShowCollectionRequest{
			MsgType: internalpb.MsgType_kDescribeCollection,
			// TODO: req_id, timestamp, proxy_id
310
		},
311 312
		masterClient: p.masterClient,
	}
D
dragondriver 已提交
313 314 315
	var cancel func()
	sct.ctx, cancel = context.WithTimeout(ctx, reqTimeoutInterval)
	defer cancel()
316

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

	err = sct.WaitToFinish()
	if err != nil {
		return &servicepb.StringListResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    err.Error(),
			},
342
		}, nil
343 344 345
	}

	return sct.result, nil
346 347 348
}

func (p *Proxy) CreatePartition(ctx context.Context, in *servicepb.PartitionName) (*commonpb.Status, error) {
N
neza2017 已提交
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
	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 已提交
372
			return p.sched.DdQueue.Enqueue(cpt)
N
neza2017 已提交
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
		}
	}()

	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

391 392 393
}

func (p *Proxy) DropPartition(ctx context.Context, in *servicepb.PartitionName) (*commonpb.Status, error) {
N
neza2017 已提交
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
	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 已提交
418
			return p.sched.DdQueue.Enqueue(dpt)
N
neza2017 已提交
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
		}
	}()

	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

437 438 439
}

func (p *Proxy) HasPartition(ctx context.Context, in *servicepb.PartitionName) (*servicepb.BoolResponse, error) {
N
neza2017 已提交
440 441 442 443 444 445 446 447 448
	hpt := &HasPartitionTask{
		Condition: NewTaskCondition(ctx),
		HasPartitionRequest: internalpb.HasPartitionRequest{
			MsgType:       internalpb.MsgType_kHasPartition,
			ReqID:         0,
			Timestamp:     0,
			ProxyID:       0,
			PartitionName: in,
			//TODO, ReqID,Timestamp,ProxyID
449
		},
N
neza2017 已提交
450 451 452 453 454 455 456 457 458 459 460 461 462 463
		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 已提交
464
			return p.sched.DdQueue.Enqueue(hpt)
N
neza2017 已提交
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
		}
	}()

	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

489 490 491
}

func (p *Proxy) DescribePartition(ctx context.Context, in *servicepb.PartitionName) (*servicepb.PartitionDescription, error) {
N
neza2017 已提交
492 493 494 495 496 497 498 499 500
	dpt := &DescribePartitionTask{
		Condition: NewTaskCondition(ctx),
		DescribePartitionRequest: internalpb.DescribePartitionRequest{
			MsgType:       internalpb.MsgType_kDescribePartition,
			ReqID:         0,
			Timestamp:     0,
			ProxyID:       0,
			PartitionName: in,
			//TODO, ReqID,Timestamp,ProxyID
501
		},
N
neza2017 已提交
502 503 504 505 506 507 508 509 510 511 512 513 514 515
		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 已提交
516
			return p.sched.DdQueue.Enqueue(dpt)
N
neza2017 已提交
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
		}
	}()

	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
543 544 545
}

func (p *Proxy) ShowPartitions(ctx context.Context, req *servicepb.CollectionName) (*servicepb.StringListResponse, error) {
N
neza2017 已提交
546 547 548 549 550 551 552 553
	spt := &ShowPartitionsTask{
		Condition: NewTaskCondition(ctx),
		ShowPartitionRequest: internalpb.ShowPartitionRequest{
			MsgType:        internalpb.MsgType_kShowPartitions,
			ReqID:          0,
			Timestamp:      0,
			ProxyID:        0,
			CollectionName: req,
554
		},
N
neza2017 已提交
555 556 557 558 559 560 561 562 563 564 565 566 567 568
		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 已提交
569
			return p.sched.DdQueue.Enqueue(spt)
N
neza2017 已提交
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
		}
	}()

	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
594
}