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-06-22 10:42:07 +08:00
|
|
|
package grpcdatacoordclient
|
2021-02-23 11:40:30 +08:00
|
|
|
|
|
|
|
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"
|
|
|
|
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/logutil"
|
2021-03-04 16:01:30 +08:00
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2021-02-23 11:40:30 +08:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2021-06-17 14:17:56 +08:00
|
|
|
grpc_opentracing "github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing"
|
2021-06-01 16:10:32 +08:00
|
|
|
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
|
2021-06-22 10:42:07 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/datacoord"
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
|
|
|
"github.com/milvus-io/milvus/internal/msgstream"
|
|
|
|
"github.com/milvus-io/milvus/internal/types"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/funcutil"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/trace"
|
2021-03-05 20:41:34 +08:00
|
|
|
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/commonpb"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/datapb"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/internalpb"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/milvuspb"
|
2021-02-23 11:40:30 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
2021-06-22 10:42:07 +08:00
|
|
|
dataCoord *datacoord.Server
|
2021-06-21 18:22:13 +08:00
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
2021-02-23 11:40:30 +08:00
|
|
|
|
|
|
|
grpcErrChan chan error
|
|
|
|
wg sync.WaitGroup
|
|
|
|
|
2021-06-21 17:28:03 +08:00
|
|
|
grpcServer *grpc.Server
|
|
|
|
rootCoord types.RootCoord
|
2021-02-24 09:48:17 +08:00
|
|
|
|
|
|
|
closer io.Closer
|
2021-02-23 11:40:30 +08:00
|
|
|
}
|
|
|
|
|
2021-05-08 14:07:04 +08:00
|
|
|
// NewServer new data service grpc server
|
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-06-22 10:42:07 +08:00
|
|
|
s.dataCoord, err = datacoord.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-06-30 16:18:13 +08:00
|
|
|
closer := trace.InitTracing("datacoord")
|
2021-03-23 01:49:50 +08:00
|
|
|
s.closer = closer
|
|
|
|
|
2021-06-22 10:42:07 +08:00
|
|
|
datacoord.Params.Init()
|
|
|
|
datacoord.Params.IP = Params.IP
|
|
|
|
datacoord.Params.Port = Params.Port
|
2021-05-19 18:36:05 +08:00
|
|
|
|
2021-06-21 18:22:13 +08:00
|
|
|
err := s.dataCoord.Register()
|
2021-05-25 15:06:05 +08:00
|
|
|
if err != nil {
|
2021-06-21 18:22:13 +08:00
|
|
|
log.Debug("DataCoord Register etcd failed", zap.Error(err))
|
2021-05-25 15:06:05 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-06-21 18:22:13 +08:00
|
|
|
log.Debug("DataCoord Register etcd success")
|
2021-05-25 15:06:05 +08:00
|
|
|
|
|
|
|
err = s.startGrpc()
|
2021-05-07 15:43:52 +08:00
|
|
|
if err != nil {
|
2021-06-21 18:22:13 +08:00
|
|
|
log.Debug("DataCoord startGrpc failed", zap.Error(err))
|
2021-02-23 11:40:30 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-21 18:22:13 +08:00
|
|
|
if err := s.dataCoord.Init(); err != nil {
|
|
|
|
log.Error("dataCoord init error", zap.Error(err))
|
2021-02-23 11:40:30 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-07 15:43:52 +08:00
|
|
|
func (s *Server) startGrpc() error {
|
|
|
|
s.wg.Add(1)
|
|
|
|
go s.startGrpcLoop(Params.Port)
|
|
|
|
// wait for grpc server loop start
|
|
|
|
err := <-s.grpcErrChan
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-23 11:40:30 +08:00
|
|
|
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-06-17 14:17:56 +08:00
|
|
|
opts := trace.GetInterceptorOpts()
|
2021-03-08 15:49:42 +08:00
|
|
|
s.grpcServer = grpc.NewServer(
|
|
|
|
grpc.MaxRecvMsgSize(math.MaxInt32),
|
|
|
|
grpc.MaxSendMsgSize(math.MaxInt32),
|
|
|
|
grpc.UnaryInterceptor(
|
2021-06-17 14:17:56 +08:00
|
|
|
grpc_opentracing.UnaryServerInterceptor(opts...)),
|
2021-02-26 17:44:24 +08:00
|
|
|
grpc.StreamInterceptor(
|
2021-06-17 14:17:56 +08:00
|
|
|
grpc_opentracing.StreamServerInterceptor(opts...)))
|
2021-06-03 19:01:33 +08:00
|
|
|
//grpc.UnaryInterceptor(grpc_prometheus.UnaryServerInterceptor))
|
2021-06-21 18:22:13 +08:00
|
|
|
datapb.RegisterDataCoordServer(s.grpcServer, s)
|
2021-06-01 16:10:32 +08:00
|
|
|
grpc_prometheus.Register(s.grpcServer)
|
2021-02-23 11:40:30 +08:00
|
|
|
go funcutil.CheckGrpcReady(ctx, s.grpcErrChan)
|
|
|
|
if err := s.grpcServer.Serve(lis); err != nil {
|
|
|
|
s.grpcErrChan <- err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) start() error {
|
2021-06-21 18:22:13 +08:00
|
|
|
return s.dataCoord.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-06-21 18:22:13 +08:00
|
|
|
err = s.dataCoord.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-06-21 18:22:13 +08:00
|
|
|
log.Debug("DataCoord 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) {
|
2021-06-21 18:22:13 +08:00
|
|
|
return s.dataCoord.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) {
|
2021-06-21 18:22:13 +08:00
|
|
|
return s.dataCoord.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) {
|
2021-06-21 18:22:13 +08:00
|
|
|
return s.dataCoord.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) {
|
2021-06-21 18:22:13 +08:00
|
|
|
return s.dataCoord.GetSegmentInfo(ctx, req)
|
2021-02-23 11:40:30 +08:00
|
|
|
}
|
|
|
|
|
2021-06-23 16:56:11 +08:00
|
|
|
func (s *Server) Flush(ctx context.Context, req *datapb.FlushRequest) (*datapb.FlushResponse, error) {
|
2021-06-21 18:22:13 +08:00
|
|
|
return s.dataCoord.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) {
|
2021-06-21 18:22:13 +08:00
|
|
|
return s.dataCoord.AssignSegmentID(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) {
|
2021-06-21 18:22:13 +08:00
|
|
|
return s.dataCoord.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) {
|
2021-06-21 18:22:13 +08:00
|
|
|
return s.dataCoord.GetInsertBinlogPaths(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) {
|
2021-06-21 18:22:13 +08:00
|
|
|
return s.dataCoord.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) {
|
2021-06-21 18:22:13 +08:00
|
|
|
return s.dataCoord.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-06-21 18:22:13 +08:00
|
|
|
return s.dataCoord.GetSegmentInfoChannel(ctx)
|
2021-02-23 11:40:30 +08:00
|
|
|
}
|
2021-05-20 11:34:45 +08:00
|
|
|
|
2021-06-21 18:22:13 +08:00
|
|
|
//SaveBinlogPaths implement DataCoordServer, saves segment, collection binlog according to datanode request
|
2021-05-20 11:34:45 +08:00
|
|
|
func (s *Server) SaveBinlogPaths(ctx context.Context, req *datapb.SaveBinlogPathsRequest) (*commonpb.Status, error) {
|
2021-06-21 18:22:13 +08:00
|
|
|
return s.dataCoord.SaveBinlogPaths(ctx, req)
|
2021-05-20 11:34:45 +08:00
|
|
|
}
|
2021-06-07 14:16:36 +08:00
|
|
|
|
|
|
|
func (s *Server) GetRecoveryInfo(ctx context.Context, req *datapb.GetRecoveryInfoRequest) (*datapb.GetRecoveryInfoResponse, error) {
|
2021-06-21 18:22:13 +08:00
|
|
|
return s.dataCoord.GetRecoveryInfo(ctx, req)
|
2021-06-07 14:16:36 +08:00
|
|
|
}
|