blob: e706a8ddd90c1bc2f73c6eb0e6eb8b039a0949b5 [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,
246 GRPC_CHANNEL_FATAL_FAILURE, "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 Quintas2a50dfe2016-05-31 15:09:12 -0700265 grpc_pops_del_to_pollset_set(exec_ctx, pp->pollent,
David Garcia Quintasf72eb972016-05-03 18:28:09 -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 Quintas2a50dfe2016-05-31 15:09:12 -0700291 grpc_pops_del_to_pollset_set(exec_ctx, pp->pollent,
David Garcia Quintasf72eb972016-05-03 18:28:09 -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 Quintas2a50dfe2016-05-31 15:09:12 -0700358 grpc_pops_add_to_pollset_set(exec_ctx, pollent, p->base.interested_parties);
Craig Tillera82950e2015-09-22 12:33:20 -0700359 pp = gpr_malloc(sizeof(*pp));
360 pp->next = p->pending_picks;
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -0700361 pp->pollent = pollent;
Craig Tillera82950e2015-09-22 12:33:20 -0700362 pp->target = target;
363 pp->on_complete = on_complete;
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800364 pp->initial_metadata_flags = initial_metadata_flags;
Craig Tillera82950e2015-09-22 12:33:20 -0700365 p->pending_picks = pp;
366 gpr_mu_unlock(&p->mu);
Craig Tiller577c9b22015-11-02 14:11:15 -0800367 return 0;
Craig Tillera82950e2015-09-22 12:33:20 -0700368 }
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700369}
370
Craig Tillera82950e2015-09-22 12:33:20 -0700371static void rr_connectivity_changed(grpc_exec_ctx *exec_ctx, void *arg,
Craig Tiller6c396862016-01-28 13:53:40 -0800372 bool iomgr_success) {
Craig Tillere2a65102015-11-30 17:51:49 -0800373 subchannel_data *sd = arg;
374 round_robin_lb_policy *p = sd->policy;
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700375 pending_pick *pp;
376 ready_list *selected;
377
378 int unref = 0;
379
Craig Tillera82950e2015-09-22 12:33:20 -0700380 gpr_mu_lock(&p->mu);
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700381
Craig Tillera82950e2015-09-22 12:33:20 -0700382 if (p->shutdown) {
383 unref = 1;
384 } else {
Craig Tillere2a65102015-11-30 17:51:49 -0800385 switch (sd->connectivity_state) {
Craig Tillera82950e2015-09-22 12:33:20 -0700386 case GRPC_CHANNEL_READY:
387 grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
388 GRPC_CHANNEL_READY, "connecting_ready");
389 /* add the newly connected subchannel to the list of connected ones.
390 * Note that it goes to the "end of the line". */
Craig Tiller1d881fb2015-12-01 07:39:04 -0800391 sd->ready_list_node = add_connected_sc_locked(p, sd->subchannel);
Craig Tillera82950e2015-09-22 12:33:20 -0700392 /* at this point we know there's at least one suitable subchannel. Go
393 * ahead and pick one and notify the pending suitors in
394 * p->pending_picks. This preemtively replicates rr_pick()'s actions. */
395 selected = peek_next_connected_locked(p);
396 if (p->pending_picks != NULL) {
397 /* if the selected subchannel is going to be used for the pending
398 * picks, update the last picked pointer */
399 advance_last_picked_locked(p);
400 }
401 while ((pp = p->pending_picks)) {
402 p->pending_picks = pp->next;
Craig Tillerab33b482015-11-21 08:11:04 -0800403 *pp->target =
404 grpc_subchannel_get_connected_subchannel(selected->subchannel);
Craig Tillera82950e2015-09-22 12:33:20 -0700405 if (grpc_lb_round_robin_trace) {
406 gpr_log(GPR_DEBUG,
407 "[RR CONN CHANGED] TARGET <-- SUBCHANNEL %p (NODE %p)",
408 selected->subchannel, selected);
409 }
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -0700410 grpc_pops_del_to_pollset_set(exec_ctx, pp->pollent,
David Garcia Quintasf72eb972016-05-03 18:28:09 -0700411 p->base.interested_parties);
Craig Tiller6c396862016-01-28 13:53:40 -0800412 grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, true, NULL);
Craig Tillera82950e2015-09-22 12:33:20 -0700413 gpr_free(pp);
414 }
415 grpc_subchannel_notify_on_state_change(
Craig Tiller69b093b2016-02-25 19:04:07 -0800416 exec_ctx, sd->subchannel, p->base.interested_parties,
Craig Tiller1d881fb2015-12-01 07:39:04 -0800417 &sd->connectivity_state, &sd->connectivity_changed_closure);
Craig Tillera82950e2015-09-22 12:33:20 -0700418 break;
419 case GRPC_CHANNEL_CONNECTING:
420 case GRPC_CHANNEL_IDLE:
421 grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
Craig Tiller1d881fb2015-12-01 07:39:04 -0800422 sd->connectivity_state,
423 "connecting_changed");
Craig Tillera82950e2015-09-22 12:33:20 -0700424 grpc_subchannel_notify_on_state_change(
Craig Tiller69b093b2016-02-25 19:04:07 -0800425 exec_ctx, sd->subchannel, p->base.interested_parties,
Craig Tiller1d881fb2015-12-01 07:39:04 -0800426 &sd->connectivity_state, &sd->connectivity_changed_closure);
Craig Tillera82950e2015-09-22 12:33:20 -0700427 break;
428 case GRPC_CHANNEL_TRANSIENT_FAILURE:
Craig Tillera82950e2015-09-22 12:33:20 -0700429 /* renew state notification */
430 grpc_subchannel_notify_on_state_change(
Craig Tiller69b093b2016-02-25 19:04:07 -0800431 exec_ctx, sd->subchannel, p->base.interested_parties,
Craig Tiller1d881fb2015-12-01 07:39:04 -0800432 &sd->connectivity_state, &sd->connectivity_changed_closure);
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700433
Craig Tillera82950e2015-09-22 12:33:20 -0700434 /* remove from ready list if still present */
Craig Tillere2a65102015-11-30 17:51:49 -0800435 if (sd->ready_list_node != NULL) {
436 remove_disconnected_sc_locked(p, sd->ready_list_node);
437 sd->ready_list_node = NULL;
Craig Tillera82950e2015-09-22 12:33:20 -0700438 }
439 grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
440 GRPC_CHANNEL_TRANSIENT_FAILURE,
441 "connecting_transient_failure");
442 break;
443 case GRPC_CHANNEL_FATAL_FAILURE:
Craig Tillere2a65102015-11-30 17:51:49 -0800444 if (sd->ready_list_node != NULL) {
Craig Tiller1d881fb2015-12-01 07:39:04 -0800445 remove_disconnected_sc_locked(p, sd->ready_list_node);
Craig Tillere2a65102015-11-30 17:51:49 -0800446 sd->ready_list_node = NULL;
Craig Tillera82950e2015-09-22 12:33:20 -0700447 }
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700448
Craig Tillera82950e2015-09-22 12:33:20 -0700449 p->num_subchannels--;
Craig Tillere2a65102015-11-30 17:51:49 -0800450 GPR_SWAP(subchannel_data *, p->subchannels[sd->index],
451 p->subchannels[p->num_subchannels]);
Craig Tiller1d881fb2015-12-01 07:39:04 -0800452 GRPC_SUBCHANNEL_UNREF(exec_ctx, sd->subchannel, "round_robin");
Craig Tillere2a65102015-11-30 17:51:49 -0800453 p->subchannels[sd->index]->index = sd->index;
454 gpr_free(sd);
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700455
Craig Tillere2a65102015-11-30 17:51:49 -0800456 unref = 1;
Craig Tillera82950e2015-09-22 12:33:20 -0700457 if (p->num_subchannels == 0) {
458 grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
459 GRPC_CHANNEL_FATAL_FAILURE,
460 "no_more_channels");
461 while ((pp = p->pending_picks)) {
462 p->pending_picks = pp->next;
463 *pp->target = NULL;
Craig Tiller6c396862016-01-28 13:53:40 -0800464 grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, true, NULL);
Craig Tillera82950e2015-09-22 12:33:20 -0700465 gpr_free(pp);
466 }
Craig Tillera82950e2015-09-22 12:33:20 -0700467 } else {
468 grpc_connectivity_state_set(exec_ctx, &p->state_tracker,
469 GRPC_CHANNEL_TRANSIENT_FAILURE,
470 "subchannel_failed");
471 }
472 } /* switch */
473 } /* !unref */
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700474
Craig Tillera82950e2015-09-22 12:33:20 -0700475 gpr_mu_unlock(&p->mu);
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700476
Craig Tillera82950e2015-09-22 12:33:20 -0700477 if (unref) {
Craig Tiller2a1bb7f2015-11-29 21:54:26 -0800478 GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &p->base, "round_robin_connectivity");
Craig Tillera82950e2015-09-22 12:33:20 -0700479 }
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700480}
481
Craig Tillera82950e2015-09-22 12:33:20 -0700482static grpc_connectivity_state rr_check_connectivity(grpc_exec_ctx *exec_ctx,
483 grpc_lb_policy *pol) {
484 round_robin_lb_policy *p = (round_robin_lb_policy *)pol;
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700485 grpc_connectivity_state st;
Craig Tillera82950e2015-09-22 12:33:20 -0700486 gpr_mu_lock(&p->mu);
487 st = grpc_connectivity_state_check(&p->state_tracker);
488 gpr_mu_unlock(&p->mu);
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700489 return st;
490}
491
Craig Tillera82950e2015-09-22 12:33:20 -0700492static void rr_notify_on_state_change(grpc_exec_ctx *exec_ctx,
493 grpc_lb_policy *pol,
494 grpc_connectivity_state *current,
495 grpc_closure *notify) {
496 round_robin_lb_policy *p = (round_robin_lb_policy *)pol;
497 gpr_mu_lock(&p->mu);
498 grpc_connectivity_state_notify_on_state_change(exec_ctx, &p->state_tracker,
499 current, notify);
500 gpr_mu_unlock(&p->mu);
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700501}
502
Craig Tiller28bf8912015-12-07 16:07:04 -0800503static void rr_ping_one(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol,
504 grpc_closure *closure) {
505 round_robin_lb_policy *p = (round_robin_lb_policy *)pol;
506 ready_list *selected;
507 grpc_connected_subchannel *target;
508 gpr_mu_lock(&p->mu);
509 if ((selected = peek_next_connected_locked(p))) {
510 gpr_mu_unlock(&p->mu);
511 target = grpc_subchannel_get_connected_subchannel(selected->subchannel);
512 grpc_connected_subchannel_ping(exec_ctx, target, closure);
513 } else {
514 gpr_mu_unlock(&p->mu);
Craig Tiller6c396862016-01-28 13:53:40 -0800515 grpc_exec_ctx_enqueue(exec_ctx, closure, false, NULL);
Craig Tiller28bf8912015-12-07 16:07:04 -0800516 }
517}
518
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700519static const grpc_lb_policy_vtable round_robin_lb_policy_vtable = {
Craig Tillerc5ff7812016-03-28 12:45:55 -0700520 rr_destroy, rr_shutdown, rr_pick,
521 rr_cancel_pick, rr_cancel_picks, rr_ping_one,
522 rr_exit_idle, rr_check_connectivity, rr_notify_on_state_change};
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700523
Craig Tillera82950e2015-09-22 12:33:20 -0700524static void round_robin_factory_ref(grpc_lb_policy_factory *factory) {}
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700525
Craig Tillera82950e2015-09-22 12:33:20 -0700526static void round_robin_factory_unref(grpc_lb_policy_factory *factory) {}
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700527
David Garcia Quintas67c0d042016-03-25 01:37:53 -0700528static grpc_lb_policy *create_round_robin(grpc_exec_ctx *exec_ctx,
529 grpc_lb_policy_factory *factory,
Craig Tillera82950e2015-09-22 12:33:20 -0700530 grpc_lb_policy_args *args) {
David Garcia Quintas67c0d042016-03-25 01:37:53 -0700531 GPR_ASSERT(args->addresses != NULL);
David Garcia Quintas86fcfcc2016-03-31 23:22:28 -0700532 GPR_ASSERT(args->client_channel_factory != NULL);
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700533
David Garcia Quintas67c0d042016-03-25 01:37:53 -0700534 round_robin_lb_policy *p = gpr_malloc(sizeof(*p));
535 memset(p, 0, sizeof(*p));
536
537 p->subchannels =
538 gpr_malloc(sizeof(*p->subchannels) * args->addresses->naddrs);
539 memset(p->subchannels, 0, sizeof(*p->subchannels) * args->addresses->naddrs);
540
541 grpc_subchannel_args sc_args;
542 size_t subchannel_idx = 0;
543 for (size_t i = 0; i < args->addresses->naddrs; i++) {
544 memset(&sc_args, 0, sizeof(grpc_subchannel_args));
545 sc_args.addr = (struct sockaddr *)(args->addresses->addrs[i].addr);
546 sc_args.addr_len = (size_t)args->addresses->addrs[i].len;
547
David Garcia Quintas86fcfcc2016-03-31 23:22:28 -0700548 grpc_subchannel *subchannel = grpc_client_channel_factory_create_subchannel(
549 exec_ctx, args->client_channel_factory, &sc_args);
David Garcia Quintas67c0d042016-03-25 01:37:53 -0700550
551 if (subchannel != NULL) {
552 subchannel_data *sd = gpr_malloc(sizeof(*sd));
553 memset(sd, 0, sizeof(*sd));
554 p->subchannels[subchannel_idx] = sd;
555 sd->policy = p;
556 sd->index = subchannel_idx;
557 sd->subchannel = subchannel;
558 ++subchannel_idx;
559 grpc_closure_init(&sd->connectivity_changed_closure,
560 rr_connectivity_changed, sd);
561 }
Craig Tillera82950e2015-09-22 12:33:20 -0700562 }
David Garcia Quintas67c0d042016-03-25 01:37:53 -0700563 if (subchannel_idx == 0) {
564 gpr_free(p->subchannels);
565 gpr_free(p);
566 return NULL;
567 }
568 p->num_subchannels = subchannel_idx;
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700569
570 /* The (dummy node) root of the ready list */
571 p->ready_list.subchannel = NULL;
572 p->ready_list.prev = NULL;
573 p->ready_list.next = NULL;
574 p->ready_list_last_pick = &p->ready_list;
575
David Garcia Quintas67c0d042016-03-25 01:37:53 -0700576 grpc_lb_policy_init(&p->base, &round_robin_lb_policy_vtable);
577 grpc_connectivity_state_init(&p->state_tracker, GRPC_CHANNEL_IDLE,
578 "round_robin");
579 gpr_mu_init(&p->mu);
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700580 return &p->base;
581}
582
583static const grpc_lb_policy_factory_vtable round_robin_factory_vtable = {
Craig Tillera82950e2015-09-22 12:33:20 -0700584 round_robin_factory_ref, round_robin_factory_unref, create_round_robin,
585 "round_robin"};
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700586
587static grpc_lb_policy_factory round_robin_lb_policy_factory = {
Craig Tillera82950e2015-09-22 12:33:20 -0700588 &round_robin_factory_vtable};
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700589
Craig Tillerfb433852016-03-29 08:51:07 -0700590static grpc_lb_policy_factory *round_robin_lb_factory_create() {
David Garcia Quintas4fb049b2015-09-03 17:26:06 -0700591 return &round_robin_lb_policy_factory;
592}
Craig Tillerfb433852016-03-29 08:51:07 -0700593
594/* Plugin registration */
595
596void grpc_lb_policy_round_robin_init() {
Craig Tiller3113ef42016-03-29 09:03:14 -0700597 grpc_register_lb_policy(round_robin_lb_factory_create());
Craig Tillerfb433852016-03-29 08:51:07 -0700598 grpc_register_tracer("round_robin", &grpc_lb_round_robin_trace);
599}
600
601void grpc_lb_policy_round_robin_shutdown() {}