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