Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [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 balancer defines APIs for load balancing in gRPC. |
| 20 | // All APIs in this package are experimental. |
| 21 | package balancer |
| 22 | |
| 23 | import ( |
| 24 | "context" |
| 25 | "errors" |
| 26 | "net" |
| 27 | "strings" |
| 28 | |
| 29 | "google.golang.org/grpc/connectivity" |
| 30 | "google.golang.org/grpc/credentials" |
| 31 | "google.golang.org/grpc/internal" |
| 32 | "google.golang.org/grpc/metadata" |
| 33 | "google.golang.org/grpc/resolver" |
| 34 | ) |
| 35 | |
| 36 | var ( |
| 37 | // m is a map from name to balancer builder. |
| 38 | m = make(map[string]Builder) |
| 39 | ) |
| 40 | |
| 41 | // Register registers the balancer builder to the balancer map. b.Name |
| 42 | // (lowercased) will be used as the name registered with this builder. |
| 43 | // |
| 44 | // NOTE: this function must only be called during initialization time (i.e. in |
| 45 | // an init() function), and is not thread-safe. If multiple Balancers are |
| 46 | // registered with the same name, the one registered last will take effect. |
| 47 | func Register(b Builder) { |
| 48 | m[strings.ToLower(b.Name())] = b |
| 49 | } |
| 50 | |
| 51 | // unregisterForTesting deletes the balancer with the given name from the |
| 52 | // balancer map. |
| 53 | // |
| 54 | // This function is not thread-safe. |
| 55 | func unregisterForTesting(name string) { |
| 56 | delete(m, name) |
| 57 | } |
| 58 | |
| 59 | func init() { |
| 60 | internal.BalancerUnregister = unregisterForTesting |
| 61 | } |
| 62 | |
| 63 | // Get returns the resolver builder registered with the given name. |
| 64 | // Note that the compare is done in a case-insensitive fashion. |
| 65 | // If no builder is register with the name, nil will be returned. |
| 66 | func Get(name string) Builder { |
| 67 | if b, ok := m[strings.ToLower(name)]; ok { |
| 68 | return b |
| 69 | } |
| 70 | return nil |
| 71 | } |
| 72 | |
| 73 | // SubConn represents a gRPC sub connection. |
| 74 | // Each sub connection contains a list of addresses. gRPC will |
| 75 | // try to connect to them (in sequence), and stop trying the |
| 76 | // remainder once one connection is successful. |
| 77 | // |
| 78 | // The reconnect backoff will be applied on the list, not a single address. |
| 79 | // For example, try_on_all_addresses -> backoff -> try_on_all_addresses. |
| 80 | // |
| 81 | // All SubConns start in IDLE, and will not try to connect. To trigger |
| 82 | // the connecting, Balancers must call Connect. |
| 83 | // When the connection encounters an error, it will reconnect immediately. |
| 84 | // When the connection becomes IDLE, it will not reconnect unless Connect is |
| 85 | // called. |
| 86 | // |
| 87 | // This interface is to be implemented by gRPC. Users should not need a |
| 88 | // brand new implementation of this interface. For the situations like |
| 89 | // testing, the new implementation should embed this interface. This allows |
| 90 | // gRPC to add new methods to this interface. |
| 91 | type SubConn interface { |
| 92 | // UpdateAddresses updates the addresses used in this SubConn. |
| 93 | // gRPC checks if currently-connected address is still in the new list. |
| 94 | // If it's in the list, the connection will be kept. |
| 95 | // If it's not in the list, the connection will gracefully closed, and |
| 96 | // a new connection will be created. |
| 97 | // |
| 98 | // This will trigger a state transition for the SubConn. |
| 99 | UpdateAddresses([]resolver.Address) |
| 100 | // Connect starts the connecting for this SubConn. |
| 101 | Connect() |
| 102 | } |
| 103 | |
| 104 | // NewSubConnOptions contains options to create new SubConn. |
| 105 | type NewSubConnOptions struct { |
| 106 | // CredsBundle is the credentials bundle that will be used in the created |
| 107 | // SubConn. If it's nil, the original creds from grpc DialOptions will be |
| 108 | // used. |
| 109 | CredsBundle credentials.Bundle |
| 110 | // HealthCheckEnabled indicates whether health check service should be |
| 111 | // enabled on this SubConn |
| 112 | HealthCheckEnabled bool |
| 113 | } |
| 114 | |
| 115 | // ClientConn represents a gRPC ClientConn. |
| 116 | // |
| 117 | // This interface is to be implemented by gRPC. Users should not need a |
| 118 | // brand new implementation of this interface. For the situations like |
| 119 | // testing, the new implementation should embed this interface. This allows |
| 120 | // gRPC to add new methods to this interface. |
| 121 | type ClientConn interface { |
| 122 | // NewSubConn is called by balancer to create a new SubConn. |
| 123 | // It doesn't block and wait for the connections to be established. |
| 124 | // Behaviors of the SubConn can be controlled by options. |
| 125 | NewSubConn([]resolver.Address, NewSubConnOptions) (SubConn, error) |
| 126 | // RemoveSubConn removes the SubConn from ClientConn. |
| 127 | // The SubConn will be shutdown. |
| 128 | RemoveSubConn(SubConn) |
| 129 | |
| 130 | // UpdateBalancerState is called by balancer to notify gRPC that some internal |
| 131 | // state in balancer has changed. |
| 132 | // |
| 133 | // gRPC will update the connectivity state of the ClientConn, and will call pick |
| 134 | // on the new picker to pick new SubConn. |
| 135 | UpdateBalancerState(s connectivity.State, p Picker) |
| 136 | |
| 137 | // ResolveNow is called by balancer to notify gRPC to do a name resolving. |
| 138 | ResolveNow(resolver.ResolveNowOption) |
| 139 | |
| 140 | // Target returns the dial target for this ClientConn. |
| 141 | // |
| 142 | // Deprecated: Use the Target field in the BuildOptions instead. |
| 143 | Target() string |
| 144 | } |
| 145 | |
| 146 | // BuildOptions contains additional information for Build. |
| 147 | type BuildOptions struct { |
| 148 | // DialCreds is the transport credential the Balancer implementation can |
| 149 | // use to dial to a remote load balancer server. The Balancer implementations |
| 150 | // can ignore this if it does not need to talk to another party securely. |
| 151 | DialCreds credentials.TransportCredentials |
| 152 | // CredsBundle is the credentials bundle that the Balancer can use. |
| 153 | CredsBundle credentials.Bundle |
| 154 | // Dialer is the custom dialer the Balancer implementation can use to dial |
| 155 | // to a remote load balancer server. The Balancer implementations |
| 156 | // can ignore this if it doesn't need to talk to remote balancer. |
| 157 | Dialer func(context.Context, string) (net.Conn, error) |
| 158 | // ChannelzParentID is the entity parent's channelz unique identification number. |
| 159 | ChannelzParentID int64 |
| 160 | // Target contains the parsed address info of the dial target. It is the same resolver.Target as |
| 161 | // passed to the resolver. |
| 162 | // See the documentation for the resolver.Target type for details about what it contains. |
| 163 | Target resolver.Target |
| 164 | } |
| 165 | |
| 166 | // Builder creates a balancer. |
| 167 | type Builder interface { |
| 168 | // Build creates a new balancer with the ClientConn. |
| 169 | Build(cc ClientConn, opts BuildOptions) Balancer |
| 170 | // Name returns the name of balancers built by this builder. |
| 171 | // It will be used to pick balancers (for example in service config). |
| 172 | Name() string |
| 173 | } |
| 174 | |
| 175 | // PickOptions contains addition information for the Pick operation. |
| 176 | type PickOptions struct { |
| 177 | // FullMethodName is the method name that NewClientStream() is called |
| 178 | // with. The canonical format is /service/Method. |
| 179 | FullMethodName string |
| 180 | } |
| 181 | |
| 182 | // DoneInfo contains additional information for done. |
| 183 | type DoneInfo struct { |
| 184 | // Err is the rpc error the RPC finished with. It could be nil. |
| 185 | Err error |
| 186 | // Trailer contains the metadata from the RPC's trailer, if present. |
| 187 | Trailer metadata.MD |
| 188 | // BytesSent indicates if any bytes have been sent to the server. |
| 189 | BytesSent bool |
| 190 | // BytesReceived indicates if any byte has been received from the server. |
| 191 | BytesReceived bool |
| 192 | // ServerLoad is the load received from server. It's usually sent as part of |
| 193 | // trailing metadata. |
| 194 | // |
| 195 | // The only supported type now is *orca_v1.LoadReport. |
| 196 | ServerLoad interface{} |
| 197 | } |
| 198 | |
| 199 | var ( |
| 200 | // ErrNoSubConnAvailable indicates no SubConn is available for pick(). |
| 201 | // gRPC will block the RPC until a new picker is available via UpdateBalancerState(). |
| 202 | ErrNoSubConnAvailable = errors.New("no SubConn is available") |
| 203 | // ErrTransientFailure indicates all SubConns are in TransientFailure. |
| 204 | // WaitForReady RPCs will block, non-WaitForReady RPCs will fail. |
| 205 | ErrTransientFailure = errors.New("all SubConns are in TransientFailure") |
| 206 | ) |
| 207 | |
| 208 | // Picker is used by gRPC to pick a SubConn to send an RPC. |
| 209 | // Balancer is expected to generate a new picker from its snapshot every time its |
| 210 | // internal state has changed. |
| 211 | // |
| 212 | // The pickers used by gRPC can be updated by ClientConn.UpdateBalancerState(). |
| 213 | type Picker interface { |
| 214 | // Pick returns the SubConn to be used to send the RPC. |
| 215 | // The returned SubConn must be one returned by NewSubConn(). |
| 216 | // |
| 217 | // This functions is expected to return: |
| 218 | // - a SubConn that is known to be READY; |
| 219 | // - ErrNoSubConnAvailable if no SubConn is available, but progress is being |
| 220 | // made (for example, some SubConn is in CONNECTING mode); |
| 221 | // - other errors if no active connecting is happening (for example, all SubConn |
| 222 | // are in TRANSIENT_FAILURE mode). |
| 223 | // |
| 224 | // If a SubConn is returned: |
| 225 | // - If it is READY, gRPC will send the RPC on it; |
| 226 | // - If it is not ready, or becomes not ready after it's returned, gRPC will |
| 227 | // block until UpdateBalancerState() is called and will call pick on the |
| 228 | // new picker. The done function returned from Pick(), if not nil, will be |
| 229 | // called with nil error, no bytes sent and no bytes received. |
| 230 | // |
| 231 | // If the returned error is not nil: |
| 232 | // - If the error is ErrNoSubConnAvailable, gRPC will block until UpdateBalancerState() |
| 233 | // - If the error is ErrTransientFailure: |
| 234 | // - If the RPC is wait-for-ready, gRPC will block until UpdateBalancerState() |
| 235 | // is called to pick again; |
| 236 | // - Otherwise, RPC will fail with unavailable error. |
| 237 | // - Else (error is other non-nil error): |
| 238 | // - The RPC will fail with unavailable error. |
| 239 | // |
| 240 | // The returned done() function will be called once the rpc has finished, |
| 241 | // with the final status of that RPC. If the SubConn returned is not a |
| 242 | // valid SubConn type, done may not be called. done may be nil if balancer |
| 243 | // doesn't care about the RPC status. |
| 244 | Pick(ctx context.Context, opts PickOptions) (conn SubConn, done func(DoneInfo), err error) |
| 245 | } |
| 246 | |
| 247 | // Balancer takes input from gRPC, manages SubConns, and collects and aggregates |
| 248 | // the connectivity states. |
| 249 | // |
| 250 | // It also generates and updates the Picker used by gRPC to pick SubConns for RPCs. |
| 251 | // |
| 252 | // HandleSubConnectionStateChange, HandleResolvedAddrs and Close are guaranteed |
| 253 | // to be called synchronously from the same goroutine. |
| 254 | // There's no guarantee on picker.Pick, it may be called anytime. |
| 255 | type Balancer interface { |
| 256 | // HandleSubConnStateChange is called by gRPC when the connectivity state |
| 257 | // of sc has changed. |
| 258 | // Balancer is expected to aggregate all the state of SubConn and report |
| 259 | // that back to gRPC. |
| 260 | // Balancer should also generate and update Pickers when its internal state has |
| 261 | // been changed by the new state. |
| 262 | // |
| 263 | // Deprecated: if V2Balancer is implemented by the Balancer, |
| 264 | // UpdateSubConnState will be called instead. |
| 265 | HandleSubConnStateChange(sc SubConn, state connectivity.State) |
| 266 | // HandleResolvedAddrs is called by gRPC to send updated resolved addresses to |
| 267 | // balancers. |
| 268 | // Balancer can create new SubConn or remove SubConn with the addresses. |
| 269 | // An empty address slice and a non-nil error will be passed if the resolver returns |
| 270 | // non-nil error to gRPC. |
| 271 | // |
| 272 | // Deprecated: if V2Balancer is implemented by the Balancer, |
| 273 | // UpdateResolverState will be called instead. |
| 274 | HandleResolvedAddrs([]resolver.Address, error) |
| 275 | // Close closes the balancer. The balancer is not required to call |
| 276 | // ClientConn.RemoveSubConn for its existing SubConns. |
| 277 | Close() |
| 278 | } |
| 279 | |
| 280 | // SubConnState describes the state of a SubConn. |
| 281 | type SubConnState struct { |
| 282 | ConnectivityState connectivity.State |
| 283 | // TODO: add last connection error |
| 284 | } |
| 285 | |
| 286 | // V2Balancer is defined for documentation purposes. If a Balancer also |
| 287 | // implements V2Balancer, its UpdateResolverState method will be called instead |
| 288 | // of HandleResolvedAddrs and its UpdateSubConnState will be called instead of |
| 289 | // HandleSubConnStateChange. |
| 290 | type V2Balancer interface { |
| 291 | // UpdateResolverState is called by gRPC when the state of the resolver |
| 292 | // changes. |
| 293 | UpdateResolverState(resolver.State) |
| 294 | // UpdateSubConnState is called by gRPC when the state of a SubConn |
| 295 | // changes. |
| 296 | UpdateSubConnState(SubConn, SubConnState) |
| 297 | // Close closes the balancer. The balancer is not required to call |
| 298 | // ClientConn.RemoveSubConn for its existing SubConns. |
| 299 | Close() |
| 300 | } |
| 301 | |
| 302 | // ConnectivityStateEvaluator takes the connectivity states of multiple SubConns |
| 303 | // and returns one aggregated connectivity state. |
| 304 | // |
| 305 | // It's not thread safe. |
| 306 | type ConnectivityStateEvaluator struct { |
| 307 | numReady uint64 // Number of addrConns in ready state. |
| 308 | numConnecting uint64 // Number of addrConns in connecting state. |
| 309 | numTransientFailure uint64 // Number of addrConns in transientFailure. |
| 310 | } |
| 311 | |
| 312 | // RecordTransition records state change happening in subConn and based on that |
| 313 | // it evaluates what aggregated state should be. |
| 314 | // |
| 315 | // - If at least one SubConn in Ready, the aggregated state is Ready; |
| 316 | // - Else if at least one SubConn in Connecting, the aggregated state is Connecting; |
| 317 | // - Else the aggregated state is TransientFailure. |
| 318 | // |
| 319 | // Idle and Shutdown are not considered. |
| 320 | func (cse *ConnectivityStateEvaluator) RecordTransition(oldState, newState connectivity.State) connectivity.State { |
| 321 | // Update counters. |
| 322 | for idx, state := range []connectivity.State{oldState, newState} { |
| 323 | updateVal := 2*uint64(idx) - 1 // -1 for oldState and +1 for new. |
| 324 | switch state { |
| 325 | case connectivity.Ready: |
| 326 | cse.numReady += updateVal |
| 327 | case connectivity.Connecting: |
| 328 | cse.numConnecting += updateVal |
| 329 | case connectivity.TransientFailure: |
| 330 | cse.numTransientFailure += updateVal |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | // Evaluate. |
| 335 | if cse.numReady > 0 { |
| 336 | return connectivity.Ready |
| 337 | } |
| 338 | if cse.numConnecting > 0 { |
| 339 | return connectivity.Connecting |
| 340 | } |
| 341 | return connectivity.TransientFailure |
| 342 | } |