2021-12-03 15:15:32 +08: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.
|
|
|
|
|
|
|
|
package mock
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
|
|
|
"github.com/milvus-io/milvus/internal/log"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/funcutil"
|
2022-10-17 20:05:26 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/generic"
|
2021-12-03 15:15:32 +08:00
|
|
|
"github.com/milvus-io/milvus/internal/util/retry"
|
|
|
|
"github.com/milvus-io/milvus/internal/util/trace"
|
|
|
|
)
|
|
|
|
|
2022-10-17 20:05:26 +08:00
|
|
|
type GRPCClientBase[T any] struct {
|
2021-12-03 15:15:32 +08:00
|
|
|
getAddrFunc func() (string, error)
|
2022-10-17 20:05:26 +08:00
|
|
|
newGrpcClient func(cc *grpc.ClientConn) T
|
2021-12-03 15:15:32 +08:00
|
|
|
|
2022-10-17 20:05:26 +08:00
|
|
|
grpcClient T
|
2021-12-03 15:15:32 +08:00
|
|
|
conn *grpc.ClientConn
|
|
|
|
grpcClientMtx sync.RWMutex
|
|
|
|
GetGrpcClientErr error
|
|
|
|
role string
|
|
|
|
}
|
|
|
|
|
2022-10-17 20:05:26 +08:00
|
|
|
func (c *GRPCClientBase[T]) SetGetAddrFunc(f func() (string, error)) {
|
2021-12-03 15:15:32 +08:00
|
|
|
c.getAddrFunc = f
|
|
|
|
}
|
|
|
|
|
2022-10-17 20:05:26 +08:00
|
|
|
func (c *GRPCClientBase[T]) GetRole() string {
|
2021-12-03 15:15:32 +08:00
|
|
|
return c.role
|
|
|
|
}
|
|
|
|
|
2022-10-17 20:05:26 +08:00
|
|
|
func (c *GRPCClientBase[T]) SetRole(role string) {
|
2021-12-03 15:15:32 +08:00
|
|
|
c.role = role
|
|
|
|
}
|
|
|
|
|
2022-10-17 20:05:26 +08:00
|
|
|
func (c *GRPCClientBase[T]) EnableEncryption() {
|
2022-09-27 22:38:55 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-10-17 20:05:26 +08:00
|
|
|
func (c *GRPCClientBase[T]) SetNewGrpcClientFunc(f func(cc *grpc.ClientConn) T) {
|
2021-12-03 15:15:32 +08:00
|
|
|
c.newGrpcClient = f
|
|
|
|
}
|
|
|
|
|
2022-10-17 20:05:26 +08:00
|
|
|
func (c *GRPCClientBase[T]) GetGrpcClient(ctx context.Context) (T, error) {
|
2021-12-03 15:15:32 +08:00
|
|
|
c.grpcClientMtx.RLock()
|
|
|
|
defer c.grpcClientMtx.RUnlock()
|
|
|
|
c.connect(ctx)
|
|
|
|
return c.grpcClient, c.GetGrpcClientErr
|
|
|
|
}
|
|
|
|
|
2022-10-17 20:05:26 +08:00
|
|
|
func (c *GRPCClientBase[T]) resetConnection(client T) {
|
2021-12-03 15:15:32 +08:00
|
|
|
c.grpcClientMtx.Lock()
|
|
|
|
defer c.grpcClientMtx.Unlock()
|
2022-10-17 20:05:26 +08:00
|
|
|
if generic.IsZero(c.grpcClient) {
|
2021-12-03 15:15:32 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-17 20:05:26 +08:00
|
|
|
if !generic.Equal(client, c.grpcClient) {
|
2021-12-03 15:15:32 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if c.conn != nil {
|
|
|
|
_ = c.conn.Close()
|
|
|
|
}
|
|
|
|
c.conn = nil
|
2022-10-17 20:05:26 +08:00
|
|
|
c.grpcClient = generic.Zero[T]()
|
2021-12-03 15:15:32 +08:00
|
|
|
}
|
|
|
|
|
2022-10-17 20:05:26 +08:00
|
|
|
func (c *GRPCClientBase[T]) connect(ctx context.Context, retryOptions ...retry.Option) error {
|
2021-12-03 15:15:32 +08:00
|
|
|
c.grpcClient = c.newGrpcClient(c.conn)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-17 20:05:26 +08:00
|
|
|
func (c *GRPCClientBase[T]) callOnce(ctx context.Context, caller func(client T) (any, error)) (any, error) {
|
2021-12-03 15:15:32 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
c.resetConnection(client)
|
|
|
|
return ret, err2
|
|
|
|
}
|
|
|
|
|
2022-10-17 20:05:26 +08:00
|
|
|
func (c *GRPCClientBase[T]) Call(ctx context.Context, caller func(client T) (any, error)) (any, error) {
|
2021-12-03 15:15:32 +08:00
|
|
|
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())
|
2022-10-17 20:05:26 +08:00
|
|
|
log.Error("GRPCClientBase[T] Call grpc first call get error ", zap.Error(traceErr))
|
2021-12-03 15:15:32 +08:00
|
|
|
return nil, traceErr
|
|
|
|
}
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
2022-10-17 20:05:26 +08:00
|
|
|
func (c *GRPCClientBase[T]) ReCall(ctx context.Context, caller func(client T) (any, error)) (any, error) {
|
2022-03-30 12:03:27 +08:00
|
|
|
// omit ctx check in mock first time to let each function has failed context
|
2021-12-03 15:15:32 +08:00
|
|
|
ret, err := c.callOnce(ctx, caller)
|
|
|
|
if err == nil {
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
traceErr := fmt.Errorf("err: %s\n, %s", err.Error(), trace.StackTrace())
|
2022-10-17 20:05:26 +08:00
|
|
|
log.Warn("GRPCClientBase[T] client grpc first call get error ", zap.Error(traceErr))
|
2021-12-03 15:15:32 +08:00
|
|
|
|
|
|
|
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())
|
2022-10-17 20:05:26 +08:00
|
|
|
log.Error("GRPCClientBase[T] client grpc second call get error ", zap.Error(traceErr))
|
2021-12-03 15:15:32 +08:00
|
|
|
return nil, traceErr
|
|
|
|
}
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
2022-10-17 20:05:26 +08:00
|
|
|
func (c *GRPCClientBase[T]) Close() error {
|
2021-12-03 15:15:32 +08:00
|
|
|
c.grpcClientMtx.Lock()
|
|
|
|
defer c.grpcClientMtx.Unlock()
|
|
|
|
if c.conn != nil {
|
|
|
|
return c.conn.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|