grpc_service.go 9.2 KB
Newer Older
1 2 3 4
package master

import (
	"context"
Z
zhenshan.cao 已提交
5 6
	"time"

7
	"github.com/zilliztech/milvus-distributed/internal/errors"
Z
zhenshan.cao 已提交
8
	"github.com/zilliztech/milvus-distributed/internal/master/id"
9 10 11 12 13
	"github.com/zilliztech/milvus-distributed/internal/proto/commonpb"
	"github.com/zilliztech/milvus-distributed/internal/proto/internalpb"
	"github.com/zilliztech/milvus-distributed/internal/proto/servicepb"
)

14 15
const slowThreshold = 5 * time.Millisecond

16
func (s *Master) CreateCollection(ctx context.Context, in *internalpb.CreateCollectionRequest) (*commonpb.Status, error) {
17 18 19
	var t task = &createCollectionTask{
		req: in,
		baseTask: baseTask{
Z
zhenshan.cao 已提交
20
			kvBase: s.kvBase,
21
			mt:     &s.mt,
22 23 24 25
			cv:     make(chan int),
		},
	}

26 27
	var err = s.scheduler.Enqueue(&t)
	if err != nil {
28 29 30 31 32 33 34
		err := errors.New("Enqueue failed")
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    "Enqueue failed",
		}, err
	}

35 36
	err = t.WaitToFinish(ctx)
	if err != nil {
37 38 39 40 41 42 43
		err := errors.New("WaitToFinish failed")
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    "WaitToFinish failed",
		}, err
	}

44 45 46
	return &commonpb.Status{
		ErrorCode: commonpb.ErrorCode_SUCCESS,
	}, nil
47 48
}

49 50 51 52
func (s *Master) DropCollection(ctx context.Context, in *internalpb.DropCollectionRequest) (*commonpb.Status, error) {
	var t task = &dropCollectionTask{
		req: in,
		baseTask: baseTask{
Z
zhenshan.cao 已提交
53
			kvBase: s.kvBase,
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
			mt:     &s.mt,
			cv:     make(chan int),
		},
	}

	var err = s.scheduler.Enqueue(&t)
	if err != nil {
		err := errors.New("Enqueue failed")
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    "Enqueue failed",
		}, err
	}

	err = t.WaitToFinish(ctx)
	if err != nil {
		err := errors.New("WaitToFinish failed")
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    "WaitToFinish failed",
		}, err
	}

77
	return &commonpb.Status{
78
		ErrorCode: commonpb.ErrorCode_SUCCESS,
79 80 81
	}, nil
}

82 83 84 85
func (s *Master) HasCollection(ctx context.Context, in *internalpb.HasCollectionRequest) (*servicepb.BoolResponse, error) {
	var t task = &hasCollectionTask{
		req: in,
		baseTask: baseTask{
Z
zhenshan.cao 已提交
86
			kvBase: s.kvBase,
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
			mt:     &s.mt,
			cv:     make(chan int),
		},
		hasCollection: false,
	}

	var err = s.scheduler.Enqueue(&t)
	if err != nil {
		err := errors.New("Enqueue failed")
		return &servicepb.BoolResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    "Enqueue failed",
			},
			Value: t.(*hasCollectionTask).hasCollection,
		}, err
	}

	err = t.WaitToFinish(ctx)
	if err != nil {
		err := errors.New("WaitToFinish failed")
		return &servicepb.BoolResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    "WaitToFinish failed",
			},
			Value: t.(*hasCollectionTask).hasCollection,
		}, err
	}

117 118
	return &servicepb.BoolResponse{
		Status: &commonpb.Status{
119
			ErrorCode: commonpb.ErrorCode_SUCCESS,
120
		},
121
		Value: t.(*hasCollectionTask).hasCollection,
122 123 124
	}, nil
}

125 126 127 128
func (s *Master) DescribeCollection(ctx context.Context, in *internalpb.DescribeCollectionRequest) (*servicepb.CollectionDescription, error) {
	var t task = &describeCollectionTask{
		req: in,
		baseTask: baseTask{
Z
zhenshan.cao 已提交
129
			kvBase: s.kvBase,
130 131
			mt:     &s.mt,
			cv:     make(chan int),
132
		},
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
		description: nil,
	}

	var err = s.scheduler.Enqueue(&t)
	if err != nil {
		err := errors.New("Enqueue failed")
		return t.(*describeCollectionTask).description, err
	}

	err = t.WaitToFinish(ctx)
	if err != nil {
		err := errors.New("WaitToFinish failed")
		return t.(*describeCollectionTask).description, err
	}

	return t.(*describeCollectionTask).description, nil
149 150
}

151 152 153 154
func (s *Master) ShowCollections(ctx context.Context, in *internalpb.ShowCollectionRequest) (*servicepb.StringListResponse, error) {
	var t task = &showCollectionsTask{
		req: in,
		baseTask: baseTask{
Z
zhenshan.cao 已提交
155
			kvBase: s.kvBase,
156 157
			mt:     &s.mt,
			cv:     make(chan int),
158
		},
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
		stringListResponse: nil,
	}

	var err = s.scheduler.Enqueue(&t)
	if err != nil {
		err := errors.New("Enqueue failed")
		return t.(*showCollectionsTask).stringListResponse, err
	}

	err = t.WaitToFinish(ctx)
	if err != nil {
		err := errors.New("WaitToFinish failed")
		return t.(*showCollectionsTask).stringListResponse, err
	}

	return t.(*showCollectionsTask).stringListResponse, nil
175 176
}

