2021-01-15 06:38:36 +00:00
|
|
|
package grpcindexnode
|
|
|
|
|
|
|
|
import (
|
2021-01-19 10:32:57 +00:00
|
|
|
"context"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
|
2021-01-29 09:08:31 +00:00
|
|
|
grpcindexserviceclient "github.com/zilliztech/milvus-distributed/internal/distributed/indexservice/client"
|
2021-01-15 06:38:36 +00:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/indexnode"
|
2021-01-19 10:32:57 +00:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/commonpb"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/indexpb"
|
2021-01-26 11:24:09 +00:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/internalpb2"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/milvuspb"
|
2021-01-29 09:08:31 +00:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/util/funcutil"
|
2021-01-15 06:38:36 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
2021-01-29 09:08:31 +00:00
|
|
|
impl *indexnode.NodeImpl
|
2021-01-15 06:38:36 +00:00
|
|
|
|
2021-01-29 09:08:31 +00:00
|
|
|
grpcServer *grpc.Server
|
|
|
|
grpcErrChan chan error
|
2021-01-19 10:32:57 +00:00
|
|
|
|
2021-01-29 09:08:31 +00:00
|
|
|
indexServiceClient *grpcindexserviceclient.Client
|
|
|
|
loopCtx context.Context
|
|
|
|
loopCancel func()
|
|
|
|
loopWg sync.WaitGroup
|
2021-01-26 11:24:09 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 09:08:31 +00:00
|
|
|
func (s *Server) Run() error {
|
2021-01-26 11:24:09 +00:00
|
|
|
|
2021-01-29 09:08:31 +00:00
|
|
|
if err := s.init(); err != nil {
|
|
|
|
return nil
|
2021-01-26 11:24:09 +00:00
|
|
|
}
|
2021-01-20 10:26:20 +00:00
|
|
|
|
2021-01-29 09:08:31 +00:00
|
|
|
if err := s.start(); err != nil {
|
2021-01-19 10:32:57 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-01-29 09:08:31 +00:00
|
|
|
return nil
|
2021-01-15 06:38:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 09:08:31 +00:00
|
|
|
func (s *Server) startGrpcLoop(grpcPort int) {
|
|
|
|
|
2021-01-20 10:26:20 +00:00
|
|
|
defer s.loopWg.Done()
|
2021-01-19 10:32:57 +00:00
|
|
|
|
2021-01-29 09:08:31 +00:00
|
|
|
log.Println("network port: ", grpcPort)
|
|
|
|
lis, err := net.Listen("tcp", ":"+strconv.Itoa(grpcPort))
|
2021-01-19 10:32:57 +00:00
|
|
|
if err != nil {
|
2021-01-29 09:08:31 +00:00
|
|
|
log.Printf("GrpcServer:failed to listen: %v", err)
|
|
|
|
s.grpcErrChan <- err
|
|
|
|
return
|
2021-01-15 06:38:36 +00:00
|
|
|
}
|
2021-01-19 10:32:57 +00:00
|
|
|
|
2021-01-29 09:08:31 +00:00
|
|
|
ctx, cancel := context.WithCancel(s.loopCtx)
|
|
|
|
defer cancel()
|
|
|
|
|
2021-01-19 10:32:57 +00:00
|
|
|
s.grpcServer = grpc.NewServer()
|
|
|
|
indexpb.RegisterIndexNodeServer(s.grpcServer, s)
|
2021-01-29 09:08:31 +00:00
|
|
|
go funcutil.CheckGrpcReady(ctx, s.grpcErrChan)
|
|
|
|
if err := s.grpcServer.Serve(lis); err != nil {
|
|
|
|
s.grpcErrChan <- err
|
2021-01-19 10:32:57 +00:00
|
|
|
}
|
2021-01-29 09:08:31 +00:00
|
|
|
|
2021-01-19 10:32:57 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 09:08:31 +00:00
|
|
|
func (s *Server) init() error {
|
|
|
|
var err error
|
|
|
|
Params.Init()
|
|
|
|
Params.LoadFromEnv()
|
|
|
|
Params.LoadFromArgs()
|
|
|
|
Params.Port = funcutil.GetAvailablePort()
|
|
|
|
Params.Address = Params.IP + ":" + strconv.FormatInt(int64(Params.Port), 10)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
err = s.Stop()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Init failed, and Stop failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2021-01-19 10:32:57 +00:00
|
|
|
|
2021-01-29 09:08:31 +00:00
|
|
|
s.loopWg.Add(1)
|
|
|
|
go s.startGrpcLoop(Params.Port)
|
|
|
|
// wait for grpc server loop start
|
|
|
|
err = <-s.grpcErrChan
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-20 10:26:20 +00:00
|
|
|
|
2021-01-29 09:08:31 +00:00
|
|
|
indexServiceAddr := Params.IndexServerAddress
|
|
|
|
s.indexServiceClient = grpcindexserviceclient.NewClient(indexServiceAddr)
|
|
|
|
err = s.indexServiceClient.Init()
|
2021-01-19 10:32:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-29 09:08:31 +00:00
|
|
|
s.impl.SetIndexServiceClient(s.indexServiceClient)
|
2021-01-19 10:32:57 +00:00
|
|
|
|
2021-01-20 10:26:20 +00:00
|
|
|
indexnode.Params.Init()
|
2021-01-29 09:08:31 +00:00
|
|
|
indexnode.Params.Port = Params.Port
|
|
|
|
indexnode.Params.IP = Params.IP
|
|
|
|
indexnode.Params.Address = Params.Address
|
2021-01-19 10:32:57 +00:00
|
|
|
|
2021-01-29 09:08:31 +00:00
|
|
|
s.impl.UpdateStateCode(internalpb2.StateCode_INITIALIZING)
|
2021-01-19 10:32:57 +00:00
|
|
|
|
2021-01-29 09:08:31 +00:00
|
|
|
err = s.impl.Init()
|
2021-01-20 10:26:20 +00:00
|
|
|
if err != nil {
|
2021-01-29 09:08:31 +00:00
|
|
|
return err
|
2021-01-20 08:46:58 +00:00
|
|
|
}
|
2021-01-29 09:08:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-01-20 08:46:58 +00:00
|
|
|
|
2021-01-29 09:08:31 +00:00
|
|
|
func (s *Server) start() error {
|
|
|
|
err := s.impl.Start()
|
2021-01-20 10:26:20 +00:00
|
|
|
if err != nil {
|
2021-01-26 01:38:40 +00:00
|
|
|
return err
|
2021-01-20 10:26:20 +00:00
|
|
|
}
|
2021-01-26 01:38:40 +00:00
|
|
|
return nil
|
2021-01-19 10:32:57 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 01:38:40 +00:00
|
|
|
func (s *Server) Stop() error {
|
|
|
|
s.loopCancel()
|
2021-01-29 09:08:31 +00:00
|
|
|
if s.impl != nil {
|
|
|
|
s.impl.Stop()
|
|
|
|
}
|
2021-01-26 01:38:40 +00:00
|
|
|
if s.grpcServer != nil {
|
|
|
|
s.grpcServer.GracefulStop()
|
|
|
|
}
|
2021-01-20 10:26:20 +00:00
|
|
|
s.loopWg.Wait()
|
2021-01-19 10:32:57 +00:00
|
|
|
|
2021-01-26 01:38:40 +00:00
|
|
|
return nil
|
2021-01-19 10:32:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) BuildIndex(ctx context.Context, req *indexpb.BuildIndexCmd) (*commonpb.Status, error) {
|
2021-01-29 09:08:31 +00:00
|
|
|
return s.impl.BuildIndex(req)
|
2021-01-15 06:38:36 +00:00
|
|
|
}
|
2021-01-26 06:31:31 +00:00
|
|
|
|
2021-01-29 09:08:31 +00:00
|
|
|
func (s *Server) GetComponentStates(ctx context.Context, empty *commonpb.Empty) (*internalpb2.ComponentStates, error) {
|
|
|
|
return s.impl.GetComponentStates()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) GetTimeTickChannel(ctx context.Context, empty *commonpb.Empty) (*milvuspb.StringResponse, error) {
|
2021-02-04 11:34:35 +00:00
|
|
|
return s.impl.GetTimeTickChannel()
|
2021-01-29 09:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) GetStatisticsChannel(ctx context.Context, empty *commonpb.Empty) (*milvuspb.StringResponse, error) {
|
2021-02-04 11:34:35 +00:00
|
|
|
return s.impl.GetStatisticsChannel()
|
2021-01-29 09:08:31 +00:00
|
|
|
}
|
2021-01-26 06:31:31 +00:00
|
|
|
|
2021-01-29 09:08:31 +00:00
|
|
|
func NewServer(ctx context.Context) (*Server, error) {
|
|
|
|
ctx1, cancel := context.WithCancel(ctx)
|
|
|
|
node, err := indexnode.NewNodeImpl(ctx1)
|
2021-01-26 06:31:31 +00:00
|
|
|
if err != nil {
|
|
|
|
defer cancel()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Server{
|
2021-01-29 09:08:31 +00:00
|
|
|
loopCtx: ctx1,
|
|
|
|
loopCancel: cancel,
|
|
|
|
impl: node,
|
|
|
|
grpcErrChan: make(chan error),
|
2021-01-26 06:31:31 +00:00
|
|
|
}, nil
|
|
|
|
}
|