picker_wrapper.go 4.9 KB

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