blob: d952f09f345abe4db0f4d9f58b8bd5c759ee8fea [file] [log] [blame]
Dinesh Belwalkare63f7f92019-11-22 23:11:16 +00001/*
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 base
20
21import (
22 "context"
Dinesh Belwalkar396b6522020-02-06 22:11:53 +000023 "errors"
Dinesh Belwalkare63f7f92019-11-22 23:11:16 +000024
25 "google.golang.org/grpc/balancer"
26 "google.golang.org/grpc/connectivity"
27 "google.golang.org/grpc/grpclog"
28 "google.golang.org/grpc/resolver"
29)
30
31type baseBuilder struct {
Dinesh Belwalkar396b6522020-02-06 22:11:53 +000032 name string
33 pickerBuilder PickerBuilder
34 v2PickerBuilder V2PickerBuilder
35 config Config
Dinesh Belwalkare63f7f92019-11-22 23:11:16 +000036}
37
38func (bb *baseBuilder) Build(cc balancer.ClientConn, opt balancer.BuildOptions) balancer.Balancer {
Dinesh Belwalkar396b6522020-02-06 22:11:53 +000039 bal := &baseBalancer{
40 cc: cc,
41 pickerBuilder: bb.pickerBuilder,
42 v2PickerBuilder: bb.v2PickerBuilder,
Dinesh Belwalkare63f7f92019-11-22 23:11:16 +000043
44 subConns: make(map[resolver.Address]balancer.SubConn),
45 scStates: make(map[balancer.SubConn]connectivity.State),
46 csEvltr: &balancer.ConnectivityStateEvaluator{},
Dinesh Belwalkar396b6522020-02-06 22:11:53 +000047 config: bb.config,
Dinesh Belwalkare63f7f92019-11-22 23:11:16 +000048 }
Dinesh Belwalkar396b6522020-02-06 22:11:53 +000049 // Initialize picker to a picker that always returns
50 // ErrNoSubConnAvailable, because when state of a SubConn changes, we
51 // may call UpdateState with this picker.
52 if bb.pickerBuilder != nil {
53 bal.picker = NewErrPicker(balancer.ErrNoSubConnAvailable)
54 } else {
55 bal.v2Picker = NewErrPickerV2(balancer.ErrNoSubConnAvailable)
56 }
57 return bal
Dinesh Belwalkare63f7f92019-11-22 23:11:16 +000058}
59
60func (bb *baseBuilder) Name() string {
61 return bb.name
62}
63
64var _ balancer.V2Balancer = (*baseBalancer)(nil) // Assert that we implement V2Balancer
65
66type baseBalancer struct {
Dinesh Belwalkar396b6522020-02-06 22:11:53 +000067 cc balancer.ClientConn
68 pickerBuilder PickerBuilder
69 v2PickerBuilder V2PickerBuilder
Dinesh Belwalkare63f7f92019-11-22 23:11:16 +000070
71 csEvltr *balancer.ConnectivityStateEvaluator
72 state connectivity.State
73
74 subConns map[resolver.Address]balancer.SubConn
75 scStates map[balancer.SubConn]connectivity.State
76 picker balancer.Picker
Dinesh Belwalkar396b6522020-02-06 22:11:53 +000077 v2Picker balancer.V2Picker
Dinesh Belwalkare63f7f92019-11-22 23:11:16 +000078 config Config
79}
80
81func (b *baseBalancer) HandleResolvedAddrs(addrs []resolver.Address, err error) {
82 panic("not implemented")
83}
84
Dinesh Belwalkar396b6522020-02-06 22:11:53 +000085func (b *baseBalancer) ResolverError(err error) {
86 switch b.state {
87 case connectivity.TransientFailure, connectivity.Idle, connectivity.Connecting:
88 if b.picker != nil {
89 b.picker = NewErrPicker(err)
90 } else {
91 b.v2Picker = NewErrPickerV2(err)
92 }
93 }
Dinesh Belwalkare63f7f92019-11-22 23:11:16 +000094}
95
96func (b *baseBalancer) UpdateClientConnState(s balancer.ClientConnState) error {
97 // TODO: handle s.ResolverState.Err (log if not nil) once implemented.
98 // TODO: handle s.ResolverState.ServiceConfig?
99 if grpclog.V(2) {
100 grpclog.Infoln("base.baseBalancer: got new ClientConn state: ", s)
101 }
102 // addrsSet is the set converted from addrs, it's used for quick lookup of an address.
103 addrsSet := make(map[resolver.Address]struct{})
104 for _, a := range s.ResolverState.Addresses {
105 addrsSet[a] = struct{}{}
106 if _, ok := b.subConns[a]; !ok {
107 // a is a new address (not existing in b.subConns).
108 sc, err := b.cc.NewSubConn([]resolver.Address{a}, balancer.NewSubConnOptions{HealthCheckEnabled: b.config.HealthCheck})
109 if err != nil {
110 grpclog.Warningf("base.baseBalancer: failed to create new SubConn: %v", err)
111 continue
112 }
113 b.subConns[a] = sc
114 b.scStates[sc] = connectivity.Idle
115 sc.Connect()
116 }
117 }
118 for a, sc := range b.subConns {
119 // a was removed by resolver.
120 if _, ok := addrsSet[a]; !ok {
121 b.cc.RemoveSubConn(sc)
122 delete(b.subConns, a)
123 // Keep the state of this sc in b.scStates until sc's state becomes Shutdown.
124 // The entry will be deleted in HandleSubConnStateChange.
125 }
126 }
127 return nil
128}
129
130// regeneratePicker takes a snapshot of the balancer, and generates a picker
131// from it. The picker is
132// - errPicker with ErrTransientFailure if the balancer is in TransientFailure,
133// - built by the pickerBuilder with all READY SubConns otherwise.
Dinesh Belwalkar396b6522020-02-06 22:11:53 +0000134func (b *baseBalancer) regeneratePicker(err error) {
Dinesh Belwalkare63f7f92019-11-22 23:11:16 +0000135 if b.state == connectivity.TransientFailure {
Dinesh Belwalkar396b6522020-02-06 22:11:53 +0000136 if b.pickerBuilder != nil {
137 b.picker = NewErrPicker(balancer.ErrTransientFailure)
138 } else {
139 if err != nil {
140 b.v2Picker = NewErrPickerV2(balancer.TransientFailureError(err))
141 } else {
142 // This means the last subchannel transition was not to
143 // TransientFailure (otherwise err must be set), but the
144 // aggregate state of the balancer is TransientFailure, meaning
145 // there are no other addresses.
146 b.v2Picker = NewErrPickerV2(balancer.TransientFailureError(errors.New("resolver returned no addresses")))
147 }
148 }
Dinesh Belwalkare63f7f92019-11-22 23:11:16 +0000149 return
150 }
Dinesh Belwalkar396b6522020-02-06 22:11:53 +0000151 if b.pickerBuilder != nil {
152 readySCs := make(map[resolver.Address]balancer.SubConn)
Dinesh Belwalkare63f7f92019-11-22 23:11:16 +0000153
Dinesh Belwalkar396b6522020-02-06 22:11:53 +0000154 // Filter out all ready SCs from full subConn map.
155 for addr, sc := range b.subConns {
156 if st, ok := b.scStates[sc]; ok && st == connectivity.Ready {
157 readySCs[addr] = sc
158 }
Dinesh Belwalkare63f7f92019-11-22 23:11:16 +0000159 }
Dinesh Belwalkar396b6522020-02-06 22:11:53 +0000160 b.picker = b.pickerBuilder.Build(readySCs)
161 } else {
162 readySCs := make(map[balancer.SubConn]SubConnInfo)
163
164 // Filter out all ready SCs from full subConn map.
165 for addr, sc := range b.subConns {
166 if st, ok := b.scStates[sc]; ok && st == connectivity.Ready {
167 readySCs[sc] = SubConnInfo{Address: addr}
168 }
169 }
170 b.v2Picker = b.v2PickerBuilder.Build(PickerBuildInfo{ReadySCs: readySCs})
Dinesh Belwalkare63f7f92019-11-22 23:11:16 +0000171 }
Dinesh Belwalkare63f7f92019-11-22 23:11:16 +0000172}
173
174func (b *baseBalancer) HandleSubConnStateChange(sc balancer.SubConn, s connectivity.State) {
175 panic("not implemented")
176}
177
178func (b *baseBalancer) UpdateSubConnState(sc balancer.SubConn, state balancer.SubConnState) {
179 s := state.ConnectivityState
180 if grpclog.V(2) {
181 grpclog.Infof("base.baseBalancer: handle SubConn state change: %p, %v", sc, s)
182 }
183 oldS, ok := b.scStates[sc]
184 if !ok {
185 if grpclog.V(2) {
186 grpclog.Infof("base.baseBalancer: got state changes for an unknown SubConn: %p, %v", sc, s)
187 }
188 return
189 }
190 b.scStates[sc] = s
191 switch s {
192 case connectivity.Idle:
193 sc.Connect()
194 case connectivity.Shutdown:
195 // When an address was removed by resolver, b called RemoveSubConn but
196 // kept the sc's state in scStates. Remove state for this sc here.
197 delete(b.scStates, sc)
198 }
199
200 oldAggrState := b.state
201 b.state = b.csEvltr.RecordTransition(oldS, s)
202
203 // Regenerate picker when one of the following happens:
204 // - this sc became ready from not-ready
205 // - this sc became not-ready from ready
206 // - the aggregated state of balancer became TransientFailure from non-TransientFailure
207 // - the aggregated state of balancer became non-TransientFailure from TransientFailure
208 if (s == connectivity.Ready) != (oldS == connectivity.Ready) ||
209 (b.state == connectivity.TransientFailure) != (oldAggrState == connectivity.TransientFailure) {
Dinesh Belwalkar396b6522020-02-06 22:11:53 +0000210 b.regeneratePicker(state.ConnectionError)
Dinesh Belwalkare63f7f92019-11-22 23:11:16 +0000211 }
212
Dinesh Belwalkar396b6522020-02-06 22:11:53 +0000213 if b.picker != nil {
214 b.cc.UpdateBalancerState(b.state, b.picker)
215 } else {
216 b.cc.UpdateState(balancer.State{ConnectivityState: b.state, Picker: b.v2Picker})
217 }
Dinesh Belwalkare63f7f92019-11-22 23:11:16 +0000218}
219
220// Close is a nop because base balancer doesn't have internal state to clean up,
221// and it doesn't need to call RemoveSubConn for the SubConns.
222func (b *baseBalancer) Close() {
223}
224
225// NewErrPicker returns a picker that always returns err on Pick().
226func NewErrPicker(err error) balancer.Picker {
227 return &errPicker{err: err}
228}
229
230type errPicker struct {
231 err error // Pick() always returns this err.
232}
233
Dinesh Belwalkar396b6522020-02-06 22:11:53 +0000234func (p *errPicker) Pick(context.Context, balancer.PickInfo) (balancer.SubConn, func(balancer.DoneInfo), error) {
Dinesh Belwalkare63f7f92019-11-22 23:11:16 +0000235 return nil, nil, p.err
236}
Dinesh Belwalkar396b6522020-02-06 22:11:53 +0000237
238// NewErrPickerV2 returns a V2Picker that always returns err on Pick().
239func NewErrPickerV2(err error) balancer.V2Picker {
240 return &errPickerV2{err: err}
241}
242
243type errPickerV2 struct {
244 err error // Pick() always returns this err.
245}
246
247func (p *errPickerV2) Pick(info balancer.PickInfo) (balancer.PickResult, error) {
248 return balancer.PickResult{}, p.err
249}