balancer.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. "golang.org/x/net/context"
  25. "google.golang.org/grpc/connectivity"
  26. "google.golang.org/grpc/credentials"
  27. "google.golang.org/grpc/resolver"
  28. )
  29. var (
  30. // m is a map from name to balancer builder.
  31. m = make(map[string]Builder)
  32. // defaultBuilder is the default balancer to use.
  33. defaultBuilder Builder // TODO(bar) install pickfirst as default.
  34. )
  35. // Register registers the balancer builder to the balancer map.
  36. // b.Name will be used as the name registered with this builder.
  37. func Register(b Builder) {
  38. m[b.Name()] = b
  39. }
  40. // Get returns the resolver builder registered with the given name.
  41. // If no builder is register with the name, the default pickfirst will
  42. // be used.
  43. func Get(name string) Builder {
  44. if b, ok := m[name]; ok {
  45. return b
  46. }
  47. return defaultBuilder
  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. type SubConn interface {
  63. // UpdateAddresses updates the addresses used in this SubConn.
  64. // gRPC checks if currently-connected address is still in the new list.
  65. // If it's in the list, the connection will be kept.
  66. // If it's not in the list, the connection will gracefully closed, and
  67. // a new connection will be created.
  68. //
  69. // This will trigger a state transition for the SubConn.
  70. UpdateAddresses([]resolver.Address)
  71. // Connect starts the connecting for this SubConn.
  72. Connect()
  73. }
  74. // NewSubConnOptions contains options to create new SubConn.
  75. type NewSubConnOptions struct{}
  76. // ClientConn represents a gRPC ClientConn.
  77. type ClientConn interface {
  78. // NewSubConn is called by balancer to create a new SubConn.
  79. // It doesn't block and wait for the connections to be established.
  80. // Behaviors of the SubConn can be controlled by options.
  81. NewSubConn([]resolver.Address, NewSubConnOptions) (SubConn, error)
  82. // RemoveSubConn removes the SubConn from ClientConn.
  83. // The SubConn will be shutdown.
  84. RemoveSubConn(SubConn)
  85. // UpdateBalancerState is called by balancer to nofity gRPC that some internal
  86. // state in balancer has changed.
  87. //
  88. // gRPC will update the connectivity state of the ClientConn, and will call pick
  89. // on the new picker to pick new SubConn.
  90. UpdateBalancerState(s connectivity.State, p Picker)
  91. // Target returns the dial target for this ClientConn.
  92. Target() string
  93. }
  94. // BuildOptions contains additional information for Build.
  95. type BuildOptions struct {
  96. // DialCreds is the transport credential the Balancer implementation can
  97. // use to dial to a remote load balancer server. The Balancer implementations
  98. // can ignore this if it does not need to talk to another party securely.
  99. DialCreds credentials.TransportCredentials
  100. // Dialer is the custom dialer the Balancer implementation can use to dial
  101. // to a remote load balancer server. The Balancer implementations
  102. // can ignore this if it doesn't need to talk to remote balancer.
  103. Dialer func(context.Context, string) (net.Conn, error)
  104. }
  105. // Builder creates a balancer.
  106. type Builder interface {
  107. // Build creates a new balancer with the ClientConn.
  108. Build(cc ClientConn, opts BuildOptions) Balancer
  109. // Name returns the name of balancers built by this builder.
  110. // It will be used to pick balancers (for example in service config).
  111. Name() string
  112. }
  113. // PickOptions contains addition information for the Pick operation.
  114. type PickOptions struct{}
  115. // DoneInfo contains additional information for done.
  116. type DoneInfo struct {
  117. // Err is the rpc error the RPC finished with. It could be nil.
  118. Err error
  119. }
  120. var (
  121. // ErrNoSubConnAvailable indicates no SubConn is available for pick().
  122. // gRPC will block the RPC until a new picker is available via UpdateBalancerState().
  123. ErrNoSubConnAvailable = errors.New("no SubConn is available")
  124. // ErrTransientFailure indicates all SubConns are in TransientFailure.
  125. // WaitForReady RPCs will block, non-WaitForReady RPCs will fail.
  126. ErrTransientFailure = errors.New("all SubConns are in TransientFailure")
  127. )
  128. // Picker is used by gRPC to pick a SubConn to send an RPC.
  129. // Balancer is expected to generate a new picker from its snapshot everytime its
  130. // internal state has changed.
  131. //
  132. // The pickers used by gRPC can be updated by ClientConn.UpdateBalancerState().
  133. type Picker interface {
  134. // Pick returns the SubConn to be used to send the RPC.
  135. // The returned SubConn must be one returned by NewSubConn().
  136. //
  137. // This functions is expected to return:
  138. // - a SubConn that is known to be READY;
  139. // - ErrNoSubConnAvailable if no SubConn is available, but progress is being
  140. // made (for example, some SubConn is in CONNECTING mode);
  141. // - other errors if no active connecting is happening (for example, all SubConn
  142. // are in TRANSIENT_FAILURE mode).
  143. //
  144. // If a SubConn is returned:
  145. // - If it is READY, gRPC will send the RPC on it;
  146. // - If it is not ready, or becomes not ready after it's returned, gRPC will block
  147. // this call until a new picker is updated and will call pick on the new picker.
  148. //
  149. // If the returned error is not nil:
  150. // - If the error is ErrNoSubConnAvailable, gRPC will block until UpdateBalancerState()
  151. // - If the error is ErrTransientFailure:
  152. // - If the RPC is wait-for-ready, gRPC will block until UpdateBalancerState()
  153. // is called to pick again;
  154. // - Otherwise, RPC will fail with unavailable error.
  155. // - Else (error is other non-nil error):
  156. // - The RPC will fail with unavailable error.
  157. //
  158. // The returned done() function will be called once the rpc has finished, with the
  159. // final status of that RPC.
  160. // done may be nil if balancer doesn't care about the RPC status.
  161. Pick(ctx context.Context, opts PickOptions) (conn SubConn, done func(DoneInfo), err error)
  162. }
  163. // Balancer takes input from gRPC, manages SubConns, and collects and aggregates
  164. // the connectivity states.
  165. //
  166. // It also generates and updates the Picker used by gRPC to pick SubConns for RPCs.
  167. //
  168. // HandleSubConnectionStateChange, HandleResolvedAddrs and Close are guaranteed
  169. // to be called synchronously from the same goroutine.
  170. // There's no guarantee on picker.Pick, it may be called anytime.
  171. type Balancer interface {
  172. // HandleSubConnStateChange is called by gRPC when the connectivity state
  173. // of sc has changed.
  174. // Balancer is expected to aggregate all the state of SubConn and report
  175. // that back to gRPC.
  176. // Balancer should also generate and update Pickers when its internal state has
  177. // been changed by the new state.
  178. HandleSubConnStateChange(sc SubConn, state connectivity.State)
  179. // HandleResolvedAddrs is called by gRPC to send updated resolved addresses to
  180. // balancers.
  181. // Balancer can create new SubConn or remove SubConn with the addresses.
  182. // An empty address slice and a non-nil error will be passed if the resolver returns
  183. // non-nil error to gRPC.
  184. HandleResolvedAddrs([]resolver.Address, error)
  185. // Close closes the balancer. The balancer is not required to call
  186. // ClientConn.RemoveSubConn for its existing SubConns.
  187. Close()
  188. }