picker_wrapper.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 (
  94. p balancer.Picker
  95. ch chan struct{}
  96. )
  97. for {
  98. bp.mu.Lock()
  99. if bp.done {
  100. bp.mu.Unlock()
  101. return nil, nil, ErrClientConnClosing
  102. }
  103. if bp.picker == nil {
  104. ch = bp.blockingCh
  105. }
  106. if ch == bp.blockingCh {
  107. // This could happen when either:
  108. // - bp.picker is nil (the previous if condition), or
  109. // - has called pick on the current picker.
  110. bp.mu.Unlock()
  111. select {
  112. case <-ctx.Done():
  113. return nil, nil, ctx.Err()
  114. case <-ch:
  115. }
  116. continue
  117. }
  118. ch = bp.blockingCh
  119. p = bp.picker
  120. bp.mu.Unlock()
  121. subConn, done, err := p.Pick(ctx, opts)
  122. if err != nil {
  123. switch err {
  124. case balancer.ErrNoSubConnAvailable:
  125. continue
  126. case balancer.ErrTransientFailure:
  127. if !failfast {
  128. continue
  129. }
  130. return nil, nil, status.Errorf(codes.Unavailable, "%v, latest connection error: %v", err, bp.connectionError())
  131. default:
  132. // err is some other error.
  133. return nil, nil, toRPCErr(err)
  134. }
  135. }
  136. acw, ok := subConn.(*acBalancerWrapper)
  137. if !ok {
  138. grpclog.Infof("subconn returned from pick is not *acBalancerWrapper")
  139. continue
  140. }
  141. if t, ok := acw.getAddrConn().getReadyTransport(); ok {
  142. if channelz.IsOn() {
  143. return t, doneChannelzWrapper(acw, done), nil
  144. }
  145. return t, done, nil
  146. }
  147. grpclog.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 (bp *pickerWrapper) close() {
  155. bp.mu.Lock()
  156. defer bp.mu.Unlock()
  157. if bp.done {
  158. return
  159. }
  160. bp.done = true
  161. close(bp.blockingCh)
  162. }