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; |
David Garcia Quintas | 2a50dfe | 2016-05-31 15:09:12 -0700 | [diff] [blame] | 51 | grpc_polling_entity *pollent; |
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, |
Craig Tiller | 48ed92e | 2016-06-02 11:07:12 -0700 | [diff] [blame] | 246 | GRPC_CHANNEL_SHUTDOWN, "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) { |
David Garcia Quintas | c4d5112 | 2016-06-06 14:56:02 -0700 | [diff] [blame] | 265 | grpc_polling_entity_del_from_pollset_set(exec_ctx, pp->pollent, |
David Garcia Quintas | 69ff63d | 2016-06-06 16:39:47 -0700 | [diff] [blame] | 266 | p->base.interested_parties); |
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) { |
David Garcia Quintas | c4d5112 | 2016-06-06 14:56:02 -0700 | [diff] [blame] | 291 | grpc_polling_entity_del_from_pollset_set(exec_ctx, pp->pollent, |
David Garcia Quintas | 69ff63d | 2016-06-06 16:39:47 -0700 | [diff] [blame] | 292 | p->base.interested_parties); |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 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 | |
Yuchen Zeng | fad0450 | 2016-04-29 16:43:46 -0700 | [diff] [blame] | 309 | 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 Tiller | 2a1bb7f | 2015-11-29 21:54:26 -0800 | [diff] [blame] | 313 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 314 | for (i = 0; i < p->num_subchannels; i++) { |
Craig Tiller | e2a6510 | 2015-11-30 17:51:49 -0800 | [diff] [blame] | 315 | subchannel_data *sd = p->subchannels[i]; |
| 316 | sd->connectivity_state = GRPC_CHANNEL_IDLE; |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 317 | grpc_subchannel_notify_on_state_change( |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 318 | exec_ctx, sd->subchannel, p->base.interested_parties, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 319 | &sd->connectivity_state, &sd->connectivity_changed_closure); |
Craig Tiller | 2a1bb7f | 2015-11-29 21:54:26 -0800 | [diff] [blame] | 320 | GRPC_LB_POLICY_WEAK_REF(&p->base, "round_robin_connectivity"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 321 | } |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 324 | 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] | 325 | 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 Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 333 | static int rr_pick(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, |
David Garcia Quintas | 2a50dfe | 2016-05-31 15:09:12 -0700 | [diff] [blame] | 334 | grpc_polling_entity *pollent, |
| 335 | grpc_metadata_batch *initial_metadata, |
Craig Tiller | 41dac84 | 2016-03-31 14:01:53 -0700 | [diff] [blame] | 336 | uint32_t initial_metadata_flags, |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 337 | grpc_connected_subchannel **target, |
| 338 | grpc_closure *on_complete) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 339 | round_robin_lb_policy *p = (round_robin_lb_policy *)pol; |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 340 | pending_pick *pp; |
| 341 | ready_list *selected; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 342 | gpr_mu_lock(&p->mu); |
| 343 | if ((selected = peek_next_connected_locked(p))) { |
| 344 | gpr_mu_unlock(&p->mu); |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 345 | *target = grpc_subchannel_get_connected_subchannel(selected->subchannel); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 346 | if (grpc_lb_round_robin_trace) { |
Craig Tiller | ab33b48 | 2015-11-21 08:11:04 -0800 | [diff] [blame] | 347 | gpr_log(GPR_DEBUG, |
| 348 | "[RR PICK] TARGET <-- CONNECTED SUBCHANNEL %p (NODE %p)", |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 349 | selected->subchannel, selected); |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 350 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 351 | /* only advance the last picked pointer if the selection was used */ |
| 352 | advance_last_picked_locked(p); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 353 | return 1; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 354 | } else { |
| 355 | if (!p->started_picking) { |
| 356 | start_picking(exec_ctx, p); |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 357 | } |
David Garcia Quintas | 69ff63d | 2016-06-06 16:39:47 -0700 | [diff] [blame] | 358 | grpc_polling_entity_add_to_pollset_set(exec_ctx, pollent, |
| 359 | p->base.interested_parties); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 360 | pp = gpr_malloc(sizeof(*pp)); |
| 361 | pp->next = p->pending_picks; |
David Garcia Quintas | 2a50dfe | 2016-05-31 15:09:12 -0700 | [diff] [blame] | 362 | pp->pollent = pollent; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 363 | pp->target = target; |
| 364 | pp->on_complete = on_complete; |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 365 | pp->initial_metadata_flags = initial_metadata_flags; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 366 | p->pending_picks = pp; |
| 367 | gpr_mu_unlock(&p->mu); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 368 | return 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 369 | } |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 372 | static void rr_connectivity_changed(grpc_exec_ctx *exec_ctx, void *arg, |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 373 | bool iomgr_success) { |
Craig Tiller | e2a6510 | 2015-11-30 17:51:49 -0800 | [diff] [blame] | 374 | subchannel_data *sd = arg; |
| 375 | round_robin_lb_policy *p = sd->policy; |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 376 | pending_pick *pp; |
| 377 | ready_list *selected; |
| 378 | |
| 379 | int unref = 0; |
| 380 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 381 | gpr_mu_lock(&p->mu); |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 382 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 383 | if (p->shutdown) { |
| 384 | unref = 1; |
| 385 | } else { |
Craig Tiller | e2a6510 | 2015-11-30 17:51:49 -0800 | [diff] [blame] | 386 | switch (sd->connectivity_state) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 387 | 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 Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 392 | sd->ready_list_node = add_connected_sc_locked(p, sd->subchannel); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 393 | /* 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 Tiller | ab33b48 | 2015-11-21 08:11:04 -0800 | [diff] [blame] | 404 | *pp->target = |
| 405 | grpc_subchannel_get_connected_subchannel(selected->subchannel); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 406 | 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 Quintas | c4d5112 | 2016-06-06 14:56:02 -0700 | [diff] [blame] | 411 | grpc_polling_entity_del_from_pollset_set(exec_ctx, pp->pollent, |
David Garcia Quintas | 69ff63d | 2016-06-06 16:39:47 -0700 | [diff] [blame] | 412 | p->base.interested_parties); |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 413 | grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, true, NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 414 | gpr_free(pp); |
| 415 | } |
| 416 | grpc_subchannel_notify_on_state_change( |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 417 | exec_ctx, sd->subchannel, p->base.interested_parties, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 418 | &sd->connectivity_state, &sd->connectivity_changed_closure); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 419 | break; |
| 420 | case GRPC_CHANNEL_CONNECTING: |
| 421 | case GRPC_CHANNEL_IDLE: |
| 422 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 423 | sd->connectivity_state, |
| 424 | "connecting_changed"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 425 | grpc_subchannel_notify_on_state_change( |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 426 | exec_ctx, sd->subchannel, p->base.interested_parties, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 427 | &sd->connectivity_state, &sd->connectivity_changed_closure); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 428 | break; |
| 429 | case GRPC_CHANNEL_TRANSIENT_FAILURE: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 430 | /* renew state notification */ |
| 431 | grpc_subchannel_notify_on_state_change( |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 432 | exec_ctx, sd->subchannel, p->base.interested_parties, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 433 | &sd->connectivity_state, &sd->connectivity_changed_closure); |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 434 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 435 | /* remove from ready list if still present */ |
Craig Tiller | e2a6510 | 2015-11-30 17:51:49 -0800 | [diff] [blame] | 436 | if (sd->ready_list_node != NULL) { |
| 437 | remove_disconnected_sc_locked(p, sd->ready_list_node); |
| 438 | sd->ready_list_node = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 439 | } |
| 440 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
| 441 | GRPC_CHANNEL_TRANSIENT_FAILURE, |
| 442 | "connecting_transient_failure"); |
| 443 | break; |
Craig Tiller | 48ed92e | 2016-06-02 11:07:12 -0700 | [diff] [blame] | 444 | case GRPC_CHANNEL_SHUTDOWN: |
Craig Tiller | e2a6510 | 2015-11-30 17:51:49 -0800 | [diff] [blame] | 445 | if (sd->ready_list_node != NULL) { |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 446 | remove_disconnected_sc_locked(p, sd->ready_list_node); |
Craig Tiller | e2a6510 | 2015-11-30 17:51:49 -0800 | [diff] [blame] | 447 | sd->ready_list_node = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 448 | } |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 449 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 450 | p->num_subchannels--; |
Craig Tiller | e2a6510 | 2015-11-30 17:51:49 -0800 | [diff] [blame] | 451 | GPR_SWAP(subchannel_data *, p->subchannels[sd->index], |
| 452 | p->subchannels[p->num_subchannels]); |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 453 | GRPC_SUBCHANNEL_UNREF(exec_ctx, sd->subchannel, "round_robin"); |
Craig Tiller | e2a6510 | 2015-11-30 17:51:49 -0800 | [diff] [blame] | 454 | p->subchannels[sd->index]->index = sd->index; |
| 455 | gpr_free(sd); |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 456 | |
Craig Tiller | e2a6510 | 2015-11-30 17:51:49 -0800 | [diff] [blame] | 457 | unref = 1; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 458 | if (p->num_subchannels == 0) { |
| 459 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
Craig Tiller | 48ed92e | 2016-06-02 11:07:12 -0700 | [diff] [blame] | 460 | GRPC_CHANNEL_SHUTDOWN, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 461 | "no_more_channels"); |
| 462 | while ((pp = p->pending_picks)) { |
| 463 | p->pending_picks = pp->next; |
| 464 | *pp->target = NULL; |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 465 | grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, true, NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 466 | gpr_free(pp); |
| 467 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 468 | } 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 Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 475 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 476 | gpr_mu_unlock(&p->mu); |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 477 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 478 | if (unref) { |
Craig Tiller | 2a1bb7f | 2015-11-29 21:54:26 -0800 | [diff] [blame] | 479 | GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &p->base, "round_robin_connectivity"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 480 | } |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 483 | static 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 Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 486 | grpc_connectivity_state st; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 487 | gpr_mu_lock(&p->mu); |
| 488 | st = grpc_connectivity_state_check(&p->state_tracker); |
| 489 | gpr_mu_unlock(&p->mu); |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 490 | return st; |
| 491 | } |
| 492 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 493 | static 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 Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 502 | } |
| 503 | |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 504 | static 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 Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 516 | grpc_exec_ctx_enqueue(exec_ctx, closure, false, NULL); |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 520 | static const grpc_lb_policy_vtable round_robin_lb_policy_vtable = { |
Craig Tiller | c5ff781 | 2016-03-28 12:45:55 -0700 | [diff] [blame] | 521 | 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 Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 524 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 525 | static void round_robin_factory_ref(grpc_lb_policy_factory *factory) {} |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 526 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 527 | static void round_robin_factory_unref(grpc_lb_policy_factory *factory) {} |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 528 | |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 529 | static grpc_lb_policy *create_round_robin(grpc_exec_ctx *exec_ctx, |
| 530 | grpc_lb_policy_factory *factory, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 531 | grpc_lb_policy_args *args) { |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 532 | GPR_ASSERT(args->addresses != NULL); |
David Garcia Quintas | 86fcfcc | 2016-03-31 23:22:28 -0700 | [diff] [blame] | 533 | GPR_ASSERT(args->client_channel_factory != NULL); |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 534 | |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 535 | 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 Quintas | 86fcfcc | 2016-03-31 23:22:28 -0700 | [diff] [blame] | 549 | grpc_subchannel *subchannel = grpc_client_channel_factory_create_subchannel( |
| 550 | exec_ctx, args->client_channel_factory, &sc_args); |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 551 | |
| 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 Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 563 | } |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 564 | 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 Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 570 | |
| 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 Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 577 | 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 Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 581 | return &p->base; |
| 582 | } |
| 583 | |
| 584 | static const grpc_lb_policy_factory_vtable round_robin_factory_vtable = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 585 | round_robin_factory_ref, round_robin_factory_unref, create_round_robin, |
| 586 | "round_robin"}; |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 587 | |
| 588 | static grpc_lb_policy_factory round_robin_lb_policy_factory = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 589 | &round_robin_factory_vtable}; |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 590 | |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 591 | static grpc_lb_policy_factory *round_robin_lb_factory_create() { |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 592 | return &round_robin_lb_policy_factory; |
| 593 | } |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 594 | |
| 595 | /* Plugin registration */ |
| 596 | |
| 597 | void grpc_lb_policy_round_robin_init() { |
Craig Tiller | 3113ef4 | 2016-03-29 09:03:14 -0700 | [diff] [blame] | 598 | grpc_register_lb_policy(round_robin_lb_factory_create()); |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 599 | grpc_register_tracer("round_robin", &grpc_lb_round_robin_trace); |
| 600 | } |
| 601 | |
| 602 | void grpc_lb_policy_round_robin_shutdown() {} |