service.go 13.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied. See the License for the specific language governing permissions and limitations under the License.

12 13 14 15
package grpcproxynode

import (
	"context"
G
godchen 已提交
16
	"fmt"
S
sunby 已提交
17
	"io"
18
	"math"
19 20 21
	"net"
	"strconv"
	"sync"
22 23
	"time"

24
	"go.uber.org/zap"
25 26
	"google.golang.org/grpc"

X
Xiangyu Wang 已提交
27 28 29 30
	grpcdataserviceclient "github.com/milvus-io/milvus/internal/distributed/dataservice/client"
	grpcindexserviceclient "github.com/milvus-io/milvus/internal/distributed/indexservice/client"
	grpcmasterserviceclient "github.com/milvus-io/milvus/internal/distributed/masterservice/client"
	grpcqueryserviceclient "github.com/milvus-io/milvus/internal/distributed/queryservice/client"
G
godchen 已提交
31
	otgrpc "github.com/opentracing-contrib/go-grpc"
32

X
Xiangyu Wang 已提交
33 34 35 36 37 38 39 40 41
	"github.com/milvus-io/milvus/internal/log"
	"github.com/milvus-io/milvus/internal/msgstream"
	"github.com/milvus-io/milvus/internal/proto/commonpb"
	"github.com/milvus-io/milvus/internal/proto/internalpb"
	"github.com/milvus-io/milvus/internal/proto/milvuspb"
	"github.com/milvus-io/milvus/internal/proto/proxypb"
	"github.com/milvus-io/milvus/internal/proxynode"
	"github.com/milvus-io/milvus/internal/util/funcutil"
	"github.com/milvus-io/milvus/internal/util/trace"
42
	"github.com/opentracing/opentracing-go"
43 44
)

45 46 47 48
const (
	GRPCMaxMagSize = 2 << 30
)

49
type Server struct {
Z
zhenshan.cao 已提交
50 51
	ctx        context.Context
	wg         sync.WaitGroup
G
godchen 已提交
52
	proxynode  *proxynode.ProxyNode
Z
zhenshan.cao 已提交
53
	grpcServer *grpc.Server
54

Z
zhenshan.cao 已提交
55
	grpcErrChan chan error
56

57 58
	masterServiceClient *grpcmasterserviceclient.GrpcClient
	dataServiceClient   *grpcdataserviceclient.Client
Z
zhenshan.cao 已提交
59 60
	queryServiceClient  *grpcqueryserviceclient.Client
	indexServiceClient  *grpcindexserviceclient.Client
S
sunby 已提交
61 62 63

	tracer opentracing.Tracer
	closer io.Closer
Z
zhenshan.cao 已提交
64
}
65

G
groot 已提交
66
func NewServer(ctx context.Context, factory msgstream.Factory) (*Server, error) {
67

S
sunby 已提交
68
	var err error
Z
zhenshan.cao 已提交
69
	server := &Server{
X
Xiangyu Wang 已提交
70
		ctx:         ctx,
Z
zhenshan.cao 已提交
71
		grpcErrChan: make(chan error),
72 73
	}

G
godchen 已提交
74
	server.proxynode, err = proxynode.NewProxyNode(server.ctx, factory)
75
	if err != nil {
Z
zhenshan.cao 已提交
76
		return nil, err
77
	}
Z
zhenshan.cao 已提交
78 79
	return server, err
}
80

Z
zhenshan.cao 已提交
81
func (s *Server) startGrpcLoop(grpcPort int) {
82

Z
zhenshan.cao 已提交
83
	defer s.wg.Done()
84

85
	log.Debug("proxynode", zap.Int("network port", grpcPort))
Z
zhenshan.cao 已提交
86
	lis, err := net.Listen("tcp", ":"+strconv.Itoa(grpcPort))
87
	if err != nil {
88
		log.Warn("proxynode", zap.String("Server:failed to listen:", err.Error()))
Z
zhenshan.cao 已提交
89 90
		s.grpcErrChan <- err
		return
91 92
	}

Z
zhenshan.cao 已提交
93 94
	ctx, cancel := context.WithCancel(s.ctx)
	defer cancel()
95

G
godchen 已提交
96
	tracer := opentracing.GlobalTracer()
97 98 99 100 101
	s.grpcServer = grpc.NewServer(
		grpc.MaxRecvMsgSize(math.MaxInt32),
		grpc.MaxSendMsgSize(math.MaxInt32),
		grpc.UnaryInterceptor(
			otgrpc.OpenTracingServerInterceptor(tracer)),
G
godchen 已提交
102
		grpc.StreamInterceptor(
103 104
			otgrpc.OpenTracingStreamServerInterceptor(tracer)),
		grpc.MaxRecvMsgSize(GRPCMaxMagSize))
Z
zhenshan.cao 已提交
105 106
	proxypb.RegisterProxyNodeServiceServer(s.grpcServer, s)
	milvuspb.RegisterMilvusServiceServer(s.grpcServer, s)
107

Z
zhenshan.cao 已提交
108 109 110
	go funcutil.CheckGrpcReady(ctx, s.grpcErrChan)
	if err := s.grpcServer.Serve(lis); err != nil {
		s.grpcErrChan <- err
111 112
	}

Z
zhenshan.cao 已提交
113
}
114

