2018-01-09 18:57:14 +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 grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"google.golang.org/grpc/balancer"
|
|
|
|
"google.golang.org/grpc/connectivity"
|
2020-01-14 10:38:55 +00:00
|
|
|
"google.golang.org/grpc/internal/buffer"
|
2021-02-10 06:51:30 +00:00
|
|
|
"google.golang.org/grpc/internal/channelz"
|
2020-01-14 10:38:55 +00:00
|
|
|
"google.golang.org/grpc/internal/grpcsync"
|
2018-01-09 18:57:14 +00:00
|
|
|
"google.golang.org/grpc/resolver"
|
|
|
|
)
|
|
|
|
|
|
|
|
// scStateUpdate contains the subConn and the new state it changed to.
|
|
|
|
type scStateUpdate struct {
|
|
|
|
sc balancer.SubConn
|
|
|
|
state connectivity.State
|
2020-01-14 10:38:55 +00:00
|
|
|
err error
|
2018-01-09 18:57:14 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 09:47:59 +00:00
|
|
|
// exitIdle contains no data and is just a signal sent on the updateCh in
|
|
|
|
// ccBalancerWrapper to instruct the balancer to exit idle.
|
|
|
|
type exitIdle struct{}
|
|
|
|
|
2018-01-09 18:57:14 +00:00
|
|
|
// ccBalancerWrapper is a wrapper on top of cc for balancers.
|
|
|
|
// It implements balancer.ClientConn interface.
|
|
|
|
type ccBalancerWrapper struct {
|
2021-09-28 09:47:59 +00:00
|
|
|
cc *ClientConn
|
|
|
|
balancerMu sync.Mutex // synchronizes calls to the balancer
|
|
|
|
balancer balancer.Balancer
|
|
|
|
hasExitIdle bool
|
|
|
|
updateCh *buffer.Unbounded
|
|
|
|
closed *grpcsync.Event
|
|
|
|
done *grpcsync.Event
|
2018-01-09 18:57:14 +00:00
|
|
|
|
|
|
|
mu sync.Mutex
|
|
|
|
subConns map[*acBalancerWrapper]struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newCCBalancerWrapper(cc *ClientConn, b balancer.Builder, bopts balancer.BuildOptions) *ccBalancerWrapper {
|
|
|
|
ccb := &ccBalancerWrapper{
|
2020-01-14 10:38:55 +00:00
|
|
|
cc: cc,
|
2021-08-31 12:38:09 +00:00
|
|
|
updateCh: buffer.NewUnbounded(),
|
|
|
|
closed: grpcsync.NewEvent(),
|
2020-01-14 10:38:55 +00:00
|
|
|
done: grpcsync.NewEvent(),
|
|
|
|
subConns: make(map[*acBalancerWrapper]struct{}),
|
2018-01-09 18:57:14 +00:00
|
|
|
}
|
|
|
|
go ccb.watcher()
|
|
|
|
ccb.balancer = b.Build(ccb, bopts)
|
2021-09-28 09:47:59 +00:00
|
|
|
_, ccb.hasExitIdle = ccb.balancer.(balancer.ExitIdler)
|
2018-01-09 18:57:14 +00:00
|
|
|
return ccb
|
|
|
|
}
|
|
|
|
|
2018-07-18 14:47:22 +00:00
|
|
|
// watcher balancer functions sequentially, so the balancer can be implemented
|
2018-01-09 18:57:14 +00:00
|
|
|
// lock-free.
|
|
|
|
func (ccb *ccBalancerWrapper) watcher() {
|
|
|
|
for {
|
|
|
|
select {
|
2021-08-31 12:38:09 +00:00
|
|
|
case t := <-ccb.updateCh.Get():
|
|
|
|
ccb.updateCh.Load()
|
|
|
|
if ccb.closed.HasFired() {
|
2020-01-14 10:38:55 +00:00
|
|
|
break
|
2018-01-09 18:57:14 +00:00
|
|
|
}
|
2021-08-31 12:38:09 +00:00
|
|
|
switch u := t.(type) {
|
|
|
|
case *scStateUpdate:
|
|
|
|
ccb.balancerMu.Lock()
|
|
|
|
ccb.balancer.UpdateSubConnState(u.sc, balancer.SubConnState{ConnectivityState: u.state, ConnectionError: u.err})
|
|
|
|
ccb.balancerMu.Unlock()
|
|
|
|
case *acBalancerWrapper:
|
|
|
|
ccb.mu.Lock()
|
|
|
|
if ccb.subConns != nil {
|
|
|
|
delete(ccb.subConns, u)
|
|
|
|
ccb.cc.removeAddrConn(u.getAddrConn(), errConnDrain)
|
|
|
|
}
|
|
|
|
ccb.mu.Unlock()
|
2021-09-28 09:47:59 +00:00
|
|
|
case exitIdle:
|
|
|
|
if ccb.cc.GetState() == connectivity.Idle {
|
|
|
|
if ei, ok := ccb.balancer.(balancer.ExitIdler); ok {
|
|
|
|
// We already checked that the balancer implements
|
|
|
|
// ExitIdle before pushing the event to updateCh, but
|
|
|
|
// check conditionally again as defensive programming.
|
|
|
|
ccb.balancerMu.Lock()
|
|
|
|
ei.ExitIdle()
|
|
|
|
ccb.balancerMu.Unlock()
|
|
|
|
}
|
|
|
|
}
|
2021-08-31 12:38:09 +00:00
|
|
|
default:
|
|
|
|
logger.Errorf("ccBalancerWrapper.watcher: unknown update %+v, type %T", t, t)
|
|
|
|
}
|
|
|
|
case <-ccb.closed.Done():
|
2018-01-09 18:57:14 +00:00
|
|
|
}
|
|
|
|
|
2021-08-31 12:38:09 +00:00
|
|
|
if ccb.closed.HasFired() {
|
|
|
|
ccb.balancerMu.Lock()
|
2018-01-09 18:57:14 +00:00
|
|
|
ccb.balancer.Close()
|
2021-08-31 12:38:09 +00:00
|
|
|
ccb.balancerMu.Unlock()
|
2018-01-09 18:57:14 +00:00
|
|
|
ccb.mu.Lock()
|
|
|
|
scs := ccb.subConns
|
|
|
|
ccb.subConns = nil
|
|
|
|
ccb.mu.Unlock()
|
2021-08-31 12:38:09 +00:00
|
|
|
ccb.UpdateState(balancer.State{ConnectivityState: connectivity.Connecting, Picker: nil})
|
|
|
|
ccb.done.Fire()
|
|
|
|
// Fire done before removing the addr conns. We can safely unblock
|
|
|
|
// ccb.close and allow the removeAddrConns to happen
|
|
|
|
// asynchronously.
|
2018-01-09 18:57:14 +00:00
|
|
|
for acbw := range scs {
|
|
|
|
ccb.cc.removeAddrConn(acbw.getAddrConn(), errConnDrain)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ccb *ccBalancerWrapper) close() {
|
2021-08-31 12:38:09 +00:00
|
|
|
ccb.closed.Fire()
|
|
|
|
<-ccb.done.Done()
|
2018-01-09 18:57:14 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 09:47:59 +00:00
|
|
|
func (ccb *ccBalancerWrapper) exitIdle() bool {
|
|
|
|
if !ccb.hasExitIdle {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
ccb.updateCh.Put(exitIdle{})
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-01-14 10:38:55 +00:00
|
|
|
func (ccb *ccBalancerWrapper) handleSubConnStateChange(sc balancer.SubConn, s connectivity.State, err error) {
|
2018-01-09 18:57:14 +00:00
|
|
|
// When updating addresses for a SubConn, if the address in use is not in
|
|
|
|
// the new addresses, the old ac will be tearDown() and a new ac will be
|
|
|
|
// created. tearDown() generates a state change with Shutdown state, we
|
|
|
|
// don't want the balancer to receive this state change. So before
|
|
|
|
// tearDown() on the old ac, ac.acbw (acWrapper) will be set to nil, and
|
|
|
|
// this function will be called with (nil, Shutdown). We don't need to call
|
|
|
|
// balancer method in this case.
|
|
|
|
if sc == nil {
|
|
|
|
return
|
|
|
|
}
|
2021-08-31 12:38:09 +00:00
|
|
|
ccb.updateCh.Put(&scStateUpdate{
|
2018-01-09 18:57:14 +00:00
|
|
|
sc: sc,
|
|
|
|
state: s,
|
2020-01-14 10:38:55 +00:00
|
|
|
err: err,
|
2018-01-09 18:57:14 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-14 10:38:55 +00:00
|
|
|
func (ccb *ccBalancerWrapper) updateClientConnState(ccs *balancer.ClientConnState) error {
|
|
|
|
ccb.balancerMu.Lock()
|
|
|
|
defer ccb.balancerMu.Unlock()
|
2021-02-10 06:51:30 +00:00
|
|
|
return ccb.balancer.UpdateClientConnState(*ccs)
|
2020-01-14 10:38:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ccb *ccBalancerWrapper) resolverError(err error) {
|
2021-02-10 06:51:30 +00:00
|
|
|
ccb.balancerMu.Lock()
|
2021-09-28 09:47:59 +00:00
|
|
|
defer ccb.balancerMu.Unlock()
|
2021-02-10 06:51:30 +00:00
|
|
|
ccb.balancer.ResolverError(err)
|
2018-01-09 18:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ccb *ccBalancerWrapper) NewSubConn(addrs []resolver.Address, opts balancer.NewSubConnOptions) (balancer.SubConn, error) {
|
|
|
|
if len(addrs) <= 0 {
|
|
|
|
return nil, fmt.Errorf("grpc: cannot create SubConn with empty address list")
|
|
|
|
}
|
|
|
|
ccb.mu.Lock()
|
|
|
|
defer ccb.mu.Unlock()
|
|
|
|
if ccb.subConns == nil {
|
|
|
|
return nil, fmt.Errorf("grpc: ClientConn balancer wrapper was closed")
|
|
|
|
}
|
2018-11-26 18:23:56 +00:00
|
|
|
ac, err := ccb.cc.newAddrConn(addrs, opts)
|
2018-01-09 18:57:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
acbw := &acBalancerWrapper{ac: ac}
|
|
|
|
acbw.ac.mu.Lock()
|
|
|
|
ac.acbw = acbw
|
|
|
|
acbw.ac.mu.Unlock()
|
|
|
|
ccb.subConns[acbw] = struct{}{}
|
|
|
|
return acbw, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ccb *ccBalancerWrapper) RemoveSubConn(sc balancer.SubConn) {
|
2021-08-31 12:38:09 +00:00
|
|
|
// The RemoveSubConn() is handled in the run() goroutine, to avoid deadlock
|
|
|
|
// during switchBalancer() if the old balancer calls RemoveSubConn() in its
|
|
|
|
// Close().
|
|
|
|
ccb.updateCh.Put(sc)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ccb *ccBalancerWrapper) UpdateAddresses(sc balancer.SubConn, addrs []resolver.Address) {
|
2018-01-09 18:57:14 +00:00
|
|
|
acbw, ok := sc.(*acBalancerWrapper)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2021-08-31 12:38:09 +00:00
|
|
|
acbw.UpdateAddresses(addrs)
|
2018-01-09 18:57:14 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 10:38:55 +00:00
|
|
|
func (ccb *ccBalancerWrapper) UpdateState(s balancer.State) {
|
|
|
|
ccb.mu.Lock()
|
|
|
|
defer ccb.mu.Unlock()
|
|
|
|
if ccb.subConns == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Update picker before updating state. Even though the ordering here does
|
|
|
|
// not matter, it can lead to multiple calls of Pick in the common start-up
|
|
|
|
// case where we wait for ready and then perform an RPC. If the picker is
|
|
|
|
// updated later, we could call the "connecting" picker when the state is
|
|
|
|
// updated, and then call the "ready" picker after the picker gets updated.
|
2021-02-10 06:51:30 +00:00
|
|
|
ccb.cc.blockingpicker.updatePicker(s.Picker)
|
2020-01-14 10:38:55 +00:00
|
|
|
ccb.cc.csMgr.updateState(s.ConnectivityState)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ccb *ccBalancerWrapper) ResolveNow(o resolver.ResolveNowOptions) {
|
2018-01-09 18:57:14 +00:00
|
|
|
ccb.cc.resolveNow(o)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ccb *ccBalancerWrapper) Target() string {
|
|
|
|
return ccb.cc.target
|
|
|
|
}
|
|
|
|
|
|
|
|
// acBalancerWrapper is a wrapper on top of ac for balancers.
|
|
|
|
// It implements balancer.SubConn interface.
|
|
|
|
type acBalancerWrapper struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
ac *addrConn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (acbw *acBalancerWrapper) UpdateAddresses(addrs []resolver.Address) {
|
|
|
|
acbw.mu.Lock()
|
|
|
|
defer acbw.mu.Unlock()
|
|
|
|
if len(addrs) <= 0 {
|
2021-08-31 12:38:09 +00:00
|
|
|
acbw.ac.cc.removeAddrConn(acbw.ac, errConnDrain)
|
2018-01-09 18:57:14 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !acbw.ac.tryUpdateAddrs(addrs) {
|
|
|
|
cc := acbw.ac.cc
|
2018-11-26 18:23:56 +00:00
|
|
|
opts := acbw.ac.scopts
|
2018-01-09 18:57:14 +00:00
|
|
|
acbw.ac.mu.Lock()
|
|
|
|
// Set old ac.acbw to nil so the Shutdown state update will be ignored
|
|
|
|
// by balancer.
|
|
|
|
//
|
|
|
|
// TODO(bar) the state transition could be wrong when tearDown() old ac
|
|
|
|
// and creating new ac, fix the transition.
|
|
|
|
acbw.ac.acbw = nil
|
|
|
|
acbw.ac.mu.Unlock()
|
|
|
|
acState := acbw.ac.getState()
|
2021-08-31 12:38:09 +00:00
|
|
|
acbw.ac.cc.removeAddrConn(acbw.ac, errConnDrain)
|
2018-01-09 18:57:14 +00:00
|
|
|
|
|
|
|
if acState == connectivity.Shutdown {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-28 09:47:59 +00:00
|
|
|
newAC, err := cc.newAddrConn(addrs, opts)
|
2018-01-09 18:57:14 +00:00
|
|
|
if err != nil {
|
2021-02-10 06:51:30 +00:00
|
|
|
channelz.Warningf(logger, acbw.ac.channelzID, "acBalancerWrapper: UpdateAddresses: failed to newAddrConn: %v", err)
|
2018-01-09 18:57:14 +00:00
|
|
|
return
|
|
|
|
}
|
2021-09-28 09:47:59 +00:00
|
|
|
acbw.ac = newAC
|
|
|
|
newAC.mu.Lock()
|
|
|
|
newAC.acbw = acbw
|
|
|
|
newAC.mu.Unlock()
|
2018-01-09 18:57:14 +00:00
|
|
|
if acState != connectivity.Idle {
|
2021-09-28 09:47:59 +00:00
|
|
|
go newAC.connect()
|
2018-01-09 18:57:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (acbw *acBalancerWrapper) Connect() {
|
|
|
|
acbw.mu.Lock()
|
|
|
|
defer acbw.mu.Unlock()
|
2021-09-28 09:47:59 +00:00
|
|
|
go acbw.ac.connect()
|
2018-01-09 18:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (acbw *acBalancerWrapper) getAddrConn() *addrConn {
|
|
|
|
acbw.mu.Lock()
|
|
|
|
defer acbw.mu.Unlock()
|
|
|
|
return acbw.ac
|
|
|
|
}
|