2021-04-19 05:47:10 +00: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 08:44:09 +00:00
|
|
|
package grpcquerycoordclient
|
2021-01-22 06:28:06 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-05-27 10:44:36 +00:00
|
|
|
"fmt"
|
2021-01-22 06:28:06 +00:00
|
|
|
"time"
|
|
|
|
|
2021-06-22 01:46:06 +00:00
|
|
|
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
|
|
|
|
grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry"
|
2021-06-17 06:17:56 +00:00
|
|
|
grpc_opentracing "github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing"
|
2021-05-27 10:44:36 +00:00
|
|
|
"github.com/milvus-io/milvus/internal/util/retry"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/sessionutil"
|
2021-06-17 06:17:56 +00:00
|
|
|
"github.com/milvus-io/milvus/internal/util/trace"
|
2021-05-27 10:44:36 +00:00
|
|
|
"github.com/milvus-io/milvus/internal/util/typeutil"
|
2021-02-27 02:11:52 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2021-04-22 06:45:57 +00:00
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
|
|
|
"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-22 06:28:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Client struct {
|
2021-06-23 01:24:10 +00:00
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
|
2021-06-22 08:44:09 +00:00
|
|
|
grpcClient querypb.QueryCoordClient
|
2021-02-02 01:52:42 +00:00
|
|
|
conn *grpc.ClientConn
|
|
|
|
|
2021-06-23 01:24:10 +00:00
|
|
|
sess *sessionutil.Session
|
|
|
|
addr string
|
|
|
|
|
|
|
|
retryOptions []retry.Option
|
2021-02-02 01:52:42 +00:00
|
|
|
}
|
|
|
|
|
2021-06-22 08:44:09 +00:00
|
|
|
func getQueryCoordAddress(sess *sessionutil.Session) (string, error) {
|
|
|
|
key := typeutil.QueryCoordRole
|
2021-05-27 10:44:36 +00:00
|
|
|
msess, _, err := sess.GetSessions(key)
|
|
|
|
if err != nil {
|
2021-06-22 08:44:09 +00:00
|
|
|
log.Debug("QueryCoordClient GetSessions failed", zap.Error(err))
|
2021-05-27 10:44:36 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
ms, ok := msess[key]
|
|
|
|
if !ok {
|
2021-06-22 08:44:09 +00:00
|
|
|
log.Debug("QueryCoordClient msess key not existed", zap.Any("key", key))
|
|
|
|
return "", fmt.Errorf("number of querycoord is incorrect, %d", len(msess))
|
2021-05-27 10:44:36 +00:00
|
|
|
}
|
|
|
|
return ms.Address, nil
|
|
|
|
}
|
|
|
|
|
2021-06-04 08:29:35 +00:00
|
|
|
// NewClient creates a client for QueryService grpc call.
|
2021-06-23 01:24:10 +00:00
|
|
|
func NewClient(ctx context.Context, metaRoot string, etcdEndpoints []string, retryOptions ...retry.Option) (*Client, error) {
|
|
|
|
sess := sessionutil.NewSession(ctx, metaRoot, etcdEndpoints)
|
|
|
|
if sess == nil {
|
|
|
|
err := fmt.Errorf("new session error, maybe can not connect to etcd")
|
|
|
|
log.Debug("QueryCoordClient NewClient failed", zap.Error(err))
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2021-02-02 01:52:42 +00:00
|
|
|
return &Client{
|
2021-06-23 01:24:10 +00:00
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
sess: sess,
|
|
|
|
retryOptions: retryOptions,
|
2021-02-02 01:52:42 +00:00
|
|
|
}, nil
|
2021-01-22 06:28:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Init() error {
|
2021-06-23 01:24:10 +00:00
|
|
|
return c.connect()
|
2021-05-27 10:44:36 +00:00
|
|
|
}
|
2021-06-11 01:50:34 +00:00
|
|
|
|
2021-06-03 11:01:33 +00:00
|
|
|
func (c *Client) connect() error {
|
2021-02-02 01:52:42 +00:00
|
|
|
var err error
|
2021-06-22 08:44:09 +00:00
|
|
|
getQueryCoordAddressFn := func() error {
|
|
|
|
c.addr, err = getQueryCoordAddress(c.sess)
|
2021-05-27 10:44:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-06-23 01:24:10 +00:00
|
|
|
err = retry.Do(c.ctx, getQueryCoordAddressFn, c.retryOptions...)
|
2021-05-27 10:44:36 +00:00
|
|
|
if err != nil {
|
2021-06-22 08:44:09 +00:00
|
|
|
log.Debug("QueryCoordClient getQueryCoordAddress failed", zap.Error(err))
|
2021-05-27 10:44:36 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
connectGrpcFunc := func() error {
|
2021-06-17 06:17:56 +00:00
|
|
|
opts := trace.GetInterceptorOpts()
|
2021-06-22 08:44:09 +00:00
|
|
|
log.Debug("QueryCoordClient try reconnect ", zap.String("address", c.addr))
|
2021-06-23 01:24:10 +00:00
|
|
|
conn, err := grpc.DialContext(c.ctx, c.addr,
|
|
|
|
grpc.WithInsecure(), grpc.WithBlock(), grpc.WithTimeout(5*time.Second),
|
2021-02-26 09:44:24 +00:00
|
|
|
grpc.WithUnaryInterceptor(
|
2021-06-22 01:46:06 +00:00
|
|
|
grpc_middleware.ChainUnaryClient(
|
|
|
|
grpc_retry.UnaryClientInterceptor(),
|
|
|
|
grpc_opentracing.UnaryClientInterceptor(opts...),
|
|
|
|
)),
|
2021-02-26 09:44:24 +00:00
|
|
|
grpc.WithStreamInterceptor(
|
2021-06-22 01:46:06 +00:00
|
|
|
grpc_middleware.ChainStreamClient(
|
|
|
|
grpc_retry.StreamClientInterceptor(),
|
|
|
|
grpc_opentracing.StreamClientInterceptor(opts...),
|
|
|
|
)),
|
|
|
|
)
|
2021-05-27 10:44:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-02-02 01:52:42 +00:00
|
|
|
}
|
2021-05-27 10:44:36 +00:00
|
|
|
c.conn = conn
|
|
|
|
return nil
|
2021-02-02 01:52:42 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 01:24:10 +00:00
|
|
|
err = retry.Do(c.ctx, connectGrpcFunc, c.retryOptions...)
|
2021-02-02 01:52:42 +00:00
|
|
|
if err != nil {
|
2021-06-22 08:44:09 +00:00
|
|
|
log.Debug("QueryCoordClient try reconnect failed", zap.Error(err))
|
2021-02-02 01:52:42 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-06-22 08:44:09 +00:00
|
|
|
log.Debug("QueryCoordClient try reconnect success")
|
|
|
|
c.grpcClient = querypb.NewQueryCoordClient(c.conn)
|
2021-02-02 01:52:42 +00:00
|
|
|
return nil
|
2021-01-22 06:28:06 +00:00
|
|
|
}
|
2021-06-23 01:24:10 +00:00
|
|
|
|
2021-05-27 10:44:36 +00:00
|
|
|
func (c *Client) recall(caller func() (interface{}, error)) (interface{}, error) {
|
|
|
|
ret, err := caller()
|
|
|
|
if err == nil {
|
|
|
|
return ret, nil
|
|
|
|
}
|
2021-06-23 01:24:10 +00:00
|
|
|
err = c.connect()
|
2021-06-22 01:46:06 +00:00
|
|
|
if err != nil {
|
2021-06-23 01:24:10 +00:00
|
|
|
return ret, err
|
2021-06-22 01:46:06 +00:00
|
|
|
}
|
|
|
|
ret, err = caller()
|
|
|
|
if err == nil {
|
|
|
|
return ret, nil
|
|
|
|
}
|
2021-05-27 10:44:36 +00:00
|
|
|
return ret, err
|
|
|
|
}
|
2021-01-22 06:28:06 +00:00
|
|
|
|
|
|
|
func (c *Client) Start() error {
|
2021-02-02 01:52:42 +00:00
|
|
|
return nil
|
2021-01-22 06:28:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Stop() error {
|
2021-06-23 01:24:10 +00:00
|
|
|
c.cancel()
|
2021-02-02 01:52:42 +00:00
|
|
|
return c.conn.Close()
|
2021-01-22 06:28:06 +00:00
|
|
|
}
|
|
|
|
|
2021-05-25 07:06:05 +00:00
|
|
|
// Register dummy
|
|
|
|
func (c *Client) Register() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-12 06:22:09 +00:00
|
|
|
func (c *Client) GetComponentStates(ctx context.Context) (*internalpb.ComponentStates, error) {
|
2021-05-27 10:44:36 +00:00
|
|
|
ret, err := c.recall(func() (interface{}, error) {
|
|
|
|
return c.grpcClient.GetComponentStates(ctx, &internalpb.GetComponentStatesRequest{})
|
|
|
|
})
|
|
|
|
return ret.(*internalpb.ComponentStates), err
|
2021-01-22 06:28:06 +00:00
|
|
|
}
|
|
|
|
|
2021-02-26 09:44:24 +00:00
|
|
|
func (c *Client) GetTimeTickChannel(ctx context.Context) (*milvuspb.StringResponse, error) {
|
2021-05-27 10:44:36 +00:00
|
|
|
ret, err := c.recall(func() (interface{}, error) {
|
|
|
|
return c.grpcClient.GetTimeTickChannel(ctx, &internalpb.GetTimeTickChannelRequest{})
|
|
|
|
})
|
|
|
|
return ret.(*milvuspb.StringResponse), err
|
2021-01-22 06:28:06 +00:00
|
|
|
}
|
|
|
|
|
2021-02-26 09:44:24 +00:00
|
|
|
func (c *Client) GetStatisticsChannel(ctx context.Context) (*milvuspb.StringResponse, error) {
|
2021-05-27 10:44:36 +00:00
|
|
|
ret, err := c.recall(func() (interface{}, error) {
|
|
|
|
return c.grpcClient.GetStatisticsChannel(ctx, &internalpb.GetStatisticsChannelRequest{})
|
|
|
|
})
|
|
|
|
return ret.(*milvuspb.StringResponse), err
|
2021-01-22 06:28:06 +00:00
|
|
|
}
|
|
|
|
|
2021-02-26 09:44:24 +00:00
|
|
|
func (c *Client) RegisterNode(ctx context.Context, req *querypb.RegisterNodeRequest) (*querypb.RegisterNodeResponse, error) {
|
2021-05-27 10:44:36 +00:00
|
|
|
ret, err := c.recall(func() (interface{}, error) {
|
|
|
|
return c.grpcClient.RegisterNode(ctx, req)
|
|
|
|
})
|
|
|
|
return ret.(*querypb.RegisterNodeResponse), err
|
2021-01-22 06:28:06 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 06:22:09 +00:00
|
|
|
func (c *Client) ShowCollections(ctx context.Context, req *querypb.ShowCollectionsRequest) (*querypb.ShowCollectionsResponse, error) {
|
2021-05-27 10:44:36 +00:00
|
|
|
ret, err := c.recall(func() (interface{}, error) {
|
|
|
|
return c.grpcClient.ShowCollections(ctx, req)
|
|
|
|
})
|
|
|
|
return ret.(*querypb.ShowCollectionsResponse), err
|
2021-01-22 06:28:06 +00:00
|
|
|
}
|
|
|
|
|
2021-02-26 09:44:24 +00:00
|
|
|
func (c *Client) LoadCollection(ctx context.Context, req *querypb.LoadCollectionRequest) (*commonpb.Status, error) {
|
2021-05-27 10:44:36 +00:00
|
|
|
ret, err := c.recall(func() (interface{}, error) {
|
|
|
|
return c.grpcClient.LoadCollection(ctx, req)
|
|
|
|
})
|
|
|
|
return ret.(*commonpb.Status), err
|
2021-01-22 06:28:06 +00:00
|
|
|
}
|
|
|
|
|
2021-02-26 09:44:24 +00:00
|
|
|
func (c *Client) ReleaseCollection(ctx context.Context, req *querypb.ReleaseCollectionRequest) (*commonpb.Status, error) {
|
2021-05-27 10:44:36 +00:00
|
|
|
ret, err := c.recall(func() (interface{}, error) {
|
|
|
|
return c.grpcClient.ReleaseCollection(ctx, req)
|
|
|
|
})
|
|
|
|
return ret.(*commonpb.Status), err
|
2021-01-22 06:28:06 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 06:22:09 +00:00
|
|
|
func (c *Client) ShowPartitions(ctx context.Context, req *querypb.ShowPartitionsRequest) (*querypb.ShowPartitionsResponse, error) {
|
2021-05-27 10:44:36 +00:00
|
|
|
ret, err := c.recall(func() (interface{}, error) {
|
|
|
|
return c.grpcClient.ShowPartitions(ctx, req)
|
|
|
|
})
|
|
|
|
return ret.(*querypb.ShowPartitionsResponse), err
|
2021-01-22 06:28:06 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 06:22:09 +00:00
|
|
|
func (c *Client) LoadPartitions(ctx context.Context, req *querypb.LoadPartitionsRequest) (*commonpb.Status, error) {
|
2021-05-27 10:44:36 +00:00
|
|
|
ret, err := c.recall(func() (interface{}, error) {
|
|
|
|
return c.grpcClient.LoadPartitions(ctx, req)
|
|
|
|
})
|
|
|
|
return ret.(*commonpb.Status), err
|
2021-01-22 06:28:06 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 06:22:09 +00:00
|
|
|
func (c *Client) ReleasePartitions(ctx context.Context, req *querypb.ReleasePartitionsRequest) (*commonpb.Status, error) {
|
2021-05-27 10:44:36 +00:00
|
|
|
ret, err := c.recall(func() (interface{}, error) {
|
|
|
|
return c.grpcClient.ReleasePartitions(ctx, req)
|
|
|
|
})
|
|
|
|
return ret.(*commonpb.Status), err
|
2021-01-22 06:28:06 +00:00
|
|
|
}
|
|
|
|
|
2021-06-15 04:41:40 +00:00
|
|
|
func (c *Client) CreateQueryChannel(ctx context.Context, req *querypb.CreateQueryChannelRequest) (*querypb.CreateQueryChannelResponse, error) {
|
2021-05-27 10:44:36 +00:00
|
|
|
ret, err := c.recall(func() (interface{}, error) {
|
2021-06-15 04:41:40 +00:00
|
|
|
return c.grpcClient.CreateQueryChannel(ctx, req)
|
2021-05-27 10:44:36 +00:00
|
|
|
})
|
|
|
|
return ret.(*querypb.CreateQueryChannelResponse), err
|
2021-01-22 06:28:06 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 06:22:09 +00:00
|
|
|
func (c *Client) GetPartitionStates(ctx context.Context, req *querypb.GetPartitionStatesRequest) (*querypb.GetPartitionStatesResponse, error) {
|
2021-05-27 10:44:36 +00:00
|
|
|
ret, err := c.recall(func() (interface{}, error) {
|
|
|
|
return c.grpcClient.GetPartitionStates(ctx, req)
|
|
|
|
})
|
|
|
|
return ret.(*querypb.GetPartitionStatesResponse), err
|
2021-01-22 06:28:06 +00:00
|
|
|
}
|
2021-02-04 03:40:14 +00:00
|
|
|
|
2021-03-12 06:22:09 +00:00
|
|
|
func (c *Client) GetSegmentInfo(ctx context.Context, req *querypb.GetSegmentInfoRequest) (*querypb.GetSegmentInfoResponse, error) {
|
2021-05-27 10:44:36 +00:00
|
|
|
ret, err := c.recall(func() (interface{}, error) {
|
|
|
|
return c.grpcClient.GetSegmentInfo(ctx, req)
|
|
|
|
})
|
|
|
|
return ret.(*querypb.GetSegmentInfoResponse), err
|
2021-02-04 03:40:14 +00:00
|
|
|
}
|