grpc_service.go 13.8 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
	}
Z
zhenshan.cao 已提交
39 40 41
	if len(it.PartitionTag) <= 0 {
		it.PartitionTag = Params.defaultPartitionTag()
	}
42

D
dragondriver 已提交
43 44
	var cancel func()
	it.ctx, cancel = context.WithTimeout(ctx, reqTimeoutInterval)
45

D
dragondriver 已提交
46
	defer cancel()
47

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

	return it.result, nil
78 79 80
}

func (p *Proxy) CreateCollection(ctx context.Context, req *schemapb.CollectionSchema) (*commonpb.Status, error) {
81
	cct := &CreateCollectionTask{
D
dragondriver 已提交
82
		Condition: NewTaskCondition(ctx),
83 84 85 86 87
		CreateCollectionRequest: internalpb.CreateCollectionRequest{
			MsgType: internalpb.MsgType_kCreateCollection,
			Schema:  &commonpb.Blob{},
		},
		masterClient: p.masterClient,
Z
zhenshan.cao 已提交
88
		schema:       req,
89
	}
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
		SearchRequest: internalpb.SearchRequest{
			MsgType: internalpb.MsgType_kSearch,
			Query:   &commonpb.Blob{},
		},
		queryMsgStream: p.queryMsgStream,
		resultBuf:      make(chan []*internalpb.SearchResult),
Z
zhenshan.cao 已提交
130
		query:          req,
131
	}
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
		DropCollectionRequest: internalpb.DropCollectionRequest{
177
			MsgType:        internalpb.MsgType_kDropCollection,
178 179 180 181
			CollectionName: req,
		},
		masterClient: p.masterClient,
	}
D
dragondriver 已提交
182 183 184
	var cancel func()
	dct.ctx, cancel = context.WithTimeout(ctx, reqTimeoutInterval)
	defer cancel()
185

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

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

	return dct.result, nil
211 212 213
}

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

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

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

	return hct.result, nil
255 256 257
}

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

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

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

	return dct.result, nil
299 300 301
}

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

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

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

	return sct.result, nil
342 343 344
}

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

	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

387 388 389
}

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

	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

433 434 435
}

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

	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

485 486 487
}

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

	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
539 540 541
}

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

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