picker_wrapper.go 4.0 KB

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