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