blob: bbf0548405229f7278fc68a44d7f29ae66be83e9 [file] [log] [blame]
Craig Tiller3bc8ebd2015-06-24 15:41:15 -07001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Craig Tiller3bc8ebd2015-06-24 15:41:15 -07004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
Craig Tiller3bc8ebd2015-06-24 15:41:15 -07008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Craig Tiller3bc8ebd2015-06-24 15:41:15 -070010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
Craig Tiller3bc8ebd2015-06-24 15:41:15 -070016 *
17 */
18
Craig Tillereb3b12e2015-06-26 14:42:49 -070019#include <string.h>
20
21#include <grpc/support/alloc.h>
Mark D. Roth5bd7be02016-10-21 14:19:50 -070022
Mark D. Roth5e9848e2017-10-06 13:59:32 -070023#include "src/core/ext/filters/client_channel/lb_policy/subchannel_list.h"
Craig Tiller9eb0fde2017-03-31 16:59:30 -070024#include "src/core/ext/filters/client_channel/lb_policy_registry.h"
25#include "src/core/ext/filters/client_channel/subchannel.h"
David Garcia Quintas87d5a312017-06-06 19:45:58 -070026#include "src/core/ext/filters/client_channel/subchannel_index.h"
Mark D. Roth557c9902016-10-24 11:12:05 -070027#include "src/core/lib/channel/channel_args.h"
Craig Tiller2400bf52017-02-09 16:25:19 -080028#include "src/core/lib/iomgr/combiner.h"
Mark D. Roth0748f392017-01-13 09:22:44 -080029#include "src/core/lib/iomgr/sockaddr_utils.h"
Craig Tiller9533d042016-03-25 17:11:06 -070030#include "src/core/lib/transport/connectivity_state.h"
Craig Tillereb3b12e2015-06-26 14:42:49 -070031
ncteisen7712c7c2017-07-12 23:11:27 -070032grpc_tracer_flag grpc_lb_pick_first_trace =
33 GRPC_TRACER_INITIALIZER(false, "pick_first");
David Garcia Quintas87d5a312017-06-06 19:45:58 -070034
Craig Tillera82950e2015-09-22 12:33:20 -070035typedef struct pending_pick {
Craig Tillereb3b12e2015-06-26 14:42:49 -070036 struct pending_pick *next;
Craig Tiller8c0d96f2016-03-11 14:27:52 -080037 uint32_t initial_metadata_flags;
Craig Tillerb5585d42015-11-17 07:18:31 -080038 grpc_connected_subchannel **target;
Craig Tiller33825112015-09-18 07:44:19 -070039 grpc_closure *on_complete;
Craig Tillereb3b12e2015-06-26 14:42:49 -070040} pending_pick;
41
Craig Tillera82950e2015-09-22 12:33:20 -070042typedef struct {
Craig Tillereb3b12e2015-06-26 14:42:49 -070043 /** base policy: must be first */
44 grpc_lb_policy base;
Craig Tillereb3b12e2015-06-26 14:42:49 -070045 /** all our subchannels */
Mark D. Roth5e9848e2017-10-06 13:59:32 -070046 grpc_lb_subchannel_list *subchannel_list;
47 /** Latest pending subchannel list. */
48 grpc_lb_subchannel_list *latest_pending_subchannel_list;
49 /** Selected subchannel in \a subchannel_list. */
50 grpc_lb_subchannel_data *selected;
Craig Tillereb3b12e2015-06-26 14:42:49 -070051 /** have we started picking? */
David Garcia Quintas87d5a312017-06-06 19:45:58 -070052 bool started_picking;
Craig Tillera14215a2015-07-17 17:21:08 -070053 /** are we shut down? */
David Garcia Quintas87d5a312017-06-06 19:45:58 -070054 bool shutdown;
Craig Tillereb3b12e2015-06-26 14:42:49 -070055 /** list of picks that are waiting on connectivity */
56 pending_pick *pending_picks;
Craig Tillerc7b5f762015-06-27 11:48:42 -070057 /** our connectivity state tracker */
58 grpc_connectivity_state_tracker state_tracker;
Craig Tillereb3b12e2015-06-26 14:42:49 -070059} pick_first_lb_policy;
60
Craig Tillerfb433852016-03-29 08:51:07 -070061static void pf_destroy(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) {
Craig Tillera82950e2015-09-22 12:33:20 -070062 pick_first_lb_policy *p = (pick_first_lb_policy *)pol;
Mark D. Roth5e9848e2017-10-06 13:59:32 -070063 GPR_ASSERT(p->subchannel_list == NULL);
64 GPR_ASSERT(p->latest_pending_subchannel_list == NULL);
Craig Tillera82950e2015-09-22 12:33:20 -070065 GPR_ASSERT(p->pending_picks == NULL);
Craig Tillera82950e2015-09-22 12:33:20 -070066 grpc_connectivity_state_destroy(exec_ctx, &p->state_tracker);
Craig Tillera82950e2015-09-22 12:33:20 -070067 gpr_free(p);
Mark D. Roth5e9848e2017-10-06 13:59:32 -070068 grpc_subchannel_index_unref();
David Garcia Quintasaf084dc2017-06-27 13:42:54 -070069 if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) {
70 gpr_log(GPR_DEBUG, "Pick First %p destroyed.", (void *)p);
71 }
Craig Tillereb3b12e2015-06-26 14:42:49 -070072}
73
Mark D. Roth5e9848e2017-10-06 13:59:32 -070074static void shutdown_locked(grpc_exec_ctx *exec_ctx, pick_first_lb_policy *p,
75 grpc_error *error) {
76 if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) {
77 gpr_log(GPR_DEBUG, "Pick First %p Shutting down", p);
78 }
David Garcia Quintas87d5a312017-06-06 19:45:58 -070079 p->shutdown = true;
Mark D. Roth5e9848e2017-10-06 13:59:32 -070080 pending_pick *pp;
81 while ((pp = p->pending_picks) != NULL) {
82 p->pending_picks = pp->next;
83 *pp->target = NULL;
84 GRPC_CLOSURE_SCHED(exec_ctx, pp->on_complete, GRPC_ERROR_REF(error));
85 gpr_free(pp);
86 }
Craig Tiller804ff712016-05-05 16:25:40 -070087 grpc_connectivity_state_set(
Craig Tillerd925c932016-06-06 08:38:50 -070088 exec_ctx, &p->state_tracker, GRPC_CHANNEL_SHUTDOWN,
Mark D. Roth5e9848e2017-10-06 13:59:32 -070089 GRPC_ERROR_REF(error), "shutdown");
90 if (p->subchannel_list != NULL) {
91 grpc_lb_subchannel_list_shutdown_and_unref(exec_ctx, p->subchannel_list,
92 "pf_shutdown");
93 p->subchannel_list = NULL;
Craig Tiller48613042015-11-29 14:45:11 -080094 }
Mark D. Roth5e9848e2017-10-06 13:59:32 -070095 if (p->latest_pending_subchannel_list != NULL) {
96 grpc_lb_subchannel_list_shutdown_and_unref(
97 exec_ctx, p->latest_pending_subchannel_list, "pf_shutdown");
98 p->latest_pending_subchannel_list = NULL;
Craig Tillera82950e2015-09-22 12:33:20 -070099 }
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700100 GRPC_ERROR_UNREF(error);
101}
102
103static void pf_shutdown_locked(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) {
104 shutdown_locked(exec_ctx, (pick_first_lb_policy *)pol,
105 GRPC_ERROR_CREATE_FROM_STATIC_STRING("Channel shutdown"));
Craig Tillereb3b12e2015-06-26 14:42:49 -0700106}
107
Craig Tiller2400bf52017-02-09 16:25:19 -0800108static void pf_cancel_pick_locked(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol,
109 grpc_connected_subchannel **target,
110 grpc_error *error) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800111 pick_first_lb_policy *p = (pick_first_lb_policy *)pol;
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700112 pending_pick *pp = p->pending_picks;
Craig Tiller577c9b22015-11-02 14:11:15 -0800113 p->pending_picks = NULL;
114 while (pp != NULL) {
115 pending_pick *next = pp->next;
116 if (pp->target == target) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800117 *target = NULL;
ncteisen274bbbe2017-06-08 14:57:11 -0700118 GRPC_CLOSURE_SCHED(exec_ctx, pp->on_complete,
ncteisen4b36a3d2017-03-13 19:08:06 -0700119 GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(
120 "Pick Cancelled", &error, 1));
Craig Tiller577c9b22015-11-02 14:11:15 -0800121 gpr_free(pp);
122 } else {
123 pp->next = p->pending_picks;
124 p->pending_picks = pp;
125 }
126 pp = next;
127 }
Mark D. Roth5f844002016-09-08 08:20:53 -0700128 GRPC_ERROR_UNREF(error);
Craig Tiller577c9b22015-11-02 14:11:15 -0800129}
130
Craig Tiller2400bf52017-02-09 16:25:19 -0800131static void pf_cancel_picks_locked(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol,
132 uint32_t initial_metadata_flags_mask,
133 uint32_t initial_metadata_flags_eq,
134 grpc_error *error) {
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800135 pick_first_lb_policy *p = (pick_first_lb_policy *)pol;
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700136 pending_pick *pp = p->pending_picks;
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800137 p->pending_picks = NULL;
138 while (pp != NULL) {
139 pending_pick *next = pp->next;
140 if ((pp->initial_metadata_flags & initial_metadata_flags_mask) ==
141 initial_metadata_flags_eq) {
ncteisen274bbbe2017-06-08 14:57:11 -0700142 GRPC_CLOSURE_SCHED(exec_ctx, pp->on_complete,
ncteisen4b36a3d2017-03-13 19:08:06 -0700143 GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(
144 "Pick Cancelled", &error, 1));
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800145 gpr_free(pp);
146 } else {
147 pp->next = p->pending_picks;
148 p->pending_picks = pp;
149 }
150 pp = next;
151 }
Mark D. Rothe65ff112016-09-09 13:48:38 -0700152 GRPC_ERROR_UNREF(error);
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800153}
154
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700155static void start_picking_locked(grpc_exec_ctx *exec_ctx,
156 pick_first_lb_policy *p) {
157 p->started_picking = true;
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700158 if (p->subchannel_list != NULL && p->subchannel_list->num_subchannels > 0) {
159 p->subchannel_list->checking_subchannel = 0;
160 grpc_lb_subchannel_list_ref_for_connectivity_watch(
161 p->subchannel_list, "connectivity_watch+start_picking");
162 grpc_lb_subchannel_data_start_connectivity_watch(
163 exec_ctx, &p->subchannel_list->subchannels[0]);
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700164 }
Craig Tiller48cb07c2015-07-15 16:16:15 -0700165}
166
Craig Tiller2400bf52017-02-09 16:25:19 -0800167static void pf_exit_idle_locked(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) {
Craig Tillera82950e2015-09-22 12:33:20 -0700168 pick_first_lb_policy *p = (pick_first_lb_policy *)pol;
Craig Tillera82950e2015-09-22 12:33:20 -0700169 if (!p->started_picking) {
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700170 start_picking_locked(exec_ctx, p);
Craig Tillera82950e2015-09-22 12:33:20 -0700171 }
Craig Tillereb3b12e2015-06-26 14:42:49 -0700172}
173
Craig Tiller2400bf52017-02-09 16:25:19 -0800174static int pf_pick_locked(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol,
175 const grpc_lb_policy_pick_args *pick_args,
Mark D. Roth09e458c2017-05-02 08:13:26 -0700176 grpc_connected_subchannel **target,
177 grpc_call_context_element *context, void **user_data,
Craig Tiller2400bf52017-02-09 16:25:19 -0800178 grpc_closure *on_complete) {
Craig Tillera82950e2015-09-22 12:33:20 -0700179 pick_first_lb_policy *p = (pick_first_lb_policy *)pol;
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700180 // If we have a selected subchannel already, return synchronously.
Craig Tiller2400bf52017-02-09 16:25:19 -0800181 if (p->selected != NULL) {
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700182 *target = GRPC_CONNECTED_SUBCHANNEL_REF(p->selected->connected_subchannel,
183 "picked");
Craig Tiller86c0f8a2015-12-01 20:05:40 -0800184 return 1;
185 }
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700186 // No subchannel selected yet, so handle asynchronously.
Craig Tiller2400bf52017-02-09 16:25:19 -0800187 if (!p->started_picking) {
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700188 start_picking_locked(exec_ctx, p);
Craig Tillera82950e2015-09-22 12:33:20 -0700189 }
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700190 pending_pick *pp = (pending_pick *)gpr_malloc(sizeof(*pp));
Craig Tiller2400bf52017-02-09 16:25:19 -0800191 pp->next = p->pending_picks;
192 pp->target = target;
193 pp->initial_metadata_flags = pick_args->initial_metadata_flags;
194 pp->on_complete = on_complete;
195 p->pending_picks = pp;
196 return 0;
Craig Tiller45724b32015-09-22 10:42:19 -0700197}
198
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700199static void destroy_unselected_subchannels_locked(grpc_exec_ctx *exec_ctx,
200 pick_first_lb_policy *p) {
201 for (size_t i = 0; i < p->subchannel_list->num_subchannels; ++i) {
202 grpc_lb_subchannel_data *sd = &p->subchannel_list->subchannels[i];
203 if (p->selected != sd) {
204 grpc_lb_subchannel_data_unref_subchannel(exec_ctx, sd,
205 "selected_different_subchannel");
206 }
Craig Tillerb09d84d2015-10-06 09:12:16 -0700207 }
Craig Tillerb09d84d2015-10-06 09:12:16 -0700208}
209
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700210static grpc_connectivity_state pf_check_connectivity_locked(
211 grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, grpc_error **error) {
212 pick_first_lb_policy *p = (pick_first_lb_policy *)pol;
213 return grpc_connectivity_state_get(&p->state_tracker, error);
214}
215
216static void pf_notify_on_state_change_locked(grpc_exec_ctx *exec_ctx,
217 grpc_lb_policy *pol,
218 grpc_connectivity_state *current,
219 grpc_closure *notify) {
220 pick_first_lb_policy *p = (pick_first_lb_policy *)pol;
221 grpc_connectivity_state_notify_on_state_change(exec_ctx, &p->state_tracker,
222 current, notify);
223}
224
225static void pf_ping_one_locked(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol,
226 grpc_closure *closure) {
227 pick_first_lb_policy *p = (pick_first_lb_policy *)pol;
228 if (p->selected) {
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700229 grpc_connected_subchannel_ping(exec_ctx, p->selected->connected_subchannel,
230 closure);
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700231 } else {
ncteisen274bbbe2017-06-08 14:57:11 -0700232 GRPC_CLOSURE_SCHED(exec_ctx, closure,
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700233 GRPC_ERROR_CREATE_FROM_STATIC_STRING("Not connected"));
234 }
235}
236
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700237static void pf_connectivity_changed_locked(grpc_exec_ctx *exec_ctx, void *arg,
238 grpc_error *error);
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700239
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700240static void pf_update_locked(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy,
241 const grpc_lb_policy_args *args) {
242 pick_first_lb_policy *p = (pick_first_lb_policy *)policy;
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700243 const grpc_arg *arg =
244 grpc_channel_args_find(args->args, GRPC_ARG_LB_ADDRESSES);
245 if (arg == NULL || arg->type != GRPC_ARG_POINTER) {
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700246 if (p->subchannel_list == NULL) {
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700247 // If we don't have a current subchannel list, go into TRANSIENT FAILURE.
248 grpc_connectivity_state_set(
249 exec_ctx, &p->state_tracker, GRPC_CHANNEL_TRANSIENT_FAILURE,
250 GRPC_ERROR_CREATE_FROM_STATIC_STRING("Missing update in args"),
251 "pf_update_missing");
252 } else {
253 // otherwise, keep using the current subchannel list (ignore this update).
254 gpr_log(GPR_ERROR,
255 "No valid LB addresses channel arg for Pick First %p update, "
256 "ignoring.",
257 (void *)p);
258 }
259 return;
260 }
Yash Tibrewalca3c1c02017-09-07 22:47:16 -0700261 const grpc_lb_addresses *addresses =
262 (const grpc_lb_addresses *)arg->value.pointer.p;
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700263 if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) {
264 gpr_log(GPR_INFO, "Pick First %p received update with %lu addresses",
Juanli Shen8af54b82017-08-30 15:55:10 -0700265 (void *)p, (unsigned long)addresses->num_addresses);
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700266 }
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700267 grpc_lb_subchannel_list *subchannel_list = grpc_lb_subchannel_list_create(
268 exec_ctx, &p->base, addresses, args, pf_connectivity_changed_locked);
269 if (subchannel_list->num_subchannels == 0) {
270 // Empty update or no valid subchannels. Unsubscribe from all current
271 // subchannels and put the channel in TRANSIENT_FAILURE.
272 grpc_connectivity_state_set(
273 exec_ctx, &p->state_tracker, GRPC_CHANNEL_TRANSIENT_FAILURE,
274 GRPC_ERROR_CREATE_FROM_STATIC_STRING("Empty update"),
275 "pf_update_empty");
276 if (p->subchannel_list != NULL) {
277 grpc_lb_subchannel_list_shutdown_and_unref(exec_ctx, p->subchannel_list,
278 "sl_shutdown_empty_update");
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700279 }
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700280 p->subchannel_list = subchannel_list; // Empty list.
281 p->selected = NULL;
282 return;
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700283 }
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700284 if (p->selected == NULL) {
285 // We don't yet have a selected subchannel, so replace the current
286 // subchannel list immediately.
287 if (p->subchannel_list != NULL) {
288 grpc_lb_subchannel_list_shutdown_and_unref(
289 exec_ctx, p->subchannel_list, "pf_update_before_selected");
290 }
291 p->subchannel_list = subchannel_list;
292 } else {
293 // We do have a selected subchannel.
294 // Check if it's present in the new list. If so, we're done.
295 for (size_t i = 0; i < subchannel_list->num_subchannels; ++i) {
296 grpc_lb_subchannel_data *sd = &subchannel_list->subchannels[i];
297 if (sd->subchannel == p->selected->subchannel) {
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700298 // The currently selected subchannel is in the update: we are done.
299 if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) {
300 gpr_log(GPR_INFO,
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700301 "Pick First %p found already selected subchannel %p "
302 "at update index %" PRIdPTR " of %" PRIdPTR "; update done",
303 p, p->selected->subchannel, i,
304 subchannel_list->num_subchannels);
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700305 }
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700306 grpc_lb_subchannel_list_ref_for_connectivity_watch(
307 subchannel_list, "connectivity_watch+replace_selected");
308 grpc_lb_subchannel_data_start_connectivity_watch(exec_ctx, sd);
309 if (p->subchannel_list != NULL) {
310 grpc_lb_subchannel_list_shutdown_and_unref(
311 exec_ctx, p->subchannel_list, "pf_update_includes_selected");
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700312 }
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700313 p->subchannel_list = subchannel_list;
314 if (p->selected->connected_subchannel != NULL) {
315 sd->connected_subchannel = GRPC_CONNECTED_SUBCHANNEL_REF(
316 grpc_subchannel_get_connected_subchannel(sd->subchannel),
317 "pf_update_includes_selected");
318 }
319 p->selected = sd;
320 destroy_unselected_subchannels_locked(exec_ctx, p);
321 // If there was a previously pending update (which may or may
322 // not have contained the currently selected subchannel), drop
323 // it, so that it doesn't override what we've done here.
324 if (p->latest_pending_subchannel_list != NULL) {
325 grpc_lb_subchannel_list_shutdown_and_unref(
326 exec_ctx, p->latest_pending_subchannel_list,
327 "pf_update_includes_selected+outdated");
328 p->latest_pending_subchannel_list = NULL;
329 }
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700330 return;
331 }
332 }
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700333 // Not keeping the previous selected subchannel, so set the latest
334 // pending subchannel list to the new subchannel list. We will wait
335 // for it to report READY before swapping it into the current
336 // subchannel list.
337 if (p->latest_pending_subchannel_list != NULL) {
338 if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) {
339 gpr_log(GPR_DEBUG,
340 "Pick First %p Shutting down latest pending subchannel list "
341 "%p, about to be replaced by newer latest %p",
342 (void *)p, (void *)p->latest_pending_subchannel_list,
343 (void *)subchannel_list);
344 }
345 grpc_lb_subchannel_list_shutdown_and_unref(
346 exec_ctx, p->latest_pending_subchannel_list,
347 "sl_outdated_dont_smash");
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700348 }
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700349 p->latest_pending_subchannel_list = subchannel_list;
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700350 }
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700351 // If we've started picking, start trying to connect to the first
352 // subchannel in the new list.
353 if (p->started_picking && subchannel_list->num_subchannels > 0) {
354 grpc_lb_subchannel_list_ref_for_connectivity_watch(
355 subchannel_list, "connectivity_watch+update");
356 grpc_lb_subchannel_data_start_connectivity_watch(
357 exec_ctx, &subchannel_list->subchannels[0]);
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700358 }
359}
360
Craig Tiller2400bf52017-02-09 16:25:19 -0800361static void pf_connectivity_changed_locked(grpc_exec_ctx *exec_ctx, void *arg,
362 grpc_error *error) {
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700363 grpc_lb_subchannel_data *sd = (grpc_lb_subchannel_data *)arg;
364 pick_first_lb_policy *p = (pick_first_lb_policy *)sd->subchannel_list->policy;
David Garcia Quintasaf084dc2017-06-27 13:42:54 -0700365 if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) {
366 gpr_log(
367 GPR_DEBUG,
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700368 "Pick First %p connectivity changed for subchannel %p (%" PRIdPTR
369 " of %" PRIdPTR "), subchannel_list %p: state=%s p->shutdown=%d "
370 "sd->subchannel_list->shutting_down=%d error=%s",
371 (void *)p, (void *)sd->subchannel,
372 p->subchannel_list->checking_subchannel,
373 sd->subchannel_list->num_subchannels, (void *)sd->subchannel_list,
374 grpc_connectivity_state_name(sd->pending_connectivity_state_unsafe),
375 p->shutdown, sd->subchannel_list->shutting_down,
376 grpc_error_string(error));
David Garcia Quintasaf084dc2017-06-27 13:42:54 -0700377 }
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700378 // If the policy is shutting down, unref and return.
379 if (p->shutdown) {
380 grpc_lb_subchannel_data_stop_connectivity_watch(exec_ctx, sd);
381 grpc_lb_subchannel_data_unref_subchannel(exec_ctx, sd, "pf_shutdown");
382 grpc_lb_subchannel_list_unref_for_connectivity_watch(
383 exec_ctx, sd->subchannel_list, "pf_shutdown");
384 return;
385 }
386 // If the subchannel list is shutting down, stop watching.
387 if (sd->subchannel_list->shutting_down || error == GRPC_ERROR_CANCELLED) {
388 grpc_lb_subchannel_data_stop_connectivity_watch(exec_ctx, sd);
389 grpc_lb_subchannel_data_unref_subchannel(exec_ctx, sd, "pf_sl_shutdown");
390 grpc_lb_subchannel_list_unref_for_connectivity_watch(
391 exec_ctx, sd->subchannel_list, "pf_sl_shutdown");
392 return;
393 }
394 // If we're still here, the notification must be for a subchannel in
395 // either the current or latest pending subchannel lists.
396 GPR_ASSERT(sd->subchannel_list == p->subchannel_list ||
397 sd->subchannel_list == p->latest_pending_subchannel_list);
398 // Update state counters.
399 sd->curr_connectivity_state = sd->pending_connectivity_state_unsafe;
400 if (sd->curr_connectivity_state == GRPC_CHANNEL_SHUTDOWN) {
401 ++sd->subchannel_list->num_shutdown;
402 }
403 sd->prev_connectivity_state = sd->curr_connectivity_state;
404 // Handle updates for the currently selected subchannel.
405 if (p->selected == sd) {
406 // If the new state is anything other than READY and there is a
407 // pending update, switch to the pending update.
408 if (sd->curr_connectivity_state != GRPC_CHANNEL_READY &&
409 p->latest_pending_subchannel_list != NULL) {
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700410 p->selected = NULL;
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700411 grpc_lb_subchannel_list_shutdown_and_unref(
412 exec_ctx, p->subchannel_list, "selected_not_ready+switch_to_update");
413 p->subchannel_list = p->latest_pending_subchannel_list;
414 p->latest_pending_subchannel_list = NULL;
415 grpc_lb_subchannel_data *new_sd = &p->subchannel_list->subchannels[
416 p->subchannel_list->checking_subchannel];
417 grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
418 new_sd->curr_connectivity_state,
419 GRPC_ERROR_REF(error),
420 "selected_not_ready+switch_to_update");
421 } else {
422 if (sd->curr_connectivity_state ==
423 GRPC_CHANNEL_TRANSIENT_FAILURE) {
424 /* if the selected channel goes bad, we're done */
425 sd->curr_connectivity_state = GRPC_CHANNEL_SHUTDOWN;
426 }
427 grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
428 sd->curr_connectivity_state,
429 GRPC_ERROR_REF(error), "selected_changed");
430 if (sd->curr_connectivity_state != GRPC_CHANNEL_SHUTDOWN) {
431 // Renew notification.
432 grpc_lb_subchannel_data_start_connectivity_watch(exec_ctx, sd);
433 } else {
434 grpc_lb_subchannel_data_stop_connectivity_watch(exec_ctx, sd);
435 grpc_lb_subchannel_list_unref_for_connectivity_watch(
436 exec_ctx, sd->subchannel_list, "pf_selected_shutdown");
437 pf_shutdown_locked(exec_ctx, &p->base);
David Garcia Quintasaf084dc2017-06-27 13:42:54 -0700438 }
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700439 }
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700440 return;
441 }
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700442 // If we get here, there are two possible cases:
443 // 1. We do not currently have a selected subchannel, and the update is
444 // for a subchannel in p->subchannel_list that we're trying to
445 // connect to. The goal here is to find a subchannel that we can
446 // select.
447 // 2. We do currently have a selected subchannel, and the update is
448 // for a subchannel in p->latest_pending_subchannel_list. The
449 // goal here is to find a subchannel from the update that we can
450 // select in place of the current one.
451 while (true) {
452 switch (sd->curr_connectivity_state) {
David Garcia Quintasea6689d2016-11-08 09:46:41 -0800453 case GRPC_CHANNEL_INIT:
Jan Tattermuschb0fb2d22016-11-16 14:04:05 +0100454 GPR_UNREACHABLE_CODE(return );
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700455 case GRPC_CHANNEL_READY: {
456 // Case 2. Promote p->latest_pending_subchannel_list to
457 // p->subchannel_list.
458 if (sd->subchannel_list == p->latest_pending_subchannel_list) {
459 GPR_ASSERT(p->subchannel_list != NULL);
460 grpc_lb_subchannel_list_shutdown_and_unref(
461 exec_ctx, p->subchannel_list, "finish_update");
462 p->subchannel_list = p->latest_pending_subchannel_list;
463 p->latest_pending_subchannel_list = NULL;
464 }
465 // Cases 1 and 2.
Craig Tillera82950e2015-09-22 12:33:20 -0700466 grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
Craig Tiller804ff712016-05-05 16:25:40 -0700467 GRPC_CHANNEL_READY, GRPC_ERROR_NONE,
468 "connecting_ready");
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700469 sd->connected_subchannel = GRPC_CONNECTED_SUBCHANNEL_REF(
470 grpc_subchannel_get_connected_subchannel(sd->subchannel),
471 "connected");
472 p->selected = sd;
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700473 if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) {
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700474 gpr_log(GPR_INFO, "Pick First %p selected subchannel %p",
475 (void *)p, (void *)sd->subchannel);
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700476 }
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700477 // Drop all other subchannels, since we are now connected.
478 destroy_unselected_subchannels_locked(exec_ctx, p);
479 // Update any calls that were waiting for a pick.
480 pending_pick *pp;
Craig Tillera82950e2015-09-22 12:33:20 -0700481 while ((pp = p->pending_picks)) {
482 p->pending_picks = pp->next;
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700483 *pp->target = GRPC_CONNECTED_SUBCHANNEL_REF(
484 p->selected->connected_subchannel, "picked");
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700485 if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) {
486 gpr_log(GPR_INFO,
487 "Servicing pending pick with selected subchannel %p",
488 (void *)p->selected);
489 }
ncteisen274bbbe2017-06-08 14:57:11 -0700490 GRPC_CLOSURE_SCHED(exec_ctx, pp->on_complete, GRPC_ERROR_NONE);
Craig Tillera82950e2015-09-22 12:33:20 -0700491 gpr_free(pp);
492 }
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700493 // Renew notification.
494 grpc_lb_subchannel_data_start_connectivity_watch(exec_ctx, sd);
495 return;
496 }
497 case GRPC_CHANNEL_TRANSIENT_FAILURE: {
498 grpc_lb_subchannel_data_stop_connectivity_watch(exec_ctx, sd);
499 sd->subchannel_list->checking_subchannel =
500 (sd->subchannel_list->checking_subchannel + 1)
501 % sd->subchannel_list->num_subchannels;
502 // Case 1: Only set state to TRANSIENT_FAILURE if we've tried
503 // all subchannels.
504 if (sd->subchannel_list->checking_subchannel == 0 &&
505 sd->subchannel_list == p->subchannel_list) {
Craig Tiller804ff712016-05-05 16:25:40 -0700506 grpc_connectivity_state_set(
507 exec_ctx, &p->state_tracker, GRPC_CHANNEL_TRANSIENT_FAILURE,
Craig Tillerf707d622016-05-06 14:26:12 -0700508 GRPC_ERROR_REF(error), "connecting_transient_failure");
Craig Tiller131b6de2016-03-31 17:05:28 -0700509 }
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700510 sd = &sd->subchannel_list->subchannels[
511 sd->subchannel_list->checking_subchannel];
512 sd->curr_connectivity_state =
513 grpc_subchannel_check_connectivity(sd->subchannel, &error);
514 if (sd->curr_connectivity_state == GRPC_CHANNEL_TRANSIENT_FAILURE) {
515 // Reuses the connectivity refs from the previous watch.
516 grpc_lb_subchannel_data_start_connectivity_watch(exec_ctx, sd);
517 return;
Craig Tillera82950e2015-09-22 12:33:20 -0700518 }
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700519 break; // Go back to top of loop.
520 }
Craig Tillera82950e2015-09-22 12:33:20 -0700521 case GRPC_CHANNEL_CONNECTING:
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700522 case GRPC_CHANNEL_IDLE: {
523 // Only update connectivity state in case 1.
524 if (sd->subchannel_list == p->subchannel_list) {
Craig Tiller804ff712016-05-05 16:25:40 -0700525 grpc_connectivity_state_set(
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700526 exec_ctx, &p->state_tracker, GRPC_CHANNEL_CONNECTING,
527 GRPC_ERROR_REF(error), "connecting_changed");
528 }
529 // Renew notification.
530 grpc_lb_subchannel_data_start_connectivity_watch(exec_ctx, sd);
531 return;
532 }
533 case GRPC_CHANNEL_SHUTDOWN: {
534 grpc_lb_subchannel_data_stop_connectivity_watch(exec_ctx, sd);
535 grpc_lb_subchannel_data_unref_subchannel(exec_ctx, sd,
536 "pf_candidate_shutdown");
537 if (sd->subchannel_list->num_shutdown ==
538 sd->subchannel_list->num_subchannels) {
539 grpc_lb_subchannel_list_unref_for_connectivity_watch(
540 exec_ctx, sd->subchannel_list, "pf_candidate_shutdown");
541 shutdown_locked(exec_ctx, p,
542 GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(
543 "Pick first exhausted channels", &error, 1));
544 return;
545 }
546 if (sd->subchannel_list == p->subchannel_list) {
Craig Tiller804ff712016-05-05 16:25:40 -0700547 grpc_connectivity_state_set(
548 exec_ctx, &p->state_tracker, GRPC_CHANNEL_TRANSIENT_FAILURE,
Craig Tillerf707d622016-05-06 14:26:12 -0700549 GRPC_ERROR_REF(error), "subchannel_failed");
Craig Tillera82950e2015-09-22 12:33:20 -0700550 }
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700551 // Advance to next subchannel and check its state.
552 sd->subchannel_list->checking_subchannel =
553 (sd->subchannel_list->checking_subchannel + 1)
554 % sd->subchannel_list->num_subchannels;
555 sd = &sd->subchannel_list->subchannels[
556 sd->subchannel_list->checking_subchannel];
557 sd->curr_connectivity_state =
558 grpc_subchannel_check_connectivity(sd->subchannel, &error);
559 if (sd->curr_connectivity_state == GRPC_CHANNEL_TRANSIENT_FAILURE) {
560 // Reuses the connectivity refs from the previous watch.
561 grpc_lb_subchannel_data_start_connectivity_watch(exec_ctx, sd);
562 return;
563 }
564 // For any other state, go back to top of loop.
565 // We will reuse the connectivity refs from the previous watch.
566 }
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700567 }
Craig Tillera82950e2015-09-22 12:33:20 -0700568 }
Craig Tillereb3b12e2015-06-26 14:42:49 -0700569}
570
571static const grpc_lb_policy_vtable pick_first_lb_policy_vtable = {
Craig Tiller2400bf52017-02-09 16:25:19 -0800572 pf_destroy,
573 pf_shutdown_locked,
574 pf_pick_locked,
575 pf_cancel_pick_locked,
576 pf_cancel_picks_locked,
577 pf_ping_one_locked,
578 pf_exit_idle_locked,
579 pf_check_connectivity_locked,
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700580 pf_notify_on_state_change_locked,
581 pf_update_locked};
Craig Tillereb3b12e2015-06-26 14:42:49 -0700582
Craig Tillera82950e2015-09-22 12:33:20 -0700583static void pick_first_factory_ref(grpc_lb_policy_factory *factory) {}
David Garcia Quintas5c4543d2015-09-03 15:49:56 -0700584
Craig Tillera82950e2015-09-22 12:33:20 -0700585static void pick_first_factory_unref(grpc_lb_policy_factory *factory) {}
David Garcia Quintas5c4543d2015-09-03 15:49:56 -0700586
David Garcia Quintas67c0d042016-03-25 01:37:53 -0700587static grpc_lb_policy *create_pick_first(grpc_exec_ctx *exec_ctx,
588 grpc_lb_policy_factory *factory,
Craig Tillera82950e2015-09-22 12:33:20 -0700589 grpc_lb_policy_args *args) {
David Garcia Quintas86fcfcc2016-03-31 23:22:28 -0700590 GPR_ASSERT(args->client_channel_factory != NULL);
Yash Tibrewalca3c1c02017-09-07 22:47:16 -0700591 pick_first_lb_policy *p = (pick_first_lb_policy *)gpr_zalloc(sizeof(*p));
David Garcia Quintasaf084dc2017-06-27 13:42:54 -0700592 if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) {
593 gpr_log(GPR_DEBUG, "Pick First %p created.", (void *)p);
594 }
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700595 pf_update_locked(exec_ctx, &p->base, args);
Craig Tiller2400bf52017-02-09 16:25:19 -0800596 grpc_lb_policy_init(&p->base, &pick_first_lb_policy_vtable, args->combiner);
Juanli Shen6502ecc2017-09-13 13:10:54 -0700597 grpc_subchannel_index_ref();
Craig Tillereb3b12e2015-06-26 14:42:49 -0700598 return &p->base;
599}
David Garcia Quintas5c4543d2015-09-03 15:49:56 -0700600
601static const grpc_lb_policy_factory_vtable pick_first_factory_vtable = {
Craig Tillera82950e2015-09-22 12:33:20 -0700602 pick_first_factory_ref, pick_first_factory_unref, create_pick_first,
603 "pick_first"};
David Garcia Quintas5c4543d2015-09-03 15:49:56 -0700604
605static grpc_lb_policy_factory pick_first_lb_policy_factory = {
Craig Tillera82950e2015-09-22 12:33:20 -0700606 &pick_first_factory_vtable};
David Garcia Quintas5c4543d2015-09-03 15:49:56 -0700607
Craig Tillerfb433852016-03-29 08:51:07 -0700608static grpc_lb_policy_factory *pick_first_lb_factory_create() {
David Garcia Quintas5c4543d2015-09-03 15:49:56 -0700609 return &pick_first_lb_policy_factory;
610}
Craig Tillerfb433852016-03-29 08:51:07 -0700611
612/* Plugin registration */
613
Yash Tibrewal83062842017-09-21 18:56:08 -0700614extern "C" void grpc_lb_policy_pick_first_init() {
Craig Tiller3113ef42016-03-29 09:03:14 -0700615 grpc_register_lb_policy(pick_first_lb_factory_create());
ncteisen06bce6e2017-07-10 07:58:49 -0700616 grpc_register_tracer(&grpc_lb_pick_first_trace);
Craig Tillerfb433852016-03-29 08:51:07 -0700617}
618
Yash Tibrewal83062842017-09-21 18:56:08 -0700619extern "C" void grpc_lb_policy_pick_first_shutdown() {}