You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

picker_wrapper.go 5.3 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. if connectionErr := bp.connectionError(); connectionErr != nil {
  111. switch ctx.Err() {
  112. case context.DeadlineExceeded:
  113. return nil, nil, status.Errorf(codes.DeadlineExceeded, "latest connection error: %v", connectionErr)
  114. case context.Canceled:
  115. return nil, nil, status.Errorf(codes.Canceled, "latest connection error: %v", connectionErr)
  116. }
  117. }
  118. return nil, nil, ctx.Err()
  119. case <-ch:
  120. }
  121. continue
  122. }
  123. ch = bp.blockingCh
  124. p := bp.picker
  125. bp.mu.Unlock()
  126. subConn, done, err := p.Pick(ctx, opts)
  127. if err != nil {
  128. switch err {
  129. case balancer.ErrNoSubConnAvailable:
  130. continue
  131. case balancer.ErrTransientFailure:
  132. if !failfast {
  133. continue
  134. }
  135. return nil, nil, status.Errorf(codes.Unavailable, "%v, latest connection error: %v", err, bp.connectionError())
  136. case context.DeadlineExceeded:
  137. return nil, nil, status.Error(codes.DeadlineExceeded, err.Error())
  138. case context.Canceled:
  139. return nil, nil, status.Error(codes.Canceled, err.Error())
  140. default:
  141. if _, ok := status.FromError(err); ok {
  142. return nil, nil, err
  143. }
  144. // err is some other error.
  145. return nil, nil, status.Error(codes.Unknown, err.Error())
  146. }
  147. }
  148. acw, ok := subConn.(*acBalancerWrapper)
  149. if !ok {
  150. grpclog.Error("subconn returned from pick is not *acBalancerWrapper")
  151. continue
  152. }
  153. if t, ok := acw.getAddrConn().getReadyTransport(); ok {
  154. if channelz.IsOn() {
  155. return t, doneChannelzWrapper(acw, done), nil
  156. }
  157. return t, done, nil
  158. }
  159. if done != nil {
  160. // Calling done with nil error, no bytes sent and no bytes received.
  161. // DoneInfo with default value works.
  162. done(balancer.DoneInfo{})
  163. }
  164. grpclog.Infof("blockingPicker: the picked transport is not ready, loop back to repick")
  165. // If ok == false, ac.state is not READY.
  166. // A valid picker always returns READY subConn. This means the state of ac
  167. // just changed, and picker will be updated shortly.
  168. // continue back to the beginning of the for loop to repick.
  169. }
  170. }
  171. func (bp *pickerWrapper) close() {
  172. bp.mu.Lock()
  173. defer bp.mu.Unlock()
  174. if bp.done {
  175. return
  176. }
  177. bp.done = true
  178. close(bp.blockingCh)
  179. }