picker_wrapper.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. "google.golang.org/grpc/internal/transport"
  27. "google.golang.org/grpc/status"
  28. )
  29. // pickerWrapper is a wrapper of balancer.Picker. It blocks on certain pick
  30. // actions and unblock when there's a picker update.
  31. type pickerWrapper struct {
  32. mu sync.Mutex
  33. done bool
  34. blockingCh chan struct{}
  35. picker balancer.Picker
  36. }
  37. func newPickerWrapper() *pickerWrapper {
  38. return &pickerWrapper{blockingCh: make(chan struct{})}
  39. }
  40. // updatePicker is called by UpdateBalancerState. It unblocks all blocked pick.
  41. func (pw *pickerWrapper) updatePicker(p balancer.Picker) {
  42. pw.mu.Lock()
  43. if pw.done {
  44. pw.mu.Unlock()
  45. return
  46. }
  47. pw.picker = p
  48. // pw.blockingCh should never be nil.
  49. close(pw.blockingCh)
  50. pw.blockingCh = make(chan struct{})
  51. pw.mu.Unlock()
  52. }
  53. func doneChannelzWrapper(acw *acBalancerWrapper, done func(balancer.DoneInfo)) func(balancer.DoneInfo) {
  54. acw.mu.Lock()
  55. ac := acw.ac
  56. acw.mu.Unlock()
  57. ac.incrCallsStarted()
  58. return func(b balancer.DoneInfo) {
  59. if b.Err != nil && b.Err != io.EOF {
  60. ac.incrCallsFailed()
  61. } else {
  62. ac.incrCallsSucceeded()
  63. }
  64. if done != nil {
  65. done(b)
  66. }
  67. }
  68. }
  69. // pick returns the transport that will be used for the RPC.
  70. // It may block in the following cases:
  71. // - there's no picker
  72. // - the current picker returns ErrNoSubConnAvailable
  73. // - the current picker returns other errors and failfast is false.
  74. // - the subConn returned by the current picker is not READY
  75. // When one of these situations happens, pick blocks until the picker gets updated.
  76. func (pw *pickerWrapper) pick(ctx context.Context, failfast bool, info balancer.PickInfo) (transport.ClientTransport, func(balancer.DoneInfo), error) {
  77. var ch chan struct{}
  78. var lastPickErr error
  79. for {
  80. pw.mu.Lock()
  81. if pw.done {
  82. pw.mu.Unlock()
  83. return nil, nil, ErrClientConnClosing
  84. }
  85. if pw.picker == nil {
  86. ch = pw.blockingCh
  87. }
  88. if ch == pw.blockingCh {
  89. // This could happen when either:
  90. // - pw.picker is nil (the previous if condition), or
  91. // - has called pick on the current picker.
  92. pw.mu.Unlock()
  93. select {
  94. case <-ctx.Done():
  95. var errStr string
  96. if lastPickErr != nil {
  97. errStr = "latest balancer error: " + lastPickErr.Error()
  98. } else {
  99. errStr = ctx.Err().Error()
  100. }
  101. switch ctx.Err() {
  102. case context.DeadlineExceeded:
  103. return nil, nil, status.Error(codes.DeadlineExceeded, errStr)
  104. case context.Canceled:
  105. return nil, nil, status.Error(codes.Canceled, errStr)
  106. }
  107. case <-ch:
  108. }
  109. continue
  110. }
  111. ch = pw.blockingCh
  112. p := pw.picker
  113. pw.mu.Unlock()
  114. pickResult, err := p.Pick(info)
  115. if err != nil {
  116. if err == balancer.ErrNoSubConnAvailable {
  117. continue
  118. }
  119. if _, ok := status.FromError(err); ok {
  120. // Status error: end the RPC unconditionally with this status.
  121. return nil, nil, err
  122. }
  123. // For all other errors, wait for ready RPCs should block and other
  124. // RPCs should fail with unavailable.
  125. if !failfast {
  126. lastPickErr = err
  127. continue
  128. }
  129. return nil, nil, status.Error(codes.Unavailable, err.Error())
  130. }
  131. acw, ok := pickResult.SubConn.(*acBalancerWrapper)
  132. if !ok {
  133. logger.Error("subconn returned from pick is not *acBalancerWrapper")
  134. continue
  135. }
  136. if t, ok := acw.getAddrConn().getReadyTransport(); ok {
  137. if channelz.IsOn() {
  138. return t, doneChannelzWrapper(acw, pickResult.Done), nil
  139. }
  140. return t, pickResult.Done, nil
  141. }
  142. if pickResult.Done != nil {
  143. // Calling done with nil error, no bytes sent and no bytes received.
  144. // DoneInfo with default value works.
  145. pickResult.Done(balancer.DoneInfo{})
  146. }
  147. logger.Infof("blockingPicker: the picked transport is not ready, loop back to repick")
  148. // If ok == false, ac.state is not READY.
  149. // A valid picker always returns READY subConn. This means the state of ac
  150. // just changed, and picker will be updated shortly.
  151. // continue back to the beginning of the for loop to repick.
  152. }
  153. }
  154. func (pw *pickerWrapper) close() {
  155. pw.mu.Lock()
  156. defer pw.mu.Unlock()
  157. if pw.done {
  158. return
  159. }
  160. pw.done = true
  161. close(pw.blockingCh)
  162. }