gRPC migration

Change-Id: Ib390f6dde0d5a8d6db12ccd7da41135570ad1354
diff --git a/vendor/google.golang.org/grpc/balancer/balancer.go b/vendor/google.golang.org/grpc/balancer/balancer.go
index 9258858..917c242 100644
--- a/vendor/google.golang.org/grpc/balancer/balancer.go
+++ b/vendor/google.golang.org/grpc/balancer/balancer.go
@@ -117,15 +117,6 @@
 	HealthCheckEnabled bool
 }
 
-// State contains the balancer's state relevant to the gRPC ClientConn.
-type State struct {
-	// State contains the connectivity state of the balancer, which is used to
-	// determine the state of the ClientConn.
-	ConnectivityState connectivity.State
-	// Picker is used to choose connections (SubConns) for RPCs.
-	Picker V2Picker
-}
-
 // ClientConn represents a gRPC ClientConn.
 //
 // This interface is to be implemented by gRPC. Users should not need a
@@ -146,19 +137,10 @@
 	//
 	// gRPC will update the connectivity state of the ClientConn, and will call pick
 	// on the new picker to pick new SubConn.
-	//
-	// Deprecated: use UpdateState instead
 	UpdateBalancerState(s connectivity.State, p Picker)
 
-	// UpdateState notifies gRPC that the balancer's internal state has
-	// changed.
-	//
-	// gRPC will update the connectivity state of the ClientConn, and will call pick
-	// on the new picker to pick new SubConns.
-	UpdateState(State)
-
 	// ResolveNow is called by balancer to notify gRPC to do a name resolving.
-	ResolveNow(resolver.ResolveNowOptions)
+	ResolveNow(resolver.ResolveNowOption)
 
 	// Target returns the dial target for this ClientConn.
 	//
@@ -203,14 +185,11 @@
 	ParseConfig(LoadBalancingConfigJSON json.RawMessage) (serviceconfig.LoadBalancingConfig, error)
 }
 
-// PickInfo contains additional information for the Pick operation.
-type PickInfo struct {
+// PickOptions contains addition information for the Pick operation.
+type PickOptions struct {
 	// FullMethodName is the method name that NewClientStream() is called
 	// with. The canonical format is /service/Method.
 	FullMethodName string
-	// Ctx is the RPC's context, and may contain relevant RPC-level information
-	// like the outgoing header metadata.
-	Ctx context.Context
 }
 
 // DoneInfo contains additional information for done.
@@ -236,7 +215,7 @@
 	ErrNoSubConnAvailable = errors.New("no SubConn is available")
 	// ErrTransientFailure indicates all SubConns are in TransientFailure.
 	// WaitForReady RPCs will block, non-WaitForReady RPCs will fail.
-	ErrTransientFailure = TransientFailureError(errors.New("all SubConns are in TransientFailure"))
+	ErrTransientFailure = errors.New("all SubConns are in TransientFailure")
 )
 
 // Picker is used by gRPC to pick a SubConn to send an RPC.
@@ -244,8 +223,6 @@
 // internal state has changed.
 //
 // The pickers used by gRPC can be updated by ClientConn.UpdateBalancerState().
