balancer.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. "context"
  23. "encoding/json"
  24. "errors"
  25. "net"
  26. "strings"
  27. "google.golang.org/grpc/channelz"
  28. "google.golang.org/grpc/connectivity"
  29. "google.golang.org/grpc/credentials"
  30. "google.golang.org/grpc/internal"
  31. "google.golang.org/grpc/metadata"
  32. "google.golang.org/grpc/resolver"
  33. "google.golang.org/grpc/serviceconfig"
  34. )
  35. var (
  36. // m is a map from name to balancer builder.
  37. m = make(map[string]Builder)
  38. )
  39. // Register registers the balancer builder to the balancer map. b.Name
  40. // (lowercased) will be used as the name registered with this builder. If the
  41. // Builder implements ConfigParser, ParseConfig will be called when new service
  42. // configs are received by the resolver, and the result will be provided to the
  43. // Balancer in UpdateClientConnState.
  44. //
  45. // NOTE: this function must only be called during initialization time (i.e. in
  46. // an init() function), and is not thread-safe. If multiple Balancers are
  47. // registered with the same name, the one registered last will take effect.
  48. func Register(b Builder) {
  49. m[strings.ToLower(b.Name())] = b
  50. }
  51. // unregisterForTesting deletes the balancer with the given name from the
  52. // balancer map.
  53. //
  54. // This function is not thread-safe.
  55. func unregisterForTesting(name string) {
  56. delete(m, name)
  57. }
  58. func init() {
  59. internal.BalancerUnregister = unregisterForTesting
  60. }
  61. // Get returns the resolver builder registered with the given name.
  62. // Note that the compare is done in a case-insensitive fashion.
  63. // If no builder is register with the name, nil will be returned.
  64. func Get(name string) Builder {
  65. if b, ok := m[strings.ToLower(name)]; ok {
  66. return b
  67. }
  68. return nil
  69. }
  70. // A SubConn represents a single connection to a gRPC backend service.
  71. //
  72. // Each SubConn contains a list of addresses.
  73. //
  74. // All SubConns start in IDLE, and will not try to connect. To trigger the
  75. // connecting, Balancers must call Connect. If a connection re-enters IDLE,
  76. // Balancers must call Connect again to trigger a new connection attempt.
  77. //
  78. // gRPC will try to connect to the addresses in sequence, and stop trying the
  79. // remainder once the first connection is successful. If an attempt to connect
  80. // to all addresses encounters an error, the SubConn will enter
  81. // TRANSIENT_FAILURE for a backoff period, and then transition to IDLE.
  82. //
  83. // Once established, if a connection is lost, the SubConn will transition
  84. // directly to IDLE.
  85. //
  86. // This interface is to be implemented by gRPC. Users should not need their own
  87. // implementation of this interface. For situations like testing, any
  88. // implementations should embed this interface. This allows gRPC to add new
  89. // methods to this interface.
  90. type SubConn interface {
  91. // UpdateAddresses updates the addresses used in this SubConn.
  92. // gRPC checks if currently-connected address is still in the new list.
  93. // If it's in the list, the connection will be kept.
  94. // If it's not in the list, the connection will gracefully closed, and
  95. // a new connection will be created.
  96. //
  97. // This will trigger a state transition for the SubConn.
  98. //
  99. // Deprecated: this method will be removed. Create new SubConns for new
  100. // addresses instead.
  101. UpdateAddresses([]resolver.Address)
  102. // Connect starts the connecting for this SubConn.
  103. Connect()
  104. // GetOrBuildProducer returns a reference to the existing Producer for this
  105. // ProducerBuilder in this SubConn, or, if one does not currently exist,
  106. // creates a new one and returns it. Returns a close function which must
  107. // be called when the Producer is no longer needed.
  108. GetOrBuildProducer(ProducerBuilder) (p Producer, close func())
  109. // Shutdown shuts down the SubConn gracefully. Any started RPCs will be
  110. // allowed to complete. No future calls should be made on the SubConn.
  111. // One final state update will be delivered to the StateListener (or
  112. // UpdateSubConnState; deprecated) with ConnectivityState of Shutdown to
  113. // indicate the shutdown operation. This may be delivered before
  114. // in-progress RPCs are complete and the actual connection is closed.
  115. Shutdown()
  116. }
  117. // NewSubConnOptions contains options to create new SubConn.
  118. type NewSubConnOptions struct {
  119. // CredsBundle is the credentials bundle that will be used in the created
  120. // SubConn. If it's nil, the original creds from grpc DialOptions will be
  121. // used.
  122. //
  123. // Deprecated: Use the Attributes field in resolver.Address to pass
  124. // arbitrary data to the credential handshaker.
  125. CredsBundle credentials.Bundle
  126. // HealthCheckEnabled indicates whether health check service should be
  127. // enabled on this SubConn
  128. HealthCheckEnabled bool
  129. // StateListener is called when the state of the subconn changes. If nil,
  130. // Balancer.UpdateSubConnState will be called instead. Will never be
  131. // invoked until after Connect() is called on the SubConn created with
  132. // these options.
  133. StateListener func(SubConnState)
  134. }
  135. // State contains the balancer's state relevant to the gRPC ClientConn.
  136. type State struct {
  137. // State contains the connectivity state of the balancer, which is used to
  138. // determine the state of the ClientConn.
  139. ConnectivityState connectivity.State
  140. // Picker is used to choose connections (SubConns) for RPCs.
  141. Picker Picker
  142. }
  143. // ClientConn represents a gRPC ClientConn.
  144. //
  145. // This interface is to be implemented by gRPC. Users should not need a
  146. // brand new implementation of this interface. For the situations like
  147. // testing, the new implementation should embed this interface. This allows
  148. // gRPC to add new methods to this interface.
  149. type ClientConn interface {
  150. // NewSubConn is called by balancer to create a new SubConn.
  151. // It doesn't block and wait for the connections to be established.
  152. // Behaviors of the SubConn can be controlled by options.
  153. //
  154. // Deprecated: please be aware that in a future version, SubConns will only
  155. // support one address per SubConn.
  156. NewSubConn([]resolver.Address, NewSubConnOptions) (SubConn, error)
  157. // RemoveSubConn removes the SubConn from ClientConn.
  158. // The SubConn will be shutdown.
  159. //
  160. // Deprecated: use SubConn.Shutdown instead.
  161. RemoveSubConn(SubConn)
  162. // UpdateAddresses updates the addresses used in the passed in SubConn.
  163. // gRPC checks if the currently connected address is still in the new list.
  164. // If so, the connection will be kept. Else, the connection will be
  165. // gracefully closed, and a new connection will be created.
  166. //
  167. // This may trigger a state transition for the SubConn.
  168. //
  169. // Deprecated: this method will be removed. Create new SubConns for new
  170. // addresses instead.
  171. UpdateAddresses(SubConn, []resolver.Address)
  172. // UpdateState notifies gRPC that the balancer's internal state has
  173. // changed.
  174. //
  175. // gRPC will update the connectivity state of the ClientConn, and will call
  176. // Pick on the new Picker to pick new SubConns.
  177. UpdateState(State)
  178. // ResolveNow is called by balancer to notify gRPC to do a name resolving.
  179. ResolveNow(resolver.ResolveNowOptions)
  180. // Target returns the dial target for this ClientConn.
  181. //
  182. // Deprecated: Use the Target field in the BuildOptions instead.
  183. Target() string
  184. }
  185. // BuildOptions contains additional information for Build.
  186. type BuildOptions struct {
  187. // DialCreds is the transport credentials to use when communicating with a
  188. // remote load balancer server. Balancer implementations which do not
  189. // communicate with a remote load balancer server can ignore this field.
  190. DialCreds credentials.TransportCredentials
  191. // CredsBundle is the credentials bundle to use when communicating with a
  192. // remote load balancer server. Balancer implementations which do not
  193. // communicate with a remote load balancer server can ignore this field.
  194. CredsBundle credentials.Bundle
  195. // Dialer is the custom dialer to use when communicating with a remote load
  196. // balancer server. Balancer implementations which do not communicate with a
  197. // remote load balancer server can ignore this field.
  198. Dialer func(context.Context, string) (net.Conn, error)
  199. // Authority is the server name to use as part of the authentication
  200. // handshake when communicating with a remote load balancer server. Balancer
  201. // implementations which do not communicate with a remote load balancer
  202. // server can ignore this field.
  203. Authority string
  204. // ChannelzParentID is the parent ClientConn's channelz ID.
  205. ChannelzParentID *channelz.Identifier
  206. // CustomUserAgent is the custom user agent set on the parent ClientConn.
  207. // The balancer should set the same custom user agent if it creates a
  208. // ClientConn.
  209. CustomUserAgent string
  210. // Target contains the parsed address info of the dial target. It is the
  211. // same resolver.Target as passed to the resolver. See the documentation for
  212. // the resolver.Target type for details about what it contains.
  213. Target resolver.Target
  214. }
  215. // Builder creates a balancer.
  216. type Builder interface {
  217. // Build creates a new balancer with the ClientConn.
  218. Build(cc ClientConn, opts BuildOptions) Balancer
  219. // Name returns the name of balancers built by this builder.
  220. // It will be used to pick balancers (for example in service config).
  221. Name() string
  222. }
  223. // ConfigParser parses load balancer configs.
  224. type ConfigParser interface {
  225. // ParseConfig parses the JSON load balancer config provided into an
  226. // internal form or returns an error if the config is invalid. For future
  227. // compatibility reasons, unknown fields in the config should be ignored.
  228. ParseConfig(LoadBalancingConfigJSON json.RawMessage) (serviceconfig.LoadBalancingConfig, error)
  229. }
  230. // PickInfo contains additional information for the Pick operation.
  231. type PickInfo struct {
  232. // FullMethodName is the method name that NewClientStream() is called
  233. // with. The canonical format is /service/Method.
  234. FullMethodName string
  235. // Ctx is the RPC's context, and may contain relevant RPC-level information
  236. // like the outgoing header metadata.
  237. Ctx context.Context
  238. }
  239. // DoneInfo contains additional information for done.
  240. type DoneInfo struct {
  241. // Err is the rpc error the RPC finished with. It could be nil.
  242. Err error
  243. // Trailer contains the metadata from the RPC's trailer, if present.
  244. Trailer metadata.MD
  245. // BytesSent indicates if any bytes have been sent to the server.
  246. BytesSent bool
  247. // BytesReceived indicates if any byte has been received from the server.
  248. BytesReceived bool
  249. // ServerLoad is the load received from server. It's usually sent as part of
  250. // trailing metadata.
  251. //
  252. // The only supported type now is *orca_v3.LoadReport.
  253. ServerLoad any
  254. }
  255. var (
  256. // ErrNoSubConnAvailable indicates no SubConn is available for pick().
  257. // gRPC will block the RPC until a new picker is available via UpdateState().
  258. ErrNoSubConnAvailable = errors.New("no SubConn is available")
  259. // ErrTransientFailure indicates all SubConns are in TransientFailure.
  260. // WaitForReady RPCs will block, non-WaitForReady RPCs will fail.
  261. //
  262. // Deprecated: return an appropriate error based on the last resolution or
  263. // connection attempt instead. The behavior is the same for any non-gRPC
  264. // status error.
  265. ErrTransientFailure = errors.New("all SubConns are in TransientFailure")
  266. )
  267. // PickResult contains information related to a connection chosen for an RPC.
  268. type PickResult struct {
  269. // SubConn is the connection to use for this pick, if its state is Ready.
  270. // If the state is not Ready, gRPC will block the RPC until a new Picker is
  271. // provided by the balancer (using ClientConn.UpdateState). The SubConn
  272. // must be one returned by ClientConn.NewSubConn.
  273. SubConn SubConn
  274. // Done is called when the RPC is completed. If the SubConn is not ready,
  275. // this will be called with a nil parameter. If the SubConn is not a valid
  276. // type, Done may not be called. May be nil if the balancer does not wish
  277. // to be notified when the RPC completes.
  278. Done func(DoneInfo)
  279. // Metadata provides a way for LB policies to inject arbitrary per-call
  280. // metadata. Any metadata returned here will be merged with existing
  281. // metadata added by the client application.
  282. //
  283. // LB policies with child policies are responsible for propagating metadata
  284. // injected by their children to the ClientConn, as part of Pick().
  285. Metadata metadata.MD
  286. }
  287. // TransientFailureError returns e. It exists for backward compatibility and
  288. // will be deleted soon.
  289. //
  290. // Deprecated: no longer necessary, picker errors are treated this way by
  291. // default.
  292. func TransientFailureError(e error) error { return e }
  293. // Picker is used by gRPC to pick a SubConn to send an RPC.
  294. // Balancer is expected to generate a new picker from its snapshot every time its
  295. // internal state has changed.
  296. //
  297. // The pickers used by gRPC can be updated by ClientConn.UpdateState().
  298. type Picker interface {
  299. // Pick returns the connection to use for this RPC and related information.
  300. //
  301. // Pick should not block. If the balancer needs to do I/O or any blocking
  302. // or time-consuming work to service this call, it should return
  303. // ErrNoSubConnAvailable, and the Pick call will be repeated by gRPC when
  304. // the Picker is updated (using ClientConn.UpdateState).
  305. //
  306. // If an error is returned:
  307. //
  308. // - If the error is ErrNoSubConnAvailable, gRPC will block until a new
  309. // Picker is provided by the balancer (using ClientConn.UpdateState).
  310. //
  311. // - If the error is a status error (implemented by the grpc/status
  312. // package), gRPC will terminate the RPC with the code and message
  313. // provided.
  314. //
  315. // - For all other errors, wait for ready RPCs will wait, but non-wait for
  316. // ready RPCs will be terminated with this error's Error() string and
  317. // status code Unavailable.
  318. Pick(info PickInfo) (PickResult, error)
  319. }
  320. // Balancer takes input from gRPC, manages SubConns, and collects and aggregates
  321. // the connectivity states.
  322. //
  323. // It also generates and updates the Picker used by gRPC to pick SubConns for RPCs.
  324. //
  325. // UpdateClientConnState, ResolverError, UpdateSubConnState, and Close are
  326. // guaranteed to be called synchronously from the same goroutine. There's no
  327. // guarantee on picker.Pick, it may be called anytime.
  328. type Balancer interface {
  329. // UpdateClientConnState is called by gRPC when the state of the ClientConn
  330. // changes. If the error returned is ErrBadResolverState, the ClientConn
  331. // will begin calling ResolveNow on the active name resolver with
  332. // exponential backoff until a subsequent call to UpdateClientConnState
  333. // returns a nil error. Any other errors are currently ignored.
  334. UpdateClientConnState(ClientConnState) error
  335. // ResolverError is called by gRPC when the name resolver reports an error.
  336. ResolverError(error)
  337. // UpdateSubConnState is called by gRPC when the state of a SubConn
  338. // changes.
  339. //
  340. // Deprecated: Use NewSubConnOptions.StateListener when creating the
  341. // SubConn instead.
  342. UpdateSubConnState(SubConn, SubConnState)
  343. // Close closes the balancer. The balancer is not currently required to
  344. // call SubConn.Shutdown for its existing SubConns; however, this will be
  345. // required in a future release, so it is recommended.
  346. Close()
  347. }
  348. // ExitIdler is an optional interface for balancers to implement. If
  349. // implemented, ExitIdle will be called when ClientConn.Connect is called, if
  350. // the ClientConn is idle. If unimplemented, ClientConn.Connect will cause
  351. // all SubConns to connect.
  352. //
  353. // Notice: it will be required for all balancers to implement this in a future
  354. // release.
  355. type ExitIdler interface {
  356. // ExitIdle instructs the LB policy to reconnect to backends / exit the
  357. // IDLE state, if appropriate and possible. Note that SubConns that enter
  358. // the IDLE state will not reconnect until SubConn.Connect is called.
  359. ExitIdle()
  360. }
  361. // SubConnState describes the state of a SubConn.
  362. type SubConnState struct {
  363. // ConnectivityState is the connectivity state of the SubConn.
  364. ConnectivityState connectivity.State
  365. // ConnectionError is set if the ConnectivityState is TransientFailure,
  366. // describing the reason the SubConn failed. Otherwise, it is nil.
  367. ConnectionError error
  368. }
  369. // ClientConnState describes the state of a ClientConn relevant to the
  370. // balancer.
  371. type ClientConnState struct {
  372. ResolverState resolver.State
  373. // The parsed load balancing configuration returned by the builder's
  374. // ParseConfig method, if implemented.
  375. BalancerConfig serviceconfig.LoadBalancingConfig
  376. }
  377. // ErrBadResolverState may be returned by UpdateClientConnState to indicate a
  378. // problem with the provided name resolver data.
  379. var ErrBadResolverState = errors.New("bad resolver state")
  380. // A ProducerBuilder is a simple constructor for a Producer. It is used by the
  381. // SubConn to create producers when needed.
  382. type ProducerBuilder interface {
  383. // Build creates a Producer. The first parameter is always a
  384. // grpc.ClientConnInterface (a type to allow creating RPCs/streams on the
  385. // associated SubConn), but is declared as `any` to avoid a dependency
  386. // cycle. Should also return a close function that will be called when all
  387. // references to the Producer have been given up.
  388. Build(grpcClientConnInterface any) (p Producer, close func())
  389. }
  390. // A Producer is a type shared among potentially many consumers. It is
  391. // associated with a SubConn, and an implementation will typically contain
  392. // other methods to provide additional functionality, e.g. configuration or
  393. // subscription registration.
  394. type Producer any