balancer_conn_wrappers.go 15 KB

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