blob: 8f00523c0e24cc907a376e1cfbb94ea1d8682ded [file] [log] [blame]
khenaidoo5fc5cea2021-08-11 17:39:16 -04001/*
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.
21package balancer
22
23import (
24 "context"
25 "encoding/json"
26 "errors"
27 "net"
28 "strings"
29
Akash Kankanala761955c2024-02-21 19:32:20 +053030 "google.golang.org/grpc/channelz"
khenaidoo5fc5cea2021-08-11 17:39:16 -040031 "google.golang.org/grpc/connectivity"
32 "google.golang.org/grpc/credentials"
33 "google.golang.org/grpc/internal"
34 "google.golang.org/grpc/metadata"
35 "google.golang.org/grpc/resolver"
36 "google.golang.org/grpc/serviceconfig"
37)
38
39var (
40 // m is a map from name to balancer builder.
41 m = make(map[string]Builder)
42)
43
44// Register registers the balancer builder to the balancer map. b.Name
45// (lowercased) will be used as the name registered with this builder. If the
46// Builder implements ConfigParser, ParseConfig will be called when new service
47// configs are received by the resolver, and the result will be provided to the
48// Balancer in UpdateClientConnState.
49//
50// NOTE: this function must only be called during initialization time (i.e. in
51// an init() function), and is not thread-safe. If multiple Balancers are
52// registered with the same name, the one registered last will take effect.
53func Register(b Builder) {
54 m[strings.ToLower(b.Name())] = b
55}
56
57// unregisterForTesting deletes the balancer with the given name from the
58// balancer map.
59//
60// This function is not thread-safe.
61func unregisterForTesting(name string) {
62 delete(m, name)
63}
64
65func init() {
66 internal.BalancerUnregister = unregisterForTesting
67}
68
69// Get returns the resolver builder registered with the given name.
70// Note that the compare is done in a case-insensitive fashion.
71// If no builder is register with the name, nil will be returned.
72func Get(name string) Builder {
73 if b, ok := m[strings.ToLower(name)]; ok {
74 return b
75 }
76 return nil
77}
78
79// A SubConn represents a single connection to a gRPC backend service.
80//
81// Each SubConn contains a list of addresses.
82//
83// All SubConns start in IDLE, and will not try to connect. To trigger the
84// connecting, Balancers must call Connect. If a connection re-enters IDLE,
85// Balancers must call Connect again to trigger a new connection attempt.
86//
87// gRPC will try to connect to the addresses in sequence, and stop trying the
88// remainder once the first connection is successful. If an attempt to connect
89// to all addresses encounters an error, the SubConn will enter
90// TRANSIENT_FAILURE for a backoff period, and then transition to IDLE.
91//
92// Once established, if a connection is lost, the SubConn will transition
93// directly to IDLE.
94//
95// This interface is to be implemented by gRPC. Users should not need their own
96// implementation of this interface. For situations like testing, any
97// implementations should embed this interface. This allows gRPC to add new
98// methods to this interface.
99type SubConn interface {
100 // UpdateAddresses updates the addresses used in this SubConn.
101 // gRPC checks if currently-connected address is still in the new list.
102 // If it's in the list, the connection will be kept.
103 // If it's not in the list, the connection will gracefully closed, and
104 // a new connection will be created.
105 //
106 // This will trigger a state transition for the SubConn.
107 //
108 // Deprecated: This method is now part of the ClientConn interface and will
109 // eventually be removed from here.
110 UpdateAddresses([]resolver.Address)
111 // Connect starts the connecting for this SubConn.
112 Connect()
Akash Kankanala761955c2024-02-21 19:32:20 +0530113 // GetOrBuildProducer returns a reference to the existing Producer for this
114 // ProducerBuilder in this SubConn, or, if one does not currently exist,
115 // creates a new one and returns it. Returns a close function which must
116 // be called when the Producer is no longer needed.
117 GetOrBuildProducer(ProducerBuilder) (p Producer, close func())
khenaidoo5fc5cea2021-08-11 17:39:16 -0400118}
119
120// NewSubConnOptions contains options to create new SubConn.
121type NewSubConnOptions struct {
122 // CredsBundle is the credentials bundle that will be used in the created
123 // SubConn. If it's nil, the original creds from grpc DialOptions will be
124 // used.
125 //
126 // Deprecated: Use the Attributes field in resolver.Address to pass
127 // arbitrary data to the credential handshaker.
128 CredsBundle credentials.Bundle
129 // HealthCheckEnabled indicates whether health check service should be
130 // enabled on this SubConn
131 HealthCheckEnabled bool
132}
133
134// State contains the balancer's state relevant to the gRPC ClientConn.
135type State struct {
136 // State contains the connectivity state of the balancer, which is used to
137 // determine the state of the ClientConn.
138 ConnectivityState connectivity.State
139 // Picker is used to choose connections (SubConns) for RPCs.
140 Picker Picker
141}
142
143// ClientConn represents a gRPC ClientConn.
144//
145// This interface is to be implemented by gRPC. Users should not need a
146// brand new implementation of this interface. For the situations like
147// testing, the new implementation should embed this interface. This allows
148// gRPC to add new methods to this interface.
149type ClientConn interface {
150 // NewSubConn is called by balancer to create a new SubConn.
151 // It doesn't block and wait for the connections to be established.
152 // Behaviors of the SubConn can be controlled by options.
153 NewSubConn([]resolver.Address, NewSubConnOptions) (SubConn, error)
154 // RemoveSubConn removes the SubConn from ClientConn.
155 // The SubConn will be shutdown.
156 RemoveSubConn(SubConn)
157 // UpdateAddresses updates the addresses used in the passed in SubConn.
158 // gRPC checks if the currently connected address is still in the new list.
159 // If so, the connection will be kept. Else, the connection will be
160 // gracefully closed, and a new connection will be created.
161 //
162 // This will trigger a state transition for the SubConn.
163 UpdateAddresses(SubConn, []resolver.Address)
164
165 // UpdateState notifies gRPC that the balancer's internal state has
166 // changed.
167 //
168 // gRPC will update the connectivity state of the ClientConn, and will call
169 // Pick on the new Picker to pick new SubConns.
170 UpdateState(State)
171
172 // ResolveNow is called by balancer to notify gRPC to do a name resolving.
173 ResolveNow(resolver.ResolveNowOptions)
174
175 // Target returns the dial target for this ClientConn.
176 //
177 // Deprecated: Use the Target field in the BuildOptions instead.
178 Target() string
179}
180
181// BuildOptions contains additional information for Build.
182type BuildOptions struct {
khenaidoo257f3192021-12-15 16:46:37 -0500183 // DialCreds is the transport credentials to use when communicating with a
184 // remote load balancer server. Balancer implementations which do not
185 // communicate with a remote load balancer server can ignore this field.
khenaidoo5fc5cea2021-08-11 17:39:16 -0400186 DialCreds credentials.TransportCredentials
khenaidoo257f3192021-12-15 16:46:37 -0500187 // CredsBundle is the credentials bundle to use when communicating with a
188 // remote load balancer server. Balancer implementations which do not
189 // communicate with a remote load balancer server can ignore this field.
khenaidoo5fc5cea2021-08-11 17:39:16 -0400190 CredsBundle credentials.Bundle
khenaidoo257f3192021-12-15 16:46:37 -0500191 // Dialer is the custom dialer to use when communicating with a remote load
192 // balancer server. Balancer implementations which do not communicate with a
193 // remote load balancer server can ignore this field.
khenaidoo5fc5cea2021-08-11 17:39:16 -0400194 Dialer func(context.Context, string) (net.Conn, error)
khenaidoo257f3192021-12-15 16:46:37 -0500195 // Authority is the server name to use as part of the authentication
196 // handshake when communicating with a remote load balancer server. Balancer
197 // implementations which do not communicate with a remote load balancer
198 // server can ignore this field.
199 Authority string
200 // ChannelzParentID is the parent ClientConn's channelz ID.
Akash Kankanala761955c2024-02-21 19:32:20 +0530201 ChannelzParentID *channelz.Identifier
khenaidoo5fc5cea2021-08-11 17:39:16 -0400202 // CustomUserAgent is the custom user agent set on the parent ClientConn.
203 // The balancer should set the same custom user agent if it creates a
204 // ClientConn.
205 CustomUserAgent string
khenaidoo257f3192021-12-15 16:46:37 -0500206 // Target contains the parsed address info of the dial target. It is the
207 // same resolver.Target as passed to the resolver. See the documentation for
208 // the resolver.Target type for details about what it contains.
khenaidoo5fc5cea2021-08-11 17:39:16 -0400209 Target resolver.Target
210}
211
212// Builder creates a balancer.
213type Builder interface {
214 // Build creates a new balancer with the ClientConn.
215 Build(cc ClientConn, opts BuildOptions) Balancer
216 // Name returns the name of balancers built by this builder.
217 // It will be used to pick balancers (for example in service config).
218 Name() string
219}
220
221// ConfigParser parses load balancer configs.
222type ConfigParser interface {
223 // ParseConfig parses the JSON load balancer config provided into an
224 // internal form or returns an error if the config is invalid. For future
225 // compatibility reasons, unknown fields in the config should be ignored.
226 ParseConfig(LoadBalancingConfigJSON json.RawMessage) (serviceconfig.LoadBalancingConfig, error)
227}
228
229// PickInfo contains additional information for the Pick operation.
230type PickInfo struct {
231 // FullMethodName is the method name that NewClientStream() is called
232 // with. The canonical format is /service/Method.
233 FullMethodName string
234 // Ctx is the RPC's context, and may contain relevant RPC-level information
235 // like the outgoing header metadata.
236 Ctx context.Context
237}
238
239// DoneInfo contains additional information for done.
240type DoneInfo struct {
241 // Err is the rpc error the RPC finished with. It could be nil.
242 Err error
243 // Trailer contains the metadata from the RPC's trailer, if present.
244 Trailer metadata.MD
245 // BytesSent indicates if any bytes have been sent to the server.
246 BytesSent bool
247 // BytesReceived indicates if any byte has been received from the server.
248 BytesReceived bool
249 // ServerLoad is the load received from server. It's usually sent as part of
250 // trailing metadata.
251 //
Akash Kankanala761955c2024-02-21 19:32:20 +0530252 // The only supported type now is *orca_v3.LoadReport.
khenaidoo5fc5cea2021-08-11 17:39:16 -0400253 ServerLoad interface{}
254}
255
256var (
257 // ErrNoSubConnAvailable indicates no SubConn is available for pick().
258 // gRPC will block the RPC until a new picker is available via UpdateState().
259 ErrNoSubConnAvailable = errors.New("no SubConn is available")
260 // ErrTransientFailure indicates all SubConns are in TransientFailure.
261 // WaitForReady RPCs will block, non-WaitForReady RPCs will fail.
262 //
263 // Deprecated: return an appropriate error based on the last resolution or
264 // connection attempt instead. The behavior is the same for any non-gRPC
265 // status error.
266 ErrTransientFailure = errors.New("all SubConns are in TransientFailure")
267)
268
269// PickResult contains information related to a connection chosen for an RPC.
270type PickResult struct {
271 // SubConn is the connection to use for this pick, if its state is Ready.
272 // If the state is not Ready, gRPC will block the RPC until a new Picker is
273 // provided by the balancer (using ClientConn.UpdateState). The SubConn
274 // must be one returned by ClientConn.NewSubConn.
275 SubConn SubConn
276
277 // Done is called when the RPC is completed. If the SubConn is not ready,
278 // this will be called with a nil parameter. If the SubConn is not a valid
279 // type, Done may not be called. May be nil if the balancer does not wish
280 // to be notified when the RPC completes.
281 Done func(DoneInfo)
Akash Kankanala761955c2024-02-21 19:32:20 +0530282
283 // Metadata provides a way for LB policies to inject arbitrary per-call
284 // metadata. Any metadata returned here will be merged with existing
285 // metadata added by the client application.
286 //
287 // LB policies with child policies are responsible for propagating metadata
288 // injected by their children to the ClientConn, as part of Pick().
289 Metadata metadata.MD
khenaidoo5fc5cea2021-08-11 17:39:16 -0400290}
291
292// TransientFailureError returns e. It exists for backward compatibility and
293// will be deleted soon.
294//
295// Deprecated: no longer necessary, picker errors are treated this way by
296// default.
297func TransientFailureError(e error) error { return e }
298
299// Picker is used by gRPC to pick a SubConn to send an RPC.
300// Balancer is expected to generate a new picker from its snapshot every time its
301// internal state has changed.
302//
303// The pickers used by gRPC can be updated by ClientConn.UpdateState().
304type Picker interface {
305 // Pick returns the connection to use for this RPC and related information.
306 //
307 // Pick should not block. If the balancer needs to do I/O or any blocking
308 // or time-consuming work to service this call, it should return
309 // ErrNoSubConnAvailable, and the Pick call will be repeated by gRPC when
310 // the Picker is updated (using ClientConn.UpdateState).
311 //
312 // If an error is returned:
313 //
314 // - If the error is ErrNoSubConnAvailable, gRPC will block until a new
315 // Picker is provided by the balancer (using ClientConn.UpdateState).
316 //
317 // - If the error is a status error (implemented by the grpc/status
318 // package), gRPC will terminate the RPC with the code and message
319 // provided.
320 //
321 // - For all other errors, wait for ready RPCs will wait, but non-wait for
322 // ready RPCs will be terminated with this error's Error() string and
323 // status code Unavailable.
324 Pick(info PickInfo) (PickResult, error)
325}
326
327// Balancer takes input from gRPC, manages SubConns, and collects and aggregates
328// the connectivity states.
329//
330// It also generates and updates the Picker used by gRPC to pick SubConns for RPCs.
331//
332// UpdateClientConnState, ResolverError, UpdateSubConnState, and Close are
333// guaranteed to be called synchronously from the same goroutine. There's no
334// guarantee on picker.Pick, it may be called anytime.
335type Balancer interface {
336 // UpdateClientConnState is called by gRPC when the state of the ClientConn
337 // changes. If the error returned is ErrBadResolverState, the ClientConn
338 // will begin calling ResolveNow on the active name resolver with
339 // exponential backoff until a subsequent call to UpdateClientConnState
340 // returns a nil error. Any other errors are currently ignored.
341 UpdateClientConnState(ClientConnState) error
342 // ResolverError is called by gRPC when the name resolver reports an error.
343 ResolverError(error)
344 // UpdateSubConnState is called by gRPC when the state of a SubConn
345 // changes.
346 UpdateSubConnState(SubConn, SubConnState)
347 // Close closes the balancer. The balancer is not required to call
348 // ClientConn.RemoveSubConn for its existing SubConns.
349 Close()
350}
351
352// ExitIdler is an optional interface for balancers to implement. If
353// implemented, ExitIdle will be called when ClientConn.Connect is called, if
354// the ClientConn is idle. If unimplemented, ClientConn.Connect will cause
355// all SubConns to connect.
356//
357// Notice: it will be required for all balancers to implement this in a future
358// release.
359type ExitIdler interface {
360 // ExitIdle instructs the LB policy to reconnect to backends / exit the
361 // IDLE state, if appropriate and possible. Note that SubConns that enter
362 // the IDLE state will not reconnect until SubConn.Connect is called.
363 ExitIdle()
364}
365
366// SubConnState describes the state of a SubConn.
367type SubConnState struct {
368 // ConnectivityState is the connectivity state of the SubConn.
369 ConnectivityState connectivity.State
370 // ConnectionError is set if the ConnectivityState is TransientFailure,
371 // describing the reason the SubConn failed. Otherwise, it is nil.
372 ConnectionError error
373}
374
375// ClientConnState describes the state of a ClientConn relevant to the
376// balancer.
377type ClientConnState struct {
378 ResolverState resolver.State
379 // The parsed load balancing configuration returned by the builder's
380 // ParseConfig method, if implemented.
381 BalancerConfig serviceconfig.LoadBalancingConfig
382}
383
384// ErrBadResolverState may be returned by UpdateClientConnState to indicate a
385// problem with the provided name resolver data.
386var ErrBadResolverState = errors.New("bad resolver state")
387
Akash Kankanala761955c2024-02-21 19:32:20 +0530388// A ProducerBuilder is a simple constructor for a Producer. It is used by the
389// SubConn to create producers when needed.
390type ProducerBuilder interface {
391 // Build creates a Producer. The first parameter is always a
392 // grpc.ClientConnInterface (a type to allow creating RPCs/streams on the
393 // associated SubConn), but is declared as interface{} to avoid a
394 // dependency cycle. Should also return a close function that will be
395 // called when all references to the Producer have been given up.
396 Build(grpcClientConnInterface interface{}) (p Producer, close func())
khenaidoo5fc5cea2021-08-11 17:39:16 -0400397}
398
Akash Kankanala761955c2024-02-21 19:32:20 +0530399// A Producer is a type shared among potentially many consumers. It is
400// associated with a SubConn, and an implementation will typically contain
401// other methods to provide additional functionality, e.g. configuration or
402// subscription registration.
403type Producer interface {
khenaidoo5fc5cea2021-08-11 17:39:16 -0400404}