Dinesh Belwalkar | e63f7f9 | 2019-11-22 23:11:16 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 19 | package base |
| 20 | |
| 21 | import ( |
| 22 | "context" |
Dinesh Belwalkar | 396b652 | 2020-02-06 22:11:53 +0000 | [diff] [blame] | 23 | "errors" |
Dinesh Belwalkar | e63f7f9 | 2019-11-22 23:11:16 +0000 | [diff] [blame] | 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 | |
| 31 | type baseBuilder struct { |
Dinesh Belwalkar | 396b652 | 2020-02-06 22:11:53 +0000 | [diff] [blame] | 32 | name string |
| 33 | pickerBuilder PickerBuilder |
| 34 | v2PickerBuilder V2PickerBuilder |
| 35 | config Config |
Dinesh Belwalkar | e63f7f9 | 2019-11-22 23:11:16 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | func (bb *baseBuilder) Build(cc balancer.ClientConn, opt balancer.BuildOptions) balancer.Balancer { |
Dinesh Belwalkar | 396b652 | 2020-02-06 22:11:53 +0000 | [diff] [blame] | 39 | bal := &baseBalancer{ |
| 40 | cc: cc, |
| 41 | pickerBuilder: bb.pickerBuilder, |
| 42 | v2PickerBuilder: bb.v2PickerBuilder, |
Dinesh Belwalkar | e63f7f9 | 2019-11-22 23:11:16 +0000 | [diff] [blame] | 43 | |
| 44 | subConns: make(map[resolver.Address]balancer.SubConn), |
| 45 | scStates: make(map[balancer.SubConn]connectivity.State), |
| 46 | csEvltr: &balancer.ConnectivityStateEvaluator{}, |
Dinesh Belwalkar | 396b652 | 2020-02-06 22:11:53 +0000 | [diff] [blame] | 47 | config: bb.config, |
Dinesh Belwalkar | e63f7f9 | 2019-11-22 23:11:16 +0000 | [diff] [blame] | 48 | } |
Dinesh Belwalkar | 396b652 | 2020-02-06 22:11:53 +0000 | [diff] [blame] | 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 |
Dinesh Belwalkar | e63f7f9 | 2019-11-22 23:11:16 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | func (bb *baseBuilder) Name() string { |
| 61 | return bb.name |
| 62 | } |
| 63 | |
| 64 | var _ balancer.V2Balancer = (*baseBalancer)(nil) // Assert that we implement V2Balancer |
| 65 | |
| 66 | type baseBalancer struct { |
Dinesh Belwalkar | 396b652 | 2020-02-06 22:11:53 +0000 | [diff] [blame] | 67 | cc balancer.ClientConn |
| 68 | pickerBuilder PickerBuilder |
| 69 | v2PickerBuilder V2PickerBuilder |
Dinesh Belwalkar | e63f7f9 | 2019-11-22 23:11:16 +0000 | [diff] [blame] | 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 |
Dinesh Belwalkar | 396b652 | 2020-02-06 22:11:53 +0000 | [diff] [blame] | 77 | v2Picker balancer.V2Picker |
Dinesh Belwalkar | e63f7f9 | 2019-11-22 23:11:16 +0000 | [diff] [blame] | 78 | config Config |
| 79 | } |
| 80 | |
| 81 | func (b *baseBalancer) HandleResolvedAddrs(addrs []resolver.Address, err error) { |
| 82 | panic("not implemented") |
| 83 | } |
| 84 | |
Dinesh Belwalkar | 396b652 | 2020-02-06 22:11:53 +0000 | [diff] [blame] | 85 | func (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 Belwalkar | e63f7f9 | 2019-11-22 23:11:16 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | func (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 Belwalkar | 396b652 | 2020-02-06 22:11:53 +0000 | [diff] [blame] | 134 | func (b *baseBalancer) regeneratePicker(err error) { |
Dinesh Belwalkar | e63f7f9 | 2019-11-22 23:11:16 +0000 | [diff] [blame] | 135 | if b.state == connectivity.TransientFailure { |
Dinesh Belwalkar | 396b652 | 2020-02-06 22:11:53 +0000 | [diff] [blame] | 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 | } |
Dinesh Belwalkar | e63f7f9 | 2019-11-22 23:11:16 +0000 | [diff] [blame] | 149 | return |
| 150 | } |
Dinesh Belwalkar | 396b652 | 2020-02-06 22:11:53 +0000 | [diff] [blame] | 151 | if b.pickerBuilder != nil { |
| 152 | readySCs := make(map[resolver.Address]balancer.SubConn) |
Dinesh Belwalkar | e63f7f9 | 2019-11-22 23:11:16 +0000 | [diff] [blame] | 153 | |
Dinesh Belwalkar | 396b652 | 2020-02-06 22:11:53 +0000 | [diff] [blame] | 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 | } |
Dinesh Belwalkar | e63f7f9 | 2019-11-22 23:11:16 +0000 | [diff] [blame] | 159 | } |
Dinesh Belwalkar | 396b652 | 2020-02-06 22:11:53 +0000 | [diff] [blame] | 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}) |
Dinesh Belwalkar | e63f7f9 | 2019-11-22 23:11:16 +0000 | [diff] [blame] | 171 | } |
Dinesh Belwalkar | e63f7f9 | 2019-11-22 23:11:16 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | func (b *baseBalancer) HandleSubConnStateChange(sc balancer.SubConn, s connectivity.State) { |
| 175 | panic("not implemented") |
| 176 | } |
| 177 | |
| 178 | func (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 Belwalkar | 396b652 | 2020-02-06 22:11:53 +0000 | [diff] [blame] | 210 | b.regeneratePicker(state.ConnectionError) |
Dinesh Belwalkar | e63f7f9 | 2019-11-22 23:11:16 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Dinesh Belwalkar | 396b652 | 2020-02-06 22:11:53 +0000 | [diff] [blame] | 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 | } |
Dinesh Belwalkar | e63f7f9 | 2019-11-22 23:11:16 +0000 | [diff] [blame] | 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. |
| 222 | func (b *baseBalancer) Close() { |
| 223 | } |
| 224 | |
| 225 | // NewErrPicker returns a picker that always returns err on Pick(). |
| 226 | func NewErrPicker(err error) balancer.Picker { |
| 227 | return &errPicker{err: err} |
| 228 | } |
| 229 | |
| 230 | type errPicker struct { |
| 231 | err error // Pick() always returns this err. |
| 232 | } |
| 233 | |
Dinesh Belwalkar | 396b652 | 2020-02-06 22:11:53 +0000 | [diff] [blame] | 234 | func (p *errPicker) Pick(context.Context, balancer.PickInfo) (balancer.SubConn, func(balancer.DoneInfo), error) { |
Dinesh Belwalkar | e63f7f9 | 2019-11-22 23:11:16 +0000 | [diff] [blame] | 235 | return nil, nil, p.err |
| 236 | } |
Dinesh Belwalkar | 396b652 | 2020-02-06 22:11:53 +0000 | [diff] [blame] | 237 | |
| 238 | // NewErrPickerV2 returns a V2Picker that always returns err on Pick(). |
| 239 | func NewErrPickerV2(err error) balancer.V2Picker { |
| 240 | return &errPickerV2{err: err} |
| 241 | } |
| 242 | |
| 243 | type errPickerV2 struct { |
| 244 | err error // Pick() always returns this err. |
| 245 | } |
| 246 | |
| 247 | func (p *errPickerV2) Pick(info balancer.PickInfo) (balancer.PickResult, error) { |
| 248 | return balancer.PickResult{}, p.err |
| 249 | } |