picker_wrapper.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. "io"
  22. "sync"
  23. "google.golang.org/grpc/balancer"
  24. "google.golang.org/grpc/codes"
  25. "google.golang.org/grpc/internal/channelz"
  26. istatus "google.golang.org/grpc/internal/status"
  27. "google.golang.org/grpc/internal/transport"
  28. "google.golang.org/grpc/stats"
  29. "google.golang.org/grpc/status"
  30. )
  31. // pickerWrapper is a wrapper of balancer.Picker. It blocks on certain pick
  32. // actions and unblock when there's a picker update.
  33. type pickerWrapper struct {
  34. mu sync.Mutex
  35. done bool
  36. idle bool
  37. blockingCh chan struct{}
  38. picker balancer.Picker
  39. statsHandlers []stats.Handler // to record blocking picker calls
  40. }
  41. func newPickerWrapper(statsHandlers []stats.Handler) *pickerWrapper {
  42. return &pickerWrapper{
  43. blockingCh: make(chan struct{}),
  44. statsHandlers: statsHandlers,
  45. }
  46. }
  47. // updatePicker is called by UpdateBalancerState. It unblocks all blocked pick.
  48. func (pw *pickerWrapper) updatePicker(p balancer.Picker) {
  49. pw.mu.Lock()
  50. if pw.done || pw.idle {
  51. // There is a small window where a picker update from the LB policy can
  52. // race with the channel going to idle mode. If the picker is idle here,
  53. // it is because the channel asked it to do so, and therefore it is sage
  54. // to ignore the update from the LB policy.
  55. pw.mu.Unlock()
  56. return
  57. }
  58. pw.picker = p
  59. // pw.blockingCh should never be nil.
  60. close(pw.blockingCh)
  61. pw.blockingCh = make(chan struct{})
  62. pw.mu.Unlock()
  63. }
  64. // doneChannelzWrapper performs the following:
  65. // - increments the calls started channelz counter
  66. // - wraps the done function in the passed in result to increment the calls
  67. // failed or calls succeeded channelz counter before invoking the actual
  68. // done function.
  69. func doneChannelzWrapper(acbw *acBalancerWrapper, result *balancer.PickResult) {
  70. ac := acbw.ac
  71. ac.incrCallsStarted()
  72. done := result.Done
  73. result.Done = func(b balancer.DoneInfo) {
  74. if b.Err != nil && b.Err != io.EOF {
  75. ac.incrCallsFailed()
  76. } else {
  77. ac.incrCallsSucceeded()
  78. }
  79. if done != nil {
  80. done(b)
  81. }
  82. }
  83. }
  84. // pick returns the transport that will be used for the RPC.
  85. // It may block in the following cases:
  86. // - there's no picker
  87. // - the current picker returns ErrNoSubConnAvailable
  88. // - the current picker returns other errors and failfast is false.
  89. // - the subConn returned by the current picker is not READY
  90. // When one of these situations happens, pick blocks until the picker gets updated.
  91. func (pw *pickerWrapper) pick(ctx context.Context, failfast bool, info balancer.PickInfo) (transport.ClientTransport, balancer.PickResult, error) {
  92. var ch chan struct{}
  93. var lastPickErr error
  94. for {
  95. pw.mu.Lock()
  96. if pw.done {
  97. pw.mu.Unlock()
  98. return nil, balancer.PickResult{}, ErrClientConnClosing
  99. }
  100. if pw.picker == nil {
  101. ch = pw.blockingCh
  102. }
  103. if ch == pw.blockingCh {
  104. // This could happen when either:
  105. // - pw.picker is nil (the previous if condition), or
  106. // - has called pick on the current picker.
  107. pw.mu.Unlock()
  108. select {
  109. case <-ctx.Done():
  110. var errStr string
  111. if lastPickErr != nil {
  112. errStr = "latest balancer error: " + lastPickErr.Error()
  113. } else {
  114. errStr = ctx.Err().Error()
  115. }
  116. switch ctx.Err() {
  117. case context.DeadlineExceeded:
  118. return nil, balancer.PickResult{}, status.Error(codes.DeadlineExceeded, errStr)
  119. case context.Canceled:
  120. return nil, balancer.PickResult{}, status.Error(codes.Canceled, errStr)
  121. }
  122. case <-ch:
  123. }
  124. continue
  125. }
  126. // If the channel is set, it means that the pick call had to wait for a
  127. // new picker at some point. Either it's the first iteration and this
  128. // function received the first picker, or a picker errored with
  129. // ErrNoSubConnAvailable or errored with failfast set to false, which
  130. // will trigger a continue to the next iteration. In the first case this
  131. // conditional will hit if this call had to block (the channel is set).
  132. // In the second case, the only way it will get to this conditional is
  133. // if there is a new picker.
  134. if ch != nil {
  135. for _, sh := range pw.statsHandlers {
  136. sh.HandleRPC(ctx, &stats.PickerUpdated{})
  137. }
  138. }
  139. ch = pw.blockingCh
  140. p := pw.picker
  141. pw.mu.Unlock()
  142. pickResult, err := p.Pick(info)
  143. if err != nil {
  144. if err == balancer.ErrNoSubConnAvailable {
  145. continue
  146. }
  147. if st, ok := status.FromError(err); ok {
  148. // Status error: end the RPC unconditionally with this status.
  149. // First restrict the code to the list allowed by gRFC A54.
  150. if istatus.IsRestrictedControlPlaneCode(st) {
  151. err = status.Errorf(codes.Internal, "received picker error with illegal status: %v", err)
  152. }
  153. return nil, balancer.PickResult{}, dropError{error: err}
  154. }
  155. // For all other errors, wait for ready RPCs should block and other
  156. // RPCs should fail with unavailable.
  157. if !failfast {
  158. lastPickErr = err
  159. continue
  160. }
  161. return nil, balancer.PickResult{}, status.Error(codes.Unavailable, err.Error())
  162. }
  163. acbw, ok := pickResult.SubConn.(*acBalancerWrapper)
  164. if !ok {
  165. logger.Errorf("subconn returned from pick is type %T, not *acBalancerWrapper", pickResult.SubConn)
  166. continue
  167. }
  168. if t := acbw.ac.getReadyTransport(); t != nil {
  169. if channelz.IsOn() {
  170. doneChannelzWrapper(acbw, &pickResult)
  171. return t, pickResult, nil
  172. }
  173. return t, pickResult, nil
  174. }
  175. if pickResult.Done != nil {
  176. // Calling done with nil error, no bytes sent and no bytes received.
  177. // DoneInfo with default value works.
  178. pickResult.Done(balancer.DoneInfo{})
  179. }
  180. logger.Infof("blockingPicker: the picked transport is not ready, loop back to repick")
  181. // If ok == false, ac.state is not READY.
  182. // A valid picker always returns READY subConn. This means the state of ac
  183. // just changed, and picker will be updated shortly.
  184. // continue back to the beginning of the for loop to repick.
  185. }
  186. }
  187. func (pw *pickerWrapper) close() {
  188. pw.mu.Lock()
  189. defer pw.mu.Unlock()
  190. if pw.done {
  191. return
  192. }
  193. pw.done = true
  194. close(pw.blockingCh)
  195. }
  196. func (pw *pickerWrapper) enterIdleMode() {
  197. pw.mu.Lock()
  198. defer pw.mu.Unlock()
  199. if pw.done {
  200. return
  201. }
  202. pw.idle = true
  203. }
  204. func (pw *pickerWrapper) exitIdleMode() {
  205. pw.mu.Lock()
  206. defer pw.mu.Unlock()
  207. if pw.done {
  208. return
  209. }
  210. pw.blockingCh = make(chan struct{})
  211. pw.idle = false
  212. }
  213. // dropError is a wrapper error that indicates the LB policy wishes to drop the
  214. // RPC and not retry it.
  215. type dropError struct {
  216. error
  217. }