balancer.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. *
  3. * Copyright 2017 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * 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. *
  17. */
  18. // Package balancer defines APIs for load balancing in gRPC.
  19. // All APIs in this package are experimental.
  20. package balancer
  21. import (
  22. "errors"
  23. "net"
  24. "strings"
  25. "golang.org/x/net/context"
  26. "google.golang.org/grpc/connectivity"
  27. "google.golang.org/grpc/credentials"
  28. "google.golang.org/grpc/resolver"
  29. )
  30. var (
  31. // m is a map from name to balancer builder.
  32. m = make(map[string]Builder)
  33. )
  34. // Register registers the balancer builder to the balancer map.
  35. // b.Name (lowercased) will be used as the name registered with
  36. // this builder.
  37. func Register(b Builder) {
  38. m[strings.ToLower(b.Name())] = b
  39. }
  40. // Get returns the resolver builder registered with the given name.
  41. // Note that the compare is done in a case-insenstive fashion.
  42. // If no builder is register with the name, nil will be returned.
  43. func Get(name string) Builder {
  44. if b, ok := m[strings.ToLower(name)]; ok {
  45. return b
  46. }
  47. return nil
  48. }
  49. // SubConn represents a gRPC sub connection.
  50. // Each sub connection contains a list of addresses. gRPC will
  51. // try to connect to them (in sequence), and stop trying the
  52. // remainder once one connection is successful.
  53. //
  54. // The reconnect backoff will be applied on the list, not a single address.
  55. // For example, try_on_all_addresses -> backoff -> try_on_all_addresses.
  56. //
  57. // All SubConns start in IDLE, and will not try to connect. To trigger
  58. // the connecting, Balancers must call Connect.
  59. // When the connection encounters an error, it will reconnect immediately.
  60. // When the connection becomes IDLE, it will not reconnect unless Connect is
  61. // called.
  62. //
  63. // This interface is to be implemented by gRPC. Users should not need a
  64. // brand new implementation of this interface. For the situations like
  65. // testing, the new implementation should embed this interface. This allows
  66. // gRPC to add new methods to this interface.
  67. type SubConn interface {
  68. // UpdateAddresses updates the addresses used in this SubConn.
  69. // gRPC checks if currently-connected address is still in the new list.
  70. // If it's in the list, the connection will be kept.
  71. // If it's not in the list, the connection will gracefully closed, and
  72. // a new connection will be created.
  73. //
  74. // This will trigger a state transition for the SubConn.
  75. UpdateAddresses([]resolver.Address)
  76. // Connect starts the connecting for this SubConn.
  77. Connect()
  78. }
  79. // NewSubConnOptions contains options to create new SubConn.
  80. type NewSubConnOptions struct{}
  81. // ClientConn represents a gRPC ClientConn.
  82. //
  83. // This interface is to be implemented by gRPC. Users should not need a
  84. // brand new implementation of this interface. For the situations like
  85. // testing, the new implementation should embed this interface. This allows
  86. // gRPC to add new methods to this interface.
  87. type ClientConn interface {
  88. // NewSubConn is called by balancer to create a new SubConn.
  89. // It doesn't block and wait for the connections to be established.
  90. // Behaviors of the SubConn can be controlled by options.
  91. NewSubConn([]resolver.Address, NewSubConnOptions) (SubConn, error)
  92. // RemoveSubConn removes the SubConn from ClientConn.
  93. // The SubConn will be shutdown.
  94. RemoveSubConn(SubConn)
  95. // UpdateBalancerState is called by balancer to nofity gRPC that some internal
  96. // state in balancer has changed.
  97. //
  98. // gRPC will update the connectivity state of the ClientConn, and will call pick
  99. // on the new picker to pick new SubConn.
  100. UpdateBalancerState(s connectivity.State, p Picker)
  101. // ResolveNow is called by balancer to notify gRPC to do a name resolving.
  102. ResolveNow(resolver.ResolveNowOption)
  103. // Target returns the dial target for this ClientConn.
  104. Target() string
  105. }
  106. // BuildOptions contains additional information for Build.
  107. type BuildOptions struct {
  108. // DialCreds is the transport credential the Balancer implementation can
  109. // use to dial to a remote load balancer server. The Balancer implementations
  110. // can ignore this if it does not need to talk to another party securely.
  111. DialCreds credentials.TransportCredentials
  112. // Dialer is the custom dialer the Balancer implementation can use to dial
  113. // to a remote load balancer server. The Balancer implementations
  114. // can ignore this if it doesn't need to talk to remote balancer.
  115. Dialer func(context.Context, string) (net.Conn, error)
  116. }
  117. // Builder creates a balancer.
  118. type Builder interface {
  119. // Build creates a new balancer with the ClientConn.
  120. Build(cc ClientConn, opts BuildOptions) Balancer
  121. // Name returns the name of balancers built by this builder.
  122. // It will be used to pick balancers (for example in service config).
  123. Name() string
  124. }
  125. // PickOptions contains addition information for the Pick operation.
  126. type PickOptions struct{}
  127. // DoneInfo contains additional information for done.
  128. type DoneInfo struct {
  129. // Err is the rpc error the RPC finished with. It could be nil.
  130. Err error
  131. // BytesSent indicates if any bytes have been sent to the server.
  132. BytesSent bool
  133. // BytesReceived indicates if any byte has been received from the server.
  134. BytesReceived bool
  135. }
  136. var (
  137. // ErrNoSubConnAvailable indicates no SubConn is available for pick().
  138. // gRPC will block the RPC until a new picker is available via UpdateBalancerState().
  139. ErrNoSubConnAvailable = errors.New("no SubConn is available")
  140. // ErrTransientFailure indicates all SubConns are in TransientFailure.
  141. // WaitForReady RPCs will block, non-WaitForReady RPCs will fail.
  142. ErrTransientFailure = errors.New("all SubConns are in TransientFailure")
  143. )
  144. // Picker is used by gRPC to pick a SubConn to send an RPC.
  145. // Balancer is expected to generate a new picker from its snapshot everytime its
  146. // internal state has changed.
  147. //
  148. // The pickers used by gRPC can be updated by ClientConn.UpdateBalancerState().
  149. type Picker interface {
  150. // Pick returns the SubConn to be used to send the RPC.
  151. // The returned SubConn must be one returned by NewSubConn().
  152. //
  153. // This functions is expected to return:
  154. // - a SubConn that is known to be READY;
  155. // - ErrNoSubConnAvailable if no SubConn is available, but progress is being
  156. // made (for example, some SubConn is in CONNECTING mode);
  157. // - other errors if no active connecting is happening (for example, all SubConn
  158. // are in TRANSIENT_FAILURE mode).
  159. //
  160. // If a SubConn is returned:
  161. // - If it is READY, gRPC will send the RPC on it;
  162. // - If it is not ready, or becomes not ready after it's returned, gRPC will block
  163. // until UpdateBalancerState() is called and will call pick on the new picker.
  164. //
  165. // If the returned error is not nil:
  166. // - If the error is ErrNoSubConnAvailable, gRPC will block until UpdateBalancerState()
  167. // - If the error is ErrTransientFailure:
  168. // - If the RPC is wait-for-ready, gRPC will block until UpdateBalancerState()
  169. // is called to pick again;
  170. // - Otherwise, RPC will fail with unavailable error.
  171. // - Else (error is other non-nil error):
  172. // - The RPC will fail with unavailable error.
  173. //
  174. // The returned done() function will be called once the rpc has finished, with the
  175. // final status of that RPC.
  176. // done may be nil if balancer doesn't care about the RPC status.
  177. Pick(ctx context.Context, opts PickOptions) (conn SubConn, done func(DoneInfo), err error)
  178. }
  179. // Balancer takes input from gRPC, manages SubConns, and collects and aggregates
  180. // the connectivity states.
  181. //
  182. // It also generates and updates the Picker used by gRPC to pick SubConns for RPCs.
  183. //
  184. // HandleSubConnectionStateChange, HandleResolvedAddrs and Close are guaranteed
  185. // to be called synchronously from the same goroutine.
  186. // There's no guarantee on picker.Pick, it may be called anytime.
  187. type Balancer interface {
  188. // HandleSubConnStateChange is called by gRPC when the connectivity state
  189. // of sc has changed.
  190. // Balancer is expected to aggregate all the state of SubConn and report
  191. // that back to gRPC.
  192. // Balancer should also generate and update Pickers when its internal state has
  193. // been changed by the new state.
  194. HandleSubConnStateChange(sc SubConn, state connectivity.State)
  195. // HandleResolvedAddrs is called by gRPC to send updated resolved addresses to
  196. // balancers.
  197. // Balancer can create new SubConn or remove SubConn with the addresses.
  198. // An empty address slice and a non-nil error will be passed if the resolver returns
  199. // non-nil error to gRPC.
  200. HandleResolvedAddrs([]resolver.Address, error)
  201. // Close closes the balancer. The balancer is not required to call
  202. // ClientConn.RemoveSubConn for its existing SubConns.
  203. Close()
  204. }