blob: 8645333c8e397612db0d7717b70e786e49eb381b [file] [log] [blame]
David Garcia Quintas4fb049b2015-09-03 17:26:06 -07001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, 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
David Garcia Quintas4fb049b2015-09-03 17:26:06 -070034#include <string.h>
35
36#include <grpc/support/alloc.h>
Craig Tillerfb433852016-03-29 08:51:07 -070037
Craig Tillerd4c98332016-03-31 13:45:47 -070038#include "src/core/ext/client_config/lb_policy_registry.h"
Craig Tillerfb433852016-03-29 08:51:07 -070039#include "src/core/lib/debug/trace.h"
Craig Tiller9533d042016-03-25 17:11:06 -070040#include "src/core/lib/transport/connectivity_state.h"
David Garcia Quintas4fb049b2015-09-03 17:26:06 -070041
Craig Tillere2a65102015-11-30 17:51:49 -080042typedef struct round_robin_lb_policy round_robin_lb_policy;
43
David Garcia Quintas4fb049b2015-09-03 17:26:06 -070044int grpc_lb_round_robin_trace = 0;
45
46/** List of entities waiting for a pick.
47 *
48 * Once a pick is available, \a target is updated and \a on_complete called. */
Craig Tillera82950e2015-09-22 12:33:20 -070049typedef struct pending_pick {
David Garcia Quintas4fb049b2015-09-03 17:26:06 -070050 struct pending_pick *next;
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -070051 grpc_polling_entity *pollent;
Craig Tiller8c0d96f2016-03-11 14:27:52 -080052 uint32_t initial_metadata_flags;
Craig Tillerb5585d42015-11-17 07:18:31 -080053 grpc_connected_subchannel **target;
Craig Tiller10ee2742015-09-22 09:25:57 -070054 grpc_closure *on_complete;
David Garcia Quintas4fb049b2015-09-03 17:26:06 -070055} pending_pick;
56
57/** List of subchannels in a connectivity READY state */
Craig Tillera82950e2015-09-22 12:33:20 -070058typedef struct ready_list {
David Garcia Quintas4fb049b2015-09-03 17:26:06 -070059 grpc_subchannel *subchannel;
60 struct ready_list *next;
61 struct ready_list *prev;
62} ready_list;
63
Craig Tillera82950e2015-09-22 12:33:20 -070064typedef struct {
Craig Tillere2a65102015-11-30 17:51:49 -080065 /** index within policy->subchannels */
66 size_t index;
67 /** backpointer to owning policy */
68 round_robin_lb_policy *policy;
69 /** subchannel itself */
70 grpc_subchannel *subchannel;
71 /** notification that connectivity has changed on subchannel */
72 grpc_closure connectivity_changed_closure;
73 /** this subchannels current position in subchannel->ready_list */
74 ready_list *ready_list_node;
75 /** last observed connectivity */
76 grpc_connectivity_state connectivity_state;
77} subchannel_data;
David Garcia Quintas4fb049b2015-09-03 17:26:06 -070078
Craig Tillere2a65102015-11-30 17:51:49 -080079struct round_robin_lb_policy {
David Garcia Quintas4fb049b2015-09-03 17:26:06 -070080 /** base policy: must be first */
81 grpc_lb_policy base;
82
83 /** all our subchannels */
David Garcia Quintas4fb049b2015-09-03 17:26:06 -070084 size_t num_subchannels;
Craig Tillere2a65102015-11-30 17:51:49 -080085 subchannel_data **subchannels;
David Garcia Quintas4fb049b2015-09-03 17:26:06 -070086
87 /** mutex protecting remaining members */
88 gpr_mu mu;
89 /** have we started picking? */
90 int started_picking;
91 /** are we shutting down? */
92 int shutdown;
David Garcia Quintas4fb049b2015-09-03 17:26:06 -070093 /** List of picks that are waiting on connectivity */
94 pending_pick *pending_picks;
95
96 /** our connectivity state tracker */
97 grpc_connectivity_state_tracker state_tracker;
98
99 /** (Dummy) root of the doubly linked list containing READY subchannels */
100 ready_list ready_list;
101 /** Last pick from the ready list. */
102 ready_list *ready_list_last_pick;
Craig Tillere2a65102015-11-30 17:51:49 -0800103};
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700104
105/** Returns the next subchannel from the connected list or NULL if the list is
106 * empty.
107 *
108 * Note that this function does *not* advance p->ready_list_last_pick. Use \a
109 * advance_last_picked_locked() for that. */
Craig Tillera82950e2015-09-22 12:33:20 -0700110static ready_list *peek_next_connected_locked(const round_robin_lb_policy *p) {
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700111 ready_list *selected;
112 selected = p->ready_list_last_pick->next;
113
Craig Tillera82950e2015-09-22 12:33:20 -0700114 while (selected != NULL) {
115 if (selected == &p->ready_list) {
116 GPR_ASSERT(selected->subchannel == NULL);
117 /* skip dummy root */
118 selected = selected->next;
119 } else {
120 GPR_ASSERT(selected->subchannel != NULL);
121 return selected;
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700122 }
Craig Tillera82950e2015-09-22 12:33:20 -0700123 }
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700124 return NULL;
125}
126
127/** Advance the \a ready_list picking head. */
Craig Tillera82950e2015-09-22 12:33:20 -0700128static void advance_last_picked_locked(round_robin_lb_policy *p) {
129 if (p->ready_list_last_pick->next != NULL) { /* non-empty list */
130 p->ready_list_last_pick = p->ready_list_last_pick->next;
131 if (p->ready_list_last_pick == &p->ready_list) {
132 /* skip dummy root */
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700133 p->ready_list_last_pick = p->ready_list_last_pick->next;
134 }
Craig Tillera82950e2015-09-22 12:33:20 -0700135 } else { /* should be an empty list */
136 GPR_ASSERT(p->ready_list_last_pick == &p->ready_list);
137 }
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700138
Craig Tillera82950e2015-09-22 12:33:20 -0700139 if (grpc_lb_round_robin_trace) {
140 gpr_log(GPR_DEBUG, "[READYLIST] ADVANCED LAST PICK. NOW AT NODE %p (SC %p)",
141 p->ready_list_last_pick, p->ready_list_last_pick->subchannel);
142 }
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700143}
144
145/** Prepends (relative to the root at p->ready_list) the connected subchannel \a
146 * csc to the list of ready subchannels. */
Craig Tillera82950e2015-09-22 12:33:20 -0700147static ready_list *add_connected_sc_locked(round_robin_lb_policy *p,
Craig Tillerb5585d42015-11-17 07:18:31 -0800148 grpc_subchannel *sc) {
Craig Tillera82950e2015-09-22 12:33:20 -0700149 ready_list *new_elem = gpr_malloc(sizeof(ready_list));
Craig Tillerb5585d42015-11-17 07:18:31 -0800150 new_elem->subchannel = sc;
Craig Tillera82950e2015-09-22 12:33:20 -0700151 if (p->ready_list.prev == NULL) {
152 /* first element */
153 new_elem->next = &p->ready_list;
154 new_elem->prev = &p->ready_list;
155 p->ready_list.next = new_elem;
156 p->ready_list.prev = new_elem;
157 } else {
158 new_elem->next = &p->ready_list;
159 new_elem->prev = p->ready_list.prev;
160 p->ready_list.prev->next = new_elem;
161 p->ready_list.prev = new_elem;
162 }
163 if (grpc_lb_round_robin_trace) {
Craig Tillerb5585d42015-11-17 07:18:31 -0800164 gpr_log(GPR_DEBUG, "[READYLIST] ADDING NODE %p (SC %p)", new_elem, sc);
Craig Tillera82950e2015-09-22 12:33:20 -0700165 }
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700166 return new_elem;
167}
168
169/** Removes \a node from the list of connected subchannels */
Craig Tillera82950e2015-09-22 12:33:20 -0700170static void remove_disconnected_sc_locked(round_robin_lb_policy *p,
171 ready_list *node) {
172 if (node == NULL) {
173 return;
174 }
175 if (node == p->ready_list_last_pick) {
176 /* If removing the lastly picked node, reset the last pick pointer to the
177 * dummy root of the list */
178 p->ready_list_last_pick = &p->ready_list;
179 }
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700180
181 /* removing last item */
Craig Tillera82950e2015-09-22 12:33:20 -0700182 if (node->next == &p->ready_list && node->prev == &p->ready_list) {
183 GPR_ASSERT(p->ready_list.next == node);
184 GPR_ASSERT(p->ready_list.prev == node);
185 p->ready_list.next = NULL;
186 p->ready_list.prev = NULL;
187 } else {
188 node->prev->next = node->next;
189 node->next->prev = node->prev;
190 }
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700191
Craig Tillera82950e2015-09-22 12:33:20 -0700192 if (grpc_lb_round_robin_trace) {
193 gpr_log(GPR_DEBUG, "[READYLIST] REMOVED NODE %p (SC %p)", node,
194 node->subchannel);
195 }
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700196
197 node->next = NULL;
198 node->prev = NULL;
199 node->subchannel = NULL;
200
Craig Tillera82950e2015-09-22 12:33:20 -0700201 gpr_free(node);
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700202}
203
Craig Tillerfb433852016-03-29 08:51:07 -0700204static void rr_destroy(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) {
Craig Tillera82950e2015-09-22 12:33:20 -0700205 round_robin_lb_policy *p = (round_robin_lb_policy *)pol;
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700206 size_t i;
207 ready_list *elem;
Craig Tillera82950e2015-09-22 12:33:20 -0700208 for (i = 0; i < p->num_subchannels; i++) {
Craig Tillere2a65102015-11-30 17:51:49 -0800209 subchannel_data *sd = p->subchannels[i];
210 GRPC_SUBCHANNEL_UNREF(exec_ctx, sd->subchannel, "round_robin");
211 gpr_free(sd);
Craig Tillera82950e2015-09-22 12:33:20 -0700212 }
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700213
Craig Tillera82950e2015-09-22 12:33:20 -0700214 grpc_connectivity_state_destroy(exec_ctx, &p->state_tracker);
215 gpr_free(p->subchannels);
216 gpr_mu_destroy(&p->mu);
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700217
218 elem = p->ready_list.next;
Craig Tillera82950e2015-09-22 12:33:20 -0700219 while (elem != NULL && elem != &p->ready_list) {
220 ready_list *tmp;
221 tmp = elem->next;
222 elem->next = NULL;
223 elem->prev = NULL;
224 elem->subchannel = NULL;
225 gpr_free(elem);
226 elem = tmp;
227 }
Craig Tillera82950e2015-09-22 12:33:20 -0700228 gpr_free(p);
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700229}
230
Craig Tillerfb433852016-03-29 08:51:07 -0700231static void rr_shutdown(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) {
Craig Tillera82950e2015-09-22 12:33:20 -0700232 round_robin_lb_policy *p = (round_robin_lb_policy *)pol;
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700233 pending_pick *pp;
Craig Tiller2a1bb7f2015-11-29 21:54:26 -0800234 size_t i;
235
Craig Tillera82950e2015-09-22 12:33:20 -0700236 gpr_mu_lock(&p->mu);
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700237
238 p->shutdown = 1;
Craig Tillera82950e2015-09-22 12:33:20 -0700239 while ((pp = p->pending_picks)) {
240 p->pending_picks = pp->next;
241 *pp->target = NULL;
Craig Tiller6c396862016-01-28 13:53:40 -0800242 grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, false, NULL);
Craig Tillera82950e2015-09-22 12:33:20 -0700243 gpr_free(pp);
244 }
245 grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
Craig Tiller48ed92e2016-06-02 11:07:12 -0700246 GRPC_CHANNEL_SHUTDOWN, "shutdown");
Craig Tiller2a1bb7f2015-11-29 21:54:26 -0800247 for (i = 0; i < p->num_subchannels; i++) {
Craig Tillere2a65102015-11-30 17:51:49 -0800248 subchannel_data *sd = p->subchannels[i];
Craig Tiller1d881fb2015-12-01 07:39:04 -0800249 grpc_subchannel_notify_on_state_change(exec_ctx, sd->subchannel, NULL, NULL,
Craig Tillere2a65102015-11-30 17:51:49 -0800250 &sd->connectivity_changed_closure);
Craig Tiller2a1bb7f2015-11-29 21:54:26 -0800251 }
Craig Tillera82950e2015-09-22 12:33:20 -0700252 gpr_mu_unlock(&p->mu);
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700253}
254
Craig Tiller577c9b22015-11-02 14:11:15 -0800255static void rr_cancel_pick(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol,
Craig Tillerb5585d42015-11-17 07:18:31 -0800256 grpc_connected_subchannel **target) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800257 round_robin_lb_policy *p = (round_robin_lb_policy *)pol;
258 pending_pick *pp;
Craig Tiller577c9b22015-11-02 14:11:15 -0800259 gpr_mu_lock(&p->mu);
260 pp = p->pending_picks;
261 p->pending_picks = NULL;
262 while (pp != NULL) {
263 pending_pick *next = pp->next;
264 if (pp->target == target) {
David Garcia Quintasc4d51122016-06-06 14:56:02 -0700265 grpc_polling_entity_del_from_pollset_set(exec_ctx, pp->pollent,
David Garcia Quintas69ff63d2016-06-06 16:39:47 -0700266 p->base.interested_parties);
Craig Tiller577c9b22015-11-02 14:11:15 -0800267 *target = NULL;
Craig Tiller6c396862016-01-28 13:53:40 -0800268 grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, false, NULL);
Craig Tiller577c9b22015-11-02 14:11:15 -0800269 gpr_free(pp);
270 } else {
271 pp->next = p->pending_picks;
272 p->pending_picks = pp;
273 }
274 pp = next;
275 }
276 gpr_mu_unlock(&p->mu);
277}
278
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800279static void rr_cancel_picks(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol,
280 uint32_t initial_metadata_flags_mask,
281 uint32_t initial_metadata_flags_eq) {
282 round_robin_lb_policy *p = (round_robin_lb_policy *)pol;
283 pending_pick *pp;
284 gpr_mu_lock(&p->mu);
285 pp = p->pending_picks;
286 p->pending_picks = NULL;
287 while (pp != NULL) {
288 pending_pick *next = pp->next;
289 if ((pp->initial_metadata_flags & initial_metadata_flags_mask) ==
290 initial_metadata_flags_eq) {
David Garcia Quintasc4d51122016-06-06 14:56:02 -0700291 grpc_polling_entity_del_from_pollset_set(exec_ctx, pp->pollent,
David Garcia Quintas69ff63d2016-06-06 16:39:47 -0700292 p->base.interested_parties);
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800293 *pp->target = NULL;
294 grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, false, NULL);
295 gpr_free(pp);
296 } else {
297 pp->next = p->pending_picks;
298 p->pending_picks = pp;
299 }
300 pp = next;
301 }
302 gpr_mu_unlock(&p->mu);
303}
304
Craig Tillera82950e2015-09-22 12:33:20 -0700305static void start_picking(grpc_exec_ctx *exec_ctx, round_robin_lb_policy *p) {
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700306 size_t i;
307 p->started_picking = 1;
308
Yuchen Zengfad04502016-04-29 16:43:46 -0700309 if (grpc_lb_round_robin_trace) {
310 gpr_log(GPR_DEBUG, "LB_POLICY: p=%p num_subchannels=%d", p,
311 p->num_subchannels);
312 }
Craig Tiller2a1bb7f2015-11-29 21:54:26 -0800313
Craig Tillera82950e2015-09-22 12:33:20 -0700314 for (i = 0; i < p->num_subchannels; i++) {
Craig Tillere2a65102015-11-30 17:51:49 -0800315 subchannel_data *sd = p->subchannels[i];
316 sd->connectivity_state = GRPC_CHANNEL_IDLE;
Craig Tiller1d881fb2015-12-01 07:39:04 -0800317 grpc_subchannel_notify_on_state_change(
Craig Tiller69b093b2016-02-25 19:04:07 -0800318 exec_ctx, sd->subchannel, p->base.interested_parties,
Craig Tiller1d881fb2015-12-01 07:39:04 -0800319 &sd->connectivity_state, &sd->connectivity_changed_closure);
Craig Tiller2a1bb7f2015-11-29 21:54:26 -0800320 GRPC_LB_POLICY_WEAK_REF(&p->base, "round_robin_connectivity");
Craig Tillera82950e2015-09-22 12:33:20 -0700321 }
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700322}
323
Craig Tillerfb433852016-03-29 08:51:07 -0700324static void rr_exit_idle(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) {
Craig Tillera82950e2015-09-22 12:33:20 -0700325 round_robin_lb_policy *p = (round_robin_lb_policy *)pol;
326 gpr_mu_lock(&p->mu);
327 if (!p->started_picking) {
328 start_picking(exec_ctx, p);
329 }
330 gpr_mu_unlock(&p->mu);
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700331}
332
Craig Tillerfb433852016-03-29 08:51:07 -0700333static int rr_pick(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol,
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -0700334 grpc_polling_entity *pollent,
335 grpc_metadata_batch *initial_metadata,
Craig Tiller41dac842016-03-31 14:01:53 -0700336 uint32_t initial_metadata_flags,
Craig Tillerfb433852016-03-29 08:51:07 -0700337 grpc_connected_subchannel **target,
338 grpc_closure *on_complete) {
Craig Tillera82950e2015-09-22 12:33:20 -0700339 round_robin_lb_policy *p = (round_robin_lb_policy *)pol;
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700340 pending_pick *pp;
341 ready_list *selected;
Craig Tillera82950e2015-09-22 12:33:20 -0700342 gpr_mu_lock(&p->mu);
343 if ((selected = peek_next_connected_locked(p))) {
344 gpr_mu_unlock(&p->mu);
Craig Tillerb5585d42015-11-17 07:18:31 -0800345 *target = grpc_subchannel_get_connected_subchannel(selected->subchannel);
Craig Tillera82950e2015-09-22 12:33:20 -0700346 if (grpc_lb_round_robin_trace) {
Craig Tillerab33b482015-11-21 08:11:04 -0800347 gpr_log(GPR_DEBUG,
348 "[RR PICK] TARGET <-- CONNECTED SUBCHANNEL %p (NODE %p)",
Craig Tillera82950e2015-09-22 12:33:20 -0700349 selected->subchannel, selected);
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700350 }
Craig Tillera82950e2015-09-22 12:33:20 -0700351 /* only advance the last picked pointer if the selection was used */
352 advance_last_picked_locked(p);
Craig Tiller577c9b22015-11-02 14:11:15 -0800353 return 1;
Craig Tillera82950e2015-09-22 12:33:20 -0700354 } else {
355 if (!p->started_picking) {
356 start_picking(exec_ctx, p);
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700357 }
David Garcia Quintas69ff63d2016-06-06 16:39:47 -0700358 grpc_polling_entity_add_to_pollset_set(exec_ctx, pollent,
359 p->base.interested_parties);
Craig Tillera82950e2015-09-22 12:33:20 -0700360 pp = gpr_malloc(sizeof(*pp));
361 pp->next = p->pending_picks;
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -0700362 pp->pollent = pollent;
Craig Tillera82950e2015-09-22 12:33:20 -0700363 pp->target = target;
364 pp->on_complete = on_complete;
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800365 pp->initial_metadata_flags = initial_metadata_flags;
Craig Tillera82950e2015-09-22 12:33:20 -0700366 p->pending_picks = pp;
367 gpr_mu_unlock(&p->mu);
Craig Tiller577c9b22015-11-02 14:11:15 -0800368 return 0;
Craig Tillera82950e2015-09-22 12:33:20 -0700369 }
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700370}
371
Craig Tillera82950e2015-09-22 12:33:20 -0700372static void rr_connectivity_changed(grpc_exec_ctx *exec_ctx, void *arg,
Craig Tiller6c396862016-01-28 13:53:40 -0800373 bool iomgr_success) {
Craig Tillere2a65102015-11-30 17:51:49 -0800374 subchannel_data *sd = arg;
375 round_robin_lb_policy *p = sd->policy;
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700376 pending_pick *pp;
377 ready_list *selected;
378
379 int unref = 0;
380
Craig Tillera82950e2015-09-22 12:33:20 -0700381 gpr_mu_lock(&p->mu);
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700382
Craig Tillera82950e2015-09-22 12:33:20 -0700383 if (p->shutdown) {
384 unref = 1;
385 } else {
Craig Tillere2a65102015-11-30 17:51:49 -0800386 switch (sd->connectivity_state) {
Craig Tillera82950e2015-09-22 12:33:20 -0700387 case GRPC_CHANNEL_READY:
388 grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
389 GRPC_CHANNEL_READY, "connecting_ready");
390 /* add the newly connected subchannel to the list of connected ones.
391 * Note that it goes to the "end of the line". */
Craig Tiller1d881fb2015-12-01 07:39:04 -0800392 sd->ready_list_node = add_connected_sc_locked(p, sd->subchannel);
Craig Tillera82950e2015-09-22 12:33:20 -0700393 /* at this point we know there's at least one suitable subchannel. Go
394 * ahead and pick one and notify the pending suitors in
395 * p->pending_picks. This preemtively replicates rr_pick()'s actions. */
396 selected = peek_next_connected_locked(p);
397 if (p->pending_picks != NULL) {
398 /* if the selected subchannel is going to be used for the pending
399 * picks, update the last picked pointer */
400 advance_last_picked_locked(p);
401 }
402 while ((pp = p->pending_picks)) {
403 p->pending_picks = pp->next;
Craig Tillerab33b482015-11-21 08:11:04 -0800404 *pp->target =
405 grpc_subchannel_get_connected_subchannel(selected->subchannel);
Craig Tillera82950e2015-09-22 12:33:20 -0700406 if (grpc_lb_round_robin_trace) {
407 gpr_log(GPR_DEBUG,
408 "[RR CONN CHANGED] TARGET <-- SUBCHANNEL %p (NODE %p)",
409 selected->subchannel, selected);
410 }
David Garcia Quintasc4d51122016-06-06 14:56:02 -0700411 grpc_polling_entity_del_from_pollset_set(exec_ctx, pp->pollent,
David Garcia Quintas69ff63d2016-06-06 16:39:47 -0700412 p->base.interested_parties);
Craig Tiller6c396862016-01-28 13:53:40 -0800413 grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, true, NULL);
Craig Tillera82950e2015-09-22 12:33:20 -0700414 gpr_free(pp);
415 }
416 grpc_subchannel_notify_on_state_change(
Craig Tiller69b093b2016-02-25 19:04:07 -0800417 exec_ctx, sd->subchannel, p->base.interested_parties,
Craig Tiller1d881fb2015-12-01 07:39:04 -0800418 &sd->connectivity_state, &sd->connectivity_changed_closure);
Craig Tillera82950e2015-09-22 12:33:20 -0700419 break;
420 case GRPC_CHANNEL_CONNECTING:
421 case GRPC_CHANNEL_IDLE:
422 grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
Craig Tiller1d881fb2015-12-01 07:39:04 -0800423 sd->connectivity_state,
424 "connecting_changed");
Craig Tillera82950e2015-09-22 12:33:20 -0700425 grpc_subchannel_notify_on_state_change(
Craig Tiller69b093b2016-02-25 19:04:07 -0800426 exec_ctx, sd->subchannel, p->base.interested_parties,
Craig Tiller1d881fb2015-12-01 07:39:04 -0800427 &sd->connectivity_state, &sd->connectivity_changed_closure);
Craig Tillera82950e2015-09-22 12:33:20 -0700428 break;
429 case GRPC_CHANNEL_TRANSIENT_FAILURE:
Craig Tillera82950e2015-09-22 12:33:20 -0700430 /* renew state notification */
431 grpc_subchannel_notify_on_state_change(
Craig Tiller69b093b2016-02-25 19:04:07 -0800432 exec_ctx, sd->subchannel, p->base.interested_parties,
Craig Tiller1d881fb2015-12-01 07:39:04 -0800433 &sd->connectivity_state, &sd->connectivity_changed_closure);
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700434
Craig Tillera82950e2015-09-22 12:33:20 -0700435 /* remove from ready list if still present */
Craig Tillere2a65102015-11-30 17:51:49 -0800436 if (sd->ready_list_node != NULL) {
437 remove_disconnected_sc_locked(p, sd->ready_list_node);
438 sd->ready_list_node = NULL;
Craig Tillera82950e2015-09-22 12:33:20 -0700439 }
440 grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
441 GRPC_CHANNEL_TRANSIENT_FAILURE,
442 "connecting_transient_failure");
443 break;
Craig Tiller48ed92e2016-06-02 11:07:12 -0700444 case GRPC_CHANNEL_SHUTDOWN:
Craig Tillere2a65102015-11-30 17:51:49 -0800445 if (sd->ready_list_node != NULL) {
Craig Tiller1d881fb2015-12-01 07:39:04 -0800446 remove_disconnected_sc_locked(p, sd->ready_list_node);
Craig Tillere2a65102015-11-30 17:51:49 -0800447 sd->ready_list_node = NULL;
Craig Tillera82950e2015-09-22 12:33:20 -0700448 }
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700449
Craig Tillera82950e2015-09-22 12:33:20 -0700450 p->num_subchannels--;
Craig Tillere2a65102015-11-30 17:51:49 -0800451 GPR_SWAP(subchannel_data *, p->subchannels[sd->index],
452 p->subchannels[p->num_subchannels]);
Craig Tiller1d881fb2015-12-01 07:39:04 -0800453 GRPC_SUBCHANNEL_UNREF(exec_ctx, sd->subchannel, "round_robin");
Craig Tillere2a65102015-11-30 17:51:49 -0800454 p->subchannels[sd->index]->index = sd->index;
455 gpr_free(sd);
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700456
Craig Tillere2a65102015-11-30 17:51:49 -0800457 unref = 1;
Craig Tillera82950e2015-09-22 12:33:20 -0700458 if (p->num_subchannels == 0) {
459 grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
Craig Tiller48ed92e2016-06-02 11:07:12 -0700460 GRPC_CHANNEL_SHUTDOWN,
Craig Tillera82950e2015-09-22 12:33:20 -0700461 "no_more_channels");
462 while ((pp = p->pending_picks)) {
463 p->pending_picks = pp->next;
464 *pp->target = NULL;
Craig Tiller6c396862016-01-28 13:53:40 -0800465 grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, true, NULL);
Craig Tillera82950e2015-09-22 12:33:20 -0700466 gpr_free(pp);
467 }
Craig Tillera82950e2015-09-22 12:33:20 -0700468 } else {
469 grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
470 GRPC_CHANNEL_TRANSIENT_FAILURE,
471 "subchannel_failed");
472 }
473 } /* switch */
474 } /* !unref */
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700475
Craig Tillera82950e2015-09-22 12:33:20 -0700476 gpr_mu_unlock(&p->mu);
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700477
Craig Tillera82950e2015-09-22 12:33:20 -0700478 if (unref) {
Craig Tiller2a1bb7f2015-11-29 21:54:26 -0800479 GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &p->base, "round_robin_connectivity");
Craig Tillera82950e2015-09-22 12:33:20 -0700480 }
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700481}
482
Craig Tillera82950e2015-09-22 12:33:20 -0700483static grpc_connectivity_state rr_check_connectivity(grpc_exec_ctx *exec_ctx,
484 grpc_lb_policy *pol) {
485 round_robin_lb_policy *p = (round_robin_lb_policy *)pol;
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700486 grpc_connectivity_state st;
Craig Tillera82950e2015-09-22 12:33:20 -0700487 gpr_mu_lock(&p->mu);
488 st = grpc_connectivity_state_check(&p->state_tracker);
489 gpr_mu_unlock(&p->mu);
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700490 return st;
491}
492
Craig Tillera82950e2015-09-22 12:33:20 -0700493static void rr_notify_on_state_change(grpc_exec_ctx *exec_ctx,
494 grpc_lb_policy *pol,
495 grpc_connectivity_state *current,
496 grpc_closure *notify) {
497 round_robin_lb_policy *p = (round_robin_lb_policy *)pol;
498 gpr_mu_lock(&p->mu);
499 grpc_connectivity_state_notify_on_state_change(exec_ctx, &p->state_tracker,
500 current, notify);
501 gpr_mu_unlock(&p->mu);
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700502}
503
Craig Tiller28bf8912015-12-07 16:07:04 -0800504static void rr_ping_one(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol,
505 grpc_closure *closure) {
506 round_robin_lb_policy *p = (round_robin_lb_policy *)pol;
507 ready_list *selected;
508 grpc_connected_subchannel *target;
509 gpr_mu_lock(&p->mu);
510 if ((selected = peek_next_connected_locked(p))) {
511 gpr_mu_unlock(&p->mu);
512 target = grpc_subchannel_get_connected_subchannel(selected->subchannel);
513 grpc_connected_subchannel_ping(exec_ctx, target, closure);
514 } else {
515 gpr_mu_unlock(&p->mu);
Craig Tiller6c396862016-01-28 13:53:40 -0800516 grpc_exec_ctx_enqueue(exec_ctx, closure, false, NULL);
Craig Tiller28bf8912015-12-07 16:07:04 -0800517 }
518}
519
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700520static const grpc_lb_policy_vtable round_robin_lb_policy_vtable = {
Craig Tillerc5ff7812016-03-28 12:45:55 -0700521 rr_destroy, rr_shutdown, rr_pick,
522 rr_cancel_pick, rr_cancel_picks, rr_ping_one,
523 rr_exit_idle, rr_check_connectivity, rr_notify_on_state_change};
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700524
Craig Tillera82950e2015-09-22 12:33:20 -0700525static void round_robin_factory_ref(grpc_lb_policy_factory *factory) {}
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700526
Craig Tillera82950e2015-09-22 12:33:20 -0700527static void round_robin_factory_unref(grpc_lb_policy_factory *factory) {}
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700528
David Garcia Quintas67c0d042016-03-25 01:37:53 -0700529static grpc_lb_policy *create_round_robin(grpc_exec_ctx *exec_ctx,
530 grpc_lb_policy_factory *factory,
Craig Tillera82950e2015-09-22 12:33:20 -0700531 grpc_lb_policy_args *args) {
David Garcia Quintas67c0d042016-03-25 01:37:53 -0700532 GPR_ASSERT(args->addresses != NULL);
David Garcia Quintas86fcfcc2016-03-31 23:22:28 -0700533 GPR_ASSERT(args->client_channel_factory != NULL);
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700534
David Garcia Quintas67c0d042016-03-25 01:37:53 -0700535 round_robin_lb_policy *p = gpr_malloc(sizeof(*p));
536 memset(p, 0, sizeof(*p));
537
538 p->subchannels =
539 gpr_malloc(sizeof(*p->subchannels) * args->addresses->naddrs);
540 memset(p->subchannels, 0, sizeof(*p->subchannels) * args->addresses->naddrs);
541
542 grpc_subchannel_args sc_args;
543 size_t subchannel_idx = 0;
544 for (size_t i = 0; i < args->addresses->naddrs; i++) {
545 memset(&sc_args, 0, sizeof(grpc_subchannel_args));
546 sc_args.addr = (struct sockaddr *)(args->addresses->addrs[i].addr);
547 sc_args.addr_len = (size_t)args->addresses->addrs[i].len;
548
David Garcia Quintas86fcfcc2016-03-31 23:22:28 -0700549 grpc_subchannel *subchannel = grpc_client_channel_factory_create_subchannel(
550 exec_ctx, args->client_channel_factory, &sc_args);
David Garcia Quintas67c0d042016-03-25 01:37:53 -0700551
552 if (subchannel != NULL) {
553 subchannel_data *sd = gpr_malloc(sizeof(*sd));
554 memset(sd, 0, sizeof(*sd));
555 p->subchannels[subchannel_idx] = sd;
556 sd->policy = p;
557 sd->index = subchannel_idx;
558 sd->subchannel = subchannel;
559 ++subchannel_idx;
560 grpc_closure_init(&sd->connectivity_changed_closure,
561 rr_connectivity_changed, sd);
562 }
Craig Tillera82950e2015-09-22 12:33:20 -0700563 }
David Garcia Quintas67c0d042016-03-25 01:37:53 -0700564 if (subchannel_idx == 0) {
565 gpr_free(p->subchannels);
566 gpr_free(p);
567 return NULL;
568 }
569 p->num_subchannels = subchannel_idx;
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700570
571 /* The (dummy node) root of the ready list */
572 p->ready_list.subchannel = NULL;
573 p->ready_list.prev = NULL;
574 p->ready_list.next = NULL;
575 p->ready_list_last_pick = &p->ready_list;
576
David Garcia Quintas67c0d042016-03-25 01:37:53 -0700577 grpc_lb_policy_init(&p->base, &round_robin_lb_policy_vtable);
578 grpc_connectivity_state_init(&p->state_tracker, GRPC_CHANNEL_IDLE,
579 "round_robin");
580 gpr_mu_init(&p->mu);
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700581 return &p->base;
582}
583
584static const grpc_lb_policy_factory_vtable round_robin_factory_vtable = {
Craig Tillera82950e2015-09-22 12:33:20 -0700585 round_robin_factory_ref, round_robin_factory_unref, create_round_robin,
586 "round_robin"};
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700587
588static grpc_lb_policy_factory round_robin_lb_policy_factory = {
Craig Tillera82950e2015-09-22 12:33:20 -0700589 &round_robin_factory_vtable};
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700590
Craig Tillerfb433852016-03-29 08:51:07 -0700591static grpc_lb_policy_factory *round_robin_lb_factory_create() {
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700592 return &round_robin_lb_policy_factory;
593}
Craig Tillerfb433852016-03-29 08:51:07 -0700594
595/* Plugin registration */
596
597void grpc_lb_policy_round_robin_init() {
Craig Tiller3113ef42016-03-29 09:03:14 -0700598 grpc_register_lb_policy(round_robin_lb_factory_create());
Craig Tillerfb433852016-03-29 08:51:07 -0700599 grpc_register_tracer("round_robin", &grpc_lb_round_robin_trace);
600}
601
602void grpc_lb_policy_round_robin_shutdown() {}