Z
zhenshan.cao 已提交
115
func (s *Server) Run() error {
116

Z
zhenshan.cao 已提交
117
	if err := s.init(); err != nil {
118
		return err
119
	}
120
	log.Debug("proxy node init done ...")
121

Z
zhenshan.cao 已提交
122
	if err := s.start(); err != nil {
123 124
		return err
	}
125
	log.Debug("proxy node start done ...")
126 127 128
	return nil
}

Z
zhenshan.cao 已提交
129 130
func (s *Server) init() error {
	var err error
131
	Params.Init()
Z
zhenshan.cao 已提交
132 133
	if !funcutil.CheckPortAvailable(Params.Port) {
		Params.Port = funcutil.GetAvailablePort()
134
		log.Warn("ProxyNode init", zap.Any("Port", Params.Port))
Z
zhenshan.cao 已提交
135
	}
Z
zhenshan.cao 已提交
136 137
	Params.LoadFromEnv()
	Params.LoadFromArgs()
Z
zhenshan.cao 已提交
138
	Params.Address = Params.IP + ":" + strconv.FormatInt(int64(Params.Port), 10)
D
dragondriver 已提交
139

B
bigsheeper 已提交
140 141 142 143 144 145 146
	proxynode.Params.Init()
	log.Debug("init params done ...")
	proxynode.Params.NetworkPort = Params.Port
	proxynode.Params.IP = Params.IP
	proxynode.Params.NetworkAddress = Params.Address
	// for purpose of ID Allocator
	proxynode.Params.MasterAddress = Params.MasterAddress
147

N
neza2017 已提交
148
	closer := trace.InitTracing(fmt.Sprintf("proxy_node ip: %s, port: %d", Params.IP, Params.Port))
G
godchen 已提交
149 150
	s.closer = closer

B
bigsheeper 已提交
151 152 153 154
	log.Debug("proxynode", zap.String("proxy host", Params.IP))
	log.Debug("proxynode", zap.Int("proxy port", Params.Port))
	log.Debug("proxynode", zap.String("proxy address", Params.Address))

155 156
	err = s.proxynode.Register()
	if err != nil {
Z
zhenshan.cao 已提交
157
		log.Debug("ProxyNode Register etcd failed ", zap.Error(err))
158 159 160
		return err
	}

Z
zhenshan.cao 已提交
161
	s.wg.Add(1)
X
Xiangyu Wang 已提交
162
	go s.startGrpcLoop(Params.Port)
Z
zhenshan.cao 已提交
163 164
	// wait for grpc server loop start
	err = <-s.grpcErrChan
165
	log.Debug("create grpc server ...")
Z
zhenshan.cao 已提交
166 167
	if err != nil {
		return err
D
dragondriver 已提交
168
	}
Z
zhenshan.cao 已提交
169 170

	masterServiceAddr := Params.MasterAddress
Z
zhenshan.cao 已提交
171
	log.Debug("ProxyNode", zap.String("master address", masterServiceAddr))
Z
zhenshan.cao 已提交
172
	timeout := 3 * time.Second
173
	s.masterServiceClient, err = grpcmasterserviceclient.NewClient(s.ctx, proxynode.Params.MetaRootPath, proxynode.Params.EtcdEndpoints, timeout)
Z
zhenshan.cao 已提交
174
	if err != nil {
Z
zhenshan.cao 已提交
175
		log.Debug("ProxyNode new masterServiceClient failed ", zap.Error(err))
Z
zhenshan.cao 已提交
176
		return err
D
dragondriver 已提交
177
	}
Z
zhenshan.cao 已提交
178
	err = s.masterServiceClient.Init()
D
dragondriver 已提交
179
	if err != nil {
Z
zhenshan.cao 已提交
180
		log.Debug("ProxyNode new masterServiceClient Init ", zap.Error(err))
Z
zhenshan.cao 已提交
181
		return err
D
dragondriver 已提交
182
	}
183
	err = funcutil.WaitForComponentHealthy(s.ctx, s.masterServiceClient, "MasterService", 1000000, time.Millisecond*200)
184 185

	if err != nil {
Z
zhenshan.cao 已提交
186
		log.Debug("ProxyNode WaitForComponentHealthy master service failed ", zap.Error(err))
187 188
		panic(err)
	}
G
godchen 已提交
189
	s.proxynode.SetMasterClient(s.masterServiceClient)
190
	log.Debug("set master client ...")
D
dragondriver 已提交
191

Z
zhenshan.cao 已提交
192
	dataServiceAddr := Params.DataServiceAddress
Z
zhenshan.cao 已提交
193
	log.Debug("ProxyNode", zap.String("data service address", dataServiceAddr))
194
	s.dataServiceClient = grpcdataserviceclient.NewClient(proxynode.Params.MetaRootPath, proxynode.Params.EtcdEndpoints, timeout)
Z
zhenshan.cao 已提交
195 196
	err = s.dataServiceClient.Init()
	if err != nil {
Z
zhenshan.cao 已提交
197
		log.Debug("ProxyNode dataServiceClient init failed ", zap.Error(err))
Z
zhenshan.cao 已提交
198 199
		return err
	}
G
godchen 已提交
200
	s.proxynode.SetDataServiceClient(s.dataServiceClient)
201
	log.Debug("set data service address ...")
Z
zhenshan.cao 已提交
202 203

	indexServiceAddr := Params.IndexServerAddress
Z
zhenshan.cao 已提交
204
	log.Debug("ProxyNode", zap.String("index server address", indexServiceAddr))
205
	s.indexServiceClient = grpcindexserviceclient.NewClient(proxynode.Params.MetaRootPath, proxynode.Params.EtcdEndpoints, timeout)
Z
zhenshan.cao 已提交
206
	err = s.indexServiceClient.Init()
207
	if err != nil {
Z
zhenshan.cao 已提交
208
		log.Debug("ProxyNode indexServiceClient init failed ", zap.Error(err))
209 210
		return err
	}
G
godchen 已提交
211
	s.proxynode.SetIndexServiceClient(s.indexServiceClient)
212
	log.Debug("set index service client ...")
213

D
dragondriver 已提交
214
	queryServiceAddr := Params.QueryServiceAddress
Z
zhenshan.cao 已提交
215
	log.Debug("ProxyNode", zap.String("query server address", queryServiceAddr))
216
	s.queryServiceClient, err = grpcqueryserviceclient.NewClient(proxynode.Params.MetaRootPath, proxynode.Params.EtcdEndpoints, timeout)
D
dragondriver 已提交
217 218 219 220 221 222 223
	if err != nil {
		return err
	}
	err = s.queryServiceClient.Init()
	if err != nil {
		return err
	}
G
godchen 已提交
224
	s.proxynode.SetQueryServiceClient(s.queryServiceClient)
225
	log.Debug("set query service client ...")
226

G
godchen 已提交
227
	s.proxynode.UpdateStateCode(internalpb.StateCode_Initializing)
G
godchen 已提交
228 229
	log.Debug("proxynode",
		zap.Any("state of proxynode", internalpb.StateCode_Initializing))
230

G
godchen 已提交
231
	if err := s.proxynode.Init(); err != nil {
G
godchen 已提交
232
		log.Debug("proxynode", zap.String("proxynode init error", err.Error()))
Z
zhenshan.cao 已提交
233 234 235 236 237
		return err
	}

	return nil
}
238

