2018-05-01 23:45:06 +00: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 base
|
|
|
|
|
|
|
|
import (
|
2020-09-09 18:09:42 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-04-17 17:15:55 +00:00
|
|
|
|
2018-05-01 23:45:06 +00:00
|
|
|
"google.golang.org/grpc/balancer"
|
|
|
|
"google.golang.org/grpc/connectivity"
|
|
|
|
"google.golang.org/grpc/grpclog"
|
|
|
|
"google.golang.org/grpc/resolver"
|
|
|
|
)
|
|
|
|
|
2020-09-09 18:09:42 +00:00
|
|
|
var logger = grpclog.Component("balancer")
|
|
|
|
|
2018-05-01 23:45:06 +00:00
|
|
|
type baseBuilder struct {
|
|
|
|
name string
|
|
|
|
pickerBuilder PickerBuilder
|
2019-04-17 17:15:55 +00:00
|
|
|
config Config
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (bb *baseBuilder) Build(cc balancer.ClientConn, opt balancer.BuildOptions) balancer.Balancer {
|
2020-09-09 18:09:42 +00:00
|
|
|
bal := &baseBalancer{
|
2018-05-01 23:45:06 +00:00
|
|
|
cc: cc,
|
|
|
|
pickerBuilder: bb.pickerBuilder,
|
|
|
|
|
|
|
|
subConns: make(map[resolver.Address]balancer.SubConn),
|
|
|
|
scStates: make(map[balancer.SubConn]connectivity.State),
|
2019-04-17 17:15:55 +00:00
|
|
|
csEvltr: &balancer.ConnectivityStateEvaluator{},
|
2020-09-09 18:09:42 +00:00
|
|
|
config: bb.config,
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
2020-09-09 18:09:42 +00:00
|
|
|
// Initialize picker to a picker that always returns
|
|
|
|
// ErrNoSubConnAvailable, because when state of a SubConn changes, we
|
|
|
|
// may call UpdateState with this picker.
|
|
|
|
bal.picker = NewErrPicker(balancer.ErrNoSubConnAvailable)
|
|
|
|
return bal
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (bb *baseBuilder) Name() string {
|
|
|
|
return bb.name
|
|
|
|
}
|
|
|
|
|
|
|
|
type baseBalancer struct {
|
|
|
|
cc balancer.ClientConn
|
|
|
|
pickerBuilder PickerBuilder
|
|
|
|
|
2019-04-17 17:15:55 +00:00
|
|
|
csEvltr *balancer.ConnectivityStateEvaluator
|
2018-05-01 23:45:06 +00:00
|
|
|
state connectivity.State
|
|
|
|
|
|
|
|
subConns map[resolver.Address]balancer.SubConn
|
|
|
|
scStates map[balancer.SubConn]connectivity.State
|
|
|
|
picker balancer.Picker
|
2019-04-17 17:15:55 +00:00
|
|
|
config Config
|
2020-09-09 18:09:42 +00:00
|
|
|
|
|
|
|
resolverErr error // the last error reported by the resolver; cleared on successful resolution
|
|
|
|
connErr error // the last connection error; cleared upon leaving TransientFailure
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 18:09:42 +00:00
|
|
|
func (b *baseBalancer) ResolverError(err error) {
|
|
|
|
b.resolverErr = err
|
|
|
|
if len(b.subConns) == 0 {
|
|
|
|
b.state = connectivity.TransientFailure
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.state != connectivity.TransientFailure {
|
|
|
|
// The picker will not change since the balancer does not currently
|
|
|
|
// report an error.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
b.regeneratePicker()
|
|
|
|
b.cc.UpdateState(balancer.State{
|
|
|
|
ConnectivityState: b.state,
|
|
|
|
Picker: b.picker,
|
|
|
|
})
|
2019-04-17 17:15:55 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 18:09:42 +00:00
|
|
|
func (b *baseBalancer) UpdateClientConnState(s balancer.ClientConnState) error {
|
2019-10-02 20:56:28 +00:00
|
|
|
// TODO: handle s.ResolverState.ServiceConfig?
|
2020-09-09 18:09:42 +00:00
|
|
|
if logger.V(2) {
|
|
|
|
logger.Info("base.baseBalancer: got new ClientConn state: ", s)
|
2019-10-02 20:56:28 +00:00
|
|
|
}
|
2020-09-09 18:09:42 +00:00
|
|
|
// Successful resolution; clear resolver error and ensure we return nil.
|
|
|
|
b.resolverErr = nil
|
2018-05-01 23:45:06 +00:00
|
|
|
// addrsSet is the set converted from addrs, it's used for quick lookup of an address.
|
|
|
|
addrsSet := make(map[resolver.Address]struct{})
|
2019-10-02 20:56:28 +00:00
|
|
|
for _, a := range s.ResolverState.Addresses {
|
2018-05-01 23:45:06 +00:00
|
|
|
addrsSet[a] = struct{}{}
|
|
|
|
if _, ok := b.subConns[a]; !ok {
|
|
|
|
// a is a new address (not existing in b.subConns).
|
2019-04-17 17:15:55 +00:00
|
|
|
sc, err := b.cc.NewSubConn([]resolver.Address{a}, balancer.NewSubConnOptions{HealthCheckEnabled: b.config.HealthCheck})
|
2018-05-01 23:45:06 +00:00
|
|
|
if err != nil {
|
2020-09-09 18:09:42 +00:00
|
|
|
logger.Warningf("base.baseBalancer: failed to create new SubConn: %v", err)
|
2018-05-01 23:45:06 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
b.subConns[a] = sc
|
|
|
|
b.scStates[sc] = connectivity.Idle
|
|
|
|
sc.Connect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for a, sc := range b.subConns {
|
|
|
|
// a was removed by resolver.
|
|
|
|
if _, ok := addrsSet[a]; !ok {
|
|
|
|
b.cc.RemoveSubConn(sc)
|
|
|
|
delete(b.subConns, a)
|
|
|
|
// Keep the state of this sc in b.scStates until sc's state becomes Shutdown.
|
2020-09-09 18:09:42 +00:00
|
|
|
// The entry will be deleted in UpdateSubConnState.
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-09 18:09:42 +00:00
|
|
|
// If resolver state contains no addresses, return an error so ClientConn
|
|
|
|
// will trigger re-resolve. Also records this as an resolver error, so when
|
|
|
|
// the overall state turns transient failure, the error message will have
|
|
|
|
// the zero address information.
|
|
|
|
if len(s.ResolverState.Addresses) == 0 {
|
|
|
|
b.ResolverError(errors.New("produced zero addresses"))
|
|
|
|
return balancer.ErrBadResolverState
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// mergeErrors builds an error from the last connection error and the last
|
|
|
|
// resolver error. Must only be called if b.state is TransientFailure.
|
|
|
|
func (b *baseBalancer) mergeErrors() error {
|
|
|
|
// connErr must always be non-nil unless there are no SubConns, in which
|
|
|
|
// case resolverErr must be non-nil.
|
|
|
|
if b.connErr == nil {
|
|
|
|
return fmt.Errorf("last resolver error: %v", b.resolverErr)
|
|
|
|
}
|
|
|
|
if b.resolverErr == nil {
|
|
|
|
return fmt.Errorf("last connection error: %v", b.connErr)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("last connection error: %v; last resolver error: %v", b.connErr, b.resolverErr)
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// regeneratePicker takes a snapshot of the balancer, and generates a picker
|
|
|
|
// from it. The picker is
|
2020-09-09 18:09:42 +00:00
|
|
|
// - errPicker if the balancer is in TransientFailure,
|
2018-05-01 23:45:06 +00:00
|
|
|
// - built by the pickerBuilder with all READY SubConns otherwise.
|
|
|
|
func (b *baseBalancer) regeneratePicker() {
|
|
|
|
if b.state == connectivity.TransientFailure {
|
2020-09-09 18:09:42 +00:00
|
|
|
b.picker = NewErrPicker(b.mergeErrors())
|
2018-05-01 23:45:06 +00:00
|
|
|
return
|
|
|
|
}
|
2020-09-09 18:09:42 +00:00
|
|
|
readySCs := make(map[balancer.SubConn]SubConnInfo)
|
2018-05-01 23:45:06 +00:00
|
|
|
|
|
|
|
// Filter out all ready SCs from full subConn map.
|
|
|
|
for addr, sc := range b.subConns {
|
|
|
|
if st, ok := b.scStates[sc]; ok && st == connectivity.Ready {
|
2020-09-09 18:09:42 +00:00
|
|
|
readySCs[sc] = SubConnInfo{Address: addr}
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-09 18:09:42 +00:00
|
|
|
b.picker = b.pickerBuilder.Build(PickerBuildInfo{ReadySCs: readySCs})
|
2019-04-17 17:15:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *baseBalancer) UpdateSubConnState(sc balancer.SubConn, state balancer.SubConnState) {
|
|
|
|
s := state.ConnectivityState
|
2020-09-09 18:09:42 +00:00
|
|
|
if logger.V(2) {
|
|
|
|
logger.Infof("base.baseBalancer: handle SubConn state change: %p, %v", sc, s)
|
2019-10-02 20:56:28 +00:00
|
|
|
}
|
2018-05-01 23:45:06 +00:00
|
|
|
oldS, ok := b.scStates[sc]
|
|
|
|
if !ok {
|
2020-09-09 18:09:42 +00:00
|
|
|
if logger.V(2) {
|
|
|
|
logger.Infof("base.baseBalancer: got state changes for an unknown SubConn: %p, %v", sc, s)
|
2019-10-02 20:56:28 +00:00
|
|
|
}
|
2018-05-01 23:45:06 +00:00
|
|
|
return
|
|
|
|
}
|
2020-09-09 18:09:42 +00:00
|
|
|
if oldS == connectivity.TransientFailure && s == connectivity.Connecting {
|
|
|
|
// Once a subconn enters TRANSIENT_FAILURE, ignore subsequent
|
|
|
|
// CONNECTING transitions to prevent the aggregated state from being
|
|
|
|
// always CONNECTING when many backends exist but are all down.
|
|
|
|
return
|
|
|
|
}
|
2018-05-01 23:45:06 +00:00
|
|
|
b.scStates[sc] = s
|
|
|
|
switch s {
|
|
|
|
case connectivity.Idle:
|
|
|
|
sc.Connect()
|
|
|
|
case connectivity.Shutdown:
|
|
|
|
// When an address was removed by resolver, b called RemoveSubConn but
|
|
|
|
// kept the sc's state in scStates. Remove state for this sc here.
|
|
|
|
delete(b.scStates, sc)
|
2020-09-09 18:09:42 +00:00
|
|
|
case connectivity.TransientFailure:
|
|
|
|
// Save error to be reported via picker.
|
|
|
|
b.connErr = state.ConnectionError
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
2019-04-17 17:15:55 +00:00
|
|
|
b.state = b.csEvltr.RecordTransition(oldS, s)
|
2018-05-01 23:45:06 +00:00
|
|
|
|
|
|
|
// Regenerate picker when one of the following happens:
|
2020-09-09 18:09:42 +00:00
|
|
|
// - this sc entered or left ready
|
|
|
|
// - the aggregated state of balancer is TransientFailure
|
|
|
|
// (may need to update error message)
|
2018-05-01 23:45:06 +00:00
|
|
|
if (s == connectivity.Ready) != (oldS == connectivity.Ready) ||
|
2020-09-09 18:09:42 +00:00
|
|
|
b.state == connectivity.TransientFailure {
|
2018-05-01 23:45:06 +00:00
|
|
|
b.regeneratePicker()
|
|
|
|
}
|
|
|
|
|
2020-09-09 18:09:42 +00:00
|
|
|
b.cc.UpdateState(balancer.State{ConnectivityState: b.state, Picker: b.picker})
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close is a nop because base balancer doesn't have internal state to clean up,
|
|
|
|
// and it doesn't need to call RemoveSubConn for the SubConns.
|
|
|
|
func (b *baseBalancer) Close() {
|
|
|
|
}
|
|
|
|
|
2020-09-09 18:09:42 +00:00
|
|
|
// NewErrPicker returns a Picker that always returns err on Pick().
|
2018-05-01 23:45:06 +00:00
|
|
|
func NewErrPicker(err error) balancer.Picker {
|
|
|
|
return &errPicker{err: err}
|
|
|
|
}
|
|
|
|
|
2020-09-09 18:09:42 +00:00
|
|
|
// NewErrPickerV2 is temporarily defined for backward compatibility reasons.
|
|
|
|
//
|
|
|
|
// Deprecated: use NewErrPicker instead.
|
|
|
|
var NewErrPickerV2 = NewErrPicker
|
|
|
|
|
2018-05-01 23:45:06 +00:00
|
|
|
type errPicker struct {
|
|
|
|
err error // Pick() always returns this err.
|
|
|
|
}
|
|
|
|
|
2020-09-09 18:09:42 +00:00
|
|
|
func (p *errPicker) Pick(info balancer.PickInfo) (balancer.PickResult, error) {
|
|
|
|
return balancer.PickResult{}, p.err
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|