balancer_conn_wrappers.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. "sync"
  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. // scStateUpdate contains the subConn and the new state it changed to.
  27. type scStateUpdate struct {
  28. sc balancer.SubConn
  29. state connectivity.State
  30. }
  31. // scStateUpdateBuffer is an unbounded channel for scStateChangeTuple.
  32. // TODO make a general purpose buffer that uses interface{}.
  33. type scStateUpdateBuffer struct {
  34. c chan *scStateUpdate
  35. mu sync.Mutex
  36. backlog []*scStateUpdate
  37. }
  38. func newSCStateUpdateBuffer() *scStateUpdateBuffer {
  39. return &scStateUpdateBuffer{
  40. c: make(chan *scStateUpdate, 1),
  41. }
  42. }
  43. func (b *scStateUpdateBuffer) put(t *scStateUpdate) {
  44. b.mu.Lock()
  45. defer b.mu.Unlock()
  46. if len(b.backlog) == 0 {
  47. select {
  48. case b.c <- t:
  49. return
  50. default:
  51. }
  52. }
  53. b.backlog = append(b.backlog, t)
  54. }
  55. func (b *scStateUpdateBuffer) load() {
  56. b.mu.Lock()
  57. defer b.mu.Unlock()
  58. if len(b.backlog) > 0 {
  59. select {
  60. case b.c <- b.backlog[0]:
  61. b.backlog[0] = nil
  62. b.backlog = b.backlog[1:]
  63. default:
  64. }
  65. }
  66. }
  67. // get returns the channel that receives a recvMsg in the buffer.
  68. //
  69. // Upon receiving, the caller should call load to send another
  70. // scStateChangeTuple onto the channel if there is any.
  71. func (b *scStateUpdateBuffer) get() <-chan *scStateUpdate {
  72. return b.c
  73. }
  74. // resolverUpdate contains the new resolved addresses or error if there's
  75. // any.
  76. type resolverUpdate struct {
  77. addrs []resolver.Address
  78. err error
  79. }
  80. // ccBalancerWrapper is a wrapper on top of cc for balancers.
  81. // It implements balancer.ClientConn interface.
  82. type ccBalancerWrapper struct {
  83. cc *ClientConn
  84. balancer balancer.Balancer
  85. stateChangeQueue *scStateUpdateBuffer
  86. resolverUpdateCh chan *resolverUpdate
  87. done chan struct{}
  88. }
  89. func newCCBalancerWrapper(cc *ClientConn, b balancer.Builder, bopts balancer.BuildOptions) *ccBalancerWrapper {
  90. ccb := &ccBalancerWrapper{
  91. cc: cc,
  92. stateChangeQueue: newSCStateUpdateBuffer(),
  93. resolverUpdateCh: make(chan *resolverUpdate, 1),
  94. done: make(chan struct{}),
  95. }
  96. go ccb.watcher()
  97. ccb.balancer = b.Build(ccb, bopts)
  98. return ccb
  99. }
  100. // watcher balancer functions sequencially, so the balancer can be implemeneted
  101. // lock-free.
  102. func (ccb *ccBalancerWrapper) watcher() {
  103. for {
  104. select {
  105. case t := <-ccb.stateChangeQueue.get():
  106. ccb.stateChangeQueue.load()
  107. ccb.balancer.HandleSubConnStateChange(t.sc, t.state)
  108. case t := <-ccb.resolverUpdateCh:
  109. ccb.balancer.HandleResolvedAddrs(t.addrs, t.err)
  110. case <-ccb.done:
  111. }
  112. select {
  113. case <-ccb.done:
  114. ccb.balancer.Close()
  115. return
  116. default:
  117. }
  118. }
  119. }
  120. func (ccb *ccBalancerWrapper) close() {
  121. close(ccb.done)
  122. }
  123. func (ccb *ccBalancerWrapper) handleSubConnStateChange(sc balancer.SubConn, s connectivity.State) {
  124. // When updating addresses for a SubConn, if the address in use is not in
  125. // the new addresses, the old ac will be tearDown() and a new ac will be
  126. // created. tearDown() generates a state change with Shutdown state, we
  127. // don't want the balancer to receive this state change. So before
  128. // tearDown() on the old ac, ac.acbw (acWrapper) will be set to nil, and
  129. // this function will be called with (nil, Shutdown). We don't need to call
  130. // balancer method in this case.
  131. if sc == nil {
  132. return
  133. }
  134. ccb.stateChangeQueue.put(&scStateUpdate{
  135. sc: sc,
  136. state: s,
  137. })
  138. }
  139. func (ccb *ccBalancerWrapper) handleResolvedAddrs(addrs []resolver.Address, err error) {
  140. select {
  141. case <-ccb.resolverUpdateCh:
  142. default:
  143. }
  144. ccb.resolverUpdateCh <- &resolverUpdate{
  145. addrs: addrs,
  146. err: err,
  147. }
  148. }
  149. func (ccb *ccBalancerWrapper) NewSubConn(addrs []resolver.Address, opts balancer.NewSubConnOptions) (balancer.SubConn, error) {
  150. grpclog.Infof("ccBalancerWrapper: new subconn: %v", addrs)
  151. ac, err := ccb.cc.newAddrConn(addrs)
  152. if err != nil {
  153. return nil, err
  154. }
  155. acbw := &acBalancerWrapper{ac: ac}
  156. ac.acbw = acbw
  157. return acbw, nil
  158. }
  159. func (ccb *ccBalancerWrapper) RemoveSubConn(sc balancer.SubConn) {
  160. grpclog.Infof("ccBalancerWrapper: removing subconn")
  161. acbw, ok := sc.(*acBalancerWrapper)
  162. if !ok {
  163. return
  164. }
  165. ccb.cc.removeAddrConn(acbw.getAddrConn(), errConnDrain)
  166. }
  167. func (ccb *ccBalancerWrapper) UpdateBalancerState(s connectivity.State, p balancer.Picker) {
  168. grpclog.Infof("ccBalancerWrapper: updating state and picker called by balancer: %v, %p", s, p)
  169. ccb.cc.csMgr.updateState(s)
  170. ccb.cc.blockingpicker.updatePicker(p)
  171. }
  172. func (ccb *ccBalancerWrapper) Target() string {
  173. return ccb.cc.target
  174. }
  175. // acBalancerWrapper is a wrapper on top of ac for balancers.
  176. // It implements balancer.SubConn interface.
  177. type acBalancerWrapper struct {
  178. mu sync.Mutex
  179. ac *addrConn
  180. }
  181. func (acbw *acBalancerWrapper) UpdateAddresses(addrs []resolver.Address) {
  182. grpclog.Infof("acBalancerWrapper: UpdateAddresses called with %v", addrs)
  183. acbw.mu.Lock()
  184. defer acbw.mu.Unlock()
  185. if !acbw.ac.tryUpdateAddrs(addrs) {
  186. cc := acbw.ac.cc
  187. acbw.ac.mu.Lock()
  188. // Set old ac.acbw to nil so the Shutdown state update will be ignored
  189. // by balancer.
  190. //
  191. // TODO(bar) the state transition could be wrong when tearDown() old ac
  192. // and creating new ac, fix the transition.
  193. acbw.ac.acbw = nil
  194. acbw.ac.mu.Unlock()
  195. acState := acbw.ac.getState()
  196. acbw.ac.tearDown(errConnDrain)
  197. if acState == connectivity.Shutdown {
  198. return
  199. }
  200. ac, err := cc.newAddrConn(addrs)
  201. if err != nil {
  202. grpclog.Warningf("acBalancerWrapper: UpdateAddresses: failed to newAddrConn: %v", err)
  203. return
  204. }
  205. acbw.ac = ac
  206. ac.acbw = acbw
  207. if acState != connectivity.Idle {
  208. ac.connect(false)
  209. }
  210. }
  211. }
  212. func (acbw *acBalancerWrapper) Connect() {
  213. acbw.mu.Lock()
  214. defer acbw.mu.Unlock()
  215. acbw.ac.connect(false)
  216. }
  217. func (acbw *acBalancerWrapper) getAddrConn() *addrConn {
  218. acbw.mu.Lock()
  219. defer acbw.mu.Unlock()
  220. return acbw.ac
  221. }