2021-01-22 04:57:23 +00:00
|
|
|
package grpcproxyservice
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-02-23 01:58:06 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2021-03-08 07:49:42 +00:00
|
|
|
"math"
|
2021-01-22 04:57:23 +00:00
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
|
2021-03-10 01:56:09 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2021-02-26 09:44:24 +00:00
|
|
|
otgrpc "github.com/opentracing-contrib/go-grpc"
|
2021-02-23 01:58:06 +00:00
|
|
|
"github.com/opentracing/opentracing-go"
|
|
|
|
"github.com/uber/jaeger-client-go/config"
|
2021-03-10 01:56:09 +00:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/log"
|
2021-02-08 06:30:54 +00:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/msgstream"
|
2021-01-29 01:27:26 +00:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/commonpb"
|
2021-01-26 06:55:57 +00:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/internalpb2"
|
2021-01-22 04:57:23 +00:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/milvuspb"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/proxypb"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proxyservice"
|
2021-01-29 01:27:26 +00:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/util/funcutil"
|
2021-01-22 04:57:23 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
2021-01-29 09:29:31 +00:00
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
wg sync.WaitGroup
|
2021-01-29 01:27:26 +00:00
|
|
|
|
|
|
|
grpcServer *grpc.Server
|
|
|
|
grpcErrChan chan error
|
|
|
|
|
2021-03-05 08:52:45 +00:00
|
|
|
impl *proxyservice.ProxyService
|
2021-02-23 01:58:06 +00:00
|
|
|
|
|
|
|
tracer opentracing.Tracer
|
|
|
|
closer io.Closer
|
2021-01-22 04:57:23 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 06:30:54 +00:00
|
|
|
func NewServer(ctx1 context.Context, factory msgstream.Factory) (*Server, error) {
|
2021-01-29 09:29:31 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx1)
|
2021-02-23 01:58:06 +00:00
|
|
|
var err error
|
2021-01-29 01:27:26 +00:00
|
|
|
|
|
|
|
server := &Server{
|
2021-01-29 09:08:31 +00:00
|
|
|
ctx: ctx,
|
2021-01-29 09:29:31 +00:00
|
|
|
cancel: cancel,
|
2021-01-29 01:27:26 +00:00
|
|
|
grpcErrChan: make(chan error),
|
|
|
|
}
|
|
|
|
|
2021-02-24 01:48:17 +00:00
|
|
|
// TODO
|
2021-02-23 01:58:06 +00:00
|
|
|
cfg := &config.Configuration{
|
2021-02-24 01:48:17 +00:00
|
|
|
ServiceName: "proxy_service",
|
2021-02-23 01:58:06 +00:00
|
|
|
Sampler: &config.SamplerConfig{
|
|
|
|
Type: "const",
|
|
|
|
Param: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
server.tracer, server.closer, err = cfg.NewTracer()
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("ERROR: cannot init Jaeger: %v\n", err))
|
|
|
|
}
|
|
|
|
opentracing.SetGlobalTracer(server.tracer)
|
|
|
|
|
2021-03-05 08:52:45 +00:00
|
|
|
server.impl, err = proxyservice.NewProxyService(server.ctx, factory)
|
2021-01-26 06:55:57 +00:00
|
|
|
if err != nil {
|
2021-01-29 01:27:26 +00:00
|
|
|
return nil, err
|
2021-01-26 06:55:57 +00:00
|
|
|
}
|
2021-02-23 03:40:30 +00:00
|
|
|
return server, nil
|
2021-01-26 06:55:57 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 01:27:26 +00:00
|
|
|
func (s *Server) Run() error {
|
2021-01-26 06:55:57 +00:00
|
|
|
|
2021-01-29 01:27:26 +00:00
|
|
|
if err := s.init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-10 01:56:09 +00:00
|
|
|
log.Debug("proxy service init done ...")
|
2021-01-29 01:27:26 +00:00
|
|
|
|
|
|
|
if err := s.start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2021-01-22 04:57:23 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 01:27:26 +00:00
|
|
|
func (s *Server) init() error {
|
2021-01-22 04:57:23 +00:00
|
|
|
Params.Init()
|
2021-01-26 05:41:41 +00:00
|
|
|
proxyservice.Params.Init()
|
2021-03-10 01:56:09 +00:00
|
|
|
log.Debug("init params done")
|
2021-01-29 01:27:26 +00:00
|
|
|
|
|
|
|
s.wg.Add(1)
|
2021-01-29 09:08:31 +00:00
|
|
|
go s.startGrpcLoop(Params.ServicePort)
|
2021-01-29 01:27:26 +00:00
|
|
|
// wait for grpc server loop start
|
|
|
|
if err := <-s.grpcErrChan; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-10 07:27:26 +00:00
|
|
|
s.impl.UpdateStateCode(internalpb2.StateCode_Initializing)
|
2021-03-10 01:56:09 +00:00
|
|
|
log.Debug("grpc init done ...")
|
2021-01-29 01:27:26 +00:00
|
|
|
|
|
|
|
if err := s.impl.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-22 04:57:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-29 01:27:26 +00:00
|
|
|
func (s *Server) startGrpcLoop(grpcPort int) {
|
|
|
|
|
|
|
|
defer s.wg.Done()
|
2021-01-22 04:57:23 +00:00
|
|
|
|
2021-03-10 01:56:09 +00:00
|
|
|
log.Debug("proxyservice", zap.Int("network port", grpcPort))
|
2021-01-29 01:27:26 +00:00
|
|
|
lis, err := net.Listen("tcp", ":"+strconv.Itoa(grpcPort))
|
|
|
|
if err != nil {
|
2021-03-10 01:56:09 +00:00
|
|
|
log.Warn("proxyservice", zap.String("GrpcServer:failed to listen", err.Error()))
|
2021-01-29 01:27:26 +00:00
|
|
|
s.grpcErrChan <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(s.ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
2021-02-26 09:44:24 +00:00
|
|
|
tracer := opentracing.GlobalTracer()
|
2021-03-08 07:49:42 +00:00
|
|
|
s.grpcServer = grpc.NewServer(
|
|
|
|
grpc.MaxRecvMsgSize(math.MaxInt32),
|
|
|
|
grpc.MaxSendMsgSize(math.MaxInt32),
|
|
|
|
grpc.UnaryInterceptor(
|
|
|
|
otgrpc.OpenTracingServerInterceptor(tracer)),
|
2021-02-26 09:44:24 +00:00
|
|
|
grpc.StreamInterceptor(
|
|
|
|
otgrpc.OpenTracingStreamServerInterceptor(tracer)))
|
2021-01-29 01:27:26 +00:00
|
|
|
proxypb.RegisterProxyServiceServer(s.grpcServer, s)
|
|
|
|
milvuspb.RegisterProxyServiceServer(s.grpcServer, s)
|
|
|
|
|
|
|
|
go funcutil.CheckGrpcReady(ctx, s.grpcErrChan)
|
|
|
|
if err := s.grpcServer.Serve(lis); err != nil {
|
|
|
|
s.grpcErrChan <- err
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) start() error {
|
2021-03-10 01:56:09 +00:00
|
|
|
log.Debug("proxy ProxyService start ...")
|
2021-01-29 01:27:26 +00:00
|
|
|
if err := s.impl.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-22 04:57:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Stop() error {
|
2021-02-24 01:48:17 +00:00
|
|
|
if err := s.closer.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-23 03:40:30 +00:00
|
|
|
s.cancel()
|
2021-02-23 01:58:06 +00:00
|
|
|
s.closer.Close()
|
2021-01-29 09:29:31 +00:00
|
|
|
err := s.impl.Stop()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-22 04:57:23 +00:00
|
|
|
if s.grpcServer != nil {
|
|
|
|
s.grpcServer.GracefulStop()
|
|
|
|
}
|
|
|
|
s.wg.Wait()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) RegisterLink(ctx context.Context, empty *commonpb.Empty) (*milvuspb.RegisterLinkResponse, error) {
|
2021-03-04 02:35:28 +00:00
|
|
|
return s.impl.RegisterLink(ctx)
|
2021-01-22 04:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) RegisterNode(ctx context.Context, request *proxypb.RegisterNodeRequest) (*proxypb.RegisterNodeResponse, error) {
|
2021-03-04 02:35:28 +00:00
|
|
|
return s.impl.RegisterNode(ctx, request)
|
2021-01-22 04:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) InvalidateCollectionMetaCache(ctx context.Context, request *proxypb.InvalidateCollMetaCacheRequest) (*commonpb.Status, error) {
|
2021-03-04 02:35:28 +00:00
|
|
|
return s.impl.InvalidateCollectionMetaCache(ctx, request)
|
2021-01-22 04:57:23 +00:00
|
|
|
}
|
2021-01-29 01:27:26 +00:00
|
|
|
|
|
|
|
func (s *Server) GetTimeTickChannel(ctx context.Context, empty *commonpb.Empty) (*milvuspb.StringResponse, error) {
|
2021-03-04 02:35:28 +00:00
|
|
|
return s.impl.GetTimeTickChannel(ctx)
|
2021-01-29 01:27:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) GetComponentStates(ctx context.Context, empty *commonpb.Empty) (*internalpb2.ComponentStates, error) {
|
2021-03-04 02:35:28 +00:00
|
|
|
return s.impl.GetComponentStates(ctx)
|
2021-01-29 01:27:26 +00:00
|
|
|
}
|