2021-04-19 19:28:11 +08:00
|
|
|
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software distributed under the License
|
|
|
|
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
|
|
|
// or implied. See the License for the specific language governing permissions and limitations under the License.
|
|
|
|
|
2021-02-23 11:40:30 +08:00
|
|
|
package grpcdataserviceclient
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-02-24 09:48:17 +08:00
|
|
|
"io"
|
2021-03-08 15:49:42 +08:00
|
|
|
"math"
|
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"
|
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-03-23 01:49:50 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/util/trace"
|
2021-03-05 20:41:34 +08:00
|
|
|
|
2021-02-23 11:40:30 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/commonpb"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/datapb"
|
2021-03-12 14:22:09 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/internalpb"
|
2021-02-23 11:40:30 +08:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/milvuspb"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
2021-03-12 14:22:09 +08:00
|
|
|
dataService *dataservice.Server
|
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
2021-02-23 11:40:30 +08:00
|
|
|
|
|
|
|
grpcErrChan chan error
|
|
|
|
wg sync.WaitGroup
|
|
|
|
|
2021-03-05 20:41:34 +08:00
|
|
|
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-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()
|
|
|
|
|
2021-03-26 15:13:33 +08:00
|
|
|
closer := trace.InitTracing("data_service")
|
2021-03-23 01:49:50 +08:00
|
|
|
s.closer = closer
|
|
|
|
|
2021-02-23 11:40:30 +08:00
|
|
|
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-12 14:22:09 +08:00
|
|
|
s.dataService.UpdateStateCode(internalpb.StateCode_Initializing)
|
2021-02-23 11:40:30 +08:00
|
|
|
|
2021-02-26 17:44:24 +08:00
|
|
|
ctx := context.Background()
|
2021-03-22 16:36:10 +08:00
|
|
|
err = funcutil.WaitForComponentInitOrHealthy(ctx, client, "MasterService", 1000000, 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()
|
2021-03-08 15:49:42 +08:00
|
|
|
s.grpcServer = grpc.NewServer(
|
|
|
|
grpc.MaxRecvMsgSize(math.MaxInt32),
|
|
|
|
grpc.MaxSendMsgSize(math.MaxInt32),
|
|
|
|
grpc.UnaryInterceptor(
|
|
|
|
otgrpc.OpenTracingServerInterceptor(tracer)),
|
2021-02-26 17:44:24 +08:00
|
|
|
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-03-26 15:13:33 +08:00
|
|
|
if s.closer != nil {
|
|
|
|
if err = s.closer.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-24 09:48:17 +08:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
func (s *Server) GetComponentStates(ctx context.Context, req *internalpb.GetComponentStatesRequest) (*internalpb.ComponentStates, error) {
|
|
|
|
return s.dataService.GetComponentStates(ctx)
|
2021-02-23 11:40:30 +08:00
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
func (s *Server) GetTimeTickChannel(ctx context.Context, req *internalpb.GetTimeTickChannelRequest) (*milvuspb.StringResponse, error) {
|
|
|
|
return s.dataService.GetTimeTickChannel(ctx)
|
2021-02-23 11:40:30 +08:00
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
func (s *Server) GetStatisticsChannel(ctx context.Context, req *internalpb.GetStatisticsChannelRequest) (*milvuspb.StringResponse, error) {
|
|
|
|
return s.dataService.GetStatisticsChannel(ctx)
|
2021-02-23 11:40:30 +08:00
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
func (s *Server) GetSegmentInfo(ctx context.Context, req *datapb.GetSegmentInfoRequest) (*datapb.GetSegmentInfoResponse, error) {
|
|
|
|
return s.dataService.GetSegmentInfo(ctx, req)
|
2021-02-23 11:40:30 +08:00
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
func (s *Server) RegisterNode(ctx context.Context, req *datapb.RegisterNodeRequest) (*datapb.RegisterNodeResponse, error) {
|
|
|
|
return s.dataService.RegisterNode(ctx, req)
|
2021-02-23 11:40:30 +08:00
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
func (s *Server) Flush(ctx context.Context, req *datapb.FlushRequest) (*commonpb.Status, error) {
|
|
|
|
return s.dataService.Flush(ctx, req)
|
2021-02-23 11:40:30 +08:00
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
func (s *Server) AssignSegmentID(ctx context.Context, req *datapb.AssignSegmentIDRequest) (*datapb.AssignSegmentIDResponse, error) {
|
|
|
|
return s.dataService.AssignSegmentID(ctx, req)
|
2021-02-23 11:40:30 +08:00
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
func (s *Server) ShowSegments(ctx context.Context, req *datapb.ShowSegmentsRequest) (*datapb.ShowSegmentsResponse, error) {
|
|
|
|
return s.dataService.ShowSegments(ctx, req)
|
2021-02-23 11:40:30 +08:00
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
func (s *Server) GetSegmentStates(ctx context.Context, req *datapb.GetSegmentStatesRequest) (*datapb.GetSegmentStatesResponse, error) {
|
|
|
|
return s.dataService.GetSegmentStates(ctx, req)
|
2021-02-23 11:40:30 +08:00
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
func (s *Server) GetInsertBinlogPaths(ctx context.Context, req *datapb.GetInsertBinlogPathsRequest) (*datapb.GetInsertBinlogPathsResponse, error) {
|
|
|
|
return s.dataService.GetInsertBinlogPaths(ctx, req)
|
2021-02-23 11:40:30 +08:00
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
func (s *Server) GetInsertChannels(ctx context.Context, req *datapb.GetInsertChannelsRequest) (*internalpb.StringList, error) {
|
|
|
|
return s.dataService.GetInsertChannels(ctx, req)
|
2021-02-23 11:40:30 +08:00
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
func (s *Server) GetCollectionStatistics(ctx context.Context, req *datapb.GetCollectionStatisticsRequest) (*datapb.GetCollectionStatisticsResponse, error) {
|
|
|
|
return s.dataService.GetCollectionStatistics(ctx, req)
|
2021-02-23 11:40:30 +08:00
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
func (s *Server) GetPartitionStatistics(ctx context.Context, req *datapb.GetPartitionStatisticsRequest) (*datapb.GetPartitionStatisticsResponse, error) {
|
|
|
|
return s.dataService.GetPartitionStatistics(ctx, req)
|
2021-02-23 11:40:30 +08:00
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
func (s *Server) GetSegmentInfoChannel(ctx context.Context, req *datapb.GetSegmentInfoChannelRequest) (*milvuspb.StringResponse, error) {
|
2021-03-05 20:41:34 +08:00
|
|
|
return s.dataService.GetSegmentInfoChannel(ctx)
|
2021-02-23 11:40:30 +08:00
|
|
|
}
|