blob: beacffcf233ae14d2dc5bd734ffebef4f1456833 [file] [log] [blame]
Craig Tiller3bc8ebd2015-06-24 15:41:15 -07001/*
2 *
3 * Copyright 2015, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
David Garcia Quintas5c4543d2015-09-03 15:49:56 -070034#include "src/core/client_config/lb_policy_factory.h"
Craig Tiller3bc8ebd2015-06-24 15:41:15 -070035#include "src/core/client_config/lb_policies/pick_first.h"
Craig Tillereb3b12e2015-06-26 14:42:49 -070036
37#include <string.h>
38
39#include <grpc/support/alloc.h>
Craig Tiller08a1cf82015-06-29 09:37:52 -070040#include "src/core/transport/connectivity_state.h"
Craig Tillereb3b12e2015-06-26 14:42:49 -070041
Craig Tillera82950e2015-09-22 12:33:20 -070042typedef struct pending_pick {
Craig Tillereb3b12e2015-06-26 14:42:49 -070043 struct pending_pick *next;
44 grpc_pollset *pollset;
Craig Tillerb5585d42015-11-17 07:18:31 -080045 grpc_connected_subchannel **target;
Craig Tiller33825112015-09-18 07:44:19 -070046 grpc_closure *on_complete;
Craig Tillereb3b12e2015-06-26 14:42:49 -070047} pending_pick;
48
Craig Tillera82950e2015-09-22 12:33:20 -070049typedef struct {
Craig Tillereb3b12e2015-06-26 14:42:49 -070050 /** base policy: must be first */
51 grpc_lb_policy base;
Craig Tillereb3b12e2015-06-26 14:42:49 -070052 /** all our subchannels */
53 grpc_subchannel **subchannels;
54 size_t num_subchannels;
55
Craig Tiller33825112015-09-18 07:44:19 -070056 grpc_closure connectivity_changed;
Craig Tillereb3b12e2015-06-26 14:42:49 -070057
58 /** mutex protecting remaining members */
59 gpr_mu mu;
Craig Tiller86c0f8a2015-12-01 20:05:40 -080060 /** the selected channel (a grpc_connected_subchannel) */
61 gpr_atm selected;
Craig Tillereb3b12e2015-06-26 14:42:49 -070062 /** have we started picking? */
63 int started_picking;
Craig Tillera14215a2015-07-17 17:21:08 -070064 /** are we shut down? */
65 int shutdown;
Craig Tillereb3b12e2015-06-26 14:42:49 -070066 /** which subchannel are we watching? */
67 size_t checking_subchannel;
68 /** what is the connectivity of that channel? */
69 grpc_connectivity_state checking_connectivity;
70 /** list of picks that are waiting on connectivity */
71 pending_pick *pending_picks;
Craig Tillerc7b5f762015-06-27 11:48:42 -070072
73 /** our connectivity state tracker */
74 grpc_connectivity_state_tracker state_tracker;
Craig Tillereb3b12e2015-06-26 14:42:49 -070075} pick_first_lb_policy;
76
Craig Tiller86c0f8a2015-12-01 20:05:40 -080077#define GET_SELECTED(p) ((grpc_connected_subchannel *)gpr_atm_no_barrier_load(&(p)->selected))
78
Craig Tillera82950e2015-09-22 12:33:20 -070079void pf_destroy(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) {
80 pick_first_lb_policy *p = (pick_first_lb_policy *)pol;
Craig Tiller86c0f8a2015-12-01 20:05:40 -080081 grpc_connected_subchannel *selected = GET_SELECTED(p);
Craig Tillerd7b68e72015-06-28 11:41:09 -070082 size_t i;
Craig Tillera82950e2015-09-22 12:33:20 -070083 GPR_ASSERT(p->pending_picks == NULL);
84 for (i = 0; i < p->num_subchannels; i++) {
85 GRPC_SUBCHANNEL_UNREF(exec_ctx, p->subchannels[i], "pick_first");
86 }
Craig Tiller86c0f8a2015-12-01 20:05:40 -080087 if (selected != NULL) {
88 GRPC_CONNECTED_SUBCHANNEL_UNREF(exec_ctx, selected, "picked_first");
Craig Tiller89a768e2015-10-06 09:55:59 -070089 }
Craig Tillera82950e2015-09-22 12:33:20 -070090 grpc_connectivity_state_destroy(exec_ctx, &p->state_tracker);
91 gpr_free(p->subchannels);
92 gpr_mu_destroy(&p->mu);
93 gpr_free(p);
Craig Tillereb3b12e2015-06-26 14:42:49 -070094}
95
Craig Tillera82950e2015-09-22 12:33:20 -070096void pf_shutdown(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) {
97 pick_first_lb_policy *p = (pick_first_lb_policy *)pol;
Craig Tillerd2cc4592015-07-01 07:50:47 -070098 pending_pick *pp;
Craig Tiller86c0f8a2015-12-01 20:05:40 -080099 grpc_connected_subchannel *selected;
Craig Tillera82950e2015-09-22 12:33:20 -0700100 gpr_mu_lock(&p->mu);
Craig Tiller86c0f8a2015-12-01 20:05:40 -0800101 selected = GET_SELECTED(p);
Craig Tillera14215a2015-07-17 17:21:08 -0700102 p->shutdown = 1;
Craig Tiller5795da72015-09-17 15:27:13 -0700103 pp = p->pending_picks;
104 p->pending_picks = NULL;
Craig Tillera82950e2015-09-22 12:33:20 -0700105 grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
106 GRPC_CHANNEL_FATAL_FAILURE, "shutdown");
Craig Tillerf036a642015-12-01 17:00:40 -0800107 /* cancel subscription */
Craig Tiller86c0f8a2015-12-01 20:05:40 -0800108 if (selected != NULL) {
Craig Tiller1d881fb2015-12-01 07:39:04 -0800109 grpc_connected_subchannel_notify_on_state_change(
Craig Tiller86c0f8a2015-12-01 20:05:40 -0800110 exec_ctx, selected, NULL, NULL, &p->connectivity_changed);
Craig Tiller48613042015-11-29 14:45:11 -0800111 } else {
Craig Tiller1d881fb2015-12-01 07:39:04 -0800112 grpc_subchannel_notify_on_state_change(
113 exec_ctx, p->subchannels[p->checking_subchannel], NULL, NULL,
114 &p->connectivity_changed);
Craig Tiller48613042015-11-29 14:45:11 -0800115 }
Craig Tillera82950e2015-09-22 12:33:20 -0700116 gpr_mu_unlock(&p->mu);
117 while (pp != NULL) {
118 pending_pick *next = pp->next;
119 *pp->target = NULL;
Craig Tiller1d881fb2015-12-01 07:39:04 -0800120 grpc_pollset_set_del_pollset(exec_ctx, &p->base.interested_parties,
121 pp->pollset);
Craig Tillera82950e2015-09-22 12:33:20 -0700122 grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, 1);
123 gpr_free(pp);
124 pp = next;
125 }
Craig Tillereb3b12e2015-06-26 14:42:49 -0700126}
127
Craig Tiller577c9b22015-11-02 14:11:15 -0800128static void pf_cancel_pick(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol,
Craig Tillerb5585d42015-11-17 07:18:31 -0800129 grpc_connected_subchannel **target) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800130 pick_first_lb_policy *p = (pick_first_lb_policy *)pol;
131 pending_pick *pp;
132 gpr_mu_lock(&p->mu);
133 pp = p->pending_picks;
134 p->pending_picks = NULL;
135 while (pp != NULL) {
136 pending_pick *next = pp->next;
137 if (pp->target == target) {
Craig Tiller1d881fb2015-12-01 07:39:04 -0800138 grpc_pollset_set_del_pollset(exec_ctx, &p->base.interested_parties,
139 pp->pollset);
Craig Tiller577c9b22015-11-02 14:11:15 -0800140 *target = NULL;
141 grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, 0);
142 gpr_free(pp);
143 } else {
144 pp->next = p->pending_picks;
145 p->pending_picks = pp;
146 }
147 pp = next;
148 }
149 gpr_mu_unlock(&p->mu);
150}
151
Craig Tillera82950e2015-09-22 12:33:20 -0700152static void start_picking(grpc_exec_ctx *exec_ctx, pick_first_lb_policy *p) {
Craig Tiller48cb07c2015-07-15 16:16:15 -0700153 p->started_picking = 1;
154 p->checking_subchannel = 0;
155 p->checking_connectivity = GRPC_CHANNEL_IDLE;
Craig Tiller48613042015-11-29 14:45:11 -0800156 GRPC_LB_POLICY_WEAK_REF(&p->base, "pick_first_connectivity");
Craig Tillera82950e2015-09-22 12:33:20 -0700157 grpc_subchannel_notify_on_state_change(
158 exec_ctx, p->subchannels[p->checking_subchannel],
Craig Tiller1d881fb2015-12-01 07:39:04 -0800159 &p->base.interested_parties, &p->checking_connectivity,
160 &p->connectivity_changed);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700161}
162
Craig Tillera82950e2015-09-22 12:33:20 -0700163void pf_exit_idle(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) {
164 pick_first_lb_policy *p = (pick_first_lb_policy *)pol;
165 gpr_mu_lock(&p->mu);
166 if (!p->started_picking) {
167 start_picking(exec_ctx, p);
168 }
169 gpr_mu_unlock(&p->mu);
Craig Tillereb3b12e2015-06-26 14:42:49 -0700170}
171
Craig Tiller577c9b22015-11-02 14:11:15 -0800172int pf_pick(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, grpc_pollset *pollset,
Craig Tillerab33b482015-11-21 08:11:04 -0800173 grpc_metadata_batch *initial_metadata,
174 grpc_connected_subchannel **target, grpc_closure *on_complete) {
Craig Tillera82950e2015-09-22 12:33:20 -0700175 pick_first_lb_policy *p = (pick_first_lb_policy *)pol;
Craig Tiller45724b32015-09-22 10:42:19 -0700176 pending_pick *pp;
Craig Tiller86c0f8a2015-12-01 20:05:40 -0800177 grpc_connected_subchannel *selected = GET_SELECTED(p);
178 if (selected != NULL) {
179 *target = selected;
180 return 1;
181 }
Craig Tillera82950e2015-09-22 12:33:20 -0700182 gpr_mu_lock(&p->mu);
Craig Tiller86c0f8a2015-12-01 20:05:40 -0800183 selected = GET_SELECTED(p);
184 if (selected) {
Craig Tillera82950e2015-09-22 12:33:20 -0700185 gpr_mu_unlock(&p->mu);
Craig Tiller86c0f8a2015-12-01 20:05:40 -0800186 *target = selected;
Craig Tiller577c9b22015-11-02 14:11:15 -0800187 return 1;
Craig Tillera82950e2015-09-22 12:33:20 -0700188 } else {
189 if (!p->started_picking) {
190 start_picking(exec_ctx, p);
Craig Tiller45724b32015-09-22 10:42:19 -0700191 }
Craig Tiller1d881fb2015-12-01 07:39:04 -0800192 grpc_pollset_set_add_pollset(exec_ctx, &p->base.interested_parties,
193 pollset);
Craig Tillera82950e2015-09-22 12:33:20 -0700194 pp = gpr_malloc(sizeof(*pp));
195 pp->next = p->pending_picks;
196 pp->pollset = pollset;
197 pp->target = target;
198 pp->on_complete = on_complete;
199 p->pending_picks = pp;
200 gpr_mu_unlock(&p->mu);
Craig Tiller577c9b22015-11-02 14:11:15 -0800201 return 0;
Craig Tillera82950e2015-09-22 12:33:20 -0700202 }
Craig Tiller45724b32015-09-22 10:42:19 -0700203}
204
Craig Tiller1f41b6b2015-10-09 15:07:02 -0700205static void destroy_subchannels(grpc_exec_ctx *exec_ctx, void *arg,
Craig Tillerb09d84d2015-10-06 09:12:16 -0700206 int iomgr_success) {
207 pick_first_lb_policy *p = arg;
208 size_t i;
Craig Tillerb09d84d2015-10-06 09:12:16 -0700209 size_t num_subchannels = p->num_subchannels;
210 grpc_subchannel **subchannels;
Craig Tillerb09d84d2015-10-06 09:12:16 -0700211
212 gpr_mu_lock(&p->mu);
213 subchannels = p->subchannels;
Craig Tillerb09d84d2015-10-06 09:12:16 -0700214 p->num_subchannels = 0;
215 p->subchannels = NULL;
216 gpr_mu_unlock(&p->mu);
Craig Tiller48613042015-11-29 14:45:11 -0800217 GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &p->base, "destroy_subchannels");
Craig Tillerb09d84d2015-10-06 09:12:16 -0700218
219 for (i = 0; i < num_subchannels; i++) {
Craig Tillerb09d84d2015-10-06 09:12:16 -0700220 GRPC_SUBCHANNEL_UNREF(exec_ctx, subchannels[i], "pick_first");
221 }
222
223 gpr_free(subchannels);
224}
225
Craig Tillera82950e2015-09-22 12:33:20 -0700226static void pf_connectivity_changed(grpc_exec_ctx *exec_ctx, void *arg,
227 int iomgr_success) {
Craig Tillereb3b12e2015-06-26 14:42:49 -0700228 pick_first_lb_policy *p = arg;
Craig Tillerb5585d42015-11-17 07:18:31 -0800229 grpc_subchannel *selected_subchannel;
Craig Tillereb3b12e2015-06-26 14:42:49 -0700230 pending_pick *pp;
Craig Tiller86c0f8a2015-12-01 20:05:40 -0800231 grpc_connected_subchannel *selected;
Craig Tillereb3b12e2015-06-26 14:42:49 -0700232
Craig Tillera82950e2015-09-22 12:33:20 -0700233 gpr_mu_lock(&p->mu);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700234
Craig Tiller86c0f8a2015-12-01 20:05:40 -0800235 selected = GET_SELECTED(p);
236
Craig Tillera82950e2015-09-22 12:33:20 -0700237 if (p->shutdown) {
238 gpr_mu_unlock(&p->mu);
Craig Tiller48613042015-11-29 14:45:11 -0800239 GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &p->base, "pick_first_connectivity");
Craig Tillera82950e2015-09-22 12:33:20 -0700240 return;
Craig Tiller86c0f8a2015-12-01 20:05:40 -0800241 } else if (selected != NULL) {
Craig Tillercb2609f2015-11-24 17:19:19 -0800242 if (p->checking_connectivity == GRPC_CHANNEL_TRANSIENT_FAILURE) {
243 /* if the selected channel goes bad, we're done */
244 p->checking_connectivity = GRPC_CHANNEL_FATAL_FAILURE;
245 }
Craig Tillera82950e2015-09-22 12:33:20 -0700246 grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
247 p->checking_connectivity, "selected_changed");
248 if (p->checking_connectivity != GRPC_CHANNEL_FATAL_FAILURE) {
Craig Tillerab33b482015-11-21 08:11:04 -0800249 grpc_connected_subchannel_notify_on_state_change(
Craig Tiller86c0f8a2015-12-01 20:05:40 -0800250 exec_ctx, selected, &p->base.interested_parties,
Craig Tillera6bebf42015-12-01 17:02:35 -0800251 &p->checking_connectivity, &p->connectivity_changed);
Craig Tillera82950e2015-09-22 12:33:20 -0700252 } else {
Craig Tiller48613042015-11-29 14:45:11 -0800253 GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &p->base, "pick_first_connectivity");
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700254 }
Craig Tillera82950e2015-09-22 12:33:20 -0700255 } else {
256 loop:
257 switch (p->checking_connectivity) {
258 case GRPC_CHANNEL_READY:
259 grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
260 GRPC_CHANNEL_READY, "connecting_ready");
Craig Tillerb5585d42015-11-17 07:18:31 -0800261 selected_subchannel = p->subchannels[p->checking_subchannel];
Craig Tiller86c0f8a2015-12-01 20:05:40 -0800262 selected = grpc_subchannel_get_connected_subchannel(selected_subchannel);
263 GPR_ASSERT(selected != NULL);
264 gpr_atm_no_barrier_store(&p->selected, (gpr_atm)selected);
265 GRPC_CONNECTED_SUBCHANNEL_REF(selected, "picked_first");
Craig Tillerb09d84d2015-10-06 09:12:16 -0700266 /* drop the pick list: we are connected now */
Craig Tiller48613042015-11-29 14:45:11 -0800267 GRPC_LB_POLICY_WEAK_REF(&p->base, "destroy_subchannels");
Craig Tiller1f41b6b2015-10-09 15:07:02 -0700268 grpc_exec_ctx_enqueue(exec_ctx,
269 grpc_closure_create(destroy_subchannels, p), 1);
Craig Tillerb09d84d2015-10-06 09:12:16 -0700270 /* update any calls that were waiting for a pick */
Craig Tillera82950e2015-09-22 12:33:20 -0700271 while ((pp = p->pending_picks)) {
272 p->pending_picks = pp->next;
Craig Tiller86c0f8a2015-12-01 20:05:40 -0800273 *pp->target = selected;
Craig Tiller1d881fb2015-12-01 07:39:04 -0800274 grpc_pollset_set_del_pollset(exec_ctx, &p->base.interested_parties,
275 pp->pollset);
Craig Tillera82950e2015-09-22 12:33:20 -0700276 grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, 1);
277 gpr_free(pp);
278 }
Craig Tillerab33b482015-11-21 08:11:04 -0800279 grpc_connected_subchannel_notify_on_state_change(
Craig Tiller86c0f8a2015-12-01 20:05:40 -0800280 exec_ctx, selected, &p->base.interested_parties,
Craig Tillera6bebf42015-12-01 17:02:35 -0800281 &p->checking_connectivity, &p->connectivity_changed);
Craig Tillera82950e2015-09-22 12:33:20 -0700282 break;
283 case GRPC_CHANNEL_TRANSIENT_FAILURE:
284 grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
285 GRPC_CHANNEL_TRANSIENT_FAILURE,
286 "connecting_transient_failure");
Craig Tillera82950e2015-09-22 12:33:20 -0700287 p->checking_subchannel =
288 (p->checking_subchannel + 1) % p->num_subchannels;
289 p->checking_connectivity = grpc_subchannel_check_connectivity(
290 p->subchannels[p->checking_subchannel]);
Craig Tillera82950e2015-09-22 12:33:20 -0700291 if (p->checking_connectivity == GRPC_CHANNEL_TRANSIENT_FAILURE) {
292 grpc_subchannel_notify_on_state_change(
293 exec_ctx, p->subchannels[p->checking_subchannel],
Craig Tiller1d881fb2015-12-01 07:39:04 -0800294 &p->base.interested_parties, &p->checking_connectivity,
295 &p->connectivity_changed);
Craig Tillera82950e2015-09-22 12:33:20 -0700296 } else {
297 goto loop;
298 }
299 break;
300 case GRPC_CHANNEL_CONNECTING:
301 case GRPC_CHANNEL_IDLE:
302 grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
303 GRPC_CHANNEL_CONNECTING,
304 "connecting_changed");
305 grpc_subchannel_notify_on_state_change(
306 exec_ctx, p->subchannels[p->checking_subchannel],
Craig Tiller1d881fb2015-12-01 07:39:04 -0800307 &p->base.interested_parties, &p->checking_connectivity,
308 &p->connectivity_changed);
Craig Tillera82950e2015-09-22 12:33:20 -0700309 break;
310 case GRPC_CHANNEL_FATAL_FAILURE:
Craig Tillera82950e2015-09-22 12:33:20 -0700311 p->num_subchannels--;
Craig Tiller86c99582015-11-25 15:22:26 -0800312 GPR_SWAP(grpc_subchannel *, p->subchannels[p->checking_subchannel],
313 p->subchannels[p->num_subchannels]);
Craig Tillera82950e2015-09-22 12:33:20 -0700314 GRPC_SUBCHANNEL_UNREF(exec_ctx, p->subchannels[p->num_subchannels],
315 "pick_first");
316 if (p->num_subchannels == 0) {
317 grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
318 GRPC_CHANNEL_FATAL_FAILURE,
319 "no_more_channels");
320 while ((pp = p->pending_picks)) {
321 p->pending_picks = pp->next;
322 *pp->target = NULL;
323 grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, 1);
324 gpr_free(pp);
325 }
Craig Tiller1d881fb2015-12-01 07:39:04 -0800326 GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &p->base,
327 "pick_first_connectivity");
Craig Tillera82950e2015-09-22 12:33:20 -0700328 } else {
329 grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
330 GRPC_CHANNEL_TRANSIENT_FAILURE,
331 "subchannel_failed");
332 p->checking_subchannel %= p->num_subchannels;
333 p->checking_connectivity = grpc_subchannel_check_connectivity(
334 p->subchannels[p->checking_subchannel]);
Craig Tillera82950e2015-09-22 12:33:20 -0700335 goto loop;
336 }
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700337 }
Craig Tillera82950e2015-09-22 12:33:20 -0700338 }
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700339
Craig Tillera82950e2015-09-22 12:33:20 -0700340 gpr_mu_unlock(&p->mu);
Craig Tillereb3b12e2015-06-26 14:42:49 -0700341}
342
Craig Tillera82950e2015-09-22 12:33:20 -0700343static grpc_connectivity_state pf_check_connectivity(grpc_exec_ctx *exec_ctx,
344 grpc_lb_policy *pol) {
345 pick_first_lb_policy *p = (pick_first_lb_policy *)pol;
Craig Tillerc7b5f762015-06-27 11:48:42 -0700346 grpc_connectivity_state st;
Craig Tillera82950e2015-09-22 12:33:20 -0700347 gpr_mu_lock(&p->mu);
348 st = grpc_connectivity_state_check(&p->state_tracker);
349 gpr_mu_unlock(&p->mu);
Craig Tillerc7b5f762015-06-27 11:48:42 -0700350 return st;
351}
352
Craig Tillera82950e2015-09-22 12:33:20 -0700353void pf_notify_on_state_change(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol,
354 grpc_connectivity_state *current,
355 grpc_closure *notify) {
356 pick_first_lb_policy *p = (pick_first_lb_policy *)pol;
357 gpr_mu_lock(&p->mu);
358 grpc_connectivity_state_notify_on_state_change(exec_ctx, &p->state_tracker,
359 current, notify);
360 gpr_mu_unlock(&p->mu);
Craig Tillerc7b5f762015-06-27 11:48:42 -0700361}
362
Craig Tillereb3b12e2015-06-26 14:42:49 -0700363static const grpc_lb_policy_vtable pick_first_lb_policy_vtable = {
Craig Tiller577c9b22015-11-02 14:11:15 -0800364 pf_destroy, pf_shutdown, pf_pick, pf_cancel_pick, pf_exit_idle,
Craig Tiller50ec2672015-11-27 21:45:11 -0800365 pf_check_connectivity, pf_notify_on_state_change};
Craig Tillereb3b12e2015-06-26 14:42:49 -0700366
Craig Tillera82950e2015-09-22 12:33:20 -0700367static void pick_first_factory_ref(grpc_lb_policy_factory *factory) {}
David Garcia Quintas5c4543d2015-09-03 15:49:56 -0700368
Craig Tillera82950e2015-09-22 12:33:20 -0700369static void pick_first_factory_unref(grpc_lb_policy_factory *factory) {}
David Garcia Quintas5c4543d2015-09-03 15:49:56 -0700370
Craig Tillera82950e2015-09-22 12:33:20 -0700371static grpc_lb_policy *create_pick_first(grpc_lb_policy_factory *factory,
372 grpc_lb_policy_args *args) {
373 pick_first_lb_policy *p = gpr_malloc(sizeof(*p));
374 GPR_ASSERT(args->num_subchannels > 0);
375 memset(p, 0, sizeof(*p));
376 grpc_lb_policy_init(&p->base, &pick_first_lb_policy_vtable);
377 p->subchannels =
378 gpr_malloc(sizeof(grpc_subchannel *) * args->num_subchannels);
David Garcia Quintasc7705c72015-09-09 17:21:11 -0700379 p->num_subchannels = args->num_subchannels;
Craig Tillera82950e2015-09-22 12:33:20 -0700380 grpc_connectivity_state_init(&p->state_tracker, GRPC_CHANNEL_IDLE,
381 "pick_first");
382 memcpy(p->subchannels, args->subchannels,
383 sizeof(grpc_subchannel *) * args->num_subchannels);
384 grpc_closure_init(&p->connectivity_changed, pf_connectivity_changed, p);
385 gpr_mu_init(&p->mu);
Craig Tillereb3b12e2015-06-26 14:42:49 -0700386 return &p->base;
387}
David Garcia Quintas5c4543d2015-09-03 15:49:56 -0700388
389static const grpc_lb_policy_factory_vtable pick_first_factory_vtable = {
Craig Tillera82950e2015-09-22 12:33:20 -0700390 pick_first_factory_ref, pick_first_factory_unref, create_pick_first,
391 "pick_first"};
David Garcia Quintas5c4543d2015-09-03 15:49:56 -0700392
393static grpc_lb_policy_factory pick_first_lb_policy_factory = {
Craig Tillera82950e2015-09-22 12:33:20 -0700394 &pick_first_factory_vtable};
David Garcia Quintas5c4543d2015-09-03 15:49:56 -0700395
Craig Tillera82950e2015-09-22 12:33:20 -0700396grpc_lb_policy_factory *grpc_pick_first_lb_factory_create() {
David Garcia Quintas5c4543d2015-09-03 15:49:56 -0700397 return &pick_first_lb_policy_factory;
398}