177 178 179 180 181
//////////////////////////////////////////////////////////////////////////
func (s *Master) CreatePartition(ctx context.Context, in *internalpb.CreatePartitionRequest) (*commonpb.Status, error) {
	var t task = &createPartitionTask{
		req: in,
		baseTask: baseTask{
Z
zhenshan.cao 已提交
182
			kvBase: s.kvBase,
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
			mt:     &s.mt,
			cv:     make(chan int),
		},
	}

	var err = s.scheduler.Enqueue(&t)
	if err != nil {
		err := errors.New("Enqueue failed")
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    "Enqueue failed",
		}, err
	}

	err = t.WaitToFinish(ctx)
	if err != nil {
		err := errors.New("WaitToFinish failed")
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    "WaitToFinish failed",
		}, err
	}

206
	return &commonpb.Status{
207
		ErrorCode: commonpb.ErrorCode_SUCCESS,
208 209 210
	}, nil
}

211 212 213 214
func (s *Master) DropPartition(ctx context.Context, in *internalpb.DropPartitionRequest) (*commonpb.Status, error) {
	var t task = &dropPartitionTask{
		req: in,
		baseTask: baseTask{
Z
zhenshan.cao 已提交
215
			kvBase: s.kvBase,
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
			mt:     &s.mt,
			cv:     make(chan int),
		},
	}

	var err = s.scheduler.Enqueue(&t)
	if err != nil {
		err := errors.New("Enqueue failed")
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    "Enqueue failed",
		}, err
	}

	err = t.WaitToFinish(ctx)
	if err != nil {
		err := errors.New("WaitToFinish failed")
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    "WaitToFinish failed",
		}, err
	}

239
	return &commonpb.Status{
240
		ErrorCode: commonpb.ErrorCode_SUCCESS,
241 242 243
	}, nil
}

244 245 246 247
func (s *Master) HasPartition(ctx context.Context, in *internalpb.HasPartitionRequest) (*servicepb.BoolResponse, error) {
	var t task = &hasPartitionTask{
		req: in,
		baseTask: baseTask{
Z
zhenshan.cao 已提交
248
			kvBase: s.kvBase,
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
			mt:     &s.mt,
			cv:     make(chan int),
		},
		hasPartition: false,
	}

	var err = s.scheduler.Enqueue(&t)
	if err != nil {
		err := errors.New("Enqueue failed")
		return &servicepb.BoolResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    "Enqueue failed",
			},
			Value: t.(*hasPartitionTask).hasPartition,
		}, err
	}

	err = t.WaitToFinish(ctx)
	if err != nil {
		err := errors.New("WaitToFinish failed")
		return &servicepb.BoolResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    "WaitToFinish failed",
			},
			Value: t.(*hasPartitionTask).hasPartition,
		}, err
	}

279 280
	return &servicepb.BoolResponse{
		Status: &commonpb.Status{
281
			ErrorCode: commonpb.ErrorCode_SUCCESS,
282
		},
283
		Value: t.(*hasPartitionTask).hasPartition,
284 285 286
	}, nil
}

287 288 289 290
func (s *Master) DescribePartition(ctx context.Context, in *internalpb.DescribePartitionRequest) (*servicepb.PartitionDescription, error) {
	var t task = &describePartitionTask{
		req: in,
		baseTask: baseTask{
Z
zhenshan.cao 已提交
291
			kvBase: s.kvBase,
292 293
			mt:     &s.mt,
			cv:     make(chan int),
294
		},
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
		description: nil,
	}

	var err = s.scheduler.Enqueue(&t)
	if err != nil {
		err := errors.New("Enqueue failed")
		return t.(*describePartitionTask).description, err
	}

	err = t.WaitToFinish(ctx)
	if err != nil {
		err := errors.New("WaitToFinish failed")
		return t.(*describePartitionTask).description, err
	}

	return t.(*describePartitionTask).description, nil
311 312
}

313 314 315 316
func (s *Master) ShowPartitions(ctx context.Context, in *internalpb.ShowPartitionRequest) (*servicepb.StringListResponse, error) {
	var t task = &showPartitionTask{
		req: in,
		baseTask: baseTask{
Z
zhenshan.cao 已提交
317
			kvBase: s.kvBase,
318 319
			mt:     &s.mt,
			cv:     make(chan int),
320
		},
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
		stringListResponse: nil,
	}

	var err = s.scheduler.Enqueue(&t)
	if err != nil {
		err := errors.New("Enqueue failed")
		return t.(*showPartitionTask).stringListResponse, err
	}

	err = t.WaitToFinish(ctx)
	if err != nil {
		err := errors.New("WaitToFinish failed")
		return t.(*showPartitionTask).stringListResponse, err
	}

	return t.(*showPartitionTask).stringListResponse, nil
337
}
338 339 340

//----------------------------------------Internal GRPC Service--------------------------------

Z
zhenshan.cao 已提交
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
func (s *Master) AllocTimestamp(ctx context.Context, request *internalpb.TsoRequest) (*internalpb.TsoResponse, error) {
	count := request.GetCount()
	ts, err := s.tsoAllocator.GenerateTSO(count)

	if err != nil {
		return &internalpb.TsoResponse{
			Status: &commonpb.Status{ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR},
		}, err
	}

	response := &internalpb.TsoResponse{
		Status:    &commonpb.Status{ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR},
		Timestamp: ts,
		Count:     count,
	}

	return response, nil
}

func (s *Master) AllocId(ctx context.Context, request *internalpb.IdRequest) (*internalpb.IdResponse, error) {
	panic("implement me")
	count := request.GetCount()
	ts, err := id.AllocOne()

	if err != nil {
		return &internalpb.IdResponse{
			Status: &commonpb.Status{ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR},
		}, err
	}

	response := &internalpb.IdResponse{
		Status: &commonpb.Status{ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR},
		Id:     ts,
		Count:  count,
	}

	return response, nil
}