-//
-// Deprecated: use V2Picker instead
 type Picker interface {
 	// Pick returns the SubConn to be used to send the RPC.
 	// The returned SubConn must be one returned by NewSubConn().
@@ -266,76 +243,18 @@
 	//
 	// If the returned error is not nil:
 	// - If the error is ErrNoSubConnAvailable, gRPC will block until UpdateBalancerState()
-	// - If the error is ErrTransientFailure or implements IsTransientFailure()
-	//   bool, returning true:
+	// - If the error is ErrTransientFailure:
 	//   - If the RPC is wait-for-ready, gRPC will block until UpdateBalancerState()
 	//     is called to pick again;
 	//   - Otherwise, RPC will fail with unavailable error.
 	// - Else (error is other non-nil error):
-	//   - The RPC will fail with the error's status code, or Unknown if it is
-	//     not a status error.
+	//   - The RPC will fail with unavailable error.
 	//
 	// The returned done() function will be called once the rpc has finished,
 	// with the final status of that RPC.  If the SubConn returned is not a
 	// valid SubConn type, done may not be called.  done may be nil if balancer
 	// doesn't care about the RPC status.
-	Pick(ctx context.Context, info PickInfo) (conn SubConn, done func(DoneInfo), err error)
-}
-
-// PickResult contains information related to a connection chosen for an RPC.
-type PickResult struct {
-	// SubConn is the connection to use for this pick, if its state is Ready.
-	// If the state is not Ready, gRPC will block the RPC until a new Picker is
-	// provided by the balancer (using ClientConn.UpdateState).  The SubConn
-	// must be one returned by ClientConn.NewSubConn.
-	SubConn SubConn
-
-	// Done is called when the RPC is completed.  If the SubConn is not ready,
-	// this will be called with a nil parameter.  If the SubConn is not a valid
-	// type, Done may not be called.  May be nil if the balancer does not wish
-	// to be notified when the RPC completes.
-	Done func(DoneInfo)
-}
-
-type transientFailureError struct {
-	error
-}
-
-func (e *transientFailureError) IsTransientFailure() bool { return true }
-
-// TransientFailureError wraps err in an error implementing
-// IsTransientFailure() bool, returning true.
-func TransientFailureError(err error) error {
-	return &transientFailureError{error: err}
-}
-
-// V2Picker is used by gRPC to pick a SubConn to send an RPC.
-// Balancer is expected to generate a new picker from its snapshot every time its
-// internal state has changed.
-//
-// The pickers used by gRPC can be updated by ClientConn.UpdateBalancerState().
-type V2Picker interface {
-	// Pick returns the connection to use for this RPC and related information.
-	//
-	// Pick should not block.  If the balancer needs to do I/O or any blocking
-	// or time-consuming work to service this call, it should return
-	// ErrNoSubConnAvailable, and the Pick call will be repeated by gRPC when
-	// the Picker is updated (using ClientConn.UpdateState).
-	//
-	// If an error is returned:
-	//
-	// - If the error is ErrNoSubConnAvailable, gRPC will block until a new
-	//   Picker is provided by the balancer (using ClientConn.UpdateState).
-	//
-	// - If the error implements IsTransientFailure() bool, returning true,
-	//   wait for ready RPCs will wait, but non-wait for ready RPCs will be
-	//   terminated with this error's Error() string and status code
-	//   Unavailable.
-	//
-	// - Any other errors terminate all RPCs with the code and message
-	//   provided.  If the error is not a status error, it will be converted by
-	//   gRPC to a status error with code Unknown.
-	Pick(info PickInfo) (PickResult, error)
+	Pick(ctx context.Context, opts PickOptions) (conn SubConn, done func(DoneInfo), err error)
 }
 
 // Balancer takes input from gRPC, manages SubConns, and collects and aggregates
@@ -373,11 +292,8 @@
 
 // SubConnState describes the state of a SubConn.
 type SubConnState struct {
-	// ConnectivityState is the connectivity state of the SubConn.
 	ConnectivityState connectivity.State
-	// ConnectionError is set if the ConnectivityState is TransientFailure,
-	// describing the reason the SubConn failed.  Otherwise, it is nil.
-	ConnectionError error
+	// TODO: add last connection error
 }
 
 // ClientConnState describes the state of a ClientConn relevant to the
@@ -419,8 +335,9 @@
 //
 // It's not thread safe.
 type ConnectivityStateEvaluator struct {
-	numReady      uint64 // Number of addrConns in ready state.
-	numConnecting uint64 // Number of addrConns in connecting state.
+	numReady            uint64 // Number of addrConns in ready state.
+	numConnecting       uint64 // Number of addrConns in connecting state.
+	numTransientFailure uint64 // Number of addrConns in transientFailure.
 }
 
 // RecordTransition records state change happening in subConn and based on that
@@ -440,6 +357,8 @@
 			cse.numReady += updateVal
 		case connectivity.Connecting:
 			cse.numConnecting += updateVal
+		case connectivity.TransientFailure:
+			cse.numTransientFailure += updateVal
 		}
 	}
 
