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