mirror of https://github.com/grpc/grpc-go.git
grpclb: teach the manual resolver to handle restarts (#6635)
This commit is contained in:
parent
1457a96132
commit
3156151aee
|
|
@ -43,6 +43,7 @@ import (
|
||||||
"google.golang.org/grpc/internal/pretty"
|
"google.golang.org/grpc/internal/pretty"
|
||||||
"google.golang.org/grpc/internal/resolver/dns"
|
"google.golang.org/grpc/internal/resolver/dns"
|
||||||
"google.golang.org/grpc/resolver"
|
"google.golang.org/grpc/resolver"
|
||||||
|
"google.golang.org/grpc/resolver/manual"
|
||||||
|
|
||||||
durationpb "github.com/golang/protobuf/ptypes/duration"
|
durationpb "github.com/golang/protobuf/ptypes/duration"
|
||||||
lbpb "google.golang.org/grpc/balancer/grpclb/grpc_lb_v1"
|
lbpb "google.golang.org/grpc/balancer/grpclb/grpc_lb_v1"
|
||||||
|
|
@ -135,7 +136,11 @@ func (b *lbBuilder) Build(cc balancer.ClientConn, opt balancer.BuildOptions) bal
|
||||||
// This generates a manual resolver builder with a fixed scheme. This
|
// This generates a manual resolver builder with a fixed scheme. This
|
||||||
// scheme will be used to dial to remote LB, so we can send filtered
|
// scheme will be used to dial to remote LB, so we can send filtered
|
||||||
// address updates to remote LB ClientConn using this manual resolver.
|
// address updates to remote LB ClientConn using this manual resolver.
|
||||||
r := &lbManualResolver{scheme: "grpclb-internal", ccb: cc}
|
mr := manual.NewBuilderWithScheme("grpclb-internal")
|
||||||
|
// ResolveNow() on this manual resolver is forwarded to the parent
|
||||||
|
// ClientConn, so when grpclb client loses contact with the remote balancer,
|
||||||
|
// the parent ClientConn's resolver will re-resolve.
|
||||||
|
mr.ResolveNowCallback = cc.ResolveNow
|
||||||
|
|
||||||
lb := &lbBalancer{
|
lb := &lbBalancer{
|
||||||
cc: newLBCacheClientConn(cc),
|
cc: newLBCacheClientConn(cc),
|
||||||
|
|
@ -145,7 +150,7 @@ func (b *lbBuilder) Build(cc balancer.ClientConn, opt balancer.BuildOptions) bal
|
||||||
fallbackTimeout: b.fallbackTimeout,
|
fallbackTimeout: b.fallbackTimeout,
|
||||||
doneCh: make(chan struct{}),
|
doneCh: make(chan struct{}),
|
||||||
|
|
||||||
manualResolver: r,
|
manualResolver: mr,
|
||||||
subConns: make(map[resolver.Address]balancer.SubConn),
|
subConns: make(map[resolver.Address]balancer.SubConn),
|
||||||
scStates: make(map[balancer.SubConn]connectivity.State),
|
scStates: make(map[balancer.SubConn]connectivity.State),
|
||||||
picker: base.NewErrPicker(balancer.ErrNoSubConnAvailable),
|
picker: base.NewErrPicker(balancer.ErrNoSubConnAvailable),
|
||||||
|
|
@ -193,7 +198,7 @@ type lbBalancer struct {
|
||||||
// manualResolver is used in the remote LB ClientConn inside grpclb. When
|
// manualResolver is used in the remote LB ClientConn inside grpclb. When
|
||||||
// resolved address updates are received by grpclb, filtered updates will be
|
// resolved address updates are received by grpclb, filtered updates will be
|
||||||
// send to remote LB ClientConn through this resolver.
|
// send to remote LB ClientConn through this resolver.
|
||||||
manualResolver *lbManualResolver
|
manualResolver *manual.Resolver
|
||||||
// The ClientConn to talk to the remote balancer.
|
// The ClientConn to talk to the remote balancer.
|
||||||
ccRemoteLB *remoteBalancerCCWrapper
|
ccRemoteLB *remoteBalancerCCWrapper
|
||||||
// backoff for calling remote balancer.
|
// backoff for calling remote balancer.
|
||||||
|
|
|
||||||
|
|
@ -27,67 +27,6 @@ import (
|
||||||
"google.golang.org/grpc/resolver"
|
"google.golang.org/grpc/resolver"
|
||||||
)
|
)
|
||||||
|
|
||||||
// The parent ClientConn should re-resolve when grpclb loses connection to the
|
|
||||||
// remote balancer. When the ClientConn inside grpclb gets a TransientFailure,
|
|
||||||
// it calls lbManualResolver.ResolveNow(), which calls parent ClientConn's
|
|
||||||
// ResolveNow, and eventually results in re-resolve happening in parent
|
|
||||||
// ClientConn's resolver (DNS for example).
|
|
||||||
//
|
|
||||||
// parent
|
|
||||||
// ClientConn
|
|
||||||
// +-----------------------------------------------------------------+
|
|
||||||
// | parent +---------------------------------+ |
|
|
||||||
// | DNS ClientConn | grpclb | |
|
|
||||||
// | resolver balancerWrapper | | |
|
|
||||||
// | + + | grpclb grpclb | |
|
|
||||||
// | | | | ManualResolver ClientConn | |
|
|
||||||
// | | | | + + | |
|
|
||||||
// | | | | | | Transient | |
|
|
||||||
// | | | | | | Failure | |
|
|
||||||
// | | | | | <--------- | | |
|
|
||||||
// | | | <--------------- | ResolveNow | | |
|
|
||||||
// | | <--------- | ResolveNow | | | | |
|
|
||||||
// | | ResolveNow | | | | | |
|
|
||||||
// | | | | | | | |
|
|
||||||
// | + + | + + | |
|
|
||||||
// | +---------------------------------+ |
|
|
||||||
// +-----------------------------------------------------------------+
|
|
||||||
|
|
||||||
// lbManualResolver is used by the ClientConn inside grpclb. It's a manual
|
|
||||||
// resolver with a special ResolveNow() function.
|
|
||||||
//
|
|
||||||
// When ResolveNow() is called, it calls ResolveNow() on the parent ClientConn,
|
|
||||||
// so when grpclb client lose contact with remote balancers, the parent
|
|
||||||
// ClientConn's resolver will re-resolve.
|
|
||||||
type lbManualResolver struct {
|
|
||||||
scheme string
|
|
||||||
ccr resolver.ClientConn
|
|
||||||
|
|
||||||
ccb balancer.ClientConn
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *lbManualResolver) Build(_ resolver.Target, cc resolver.ClientConn, _ resolver.BuildOptions) (resolver.Resolver, error) {
|
|
||||||
r.ccr = cc
|
|
||||||
return r, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *lbManualResolver) Scheme() string {
|
|
||||||
return r.scheme
|
|
||||||
}
|
|
||||||
|
|
||||||
// ResolveNow calls resolveNow on the parent ClientConn.
|
|
||||||
func (r *lbManualResolver) ResolveNow(o resolver.ResolveNowOptions) {
|
|
||||||
r.ccb.ResolveNow(o)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close is a noop for Resolver.
|
|
||||||
func (*lbManualResolver) Close() {}
|
|
||||||
|
|
||||||
// UpdateState calls cc.UpdateState.
|
|
||||||
func (r *lbManualResolver) UpdateState(s resolver.State) {
|
|
||||||
r.ccr.UpdateState(s)
|
|
||||||
}
|
|
||||||
|
|
||||||
const subConnCacheTime = time.Second * 10
|
const subConnCacheTime = time.Second * 10
|
||||||
|
|
||||||
// lbCacheClientConn is a wrapper balancer.ClientConn with a SubConn cache.
|
// lbCacheClientConn is a wrapper balancer.ClientConn with a SubConn cache.
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue