blob: 1b965183f6bc186b52c40cb611f25274a14291f4 [file] [log] [blame]
Craig Tiller3bc8ebd2015-06-24 15:41:15 -07001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Craig Tiller3bc8ebd2015-06-24 15:41:15 -07004 * 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
Craig Tillereb3b12e2015-06-26 14:42:49 -070034#include <string.h>
35
36#include <grpc/support/alloc.h>
Mark D. Roth5bd7be02016-10-21 14:19:50 -070037
Mark D. Roth2137cd82016-09-14 09:04:00 -070038#include "src/core/ext/client_channel/lb_policy_registry.h"
Mark D. Roth0748f392017-01-13 09:22:44 -080039#include "src/core/ext/client_channel/subchannel.h"
Mark D. Roth557c9902016-10-24 11:12:05 -070040#include "src/core/lib/channel/channel_args.h"
Mark D. Roth0748f392017-01-13 09:22:44 -080041#include "src/core/lib/iomgr/sockaddr_utils.h"
Craig Tiller9533d042016-03-25 17:11:06 -070042#include "src/core/lib/transport/connectivity_state.h"
Craig Tillereb3b12e2015-06-26 14:42:49 -070043
Craig Tillera82950e2015-09-22 12:33:20 -070044typedef struct pending_pick {
Craig Tillereb3b12e2015-06-26 14:42:49 -070045 struct pending_pick *next;
Craig Tiller8c0d96f2016-03-11 14:27:52 -080046 uint32_t initial_metadata_flags;
Craig Tillerb5585d42015-11-17 07:18:31 -080047 grpc_connected_subchannel **target;
Craig Tiller33825112015-09-18 07:44:19 -070048 grpc_closure *on_complete;
Craig Tillereb3b12e2015-06-26 14:42:49 -070049} pending_pick;
50
Craig Tillera82950e2015-09-22 12:33:20 -070051typedef struct {
Craig Tillereb3b12e2015-06-26 14:42:49 -070052 /** base policy: must be first */
53 grpc_lb_policy base;
Craig Tillereb3b12e2015-06-26 14:42:49 -070054 /** all our subchannels */
55 grpc_subchannel **subchannels;
56 size_t num_subchannels;
57
Craig Tiller33825112015-09-18 07:44:19 -070058 grpc_closure connectivity_changed;
Craig Tillereb3b12e2015-06-26 14:42:49 -070059
Craig Tiller86c0f8a2015-12-01 20:05:40 -080060 /** the selected channel (a grpc_connected_subchannel) */
61 gpr_atm selected;
Craig Tiller320bee02016-01-06 17:33:45 -080062
63 /** mutex protecting remaining members */
64 gpr_mu mu;
Craig Tillereb3b12e2015-06-26 14:42:49 -070065 /** have we started picking? */
66 int started_picking;
Craig Tillera14215a2015-07-17 17:21:08 -070067 /** are we shut down? */
68 int shutdown;
Craig Tillereb3b12e2015-06-26 14:42:49 -070069 /** which subchannel are we watching? */
70 size_t checking_subchannel;
71 /** what is the connectivity of that channel? */
72 grpc_connectivity_state checking_connectivity;
73 /** list of picks that are waiting on connectivity */
74 pending_pick *pending_picks;
Craig Tillerc7b5f762015-06-27 11:48:42 -070075
76 /** our connectivity state tracker */
77 grpc_connectivity_state_tracker state_tracker;
Craig Tillereb3b12e2015-06-26 14:42:49 -070078} pick_first_lb_policy;
79
Craig Tiller81afdda2016-01-11 17:09:18 -080080#define GET_SELECTED(p) \
Craig Tillerd9d474a2016-01-26 06:50:51 -080081 ((grpc_connected_subchannel *)gpr_atm_acq_load(&(p)->selected))
Craig Tiller86c0f8a2015-12-01 20:05:40 -080082
Craig Tillerfb433852016-03-29 08:51:07 -070083static void pf_destroy(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) {
Craig Tillera82950e2015-09-22 12:33:20 -070084 pick_first_lb_policy *p = (pick_first_lb_policy *)pol;
Craig Tiller86c0f8a2015-12-01 20:05:40 -080085 grpc_connected_subchannel *selected = GET_SELECTED(p);
Craig Tillerd7b68e72015-06-28 11:41:09 -070086 size_t i;
Craig Tillera82950e2015-09-22 12:33:20 -070087 GPR_ASSERT(p->pending_picks == NULL);
88 for (i = 0; i < p->num_subchannels; i++) {
89 GRPC_SUBCHANNEL_UNREF(exec_ctx, p->subchannels[i], "pick_first");
90 }
Craig Tiller86c0f8a2015-12-01 20:05:40 -080091 if (selected != NULL) {
92 GRPC_CONNECTED_SUBCHANNEL_UNREF(exec_ctx, selected, "picked_first");
Craig Tiller89a768e2015-10-06 09:55:59 -070093 }
Craig Tillera82950e2015-09-22 12:33:20 -070094 grpc_connectivity_state_destroy(exec_ctx, &p->state_tracker);
95 gpr_free(p->subchannels);
96 gpr_mu_destroy(&p->mu);
97 gpr_free(p);
Craig Tillereb3b12e2015-06-26 14:42:49 -070098}
99
Craig Tillerfb433852016-03-29 08:51:07 -0700100static void pf_shutdown(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) {
Craig Tillera82950e2015-09-22 12:33:20 -0700101 pick_first_lb_policy *p = (pick_first_lb_policy *)pol;
Craig Tillerd2cc4592015-07-01 07:50:47 -0700102 pending_pick *pp;
Craig Tiller86c0f8a2015-12-01 20:05:40 -0800103 grpc_connected_subchannel *selected;
Craig Tillera82950e2015-09-22 12:33:20 -0700104 gpr_mu_lock(&p->mu);
Craig Tiller86c0f8a2015-12-01 20:05:40 -0800105 selected = GET_SELECTED(p);
Craig Tillera14215a2015-07-17 17:21:08 -0700106 p->shutdown = 1;
Craig Tiller5795da72015-09-17 15:27:13 -0700107 pp = p->pending_picks;
108 p->pending_picks = NULL;
Craig Tiller804ff712016-05-05 16:25:40 -0700109 grpc_connectivity_state_set(
Craig Tillerd925c932016-06-06 08:38:50 -0700110 exec_ctx, &p->state_tracker, GRPC_CHANNEL_SHUTDOWN,
Craig Tiller804ff712016-05-05 16:25:40 -0700111 GRPC_ERROR_CREATE("Channel shutdown"), "shutdown");
Craig Tillerf036a642015-12-01 17:00:40 -0800112 /* cancel subscription */
Craig Tiller86c0f8a2015-12-01 20:05:40 -0800113 if (selected != NULL) {
Craig Tiller1d881fb2015-12-01 07:39:04 -0800114 grpc_connected_subchannel_notify_on_state_change(
Craig Tiller86c0f8a2015-12-01 20:05:40 -0800115 exec_ctx, selected, NULL, NULL, &p->connectivity_changed);
Craig Tillere2b86c72016-04-14 17:34:01 -0700116 } else if (p->num_subchannels > 0) {
Craig Tiller1d881fb2015-12-01 07:39:04 -0800117 grpc_subchannel_notify_on_state_change(
118 exec_ctx, p->subchannels[p->checking_subchannel], NULL, NULL,
119 &p->connectivity_changed);
Craig Tiller48613042015-11-29 14:45:11 -0800120 }
Craig Tillera82950e2015-09-22 12:33:20 -0700121 gpr_mu_unlock(&p->mu);
122 while (pp != NULL) {
123 pending_pick *next = pp->next;
124 *pp->target = NULL;
Craig Tiller91031da2016-12-28 15:44:25 -0800125 grpc_closure_sched(exec_ctx, pp->on_complete, GRPC_ERROR_NONE);
Craig Tillera82950e2015-09-22 12:33:20 -0700126 gpr_free(pp);
127 pp = next;
128 }
Craig Tillereb3b12e2015-06-26 14:42:49 -0700129}
130
Craig Tiller577c9b22015-11-02 14:11:15 -0800131static void pf_cancel_pick(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol,
Mark D. Roth5f844002016-09-08 08:20:53 -0700132 grpc_connected_subchannel **target,
133 grpc_error *error) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800134 pick_first_lb_policy *p = (pick_first_lb_policy *)pol;
135 pending_pick *pp;
136 gpr_mu_lock(&p->mu);
137 pp = p->pending_picks;
138 p->pending_picks = NULL;
139 while (pp != NULL) {
140 pending_pick *next = pp->next;
141 if (pp->target == target) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800142 *target = NULL;
Craig Tiller91031da2016-12-28 15:44:25 -0800143 grpc_closure_sched(
Mark D. Roth932b10c2016-09-09 08:44:30 -0700144 exec_ctx, pp->on_complete,
Craig Tiller91031da2016-12-28 15:44:25 -0800145 GRPC_ERROR_CREATE_REFERENCING("Pick Cancelled", &error, 1));
Craig Tiller577c9b22015-11-02 14:11:15 -0800146 gpr_free(pp);
147 } else {
148 pp->next = p->pending_picks;
149 p->pending_picks = pp;
150 }
151 pp = next;
152 }
153 gpr_mu_unlock(&p->mu);
Mark D. Roth5f844002016-09-08 08:20:53 -0700154 GRPC_ERROR_UNREF(error);
Craig Tiller577c9b22015-11-02 14:11:15 -0800155}
156
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800157static void pf_cancel_picks(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol,
158 uint32_t initial_metadata_flags_mask,
Mark D. Rothe65ff112016-09-09 13:48:38 -0700159 uint32_t initial_metadata_flags_eq,
160 grpc_error *error) {
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800161 pick_first_lb_policy *p = (pick_first_lb_policy *)pol;
162 pending_pick *pp;
163 gpr_mu_lock(&p->mu);
164 pp = p->pending_picks;
165 p->pending_picks = NULL;
166 while (pp != NULL) {
167 pending_pick *next = pp->next;
168 if ((pp->initial_metadata_flags & initial_metadata_flags_mask) ==
169 initial_metadata_flags_eq) {
Craig Tiller91031da2016-12-28 15:44:25 -0800170 grpc_closure_sched(
Mark D. Roth58f52b72016-09-09 13:55:18 -0700171 exec_ctx, pp->on_complete,
Craig Tiller91031da2016-12-28 15:44:25 -0800172 GRPC_ERROR_CREATE_REFERENCING("Pick Cancelled", &error, 1));
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800173 gpr_free(pp);
174 } else {
175 pp->next = p->pending_picks;
176 p->pending_picks = pp;
177 }
178 pp = next;
179 }
180 gpr_mu_unlock(&p->mu);
Mark D. Rothe65ff112016-09-09 13:48:38 -0700181 GRPC_ERROR_UNREF(error);
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800182}
183
Craig Tillera82950e2015-09-22 12:33:20 -0700184static void start_picking(grpc_exec_ctx *exec_ctx, pick_first_lb_policy *p) {
Craig Tiller48cb07c2015-07-15 16:16:15 -0700185 p->started_picking = 1;
186 p->checking_subchannel = 0;
187 p->checking_connectivity = GRPC_CHANNEL_IDLE;
Craig Tiller48613042015-11-29 14:45:11 -0800188 GRPC_LB_POLICY_WEAK_REF(&p->base, "pick_first_connectivity");
Craig Tillera82950e2015-09-22 12:33:20 -0700189 grpc_subchannel_notify_on_state_change(
190 exec_ctx, p->subchannels[p->checking_subchannel],
Craig Tiller69b093b2016-02-25 19:04:07 -0800191 p->base.interested_parties, &p->checking_connectivity,
Craig Tiller1d881fb2015-12-01 07:39:04 -0800192 &p->connectivity_changed);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700193}
194
Craig Tillerfb433852016-03-29 08:51:07 -0700195static void pf_exit_idle(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) {
Craig Tillera82950e2015-09-22 12:33:20 -0700196 pick_first_lb_policy *p = (pick_first_lb_policy *)pol;
197 gpr_mu_lock(&p->mu);
198 if (!p->started_picking) {
199 start_picking(exec_ctx, p);
200 }
201 gpr_mu_unlock(&p->mu);
Craig Tillereb3b12e2015-06-26 14:42:49 -0700202}
203
Craig Tillerfb433852016-03-29 08:51:07 -0700204static int pf_pick(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol,
David Garcia Quintas8aace512016-08-15 14:55:12 -0700205 const grpc_lb_policy_pick_args *pick_args,
David Garcia Quintas331b9c02016-09-12 18:37:05 -0700206 grpc_connected_subchannel **target, void **user_data,
Craig Tillerfb433852016-03-29 08:51:07 -0700207 grpc_closure *on_complete) {
Craig Tillera82950e2015-09-22 12:33:20 -0700208 pick_first_lb_policy *p = (pick_first_lb_policy *)pol;
Craig Tiller45724b32015-09-22 10:42:19 -0700209 pending_pick *pp;
Craig Tiller320bee02016-01-06 17:33:45 -0800210
211 /* Check atomically for a selected channel */
Craig Tiller86c0f8a2015-12-01 20:05:40 -0800212 grpc_connected_subchannel *selected = GET_SELECTED(p);
213 if (selected != NULL) {
Craig Tiller693d3942016-10-27 16:51:25 -0700214 *target = GRPC_CONNECTED_SUBCHANNEL_REF(selected, "picked");
Craig Tiller86c0f8a2015-12-01 20:05:40 -0800215 return 1;
216 }
Craig Tiller320bee02016-01-06 17:33:45 -0800217
218 /* No subchannel selected yet, so acquire lock and then attempt again */
Craig Tillera82950e2015-09-22 12:33:20 -0700219 gpr_mu_lock(&p->mu);
Craig Tiller86c0f8a2015-12-01 20:05:40 -0800220 selected = GET_SELECTED(p);
221 if (selected) {
Craig Tillera82950e2015-09-22 12:33:20 -0700222 gpr_mu_unlock(&p->mu);
Craig Tiller693d3942016-10-27 16:51:25 -0700223 *target = GRPC_CONNECTED_SUBCHANNEL_REF(selected, "picked");
Craig Tiller577c9b22015-11-02 14:11:15 -0800224 return 1;
Craig Tillera82950e2015-09-22 12:33:20 -0700225 } else {
226 if (!p->started_picking) {
227 start_picking(exec_ctx, p);
Craig Tiller45724b32015-09-22 10:42:19 -0700228 }
Craig Tillera82950e2015-09-22 12:33:20 -0700229 pp = gpr_malloc(sizeof(*pp));
230 pp->next = p->pending_picks;
Craig Tillera82950e2015-09-22 12:33:20 -0700231 pp->target = target;
David Garcia Quintas8aace512016-08-15 14:55:12 -0700232 pp->initial_metadata_flags = pick_args->initial_metadata_flags;
Craig Tillera82950e2015-09-22 12:33:20 -0700233 pp->on_complete = on_complete;
234 p->pending_picks = pp;
235 gpr_mu_unlock(&p->mu);
Craig Tiller577c9b22015-11-02 14:11:15 -0800236 return 0;
Craig Tillera82950e2015-09-22 12:33:20 -0700237 }
Craig Tiller45724b32015-09-22 10:42:19 -0700238}
239
Craig Tiller1f41b6b2015-10-09 15:07:02 -0700240static void destroy_subchannels(grpc_exec_ctx *exec_ctx, void *arg,
Craig Tiller804ff712016-05-05 16:25:40 -0700241 grpc_error *error) {
Craig Tillerb09d84d2015-10-06 09:12:16 -0700242 pick_first_lb_policy *p = arg;
243 size_t i;
Craig Tillerb09d84d2015-10-06 09:12:16 -0700244 size_t num_subchannels = p->num_subchannels;
245 grpc_subchannel **subchannels;
Craig Tillerb09d84d2015-10-06 09:12:16 -0700246
247 gpr_mu_lock(&p->mu);
248 subchannels = p->subchannels;
Craig Tillerb09d84d2015-10-06 09:12:16 -0700249 p->num_subchannels = 0;
250 p->subchannels = NULL;
251 gpr_mu_unlock(&p->mu);
Craig Tiller48613042015-11-29 14:45:11 -0800252 GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &p->base, "destroy_subchannels");
Craig Tillerb09d84d2015-10-06 09:12:16 -0700253
254 for (i = 0; i < num_subchannels; i++) {
Craig Tillerb09d84d2015-10-06 09:12:16 -0700255 GRPC_SUBCHANNEL_UNREF(exec_ctx, subchannels[i], "pick_first");
256 }
257
258 gpr_free(subchannels);
259}
260
Craig Tillera82950e2015-09-22 12:33:20 -0700261static void pf_connectivity_changed(grpc_exec_ctx *exec_ctx, void *arg,
Craig Tiller804ff712016-05-05 16:25:40 -0700262 grpc_error *error) {
Craig Tillereb3b12e2015-06-26 14:42:49 -0700263 pick_first_lb_policy *p = arg;
Craig Tillerb5585d42015-11-17 07:18:31 -0800264 grpc_subchannel *selected_subchannel;
Craig Tillereb3b12e2015-06-26 14:42:49 -0700265 pending_pick *pp;
Craig Tiller86c0f8a2015-12-01 20:05:40 -0800266 grpc_connected_subchannel *selected;
Craig Tillereb3b12e2015-06-26 14:42:49 -0700267
Craig Tillerf707d622016-05-06 14:26:12 -0700268 GRPC_ERROR_REF(error);
Craig Tiller804ff712016-05-05 16:25:40 -0700269
Craig Tillera82950e2015-09-22 12:33:20 -0700270 gpr_mu_lock(&p->mu);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700271
Craig Tiller86c0f8a2015-12-01 20:05:40 -0800272 selected = GET_SELECTED(p);
273
Craig Tillera82950e2015-09-22 12:33:20 -0700274 if (p->shutdown) {
275 gpr_mu_unlock(&p->mu);
Craig Tiller48613042015-11-29 14:45:11 -0800276 GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &p->base, "pick_first_connectivity");
Craig Tillerae125932016-05-13 16:34:29 -0700277 GRPC_ERROR_UNREF(error);
Craig Tillera82950e2015-09-22 12:33:20 -0700278 return;
Craig Tiller86c0f8a2015-12-01 20:05:40 -0800279 } else if (selected != NULL) {
Craig Tillercb2609f2015-11-24 17:19:19 -0800280 if (p->checking_connectivity == GRPC_CHANNEL_TRANSIENT_FAILURE) {
281 /* if the selected channel goes bad, we're done */
Craig Tiller48ed92e2016-06-02 11:07:12 -0700282 p->checking_connectivity = GRPC_CHANNEL_SHUTDOWN;
Craig Tillercb2609f2015-11-24 17:19:19 -0800283 }
Craig Tillera82950e2015-09-22 12:33:20 -0700284 grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
Craig Tillerf707d622016-05-06 14:26:12 -0700285 p->checking_connectivity, GRPC_ERROR_REF(error),
Craig Tiller804ff712016-05-05 16:25:40 -0700286 "selected_changed");
Craig Tiller48ed92e2016-06-02 11:07:12 -0700287 if (p->checking_connectivity != GRPC_CHANNEL_SHUTDOWN) {
Craig Tillerab33b482015-11-21 08:11:04 -0800288 grpc_connected_subchannel_notify_on_state_change(
Craig Tiller69b093b2016-02-25 19:04:07 -0800289 exec_ctx, selected, p->base.interested_parties,
Craig Tillera6bebf42015-12-01 17:02:35 -0800290 &p->checking_connectivity, &p->connectivity_changed);
Craig Tillera82950e2015-09-22 12:33:20 -0700291 } else {
Craig Tiller48613042015-11-29 14:45:11 -0800292 GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &p->base, "pick_first_connectivity");
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700293 }
Craig Tillera82950e2015-09-22 12:33:20 -0700294 } else {
295 loop:
296 switch (p->checking_connectivity) {
David Garcia Quintasea6689d2016-11-08 09:46:41 -0800297 case GRPC_CHANNEL_INIT:
Jan Tattermuschb0fb2d22016-11-16 14:04:05 +0100298 GPR_UNREACHABLE_CODE(return );
Craig Tillera82950e2015-09-22 12:33:20 -0700299 case GRPC_CHANNEL_READY:
300 grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
Craig Tiller804ff712016-05-05 16:25:40 -0700301 GRPC_CHANNEL_READY, GRPC_ERROR_NONE,
302 "connecting_ready");
Craig Tillerb5585d42015-11-17 07:18:31 -0800303 selected_subchannel = p->subchannels[p->checking_subchannel];
Craig Tiller81afdda2016-01-11 17:09:18 -0800304 selected =
305 grpc_subchannel_get_connected_subchannel(selected_subchannel);
Craig Tiller86c0f8a2015-12-01 20:05:40 -0800306 GPR_ASSERT(selected != NULL);
Craig Tiller86c0f8a2015-12-01 20:05:40 -0800307 GRPC_CONNECTED_SUBCHANNEL_REF(selected, "picked_first");
Craig Tillerb09d84d2015-10-06 09:12:16 -0700308 /* drop the pick list: we are connected now */
Craig Tiller48613042015-11-29 14:45:11 -0800309 GRPC_LB_POLICY_WEAK_REF(&p->base, "destroy_subchannels");
Craig Tillerd9d474a2016-01-26 06:50:51 -0800310 gpr_atm_rel_store(&p->selected, (gpr_atm)selected);
Craig Tiller91031da2016-12-28 15:44:25 -0800311 grpc_closure_sched(exec_ctx,
312 grpc_closure_create(destroy_subchannels, p,
313 grpc_schedule_on_exec_ctx),
314 GRPC_ERROR_NONE);
Craig Tillerb09d84d2015-10-06 09:12:16 -0700315 /* update any calls that were waiting for a pick */
Craig Tillera82950e2015-09-22 12:33:20 -0700316 while ((pp = p->pending_picks)) {
317 p->pending_picks = pp->next;
Craig Tiller693d3942016-10-27 16:51:25 -0700318 *pp->target = GRPC_CONNECTED_SUBCHANNEL_REF(selected, "picked");
Craig Tiller91031da2016-12-28 15:44:25 -0800319 grpc_closure_sched(exec_ctx, pp->on_complete, GRPC_ERROR_NONE);
Craig Tillera82950e2015-09-22 12:33:20 -0700320 gpr_free(pp);
321 }
Craig Tillerab33b482015-11-21 08:11:04 -0800322 grpc_connected_subchannel_notify_on_state_change(
Craig Tiller69b093b2016-02-25 19:04:07 -0800323 exec_ctx, selected, p->base.interested_parties,
Craig Tillera6bebf42015-12-01 17:02:35 -0800324 &p->checking_connectivity, &p->connectivity_changed);
Craig Tillera82950e2015-09-22 12:33:20 -0700325 break;
326 case GRPC_CHANNEL_TRANSIENT_FAILURE:
Craig Tillera82950e2015-09-22 12:33:20 -0700327 p->checking_subchannel =
328 (p->checking_subchannel + 1) % p->num_subchannels;
Craig Tiller131b6de2016-03-31 17:05:28 -0700329 if (p->checking_subchannel == 0) {
330 /* only trigger transient failure when we've tried all alternatives */
Craig Tiller804ff712016-05-05 16:25:40 -0700331 grpc_connectivity_state_set(
332 exec_ctx, &p->state_tracker, GRPC_CHANNEL_TRANSIENT_FAILURE,
Craig Tillerf707d622016-05-06 14:26:12 -0700333 GRPC_ERROR_REF(error), "connecting_transient_failure");
Craig Tiller131b6de2016-03-31 17:05:28 -0700334 }
Craig Tillerf707d622016-05-06 14:26:12 -0700335 GRPC_ERROR_UNREF(error);
Craig Tillera82950e2015-09-22 12:33:20 -0700336 p->checking_connectivity = grpc_subchannel_check_connectivity(
Craig Tiller804ff712016-05-05 16:25:40 -0700337 p->subchannels[p->checking_subchannel], &error);
Craig Tillera82950e2015-09-22 12:33:20 -0700338 if (p->checking_connectivity == GRPC_CHANNEL_TRANSIENT_FAILURE) {
339 grpc_subchannel_notify_on_state_change(
340 exec_ctx, p->subchannels[p->checking_subchannel],
Craig Tiller69b093b2016-02-25 19:04:07 -0800341 p->base.interested_parties, &p->checking_connectivity,
Craig Tiller1d881fb2015-12-01 07:39:04 -0800342 &p->connectivity_changed);
Craig Tillera82950e2015-09-22 12:33:20 -0700343 } else {
344 goto loop;
345 }
346 break;
347 case GRPC_CHANNEL_CONNECTING:
348 case GRPC_CHANNEL_IDLE:
Craig Tiller804ff712016-05-05 16:25:40 -0700349 grpc_connectivity_state_set(
350 exec_ctx, &p->state_tracker, GRPC_CHANNEL_CONNECTING,
Craig Tillerf707d622016-05-06 14:26:12 -0700351 GRPC_ERROR_REF(error), "connecting_changed");
Craig Tillera82950e2015-09-22 12:33:20 -0700352 grpc_subchannel_notify_on_state_change(
353 exec_ctx, p->subchannels[p->checking_subchannel],
Craig Tiller69b093b2016-02-25 19:04:07 -0800354 p->base.interested_parties, &p->checking_connectivity,
Craig Tiller1d881fb2015-12-01 07:39:04 -0800355 &p->connectivity_changed);
Craig Tillera82950e2015-09-22 12:33:20 -0700356 break;
Craig Tiller48ed92e2016-06-02 11:07:12 -0700357 case GRPC_CHANNEL_SHUTDOWN:
Craig Tillera82950e2015-09-22 12:33:20 -0700358 p->num_subchannels--;
Craig Tiller86c99582015-11-25 15:22:26 -0800359 GPR_SWAP(grpc_subchannel *, p->subchannels[p->checking_subchannel],
360 p->subchannels[p->num_subchannels]);
Craig Tillera82950e2015-09-22 12:33:20 -0700361 GRPC_SUBCHANNEL_UNREF(exec_ctx, p->subchannels[p->num_subchannels],
362 "pick_first");
363 if (p->num_subchannels == 0) {
Craig Tiller804ff712016-05-05 16:25:40 -0700364 grpc_connectivity_state_set(
Craig Tillerd925c932016-06-06 08:38:50 -0700365 exec_ctx, &p->state_tracker, GRPC_CHANNEL_SHUTDOWN,
Craig Tiller804ff712016-05-05 16:25:40 -0700366 GRPC_ERROR_CREATE_REFERENCING("Pick first exhausted channels",
367 &error, 1),
368 "no_more_channels");
Craig Tillera82950e2015-09-22 12:33:20 -0700369 while ((pp = p->pending_picks)) {
370 p->pending_picks = pp->next;
371 *pp->target = NULL;
Craig Tiller91031da2016-12-28 15:44:25 -0800372 grpc_closure_sched(exec_ctx, pp->on_complete, GRPC_ERROR_NONE);
Craig Tillera82950e2015-09-22 12:33:20 -0700373 gpr_free(pp);
374 }
Craig Tiller1d881fb2015-12-01 07:39:04 -0800375 GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &p->base,
376 "pick_first_connectivity");
Craig Tillera82950e2015-09-22 12:33:20 -0700377 } else {
Craig Tiller804ff712016-05-05 16:25:40 -0700378 grpc_connectivity_state_set(
379 exec_ctx, &p->state_tracker, GRPC_CHANNEL_TRANSIENT_FAILURE,
Craig Tillerf707d622016-05-06 14:26:12 -0700380 GRPC_ERROR_REF(error), "subchannel_failed");
Craig Tillera82950e2015-09-22 12:33:20 -0700381 p->checking_subchannel %= p->num_subchannels;
Craig Tillerf707d622016-05-06 14:26:12 -0700382 GRPC_ERROR_UNREF(error);
Craig Tillera82950e2015-09-22 12:33:20 -0700383 p->checking_connectivity = grpc_subchannel_check_connectivity(
Craig Tiller804ff712016-05-05 16:25:40 -0700384 p->subchannels[p->checking_subchannel], &error);
Craig Tillera82950e2015-09-22 12:33:20 -0700385 goto loop;
386 }
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700387 }
Craig Tillera82950e2015-09-22 12:33:20 -0700388 }
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700389
Craig Tillera82950e2015-09-22 12:33:20 -0700390 gpr_mu_unlock(&p->mu);
Craig Tiller804ff712016-05-05 16:25:40 -0700391
Craig Tillerf707d622016-05-06 14:26:12 -0700392 GRPC_ERROR_UNREF(error);
Craig Tillereb3b12e2015-06-26 14:42:49 -0700393}
394
Craig Tillera82950e2015-09-22 12:33:20 -0700395static grpc_connectivity_state pf_check_connectivity(grpc_exec_ctx *exec_ctx,
Craig Tiller804ff712016-05-05 16:25:40 -0700396 grpc_lb_policy *pol,
397 grpc_error **error) {
Craig Tillera82950e2015-09-22 12:33:20 -0700398 pick_first_lb_policy *p = (pick_first_lb_policy *)pol;
Craig Tillerc7b5f762015-06-27 11:48:42 -0700399 grpc_connectivity_state st;
Craig Tillera82950e2015-09-22 12:33:20 -0700400 gpr_mu_lock(&p->mu);
Craig Tiller613dafa2017-02-09 12:00:43 -0800401 st = grpc_connectivity_state_get(&p->state_tracker, error);
Craig Tillera82950e2015-09-22 12:33:20 -0700402 gpr_mu_unlock(&p->mu);
Craig Tillerc7b5f762015-06-27 11:48:42 -0700403 return st;
404}
405
Craig Tillerfb433852016-03-29 08:51:07 -0700406static void pf_notify_on_state_change(grpc_exec_ctx *exec_ctx,
407 grpc_lb_policy *pol,
408 grpc_connectivity_state *current,
409 grpc_closure *notify) {
Craig Tillera82950e2015-09-22 12:33:20 -0700410 pick_first_lb_policy *p = (pick_first_lb_policy *)pol;
411 gpr_mu_lock(&p->mu);
412 grpc_connectivity_state_notify_on_state_change(exec_ctx, &p->state_tracker,
413 current, notify);
414 gpr_mu_unlock(&p->mu);
Craig Tillerc7b5f762015-06-27 11:48:42 -0700415}
416
Craig Tillerfb433852016-03-29 08:51:07 -0700417static void pf_ping_one(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol,
418 grpc_closure *closure) {
Craig Tiller28bf8912015-12-07 16:07:04 -0800419 pick_first_lb_policy *p = (pick_first_lb_policy *)pol;
Craig Tiller093193e2016-01-07 07:14:44 -0800420 grpc_connected_subchannel *selected = GET_SELECTED(p);
421 if (selected) {
422 grpc_connected_subchannel_ping(exec_ctx, selected, closure);
Craig Tiller28bf8912015-12-07 16:07:04 -0800423 } else {
Craig Tiller91031da2016-12-28 15:44:25 -0800424 grpc_closure_sched(exec_ctx, closure, GRPC_ERROR_CREATE("Not connected"));
Craig Tiller28bf8912015-12-07 16:07:04 -0800425 }
Craig Tiller28bf8912015-12-07 16:07:04 -0800426}
427
Craig Tillereb3b12e2015-06-26 14:42:49 -0700428static const grpc_lb_policy_vtable pick_first_lb_policy_vtable = {
Craig Tillerc5ff7812016-03-28 12:45:55 -0700429 pf_destroy, pf_shutdown, pf_pick,
430 pf_cancel_pick, pf_cancel_picks, pf_ping_one,
431 pf_exit_idle, pf_check_connectivity, pf_notify_on_state_change};
Craig Tillereb3b12e2015-06-26 14:42:49 -0700432
Craig Tillera82950e2015-09-22 12:33:20 -0700433static void pick_first_factory_ref(grpc_lb_policy_factory *factory) {}
David Garcia Quintas5c4543d2015-09-03 15:49:56 -0700434
Craig Tillera82950e2015-09-22 12:33:20 -0700435static void pick_first_factory_unref(grpc_lb_policy_factory *factory) {}
David Garcia Quintas5c4543d2015-09-03 15:49:56 -0700436
David Garcia Quintas67c0d042016-03-25 01:37:53 -0700437static grpc_lb_policy *create_pick_first(grpc_exec_ctx *exec_ctx,
438 grpc_lb_policy_factory *factory,
Craig Tillera82950e2015-09-22 12:33:20 -0700439 grpc_lb_policy_args *args) {
David Garcia Quintas86fcfcc2016-03-31 23:22:28 -0700440 GPR_ASSERT(args->client_channel_factory != NULL);
David Garcia Quintas67c0d042016-03-25 01:37:53 -0700441
Mark D. Rothe011b1e2016-09-07 08:28:00 -0700442 /* Find the number of backend addresses. We ignore balancer
443 * addresses, since we don't know how to handle them. */
Mark D. Roth201db7d2016-12-12 09:36:02 -0800444 const grpc_arg *arg =
445 grpc_channel_args_find(args->args, GRPC_ARG_LB_ADDRESSES);
Mark D. Roth5bd7be02016-10-21 14:19:50 -0700446 GPR_ASSERT(arg != NULL && arg->type == GRPC_ARG_POINTER);
Mark D. Roth557c9902016-10-24 11:12:05 -0700447 grpc_lb_addresses *addresses = arg->value.pointer.p;
Mark D. Rothf655c852016-09-06 10:40:38 -0700448 size_t num_addrs = 0;
Mark D. Roth5bd7be02016-10-21 14:19:50 -0700449 for (size_t i = 0; i < addresses->num_addresses; i++) {
450 if (!addresses->addresses[i].is_balancer) ++num_addrs;
Mark D. Rothf655c852016-09-06 10:40:38 -0700451 }
452 if (num_addrs == 0) return NULL;
David Garcia Quintas67c0d042016-03-25 01:37:53 -0700453
Craig Tillera82950e2015-09-22 12:33:20 -0700454 pick_first_lb_policy *p = gpr_malloc(sizeof(*p));
Craig Tillera82950e2015-09-22 12:33:20 -0700455 memset(p, 0, sizeof(*p));
David Garcia Quintas67c0d042016-03-25 01:37:53 -0700456
Mark D. Roth989cdcd2016-09-06 13:28:28 -0700457 p->subchannels = gpr_malloc(sizeof(grpc_subchannel *) * num_addrs);
Mark D. Rothf655c852016-09-06 10:40:38 -0700458 memset(p->subchannels, 0, sizeof(*p->subchannels) * num_addrs);
David Garcia Quintas67c0d042016-03-25 01:37:53 -0700459 grpc_subchannel_args sc_args;
460 size_t subchannel_idx = 0;
Mark D. Roth5bd7be02016-10-21 14:19:50 -0700461 for (size_t i = 0; i < addresses->num_addresses; i++) {
Mark D. Rothe011b1e2016-09-07 08:28:00 -0700462 /* Skip balancer addresses, since we only know how to handle backends. */
Mark D. Roth5bd7be02016-10-21 14:19:50 -0700463 if (addresses->addresses[i].is_balancer) continue;
Mark D. Rothf655c852016-09-06 10:40:38 -0700464
Mark D. Roth5bd7be02016-10-21 14:19:50 -0700465 if (addresses->addresses[i].user_data != NULL) {
David Garcia Quintas5ebb7af2016-09-15 10:02:16 -0700466 gpr_log(GPR_ERROR,
467 "This LB policy doesn't support user data. It will be ignored");
468 }
David Garcia Quintasf47d6fb2016-09-14 12:59:17 -0700469
David Garcia Quintas67c0d042016-03-25 01:37:53 -0700470 memset(&sc_args, 0, sizeof(grpc_subchannel_args));
Mark D. Rothdf8f1222017-01-13 22:59:39 +0000471 grpc_arg addr_arg =
472 grpc_create_subchannel_address_arg(&addresses->addresses[i].address);
Mark D. Roth0748f392017-01-13 09:22:44 -0800473 grpc_channel_args *new_args =
474 grpc_channel_args_copy_and_add(args->args, &addr_arg, 1);
475 gpr_free(addr_arg.value.string);
476 sc_args.args = new_args;
David Garcia Quintas86fcfcc2016-03-31 23:22:28 -0700477 grpc_subchannel *subchannel = grpc_client_channel_factory_create_subchannel(
478 exec_ctx, args->client_channel_factory, &sc_args);
Mark D. Roth0748f392017-01-13 09:22:44 -0800479 grpc_channel_args_destroy(exec_ctx, new_args);
David Garcia Quintas67c0d042016-03-25 01:37:53 -0700480
481 if (subchannel != NULL) {
482 p->subchannels[subchannel_idx++] = subchannel;
483 }
484 }
485 if (subchannel_idx == 0) {
486 gpr_free(p->subchannels);
487 gpr_free(p);
488 return NULL;
489 }
490 p->num_subchannels = subchannel_idx;
491
492 grpc_lb_policy_init(&p->base, &pick_first_lb_policy_vtable);
Craig Tiller91031da2016-12-28 15:44:25 -0800493 grpc_closure_init(&p->connectivity_changed, pf_connectivity_changed, p,
494 grpc_schedule_on_exec_ctx);
Craig Tillera82950e2015-09-22 12:33:20 -0700495 gpr_mu_init(&p->mu);
Craig Tillereb3b12e2015-06-26 14:42:49 -0700496 return &p->base;
497}
David Garcia Quintas5c4543d2015-09-03 15:49:56 -0700498
499static const grpc_lb_policy_factory_vtable pick_first_factory_vtable = {
Craig Tillera82950e2015-09-22 12:33:20 -0700500 pick_first_factory_ref, pick_first_factory_unref, create_pick_first,
501 "pick_first"};
David Garcia Quintas5c4543d2015-09-03 15:49:56 -0700502
503static grpc_lb_policy_factory pick_first_lb_policy_factory = {
Craig Tillera82950e2015-09-22 12:33:20 -0700504 &pick_first_factory_vtable};
David Garcia Quintas5c4543d2015-09-03 15:49:56 -0700505
Craig Tillerfb433852016-03-29 08:51:07 -0700506static grpc_lb_policy_factory *pick_first_lb_factory_create() {
David Garcia Quintas5c4543d2015-09-03 15:49:56 -0700507 return &pick_first_lb_policy_factory;
508}
Craig Tillerfb433852016-03-29 08:51:07 -0700509
510/* Plugin registration */
511
512void grpc_lb_policy_pick_first_init() {
Craig Tiller3113ef42016-03-29 09:03:14 -0700513 grpc_register_lb_policy(pick_first_lb_factory_create());
Craig Tillerfb433852016-03-29 08:51:07 -0700514}
515
516void grpc_lb_policy_pick_first_shutdown() {}