khenaidoo | 5fc5cea | 2021-08-11 17:39:16 -0400 | [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 | "errors" |
| 23 | "fmt" |
| 24 | |
| 25 | "google.golang.org/grpc/attributes" |
| 26 | "google.golang.org/grpc/balancer" |
| 27 | "google.golang.org/grpc/connectivity" |
| 28 | "google.golang.org/grpc/grpclog" |
| 29 | "google.golang.org/grpc/resolver" |
| 30 | ) |
| 31 | |
| 32 | var logger = grpclog.Component("balancer") |
| 33 | |
| 34 | type baseBuilder struct { |
| 35 | name string |
| 36 | pickerBuilder PickerBuilder |
| 37 | config Config |
| 38 | } |
| 39 | |
| 40 | func (bb *baseBuilder) Build(cc balancer.ClientConn, opt balancer.BuildOptions) balancer.Balancer { |
| 41 | bal := &baseBalancer{ |
| 42 | cc: cc, |
| 43 | pickerBuilder: bb.pickerBuilder, |
| 44 | |
| 45 | subConns: make(map[resolver.Address]subConnInfo), |
| 46 | scStates: make(map[balancer.SubConn]connectivity.State), |
| 47 | csEvltr: &balancer.ConnectivityStateEvaluator{}, |
| 48 | config: bb.config, |
| 49 | } |
| 50 | // Initialize picker to a picker that always returns |
| 51 | // ErrNoSubConnAvailable, because when state of a SubConn changes, we |
| 52 | // may call UpdateState with this picker. |
| 53 | bal.picker = NewErrPicker(balancer.ErrNoSubConnAvailable) |
| 54 | return bal |
| 55 | } |
| 56 | |
| 57 | func (bb *baseBuilder) Name() string { |
| 58 | return bb.name |
| 59 | } |
| 60 | |
| 61 | type subConnInfo struct { |
| 62 | subConn balancer.SubConn |
| 63 | attrs *attributes.Attributes |
| 64 | } |
| 65 | |
| 66 | type baseBalancer struct { |
| 67 | cc balancer.ClientConn |
| 68 | pickerBuilder PickerBuilder |
| 69 | |
| 70 | csEvltr *balancer.ConnectivityStateEvaluator |
| 71 | state connectivity.State |
| 72 | |
| 73 | subConns map[resolver.Address]subConnInfo // `attributes` is stripped from the keys of this map (the addresses) |
| 74 | scStates map[balancer.SubConn]connectivity.State |
| 75 | picker balancer.Picker |
| 76 | config Config |
| 77 | |
| 78 | resolverErr error // the last error reported by the resolver; cleared on successful resolution |
| 79 | connErr error // the last connection error; cleared upon leaving TransientFailure |
| 80 | } |
| 81 | |
| 82 | func (b *baseBalancer) ResolverError(err error) { |
| 83 | b.resolverErr = err |
| 84 | if len(b.subConns) == 0 { |
| 85 | b.state = connectivity.TransientFailure |
| 86 | } |
| 87 | |
| 88 | if b.state != connectivity.TransientFailure { |
| 89 | // The picker will not change since the balancer does not currently |
| 90 | // report an error. |
| 91 | return |
| 92 | } |
| 93 | b.regeneratePicker() |
| 94 | b.cc.UpdateState(balancer.State{ |
| 95 | ConnectivityState: b.state, |
| 96 | Picker: b.picker, |
| 97 | }) |
| 98 | } |
| 99 | |
| 100 | func (b *baseBalancer) UpdateClientConnState(s balancer.ClientConnState) error { |
| 101 | // TODO: handle s.ResolverState.ServiceConfig? |
| 102 | if logger.V(2) { |
| 103 | logger.Info("base.baseBalancer: got new ClientConn state: ", s) |
| 104 | } |
| 105 | // Successful resolution; clear resolver error and ensure we return nil. |
| 106 | b.resolverErr = nil |
| 107 | // addrsSet is the set converted from addrs, it's used for quick lookup of an address. |
| 108 | addrsSet := make(map[resolver.Address]struct{}) |
| 109 | for _, a := range s.ResolverState.Addresses { |
| 110 | // Strip attributes from addresses before using them as map keys. So |
| 111 | // that when two addresses only differ in attributes pointers (but with |
| 112 | // the same attribute content), they are considered the same address. |
| 113 | // |
| 114 | // Note that this doesn't handle the case where the attribute content is |
| 115 | // different. So if users want to set different attributes to create |
| 116 | // duplicate connections to the same backend, it doesn't work. This is |
| 117 | // fine for now, because duplicate is done by setting Metadata today. |
| 118 | // |
| 119 | // TODO: read attributes to handle duplicate connections. |
| 120 | aNoAttrs := a |
| 121 | aNoAttrs.Attributes = nil |
| 122 | addrsSet[aNoAttrs] = struct{}{} |
| 123 | if scInfo, ok := b.subConns[aNoAttrs]; !ok { |
| 124 | // a is a new address (not existing in b.subConns). |
| 125 | // |
| 126 | // When creating SubConn, the original address with attributes is |
| 127 | // passed through. So that connection configurations in attributes |
| 128 | // (like creds) will be used. |
| 129 | sc, err := b.cc.NewSubConn([]resolver.Address{a}, balancer.NewSubConnOptions{HealthCheckEnabled: b.config.HealthCheck}) |
| 130 | if err != nil { |
| 131 | logger.Warningf("base.baseBalancer: failed to create new SubConn: %v", err) |
| 132 | continue |
| 133 | } |
| 134 | b.subConns[aNoAttrs] = subConnInfo{subConn: sc, attrs: a.Attributes} |
| 135 | b.scStates[sc] = connectivity.Idle |
| 136 | b.csEvltr.RecordTransition(connectivity.Shutdown, connectivity.Idle) |
| 137 | sc.Connect() |
| 138 | } else { |
| 139 | // Always update the subconn's address in case the attributes |
| 140 | // changed. |
| 141 | // |
| 142 | // The SubConn does a reflect.DeepEqual of the new and old |
| 143 | // addresses. So this is a noop if the current address is the same |
| 144 | // as the old one (including attributes). |
| 145 | scInfo.attrs = a.Attributes |
| 146 | b.subConns[aNoAttrs] = scInfo |
| 147 | b.cc.UpdateAddresses(scInfo.subConn, []resolver.Address{a}) |
| 148 | } |
| 149 | } |
| 150 | for a, scInfo := range b.subConns { |
| 151 | // a was removed by resolver. |
| 152 | if _, ok := addrsSet[a]; !ok { |
| 153 | b.cc.RemoveSubConn(scInfo.subConn) |
| 154 | delete(b.subConns, a) |
| 155 | // Keep the state of this sc in b.scStates until sc's state becomes Shutdown. |
| 156 | // The entry will be deleted in UpdateSubConnState. |
| 157 | } |
| 158 | } |
| 159 | // If resolver state contains no addresses, return an error so ClientConn |
| 160 | // will trigger re-resolve. Also records this as an resolver error, so when |
| 161 | // the overall state turns transient failure, the error message will have |
| 162 | // the zero address information. |
| 163 | if len(s.ResolverState.Addresses) == 0 { |
| 164 | b.ResolverError(errors.New("produced zero addresses")) |
| 165 | return balancer.ErrBadResolverState |
| 166 | } |
| 167 | return nil |
| 168 | } |
| 169 | |
| 170 | // mergeErrors builds an error from the last connection error and the last |
| 171 | // resolver error. Must only be called if b.state is TransientFailure. |
| 172 | func (b *baseBalancer) mergeErrors() error { |
| 173 | // connErr must always be non-nil unless there are no SubConns, in which |
| 174 | // case resolverErr must be non-nil. |
| 175 | if b.connErr == nil { |
| 176 | return fmt.Errorf("last resolver error: %v", b.resolverErr) |
| 177 | } |
| 178 | if b.resolverErr == nil { |
| 179 | return fmt.Errorf("last connection error: %v", b.connErr) |
| 180 | } |
| 181 | return fmt.Errorf("last connection error: %v; last resolver error: %v", b.connErr, b.resolverErr) |
| 182 | } |
| 183 | |
| 184 | // regeneratePicker takes a snapshot of the balancer, and generates a picker |
| 185 | // from it. The picker is |
| 186 | // - errPicker if the balancer is in TransientFailure, |
| 187 | // - built by the pickerBuilder with all READY SubConns otherwise. |
| 188 | func (b *baseBalancer) regeneratePicker() { |
| 189 | if b.state == connectivity.TransientFailure { |
| 190 | b.picker = NewErrPicker(b.mergeErrors()) |
| 191 | return |
| 192 | } |
| 193 | readySCs := make(map[balancer.SubConn]SubConnInfo) |
| 194 | |
| 195 | // Filter out all ready SCs from full subConn map. |
| 196 | for addr, scInfo := range b.subConns { |
| 197 | if st, ok := b.scStates[scInfo.subConn]; ok && st == connectivity.Ready { |
| 198 | addr.Attributes = scInfo.attrs |
| 199 | readySCs[scInfo.subConn] = SubConnInfo{Address: addr} |
| 200 | } |
| 201 | } |
| 202 | b.picker = b.pickerBuilder.Build(PickerBuildInfo{ReadySCs: readySCs}) |
| 203 | } |
| 204 | |
| 205 | func (b *baseBalancer) UpdateSubConnState(sc balancer.SubConn, state balancer.SubConnState) { |
| 206 | s := state.ConnectivityState |
| 207 | if logger.V(2) { |
| 208 | logger.Infof("base.baseBalancer: handle SubConn state change: %p, %v", sc, s) |
| 209 | } |
| 210 | oldS, ok := b.scStates[sc] |
| 211 | if !ok { |
| 212 | if logger.V(2) { |
| 213 | logger.Infof("base.baseBalancer: got state changes for an unknown SubConn: %p, %v", sc, s) |
| 214 | } |
| 215 | return |
| 216 | } |
| 217 | if oldS == connectivity.TransientFailure && |
| 218 | (s == connectivity.Connecting || s == connectivity.Idle) { |
| 219 | // Once a subconn enters TRANSIENT_FAILURE, ignore subsequent IDLE or |
| 220 | // CONNECTING transitions to prevent the aggregated state from being |
| 221 | // always CONNECTING when many backends exist but are all down. |
| 222 | if s == connectivity.Idle { |
| 223 | sc.Connect() |
| 224 | } |
| 225 | return |
| 226 | } |
| 227 | b.scStates[sc] = s |
| 228 | switch s { |
| 229 | case connectivity.Idle: |
| 230 | sc.Connect() |
| 231 | case connectivity.Shutdown: |
| 232 | // When an address was removed by resolver, b called RemoveSubConn but |
| 233 | // kept the sc's state in scStates. Remove state for this sc here. |
| 234 | delete(b.scStates, sc) |
| 235 | case connectivity.TransientFailure: |
| 236 | // Save error to be reported via picker. |
| 237 | b.connErr = state.ConnectionError |
| 238 | } |
| 239 | |
| 240 | b.state = b.csEvltr.RecordTransition(oldS, s) |
| 241 | |
| 242 | // Regenerate picker when one of the following happens: |
| 243 | // - this sc entered or left ready |
| 244 | // - the aggregated state of balancer is TransientFailure |
| 245 | // (may need to update error message) |
| 246 | if (s == connectivity.Ready) != (oldS == connectivity.Ready) || |
| 247 | b.state == connectivity.TransientFailure { |
| 248 | b.regeneratePicker() |
| 249 | } |
| 250 | b.cc.UpdateState(balancer.State{ConnectivityState: b.state, Picker: b.picker}) |
| 251 | } |
| 252 | |
| 253 | // Close is a nop because base balancer doesn't have internal state to clean up, |
| 254 | // and it doesn't need to call RemoveSubConn for the SubConns. |
| 255 | func (b *baseBalancer) Close() { |
| 256 | } |
| 257 | |
| 258 | // ExitIdle is a nop because the base balancer attempts to stay connected to |
| 259 | // all SubConns at all times. |
| 260 | func (b *baseBalancer) ExitIdle() { |
| 261 | } |
| 262 | |
| 263 | // NewErrPicker returns a Picker that always returns err on Pick(). |
| 264 | func NewErrPicker(err error) balancer.Picker { |
| 265 | return &errPicker{err: err} |
| 266 | } |
| 267 | |
| 268 | // NewErrPickerV2 is temporarily defined for backward compatibility reasons. |
| 269 | // |
| 270 | // Deprecated: use NewErrPicker instead. |
| 271 | var NewErrPickerV2 = NewErrPicker |
| 272 | |
| 273 | type errPicker struct { |
| 274 | err error // Pick() always returns this err. |
| 275 | } |
| 276 | |
| 277 | func (p *errPicker) Pick(info balancer.PickInfo) (balancer.PickResult, error) { |
| 278 | return balancer.PickResult{}, p.err |
| 279 | } |