2021-10-19 08:51:32 +00:00
|
|
|
// Licensed to the LF AI & Data foundation under one
|
|
|
|
// or more contributor license agreements. See the NOTICE file
|
|
|
|
// distributed with this work for additional information
|
|
|
|
// regarding copyright ownership. The ASF licenses this file
|
|
|
|
// to you under the Apache License, Version 2.0 (the
|
|
|
|
// "License"); you may not use this file except in compliance
|
2021-04-19 07:16:33 +00:00
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
2021-10-19 08:51:32 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2021-04-19 07:16:33 +00:00
|
|
|
//
|
2021-10-19 08:51:32 +00:00
|
|
|
// 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-04-19 07:16:33 +00:00
|
|
|
|
2021-02-23 03:40:30 +00:00
|
|
|
package grpcdatanodeclient
|
2021-01-19 03:37:16 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-05-25 07:47:08 +00:00
|
|
|
"fmt"
|
2021-09-17 11:45:42 +00:00
|
|
|
"sync"
|
2021-08-20 09:52:11 +00:00
|
|
|
"time"
|
2021-01-19 03:37:16 +00:00
|
|
|
|
2021-04-22 06:45:57 +00:00
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/retry"
|
2021-03-05 12:41:34 +00:00
|
|
|
|
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-04-22 06:45:57 +00: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-06-17 06:17:56 +00:00
|
|
|
"github.com/milvus-io/milvus/internal/util/trace"
|
2021-07-01 07:24:17 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
2021-01-24 13:20:11 +00:00
|
|
|
|
2021-02-26 02:13:36 +00:00
|
|
|
"go.uber.org/zap"
|
2021-01-24 13:20:11 +00:00
|
|
|
"google.golang.org/grpc"
|
2021-01-19 03:37:16 +00:00
|
|
|
)
|
|
|
|
|
2021-10-12 12:30:49 +00:00
|
|
|
// Client is the grpc client for DataNode
|
2021-01-19 03:37:16 +00:00
|
|
|
type Client struct {
|
2021-06-23 01:24:10 +00:00
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
2021-05-25 07:47:08 +00:00
|
|
|
|
2021-09-17 11:45:42 +00:00
|
|
|
grpc datapb.DataNodeClient
|
|
|
|
conn *grpc.ClientConn
|
|
|
|
grpcMtx sync.RWMutex
|
2021-05-25 07:47:08 +00:00
|
|
|
|
2021-06-03 11:01:33 +00:00
|
|
|
addr string
|
2021-05-25 07:47:08 +00:00
|
|
|
|
2021-06-23 01:24:10 +00:00
|
|
|
retryOptions []retry.Option
|
2021-09-27 11:00:22 +00:00
|
|
|
|
|
|
|
getGrpcClient func() (datapb.DataNodeClient, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) setGetGrpcClientFunc() {
|
|
|
|
c.getGrpcClient = c.getGrpcClientFunc
|
2021-01-24 13:20:11 +00:00
|
|
|
}
|
2021-01-22 01:36:40 +00:00
|
|
|
|
2021-09-27 11:00:22 +00:00
|
|
|
func (c *Client) getGrpcClientFunc() (datapb.DataNodeClient, error) {
|
2021-09-17 11:45:42 +00:00
|
|
|
c.grpcMtx.RLock()
|
|
|
|
if c.grpc != nil {
|
|
|
|
defer c.grpcMtx.RUnlock()
|
|
|
|
return c.grpc, nil
|
|
|
|
}
|
|
|
|
c.grpcMtx.RUnlock()
|
|
|
|
|
|
|
|
c.grpcMtx.Lock()
|
|
|
|
defer c.grpcMtx.Unlock()
|
|
|
|
|
|
|
|
if c.grpc != nil {
|
|
|
|
return c.grpc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME(dragondriver): how to handle error here?
|
|
|
|
// if we return nil here, then we should check if client is nil outside,
|
|
|
|
err := c.connect(retry.Attempts(20))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.grpc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) resetConnection() {
|
|
|
|
c.grpcMtx.Lock()
|
|
|
|
defer c.grpcMtx.Unlock()
|
|
|
|
|
|
|
|
if c.conn != nil {
|
|
|
|
_ = c.conn.Close()
|
|
|
|
}
|
|
|
|
c.conn = nil
|
|
|
|
c.grpc = nil
|
|
|
|
}
|
|
|
|
|
2021-11-03 15:57:27 +00:00
|
|
|
// NewClient creates a client for DataNode.
|
2021-06-23 03:48:06 +00:00
|
|
|
func NewClient(ctx context.Context, addr string, retryOptions ...retry.Option) (*Client, error) {
|
2021-06-03 11:01:33 +00:00
|
|
|
if addr == "" {
|
2021-05-26 10:41:37 +00:00
|
|
|
return nil, fmt.Errorf("address is empty")
|
2021-05-25 07:47:08 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 01:24:10 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2021-09-27 11:00:22 +00:00
|
|
|
client := &Client{
|
2021-06-23 01:24:10 +00:00
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
addr: addr,
|
|
|
|
retryOptions: retryOptions,
|
2021-09-27 11:00:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
client.setGetGrpcClientFunc()
|
|
|
|
return client, nil
|
2021-01-19 03:37:16 +00:00
|
|
|
}
|
|
|
|
|
2021-11-03 15:57:27 +00:00
|
|
|
// Init initializes the client.
|
2021-01-21 02:01:29 +00:00
|
|
|
func (c *Client) Init() error {
|
2021-08-04 05:03:24 +00:00
|
|
|
Params.Init()
|
2021-09-27 11:00:22 +00:00
|
|
|
return nil
|
2021-05-25 07:47:08 +00:00
|
|
|
}
|
|
|
|
|
2021-06-24 11:05:06 +00:00
|
|
|
func (c *Client) connect(retryOptions ...retry.Option) error {
|
2021-02-23 03:40:30 +00:00
|
|
|
connectGrpcFunc := func() error {
|
2021-06-17 06:17:56 +00:00
|
|
|
opts := trace.GetInterceptorOpts()
|
|
|
|
log.Debug("DataNode connect ", zap.String("address", c.addr))
|
2021-08-20 09:52:11 +00:00
|
|
|
ctx, cancel := context.WithTimeout(c.ctx, 15*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
conn, err := grpc.DialContext(ctx, c.addr,
|
2021-08-06 06:09:26 +00:00
|
|
|
grpc.WithInsecure(), grpc.WithBlock(),
|
2021-08-04 05:03:24 +00:00
|
|
|
grpc.WithDefaultCallOptions(
|
|
|
|
grpc.MaxCallRecvMsgSize(Params.ClientMaxRecvSize),
|
|
|
|
grpc.MaxCallSendMsgSize(Params.ClientMaxSendSize)),
|
2021-06-29 01:30:17 +00:00
|
|
|
grpc.WithDisableRetry(),
|
2021-02-26 09:44:24 +00:00
|
|
|
grpc.WithUnaryInterceptor(
|
2021-06-22 01:46:06 +00:00
|
|
|
grpc_middleware.ChainUnaryClient(
|
2021-07-01 07:24:17 +00:00
|
|
|
grpc_retry.UnaryClientInterceptor(
|
|
|
|
grpc_retry.WithMax(3),
|
|
|
|
grpc_retry.WithCodes(codes.Aborted, codes.Unavailable),
|
|
|
|
),
|
2021-06-22 01:46:06 +00:00
|
|
|
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(
|
2021-07-01 07:24:17 +00:00
|
|
|
grpc_retry.StreamClientInterceptor(
|
|
|
|
grpc_retry.WithMax(3),
|
|
|
|
grpc_retry.WithCodes(codes.Aborted, codes.Unavailable),
|
|
|
|
),
|
2021-06-22 01:46:06 +00:00
|
|
|
grpc_opentracing.StreamClientInterceptor(opts...),
|
|
|
|
)),
|
|
|
|
)
|
2021-02-23 03:40:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-01-24 13:20:11 +00:00
|
|
|
}
|
2021-09-17 11:45:42 +00:00
|
|
|
if c.conn != nil {
|
|
|
|
_ = c.conn.Close()
|
|
|
|
}
|
2021-02-23 03:40:30 +00:00
|
|
|
c.conn = conn
|
|
|
|
return nil
|
2021-01-24 13:20:11 +00:00
|
|
|
}
|
2021-02-23 03:40:30 +00:00
|
|
|
|
2021-06-24 11:05:06 +00:00
|
|
|
err := retry.Do(c.ctx, connectGrpcFunc, retryOptions...)
|
2021-01-24 13:20:11 +00:00
|
|
|
if err != nil {
|
2021-06-11 01:50:34 +00:00
|
|
|
log.Debug("DataNodeClient try connect failed", zap.Error(err))
|
2021-01-24 13:20:11 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-06-11 01:50:34 +00:00
|
|
|
log.Debug("DataNodeClient connect success")
|
2021-01-24 13:20:11 +00:00
|
|
|
c.grpc = datapb.NewDataNodeClient(c.conn)
|
|
|
|
return nil
|
2021-01-19 03:37:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-25 07:47:08 +00:00
|
|
|
func (c *Client) recall(caller func() (interface{}, error)) (interface{}, error) {
|
|
|
|
ret, err := caller()
|
|
|
|
if err == nil {
|
|
|
|
return ret, nil
|
|
|
|
}
|
2021-07-10 02:21:52 +00:00
|
|
|
log.Debug("DataNode Client grpc error", zap.Error(err))
|
2021-09-17 11:45:42 +00:00
|
|
|
|
|
|
|
c.resetConnection()
|
|
|
|
|
2021-06-22 01:46:06 +00:00
|
|
|
ret, err = caller()
|
|
|
|
if err == nil {
|
|
|
|
return ret, nil
|
|
|
|
}
|
2021-05-25 07:47:08 +00:00
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
2021-11-03 15:57:27 +00:00
|
|
|
// Start starts the client.
|
|
|
|
// Currently, it does nothing.
|
2021-01-21 02:01:29 +00:00
|
|
|
func (c *Client) Start() error {
|
2021-01-24 13:20:11 +00:00
|
|
|
return nil
|
2021-01-19 03:37:16 +00:00
|
|
|
}
|
|
|
|
|
2021-11-03 15:57:27 +00:00
|
|
|
// Stop stops the client.
|
|
|
|
// Currently, it closes the grpc connection with the DataNode.
|
2021-01-21 02:01:29 +00:00
|
|
|
func (c *Client) Stop() error {
|
2021-06-23 01:24:10 +00:00
|
|
|
c.cancel()
|
2021-09-17 11:45:42 +00:00
|
|
|
c.grpcMtx.Lock()
|
|
|
|
defer c.grpcMtx.Unlock()
|
|
|
|
if c.conn != nil {
|
|
|
|
return c.conn.Close()
|
|
|
|
}
|
|
|
|
return nil
|
2021-01-19 03:37:16 +00:00
|
|
|
}
|
|
|
|
|
2021-11-03 15:57:27 +00:00
|
|
|
// Register does nothing.
|
2021-05-25 07:06:05 +00:00
|
|
|
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-25 07:47:08 +00:00
|
|
|
ret, err := c.recall(func() (interface{}, error) {
|
2021-09-17 11:45:42 +00:00
|
|
|
client, err := c.getGrpcClient()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return client.GetComponentStates(ctx, &internalpb.GetComponentStatesRequest{})
|
2021-05-25 07:47:08 +00:00
|
|
|
})
|
2021-09-17 11:45:42 +00:00
|
|
|
if err != nil || ret == nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-25 07:47:08 +00:00
|
|
|
return ret.(*internalpb.ComponentStates), err
|
2021-03-05 12:41:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetStatisticsChannel(ctx context.Context) (*milvuspb.StringResponse, error) {
|
2021-05-25 07:47:08 +00:00
|
|
|
ret, err := c.recall(func() (interface{}, error) {
|
2021-09-17 11:45:42 +00:00
|
|
|
client, err := c.getGrpcClient()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return client.GetStatisticsChannel(ctx, &internalpb.GetStatisticsChannelRequest{})
|
2021-05-25 07:47:08 +00:00
|
|
|
})
|
2021-09-17 11:45:42 +00:00
|
|
|
if err != nil || ret == nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-25 07:47:08 +00:00
|
|
|
return ret.(*milvuspb.StringResponse), err
|
2021-01-22 01:36:40 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 06:22:09 +00:00
|
|
|
func (c *Client) WatchDmChannels(ctx context.Context, req *datapb.WatchDmChannelsRequest) (*commonpb.Status, error) {
|
2021-05-25 07:47:08 +00:00
|
|
|
ret, err := c.recall(func() (interface{}, error) {
|
2021-09-17 11:45:42 +00:00
|
|
|
client, err := c.getGrpcClient()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return client.WatchDmChannels(ctx, req)
|
2021-05-25 07:47:08 +00:00
|
|
|
})
|
2021-09-17 11:45:42 +00:00
|
|
|
if err != nil || ret == nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-25 07:47:08 +00:00
|
|
|
return ret.(*commonpb.Status), err
|
2021-01-19 03:37:16 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 12:32:43 +00:00
|
|
|
// FlushSegments notifies DataNode to flush the segments req provids. The flush tasks are async to this
|
|
|
|
// rpc, DataNode will flush the segments in the background.
|
|
|
|
//
|
|
|
|
// Return UnexpectedError code in status:
|
|
|
|
// If DataNode isn't in HEALTHY: states not HEALTHY or dynamic checks not HEALTHY
|
|
|
|
// If DataNode doesn't find the correspounding segmentID in its memeory replica
|
|
|
|
// Return Success code in status and trigers background flush:
|
|
|
|
// Log an info log if a segment is under flushing
|
2021-03-12 06:22:09 +00:00
|
|
|
func (c *Client) FlushSegments(ctx context.Context, req *datapb.FlushSegmentsRequest) (*commonpb.Status, error) {
|
2021-05-25 07:47:08 +00:00
|
|
|
ret, err := c.recall(func() (interface{}, error) {
|
2021-09-17 11:45:42 +00:00
|
|
|
client, err := c.getGrpcClient()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return client.FlushSegments(ctx, req)
|
2021-05-25 07:47:08 +00:00
|
|
|
})
|
2021-09-17 11:45:42 +00:00
|
|
|
if err != nil || ret == nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-25 07:47:08 +00:00
|
|
|
return ret.(*commonpb.Status), err
|
2021-01-19 03:37:16 +00:00
|
|
|
}
|
2021-09-01 02:13:15 +00:00
|
|
|
|
|
|
|
func (c *Client) GetMetrics(ctx context.Context, req *milvuspb.GetMetricsRequest) (*milvuspb.GetMetricsResponse, error) {
|
|
|
|
ret, err := c.recall(func() (interface{}, error) {
|
2021-09-17 11:45:42 +00:00
|
|
|
client, err := c.getGrpcClient()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return client.GetMetrics(ctx, req)
|
2021-09-01 02:13:15 +00:00
|
|
|
})
|
2021-09-17 11:45:42 +00:00
|
|
|
if err != nil || ret == nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-01 02:13:15 +00:00
|
|
|
return ret.(*milvuspb.GetMetricsResponse), err
|
|
|
|
}
|