2021-01-22 12:57:23 +08:00
|
|
|
package grpcproxyservice
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-02-23 09:58:06 +08:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2021-01-29 09:27:26 +08:00
|
|
|
"log"
|
2021-01-22 12:57:23 +08:00
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
|
2021-02-23 09:58:06 +08:00
|
|
|
"github.com/opentracing/opentracing-go"
|
|
|
|
"github.com/uber/jaeger-client-go/config"
|
2021-02-08 14:30:54 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/msgstream"
|
2021-01-29 09:27:26 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/commonpb"
|
2021-01-26 14:55:57 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/internalpb2"
|
2021-01-22 12:57:23 +08: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 09:27:26 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/util/funcutil"
|
2021-01-22 12:57:23 +08:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
2021-01-29 17:29:31 +08:00
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
wg sync.WaitGroup
|
2021-01-29 09:27:26 +08:00
|
|
|
|
|
|
|
grpcServer *grpc.Server
|
|
|
|
grpcErrChan chan error
|
|
|
|
|
|
|
|
impl *proxyservice.ServiceImpl
|
2021-02-23 09:58:06 +08:00
|
|
|
|
|
|
|
tracer opentracing.Tracer
|
|
|
|
closer io.Closer
|
2021-01-22 12:57:23 +08:00
|
|
|
}
|
|
|
|
|
2021-02-08 14:30:54 +08:00
|
|
|
func NewServer(ctx1 context.Context, factory msgstream.Factory) (*Server, error) {
|
2021-01-29 17:29:31 +08:00
|
|
|
ctx, cancel := context.WithCancel(ctx1)
|
2021-02-23 09:58:06 +08:00
|
|
|
var err error
|
2021-01-29 09:27:26 +08:00
|
|
|
|
|
|
|
server := &Server{
|
2021-01-29 17:08:31 +08:00
|
|
|
ctx: ctx,
|
2021-01-29 17:29:31 +08:00
|
|
|
cancel: cancel,
|
2021-01-29 09:27:26 +08:00
|
|
|
grpcErrChan: make(chan error),
|
|
|
|
}
|
|
|
|
|
2021-02-23 09:58:06 +08:00
|
|
|
cfg := &config.Configuration{
|
|
|
|
ServiceName: "proxyservice",
|
|
|
|
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-02-08 14:30:54 +08:00
|
|
|
server.impl, err = proxyservice.NewServiceImpl(server.ctx, factory)
|
2021-01-26 14:55:57 +08:00
|
|
|
if err != nil {
|
2021-01-29 09:27:26 +08:00
|
|
|
return nil, err
|
2021-01-26 14:55:57 +08:00
|
|
|
}
|
2021-02-23 11:40:30 +08:00
|
|
|
return server, nil
|
2021-01-26 14:55:57 +08:00
|
|
|
}
|
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
func (s *Server) Run() error {
|
2021-01-26 14:55:57 +08:00
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
if err := s.init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-29 17:29:31 +08:00
|
|
|
log.Println("proxy service init done ...")
|
2021-01-29 09:27:26 +08:00
|
|
|
|
|
|
|
if err := s.start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2021-01-22 12:57:23 +08:00
|
|
|
}
|
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
func (s *Server) init() error {
|
2021-01-22 12:57:23 +08:00
|
|
|
Params.Init()
|
2021-01-26 13:41:41 +08:00
|
|
|
proxyservice.Params.Init()
|
2021-01-29 17:29:31 +08:00
|
|
|
log.Println("init params done")
|
2021-01-29 09:27:26 +08:00
|
|
|
|
|
|
|
s.wg.Add(1)
|
2021-01-29 17:08:31 +08:00
|
|
|
go s.startGrpcLoop(Params.ServicePort)
|
2021-01-29 09:27:26 +08:00
|
|
|
// wait for grpc server loop start
|
|
|
|
if err := <-s.grpcErrChan; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.impl.UpdateStateCode(internalpb2.StateCode_INITIALIZING)
|
2021-01-29 17:29:31 +08:00
|
|
|
log.Println("grpc init done ...")
|
2021-01-29 09:27:26 +08:00
|
|
|
|
|
|
|
if err := s.impl.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-22 12:57:23 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
func (s *Server) startGrpcLoop(grpcPort int) {
|
|
|
|
|
|
|
|
defer s.wg.Done()
|
2021-01-22 12:57:23 +08:00
|
|
|
|
2021-01-29 17:29:31 +08:00
|
|
|
log.Println("network port: ", grpcPort)
|
2021-01-29 09:27:26 +08:00
|
|
|
lis, err := net.Listen("tcp", ":"+strconv.Itoa(grpcPort))
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("GrpcServer:failed to listen: %v", err)
|
|
|
|
s.grpcErrChan <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(s.ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
s.grpcServer = grpc.NewServer()
|
|
|
|
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-01-29 17:29:31 +08:00
|
|
|
log.Println("proxy ServiceImpl start ...")
|
2021-01-29 09:27:26 +08:00
|
|
|
if err := s.impl.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-22 12:57:23 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Stop() error {
|
2021-02-23 11:40:30 +08:00
|
|
|
s.cancel()
|
2021-02-23 09:58:06 +08:00
|
|
|
s.closer.Close()
|
2021-01-29 17:29:31 +08:00
|
|
|
err := s.impl.Stop()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-22 12:57:23 +08: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) {
|
|
|
|
return s.impl.RegisterLink()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) RegisterNode(ctx context.Context, request *proxypb.RegisterNodeRequest) (*proxypb.RegisterNodeResponse, error) {
|
|
|
|
return s.impl.RegisterNode(request)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) InvalidateCollectionMetaCache(ctx context.Context, request *proxypb.InvalidateCollMetaCacheRequest) (*commonpb.Status, error) {
|
2021-02-04 19:34:35 +08:00
|
|
|
return s.impl.InvalidateCollectionMetaCache(request)
|
2021-01-22 12:57:23 +08:00
|
|
|
}
|
2021-01-29 09:27:26 +08:00
|
|
|
|
|
|
|
func (s *Server) GetTimeTickChannel(ctx context.Context, empty *commonpb.Empty) (*milvuspb.StringResponse, error) {
|
2021-02-04 19:34:35 +08:00
|
|
|
return s.impl.GetTimeTickChannel()
|
2021-01-29 09:27:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) GetComponentStates(ctx context.Context, empty *commonpb.Empty) (*internalpb2.ComponentStates, error) {
|
|
|
|
return s.impl.GetComponentStates()
|
|
|
|
}
|