2021-02-23 03:40:30 +00:00
|
|
|
package grpcdatanode
|
2021-01-19 03:37:16 +00:00
|
|
|
|
|
|
|
import (
|
2021-01-22 01:36:40 +00:00
|
|
|
"context"
|
2021-02-24 01:48:17 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2021-01-24 13:20:11 +00:00
|
|
|
"net"
|
|
|
|
"strconv"
|
2021-02-26 02:13:36 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
2021-01-22 01:36:40 +00:00
|
|
|
|
2021-02-26 09:44:24 +00:00
|
|
|
otgrpc "github.com/opentracing-contrib/go-grpc"
|
2021-02-24 01:48:17 +00:00
|
|
|
"github.com/opentracing/opentracing-go"
|
|
|
|
"github.com/uber/jaeger-client-go/config"
|
2021-02-26 02:13:36 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2021-01-24 13:20:11 +00:00
|
|
|
dn "github.com/zilliztech/milvus-distributed/internal/datanode"
|
2021-02-23 03:40:30 +00:00
|
|
|
dsc "github.com/zilliztech/milvus-distributed/internal/distributed/dataservice/client"
|
|
|
|
msc "github.com/zilliztech/milvus-distributed/internal/distributed/masterservice/client"
|
2021-02-26 02:13:36 +00:00
|
|
|
|
2021-02-02 01:52:42 +00:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/errors"
|
2021-02-26 02:13:36 +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-22 01:36:40 +00:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/commonpb"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/datapb"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/internalpb2"
|
2021-02-26 02:13:36 +00:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/util/funcutil"
|
2021-01-19 03:37:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
2021-02-23 03:40:30 +00:00
|
|
|
impl *dn.DataNode
|
|
|
|
wg sync.WaitGroup
|
|
|
|
grpcErrChan chan error
|
|
|
|
grpcServer *grpc.Server
|
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
2021-02-08 06:30:54 +00:00
|
|
|
|
|
|
|
msFactory msgstream.Factory
|
2021-02-23 03:40:30 +00:00
|
|
|
|
|
|
|
masterService *msc.GrpcClient
|
|
|
|
dataService *dsc.Client
|
2021-02-24 01:48:17 +00:00
|
|
|
|
|
|
|
closer io.Closer
|
2021-01-22 01:36:40 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 06:30:54 +00:00
|
|
|
func New(ctx context.Context, factory msgstream.Factory) (*Server, error) {
|
2021-02-04 12:31:23 +00:00
|
|
|
ctx1, cancel := context.WithCancel(ctx)
|
2021-01-27 03:34:16 +00:00
|
|
|
var s = &Server{
|
2021-02-23 03:40:30 +00:00
|
|
|
ctx: ctx1,
|
|
|
|
cancel: cancel,
|
|
|
|
msFactory: factory,
|
|
|
|
grpcErrChan: make(chan error),
|
2021-01-27 03:34:16 +00:00
|
|
|
}
|
2021-01-24 13:20:11 +00:00
|
|
|
|
2021-02-23 03:40:30 +00:00
|
|
|
s.impl = dn.NewDataNode(s.ctx, s.msFactory)
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) startGrpcLoop(grpcPort int) {
|
|
|
|
defer s.wg.Done()
|
|
|
|
|
|
|
|
addr := ":" + strconv.Itoa(grpcPort)
|
|
|
|
|
2021-01-24 13:20:11 +00:00
|
|
|
lis, err := net.Listen("tcp", addr)
|
|
|
|
if err != nil {
|
2021-02-26 02:13:36 +00:00
|
|
|
log.Warn("GrpcServer failed to listen", zap.Error(err))
|
2021-02-23 03:40:30 +00:00
|
|
|
s.grpcErrChan <- err
|
|
|
|
return
|
2021-01-24 13:20:11 +00:00
|
|
|
}
|
2021-02-26 02:13:36 +00:00
|
|
|
log.Debug("DataNode address", zap.String("address", addr))
|
2021-01-24 13:20:11 +00:00
|
|
|
|
2021-02-26 09:44:24 +00:00
|
|
|
tracer := opentracing.GlobalTracer()
|
|
|
|
s.grpcServer = grpc.NewServer(grpc.UnaryInterceptor(
|
|
|
|
otgrpc.OpenTracingServerInterceptor(tracer)),
|
|
|
|
grpc.StreamInterceptor(
|
|
|
|
otgrpc.OpenTracingStreamServerInterceptor(tracer)))
|
2021-02-23 03:40:30 +00:00
|
|
|
datapb.RegisterDataNodeServer(s.grpcServer, s)
|
2021-01-24 13:20:11 +00:00
|
|
|
|
2021-02-23 03:40:30 +00:00
|
|
|
ctx, cancel := context.WithCancel(s.ctx)
|
|
|
|
defer cancel()
|
2021-01-24 13:20:11 +00:00
|
|
|
|
2021-02-23 03:40:30 +00:00
|
|
|
go funcutil.CheckGrpcReady(ctx, s.grpcErrChan)
|
|
|
|
if err := s.grpcServer.Serve(lis); err != nil {
|
2021-02-26 02:13:36 +00:00
|
|
|
log.Warn("DataNode Start Grpc Failed!")
|
2021-02-23 03:40:30 +00:00
|
|
|
s.grpcErrChan <- err
|
2021-01-24 13:20:11 +00:00
|
|
|
}
|
2021-02-23 03:40:30 +00:00
|
|
|
|
2021-01-22 01:36:40 +00:00
|
|
|
}
|
|
|
|
|
2021-02-26 09:44:24 +00:00
|
|
|
func (s *Server) SetMasterServiceInterface(ctx context.Context, ms dn.MasterServiceInterface) error {
|
|
|
|
return s.impl.SetMasterServiceInterface(ctx, ms)
|
2021-01-26 06:46:54 +00:00
|
|
|
}
|
|
|
|
|
2021-02-26 09:44:24 +00:00
|
|
|
func (s *Server) SetDataServiceInterface(ctx context.Context, ds dn.DataServiceInterface) error {
|
|
|
|
return s.impl.SetDataServiceInterface(ctx, ds)
|
2021-01-26 06:46:54 +00:00
|
|
|
}
|
|
|
|
|
2021-02-23 03:40:30 +00:00
|
|
|
func (s *Server) Run() error {
|
2021-01-22 01:36:40 +00:00
|
|
|
|
2021-02-23 03:40:30 +00:00
|
|
|
if err := s.init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-26 02:13:36 +00:00
|
|
|
log.Debug("data node init done ...")
|
2021-02-23 03:40:30 +00:00
|
|
|
|
|
|
|
if err := s.start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-26 02:13:36 +00:00
|
|
|
log.Debug("data node start done ...")
|
2021-02-23 03:40:30 +00:00
|
|
|
return nil
|
2021-01-22 01:36:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Stop() error {
|
2021-02-24 01:48:17 +00:00
|
|
|
if err := s.closer.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-04 12:31:23 +00:00
|
|
|
s.cancel()
|
2021-02-23 03:40:30 +00:00
|
|
|
if s.grpcServer != nil {
|
|
|
|
s.grpcServer.GracefulStop()
|
|
|
|
}
|
|
|
|
|
2021-02-24 01:48:17 +00:00
|
|
|
err := s.impl.Stop()
|
2021-02-23 03:40:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.wg.Wait()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) init() error {
|
2021-02-26 09:44:24 +00:00
|
|
|
ctx := context.Background()
|
2021-02-23 03:40:30 +00:00
|
|
|
Params.Init()
|
2021-02-23 10:08:17 +00:00
|
|
|
if !funcutil.CheckPortAvailable(Params.Port) {
|
|
|
|
Params.Port = funcutil.GetAvailablePort()
|
|
|
|
}
|
2021-02-23 03:40:30 +00:00
|
|
|
Params.LoadFromEnv()
|
|
|
|
Params.LoadFromArgs()
|
|
|
|
|
2021-02-26 02:13:36 +00:00
|
|
|
log.Debug("DataNode port", zap.Int("port", Params.Port))
|
2021-02-26 09:44:24 +00:00
|
|
|
// TODO
|
|
|
|
cfg := &config.Configuration{
|
|
|
|
ServiceName: fmt.Sprintf("data_node ip: %s, port: %d", Params.IP, Params.Port),
|
|
|
|
Sampler: &config.SamplerConfig{
|
|
|
|
Type: "const",
|
|
|
|
Param: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
tracer, closer, err := cfg.NewTracer()
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("ERROR: cannot init Jaeger: %v\n", err))
|
|
|
|
}
|
|
|
|
opentracing.SetGlobalTracer(tracer)
|
|
|
|
s.closer = closer
|
|
|
|
|
2021-02-23 03:40:30 +00:00
|
|
|
s.wg.Add(1)
|
|
|
|
go s.startGrpcLoop(Params.Port)
|
|
|
|
// wait for grpc server loop start
|
2021-02-26 09:44:24 +00:00
|
|
|
err = <-s.grpcErrChan
|
2021-02-23 03:40:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- Master Server Client ---
|
2021-02-26 02:13:36 +00:00
|
|
|
log.Debug("Master service address", zap.String("address", Params.MasterAddress))
|
|
|
|
log.Debug("Init master service client ...")
|
2021-02-23 03:40:30 +00:00
|
|
|
masterClient, err := msc.NewClient(Params.MasterAddress, 20*time.Second)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = masterClient.Init(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = masterClient.Start(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-02-26 09:44:24 +00:00
|
|
|
err = funcutil.WaitForComponentHealthy(ctx, masterClient, "MasterService", 100, time.Millisecond*200)
|
2021-02-23 03:40:30 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-02-26 09:44:24 +00:00
|
|
|
if err := s.SetMasterServiceInterface(ctx, masterClient); err != nil {
|
2021-02-23 03:40:30 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- Data Server Client ---
|
2021-02-26 02:13:36 +00:00
|
|
|
log.Debug("Data service address", zap.String("address", Params.DataServiceAddress))
|
|
|
|
log.Debug("DataNode Init data service client ...")
|
2021-02-23 03:40:30 +00:00
|
|
|
dataService := dsc.NewClient(Params.DataServiceAddress)
|
|
|
|
if err = dataService.Init(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err = dataService.Start(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-02-26 09:44:24 +00:00
|
|
|
err = funcutil.WaitForComponentInitOrHealthy(ctx, dataService, "DataService", 100, time.Millisecond*200)
|
2021-02-23 03:40:30 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-02-26 09:44:24 +00:00
|
|
|
if err := s.SetDataServiceInterface(ctx, dataService); err != nil {
|
2021-02-23 03:40:30 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dn.Params.Init()
|
|
|
|
dn.Params.Port = Params.Port
|
|
|
|
dn.Params.IP = Params.IP
|
|
|
|
|
|
|
|
s.impl.NodeID = dn.Params.NodeID
|
|
|
|
s.impl.UpdateStateCode(internalpb2.StateCode_INITIALIZING)
|
|
|
|
|
|
|
|
if err := s.impl.Init(); err != nil {
|
2021-02-26 02:13:36 +00:00
|
|
|
log.Warn("impl init error: ", zap.Error(err))
|
2021-02-23 03:40:30 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) start() error {
|
|
|
|
return s.impl.Start()
|
2021-01-22 01:36:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) GetComponentStates(ctx context.Context, empty *commonpb.Empty) (*internalpb2.ComponentStates, error) {
|
2021-02-26 09:44:24 +00:00
|
|
|
return s.impl.GetComponentStates(ctx)
|
2021-01-22 01:36:40 +00:00
|
|
|
}
|
|
|
|
|
2021-01-24 13:20:11 +00:00
|
|
|
func (s *Server) WatchDmChannels(ctx context.Context, in *datapb.WatchDmChannelRequest) (*commonpb.Status, error) {
|
2021-02-26 09:44:24 +00:00
|
|
|
return s.impl.WatchDmChannels(ctx, in)
|
2021-01-22 01:36:40 +00:00
|
|
|
}
|
|
|
|
|
2021-01-24 13:20:11 +00:00
|
|
|
func (s *Server) FlushSegments(ctx context.Context, in *datapb.FlushSegRequest) (*commonpb.Status, error) {
|
2021-02-23 03:40:30 +00:00
|
|
|
if s.impl.State.Load().(internalpb2.StateCode) != internalpb2.StateCode_HEALTHY {
|
2021-02-02 01:52:42 +00:00
|
|
|
return &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
|
|
|
|
Reason: "DataNode isn't healthy.",
|
|
|
|
}, errors.Errorf("DataNode is not ready yet")
|
|
|
|
}
|
2021-01-24 13:20:11 +00:00
|
|
|
return &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_SUCCESS,
|
2021-02-26 09:44:24 +00:00
|
|
|
}, s.impl.FlushSegments(ctx, in)
|
2021-01-19 03:37:16 +00:00
|
|
|
}
|