client.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 client
  17. import (
  18. "context"
  19. "crypto/tls"
  20. "fmt"
  21. "math"
  22. "os"
  23. "strconv"
  24. "sync"
  25. "time"
  26. grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry"
  27. "google.golang.org/grpc"
  28. "google.golang.org/grpc/codes"
  29. "google.golang.org/grpc/credentials"
  30. "google.golang.org/grpc/credentials/insecure"
  31. "google.golang.org/grpc/status"
  32. "github.com/milvus-io/milvus-proto/go-api/v2/commonpb"
  33. "github.com/milvus-io/milvus-proto/go-api/v2/milvuspb"
  34. "github.com/milvus-io/milvus/client/v2/common"
  35. "github.com/milvus-io/milvus/client/v2/entity"
  36. "github.com/milvus-io/milvus/pkg/util/merr"
  37. )
  38. type Client struct {
  39. conn *grpc.ClientConn
  40. service milvuspb.MilvusServiceClient
  41. config *ClientConfig
  42. // mutable status
  43. stateMut sync.RWMutex
  44. currentDB string
  45. identifier string
  46. collCache *CollectionCache
  47. }
  48. func New(ctx context.Context, config *ClientConfig) (*Client, error) {
  49. if err := config.parse(); err != nil {
  50. return nil, err
  51. }
  52. c := &Client{
  53. config: config,
  54. }
  55. // Parse remote address.
  56. addr := c.config.getParsedAddress()
  57. // parse authentication parameters
  58. c.config.parseAuthentication()
  59. // Parse grpc options
  60. options := c.dialOptions()
  61. // Connect the grpc server.
  62. if err := c.connect(ctx, addr, options...); err != nil {
  63. return nil, err
  64. }
  65. c.collCache = NewCollectionCache(func(ctx context.Context, collName string) (*entity.Collection, error) {
  66. return c.DescribeCollection(ctx, NewDescribeCollectionOption(collName))
  67. })
  68. return c, nil
  69. }
  70. func (c *Client) dialOptions() []grpc.DialOption {
  71. var options []grpc.DialOption
  72. // Construct dial option.
  73. if c.config.EnableTLSAuth {
  74. options = append(options, grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})))
  75. } else {
  76. options = append(options, grpc.WithTransportCredentials(insecure.NewCredentials()))
  77. }
  78. if c.config.DialOptions == nil {
  79. // Add default connection options.
  80. options = append(options, DefaultGrpcOpts...)
  81. } else {
  82. options = append(options, c.config.DialOptions...)
  83. }
  84. options = append(options,
  85. grpc.WithChainUnaryInterceptor(grpc_retry.UnaryClientInterceptor(
  86. grpc_retry.WithMax(6),
  87. grpc_retry.WithBackoff(func(attempt uint) time.Duration {
  88. return 60 * time.Millisecond * time.Duration(math.Pow(3, float64(attempt)))
  89. }),
  90. grpc_retry.WithCodes(codes.Unavailable, codes.ResourceExhausted)),
  91. // c.getRetryOnRateLimitInterceptor(),
  92. ))
  93. options = append(options, grpc.WithChainUnaryInterceptor(
  94. c.MetadataUnaryInterceptor(),
  95. ))
  96. return options
  97. }
  98. func (c *Client) Close(ctx context.Context) error {
  99. if c.conn == nil {
  100. return nil
  101. }
  102. err := c.conn.Close()
  103. if err != nil {
  104. return err
  105. }
  106. c.conn = nil
  107. c.service = nil
  108. return nil
  109. }
  110. func (c *Client) usingDatabase(dbName string) {
  111. c.stateMut.Lock()
  112. defer c.stateMut.Unlock()
  113. c.currentDB = dbName
  114. }
  115. func (c *Client) setIdentifier(identifier string) {
  116. c.stateMut.Lock()
  117. defer c.stateMut.Unlock()
  118. c.identifier = identifier
  119. }
  120. func (c *Client) connect(ctx context.Context, addr string, options ...grpc.DialOption) error {
  121. if addr == "" {
  122. return fmt.Errorf("address is empty")
  123. }
  124. conn, err := grpc.DialContext(ctx, addr, options...)
  125. if err != nil {
  126. return err
  127. }
  128. c.conn = conn
  129. c.service = milvuspb.NewMilvusServiceClient(c.conn)
  130. if !c.config.DisableConn {
  131. err = c.connectInternal(ctx)
  132. if err != nil {
  133. return err
  134. }
  135. }
  136. return nil
  137. }
  138. func (c *Client) connectInternal(ctx context.Context) error {
  139. hostName, err := os.Hostname()
  140. if err != nil {
  141. return err
  142. }
  143. req := &milvuspb.ConnectRequest{
  144. ClientInfo: &commonpb.ClientInfo{
  145. SdkType: "GoMilvusClient",
  146. SdkVersion: common.SDKVersion,
  147. LocalTime: time.Now().String(),
  148. User: c.config.Username,
  149. Host: hostName,
  150. },
  151. }
  152. resp, err := c.service.Connect(ctx, req)
  153. if err != nil {
  154. status, ok := status.FromError(err)
  155. if ok {
  156. if status.Code() == codes.Unimplemented {
  157. // disable unsupported feature
  158. c.config.addFlags(
  159. disableDatabase |
  160. disableJSON |
  161. disableParitionKey |
  162. disableDynamicSchema)
  163. return nil
  164. }
  165. }
  166. return err
  167. }
  168. if !merr.Ok(resp.GetStatus()) {
  169. return merr.Error(resp.GetStatus())
  170. }
  171. c.config.setServerInfo(resp.GetServerInfo().GetBuildTags())
  172. c.setIdentifier(strconv.FormatInt(resp.GetIdentifier(), 10))
  173. if c.collCache != nil {
  174. c.collCache.Reset()
  175. }
  176. return nil
  177. }
  178. func (c *Client) callService(fn func(milvusService milvuspb.MilvusServiceClient) error) error {
  179. service := c.service
  180. if service == nil {
  181. return merr.WrapErrServiceNotReady("SDK", 0, "not connected")
  182. }
  183. return fn(c.service)
  184. }