grpcclient.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Licensed to the LF AI & Data foundation under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. package mock
  17. import (
  18. "context"
  19. "fmt"
  20. "sync"
  21. "go.uber.org/zap"
  22. "google.golang.org/grpc"
  23. "github.com/milvus-io/milvus/internal/util/sessionutil"
  24. "github.com/milvus-io/milvus/pkg/log"
  25. "github.com/milvus-io/milvus/pkg/tracer"
  26. "github.com/milvus-io/milvus/pkg/util/funcutil"
  27. "github.com/milvus-io/milvus/pkg/util/generic"
  28. "github.com/milvus-io/milvus/pkg/util/retry"
  29. )
  30. type GRPCClientBase[T any] struct {
  31. getAddrFunc func() (string, error)
  32. newGrpcClient func(cc *grpc.ClientConn) T
  33. grpcClient T
  34. conn *grpc.ClientConn
  35. grpcClientMtx sync.RWMutex
  36. GetGrpcClientErr error
  37. role string
  38. nodeID int64
  39. sess *sessionutil.Session
  40. }
  41. func (c *GRPCClientBase[T]) SetGetAddrFunc(f func() (string, error)) {
  42. c.getAddrFunc = f
  43. }
  44. func (c *GRPCClientBase[T]) GetRole() string {
  45. return c.role
  46. }
  47. func (c *GRPCClientBase[T]) SetRole(role string) {
  48. c.role = role
  49. }
  50. func (c *GRPCClientBase[T]) EnableEncryption() {
  51. }
  52. func (c *GRPCClientBase[T]) SetNewGrpcClientFunc(f func(cc *grpc.ClientConn) T) {
  53. c.newGrpcClient = f
  54. }
  55. func (c *GRPCClientBase[T]) GetGrpcClient(ctx context.Context) (T, error) {
  56. c.grpcClientMtx.RLock()
  57. defer c.grpcClientMtx.RUnlock()
  58. c.connect(ctx)
  59. return c.grpcClient, c.GetGrpcClientErr
  60. }
  61. func (c *GRPCClientBase[T]) resetConnection(client T) {
  62. c.grpcClientMtx.Lock()
  63. defer c.grpcClientMtx.Unlock()
  64. if generic.IsZero(c.grpcClient) {
  65. return
  66. }
  67. if !generic.Equal(client, c.grpcClient) {
  68. return
  69. }
  70. if c.conn != nil {
  71. _ = c.conn.Close()
  72. }
  73. c.conn = nil
  74. c.grpcClient = generic.Zero[T]()
  75. }
  76. func (c *GRPCClientBase[T]) connect(ctx context.Context, retryOptions ...retry.Option) error {
  77. c.grpcClient = c.newGrpcClient(c.conn)
  78. return nil
  79. }
  80. func (c *GRPCClientBase[T]) callOnce(ctx context.Context, caller func(client T) (any, error)) (any, error) {
  81. client, err := c.GetGrpcClient(ctx)
  82. if err != nil {
  83. return nil, err
  84. }
  85. ret, err2 := caller(client)
  86. if err2 == nil {
  87. return ret, nil
  88. }
  89. if err2 == context.Canceled || err2 == context.DeadlineExceeded {
  90. return nil, err2
  91. }
  92. c.resetConnection(client)
  93. return ret, err2
  94. }
  95. func (c *GRPCClientBase[T]) Call(ctx context.Context, caller func(client T) (any, error)) (any, error) {
  96. if !funcutil.CheckCtxValid(ctx) {
  97. return nil, ctx.Err()
  98. }
  99. ret, err := c.callOnce(ctx, caller)
  100. if err != nil {
  101. traceErr := fmt.Errorf("err: %s\n, %s", err.Error(), tracer.StackTrace())
  102. log.Error("GRPCClientBase[T] Call grpc first call get error ", zap.Error(traceErr))
  103. return nil, traceErr
  104. }
  105. return ret, err
  106. }
  107. func (c *GRPCClientBase[T]) ReCall(ctx context.Context, caller func(client T) (any, error)) (any, error) {
  108. // omit ctx check in mock first time to let each function has failed context
  109. ret, err := c.callOnce(ctx, caller)
  110. if err == nil {
  111. return ret, nil
  112. }
  113. traceErr := fmt.Errorf("err: %s\n, %s", err.Error(), tracer.StackTrace())
  114. log.Warn("GRPCClientBase[T] client grpc first call get error ", zap.Error(traceErr))
  115. if !funcutil.CheckCtxValid(ctx) {
  116. return nil, ctx.Err()
  117. }
  118. ret, err = c.callOnce(ctx, caller)
  119. if err != nil {
  120. traceErr = fmt.Errorf("err: %s\n, %s", err.Error(), tracer.StackTrace())
  121. log.Error("GRPCClientBase[T] client grpc second call get error ", zap.Error(traceErr))
  122. return nil, traceErr
  123. }
  124. return ret, err
  125. }
  126. func (c *GRPCClientBase[T]) Close() error {
  127. c.grpcClientMtx.Lock()
  128. defer c.grpcClientMtx.Unlock()
  129. if c.conn != nil {
  130. return c.conn.Close()
  131. }
  132. return nil
  133. }
  134. func (c *GRPCClientBase[T]) GetNodeID() int64 {
  135. return c.nodeID
  136. }
  137. func (c *GRPCClientBase[T]) SetNodeID(nodeID int64) {
  138. c.nodeID = nodeID
  139. }
  140. func (c *GRPCClientBase[T]) SetSession(sess *sessionutil.Session) {
  141. c.sess = sess
  142. }