blob: 114ece6e4d9c42b7be95fe169e228f129ddab051 [file] [log] [blame]
David Garcia Quintas4fb049b2015-09-03 17:26:06 -07001/*
2 *
Craig Tillera93a25f2016-01-28 13:55:49 -08003 * Copyright 2015-2016, Google Inc.
David Garcia Quintas4fb049b2015-09-03 17:26:06 -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
34#include "src/core/client_config/lb_policies/round_robin.h"
35
36#include <string.h>
37
38#include <grpc/support/alloc.h>
39#include "src/core/transport/connectivity_state.h"
40
Craig Tillere2a65102015-11-30 17:51:49 -080041typedef struct round_robin_lb_policy round_robin_lb_policy;
42
David Garcia Quintas4fb049b2015-09-03 17:26:06 -070043int grpc_lb_round_robin_trace = 0;
44
45/** List of entities waiting for a pick.
46 *
47 * Once a pick is available, \a target is updated and \a on_complete called. */
Craig Tillera82950e2015-09-22 12:33:20 -070048typedef struct pending_pick {
David Garcia Quintas4fb049b2015-09-03 17:26:06 -070049 struct pending_pick *next;
50 grpc_pollset *pollset;
Craig Tillerb5585d42015-11-17 07:18:31 -080051 grpc_connected_subchannel **target;
Craig Tiller10ee2742015-09-22 09:25:57 -070052 grpc_closure *on_complete;
David Garcia Quintas4fb049b2015-09-03 17:26:06 -070053} pending_pick;
54
55/** List of subchannels in a connectivity READY state */
Craig Tillera82950e2015-09-22 12:33:20 -070056typedef struct ready_list {
David Garcia Quintas4fb049b2015-09-03 17:26:06 -070057 grpc_subchannel *subchannel;
58 struct ready_list *next;
59 struct ready_list *prev;
60} ready_list;
61
Craig Tillera82950e2015-09-22 12:33:20 -070062typedef struct {
Craig Tillere2a65102015-11-30 17:51:49 -080063 /** index within policy->subchannels */
64 size_t index;
65 /** backpointer to owning policy */
66 round_robin_lb_policy *policy;
67 /** subchannel itself */
68 grpc_subchannel *subchannel;
69 /** notification that connectivity has changed on subchannel */
70 grpc_closure connectivity_changed_closure;
71 /** this subchannels current position in subchannel->ready_list */
72 ready_list *ready_list_node;
73 /** last observed connectivity */
74 grpc_connectivity_state connectivity_state;
75} subchannel_data;
David Garcia Quintas4fb049b2015-09-03 17:26:06 -070076
Craig Tillere2a65102015-11-30 17:51:49 -080077struct round_robin_lb_policy {
David Garcia Quintas4fb049b2015-09-03 17:26:06 -070078 /** base policy: must be first */
79 grpc_lb_policy base;
80
81 /** all our subchannels */
David Garcia Quintas4fb049b2015-09-03 17:26:06 -070082 size_t num_subchannels;
Craig Tillere2a65102015-11-30 17:51:49 -080083 subchannel_data **subchannels;
David Garcia Quintas4fb049b2015-09-03 17:26:06 -070084
85 /** mutex protecting remaining members */
86 gpr_mu mu;
87 /** have we started picking? */
88 int started_picking;
89 /** are we shutting down? */
90 int shutdown;
David Garcia Quintas4fb049b2015-09-03 17:26:06 -070091 /** List of picks that are waiting on connectivity */
92 pending_pick *pending_picks;
93
94 /** our connectivity state tracker */
95 grpc_connectivity_state_tracker state_tracker;
96
97 /** (Dummy) root of the doubly linked list containing READY subchannels */
98 ready_list ready_list;
99 /** Last pick from the ready list. */
100 ready_list *ready_list_last_pick;
Craig Tillere2a65102015-11-30 17:51:49 -0800101};
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700102
103/** Returns the next subchannel from the connected list or NULL if the list is
104 * empty.
105 *
106 * Note that this function does *not* advance p->ready_list_last_pick. Use \a
107 * advance_last_picked_locked() for that. */
Craig Tillera82950e2015-09-22 12:33:20 -0700108static ready_list *peek_next_connected_locked(const round_robin_lb_policy *p) {
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700109 ready_list *selected;
110 selected = p->ready_list_last_pick->next;
111
Craig Tillera82950e2015-09-22 12:33:20 -0700112 while (selected != NULL) {
113 if (selected == &p->ready_list) {
114 GPR_ASSERT(selected->subchannel == NULL);
115 /* skip dummy root */
116 selected = selected->next;
117 } else {
118 GPR_ASSERT(selected->subchannel != NULL);
119 return selected;
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700120 }
Craig Tillera82950e2015-09-22 12:33:20 -0700121 }
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700122 return NULL;
123}
124
125/** Advance the \a ready_list picking head. */
Craig Tillera82950e2015-09-22 12:33:20 -0700126static void advance_last_picked_locked(round_robin_lb_policy *p) {
127 if (p->ready_list_last_pick->next != NULL) { /* non-empty list */
128 p->ready_list_last_pick = p->ready_list_last_pick->next;
129 if (p->ready_list_last_pick == &p->ready_list) {
130 /* skip dummy root */
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700131 p->ready_list_last_pick = p->ready_list_last_pick->next;
132 }
Craig Tillera82950e2015-09-22 12:33:20 -0700133 } else { /* should be an empty list */
134 GPR_ASSERT(p->ready_list_last_pick == &p->ready_list);
135 }
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700136
Craig Tillera82950e2015-09-22 12:33:20 -0700137 if (grpc_lb_round_robin_trace) {
138 gpr_log(GPR_DEBUG, "[READYLIST] ADVANCED LAST PICK. NOW AT NODE %p (SC %p)",
139 p->ready_list_last_pick, p->ready_list_last_pick->subchannel);
140 }
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700141}
142
143/** Prepends (relative to the root at p->ready_list) the connected subchannel \a
144 * csc to the list of ready subchannels. */
Craig Tillera82950e2015-09-22 12:33:20 -0700145static ready_list *add_connected_sc_locked(round_robin_lb_policy *p,
Craig Tillerb5585d42015-11-17 07:18:31 -0800146 grpc_subchannel *sc) {
Craig Tillera82950e2015-09-22 12:33:20 -0700147 ready_list *new_elem = gpr_malloc(sizeof(ready_list));
Craig Tillerb5585d42015-11-17 07:18:31 -0800148 new_elem->subchannel = sc;
Craig Tillera82950e2015-09-22 12:33:20 -0700149 if (p->ready_list.prev == NULL) {
150 /* first element */
151 new_elem->next = &p->ready_list;
152 new_elem->prev = &p->ready_list;
153 p->ready_list.next = new_elem;
154 p->ready_list.prev = new_elem;
155 } else {
156 new_elem->next = &p->ready_list;
157 new_elem->prev = p->ready_list.prev;
158 p->ready_list.prev->next = new_elem;
159 p->ready_list.prev = new_elem;
160 }
161 if (grpc_lb_round_robin_trace) {
Craig Tillerb5585d42015-11-17 07:18:31 -0800162 gpr_log(GPR_DEBUG, "[READYLIST] ADDING NODE %p (SC %p)", new_elem, sc);
Craig Tillera82950e2015-09-22 12:33:20 -0700163 }
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700164 return new_elem;
165}
166
167/** Removes \a node from the list of connected subchannels */
Craig Tillera82950e2015-09-22 12:33:20 -0700168static void remove_disconnected_sc_locked(round_robin_lb_policy *p,
169 ready_list *node) {
170 if (node == NULL) {
171 return;
172 }
173 if (node == p->ready_list_last_pick) {
174 /* If removing the lastly picked node, reset the last pick pointer to the
175 * dummy root of the list */
176 p->ready_list_last_pick = &p->ready_list;
177 }
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700178
179 /* removing last item */
Craig Tillera82950e2015-09-22 12:33:20 -0700180 if (node->next == &p->ready_list && node->prev == &p->ready_list) {
181 GPR_ASSERT(p->ready_list.next == node);
182 GPR_ASSERT(p->ready_list.prev == node);
183 p->ready_list.next = NULL;
184 p->ready_list.prev = NULL;
185 } else {
186 node->prev->next = node->next;
187 node->next->prev = node->prev;
188 }
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700189
Craig Tillera82950e2015-09-22 12:33:20 -0700190 if (grpc_lb_round_robin_trace) {
191 gpr_log(GPR_DEBUG, "[READYLIST] REMOVED NODE %p (SC %p)", node,
192 node->subchannel);
193 }
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700194
195 node->next = NULL;
196 node->prev = NULL;
197 node->subchannel = NULL;
198
Craig Tillera82950e2015-09-22 12:33:20 -0700199 gpr_free(node);
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700200}
201
Craig Tillera82950e2015-09-22 12:33:20 -0700202void rr_destroy(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) {
203 round_robin_lb_policy *p = (round_robin_lb_policy *)pol;
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700204 size_t i;
205 ready_list *elem;
Craig Tillera82950e2015-09-22 12:33:20 -0700206 for (i = 0; i < p->num_subchannels; i++) {
Craig Tillere2a65102015-11-30 17:51:49 -0800207 subchannel_data *sd = p->subchannels[i];
208 GRPC_SUBCHANNEL_UNREF(exec_ctx, sd->subchannel, "round_robin");
209 gpr_free(sd);
Craig Tillera82950e2015-09-22 12:33:20 -0700210 }
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700211
Craig Tillera82950e2015-09-22 12:33:20 -0700212 grpc_connectivity_state_destroy(exec_ctx, &p->state_tracker);
213 gpr_free(p->subchannels);
214 gpr_mu_destroy(&p->mu);
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700215
216 elem = p->ready_list.next;
Craig Tillera82950e2015-09-22 12:33:20 -0700217 while (elem != NULL && elem != &p->ready_list) {
218 ready_list *tmp;
219 tmp = elem->next;
220 elem->next = NULL;
221 elem->prev = NULL;
222 elem->subchannel = NULL;
223 gpr_free(elem);
224 elem = tmp;
225 }
Craig Tillera82950e2015-09-22 12:33:20 -0700226 gpr_free(p);
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700227}
228
Craig Tillera82950e2015-09-22 12:33:20 -0700229void rr_shutdown(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) {
Craig Tillera82950e2015-09-22 12:33:20 -0700230 round_robin_lb_policy *p = (round_robin_lb_policy *)pol;
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700231 pending_pick *pp;
Craig Tiller2a1bb7f2015-11-29 21:54:26 -0800232 size_t i;
233
Craig Tillera82950e2015-09-22 12:33:20 -0700234 gpr_mu_lock(&p->mu);
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700235
236 p->shutdown = 1;
Craig Tillera82950e2015-09-22 12:33:20 -0700237 while ((pp = p->pending_picks)) {
238 p->pending_picks = pp->next;
239 *pp->target = NULL;
Craig Tiller6c396862016-01-28 13:53:40 -0800240 grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, false, NULL);
Craig Tillera82950e2015-09-22 12:33:20 -0700241 gpr_free(pp);
242 }
243 grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
244 GRPC_CHANNEL_FATAL_FAILURE, "shutdown");
Craig Tiller2a1bb7f2015-11-29 21:54:26 -0800245 for (i = 0; i < p->num_subchannels; i++) {
Craig Tillere2a65102015-11-30 17:51:49 -0800246 subchannel_data *sd = p->subchannels[i];
Craig Tiller1d881fb2015-12-01 07:39:04 -0800247 grpc_subchannel_notify_on_state_change(exec_ctx, sd->subchannel, NULL, NULL,
Craig Tillere2a65102015-11-30 17:51:49 -0800248 &sd->connectivity_changed_closure);
Craig Tiller2a1bb7f2015-11-29 21:54:26 -0800249 }
Craig Tillera82950e2015-09-22 12:33:20 -0700250 gpr_mu_unlock(&p->mu);
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700251}
252
Craig Tiller577c9b22015-11-02 14:11:15 -0800253static void rr_cancel_pick(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol,
Craig Tillerb5585d42015-11-17 07:18:31 -0800254 grpc_connected_subchannel **target) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800255 round_robin_lb_policy *p = (round_robin_lb_policy *)pol;
256 pending_pick *pp;
Craig Tiller577c9b22015-11-02 14:11:15 -0800257 gpr_mu_lock(&p->mu);
258 pp = p->pending_picks;
259 p->pending_picks = NULL;
260 while (pp != NULL) {
261 pending_pick *next = pp->next;
262 if (pp->target == target) {
Craig Tiller69b093b2016-02-25 19:04:07 -0800263 grpc_pollset_set_del_pollset(exec_ctx, p->base.interested_parties,
Craig Tiller1d881fb2015-12-01 07:39:04 -0800264 pp->pollset);
Craig Tiller577c9b22015-11-02 14:11:15 -0800265 *target = NULL;
Craig Tiller6c396862016-01-28 13:53:40 -0800266 grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, false, NULL);
Craig Tiller577c9b22015-11-02 14:11:15 -0800267 gpr_free(pp);
268 } else {
269 pp->next = p->pending_picks;
270 p->pending_picks = pp;
271 }
272 pp = next;
273 }
274 gpr_mu_unlock(&p->mu);
275}
276
Craig Tillera82950e2015-09-22 12:33:20 -0700277static void start_picking(grpc_exec_ctx *exec_ctx, round_robin_lb_policy *p) {
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700278 size_t i;
279 p->started_picking = 1;
280
Craig Tiller1d881fb2015-12-01 07:39:04 -0800281 gpr_log(GPR_DEBUG, "LB_POLICY: p=%p num_subchannels=%d", p,
282 p->num_subchannels);
Craig Tiller2a1bb7f2015-11-29 21:54:26 -0800283
Craig Tillera82950e2015-09-22 12:33:20 -0700284 for (i = 0; i < p->num_subchannels; i++) {
Craig Tillere2a65102015-11-30 17:51:49 -0800285 subchannel_data *sd = p->subchannels[i];
286 sd->connectivity_state = GRPC_CHANNEL_IDLE;
Craig Tiller1d881fb2015-12-01 07:39:04 -0800287 grpc_subchannel_notify_on_state_change(
Craig Tiller69b093b2016-02-25 19:04:07 -0800288 exec_ctx, sd->subchannel, p->base.interested_parties,
Craig Tiller1d881fb2015-12-01 07:39:04 -0800289 &sd->connectivity_state, &sd->connectivity_changed_closure);
Craig Tiller2a1bb7f2015-11-29 21:54:26 -0800290 GRPC_LB_POLICY_WEAK_REF(&p->base, "round_robin_connectivity");
Craig Tillera82950e2015-09-22 12:33:20 -0700291 }
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700292}
293
Craig Tillera82950e2015-09-22 12:33:20 -0700294void rr_exit_idle(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) {
295 round_robin_lb_policy *p = (round_robin_lb_policy *)pol;
296 gpr_mu_lock(&p->mu);
297 if (!p->started_picking) {
298 start_picking(exec_ctx, p);
299 }
300 gpr_mu_unlock(&p->mu);
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700301}
302
Craig Tiller577c9b22015-11-02 14:11:15 -0800303int rr_pick(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, grpc_pollset *pollset,
Craig Tillerab33b482015-11-21 08:11:04 -0800304 grpc_metadata_batch *initial_metadata,
305 grpc_connected_subchannel **target, grpc_closure *on_complete) {
Craig Tillera82950e2015-09-22 12:33:20 -0700306 round_robin_lb_policy *p = (round_robin_lb_policy *)pol;
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700307 pending_pick *pp;
308 ready_list *selected;
Craig Tillera82950e2015-09-22 12:33:20 -0700309 gpr_mu_lock(&p->mu);
310 if ((selected = peek_next_connected_locked(p))) {
311 gpr_mu_unlock(&p->mu);
Craig Tillerb5585d42015-11-17 07:18:31 -0800312 *target = grpc_subchannel_get_connected_subchannel(selected->subchannel);
Craig Tillera82950e2015-09-22 12:33:20 -0700313 if (grpc_lb_round_robin_trace) {
Craig Tillerab33b482015-11-21 08:11:04 -0800314 gpr_log(GPR_DEBUG,
315 "[RR PICK] TARGET <-- CONNECTED SUBCHANNEL %p (NODE %p)",
Craig Tillera82950e2015-09-22 12:33:20 -0700316 selected->subchannel, selected);
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700317 }
Craig Tillera82950e2015-09-22 12:33:20 -0700318 /* only advance the last picked pointer if the selection was used */
319 advance_last_picked_locked(p);
Craig Tiller577c9b22015-11-02 14:11:15 -0800320 return 1;
Craig Tillera82950e2015-09-22 12:33:20 -0700321 } else {
322 if (!p->started_picking) {
323 start_picking(exec_ctx, p);
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700324 }
Craig Tiller69b093b2016-02-25 19:04:07 -0800325 grpc_pollset_set_add_pollset(exec_ctx, p->base.interested_parties, pollset);
Craig Tillera82950e2015-09-22 12:33:20 -0700326 pp = gpr_malloc(sizeof(*pp));
327 pp->next = p->pending_picks;
328 pp->pollset = pollset;
329 pp->target = target;
330 pp->on_complete = on_complete;
331 p->pending_picks = pp;
332 gpr_mu_unlock(&p->mu);
Craig Tiller577c9b22015-11-02 14:11:15 -0800333 return 0;
Craig Tillera82950e2015-09-22 12:33:20 -0700334 }
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700335}
336
Craig Tillera82950e2015-09-22 12:33:20 -0700337static void rr_connectivity_changed(grpc_exec_ctx *exec_ctx, void *arg,
Craig Tiller6c396862016-01-28 13:53:40 -0800338 bool iomgr_success) {
Craig Tillere2a65102015-11-30 17:51:49 -0800339 subchannel_data *sd = arg;
340 round_robin_lb_policy *p = sd->policy;
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700341 pending_pick *pp;
342 ready_list *selected;
343
344 int unref = 0;
345
Craig Tillera82950e2015-09-22 12:33:20 -0700346 gpr_mu_lock(&p->mu);
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700347
Craig Tillera82950e2015-09-22 12:33:20 -0700348 if (p->shutdown) {
349 unref = 1;
350 } else {
Craig Tillere2a65102015-11-30 17:51:49 -0800351 switch (sd->connectivity_state) {
Craig Tillera82950e2015-09-22 12:33:20 -0700352 case GRPC_CHANNEL_READY:
353 grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
354 GRPC_CHANNEL_READY, "connecting_ready");
355 /* add the newly connected subchannel to the list of connected ones.
356 * Note that it goes to the "end of the line". */
Craig Tiller1d881fb2015-12-01 07:39:04 -0800357 sd->ready_list_node = add_connected_sc_locked(p, sd->subchannel);
Craig Tillera82950e2015-09-22 12:33:20 -0700358 /* at this point we know there's at least one suitable subchannel. Go
359 * ahead and pick one and notify the pending suitors in
360 * p->pending_picks. This preemtively replicates rr_pick()'s actions. */
361 selected = peek_next_connected_locked(p);
362 if (p->pending_picks != NULL) {
363 /* if the selected subchannel is going to be used for the pending
364 * picks, update the last picked pointer */
365 advance_last_picked_locked(p);
366 }
367 while ((pp = p->pending_picks)) {
368 p->pending_picks = pp->next;
Craig Tillerab33b482015-11-21 08:11:04 -0800369 *pp->target =
370 grpc_subchannel_get_connected_subchannel(selected->subchannel);
Craig Tillera82950e2015-09-22 12:33:20 -0700371 if (grpc_lb_round_robin_trace) {
372 gpr_log(GPR_DEBUG,
373 "[RR CONN CHANGED] TARGET <-- SUBCHANNEL %p (NODE %p)",
374 selected->subchannel, selected);
375 }
Craig Tiller69b093b2016-02-25 19:04:07 -0800376 grpc_pollset_set_del_pollset(exec_ctx, p->base.interested_parties,
Craig Tiller1d881fb2015-12-01 07:39:04 -0800377 pp->pollset);
Craig Tiller6c396862016-01-28 13:53:40 -0800378 grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, true, NULL);
Craig Tillera82950e2015-09-22 12:33:20 -0700379 gpr_free(pp);
380 }
381 grpc_subchannel_notify_on_state_change(
Craig Tiller69b093b2016-02-25 19:04:07 -0800382 exec_ctx, sd->subchannel, p->base.interested_parties,
Craig Tiller1d881fb2015-12-01 07:39:04 -0800383 &sd->connectivity_state, &sd->connectivity_changed_closure);
Craig Tillera82950e2015-09-22 12:33:20 -0700384 break;
385 case GRPC_CHANNEL_CONNECTING:
386 case GRPC_CHANNEL_IDLE:
387 grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
Craig Tiller1d881fb2015-12-01 07:39:04 -0800388 sd->connectivity_state,
389 "connecting_changed");
Craig Tillera82950e2015-09-22 12:33:20 -0700390 grpc_subchannel_notify_on_state_change(
Craig Tiller69b093b2016-02-25 19:04:07 -0800391 exec_ctx, sd->subchannel, p->base.interested_parties,
Craig Tiller1d881fb2015-12-01 07:39:04 -0800392 &sd->connectivity_state, &sd->connectivity_changed_closure);
Craig Tillera82950e2015-09-22 12:33:20 -0700393 break;
394 case GRPC_CHANNEL_TRANSIENT_FAILURE:
Craig Tillera82950e2015-09-22 12:33:20 -0700395 /* renew state notification */
396 grpc_subchannel_notify_on_state_change(
Craig Tiller69b093b2016-02-25 19:04:07 -0800397 exec_ctx, sd->subchannel, p->base.interested_parties,
Craig Tiller1d881fb2015-12-01 07:39:04 -0800398 &sd->connectivity_state, &sd->connectivity_changed_closure);
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700399
Craig Tillera82950e2015-09-22 12:33:20 -0700400 /* remove from ready list if still present */
Craig Tillere2a65102015-11-30 17:51:49 -0800401 if (sd->ready_list_node != NULL) {
402 remove_disconnected_sc_locked(p, sd->ready_list_node);
403 sd->ready_list_node = NULL;
Craig Tillera82950e2015-09-22 12:33:20 -0700404 }
405 grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
406 GRPC_CHANNEL_TRANSIENT_FAILURE,
407 "connecting_transient_failure");
408 break;
409 case GRPC_CHANNEL_FATAL_FAILURE:
Craig Tillere2a65102015-11-30 17:51:49 -0800410 if (sd->ready_list_node != NULL) {
Craig Tiller1d881fb2015-12-01 07:39:04 -0800411 remove_disconnected_sc_locked(p, sd->ready_list_node);
Craig Tillere2a65102015-11-30 17:51:49 -0800412 sd->ready_list_node = NULL;
Craig Tillera82950e2015-09-22 12:33:20 -0700413 }
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700414
Craig Tillera82950e2015-09-22 12:33:20 -0700415 p->num_subchannels--;
Craig Tillere2a65102015-11-30 17:51:49 -0800416 GPR_SWAP(subchannel_data *, p->subchannels[sd->index],
417 p->subchannels[p->num_subchannels]);
Craig Tiller1d881fb2015-12-01 07:39:04 -0800418 GRPC_SUBCHANNEL_UNREF(exec_ctx, sd->subchannel, "round_robin");
Craig Tillere2a65102015-11-30 17:51:49 -0800419 p->subchannels[sd->index]->index = sd->index;
420 gpr_free(sd);
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700421
Craig Tillere2a65102015-11-30 17:51:49 -0800422 unref = 1;
Craig Tillera82950e2015-09-22 12:33:20 -0700423 if (p->num_subchannels == 0) {
424 grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
425 GRPC_CHANNEL_FATAL_FAILURE,
426 "no_more_channels");
427 while ((pp = p->pending_picks)) {
428 p->pending_picks = pp->next;
429 *pp->target = NULL;
Craig Tiller6c396862016-01-28 13:53:40 -0800430 grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, true, NULL);
Craig Tillera82950e2015-09-22 12:33:20 -0700431 gpr_free(pp);
432 }
Craig Tillera82950e2015-09-22 12:33:20 -0700433 } else {
434 grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
435 GRPC_CHANNEL_TRANSIENT_FAILURE,
436 "subchannel_failed");
437 }
438 } /* switch */
439 } /* !unref */
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700440
Craig Tillera82950e2015-09-22 12:33:20 -0700441 gpr_mu_unlock(&p->mu);
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700442
Craig Tillera82950e2015-09-22 12:33:20 -0700443 if (unref) {
Craig Tiller2a1bb7f2015-11-29 21:54:26 -0800444 GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &p->base, "round_robin_connectivity");
Craig Tillera82950e2015-09-22 12:33:20 -0700445 }
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700446}
447
Craig Tillera82950e2015-09-22 12:33:20 -0700448static grpc_connectivity_state rr_check_connectivity(grpc_exec_ctx *exec_ctx,
449 grpc_lb_policy *pol) {
450 round_robin_lb_policy *p = (round_robin_lb_policy *)pol;
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700451 grpc_connectivity_state st;
Craig Tillera82950e2015-09-22 12:33:20 -0700452 gpr_mu_lock(&p->mu);
453 st = grpc_connectivity_state_check(&p->state_tracker);
454 gpr_mu_unlock(&p->mu);
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700455 return st;
456}
457
Craig Tillera82950e2015-09-22 12:33:20 -0700458static void rr_notify_on_state_change(grpc_exec_ctx *exec_ctx,
459 grpc_lb_policy *pol,
460 grpc_connectivity_state *current,
461 grpc_closure *notify) {
462 round_robin_lb_policy *p = (round_robin_lb_policy *)pol;
463 gpr_mu_lock(&p->mu);
464 grpc_connectivity_state_notify_on_state_change(exec_ctx, &p->state_tracker,
465 current, notify);
466 gpr_mu_unlock(&p->mu);
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700467}
468
Craig Tiller28bf8912015-12-07 16:07:04 -0800469static void rr_ping_one(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol,
470 grpc_closure *closure) {
471 round_robin_lb_policy *p = (round_robin_lb_policy *)pol;
472 ready_list *selected;
473 grpc_connected_subchannel *target;
474 gpr_mu_lock(&p->mu);
475 if ((selected = peek_next_connected_locked(p))) {
476 gpr_mu_unlock(&p->mu);
477 target = grpc_subchannel_get_connected_subchannel(selected->subchannel);
478 grpc_connected_subchannel_ping(exec_ctx, target, closure);
479 } else {
480 gpr_mu_unlock(&p->mu);
Craig Tiller6c396862016-01-28 13:53:40 -0800481 grpc_exec_ctx_enqueue(exec_ctx, closure, false, NULL);
Craig Tiller28bf8912015-12-07 16:07:04 -0800482 }
483}
484
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700485static const grpc_lb_policy_vtable round_robin_lb_policy_vtable = {
Craig Tillerf40df232016-03-25 13:38:14 -0700486 rr_destroy,
487 rr_shutdown,
488 rr_pick,
489 rr_cancel_pick,
490 rr_ping_one,
491 rr_exit_idle,
492 rr_check_connectivity,
493 rr_notify_on_state_change};
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700494
Craig Tillera82950e2015-09-22 12:33:20 -0700495static void round_robin_factory_ref(grpc_lb_policy_factory *factory) {}
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700496
Craig Tillera82950e2015-09-22 12:33:20 -0700497static void round_robin_factory_unref(grpc_lb_policy_factory *factory) {}
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700498
Craig Tillera82950e2015-09-22 12:33:20 -0700499static grpc_lb_policy *create_round_robin(grpc_lb_policy_factory *factory,
500 grpc_lb_policy_args *args) {
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700501 size_t i;
Craig Tillera82950e2015-09-22 12:33:20 -0700502 round_robin_lb_policy *p = gpr_malloc(sizeof(*p));
503 GPR_ASSERT(args->num_subchannels > 0);
504 memset(p, 0, sizeof(*p));
505 grpc_lb_policy_init(&p->base, &round_robin_lb_policy_vtable);
David Garcia Quintasa4c43a62015-09-11 13:33:57 -0700506 p->num_subchannels = args->num_subchannels;
Craig Tiller1d881fb2015-12-01 07:39:04 -0800507 p->subchannels = gpr_malloc(sizeof(*p->subchannels) * p->num_subchannels);
Craig Tillere2a65102015-11-30 17:51:49 -0800508 memset(p->subchannels, 0, sizeof(*p->subchannels) * p->num_subchannels);
Craig Tillera82950e2015-09-22 12:33:20 -0700509 grpc_connectivity_state_init(&p->state_tracker, GRPC_CHANNEL_IDLE,
510 "round_robin");
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700511
Craig Tillera82950e2015-09-22 12:33:20 -0700512 gpr_mu_init(&p->mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700513 for (i = 0; i < args->num_subchannels; i++) {
Craig Tillere2a65102015-11-30 17:51:49 -0800514 subchannel_data *sd = gpr_malloc(sizeof(*sd));
515 memset(sd, 0, sizeof(*sd));
516 p->subchannels[i] = sd;
517 sd->policy = p;
518 sd->index = i;
519 sd->subchannel = args->subchannels[i];
Craig Tiller1d881fb2015-12-01 07:39:04 -0800520 grpc_closure_init(&sd->connectivity_changed_closure,
521 rr_connectivity_changed, sd);
Craig Tillera82950e2015-09-22 12:33:20 -0700522 }
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700523
524 /* The (dummy node) root of the ready list */
525 p->ready_list.subchannel = NULL;
526 p->ready_list.prev = NULL;
527 p->ready_list.next = NULL;
528 p->ready_list_last_pick = &p->ready_list;
529
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700530 return &p->base;
531}
532
533static const grpc_lb_policy_factory_vtable round_robin_factory_vtable = {
Craig Tillera82950e2015-09-22 12:33:20 -0700534 round_robin_factory_ref, round_robin_factory_unref, create_round_robin,
535 "round_robin"};
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700536
537static grpc_lb_policy_factory round_robin_lb_policy_factory = {
Craig Tillera82950e2015-09-22 12:33:20 -0700538 &round_robin_factory_vtable};
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700539
Craig Tillera82950e2015-09-22 12:33:20 -0700540grpc_lb_policy_factory *grpc_round_robin_lb_factory_create() {
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700541 return &round_robin_lb_policy_factory;
542}