balancer.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. "context"
  21. "google.golang.org/grpc/balancer"
  22. "google.golang.org/grpc/connectivity"
  23. "google.golang.org/grpc/grpclog"
  24. "google.golang.org/grpc/resolver"
  25. )
  26. type baseBuilder struct {
  27. name string
  28. pickerBuilder PickerBuilder
  29. config Config
  30. }
  31. func (bb *baseBuilder) Build(cc balancer.ClientConn, opt balancer.BuildOptions) balancer.Balancer {
  32. return &baseBalancer{
  33. cc: cc,
  34. pickerBuilder: bb.pickerBuilder,
  35. subConns: make(map[resolver.Address]balancer.SubConn),
  36. scStates: make(map[balancer.SubConn]connectivity.State),
  37. csEvltr: &connectivityStateEvaluator{},
  38. // Initialize picker to a picker that always return
  39. // ErrNoSubConnAvailable, because when state of a SubConn changes, we
  40. // may call UpdateBalancerState with this picker.
  41. picker: NewErrPicker(balancer.ErrNoSubConnAvailable),
  42. config: bb.config,
  43. }
  44. }
  45. func (bb *baseBuilder) Name() string {
  46. return bb.name
  47. }
  48. type baseBalancer struct {
  49. cc balancer.ClientConn
  50. pickerBuilder PickerBuilder
  51. csEvltr *connectivityStateEvaluator
  52. state connectivity.State
  53. subConns map[resolver.Address]balancer.SubConn
  54. scStates map[balancer.SubConn]connectivity.State
  55. picker balancer.Picker
  56. config Config
  57. }
  58. func (b *baseBalancer) HandleResolvedAddrs(addrs []resolver.Address, err error) {
  59. if err != nil {
  60. grpclog.Infof("base.baseBalancer: HandleResolvedAddrs called with error %v", err)
  61. return
  62. }
  63. grpclog.Infoln("base.baseBalancer: got new resolved addresses: ", addrs)
  64. // addrsSet is the set converted from addrs, it's used for quick lookup of an address.
  65. addrsSet := make(map[resolver.Address]struct{})
  66. for _, a := range addrs {
  67. addrsSet[a] = struct{}{}
  68. if _, ok := b.subConns[a]; !ok {
  69. // a is a new address (not existing in b.subConns).
  70. sc, err := b.cc.NewSubConn([]resolver.Address{a}, balancer.NewSubConnOptions{HealthCheckEnabled: b.config.HealthCheck})
  71. if err != nil {
  72. grpclog.Warningf("base.baseBalancer: failed to create new SubConn: %v", err)
  73. continue
  74. }
  75. b.subConns[a] = sc
  76. b.scStates[sc] = connectivity.Idle
  77. sc.Connect()
  78. }
  79. }
  80. for a, sc := range b.subConns {
  81. // a was removed by resolver.
  82. if _, ok := addrsSet[a]; !ok {
  83. b.cc.RemoveSubConn(sc)
  84. delete(b.subConns, a)
  85. // Keep the state of this sc in b.scStates until sc's state becomes Shutdown.
  86. // The entry will be deleted in HandleSubConnStateChange.
  87. }
  88. }
  89. }
  90. // regeneratePicker takes a snapshot of the balancer, and generates a picker
  91. // from it. The picker is
  92. // - errPicker with ErrTransientFailure if the balancer is in TransientFailure,
  93. // - built by the pickerBuilder with all READY SubConns otherwise.
  94. func (b *baseBalancer) regeneratePicker() {
  95. if b.state == connectivity.TransientFailure {
  96. b.picker = NewErrPicker(balancer.ErrTransientFailure)
  97. return
  98. }
  99. readySCs := make(map[resolver.Address]balancer.SubConn)
  100. // Filter out all ready SCs from full subConn map.
  101. for addr, sc := range b.subConns {
  102. if st, ok := b.scStates[sc]; ok && st == connectivity.Ready {
  103. readySCs[addr] = sc
  104. }
  105. }
  106. b.picker = b.pickerBuilder.Build(readySCs)
  107. }
  108. func (b *baseBalancer) HandleSubConnStateChange(sc balancer.SubConn, s connectivity.State) {
  109. grpclog.Infof("base.baseBalancer: handle SubConn state change: %p, %v", sc, s)
  110. oldS, ok := b.scStates[sc]
  111. if !ok {
  112. grpclog.Infof("base.baseBalancer: got state changes for an unknown SubConn: %p, %v", sc, s)
  113. return
  114. }
  115. b.scStates[sc] = s
  116. switch s {
  117. case connectivity.Idle:
  118. sc.Connect()
  119. case connectivity.Shutdown:
  120. // When an address was removed by resolver, b called RemoveSubConn but
  121. // kept the sc's state in scStates. Remove state for this sc here.
  122. delete(b.scStates, sc)
  123. }
  124. oldAggrState := b.state
  125. b.state = b.csEvltr.recordTransition(oldS, s)
  126. // Regenerate picker when one of the following happens:
  127. // - this sc became ready from not-ready
  128. // - this sc became not-ready from ready
  129. // - the aggregated state of balancer became TransientFailure from non-TransientFailure
  130. // - the aggregated state of balancer became non-TransientFailure from TransientFailure
  131. if (s == connectivity.Ready) != (oldS == connectivity.Ready) ||
  132. (b.state == connectivity.TransientFailure) != (oldAggrState == connectivity.TransientFailure) {
  133. b.regeneratePicker()
  134. }
  135. b.cc.UpdateBalancerState(b.state, b.picker)
  136. }
  137. // Close is a nop because base balancer doesn't have internal state to clean up,
  138. // and it doesn't need to call RemoveSubConn for the SubConns.
  139. func (b *baseBalancer) Close() {
  140. }
  141. // NewErrPicker returns a picker that always returns err on Pick().
  142. func NewErrPicker(err error) balancer.Picker {
  143. return &errPicker{err: err}
  144. }
  145. type errPicker struct {
  146. err error // Pick() always returns this err.
  147. }
  148. func (p *errPicker) Pick(ctx context.Context, opts balancer.PickOptions) (balancer.SubConn, func(balancer.DoneInfo), error) {
  149. return nil, nil, p.err
  150. }
  151. // connectivityStateEvaluator gets updated by addrConns when their
  152. // states transition, based on which it evaluates the state of
  153. // ClientConn.
  154. type connectivityStateEvaluator struct {
  155. numReady uint64 // Number of addrConns in ready state.
  156. numConnecting uint64 // Number of addrConns in connecting state.
  157. numTransientFailure uint64 // Number of addrConns in transientFailure.
  158. }
  159. // recordTransition records state change happening in every subConn and based on
  160. // that it evaluates what aggregated state should be.
  161. // It can only transition between Ready, Connecting and TransientFailure. Other states,
  162. // Idle and Shutdown are transitioned into by ClientConn; in the beginning of the connection
  163. // before any subConn is created ClientConn is in idle state. In the end when ClientConn
  164. // closes it is in Shutdown state.
  165. //
  166. // recordTransition should only be called synchronously from the same goroutine.
  167. func (cse *connectivityStateEvaluator) recordTransition(oldState, newState connectivity.State) connectivity.State {
  168. // Update counters.
  169. for idx, state := range []connectivity.State{oldState, newState} {
  170. updateVal := 2*uint64(idx) - 1 // -1 for oldState and +1 for new.
  171. switch state {
  172. case connectivity.Ready:
  173. cse.numReady += updateVal
  174. case connectivity.Connecting:
  175. cse.numConnecting += updateVal
  176. case connectivity.TransientFailure:
  177. cse.numTransientFailure += updateVal
  178. }
  179. }
  180. // Evaluate.
  181. if cse.numReady > 0 {
  182. return connectivity.Ready
  183. }
  184. if cse.numConnecting > 0 {
  185. return connectivity.Connecting
  186. }
  187. return connectivity.TransientFailure
  188. }