2021-01-22 04:57:23 +00:00
|
|
|
package grpcproxyservice
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-01-29 01:27:26 +00:00
|
|
|
"log"
|
2021-01-22 04:57:23 +00:00
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
|
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 01:27:26 +00:00
|
|
|
ctx context.Context
|
|
|
|
wg sync.WaitGroup
|
|
|
|
|
|
|
|
grpcServer *grpc.Server
|
|
|
|
grpcErrChan chan error
|
|
|
|
|
|
|
|
impl *proxyservice.ServiceImpl
|
2021-01-22 04:57:23 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 09:08:31 +00:00
|
|
|
func NewServer(ctx context.Context) (*Server, error) {
|
2021-01-29 01:27:26 +00:00
|
|
|
|
|
|
|
server := &Server{
|
2021-01-29 09:08:31 +00:00
|
|
|
ctx: ctx,
|
2021-01-29 01:27:26 +00:00
|
|
|
grpcErrChan: make(chan error),
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
server.impl, err = proxyservice.NewServiceImpl(server.ctx)
|
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-01-29 01:27:26 +00:00
|
|
|
return server, err
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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
|
|
|
|
}
|
|
|
|
s.impl.UpdateStateCode(internalpb2.StateCode_INITIALIZING)
|
|
|
|
|
|
|
|
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-01-29 01:27:26 +00:00
|
|
|
fmt.Println("network port: ", grpcPort)
|
|
|
|
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 {
|
|
|
|
fmt.Println("proxy ServiceImpl start ...")
|
|
|
|
if err := s.impl.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-22 04:57:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Stop() error {
|
|
|
|
s.impl.Stop()
|
|
|
|
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) {
|
|
|
|
return &commonpb.Status{}, s.impl.InvalidateCollectionMetaCache(request)
|
|
|
|
}
|
2021-01-29 01:27:26 +00:00
|
|
|
|
|
|
|
func (s *Server) GetTimeTickChannel(ctx context.Context, empty *commonpb.Empty) (*milvuspb.StringResponse, error) {
|
|
|
|
channel, err := s.impl.GetTimeTickChannel()
|
|
|
|
if err != nil {
|
|
|
|
return &milvuspb.StringResponse{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
|
|
|
|
Reason: err.Error(),
|
|
|
|
},
|
|
|
|
Value: "",
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
return &milvuspb.StringResponse{
|
|
|
|
Value: channel,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) GetComponentStates(ctx context.Context, empty *commonpb.Empty) (*internalpb2.ComponentStates, error) {
|
|
|
|
return s.impl.GetComponentStates()
|
|
|
|
}
|