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

import (
	"context"
Z
zhenshan.cao 已提交
5
	"errors"
6 7
	"log"

C
cai.zhang 已提交
8 9 10
	"github.com/golang/protobuf/proto"

	"github.com/zilliztech/milvus-distributed/internal/msgstream"
11 12 13 14 15 16 17 18
	"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"
)

func (p *Proxy) Insert(ctx context.Context, in *servicepb.RowBatch) (*servicepb.IntegerRangeResponse, error) {
	it := &InsertTask{
19
		BaseInsertTask: BaseInsertTask{
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
			BaseMsg: msgstream.BaseMsg{
				HashValues: in.HashKeys,
			},
			InsertRequest: internalpb.InsertRequest{
				MsgType:        internalpb.MsgType_kInsert,
				CollectionName: in.CollectionName,
				PartitionTag:   in.PartitionTag,
				RowData:        in.RowData,
			},
		},
		done:                  make(chan error),
		resultChan:            make(chan *servicepb.IntegerRangeResponse),
		manipulationMsgStream: p.manipulationMsgStream,
	}
	it.ctx, it.cancel = context.WithCancel(ctx)
	// TODO: req_id, segment_id, channel_id, proxy_id, timestamps, row_ids

	defer it.cancel()

B
bigsheeper 已提交
39 40 41 42 43 44 45 46 47
	p.taskSch.DmQueue.Enqueue(it)
	select {
	case <-ctx.Done():
		log.Print("insert timeout!")
		return &servicepb.IntegerRangeResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    "insert timeout!",
			},
C
cai.zhang 已提交
48
		}, errors.New("insert timeout")
B
bigsheeper 已提交
49 50
	case result := <-it.resultChan:
		return result, nil
51 52 53 54
	}
}

func (p *Proxy) CreateCollection(ctx context.Context, req *schemapb.CollectionSchema) (*commonpb.Status, error) {
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
	cct := &CreateCollectionTask{
		CreateCollectionRequest: internalpb.CreateCollectionRequest{
			MsgType: internalpb.MsgType_kCreateCollection,
			Schema:  &commonpb.Blob{},
			// TODO: req_id, timestamp, proxy_id
		},
		masterClient: p.masterClient,
		done:         make(chan error),
		resultChan:   make(chan *commonpb.Status),
	}
	schemaBytes, _ := proto.Marshal(req)
	cct.CreateCollectionRequest.Schema.Value = schemaBytes
	cct.ctx, cct.cancel = context.WithCancel(ctx)
	defer cct.cancel()

B
bigsheeper 已提交
70 71 72 73 74 75 76
	p.taskSch.DdQueue.Enqueue(cct)
	select {
	case <-ctx.Done():
		log.Print("create collection timeout!")
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    "create collection timeout!",
C
cai.zhang 已提交
77
		}, errors.New("create collection timeout")
B
bigsheeper 已提交
78 79
	case result := <-cct.resultChan:
		return result, nil
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
	}
}

func (p *Proxy) Search(ctx context.Context, req *servicepb.Query) (*servicepb.QueryResult, error) {
	qt := &QueryTask{
		SearchRequest: internalpb.SearchRequest{
			MsgType: internalpb.MsgType_kSearch,
			Query:   &commonpb.Blob{},
			// TODO: req_id, proxy_id, timestamp, result_channel_id
		},
		queryMsgStream: p.queryMsgStream,
		done:           make(chan error),
		resultBuf:      make(chan []*internalpb.SearchResult),
		resultChan:     make(chan *servicepb.QueryResult),
	}
	qt.ctx, qt.cancel = context.WithCancel(ctx)
	queryBytes, _ := proto.Marshal(req)
	qt.SearchRequest.Query.Value = queryBytes
	defer qt.cancel()

B
bigsheeper 已提交
100 101 102 103 104 105 106 107 108
	p.taskSch.DqQueue.Enqueue(qt)
	select {
	case <-ctx.Done():
		log.Print("query timeout!")
		return &servicepb.QueryResult{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    "query timeout!",
			},
C
cai.zhang 已提交
109
		}, errors.New("query timeout")
B
bigsheeper 已提交
110 111
	case result := <-qt.resultChan:
		return result, nil
112
	}
113 114 115
}

func (p *Proxy) DropCollection(ctx context.Context, req *servicepb.CollectionName) (*commonpb.Status, error) {
116 117 118 119 120 121 122 123 124 125 126 127 128
	dct := &DropCollectionTask{
		DropCollectionRequest: internalpb.DropCollectionRequest{
			MsgType: internalpb.MsgType_kDropCollection,
			// TODO: req_id, timestamp, proxy_id
			CollectionName: req,
		},
		masterClient: p.masterClient,
		done:         make(chan error),
		resultChan:   make(chan *commonpb.Status),
	}
	dct.ctx, dct.cancel = context.WithCancel(ctx)
	defer dct.cancel()

B
bigsheeper 已提交
129 130 131 132 133 134 135
	p.taskSch.DdQueue.Enqueue(dct)
	select {
	case <-ctx.Done():
		log.Print("create collection timeout!")
		return &commonpb.Status{
			ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
			Reason:    "create collection timeout!",
C
cai.zhang 已提交
136
		}, errors.New("create collection timeout")
B
bigsheeper 已提交
137 138
	case result := <-dct.resultChan:
		return result, nil
139
	}
140 141 142
}

