blob: cd40b4dcf72cbf26b632fc3af336914a0a22d026 [file] [log] [blame]
Craig Tiller9d0e0472015-06-23 09:12:48 -07001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Craig Tiller9d0e0472015-06-23 09:12:48 -07004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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
Craig Tiller9d0e0472015-06-23 09:12:48 -07008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Craig Tiller9d0e0472015-06-23 09:12:48 -070010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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.
Craig Tiller9d0e0472015-06-23 09:12:48 -070016 *
17 */
18
Craig Tillerd608cd62017-04-02 16:23:17 -070019#ifndef GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_LB_POLICY_H
20#define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_LB_POLICY_H
Craig Tiller9d0e0472015-06-23 09:12:48 -070021
Craig Tiller9eb0fde2017-03-31 16:59:30 -070022#include "src/core/ext/filters/client_channel/subchannel.h"
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -070023#include "src/core/lib/iomgr/polling_entity.h"
Craig Tiller9533d042016-03-25 17:11:06 -070024#include "src/core/lib/transport/connectivity_state.h"
Craig Tiller9d0e0472015-06-23 09:12:48 -070025
Yash Tibrewala7e6d652017-09-20 18:56:37 -070026#ifdef __cplusplus
27extern "C" {
28#endif
29
Craig Tiller9d0e0472015-06-23 09:12:48 -070030/** A load balancing policy: specified by a vtable and a struct (which
31 is expected to be extended to contain some parameters) */
32typedef struct grpc_lb_policy grpc_lb_policy;
33typedef struct grpc_lb_policy_vtable grpc_lb_policy_vtable;
David Garcia Quintas87d5a312017-06-06 19:45:58 -070034typedef struct grpc_lb_policy_args grpc_lb_policy_args;
Craig Tiller9d0e0472015-06-23 09:12:48 -070035
ncteisen05294b62017-11-10 16:57:53 -080036extern grpc_core::DebugOnlyTraceFlag grpc_trace_lb_policy_refcount;
ncteisen9c43fc02017-06-08 16:06:23 -070037
Craig Tillera82950e2015-09-22 12:33:20 -070038struct grpc_lb_policy {
Craig Tillerbaa14a92017-11-03 09:09:36 -070039 const grpc_lb_policy_vtable* vtable;
Craig Tiller48613042015-11-29 14:45:11 -080040 gpr_atm ref_pair;
Craig Tiller69b093b2016-02-25 19:04:07 -080041 /* owned pointer to interested parties in load balancing decisions */
Craig Tillerbaa14a92017-11-03 09:09:36 -070042 grpc_pollset_set* interested_parties;
Craig Tiller2400bf52017-02-09 16:25:19 -080043 /* combiner under which lb_policy actions take place */
Craig Tillerbaa14a92017-11-03 09:09:36 -070044 grpc_combiner* combiner;
Craig Tiller7c0b4d72015-06-25 14:44:44 -070045};
46
David Garcia Quintas8aace512016-08-15 14:55:12 -070047/** Extra arguments for an LB pick */
48typedef struct grpc_lb_policy_pick_args {
David Garcia Quintas8aace512016-08-15 14:55:12 -070049 /** Initial metadata associated with the picking call. */
Craig Tillerbaa14a92017-11-03 09:09:36 -070050 grpc_metadata_batch* initial_metadata;
David Garcia Quintas92eb6b92016-09-30 14:07:39 -070051 /** Bitmask used for selective cancelling. See \a
52 * grpc_lb_policy_cancel_picks() and \a GRPC_INITIAL_METADATA_* in
53 * grpc_types.h */
David Garcia Quintas8aace512016-08-15 14:55:12 -070054 uint32_t initial_metadata_flags;
David Garcia Quintas5b0e9462016-08-15 19:38:39 -070055 /** Storage for LB token in \a initial_metadata, or NULL if not used */
Craig Tillerbaa14a92017-11-03 09:09:36 -070056 grpc_linked_mdelem* lb_token_mdelem_storage;
David Garcia Quintas8aace512016-08-15 14:55:12 -070057} grpc_lb_policy_pick_args;
58
Craig Tillera82950e2015-09-22 12:33:20 -070059struct grpc_lb_policy_vtable {
Craig Tillerbaa14a92017-11-03 09:09:36 -070060 void (*destroy)(grpc_exec_ctx* exec_ctx, grpc_lb_policy* policy);
61 void (*shutdown_locked)(grpc_exec_ctx* exec_ctx, grpc_lb_policy* policy);
Craig Tiller9d0e0472015-06-23 09:12:48 -070062
David Garcia Quintas8aace512016-08-15 14:55:12 -070063 /** \see grpc_lb_policy_pick */
Craig Tillerbaa14a92017-11-03 09:09:36 -070064 int (*pick_locked)(grpc_exec_ctx* exec_ctx, grpc_lb_policy* policy,
65 const grpc_lb_policy_pick_args* pick_args,
66 grpc_connected_subchannel** target,
67 grpc_call_context_element* context, void** user_data,
68 grpc_closure* on_complete);
David Garcia Quintas8aace512016-08-15 14:55:12 -070069
70 /** \see grpc_lb_policy_cancel_pick */
Craig Tillerbaa14a92017-11-03 09:09:36 -070071 void (*cancel_pick_locked)(grpc_exec_ctx* exec_ctx, grpc_lb_policy* policy,
72 grpc_connected_subchannel** target,
73 grpc_error* error);
David Garcia Quintas8aace512016-08-15 14:55:12 -070074
75 /** \see grpc_lb_policy_cancel_picks */
Craig Tillerbaa14a92017-11-03 09:09:36 -070076 void (*cancel_picks_locked)(grpc_exec_ctx* exec_ctx, grpc_lb_policy* policy,
Craig Tiller2400bf52017-02-09 16:25:19 -080077 uint32_t initial_metadata_flags_mask,
78 uint32_t initial_metadata_flags_eq,
Craig Tillerbaa14a92017-11-03 09:09:36 -070079 grpc_error* error);
Craig Tillerc7b5f762015-06-27 11:48:42 -070080
David Garcia Quintas8aace512016-08-15 14:55:12 -070081 /** \see grpc_lb_policy_ping_one */
Craig Tillerbaa14a92017-11-03 09:09:36 -070082 void (*ping_one_locked)(grpc_exec_ctx* exec_ctx, grpc_lb_policy* policy,
83 grpc_closure* closure);
Craig Tiller26dab312015-12-07 14:43:47 -080084
David Garcia Quintas3fb8f732016-06-15 22:53:08 -070085 /** Try to enter a READY connectivity state */
Craig Tillerbaa14a92017-11-03 09:09:36 -070086 void (*exit_idle_locked)(grpc_exec_ctx* exec_ctx, grpc_lb_policy* policy);
Craig Tiller48cb07c2015-07-15 16:16:15 -070087
Craig Tillerc7b5f762015-06-27 11:48:42 -070088 /** check the current connectivity of the lb_policy */
Craig Tiller2400bf52017-02-09 16:25:19 -080089 grpc_connectivity_state (*check_connectivity_locked)(
Craig Tillerbaa14a92017-11-03 09:09:36 -070090 grpc_exec_ctx* exec_ctx, grpc_lb_policy* policy,
91 grpc_error** connectivity_error);
Craig Tillerc7b5f762015-06-27 11:48:42 -070092
93 /** call notify when the connectivity state of a channel changes from *state.
David Garcia Quintas3fb8f732016-06-15 22:53:08 -070094 Updates *state with the new state of the policy. Calling with a NULL \a
David Garcia Quintas8aace512016-08-15 14:55:12 -070095 state cancels the subscription. */
Craig Tillerbaa14a92017-11-03 09:09:36 -070096 void (*notify_on_state_change_locked)(grpc_exec_ctx* exec_ctx,
97 grpc_lb_policy* policy,
98 grpc_connectivity_state* state,
99 grpc_closure* closure);
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700100
Craig Tillerbaa14a92017-11-03 09:09:36 -0700101 void (*update_locked)(grpc_exec_ctx* exec_ctx, grpc_lb_policy* policy,
102 const grpc_lb_policy_args* args);
Craig Tiller9d0e0472015-06-23 09:12:48 -0700103};
104
ncteisen9c43fc02017-06-08 16:06:23 -0700105#ifndef NDEBUG
David Garcia Quintase224a762016-11-01 13:00:58 -0700106
107/* Strong references: the policy will shutdown when they reach zero */
Craig Tiller4ab82d22015-06-29 09:40:33 -0700108#define GRPC_LB_POLICY_REF(p, r) \
109 grpc_lb_policy_ref((p), __FILE__, __LINE__, (r))
Craig Tiller8af4c332015-09-22 12:32:31 -0700110#define GRPC_LB_POLICY_UNREF(exec_ctx, p, r) \
111 grpc_lb_policy_unref((exec_ctx), (p), __FILE__, __LINE__, (r))
David Garcia Quintase224a762016-11-01 13:00:58 -0700112
113/* Weak references: they don't prevent the shutdown of the LB policy. When no
114 * strong references are left but there are still weak ones, shutdown is called.
115 * Once the weak reference also reaches zero, the LB policy is destroyed. */
Craig Tiller48613042015-11-29 14:45:11 -0800116#define GRPC_LB_POLICY_WEAK_REF(p, r) \
117 grpc_lb_policy_weak_ref((p), __FILE__, __LINE__, (r))
118#define GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, p, r) \
119 grpc_lb_policy_weak_unref((exec_ctx), (p), __FILE__, __LINE__, (r))
Craig Tillerbaa14a92017-11-03 09:09:36 -0700120void grpc_lb_policy_ref(grpc_lb_policy* policy, const char* file, int line,
121 const char* reason);
122void grpc_lb_policy_unref(grpc_exec_ctx* exec_ctx, grpc_lb_policy* policy,
123 const char* file, int line, const char* reason);
124void grpc_lb_policy_weak_ref(grpc_lb_policy* policy, const char* file, int line,
125 const char* reason);
126void grpc_lb_policy_weak_unref(grpc_exec_ctx* exec_ctx, grpc_lb_policy* policy,
127 const char* file, int line, const char* reason);
Craig Tillerd7b68e72015-06-28 11:41:09 -0700128#else
129#define GRPC_LB_POLICY_REF(p, r) grpc_lb_policy_ref((p))
Craig Tiller8af4c332015-09-22 12:32:31 -0700130#define GRPC_LB_POLICY_UNREF(cl, p, r) grpc_lb_policy_unref((cl), (p))
Craig Tiller48613042015-11-29 14:45:11 -0800131#define GRPC_LB_POLICY_WEAK_REF(p, r) grpc_lb_policy_weak_ref((p))
132#define GRPC_LB_POLICY_WEAK_UNREF(cl, p, r) grpc_lb_policy_weak_unref((cl), (p))
Craig Tillerbaa14a92017-11-03 09:09:36 -0700133void grpc_lb_policy_ref(grpc_lb_policy* policy);
134void grpc_lb_policy_unref(grpc_exec_ctx* exec_ctx, grpc_lb_policy* policy);
135void grpc_lb_policy_weak_ref(grpc_lb_policy* policy);
136void grpc_lb_policy_weak_unref(grpc_exec_ctx* exec_ctx, grpc_lb_policy* policy);
Craig Tillerd7b68e72015-06-28 11:41:09 -0700137#endif
138
139/** called by concrete implementations to initialize the base struct */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700140void grpc_lb_policy_init(grpc_lb_policy* policy,
141 const grpc_lb_policy_vtable* vtable,
142 grpc_combiner* combiner);
Craig Tillerf5f17122015-06-25 08:47:26 -0700143
Mark D. Roth1e5f6af2016-10-07 08:32:58 -0700144/** Finds an appropriate subchannel for a call, based on \a pick_args.
145
Mark D. Rothd7389b42017-05-17 12:22:17 -0700146 \a target will be set to the selected subchannel, or NULL on failure
147 or when the LB policy decides to drop the call.
148
Mark D. Roth1e5f6af2016-10-07 08:32:58 -0700149 Upon success, \a user_data will be set to whatever opaque information
150 may need to be propagated from the LB policy, or NULL if not needed.
Mark D. Roth09e458c2017-05-02 08:13:26 -0700151 \a context will be populated with context to pass to the subchannel
152 call, if needed.
Mark D. Roth1e5f6af2016-10-07 08:32:58 -0700153
154 If the pick succeeds and a result is known immediately, a non-zero
155 value will be returned. Otherwise, \a on_complete will be invoked
156 once the pick is complete with its error argument set to indicate
157 success or failure.
158
Yuchen Zeng3efcb482016-10-10 15:28:50 -0700159 Any IO should be done under the \a interested_parties \a grpc_pollset_set
160 in the \a grpc_lb_policy struct. */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700161int grpc_lb_policy_pick_locked(grpc_exec_ctx* exec_ctx, grpc_lb_policy* policy,
162 const grpc_lb_policy_pick_args* pick_args,
163 grpc_connected_subchannel** target,
164 grpc_call_context_element* context,
165 void** user_data, grpc_closure* on_complete);
Craig Tiller577c9b22015-11-02 14:11:15 -0800166
David Garcia Quintas8aace512016-08-15 14:55:12 -0700167/** Perform a connected subchannel ping (see \a grpc_connected_subchannel_ping)
168 against one of the connected subchannels managed by \a policy. */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700169void grpc_lb_policy_ping_one_locked(grpc_exec_ctx* exec_ctx,
170 grpc_lb_policy* policy,
171 grpc_closure* closure);
Craig Tiller26dab312015-12-07 14:43:47 -0800172
David Garcia Quintas8aace512016-08-15 14:55:12 -0700173/** Cancel picks for \a target.
174 The \a on_complete callback of the pending picks will be invoked with \a
175 *target set to NULL. */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700176void grpc_lb_policy_cancel_pick_locked(grpc_exec_ctx* exec_ctx,
177 grpc_lb_policy* policy,
178 grpc_connected_subchannel** target,
179 grpc_error* error);
Craig Tillerca3e9d32015-06-27 18:37:27 -0700180
David Garcia Quintas8aace512016-08-15 14:55:12 -0700181/** Cancel all pending picks for which their \a initial_metadata_flags (as given
182 in the call to \a grpc_lb_policy_pick) matches \a initial_metadata_flags_eq
183 when AND'd with \a initial_metadata_flags_mask */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700184void grpc_lb_policy_cancel_picks_locked(grpc_exec_ctx* exec_ctx,
185 grpc_lb_policy* policy,
Craig Tiller2400bf52017-02-09 16:25:19 -0800186 uint32_t initial_metadata_flags_mask,
187 uint32_t initial_metadata_flags_eq,
Craig Tillerbaa14a92017-11-03 09:09:36 -0700188 grpc_error* error);
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800189
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700190/** Try to enter a READY connectivity state */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700191void grpc_lb_policy_exit_idle_locked(grpc_exec_ctx* exec_ctx,
192 grpc_lb_policy* policy);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700193
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700194/* Call notify when the connectivity state of a channel changes from \a *state.
195 * Updates \a *state with the new state of the policy */
Craig Tiller2400bf52017-02-09 16:25:19 -0800196void grpc_lb_policy_notify_on_state_change_locked(
Craig Tillerbaa14a92017-11-03 09:09:36 -0700197 grpc_exec_ctx* exec_ctx, grpc_lb_policy* policy,
198 grpc_connectivity_state* state, grpc_closure* closure);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700199
Craig Tiller2400bf52017-02-09 16:25:19 -0800200grpc_connectivity_state grpc_lb_policy_check_connectivity_locked(
Craig Tillerbaa14a92017-11-03 09:09:36 -0700201 grpc_exec_ctx* exec_ctx, grpc_lb_policy* policy,
202 grpc_error** connectivity_error);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700203
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700204/** Update \a policy with \a lb_policy_args. */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700205void grpc_lb_policy_update_locked(grpc_exec_ctx* exec_ctx,
206 grpc_lb_policy* policy,
207 const grpc_lb_policy_args* lb_policy_args);
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700208
Yash Tibrewala7e6d652017-09-20 18:56:37 -0700209#ifdef __cplusplus
210}
211#endif
212
Craig Tillerd608cd62017-04-02 16:23:17 -0700213#endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_LB_POLICY_H */