balancer.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 base
  19. import (
  20. "errors"
  21. "fmt"
  22. "google.golang.org/grpc/balancer"
  23. "google.golang.org/grpc/connectivity"
  24. "google.golang.org/grpc/grpclog"
  25. "google.golang.org/grpc/resolver"
  26. )
  27. var logger = grpclog.Component("balancer")
  28. type baseBuilder struct {
  29. name string
  30. pickerBuilder PickerBuilder
  31. config Config
  32. }
  33. func (bb *baseBuilder) Build(cc balancer.ClientConn, opt balancer.BuildOptions) balancer.Balancer {
  34. bal := &baseBalancer{
  35. cc: cc,
  36. pickerBuilder: bb.pickerBuilder,
  37. subConns: resolver.NewAddressMap(),
  38. scStates: make(map[balancer.SubConn]connectivity.State),
  39. csEvltr: &balancer.ConnectivityStateEvaluator{},
  40. config: bb.config,
  41. state: connectivity.Connecting,
  42. }
  43. // Initialize picker to a picker that always returns
  44. // ErrNoSubConnAvailable, because when state of a SubConn changes, we
  45. // may call UpdateState with this picker.
  46. bal.picker = NewErrPicker(balancer.ErrNoSubConnAvailable)
  47. return bal
  48. }
  49. func (bb *baseBuilder) Name() string {
  50. return bb.name
  51. }
  52. type baseBalancer struct {
  53. cc balancer.ClientConn
  54. pickerBuilder PickerBuilder
  55. csEvltr *balancer.ConnectivityStateEvaluator
  56. state connectivity.State
  57. subConns *resolver.AddressMap
  58. scStates map[balancer.SubConn]connectivity.State
  59. picker balancer.Picker
  60. config Config
  61. resolverErr error // the last error reported by the resolver; cleared on successful resolution
  62. connErr error // the last connection error; cleared upon leaving TransientFailure
  63. }
  64. func (b *baseBalancer) ResolverError(err error) {
  65. b.resolverErr = err
  66. if b.subConns.Len() == 0 {
  67. b.state = connectivity.TransientFailure
  68. }
  69. if b.state != connectivity.TransientFailure {
  70. // The picker will not change since the balancer does not currently
  71. // report an error.
  72. return
  73. }
  74. b.regeneratePicker()
  75. b.cc.UpdateState(balancer.State{
  76. ConnectivityState: b.state,
  77. Picker: b.picker,
  78. })
  79. }
  80. func (b *baseBalancer) UpdateClientConnState(s balancer.ClientConnState) error {
  81. // TODO: handle s.ResolverState.ServiceConfig?
  82. if logger.V(2) {
  83. logger.Info("base.baseBalancer: got new ClientConn state: ", s)
  84. }
  85. // Successful resolution; clear resolver error and ensure we return nil.
  86. b.resolverErr = nil
  87. // addrsSet is the set converted from addrs, it's used for quick lookup of an address.
  88. addrsSet := resolver.NewAddressMap()
  89. for _, a := range s.ResolverState.Addresses {
  90. addrsSet.Set(a, nil)
  91. if _, ok := b.subConns.Get(a); !ok {
  92. // a is a new address (not existing in b.subConns).
  93. sc, err := b.cc.NewSubConn([]resolver.Address{a}, balancer.NewSubConnOptions{HealthCheckEnabled: b.config.HealthCheck})
  94. if err != nil {
  95. logger.Warningf("base.baseBalancer: failed to create new SubConn: %v", err)
  96. continue
  97. }
  98. b.subConns.Set(a, sc)
  99. b.scStates[sc] = connectivity.Idle
  100. b.csEvltr.RecordTransition(connectivity.Shutdown, connectivity.Idle)
  101. sc.Connect()
  102. }
  103. }
  104. for _, a := range b.subConns.Keys() {
  105. sci, _ := b.subConns.Get(a)
  106. sc := sci.(balancer.SubConn)
  107. // a was removed by resolver.
  108. if _, ok := addrsSet.Get(a); !ok {
  109. b.cc.RemoveSubConn(sc)
  110. b.subConns.Delete(a)
  111. // Keep the state of this sc in b.scStates until sc's state becomes Shutdown.
  112. // The entry will be deleted in UpdateSubConnState.
  113. }
  114. }
  115. // If resolver state contains no addresses, return an error so ClientConn
  116. // will trigger re-resolve. Also records this as an resolver error, so when
  117. // the overall state turns transient failure, the error message will have
  118. // the zero address information.
  119. if len(s.ResolverState.Addresses) == 0 {
  120. b.ResolverError(errors.New("produced zero addresses"))
  121. return balancer.ErrBadResolverState
  122. }
  123. b.regeneratePicker()
  124. b.cc.UpdateState(balancer.State{ConnectivityState: b.state, Picker: b.picker})
  125. return nil
  126. }
  127. // mergeErrors builds an error from the last connection error and the last
  128. // resolver error. Must only be called if b.state is TransientFailure.
  129. func (b *baseBalancer) mergeErrors() error {
  130. // connErr must always be non-nil unless there are no SubConns, in which
  131. // case resolverErr must be non-nil.
  132. if b.connErr == nil {
  133. return fmt.Errorf("last resolver error: %v", b.resolverErr)
  134. }
  135. if b.resolverErr == nil {
  136. return fmt.Errorf("last connection error: %v", b.connErr)
  137. }
  138. return fmt.Errorf("last connection error: %v; last resolver error: %v", b.connErr, b.resolverErr)
  139. }
  140. // regeneratePicker takes a snapshot of the balancer, and generates a picker
  141. // from it. The picker is
  142. // - errPicker if the balancer is in TransientFailure,
  143. // - built by the pickerBuilder with all READY SubConns otherwise.
  144. func (b *baseBalancer) regeneratePicker() {
  145. if b.state == connectivity.TransientFailure {
  146. b.picker = NewErrPicker(b.mergeErrors())
  147. return
  148. }
  149. readySCs := make(map[balancer.SubConn]SubConnInfo)
  150. // Filter out all ready SCs from full subConn map.
  151. for _, addr := range b.subConns.Keys() {
  152. sci, _ := b.subConns.Get(addr)
  153. sc := sci.(balancer.SubConn)
  154. if st, ok := b.scStates[sc]; ok && st == connectivity.Ready {
  155. readySCs[sc] = SubConnInfo{Address: addr}
  156. }
  157. }
  158. b.picker = b.pickerBuilder.Build(PickerBuildInfo{ReadySCs: readySCs})
  159. }
  160. func (b *baseBalancer) UpdateSubConnState(sc balancer.SubConn, state balancer.SubConnState) {
  161. s := state.ConnectivityState
  162. if logger.V(2) {
  163. logger.Infof("base.baseBalancer: handle SubConn state change: %p, %v", sc, s)
  164. }
  165. oldS, ok := b.scStates[sc]
  166. if !ok {
  167. if logger.V(2) {
  168. logger.Infof("base.baseBalancer: got state changes for an unknown SubConn: %p, %v", sc, s)
  169. }
  170. return
  171. }
  172. if oldS == connectivity.TransientFailure &&
  173. (s == connectivity.Connecting || s == connectivity.Idle) {
  174. // Once a subconn enters TRANSIENT_FAILURE, ignore subsequent IDLE or
  175. // CONNECTING transitions to prevent the aggregated state from being
  176. // always CONNECTING when many backends exist but are all down.
  177. if s == connectivity.Idle {
  178. sc.Connect()
  179. }
  180. return
  181. }
  182. b.scStates[sc] = s
  183. switch s {
  184. case connectivity.Idle:
  185. sc.Connect()
  186. case connectivity.Shutdown:
  187. // When an address was removed by resolver, b called RemoveSubConn but
  188. // kept the sc's state in scStates. Remove state for this sc here.
  189. delete(b.scStates, sc)
  190. case connectivity.TransientFailure:
  191. // Save error to be reported via picker.
  192. b.connErr = state.ConnectionError
  193. }
  194. b.state = b.csEvltr.RecordTransition(oldS, s)
  195. // Regenerate picker when one of the following happens:
  196. // - this sc entered or left ready
  197. // - the aggregated state of balancer is TransientFailure
  198. // (may need to update error message)
  199. if (s == connectivity.Ready) != (oldS == connectivity.Ready) ||
  200. b.state == connectivity.TransientFailure {
  201. b.regeneratePicker()
  202. }
  203. b.cc.UpdateState(balancer.State{ConnectivityState: b.state, Picker: b.picker})
  204. }
  205. // Close is a nop because base balancer doesn't have internal state to clean up,
  206. // and it doesn't need to call RemoveSubConn for the SubConns.
  207. func (b *baseBalancer) Close() {
  208. }
  209. // ExitIdle is a nop because the base balancer attempts to stay connected to
  210. // all SubConns at all times.
  211. func (b *baseBalancer) ExitIdle() {
  212. }
  213. // NewErrPicker returns a Picker that always returns err on Pick().
  214. func NewErrPicker(err error) balancer.Picker {
  215. return &errPicker{err: err}
  216. }
  217. // NewErrPickerV2 is temporarily defined for backward compatibility reasons.
  218. //
  219. // Deprecated: use NewErrPicker instead.
  220. var NewErrPickerV2 = NewErrPicker
  221. type errPicker struct {
  222. err error // Pick() always returns this err.
  223. }
  224. func (p *errPicker) Pick(info balancer.PickInfo) (balancer.PickResult, error) {
  225. return balancer.PickResult{}, p.err
  226. }