blob: d952f09f345abe4db0f4d9f58b8bd5c759ee8fea [file] [log] [blame]
onkarkundargi72cfd362020-02-27 12:34:37 +05301/*
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"
23 "errors"
24
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 {
32 name string
33 pickerBuilder PickerBuilder
34 v2PickerBuilder V2PickerBuilder
35 config Config
36}
37
38func (bb *baseBuilder) Build(cc balancer.ClientConn, opt balancer.BuildOptions) balancer.Balancer {
39 bal := &baseBalancer{
40 cc: cc,
41 pickerBuilder: bb.pickerBuilder,
42 v2PickerBuilder: bb.v2PickerBuilder,
43
44 subConns: make(map[resolver.Address]balancer.SubConn),
45 scStates: make(map[balancer.SubConn]connectivity.State),
46 csEvltr: &balancer.ConnectivityStateEvaluator{},
47 config: bb.config,
48 }
49 // 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
58}
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 {
67 cc balancer.ClientConn
68 pickerBuilder PickerBuilder
69 v2PickerBuilder V2PickerBuilder
70
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
77 v2Picker balancer.V2Picker
78 config Config
79}
80
81func (b *baseBalancer) HandleResolvedAddrs(addrs []resolver.Address, err error) {
82 panic("not implemented")
83}
84
85func (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 }
94}
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.
134func (b *baseBalancer) regeneratePicker(err error) {
135 if b.state == connectivity.TransientFailure {
136 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 }
149 return
150 }
151 if b.pickerBuilder != nil {
152 readySCs := make(map[resolver.Address]balancer.SubConn)
153
154 // 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 }
159 }
160 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})
171 }
172}
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) {
210 b.regeneratePicker(state.ConnectionError)
211 }
212
213 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 }
218}
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
234func (p *errPicker) Pick(context.Context, balancer.PickInfo) (balancer.SubConn, func(balancer.DoneInfo), error) {
235 return nil, nil, p.err
236}
237
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}