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

import (
	"context"
Z
zhenshan.cao 已提交
5
	"errors"
6 7
	"github.com/gogo/protobuf/proto"
	"github.com/zilliztech/milvus-distributed/internal/msgstream"
8 9 10 11 12 13 14 15 16 17
	"log"

	"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{
18
		BaseInsertTask: BaseInsertTask{
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
			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()

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
	var t task = it
	p.taskSch.DmQueue.Enqueue(&t)
	for {
		select {
		case <-ctx.Done():
			log.Print("insert timeout!")
			return &servicepb.IntegerRangeResponse{
				Status: &commonpb.Status{
					ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
					Reason:    "insert timeout!",
				},
			}, errors.New("insert timeout!")
		case result := <-it.resultChan:
			return result, nil
		}
53 54 55 56
	}
}

func (p *Proxy) CreateCollection(ctx context.Context, req *schemapb.CollectionSchema) (*commonpb.Status, error) {
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
	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()

72 73 74 75 76 77 78 79 80 81 82 83 84
	var t task = cct
	p.taskSch.DdQueue.Enqueue(&t)
	for {
		select {
		case <-ctx.Done():
			log.Print("create collection timeout!")
			return &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    "create collection timeout!",
			}, errors.New("create collection timeout!")
		case result := <-cct.resultChan:
			return result, nil
		}
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
	}
}

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()

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
	var t task = qt
	p.taskSch.DqQueue.Enqueue(&t)
	for {
		select {
		case <-ctx.Done():
			log.Print("query timeout!")
			return &servicepb.QueryResult{
				Status: &commonpb.Status{
					ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
					Reason:    "query timeout!",
				},
			}, errors.New("query timeout!")
		case result := <-qt.resultChan:
			return result, nil
		}
120
	}
121 122 123
}

func (p *Proxy) DropCollection(ctx context.Context, req *servicepb.CollectionName) (*commonpb.Status, error) {
124 125 126 127 128 129 130 131 132 133 134 135 136
	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()

137 138 139 140 141 142 143 144 145 146 147 148 149
	var t task = dct
	p.taskSch.DdQueue.Enqueue(&t)
	for {
		select {
		case <-ctx.Done():
			log.Print("create collection timeout!")
			return &commonpb.Status{
				ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
				Reason:    "create collection timeout!",
			}, errors.New("create collection timeout!")
		case result := <-dct.resultChan:
			return result, nil
		}
150
	}
151 152 153
}

func (p *Proxy) HasCollection(ctx context.Context, req *servicepb.CollectionName) (*servicepb.BoolResponse, error) {
154 155 156 157 158
	hct := &HasCollectionTask{
		HasCollectionRequest: internalpb.HasCollectionRequest{
			MsgType: internalpb.MsgType_kHasCollection,
			// TODO: req_id, timestamp, proxy_id
			CollectionName: req,
159
		},
160 161 162 163 164 165 166
		masterClient: p.masterClient,
		done:         make(chan error),
		resultChan:   make(chan *servicepb.BoolResponse),
	}
	hct.ctx, hct.cancel = context.WithCancel(ctx)
	defer hct.cancel()

167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
	var t task = hct
	p.taskSch.DqQueue.Enqueue(&t)
	for {
		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,
			}, errors.New("has collection timeout!")
		case result := <-hct.resultChan:
			return result, nil
		}
183
	}
184 185 186
}

func (p *Proxy) DescribeCollection(ctx context.Context, req *servicepb.CollectionName) (*servicepb.CollectionDescription, error) {
187 188 189 190 191
	dct := &DescribeCollectionTask{
		DescribeCollectionRequest: internalpb.DescribeCollectionRequest{
			MsgType: internalpb.MsgType_kDescribeCollection,
			// TODO: req_id, timestamp, proxy_id
			CollectionName: req,
192
		},
193 194 195 196 197 198 199
		masterClient: p.masterClient,
		done:         make(chan error),
		resultChan:   make(chan *servicepb.CollectionDescription),
	}
	dct.ctx, dct.cancel = context.WithCancel(ctx)
	defer dct.cancel()

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
	var t task = dct
	p.taskSch.DqQueue.Enqueue(&t)
	for {
		select {
		case <-ctx.Done():
			log.Print("has collection timeout!")
			return &servicepb.CollectionDescription{
				Status: &commonpb.Status{
					ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
					Reason:    "describe collection timeout!",
				},
			}, errors.New("describe collection timeout!")
		case result := <-dct.resultChan:
			return result, nil
		}
215
	}
216 217 218
}

func (p *Proxy) ShowCollections(ctx context.Context, req *commonpb.Empty) (*servicepb.StringListResponse, error) {
219 220 221 222
	sct := &ShowCollectionsTask{
		ShowCollectionRequest: internalpb.ShowCollectionRequest{
			MsgType: internalpb.MsgType_kDescribeCollection,
			// TODO: req_id, timestamp, proxy_id
223
		},
224 225 226 227 228 229 230
		masterClient: p.masterClient,
		done:         make(chan error),
		resultChan:   make(chan *servicepb.StringListResponse),
	}
	sct.ctx, sct.cancel = context.WithCancel(ctx)
	defer sct.cancel()

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
	var t task = sct
	p.taskSch.DqQueue.Enqueue(&t)
	for {
		select {
		case <-ctx.Done():
			log.Print("show collections timeout!")
			return &servicepb.StringListResponse{
				Status: &commonpb.Status{
					ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
					Reason:    "show collections timeout!",
				},
			}, errors.New("show collections timeout!")
		case result := <-sct.resultChan:
			return result, nil
		}
246
	}
247 248 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 279 280 281 282 283 284 285 286 287 288 289
}

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
}