gracefulswitch.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. *
  3. * Copyright 2022 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 gracefulswitch implements a graceful switch load balancer.
  19. package gracefulswitch
  20. import (
  21. "errors"
  22. "fmt"
  23. "sync"
  24. "google.golang.org/grpc/balancer"
  25. "google.golang.org/grpc/balancer/base"
  26. "google.golang.org/grpc/connectivity"
  27. "google.golang.org/grpc/resolver"
  28. )
  29. var errBalancerClosed = errors.New("gracefulSwitchBalancer is closed")
  30. var _ balancer.Balancer = (*Balancer)(nil)
  31. // NewBalancer returns a graceful switch Balancer.
  32. func NewBalancer(cc balancer.ClientConn, opts balancer.BuildOptions) *Balancer {
  33. return &Balancer{
  34. cc: cc,
  35. bOpts: opts,
  36. }
  37. }
  38. // Balancer is a utility to gracefully switch from one balancer to
  39. // a new balancer. It implements the balancer.Balancer interface.
  40. type Balancer struct {
  41. bOpts balancer.BuildOptions
  42. cc balancer.ClientConn
  43. // mu protects the following fields and all fields within balancerCurrent
  44. // and balancerPending. mu does not need to be held when calling into the
  45. // child balancers, as all calls into these children happen only as a direct
  46. // result of a call into the gracefulSwitchBalancer, which are also
  47. // guaranteed to be synchronous. There is one exception: an UpdateState call
  48. // from a child balancer when current and pending are populated can lead to
  49. // calling Close() on the current. To prevent that racing with an
  50. // UpdateSubConnState from the channel, we hold currentMu during Close and
  51. // UpdateSubConnState calls.
  52. mu sync.Mutex
  53. balancerCurrent *balancerWrapper
  54. balancerPending *balancerWrapper
  55. closed bool // set to true when this balancer is closed
  56. // currentMu must be locked before mu. This mutex guards against this
  57. // sequence of events: UpdateSubConnState() called, finds the
  58. // balancerCurrent, gives up lock, updateState comes in, causes Close() on
  59. // balancerCurrent before the UpdateSubConnState is called on the
  60. // balancerCurrent.
  61. currentMu sync.Mutex
  62. }
  63. // swap swaps out the current lb with the pending lb and updates the ClientConn.
  64. // The caller must hold gsb.mu.
  65. func (gsb *Balancer) swap() {
  66. gsb.cc.UpdateState(gsb.balancerPending.lastState)
  67. cur := gsb.balancerCurrent
  68. gsb.balancerCurrent = gsb.balancerPending
  69. gsb.balancerPending = nil
  70. go func() {
  71. gsb.currentMu.Lock()
  72. defer gsb.currentMu.Unlock()
  73. cur.Close()
  74. }()
  75. }
  76. // Helper function that checks if the balancer passed in is current or pending.
  77. // The caller must hold gsb.mu.
  78. func (gsb *Balancer) balancerCurrentOrPending(bw *balancerWrapper) bool {
  79. return bw == gsb.balancerCurrent || bw == gsb.balancerPending
  80. }
  81. // SwitchTo initializes the graceful switch process, which completes based on
  82. // connectivity state changes on the current/pending balancer. Thus, the switch
  83. // process is not complete when this method returns. This method must be called
  84. // synchronously alongside the rest of the balancer.Balancer methods this
  85. // Graceful Switch Balancer implements.
  86. func (gsb *Balancer) SwitchTo(builder balancer.Builder) error {
  87. gsb.mu.Lock()
  88. if gsb.closed {
  89. gsb.mu.Unlock()
  90. return errBalancerClosed
  91. }
  92. bw := &balancerWrapper{
  93. gsb: gsb,
  94. lastState: balancer.State{
  95. ConnectivityState: connectivity.Connecting,
  96. Picker: base.NewErrPicker(balancer.ErrNoSubConnAvailable),
  97. },
  98. subconns: make(map[balancer.SubConn]bool),
  99. }
  100. balToClose := gsb.balancerPending // nil if there is no pending balancer
  101. if gsb.balancerCurrent == nil {
  102. gsb.balancerCurrent = bw
  103. } else {
  104. gsb.balancerPending = bw
  105. }
  106. gsb.mu.Unlock()
  107. balToClose.Close()
  108. // This function takes a builder instead of a balancer because builder.Build
  109. // can call back inline, and this utility needs to handle the callbacks.
  110. newBalancer := builder.Build(bw, gsb.bOpts)
  111. if newBalancer == nil {
  112. // This is illegal and should never happen; we clear the balancerWrapper
  113. // we were constructing if it happens to avoid a potential panic.
  114. gsb.mu.Lock()
  115. if gsb.balancerPending != nil {
  116. gsb.balancerPending = nil
  117. } else {
  118. gsb.balancerCurrent = nil
  119. }
  120. gsb.mu.Unlock()
  121. return balancer.ErrBadResolverState
  122. }
  123. // This write doesn't need to take gsb.mu because this field never gets read
  124. // or written to on any calls from the current or pending. Calls from grpc
  125. // to this balancer are guaranteed to be called synchronously, so this
  126. // bw.Balancer field will never be forwarded to until this SwitchTo()
  127. // function returns.
  128. bw.Balancer = newBalancer
  129. return nil
  130. }
  131. // Returns nil if the graceful switch balancer is closed.
  132. func (gsb *Balancer) latestBalancer() *balancerWrapper {
  133. gsb.mu.Lock()
  134. defer gsb.mu.Unlock()
  135. if gsb.balancerPending != nil {
  136. return gsb.balancerPending
  137. }
  138. return gsb.balancerCurrent
  139. }
  140. // UpdateClientConnState forwards the update to the latest balancer created.
  141. func (gsb *Balancer) UpdateClientConnState(state balancer.ClientConnState) error {
  142. // The resolver data is only relevant to the most recent LB Policy.
  143. balToUpdate := gsb.latestBalancer()
  144. if balToUpdate == nil {
  145. return errBalancerClosed
  146. }
  147. // Perform this call without gsb.mu to prevent deadlocks if the child calls
  148. // back into the channel. The latest balancer can never be closed during a
  149. // call from the channel, even without gsb.mu held.
  150. return balToUpdate.UpdateClientConnState(state)
  151. }
  152. // ResolverError forwards the error to the latest balancer created.
  153. func (gsb *Balancer) ResolverError(err error) {
  154. // The resolver data is only relevant to the most recent LB Policy.
  155. balToUpdate := gsb.latestBalancer()
  156. if balToUpdate == nil {
  157. return
  158. }
  159. // Perform this call without gsb.mu to prevent deadlocks if the child calls
  160. // back into the channel. The latest balancer can never be closed during a
  161. // call from the channel, even without gsb.mu held.
  162. balToUpdate.ResolverError(err)
  163. }
  164. // ExitIdle forwards the call to the latest balancer created.
  165. //
  166. // If the latest balancer does not support ExitIdle, the subConns are
  167. // re-connected to manually.
  168. func (gsb *Balancer) ExitIdle() {
  169. balToUpdate := gsb.latestBalancer()
  170. if balToUpdate == nil {
  171. return
  172. }
  173. // There is no need to protect this read with a mutex, as the write to the
  174. // Balancer field happens in SwitchTo, which completes before this can be
  175. // called.
  176. if ei, ok := balToUpdate.Balancer.(balancer.ExitIdler); ok {
  177. ei.ExitIdle()
  178. return
  179. }
  180. gsb.mu.Lock()
  181. defer gsb.mu.Unlock()
  182. for sc := range balToUpdate.subconns {
  183. sc.Connect()
  184. }
  185. }
  186. // updateSubConnState forwards the update to the appropriate child.
  187. func (gsb *Balancer) updateSubConnState(sc balancer.SubConn, state balancer.SubConnState, cb func(balancer.SubConnState)) {
  188. gsb.currentMu.Lock()
  189. defer gsb.currentMu.Unlock()
  190. gsb.mu.Lock()
  191. // Forward update to the appropriate child. Even if there is a pending
  192. // balancer, the current balancer should continue to get SubConn updates to
  193. // maintain the proper state while the pending is still connecting.
  194. var balToUpdate *balancerWrapper
  195. if gsb.balancerCurrent != nil && gsb.balancerCurrent.subconns[sc] {
  196. balToUpdate = gsb.balancerCurrent
  197. } else if gsb.balancerPending != nil && gsb.balancerPending.subconns[sc] {
  198. balToUpdate = gsb.balancerPending
  199. }
  200. if balToUpdate == nil {
  201. // SubConn belonged to a stale lb policy that has not yet fully closed,
  202. // or the balancer was already closed.
  203. gsb.mu.Unlock()
  204. return
  205. }
  206. if state.ConnectivityState == connectivity.Shutdown {
  207. delete(balToUpdate.subconns, sc)
  208. }
  209. gsb.mu.Unlock()
  210. if cb != nil {
  211. cb(state)
  212. } else {
  213. balToUpdate.UpdateSubConnState(sc, state)
  214. }
  215. }
  216. // UpdateSubConnState forwards the update to the appropriate child.
  217. func (gsb *Balancer) UpdateSubConnState(sc balancer.SubConn, state balancer.SubConnState) {
  218. gsb.updateSubConnState(sc, state, nil)
  219. }
  220. // Close closes any active child balancers.
  221. func (gsb *Balancer) Close() {
  222. gsb.mu.Lock()
  223. gsb.closed = true
  224. currentBalancerToClose := gsb.balancerCurrent
  225. gsb.balancerCurrent = nil
  226. pendingBalancerToClose := gsb.balancerPending
  227. gsb.balancerPending = nil
  228. gsb.mu.Unlock()
  229. currentBalancerToClose.Close()
  230. pendingBalancerToClose.Close()
  231. }
  232. // balancerWrapper wraps a balancer.Balancer, and overrides some Balancer
  233. // methods to help cleanup SubConns created by the wrapped balancer.
  234. //
  235. // It implements the balancer.ClientConn interface and is passed down in that
  236. // capacity to the wrapped balancer. It maintains a set of subConns created by
  237. // the wrapped balancer and calls from the latter to create/update/shutdown
  238. // SubConns update this set before being forwarded to the parent ClientConn.
  239. // State updates from the wrapped balancer can result in invocation of the
  240. // graceful switch logic.
  241. type balancerWrapper struct {
  242. balancer.Balancer
  243. gsb *Balancer
  244. lastState balancer.State
  245. subconns map[balancer.SubConn]bool // subconns created by this balancer
  246. }
  247. // Close closes the underlying LB policy and shuts down the subconns it
  248. // created. bw must not be referenced via balancerCurrent or balancerPending in
  249. // gsb when called. gsb.mu must not be held. Does not panic with a nil
  250. // receiver.
  251. func (bw *balancerWrapper) Close() {
  252. // before Close is called.
  253. if bw == nil {
  254. return
  255. }
  256. // There is no need to protect this read with a mutex, as Close() is
  257. // impossible to be called concurrently with the write in SwitchTo(). The
  258. // callsites of Close() for this balancer in Graceful Switch Balancer will
  259. // never be called until SwitchTo() returns.
  260. bw.Balancer.Close()
  261. bw.gsb.mu.Lock()
  262. for sc := range bw.subconns {
  263. sc.Shutdown()
  264. }
  265. bw.gsb.mu.Unlock()
  266. }
  267. func (bw *balancerWrapper) UpdateState(state balancer.State) {
  268. // Hold the mutex for this entire call to ensure it cannot occur
  269. // concurrently with other updateState() calls. This causes updates to
  270. // lastState and calls to cc.UpdateState to happen atomically.
  271. bw.gsb.mu.Lock()
  272. defer bw.gsb.mu.Unlock()
  273. bw.lastState = state
  274. if !bw.gsb.balancerCurrentOrPending(bw) {
  275. return
  276. }
  277. if bw == bw.gsb.balancerCurrent {
  278. // In the case that the current balancer exits READY, and there is a pending
  279. // balancer, you can forward the pending balancer's cached State up to
  280. // ClientConn and swap the pending into the current. This is because there
  281. // is no reason to gracefully switch from and keep using the old policy as
  282. // the ClientConn is not connected to any backends.
  283. if state.ConnectivityState != connectivity.Ready && bw.gsb.balancerPending != nil {
  284. bw.gsb.swap()
  285. return
  286. }
  287. // Even if there is a pending balancer waiting to be gracefully switched to,
  288. // continue to forward current balancer updates to the Client Conn. Ignoring
  289. // state + picker from the current would cause undefined behavior/cause the
  290. // system to behave incorrectly from the current LB policies perspective.
  291. // Also, the current LB is still being used by grpc to choose SubConns per
  292. // RPC, and thus should use the most updated form of the current balancer.
  293. bw.gsb.cc.UpdateState(state)
  294. return
  295. }
  296. // This method is now dealing with a state update from the pending balancer.
  297. // If the current balancer is currently in a state other than READY, the new
  298. // policy can be swapped into place immediately. This is because there is no
  299. // reason to gracefully switch from and keep using the old policy as the
  300. // ClientConn is not connected to any backends.
  301. if state.ConnectivityState != connectivity.Connecting || bw.gsb.balancerCurrent.lastState.ConnectivityState != connectivity.Ready {
  302. bw.gsb.swap()
  303. }
  304. }
  305. func (bw *balancerWrapper) NewSubConn(addrs []resolver.Address, opts balancer.NewSubConnOptions) (balancer.SubConn, error) {
  306. bw.gsb.mu.Lock()
  307. if !bw.gsb.balancerCurrentOrPending(bw) {
  308. bw.gsb.mu.Unlock()
  309. return nil, fmt.Errorf("%T at address %p that called NewSubConn is deleted", bw, bw)
  310. }
  311. bw.gsb.mu.Unlock()
  312. var sc balancer.SubConn
  313. oldListener := opts.StateListener
  314. opts.StateListener = func(state balancer.SubConnState) { bw.gsb.updateSubConnState(sc, state, oldListener) }
  315. sc, err := bw.gsb.cc.NewSubConn(addrs, opts)
  316. if err != nil {
  317. return nil, err
  318. }
  319. bw.gsb.mu.Lock()
  320. if !bw.gsb.balancerCurrentOrPending(bw) { // balancer was closed during this call
  321. sc.Shutdown()
  322. bw.gsb.mu.Unlock()
  323. return nil, fmt.Errorf("%T at address %p that called NewSubConn is deleted", bw, bw)
  324. }
  325. bw.subconns[sc] = true
  326. bw.gsb.mu.Unlock()
  327. return sc, nil
  328. }
  329. func (bw *balancerWrapper) ResolveNow(opts resolver.ResolveNowOptions) {
  330. // Ignore ResolveNow requests from anything other than the most recent
  331. // balancer, because older balancers were already removed from the config.
  332. if bw != bw.gsb.latestBalancer() {
  333. return
  334. }
  335. bw.gsb.cc.ResolveNow(opts)
  336. }
  337. func (bw *balancerWrapper) RemoveSubConn(sc balancer.SubConn) {
  338. // Note: existing third party balancers may call this, so it must remain
  339. // until RemoveSubConn is fully removed.
  340. sc.Shutdown()
  341. }
  342. func (bw *balancerWrapper) UpdateAddresses(sc balancer.SubConn, addrs []resolver.Address) {
  343. bw.gsb.mu.Lock()
  344. if !bw.gsb.balancerCurrentOrPending(bw) {
  345. bw.gsb.mu.Unlock()
  346. return
  347. }
  348. bw.gsb.mu.Unlock()
  349. bw.gsb.cc.UpdateAddresses(sc, addrs)
  350. }
  351. func (bw *balancerWrapper) Target() string {
  352. return bw.gsb.cc.Target()
  353. }