2018-06-08 05:26:10 -04:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Copyright 2017 gRPC authors.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
2019-04-12 14:36:17 -04:00
|
|
|
"context"
|
2018-06-08 05:26:10 -04:00
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"google.golang.org/grpc/balancer"
|
|
|
|
"google.golang.org/grpc/codes"
|
2019-04-12 14:36:17 -04:00
|
|
|
"google.golang.org/grpc/internal/channelz"
|
|
|
|
"google.golang.org/grpc/internal/transport"
|
2018-06-08 05:26:10 -04:00
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
)
|
|
|
|
|
|
|
|
// pickerWrapper is a wrapper of balancer.Picker. It blocks on certain pick
|
|
|
|
// actions and unblock when there's a picker update.
|
|
|
|
type pickerWrapper struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
done bool
|
|
|
|
blockingCh chan struct{}
|
2021-08-12 11:11:44 -04:00
|
|
|
picker balancer.Picker
|
2018-06-08 05:26:10 -04:00
|
|
|
}
|
|
|
|
|
2020-04-16 10:34:35 -04:00
|
|
|
func newPickerWrapper() *pickerWrapper {
|
2021-08-12 11:11:44 -04:00
|
|
|
return &pickerWrapper{blockingCh: make(chan struct{})}
|
2020-04-16 10:34:35 -04:00
|
|
|
}
|
|
|
|
|
2018-06-08 05:26:10 -04:00
|
|
|
// updatePicker is called by UpdateBalancerState. It unblocks all blocked pick.
|
2020-04-16 10:34:35 -04:00
|
|
|
func (pw *pickerWrapper) updatePicker(p balancer.Picker) {
|
|
|
|
pw.mu.Lock()
|
|
|
|
if pw.done {
|
|
|
|
pw.mu.Unlock()
|
2018-06-08 05:26:10 -04:00
|
|
|
return
|
|
|
|
}
|
2020-04-16 10:34:35 -04:00
|
|
|
pw.picker = p
|
|
|
|
// pw.blockingCh should never be nil.
|
|
|
|
close(pw.blockingCh)
|
|
|
|
pw.blockingCh = make(chan struct{})
|
|
|
|
pw.mu.Unlock()
|
2018-06-08 05:26:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func doneChannelzWrapper(acw *acBalancerWrapper, done func(balancer.DoneInfo)) func(balancer.DoneInfo) {
|
|
|
|
acw.mu.Lock()
|
|
|
|
ac := acw.ac
|
|
|
|
acw.mu.Unlock()
|
|
|
|
ac.incrCallsStarted()
|
|
|
|
return func(b balancer.DoneInfo) {
|
|
|
|
if b.Err != nil && b.Err != io.EOF {
|
|
|
|
ac.incrCallsFailed()
|
|
|
|
} else {
|
|
|
|
ac.incrCallsSucceeded()
|
|
|
|
}
|
|
|
|
if done != nil {
|
|
|
|
done(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// pick returns the transport that will be used for the RPC.
|
|
|
|
// It may block in the following cases:
|
|
|
|
// - there's no picker
|
|
|
|
// - the current picker returns ErrNoSubConnAvailable
|
|
|
|
// - the current picker returns other errors and failfast is false.
|
|
|
|
// - the subConn returned by the current picker is not READY
|
|
|
|
// When one of these situations happens, pick blocks until the picker gets updated.
|
2020-04-16 10:34:35 -04:00
|
|
|
func (pw *pickerWrapper) pick(ctx context.Context, failfast bool, info balancer.PickInfo) (transport.ClientTransport, func(balancer.DoneInfo), error) {
|
2019-04-12 14:36:17 -04:00
|
|
|
var ch chan struct{}
|
2018-06-08 05:26:10 -04:00
|
|
|
|
2020-04-16 10:34:35 -04:00
|
|
|
var lastPickErr error
|
2018-06-08 05:26:10 -04:00
|
|
|
for {
|
2020-04-16 10:34:35 -04:00
|
|
|
pw.mu.Lock()
|
|
|
|
if pw.done {
|
|
|
|
pw.mu.Unlock()
|
2018-06-08 05:26:10 -04:00
|
|
|
return nil, nil, ErrClientConnClosing
|
|
|
|
}
|
|
|
|
|
2020-04-16 10:34:35 -04:00
|
|
|
if pw.picker == nil {
|
|
|
|
ch = pw.blockingCh
|
2018-06-08 05:26:10 -04:00
|
|
|
}
|
2020-04-16 10:34:35 -04:00
|
|
|
if ch == pw.blockingCh {
|
2018-06-08 05:26:10 -04:00
|
|
|
// This could happen when either:
|
2020-04-16 10:34:35 -04:00
|
|
|
// - pw.picker is nil (the previous if condition), or
|
2018-06-08 05:26:10 -04:00
|
|
|
// - has called pick on the current picker.
|
2020-04-16 10:34:35 -04:00
|
|
|
pw.mu.Unlock()
|
2018-06-08 05:26:10 -04:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2020-04-16 10:34:35 -04:00
|
|
|
var errStr string
|
|
|
|
if lastPickErr != nil {
|
|
|
|
errStr = "latest balancer error: " + lastPickErr.Error()
|
|
|
|
} else {
|
|
|
|
errStr = ctx.Err().Error()
|
|
|
|
}
|
|
|
|
switch ctx.Err() {
|
|
|
|
case context.DeadlineExceeded:
|
|
|
|
return nil, nil, status.Error(codes.DeadlineExceeded, errStr)
|
|
|
|
case context.Canceled:
|
|
|
|
return nil, nil, status.Error(codes.Canceled, errStr)
|
2019-10-02 13:42:55 -04:00
|
|
|
}
|
2018-06-08 05:26:10 -04:00
|
|
|
case <-ch:
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-04-16 10:34:35 -04:00
|
|
|
ch = pw.blockingCh
|
|
|
|
p := pw.picker
|
|
|
|
pw.mu.Unlock()
|
2018-06-08 05:26:10 -04:00
|
|
|
|
2020-04-16 10:34:35 -04:00
|
|
|
pickResult, err := p.Pick(info)
|
2018-06-08 05:26:10 -04:00
|
|
|
|
|
|
|
if err != nil {
|
2020-04-16 10:34:35 -04:00
|
|
|
if err == balancer.ErrNoSubConnAvailable {
|
2018-06-08 05:26:10 -04:00
|
|
|
continue
|
2020-04-16 10:34:35 -04:00
|
|
|
}
|
|
|
|
if _, ok := status.FromError(err); ok {
|
2021-08-12 11:11:44 -04:00
|
|
|
// Status error: end the RPC unconditionally with this status.
|
2022-11-16 10:32:17 -05:00
|
|
|
return nil, nil, dropError{error: err}
|
2020-04-16 10:34:35 -04:00
|
|
|
}
|
2021-08-12 11:11:44 -04:00
|
|
|
// For all other errors, wait for ready RPCs should block and other
|
|
|
|
// RPCs should fail with unavailable.
|
|
|
|
if !failfast {
|
|
|
|
lastPickErr = err
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return nil, nil, status.Error(codes.Unavailable, err.Error())
|
2018-06-08 05:26:10 -04:00
|
|
|
}
|
|
|
|
|
2020-04-16 10:34:35 -04:00
|
|
|
acw, ok := pickResult.SubConn.(*acBalancerWrapper)
|
2018-06-08 05:26:10 -04:00
|
|
|
if !ok {
|
2022-03-24 08:21:41 -04:00
|
|
|
logger.Errorf("subconn returned from pick is type %T, not *acBalancerWrapper", pickResult.SubConn)
|
2018-06-08 05:26:10 -04:00
|
|
|
continue
|
|
|
|
}
|
2022-03-24 08:21:41 -04:00
|
|
|
if t := acw.getAddrConn().getReadyTransport(); t != nil {
|
2018-06-08 05:26:10 -04:00
|
|
|
if channelz.IsOn() {
|
2020-04-16 10:34:35 -04:00
|
|
|
return t, doneChannelzWrapper(acw, pickResult.Done), nil
|
2018-06-08 05:26:10 -04:00
|
|
|
}
|
2020-04-16 10:34:35 -04:00
|
|
|
return t, pickResult.Done, nil
|
2018-06-08 05:26:10 -04:00
|
|
|
}
|
2020-04-16 10:34:35 -04:00
|
|
|
if pickResult.Done != nil {
|
2019-04-12 14:36:17 -04:00
|
|
|
// Calling done with nil error, no bytes sent and no bytes received.
|
|
|
|
// DoneInfo with default value works.
|
2020-04-16 10:34:35 -04:00
|
|
|
pickResult.Done(balancer.DoneInfo{})
|
2019-04-12 14:36:17 -04:00
|
|
|
}
|
2021-08-12 11:11:44 -04:00
|
|
|
logger.Infof("blockingPicker: the picked transport is not ready, loop back to repick")
|
2018-06-08 05:26:10 -04:00
|
|
|
// If ok == false, ac.state is not READY.
|
|
|
|
// A valid picker always returns READY subConn. This means the state of ac
|
|
|
|
// just changed, and picker will be updated shortly.
|
|
|
|
// continue back to the beginning of the for loop to repick.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 10:34:35 -04:00
|
|
|
func (pw *pickerWrapper) close() {
|
|
|
|
pw.mu.Lock()
|
|
|
|
defer pw.mu.Unlock()
|
|
|
|
if pw.done {
|
2018-06-08 05:26:10 -04:00
|
|
|
return
|
|
|
|
}
|
2020-04-16 10:34:35 -04:00
|
|
|
pw.done = true
|
|
|
|
close(pw.blockingCh)
|
2018-06-08 05:26:10 -04:00
|
|
|
}
|
2022-11-16 10:32:17 -05:00
|
|
|
|
|
|
|
// dropError is a wrapper error that indicates the LB policy wishes to drop the
|
|
|
|
// RPC and not retry it.
|
|
|
|
type dropError struct {
|
|
|
|
error
|
|
|
|
}
|