balancer.go 18 KB

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