You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

balancer.go 14 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. "errors"
  24. "net"
  25. "strings"
  26. "google.golang.org/grpc/connectivity"
  27. "google.golang.org/grpc/credentials"
  28. "google.golang.org/grpc/internal"
  29. "google.golang.org/grpc/metadata"
  30. "google.golang.org/grpc/resolver"
  31. )
  32. var (
  33. // m is a map from name to balancer builder.
  34. m = make(map[string]Builder)
  35. )
  36. // Register registers the balancer builder to the balancer map. b.Name
  37. // (lowercased) will be used as the name registered with this builder.
  38. //
  39. // NOTE: this function must only be called during initialization time (i.e. in
  40. // an init() function), and is not thread-safe. If multiple Balancers are
  41. // registered with the same name, the one registered last will take effect.
  42. func Register(b Builder) {
  43. m[strings.ToLower(b.Name())] = b
  44. }
  45. // unregisterForTesting deletes the balancer with the given name from the
  46. // balancer map.
  47. //
  48. // This function is not thread-safe.
  49. func unregisterForTesting(name string) {
  50. delete(m, name)
  51. }
  52. func init() {
  53. internal.BalancerUnregister = unregisterForTesting
  54. }
  55. // Get returns the resolver builder registered with the given name.
  56. // Note that the compare is done in a case-insensitive fashion.
  57. // If no builder is register with the name, nil will be returned.
  58. func Get(name string) Builder {
  59. if b, ok := m[strings.ToLower(name)]; ok {
  60. return b
  61. }
  62. return nil
  63. }
  64. // SubConn represents a gRPC sub connection.
  65. // Each sub connection contains a list of addresses. gRPC will
  66. // try to connect to them (in sequence), and stop trying the
  67. // remainder once one connection is successful.
  68. //
  69. // The reconnect backoff will be applied on the list, not a single address.
  70. // For example, try_on_all_addresses -> backoff -> try_on_all_addresses.
  71. //
  72. // All SubConns start in IDLE, and will not try to connect. To trigger
  73. // the connecting, Balancers must call Connect.
  74. // When the connection encounters an error, it will reconnect immediately.
  75. // When the connection becomes IDLE, it will not reconnect unless Connect is
  76. // called.
  77. //
  78. // This interface is to be implemented by gRPC. Users should not need a
  79. // brand new implementation of this interface. For the situations like
  80. // testing, the new implementation should embed this interface. This allows
  81. // gRPC to add new methods to this interface.
  82. type SubConn interface {
  83. // UpdateAddresses updates the addresses used in this SubConn.
  84. // gRPC checks if currently-connected address is still in the new list.
  85. // If it's in the list, the connection will be kept.
  86. // If it's not in the list, the connection will gracefully closed, and
  87. // a new connection will be created.
  88. //
  89. // This will trigger a state transition for the SubConn.
  90. UpdateAddresses([]resolver.Address)
  91. // Connect starts the connecting for this SubConn.
  92. Connect()
  93. }
  94. // NewSubConnOptions contains options to create new SubConn.
  95. type NewSubConnOptions struct {
  96. // CredsBundle is the credentials bundle that will be used in the created
  97. // SubConn. If it's nil, the original creds from grpc DialOptions will be
  98. // used.
  99. CredsBundle credentials.Bundle
  100. // HealthCheckEnabled indicates whether health check service should be
  101. // enabled on this SubConn
  102. HealthCheckEnabled bool
  103. }
  104. // ClientConn represents a gRPC ClientConn.
  105. //
  106. // This interface is to be implemented by gRPC. Users should not need a
  107. // brand new implementation of this interface. For the situations like
  108. // testing, the new implementation should embed this interface. This allows
  109. // gRPC to add new methods to this interface.
  110. type ClientConn interface {
  111. // NewSubConn is called by balancer to create a new SubConn.
  112. // It doesn't block and wait for the connections to be established.
  113. // Behaviors of the SubConn can be controlled by options.
  114. NewSubConn([]resolver.Address, NewSubConnOptions) (SubConn, error)
  115. // RemoveSubConn removes the SubConn from ClientConn.
  116. // The SubConn will be shutdown.
  117. RemoveSubConn(SubConn)
  118. // UpdateBalancerState is called by balancer to notify gRPC that some internal
  119. // state in balancer has changed.
  120. //
  121. // gRPC will update the connectivity state of the ClientConn, and will call pick
  122. // on the new picker to pick new SubConn.
  123. UpdateBalancerState(s connectivity.State, p Picker)
  124. // ResolveNow is called by balancer to notify gRPC to do a name resolving.
  125. ResolveNow(resolver.ResolveNowOption)
  126. // Target returns the dial target for this ClientConn.
  127. //
  128. // Deprecated: Use the Target field in the BuildOptions instead.
  129. Target() string
  130. }
  131. // BuildOptions contains additional information for Build.
  132. type BuildOptions struct {
  133. // DialCreds is the transport credential the Balancer implementation can
  134. // use to dial to a remote load balancer server. The Balancer implementations
  135. // can ignore this if it does not need to talk to another party securely.
  136. DialCreds credentials.TransportCredentials
  137. // CredsBundle is the credentials bundle that the Balancer can use.
  138. CredsBundle credentials.Bundle
  139. // Dialer is the custom dialer the Balancer implementation can use to dial
  140. // to a remote load balancer server. The Balancer implementations
  141. // can ignore this if it doesn't need to talk to remote balancer.
  142. Dialer func(context.Context, string) (net.Conn, error)
  143. // ChannelzParentID is the entity parent's channelz unique identification number.
  144. ChannelzParentID int64
  145. // Target contains the parsed address info of the dial target. It is the same resolver.Target as
  146. // passed to the resolver.
  147. // See the documentation for the resolver.Target type for details about what it contains.
  148. Target resolver.Target
  149. }
  150. // Builder creates a balancer.
  151. type Builder interface {
  152. // Build creates a new balancer with the ClientConn.
  153. Build(cc ClientConn, opts BuildOptions) Balancer
  154. // Name returns the name of balancers built by this builder.
  155. // It will be used to pick balancers (for example in service config).
  156. Name() string
  157. }
  158. // PickOptions contains addition information for the Pick operation.
  159. type PickOptions struct {
  160. // FullMethodName is the method name that NewClientStream() is called
  161. // with. The canonical format is /service/Method.
  162. FullMethodName string
  163. }
  164. // DoneInfo contains additional information for done.
  165. type DoneInfo struct {
  166. // Err is the rpc error the RPC finished with. It could be nil.
  167. Err error
  168. // Trailer contains the metadata from the RPC's trailer, if present.
  169. Trailer metadata.MD
  170. // BytesSent indicates if any bytes have been sent to the server.
  171. BytesSent bool
  172. // BytesReceived indicates if any byte has been received from the server.
  173. BytesReceived bool
  174. // ServerLoad is the load received from server. It's usually sent as part of
  175. // trailing metadata.
  176. //
  177. // The only supported type now is *orca_v1.LoadReport.
  178. ServerLoad interface{}
  179. }
  180. var (
  181. // ErrNoSubConnAvailable indicates no SubConn is available for pick().
  182. // gRPC will block the RPC until a new picker is available via UpdateBalancerState().
  183. ErrNoSubConnAvailable = errors.New("no SubConn is available")
  184. // ErrTransientFailure indicates all SubConns are in TransientFailure.
  185. // WaitForReady RPCs will block, non-WaitForReady RPCs will fail.
  186. ErrTransientFailure = errors.New("all SubConns are in TransientFailure")
  187. )
  188. // Picker is used by gRPC to pick a SubConn to send an RPC.
  189. // Balancer is expected to generate a new picker from its snapshot every time its
  190. // internal state has changed.
  191. //
  192. // The pickers used by gRPC can be updated by ClientConn.UpdateBalancerState().
  193. type Picker interface {
  194. // Pick returns the SubConn to be used to send the RPC.
  195. // The returned SubConn must be one returned by NewSubConn().
  196. //
  197. // This functions is expected to return:
  198. // - a SubConn that is known to be READY;
  199. // - ErrNoSubConnAvailable if no SubConn is available, but progress is being
  200. // made (for example, some SubConn is in CONNECTING mode);
  201. // - other errors if no active connecting is happening (for example, all SubConn
  202. // are in TRANSIENT_FAILURE mode).
  203. //
  204. // If a SubConn is returned:
  205. // - If it is READY, gRPC will send the RPC on it;
  206. // - If it is not ready, or becomes not ready after it's returned, gRPC will
  207. // block until UpdateBalancerState() is called and will call pick on the
  208. // new picker. The done function returned from Pick(), if not nil, will be
  209. // called with nil error, no bytes sent and no bytes received.
  210. //
  211. // If the returned error is not nil:
  212. // - If the error is ErrNoSubConnAvailable, gRPC will block until UpdateBalancerState()
  213. // - If the error is ErrTransientFailure:
  214. // - If the RPC is wait-for-ready, gRPC will block until UpdateBalancerState()
  215. // is called to pick again;
  216. // - Otherwise, RPC will fail with unavailable error.
  217. // - Else (error is other non-nil error):
  218. // - The RPC will fail with unavailable error.
  219. //
  220. // The returned done() function will be called once the rpc has finished,
  221. // with the final status of that RPC. If the SubConn returned is not a
  222. // valid SubConn type, done may not be called. done may be nil if balancer
  223. // doesn't care about the RPC status.
  224. Pick(ctx context.Context, opts PickOptions) (conn SubConn, done func(DoneInfo), err error)
  225. }
  226. // Balancer takes input from gRPC, manages SubConns, and collects and aggregates
  227. // the connectivity states.
  228. //
  229. // It also generates and updates the Picker used by gRPC to pick SubConns for RPCs.
  230. //
  231. // HandleSubConnectionStateChange, HandleResolvedAddrs and Close are guaranteed
  232. // to be called synchronously from the same goroutine.
  233. // There's no guarantee on picker.Pick, it may be called anytime.
  234. type Balancer interface {
  235. // HandleSubConnStateChange is called by gRPC when the connectivity state
  236. // of sc has changed.
  237. // Balancer is expected to aggregate all the state of SubConn and report
  238. // that back to gRPC.
  239. // Balancer should also generate and update Pickers when its internal state has
  240. // been changed by the new state.
  241. //
  242. // Deprecated: if V2Balancer is implemented by the Balancer,
  243. // UpdateSubConnState will be called instead.
  244. HandleSubConnStateChange(sc SubConn, state connectivity.State)
  245. // HandleResolvedAddrs is called by gRPC to send updated resolved addresses to
  246. // balancers.
  247. // Balancer can create new SubConn or remove SubConn with the addresses.
  248. // An empty address slice and a non-nil error will be passed if the resolver returns
  249. // non-nil error to gRPC.
  250. //
  251. // Deprecated: if V2Balancer is implemented by the Balancer,
  252. // UpdateResolverState will be called instead.
  253. HandleResolvedAddrs([]resolver.Address, error)
  254. // Close closes the balancer. The balancer is not required to call
  255. // ClientConn.RemoveSubConn for its existing SubConns.
  256. Close()
  257. }
  258. // SubConnState describes the state of a SubConn.
  259. type SubConnState struct {
  260. ConnectivityState connectivity.State
  261. // TODO: add last connection error
  262. }
  263. // V2Balancer is defined for documentation purposes. If a Balancer also
  264. // implements V2Balancer, its UpdateResolverState method will be called instead
  265. // of HandleResolvedAddrs and its UpdateSubConnState will be called instead of
  266. // HandleSubConnStateChange.
  267. type V2Balancer interface {
  268. // UpdateResolverState is called by gRPC when the state of the resolver
  269. // changes.
  270. UpdateResolverState(resolver.State)
  271. // UpdateSubConnState is called by gRPC when the state of a SubConn
  272. // changes.
  273. UpdateSubConnState(SubConn, SubConnState)
  274. // Close closes the balancer. The balancer is not required to call
  275. // ClientConn.RemoveSubConn for its existing SubConns.
  276. Close()
  277. }
  278. // ConnectivityStateEvaluator takes the connectivity states of multiple SubConns
  279. // and returns one aggregated connectivity state.
  280. //
  281. // It's not thread safe.
  282. type ConnectivityStateEvaluator struct {
  283. numReady uint64 // Number of addrConns in ready state.
  284. numConnecting uint64 // Number of addrConns in connecting state.
  285. numTransientFailure uint64 // Number of addrConns in transientFailure.
  286. }
  287. // RecordTransition records state change happening in subConn and based on that
  288. // it evaluates what aggregated state should be.
  289. //
  290. // - If at least one SubConn in Ready, the aggregated state is Ready;
  291. // - Else if at least one SubConn in Connecting, the aggregated state is Connecting;
  292. // - Else the aggregated state is TransientFailure.
  293. //
  294. // Idle and Shutdown are not considered.
  295. func (cse *ConnectivityStateEvaluator) RecordTransition(oldState, newState connectivity.State) connectivity.State {
  296. // Update counters.
  297. for idx, state := range []connectivity.State{oldState, newState} {
  298. updateVal := 2*uint64(idx) - 1 // -1 for oldState and +1 for new.
  299. switch state {
  300. case connectivity.Ready:
  301. cse.numReady += updateVal
  302. case connectivity.Connecting:
  303. cse.numConnecting += updateVal
  304. case connectivity.TransientFailure:
  305. cse.numTransientFailure += updateVal
  306. }
  307. }
  308. // Evaluate.
  309. if cse.numReady > 0 {
  310. return connectivity.Ready
  311. }
  312. if cse.numConnecting > 0 {
  313. return connectivity.Connecting
  314. }
  315. return connectivity.TransientFailure
  316. }