2021-12-03 07:15:32 +00:00
|
|
|
package grpcclient
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
grpcmiddleware "github.com/grpc-ecosystem/go-grpc-middleware"
|
|
|
|
grpcretry "github.com/grpc-ecosystem/go-grpc-middleware/retry"
|
|
|
|
grpcopentracing "github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing"
|
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/funcutil"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/trace"
|
2021-12-17 02:33:15 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"google.golang.org/grpc"
|
2021-12-25 06:24:18 +00:00
|
|
|
"google.golang.org/grpc/backoff"
|
2021-12-17 02:33:15 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/keepalive"
|
2021-12-03 07:15:32 +00:00
|
|
|
)
|
|
|
|
|
2021-12-25 06:24:18 +00:00
|
|
|
const (
|
|
|
|
dialTimeout = 5 * time.Second
|
|
|
|
keepAliveTime = 10 * time.Second
|
|
|
|
keepAliveTimeout = 3 * time.Second
|
|
|
|
)
|
|
|
|
|
2021-12-06 11:05:55 +00:00
|
|
|
// GrpcClient abstracts client of grpc
|
2021-12-03 07:15:32 +00:00
|
|
|
type GrpcClient interface {
|
|
|
|
SetRole(string)
|
|
|
|
GetRole() string
|
|
|
|
SetGetAddrFunc(func() (string, error))
|
|
|
|
SetNewGrpcClientFunc(func(cc *grpc.ClientConn) interface{})
|
|
|
|
GetGrpcClient(ctx context.Context) (interface{}, error)
|
|
|
|
ReCall(ctx context.Context, caller func(client interface{}) (interface{}, error)) (interface{}, error)
|
|
|
|
Call(ctx context.Context, caller func(client interface{}) (interface{}, error)) (interface{}, error)
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:15:42 +00:00
|
|
|
// ClientBase is a base of grpc client
|
2021-12-03 07:15:32 +00:00
|
|
|
type ClientBase struct {
|
|
|
|
getAddrFunc func() (string, error)
|
|
|
|
newGrpcClient func(cc *grpc.ClientConn) interface{}
|
|
|
|
|
|
|
|
grpcClient interface{}
|
|
|
|
conn *grpc.ClientConn
|
|
|
|
grpcClientMtx sync.RWMutex
|
|
|
|
role string
|
|
|
|
ClientMaxSendSize int
|
|
|
|
ClientMaxRecvSize int
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:15:42 +00:00
|
|
|
// SetRole sets role of client
|
2021-12-03 07:15:32 +00:00
|
|
|
func (c *ClientBase) SetRole(role string) {
|
|
|
|
c.role = role
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:15:42 +00:00
|
|
|
// GetRole returns role of client
|
2021-12-03 07:15:32 +00:00
|
|
|
func (c *ClientBase) GetRole() string {
|
|
|
|
return c.role
|
|
|
|
}
|
|
|
|
|
2021-12-10 09:05:42 +00:00
|
|
|
// SetGetAddrFunc sets getAddrFunc of client
|
2021-12-03 07:15:32 +00:00
|
|
|
func (c *ClientBase) SetGetAddrFunc(f func() (string, error)) {
|
|
|
|
c.getAddrFunc = f
|
|
|
|
}
|
|
|
|
|
2021-12-10 09:05:42 +00:00
|
|
|
// SetNewGrpcClientFunc sets newGrpcClient of client
|
2021-12-03 07:15:32 +00:00
|
|
|
func (c *ClientBase) SetNewGrpcClientFunc(f func(cc *grpc.ClientConn) interface{}) {
|
|
|
|
c.newGrpcClient = f
|
|
|
|
}
|
|
|
|
|
2021-12-13 11:22:08 +00:00
|
|
|
// GetGrpcClient returns grpc client
|
2021-12-03 07:15:32 +00:00
|
|
|
func (c *ClientBase) GetGrpcClient(ctx context.Context) (interface{}, error) {
|
|
|
|
c.grpcClientMtx.RLock()
|
|
|
|
|
|
|
|
if c.grpcClient != nil {
|
|
|
|
defer c.grpcClientMtx.RUnlock()
|
|
|
|
return c.grpcClient, nil
|
|
|
|
}
|
|
|
|
c.grpcClientMtx.RUnlock()
|
|
|
|
|
|
|
|
c.grpcClientMtx.Lock()
|
|
|
|
defer c.grpcClientMtx.Unlock()
|
|
|
|
|
|
|
|
if c.grpcClient != nil {
|
|
|
|
return c.grpcClient, nil
|
|
|
|
}
|
|
|
|
|
2021-12-25 06:24:18 +00:00
|
|
|
err := c.connect(ctx)
|
2021-12-03 07:15:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.grpcClient, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientBase) resetConnection(client interface{}) {
|
|
|
|
c.grpcClientMtx.Lock()
|
|
|
|
defer c.grpcClientMtx.Unlock()
|
|
|
|
if c.grpcClient == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if client != c.grpcClient {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if c.conn != nil {
|
|
|
|
_ = c.conn.Close()
|
|
|
|
}
|
|
|
|
c.conn = nil
|
|
|
|
c.grpcClient = nil
|
|
|
|
}
|
|
|
|
|
2021-12-25 06:24:18 +00:00
|
|
|
func (c *ClientBase) connect(ctx context.Context) error {
|
|
|
|
addr, err := c.getAddrFunc()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("failed to get cclient address", zap.Error(err))
|
|
|
|
return err
|
2021-12-03 07:15:32 +00:00
|
|
|
}
|
|
|
|
|
2021-12-25 06:24:18 +00:00
|
|
|
opts := trace.GetInterceptorOpts()
|
|
|
|
dialContext, cancel := context.WithTimeout(ctx, dialTimeout)
|
|
|
|
conn, err := grpc.DialContext(
|
|
|
|
dialContext,
|
|
|
|
addr,
|
|
|
|
grpc.WithInsecure(),
|
|
|
|
grpc.WithBlock(),
|
|
|
|
grpc.WithDefaultCallOptions(
|
|
|
|
grpc.MaxCallRecvMsgSize(c.ClientMaxRecvSize),
|
|
|
|
grpc.MaxCallSendMsgSize(c.ClientMaxSendSize)),
|
|
|
|
grpc.WithUnaryInterceptor(
|
|
|
|
grpcmiddleware.ChainUnaryClient(
|
|
|
|
grpcretry.UnaryClientInterceptor(
|
|
|
|
grpcretry.WithMax(3),
|
|
|
|
grpcretry.WithCodes(codes.Aborted, codes.Unavailable),
|
|
|
|
),
|
|
|
|
grpcopentracing.UnaryClientInterceptor(opts...),
|
|
|
|
)),
|
|
|
|
grpc.WithStreamInterceptor(
|
|
|
|
grpcmiddleware.ChainStreamClient(
|
|
|
|
grpcretry.StreamClientInterceptor(grpcretry.WithMax(3),
|
|
|
|
grpcretry.WithCodes(codes.Aborted, codes.Unavailable),
|
|
|
|
),
|
|
|
|
grpcopentracing.StreamClientInterceptor(opts...),
|
|
|
|
)),
|
|
|
|
grpc.WithKeepaliveParams(keepalive.ClientParameters{
|
|
|
|
Time: keepAliveTime, // send pings every 60 seconds if there is no activity
|
|
|
|
Timeout: keepAliveTimeout, // wait 6 second for ping ack before considering the connection dead
|
|
|
|
PermitWithoutStream: true, // send pings even without active streams
|
|
|
|
}),
|
|
|
|
grpc.WithConnectParams(grpc.ConnectParams{
|
|
|
|
Backoff: backoff.Config{
|
|
|
|
BaseDelay: 100 * time.Millisecond,
|
|
|
|
Multiplier: 1.6,
|
|
|
|
Jitter: 0.2,
|
|
|
|
MaxDelay: 3 * time.Second,
|
|
|
|
},
|
|
|
|
MinConnectTimeout: dialTimeout,
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
cancel()
|
2021-12-03 07:15:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-25 06:24:18 +00:00
|
|
|
if c.conn != nil {
|
|
|
|
_ = c.conn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
c.conn = conn
|
2021-12-03 07:15:32 +00:00
|
|
|
c.grpcClient = c.newGrpcClient(c.conn)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientBase) callOnce(ctx context.Context, caller func(client interface{}) (interface{}, error)) (interface{}, error) {
|
|
|
|
client, err := c.GetGrpcClient(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ret, err2 := caller(client)
|
|
|
|
if err2 == nil {
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
if err2 == context.Canceled || err2 == context.DeadlineExceeded {
|
|
|
|
return nil, err2
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug(c.GetRole()+" ClientBase grpc error, start to reset connection", zap.Error(err2))
|
|
|
|
|
|
|
|
c.resetConnection(client)
|
|
|
|
return ret, err2
|
|
|
|
}
|
|
|
|
|
2021-12-13 11:24:40 +00:00
|
|
|
// Call does a grpc call
|
2021-12-03 07:15:32 +00:00
|
|
|
func (c *ClientBase) Call(ctx context.Context, caller func(client interface{}) (interface{}, error)) (interface{}, error) {
|
|
|
|
if !funcutil.CheckCtxValid(ctx) {
|
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
ret, err := c.callOnce(ctx, caller)
|
|
|
|
if err != nil {
|
|
|
|
traceErr := fmt.Errorf("err: %s\n, %s", err.Error(), trace.StackTrace())
|
|
|
|
log.Error(c.GetRole()+" ClientBase Call grpc first call get error ", zap.Error(traceErr))
|
|
|
|
return nil, traceErr
|
|
|
|
}
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientBase) ReCall(ctx context.Context, caller func(client interface{}) (interface{}, error)) (interface{}, error) {
|
|
|
|
if !funcutil.CheckCtxValid(ctx) {
|
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
ret, err := c.callOnce(ctx, caller)
|
|
|
|
if err == nil {
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
traceErr := fmt.Errorf("err: %s\n, %s", err.Error(), trace.StackTrace())
|
|
|
|
log.Warn(c.GetRole()+" ClientBase ReCall grpc first call get error ", zap.Error(traceErr))
|
|
|
|
|
|
|
|
if !funcutil.CheckCtxValid(ctx) {
|
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
ret, err = c.callOnce(ctx, caller)
|
|
|
|
if err != nil {
|
|
|
|
traceErr = fmt.Errorf("err: %s\n, %s", err.Error(), trace.StackTrace())
|
|
|
|
log.Error(c.GetRole()+" ClientBase ReCall grpc second call get error ", zap.Error(traceErr))
|
|
|
|
return nil, traceErr
|
|
|
|
}
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
2021-12-13 11:26:53 +00:00
|
|
|
// Close close the client connection
|
2021-12-03 07:15:32 +00:00
|
|
|
func (c *ClientBase) Close() error {
|
|
|
|
c.grpcClientMtx.Lock()
|
|
|
|
defer c.grpcClientMtx.Unlock()
|
|
|
|
if c.conn != nil {
|
|
|
|
return c.conn.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|