Craig Tiller | 9d0e047 | 2015-06-23 09:12:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame^] | 3 | * Copyright 2015 gRPC authors. |
Craig Tiller | 9d0e047 | 2015-06-23 09:12:48 -0700 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame^] | 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 |
Craig Tiller | 9d0e047 | 2015-06-23 09:12:48 -0700 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame^] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Craig Tiller | 9d0e047 | 2015-06-23 09:12:48 -0700 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame^] | 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. |
Craig Tiller | 9d0e047 | 2015-06-23 09:12:48 -0700 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
Craig Tiller | d608cd6 | 2017-04-02 16:23:17 -0700 | [diff] [blame] | 19 | #ifndef GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_LB_POLICY_H |
| 20 | #define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_LB_POLICY_H |
Craig Tiller | 9d0e047 | 2015-06-23 09:12:48 -0700 | [diff] [blame] | 21 | |
Craig Tiller | 9eb0fde | 2017-03-31 16:59:30 -0700 | [diff] [blame] | 22 | #include "src/core/ext/filters/client_channel/subchannel.h" |
David Garcia Quintas | 2a50dfe | 2016-05-31 15:09:12 -0700 | [diff] [blame] | 23 | #include "src/core/lib/iomgr/polling_entity.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 24 | #include "src/core/lib/transport/connectivity_state.h" |
Craig Tiller | 9d0e047 | 2015-06-23 09:12:48 -0700 | [diff] [blame] | 25 | |
| 26 | /** A load balancing policy: specified by a vtable and a struct (which |
| 27 | is expected to be extended to contain some parameters) */ |
| 28 | typedef struct grpc_lb_policy grpc_lb_policy; |
| 29 | typedef struct grpc_lb_policy_vtable grpc_lb_policy_vtable; |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 30 | typedef struct grpc_lb_policy_args grpc_lb_policy_args; |
Craig Tiller | 9d0e047 | 2015-06-23 09:12:48 -0700 | [diff] [blame] | 31 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 32 | struct grpc_lb_policy { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 33 | const grpc_lb_policy_vtable *vtable; |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 34 | gpr_atm ref_pair; |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 35 | /* owned pointer to interested parties in load balancing decisions */ |
| 36 | grpc_pollset_set *interested_parties; |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 37 | /* combiner under which lb_policy actions take place */ |
| 38 | grpc_combiner *combiner; |
Craig Tiller | 7c0b4d7 | 2015-06-25 14:44:44 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
David Garcia Quintas | 8aace51 | 2016-08-15 14:55:12 -0700 | [diff] [blame] | 41 | /** Extra arguments for an LB pick */ |
| 42 | typedef struct grpc_lb_policy_pick_args { |
David Garcia Quintas | 8aace51 | 2016-08-15 14:55:12 -0700 | [diff] [blame] | 43 | /** Initial metadata associated with the picking call. */ |
| 44 | grpc_metadata_batch *initial_metadata; |
David Garcia Quintas | 92eb6b9 | 2016-09-30 14:07:39 -0700 | [diff] [blame] | 45 | /** Bitmask used for selective cancelling. See \a |
| 46 | * grpc_lb_policy_cancel_picks() and \a GRPC_INITIAL_METADATA_* in |
| 47 | * grpc_types.h */ |
David Garcia Quintas | 8aace51 | 2016-08-15 14:55:12 -0700 | [diff] [blame] | 48 | uint32_t initial_metadata_flags; |
David Garcia Quintas | 5b0e946 | 2016-08-15 19:38:39 -0700 | [diff] [blame] | 49 | /** Storage for LB token in \a initial_metadata, or NULL if not used */ |
| 50 | grpc_linked_mdelem *lb_token_mdelem_storage; |
David Garcia Quintas | 8aace51 | 2016-08-15 14:55:12 -0700 | [diff] [blame] | 51 | } grpc_lb_policy_pick_args; |
| 52 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 53 | struct grpc_lb_policy_vtable { |
| 54 | void (*destroy)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy); |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 55 | void (*shutdown_locked)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy); |
Craig Tiller | 9d0e047 | 2015-06-23 09:12:48 -0700 | [diff] [blame] | 56 | |
David Garcia Quintas | 8aace51 | 2016-08-15 14:55:12 -0700 | [diff] [blame] | 57 | /** \see grpc_lb_policy_pick */ |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 58 | int (*pick_locked)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy, |
| 59 | const grpc_lb_policy_pick_args *pick_args, |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 60 | grpc_connected_subchannel **target, |
| 61 | grpc_call_context_element *context, void **user_data, |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 62 | grpc_closure *on_complete); |
David Garcia Quintas | 8aace51 | 2016-08-15 14:55:12 -0700 | [diff] [blame] | 63 | |
| 64 | /** \see grpc_lb_policy_cancel_pick */ |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 65 | void (*cancel_pick_locked)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy, |
| 66 | grpc_connected_subchannel **target, |
| 67 | grpc_error *error); |
David Garcia Quintas | 8aace51 | 2016-08-15 14:55:12 -0700 | [diff] [blame] | 68 | |
| 69 | /** \see grpc_lb_policy_cancel_picks */ |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 70 | void (*cancel_picks_locked)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy, |
| 71 | uint32_t initial_metadata_flags_mask, |
| 72 | uint32_t initial_metadata_flags_eq, |
| 73 | grpc_error *error); |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 74 | |
David Garcia Quintas | 8aace51 | 2016-08-15 14:55:12 -0700 | [diff] [blame] | 75 | /** \see grpc_lb_policy_ping_one */ |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 76 | void (*ping_one_locked)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy, |
| 77 | grpc_closure *closure); |
Craig Tiller | 26dab31 | 2015-12-07 14:43:47 -0800 | [diff] [blame] | 78 | |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 79 | /** Try to enter a READY connectivity state */ |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 80 | void (*exit_idle_locked)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 81 | |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 82 | /** check the current connectivity of the lb_policy */ |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 83 | grpc_connectivity_state (*check_connectivity_locked)( |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 84 | grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy, |
| 85 | grpc_error **connectivity_error); |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 86 | |
| 87 | /** call notify when the connectivity state of a channel changes from *state. |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 88 | Updates *state with the new state of the policy. Calling with a NULL \a |
David Garcia Quintas | 8aace51 | 2016-08-15 14:55:12 -0700 | [diff] [blame] | 89 | state cancels the subscription. */ |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 90 | void (*notify_on_state_change_locked)(grpc_exec_ctx *exec_ctx, |
| 91 | grpc_lb_policy *policy, |
| 92 | grpc_connectivity_state *state, |
| 93 | grpc_closure *closure); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 94 | |
| 95 | void (*update_locked)(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy, |
| 96 | const grpc_lb_policy_args *args); |
Craig Tiller | 9d0e047 | 2015-06-23 09:12:48 -0700 | [diff] [blame] | 97 | }; |
| 98 | |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 99 | //#define GRPC_LB_POLICY_REFCOUNT_DEBUG |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 100 | #ifdef GRPC_LB_POLICY_REFCOUNT_DEBUG |
David Garcia Quintas | e224a76 | 2016-11-01 13:00:58 -0700 | [diff] [blame] | 101 | |
| 102 | /* Strong references: the policy will shutdown when they reach zero */ |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 103 | #define GRPC_LB_POLICY_REF(p, r) \ |
| 104 | grpc_lb_policy_ref((p), __FILE__, __LINE__, (r)) |
Craig Tiller | 8af4c33 | 2015-09-22 12:32:31 -0700 | [diff] [blame] | 105 | #define GRPC_LB_POLICY_UNREF(exec_ctx, p, r) \ |
| 106 | grpc_lb_policy_unref((exec_ctx), (p), __FILE__, __LINE__, (r)) |
David Garcia Quintas | e224a76 | 2016-11-01 13:00:58 -0700 | [diff] [blame] | 107 | |
| 108 | /* Weak references: they don't prevent the shutdown of the LB policy. When no |
| 109 | * strong references are left but there are still weak ones, shutdown is called. |
| 110 | * Once the weak reference also reaches zero, the LB policy is destroyed. */ |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 111 | #define GRPC_LB_POLICY_WEAK_REF(p, r) \ |
| 112 | grpc_lb_policy_weak_ref((p), __FILE__, __LINE__, (r)) |
| 113 | #define GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, p, r) \ |
| 114 | grpc_lb_policy_weak_unref((exec_ctx), (p), __FILE__, __LINE__, (r)) |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 115 | void grpc_lb_policy_ref(grpc_lb_policy *policy, const char *file, int line, |
| 116 | const char *reason); |
| 117 | void grpc_lb_policy_unref(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy, |
| 118 | const char *file, int line, const char *reason); |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 119 | void grpc_lb_policy_weak_ref(grpc_lb_policy *policy, const char *file, int line, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 120 | const char *reason); |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 121 | void grpc_lb_policy_weak_unref(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 122 | const char *file, int line, const char *reason); |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 123 | #else |
| 124 | #define GRPC_LB_POLICY_REF(p, r) grpc_lb_policy_ref((p)) |
Craig Tiller | 8af4c33 | 2015-09-22 12:32:31 -0700 | [diff] [blame] | 125 | #define GRPC_LB_POLICY_UNREF(cl, p, r) grpc_lb_policy_unref((cl), (p)) |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 126 | #define GRPC_LB_POLICY_WEAK_REF(p, r) grpc_lb_policy_weak_ref((p)) |
| 127 | #define GRPC_LB_POLICY_WEAK_UNREF(cl, p, r) grpc_lb_policy_weak_unref((cl), (p)) |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 128 | void grpc_lb_policy_ref(grpc_lb_policy *policy); |
| 129 | void grpc_lb_policy_unref(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy); |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 130 | void grpc_lb_policy_weak_ref(grpc_lb_policy *policy); |
| 131 | void grpc_lb_policy_weak_unref(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy); |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 132 | #endif |
| 133 | |
| 134 | /** called by concrete implementations to initialize the base struct */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 135 | void grpc_lb_policy_init(grpc_lb_policy *policy, |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 136 | const grpc_lb_policy_vtable *vtable, |
| 137 | grpc_combiner *combiner); |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 138 | |
Mark D. Roth | 1e5f6af | 2016-10-07 08:32:58 -0700 | [diff] [blame] | 139 | /** Finds an appropriate subchannel for a call, based on \a pick_args. |
| 140 | |
Mark D. Roth | d7389b4 | 2017-05-17 12:22:17 -0700 | [diff] [blame] | 141 | \a target will be set to the selected subchannel, or NULL on failure |
| 142 | or when the LB policy decides to drop the call. |
| 143 | |
Mark D. Roth | 1e5f6af | 2016-10-07 08:32:58 -0700 | [diff] [blame] | 144 | Upon success, \a user_data will be set to whatever opaque information |
| 145 | may need to be propagated from the LB policy, or NULL if not needed. |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 146 | \a context will be populated with context to pass to the subchannel |
| 147 | call, if needed. |
Mark D. Roth | 1e5f6af | 2016-10-07 08:32:58 -0700 | [diff] [blame] | 148 | |
| 149 | If the pick succeeds and a result is known immediately, a non-zero |
| 150 | value will be returned. Otherwise, \a on_complete will be invoked |
| 151 | once the pick is complete with its error argument set to indicate |
| 152 | success or failure. |
| 153 | |
Yuchen Zeng | 3efcb48 | 2016-10-10 15:28:50 -0700 | [diff] [blame] | 154 | Any IO should be done under the \a interested_parties \a grpc_pollset_set |
| 155 | in the \a grpc_lb_policy struct. */ |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 156 | int grpc_lb_policy_pick_locked(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy, |
| 157 | const grpc_lb_policy_pick_args *pick_args, |
| 158 | grpc_connected_subchannel **target, |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 159 | grpc_call_context_element *context, |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 160 | void **user_data, grpc_closure *on_complete); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 161 | |
David Garcia Quintas | 8aace51 | 2016-08-15 14:55:12 -0700 | [diff] [blame] | 162 | /** Perform a connected subchannel ping (see \a grpc_connected_subchannel_ping) |
| 163 | against one of the connected subchannels managed by \a policy. */ |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 164 | void grpc_lb_policy_ping_one_locked(grpc_exec_ctx *exec_ctx, |
| 165 | grpc_lb_policy *policy, |
| 166 | grpc_closure *closure); |
Craig Tiller | 26dab31 | 2015-12-07 14:43:47 -0800 | [diff] [blame] | 167 | |
David Garcia Quintas | 8aace51 | 2016-08-15 14:55:12 -0700 | [diff] [blame] | 168 | /** Cancel picks for \a target. |
| 169 | The \a on_complete callback of the pending picks will be invoked with \a |
| 170 | *target set to NULL. */ |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 171 | void grpc_lb_policy_cancel_pick_locked(grpc_exec_ctx *exec_ctx, |
| 172 | grpc_lb_policy *policy, |
| 173 | grpc_connected_subchannel **target, |
| 174 | grpc_error *error); |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 175 | |
David Garcia Quintas | 8aace51 | 2016-08-15 14:55:12 -0700 | [diff] [blame] | 176 | /** Cancel all pending picks for which their \a initial_metadata_flags (as given |
| 177 | in the call to \a grpc_lb_policy_pick) matches \a initial_metadata_flags_eq |
| 178 | when AND'd with \a initial_metadata_flags_mask */ |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 179 | void grpc_lb_policy_cancel_picks_locked(grpc_exec_ctx *exec_ctx, |
| 180 | grpc_lb_policy *policy, |
| 181 | uint32_t initial_metadata_flags_mask, |
| 182 | uint32_t initial_metadata_flags_eq, |
| 183 | grpc_error *error); |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 184 | |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 185 | /** Try to enter a READY connectivity state */ |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 186 | void grpc_lb_policy_exit_idle_locked(grpc_exec_ctx *exec_ctx, |
| 187 | grpc_lb_policy *policy); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 188 | |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 189 | /* Call notify when the connectivity state of a channel changes from \a *state. |
| 190 | * Updates \a *state with the new state of the policy */ |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 191 | void grpc_lb_policy_notify_on_state_change_locked( |
| 192 | grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy, |
| 193 | grpc_connectivity_state *state, grpc_closure *closure); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 194 | |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 195 | grpc_connectivity_state grpc_lb_policy_check_connectivity_locked( |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 196 | grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy, |
| 197 | grpc_error **connectivity_error); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 198 | |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 199 | /** Update \a policy with \a lb_policy_args. */ |
| 200 | void grpc_lb_policy_update_locked(grpc_exec_ctx *exec_ctx, |
| 201 | grpc_lb_policy *policy, |
| 202 | const grpc_lb_policy_args *lb_policy_args); |
| 203 | |
Craig Tiller | d608cd6 | 2017-04-02 16:23:17 -0700 | [diff] [blame] | 204 | #endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_LB_POLICY_H */ |