diff --git a/vendor/google.golang.org/grpc/balancer/base/balancer.go b/vendor/google.golang.org/grpc/balancer/base/balancer.go
index 80559b8..1a5c1aa 100644
--- a/vendor/google.golang.org/grpc/balancer/base/balancer.go
+++ b/vendor/google.golang.org/grpc/balancer/base/balancer.go
@@ -20,8 +20,6 @@
 
 import (
 	"context"
-	"errors"
-	"fmt"
 
 	"google.golang.org/grpc/balancer"
 	"google.golang.org/grpc/connectivity"
@@ -30,32 +28,25 @@
 )
 
 type baseBuilder struct {
-	name            string
-	pickerBuilder   PickerBuilder
-	v2PickerBuilder V2PickerBuilder
-	config          Config
+	name          string
+	pickerBuilder PickerBuilder
+	config        Config
 }
 
 func (bb *baseBuilder) Build(cc balancer.ClientConn, opt balancer.BuildOptions) balancer.Balancer {
-	bal := &baseBalancer{
-		cc:              cc,
-		pickerBuilder:   bb.pickerBuilder,
-		v2PickerBuilder: bb.v2PickerBuilder,
+	return &baseBalancer{
+		cc:            cc,
+		pickerBuilder: bb.pickerBuilder,
 
 		subConns: make(map[resolver.Address]balancer.SubConn),
 		scStates: make(map[balancer.SubConn]connectivity.State),
 		csEvltr:  &balancer.ConnectivityStateEvaluator{},
-		config:   bb.config,
+		// Initialize picker to a picker that always return
+		// ErrNoSubConnAvailable, because when state of a SubConn changes, we
+		// may call UpdateBalancerState with this picker.
+		picker: NewErrPicker(balancer.ErrNoSubConnAvailable),
+		config: bb.config,
 	}
-	// Initialize picker to a picker that always returns
-	// ErrNoSubConnAvailable, because when state of a SubConn changes, we
-	// may call UpdateState with this picker.
-	if bb.pickerBuilder != nil {
-		bal.picker = NewErrPicker(balancer.ErrNoSubConnAvailable)
-	} else {
-		bal.v2Picker = NewErrPickerV2(balancer.ErrNoSubConnAvailable)
-	}
-	return bal
 }
 
 func (bb *baseBuilder) Name() string {
@@ -65,9 +56,8 @@
 var _ balancer.V2Balancer = (*baseBalancer)(nil) // Assert that we implement V2Balancer
 
 type baseBalancer struct {
-	cc              balancer.ClientConn
-	pickerBuilder   PickerBuilder
-	v2PickerBuilder V2PickerBuilder
+	cc            balancer.ClientConn
+	pickerBuilder PickerBuilder
 
 	csEvltr *balancer.ConnectivityStateEvaluator
 	state   connectivity.State
@@ -75,36 +65,15 @@
 	subConns map[resolver.Address]balancer.SubConn
 	scStates map[balancer.SubConn]connectivity.State
 	picker   balancer.Picker
-	v2Picker balancer.V2Picker
 	config   Config
-
-	resolverErr error // the last error reported by the resolver; cleared on successful resolution
-	connErr     error // the last connection error; cleared upon leaving TransientFailure
 }
 
 func (b *baseBalancer) HandleResolvedAddrs(addrs []resolver.Address, err error) {
 	panic("not implemented")
 }
 
-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()
-	if b.picker != nil {
-		b.cc.UpdateBalancerState(b.state, b.picker)
-	} else {
-		b.cc.UpdateState(balancer.State{
-			ConnectivityState: b.state,
-			Picker:            b.v2Picker,
-		})
-	}
+func (b *baseBalancer) ResolverError(error) {
+	// Ignore
 }
 
 func (b *baseBalancer) UpdateClientConnState(s balancer.ClientConnState) error {
@@ -113,8 +82,6 @@
 	if grpclog.V(2) {
 		grpclog.Infoln("base.baseBalancer: got new ClientConn state: ", s)
 	}
-	// Successful resolution; clear resolver error and ensure we return nil.
-	b.resolverErr = nil
 	// addrsSet is the set converted from addrs, it's used for quick lookup of an address.
 	addrsSet := make(map[resolver.Address]struct{})
 	for _, a := range s.ResolverState.Addresses {
@@ -140,65 +107,27 @@
 			// The entry will be deleted in HandleSubConnStateChange.
 		}
 	}
-	// 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)
-}
-
 // regeneratePicker takes a snapshot of the balancer, and generates a picker
 // from it. The picker is
-//  - errPicker if the balancer is in TransientFailure,
+//  - errPicker with ErrTransientFailure if the balancer is in TransientFailure,
 //  - built by the pickerBuilder with all READY SubConns otherwise.
 func (b *baseBalancer) regeneratePicker() {
 	if b.state == connectivity.TransientFailure {
-		if b.pickerBuilder != nil {
-			b.picker = NewErrPicker(balancer.ErrTransientFailure)
-		} else {
-			b.v2Picker = NewErrPickerV2(balancer.TransientFailureError(b.mergeErrors()))
-		}
+		b.picker = NewErrPicker(balancer.ErrTransientFailure)
 		return
 	}
-	if b.pickerBuilder != nil {
-		readySCs := make(map[resolver.Address]balancer.SubConn)
+	readySCs := make(map[resolver.Address]balancer.SubConn)
 
-		// 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 {
-				readySCs[addr] = sc
-			}
+	// 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 {
+			readySCs[addr] = sc
 		}
-		b.picker = b.pickerBuilder.Build(readySCs)
-	} else {
-		readySCs := make(map[balancer.SubConn]SubConnInfo)
-
-		// 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 {
-				readySCs[sc] = SubConnInfo{Address: addr}
-			}
-		}
-		b.v2Picker = b.v2PickerBuilder.Build(PickerBuildInfo{ReadySCs: readySCs})
 	}
