2021-01-22 09:36:18 +08:00
|
|
|
package grpcproxynode
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-01-29 09:27:26 +08:00
|
|
|
"log"
|
2021-01-22 09:36:18 +08:00
|
|
|
"net"
|
2021-01-22 12:57:23 +08:00
|
|
|
"os"
|
2021-01-22 09:36:18 +08:00
|
|
|
"strconv"
|
|
|
|
"sync"
|
2021-01-28 20:51:44 +08:00
|
|
|
"time"
|
|
|
|
|
2021-01-29 17:29:31 +08:00
|
|
|
grpcproxyserviceclient "github.com/zilliztech/milvus-distributed/internal/distributed/proxyservice/client"
|
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/util/funcutil"
|
2021-01-28 20:51:44 +08:00
|
|
|
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/internalpb2"
|
2021-01-22 09:36:18 +08:00
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
"google.golang.org/grpc"
|
2021-01-22 12:57:23 +08:00
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
grpcdataservice "github.com/zilliztech/milvus-distributed/internal/distributed/dataservice"
|
|
|
|
grpcindexserviceclient "github.com/zilliztech/milvus-distributed/internal/distributed/indexservice/client"
|
|
|
|
grcpmasterservice "github.com/zilliztech/milvus-distributed/internal/distributed/masterservice"
|
|
|
|
grpcqueryserviceclient "github.com/zilliztech/milvus-distributed/internal/distributed/queryservice/client"
|
2021-01-22 09:36:18 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/commonpb"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/milvuspb"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/proxypb"
|
2021-01-29 09:27:26 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proxynode"
|
2021-01-22 09:36:18 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
2021-01-29 09:27:26 +08:00
|
|
|
ctx context.Context
|
|
|
|
wg sync.WaitGroup
|
|
|
|
impl *proxynode.NodeImpl
|
|
|
|
grpcServer *grpc.Server
|
2021-01-22 09:36:18 +08:00
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
grpcErrChan chan error
|
2021-01-28 20:51:44 +08:00
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
ip string
|
|
|
|
port int
|
2021-01-28 20:51:44 +08:00
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
//todo
|
2021-01-29 17:29:31 +08:00
|
|
|
proxyServiceClient *grpcproxyserviceclient.Client
|
2021-01-28 20:51:44 +08:00
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
// todo InitParams Service addrs
|
|
|
|
masterServiceClient *grcpmasterservice.GrpcClient
|
|
|
|
dataServiceClient *grpcdataservice.Client
|
|
|
|
queryServiceClient *grpcqueryserviceclient.Client
|
|
|
|
indexServiceClient *grpcindexserviceclient.Client
|
|
|
|
}
|
2021-01-28 20:51:44 +08:00
|
|
|
|
2021-01-29 17:08:31 +08:00
|
|
|
func NewServer(ctx context.Context) (*Server, error) {
|
2021-01-28 20:51:44 +08:00
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
server := &Server{
|
2021-01-29 17:08:31 +08:00
|
|
|
ctx: ctx,
|
2021-01-29 09:27:26 +08:00
|
|
|
grpcErrChan: make(chan error),
|
2021-01-28 20:51:44 +08:00
|
|
|
}
|
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
var err error
|
|
|
|
server.impl, err = proxynode.NewProxyNodeImpl(server.ctx)
|
2021-01-28 20:51:44 +08:00
|
|
|
if err != nil {
|
2021-01-29 09:27:26 +08:00
|
|
|
return nil, err
|
2021-01-28 20:51:44 +08:00
|
|
|
}
|
2021-01-29 09:27:26 +08:00
|
|
|
return server, err
|
|
|
|
}
|
2021-01-28 20:51:44 +08:00
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
func (s *Server) startGrpcLoop(grpcPort int) {
|
2021-01-28 20:51:44 +08:00
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
defer s.wg.Done()
|
2021-01-28 20:51:44 +08:00
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
log.Println("network port: ", grpcPort)
|
|
|
|
lis, err := net.Listen("tcp", ":"+strconv.Itoa(grpcPort))
|
2021-01-28 20:51:44 +08:00
|
|
|
if err != nil {
|
2021-01-29 09:27:26 +08:00
|
|
|
log.Printf("GrpcServer:failed to listen: %v", err)
|
|
|
|
s.grpcErrChan <- err
|
|
|
|
return
|
2021-01-28 20:51:44 +08:00
|
|
|
}
|
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
ctx, cancel := context.WithCancel(s.ctx)
|
|
|
|
defer cancel()
|
2021-01-28 20:51:44 +08:00
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
s.grpcServer = grpc.NewServer()
|
|
|
|
proxypb.RegisterProxyNodeServiceServer(s.grpcServer, s)
|
|
|
|
milvuspb.RegisterMilvusServiceServer(s.grpcServer, s)
|
2021-01-28 20:51:44 +08:00
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
go funcutil.CheckGrpcReady(ctx, s.grpcErrChan)
|
|
|
|
if err := s.grpcServer.Serve(lis); err != nil {
|
|
|
|
s.grpcErrChan <- err
|
2021-01-28 20:51:44 +08:00
|
|
|
}
|
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
}
|
2021-01-28 20:51:44 +08:00
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
func (s *Server) Run() error {
|
2021-01-28 20:51:44 +08:00
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
if err := s.init(); err != nil {
|
2021-01-29 17:29:31 +08:00
|
|
|
return err
|
2021-01-28 20:51:44 +08:00
|
|
|
}
|
2021-01-29 17:29:31 +08:00
|
|
|
log.Println("proxy node init done ...")
|
2021-01-28 20:51:44 +08:00
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
if err := s.start(); err != nil {
|
2021-01-28 20:51:44 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-01-29 17:29:31 +08:00
|
|
|
log.Println("proxy node start done ...")
|
2021-01-28 20:51:44 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
func (s *Server) init() error {
|
|
|
|
var err error
|
2021-01-28 20:51:44 +08:00
|
|
|
Params.Init()
|
2021-01-22 12:57:23 +08:00
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
Params.IP = funcutil.GetLocalIP()
|
|
|
|
host := os.Getenv("PROXY_NODE_HOST")
|
|
|
|
if len(host) > 0 {
|
|
|
|
Params.IP = host
|
|
|
|
}
|
|
|
|
|
|
|
|
Params.Port = funcutil.GetAvailablePort()
|
|
|
|
Params.Address = Params.IP + ":" + strconv.FormatInt(int64(Params.Port), 10)
|
2021-01-22 12:57:23 +08:00
|
|
|
|
2021-01-29 17:29:31 +08:00
|
|
|
log.Println("proxy host: ", Params.IP)
|
|
|
|
log.Println("proxy port: ", Params.Port)
|
|
|
|
log.Println("proxy address: ", Params.Address)
|
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
defer func() {
|
2021-01-22 12:57:23 +08:00
|
|
|
if err != nil {
|
2021-01-29 09:27:26 +08:00
|
|
|
err2 := s.Stop()
|
|
|
|
if err2 != nil {
|
|
|
|
log.Println("Init failed, and Stop failed")
|
|
|
|
}
|
2021-01-22 12:57:23 +08:00
|
|
|
}
|
2021-01-29 09:27:26 +08:00
|
|
|
}()
|
2021-01-22 12:57:23 +08:00
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
s.wg.Add(1)
|
2021-01-29 17:08:31 +08:00
|
|
|
go s.startGrpcLoop(Params.Port)
|
2021-01-29 09:27:26 +08:00
|
|
|
// wait for grpc server loop start
|
|
|
|
err = <-s.grpcErrChan
|
2021-01-29 17:29:31 +08:00
|
|
|
log.Println("create grpc server ...")
|
2021-01-29 09:27:26 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-01-22 12:57:23 +08:00
|
|
|
}
|
2021-01-29 09:27:26 +08:00
|
|
|
|
2021-01-29 17:29:31 +08:00
|
|
|
s.proxyServiceClient = grpcproxyserviceclient.NewClient(Params.ProxyServiceAddress)
|
2021-01-29 09:27:26 +08:00
|
|
|
err = s.proxyServiceClient.Init()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-01-22 12:57:23 +08:00
|
|
|
}
|
2021-01-29 09:27:26 +08:00
|
|
|
s.impl.SetProxyServiceClient(s.proxyServiceClient)
|
2021-01-29 17:29:31 +08:00
|
|
|
log.Println("set proxy service client ...")
|
2021-01-29 09:27:26 +08:00
|
|
|
|
|
|
|
masterServiceAddr := Params.MasterAddress
|
2021-01-29 17:29:31 +08:00
|
|
|
log.Println("master address: ", masterServiceAddr)
|
2021-01-29 09:27:26 +08:00
|
|
|
timeout := 3 * time.Second
|
|
|
|
s.masterServiceClient, err = grcpmasterservice.NewGrpcClient(masterServiceAddr, timeout)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-01-22 12:57:23 +08:00
|
|
|
}
|
2021-01-29 09:27:26 +08:00
|
|
|
err = s.masterServiceClient.Init()
|
2021-01-22 12:57:23 +08:00
|
|
|
if err != nil {
|
2021-01-29 09:27:26 +08:00
|
|
|
return err
|
2021-01-22 12:57:23 +08:00
|
|
|
}
|
2021-01-29 09:27:26 +08:00
|
|
|
s.impl.SetMasterClient(s.masterServiceClient)
|
2021-01-29 17:29:31 +08:00
|
|
|
log.Println("set master client ...")
|
2021-01-22 12:57:23 +08:00
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
dataServiceAddr := Params.DataServiceAddress
|
2021-01-29 17:29:31 +08:00
|
|
|
log.Println("data service address ...")
|
2021-01-29 09:27:26 +08:00
|
|
|
s.dataServiceClient = grpcdataservice.NewClient(dataServiceAddr)
|
|
|
|
err = s.dataServiceClient.Init()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.impl.SetDataServiceClient(s.dataServiceClient)
|
2021-01-29 17:29:31 +08:00
|
|
|
log.Println("set data service address ...")
|
2021-01-29 09:27:26 +08:00
|
|
|
|
|
|
|
indexServiceAddr := Params.IndexServerAddress
|
2021-01-29 17:29:31 +08:00
|
|
|
log.Println("index server address: ", indexServiceAddr)
|
2021-01-29 09:27:26 +08:00
|
|
|
s.indexServiceClient = grpcindexserviceclient.NewClient(indexServiceAddr)
|
|
|
|
err = s.indexServiceClient.Init()
|
2021-01-22 09:36:18 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-29 09:27:26 +08:00
|
|
|
s.impl.SetIndexServiceClient(s.indexServiceClient)
|
2021-01-29 17:29:31 +08:00
|
|
|
log.Println("set index service client ...")
|
|
|
|
|
|
|
|
// queryServiceAddr := Params.QueryServiceAddress
|
|
|
|
// log.Println("query service address: ", queryServiceAddr)
|
|
|
|
// s.queryServiceClient = grpcqueryserviceclient.NewClient(queryServiceAddr)
|
|
|
|
// err = s.queryServiceClient.Init()
|
|
|
|
// if err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
// s.impl.SetQueryServiceClient(s.queryServiceClient)
|
|
|
|
// log.Println("set query service client ...")
|
2021-01-22 09:36:18 +08:00
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
proxynode.Params.Init()
|
2021-01-29 17:29:31 +08:00
|
|
|
log.Println("init params done ...")
|
2021-01-29 09:27:26 +08:00
|
|
|
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
|
2021-01-22 09:36:18 +08:00
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
s.impl.UpdateStateCode(internalpb2.StateCode_INITIALIZING)
|
2021-01-22 09:36:18 +08:00
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
if err := s.impl.Init(); err != nil {
|
2021-01-29 17:29:31 +08:00
|
|
|
log.Println("impl init error: ", err)
|
2021-01-29 09:27:26 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-01-22 09:36:18 +08:00
|
|
|
|
2021-01-29 09:27:26 +08:00
|
|
|
func (s *Server) start() error {
|
2021-01-22 09:36:18 +08:00
|
|
|
return s.impl.Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Stop() error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if s.grpcServer != nil {
|
|
|
|
s.grpcServer.GracefulStop()
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.impl.Stop()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.wg.Wait()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) InvalidateCollectionMetaCache(ctx context.Context, request *proxypb.InvalidateCollMetaCacheRequest) (*commonpb.Status, error) {
|
|
|
|
return s.impl.InvalidateCollectionMetaCache(ctx, request)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) CreateCollection(ctx context.Context, request *milvuspb.CreateCollectionRequest) (*commonpb.Status, error) {
|
2021-01-29 09:27:26 +08:00
|
|
|
return s.impl.CreateCollection(request)
|
2021-01-22 09:36:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) DropCollection(ctx context.Context, request *milvuspb.DropCollectionRequest) (*commonpb.Status, error) {
|
2021-01-29 09:27:26 +08:00
|
|
|
return s.impl.DropCollection(request)
|
2021-01-22 09:36:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) HasCollection(ctx context.Context, request *milvuspb.HasCollectionRequest) (*milvuspb.BoolResponse, error) {
|
2021-01-29 09:27:26 +08:00
|
|
|
return s.impl.HasCollection(request)
|
2021-01-22 09:36:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) LoadCollection(ctx context.Context, request *milvuspb.LoadCollectionRequest) (*commonpb.Status, error) {
|
2021-01-29 09:27:26 +08:00
|
|
|
return s.impl.LoadCollection(request)
|
2021-01-22 09:36:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) ReleaseCollection(ctx context.Context, request *milvuspb.ReleaseCollectionRequest) (*commonpb.Status, error) {
|
2021-01-29 09:27:26 +08:00
|
|
|
return s.impl.ReleaseCollection(request)
|
2021-01-22 09:36:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) DescribeCollection(ctx context.Context, request *milvuspb.DescribeCollectionRequest) (*milvuspb.DescribeCollectionResponse, error) {
|
2021-01-29 09:27:26 +08:00
|
|
|
return s.impl.DescribeCollection(request)
|
2021-01-22 09:36:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) GetCollectionStatistics(ctx context.Context, request *milvuspb.CollectionStatsRequest) (*milvuspb.CollectionStatsResponse, error) {
|
2021-01-29 09:27:26 +08:00
|
|
|
return s.impl.GetCollectionStatistics(request)
|
2021-01-22 09:36:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) ShowCollections(ctx context.Context, request *milvuspb.ShowCollectionRequest) (*milvuspb.ShowCollectionResponse, error) {
|
2021-01-29 09:27:26 +08:00
|
|
|
return s.impl.ShowCollections(request)
|
2021-01-22 09:36:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) CreatePartition(ctx context.Context, request *milvuspb.CreatePartitionRequest) (*commonpb.Status, error) {
|
2021-01-29 09:27:26 +08:00
|
|
|
return s.impl.CreatePartition(request)
|
2021-01-22 09:36:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) DropPartition(ctx context.Context, request *milvuspb.DropPartitionRequest) (*commonpb.Status, error) {
|
2021-01-29 09:27:26 +08:00
|
|
|
return s.impl.DropPartition(request)
|
2021-01-22 09:36:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) HasPartition(ctx context.Context, request *milvuspb.HasPartitionRequest) (*milvuspb.BoolResponse, error) {
|
2021-01-29 09:27:26 +08:00
|
|
|
return s.impl.HasPartition(request)
|
2021-01-22 09:36:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) LoadPartitions(ctx context.Context, request *milvuspb.LoadPartitonRequest) (*commonpb.Status, error) {
|
2021-01-29 09:27:26 +08:00
|
|
|
return s.impl.LoadPartitions(request)
|
2021-01-22 09:36:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) ReleasePartitions(ctx context.Context, request *milvuspb.ReleasePartitionRequest) (*commonpb.Status, error) {
|
2021-01-29 09:27:26 +08:00
|
|
|
return s.impl.ReleasePartitions(request)
|
2021-01-22 09:36:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) GetPartitionStatistics(ctx context.Context, request *milvuspb.PartitionStatsRequest) (*milvuspb.PartitionStatsResponse, error) {
|
2021-01-29 09:27:26 +08:00
|
|
|
return s.impl.GetPartitionStatistics(request)
|
2021-01-22 09:36:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) ShowPartitions(ctx context.Context, request *milvuspb.ShowPartitionRequest) (*milvuspb.ShowPartitionResponse, error) {
|
2021-01-29 09:27:26 +08:00
|
|
|
return s.impl.ShowPartitions(request)
|
2021-01-22 09:36:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) CreateIndex(ctx context.Context, request *milvuspb.CreateIndexRequest) (*commonpb.Status, error) {
|
2021-01-29 09:27:26 +08:00
|
|
|
return s.impl.CreateIndex(request)
|
2021-01-22 09:36:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) DescribeIndex(ctx context.Context, request *milvuspb.DescribeIndexRequest) (*milvuspb.DescribeIndexResponse, error) {
|
2021-01-29 09:27:26 +08:00
|
|
|
return s.impl.DescribeIndex(request)
|
2021-01-22 09:36:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) GetIndexState(ctx context.Context, request *milvuspb.IndexStateRequest) (*milvuspb.IndexStateResponse, error) {
|
2021-01-29 09:27:26 +08:00
|
|
|
return s.impl.GetIndexState(request)
|
2021-01-22 09:36:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Insert(ctx context.Context, request *milvuspb.InsertRequest) (*milvuspb.InsertResponse, error) {
|
2021-01-29 09:27:26 +08:00
|
|
|
return s.impl.Insert(request)
|
2021-01-22 09:36:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Search(ctx context.Context, request *milvuspb.SearchRequest) (*milvuspb.SearchResults, error) {
|
2021-01-29 09:27:26 +08:00
|
|
|
return s.impl.Search(request)
|
2021-01-22 09:36:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Flush(ctx context.Context, request *milvuspb.FlushRequest) (*commonpb.Status, error) {
|
2021-01-29 09:27:26 +08:00
|
|
|
return s.impl.Flush(request)
|
2021-01-22 09:36:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) GetDdChannel(ctx context.Context, request *commonpb.Empty) (*milvuspb.StringResponse, error) {
|
2021-01-29 09:27:26 +08:00
|
|
|
return s.impl.GetDdChannel(request)
|
2021-01-22 09:36:18 +08:00
|
|
|
}
|