2022-01-07 15:11:56 +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
|
|
|
|
// 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-12-03 07:15:32 +00:00
|
|
|
package grpcclient
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-09-27 14:38:55 +00:00
|
|
|
"crypto/tls"
|
2021-12-03 07:15:32 +00:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2022-09-27 14:38:55 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"google.golang.org/grpc"
|
2022-06-22 14:22:13 +00:00
|
|
|
"google.golang.org/grpc/backoff"
|
2022-09-27 14:38:55 +00:00
|
|
|
"google.golang.org/grpc/credentials"
|
|
|
|
"google.golang.org/grpc/keepalive"
|
2022-04-11 11:49:34 +00:00
|
|
|
|
2021-12-03 07:15:32 +00:00
|
|
|
grpcopentracing "github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing"
|
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
2022-09-27 14:38:55 +00:00
|
|
|
"github.com/milvus-io/milvus/internal/util"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/crypto"
|
2021-12-03 07:15:32 +00:00
|
|
|
"github.com/milvus-io/milvus/internal/util/funcutil"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/trace"
|
|
|
|
)
|
|
|
|
|
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))
|
2022-09-27 14:38:55 +00:00
|
|
|
EnableEncryption()
|
2021-12-03 07:15:32 +00:00
|
|
|
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{}
|
|
|
|
|
2022-06-22 14:22:13 +00:00
|
|
|
grpcClient interface{}
|
2022-09-27 14:38:55 +00:00
|
|
|
encryption bool
|
2022-06-22 14:22:13 +00:00
|
|
|
conn *grpc.ClientConn
|
|
|
|
grpcClientMtx sync.RWMutex
|
|
|
|
role string
|
|
|
|
ClientMaxSendSize int
|
|
|
|
ClientMaxRecvSize int
|
|
|
|
RetryServiceNameConfig string
|
2022-05-05 02:35:50 +00:00
|
|
|
|
|
|
|
DialTimeout time.Duration
|
|
|
|
KeepAliveTime time.Duration
|
|
|
|
KeepAliveTimeout time.Duration
|
2022-06-22 14:22:13 +00:00
|
|
|
|
|
|
|
MaxAttempts int
|
|
|
|
InitialBackoff float32
|
|
|
|
MaxBackoff float32
|
|
|
|
BackoffMultiplier float32
|
2021-12-03 07:15:32 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-09-27 14:38:55 +00:00
|
|
|
func (c *ClientBase) EnableEncryption() {
|
|
|
|
c.encryption = true
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-12-29 06:35:21 +00:00
|
|
|
log.Error("failed to get client address", zap.Error(err))
|
2021-12-25 06:24:18 +00:00
|
|
|
return err
|
2021-12-03 07:15:32 +00:00
|
|
|
}
|
|
|
|
|
2021-12-25 06:24:18 +00:00
|
|
|
opts := trace.GetInterceptorOpts()
|
2022-05-05 02:35:50 +00:00
|
|
|
dialContext, cancel := context.WithTimeout(ctx, c.DialTimeout)
|
2021-12-25 10:52:28 +00:00
|
|
|
|
2021-12-28 08:17:51 +00:00
|
|
|
// refer to https://github.com/grpc/grpc-proto/blob/master/grpc/service_config/service_config.proto
|
2022-06-22 14:22:13 +00:00
|
|
|
retryPolicy := fmt.Sprintf(`{
|
2021-12-25 10:52:28 +00:00
|
|
|
"methodConfig": [{
|
2022-06-22 14:22:13 +00:00
|
|
|
"name": [{"service": "%s"}],
|
2021-12-25 10:52:28 +00:00
|
|
|
"retryPolicy": {
|
2022-06-22 14:22:13 +00:00
|
|
|
"MaxAttempts": %d,
|
|
|
|
"InitialBackoff": "%fs",
|
|
|
|
"MaxBackoff": "%fs",
|
|
|
|
"BackoffMultiplier": %f,
|
2021-12-25 10:52:28 +00:00
|
|
|
"RetryableStatusCodes": [ "UNAVAILABLE" ]
|
|
|
|
}
|
2022-06-22 14:22:13 +00:00
|
|
|
}]}`, c.RetryServiceNameConfig, c.MaxAttempts, c.InitialBackoff, c.MaxBackoff, c.BackoffMultiplier)
|
2021-12-25 10:52:28 +00:00
|
|
|
|
2022-09-27 14:38:55 +00:00
|
|
|
var conn *grpc.ClientConn
|
|
|
|
if c.encryption {
|
|
|
|
conn, err = grpc.DialContext(
|
|
|
|
dialContext,
|
|
|
|
addr,
|
|
|
|
//grpc.WithInsecure(),
|
|
|
|
// #nosec G402
|
|
|
|
grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})),
|
|
|
|
grpc.WithBlock(),
|
|
|
|
grpc.WithDefaultCallOptions(
|
|
|
|
grpc.MaxCallRecvMsgSize(c.ClientMaxRecvSize),
|
|
|
|
grpc.MaxCallSendMsgSize(c.ClientMaxSendSize),
|
|
|
|
),
|
|
|
|
grpc.WithUnaryInterceptor(grpcopentracing.UnaryClientInterceptor(opts...)),
|
|
|
|
grpc.WithStreamInterceptor(grpcopentracing.StreamClientInterceptor(opts...)),
|
|
|
|
grpc.WithDefaultServiceConfig(retryPolicy),
|
|
|
|
grpc.WithKeepaliveParams(keepalive.ClientParameters{
|
|
|
|
Time: c.KeepAliveTime,
|
|
|
|
Timeout: c.KeepAliveTimeout,
|
|
|
|
PermitWithoutStream: true,
|
|
|
|
}),
|
|
|
|
grpc.WithConnectParams(grpc.ConnectParams{
|
|
|
|
Backoff: backoff.Config{
|
|
|
|
BaseDelay: 100 * time.Millisecond,
|
|
|
|
Multiplier: 1.6,
|
|
|
|
Jitter: 0.2,
|
|
|
|
MaxDelay: 3 * time.Second,
|
|
|
|
},
|
|
|
|
MinConnectTimeout: c.DialTimeout,
|
|
|
|
}),
|
|
|
|
grpc.WithPerRPCCredentials(&Token{Value: crypto.Base64Encode(util.MemberCredID)}),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
conn, err = grpc.DialContext(
|
|
|
|
dialContext,
|
|
|
|
addr,
|
|
|
|
grpc.WithInsecure(),
|
|
|
|
//grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})),
|
|
|
|
grpc.WithBlock(),
|
|
|
|
grpc.WithDefaultCallOptions(
|
|
|
|
grpc.MaxCallRecvMsgSize(c.ClientMaxRecvSize),
|
|
|
|
grpc.MaxCallSendMsgSize(c.ClientMaxSendSize),
|
|
|
|
),
|
|
|
|
grpc.WithUnaryInterceptor(grpcopentracing.UnaryClientInterceptor(opts...)),
|
|
|
|
grpc.WithStreamInterceptor(grpcopentracing.StreamClientInterceptor(opts...)),
|
|
|
|
grpc.WithDefaultServiceConfig(retryPolicy),
|
|
|
|
grpc.WithKeepaliveParams(keepalive.ClientParameters{
|
|
|
|
Time: c.KeepAliveTime,
|
|
|
|
Timeout: c.KeepAliveTimeout,
|
|
|
|
PermitWithoutStream: true,
|
|
|
|
}),
|
|
|
|
grpc.WithConnectParams(grpc.ConnectParams{
|
|
|
|
Backoff: backoff.Config{
|
|
|
|
BaseDelay: 100 * time.Millisecond,
|
|
|
|
Multiplier: 1.6,
|
|
|
|
Jitter: 0.2,
|
|
|
|
MaxDelay: 3 * time.Second,
|
|
|
|
},
|
|
|
|
MinConnectTimeout: c.DialTimeout,
|
|
|
|
}),
|
|
|
|
grpc.WithPerRPCCredentials(&Token{Value: crypto.Base64Encode(util.MemberCredID)}),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-25 06:24:18 +00:00
|
|
|
cancel()
|
2021-12-03 07:15:32 +00:00
|
|
|
if err != nil {
|
2022-06-07 04:20:06 +00:00
|
|
|
return wrapErrConnect(addr, err)
|
2021-12-03 07:15:32 +00:00
|
|
|
}
|
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
|
|
|
|
}
|
2022-01-04 07:13:18 +00:00
|
|
|
|
|
|
|
if !funcutil.CheckCtxValid(ctx) {
|
2021-12-03 07:15:32 +00:00
|
|
|
return nil, err2
|
|
|
|
}
|
2022-06-02 04:16:03 +00:00
|
|
|
if !funcutil.IsGrpcErr(err2) {
|
|
|
|
log.Debug("ClientBase:isNotGrpcErr", zap.Error(err2))
|
|
|
|
return nil, err2
|
|
|
|
}
|
2021-12-03 07:15:32 +00:00
|
|
|
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 {
|
2022-06-13 04:48:08 +00:00
|
|
|
traceErr := fmt.Errorf("err: %w\n, %s", err, trace.StackTrace())
|
|
|
|
log.Error("ClientBase Call grpc first call get error", zap.String("role", c.GetRole()), zap.Error(traceErr))
|
2021-12-03 07:15:32 +00:00
|
|
|
return nil, traceErr
|
|
|
|
}
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
2021-12-30 02:47:46 +00:00
|
|
|
// ReCall does the grpc call twice
|
2021-12-03 07:15:32 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-06-13 04:48:08 +00:00
|
|
|
traceErr := fmt.Errorf("err: %w\n, %s", err, trace.StackTrace())
|
2021-12-03 07:15:32 +00:00
|
|
|
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 {
|
2022-06-13 04:48:08 +00:00
|
|
|
traceErr = fmt.Errorf("err: %w\n, %s", err, trace.StackTrace())
|
|
|
|
log.Error("ClientBase ReCall grpc second call get error", zap.String("role", c.GetRole()), zap.Error(traceErr))
|
2021-12-03 07:15:32 +00:00
|
|
|
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
|
|
|
|
}
|