+	b.picker = b.pickerBuilder.Build(readySCs)
 }
 
 func (b *baseBalancer) HandleSubConnStateChange(sc balancer.SubConn, s connectivity.State) {
@@ -217,12 +146,6 @@
 		}
 		return
 	}
-	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
-	}
 	b.scStates[sc] = s
 	switch s {
 	case connectivity.Idle:
@@ -231,27 +154,22 @@
 		// 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)
-	case connectivity.TransientFailure:
-		// Save error to be reported via picker.
-		b.connErr = state.ConnectionError
 	}
 
+	oldAggrState := b.state
 	b.state = b.csEvltr.RecordTransition(oldS, s)
 
 	// Regenerate picker when one of the following happens:
-	//  - this sc entered or left ready
-	//  - the aggregated state of balancer is TransientFailure
-	//    (may need to update error message)
+	//  - 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
 	if (s == connectivity.Ready) != (oldS == connectivity.Ready) ||
-		b.state == connectivity.TransientFailure {
+		(b.state == connectivity.TransientFailure) != (oldAggrState == connectivity.TransientFailure) {
 		b.regeneratePicker()
 	}
 
-	if b.picker != nil {
-		b.cc.UpdateBalancerState(b.state, b.picker)
-	} else {
-		b.cc.UpdateState(balancer.State{ConnectivityState: b.state, Picker: b.v2Picker})
-	}
+	b.cc.UpdateBalancerState(b.state, b.picker)
 }
 
 // Close is a nop because base balancer doesn't have internal state to clean up,
@@ -268,19 +186,6 @@
 	err error // Pick() always returns this err.
 }
 
-func (p *errPicker) Pick(context.Context, balancer.PickInfo) (balancer.SubConn, func(balancer.DoneInfo), error) {
+func (p *errPicker) Pick(ctx context.Context, opts balancer.PickOptions) (balancer.SubConn, func(balancer.DoneInfo), error) {
 	return nil, nil, p.err
 }
-
-// NewErrPickerV2 returns a V2Picker that always returns err on Pick().
-func NewErrPickerV2(err error) balancer.V2Picker {
-	return &errPickerV2{err: err}
-}
-
-type errPickerV2 struct {
-	err error // Pick() always returns this err.
-}
-
-func (p *errPickerV2) Pick(info balancer.PickInfo) (balancer.PickResult, error) {
-	return balancer.PickResult{}, p.err
-}
diff --git a/vendor/google.golang.org/grpc/balancer/base/base.go b/vendor/google.golang.org/grpc/balancer/base/base.go
index 4192918..34b1f29 100644
--- a/vendor/google.golang.org/grpc/balancer/base/base.go
+++ b/vendor/google.golang.org/grpc/balancer/base/base.go
@@ -42,26 +42,6 @@
 	Build(readySCs map[resolver.Address]balancer.SubConn) balancer.Picker
 }
 
