David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 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 Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 34 | #include <string.h> |
| 35 | |
| 36 | #include <grpc/support/alloc.h> |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 37 | |
Craig Tiller | d4c9833 | 2016-03-31 13:45:47 -0700 | [diff] [blame] | 38 | #include "src/core/ext/client_config/lb_policy_registry.h" |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 39 | #include "src/core/lib/debug/trace.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 40 | #include "src/core/lib/transport/connectivity_state.h" |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 41 | |
Craig Tiller | e2a6510 | 2015-11-30 17:51:49 -0800 | [diff] [blame] | 42 | typedef struct round_robin_lb_policy round_robin_lb_policy; |
| 43 | |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 44 | int 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 Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 49 | typedef struct pending_pick { |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 50 | struct pending_pick *next; |
| 51 | grpc_pollset *pollset; |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 52 | uint32_t initial_metadata_flags; |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 53 | grpc_connected_subchannel **target; |
Craig Tiller | 10ee274 | 2015-09-22 09:25:57 -0700 | [diff] [blame] | 54 | grpc_closure *on_complete; |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 55 | } pending_pick; |
| 56 | |
| 57 | /** List of subchannels in a connectivity READY state */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 58 | typedef struct ready_list { |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 59 | grpc_subchannel *subchannel; |
| 60 | struct ready_list *next; |
| 61 | struct ready_list *prev; |
| 62 | } ready_list; |
| 63 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 64 | typedef struct { |
Craig Tiller | e2a6510 | 2015-11-30 17:51:49 -0800 | [diff] [blame] | 65 | /** 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 Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 78 | |
Craig Tiller | e2a6510 | 2015-11-30 17:51:49 -0800 | [diff] [blame] | 79 | struct round_robin_lb_policy { |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 80 | /** base policy: must be first */ |
| 81 | grpc_lb_policy base; |
| 82 | |
| 83 | /** all our subchannels */ |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 84 | size_t num_subchannels; |
Craig Tiller | e2a6510 | 2015-11-30 17:51:49 -0800 | [diff] [blame] | 85 | subchannel_data **subchannels; |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 86 | |
| 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 Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 93 | /** 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 Tiller | e2a6510 | 2015-11-30 17:51:49 -0800 | [diff] [blame] | 103 | }; |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 104 | |
| 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 Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 110 | static ready_list *peek_next_connected_locked(const round_robin_lb_policy *p) { |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 111 | ready_list *selected; |
| 112 | selected = p->ready_list_last_pick->next; |
| 113 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 114 | 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 Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 122 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 123 | } |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 124 | return NULL; |
| 125 | } |
| 126 | |
| 127 | /** Advance the \a ready_list picking head. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 128 | static 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 Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 133 | p->ready_list_last_pick = p->ready_list_last_pick->next; |
| 134 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 135 | } else { /* should be an empty list */ |
| 136 | GPR_ASSERT(p->ready_list_last_pick == &p->ready_list); |
| 137 | } |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 138 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 139 | 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 Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /** Prepends (relative to the root at p->ready_list) the connected subchannel \a |
| 146 | * csc to the list of ready subchannels. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 147 | static ready_list *add_connected_sc_locked(round_robin_lb_policy *p, |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 148 | grpc_subchannel *sc) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 149 | ready_list *new_elem = gpr_malloc(sizeof(ready_list)); |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 150 | new_elem->subchannel = sc; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 151 | 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 Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 164 | gpr_log(GPR_DEBUG, "[READYLIST] ADDING NODE %p (SC %p)", new_elem, sc); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 165 | } |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 166 | return new_elem; |
| 167 | } |
| 168 | |
| 169 | /** Removes \a node from the list of connected subchannels */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 170 | static 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 Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 180 | |
| 181 | /* removing last item */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 182 | 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 Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 191 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 192 | if (grpc_lb_round_robin_trace) { |
| 193 | gpr_log(GPR_DEBUG, "[READYLIST] REMOVED NODE %p (SC %p)", node, |
| 194 | node->subchannel); |
| 195 | } |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 196 | |
| 197 | node->next = NULL; |
| 198 | node->prev = NULL; |
| 199 | node->subchannel = NULL; |
| 200 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 201 | gpr_free(node); |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 204 | static void rr_destroy(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 205 | round_robin_lb_policy *p = (round_robin_lb_policy *)pol; |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 206 | size_t i; |
| 207 | ready_list *elem; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 208 | for (i = 0; i < p->num_subchannels; i++) { |
Craig Tiller | e2a6510 | 2015-11-30 17:51:49 -0800 | [diff] [blame] | 209 | subchannel_data *sd = p->subchannels[i]; |
| 210 | GRPC_SUBCHANNEL_UNREF(exec_ctx, sd->subchannel, "round_robin"); |
| 211 | gpr_free(sd); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 212 | } |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 213 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 214 | grpc_connectivity_state_destroy(exec_ctx, &p->state_tracker); |
| 215 | gpr_free(p->subchannels); |
| 216 | gpr_mu_destroy(&p->mu); |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 217 | |
| 218 | elem = p->ready_list.next; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 219 | 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 Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 228 | gpr_free(p); |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 231 | static void rr_shutdown(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 232 | round_robin_lb_policy *p = (round_robin_lb_policy *)pol; |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 233 | pending_pick *pp; |
Craig Tiller | 2a1bb7f | 2015-11-29 21:54:26 -0800 | [diff] [blame] | 234 | size_t i; |
| 235 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 236 | gpr_mu_lock(&p->mu); |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 237 | |
| 238 | p->shutdown = 1; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 239 | while ((pp = p->pending_picks)) { |
| 240 | p->pending_picks = pp->next; |
| 241 | *pp->target = NULL; |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 242 | grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, false, NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 243 | gpr_free(pp); |
| 244 | } |
| 245 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
| 246 | GRPC_CHANNEL_FATAL_FAILURE, "shutdown"); |
Craig Tiller | 2a1bb7f | 2015-11-29 21:54:26 -0800 | [diff] [blame] | 247 | for (i = 0; i < p->num_subchannels; i++) { |
Craig Tiller | e2a6510 | 2015-11-30 17:51:49 -0800 | [diff] [blame] | 248 | subchannel_data *sd = p->subchannels[i]; |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 249 | grpc_subchannel_notify_on_state_change(exec_ctx, sd->subchannel, NULL, NULL, |
Craig Tiller | e2a6510 | 2015-11-30 17:51:49 -0800 | [diff] [blame] | 250 | &sd->connectivity_changed_closure); |
Craig Tiller | 2a1bb7f | 2015-11-29 21:54:26 -0800 | [diff] [blame] | 251 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 252 | gpr_mu_unlock(&p->mu); |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 255 | static void rr_cancel_pick(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 256 | grpc_connected_subchannel **target) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 257 | round_robin_lb_policy *p = (round_robin_lb_policy *)pol; |
| 258 | pending_pick *pp; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 259 | 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) { |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 265 | grpc_pollset_set_del_pollset(exec_ctx, p->base.interested_parties, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 266 | pp->pollset); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 267 | *target = NULL; |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 268 | grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, false, NULL); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 269 | 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 Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 279 | static 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) { |
| 291 | grpc_pollset_set_del_pollset(exec_ctx, p->base.interested_parties, |
| 292 | pp->pollset); |
| 293 | *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 Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 305 | static void start_picking(grpc_exec_ctx *exec_ctx, round_robin_lb_policy *p) { |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 306 | size_t i; |
| 307 | p->started_picking = 1; |
| 308 | |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 309 | gpr_log(GPR_DEBUG, "LB_POLICY: p=%p num_subchannels=%d", p, |
| 310 | p->num_subchannels); |
Craig Tiller | 2a1bb7f | 2015-11-29 21:54:26 -0800 | [diff] [blame] | 311 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 312 | for (i = 0; i < p->num_subchannels; i++) { |
Craig Tiller | e2a6510 | 2015-11-30 17:51:49 -0800 | [diff] [blame] | 313 | subchannel_data *sd = p->subchannels[i]; |
| 314 | sd->connectivity_state = GRPC_CHANNEL_IDLE; |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 315 | grpc_subchannel_notify_on_state_change( |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 316 | exec_ctx, sd->subchannel, p->base.interested_parties, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 317 | &sd->connectivity_state, &sd->connectivity_changed_closure); |
Craig Tiller | 2a1bb7f | 2015-11-29 21:54:26 -0800 | [diff] [blame] | 318 | GRPC_LB_POLICY_WEAK_REF(&p->base, "round_robin_connectivity"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 319 | } |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 322 | static void rr_exit_idle(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 323 | round_robin_lb_policy *p = (round_robin_lb_policy *)pol; |
| 324 | gpr_mu_lock(&p->mu); |
| 325 | if (!p->started_picking) { |
| 326 | start_picking(exec_ctx, p); |
| 327 | } |
| 328 | gpr_mu_unlock(&p->mu); |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 331 | static int rr_pick(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, |
| 332 | grpc_pollset *pollset, grpc_metadata_batch *initial_metadata, |
Craig Tiller | 41dac84 | 2016-03-31 14:01:53 -0700 | [diff] [blame] | 333 | uint32_t initial_metadata_flags, |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 334 | grpc_connected_subchannel **target, |
| 335 | grpc_closure *on_complete) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 336 | round_robin_lb_policy *p = (round_robin_lb_policy *)pol; |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 337 | pending_pick *pp; |
| 338 | ready_list *selected; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 339 | gpr_mu_lock(&p->mu); |
| 340 | if ((selected = peek_next_connected_locked(p))) { |
| 341 | gpr_mu_unlock(&p->mu); |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 342 | *target = grpc_subchannel_get_connected_subchannel(selected->subchannel); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 343 | if (grpc_lb_round_robin_trace) { |
Craig Tiller | ab33b48 | 2015-11-21 08:11:04 -0800 | [diff] [blame] | 344 | gpr_log(GPR_DEBUG, |
| 345 | "[RR PICK] TARGET <-- CONNECTED SUBCHANNEL %p (NODE %p)", |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 346 | selected->subchannel, selected); |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 347 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 348 | /* only advance the last picked pointer if the selection was used */ |
| 349 | advance_last_picked_locked(p); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 350 | return 1; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 351 | } else { |
| 352 | if (!p->started_picking) { |
| 353 | start_picking(exec_ctx, p); |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 354 | } |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 355 | grpc_pollset_set_add_pollset(exec_ctx, p->base.interested_parties, pollset); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 356 | pp = gpr_malloc(sizeof(*pp)); |
| 357 | pp->next = p->pending_picks; |
| 358 | pp->pollset = pollset; |
| 359 | pp->target = target; |
| 360 | pp->on_complete = on_complete; |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 361 | pp->initial_metadata_flags = initial_metadata_flags; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 362 | p->pending_picks = pp; |
| 363 | gpr_mu_unlock(&p->mu); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 364 | return 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 365 | } |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 368 | static void rr_connectivity_changed(grpc_exec_ctx *exec_ctx, void *arg, |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 369 | bool iomgr_success) { |
Craig Tiller | e2a6510 | 2015-11-30 17:51:49 -0800 | [diff] [blame] | 370 | subchannel_data *sd = arg; |
| 371 | round_robin_lb_policy *p = sd->policy; |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 372 | pending_pick *pp; |
| 373 | ready_list *selected; |
| 374 | |
| 375 | int unref = 0; |
| 376 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 377 | gpr_mu_lock(&p->mu); |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 378 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 379 | if (p->shutdown) { |
| 380 | unref = 1; |
| 381 | } else { |
Craig Tiller | e2a6510 | 2015-11-30 17:51:49 -0800 | [diff] [blame] | 382 | switch (sd->connectivity_state) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 383 | case GRPC_CHANNEL_READY: |
| 384 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
| 385 | GRPC_CHANNEL_READY, "connecting_ready"); |
| 386 | /* add the newly connected subchannel to the list of connected ones. |
| 387 | * Note that it goes to the "end of the line". */ |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 388 | sd->ready_list_node = add_connected_sc_locked(p, sd->subchannel); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 389 | /* at this point we know there's at least one suitable subchannel. Go |
| 390 | * ahead and pick one and notify the pending suitors in |
| 391 | * p->pending_picks. This preemtively replicates rr_pick()'s actions. */ |
| 392 | selected = peek_next_connected_locked(p); |
| 393 | if (p->pending_picks != NULL) { |
| 394 | /* if the selected subchannel is going to be used for the pending |
| 395 | * picks, update the last picked pointer */ |
| 396 | advance_last_picked_locked(p); |
| 397 | } |
| 398 | while ((pp = p->pending_picks)) { |
| 399 | p->pending_picks = pp->next; |
Craig Tiller | ab33b48 | 2015-11-21 08:11:04 -0800 | [diff] [blame] | 400 | *pp->target = |
| 401 | grpc_subchannel_get_connected_subchannel(selected->subchannel); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 402 | if (grpc_lb_round_robin_trace) { |
| 403 | gpr_log(GPR_DEBUG, |
| 404 | "[RR CONN CHANGED] TARGET <-- SUBCHANNEL %p (NODE %p)", |
| 405 | selected->subchannel, selected); |
| 406 | } |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 407 | grpc_pollset_set_del_pollset(exec_ctx, p->base.interested_parties, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 408 | pp->pollset); |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 409 | grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, true, NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 410 | gpr_free(pp); |
| 411 | } |
| 412 | grpc_subchannel_notify_on_state_change( |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 413 | exec_ctx, sd->subchannel, p->base.interested_parties, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 414 | &sd->connectivity_state, &sd->connectivity_changed_closure); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 415 | break; |
| 416 | case GRPC_CHANNEL_CONNECTING: |
| 417 | case GRPC_CHANNEL_IDLE: |
| 418 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 419 | sd->connectivity_state, |
| 420 | "connecting_changed"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 421 | grpc_subchannel_notify_on_state_change( |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 422 | exec_ctx, sd->subchannel, p->base.interested_parties, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 423 | &sd->connectivity_state, &sd->connectivity_changed_closure); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 424 | break; |
| 425 | case GRPC_CHANNEL_TRANSIENT_FAILURE: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 426 | /* renew state notification */ |
| 427 | grpc_subchannel_notify_on_state_change( |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 428 | exec_ctx, sd->subchannel, p->base.interested_parties, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 429 | &sd->connectivity_state, &sd->connectivity_changed_closure); |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 430 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 431 | /* remove from ready list if still present */ |
Craig Tiller | e2a6510 | 2015-11-30 17:51:49 -0800 | [diff] [blame] | 432 | if (sd->ready_list_node != NULL) { |
| 433 | remove_disconnected_sc_locked(p, sd->ready_list_node); |
| 434 | sd->ready_list_node = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 435 | } |
| 436 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
| 437 | GRPC_CHANNEL_TRANSIENT_FAILURE, |
| 438 | "connecting_transient_failure"); |
| 439 | break; |
| 440 | case GRPC_CHANNEL_FATAL_FAILURE: |
Craig Tiller | e2a6510 | 2015-11-30 17:51:49 -0800 | [diff] [blame] | 441 | if (sd->ready_list_node != NULL) { |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 442 | remove_disconnected_sc_locked(p, sd->ready_list_node); |
Craig Tiller | e2a6510 | 2015-11-30 17:51:49 -0800 | [diff] [blame] | 443 | sd->ready_list_node = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 444 | } |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 445 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 446 | p->num_subchannels--; |
Craig Tiller | e2a6510 | 2015-11-30 17:51:49 -0800 | [diff] [blame] | 447 | GPR_SWAP(subchannel_data *, p->subchannels[sd->index], |
| 448 | p->subchannels[p->num_subchannels]); |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 449 | GRPC_SUBCHANNEL_UNREF(exec_ctx, sd->subchannel, "round_robin"); |
Craig Tiller | e2a6510 | 2015-11-30 17:51:49 -0800 | [diff] [blame] | 450 | p->subchannels[sd->index]->index = sd->index; |
| 451 | gpr_free(sd); |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 452 | |
Craig Tiller | e2a6510 | 2015-11-30 17:51:49 -0800 | [diff] [blame] | 453 | unref = 1; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 454 | if (p->num_subchannels == 0) { |
| 455 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
| 456 | GRPC_CHANNEL_FATAL_FAILURE, |
| 457 | "no_more_channels"); |
| 458 | while ((pp = p->pending_picks)) { |
| 459 | p->pending_picks = pp->next; |
| 460 | *pp->target = NULL; |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 461 | grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, true, NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 462 | gpr_free(pp); |
| 463 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 464 | } else { |
| 465 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
| 466 | GRPC_CHANNEL_TRANSIENT_FAILURE, |
| 467 | "subchannel_failed"); |
| 468 | } |
| 469 | } /* switch */ |
| 470 | } /* !unref */ |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 471 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 472 | gpr_mu_unlock(&p->mu); |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 473 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 474 | if (unref) { |
Craig Tiller | 2a1bb7f | 2015-11-29 21:54:26 -0800 | [diff] [blame] | 475 | GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &p->base, "round_robin_connectivity"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 476 | } |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 477 | } |
| 478 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 479 | static grpc_connectivity_state rr_check_connectivity(grpc_exec_ctx *exec_ctx, |
| 480 | grpc_lb_policy *pol) { |
| 481 | round_robin_lb_policy *p = (round_robin_lb_policy *)pol; |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 482 | grpc_connectivity_state st; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 483 | gpr_mu_lock(&p->mu); |
| 484 | st = grpc_connectivity_state_check(&p->state_tracker); |
| 485 | gpr_mu_unlock(&p->mu); |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 486 | return st; |
| 487 | } |
| 488 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 489 | static void rr_notify_on_state_change(grpc_exec_ctx *exec_ctx, |
| 490 | grpc_lb_policy *pol, |
| 491 | grpc_connectivity_state *current, |
| 492 | grpc_closure *notify) { |
| 493 | round_robin_lb_policy *p = (round_robin_lb_policy *)pol; |
| 494 | gpr_mu_lock(&p->mu); |
| 495 | grpc_connectivity_state_notify_on_state_change(exec_ctx, &p->state_tracker, |
| 496 | current, notify); |
| 497 | gpr_mu_unlock(&p->mu); |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 498 | } |
| 499 | |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 500 | static void rr_ping_one(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, |
| 501 | grpc_closure *closure) { |
| 502 | round_robin_lb_policy *p = (round_robin_lb_policy *)pol; |
| 503 | ready_list *selected; |
| 504 | grpc_connected_subchannel *target; |
| 505 | gpr_mu_lock(&p->mu); |
| 506 | if ((selected = peek_next_connected_locked(p))) { |
| 507 | gpr_mu_unlock(&p->mu); |
| 508 | target = grpc_subchannel_get_connected_subchannel(selected->subchannel); |
| 509 | grpc_connected_subchannel_ping(exec_ctx, target, closure); |
| 510 | } else { |
| 511 | gpr_mu_unlock(&p->mu); |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 512 | grpc_exec_ctx_enqueue(exec_ctx, closure, false, NULL); |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 516 | static const grpc_lb_policy_vtable round_robin_lb_policy_vtable = { |
Craig Tiller | c5ff781 | 2016-03-28 12:45:55 -0700 | [diff] [blame] | 517 | rr_destroy, rr_shutdown, rr_pick, |
| 518 | rr_cancel_pick, rr_cancel_picks, rr_ping_one, |
| 519 | rr_exit_idle, rr_check_connectivity, rr_notify_on_state_change}; |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 520 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 521 | static void round_robin_factory_ref(grpc_lb_policy_factory *factory) {} |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 522 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 523 | static void round_robin_factory_unref(grpc_lb_policy_factory *factory) {} |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 524 | |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 525 | static grpc_lb_policy *create_round_robin(grpc_exec_ctx *exec_ctx, |
| 526 | grpc_lb_policy_factory *factory, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 527 | grpc_lb_policy_args *args) { |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 528 | GPR_ASSERT(args->addresses != NULL); |
David Garcia Quintas | 86fcfcc | 2016-03-31 23:22:28 -0700 | [diff] [blame] | 529 | GPR_ASSERT(args->client_channel_factory != NULL); |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 530 | |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 531 | round_robin_lb_policy *p = gpr_malloc(sizeof(*p)); |
| 532 | memset(p, 0, sizeof(*p)); |
| 533 | |
| 534 | p->subchannels = |
| 535 | gpr_malloc(sizeof(*p->subchannels) * args->addresses->naddrs); |
| 536 | memset(p->subchannels, 0, sizeof(*p->subchannels) * args->addresses->naddrs); |
| 537 | |
| 538 | grpc_subchannel_args sc_args; |
| 539 | size_t subchannel_idx = 0; |
| 540 | for (size_t i = 0; i < args->addresses->naddrs; i++) { |
| 541 | memset(&sc_args, 0, sizeof(grpc_subchannel_args)); |
| 542 | sc_args.addr = (struct sockaddr *)(args->addresses->addrs[i].addr); |
| 543 | sc_args.addr_len = (size_t)args->addresses->addrs[i].len; |
| 544 | |
David Garcia Quintas | 86fcfcc | 2016-03-31 23:22:28 -0700 | [diff] [blame] | 545 | grpc_subchannel *subchannel = grpc_client_channel_factory_create_subchannel( |
| 546 | exec_ctx, args->client_channel_factory, &sc_args); |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 547 | |
| 548 | if (subchannel != NULL) { |
| 549 | subchannel_data *sd = gpr_malloc(sizeof(*sd)); |
| 550 | memset(sd, 0, sizeof(*sd)); |
| 551 | p->subchannels[subchannel_idx] = sd; |
| 552 | sd->policy = p; |
| 553 | sd->index = subchannel_idx; |
| 554 | sd->subchannel = subchannel; |
| 555 | ++subchannel_idx; |
| 556 | grpc_closure_init(&sd->connectivity_changed_closure, |
| 557 | rr_connectivity_changed, sd); |
| 558 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 559 | } |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 560 | if (subchannel_idx == 0) { |
| 561 | gpr_free(p->subchannels); |
| 562 | gpr_free(p); |
| 563 | return NULL; |
| 564 | } |
| 565 | p->num_subchannels = subchannel_idx; |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 566 | |
| 567 | /* The (dummy node) root of the ready list */ |
| 568 | p->ready_list.subchannel = NULL; |
| 569 | p->ready_list.prev = NULL; |
| 570 | p->ready_list.next = NULL; |
| 571 | p->ready_list_last_pick = &p->ready_list; |
| 572 | |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 573 | grpc_lb_policy_init(&p->base, &round_robin_lb_policy_vtable); |
| 574 | grpc_connectivity_state_init(&p->state_tracker, GRPC_CHANNEL_IDLE, |
| 575 | "round_robin"); |
| 576 | gpr_mu_init(&p->mu); |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 577 | return &p->base; |
| 578 | } |
| 579 | |
| 580 | static const grpc_lb_policy_factory_vtable round_robin_factory_vtable = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 581 | round_robin_factory_ref, round_robin_factory_unref, create_round_robin, |
| 582 | "round_robin"}; |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 583 | |
| 584 | static grpc_lb_policy_factory round_robin_lb_policy_factory = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 585 | &round_robin_factory_vtable}; |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 586 | |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 587 | static grpc_lb_policy_factory *round_robin_lb_factory_create() { |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 588 | return &round_robin_lb_policy_factory; |
| 589 | } |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 590 | |
| 591 | /* Plugin registration */ |
| 592 | |
| 593 | void grpc_lb_policy_round_robin_init() { |
Craig Tiller | 3113ef4 | 2016-03-29 09:03:14 -0700 | [diff] [blame] | 594 | grpc_register_lb_policy(round_robin_lb_factory_create()); |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 595 | grpc_register_tracer("round_robin", &grpc_lb_round_robin_trace); |
| 596 | } |
| 597 | |
| 598 | void grpc_lb_policy_round_robin_shutdown() {} |