balancer_conn_wrappers.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. "context"
  21. "fmt"
  22. "strings"
  23. "sync"
  24. "google.golang.org/grpc/balancer"
  25. "google.golang.org/grpc/connectivity"
  26. "google.golang.org/grpc/internal/balancer/gracefulswitch"
  27. "google.golang.org/grpc/internal/channelz"
  28. "google.golang.org/grpc/internal/grpcsync"
  29. "google.golang.org/grpc/resolver"
  30. )
  31. type ccbMode int
  32. const (
  33. ccbModeActive = iota
  34. ccbModeIdle
  35. ccbModeClosed
  36. ccbModeExitingIdle
  37. )
  38. // ccBalancerWrapper sits between the ClientConn and the Balancer.
  39. //
  40. // ccBalancerWrapper implements methods corresponding to the ones on the
  41. // balancer.Balancer interface. The ClientConn is free to call these methods
  42. // concurrently and the ccBalancerWrapper ensures that calls from the ClientConn
  43. // to the Balancer happen synchronously and in order.
  44. //
  45. // ccBalancerWrapper also implements the balancer.ClientConn interface and is
  46. // passed to the Balancer implementations. It invokes unexported methods on the
  47. // ClientConn to handle these calls from the Balancer.
  48. //
  49. // It uses the gracefulswitch.Balancer internally to ensure that balancer
  50. // switches happen in a graceful manner.
  51. type ccBalancerWrapper struct {
  52. // The following fields are initialized when the wrapper is created and are
  53. // read-only afterwards, and therefore can be accessed without a mutex.
  54. cc *ClientConn
  55. opts balancer.BuildOptions
  56. // Outgoing (gRPC --> balancer) calls are guaranteed to execute in a
  57. // mutually exclusive manner as they are scheduled in the serializer. Fields
  58. // accessed *only* in these serializer callbacks, can therefore be accessed
  59. // without a mutex.
  60. balancer *gracefulswitch.Balancer
  61. curBalancerName string
  62. // mu guards access to the below fields. Access to the serializer and its
  63. // cancel function needs to be mutex protected because they are overwritten
  64. // when the wrapper exits idle mode.
  65. mu sync.Mutex
  66. serializer *grpcsync.CallbackSerializer // To serialize all outoing calls.
  67. serializerCancel context.CancelFunc // To close the seralizer at close/enterIdle time.
  68. mode ccbMode // Tracks the current mode of the wrapper.
  69. }
  70. // newCCBalancerWrapper creates a new balancer wrapper. The underlying balancer
  71. // is not created until the switchTo() method is invoked.
  72. func newCCBalancerWrapper(cc *ClientConn, bopts balancer.BuildOptions) *ccBalancerWrapper {
  73. ctx, cancel := context.WithCancel(context.Background())
  74. ccb := &ccBalancerWrapper{
  75. cc: cc,
  76. opts: bopts,
  77. serializer: grpcsync.NewCallbackSerializer(ctx),
  78. serializerCancel: cancel,
  79. }
  80. ccb.balancer = gracefulswitch.NewBalancer(ccb, bopts)
  81. return ccb
  82. }
  83. // updateClientConnState is invoked by grpc to push a ClientConnState update to
  84. // the underlying balancer.
  85. func (ccb *ccBalancerWrapper) updateClientConnState(ccs *balancer.ClientConnState) error {
  86. ccb.mu.Lock()
  87. errCh := make(chan error, 1)
  88. // Here and everywhere else where Schedule() is called, it is done with the
  89. // lock held. But the lock guards only the scheduling part. The actual
  90. // callback is called asynchronously without the lock being held.
  91. ok := ccb.serializer.Schedule(func(_ context.Context) {
  92. // If the addresses specified in the update contain addresses of type
  93. // "grpclb" and the selected LB policy is not "grpclb", these addresses
  94. // will be filtered out and ccs will be modified with the updated
  95. // address list.
  96. if ccb.curBalancerName != grpclbName {
  97. var addrs []resolver.Address
  98. for _, addr := range ccs.ResolverState.Addresses {
  99. if addr.Type == resolver.GRPCLB {
  100. continue
  101. }
  102. addrs = append(addrs, addr)
  103. }
  104. ccs.ResolverState.Addresses = addrs
  105. }
  106. errCh <- ccb.balancer.UpdateClientConnState(*ccs)
  107. })
  108. if !ok {
  109. // If we are unable to schedule a function with the serializer, it
  110. // indicates that it has been closed. A serializer is only closed when
  111. // the wrapper is closed or is in idle.
  112. ccb.mu.Unlock()
  113. return fmt.Errorf("grpc: cannot send state update to a closed or idle balancer")
  114. }
  115. ccb.mu.Unlock()
  116. // We get here only if the above call to Schedule succeeds, in which case it
  117. // is guaranteed that the scheduled function will run. Therefore it is safe
  118. // to block on this channel.
  119. err := <-errCh
  120. if logger.V(2) && err != nil {
  121. logger.Infof("error from balancer.UpdateClientConnState: %v", err)
  122. }
  123. return err
  124. }
  125. // updateSubConnState is invoked by grpc to push a subConn state update to the
  126. // underlying balancer.
  127. func (ccb *ccBalancerWrapper) updateSubConnState(sc balancer.SubConn, s connectivity.State, err error) {
  128. ccb.mu.Lock()
  129. ccb.serializer.Schedule(func(_ context.Context) {
  130. ccb.balancer.UpdateSubConnState(sc, balancer.SubConnState{ConnectivityState: s, ConnectionError: err})
  131. })
  132. ccb.mu.Unlock()
  133. }
  134. func (ccb *ccBalancerWrapper) resolverError(err error) {
  135. ccb.mu.Lock()
  136. ccb.serializer.Schedule(func(_ context.Context) {
  137. ccb.balancer.ResolverError(err)
  138. })
  139. ccb.mu.Unlock()
  140. }
  141. // switchTo is invoked by grpc to instruct the balancer wrapper to switch to the
  142. // LB policy identified by name.
  143. //
  144. // ClientConn calls newCCBalancerWrapper() at creation time. Upon receipt of the
  145. // first good update from the name resolver, it determines the LB policy to use
  146. // and invokes the switchTo() method. Upon receipt of every subsequent update
  147. // from the name resolver, it invokes this method.
  148. //
  149. // the ccBalancerWrapper keeps track of the current LB policy name, and skips
  150. // the graceful balancer switching process if the name does not change.
  151. func (ccb *ccBalancerWrapper) switchTo(name string) {
  152. ccb.mu.Lock()
  153. ccb.serializer.Schedule(func(_ context.Context) {
  154. // TODO: Other languages use case-sensitive balancer registries. We should
  155. // switch as well. See: https://github.com/grpc/grpc-go/issues/5288.
  156. if strings.EqualFold(ccb.curBalancerName, name) {
  157. return
  158. }
  159. ccb.buildLoadBalancingPolicy(name)
  160. })
  161. ccb.mu.Unlock()
  162. }
  163. // buildLoadBalancingPolicy performs the following:
  164. // - retrieve a balancer builder for the given name. Use the default LB
  165. // policy, pick_first, if no LB policy with name is found in the registry.
  166. // - instruct the gracefulswitch balancer to switch to the above builder. This
  167. // will actually build the new balancer.
  168. // - update the `curBalancerName` field
  169. //
  170. // Must be called from a serializer callback.
  171. func (ccb *ccBalancerWrapper) buildLoadBalancingPolicy(name string) {
  172. builder := balancer.Get(name)
  173. if builder == nil {
  174. channelz.Warningf(logger, ccb.cc.channelzID, "Channel switches to new LB policy %q, since the specified LB policy %q was not registered", PickFirstBalancerName, name)
  175. builder = newPickfirstBuilder()
  176. } else {
  177. channelz.Infof(logger, ccb.cc.channelzID, "Channel switches to new LB policy %q", name)
  178. }
  179. if err := ccb.balancer.SwitchTo(builder); err != nil {
  180. channelz.Errorf(logger, ccb.cc.channelzID, "Channel failed to build new LB policy %q: %v", name, err)
  181. return
  182. }
  183. ccb.curBalancerName = builder.Name()
  184. }
  185. func (ccb *ccBalancerWrapper) close() {
  186. channelz.Info(logger, ccb.cc.channelzID, "ccBalancerWrapper: closing")
  187. ccb.closeBalancer(ccbModeClosed)
  188. }
  189. // enterIdleMode is invoked by grpc when the channel enters idle mode upon
  190. // expiry of idle_timeout. This call blocks until the balancer is closed.
  191. func (ccb *ccBalancerWrapper) enterIdleMode() {
  192. channelz.Info(logger, ccb.cc.channelzID, "ccBalancerWrapper: entering idle mode")
  193. ccb.closeBalancer(ccbModeIdle)
  194. }
  195. // closeBalancer is invoked when the channel is being closed or when it enters
  196. // idle mode upon expiry of idle_timeout.
  197. func (ccb *ccBalancerWrapper) closeBalancer(m ccbMode) {
  198. ccb.mu.Lock()
  199. if ccb.mode == ccbModeClosed || ccb.mode == ccbModeIdle {
  200. ccb.mu.Unlock()
  201. return
  202. }
  203. ccb.mode = m
  204. done := ccb.serializer.Done
  205. b := ccb.balancer
  206. ok := ccb.serializer.Schedule(func(_ context.Context) {
  207. // Close the serializer to ensure that no more calls from gRPC are sent
  208. // to the balancer.
  209. ccb.serializerCancel()
  210. // Empty the current balancer name because we don't have a balancer
  211. // anymore and also so that we act on the next call to switchTo by
  212. // creating a new balancer specified by the new resolver.
  213. ccb.curBalancerName = ""
  214. })
  215. if !ok {
  216. ccb.mu.Unlock()
  217. return
  218. }
  219. ccb.mu.Unlock()
  220. // Give enqueued callbacks a chance to finish.
  221. <-done
  222. // Spawn a goroutine to close the balancer (since it may block trying to
  223. // cleanup all allocated resources) and return early.
  224. go b.Close()
  225. }
  226. // exitIdleMode is invoked by grpc when the channel exits idle mode either
  227. // because of an RPC or because of an invocation of the Connect() API. This
  228. // recreates the balancer that was closed previously when entering idle mode.
  229. //
  230. // If the channel is not in idle mode, we know for a fact that we are here as a
  231. // result of the user calling the Connect() method on the ClientConn. In this
  232. // case, we can simply forward the call to the underlying balancer, instructing
  233. // it to reconnect to the backends.
  234. func (ccb *ccBalancerWrapper) exitIdleMode() {
  235. ccb.mu.Lock()
  236. if ccb.mode == ccbModeClosed {
  237. // Request to exit idle is a no-op when wrapper is already closed.
  238. ccb.mu.Unlock()
  239. return
  240. }
  241. if ccb.mode == ccbModeIdle {
  242. // Recreate the serializer which was closed when we entered idle.
  243. ctx, cancel := context.WithCancel(context.Background())
  244. ccb.serializer = grpcsync.NewCallbackSerializer(ctx)
  245. ccb.serializerCancel = cancel
  246. }
  247. // The ClientConn guarantees that mutual exclusion between close() and
  248. // exitIdleMode(), and since we just created a new serializer, we can be
  249. // sure that the below function will be scheduled.
  250. done := make(chan struct{})
  251. ccb.serializer.Schedule(func(_ context.Context) {
  252. defer close(done)
  253. ccb.mu.Lock()
  254. defer ccb.mu.Unlock()
  255. if ccb.mode != ccbModeIdle {
  256. ccb.balancer.ExitIdle()
  257. return
  258. }
  259. // Gracefulswitch balancer does not support a switchTo operation after
  260. // being closed. Hence we need to create a new one here.
  261. ccb.balancer = gracefulswitch.NewBalancer(ccb, ccb.opts)
  262. ccb.mode = ccbModeActive
  263. channelz.Info(logger, ccb.cc.channelzID, "ccBalancerWrapper: exiting idle mode")
  264. })
  265. ccb.mu.Unlock()
  266. <-done
  267. }
  268. func (ccb *ccBalancerWrapper) isIdleOrClosed() bool {
  269. ccb.mu.Lock()
  270. defer ccb.mu.Unlock()
  271. return ccb.mode == ccbModeIdle || ccb.mode == ccbModeClosed
  272. }
  273. func (ccb *ccBalancerWrapper) NewSubConn(addrs []resolver.Address, opts balancer.NewSubConnOptions) (balancer.SubConn, error) {
  274. if ccb.isIdleOrClosed() {
  275. return nil, fmt.Errorf("grpc: cannot create SubConn when balancer is closed or idle")
  276. }
  277. if len(addrs) == 0 {
  278. return nil, fmt.Errorf("grpc: cannot create SubConn with empty address list")
  279. }
  280. ac, err := ccb.cc.newAddrConn(addrs, opts)
  281. if err != nil {
  282. channelz.Warningf(logger, ccb.cc.channelzID, "acBalancerWrapper: NewSubConn: failed to newAddrConn: %v", err)
  283. return nil, err
  284. }
  285. acbw := &acBalancerWrapper{ac: ac, producers: make(map[balancer.ProducerBuilder]*refCountedProducer)}
  286. ac.acbw = acbw
  287. return acbw, nil
  288. }
  289. func (ccb *ccBalancerWrapper) RemoveSubConn(sc balancer.SubConn) {
  290. if ccb.isIdleOrClosed() {
  291. // It it safe to ignore this call when the balancer is closed or in idle
  292. // because the ClientConn takes care of closing the connections.
  293. //
  294. // Not returning early from here when the balancer is closed or in idle
  295. // leads to a deadlock though, because of the following sequence of
  296. // calls when holding cc.mu:
  297. // cc.exitIdleMode --> ccb.enterIdleMode --> gsw.Close -->
  298. // ccb.RemoveAddrConn --> cc.removeAddrConn
  299. return
  300. }
  301. acbw, ok := sc.(*acBalancerWrapper)
  302. if !ok {
  303. return
  304. }
  305. ccb.cc.removeAddrConn(acbw.ac, errConnDrain)
  306. }
  307. func (ccb *ccBalancerWrapper) UpdateAddresses(sc balancer.SubConn, addrs []resolver.Address) {
  308. if ccb.isIdleOrClosed() {
  309. return
  310. }
  311. acbw, ok := sc.(*acBalancerWrapper)
  312. if !ok {
  313. return
  314. }
  315. acbw.UpdateAddresses(addrs)
  316. }
  317. func (ccb *ccBalancerWrapper) UpdateState(s balancer.State) {
  318. if ccb.isIdleOrClosed() {
  319. return
  320. }
  321. // Update picker before updating state. Even though the ordering here does
  322. // not matter, it can lead to multiple calls of Pick in the common start-up
  323. // case where we wait for ready and then perform an RPC. If the picker is
  324. // updated later, we could call the "connecting" picker when the state is
  325. // updated, and then call the "ready" picker after the picker gets updated.
  326. ccb.cc.blockingpicker.updatePicker(s.Picker)
  327. ccb.cc.csMgr.updateState(s.ConnectivityState)
  328. }
  329. func (ccb *ccBalancerWrapper) ResolveNow(o resolver.ResolveNowOptions) {
  330. if ccb.isIdleOrClosed() {
  331. return
  332. }
  333. ccb.cc.resolveNow(o)
  334. }
  335. func (ccb *ccBalancerWrapper) Target() string {
  336. return ccb.cc.target
  337. }
  338. // acBalancerWrapper is a wrapper on top of ac for balancers.
  339. // It implements balancer.SubConn interface.
  340. type acBalancerWrapper struct {
  341. ac *addrConn // read-only
  342. mu sync.Mutex
  343. producers map[balancer.ProducerBuilder]*refCountedProducer
  344. }
  345. func (acbw *acBalancerWrapper) String() string {
  346. return fmt.Sprintf("SubConn(id:%d)", acbw.ac.channelzID.Int())
  347. }
  348. func (acbw *acBalancerWrapper) UpdateAddresses(addrs []resolver.Address) {
  349. acbw.ac.updateAddrs(addrs)
  350. }
  351. func (acbw *acBalancerWrapper) Connect() {
  352. go acbw.ac.connect()
  353. }
  354. // NewStream begins a streaming RPC on the addrConn. If the addrConn is not
  355. // ready, blocks until it is or ctx expires. Returns an error when the context
  356. // expires or the addrConn is shut down.
  357. func (acbw *acBalancerWrapper) NewStream(ctx context.Context, desc *StreamDesc, method string, opts ...CallOption) (ClientStream, error) {
  358. transport, err := acbw.ac.getTransport(ctx)
  359. if err != nil {
  360. return nil, err
  361. }
  362. return newNonRetryClientStream(ctx, desc, method, transport, acbw.ac, opts...)
  363. }
  364. // Invoke performs a unary RPC. If the addrConn is not ready, returns
  365. // errSubConnNotReady.
  366. func (acbw *acBalancerWrapper) Invoke(ctx context.Context, method string, args interface{}, reply interface{}, opts ...CallOption) error {
  367. cs, err := acbw.NewStream(ctx, unaryStreamDesc, method, opts...)
  368. if err != nil {
  369. return err
  370. }
  371. if err := cs.SendMsg(args); err != nil {
  372. return err
  373. }
  374. return cs.RecvMsg(reply)
  375. }
  376. type refCountedProducer struct {
  377. producer balancer.Producer
  378. refs int // number of current refs to the producer
  379. close func() // underlying producer's close function
  380. }
  381. func (acbw *acBalancerWrapper) GetOrBuildProducer(pb balancer.ProducerBuilder) (balancer.Producer, func()) {
  382. acbw.mu.Lock()
  383. defer acbw.mu.Unlock()
  384. // Look up existing producer from this builder.
  385. pData := acbw.producers[pb]
  386. if pData == nil {
  387. // Not found; create a new one and add it to the producers map.
  388. p, close := pb.Build(acbw)
  389. pData = &refCountedProducer{producer: p, close: close}
  390. acbw.producers[pb] = pData
  391. }
  392. // Account for this new reference.
  393. pData.refs++
  394. // Return a cleanup function wrapped in a OnceFunc to remove this reference
  395. // and delete the refCountedProducer from the map if the total reference
  396. // count goes to zero.
  397. unref := func() {
  398. acbw.mu.Lock()
  399. pData.refs--
  400. if pData.refs == 0 {
  401. defer pData.close() // Run outside the acbw mutex
  402. delete(acbw.producers, pb)
  403. }
  404. acbw.mu.Unlock()
  405. }
  406. return pData.producer, grpcsync.OnceFunc(unref)
  407. }