2018-03-19 21:17:00 +03:00
|
|
|
// Copyright 2018 The etcd 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.
|
|
|
|
|
2019-07-22 20:57:35 +03:00
|
|
|
// Package balancer implements client balancer.
|
2018-03-19 21:17:00 +03:00
|
|
|
package balancer
|
|
|
|
|
|
|
|
import (
|
2018-04-11 02:58:52 +03:00
|
|
|
"strconv"
|
2018-03-19 21:17:00 +03:00
|
|
|
"sync"
|
2018-04-11 02:58:52 +03:00
|
|
|
"time"
|
2018-03-19 21:17:00 +03:00
|
|
|
|
2019-07-22 20:57:35 +03:00
|
|
|
"go.etcd.io/etcd/clientv3/balancer/connectivity"
|
2019-05-29 00:59:02 +03:00
|
|
|
"go.etcd.io/etcd/clientv3/balancer/picker"
|
2018-03-19 21:17:00 +03:00
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"google.golang.org/grpc/balancer"
|
2019-07-22 20:57:35 +03:00
|
|
|
grpcconnectivity "google.golang.org/grpc/connectivity"
|
2018-03-19 21:17:00 +03:00
|
|
|
"google.golang.org/grpc/resolver"
|
|
|
|
_ "google.golang.org/grpc/resolver/dns" // register DNS resolver
|
|
|
|
_ "google.golang.org/grpc/resolver/passthrough" // register passthrough resolver
|
|
|
|
)
|
|
|
|
|
2019-07-22 20:57:35 +03:00
|
|
|
// Config defines balancer configurations.
|
|
|
|
type Config struct {
|
|
|
|
// Policy configures balancer policy.
|
|
|
|
Policy picker.Policy
|
|
|
|
|
|
|
|
// Picker implements gRPC picker.
|
|
|
|
// Leave empty if "Policy" field is not custom.
|
|
|
|
// TODO: currently custom policy is not supported.
|
|
|
|
// Picker picker.Picker
|
|
|
|
|
|
|
|
// Name defines an additional name for balancer.
|
|
|
|
// Useful for balancer testing to avoid register conflicts.
|
|
|
|
// If empty, defaults to policy name.
|
|
|
|
Name string
|
|
|
|
|
|
|
|
// Logger configures balancer logging.
|
|
|
|
// If nil, logs are discarded.
|
|
|
|
Logger *zap.Logger
|
|
|
|
}
|
|
|
|
|
2018-04-10 20:44:50 +03:00
|
|
|
// RegisterBuilder creates and registers a builder. Since this function calls balancer.Register, it
|
|
|
|
// must be invoked at initialization time.
|
|
|
|
func RegisterBuilder(cfg Config) {
|
|
|
|
bb := &builder{cfg}
|
|
|
|
balancer.Register(bb)
|
2018-03-19 21:17:00 +03:00
|
|
|
|
2019-02-21 21:57:38 +03:00
|
|
|
bb.cfg.Logger.Debug(
|
2018-04-10 20:44:50 +03:00
|
|
|
"registered balancer",
|
|
|
|
zap.String("policy", bb.cfg.Policy.String()),
|
|
|
|
zap.String("name", bb.cfg.Name),
|
|
|
|
)
|
2018-03-19 21:17:00 +03:00
|
|
|
}
|
|
|
|
|
2018-04-10 20:44:50 +03:00
|
|
|
type builder struct {
|
|
|
|
cfg Config
|
2018-03-19 21:17:00 +03:00
|
|
|
}
|
|
|
|
|
2018-04-10 20:44:50 +03:00
|
|
|
// Build is called initially when creating "ccBalancerWrapper".
|
|
|
|
// "grpc.Dial" is called to this client connection.
|
2018-06-08 23:44:53 +03:00
|
|
|
// Then, resolved addresses will be handled via "HandleResolvedAddrs".
|
2018-04-10 20:44:50 +03:00
|
|
|
func (b *builder) Build(cc balancer.ClientConn, opt balancer.BuildOptions) balancer.Balancer {
|
2018-03-19 21:17:00 +03:00
|
|
|
bb := &baseBalancer{
|
2018-04-11 02:58:52 +03:00
|
|
|
id: strconv.FormatInt(time.Now().UnixNano(), 36),
|
2018-04-10 20:44:50 +03:00
|
|
|
policy: b.cfg.Policy,
|
2019-03-18 06:05:22 +03:00
|
|
|
name: b.cfg.Name,
|
2018-04-10 20:44:50 +03:00
|
|
|
lg: b.cfg.Logger,
|
2018-03-19 21:17:00 +03:00
|
|
|
|
|
|
|
addrToSc: make(map[resolver.Address]balancer.SubConn),
|
|
|
|
scToAddr: make(map[balancer.SubConn]resolver.Address),
|
2019-07-22 20:57:35 +03:00
|
|
|
scToSt: make(map[balancer.SubConn]grpcconnectivity.State),
|
2018-03-19 21:17:00 +03:00
|
|
|
|
2019-07-22 20:57:35 +03:00
|
|
|
currentConn: nil,
|
|
|
|
connectivityRecorder: connectivity.New(b.cfg.Logger),
|
2018-03-19 21:17:00 +03:00
|
|
|
|
|
|
|
// initialize picker always returns "ErrNoSubConnAvailable"
|
2019-07-22 20:57:35 +03:00
|
|
|
picker: picker.NewErr(balancer.ErrNoSubConnAvailable),
|
2018-03-19 21:17:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: support multiple connections
|
|
|
|
bb.mu.Lock()
|
2018-04-06 03:33:07 +03:00
|
|
|
bb.currentConn = cc
|
2018-03-19 21:17:00 +03:00
|
|
|
bb.mu.Unlock()
|
|
|
|
|
|
|
|
bb.lg.Info(
|
|
|
|
"built balancer",
|
2018-04-11 02:58:52 +03:00
|
|
|
zap.String("balancer-id", bb.id),
|
2018-03-19 21:17:00 +03:00
|
|
|
zap.String("policy", bb.policy.String()),
|
|
|
|
zap.String("resolver-target", cc.Target()),
|
|
|
|
)
|
|
|
|
return bb
|
|
|
|
}
|
|
|
|
|
2018-04-10 20:44:50 +03:00
|
|
|
// Name implements "grpc/balancer.Builder" interface.
|
|
|
|
func (b *builder) Name() string { return b.cfg.Name }
|
|
|
|
|
|
|
|
// Balancer defines client balancer interface.
|
|
|
|
type Balancer interface {
|
|
|
|
// Balancer is called on specified client connection. Client initiates gRPC
|
|
|
|
// connection with "grpc.Dial(addr, grpc.WithBalancerName)", and then those resolved
|
|
|
|
// addresses are passed to "grpc/balancer.Balancer.HandleResolvedAddrs".
|
|
|
|
// For each resolved address, balancer calls "balancer.ClientConn.NewSubConn".
|
|
|
|
// "grpc/balancer.Balancer.HandleSubConnStateChange" is called when connectivity state
|
|
|
|
// changes, thus requires failover logic in this method.
|
|
|
|
balancer.Balancer
|
|
|
|
|
|
|
|
// Picker calls "Pick" for every client request.
|
|
|
|
picker.Picker
|
|
|
|
}
|
|
|
|
|
|
|
|
type baseBalancer struct {
|
2018-04-11 02:58:52 +03:00
|
|
|
id string
|
2018-04-10 20:44:50 +03:00
|
|
|
policy picker.Policy
|
|
|
|
name string
|
|
|
|
lg *zap.Logger
|
|
|
|
|
|
|
|
mu sync.RWMutex
|
|
|
|
|
|
|
|
addrToSc map[resolver.Address]balancer.SubConn
|
|
|
|
scToAddr map[balancer.SubConn]resolver.Address
|
2019-07-22 20:57:35 +03:00
|
|
|
scToSt map[balancer.SubConn]grpcconnectivity.State
|
2018-04-10 20:44:50 +03:00
|
|
|
|
2019-07-22 20:57:35 +03:00
|
|
|
currentConn balancer.ClientConn
|
|
|
|
connectivityRecorder connectivity.Recorder
|
2018-04-10 20:44:50 +03:00
|
|
|
|
2019-07-22 20:57:35 +03:00
|
|
|
picker picker.Picker
|
2018-04-10 20:44:50 +03:00
|
|
|
}
|
|
|
|
|
2018-03-19 21:17:00 +03:00
|
|
|
// HandleResolvedAddrs implements "grpc/balancer.Balancer" interface.
|
|
|
|
// gRPC sends initial or updated resolved addresses from "Build".
|
|
|
|
func (bb *baseBalancer) HandleResolvedAddrs(addrs []resolver.Address, err error) {
|
|
|
|
if err != nil {
|
2018-04-11 02:58:52 +03:00
|
|
|
bb.lg.Warn("HandleResolvedAddrs called with error", zap.String("balancer-id", bb.id), zap.Error(err))
|
2018-03-19 21:17:00 +03:00
|
|
|
return
|
|
|
|
}
|
2019-07-22 20:57:35 +03:00
|
|
|
bb.lg.Info("resolved",
|
|
|
|
zap.String("picker", bb.picker.String()),
|
|
|
|
zap.String("balancer-id", bb.id),
|
|
|
|
zap.Strings("addresses", addrsToStrings(addrs)),
|
|
|
|
)
|
2018-03-19 21:17:00 +03:00
|
|
|
|
|
|
|
bb.mu.Lock()
|
|
|
|
defer bb.mu.Unlock()
|
|
|
|
|
|
|
|
resolved := make(map[resolver.Address]struct{})
|
|
|
|
for _, addr := range addrs {
|
|
|
|
resolved[addr] = struct{}{}
|
|
|
|
if _, ok := bb.addrToSc[addr]; !ok {
|
2018-04-06 03:33:07 +03:00
|
|
|
sc, err := bb.currentConn.NewSubConn([]resolver.Address{addr}, balancer.NewSubConnOptions{})
|
2018-03-19 21:17:00 +03:00
|
|
|
if err != nil {
|
2019-07-22 20:57:35 +03:00
|
|
|
bb.lg.Warn("NewSubConn failed", zap.String("picker", bb.picker.String()), zap.String("balancer-id", bb.id), zap.Error(err), zap.String("address", addr.Addr))
|
2018-03-19 21:17:00 +03:00
|
|
|
continue
|
|
|
|
}
|
2019-07-22 20:57:35 +03:00
|
|
|
bb.lg.Info("created subconn", zap.String("address", addr.Addr))
|
2018-03-19 21:17:00 +03:00
|
|
|
bb.addrToSc[addr] = sc
|
|
|
|
bb.scToAddr[sc] = addr
|
2019-07-22 20:57:35 +03:00
|
|
|
bb.scToSt[sc] = grpcconnectivity.Idle
|
2018-03-19 21:17:00 +03:00
|
|
|
sc.Connect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for addr, sc := range bb.addrToSc {
|
|
|
|
if _, ok := resolved[addr]; !ok {
|
|
|
|
// was removed by resolver or failed to create subconn
|
2018-04-06 03:33:07 +03:00
|
|
|
bb.currentConn.RemoveSubConn(sc)
|
2018-03-19 21:17:00 +03:00
|
|
|
delete(bb.addrToSc, addr)
|
|
|
|
|
|
|
|
bb.lg.Info(
|
|
|
|
"removed subconn",
|
2019-07-22 20:57:35 +03:00
|
|
|
zap.String("picker", bb.picker.String()),
|
2018-04-11 02:58:52 +03:00
|
|
|
zap.String("balancer-id", bb.id),
|
2018-03-19 21:17:00 +03:00
|
|
|
zap.String("address", addr.Addr),
|
|
|
|
zap.String("subconn", scToString(sc)),
|
|
|
|
)
|
|
|
|
|
|
|
|
// Keep the state of this sc in bb.scToSt until sc's state becomes Shutdown.
|
|
|
|
// The entry will be deleted in HandleSubConnStateChange.
|
|
|
|
// (DO NOT) delete(bb.scToAddr, sc)
|
|
|
|
// (DO NOT) delete(bb.scToSt, sc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleSubConnStateChange implements "grpc/balancer.Balancer" interface.
|
2019-07-22 20:57:35 +03:00
|
|
|
func (bb *baseBalancer) HandleSubConnStateChange(sc balancer.SubConn, s grpcconnectivity.State) {
|
2018-03-19 21:17:00 +03:00
|
|
|
bb.mu.Lock()
|
|
|
|
defer bb.mu.Unlock()
|
|
|
|
|
|
|
|
old, ok := bb.scToSt[sc]
|
|
|
|
if !ok {
|
|
|
|
bb.lg.Warn(
|
|
|
|
"state change for an unknown subconn",
|
2019-07-22 20:57:35 +03:00
|
|
|
zap.String("picker", bb.picker.String()),
|
2018-04-11 02:58:52 +03:00
|
|
|
zap.String("balancer-id", bb.id),
|
2018-03-19 21:17:00 +03:00
|
|
|
zap.String("subconn", scToString(sc)),
|
2019-07-22 20:57:35 +03:00
|
|
|
zap.Int("subconn-size", len(bb.scToAddr)),
|
2018-03-19 21:17:00 +03:00
|
|
|
zap.String("state", s.String()),
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
bb.lg.Info(
|
|
|
|
"state changed",
|
2019-07-22 20:57:35 +03:00
|
|
|
zap.String("picker", bb.picker.String()),
|
2018-04-11 02:58:52 +03:00
|
|
|
zap.String("balancer-id", bb.id),
|
2019-07-22 20:57:35 +03:00
|
|
|
zap.Bool("connected", s == grpcconnectivity.Ready),
|
2018-03-19 21:17:00 +03:00
|
|
|
zap.String("subconn", scToString(sc)),
|
2019-07-22 20:57:35 +03:00
|
|
|
zap.Int("subconn-size", len(bb.scToAddr)),
|
2018-03-19 21:17:00 +03:00
|
|
|
zap.String("address", bb.scToAddr[sc].Addr),
|
|
|
|
zap.String("old-state", old.String()),
|
|
|
|
zap.String("new-state", s.String()),
|
|
|
|
)
|
|
|
|
|
|
|
|
bb.scToSt[sc] = s
|
|
|
|
switch s {
|
2019-07-22 20:57:35 +03:00
|
|
|
case grpcconnectivity.Idle:
|
2018-03-19 21:17:00 +03:00
|
|
|
sc.Connect()
|
2019-07-22 20:57:35 +03:00
|
|
|
case grpcconnectivity.Shutdown:
|
2018-03-19 21:17:00 +03:00
|
|
|
// When an address was removed by resolver, b called RemoveSubConn but
|
|
|
|
// kept the sc's state in scToSt. Remove state for this sc here.
|
|
|
|
delete(bb.scToAddr, sc)
|
|
|
|
delete(bb.scToSt, sc)
|
|
|
|
}
|
|
|
|
|
2019-07-22 20:57:35 +03:00
|
|
|
oldAggrState := bb.connectivityRecorder.GetCurrentState()
|
|
|
|
bb.connectivityRecorder.RecordTransition(old, s)
|
2018-03-19 21:17:00 +03:00
|
|
|
|
2019-07-22 20:57:35 +03:00
|
|
|
// Update balancer picker when one of the following happens:
|
2018-03-19 21:17:00 +03:00
|
|
|
// - this sc became ready from not-ready
|
|
|
|
// - this sc became not-ready from ready
|
|
|
|
// - the aggregated state of balancer became TransientFailure from non-TransientFailure
|
|
|
|
// - the aggregated state of balancer became non-TransientFailure from TransientFailure
|
2019-07-22 20:57:35 +03:00
|
|
|
if (s == grpcconnectivity.Ready) != (old == grpcconnectivity.Ready) ||
|
|
|
|
(bb.connectivityRecorder.GetCurrentState() == grpcconnectivity.TransientFailure) != (oldAggrState == grpcconnectivity.TransientFailure) {
|
|
|
|
bb.updatePicker()
|
2018-03-19 21:17:00 +03:00
|
|
|
}
|
|
|
|
|
2019-07-22 20:57:35 +03:00
|
|
|
bb.currentConn.UpdateBalancerState(bb.connectivityRecorder.GetCurrentState(), bb.picker)
|
2018-03-19 21:17:00 +03:00
|
|
|
}
|
|
|
|
|
2019-07-22 20:57:35 +03:00
|
|
|
func (bb *baseBalancer) updatePicker() {
|
|
|
|
if bb.connectivityRecorder.GetCurrentState() == grpcconnectivity.TransientFailure {
|
|
|
|
bb.picker = picker.NewErr(balancer.ErrTransientFailure)
|
2018-04-11 02:58:52 +03:00
|
|
|
bb.lg.Info(
|
2019-07-22 20:57:35 +03:00
|
|
|
"updated picker to transient error picker",
|
|
|
|
zap.String("picker", bb.picker.String()),
|
2018-04-11 02:58:52 +03:00
|
|
|
zap.String("balancer-id", bb.id),
|
|
|
|
zap.String("policy", bb.policy.String()),
|
|
|
|
)
|
2018-03-19 21:17:00 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// only pass ready subconns to picker
|
|
|
|
scToAddr := make(map[balancer.SubConn]resolver.Address)
|
|
|
|
for addr, sc := range bb.addrToSc {
|
2019-07-22 20:57:35 +03:00
|
|
|
if st, ok := bb.scToSt[sc]; ok && st == grpcconnectivity.Ready {
|
2018-03-19 21:17:00 +03:00
|
|
|
scToAddr[sc] = addr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-22 20:57:35 +03:00
|
|
|
bb.picker = picker.New(picker.Config{
|
|
|
|
Policy: bb.policy,
|
|
|
|
Logger: bb.lg,
|
|
|
|
SubConnToResolverAddress: scToAddr,
|
|
|
|
})
|
2018-03-19 21:17:00 +03:00
|
|
|
bb.lg.Info(
|
2019-07-22 20:57:35 +03:00
|
|
|
"updated picker",
|
|
|
|
zap.String("picker", bb.picker.String()),
|
2018-04-11 02:58:52 +03:00
|
|
|
zap.String("balancer-id", bb.id),
|
2018-03-19 21:17:00 +03:00
|
|
|
zap.String("policy", bb.policy.String()),
|
2019-07-22 20:57:35 +03:00
|
|
|
zap.Strings("subconn-ready", scsToStrings(scToAddr)),
|
|
|
|
zap.Int("subconn-size", len(scToAddr)),
|
2018-03-19 21:17:00 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements "grpc/balancer.Balancer" interface.
|
|
|
|
// 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 (bb *baseBalancer) Close() {
|
|
|
|
// TODO
|
|
|
|
}
|