blob: a622d98317cac920b8cb323ac07f288e21c12f73 [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;
60 /** the selected channel
61 TODO(ctiller): this should be atomically set so we don't
62 need to take a mutex in the common case */
Craig Tillerb5585d42015-11-17 07:18:31 -080063 grpc_connected_subchannel *selected;
Craig Tillereb3b12e2015-06-26 14:42:49 -070064 /** have we started picking? */
65 int started_picking;
Craig Tillera14215a2015-07-17 17:21:08 -070066 /** are we shut down? */
67 int shutdown;
Craig Tillereb3b12e2015-06-26 14:42:49 -070068 /** which subchannel are we watching? */
69 size_t checking_subchannel;
70 /** what is the connectivity of that channel? */
71 grpc_connectivity_state checking_connectivity;
72 /** list of picks that are waiting on connectivity */
73 pending_pick *pending_picks;
Craig Tillerc7b5f762015-06-27 11:48:42 -070074
75 /** our connectivity state tracker */
76 grpc_connectivity_state_tracker state_tracker;
Craig Tillereb3b12e2015-06-26 14:42:49 -070077} pick_first_lb_policy;
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 Tillerd7b68e72015-06-28 11:41:09 -070081 size_t i;
Craig Tillera82950e2015-09-22 12:33:20 -070082 GPR_ASSERT(p->pending_picks == NULL);
83 for (i = 0; i < p->num_subchannels; i++) {
84 GRPC_SUBCHANNEL_UNREF(exec_ctx, p->subchannels[i], "pick_first");
85 }
Craig Tiller89a768e2015-10-06 09:55:59 -070086 if (p->selected) {
Craig Tillerb5585d42015-11-17 07:18:31 -080087 GRPC_CONNECTED_SUBCHANNEL_UNREF(exec_ctx, p->selected, "picked_first");
Craig Tiller89a768e2015-10-06 09:55:59 -070088 }
Craig Tillera82950e2015-09-22 12:33:20 -070089 grpc_connectivity_state_destroy(exec_ctx, &p->state_tracker);
90 gpr_free(p->subchannels);
91 gpr_mu_destroy(&p->mu);
92 gpr_free(p);
Craig Tillereb3b12e2015-06-26 14:42:49 -070093}
94
Craig Tillera82950e2015-09-22 12:33:20 -070095void pf_shutdown(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) {
96 pick_first_lb_policy *p = (pick_first_lb_policy *)pol;
Craig Tillerd2cc4592015-07-01 07:50:47 -070097 pending_pick *pp;
Craig Tillera82950e2015-09-22 12:33:20 -070098 gpr_mu_lock(&p->mu);
Craig Tiller48613042015-11-29 14:45:11 -080099 gpr_log(GPR_DEBUG, "LB_POLICY: pf_shutdown: %p", p);
Craig Tillera14215a2015-07-17 17:21:08 -0700100 p->shutdown = 1;
Craig Tiller5795da72015-09-17 15:27:13 -0700101 pp = p->pending_picks;
102 p->pending_picks = NULL;
Craig Tillera82950e2015-09-22 12:33:20 -0700103 grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
104 GRPC_CHANNEL_FATAL_FAILURE, "shutdown");
Craig Tiller48613042015-11-29 14:45:11 -0800105 if (p->selected != NULL) {
106 grpc_connected_subchannel_notify_on_state_change(exec_ctx, p->selected, NULL, &p->connectivity_changed);
107 } else {
108 grpc_subchannel_notify_on_state_change(exec_ctx, p->subchannels[p->checking_connectivity], NULL, NULL, &p->connectivity_changed);
109 }
Craig Tillera82950e2015-09-22 12:33:20 -0700110 gpr_mu_unlock(&p->mu);
111 while (pp != NULL) {
112 pending_pick *next = pp->next;
113 *pp->target = NULL;
Craig Tiller86c99582015-11-25 15:22:26 -0800114 grpc_pollset_set_del_pollset(exec_ctx, &p->base.interested_parties, pp->pollset);
Craig Tillera82950e2015-09-22 12:33:20 -0700115 grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, 1);
116 gpr_free(pp);
117 pp = next;
118 }
Craig Tillereb3b12e2015-06-26 14:42:49 -0700119}
120
Craig Tiller577c9b22015-11-02 14:11:15 -0800121static void pf_cancel_pick(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol,
Craig Tillerb5585d42015-11-17 07:18:31 -0800122 grpc_connected_subchannel **target) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800123 pick_first_lb_policy *p = (pick_first_lb_policy *)pol;
124 pending_pick *pp;
125 gpr_mu_lock(&p->mu);
126 pp = p->pending_picks;
127 p->pending_picks = NULL;
128 while (pp != NULL) {
129 pending_pick *next = pp->next;
130 if (pp->target == target) {
Craig Tiller86c99582015-11-25 15:22:26 -0800131 grpc_pollset_set_del_pollset(exec_ctx, &p->base.interested_parties, pp->pollset);
Craig Tiller577c9b22015-11-02 14:11:15 -0800132 *target = NULL;
133 grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, 0);
134 gpr_free(pp);
135 } else {
136 pp->next = p->pending_picks;
137 p->pending_picks = pp;
138 }
139 pp = next;
140 }
141 gpr_mu_unlock(&p->mu);
142}
143
Craig Tillera82950e2015-09-22 12:33:20 -0700144static void start_picking(grpc_exec_ctx *exec_ctx, pick_first_lb_policy *p) {
Craig Tiller48cb07c2015-07-15 16:16:15 -0700145 p->started_picking = 1;
146 p->checking_subchannel = 0;
147 p->checking_connectivity = GRPC_CHANNEL_IDLE;
Craig Tiller48613042015-11-29 14:45:11 -0800148 GRPC_LB_POLICY_WEAK_REF(&p->base, "pick_first_connectivity");
Craig Tillera82950e2015-09-22 12:33:20 -0700149 grpc_subchannel_notify_on_state_change(
150 exec_ctx, p->subchannels[p->checking_subchannel],
Craig Tiller86c99582015-11-25 15:22:26 -0800151 &p->base.interested_parties,
Craig Tillera82950e2015-09-22 12:33:20 -0700152 &p->checking_connectivity, &p->connectivity_changed);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700153}
154
Craig Tillera82950e2015-09-22 12:33:20 -0700155void pf_exit_idle(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) {
156 pick_first_lb_policy *p = (pick_first_lb_policy *)pol;
157 gpr_mu_lock(&p->mu);
158 if (!p->started_picking) {
159 start_picking(exec_ctx, p);
160 }
161 gpr_mu_unlock(&p->mu);
Craig Tillereb3b12e2015-06-26 14:42:49 -0700162}
163
Craig Tiller577c9b22015-11-02 14:11:15 -0800164int pf_pick(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, grpc_pollset *pollset,
Craig Tillerab33b482015-11-21 08:11:04 -0800165 grpc_metadata_batch *initial_metadata,
166 grpc_connected_subchannel **target, grpc_closure *on_complete) {
Craig Tillera82950e2015-09-22 12:33:20 -0700167 pick_first_lb_policy *p = (pick_first_lb_policy *)pol;
Craig Tiller45724b32015-09-22 10:42:19 -0700168 pending_pick *pp;
Craig Tillera82950e2015-09-22 12:33:20 -0700169 gpr_mu_lock(&p->mu);
170 if (p->selected) {
171 gpr_mu_unlock(&p->mu);
172 *target = p->selected;
Craig Tiller577c9b22015-11-02 14:11:15 -0800173 return 1;
Craig Tillera82950e2015-09-22 12:33:20 -0700174 } else {
175 if (!p->started_picking) {
176 start_picking(exec_ctx, p);
Craig Tiller45724b32015-09-22 10:42:19 -0700177 }
Craig Tiller86c99582015-11-25 15:22:26 -0800178 grpc_pollset_set_add_pollset(exec_ctx, &p->base.interested_parties, pollset);
Craig Tillera82950e2015-09-22 12:33:20 -0700179 pp = gpr_malloc(sizeof(*pp));
180 pp->next = p->pending_picks;
181 pp->pollset = pollset;
182 pp->target = target;
183 pp->on_complete = on_complete;
184 p->pending_picks = pp;
185 gpr_mu_unlock(&p->mu);
Craig Tiller577c9b22015-11-02 14:11:15 -0800186 return 0;
Craig Tillera82950e2015-09-22 12:33:20 -0700187 }
Craig Tiller45724b32015-09-22 10:42:19 -0700188}
189
Craig Tiller1f41b6b2015-10-09 15:07:02 -0700190static void destroy_subchannels(grpc_exec_ctx *exec_ctx, void *arg,
Craig Tillerb09d84d2015-10-06 09:12:16 -0700191 int iomgr_success) {
192 pick_first_lb_policy *p = arg;
193 size_t i;
Craig Tillerb09d84d2015-10-06 09:12:16 -0700194 size_t num_subchannels = p->num_subchannels;
195 grpc_subchannel **subchannels;
Craig Tillerb5585d42015-11-17 07:18:31 -0800196 grpc_connected_subchannel *exclude_subchannel;
Craig Tillerb09d84d2015-10-06 09:12:16 -0700197
198 gpr_mu_lock(&p->mu);
199 subchannels = p->subchannels;
Craig Tillerb09d84d2015-10-06 09:12:16 -0700200 p->num_subchannels = 0;
201 p->subchannels = NULL;
Craig Tiller89a768e2015-10-06 09:55:59 -0700202 exclude_subchannel = p->selected;
Craig Tillerb09d84d2015-10-06 09:12:16 -0700203 gpr_mu_unlock(&p->mu);
Craig Tiller48613042015-11-29 14:45:11 -0800204 GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &p->base, "destroy_subchannels");
Craig Tillerb09d84d2015-10-06 09:12:16 -0700205
206 for (i = 0; i < num_subchannels; i++) {
Craig Tillerb09d84d2015-10-06 09:12:16 -0700207 GRPC_SUBCHANNEL_UNREF(exec_ctx, subchannels[i], "pick_first");
208 }
209
210 gpr_free(subchannels);
211}
212
Craig Tillera82950e2015-09-22 12:33:20 -0700213static void pf_connectivity_changed(grpc_exec_ctx *exec_ctx, void *arg,
214 int iomgr_success) {
Craig Tillereb3b12e2015-06-26 14:42:49 -0700215 pick_first_lb_policy *p = arg;
Craig Tillerb5585d42015-11-17 07:18:31 -0800216 grpc_subchannel *selected_subchannel;
Craig Tillereb3b12e2015-06-26 14:42:49 -0700217 pending_pick *pp;
Craig Tillereb3b12e2015-06-26 14:42:49 -0700218
Craig Tillera82950e2015-09-22 12:33:20 -0700219 gpr_mu_lock(&p->mu);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700220
Craig Tiller48613042015-11-29 14:45:11 -0800221 gpr_log(GPR_DEBUG, "LB_POLICY: pf_connectivity_changed: %p success=%d shutdown=%d", p, iomgr_success, p->shutdown);
222
Craig Tillera82950e2015-09-22 12:33:20 -0700223 if (p->shutdown) {
224 gpr_mu_unlock(&p->mu);
Craig Tiller48613042015-11-29 14:45:11 -0800225 GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &p->base, "pick_first_connectivity");
Craig Tillera82950e2015-09-22 12:33:20 -0700226 return;
227 } else if (p->selected != NULL) {
Craig Tillercb2609f2015-11-24 17:19:19 -0800228 if (p->checking_connectivity == GRPC_CHANNEL_TRANSIENT_FAILURE) {
229 /* if the selected channel goes bad, we're done */
230 p->checking_connectivity = GRPC_CHANNEL_FATAL_FAILURE;
231 }
Craig Tillera82950e2015-09-22 12:33:20 -0700232 grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
233 p->checking_connectivity, "selected_changed");
234 if (p->checking_connectivity != GRPC_CHANNEL_FATAL_FAILURE) {
Craig Tillerab33b482015-11-21 08:11:04 -0800235 grpc_connected_subchannel_notify_on_state_change(
236 exec_ctx, p->selected, &p->checking_connectivity,
237 &p->connectivity_changed);
Craig Tillera82950e2015-09-22 12:33:20 -0700238 } else {
Craig Tiller48613042015-11-29 14:45:11 -0800239 GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &p->base, "pick_first_connectivity");
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700240 }
Craig Tillera82950e2015-09-22 12:33:20 -0700241 } else {
242 loop:
243 switch (p->checking_connectivity) {
244 case GRPC_CHANNEL_READY:
245 grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
246 GRPC_CHANNEL_READY, "connecting_ready");
Craig Tillerb5585d42015-11-17 07:18:31 -0800247 selected_subchannel = p->subchannels[p->checking_subchannel];
Craig Tillerab33b482015-11-21 08:11:04 -0800248 p->selected =
249 grpc_subchannel_get_connected_subchannel(selected_subchannel);
Craig Tillerb5585d42015-11-17 07:18:31 -0800250 GPR_ASSERT(p->selected);
251 GRPC_CONNECTED_SUBCHANNEL_REF(p->selected, "picked_first");
Craig Tillerb09d84d2015-10-06 09:12:16 -0700252 /* drop the pick list: we are connected now */
Craig Tiller48613042015-11-29 14:45:11 -0800253 GRPC_LB_POLICY_WEAK_REF(&p->base, "destroy_subchannels");
Craig Tiller1f41b6b2015-10-09 15:07:02 -0700254 grpc_exec_ctx_enqueue(exec_ctx,
255 grpc_closure_create(destroy_subchannels, p), 1);
Craig Tillerb09d84d2015-10-06 09:12:16 -0700256 /* update any calls that were waiting for a pick */
Craig Tillera82950e2015-09-22 12:33:20 -0700257 while ((pp = p->pending_picks)) {
258 p->pending_picks = pp->next;
259 *pp->target = p->selected;
Craig Tiller86c99582015-11-25 15:22:26 -0800260 grpc_pollset_set_del_pollset(exec_ctx, &p->base.interested_parties, pp->pollset);
Craig Tillera82950e2015-09-22 12:33:20 -0700261 grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, 1);
262 gpr_free(pp);
263 }
Craig Tillerab33b482015-11-21 08:11:04 -0800264 grpc_connected_subchannel_notify_on_state_change(
265 exec_ctx, p->selected, &p->checking_connectivity,
266 &p->connectivity_changed);
Craig Tillera82950e2015-09-22 12:33:20 -0700267 break;
268 case GRPC_CHANNEL_TRANSIENT_FAILURE:
269 grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
270 GRPC_CHANNEL_TRANSIENT_FAILURE,
271 "connecting_transient_failure");
Craig Tillera82950e2015-09-22 12:33:20 -0700272 p->checking_subchannel =
273 (p->checking_subchannel + 1) % p->num_subchannels;
274 p->checking_connectivity = grpc_subchannel_check_connectivity(
275 p->subchannels[p->checking_subchannel]);
Craig Tillera82950e2015-09-22 12:33:20 -0700276 if (p->checking_connectivity == GRPC_CHANNEL_TRANSIENT_FAILURE) {
277 grpc_subchannel_notify_on_state_change(
278 exec_ctx, p->subchannels[p->checking_subchannel],
Craig Tiller86c99582015-11-25 15:22:26 -0800279 &p->base.interested_parties,
Craig Tillera82950e2015-09-22 12:33:20 -0700280 &p->checking_connectivity, &p->connectivity_changed);
281 } else {
282 goto loop;
283 }
284 break;
285 case GRPC_CHANNEL_CONNECTING:
286 case GRPC_CHANNEL_IDLE:
287 grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
288 GRPC_CHANNEL_CONNECTING,
289 "connecting_changed");
290 grpc_subchannel_notify_on_state_change(
291 exec_ctx, p->subchannels[p->checking_subchannel],
Craig Tiller86c99582015-11-25 15:22:26 -0800292 &p->base.interested_parties,
Craig Tillera82950e2015-09-22 12:33:20 -0700293 &p->checking_connectivity, &p->connectivity_changed);
294 break;
295 case GRPC_CHANNEL_FATAL_FAILURE:
Craig Tillera82950e2015-09-22 12:33:20 -0700296 p->num_subchannels--;
Craig Tiller86c99582015-11-25 15:22:26 -0800297 GPR_SWAP(grpc_subchannel *, p->subchannels[p->checking_subchannel],
298 p->subchannels[p->num_subchannels]);
Craig Tillera82950e2015-09-22 12:33:20 -0700299 GRPC_SUBCHANNEL_UNREF(exec_ctx, p->subchannels[p->num_subchannels],
300 "pick_first");
301 if (p->num_subchannels == 0) {
302 grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
303 GRPC_CHANNEL_FATAL_FAILURE,
304 "no_more_channels");
305 while ((pp = p->pending_picks)) {
306 p->pending_picks = pp->next;
307 *pp->target = NULL;
308 grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, 1);
309 gpr_free(pp);
310 }
Craig Tiller48613042015-11-29 14:45:11 -0800311 GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &p->base, "pick_first_connectivity");
Craig Tillera82950e2015-09-22 12:33:20 -0700312 } else {
313 grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
314 GRPC_CHANNEL_TRANSIENT_FAILURE,
315 "subchannel_failed");
316 p->checking_subchannel %= p->num_subchannels;
317 p->checking_connectivity = grpc_subchannel_check_connectivity(
318 p->subchannels[p->checking_subchannel]);
Craig Tillera82950e2015-09-22 12:33:20 -0700319 goto loop;
320 }
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700321 }
Craig Tillera82950e2015-09-22 12:33:20 -0700322 }
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700323
Craig Tillera82950e2015-09-22 12:33:20 -0700324 gpr_mu_unlock(&p->mu);
Craig Tillereb3b12e2015-06-26 14:42:49 -0700325}
326
Craig Tillera82950e2015-09-22 12:33:20 -0700327static grpc_connectivity_state pf_check_connectivity(grpc_exec_ctx *exec_ctx,
328 grpc_lb_policy *pol) {
329 pick_first_lb_policy *p = (pick_first_lb_policy *)pol;
Craig Tillerc7b5f762015-06-27 11:48:42 -0700330 grpc_connectivity_state st;
Craig Tillera82950e2015-09-22 12:33:20 -0700331 gpr_mu_lock(&p->mu);
332 st = grpc_connectivity_state_check(&p->state_tracker);
333 gpr_mu_unlock(&p->mu);
Craig Tillerc7b5f762015-06-27 11:48:42 -0700334 return st;
335}
336
Craig Tillera82950e2015-09-22 12:33:20 -0700337void pf_notify_on_state_change(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol,
338 grpc_connectivity_state *current,
339 grpc_closure *notify) {
340 pick_first_lb_policy *p = (pick_first_lb_policy *)pol;
341 gpr_mu_lock(&p->mu);
342 grpc_connectivity_state_notify_on_state_change(exec_ctx, &p->state_tracker,
343 current, notify);
344 gpr_mu_unlock(&p->mu);
Craig Tillerc7b5f762015-06-27 11:48:42 -0700345}
346
Craig Tillereb3b12e2015-06-26 14:42:49 -0700347static const grpc_lb_policy_vtable pick_first_lb_policy_vtable = {
Craig Tiller577c9b22015-11-02 14:11:15 -0800348 pf_destroy, pf_shutdown, pf_pick, pf_cancel_pick, pf_exit_idle,
Craig Tiller50ec2672015-11-27 21:45:11 -0800349 pf_check_connectivity, pf_notify_on_state_change};
Craig Tillereb3b12e2015-06-26 14:42:49 -0700350
Craig Tillera82950e2015-09-22 12:33:20 -0700351static void pick_first_factory_ref(grpc_lb_policy_factory *factory) {}
David Garcia Quintas5c4543d2015-09-03 15:49:56 -0700352
Craig Tillera82950e2015-09-22 12:33:20 -0700353static void pick_first_factory_unref(grpc_lb_policy_factory *factory) {}
David Garcia Quintas5c4543d2015-09-03 15:49:56 -0700354
Craig Tillera82950e2015-09-22 12:33:20 -0700355static grpc_lb_policy *create_pick_first(grpc_lb_policy_factory *factory,
356 grpc_lb_policy_args *args) {
357 pick_first_lb_policy *p = gpr_malloc(sizeof(*p));
358 GPR_ASSERT(args->num_subchannels > 0);
359 memset(p, 0, sizeof(*p));
360 grpc_lb_policy_init(&p->base, &pick_first_lb_policy_vtable);
361 p->subchannels =
362 gpr_malloc(sizeof(grpc_subchannel *) * args->num_subchannels);
David Garcia Quintasc7705c72015-09-09 17:21:11 -0700363 p->num_subchannels = args->num_subchannels;
Craig Tillera82950e2015-09-22 12:33:20 -0700364 grpc_connectivity_state_init(&p->state_tracker, GRPC_CHANNEL_IDLE,
365 "pick_first");
366 memcpy(p->subchannels, args->subchannels,
367 sizeof(grpc_subchannel *) * args->num_subchannels);
368 grpc_closure_init(&p->connectivity_changed, pf_connectivity_changed, p);
369 gpr_mu_init(&p->mu);
Craig Tillereb3b12e2015-06-26 14:42:49 -0700370 return &p->base;
371}
David Garcia Quintas5c4543d2015-09-03 15:49:56 -0700372
373static const grpc_lb_policy_factory_vtable pick_first_factory_vtable = {
Craig Tillera82950e2015-09-22 12:33:20 -0700374 pick_first_factory_ref, pick_first_factory_unref, create_pick_first,
375 "pick_first"};
David Garcia Quintas5c4543d2015-09-03 15:49:56 -0700376
377static grpc_lb_policy_factory pick_first_lb_policy_factory = {
Craig Tillera82950e2015-09-22 12:33:20 -0700378 &pick_first_factory_vtable};
David Garcia Quintas5c4543d2015-09-03 15:49:56 -0700379
Craig Tillera82950e2015-09-22 12:33:20 -0700380grpc_lb_policy_factory *grpc_pick_first_lb_factory_create() {
David Garcia Quintas5c4543d2015-09-03 15:49:56 -0700381 return &pick_first_lb_policy_factory;
382}