-// V2PickerBuilder creates balancer.V2Picker.
-type V2PickerBuilder interface {
-	// Build returns a picker that will be used by gRPC to pick a SubConn.
-	Build(info PickerBuildInfo) balancer.V2Picker
-}
-
-// PickerBuildInfo contains information needed by the picker builder to
-// construct a picker.
-type PickerBuildInfo struct {
-	// ReadySCs is a map from all ready SubConns to the Addresses used to
-	// create them.
-	ReadySCs map[balancer.SubConn]SubConnInfo
-}
-
-// SubConnInfo contains information about a SubConn created by the base
-// balancer.
-type SubConnInfo struct {
-	Address resolver.Address // the address used to create this SubConn
-}
-
 // NewBalancerBuilder returns a balancer builder. The balancers
 // built by this builder will use the picker builder to build pickers.
 func NewBalancerBuilder(name string, pb PickerBuilder) balancer.Builder {
@@ -82,12 +62,3 @@
 		config:        config,
 	}
 }
-
-// NewBalancerBuilderV2 returns a base balancer builder configured by the provided config.
-func NewBalancerBuilderV2(name string, pb V2PickerBuilder, config Config) balancer.Builder {
-	return &baseBuilder{
-		name:            name,
-		v2PickerBuilder: pb,
-		config:          config,
-	}
-}
diff --git a/vendor/google.golang.org/grpc/balancer/roundrobin/roundrobin.go b/vendor/google.golang.org/grpc/balancer/roundrobin/roundrobin.go
index d4d6455..29f7a4d 100644
--- a/vendor/google.golang.org/grpc/balancer/roundrobin/roundrobin.go
+++ b/vendor/google.golang.org/grpc/balancer/roundrobin/roundrobin.go
@@ -22,12 +22,14 @@
 package roundrobin
 
 import (
+	"context"
 	"sync"
 
 	"google.golang.org/grpc/balancer"
 	"google.golang.org/grpc/balancer/base"
 	"google.golang.org/grpc/grpclog"
 	"google.golang.org/grpc/internal/grpcrand"
+	"google.golang.org/grpc/resolver"
 )
 
 // Name is the name of round_robin balancer.
@@ -35,7 +37,7 @@
 
 // newBuilder creates a new roundrobin balancer builder.
 func newBuilder() balancer.Builder {
-	return base.NewBalancerBuilderV2(Name, &rrPickerBuilder{}, base.Config{HealthCheck: true})
+	return base.NewBalancerBuilderWithConfig(Name, &rrPickerBuilder{}, base.Config{HealthCheck: true})
 }
 
 func init() {
@@ -44,13 +46,13 @@
 
 type rrPickerBuilder struct{}
 
-func (*rrPickerBuilder) Build(info base.PickerBuildInfo) balancer.V2Picker {
-	grpclog.Infof("roundrobinPicker: newPicker called with info: %v", info)
-	if len(info.ReadySCs) == 0 {
-		return base.NewErrPickerV2(balancer.ErrNoSubConnAvailable)
+func (*rrPickerBuilder) Build(readySCs map[resolver.Address]balancer.SubConn) balancer.Picker {
+	grpclog.Infof("roundrobinPicker: newPicker called with readySCs: %v", readySCs)
+	if len(readySCs) == 0 {
+		return base.NewErrPicker(balancer.ErrNoSubConnAvailable)
 	}
 	var scs []balancer.SubConn
-	for sc := range info.ReadySCs {
+	for _, sc := range readySCs {
 		scs = append(scs, sc)
 	}
 	return &rrPicker{
@@ -72,10 +74,10 @@
 	next int
 }
 
-func (p *rrPicker) Pick(balancer.PickInfo) (balancer.PickResult, error) {
+func (p *rrPicker) Pick(ctx context.Context, opts balancer.PickOptions) (balancer.SubConn, func(balancer.DoneInfo), error) {
 	p.mu.Lock()
 	sc := p.subConns[p.next]
 	p.next = (p.next + 1) % len(p.subConns)
 	p.mu.Unlock()
-	return balancer.PickResult{SubConn: sc}, nil
+	return sc, nil, nil
 }