blob: 81f9bc5a989b73a1035f6b0c8ce1f596388a47c7 [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;
Mark D. Roth5132d0e2017-10-13 13:20:52 -070047 /** latest pending subchannel list */
Mark D. Roth5e9848e2017-10-06 13:59:32 -070048 grpc_lb_subchannel_list *latest_pending_subchannel_list;
Mark D. Roth5132d0e2017-10-13 13:20:52 -070049 /** selected subchannel in \a subchannel_list */
Mark D. Roth5e9848e2017-10-06 13:59:32 -070050 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 }
Mark D. Roth62ca6ce2017-10-10 10:01:51 -070087 grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
88 GRPC_CHANNEL_SHUTDOWN, GRPC_ERROR_REF(error),
89 "shutdown");
Mark D. Roth5e9848e2017-10-06 13:59:32 -070090 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(
Mark D. Roth901bb4f2017-10-11 08:49:07 -0700268 exec_ctx, &p->base, &grpc_lb_pick_first_trace, addresses, args,
269 pf_connectivity_changed_locked);
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700270 if (subchannel_list->num_subchannels == 0) {
271 // Empty update or no valid subchannels. Unsubscribe from all current
272 // subchannels and put the channel in TRANSIENT_FAILURE.
273 grpc_connectivity_state_set(
274 exec_ctx, &p->state_tracker, GRPC_CHANNEL_TRANSIENT_FAILURE,
275 GRPC_ERROR_CREATE_FROM_STATIC_STRING("Empty update"),
276 "pf_update_empty");
277 if (p->subchannel_list != NULL) {
278 grpc_lb_subchannel_list_shutdown_and_unref(exec_ctx, p->subchannel_list,
279 "sl_shutdown_empty_update");
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700280 }
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700281 p->subchannel_list = subchannel_list; // Empty list.
282 p->selected = NULL;
283 return;
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700284 }
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700285 if (p->selected == NULL) {
286 // We don't yet have a selected subchannel, so replace the current
287 // subchannel list immediately.
288 if (p->subchannel_list != NULL) {
Mark D. Roth62ca6ce2017-10-10 10:01:51 -0700289 grpc_lb_subchannel_list_shutdown_and_unref(exec_ctx, p->subchannel_list,
290 "pf_update_before_selected");
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700291 }
292 p->subchannel_list = subchannel_list;
293 } else {
294 // We do have a selected subchannel.
295 // Check if it's present in the new list. If so, we're done.
296 for (size_t i = 0; i < subchannel_list->num_subchannels; ++i) {
297 grpc_lb_subchannel_data *sd = &subchannel_list->subchannels[i];
298 if (sd->subchannel == p->selected->subchannel) {
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700299 // The currently selected subchannel is in the update: we are done.
300 if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) {
301 gpr_log(GPR_INFO,
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700302 "Pick First %p found already selected subchannel %p "
303 "at update index %" PRIdPTR " of %" PRIdPTR "; update done",
304 p, p->selected->subchannel, i,
305 subchannel_list->num_subchannels);
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700306 }
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700307 grpc_lb_subchannel_list_ref_for_connectivity_watch(
308 subchannel_list, "connectivity_watch+replace_selected");
309 grpc_lb_subchannel_data_start_connectivity_watch(exec_ctx, sd);
310 if (p->subchannel_list != NULL) {
311 grpc_lb_subchannel_list_shutdown_and_unref(
312 exec_ctx, p->subchannel_list, "pf_update_includes_selected");
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700313 }
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700314 p->subchannel_list = subchannel_list;
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700315 p->selected = sd;
316 destroy_unselected_subchannels_locked(exec_ctx, p);
317 // If there was a previously pending update (which may or may
318 // not have contained the currently selected subchannel), drop
319 // it, so that it doesn't override what we've done here.
320 if (p->latest_pending_subchannel_list != NULL) {
321 grpc_lb_subchannel_list_shutdown_and_unref(
322 exec_ctx, p->latest_pending_subchannel_list,
323 "pf_update_includes_selected+outdated");
324 p->latest_pending_subchannel_list = NULL;
325 }
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700326 return;
327 }
328 }
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700329 // Not keeping the previous selected subchannel, so set the latest
330 // pending subchannel list to the new subchannel list. We will wait
331 // for it to report READY before swapping it into the current
332 // subchannel list.
333 if (p->latest_pending_subchannel_list != NULL) {
334 if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) {
335 gpr_log(GPR_DEBUG,
336 "Pick First %p Shutting down latest pending subchannel list "
337 "%p, about to be replaced by newer latest %p",
338 (void *)p, (void *)p->latest_pending_subchannel_list,
339 (void *)subchannel_list);
340 }
341 grpc_lb_subchannel_list_shutdown_and_unref(
342 exec_ctx, p->latest_pending_subchannel_list,
343 "sl_outdated_dont_smash");
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700344 }
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700345 p->latest_pending_subchannel_list = subchannel_list;
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700346 }
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700347 // If we've started picking, start trying to connect to the first
348 // subchannel in the new list.
Mark D. Roth5132d0e2017-10-13 13:20:52 -0700349 if (p->started_picking) {
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700350 grpc_lb_subchannel_list_ref_for_connectivity_watch(
351 subchannel_list, "connectivity_watch+update");
352 grpc_lb_subchannel_data_start_connectivity_watch(
353 exec_ctx, &subchannel_list->subchannels[0]);
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700354 }
355}
356
Craig Tiller2400bf52017-02-09 16:25:19 -0800357static void pf_connectivity_changed_locked(grpc_exec_ctx *exec_ctx, void *arg,
358 grpc_error *error) {
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700359 grpc_lb_subchannel_data *sd = (grpc_lb_subchannel_data *)arg;
360 pick_first_lb_policy *p = (pick_first_lb_policy *)sd->subchannel_list->policy;
David Garcia Quintasaf084dc2017-06-27 13:42:54 -0700361 if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) {
Mark D. Roth62ca6ce2017-10-10 10:01:51 -0700362 gpr_log(GPR_DEBUG,
363 "Pick First %p connectivity changed for subchannel %p (%" PRIdPTR
364 " of %" PRIdPTR
365 "), subchannel_list %p: state=%s p->shutdown=%d "
366 "sd->subchannel_list->shutting_down=%d error=%s",
367 (void *)p, (void *)sd->subchannel,
368 sd->subchannel_list->checking_subchannel,
369 sd->subchannel_list->num_subchannels, (void *)sd->subchannel_list,
370 grpc_connectivity_state_name(sd->pending_connectivity_state_unsafe),
371 p->shutdown, sd->subchannel_list->shutting_down,
372 grpc_error_string(error));
David Garcia Quintasaf084dc2017-06-27 13:42:54 -0700373 }
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700374 // If the policy is shutting down, unref and return.
375 if (p->shutdown) {
376 grpc_lb_subchannel_data_stop_connectivity_watch(exec_ctx, sd);
377 grpc_lb_subchannel_data_unref_subchannel(exec_ctx, sd, "pf_shutdown");
378 grpc_lb_subchannel_list_unref_for_connectivity_watch(
379 exec_ctx, sd->subchannel_list, "pf_shutdown");
380 return;
381 }
382 // If the subchannel list is shutting down, stop watching.
383 if (sd->subchannel_list->shutting_down || error == GRPC_ERROR_CANCELLED) {
384 grpc_lb_subchannel_data_stop_connectivity_watch(exec_ctx, sd);
385 grpc_lb_subchannel_data_unref_subchannel(exec_ctx, sd, "pf_sl_shutdown");
386 grpc_lb_subchannel_list_unref_for_connectivity_watch(
387 exec_ctx, sd->subchannel_list, "pf_sl_shutdown");
388 return;
389 }
390 // If we're still here, the notification must be for a subchannel in
391 // either the current or latest pending subchannel lists.
392 GPR_ASSERT(sd->subchannel_list == p->subchannel_list ||
393 sd->subchannel_list == p->latest_pending_subchannel_list);
Mark D. Roth5132d0e2017-10-13 13:20:52 -0700394 // Update state.
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700395 sd->curr_connectivity_state = sd->pending_connectivity_state_unsafe;
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700396 // Handle updates for the currently selected subchannel.
397 if (p->selected == sd) {
398 // If the new state is anything other than READY and there is a
399 // pending update, switch to the pending update.
400 if (sd->curr_connectivity_state != GRPC_CHANNEL_READY &&
401 p->latest_pending_subchannel_list != NULL) {
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700402 p->selected = NULL;
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700403 grpc_lb_subchannel_list_shutdown_and_unref(
404 exec_ctx, p->subchannel_list, "selected_not_ready+switch_to_update");
405 p->subchannel_list = p->latest_pending_subchannel_list;
406 p->latest_pending_subchannel_list = NULL;
Mark D. Roth62ca6ce2017-10-10 10:01:51 -0700407 grpc_connectivity_state_set(
Mark D. Roth99f54e12017-10-16 09:55:53 -0700408 exec_ctx, &p->state_tracker, GRPC_CHANNEL_TRANSIENT_FAILURE,
Mark D. Roth62ca6ce2017-10-10 10:01:51 -0700409 GRPC_ERROR_REF(error), "selected_not_ready+switch_to_update");
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700410 } else {
Mark D. Roth62ca6ce2017-10-10 10:01:51 -0700411 if (sd->curr_connectivity_state == GRPC_CHANNEL_TRANSIENT_FAILURE) {
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700412 /* if the selected channel goes bad, we're done */
413 sd->curr_connectivity_state = GRPC_CHANNEL_SHUTDOWN;
414 }
415 grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
416 sd->curr_connectivity_state,
417 GRPC_ERROR_REF(error), "selected_changed");
418 if (sd->curr_connectivity_state != GRPC_CHANNEL_SHUTDOWN) {
419 // Renew notification.
420 grpc_lb_subchannel_data_start_connectivity_watch(exec_ctx, sd);
421 } else {
422 grpc_lb_subchannel_data_stop_connectivity_watch(exec_ctx, sd);
423 grpc_lb_subchannel_list_unref_for_connectivity_watch(
424 exec_ctx, sd->subchannel_list, "pf_selected_shutdown");
Mark D. Rothaadf9f42017-10-19 07:53:53 -0700425 shutdown_locked(exec_ctx, p, GRPC_ERROR_REF(error));
David Garcia Quintasaf084dc2017-06-27 13:42:54 -0700426 }
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700427 }
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700428 return;
429 }
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700430 // If we get here, there are two possible cases:
431 // 1. We do not currently have a selected subchannel, and the update is
432 // for a subchannel in p->subchannel_list that we're trying to
433 // connect to. The goal here is to find a subchannel that we can
434 // select.
435 // 2. We do currently have a selected subchannel, and the update is
436 // for a subchannel in p->latest_pending_subchannel_list. The
437 // goal here is to find a subchannel from the update that we can
438 // select in place of the current one.
Mark D. Roth7ba40ba2017-10-10 13:17:13 -0700439 if (sd->curr_connectivity_state == GRPC_CHANNEL_TRANSIENT_FAILURE ||
440 sd->curr_connectivity_state == GRPC_CHANNEL_SHUTDOWN) {
441 grpc_lb_subchannel_data_stop_connectivity_watch(exec_ctx, sd);
442 }
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700443 while (true) {
444 switch (sd->curr_connectivity_state) {
David Garcia Quintasea6689d2016-11-08 09:46:41 -0800445 case GRPC_CHANNEL_INIT:
Jan Tattermuschb0fb2d22016-11-16 14:04:05 +0100446 GPR_UNREACHABLE_CODE(return );
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700447 case GRPC_CHANNEL_READY: {
448 // Case 2. Promote p->latest_pending_subchannel_list to
449 // p->subchannel_list.
450 if (sd->subchannel_list == p->latest_pending_subchannel_list) {
451 GPR_ASSERT(p->subchannel_list != NULL);
452 grpc_lb_subchannel_list_shutdown_and_unref(
453 exec_ctx, p->subchannel_list, "finish_update");
454 p->subchannel_list = p->latest_pending_subchannel_list;
455 p->latest_pending_subchannel_list = NULL;
456 }
457 // Cases 1 and 2.
Craig Tillera82950e2015-09-22 12:33:20 -0700458 grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
Craig Tiller804ff712016-05-05 16:25:40 -0700459 GRPC_CHANNEL_READY, GRPC_ERROR_NONE,
460 "connecting_ready");
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700461 sd->connected_subchannel = GRPC_CONNECTED_SUBCHANNEL_REF(
462 grpc_subchannel_get_connected_subchannel(sd->subchannel),
463 "connected");
464 p->selected = sd;
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700465 if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) {
Mark D. Roth62ca6ce2017-10-10 10:01:51 -0700466 gpr_log(GPR_INFO, "Pick First %p selected subchannel %p", (void *)p,
467 (void *)sd->subchannel);
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700468 }
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700469 // Drop all other subchannels, since we are now connected.
470 destroy_unselected_subchannels_locked(exec_ctx, p);
471 // Update any calls that were waiting for a pick.
472 pending_pick *pp;
Craig Tillera82950e2015-09-22 12:33:20 -0700473 while ((pp = p->pending_picks)) {
474 p->pending_picks = pp->next;
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700475 *pp->target = GRPC_CONNECTED_SUBCHANNEL_REF(
476 p->selected->connected_subchannel, "picked");
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700477 if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) {
478 gpr_log(GPR_INFO,
479 "Servicing pending pick with selected subchannel %p",
480 (void *)p->selected);
481 }
ncteisen274bbbe2017-06-08 14:57:11 -0700482 GRPC_CLOSURE_SCHED(exec_ctx, pp->on_complete, GRPC_ERROR_NONE);
Craig Tillera82950e2015-09-22 12:33:20 -0700483 gpr_free(pp);
484 }
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700485 // Renew notification.
486 grpc_lb_subchannel_data_start_connectivity_watch(exec_ctx, sd);
487 return;
488 }
489 case GRPC_CHANNEL_TRANSIENT_FAILURE: {
Mark D. Roth7ba40ba2017-10-10 13:17:13 -0700490 do {
491 sd->subchannel_list->checking_subchannel =
492 (sd->subchannel_list->checking_subchannel + 1) %
493 sd->subchannel_list->num_subchannels;
494 sd = &sd->subchannel_list
495 ->subchannels[sd->subchannel_list->checking_subchannel];
496 } while (sd->subchannel == NULL);
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700497 // Case 1: Only set state to TRANSIENT_FAILURE if we've tried
498 // all subchannels.
499 if (sd->subchannel_list->checking_subchannel == 0 &&
500 sd->subchannel_list == p->subchannel_list) {
Craig Tiller804ff712016-05-05 16:25:40 -0700501 grpc_connectivity_state_set(
502 exec_ctx, &p->state_tracker, GRPC_CHANNEL_TRANSIENT_FAILURE,
Craig Tillerf707d622016-05-06 14:26:12 -0700503 GRPC_ERROR_REF(error), "connecting_transient_failure");
Craig Tiller131b6de2016-03-31 17:05:28 -0700504 }
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700505 sd->curr_connectivity_state =
506 grpc_subchannel_check_connectivity(sd->subchannel, &error);
Mark D. Roth7ba40ba2017-10-10 13:17:13 -0700507 GRPC_ERROR_UNREF(error);
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700508 if (sd->curr_connectivity_state == GRPC_CHANNEL_TRANSIENT_FAILURE) {
509 // Reuses the connectivity refs from the previous watch.
510 grpc_lb_subchannel_data_start_connectivity_watch(exec_ctx, sd);
511 return;
Craig Tillera82950e2015-09-22 12:33:20 -0700512 }
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700513 break; // Go back to top of loop.
514 }
Craig Tillera82950e2015-09-22 12:33:20 -0700515 case GRPC_CHANNEL_CONNECTING:
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700516 case GRPC_CHANNEL_IDLE: {
517 // Only update connectivity state in case 1.
518 if (sd->subchannel_list == p->subchannel_list) {
Craig Tiller804ff712016-05-05 16:25:40 -0700519 grpc_connectivity_state_set(
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700520 exec_ctx, &p->state_tracker, GRPC_CHANNEL_CONNECTING,
521 GRPC_ERROR_REF(error), "connecting_changed");
522 }
523 // Renew notification.
524 grpc_lb_subchannel_data_start_connectivity_watch(exec_ctx, sd);
525 return;
526 }
527 case GRPC_CHANNEL_SHUTDOWN: {
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700528 grpc_lb_subchannel_data_unref_subchannel(exec_ctx, sd,
529 "pf_candidate_shutdown");
Mark D. Roth7ba40ba2017-10-10 13:17:13 -0700530 // Advance to next subchannel and check its state.
531 grpc_lb_subchannel_data *original_sd = sd;
532 do {
533 sd->subchannel_list->checking_subchannel =
534 (sd->subchannel_list->checking_subchannel + 1) %
535 sd->subchannel_list->num_subchannels;
536 sd = &sd->subchannel_list
537 ->subchannels[sd->subchannel_list->checking_subchannel];
538 } while (sd->subchannel == NULL && sd != original_sd);
539 if (sd == original_sd) {
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700540 grpc_lb_subchannel_list_unref_for_connectivity_watch(
541 exec_ctx, sd->subchannel_list, "pf_candidate_shutdown");
542 shutdown_locked(exec_ctx, p,
543 GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(
544 "Pick first exhausted channels", &error, 1));
545 return;
546 }
547 if (sd->subchannel_list == p->subchannel_list) {
Craig Tiller804ff712016-05-05 16:25:40 -0700548 grpc_connectivity_state_set(
549 exec_ctx, &p->state_tracker, GRPC_CHANNEL_TRANSIENT_FAILURE,
Craig Tillerf707d622016-05-06 14:26:12 -0700550 GRPC_ERROR_REF(error), "subchannel_failed");
Craig Tillera82950e2015-09-22 12:33:20 -0700551 }
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700552 sd->curr_connectivity_state =
553 grpc_subchannel_check_connectivity(sd->subchannel, &error);
Mark D. Roth7ba40ba2017-10-10 13:17:13 -0700554 GRPC_ERROR_UNREF(error);
Mark D. Roth5e9848e2017-10-06 13:59:32 -0700555 if (sd->curr_connectivity_state == GRPC_CHANNEL_TRANSIENT_FAILURE) {
556 // Reuses the connectivity refs from the previous watch.
557 grpc_lb_subchannel_data_start_connectivity_watch(exec_ctx, sd);
558 return;
559 }
560 // For any other state, go back to top of loop.
561 // We will reuse the connectivity refs from the previous watch.
562 }
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700563 }
Craig Tillera82950e2015-09-22 12:33:20 -0700564 }
Craig Tillereb3b12e2015-06-26 14:42:49 -0700565}
566
567static const grpc_lb_policy_vtable pick_first_lb_policy_vtable = {
Craig Tiller2400bf52017-02-09 16:25:19 -0800568 pf_destroy,
569 pf_shutdown_locked,
570 pf_pick_locked,
571 pf_cancel_pick_locked,
572 pf_cancel_picks_locked,
573 pf_ping_one_locked,
574 pf_exit_idle_locked,
575 pf_check_connectivity_locked,
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700576 pf_notify_on_state_change_locked,
577 pf_update_locked};
Craig Tillereb3b12e2015-06-26 14:42:49 -0700578
Craig Tillera82950e2015-09-22 12:33:20 -0700579static void pick_first_factory_ref(grpc_lb_policy_factory *factory) {}
David Garcia Quintas5c4543d2015-09-03 15:49:56 -0700580
Craig Tillera82950e2015-09-22 12:33:20 -0700581static void pick_first_factory_unref(grpc_lb_policy_factory *factory) {}
David Garcia Quintas5c4543d2015-09-03 15:49:56 -0700582
David Garcia Quintas67c0d042016-03-25 01:37:53 -0700583static grpc_lb_policy *create_pick_first(grpc_exec_ctx *exec_ctx,
584 grpc_lb_policy_factory *factory,
Craig Tillera82950e2015-09-22 12:33:20 -0700585 grpc_lb_policy_args *args) {
David Garcia Quintas86fcfcc2016-03-31 23:22:28 -0700586 GPR_ASSERT(args->client_channel_factory != NULL);
Yash Tibrewalca3c1c02017-09-07 22:47:16 -0700587 pick_first_lb_policy *p = (pick_first_lb_policy *)gpr_zalloc(sizeof(*p));
David Garcia Quintasaf084dc2017-06-27 13:42:54 -0700588 if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) {
589 gpr_log(GPR_DEBUG, "Pick First %p created.", (void *)p);
590 }
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700591 pf_update_locked(exec_ctx, &p->base, args);
Craig Tiller2400bf52017-02-09 16:25:19 -0800592 grpc_lb_policy_init(&p->base, &pick_first_lb_policy_vtable, args->combiner);
Juanli Shen6502ecc2017-09-13 13:10:54 -0700593 grpc_subchannel_index_ref();
Craig Tillereb3b12e2015-06-26 14:42:49 -0700594 return &p->base;
595}
David Garcia Quintas5c4543d2015-09-03 15:49:56 -0700596
597static const grpc_lb_policy_factory_vtable pick_first_factory_vtable = {
Craig Tillera82950e2015-09-22 12:33:20 -0700598 pick_first_factory_ref, pick_first_factory_unref, create_pick_first,
599 "pick_first"};
David Garcia Quintas5c4543d2015-09-03 15:49:56 -0700600
601static grpc_lb_policy_factory pick_first_lb_policy_factory = {
Craig Tillera82950e2015-09-22 12:33:20 -0700602 &pick_first_factory_vtable};
David Garcia Quintas5c4543d2015-09-03 15:49:56 -0700603
Craig Tillerfb433852016-03-29 08:51:07 -0700604static grpc_lb_policy_factory *pick_first_lb_factory_create() {
David Garcia Quintas5c4543d2015-09-03 15:49:56 -0700605 return &pick_first_lb_policy_factory;
606}
Craig Tillerfb433852016-03-29 08:51:07 -0700607
608/* Plugin registration */
609
Yash Tibrewal83062842017-09-21 18:56:08 -0700610extern "C" void grpc_lb_policy_pick_first_init() {
Craig Tiller3113ef42016-03-29 09:03:14 -0700611 grpc_register_lb_policy(pick_first_lb_factory_create());
ncteisen06bce6e2017-07-10 07:58:49 -0700612 grpc_register_tracer(&grpc_lb_pick_first_trace);
Craig Tillerfb433852016-03-29 08:51:07 -0700613}
614
Yash Tibrewal83062842017-09-21 18:56:08 -0700615extern "C" void grpc_lb_policy_pick_first_shutdown() {}