2021-01-22 06:28:06 +00:00
|
|
|
package grpcqueryservice
|
2021-01-15 07:28:54 +00:00
|
|
|
|
|
|
|
import (
|
2021-01-22 06:28:06 +00:00
|
|
|
"context"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
|
2021-01-15 07:28:54 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2021-01-22 06:28:06 +00:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/commonpb"
|
2021-01-26 07:13:20 +00:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/internalpb2"
|
2021-01-22 06:28:06 +00:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/milvuspb"
|
2021-01-15 07:28:54 +00:00
|
|
|
"github.com/zilliztech/milvus-distributed/internal/proto/querypb"
|
2021-02-05 09:57:41 +00:00
|
|
|
qs "github.com/zilliztech/milvus-distributed/internal/queryservice"
|
2021-01-15 07:28:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
2021-02-05 09:57:41 +00:00
|
|
|
grpcServer *grpc.Server
|
|
|
|
grpcError error
|
|
|
|
grpcErrMux sync.Mutex
|
2021-01-22 06:28:06 +00:00
|
|
|
|
|
|
|
loopCtx context.Context
|
2021-02-05 09:57:41 +00:00
|
|
|
loopCancel context.CancelFunc
|
|
|
|
|
|
|
|
queryService *qs.QueryService
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewServer(ctx context.Context) (*Server, error) {
|
|
|
|
ctx1, cancel := context.WithCancel(ctx)
|
|
|
|
service, err := qs.NewQueryService(ctx1)
|
|
|
|
if err != nil {
|
|
|
|
cancel()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Server{
|
|
|
|
queryService: service,
|
|
|
|
loopCtx: ctx1,
|
|
|
|
loopCancel: cancel,
|
|
|
|
}, nil
|
2021-01-16 07:31:10 +00:00
|
|
|
}
|
|
|
|
|
2021-01-21 02:01:29 +00:00
|
|
|
func (s *Server) Init() error {
|
2021-01-26 07:13:20 +00:00
|
|
|
log.Println("query service init")
|
2021-01-27 01:50:52 +00:00
|
|
|
if err := s.queryService.Init(); err != nil {
|
2021-02-04 09:47:19 +00:00
|
|
|
return err
|
2021-01-27 01:50:52 +00:00
|
|
|
}
|
2021-01-26 07:13:20 +00:00
|
|
|
s.queryService.SetEnableGrpc(true)
|
2021-01-22 06:28:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-21 02:01:29 +00:00
|
|
|
func (s *Server) Start() error {
|
2021-02-05 09:57:41 +00:00
|
|
|
log.Println("start query service ...")
|
|
|
|
|
|
|
|
s.grpcServer = grpc.NewServer()
|
|
|
|
querypb.RegisterQueryServiceServer(s.grpcServer, s)
|
|
|
|
log.Println("Starting start query service Server")
|
|
|
|
lis, err := net.Listen("tcp", ":"+strconv.Itoa(qs.Params.Port))
|
2021-01-26 07:13:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-05 09:57:41 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err := s.grpcServer.Serve(lis); err != nil {
|
|
|
|
s.grpcErrMux.Lock()
|
|
|
|
defer s.grpcErrMux.Unlock()
|
|
|
|
s.grpcError = err
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
s.grpcErrMux.Lock()
|
|
|
|
err = s.grpcError
|
|
|
|
s.grpcErrMux.Unlock()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-22 06:28:06 +00:00
|
|
|
s.queryService.Start()
|
|
|
|
return nil
|
2021-01-16 07:31:10 +00:00
|
|
|
}
|
|
|
|
|
2021-01-21 02:01:29 +00:00
|
|
|
func (s *Server) Stop() error {
|
2021-02-05 09:57:41 +00:00
|
|
|
err := s.queryService.Stop()
|
2021-01-22 06:28:06 +00:00
|
|
|
s.loopCancel()
|
|
|
|
if s.grpcServer != nil {
|
|
|
|
s.grpcServer.GracefulStop()
|
|
|
|
}
|
2021-02-05 09:57:41 +00:00
|
|
|
return err
|
2021-01-22 06:28:06 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 07:13:20 +00:00
|
|
|
func (s *Server) GetComponentStates(ctx context.Context, req *commonpb.Empty) (*internalpb2.ComponentStates, error) {
|
2021-01-22 06:28:06 +00:00
|
|
|
componentStates, err := s.queryService.GetComponentStates()
|
|
|
|
if err != nil {
|
2021-01-26 07:13:20 +00:00
|
|
|
return &internalpb2.ComponentStates{
|
2021-01-22 06:28:06 +00:00
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
|
|
|
|
Reason: err.Error(),
|
|
|
|
},
|
|
|
|
}, err
|
|
|
|
}
|
|
|
|
|
2021-01-26 07:13:20 +00:00
|
|
|
return componentStates, nil
|
2021-01-16 07:31:10 +00:00
|
|
|
}
|
|
|
|
|
2021-01-22 06:28:06 +00:00
|
|
|
func (s *Server) GetTimeTickChannel(ctx context.Context, req *commonpb.Empty) (*milvuspb.StringResponse, error) {
|
|
|
|
channel, err := s.queryService.GetTimeTickChannel()
|
|
|
|
if err != nil {
|
|
|
|
return &milvuspb.StringResponse{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
|
|
|
|
Reason: err.Error(),
|
|
|
|
},
|
|
|
|
}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &milvuspb.StringResponse{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_SUCCESS,
|
|
|
|
Reason: "",
|
|
|
|
},
|
|
|
|
Value: channel,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) GetStatisticsChannel(ctx context.Context, req *commonpb.Empty) (*milvuspb.StringResponse, error) {
|
|
|
|
statisticsChannel, err := s.queryService.GetStatisticsChannel()
|
|
|
|
if err != nil {
|
|
|
|
return &milvuspb.StringResponse{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_UNEXPECTED_ERROR,
|
|
|
|
Reason: err.Error(),
|
|
|
|
},
|
|
|
|
}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &milvuspb.StringResponse{
|
|
|
|
Status: &commonpb.Status{
|
|
|
|
ErrorCode: commonpb.ErrorCode_SUCCESS,
|
|
|
|
Reason: "",
|
|
|
|
},
|
|
|
|
Value: statisticsChannel,
|
|
|
|
}, nil
|
2021-01-16 07:31:10 +00:00
|
|
|
}
|
|
|
|
|
2021-02-05 09:57:41 +00:00
|
|
|
func (s *Server) SetMasterService(m qs.MasterServiceInterface) error {
|
2021-02-02 01:52:42 +00:00
|
|
|
s.queryService.SetMasterService(m)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-05 09:57:41 +00:00
|
|
|
func (s *Server) SetDataService(d qs.DataServiceInterface) error {
|
2021-02-02 01:52:42 +00:00
|
|
|
s.queryService.SetDataService(d)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-22 06:28:06 +00:00
|
|
|
func (s *Server) RegisterNode(ctx context.Context, req *querypb.RegisterNodeRequest) (*querypb.RegisterNodeResponse, error) {
|
|
|
|
return s.queryService.RegisterNode(req)
|
2021-01-16 07:31:10 +00:00
|
|
|
}
|
|
|
|
|
2021-01-22 06:28:06 +00:00
|
|
|
func (s *Server) ShowCollections(ctx context.Context, req *querypb.ShowCollectionRequest) (*querypb.ShowCollectionResponse, error) {
|
|
|
|
return s.queryService.ShowCollections(req)
|
2021-01-15 07:28:54 +00:00
|
|
|
}
|
|
|
|
|
2021-01-22 06:28:06 +00:00
|
|
|
func (s *Server) LoadCollection(ctx context.Context, req *querypb.LoadCollectionRequest) (*commonpb.Status, error) {
|
|
|
|
return s.queryService.LoadCollection(req)
|
2021-01-15 07:28:54 +00:00
|
|
|
}
|
|
|
|
|
2021-01-22 06:28:06 +00:00
|
|
|
func (s *Server) ReleaseCollection(ctx context.Context, req *querypb.ReleaseCollectionRequest) (*commonpb.Status, error) {
|
|
|
|
return s.queryService.ReleaseCollection(req)
|
2021-01-15 07:28:54 +00:00
|
|
|
}
|
|
|
|
|
2021-01-22 06:28:06 +00:00
|
|
|
func (s *Server) ShowPartitions(ctx context.Context, req *querypb.ShowPartitionRequest) (*querypb.ShowPartitionResponse, error) {
|
|
|
|
return s.queryService.ShowPartitions(req)
|
2021-01-15 07:28:54 +00:00
|
|
|
}
|
|
|
|
|
2021-01-22 06:28:06 +00:00
|
|
|
func (s *Server) GetPartitionStates(ctx context.Context, req *querypb.PartitionStatesRequest) (*querypb.PartitionStatesResponse, error) {
|
|
|
|
return s.queryService.GetPartitionStates(req)
|
2021-01-15 07:28:54 +00:00
|
|
|
}
|
|
|
|
|
2021-01-22 06:28:06 +00:00
|
|
|
func (s *Server) LoadPartitions(ctx context.Context, req *querypb.LoadPartitionRequest) (*commonpb.Status, error) {
|
|
|
|
return s.queryService.LoadPartitions(req)
|
2021-01-15 07:28:54 +00:00
|
|
|
}
|
|
|
|
|
2021-01-22 06:28:06 +00:00
|
|
|
func (s *Server) ReleasePartitions(ctx context.Context, req *querypb.ReleasePartitionRequest) (*commonpb.Status, error) {
|
|
|
|
return s.queryService.ReleasePartitions(req)
|
2021-01-15 07:28:54 +00:00
|
|
|
}
|
|
|
|
|
2021-01-22 06:28:06 +00:00
|
|
|
func (s *Server) CreateQueryChannel(ctx context.Context, req *commonpb.Empty) (*querypb.CreateQueryChannelResponse, error) {
|
|
|
|
return s.queryService.CreateQueryChannel()
|
2021-01-15 07:28:54 +00:00
|
|
|
}
|
|
|
|
|
2021-02-04 03:40:14 +00:00
|
|
|
func (s *Server) GetSegmentInfo(ctx context.Context, req *querypb.SegmentInfoRequest) (*querypb.SegmentInfoResponse, error) {
|
|
|
|
return s.queryService.GetSegmentInfo(req)
|
|
|
|
}
|