blob: de424cd10522f29d2e15fff379844d8ca21398b5 [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 Tiller5aceb272016-03-31 13:50:42 -070034#ifndef GRPC_CORE_EXT_CLIENT_CONFIG_LB_POLICY_H
35#define GRPC_CORE_EXT_CLIENT_CONFIG_LB_POLICY_H
Craig Tiller9d0e0472015-06-23 09:12:48 -070036
Craig Tillerd4c98332016-03-31 13:45:47 -070037#include "src/core/ext/client_config/subchannel.h"
Craig Tiller9533d042016-03-25 17:11:06 -070038#include "src/core/lib/transport/connectivity_state.h"
Craig Tiller9d0e0472015-06-23 09:12:48 -070039
40/** A load balancing policy: specified by a vtable and a struct (which
41 is expected to be extended to contain some parameters) */
42typedef struct grpc_lb_policy grpc_lb_policy;
43typedef struct grpc_lb_policy_vtable grpc_lb_policy_vtable;
44
Craig Tillera82950e2015-09-22 12:33:20 -070045typedef void (*grpc_lb_completion)(void *cb_arg, grpc_subchannel *subchannel,
46 grpc_status_code status, const char *errmsg);
Craig Tiller9d0e0472015-06-23 09:12:48 -070047
Craig Tillera82950e2015-09-22 12:33:20 -070048struct grpc_lb_policy {
Craig Tillereb3b12e2015-06-26 14:42:49 -070049 const grpc_lb_policy_vtable *vtable;
Craig Tiller48613042015-11-29 14:45:11 -080050 gpr_atm ref_pair;
Craig Tiller69b093b2016-02-25 19:04:07 -080051 /* owned pointer to interested parties in load balancing decisions */
52 grpc_pollset_set *interested_parties;
Craig Tiller7c0b4d72015-06-25 14:44:44 -070053};
54
David Garcia Quintas8aace512016-08-15 14:55:12 -070055/** Extra arguments for an LB pick */
56typedef struct grpc_lb_policy_pick_args {
David Garcia Quintas8aace512016-08-15 14:55:12 -070057 /** Initial metadata associated with the picking call. */
58 grpc_metadata_batch *initial_metadata;
David Garcia Quintas92eb6b92016-09-30 14:07:39 -070059 /** Bitmask used for selective cancelling. See \a
60 * grpc_lb_policy_cancel_picks() and \a GRPC_INITIAL_METADATA_* in
61 * grpc_types.h */
David Garcia Quintas8aace512016-08-15 14:55:12 -070062 uint32_t initial_metadata_flags;
David Garcia Quintas5b0e9462016-08-15 19:38:39 -070063 /** Storage for LB token in \a initial_metadata, or NULL if not used */
64 grpc_linked_mdelem *lb_token_mdelem_storage;
David Garcia Quintas5cf3c372016-10-03 14:30:03 -070065 /** Deadline for the call to the LB server */
David Garcia Quintas92eb6b92016-09-30 14:07:39 -070066 gpr_timespec deadline;
David Garcia Quintas8aace512016-08-15 14:55:12 -070067} grpc_lb_policy_pick_args;
68
Craig Tillera82950e2015-09-22 12:33:20 -070069struct grpc_lb_policy_vtable {
70 void (*destroy)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy);
Craig Tillera82950e2015-09-22 12:33:20 -070071 void (*shutdown)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy);
Craig Tiller9d0e0472015-06-23 09:12:48 -070072
David Garcia Quintas8aace512016-08-15 14:55:12 -070073 /** \see grpc_lb_policy_pick */
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -070074 int (*pick)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
David Garcia Quintas8aace512016-08-15 14:55:12 -070075 const grpc_lb_policy_pick_args *pick_args,
David Garcia Quintas331b9c02016-09-12 18:37:05 -070076 grpc_connected_subchannel **target, void **user_data,
77 grpc_closure *on_complete);
David Garcia Quintas8aace512016-08-15 14:55:12 -070078
79 /** \see grpc_lb_policy_cancel_pick */
Craig Tiller577c9b22015-11-02 14:11:15 -080080 void (*cancel_pick)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
Mark D. Roth5f844002016-09-08 08:20:53 -070081 grpc_connected_subchannel **target, grpc_error *error);
David Garcia Quintas8aace512016-08-15 14:55:12 -070082
83 /** \see grpc_lb_policy_cancel_picks */
Craig Tiller8c0d96f2016-03-11 14:27:52 -080084 void (*cancel_picks)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
85 uint32_t initial_metadata_flags_mask,
Mark D. Rothe65ff112016-09-09 13:48:38 -070086 uint32_t initial_metadata_flags_eq, grpc_error *error);
Craig Tillerc7b5f762015-06-27 11:48:42 -070087
David Garcia Quintas8aace512016-08-15 14:55:12 -070088 /** \see grpc_lb_policy_ping_one */
Craig Tillere2c62372015-12-07 16:11:03 -080089 void (*ping_one)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
90 grpc_closure *closure);
Craig Tiller26dab312015-12-07 14:43:47 -080091
David Garcia Quintas3fb8f732016-06-15 22:53:08 -070092 /** Try to enter a READY connectivity state */
Craig Tillera82950e2015-09-22 12:33:20 -070093 void (*exit_idle)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy);
Craig Tiller48cb07c2015-07-15 16:16:15 -070094
Craig Tillerc7b5f762015-06-27 11:48:42 -070095 /** check the current connectivity of the lb_policy */
Craig Tiller804ff712016-05-05 16:25:40 -070096 grpc_connectivity_state (*check_connectivity)(
97 grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
98 grpc_error **connectivity_error);
Craig Tillerc7b5f762015-06-27 11:48:42 -070099
100 /** call notify when the connectivity state of a channel changes from *state.
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700101 Updates *state with the new state of the policy. Calling with a NULL \a
David Garcia Quintas8aace512016-08-15 14:55:12 -0700102 state cancels the subscription. */
Craig Tillera82950e2015-09-22 12:33:20 -0700103 void (*notify_on_state_change)(grpc_exec_ctx *exec_ctx,
104 grpc_lb_policy *policy,
105 grpc_connectivity_state *state,
106 grpc_closure *closure);
Craig Tiller9d0e0472015-06-23 09:12:48 -0700107};
108
Craig Tiller54914ee2015-12-01 06:23:44 -0800109/*#define GRPC_LB_POLICY_REFCOUNT_DEBUG*/
Craig Tillerd7b68e72015-06-28 11:41:09 -0700110#ifdef GRPC_LB_POLICY_REFCOUNT_DEBUG
Craig Tiller4ab82d22015-06-29 09:40:33 -0700111#define GRPC_LB_POLICY_REF(p, r) \
112 grpc_lb_policy_ref((p), __FILE__, __LINE__, (r))
Craig Tiller8af4c332015-09-22 12:32:31 -0700113#define GRPC_LB_POLICY_UNREF(exec_ctx, p, r) \
114 grpc_lb_policy_unref((exec_ctx), (p), __FILE__, __LINE__, (r))
Craig Tiller48613042015-11-29 14:45:11 -0800115#define GRPC_LB_POLICY_WEAK_REF(p, r) \
116 grpc_lb_policy_weak_ref((p), __FILE__, __LINE__, (r))
117#define GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, p, r) \
118 grpc_lb_policy_weak_unref((exec_ctx), (p), __FILE__, __LINE__, (r))
Craig Tillera82950e2015-09-22 12:33:20 -0700119void grpc_lb_policy_ref(grpc_lb_policy *policy, const char *file, int line,
120 const char *reason);
121void grpc_lb_policy_unref(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
122 const char *file, int line, const char *reason);
Craig Tiller48613042015-11-29 14:45:11 -0800123void grpc_lb_policy_weak_ref(grpc_lb_policy *policy, const char *file, int line,
Craig Tiller1d881fb2015-12-01 07:39:04 -0800124 const char *reason);
Craig Tiller48613042015-11-29 14:45:11 -0800125void grpc_lb_policy_weak_unref(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
Craig Tiller1d881fb2015-12-01 07:39:04 -0800126 const char *file, int line, const char *reason);
Craig Tillerd7b68e72015-06-28 11:41:09 -0700127#else
128#define GRPC_LB_POLICY_REF(p, r) grpc_lb_policy_ref((p))
Craig Tiller8af4c332015-09-22 12:32:31 -0700129#define GRPC_LB_POLICY_UNREF(cl, p, r) grpc_lb_policy_unref((cl), (p))
Craig Tiller48613042015-11-29 14:45:11 -0800130#define GRPC_LB_POLICY_WEAK_REF(p, r) grpc_lb_policy_weak_ref((p))
131#define GRPC_LB_POLICY_WEAK_UNREF(cl, p, r) grpc_lb_policy_weak_unref((cl), (p))
Craig Tillera82950e2015-09-22 12:33:20 -0700132void grpc_lb_policy_ref(grpc_lb_policy *policy);
133void grpc_lb_policy_unref(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy);
Craig Tiller48613042015-11-29 14:45:11 -0800134void grpc_lb_policy_weak_ref(grpc_lb_policy *policy);
135void grpc_lb_policy_weak_unref(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy);
Craig Tillerd7b68e72015-06-28 11:41:09 -0700136#endif
137
138/** called by concrete implementations to initialize the base struct */
Craig Tillera82950e2015-09-22 12:33:20 -0700139void grpc_lb_policy_init(grpc_lb_policy *policy,
140 const grpc_lb_policy_vtable *vtable);
Craig Tillerf5f17122015-06-25 08:47:26 -0700141
Mark D. Roth1e5f6af2016-10-07 08:32:58 -0700142/** Finds an appropriate subchannel for a call, based on \a pick_args.
143
144 \a target will be set to the selected subchannel, or NULL on failure.
145 Upon success, \a user_data will be set to whatever opaque information
146 may need to be propagated from the LB policy, or NULL if not needed.
147
148 If the pick succeeds and a result is known immediately, a non-zero
149 value will be returned. Otherwise, \a on_complete will be invoked
150 once the pick is complete with its error argument set to indicate
151 success or failure.
152
Yuchen Zeng3efcb482016-10-10 15:28:50 -0700153 Any IO should be done under the \a interested_parties \a grpc_pollset_set
154 in the \a grpc_lb_policy struct. */
Craig Tiller577c9b22015-11-02 14:11:15 -0800155int grpc_lb_policy_pick(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
David Garcia Quintas8aace512016-08-15 14:55:12 -0700156 const grpc_lb_policy_pick_args *pick_args,
David Garcia Quintas331b9c02016-09-12 18:37:05 -0700157 grpc_connected_subchannel **target, void **user_data,
Craig Tillerab33b482015-11-21 08:11:04 -0800158 grpc_closure *on_complete);
Craig Tiller577c9b22015-11-02 14:11:15 -0800159
David Garcia Quintas8aace512016-08-15 14:55:12 -0700160/** Perform a connected subchannel ping (see \a grpc_connected_subchannel_ping)
161 against one of the connected subchannels managed by \a policy. */
Craig Tillere2c62372015-12-07 16:11:03 -0800162void grpc_lb_policy_ping_one(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
163 grpc_closure *closure);
Craig Tiller26dab312015-12-07 14:43:47 -0800164
David Garcia Quintas8aace512016-08-15 14:55:12 -0700165/** Cancel picks for \a target.
166 The \a on_complete callback of the pending picks will be invoked with \a
167 *target set to NULL. */
Craig Tiller577c9b22015-11-02 14:11:15 -0800168void grpc_lb_policy_cancel_pick(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
Mark D. Roth5f844002016-09-08 08:20:53 -0700169 grpc_connected_subchannel **target,
170 grpc_error *error);
Craig Tillerca3e9d32015-06-27 18:37:27 -0700171
David Garcia Quintas8aace512016-08-15 14:55:12 -0700172/** Cancel all pending picks for which their \a initial_metadata_flags (as given
173 in the call to \a grpc_lb_policy_pick) matches \a initial_metadata_flags_eq
174 when AND'd with \a initial_metadata_flags_mask */
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800175void grpc_lb_policy_cancel_picks(grpc_exec_ctx *exec_ctx,
176 grpc_lb_policy *policy,
177 uint32_t initial_metadata_flags_mask,
Mark D. Rothe65ff112016-09-09 13:48:38 -0700178 uint32_t initial_metadata_flags_eq,
179 grpc_error *error);
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800180
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700181/** Try to enter a READY connectivity state */
Craig Tillera82950e2015-09-22 12:33:20 -0700182void grpc_lb_policy_exit_idle(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700183
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700184/* Call notify when the connectivity state of a channel changes from \a *state.
185 * Updates \a *state with the new state of the policy */
Craig Tillera82950e2015-09-22 12:33:20 -0700186void grpc_lb_policy_notify_on_state_change(grpc_exec_ctx *exec_ctx,
187 grpc_lb_policy *policy,
188 grpc_connectivity_state *state,
189 grpc_closure *closure);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700190
Craig Tillera82950e2015-09-22 12:33:20 -0700191grpc_connectivity_state grpc_lb_policy_check_connectivity(
Craig Tiller804ff712016-05-05 16:25:40 -0700192 grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
193 grpc_error **connectivity_error);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700194
Craig Tiller5aceb272016-03-31 13:50:42 -0700195#endif /* GRPC_CORE_EXT_CLIENT_CONFIG_LB_POLICY_H */