balancer_conn_wrappers.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 grpc
  19. import (
  20. "fmt"
  21. "sync"
  22. "google.golang.org/grpc/balancer"
  23. "google.golang.org/grpc/connectivity"
  24. "google.golang.org/grpc/internal/buffer"
  25. "google.golang.org/grpc/internal/channelz"
  26. "google.golang.org/grpc/internal/grpcsync"
  27. "google.golang.org/grpc/resolver"
  28. )
  29. // scStateUpdate contains the subConn and the new state it changed to.
  30. type scStateUpdate struct {
  31. sc balancer.SubConn
  32. state connectivity.State
  33. err error
  34. }
  35. // ccBalancerWrapper is a wrapper on top of cc for balancers.
  36. // It implements balancer.ClientConn interface.
  37. type ccBalancerWrapper struct {
  38. cc *ClientConn
  39. balancerMu sync.Mutex // synchronizes calls to the balancer
  40. balancer balancer.Balancer
  41. scBuffer *buffer.Unbounded
  42. done *grpcsync.Event
  43. mu sync.Mutex
  44. subConns map[*acBalancerWrapper]struct{}
  45. }
  46. func newCCBalancerWrapper(cc *ClientConn, b balancer.Builder, bopts balancer.BuildOptions) *ccBalancerWrapper {
  47. ccb := &ccBalancerWrapper{
  48. cc: cc,
  49. scBuffer: buffer.NewUnbounded(),
  50. done: grpcsync.NewEvent(),
  51. subConns: make(map[*acBalancerWrapper]struct{}),
  52. }
  53. go ccb.watcher()
  54. ccb.balancer = b.Build(ccb, bopts)
  55. return ccb
  56. }
  57. // watcher balancer functions sequentially, so the balancer can be implemented
  58. // lock-free.
  59. func (ccb *ccBalancerWrapper) watcher() {
  60. for {
  61. select {
  62. case t := <-ccb.scBuffer.Get():
  63. ccb.scBuffer.Load()
  64. if ccb.done.HasFired() {
  65. break
  66. }
  67. ccb.balancerMu.Lock()
  68. su := t.(*scStateUpdate)
  69. if ub, ok := ccb.balancer.(balancer.V2Balancer); ok {
  70. ub.UpdateSubConnState(su.sc, balancer.SubConnState{ConnectivityState: su.state, ConnectionError: su.err})
  71. } else {
  72. ccb.balancer.HandleSubConnStateChange(su.sc, su.state)
  73. }
  74. ccb.balancerMu.Unlock()
  75. case <-ccb.done.Done():
  76. }
  77. if ccb.done.HasFired() {
  78. ccb.balancer.Close()
  79. ccb.mu.Lock()
  80. scs := ccb.subConns
  81. ccb.subConns = nil
  82. ccb.mu.Unlock()
  83. for acbw := range scs {
  84. ccb.cc.removeAddrConn(acbw.getAddrConn(), errConnDrain)
  85. }
  86. ccb.UpdateState(balancer.State{ConnectivityState: connectivity.Connecting, Picker: nil})
  87. return
  88. }
  89. }
  90. }
  91. func (ccb *ccBalancerWrapper) close() {
  92. ccb.done.Fire()
  93. }
  94. func (ccb *ccBalancerWrapper) handleSubConnStateChange(sc balancer.SubConn, s connectivity.State, err error) {
  95. // When updating addresses for a SubConn, if the address in use is not in
  96. // the new addresses, the old ac will be tearDown() and a new ac will be
  97. // created. tearDown() generates a state change with Shutdown state, we
  98. // don't want the balancer to receive this state change. So before
  99. // tearDown() on the old ac, ac.acbw (acWrapper) will be set to nil, and
  100. // this function will be called with (nil, Shutdown). We don't need to call
  101. // balancer method in this case.
  102. if sc == nil {
  103. return
  104. }
  105. ccb.scBuffer.Put(&scStateUpdate{
  106. sc: sc,
  107. state: s,
  108. err: err,
  109. })
  110. }
  111. func (ccb *ccBalancerWrapper) updateClientConnState(ccs *balancer.ClientConnState) error {
  112. ccb.balancerMu.Lock()
  113. defer ccb.balancerMu.Unlock()
  114. if ub, ok := ccb.balancer.(balancer.V2Balancer); ok {
  115. return ub.UpdateClientConnState(*ccs)
  116. }
  117. ccb.balancer.HandleResolvedAddrs(ccs.ResolverState.Addresses, nil)
  118. return nil
  119. }
  120. func (ccb *ccBalancerWrapper) resolverError(err error) {
  121. if ub, ok := ccb.balancer.(balancer.V2Balancer); ok {
  122. ccb.balancerMu.Lock()
  123. ub.ResolverError(err)
  124. ccb.balancerMu.Unlock()
  125. }
  126. }
  127. func (ccb *ccBalancerWrapper) NewSubConn(addrs []resolver.Address, opts balancer.NewSubConnOptions) (balancer.SubConn, error) {
  128. if len(addrs) <= 0 {
  129. return nil, fmt.Errorf("grpc: cannot create SubConn with empty address list")
  130. }
  131. ccb.mu.Lock()
  132. defer ccb.mu.Unlock()
  133. if ccb.subConns == nil {
  134. return nil, fmt.Errorf("grpc: ClientConn balancer wrapper was closed")
  135. }
  136. ac, err := ccb.cc.newAddrConn(addrs, opts)
  137. if err != nil {
  138. return nil, err
  139. }
  140. acbw := &acBalancerWrapper{ac: ac}
  141. acbw.ac.mu.Lock()
  142. ac.acbw = acbw
  143. acbw.ac.mu.Unlock()
  144. ccb.subConns[acbw] = struct{}{}
  145. return acbw, nil
  146. }
  147. func (ccb *ccBalancerWrapper) RemoveSubConn(sc balancer.SubConn) {
  148. acbw, ok := sc.(*acBalancerWrapper)
  149. if !ok {
  150. return
  151. }
  152. ccb.mu.Lock()
  153. defer ccb.mu.Unlock()
  154. if ccb.subConns == nil {
  155. return
  156. }
  157. delete(ccb.subConns, acbw)
  158. ccb.cc.removeAddrConn(acbw.getAddrConn(), errConnDrain)
  159. }
  160. func (ccb *ccBalancerWrapper) UpdateBalancerState(s connectivity.State, p balancer.Picker) {
  161. ccb.mu.Lock()
  162. defer ccb.mu.Unlock()
  163. if ccb.subConns == nil {
  164. return
  165. }
  166. // Update picker before updating state. Even though the ordering here does
  167. // not matter, it can lead to multiple calls of Pick in the common start-up
  168. // case where we wait for ready and then perform an RPC. If the picker is
  169. // updated later, we could call the "connecting" picker when the state is
  170. // updated, and then call the "ready" picker after the picker gets updated.
  171. ccb.cc.blockingpicker.updatePicker(p)
  172. ccb.cc.csMgr.updateState(s)
  173. }
  174. func (ccb *ccBalancerWrapper) UpdateState(s balancer.State) {
  175. ccb.mu.Lock()
  176. defer ccb.mu.Unlock()
  177. if ccb.subConns == nil {
  178. return
  179. }
  180. // Update picker before updating state. Even though the ordering here does
  181. // not matter, it can lead to multiple calls of Pick in the common start-up
  182. // case where we wait for ready and then perform an RPC. If the picker is
  183. // updated later, we could call the "connecting" picker when the state is
  184. // updated, and then call the "ready" picker after the picker gets updated.
  185. ccb.cc.blockingpicker.updatePickerV2(s.Picker)
  186. ccb.cc.csMgr.updateState(s.ConnectivityState)
  187. }
  188. func (ccb *ccBalancerWrapper) ResolveNow(o resolver.ResolveNowOptions) {
  189. ccb.cc.resolveNow(o)
  190. }
  191. func (ccb *ccBalancerWrapper) Target() string {
  192. return ccb.cc.target
  193. }
  194. // acBalancerWrapper is a wrapper on top of ac for balancers.
  195. // It implements balancer.SubConn interface.
  196. type acBalancerWrapper struct {
  197. mu sync.Mutex
  198. ac *addrConn
  199. }
  200. func (acbw *acBalancerWrapper) UpdateAddresses(addrs []resolver.Address) {
  201. acbw.mu.Lock()
  202. defer acbw.mu.Unlock()
  203. if len(addrs) <= 0 {
  204. acbw.ac.tearDown(errConnDrain)
  205. return
  206. }
  207. if !acbw.ac.tryUpdateAddrs(addrs) {
  208. cc := acbw.ac.cc
  209. opts := acbw.ac.scopts
  210. acbw.ac.mu.Lock()
  211. // Set old ac.acbw to nil so the Shutdown state update will be ignored
  212. // by balancer.
  213. //
  214. // TODO(bar) the state transition could be wrong when tearDown() old ac
  215. // and creating new ac, fix the transition.
  216. acbw.ac.acbw = nil
  217. acbw.ac.mu.Unlock()
  218. acState := acbw.ac.getState()
  219. acbw.ac.tearDown(errConnDrain)
  220. if acState == connectivity.Shutdown {
  221. return
  222. }
  223. ac, err := cc.newAddrConn(addrs, opts)
  224. if err != nil {
  225. channelz.Warningf(acbw.ac.channelzID, "acBalancerWrapper: UpdateAddresses: failed to newAddrConn: %v", err)
  226. return
  227. }
  228. acbw.ac = ac
  229. ac.mu.Lock()
  230. ac.acbw = acbw
  231. ac.mu.Unlock()
  232. if acState != connectivity.Idle {
  233. ac.connect()
  234. }
  235. }
  236. }
  237. func (acbw *acBalancerWrapper) Connect() {
  238. acbw.mu.Lock()
  239. defer acbw.mu.Unlock()
  240. acbw.ac.connect()
  241. }
  242. func (acbw *acBalancerWrapper) getAddrConn() *addrConn {
  243. acbw.mu.Lock()
  244. defer acbw.mu.Unlock()
  245. return acbw.ac
  246. }