2021-04-19 13:47:10 +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-01-22 14:28:06 +08:00
|
|
|
package grpcqueryservice
|
2021-01-15 15:28:54 +08:00
|
|
|
|
|
|
|
import (
|
2021-01-22 14:28:06 +08:00
|
|
|
"context"
|
2021-03-23 01:49:50 +08:00
|
|
|
"io"
|
2021-03-08 15:49:42 +08:00
|
|
|
"math"
|
2021-01-22 14:28:06 +08:00
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
2021-02-23 11:40:30 +08:00
|
|
|
"time"
|
|
|
|
|
2021-04-22 14:45:57 +08:00
|
|
|
dsc "github.com/milvus-io/milvus/internal/distributed/dataservice/client"
|
|
|
|
msc "github.com/milvus-io/milvus/internal/distributed/masterservice/client"
|
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
|
|
|
"github.com/milvus-io/milvus/internal/msgstream"
|
|
|
|
qs "github.com/milvus-io/milvus/internal/queryservice"
|
|
|
|
"github.com/milvus-io/milvus/internal/types"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/funcutil"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/trace"
|
2021-02-26 17:44:24 +08:00
|
|
|
otgrpc "github.com/opentracing-contrib/go-grpc"
|
|
|
|
"github.com/opentracing/opentracing-go"
|
2021-03-12 14:22:09 +08:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2021-04-22 14:45:57 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/proto/commonpb"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/internalpb"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/milvuspb"
|
|
|
|
"github.com/milvus-io/milvus/internal/proto/querypb"
|
2021-01-15 15:28:54 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
2021-02-23 11:40:30 +08:00
|
|
|
wg sync.WaitGroup
|
2021-01-22 14:28:06 +08:00
|
|
|
loopCtx context.Context
|
2021-02-05 17:57:41 +08:00
|
|
|
loopCancel context.CancelFunc
|
2021-02-23 11:40:30 +08:00
|
|
|
grpcServer *grpc.Server
|
|
|
|
|
|
|
|
grpcErrChan chan error
|
2021-02-05 17:57:41 +08:00
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
queryservice *qs.QueryService
|
2021-02-08 14:30:54 +08:00
|
|
|
|
|
|
|
msFactory msgstream.Factory
|
2021-02-23 11:40:30 +08:00
|
|
|
|
|
|
|
dataService *dsc.Client
|
|
|
|
masterService *msc.GrpcClient
|
2021-03-23 01:49:50 +08:00
|
|
|
|
|
|
|
closer io.Closer
|
2021-02-05 17:57:41 +08:00
|
|
|
}
|
|
|
|
|
2021-02-08 14:30:54 +08:00
|
|
|
func NewServer(ctx context.Context, factory msgstream.Factory) (*Server, error) {
|
2021-02-05 17:57:41 +08:00
|
|
|
ctx1, cancel := context.WithCancel(ctx)
|
2021-02-23 11:40:30 +08:00
|
|
|
svr, err := qs.NewQueryService(ctx1, factory)
|
2021-02-05 17:57:41 +08:00
|
|
|
if err != nil {
|
|
|
|
cancel()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Server{
|
2021-03-12 14:22:09 +08:00
|
|
|
queryservice: svr,
|
|
|
|
loopCtx: ctx1,
|
|
|
|
loopCancel: cancel,
|
|
|
|
msFactory: factory,
|
|
|
|
grpcErrChan: make(chan error),
|
2021-02-05 17:57:41 +08:00
|
|
|
}, nil
|
2021-01-16 15:31:10 +08:00
|
|
|
}
|
|
|
|
|
2021-02-23 11:40:30 +08:00
|
|
|
func (s *Server) Run() error {
|
|
|
|
|
|
|
|
if err := s.init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-04 16:29:35 +08:00
|
|
|
log.Debug("QueryService init done ...")
|
2021-02-23 11:40:30 +08:00
|
|
|
|
|
|
|
if err := s.start(); err != nil {
|
2021-02-04 17:47:19 +08:00
|
|
|
return err
|
2021-01-27 09:50:52 +08:00
|
|
|
}
|
2021-01-22 14:28:06 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-23 11:40:30 +08:00
|
|
|
func (s *Server) init() error {
|
2021-02-26 17:44:24 +08:00
|
|
|
ctx := context.Background()
|
2021-02-23 11:40:30 +08:00
|
|
|
Params.Init()
|
2021-03-23 01:49:50 +08:00
|
|
|
qs.Params.Init()
|
2021-05-21 19:28:52 +08:00
|
|
|
qs.Params.Port = Params.Port
|
2021-03-23 01:49:50 +08:00
|
|
|
|
2021-03-26 15:13:33 +08:00
|
|
|
closer := trace.InitTracing("query_service")
|
2021-03-23 01:49:50 +08:00
|
|
|
s.closer = closer
|
2021-02-05 17:57:41 +08:00
|
|
|
|
2021-05-25 15:06:05 +08:00
|
|
|
if err := s.queryservice.Register(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-01-26 15:13:20 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-02-05 17:57:41 +08:00
|
|
|
|
2021-02-23 11:40:30 +08:00
|
|
|
// --- Master Server Client ---
|
2021-06-04 16:29:35 +08:00
|
|
|
log.Debug("QueryService try to new MasterService client", zap.Any("MasterServiceAddress", Params.MasterAddress))
|
2021-05-26 20:14:30 +08:00
|
|
|
masterService, err := msc.NewClient(Params.MasterAddress, qs.Params.MetaRootPath, []string{qs.Params.EtcdAddress}, 20*time.Second)
|
2021-02-05 17:57:41 +08:00
|
|
|
if err != nil {
|
2021-06-04 16:29:35 +08:00
|
|
|
log.Debug("QueryService try to new MasterService client failed", zap.Error(err))
|
2021-02-23 11:40:30 +08:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = masterService.Init(); err != nil {
|
2021-06-04 16:29:35 +08:00
|
|
|
log.Debug("QueryService MasterServiceClient Init failed", zap.Error(err))
|
2021-02-23 11:40:30 +08:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = masterService.Start(); err != nil {
|
2021-06-04 16:29:35 +08:00
|
|
|
log.Debug("QueryService MasterServiceClient Start failed", zap.Error(err))
|
2021-02-23 11:40:30 +08:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
// wait for master init or healthy
|
2021-06-04 16:29:35 +08:00
|
|
|
log.Debug("QueryService try to wait for MasterService ready")
|
2021-03-22 16:36:10 +08:00
|
|
|
err = funcutil.WaitForComponentInitOrHealthy(ctx, masterService, "MasterService", 1000000, time.Millisecond*200)
|
2021-02-23 11:40:30 +08:00
|
|
|
if err != nil {
|
2021-06-04 16:29:35 +08:00
|
|
|
log.Debug("QueryService wait for MasterService ready failed", zap.Error(err))
|
2021-02-23 11:40:30 +08:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.SetMasterService(masterService); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-06-04 16:29:35 +08:00
|
|
|
log.Debug("QueryService report MasterService ready")
|
2021-02-23 11:40:30 +08:00
|
|
|
|
|
|
|
// --- Data service client ---
|
2021-06-04 16:29:35 +08:00
|
|
|
log.Debug("QueryService try to new DataService client", zap.Any("DataServiceAddress", Params.DataServiceAddress))
|
2021-02-23 11:40:30 +08:00
|
|
|
|
2021-06-04 16:29:35 +08:00
|
|
|
dataService := dsc.NewClient(Params.DataServiceAddress, qs.Params.MetaRootPath, []string{qs.Params.EtcdAddress}, 10*time.Second)
|
2021-02-23 11:40:30 +08:00
|
|
|
if err = dataService.Init(); err != nil {
|
2021-06-04 16:29:35 +08:00
|
|
|
log.Debug("QueryService DataServiceClient Init failed", zap.Error(err))
|
2021-02-23 11:40:30 +08:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err = dataService.Start(); err != nil {
|
2021-06-04 16:29:35 +08:00
|
|
|
log.Debug("QueryService DataServiceClient Start failed", zap.Error(err))
|
2021-02-23 11:40:30 +08:00
|
|
|
panic(err)
|
|
|
|
}
|
2021-06-04 16:29:35 +08:00
|
|
|
log.Debug("QueryService try to wait for DataService ready")
|
2021-03-22 16:36:10 +08:00
|
|
|
err = funcutil.WaitForComponentInitOrHealthy(ctx, dataService, "DataService", 1000000, time.Millisecond*200)
|
2021-02-23 11:40:30 +08:00
|
|
|
if err != nil {
|
2021-06-04 16:29:35 +08:00
|
|
|
log.Debug("QueryService wait for DataService ready failed", zap.Error(err))
|
2021-02-23 11:40:30 +08:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err := s.SetDataService(dataService); err != nil {
|
|
|
|
panic(err)
|
2021-02-05 17:57:41 +08:00
|
|
|
}
|
2021-06-04 16:29:35 +08:00
|
|
|
log.Debug("QueryService report DataService ready")
|
2021-02-05 17:57:41 +08:00
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
s.queryservice.UpdateStateCode(internalpb.StateCode_Initializing)
|
2021-06-04 16:29:35 +08:00
|
|
|
log.Debug("QueryService", zap.Any("State", internalpb.StateCode_Initializing))
|
2021-03-12 14:22:09 +08:00
|
|
|
if err := s.queryservice.Init(); err != nil {
|
2021-02-23 11:40:30 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-01-22 14:28:06 +08:00
|
|
|
return nil
|
2021-01-16 15:31:10 +08:00
|
|
|
}
|
|
|
|
|
2021-02-23 11:40:30 +08:00
|
|
|
func (s *Server) startGrpcLoop(grpcPort int) {
|
|
|
|
|
|
|
|
defer s.wg.Done()
|
|
|
|
|
2021-02-27 10:11:52 +08:00
|
|
|
log.Debug("network", zap.String("port", strconv.Itoa(grpcPort)))
|
2021-02-23 11:40:30 +08:00
|
|
|
lis, err := net.Listen("tcp", ":"+strconv.Itoa(grpcPort))
|
|
|
|
if err != nil {
|
2021-02-27 10:11:52 +08:00
|
|
|
log.Debug("GrpcServer:failed to listen:", zap.String("error", err.Error()))
|
2021-02-23 11:40:30 +08:00
|
|
|
s.grpcErrChan <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(s.loopCtx)
|
|
|
|
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
|
|
|
querypb.RegisterQueryServiceServer(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-12 14:22:09 +08:00
|
|
|
return s.queryservice.Start()
|
2021-02-23 11:40:30 +08:00
|
|
|
}
|
|
|
|
|
2021-01-21 10:01:29 +08:00
|
|
|
func (s *Server) Stop() error {
|
2021-03-26 15:13:33 +08:00
|
|
|
if s.closer != nil {
|
|
|
|
if err := s.closer.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-23 01:49:50 +08:00
|
|
|
}
|
2021-03-26 15:13:33 +08:00
|
|
|
err := s.queryservice.Stop()
|
2021-01-22 14:28:06 +08:00
|
|
|
s.loopCancel()
|
|
|
|
if s.grpcServer != nil {
|
|
|
|
s.grpcServer.GracefulStop()
|
|
|
|
}
|
2021-02-05 17:57:41 +08:00
|
|
|
return err
|
2021-01-22 14:28:06 +08:00
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
func (s *Server) SetMasterService(m types.MasterService) error {
|
|
|
|
s.queryservice.SetMasterService(m)
|
|
|
|
return nil
|
2021-01-16 15:31:10 +08:00
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
func (s *Server) SetDataService(d types.DataService) error {
|
|
|
|
s.queryservice.SetDataService(d)
|
|
|
|
return nil
|
2021-01-22 14:28:06 +08:00
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
func (s *Server) GetComponentStates(ctx context.Context, req *internalpb.GetComponentStatesRequest) (*internalpb.ComponentStates, error) {
|
|
|
|
return s.queryservice.GetComponentStates(ctx)
|
2021-01-16 15:31:10 +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.queryservice.GetTimeTickChannel(ctx)
|
2021-02-02 09:52:42 +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.queryservice.GetStatisticsChannel(ctx)
|
2021-02-02 09:52:42 +08:00
|
|
|
}
|
|
|
|
|
2021-01-22 14:28:06 +08:00
|
|
|
func (s *Server) RegisterNode(ctx context.Context, req *querypb.RegisterNodeRequest) (*querypb.RegisterNodeResponse, error) {
|
2021-03-12 14:22:09 +08:00
|
|
|
return s.queryservice.RegisterNode(ctx, req)
|
2021-01-16 15:31:10 +08:00
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
func (s *Server) ShowCollections(ctx context.Context, req *querypb.ShowCollectionsRequest) (*querypb.ShowCollectionsResponse, error) {
|
|
|
|
return s.queryservice.ShowCollections(ctx, req)
|
2021-01-15 15:28:54 +08:00
|
|
|
}
|
|
|
|
|
2021-01-22 14:28:06 +08:00
|
|
|
func (s *Server) LoadCollection(ctx context.Context, req *querypb.LoadCollectionRequest) (*commonpb.Status, error) {
|
2021-03-12 14:22:09 +08:00
|
|
|
return s.queryservice.LoadCollection(ctx, req)
|
2021-01-15 15:28:54 +08:00
|
|
|
}
|
|
|
|
|
2021-01-22 14:28:06 +08:00
|
|
|
func (s *Server) ReleaseCollection(ctx context.Context, req *querypb.ReleaseCollectionRequest) (*commonpb.Status, error) {
|
2021-03-12 14:22:09 +08:00
|
|
|
return s.queryservice.ReleaseCollection(ctx, req)
|
2021-01-15 15:28:54 +08:00
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
func (s *Server) ShowPartitions(ctx context.Context, req *querypb.ShowPartitionsRequest) (*querypb.ShowPartitionsResponse, error) {
|
|
|
|
return s.queryservice.ShowPartitions(ctx, req)
|
2021-01-15 15:28:54 +08:00
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
func (s *Server) GetPartitionStates(ctx context.Context, req *querypb.GetPartitionStatesRequest) (*querypb.GetPartitionStatesResponse, error) {
|
|
|
|
return s.queryservice.GetPartitionStates(ctx, req)
|
2021-01-15 15:28:54 +08:00
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
func (s *Server) LoadPartitions(ctx context.Context, req *querypb.LoadPartitionsRequest) (*commonpb.Status, error) {
|
|
|
|
return s.queryservice.LoadPartitions(ctx, req)
|
2021-01-15 15:28:54 +08:00
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
func (s *Server) ReleasePartitions(ctx context.Context, req *querypb.ReleasePartitionsRequest) (*commonpb.Status, error) {
|
|
|
|
return s.queryservice.ReleasePartitions(ctx, req)
|
2021-01-15 15:28:54 +08:00
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
func (s *Server) CreateQueryChannel(ctx context.Context, req *querypb.CreateQueryChannelRequest) (*querypb.CreateQueryChannelResponse, error) {
|
|
|
|
return s.queryservice.CreateQueryChannel(ctx)
|
2021-01-15 15:28:54 +08:00
|
|
|
}
|
|
|
|
|
2021-03-12 14:22:09 +08:00
|
|
|
func (s *Server) GetSegmentInfo(ctx context.Context, req *querypb.GetSegmentInfoRequest) (*querypb.GetSegmentInfoResponse, error) {
|
|
|
|
return s.queryservice.GetSegmentInfo(ctx, req)
|
2021-02-04 11:40:14 +08:00
|
|
|
}
|