Z
zhenshan.cao 已提交
239
func (s *Server) start() error {
G
godchen 已提交
240
	return s.proxynode.Start()
241 242 243 244
}

func (s *Server) Stop() error {
	var err error
N
neza2017 已提交
245 246 247 248 249
	if s.closer != nil {
		if err = s.closer.Close(); err != nil {
			return err
		}
	}
250 251 252 253 254

	if s.grpcServer != nil {
		s.grpcServer.GracefulStop()
	}

G
godchen 已提交
255
	err = s.proxynode.Stop()
256 257 258 259 260 261 262 263 264
	if err != nil {
		return err
	}

	s.wg.Wait()

	return nil
}

G
godchen 已提交
265 266 267 268 269 270 271 272
func (s *Server) GetComponentStates(ctx context.Context, request *internalpb.GetComponentStatesRequest) (*internalpb.ComponentStates, error) {
	return s.proxynode.GetComponentStates(ctx)
}

func (s *Server) GetStatisticsChannel(ctx context.Context, request *internalpb.GetStatisticsChannelRequest) (*milvuspb.StringResponse, error) {
	return s.proxynode.GetStatisticsChannel(ctx)
}

273
func (s *Server) InvalidateCollectionMetaCache(ctx context.Context, request *proxypb.InvalidateCollMetaCacheRequest) (*commonpb.Status, error) {
G
godchen 已提交
274
	return s.proxynode.InvalidateCollectionMetaCache(ctx, request)
275 276 277
}

