blob: c43dac9ad842be45f3f880d4f73615e0d7f35fee [file] [log] [blame]
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -07001/*
2 *
3 * Copyright 2017 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19package grpc
20
21import (
Arjun E K57a7fcb2020-01-30 06:44:45 +000022 "errors"
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070023
24 "google.golang.org/grpc/balancer"
Arjun E K57a7fcb2020-01-30 06:44:45 +000025 "google.golang.org/grpc/codes"
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070026 "google.golang.org/grpc/connectivity"
27 "google.golang.org/grpc/grpclog"
28 "google.golang.org/grpc/resolver"
Arjun E K57a7fcb2020-01-30 06:44:45 +000029 "google.golang.org/grpc/status"
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070030)
31
32// PickFirstBalancerName is the name of the pick_first balancer.
33const PickFirstBalancerName = "pick_first"
34
35func newPickfirstBuilder() balancer.Builder {
36 return &pickfirstBuilder{}
37}
38
39type pickfirstBuilder struct{}
40
41func (*pickfirstBuilder) Build(cc balancer.ClientConn, opt balancer.BuildOptions) balancer.Balancer {
42 return &pickfirstBalancer{cc: cc}
43}
44
45func (*pickfirstBuilder) Name() string {
46 return PickFirstBalancerName
47}
48
49type pickfirstBalancer struct {
Arjun E K57a7fcb2020-01-30 06:44:45 +000050 state connectivity.State
51 cc balancer.ClientConn
52 sc balancer.SubConn
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070053}
54
Arjun E K57a7fcb2020-01-30 06:44:45 +000055var _ balancer.V2Balancer = &pickfirstBalancer{} // Assert we implement v2
56
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070057func (b *pickfirstBalancer) HandleResolvedAddrs(addrs []resolver.Address, err error) {
58 if err != nil {
Arjun E K57a7fcb2020-01-30 06:44:45 +000059 b.ResolverError(err)
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070060 return
61 }
Arjun E K57a7fcb2020-01-30 06:44:45 +000062 b.UpdateClientConnState(balancer.ClientConnState{ResolverState: resolver.State{Addresses: addrs}}) // Ignore error
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070063}
64
65func (b *pickfirstBalancer) HandleSubConnStateChange(sc balancer.SubConn, s connectivity.State) {
Arjun E K57a7fcb2020-01-30 06:44:45 +000066 b.UpdateSubConnState(sc, balancer.SubConnState{ConnectivityState: s})
67}
68
69func (b *pickfirstBalancer) ResolverError(err error) {
70 switch b.state {
71 case connectivity.TransientFailure, connectivity.Idle, connectivity.Connecting:
72 // Set a failing picker if we don't have a good picker.
73 b.cc.UpdateState(balancer.State{ConnectivityState: connectivity.TransientFailure,
74 Picker: &picker{err: status.Errorf(codes.Unavailable, "name resolver error: %v", err)}},
75 )
76 }
77 if grpclog.V(2) {
78 grpclog.Infof("pickfirstBalancer: ResolverError called with error %v", err)
79 }
80}
81
82func (b *pickfirstBalancer) UpdateClientConnState(cs balancer.ClientConnState) error {
83 if len(cs.ResolverState.Addresses) == 0 {
84 b.ResolverError(errors.New("produced zero addresses"))
85 return balancer.ErrBadResolverState
86 }
87 if b.sc == nil {
88 var err error
89 b.sc, err = b.cc.NewSubConn(cs.ResolverState.Addresses, balancer.NewSubConnOptions{})
90 if err != nil {
91 if grpclog.V(2) {
92 grpclog.Errorf("pickfirstBalancer: failed to NewSubConn: %v", err)
93 }
94 b.state = connectivity.TransientFailure
95 b.cc.UpdateState(balancer.State{ConnectivityState: connectivity.TransientFailure,
96 Picker: &picker{err: status.Errorf(codes.Unavailable, "error creating connection: %v", err)}},
97 )
98 return balancer.ErrBadResolverState
99 }
100 b.state = connectivity.Idle
101 b.cc.UpdateState(balancer.State{ConnectivityState: connectivity.Idle, Picker: &picker{result: balancer.PickResult{SubConn: b.sc}}})
102 b.sc.Connect()
103 } else {
104 b.sc.UpdateAddresses(cs.ResolverState.Addresses)
105 b.sc.Connect()
106 }
107 return nil
108}
109
110func (b *pickfirstBalancer) UpdateSubConnState(sc balancer.SubConn, s balancer.SubConnState) {
111 if grpclog.V(2) {
112 grpclog.Infof("pickfirstBalancer: HandleSubConnStateChange: %p, %v", sc, s)
113 }
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700114 if b.sc != sc {
Arjun E K57a7fcb2020-01-30 06:44:45 +0000115 if grpclog.V(2) {
116 grpclog.Infof("pickfirstBalancer: ignored state change because sc is not recognized")
117 }
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700118 return
119 }
Arjun E K57a7fcb2020-01-30 06:44:45 +0000120 b.state = s.ConnectivityState
121 if s.ConnectivityState == connectivity.Shutdown {
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700122 b.sc = nil
123 return
124 }
125
Arjun E K57a7fcb2020-01-30 06:44:45 +0000126 switch s.ConnectivityState {
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700127 case connectivity.Ready, connectivity.Idle:
Arjun E K57a7fcb2020-01-30 06:44:45 +0000128 b.cc.UpdateState(balancer.State{ConnectivityState: s.ConnectivityState, Picker: &picker{result: balancer.PickResult{SubConn: sc}}})
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700129 case connectivity.Connecting:
Arjun E K57a7fcb2020-01-30 06:44:45 +0000130 b.cc.UpdateState(balancer.State{ConnectivityState: s.ConnectivityState, Picker: &picker{err: balancer.ErrNoSubConnAvailable}})
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700131 case connectivity.TransientFailure:
Arjun E K57a7fcb2020-01-30 06:44:45 +0000132 err := balancer.ErrTransientFailure
133 // TODO: this can be unconditional after the V1 API is removed, as
134 // SubConnState will always contain a connection error.
135 if s.ConnectionError != nil {
136 err = balancer.TransientFailureError(s.ConnectionError)
137 }
138 b.cc.UpdateState(balancer.State{
139 ConnectivityState: s.ConnectivityState,
140 Picker: &picker{err: err},
141 })
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700142 }
143}
144
145func (b *pickfirstBalancer) Close() {
146}
147
148type picker struct {
Arjun E K57a7fcb2020-01-30 06:44:45 +0000149 result balancer.PickResult
150 err error
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700151}
152
Arjun E K57a7fcb2020-01-30 06:44:45 +0000153func (p *picker) Pick(info balancer.PickInfo) (balancer.PickResult, error) {
154 return p.result, p.err
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700155}
156
157func init() {
158 balancer.Register(newPickfirstBuilder())
159}