2021-04-19 07:16:33 +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-02-23 03:40:30 +00:00
|
|
|
package grpcdatanodeclient
|
2021-01-19 03:37:16 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-06-29 06:30:17 +00:00
|
|
|
"errors"
|
2021-05-25 07:47:08 +00:00
|
|
|
"fmt"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
grpc datapb.DataNodeClient
|
|
|
|
conn *grpc.ClientConn
|
|
|
|
|
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-01-24 13:20:11 +00:00
|
|
|
}
|
2021-01-22 01:36:40 +00:00
|
|
|
|
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-05-25 07:47:08 +00:00
|
|
|
return &Client{
|
2021-06-23 01:24:10 +00:00
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
addr: addr,
|
|
|
|
retryOptions: retryOptions,
|
2021-05-25 07:47:08 +00:00
|
|
|
}, nil
|
2021-01-19 03:37:16 +00:00
|
|
|
}
|
|
|
|
|
2021-01-21 02:01:29 +00:00
|
|
|
func (c *Client) Init() error {
|
2021-08-04 05:03:24 +00:00
|
|
|
Params.Init()
|
2021-06-25 07:50:23 +00:00
|
|
|
return c.connect(retry.Attempts(20))
|
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-07-01 07:24:17 +00:00
|
|
|
conn, err := grpc.DialContext(c.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-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-06-23 01:24:10 +00:00
|
|
|
err = c.connect()
|
2021-06-22 01:46:06 +00:00
|
|
|
if err != nil {
|
2021-06-29 06:30:17 +00:00
|
|
|
return ret, errors.New("Connect to datanode failed with error:\n" + err.Error())
|
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-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-01-21 02:01:29 +00:00
|
|
|
func (c *Client) Stop() error {
|
2021-06-23 01:24:10 +00:00
|
|
|
c.cancel()
|
2021-01-24 13:20:11 +00:00
|
|
|
return c.conn.Close()
|
2021-01-19 03:37:16 +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-25 07:47:08 +00:00
|
|
|
ret, err := c.recall(func() (interface{}, error) {
|
|
|
|
return c.grpc.GetComponentStates(ctx, &internalpb.GetComponentStatesRequest{})
|
|
|
|
})
|
|
|
|
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) {
|
|
|
|
return c.grpc.GetStatisticsChannel(ctx, &internalpb.GetStatisticsChannelRequest{})
|
|
|
|
})
|
|
|
|
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) {
|
|
|
|
return c.grpc.WatchDmChannels(ctx, req)
|
|
|
|
})
|
|
|
|
return ret.(*commonpb.Status), err
|
2021-01-19 03:37:16 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
return c.grpc.FlushSegments(ctx, req)
|
|
|
|
})
|
|
|
|
return ret.(*commonpb.Status), err
|
2021-01-19 03:37:16 +00:00
|
|
|
}
|