func (s *Server) CreateCollection(ctx context.Context, request *milvuspb.CreateCollectionRequest) (*commonpb.Status, error) {
G
godchen 已提交
278
	return s.proxynode.CreateCollection(ctx, request)
279 280 281
}

func (s *Server) DropCollection(ctx context.Context, request *milvuspb.DropCollectionRequest) (*commonpb.Status, error) {
G
godchen 已提交
282
	return s.proxynode.DropCollection(ctx, request)
283 284 285
}

func (s *Server) HasCollection(ctx context.Context, request *milvuspb.HasCollectionRequest) (*milvuspb.BoolResponse, error) {
G
godchen 已提交
286
	return s.proxynode.HasCollection(ctx, request)
287 288 289
}

func (s *Server) LoadCollection(ctx context.Context, request *milvuspb.LoadCollectionRequest) (*commonpb.Status, error) {
G
godchen 已提交
290
	return s.proxynode.LoadCollection(ctx, request)
291 292 293
}

func (s *Server) ReleaseCollection(ctx context.Context, request *milvuspb.ReleaseCollectionRequest) (*commonpb.Status, error) {
G
godchen 已提交
294
	return s.proxynode.ReleaseCollection(ctx, request)
295 296 297
}

func (s *Server) DescribeCollection(ctx context.Context, request *milvuspb.DescribeCollectionRequest) (*milvuspb.DescribeCollectionResponse, error) {
G
godchen 已提交
298
	return s.proxynode.DescribeCollection(ctx, request)
299 300
}

G
godchen 已提交
301 302
func (s *Server) GetCollectionStatistics(ctx context.Context, request *milvuspb.GetCollectionStatisticsRequest) (*milvuspb.GetCollectionStatisticsResponse, error) {
	return s.proxynode.GetCollectionStatistics(ctx, request)
303 304
}

G
godchen 已提交
305 306
func (s *Server) ShowCollections(ctx context.Context, request *milvuspb.ShowCollectionsRequest) (*milvuspb.ShowCollectionsResponse, error) {
	return s.proxynode.ShowCollections(ctx, request)
307 308 309
}

func (s *Server) CreatePartition(ctx context.Context, request *milvuspb.CreatePartitionRequest) (*commonpb.Status, error) {
G
godchen 已提交
310
	return s.proxynode.CreatePartition(ctx, request)
311 312 313
}

func (s *Server) DropPartition(ctx context.Context, request *milvuspb.DropPartitionRequest) (*commonpb.Status, error) {
G
godchen 已提交
314
	return s.proxynode.DropPartition(ctx, request)
315 316 317
}

func (s *Server) HasPartition(ctx context.Context, request *milvuspb.HasPartitionRequest) (*milvuspb.BoolResponse, error) {
G
godchen 已提交
318
	return s.proxynode.HasPartition(ctx, request)
319 320
}

G
godchen 已提交
321 322
func (s *Server) LoadPartitions(ctx context.Context, request *milvuspb.LoadPartitionsRequest) (*commonpb.Status, error) {
	return s.proxynode.LoadPartitions(ctx, request)
323 324
}

G
godchen 已提交
325 326
func (s *Server) ReleasePartitions(ctx context.Context, request *milvuspb.ReleasePartitionsRequest) (*commonpb.Status, error) {
	return s.proxynode.ReleasePartitions(ctx, request)
327 328
}

G
godchen 已提交
329 330
func (s *Server) GetPartitionStatistics(ctx context.Context, request *milvuspb.GetPartitionStatisticsRequest) (*milvuspb.GetPartitionStatisticsResponse, error) {
	return s.proxynode.GetPartitionStatistics(ctx, request)
331 332
}