func (p *Proxy) HasCollection(ctx context.Context, req *servicepb.CollectionName) (*servicepb.BoolResponse, error) {
143 144 145 146 147
	hct := &HasCollectionTask{
		HasCollectionRequest: internalpb.HasCollectionRequest{
			MsgType: internalpb.MsgType_kHasCollection,
			// TODO: req_id, timestamp, proxy_id
			CollectionName: req,
148
		},
149 150 151 152 153 154 155
		masterClient: p.masterClient,
		done:         make(chan error),
		resultChan:   make(chan *servicepb.BoolResponse),
	}
	hct.ctx, hct.cancel = context.WithCancel(ctx)
	defer hct.cancel()

B
bigsheeper 已提交
156 157 158 159 160 161 162 163 164 165
	p.taskSch.DqQueue.Enqueue(hct)
	select {
	case <-ctx.Done():
		log.Print("has collection timeout!")
		return &servicepb.BoolResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    "has collection timeout!",
			},
			Value: false,
C
cai.zhang 已提交
166
		}, errors.New("has collection timeout")
B
bigsheeper 已提交
167 168
	case result := <-hct.resultChan:
		return result, nil
169
	}
170 171 172
}

func (p *Proxy) DescribeCollection(ctx context.Context, req *servicepb.CollectionName) (*servicepb.CollectionDescription, error) {
173 174 175 176 177
	dct := &DescribeCollectionTask{
		DescribeCollectionRequest: internalpb.DescribeCollectionRequest{
			MsgType: internalpb.MsgType_kDescribeCollection,
			// TODO: req_id, timestamp, proxy_id
			CollectionName: req,
178
		},
179 180 181 182 183 184 185
		masterClient: p.masterClient,
		done:         make(chan error),
		resultChan:   make(chan *servicepb.CollectionDescription),
	}
	dct.ctx, dct.cancel = context.WithCancel(ctx)
	defer dct.cancel()

B
bigsheeper 已提交
186 187 188 189 190 191 192 193 194
	p.taskSch.DqQueue.Enqueue(dct)
	select {
	case <-ctx.Done():
		log.Print("has collection timeout!")
		return &servicepb.CollectionDescription{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    "describe collection timeout!",
			},
C
cai.zhang 已提交
195
		}, errors.New("describe collection timeout")
B
bigsheeper 已提交
196 197
	case result := <-dct.resultChan:
		return result, nil
198
	}
199 200 201
}

func (p *Proxy) ShowCollections(ctx context.Context, req *commonpb.Empty) (*servicepb.StringListResponse, error) {
202 203 204 205
	sct := &ShowCollectionsTask{
		ShowCollectionRequest: internalpb.ShowCollectionRequest{
			MsgType: internalpb.MsgType_kDescribeCollection,
			// TODO: req_id, timestamp, proxy_id
206
		},
207 208 209 210 211 212 213
		masterClient: p.masterClient,
		done:         make(chan error),
		resultChan:   make(chan *servicepb.StringListResponse),
	}
	sct.ctx, sct.cancel = context.WithCancel(ctx)
	defer sct.cancel()

B
bigsheeper 已提交
214 215 216 217 218 219 220 221 222
	p.taskSch.DqQueue.Enqueue(sct)
	select {
	case <-ctx.Done():
		log.Print("show collections timeout!")
		return &servicepb.StringListResponse{
			Status: &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    "show collections timeout!",
			},
C
cai.zhang 已提交
223
		}, errors.New("show collections timeout")
B
bigsheeper 已提交
224 225
	case result := <-sct.resultChan:
		return result, nil
226
	}
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
}

func (p *Proxy) CreatePartition(ctx context.Context, in *servicepb.PartitionName) (*commonpb.Status, error) {
	return &commonpb.Status{
		ErrorCode: 0,
		Reason:    "",
	}, nil
}

func (p *Proxy) DropPartition(ctx context.Context, in *servicepb.PartitionName) (*commonpb.Status, error) {
	return &commonpb.Status{
		ErrorCode: 0,
		Reason:    "",
	}, nil
}

func (p *Proxy) HasPartition(ctx context.Context, in *servicepb.PartitionName) (*servicepb.BoolResponse, error) {
	return &servicepb.BoolResponse{
		Status: &commonpb.Status{
			ErrorCode: 0,
			Reason:    "",
		},
		Value: true,
	}, nil
}

func (p *Proxy) DescribePartition(ctx context.Context, in *servicepb.PartitionName) (*servicepb.PartitionDescription, error) {
	return &servicepb.PartitionDescription{
		Status: &commonpb.Status{
			ErrorCode: 0,
			Reason:    "",
		},
	}, nil
}

func (p *Proxy) ShowPartitions(ctx context.Context, req *servicepb.CollectionName) (*servicepb.StringListResponse, error) {
	return &servicepb.StringListResponse{
		Status: &commonpb.Status{
			ErrorCode: 0,
			Reason:    "",
		},
	}, nil
}