2021-02-23 11:40:30 +08:00
|
|
|
package grpcdataserviceclient
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-02-24 09:48:17 +08:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2021-02-23 11:40:30 +08:00
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2021-03-04 16:01:30 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/logutil"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2021-02-23 11:40:30 +08:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2021-02-26 17:44:24 +08:00
|
|
|
otgrpc "github.com/opentracing-contrib/go-grpc"
|
2021-02-24 09:48:17 +08:00
|
|
|
"github.com/opentracing/opentracing-go"
|
|
|
|
"github.com/uber/jaeger-client-go/config"
|
2021-02-23 11:40:30 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/dataservice"
|
2021-03-05 20:41:34 +08:00
|
|
|
msc "github.com/zilliztech/milvus-distributed/internal/distributed/masterservice/client"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/log"
|
2021-02-23 11:40:30 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/msgstream"
|
2021-03-05 20:41:34 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/types"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/util/funcutil"
|
|
|
|
|
2021-02-23 11:40:30 +08: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"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/milvuspb"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
|
|
|
|
grpcErrChan chan error
|
|
|
|
wg sync.WaitGroup
|
|
|
|
|
2021-03-05 20:41:34 +08:00
|
|
|
dataService *dataservice.Server
|
|
|
|
grpcServer *grpc.Server
|
|
|
|
masterService types.MasterService
|
2021-02-24 09:48:17 +08:00
|
|
|
|
|
|
|
closer io.Closer
|
2021-02-23 11:40:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewServer(ctx context.Context, factory msgstream.Factory) (*Server, error) {
|
2021-02-24 09:48:17 +08:00
|
|
|
var err error
|
2021-02-23 11:40:30 +08:00
|
|
|
ctx1, cancel := context.WithCancel(ctx)
|
|
|
|
|
|
|
|
s := &Server{
|
|
|
|
ctx: ctx1,
|
|
|
|
cancel: cancel,
|
|
|
|
grpcErrChan: make(chan error),
|
|
|
|
}
|
|
|
|
|
2021-02-24 09:48:17 +08:00
|
|
|
// TODO
|
|
|
|
cfg := &config.Configuration{
|
|
|
|
ServiceName: "data_service",
|
|
|
|
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-03-05 20:41:34 +08:00
|
|
|
s.dataService, err = dataservice.CreateServer(s.ctx, factory)
|
2021-02-23 11:40:30 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) init() error {
|
|
|
|
Params.Init()
|
|
|
|
Params.LoadFromEnv()
|
|
|
|
|
|
|
|
s.wg.Add(1)
|
|
|
|
go s.startGrpcLoop(Params.Port)
|
|
|
|
// wait for grpc server loop start
|
|
|
|
if err := <-s.grpcErrChan; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-04 16:01:30 +08:00
|
|
|
log.Debug("master address", zap.String("address", Params.MasterAddress))
|
2021-02-23 11:40:30 +08:00
|
|
|
client, err := msc.NewClient(Params.MasterAddress, 10*time.Second)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-03-04 16:01:30 +08:00
|
|
|
log.Debug("master client create complete")
|
2021-02-23 11:40:30 +08:00
|
|
|
if err = client.Init(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err = client.Start(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-03-05 20:41:34 +08:00
|
|
|
s.dataService.UpdateStateCode(internalpb2.StateCode_INITIALIZING)
|
2021-02-23 11:40:30 +08:00
|
|
|
|
2021-02-26 17:44:24 +08:00
|
|
|
ctx := context.Background()
|
|
|
|
err = funcutil.WaitForComponentInitOrHealthy(ctx, client, "MasterService", 100, time.Millisecond*200)
|
2021-02-23 11:40:30 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-03-05 20:41:34 +08:00
|
|
|
s.dataService.SetMasterClient(client)
|
2021-02-23 11:40:30 +08:00
|
|
|
|
|
|
|
dataservice.Params.Init()
|
2021-03-05 20:41:34 +08:00
|
|
|
if err := s.dataService.Init(); err != nil {
|
|
|
|
log.Error("dataService init error", zap.Error(err))
|
2021-02-23 11:40:30 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) startGrpcLoop(grpcPort int) {
|
2021-03-04 16:01:30 +08:00
|
|
|
defer logutil.LogPanic()
|
2021-02-23 11:40:30 +08:00
|
|
|
defer s.wg.Done()
|
|
|
|
|
2021-03-04 16:01:30 +08:00
|
|
|
log.Debug("network port", zap.Int("port", grpcPort))
|
2021-02-23 11:40:30 +08:00
|
|
|
lis, err := net.Listen("tcp", ":"+strconv.Itoa(grpcPort))
|
|
|
|
if err != nil {
|
2021-03-04 16:01:30 +08:00
|
|
|
log.Error("grpc server failed to listen error", zap.Error(err))
|
2021-02-23 11:40:30 +08:00
|
|
|
s.grpcErrChan <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(s.ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
2021-02-26 17:44:24 +08:00
|
|
|
tracer := opentracing.GlobalTracer()
|
|
|
|
s.grpcServer = grpc.NewServer(grpc.UnaryInterceptor(
|
|
|
|
otgrpc.OpenTracingServerInterceptor(tracer)),
|
|
|
|
grpc.StreamInterceptor(
|
|
|
|
otgrpc.OpenTracingStreamServerInterceptor(tracer)))
|
2021-02-23 11:40:30 +08:00
|
|
|
datapb.RegisterDataServiceServer(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-03-05 20:41:34 +08:00
|
|
|
return s.dataService.Start()
|
2021-02-23 11:40:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Stop() error {
|
|
|
|
var err error
|
2021-02-24 09:48:17 +08:00
|
|
|
if err = s.closer.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.cancel()
|
2021-02-23 11:40:30 +08:00
|
|
|
|
|
|
|
if s.grpcServer != nil {
|
|
|
|
s.grpcServer.GracefulStop()
|
|
|
|
}
|
|
|
|
|
2021-03-05 20:41:34 +08:00
|
|
|
err = s.dataService.Stop()
|
2021-02-23 11:40:30 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.wg.Wait()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Run() error {
|
|
|
|
|
|
|
|
if err := s.init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-04 16:01:30 +08:00
|
|
|
log.Debug("dataservice init done ...")
|
2021-02-23 11:40:30 +08:00
|
|
|
|
|
|
|
if err := s.start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) GetSegmentInfo(ctx context.Context, request *datapb.SegmentInfoRequest) (*datapb.SegmentInfoResponse, error) {
|
2021-03-05 20:41:34 +08:00
|
|
|
return s.dataService.GetSegmentInfo(ctx, request)
|
2021-02-23 11:40:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) RegisterNode(ctx context.Context, request *datapb.RegisterNodeRequest) (*datapb.RegisterNodeResponse, error) {
|
2021-03-05 20:41:34 +08:00
|
|
|
return s.dataService.RegisterNode(ctx, request)
|
2021-02-23 11:40:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Flush(ctx context.Context, request *datapb.FlushRequest) (*commonpb.Status, error) {
|
2021-03-05 20:41:34 +08:00
|
|
|
return s.dataService.Flush(ctx, request)
|
2021-02-23 11:40:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) AssignSegmentID(ctx context.Context, request *datapb.AssignSegIDRequest) (*datapb.AssignSegIDResponse, error) {
|
2021-03-05 20:41:34 +08:00
|
|
|
return s.dataService.AssignSegmentID(ctx, request)
|
2021-02-23 11:40:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) ShowSegments(ctx context.Context, request *datapb.ShowSegmentRequest) (*datapb.ShowSegmentResponse, error) {
|
2021-03-05 20:41:34 +08:00
|
|
|
return s.dataService.ShowSegments(ctx, request)
|
2021-02-23 11:40:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) GetSegmentStates(ctx context.Context, request *datapb.SegmentStatesRequest) (*datapb.SegmentStatesResponse, error) {
|
2021-03-05 20:41:34 +08:00
|
|
|
return s.dataService.GetSegmentStates(ctx, request)
|
2021-02-23 11:40:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) GetInsertBinlogPaths(ctx context.Context, request *datapb.InsertBinlogPathRequest) (*datapb.InsertBinlogPathsResponse, error) {
|
2021-03-05 20:41:34 +08:00
|
|
|
return s.dataService.GetInsertBinlogPaths(ctx, request)
|
2021-02-23 11:40:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) GetInsertChannels(ctx context.Context, request *datapb.InsertChannelRequest) (*internalpb2.StringList, error) {
|
2021-03-05 20:41:34 +08:00
|
|
|
return s.dataService.GetInsertChannels(ctx, request)
|
2021-02-23 11:40:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) GetCollectionStatistics(ctx context.Context, request *datapb.CollectionStatsRequest) (*datapb.CollectionStatsResponse, error) {
|
2021-03-05 20:41:34 +08:00
|
|
|
return s.dataService.GetCollectionStatistics(ctx, request)
|
2021-02-23 11:40:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) GetPartitionStatistics(ctx context.Context, request *datapb.PartitionStatsRequest) (*datapb.PartitionStatsResponse, error) {
|
2021-03-05 20:41:34 +08:00
|
|
|
return s.dataService.GetPartitionStatistics(ctx, request)
|
2021-02-23 11:40:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) GetComponentStates(ctx context.Context, empty *commonpb.Empty) (*internalpb2.ComponentStates, error) {
|
2021-03-05 20:41:34 +08:00
|
|
|
return s.dataService.GetComponentStates(ctx)
|
2021-02-23 11:40:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) GetTimeTickChannel(ctx context.Context, empty *commonpb.Empty) (*milvuspb.StringResponse, error) {
|
2021-03-05 20:41:34 +08:00
|
|
|
return s.dataService.GetTimeTickChannel(ctx)
|
2021-02-23 11:40:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) GetStatisticsChannel(ctx context.Context, empty *commonpb.Empty) (*milvuspb.StringResponse, error) {
|
2021-03-05 20:41:34 +08:00
|
|
|
return s.dataService.GetStatisticsChannel(ctx)
|
2021-02-23 11:40:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) GetSegmentInfoChannel(ctx context.Context, empty *commonpb.Empty) (*milvuspb.StringResponse, error) {
|
2021-03-05 20:41:34 +08:00
|
|
|
return s.dataService.GetSegmentInfoChannel(ctx)
|
2021-02-23 11:40:30 +08:00
|
|
|
}
|