G
godchen 已提交
333 334
func (s *Server) ShowPartitions(ctx context.Context, request *milvuspb.ShowPartitionsRequest) (*milvuspb.ShowPartitionsResponse, error) {
	return s.proxynode.ShowPartitions(ctx, request)
335 336 337
}

func (s *Server) CreateIndex(ctx context.Context, request *milvuspb.CreateIndexRequest) (*commonpb.Status, error) {
G
godchen 已提交
338
	return s.proxynode.CreateIndex(ctx, request)
339 340
}

B
BossZou 已提交
341
func (s *Server) DropIndex(ctx context.Context, request *milvuspb.DropIndexRequest) (*commonpb.Status, error) {
G
godchen 已提交
342
	return s.proxynode.DropIndex(ctx, request)
B
BossZou 已提交
343 344
}

345
func (s *Server) DescribeIndex(ctx context.Context, request *milvuspb.DescribeIndexRequest) (*milvuspb.DescribeIndexResponse, error) {
G
godchen 已提交
346
	return s.proxynode.DescribeIndex(ctx, request)
347 348
}

349 350 351 352 353 354
// GetIndexBuildProgress gets index build progress with filed_name and index_name.
// IndexRows is the num of indexed rows. And TotalRows is the total number of segment rows.
func (s *Server) GetIndexBuildProgress(ctx context.Context, request *milvuspb.GetIndexBuildProgressRequest) (*milvuspb.GetIndexBuildProgressResponse, error) {
	return s.proxynode.GetIndexBuildProgress(ctx, request)
}

G
godchen 已提交
355 356
func (s *Server) GetIndexState(ctx context.Context, request *milvuspb.GetIndexStateRequest) (*milvuspb.GetIndexStateResponse, error) {
	return s.proxynode.GetIndexState(ctx, request)
357 358 359
}

func (s *Server) Insert(ctx context.Context, request *milvuspb.InsertRequest) (*milvuspb.InsertResponse, error) {
G
godchen 已提交
360
	return s.proxynode.Insert(ctx, request)
361 362 363
}

func (s *Server) Search(ctx context.Context, request *milvuspb.SearchRequest) (*milvuspb.SearchResults, error) {
G
godchen 已提交
364
	return s.proxynode.Search(ctx, request)
365 366
}

367 368 369 370
func (s *Server) Retrieve(ctx context.Context, request *milvuspb.RetrieveRequest) (*milvuspb.RetrieveResults, error) {
	return s.proxynode.Retrieve(ctx, request)
}

371
func (s *Server) Flush(ctx context.Context, request *milvuspb.FlushRequest) (*commonpb.Status, error) {
G
godchen 已提交
372
	return s.proxynode.Flush(ctx, request)
373 374
}

X
Xiangyu Wang 已提交
375 376 377 378
func (s *Server) Query(ctx context.Context, request *milvuspb.QueryRequest) (*milvuspb.QueryResults, error) {
	return s.proxynode.Query(ctx, request)
}

G
godchen 已提交
379 380
func (s *Server) GetDdChannel(ctx context.Context, request *internalpb.GetDdChannelRequest) (*milvuspb.StringResponse, error) {
	return s.proxynode.GetDdChannel(ctx, request)
381
}
Z
zhenshan.cao 已提交
382

G
godchen 已提交
383 384
func (s *Server) GetPersistentSegmentInfo(ctx context.Context, request *milvuspb.GetPersistentSegmentInfoRequest) (*milvuspb.GetPersistentSegmentInfoResponse, error) {
	return s.proxynode.GetPersistentSegmentInfo(ctx, request)
Z
zhenshan.cao 已提交
385
}
Z
zhenshan.cao 已提交
386

G
godchen 已提交
387 388
func (s *Server) GetQuerySegmentInfo(ctx context.Context, request *milvuspb.GetQuerySegmentInfoRequest) (*milvuspb.GetQuerySegmentInfoResponse, error) {
	return s.proxynode.GetQuerySegmentInfo(ctx, request)
Z
zhenshan.cao 已提交
389 390

}
391

X
Xiangyu Wang 已提交
392 393 394 395
func (s *Server) Dummy(ctx context.Context, request *milvuspb.DummyRequest) (*milvuspb.DummyResponse, error) {
	return s.proxynode.Dummy(ctx, request)
}

G
godchen 已提交
396 397
func (s *Server) RegisterLink(ctx context.Context, request *milvuspb.RegisterLinkRequest) (*milvuspb.RegisterLinkResponse, error) {
	return s.proxynode.RegisterLink(ctx, request)
398
}