David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2016, Google Inc. |
| 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 | |
David Garcia Quintas | 8b3b97f | 2016-07-15 07:46:47 -0700 | [diff] [blame] | 34 | /** Implementation of the gRPC LB policy. |
| 35 | * |
David Garcia Quintas | 4333984 | 2016-07-18 12:56:09 -0700 | [diff] [blame] | 36 | * This policy takes as input a set of resolved addresses {a1..an} for which the |
| 37 | * LB set was set (it's the resolver's responsibility to ensure this). That is |
| 38 | * to say, {a1..an} represent a collection of LB servers. |
| 39 | * |
| 40 | * An internal channel (\a glb_lb_policy.lb_channel) is created over {a1..an}. |
| 41 | * This channel behaves just like a regular channel. In particular, the |
| 42 | * constructed URI over the addresses a1..an will use the default pick first |
| 43 | * policy to select from this list of LB server backends. |
| 44 | * |
David Garcia Quintas | 41bef45 | 2016-07-28 19:19:58 -0700 | [diff] [blame] | 45 | * The first time the policy gets a request for a pick, a ping, or to exit the |
| 46 | * idle state, \a query_for_backends() is called. It creates an instance of \a |
David Garcia Quintas | 4333984 | 2016-07-18 12:56:09 -0700 | [diff] [blame] | 47 | * lb_client_data, an internal struct meant to contain the data associated with |
| 48 | * the internal communication with the LB server. This instance is created via |
| 49 | * \a lb_client_data_create(). There, the call over lb_channel to pick-first |
| 50 | * from {a1..an} is created, the \a LoadBalancingRequest message is assembled |
| 51 | * and all necessary callbacks for the progress of the internal call configured. |
| 52 | * |
| 53 | * Back in \a query_for_backends(), the internal *streaming* call to the LB |
| 54 | * server (whichever address from {a1..an} pick-first chose) is kicked off. |
| 55 | * It'll progress over the callbacks configured in \a lb_client_data_create() |
| 56 | * (see the field docstrings of \a lb_client_data for more details). |
| 57 | * |
| 58 | * If the call fails with UNIMPLEMENTED, the original call will also fail. |
| 59 | * There's a misconfiguration somewhere: at least one of {a1..an} isn't a LB |
| 60 | * server, which contradicts the LB bit being set. If the internal call times |
| 61 | * out, the usual behavior of pick-first applies, continuing to pick from the |
| 62 | * list {a1..an}. |
| 63 | * |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 64 | * Upon sucesss, a \a LoadBalancingResponse is expected in \a res_recv_cb. An |
David Garcia Quintas | 4333984 | 2016-07-18 12:56:09 -0700 | [diff] [blame] | 65 | * invalid one results in the termination of the streaming call. A new streaming |
| 66 | * call should be created if possible, failing the original call otherwise. |
| 67 | * For a valid \a LoadBalancingResponse, the server list of actual backends is |
| 68 | * extracted. A Round Robin policy will be created from this list. There are two |
| 69 | * possible scenarios: |
| 70 | * |
| 71 | * 1. This is the first server list received. There was no previous instance of |
| 72 | * the Round Robin policy. \a rr_handover() will instantiate the RR policy |
| 73 | * and perform all the pending operations over it. |
| 74 | * 2. There's already a RR policy instance active. We need to introduce the new |
| 75 | * one build from the new serverlist, but taking care not to disrupt the |
| 76 | * operations in progress over the old RR instance. This is done by |
| 77 | * decreasing the reference count on the old policy. The moment no more |
| 78 | * references are held on the old RR policy, it'll be destroyed and \a |
David Garcia Quintas | 348cfdb | 2016-08-19 12:19:43 -0700 | [diff] [blame] | 79 | * glb_rr_connectivity_changed notified with a \a GRPC_CHANNEL_SHUTDOWN |
| 80 | * state. At this point we can transition to a new RR instance safely, which |
| 81 | * is done once again via \a rr_handover(). |
David Garcia Quintas | 4333984 | 2016-07-18 12:56:09 -0700 | [diff] [blame] | 82 | * |
| 83 | * |
| 84 | * Once a RR policy instance is in place (and getting updated as described), |
| 85 | * calls to for a pick, a ping or a cancellation will be serviced right away by |
| 86 | * forwarding them to the RR instance. Any time there's no RR policy available |
David Garcia Quintas | d4a756b | 2016-07-19 11:35:15 -0700 | [diff] [blame] | 87 | * (ie, right after the creation of the gRPCLB policy, if an empty serverlist |
David Garcia Quintas | 4333984 | 2016-07-18 12:56:09 -0700 | [diff] [blame] | 88 | * is received, etc), pick/ping requests are added to a list of pending |
| 89 | * picks/pings to be flushed and serviced as part of \a rr_handover() the moment |
| 90 | * the RR policy instance becomes available. |
| 91 | * |
| 92 | * \see https://github.com/grpc/grpc/blob/master/doc/load-balancing.md for the |
| 93 | * high level design and details. */ |
David Garcia Quintas | 8b3b97f | 2016-07-15 07:46:47 -0700 | [diff] [blame] | 94 | |
| 95 | /* TODO(dgq): |
| 96 | * - Implement LB service forwarding (point 2c. in the doc's diagram). |
| 97 | */ |
| 98 | |
David Garcia Quintas | 8a81aa1 | 2016-08-22 15:06:49 -0700 | [diff] [blame] | 99 | #include <errno.h> |
| 100 | |
David Garcia Quintas | 22e8f1d | 2016-06-15 23:53:00 -0700 | [diff] [blame] | 101 | #include <string.h> |
| 102 | |
| 103 | #include <grpc/byte_buffer_reader.h> |
| 104 | #include <grpc/grpc.h> |
| 105 | #include <grpc/support/alloc.h> |
| 106 | #include <grpc/support/host_port.h> |
| 107 | #include <grpc/support/string_util.h> |
| 108 | |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 109 | #include "src/core/ext/client_config/client_channel_factory.h" |
| 110 | #include "src/core/ext/client_config/lb_policy_registry.h" |
| 111 | #include "src/core/ext/client_config/parse_address.h" |
David Garcia Quintas | 8782d1b | 2016-06-15 23:58:44 -0700 | [diff] [blame] | 112 | #include "src/core/ext/lb_policy/grpclb/grpclb.h" |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 113 | #include "src/core/ext/lb_policy/grpclb/load_balancer_api.h" |
David Garcia Quintas | bc33491 | 2016-08-22 16:57:20 -0700 | [diff] [blame] | 114 | #include "src/core/lib/iomgr/sockaddr.h" |
David Garcia Quintas | b8b384a | 2016-08-23 21:10:29 -0700 | [diff] [blame] | 115 | #include "src/core/lib/iomgr/sockaddr_utils.h" |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 116 | #include "src/core/lib/support/string.h" |
| 117 | #include "src/core/lib/surface/call.h" |
| 118 | #include "src/core/lib/surface/channel.h" |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame^] | 119 | #include "src/core/lib/transport/static_metadata.h" |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 120 | |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 121 | int grpc_lb_glb_trace = 0; |
| 122 | |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame^] | 123 | static void *user_data_copy(void *user_data) { |
| 124 | if (user_data == NULL) return NULL; |
| 125 | return GRPC_MDELEM_REF(user_data); |
| 126 | } |
| 127 | |
| 128 | static void user_data_destroy(void *user_data) { |
| 129 | if (user_data == NULL) return; |
| 130 | GRPC_MDELEM_UNREF(user_data); |
| 131 | } |
| 132 | |
| 133 | /* add lb_token of selected subchannel (address) to the call's initial |
| 134 | * metadata */ |
| 135 | static void initial_metadata_add_lb_token( |
| 136 | grpc_metadata_batch *initial_metadata, |
| 137 | grpc_linked_mdelem *lb_token_mdelem_storage, grpc_mdelem *lb_token) { |
| 138 | GPR_ASSERT(lb_token_mdelem_storage != NULL); |
| 139 | GPR_ASSERT(lb_token != NULL); |
| 140 | grpc_metadata_batch_add_tail(initial_metadata, lb_token_mdelem_storage, |
| 141 | lb_token); |
| 142 | } |
| 143 | |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 144 | typedef struct wrapped_rr_closure_arg { |
David Garcia Quintas | 4333984 | 2016-07-18 12:56:09 -0700 | [diff] [blame] | 145 | /* the original closure. Usually a on_complete/notify cb for pick() and ping() |
| 146 | * calls against the internal RR instance, respectively. */ |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 147 | grpc_closure *wrapped_closure; |
David Garcia Quintas | 4333984 | 2016-07-18 12:56:09 -0700 | [diff] [blame] | 148 | |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame^] | 149 | /* the pick's initial metadata, kept in order to append the LB token for the |
| 150 | * pick */ |
| 151 | grpc_metadata_batch *initial_metadata; |
| 152 | |
| 153 | /* the picked target, used to determine which LB token to add to the pick's |
| 154 | * initial metadata */ |
| 155 | grpc_connected_subchannel **target; |
| 156 | |
| 157 | /* the LB token associated with the pick */ |
| 158 | grpc_mdelem *lb_token; |
| 159 | |
| 160 | /* storage for the lb token initial metadata mdelem */ |
| 161 | grpc_linked_mdelem *lb_token_mdelem_storage; |
| 162 | |
David Garcia Quintas | 4333984 | 2016-07-18 12:56:09 -0700 | [diff] [blame] | 163 | /* The RR instance related to the closure */ |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 164 | grpc_lb_policy *rr_policy; |
David Garcia Quintas | 4333984 | 2016-07-18 12:56:09 -0700 | [diff] [blame] | 165 | |
| 166 | /* when not NULL, represents a pending_{pick,ping} node to be freed upon |
| 167 | * closure execution */ |
| 168 | void *owning_pending_node; /* to be freed if not NULL */ |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 169 | } wrapped_rr_closure_arg; |
| 170 | |
| 171 | /* The \a on_complete closure passed as part of the pick requires keeping a |
| 172 | * reference to its associated round robin instance. We wrap this closure in |
| 173 | * order to unref the round robin instance upon its invocation */ |
| 174 | static void wrapped_rr_closure(grpc_exec_ctx *exec_ctx, void *arg, |
David Garcia Quintas | 280fd2a | 2016-06-20 22:04:48 -0700 | [diff] [blame] | 175 | grpc_error *error) { |
David Garcia Quintas | 4333984 | 2016-07-18 12:56:09 -0700 | [diff] [blame] | 176 | wrapped_rr_closure_arg *wc_arg = arg; |
David Garcia Quintas | 4333984 | 2016-07-18 12:56:09 -0700 | [diff] [blame] | 177 | if (wc_arg->rr_policy != NULL) { |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 178 | if (grpc_lb_glb_trace) { |
David Garcia Quintas | 280fd2a | 2016-06-20 22:04:48 -0700 | [diff] [blame] | 179 | gpr_log(GPR_INFO, "Unreffing RR (0x%" PRIxPTR ")", |
David Garcia Quintas | 4333984 | 2016-07-18 12:56:09 -0700 | [diff] [blame] | 180 | (intptr_t)wc_arg->rr_policy); |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 181 | } |
David Garcia Quintas | 4333984 | 2016-07-18 12:56:09 -0700 | [diff] [blame] | 182 | GRPC_LB_POLICY_UNREF(exec_ctx, wc_arg->rr_policy, "wrapped_rr_closure"); |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 183 | } |
David Garcia Quintas | 5a87616 | 2016-07-18 13:08:42 -0700 | [diff] [blame] | 184 | GPR_ASSERT(wc_arg->wrapped_closure != NULL); |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame^] | 185 | |
| 186 | initial_metadata_add_lb_token(wc_arg->initial_metadata, |
| 187 | wc_arg->lb_token_mdelem_storage, |
| 188 | wc_arg->lb_token); |
| 189 | |
David Garcia Quintas | 5a87616 | 2016-07-18 13:08:42 -0700 | [diff] [blame] | 190 | grpc_exec_ctx_sched(exec_ctx, wc_arg->wrapped_closure, error, NULL); |
David Garcia Quintas | 4333984 | 2016-07-18 12:56:09 -0700 | [diff] [blame] | 191 | gpr_free(wc_arg->owning_pending_node); |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 192 | } |
| 193 | |
David Garcia Quintas | ea11d16 | 2016-07-14 17:27:28 -0700 | [diff] [blame] | 194 | /* Linked list of pending pick requests. It stores all information needed to |
| 195 | * eventually call (Round Robin's) pick() on them. They mainly stay pending |
| 196 | * waiting for the RR policy to be created/updated. |
| 197 | * |
| 198 | * One particularity is the wrapping of the user-provided \a on_complete closure |
| 199 | * (in \a wrapped_on_complete and \a wrapped_on_complete_arg). This is needed in |
| 200 | * order to correctly unref the RR policy instance upon completion of the pick. |
| 201 | * See \a wrapped_rr_closure for details. */ |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 202 | typedef struct pending_pick { |
| 203 | struct pending_pick *next; |
David Garcia Quintas | 4333984 | 2016-07-18 12:56:09 -0700 | [diff] [blame] | 204 | |
| 205 | /* polling entity for the pick()'s async notification */ |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 206 | grpc_polling_entity *pollent; |
David Garcia Quintas | 4333984 | 2016-07-18 12:56:09 -0700 | [diff] [blame] | 207 | |
| 208 | /* the initial metadata for the pick. See grpc_lb_policy_pick() */ |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 209 | grpc_metadata_batch *initial_metadata; |
David Garcia Quintas | 4333984 | 2016-07-18 12:56:09 -0700 | [diff] [blame] | 210 | |
David Garcia Quintas | 5b0e946 | 2016-08-15 19:38:39 -0700 | [diff] [blame] | 211 | /* storage for the lb token initial metadata mdelem */ |
| 212 | grpc_linked_mdelem *lb_token_mdelem_storage; |
| 213 | |
David Garcia Quintas | 4333984 | 2016-07-18 12:56:09 -0700 | [diff] [blame] | 214 | /* bitmask passed to pick() and used for selective cancelling. See |
| 215 | * grpc_lb_policy_cancel_picks() */ |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 216 | uint32_t initial_metadata_flags; |
David Garcia Quintas | 4333984 | 2016-07-18 12:56:09 -0700 | [diff] [blame] | 217 | |
| 218 | /* output argument where to store the pick()ed connected subchannel, or NULL |
| 219 | * upon error. */ |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 220 | grpc_connected_subchannel **target; |
David Garcia Quintas | 4333984 | 2016-07-18 12:56:09 -0700 | [diff] [blame] | 221 | |
| 222 | /* a closure wrapping the original on_complete one to be invoked once the |
| 223 | * pick() has completed (regardless of success) */ |
| 224 | grpc_closure wrapped_on_complete; |
| 225 | |
| 226 | /* args for wrapped_on_complete */ |
| 227 | wrapped_rr_closure_arg wrapped_on_complete_arg; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 228 | } pending_pick; |
| 229 | |
David Garcia Quintas | 8aace51 | 2016-08-15 14:55:12 -0700 | [diff] [blame] | 230 | static void add_pending_pick(pending_pick **root, |
| 231 | const grpc_lb_policy_pick_args *pick_args, |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 232 | grpc_connected_subchannel **target, |
| 233 | grpc_closure *on_complete) { |
| 234 | pending_pick *pp = gpr_malloc(sizeof(*pp)); |
| 235 | memset(pp, 0, sizeof(pending_pick)); |
| 236 | memset(&pp->wrapped_on_complete_arg, 0, sizeof(wrapped_rr_closure_arg)); |
| 237 | pp->next = *root; |
David Garcia Quintas | 8aace51 | 2016-08-15 14:55:12 -0700 | [diff] [blame] | 238 | pp->pollent = pick_args->pollent; |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 239 | pp->target = target; |
David Garcia Quintas | 8aace51 | 2016-08-15 14:55:12 -0700 | [diff] [blame] | 240 | pp->initial_metadata = pick_args->initial_metadata; |
| 241 | pp->initial_metadata_flags = pick_args->initial_metadata_flags; |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame^] | 242 | pp->lb_token_mdelem_storage = pick_args->lb_token_mdelem_storage; |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 243 | pp->wrapped_on_complete_arg.wrapped_closure = on_complete; |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame^] | 244 | pp->wrapped_on_complete_arg.initial_metadata = pick_args->initial_metadata; |
| 245 | pp->wrapped_on_complete_arg.lb_token_mdelem_storage = |
| 246 | pick_args->lb_token_mdelem_storage; |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 247 | grpc_closure_init(&pp->wrapped_on_complete, wrapped_rr_closure, |
| 248 | &pp->wrapped_on_complete_arg); |
| 249 | *root = pp; |
| 250 | } |
| 251 | |
David Garcia Quintas | ea11d16 | 2016-07-14 17:27:28 -0700 | [diff] [blame] | 252 | /* Same as the \a pending_pick struct but for ping operations */ |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 253 | typedef struct pending_ping { |
| 254 | struct pending_ping *next; |
David Garcia Quintas | 4333984 | 2016-07-18 12:56:09 -0700 | [diff] [blame] | 255 | |
| 256 | /* a closure wrapping the original on_complete one to be invoked once the |
| 257 | * ping() has completed (regardless of success) */ |
| 258 | grpc_closure wrapped_notify; |
| 259 | |
| 260 | /* args for wrapped_notify */ |
| 261 | wrapped_rr_closure_arg wrapped_notify_arg; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 262 | } pending_ping; |
| 263 | |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 264 | static void add_pending_ping(pending_ping **root, grpc_closure *notify) { |
| 265 | pending_ping *pping = gpr_malloc(sizeof(*pping)); |
| 266 | memset(pping, 0, sizeof(pending_ping)); |
| 267 | memset(&pping->wrapped_notify_arg, 0, sizeof(wrapped_rr_closure_arg)); |
| 268 | pping->next = *root; |
| 269 | grpc_closure_init(&pping->wrapped_notify, wrapped_rr_closure, |
| 270 | &pping->wrapped_notify_arg); |
| 271 | pping->wrapped_notify_arg.wrapped_closure = notify; |
| 272 | *root = pping; |
| 273 | } |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 274 | |
David Garcia Quintas | 8d48911 | 2016-07-29 15:20:42 -0700 | [diff] [blame] | 275 | /* |
| 276 | * glb_lb_policy |
| 277 | */ |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 278 | typedef struct rr_connectivity_data rr_connectivity_data; |
David Garcia Quintas | a0e278e | 2016-08-01 11:36:14 -0700 | [diff] [blame] | 279 | struct lb_client_data; |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 280 | static const grpc_lb_policy_vtable glb_lb_policy_vtable; |
| 281 | typedef struct glb_lb_policy { |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 282 | /** base policy: must be first */ |
| 283 | grpc_lb_policy base; |
| 284 | |
| 285 | /** mutex protecting remaining members */ |
| 286 | gpr_mu mu; |
| 287 | |
| 288 | grpc_client_channel_factory *cc_factory; |
| 289 | |
| 290 | /** for communicating with the LB server */ |
David Garcia Quintas | ea11d16 | 2016-07-14 17:27:28 -0700 | [diff] [blame] | 291 | grpc_channel *lb_channel; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 292 | |
| 293 | /** the RR policy to use of the backend servers returned by the LB server */ |
| 294 | grpc_lb_policy *rr_policy; |
| 295 | |
| 296 | bool started_picking; |
| 297 | |
| 298 | /** our connectivity state tracker */ |
| 299 | grpc_connectivity_state_tracker state_tracker; |
| 300 | |
David Garcia Quintas | ea11d16 | 2016-07-14 17:27:28 -0700 | [diff] [blame] | 301 | /** stores the deserialized response from the LB. May be NULL until one such |
| 302 | * response has arrived. */ |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 303 | grpc_grpclb_serverlist *serverlist; |
| 304 | |
David Garcia Quintas | ea11d16 | 2016-07-14 17:27:28 -0700 | [diff] [blame] | 305 | /** list of picks that are waiting on RR's policy connectivity */ |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 306 | pending_pick *pending_picks; |
| 307 | |
David Garcia Quintas | ea11d16 | 2016-07-14 17:27:28 -0700 | [diff] [blame] | 308 | /** list of pings that are waiting on RR's policy connectivity */ |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 309 | pending_ping *pending_pings; |
| 310 | |
David Garcia Quintas | ea11d16 | 2016-07-14 17:27:28 -0700 | [diff] [blame] | 311 | /** client data associated with the LB server communication */ |
David Garcia Quintas | a0e278e | 2016-08-01 11:36:14 -0700 | [diff] [blame] | 312 | struct lb_client_data *lb_client; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 313 | |
| 314 | /** for tracking of the RR connectivity */ |
| 315 | rr_connectivity_data *rr_connectivity; |
David Garcia Quintas | 4333984 | 2016-07-18 12:56:09 -0700 | [diff] [blame] | 316 | |
David Garcia Quintas | 41bef45 | 2016-07-28 19:19:58 -0700 | [diff] [blame] | 317 | /* a wrapped (see \a wrapped_rr_closure) on-complete closure for readily |
| 318 | * available RR picks */ |
David Garcia Quintas | 4333984 | 2016-07-18 12:56:09 -0700 | [diff] [blame] | 319 | grpc_closure wrapped_on_complete; |
| 320 | |
| 321 | /* arguments for the wrapped_on_complete closure */ |
| 322 | wrapped_rr_closure_arg wc_arg; |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 323 | } glb_lb_policy; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 324 | |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 325 | /* Keeps track and reacts to changes in connectivity of the RR instance */ |
| 326 | struct rr_connectivity_data { |
| 327 | grpc_closure on_change; |
| 328 | grpc_connectivity_state state; |
| 329 | glb_lb_policy *glb_policy; |
| 330 | }; |
David Garcia Quintas | 8d48911 | 2016-07-29 15:20:42 -0700 | [diff] [blame] | 331 | |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame^] | 332 | /* populate \a addresses according to \a serverlist. Returns the number of |
| 333 | * addresses successfully parsed and added to \a addresses */ |
| 334 | static size_t process_serverlist(const grpc_grpclb_serverlist *serverlist, |
| 335 | grpc_lb_address **lb_addresses) { |
| 336 | size_t num_valid = 0; |
| 337 | /* first pass: count how many are valid in order to allocate the necessary |
| 338 | * memory in a single block */ |
| 339 | for (size_t i = 0; i < serverlist->num_servers; ++i) { |
| 340 | const grpc_grpclb_server *server = serverlist->servers[i]; |
| 341 | const grpc_grpclb_ip_address *ip = &server->ip_address; |
| 342 | |
| 343 | if (server->port >> 16 != 0) { |
| 344 | gpr_log(GPR_ERROR, |
| 345 | "Invalid port '%d' at index %zu of serverlist. Ignoring.", |
| 346 | server->port, i); |
| 347 | continue; |
| 348 | } |
| 349 | |
| 350 | if (ip->size != 4 && ip->size != 16) { |
| 351 | gpr_log(GPR_ERROR, |
| 352 | "Expected IP to be 4 or 16 bytes, got %d at index %zu of " |
| 353 | "serverlist. Ignoring", |
| 354 | ip->size, i); |
| 355 | continue; |
| 356 | } |
| 357 | ++num_valid; |
David Garcia Quintas | b8b384a | 2016-08-23 21:10:29 -0700 | [diff] [blame] | 358 | } |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame^] | 359 | if (num_valid == 0) { |
| 360 | return 0; |
David Garcia Quintas | b8b384a | 2016-08-23 21:10:29 -0700 | [diff] [blame] | 361 | } |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame^] | 362 | |
| 363 | /* allocate the memory block for the "resolved" addresses. */ |
| 364 | grpc_resolved_address *r_addrs_memblock = |
| 365 | gpr_malloc(sizeof(grpc_resolved_address) * num_valid); |
| 366 | memset(r_addrs_memblock, 0, sizeof(grpc_resolved_address) * num_valid); |
| 367 | grpc_lb_address *lb_addrs = gpr_malloc(sizeof(grpc_lb_address) * num_valid); |
| 368 | memset(lb_addrs, 0, sizeof(grpc_lb_address) * num_valid); |
| 369 | |
| 370 | /* second pass: actually populate the addresses and LB tokens (aka user data |
| 371 | * to the outside world) to be read by the RR policy during its creation */ |
| 372 | for (size_t i = 0; i < num_valid; ++i) { |
| 373 | const grpc_grpclb_server *server = serverlist->servers[i]; |
| 374 | grpc_lb_address *const lb_addr = &lb_addrs[i]; |
| 375 | |
| 376 | /* lb token processing */ |
| 377 | if (server->has_load_balance_token) { |
| 378 | const size_t lb_token_size = |
| 379 | GPR_ARRAY_SIZE(server->load_balance_token) - 1; |
| 380 | grpc_mdstr *lb_token_mdstr = grpc_mdstr_from_buffer( |
| 381 | (uint8_t *)server->load_balance_token, lb_token_size); |
| 382 | lb_addr->user_data = grpc_mdelem_from_metadata_strings( |
| 383 | GRPC_MDSTR_LOAD_REPORTING_INITIAL, lb_token_mdstr); |
| 384 | } |
| 385 | |
| 386 | /* address processing */ |
| 387 | const uint16_t netorder_port = htons((uint16_t)server->port); |
| 388 | /* the addresses are given in binary format (a in(6)_addr struct) in |
| 389 | * server->ip_address.bytes. */ |
| 390 | const grpc_grpclb_ip_address *ip = &server->ip_address; |
| 391 | |
| 392 | lb_addr->resolved_address = &r_addrs_memblock[i]; |
| 393 | struct sockaddr_storage *sa = |
| 394 | (struct sockaddr_storage *)lb_addr->resolved_address->addr; |
| 395 | size_t *sa_len = &lb_addr->resolved_address->len; |
| 396 | *sa_len = 0; |
| 397 | if (ip->size == 4) { |
| 398 | struct sockaddr_in *addr4 = (struct sockaddr_in *)sa; |
| 399 | *sa_len = sizeof(struct sockaddr_in); |
| 400 | memset(addr4, 0, *sa_len); |
| 401 | addr4->sin_family = AF_INET; |
| 402 | memcpy(&addr4->sin_addr, ip->bytes, ip->size); |
| 403 | addr4->sin_port = netorder_port; |
| 404 | } else if (ip->size == 16) { |
| 405 | struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)sa; |
| 406 | *sa_len = sizeof(struct sockaddr_in6); |
| 407 | memset(addr6, 0, *sa_len); |
| 408 | addr6->sin6_family = AF_INET; |
| 409 | memcpy(&addr6->sin6_addr, ip->bytes, ip->size); |
| 410 | addr6->sin6_port = netorder_port; |
| 411 | } |
| 412 | GPR_ASSERT(*sa_len > 0); |
| 413 | } |
| 414 | *lb_addresses = lb_addrs; |
| 415 | return num_valid; |
David Garcia Quintas | b8b384a | 2016-08-23 21:10:29 -0700 | [diff] [blame] | 416 | } |
| 417 | |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 418 | static grpc_lb_policy *create_rr(grpc_exec_ctx *exec_ctx, |
| 419 | const grpc_grpclb_serverlist *serverlist, |
| 420 | glb_lb_policy *glb_policy) { |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 421 | GPR_ASSERT(serverlist != NULL && serverlist->num_servers > 0); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 422 | |
| 423 | grpc_lb_policy_args args; |
David Garcia Quintas | 5b0e946 | 2016-08-15 19:38:39 -0700 | [diff] [blame] | 424 | memset(&args, 0, sizeof(args)); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 425 | args.client_channel_factory = glb_policy->cc_factory; |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame^] | 426 | args.num_addresses = process_serverlist(serverlist, &args.lb_addresses); |
| 427 | args.user_data_vtable.copy = user_data_copy; |
| 428 | args.user_data_vtable.destroy = user_data_destroy; |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 429 | |
| 430 | grpc_lb_policy *rr = grpc_lb_policy_create(exec_ctx, "round_robin", &args); |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame^] | 431 | if (args.num_addresses > 0) { |
| 432 | /* free "resolved" addresses memblock */ |
| 433 | gpr_free(args.lb_addresses->resolved_address); |
| 434 | } |
| 435 | for (size_t i = 0; i < args.num_addresses; ++i) { |
| 436 | args.user_data_vtable.destroy(args.lb_addresses[i].user_data); |
| 437 | } |
| 438 | gpr_free(args.lb_addresses); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 439 | return rr; |
| 440 | } |
David Garcia Quintas | 8d48911 | 2016-07-29 15:20:42 -0700 | [diff] [blame] | 441 | |
David Garcia Quintas | 41bef45 | 2016-07-28 19:19:58 -0700 | [diff] [blame] | 442 | static void rr_handover(grpc_exec_ctx *exec_ctx, glb_lb_policy *glb_policy, |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 443 | grpc_error *error) { |
David Garcia Quintas | 5b0e946 | 2016-08-15 19:38:39 -0700 | [diff] [blame] | 444 | GPR_ASSERT(glb_policy->serverlist != NULL && |
| 445 | glb_policy->serverlist->num_servers > 0); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 446 | glb_policy->rr_policy = |
| 447 | create_rr(exec_ctx, glb_policy->serverlist, glb_policy); |
| 448 | |
| 449 | if (grpc_lb_glb_trace) { |
| 450 | gpr_log(GPR_INFO, "Created RR policy (0x%" PRIxPTR ")", |
| 451 | (intptr_t)glb_policy->rr_policy); |
| 452 | } |
| 453 | GPR_ASSERT(glb_policy->rr_policy != NULL); |
| 454 | glb_policy->rr_connectivity->state = grpc_lb_policy_check_connectivity( |
| 455 | exec_ctx, glb_policy->rr_policy, &error); |
| 456 | grpc_lb_policy_notify_on_state_change( |
| 457 | exec_ctx, glb_policy->rr_policy, &glb_policy->rr_connectivity->state, |
| 458 | &glb_policy->rr_connectivity->on_change); |
| 459 | grpc_connectivity_state_set(exec_ctx, &glb_policy->state_tracker, |
David Garcia Quintas | 348cfdb | 2016-08-19 12:19:43 -0700 | [diff] [blame] | 460 | glb_policy->rr_connectivity->state, |
| 461 | GRPC_ERROR_REF(error), "rr_handover"); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 462 | grpc_lb_policy_exit_idle(exec_ctx, glb_policy->rr_policy); |
| 463 | |
| 464 | /* flush pending ops */ |
| 465 | pending_pick *pp; |
| 466 | while ((pp = glb_policy->pending_picks)) { |
| 467 | glb_policy->pending_picks = pp->next; |
| 468 | GRPC_LB_POLICY_REF(glb_policy->rr_policy, "rr_handover_pending_pick"); |
| 469 | pp->wrapped_on_complete_arg.rr_policy = glb_policy->rr_policy; |
| 470 | if (grpc_lb_glb_trace) { |
| 471 | gpr_log(GPR_INFO, "Pending pick about to PICK from 0x%" PRIxPTR "", |
| 472 | (intptr_t)glb_policy->rr_policy); |
| 473 | } |
David Garcia Quintas | 8aace51 | 2016-08-15 14:55:12 -0700 | [diff] [blame] | 474 | const grpc_lb_policy_pick_args pick_args = { |
David Garcia Quintas | 5b0e946 | 2016-08-15 19:38:39 -0700 | [diff] [blame] | 475 | pp->pollent, pp->initial_metadata, pp->initial_metadata_flags, |
| 476 | pp->lb_token_mdelem_storage}; |
David Garcia Quintas | 8aace51 | 2016-08-15 14:55:12 -0700 | [diff] [blame] | 477 | grpc_lb_policy_pick(exec_ctx, glb_policy->rr_policy, &pick_args, pp->target, |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame^] | 478 | (void **)&pp->wrapped_on_complete_arg.lb_token, |
David Garcia Quintas | 8aace51 | 2016-08-15 14:55:12 -0700 | [diff] [blame] | 479 | &pp->wrapped_on_complete); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 480 | pp->wrapped_on_complete_arg.owning_pending_node = pp; |
| 481 | } |
| 482 | |
| 483 | pending_ping *pping; |
| 484 | while ((pping = glb_policy->pending_pings)) { |
| 485 | glb_policy->pending_pings = pping->next; |
| 486 | GRPC_LB_POLICY_REF(glb_policy->rr_policy, "rr_handover_pending_ping"); |
| 487 | pping->wrapped_notify_arg.rr_policy = glb_policy->rr_policy; |
| 488 | if (grpc_lb_glb_trace) { |
| 489 | gpr_log(GPR_INFO, "Pending ping about to PING from 0x%" PRIxPTR "", |
| 490 | (intptr_t)glb_policy->rr_policy); |
| 491 | } |
| 492 | grpc_lb_policy_ping_one(exec_ctx, glb_policy->rr_policy, |
| 493 | &pping->wrapped_notify); |
| 494 | pping->wrapped_notify_arg.owning_pending_node = pping; |
| 495 | } |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 496 | } |
David Garcia Quintas | 8d48911 | 2016-07-29 15:20:42 -0700 | [diff] [blame] | 497 | |
David Garcia Quintas | 348cfdb | 2016-08-19 12:19:43 -0700 | [diff] [blame] | 498 | static void glb_rr_connectivity_changed(grpc_exec_ctx *exec_ctx, void *arg, |
| 499 | grpc_error *error) { |
David Garcia Quintas | 41bef45 | 2016-07-28 19:19:58 -0700 | [diff] [blame] | 500 | rr_connectivity_data *rr_conn_data = arg; |
| 501 | glb_lb_policy *glb_policy = rr_conn_data->glb_policy; |
David Garcia Quintas | 348cfdb | 2016-08-19 12:19:43 -0700 | [diff] [blame] | 502 | |
David Garcia Quintas | 41bef45 | 2016-07-28 19:19:58 -0700 | [diff] [blame] | 503 | if (rr_conn_data->state == GRPC_CHANNEL_SHUTDOWN) { |
| 504 | if (glb_policy->serverlist != NULL) { |
David Garcia Quintas | 280fd2a | 2016-06-20 22:04:48 -0700 | [diff] [blame] | 505 | /* a RR policy is shutting down but there's a serverlist available -> |
| 506 | * perform a handover */ |
David Garcia Quintas | 41bef45 | 2016-07-28 19:19:58 -0700 | [diff] [blame] | 507 | rr_handover(exec_ctx, glb_policy, error); |
David Garcia Quintas | 280fd2a | 2016-06-20 22:04:48 -0700 | [diff] [blame] | 508 | } else { |
David Garcia Quintas | ea11d16 | 2016-07-14 17:27:28 -0700 | [diff] [blame] | 509 | /* shutting down and no new serverlist available. Bail out. */ |
David Garcia Quintas | 41bef45 | 2016-07-28 19:19:58 -0700 | [diff] [blame] | 510 | gpr_free(rr_conn_data); |
David Garcia Quintas | 280fd2a | 2016-06-20 22:04:48 -0700 | [diff] [blame] | 511 | } |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 512 | } else { |
David Garcia Quintas | 280fd2a | 2016-06-20 22:04:48 -0700 | [diff] [blame] | 513 | if (error == GRPC_ERROR_NONE) { |
David Garcia Quintas | ea11d16 | 2016-07-14 17:27:28 -0700 | [diff] [blame] | 514 | /* RR not shutting down. Mimic the RR's policy state */ |
David Garcia Quintas | 41bef45 | 2016-07-28 19:19:58 -0700 | [diff] [blame] | 515 | grpc_connectivity_state_set(exec_ctx, &glb_policy->state_tracker, |
David Garcia Quintas | 348cfdb | 2016-08-19 12:19:43 -0700 | [diff] [blame] | 516 | rr_conn_data->state, GRPC_ERROR_REF(error), |
| 517 | "glb_rr_connectivity_changed"); |
David Garcia Quintas | 280fd2a | 2016-06-20 22:04:48 -0700 | [diff] [blame] | 518 | /* resubscribe */ |
David Garcia Quintas | 41bef45 | 2016-07-28 19:19:58 -0700 | [diff] [blame] | 519 | grpc_lb_policy_notify_on_state_change(exec_ctx, glb_policy->rr_policy, |
| 520 | &rr_conn_data->state, |
| 521 | &rr_conn_data->on_change); |
David Garcia Quintas | 280fd2a | 2016-06-20 22:04:48 -0700 | [diff] [blame] | 522 | } else { /* error */ |
David Garcia Quintas | 41bef45 | 2016-07-28 19:19:58 -0700 | [diff] [blame] | 523 | gpr_free(rr_conn_data); |
David Garcia Quintas | 280fd2a | 2016-06-20 22:04:48 -0700 | [diff] [blame] | 524 | } |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 525 | } |
| 526 | } |
| 527 | |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 528 | static grpc_lb_policy *glb_create(grpc_exec_ctx *exec_ctx, |
| 529 | grpc_lb_policy_factory *factory, |
| 530 | grpc_lb_policy_args *args) { |
| 531 | glb_lb_policy *glb_policy = gpr_malloc(sizeof(*glb_policy)); |
| 532 | memset(glb_policy, 0, sizeof(*glb_policy)); |
| 533 | |
| 534 | /* All input addresses in args->addresses come from a resolver that claims |
| 535 | * they are LB services. It's the resolver's responsibility to make sure this |
| 536 | * policy is only instantiated and used in that case. |
| 537 | * |
| 538 | * Create a client channel over them to communicate with a LB service */ |
| 539 | glb_policy->cc_factory = args->client_channel_factory; |
| 540 | GPR_ASSERT(glb_policy->cc_factory != NULL); |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame^] | 541 | if (args->num_addresses == 0) { |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 542 | return NULL; |
| 543 | } |
| 544 | |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame^] | 545 | /* construct a target from the addresses in args, given in the form |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 546 | * ipvX://ip1:port1,ip2:port2,... |
| 547 | * TODO(dgq): support mixed ip version */ |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame^] | 548 | char **addr_strs = gpr_malloc(sizeof(char *) * args->num_addresses); |
| 549 | addr_strs[0] = grpc_sockaddr_to_uri( |
| 550 | (const struct sockaddr *)&args->lb_addresses[0].resolved_address->addr); |
| 551 | for (size_t i = 1; i < args->num_addresses; i++) { |
| 552 | GPR_ASSERT( |
| 553 | grpc_sockaddr_to_string(&addr_strs[i], |
| 554 | (const struct sockaddr *)&args->lb_addresses[i] |
| 555 | .resolved_address->addr, |
| 556 | true) == 0); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 557 | } |
| 558 | size_t uri_path_len; |
| 559 | char *target_uri_str = gpr_strjoin_sep( |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame^] | 560 | (const char **)addr_strs, args->num_addresses, ",", &uri_path_len); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 561 | |
| 562 | /* will pick using pick_first */ |
| 563 | glb_policy->lb_channel = grpc_client_channel_factory_create_channel( |
| 564 | exec_ctx, glb_policy->cc_factory, target_uri_str, |
| 565 | GRPC_CLIENT_CHANNEL_TYPE_LOAD_BALANCING, NULL); |
| 566 | |
| 567 | gpr_free(target_uri_str); |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame^] | 568 | for (size_t i = 0; i < args->num_addresses; i++) { |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 569 | gpr_free(addr_strs[i]); |
| 570 | } |
| 571 | gpr_free(addr_strs); |
| 572 | |
| 573 | if (glb_policy->lb_channel == NULL) { |
| 574 | gpr_free(glb_policy); |
| 575 | return NULL; |
| 576 | } |
| 577 | |
| 578 | rr_connectivity_data *rr_connectivity = |
| 579 | gpr_malloc(sizeof(rr_connectivity_data)); |
| 580 | memset(rr_connectivity, 0, sizeof(rr_connectivity_data)); |
David Garcia Quintas | 348cfdb | 2016-08-19 12:19:43 -0700 | [diff] [blame] | 581 | grpc_closure_init(&rr_connectivity->on_change, glb_rr_connectivity_changed, |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 582 | rr_connectivity); |
| 583 | rr_connectivity->glb_policy = glb_policy; |
| 584 | glb_policy->rr_connectivity = rr_connectivity; |
| 585 | |
| 586 | grpc_lb_policy_init(&glb_policy->base, &glb_lb_policy_vtable); |
| 587 | gpr_mu_init(&glb_policy->mu); |
| 588 | grpc_connectivity_state_init(&glb_policy->state_tracker, GRPC_CHANNEL_IDLE, |
| 589 | "grpclb"); |
| 590 | return &glb_policy->base; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 591 | } |
| 592 | |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 593 | static void glb_destroy(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) { |
| 594 | glb_lb_policy *glb_policy = (glb_lb_policy *)pol; |
| 595 | GPR_ASSERT(glb_policy->pending_picks == NULL); |
| 596 | GPR_ASSERT(glb_policy->pending_pings == NULL); |
| 597 | grpc_channel_destroy(glb_policy->lb_channel); |
| 598 | glb_policy->lb_channel = NULL; |
| 599 | grpc_connectivity_state_destroy(exec_ctx, &glb_policy->state_tracker); |
| 600 | if (glb_policy->serverlist != NULL) { |
| 601 | grpc_grpclb_destroy_serverlist(glb_policy->serverlist); |
| 602 | } |
| 603 | gpr_mu_destroy(&glb_policy->mu); |
| 604 | gpr_free(glb_policy); |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 605 | } |
| 606 | |
David Garcia Quintas | 740759e | 2016-08-01 14:49:49 -0700 | [diff] [blame] | 607 | static void lb_client_data_destroy(struct lb_client_data *lb_client); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 608 | static void glb_shutdown(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) { |
| 609 | glb_lb_policy *glb_policy = (glb_lb_policy *)pol; |
| 610 | gpr_mu_lock(&glb_policy->mu); |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 611 | |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 612 | pending_pick *pp = glb_policy->pending_picks; |
| 613 | glb_policy->pending_picks = NULL; |
| 614 | pending_ping *pping = glb_policy->pending_pings; |
| 615 | glb_policy->pending_pings = NULL; |
| 616 | gpr_mu_unlock(&glb_policy->mu); |
| 617 | |
| 618 | while (pp != NULL) { |
| 619 | pending_pick *next = pp->next; |
| 620 | *pp->target = NULL; |
| 621 | grpc_exec_ctx_sched(exec_ctx, &pp->wrapped_on_complete, GRPC_ERROR_NONE, |
| 622 | NULL); |
| 623 | gpr_free(pp); |
| 624 | pp = next; |
| 625 | } |
| 626 | |
| 627 | while (pping != NULL) { |
| 628 | pending_ping *next = pping->next; |
| 629 | grpc_exec_ctx_sched(exec_ctx, &pping->wrapped_notify, GRPC_ERROR_NONE, |
| 630 | NULL); |
| 631 | pping = next; |
| 632 | } |
| 633 | |
| 634 | if (glb_policy->rr_policy) { |
| 635 | /* unsubscribe */ |
| 636 | grpc_lb_policy_notify_on_state_change( |
| 637 | exec_ctx, glb_policy->rr_policy, NULL, |
| 638 | &glb_policy->rr_connectivity->on_change); |
| 639 | GRPC_LB_POLICY_UNREF(exec_ctx, glb_policy->rr_policy, "glb_shutdown"); |
| 640 | } |
| 641 | |
David Garcia Quintas | 740759e | 2016-08-01 14:49:49 -0700 | [diff] [blame] | 642 | lb_client_data_destroy(glb_policy->lb_client); |
| 643 | glb_policy->lb_client = NULL; |
| 644 | |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 645 | grpc_connectivity_state_set( |
| 646 | exec_ctx, &glb_policy->state_tracker, GRPC_CHANNEL_SHUTDOWN, |
| 647 | GRPC_ERROR_CREATE("Channel Shutdown"), "glb_shutdown"); |
| 648 | } |
| 649 | |
| 650 | static void glb_cancel_pick(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, |
| 651 | grpc_connected_subchannel **target) { |
| 652 | glb_lb_policy *glb_policy = (glb_lb_policy *)pol; |
| 653 | gpr_mu_lock(&glb_policy->mu); |
| 654 | pending_pick *pp = glb_policy->pending_picks; |
| 655 | glb_policy->pending_picks = NULL; |
| 656 | while (pp != NULL) { |
| 657 | pending_pick *next = pp->next; |
| 658 | if (pp->target == target) { |
| 659 | grpc_polling_entity_del_from_pollset_set( |
| 660 | exec_ctx, pp->pollent, glb_policy->base.interested_parties); |
| 661 | *target = NULL; |
| 662 | grpc_exec_ctx_sched(exec_ctx, &pp->wrapped_on_complete, |
| 663 | GRPC_ERROR_CANCELLED, NULL); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 664 | } else { |
| 665 | pp->next = glb_policy->pending_picks; |
| 666 | glb_policy->pending_picks = pp; |
| 667 | } |
| 668 | pp = next; |
| 669 | } |
| 670 | gpr_mu_unlock(&glb_policy->mu); |
| 671 | } |
| 672 | |
David Garcia Quintas | a0e278e | 2016-08-01 11:36:14 -0700 | [diff] [blame] | 673 | static grpc_call *lb_client_data_get_call(struct lb_client_data *lb_client); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 674 | static void glb_cancel_picks(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, |
| 675 | uint32_t initial_metadata_flags_mask, |
| 676 | uint32_t initial_metadata_flags_eq) { |
| 677 | glb_lb_policy *glb_policy = (glb_lb_policy *)pol; |
| 678 | gpr_mu_lock(&glb_policy->mu); |
| 679 | if (glb_policy->lb_client != NULL) { |
| 680 | /* cancel the call to the load balancer service, if any */ |
| 681 | grpc_call_cancel(lb_client_data_get_call(glb_policy->lb_client), NULL); |
| 682 | } |
| 683 | pending_pick *pp = glb_policy->pending_picks; |
| 684 | glb_policy->pending_picks = NULL; |
| 685 | while (pp != NULL) { |
| 686 | pending_pick *next = pp->next; |
| 687 | if ((pp->initial_metadata_flags & initial_metadata_flags_mask) == |
| 688 | initial_metadata_flags_eq) { |
| 689 | grpc_polling_entity_del_from_pollset_set( |
| 690 | exec_ctx, pp->pollent, glb_policy->base.interested_parties); |
| 691 | grpc_exec_ctx_sched(exec_ctx, &pp->wrapped_on_complete, |
| 692 | GRPC_ERROR_CANCELLED, NULL); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 693 | } else { |
| 694 | pp->next = glb_policy->pending_picks; |
| 695 | glb_policy->pending_picks = pp; |
| 696 | } |
| 697 | pp = next; |
| 698 | } |
| 699 | gpr_mu_unlock(&glb_policy->mu); |
| 700 | } |
David Garcia Quintas | 8d48911 | 2016-07-29 15:20:42 -0700 | [diff] [blame] | 701 | |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 702 | static void query_for_backends(grpc_exec_ctx *exec_ctx, |
| 703 | glb_lb_policy *glb_policy); |
| 704 | static void start_picking(grpc_exec_ctx *exec_ctx, glb_lb_policy *glb_policy) { |
| 705 | glb_policy->started_picking = true; |
| 706 | query_for_backends(exec_ctx, glb_policy); |
| 707 | } |
David Garcia Quintas | 8d48911 | 2016-07-29 15:20:42 -0700 | [diff] [blame] | 708 | |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 709 | static void glb_exit_idle(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) { |
| 710 | glb_lb_policy *glb_policy = (glb_lb_policy *)pol; |
| 711 | gpr_mu_lock(&glb_policy->mu); |
| 712 | if (!glb_policy->started_picking) { |
| 713 | start_picking(exec_ctx, glb_policy); |
| 714 | } |
| 715 | gpr_mu_unlock(&glb_policy->mu); |
| 716 | } |
David Garcia Quintas | 8d48911 | 2016-07-29 15:20:42 -0700 | [diff] [blame] | 717 | |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 718 | static int glb_pick(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, |
David Garcia Quintas | 8aace51 | 2016-08-15 14:55:12 -0700 | [diff] [blame] | 719 | const grpc_lb_policy_pick_args *pick_args, |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame^] | 720 | grpc_connected_subchannel **target, void **user_data, |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 721 | grpc_closure *on_complete) { |
| 722 | glb_lb_policy *glb_policy = (glb_lb_policy *)pol; |
David Garcia Quintas | 5b0e946 | 2016-08-15 19:38:39 -0700 | [diff] [blame] | 723 | |
| 724 | if (pick_args->lb_token_mdelem_storage == NULL) { |
| 725 | /* TODO(dgq): should this be an assert? If storage is NULL, something has |
| 726 | * gone very wrong at the client channel filter */ |
| 727 | gpr_log(GPR_ERROR, |
| 728 | "No mdelem storage for the LB token. Load reporting won't work " |
| 729 | "without it. Failing"); |
| 730 | *target = NULL; |
| 731 | grpc_exec_ctx_sched(exec_ctx, on_complete, GRPC_ERROR_NONE, NULL); |
| 732 | return 1; |
| 733 | } |
| 734 | |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 735 | gpr_mu_lock(&glb_policy->mu); |
| 736 | int r; |
| 737 | |
| 738 | if (glb_policy->rr_policy != NULL) { |
| 739 | if (grpc_lb_glb_trace) { |
| 740 | gpr_log(GPR_INFO, "about to PICK from 0x%" PRIxPTR "", |
| 741 | (intptr_t)glb_policy->rr_policy); |
| 742 | } |
| 743 | GRPC_LB_POLICY_REF(glb_policy->rr_policy, "glb_pick"); |
| 744 | memset(&glb_policy->wc_arg, 0, sizeof(wrapped_rr_closure_arg)); |
| 745 | glb_policy->wc_arg.rr_policy = glb_policy->rr_policy; |
| 746 | glb_policy->wc_arg.wrapped_closure = on_complete; |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame^] | 747 | glb_policy->wc_arg.lb_token_mdelem_storage = |
| 748 | pick_args->lb_token_mdelem_storage; |
| 749 | glb_policy->wc_arg.initial_metadata = pick_args->initial_metadata; |
| 750 | glb_policy->wc_arg.owning_pending_node = NULL; |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 751 | grpc_closure_init(&glb_policy->wrapped_on_complete, wrapped_rr_closure, |
| 752 | &glb_policy->wc_arg); |
David Garcia Quintas | 8aace51 | 2016-08-15 14:55:12 -0700 | [diff] [blame] | 753 | |
| 754 | r = grpc_lb_policy_pick(exec_ctx, glb_policy->rr_policy, pick_args, target, |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame^] | 755 | (void **)&glb_policy->wc_arg.lb_token, |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 756 | &glb_policy->wrapped_on_complete); |
| 757 | if (r != 0) { |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame^] | 758 | /* synchronous grpc_lb_policy_pick call. Unref the RR policy. */ |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 759 | if (grpc_lb_glb_trace) { |
| 760 | gpr_log(GPR_INFO, "Unreffing RR (0x%" PRIxPTR ")", |
| 761 | (intptr_t)glb_policy->wc_arg.rr_policy); |
| 762 | } |
| 763 | GRPC_LB_POLICY_UNREF(exec_ctx, glb_policy->wc_arg.rr_policy, "glb_pick"); |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame^] | 764 | |
| 765 | /* add the load reporting initial metadata */ |
| 766 | initial_metadata_add_lb_token(pick_args->initial_metadata, |
| 767 | pick_args->lb_token_mdelem_storage, |
| 768 | glb_policy->wc_arg.lb_token); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 769 | } |
| 770 | } else { |
David Garcia Quintas | 8aace51 | 2016-08-15 14:55:12 -0700 | [diff] [blame] | 771 | grpc_polling_entity_add_to_pollset_set(exec_ctx, pick_args->pollent, |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 772 | glb_policy->base.interested_parties); |
David Garcia Quintas | 8aace51 | 2016-08-15 14:55:12 -0700 | [diff] [blame] | 773 | add_pending_pick(&glb_policy->pending_picks, pick_args, target, |
| 774 | on_complete); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 775 | |
| 776 | if (!glb_policy->started_picking) { |
| 777 | start_picking(exec_ctx, glb_policy); |
| 778 | } |
| 779 | r = 0; |
| 780 | } |
| 781 | gpr_mu_unlock(&glb_policy->mu); |
| 782 | return r; |
| 783 | } |
David Garcia Quintas | 8d48911 | 2016-07-29 15:20:42 -0700 | [diff] [blame] | 784 | |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 785 | static grpc_connectivity_state glb_check_connectivity( |
| 786 | grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, |
| 787 | grpc_error **connectivity_error) { |
| 788 | glb_lb_policy *glb_policy = (glb_lb_policy *)pol; |
| 789 | grpc_connectivity_state st; |
| 790 | gpr_mu_lock(&glb_policy->mu); |
| 791 | st = grpc_connectivity_state_check(&glb_policy->state_tracker, |
| 792 | connectivity_error); |
| 793 | gpr_mu_unlock(&glb_policy->mu); |
| 794 | return st; |
| 795 | } |
David Garcia Quintas | 8d48911 | 2016-07-29 15:20:42 -0700 | [diff] [blame] | 796 | |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 797 | static void glb_ping_one(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, |
| 798 | grpc_closure *closure) { |
| 799 | glb_lb_policy *glb_policy = (glb_lb_policy *)pol; |
| 800 | gpr_mu_lock(&glb_policy->mu); |
| 801 | if (glb_policy->rr_policy) { |
| 802 | grpc_lb_policy_ping_one(exec_ctx, glb_policy->rr_policy, closure); |
| 803 | } else { |
| 804 | add_pending_ping(&glb_policy->pending_pings, closure); |
| 805 | if (!glb_policy->started_picking) { |
| 806 | start_picking(exec_ctx, glb_policy); |
| 807 | } |
| 808 | } |
| 809 | gpr_mu_unlock(&glb_policy->mu); |
| 810 | } |
David Garcia Quintas | 8d48911 | 2016-07-29 15:20:42 -0700 | [diff] [blame] | 811 | |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 812 | static void glb_notify_on_state_change(grpc_exec_ctx *exec_ctx, |
| 813 | grpc_lb_policy *pol, |
| 814 | grpc_connectivity_state *current, |
| 815 | grpc_closure *notify) { |
| 816 | glb_lb_policy *glb_policy = (glb_lb_policy *)pol; |
| 817 | gpr_mu_lock(&glb_policy->mu); |
| 818 | grpc_connectivity_state_notify_on_state_change( |
| 819 | exec_ctx, &glb_policy->state_tracker, current, notify); |
| 820 | |
| 821 | gpr_mu_unlock(&glb_policy->mu); |
| 822 | } |
| 823 | |
David Garcia Quintas | 8d48911 | 2016-07-29 15:20:42 -0700 | [diff] [blame] | 824 | /* |
| 825 | * lb_client_data |
| 826 | * |
| 827 | * Used internally for the client call to the LB */ |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 828 | typedef struct lb_client_data { |
| 829 | gpr_mu mu; |
| 830 | |
| 831 | /* called once initial metadata's been sent */ |
| 832 | grpc_closure md_sent; |
| 833 | |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 834 | /* called once the LoadBalanceRequest has been sent to the LB server. See |
| 835 | * src/proto/grpc/.../load_balancer.proto */ |
| 836 | grpc_closure req_sent; |
| 837 | |
| 838 | /* A response from the LB server has been received (or error). Process it */ |
| 839 | grpc_closure res_rcvd; |
| 840 | |
| 841 | /* After the client has sent a close to the LB server */ |
| 842 | grpc_closure close_sent; |
| 843 | |
| 844 | /* ... and the status from the LB server has been received */ |
| 845 | grpc_closure srv_status_rcvd; |
| 846 | |
| 847 | grpc_call *lb_call; /* streaming call to the LB server, */ |
| 848 | gpr_timespec deadline; /* for the streaming call to the LB server */ |
| 849 | |
| 850 | grpc_metadata_array initial_metadata_recv; /* initial MD from LB server */ |
| 851 | grpc_metadata_array trailing_metadata_recv; /* trailing MD from LB server */ |
| 852 | |
| 853 | /* what's being sent to the LB server. Note that its value may vary if the LB |
| 854 | * server indicates a redirect. */ |
| 855 | grpc_byte_buffer *request_payload; |
| 856 | |
| 857 | /* response from the LB server, if any. Processed in res_recv_cb() */ |
| 858 | grpc_byte_buffer *response_payload; |
| 859 | |
| 860 | /* the call's status and status detailset in srv_status_rcvd_cb() */ |
| 861 | grpc_status_code status; |
| 862 | char *status_details; |
| 863 | size_t status_details_capacity; |
| 864 | |
| 865 | /* pointer back to the enclosing policy */ |
| 866 | glb_lb_policy *glb_policy; |
| 867 | } lb_client_data; |
| 868 | |
| 869 | static void md_sent_cb(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 870 | static void req_sent_cb(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 871 | static void res_recv_cb(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error); |
| 872 | static void close_sent_cb(grpc_exec_ctx *exec_ctx, void *arg, |
| 873 | grpc_error *error); |
| 874 | static void srv_status_rcvd_cb(grpc_exec_ctx *exec_ctx, void *arg, |
| 875 | grpc_error *error); |
| 876 | |
| 877 | static lb_client_data *lb_client_data_create(glb_lb_policy *glb_policy) { |
| 878 | lb_client_data *lb_client = gpr_malloc(sizeof(lb_client_data)); |
| 879 | memset(lb_client, 0, sizeof(lb_client_data)); |
| 880 | |
| 881 | gpr_mu_init(&lb_client->mu); |
| 882 | grpc_closure_init(&lb_client->md_sent, md_sent_cb, lb_client); |
| 883 | |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 884 | grpc_closure_init(&lb_client->req_sent, req_sent_cb, lb_client); |
| 885 | grpc_closure_init(&lb_client->res_rcvd, res_recv_cb, lb_client); |
| 886 | grpc_closure_init(&lb_client->close_sent, close_sent_cb, lb_client); |
| 887 | grpc_closure_init(&lb_client->srv_status_rcvd, srv_status_rcvd_cb, lb_client); |
| 888 | |
| 889 | /* TODO(dgq): get the deadline from the client config instead of fabricating |
| 890 | * one here. */ |
| 891 | lb_client->deadline = gpr_time_add(gpr_now(GPR_CLOCK_MONOTONIC), |
| 892 | gpr_time_from_seconds(3, GPR_TIMESPAN)); |
| 893 | |
David Garcia Quintas | 15eba13 | 2016-08-09 15:20:48 -0700 | [diff] [blame] | 894 | /* Note the following LB call progresses every time there's activity in \a |
| 895 | * glb_policy->base.interested_parties, which is comprised of the polling |
| 896 | * entities passed to glb_pick(). */ |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 897 | lb_client->lb_call = grpc_channel_create_pollset_set_call( |
| 898 | glb_policy->lb_channel, NULL, GRPC_PROPAGATE_DEFAULTS, |
| 899 | glb_policy->base.interested_parties, "/BalanceLoad", |
| 900 | NULL, /* FIXME(dgq): which "host" value to use? */ |
| 901 | lb_client->deadline, NULL); |
| 902 | |
| 903 | grpc_metadata_array_init(&lb_client->initial_metadata_recv); |
| 904 | grpc_metadata_array_init(&lb_client->trailing_metadata_recv); |
| 905 | |
| 906 | grpc_grpclb_request *request = grpc_grpclb_request_create( |
| 907 | "load.balanced.service.name"); /* FIXME(dgq): get the name of the load |
| 908 | balanced service from the resolver */ |
| 909 | gpr_slice request_payload_slice = grpc_grpclb_request_encode(request); |
| 910 | lb_client->request_payload = |
| 911 | grpc_raw_byte_buffer_create(&request_payload_slice, 1); |
| 912 | gpr_slice_unref(request_payload_slice); |
| 913 | grpc_grpclb_request_destroy(request); |
| 914 | |
| 915 | lb_client->status_details = NULL; |
| 916 | lb_client->status_details_capacity = 0; |
| 917 | lb_client->glb_policy = glb_policy; |
| 918 | return lb_client; |
| 919 | } |
David Garcia Quintas | 8d48911 | 2016-07-29 15:20:42 -0700 | [diff] [blame] | 920 | |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 921 | static void lb_client_data_destroy(lb_client_data *lb_client) { |
David Garcia Quintas | 740759e | 2016-08-01 14:49:49 -0700 | [diff] [blame] | 922 | grpc_call_destroy(lb_client->lb_call); |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 923 | grpc_metadata_array_destroy(&lb_client->initial_metadata_recv); |
| 924 | grpc_metadata_array_destroy(&lb_client->trailing_metadata_recv); |
| 925 | |
| 926 | grpc_byte_buffer_destroy(lb_client->request_payload); |
| 927 | |
| 928 | gpr_free(lb_client->status_details); |
| 929 | gpr_mu_destroy(&lb_client->mu); |
| 930 | gpr_free(lb_client); |
| 931 | } |
| 932 | static grpc_call *lb_client_data_get_call(lb_client_data *lb_client) { |
| 933 | return lb_client->lb_call; |
| 934 | } |
| 935 | |
David Garcia Quintas | 8d48911 | 2016-07-29 15:20:42 -0700 | [diff] [blame] | 936 | /* |
| 937 | * Auxiliary functions and LB client callbacks. |
| 938 | */ |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 939 | static void query_for_backends(grpc_exec_ctx *exec_ctx, |
| 940 | glb_lb_policy *glb_policy) { |
| 941 | GPR_ASSERT(glb_policy->lb_channel != NULL); |
| 942 | |
| 943 | glb_policy->lb_client = lb_client_data_create(glb_policy); |
| 944 | grpc_call_error call_error; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 945 | grpc_op ops[1]; |
| 946 | memset(ops, 0, sizeof(ops)); |
| 947 | grpc_op *op = ops; |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 948 | op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 949 | op->data.send_initial_metadata.count = 0; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 950 | op->flags = 0; |
| 951 | op->reserved = NULL; |
| 952 | op++; |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 953 | call_error = grpc_call_start_batch_and_execute( |
| 954 | exec_ctx, glb_policy->lb_client->lb_call, ops, (size_t)(op - ops), |
| 955 | &glb_policy->lb_client->md_sent); |
| 956 | GPR_ASSERT(GRPC_CALL_OK == call_error); |
| 957 | |
| 958 | op = ops; |
| 959 | op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
| 960 | op->data.recv_status_on_client.trailing_metadata = |
| 961 | &glb_policy->lb_client->trailing_metadata_recv; |
| 962 | op->data.recv_status_on_client.status = &glb_policy->lb_client->status; |
| 963 | op->data.recv_status_on_client.status_details = |
| 964 | &glb_policy->lb_client->status_details; |
| 965 | op->data.recv_status_on_client.status_details_capacity = |
| 966 | &glb_policy->lb_client->status_details_capacity; |
| 967 | op->flags = 0; |
| 968 | op->reserved = NULL; |
| 969 | op++; |
| 970 | call_error = grpc_call_start_batch_and_execute( |
| 971 | exec_ctx, glb_policy->lb_client->lb_call, ops, (size_t)(op - ops), |
| 972 | &glb_policy->lb_client->srv_status_rcvd); |
David Garcia Quintas | 280fd2a | 2016-06-20 22:04:48 -0700 | [diff] [blame] | 973 | GPR_ASSERT(GRPC_CALL_OK == call_error); |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 974 | } |
| 975 | |
David Garcia Quintas | 4166cb0 | 2016-07-29 14:33:15 -0700 | [diff] [blame] | 976 | static void md_sent_cb(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error) { |
| 977 | lb_client_data *lb_client = arg; |
| 978 | GPR_ASSERT(lb_client->lb_call); |
| 979 | grpc_op ops[1]; |
| 980 | memset(ops, 0, sizeof(ops)); |
| 981 | grpc_op *op = ops; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 982 | |
| 983 | op->op = GRPC_OP_SEND_MESSAGE; |
David Garcia Quintas | 41bef45 | 2016-07-28 19:19:58 -0700 | [diff] [blame] | 984 | op->data.send_message = lb_client->request_payload; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 985 | op->flags = 0; |
| 986 | op->reserved = NULL; |
| 987 | op++; |
David Garcia Quintas | 41bef45 | 2016-07-28 19:19:58 -0700 | [diff] [blame] | 988 | grpc_call_error call_error = grpc_call_start_batch_and_execute( |
| 989 | exec_ctx, lb_client->lb_call, ops, (size_t)(op - ops), |
| 990 | &lb_client->req_sent); |
David Garcia Quintas | 280fd2a | 2016-06-20 22:04:48 -0700 | [diff] [blame] | 991 | GPR_ASSERT(GRPC_CALL_OK == call_error); |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 992 | } |
| 993 | |
David Garcia Quintas | 280fd2a | 2016-06-20 22:04:48 -0700 | [diff] [blame] | 994 | static void req_sent_cb(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error) { |
David Garcia Quintas | 41bef45 | 2016-07-28 19:19:58 -0700 | [diff] [blame] | 995 | lb_client_data *lb_client = arg; |
David Garcia Quintas | 601bb12 | 2016-08-18 15:03:59 -0700 | [diff] [blame] | 996 | GPR_ASSERT(lb_client->lb_call); |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 997 | |
David Garcia Quintas | 601bb12 | 2016-08-18 15:03:59 -0700 | [diff] [blame] | 998 | grpc_op ops[2]; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 999 | memset(ops, 0, sizeof(ops)); |
| 1000 | grpc_op *op = ops; |
| 1001 | |
David Garcia Quintas | 601bb12 | 2016-08-18 15:03:59 -0700 | [diff] [blame] | 1002 | op->op = GRPC_OP_RECV_INITIAL_METADATA; |
| 1003 | op->data.recv_initial_metadata = &lb_client->initial_metadata_recv; |
| 1004 | op->flags = 0; |
| 1005 | op->reserved = NULL; |
| 1006 | op++; |
| 1007 | |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1008 | op->op = GRPC_OP_RECV_MESSAGE; |
David Garcia Quintas | 41bef45 | 2016-07-28 19:19:58 -0700 | [diff] [blame] | 1009 | op->data.recv_message = &lb_client->response_payload; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1010 | op->flags = 0; |
| 1011 | op->reserved = NULL; |
| 1012 | op++; |
David Garcia Quintas | 41bef45 | 2016-07-28 19:19:58 -0700 | [diff] [blame] | 1013 | grpc_call_error call_error = grpc_call_start_batch_and_execute( |
| 1014 | exec_ctx, lb_client->lb_call, ops, (size_t)(op - ops), |
| 1015 | &lb_client->res_rcvd); |
David Garcia Quintas | 280fd2a | 2016-06-20 22:04:48 -0700 | [diff] [blame] | 1016 | GPR_ASSERT(GRPC_CALL_OK == call_error); |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1017 | } |
| 1018 | |
David Garcia Quintas | 6531826 | 2016-07-29 13:43:38 -0700 | [diff] [blame] | 1019 | static void res_recv_cb(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error) { |
David Garcia Quintas | 41bef45 | 2016-07-28 19:19:58 -0700 | [diff] [blame] | 1020 | lb_client_data *lb_client = arg; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1021 | grpc_op ops[2]; |
| 1022 | memset(ops, 0, sizeof(ops)); |
| 1023 | grpc_op *op = ops; |
David Garcia Quintas | 41bef45 | 2016-07-28 19:19:58 -0700 | [diff] [blame] | 1024 | if (lb_client->response_payload != NULL) { |
| 1025 | /* Received data from the LB server. Look inside |
David Garcia Quintas | 601bb12 | 2016-08-18 15:03:59 -0700 | [diff] [blame] | 1026 | * lb_client->response_payload, for a serverlist. */ |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1027 | grpc_byte_buffer_reader bbr; |
David Garcia Quintas | 41bef45 | 2016-07-28 19:19:58 -0700 | [diff] [blame] | 1028 | grpc_byte_buffer_reader_init(&bbr, lb_client->response_payload); |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1029 | gpr_slice response_slice = grpc_byte_buffer_reader_readall(&bbr); |
David Garcia Quintas | 41bef45 | 2016-07-28 19:19:58 -0700 | [diff] [blame] | 1030 | grpc_byte_buffer_destroy(lb_client->response_payload); |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1031 | grpc_grpclb_serverlist *serverlist = |
| 1032 | grpc_grpclb_response_parse_serverlist(response_slice); |
David Garcia Quintas | ea11d16 | 2016-07-14 17:27:28 -0700 | [diff] [blame] | 1033 | if (serverlist != NULL) { |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1034 | gpr_slice_unref(response_slice); |
| 1035 | if (grpc_lb_glb_trace) { |
| 1036 | gpr_log(GPR_INFO, "Serverlist with %zu servers received", |
| 1037 | serverlist->num_servers); |
| 1038 | } |
David Garcia Quintas | ea11d16 | 2016-07-14 17:27:28 -0700 | [diff] [blame] | 1039 | |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1040 | /* update serverlist */ |
| 1041 | if (serverlist->num_servers > 0) { |
David Garcia Quintas | 41bef45 | 2016-07-28 19:19:58 -0700 | [diff] [blame] | 1042 | if (grpc_grpclb_serverlist_equals(lb_client->glb_policy->serverlist, |
| 1043 | serverlist)) { |
David Garcia Quintas | ea11d16 | 2016-07-14 17:27:28 -0700 | [diff] [blame] | 1044 | if (grpc_lb_glb_trace) { |
| 1045 | gpr_log(GPR_INFO, |
| 1046 | "Incoming server list identical to current, ignoring."); |
| 1047 | } |
| 1048 | } else { /* new serverlist */ |
David Garcia Quintas | 41bef45 | 2016-07-28 19:19:58 -0700 | [diff] [blame] | 1049 | if (lb_client->glb_policy->serverlist != NULL) { |
David Garcia Quintas | ea11d16 | 2016-07-14 17:27:28 -0700 | [diff] [blame] | 1050 | /* dispose of the old serverlist */ |
David Garcia Quintas | 41bef45 | 2016-07-28 19:19:58 -0700 | [diff] [blame] | 1051 | grpc_grpclb_destroy_serverlist(lb_client->glb_policy->serverlist); |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1052 | } |
David Garcia Quintas | ea11d16 | 2016-07-14 17:27:28 -0700 | [diff] [blame] | 1053 | /* and update the copy in the glb_lb_policy instance */ |
David Garcia Quintas | 41bef45 | 2016-07-28 19:19:58 -0700 | [diff] [blame] | 1054 | lb_client->glb_policy->serverlist = serverlist; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1055 | } |
David Garcia Quintas | 41bef45 | 2016-07-28 19:19:58 -0700 | [diff] [blame] | 1056 | if (lb_client->glb_policy->rr_policy == NULL) { |
David Garcia Quintas | ea11d16 | 2016-07-14 17:27:28 -0700 | [diff] [blame] | 1057 | /* initial "handover", in this case from a null RR policy, meaning |
David Garcia Quintas | 4333984 | 2016-07-18 12:56:09 -0700 | [diff] [blame] | 1058 | * it'll just create the first RR policy instance */ |
David Garcia Quintas | 41bef45 | 2016-07-28 19:19:58 -0700 | [diff] [blame] | 1059 | rr_handover(exec_ctx, lb_client->glb_policy, error); |
David Garcia Quintas | ea11d16 | 2016-07-14 17:27:28 -0700 | [diff] [blame] | 1060 | } else { |
| 1061 | /* unref the RR policy, eventually leading to its substitution with a |
| 1062 | * new one constructed from the received serverlist (see |
David Garcia Quintas | 348cfdb | 2016-08-19 12:19:43 -0700 | [diff] [blame] | 1063 | * glb_rr_connectivity_changed) */ |
David Garcia Quintas | 41bef45 | 2016-07-28 19:19:58 -0700 | [diff] [blame] | 1064 | GRPC_LB_POLICY_UNREF(exec_ctx, lb_client->glb_policy->rr_policy, |
David Garcia Quintas | ea11d16 | 2016-07-14 17:27:28 -0700 | [diff] [blame] | 1065 | "serverlist_received"); |
| 1066 | } |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1067 | } else { |
David Garcia Quintas | ea11d16 | 2016-07-14 17:27:28 -0700 | [diff] [blame] | 1068 | if (grpc_lb_glb_trace) { |
| 1069 | gpr_log(GPR_INFO, |
| 1070 | "Received empty server list. Picks will stay pending until a " |
| 1071 | "response with > 0 servers is received"); |
| 1072 | } |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1073 | } |
| 1074 | |
David Garcia Quintas | ea11d16 | 2016-07-14 17:27:28 -0700 | [diff] [blame] | 1075 | /* keep listening for serverlist updates */ |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1076 | op->op = GRPC_OP_RECV_MESSAGE; |
David Garcia Quintas | 41bef45 | 2016-07-28 19:19:58 -0700 | [diff] [blame] | 1077 | op->data.recv_message = &lb_client->response_payload; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1078 | op->flags = 0; |
| 1079 | op->reserved = NULL; |
| 1080 | op++; |
David Garcia Quintas | 280fd2a | 2016-06-20 22:04:48 -0700 | [diff] [blame] | 1081 | const grpc_call_error call_error = grpc_call_start_batch_and_execute( |
David Garcia Quintas | 41bef45 | 2016-07-28 19:19:58 -0700 | [diff] [blame] | 1082 | exec_ctx, lb_client->lb_call, ops, (size_t)(op - ops), |
| 1083 | &lb_client->res_rcvd); /* loop */ |
David Garcia Quintas | 280fd2a | 2016-06-20 22:04:48 -0700 | [diff] [blame] | 1084 | GPR_ASSERT(GRPC_CALL_OK == call_error); |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1085 | return; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1086 | } |
David Garcia Quintas | ea11d16 | 2016-07-14 17:27:28 -0700 | [diff] [blame] | 1087 | |
| 1088 | GPR_ASSERT(serverlist == NULL); |
| 1089 | gpr_log(GPR_ERROR, "Invalid LB response received: '%s'", |
| 1090 | gpr_dump_slice(response_slice, GPR_DUMP_ASCII)); |
| 1091 | gpr_slice_unref(response_slice); |
| 1092 | |
| 1093 | /* Disconnect from server returning invalid response. */ |
| 1094 | op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
| 1095 | op->flags = 0; |
| 1096 | op->reserved = NULL; |
| 1097 | op++; |
| 1098 | grpc_call_error call_error = grpc_call_start_batch_and_execute( |
David Garcia Quintas | 41bef45 | 2016-07-28 19:19:58 -0700 | [diff] [blame] | 1099 | exec_ctx, lb_client->lb_call, ops, (size_t)(op - ops), |
| 1100 | &lb_client->close_sent); |
David Garcia Quintas | ea11d16 | 2016-07-14 17:27:28 -0700 | [diff] [blame] | 1101 | GPR_ASSERT(GRPC_CALL_OK == call_error); |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1102 | } |
| 1103 | /* empty payload: call cancelled by server. Cleanups happening in |
| 1104 | * srv_status_rcvd_cb */ |
| 1105 | } |
David Garcia Quintas | ea11d16 | 2016-07-14 17:27:28 -0700 | [diff] [blame] | 1106 | |
David Garcia Quintas | 280fd2a | 2016-06-20 22:04:48 -0700 | [diff] [blame] | 1107 | static void close_sent_cb(grpc_exec_ctx *exec_ctx, void *arg, |
| 1108 | grpc_error *error) { |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1109 | if (grpc_lb_glb_trace) { |
| 1110 | gpr_log(GPR_INFO, |
| 1111 | "Close from LB client sent. Waiting from server status now"); |
| 1112 | } |
| 1113 | } |
David Garcia Quintas | ea11d16 | 2016-07-14 17:27:28 -0700 | [diff] [blame] | 1114 | |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1115 | static void srv_status_rcvd_cb(grpc_exec_ctx *exec_ctx, void *arg, |
David Garcia Quintas | 280fd2a | 2016-06-20 22:04:48 -0700 | [diff] [blame] | 1116 | grpc_error *error) { |
David Garcia Quintas | 41bef45 | 2016-07-28 19:19:58 -0700 | [diff] [blame] | 1117 | lb_client_data *lb_client = arg; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1118 | if (grpc_lb_glb_trace) { |
David Garcia Quintas | ea11d16 | 2016-07-14 17:27:28 -0700 | [diff] [blame] | 1119 | gpr_log(GPR_INFO, |
| 1120 | "status from lb server received. Status = %d, Details = '%s', " |
| 1121 | "Capaticy " |
| 1122 | "= %zu", |
David Garcia Quintas | 41bef45 | 2016-07-28 19:19:58 -0700 | [diff] [blame] | 1123 | lb_client->status, lb_client->status_details, |
| 1124 | lb_client->status_details_capacity); |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1125 | } |
David Garcia Quintas | 4333984 | 2016-07-18 12:56:09 -0700 | [diff] [blame] | 1126 | /* TODO(dgq): deal with stream termination properly (fire up another one? fail |
| 1127 | * the original call?) */ |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1128 | } |
| 1129 | |
David Garcia Quintas | 8d48911 | 2016-07-29 15:20:42 -0700 | [diff] [blame] | 1130 | /* Code wiring the policy with the rest of the core */ |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1131 | static const grpc_lb_policy_vtable glb_lb_policy_vtable = { |
| 1132 | glb_destroy, glb_shutdown, glb_pick, |
| 1133 | glb_cancel_pick, glb_cancel_picks, glb_ping_one, |
| 1134 | glb_exit_idle, glb_check_connectivity, glb_notify_on_state_change}; |
| 1135 | |
| 1136 | static void glb_factory_ref(grpc_lb_policy_factory *factory) {} |
| 1137 | |
| 1138 | static void glb_factory_unref(grpc_lb_policy_factory *factory) {} |
| 1139 | |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1140 | static const grpc_lb_policy_factory_vtable glb_factory_vtable = { |
| 1141 | glb_factory_ref, glb_factory_unref, glb_create, "grpclb"}; |
| 1142 | |
| 1143 | static grpc_lb_policy_factory glb_lb_policy_factory = {&glb_factory_vtable}; |
| 1144 | |
| 1145 | grpc_lb_policy_factory *grpc_glb_lb_factory_create() { |
| 1146 | return &glb_lb_policy_factory; |
| 1147 | } |
| 1148 | |
| 1149 | /* Plugin registration */ |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 1150 | void grpc_lb_policy_grpclb_init() { |
| 1151 | grpc_register_lb_policy(grpc_glb_lb_factory_create()); |
| 1152 | grpc_register_tracer("glb", &grpc_lb_glb_trace); |
| 1153 | } |
| 1154 | |
| 1155 | void grpc_lb_policy_grpclb_shutdown() {} |