blob: 07e795300951dfdc884cc24ff695722ade132b04 [file] [log] [blame]
Craig Tiller9d0e0472015-06-23 09:12:48 -07001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Craig Tiller9d0e0472015-06-23 09:12:48 -07004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
Craig Tillerd608cd62017-04-02 16:23:17 -070034#ifndef GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_LB_POLICY_H
35#define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_LB_POLICY_H
Craig Tiller9d0e0472015-06-23 09:12:48 -070036
Craig Tiller9eb0fde2017-03-31 16:59:30 -070037#include "src/core/ext/filters/client_channel/subchannel.h"
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -070038#include "src/core/lib/iomgr/polling_entity.h"
Craig Tiller9533d042016-03-25 17:11:06 -070039#include "src/core/lib/transport/connectivity_state.h"
Craig Tiller9d0e0472015-06-23 09:12:48 -070040
41/** A load balancing policy: specified by a vtable and a struct (which
42 is expected to be extended to contain some parameters) */
43typedef struct grpc_lb_policy grpc_lb_policy;
44typedef struct grpc_lb_policy_vtable grpc_lb_policy_vtable;
David Garcia Quintas87d5a312017-06-06 19:45:58 -070045typedef struct grpc_lb_policy_args grpc_lb_policy_args;
Craig Tiller9d0e0472015-06-23 09:12:48 -070046
Craig Tillera82950e2015-09-22 12:33:20 -070047struct grpc_lb_policy {
Craig Tillereb3b12e2015-06-26 14:42:49 -070048 const grpc_lb_policy_vtable *vtable;
Craig Tiller48613042015-11-29 14:45:11 -080049 gpr_atm ref_pair;
Craig Tiller69b093b2016-02-25 19:04:07 -080050 /* owned pointer to interested parties in load balancing decisions */
51 grpc_pollset_set *interested_parties;
Craig Tiller2400bf52017-02-09 16:25:19 -080052 /* combiner under which lb_policy actions take place */
53 grpc_combiner *combiner;
Craig Tiller7c0b4d72015-06-25 14:44:44 -070054};
55
David Garcia Quintas8aace512016-08-15 14:55:12 -070056/** Extra arguments for an LB pick */
57typedef struct grpc_lb_policy_pick_args {
David Garcia Quintas8aace512016-08-15 14:55:12 -070058 /** Initial metadata associated with the picking call. */
59 grpc_metadata_batch *initial_metadata;
David Garcia Quintas92eb6b92016-09-30 14:07:39 -070060 /** Bitmask used for selective cancelling. See \a
61 * grpc_lb_policy_cancel_picks() and \a GRPC_INITIAL_METADATA_* in
62 * grpc_types.h */
David Garcia Quintas8aace512016-08-15 14:55:12 -070063 uint32_t initial_metadata_flags;
David Garcia Quintas5b0e9462016-08-15 19:38:39 -070064 /** Storage for LB token in \a initial_metadata, or NULL if not used */
65 grpc_linked_mdelem *lb_token_mdelem_storage;
David Garcia Quintas8aace512016-08-15 14:55:12 -070066} grpc_lb_policy_pick_args;
67
Craig Tillera82950e2015-09-22 12:33:20 -070068struct grpc_lb_policy_vtable {
69 void (*destroy)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy);
Craig Tiller2400bf52017-02-09 16:25:19 -080070 void (*shutdown_locked)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy);
Craig Tiller9d0e0472015-06-23 09:12:48 -070071
David Garcia Quintas8aace512016-08-15 14:55:12 -070072 /** \see grpc_lb_policy_pick */
Craig Tiller2400bf52017-02-09 16:25:19 -080073 int (*pick_locked)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
74 const grpc_lb_policy_pick_args *pick_args,
Mark D. Roth09e458c2017-05-02 08:13:26 -070075 grpc_connected_subchannel **target,
76 grpc_call_context_element *context, void **user_data,
Craig Tiller2400bf52017-02-09 16:25:19 -080077 grpc_closure *on_complete);
David Garcia Quintas8aace512016-08-15 14:55:12 -070078
79 /** \see grpc_lb_policy_cancel_pick */
Craig Tiller2400bf52017-02-09 16:25:19 -080080 void (*cancel_pick_locked)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
81 grpc_connected_subchannel **target,
82 grpc_error *error);
David Garcia Quintas8aace512016-08-15 14:55:12 -070083
84 /** \see grpc_lb_policy_cancel_picks */
Craig Tiller2400bf52017-02-09 16:25:19 -080085 void (*cancel_picks_locked)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
86 uint32_t initial_metadata_flags_mask,
87 uint32_t initial_metadata_flags_eq,
88 grpc_error *error);
Craig Tillerc7b5f762015-06-27 11:48:42 -070089
David Garcia Quintas8aace512016-08-15 14:55:12 -070090 /** \see grpc_lb_policy_ping_one */
Craig Tiller2400bf52017-02-09 16:25:19 -080091 void (*ping_one_locked)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
92 grpc_closure *closure);
Craig Tiller26dab312015-12-07 14:43:47 -080093
David Garcia Quintas3fb8f732016-06-15 22:53:08 -070094 /** Try to enter a READY connectivity state */
Craig Tiller2400bf52017-02-09 16:25:19 -080095 void (*exit_idle_locked)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy);
Craig Tiller48cb07c2015-07-15 16:16:15 -070096
Craig Tillerc7b5f762015-06-27 11:48:42 -070097 /** check the current connectivity of the lb_policy */
Craig Tiller2400bf52017-02-09 16:25:19 -080098 grpc_connectivity_state (*check_connectivity_locked)(
Craig Tiller804ff712016-05-05 16:25:40 -070099 grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
100 grpc_error **connectivity_error);
Craig Tillerc7b5f762015-06-27 11:48:42 -0700101
102 /** call notify when the connectivity state of a channel changes from *state.
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700103 Updates *state with the new state of the policy. Calling with a NULL \a
David Garcia Quintas8aace512016-08-15 14:55:12 -0700104 state cancels the subscription. */
Craig Tiller2400bf52017-02-09 16:25:19 -0800105 void (*notify_on_state_change_locked)(grpc_exec_ctx *exec_ctx,
106 grpc_lb_policy *policy,
107 grpc_connectivity_state *state,
108 grpc_closure *closure);
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700109
110 void (*update_locked)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
111 const grpc_lb_policy_args *args);
Craig Tiller9d0e0472015-06-23 09:12:48 -0700112};
113
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700114//#define GRPC_LB_POLICY_REFCOUNT_DEBUG
Craig Tillerd7b68e72015-06-28 11:41:09 -0700115#ifdef GRPC_LB_POLICY_REFCOUNT_DEBUG
David Garcia Quintase224a762016-11-01 13:00:58 -0700116
117/* Strong references: the policy will shutdown when they reach zero */
Craig Tiller4ab82d22015-06-29 09:40:33 -0700118#define GRPC_LB_POLICY_REF(p, r) \
119 grpc_lb_policy_ref((p), __FILE__, __LINE__, (r))
Craig Tiller8af4c332015-09-22 12:32:31 -0700120#define GRPC_LB_POLICY_UNREF(exec_ctx, p, r) \
121 grpc_lb_policy_unref((exec_ctx), (p), __FILE__, __LINE__, (r))
David Garcia Quintase224a762016-11-01 13:00:58 -0700122
123/* Weak references: they don't prevent the shutdown of the LB policy. When no
124 * strong references are left but there are still weak ones, shutdown is called.
125 * Once the weak reference also reaches zero, the LB policy is destroyed. */
Craig Tiller48613042015-11-29 14:45:11 -0800126#define GRPC_LB_POLICY_WEAK_REF(p, r) \
127 grpc_lb_policy_weak_ref((p), __FILE__, __LINE__, (r))
128#define GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, p, r) \
129 grpc_lb_policy_weak_unref((exec_ctx), (p), __FILE__, __LINE__, (r))
Craig Tillera82950e2015-09-22 12:33:20 -0700130void grpc_lb_policy_ref(grpc_lb_policy *policy, const char *file, int line,
131 const char *reason);
132void grpc_lb_policy_unref(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
133 const char *file, int line, const char *reason);
Craig Tiller48613042015-11-29 14:45:11 -0800134void grpc_lb_policy_weak_ref(grpc_lb_policy *policy, const char *file, int line,
Craig Tiller1d881fb2015-12-01 07:39:04 -0800135 const char *reason);
Craig Tiller48613042015-11-29 14:45:11 -0800136void grpc_lb_policy_weak_unref(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
Craig Tiller1d881fb2015-12-01 07:39:04 -0800137 const char *file, int line, const char *reason);
Craig Tillerd7b68e72015-06-28 11:41:09 -0700138#else
139#define GRPC_LB_POLICY_REF(p, r) grpc_lb_policy_ref((p))
Craig Tiller8af4c332015-09-22 12:32:31 -0700140#define GRPC_LB_POLICY_UNREF(cl, p, r) grpc_lb_policy_unref((cl), (p))
Craig Tiller48613042015-11-29 14:45:11 -0800141#define GRPC_LB_POLICY_WEAK_REF(p, r) grpc_lb_policy_weak_ref((p))
142#define GRPC_LB_POLICY_WEAK_UNREF(cl, p, r) grpc_lb_policy_weak_unref((cl), (p))
Craig Tillera82950e2015-09-22 12:33:20 -0700143void grpc_lb_policy_ref(grpc_lb_policy *policy);
144void grpc_lb_policy_unref(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy);
Craig Tiller48613042015-11-29 14:45:11 -0800145void grpc_lb_policy_weak_ref(grpc_lb_policy *policy);
146void grpc_lb_policy_weak_unref(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy);
Craig Tillerd7b68e72015-06-28 11:41:09 -0700147#endif
148
149/** called by concrete implementations to initialize the base struct */
Craig Tillera82950e2015-09-22 12:33:20 -0700150void grpc_lb_policy_init(grpc_lb_policy *policy,
Craig Tiller2400bf52017-02-09 16:25:19 -0800151 const grpc_lb_policy_vtable *vtable,
152 grpc_combiner *combiner);
Craig Tillerf5f17122015-06-25 08:47:26 -0700153
Mark D. Roth1e5f6af2016-10-07 08:32:58 -0700154/** Finds an appropriate subchannel for a call, based on \a pick_args.
155
Mark D. Rothd7389b42017-05-17 12:22:17 -0700156 \a target will be set to the selected subchannel, or NULL on failure
157 or when the LB policy decides to drop the call.
158
Mark D. Roth1e5f6af2016-10-07 08:32:58 -0700159 Upon success, \a user_data will be set to whatever opaque information
160 may need to be propagated from the LB policy, or NULL if not needed.
Mark D. Roth09e458c2017-05-02 08:13:26 -0700161 \a context will be populated with context to pass to the subchannel
162 call, if needed.
Mark D. Roth1e5f6af2016-10-07 08:32:58 -0700163
164 If the pick succeeds and a result is known immediately, a non-zero
165 value will be returned. Otherwise, \a on_complete will be invoked
166 once the pick is complete with its error argument set to indicate
167 success or failure.
168
Yuchen Zeng3efcb482016-10-10 15:28:50 -0700169 Any IO should be done under the \a interested_parties \a grpc_pollset_set
170 in the \a grpc_lb_policy struct. */
Craig Tiller2400bf52017-02-09 16:25:19 -0800171int grpc_lb_policy_pick_locked(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
172 const grpc_lb_policy_pick_args *pick_args,
173 grpc_connected_subchannel **target,
Mark D. Roth09e458c2017-05-02 08:13:26 -0700174 grpc_call_context_element *context,
Craig Tiller2400bf52017-02-09 16:25:19 -0800175 void **user_data, grpc_closure *on_complete);
Craig Tiller577c9b22015-11-02 14:11:15 -0800176
David Garcia Quintas8aace512016-08-15 14:55:12 -0700177/** Perform a connected subchannel ping (see \a grpc_connected_subchannel_ping)
178 against one of the connected subchannels managed by \a policy. */
Craig Tiller2400bf52017-02-09 16:25:19 -0800179void grpc_lb_policy_ping_one_locked(grpc_exec_ctx *exec_ctx,
180 grpc_lb_policy *policy,
181 grpc_closure *closure);
Craig Tiller26dab312015-12-07 14:43:47 -0800182
David Garcia Quintas8aace512016-08-15 14:55:12 -0700183/** Cancel picks for \a target.
184 The \a on_complete callback of the pending picks will be invoked with \a
185 *target set to NULL. */
Craig Tiller2400bf52017-02-09 16:25:19 -0800186void grpc_lb_policy_cancel_pick_locked(grpc_exec_ctx *exec_ctx,
187 grpc_lb_policy *policy,
188 grpc_connected_subchannel **target,
189 grpc_error *error);
Craig Tillerca3e9d32015-06-27 18:37:27 -0700190
David Garcia Quintas8aace512016-08-15 14:55:12 -0700191/** Cancel all pending picks for which their \a initial_metadata_flags (as given
192 in the call to \a grpc_lb_policy_pick) matches \a initial_metadata_flags_eq
193 when AND'd with \a initial_metadata_flags_mask */
Craig Tiller2400bf52017-02-09 16:25:19 -0800194void grpc_lb_policy_cancel_picks_locked(grpc_exec_ctx *exec_ctx,
195 grpc_lb_policy *policy,
196 uint32_t initial_metadata_flags_mask,
197 uint32_t initial_metadata_flags_eq,
198 grpc_error *error);
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800199
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700200/** Try to enter a READY connectivity state */
Craig Tiller2400bf52017-02-09 16:25:19 -0800201void grpc_lb_policy_exit_idle_locked(grpc_exec_ctx *exec_ctx,
202 grpc_lb_policy *policy);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700203
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700204/* Call notify when the connectivity state of a channel changes from \a *state.
205 * Updates \a *state with the new state of the policy */
Craig Tiller2400bf52017-02-09 16:25:19 -0800206void grpc_lb_policy_notify_on_state_change_locked(
207 grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
208 grpc_connectivity_state *state, grpc_closure *closure);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700209
Craig Tiller2400bf52017-02-09 16:25:19 -0800210grpc_connectivity_state grpc_lb_policy_check_connectivity_locked(
Craig Tiller804ff712016-05-05 16:25:40 -0700211 grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
212 grpc_error **connectivity_error);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700213
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700214/** Update \a policy with \a lb_policy_args. */
215void grpc_lb_policy_update_locked(grpc_exec_ctx *exec_ctx,
216 grpc_lb_policy *policy,
217 const grpc_lb_policy_args *lb_policy_args);
218
Craig Tillerd608cd62017-04-02 16:23:17 -0700219#endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_LB_POLICY_H */