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 | 0200870 | 2016-06-02 21:40:45 -0700 | [diff] [blame] | 34 | /** Round Robin Policy. |
| 35 | * |
| 36 | * This policy keeps: |
David Garcia Quintas | 23bb526 | 2016-06-03 13:12:30 -0700 | [diff] [blame] | 37 | * - A circular list of ready (connected) subchannels, the *readylist*. An empty |
| 38 | * readylist consists solely of its root (dummy) node. |
David Garcia Quintas | 0200870 | 2016-06-02 21:40:45 -0700 | [diff] [blame] | 39 | * - A pointer to the last element picked from the readylist, the *lastpick*. |
| 40 | * Initially set to point to the readylist's root. |
| 41 | * |
| 42 | * Behavior: |
| 43 | * - When a subchannel connects, it's *prepended* to the readylist's root node. |
| 44 | * Ie, if readylist = A <-> B <-> ROOT <-> C |
| 45 | * ^ ^ |
| 46 | * |____________________| |
| 47 | * and subchannel D becomes connected, the addition of D to the readylist |
| 48 | * results in readylist = A <-> B <-> D <-> ROOT <-> C |
| 49 | * ^ ^ |
| 50 | * |__________________________| |
| 51 | * - When a subchannel disconnects, it's removed from the readylist. If the |
| 52 | * subchannel being removed was the most recently picked, the *lastpick* |
| 53 | * pointer moves to the removed node's previous element. Note that if the |
| 54 | * readylist only had one element, this is still legal, as the lastpick would |
| 55 | * point to the dummy root node, for an empty readylist. |
| 56 | * - Upon picking, *lastpick* is updated to point to the returned (connected) |
David Garcia Quintas | 23bb526 | 2016-06-03 13:12:30 -0700 | [diff] [blame] | 57 | * subchannel. Note that it's possible that the selected subchannel becomes |
David Garcia Quintas | 0200870 | 2016-06-02 21:40:45 -0700 | [diff] [blame] | 58 | * disconnected in the interim between the selection and the actual usage of |
| 59 | * the subchannel by the caller. |
| 60 | */ |
| 61 | |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 62 | #include <string.h> |
| 63 | |
| 64 | #include <grpc/support/alloc.h> |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 65 | |
Mark D. Roth | 2137cd8 | 2016-09-14 09:04:00 -0700 | [diff] [blame] | 66 | #include "src/core/ext/client_channel/lb_policy_registry.h" |
Mark D. Roth | 0748f39 | 2017-01-13 09:22:44 -0800 | [diff] [blame] | 67 | #include "src/core/ext/client_channel/subchannel.h" |
Mark D. Roth | 5bd7be0 | 2016-10-21 14:19:50 -0700 | [diff] [blame] | 68 | #include "src/core/lib/channel/channel_args.h" |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 69 | #include "src/core/lib/debug/trace.h" |
Mark D. Roth | 0748f39 | 2017-01-13 09:22:44 -0800 | [diff] [blame] | 70 | #include "src/core/lib/iomgr/sockaddr_utils.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 71 | #include "src/core/lib/transport/connectivity_state.h" |
David Garcia Quintas | 5b0e946 | 2016-08-15 19:38:39 -0700 | [diff] [blame] | 72 | #include "src/core/lib/transport/static_metadata.h" |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 73 | |
Craig Tiller | e2a6510 | 2015-11-30 17:51:49 -0800 | [diff] [blame] | 74 | typedef struct round_robin_lb_policy round_robin_lb_policy; |
| 75 | |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 76 | int grpc_lb_round_robin_trace = 0; |
| 77 | |
| 78 | /** List of entities waiting for a pick. |
| 79 | * |
| 80 | * 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] | 81 | typedef struct pending_pick { |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 82 | struct pending_pick *next; |
David Garcia Quintas | 5b0e946 | 2016-08-15 19:38:39 -0700 | [diff] [blame] | 83 | |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame] | 84 | /* output argument where to store the pick()ed user_data. It'll be NULL if no |
| 85 | * such data is present or there's an error (the definite test for errors is |
| 86 | * \a target being NULL). */ |
| 87 | void **user_data; |
David Garcia Quintas | 5b0e946 | 2016-08-15 19:38:39 -0700 | [diff] [blame] | 88 | |
| 89 | /* bitmask passed to pick() and used for selective cancelling. See |
| 90 | * grpc_lb_policy_cancel_picks() */ |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 91 | uint32_t initial_metadata_flags; |
David Garcia Quintas | 5b0e946 | 2016-08-15 19:38:39 -0700 | [diff] [blame] | 92 | |
| 93 | /* output argument where to store the pick()ed connected subchannel, or NULL |
| 94 | * upon error. */ |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 95 | grpc_connected_subchannel **target; |
David Garcia Quintas | 5b0e946 | 2016-08-15 19:38:39 -0700 | [diff] [blame] | 96 | |
| 97 | /* to be invoked once the pick() has completed (regardless of success) */ |
Craig Tiller | 10ee274 | 2015-09-22 09:25:57 -0700 | [diff] [blame] | 98 | grpc_closure *on_complete; |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 99 | } pending_pick; |
| 100 | |
| 101 | /** List of subchannels in a connectivity READY state */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 102 | typedef struct ready_list { |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 103 | grpc_subchannel *subchannel; |
David Garcia Quintas | 5b0e946 | 2016-08-15 19:38:39 -0700 | [diff] [blame] | 104 | /* references namesake entry in subchannel_data */ |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame] | 105 | void *user_data; |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 106 | struct ready_list *next; |
| 107 | struct ready_list *prev; |
| 108 | } ready_list; |
| 109 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 110 | typedef struct { |
Craig Tiller | e2a6510 | 2015-11-30 17:51:49 -0800 | [diff] [blame] | 111 | /** index within policy->subchannels */ |
| 112 | size_t index; |
| 113 | /** backpointer to owning policy */ |
| 114 | round_robin_lb_policy *policy; |
| 115 | /** subchannel itself */ |
| 116 | grpc_subchannel *subchannel; |
| 117 | /** notification that connectivity has changed on subchannel */ |
| 118 | grpc_closure connectivity_changed_closure; |
| 119 | /** this subchannels current position in subchannel->ready_list */ |
| 120 | ready_list *ready_list_node; |
David Garcia Quintas | a9cf87d | 2016-11-07 17:43:15 -0800 | [diff] [blame] | 121 | /** last observed connectivity. Not updated by |
| 122 | * \a grpc_subchannel_notify_on_state_change. Used to determine the previous |
| 123 | * state while processing the new state in \a rr_connectivity_changed */ |
| 124 | grpc_connectivity_state prev_connectivity_state; |
| 125 | /** current connectivity state. Updated by \a |
| 126 | * grpc_subchannel_notify_on_state_change */ |
| 127 | grpc_connectivity_state curr_connectivity_state; |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame] | 128 | /** the subchannel's target user data */ |
| 129 | void *user_data; |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 130 | /** vtable to operate over \a user_data */ |
David Garcia Quintas | 7ec2913 | 2016-11-01 04:09:05 +0100 | [diff] [blame] | 131 | const grpc_lb_user_data_vtable *user_data_vtable; |
Craig Tiller | e2a6510 | 2015-11-30 17:51:49 -0800 | [diff] [blame] | 132 | } subchannel_data; |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 133 | |
Craig Tiller | e2a6510 | 2015-11-30 17:51:49 -0800 | [diff] [blame] | 134 | struct round_robin_lb_policy { |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 135 | /** base policy: must be first */ |
| 136 | grpc_lb_policy base; |
David Garcia Quintas | a9cf87d | 2016-11-07 17:43:15 -0800 | [diff] [blame] | 137 | gpr_mu mu; |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 138 | |
David Garcia Quintas | 5b0e946 | 2016-08-15 19:38:39 -0700 | [diff] [blame] | 139 | /** total number of addresses received at creation time */ |
| 140 | size_t num_addresses; |
David Garcia Quintas | 5b0e946 | 2016-08-15 19:38:39 -0700 | [diff] [blame] | 141 | |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 142 | /** all our subchannels */ |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 143 | size_t num_subchannels; |
Craig Tiller | e2a6510 | 2015-11-30 17:51:49 -0800 | [diff] [blame] | 144 | subchannel_data **subchannels; |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 145 | |
David Garcia Quintas | a9cf87d | 2016-11-07 17:43:15 -0800 | [diff] [blame] | 146 | /** how many subchannels are in TRANSIENT_FAILURE */ |
| 147 | size_t num_transient_failures; |
| 148 | /** how many subchannels are IDLE */ |
| 149 | size_t num_idle; |
| 150 | |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 151 | /** have we started picking? */ |
| 152 | int started_picking; |
| 153 | /** are we shutting down? */ |
| 154 | int shutdown; |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 155 | /** List of picks that are waiting on connectivity */ |
| 156 | pending_pick *pending_picks; |
| 157 | |
| 158 | /** our connectivity state tracker */ |
| 159 | grpc_connectivity_state_tracker state_tracker; |
| 160 | |
| 161 | /** (Dummy) root of the doubly linked list containing READY subchannels */ |
| 162 | ready_list ready_list; |
| 163 | /** Last pick from the ready list. */ |
| 164 | ready_list *ready_list_last_pick; |
Craig Tiller | e2a6510 | 2015-11-30 17:51:49 -0800 | [diff] [blame] | 165 | }; |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 166 | |
| 167 | /** Returns the next subchannel from the connected list or NULL if the list is |
| 168 | * empty. |
| 169 | * |
| 170 | * Note that this function does *not* advance p->ready_list_last_pick. Use \a |
| 171 | * advance_last_picked_locked() for that. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 172 | 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] | 173 | ready_list *selected; |
| 174 | selected = p->ready_list_last_pick->next; |
| 175 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 176 | while (selected != NULL) { |
| 177 | if (selected == &p->ready_list) { |
| 178 | GPR_ASSERT(selected->subchannel == NULL); |
| 179 | /* skip dummy root */ |
| 180 | selected = selected->next; |
| 181 | } else { |
| 182 | GPR_ASSERT(selected->subchannel != NULL); |
| 183 | return selected; |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 184 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 185 | } |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 186 | return NULL; |
| 187 | } |
| 188 | |
| 189 | /** Advance the \a ready_list picking head. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 190 | static void advance_last_picked_locked(round_robin_lb_policy *p) { |
| 191 | if (p->ready_list_last_pick->next != NULL) { /* non-empty list */ |
| 192 | p->ready_list_last_pick = p->ready_list_last_pick->next; |
| 193 | if (p->ready_list_last_pick == &p->ready_list) { |
| 194 | /* skip dummy root */ |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 195 | p->ready_list_last_pick = p->ready_list_last_pick->next; |
| 196 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 197 | } else { /* should be an empty list */ |
| 198 | GPR_ASSERT(p->ready_list_last_pick == &p->ready_list); |
| 199 | } |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 200 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 201 | if (grpc_lb_round_robin_trace) { |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 202 | gpr_log(GPR_DEBUG, |
| 203 | "[READYLIST, RR: %p] ADVANCED LAST PICK. NOW AT NODE %p (SC %p, " |
| 204 | "CSC %p)", |
| 205 | (void *)p, (void *)p->ready_list_last_pick, |
| 206 | (void *)p->ready_list_last_pick->subchannel, |
| 207 | (void *)grpc_subchannel_get_connected_subchannel( |
| 208 | p->ready_list_last_pick->subchannel)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 209 | } |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /** Prepends (relative to the root at p->ready_list) the connected subchannel \a |
| 213 | * csc to the list of ready subchannels. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 214 | static ready_list *add_connected_sc_locked(round_robin_lb_policy *p, |
David Garcia Quintas | 5b0e946 | 2016-08-15 19:38:39 -0700 | [diff] [blame] | 215 | subchannel_data *sd) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 216 | ready_list *new_elem = gpr_malloc(sizeof(ready_list)); |
David Garcia Quintas | 5b0e946 | 2016-08-15 19:38:39 -0700 | [diff] [blame] | 217 | memset(new_elem, 0, sizeof(ready_list)); |
| 218 | new_elem->subchannel = sd->subchannel; |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame] | 219 | new_elem->user_data = sd->user_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 220 | if (p->ready_list.prev == NULL) { |
| 221 | /* first element */ |
| 222 | new_elem->next = &p->ready_list; |
| 223 | new_elem->prev = &p->ready_list; |
| 224 | p->ready_list.next = new_elem; |
| 225 | p->ready_list.prev = new_elem; |
| 226 | } else { |
| 227 | new_elem->next = &p->ready_list; |
| 228 | new_elem->prev = p->ready_list.prev; |
| 229 | p->ready_list.prev->next = new_elem; |
| 230 | p->ready_list.prev = new_elem; |
| 231 | } |
| 232 | if (grpc_lb_round_robin_trace) { |
David Garcia Quintas | 5b0e946 | 2016-08-15 19:38:39 -0700 | [diff] [blame] | 233 | gpr_log(GPR_DEBUG, "[READYLIST] ADDING NODE %p (Conn. SC %p)", |
| 234 | (void *)new_elem, (void *)sd->subchannel); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 235 | } |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 236 | return new_elem; |
| 237 | } |
| 238 | |
| 239 | /** Removes \a node from the list of connected subchannels */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 240 | static void remove_disconnected_sc_locked(round_robin_lb_policy *p, |
| 241 | ready_list *node) { |
| 242 | if (node == NULL) { |
| 243 | return; |
| 244 | } |
| 245 | if (node == p->ready_list_last_pick) { |
David Garcia Quintas | 0200870 | 2016-06-02 21:40:45 -0700 | [diff] [blame] | 246 | p->ready_list_last_pick = p->ready_list_last_pick->prev; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 247 | } |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 248 | |
| 249 | /* removing last item */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 250 | if (node->next == &p->ready_list && node->prev == &p->ready_list) { |
| 251 | GPR_ASSERT(p->ready_list.next == node); |
| 252 | GPR_ASSERT(p->ready_list.prev == node); |
| 253 | p->ready_list.next = NULL; |
| 254 | p->ready_list.prev = NULL; |
| 255 | } else { |
| 256 | node->prev->next = node->next; |
| 257 | node->next->prev = node->prev; |
| 258 | } |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 259 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 260 | if (grpc_lb_round_robin_trace) { |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame] | 261 | gpr_log(GPR_DEBUG, "[READYLIST] REMOVED NODE %p (SC %p)", (void *)node, |
David Garcia Quintas | 5b0e946 | 2016-08-15 19:38:39 -0700 | [diff] [blame] | 262 | (void *)node->subchannel); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 263 | } |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 264 | |
| 265 | node->next = NULL; |
| 266 | node->prev = NULL; |
| 267 | node->subchannel = NULL; |
| 268 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 269 | gpr_free(node); |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 270 | } |
| 271 | |
David Garcia Quintas | a9cf87d | 2016-11-07 17:43:15 -0800 | [diff] [blame] | 272 | static bool is_ready_list_empty(round_robin_lb_policy *p) { |
| 273 | return p->ready_list.prev == NULL; |
| 274 | } |
| 275 | |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 276 | 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] | 277 | round_robin_lb_policy *p = (round_robin_lb_policy *)pol; |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 278 | ready_list *elem; |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 279 | |
| 280 | if (grpc_lb_round_robin_trace) { |
David Garcia Quintas | 2ff8180 | 2016-11-01 21:58:16 -0700 | [diff] [blame] | 281 | gpr_log(GPR_DEBUG, "Destroying Round Robin policy at %p", (void *)pol); |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 282 | } |
| 283 | |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame] | 284 | for (size_t i = 0; i < p->num_subchannels; i++) { |
Craig Tiller | e2a6510 | 2015-11-30 17:51:49 -0800 | [diff] [blame] | 285 | subchannel_data *sd = p->subchannels[i]; |
David Garcia Quintas | a9cf87d | 2016-11-07 17:43:15 -0800 | [diff] [blame] | 286 | GRPC_SUBCHANNEL_UNREF(exec_ctx, sd->subchannel, "rr_destroy"); |
David Garcia Quintas | 246c564 | 2016-11-01 11:16:52 -0700 | [diff] [blame] | 287 | if (sd->user_data != NULL) { |
David Garcia Quintas | e224a76 | 2016-11-01 13:00:58 -0700 | [diff] [blame] | 288 | GPR_ASSERT(sd->user_data_vtable != NULL); |
Craig Tiller | 87a7e1f | 2016-11-09 09:42:19 -0800 | [diff] [blame] | 289 | sd->user_data_vtable->destroy(exec_ctx, sd->user_data); |
David Garcia Quintas | 7ec2913 | 2016-11-01 04:09:05 +0100 | [diff] [blame] | 290 | } |
Craig Tiller | e2a6510 | 2015-11-30 17:51:49 -0800 | [diff] [blame] | 291 | gpr_free(sd); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 292 | } |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 293 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 294 | grpc_connectivity_state_destroy(exec_ctx, &p->state_tracker); |
| 295 | gpr_free(p->subchannels); |
| 296 | gpr_mu_destroy(&p->mu); |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 297 | |
| 298 | elem = p->ready_list.next; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 299 | while (elem != NULL && elem != &p->ready_list) { |
| 300 | ready_list *tmp; |
| 301 | tmp = elem->next; |
| 302 | elem->next = NULL; |
| 303 | elem->prev = NULL; |
| 304 | elem->subchannel = NULL; |
| 305 | gpr_free(elem); |
| 306 | elem = tmp; |
| 307 | } |
David Garcia Quintas | 5b0e946 | 2016-08-15 19:38:39 -0700 | [diff] [blame] | 308 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 309 | gpr_free(p); |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 312 | 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] | 313 | round_robin_lb_policy *p = (round_robin_lb_policy *)pol; |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 314 | pending_pick *pp; |
Craig Tiller | 2a1bb7f | 2015-11-29 21:54:26 -0800 | [diff] [blame] | 315 | size_t i; |
| 316 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 317 | gpr_mu_lock(&p->mu); |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 318 | if (grpc_lb_round_robin_trace) { |
David Garcia Quintas | 7ec2913 | 2016-11-01 04:09:05 +0100 | [diff] [blame] | 319 | gpr_log(GPR_DEBUG, "Shutting down Round Robin policy at %p", (void *)pol); |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 320 | } |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 321 | |
| 322 | p->shutdown = 1; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 323 | while ((pp = p->pending_picks)) { |
| 324 | p->pending_picks = pp->next; |
| 325 | *pp->target = NULL; |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 326 | grpc_closure_sched(exec_ctx, pp->on_complete, |
| 327 | GRPC_ERROR_CREATE("Channel Shutdown")); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 328 | gpr_free(pp); |
| 329 | } |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 330 | grpc_connectivity_state_set( |
Craig Tiller | d925c93 | 2016-06-06 08:38:50 -0700 | [diff] [blame] | 331 | exec_ctx, &p->state_tracker, GRPC_CHANNEL_SHUTDOWN, |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 332 | GRPC_ERROR_CREATE("Channel Shutdown"), "rr_shutdown"); |
Craig Tiller | 2a1bb7f | 2015-11-29 21:54:26 -0800 | [diff] [blame] | 333 | for (i = 0; i < p->num_subchannels; i++) { |
Craig Tiller | e2a6510 | 2015-11-30 17:51:49 -0800 | [diff] [blame] | 334 | subchannel_data *sd = p->subchannels[i]; |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 335 | grpc_subchannel_notify_on_state_change(exec_ctx, sd->subchannel, NULL, NULL, |
Craig Tiller | e2a6510 | 2015-11-30 17:51:49 -0800 | [diff] [blame] | 336 | &sd->connectivity_changed_closure); |
Craig Tiller | 2a1bb7f | 2015-11-29 21:54:26 -0800 | [diff] [blame] | 337 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 338 | gpr_mu_unlock(&p->mu); |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 341 | static void rr_cancel_pick(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, |
Mark D. Roth | 5f84400 | 2016-09-08 08:20:53 -0700 | [diff] [blame] | 342 | grpc_connected_subchannel **target, |
| 343 | grpc_error *error) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 344 | round_robin_lb_policy *p = (round_robin_lb_policy *)pol; |
| 345 | pending_pick *pp; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 346 | gpr_mu_lock(&p->mu); |
| 347 | pp = p->pending_picks; |
| 348 | p->pending_picks = NULL; |
| 349 | while (pp != NULL) { |
| 350 | pending_pick *next = pp->next; |
| 351 | if (pp->target == target) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 352 | *target = NULL; |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 353 | grpc_closure_sched( |
Mark D. Roth | 932b10c | 2016-09-09 08:44:30 -0700 | [diff] [blame] | 354 | exec_ctx, pp->on_complete, |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 355 | GRPC_ERROR_CREATE_REFERENCING("Pick cancelled", &error, 1)); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 356 | gpr_free(pp); |
| 357 | } else { |
| 358 | pp->next = p->pending_picks; |
| 359 | p->pending_picks = pp; |
| 360 | } |
| 361 | pp = next; |
| 362 | } |
| 363 | gpr_mu_unlock(&p->mu); |
Mark D. Roth | 5f84400 | 2016-09-08 08:20:53 -0700 | [diff] [blame] | 364 | GRPC_ERROR_UNREF(error); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 365 | } |
| 366 | |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 367 | static void rr_cancel_picks(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, |
| 368 | uint32_t initial_metadata_flags_mask, |
Mark D. Roth | e65ff11 | 2016-09-09 13:48:38 -0700 | [diff] [blame] | 369 | uint32_t initial_metadata_flags_eq, |
| 370 | grpc_error *error) { |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 371 | round_robin_lb_policy *p = (round_robin_lb_policy *)pol; |
| 372 | pending_pick *pp; |
| 373 | gpr_mu_lock(&p->mu); |
| 374 | pp = p->pending_picks; |
| 375 | p->pending_picks = NULL; |
| 376 | while (pp != NULL) { |
| 377 | pending_pick *next = pp->next; |
| 378 | if ((pp->initial_metadata_flags & initial_metadata_flags_mask) == |
| 379 | initial_metadata_flags_eq) { |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 380 | *pp->target = NULL; |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 381 | grpc_closure_sched( |
Mark D. Roth | 58f52b7 | 2016-09-09 13:55:18 -0700 | [diff] [blame] | 382 | exec_ctx, pp->on_complete, |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 383 | GRPC_ERROR_CREATE_REFERENCING("Pick cancelled", &error, 1)); |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 384 | gpr_free(pp); |
| 385 | } else { |
| 386 | pp->next = p->pending_picks; |
| 387 | p->pending_picks = pp; |
| 388 | } |
| 389 | pp = next; |
| 390 | } |
| 391 | gpr_mu_unlock(&p->mu); |
Mark D. Roth | e65ff11 | 2016-09-09 13:48:38 -0700 | [diff] [blame] | 392 | GRPC_ERROR_UNREF(error); |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 393 | } |
| 394 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 395 | 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] | 396 | size_t i; |
| 397 | p->started_picking = 1; |
| 398 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 399 | for (i = 0; i < p->num_subchannels; i++) { |
Craig Tiller | e2a6510 | 2015-11-30 17:51:49 -0800 | [diff] [blame] | 400 | subchannel_data *sd = p->subchannels[i]; |
David Garcia Quintas | a9cf87d | 2016-11-07 17:43:15 -0800 | [diff] [blame] | 401 | /* use some sentinel value outside of the range of grpc_connectivity_state |
| 402 | * to signal an undefined previous state. We won't be referring to this |
| 403 | * value again and it'll be overwritten after the first call to |
| 404 | * rr_connectivity_changed */ |
David Garcia Quintas | ea6689d | 2016-11-08 09:46:41 -0800 | [diff] [blame] | 405 | sd->prev_connectivity_state = GRPC_CHANNEL_INIT; |
David Garcia Quintas | a9cf87d | 2016-11-07 17:43:15 -0800 | [diff] [blame] | 406 | sd->curr_connectivity_state = GRPC_CHANNEL_IDLE; |
| 407 | GRPC_LB_POLICY_WEAK_REF(&p->base, "rr_connectivity"); |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 408 | grpc_subchannel_notify_on_state_change( |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 409 | exec_ctx, sd->subchannel, p->base.interested_parties, |
David Garcia Quintas | a9cf87d | 2016-11-07 17:43:15 -0800 | [diff] [blame] | 410 | &sd->curr_connectivity_state, &sd->connectivity_changed_closure); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 411 | } |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 414 | 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] | 415 | round_robin_lb_policy *p = (round_robin_lb_policy *)pol; |
| 416 | gpr_mu_lock(&p->mu); |
| 417 | if (!p->started_picking) { |
| 418 | start_picking(exec_ctx, p); |
| 419 | } |
| 420 | gpr_mu_unlock(&p->mu); |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 423 | static int rr_pick(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, |
David Garcia Quintas | 8aace51 | 2016-08-15 14:55:12 -0700 | [diff] [blame] | 424 | const grpc_lb_policy_pick_args *pick_args, |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame] | 425 | grpc_connected_subchannel **target, void **user_data, |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 426 | grpc_closure *on_complete) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 427 | round_robin_lb_policy *p = (round_robin_lb_policy *)pol; |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 428 | pending_pick *pp; |
| 429 | ready_list *selected; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 430 | gpr_mu_lock(&p->mu); |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 431 | |
| 432 | if (grpc_lb_round_robin_trace) { |
David Garcia Quintas | 7ec2913 | 2016-11-01 04:09:05 +0100 | [diff] [blame] | 433 | gpr_log(GPR_INFO, "Round Robin %p trying to pick", (void *)pol); |
David Garcia Quintas | 98da61b | 2016-10-29 08:46:31 +0200 | [diff] [blame] | 434 | } |
| 435 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 436 | if ((selected = peek_next_connected_locked(p))) { |
David Garcia Quintas | 5b0e946 | 2016-08-15 19:38:39 -0700 | [diff] [blame] | 437 | /* readily available, report right away */ |
Craig Tiller | 693d394 | 2016-10-27 16:51:25 -0700 | [diff] [blame] | 438 | *target = GRPC_CONNECTED_SUBCHANNEL_REF( |
| 439 | grpc_subchannel_get_connected_subchannel(selected->subchannel), |
David Garcia Quintas | e9448df | 2016-11-09 15:55:17 -0800 | [diff] [blame] | 440 | "rr_picked"); |
David Garcia Quintas | 155b820 | 2016-09-15 16:14:15 -0700 | [diff] [blame] | 441 | |
| 442 | if (user_data != NULL) { |
| 443 | *user_data = selected->user_data; |
| 444 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 445 | if (grpc_lb_round_robin_trace) { |
Craig Tiller | ab33b48 | 2015-11-21 08:11:04 -0800 | [diff] [blame] | 446 | gpr_log(GPR_DEBUG, |
David Garcia Quintas | 5b0e946 | 2016-08-15 19:38:39 -0700 | [diff] [blame] | 447 | "[RR PICK] TARGET <-- CONNECTED SUBCHANNEL %p (NODE %p)", |
| 448 | (void *)*target, (void *)selected); |
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 | /* only advance the last picked pointer if the selection was used */ |
| 451 | advance_last_picked_locked(p); |
David Garcia Quintas | 90712d5 | 2016-10-13 19:33:04 -0700 | [diff] [blame] | 452 | gpr_mu_unlock(&p->mu); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 453 | return 1; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 454 | } else { |
David Garcia Quintas | 5b0e946 | 2016-08-15 19:38:39 -0700 | [diff] [blame] | 455 | /* no pick currently available. Save for later in list of pending picks */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 456 | if (!p->started_picking) { |
| 457 | start_picking(exec_ctx, p); |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 458 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 459 | pp = gpr_malloc(sizeof(*pp)); |
| 460 | pp->next = p->pending_picks; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 461 | pp->target = target; |
| 462 | pp->on_complete = on_complete; |
David Garcia Quintas | 8aace51 | 2016-08-15 14:55:12 -0700 | [diff] [blame] | 463 | pp->initial_metadata_flags = pick_args->initial_metadata_flags; |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame] | 464 | pp->user_data = user_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 465 | p->pending_picks = pp; |
| 466 | gpr_mu_unlock(&p->mu); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 467 | return 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 468 | } |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 469 | } |
| 470 | |
David Garcia Quintas | a9cf87d | 2016-11-07 17:43:15 -0800 | [diff] [blame] | 471 | static void update_state_counters(subchannel_data *sd) { |
| 472 | round_robin_lb_policy *p = sd->policy; |
| 473 | |
| 474 | /* update p->num_transient_failures (resp. p->num_idle): if the previous |
| 475 | * state was TRANSIENT_FAILURE (resp. IDLE), decrement |
| 476 | * p->num_transient_failures (resp. p->num_idle). */ |
| 477 | if (sd->prev_connectivity_state == GRPC_CHANNEL_TRANSIENT_FAILURE) { |
| 478 | GPR_ASSERT(p->num_transient_failures > 0); |
| 479 | --p->num_transient_failures; |
| 480 | } else if (sd->prev_connectivity_state == GRPC_CHANNEL_IDLE) { |
| 481 | GPR_ASSERT(p->num_idle > 0); |
| 482 | --p->num_idle; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | /* sd is the subchannel_data associted with the updated subchannel. |
| 487 | * shutdown_error will only be used upon policy transition to TRANSIENT_FAILURE |
| 488 | * or SHUTDOWN */ |
| 489 | static grpc_connectivity_state update_lb_connectivity_status( |
| 490 | grpc_exec_ctx *exec_ctx, subchannel_data *sd, grpc_error *error) { |
| 491 | /* In priority order. The first rule to match terminates the search (ie, if we |
| 492 | * are on rule n, all previous rules were unfulfilled). |
| 493 | * |
| 494 | * 1) RULE: ANY subchannel is READY => policy is READY. |
| 495 | * CHECK: At least one subchannel is ready iff p->ready_list is NOT empty. |
| 496 | * |
| 497 | * 2) RULE: ANY subchannel is CONNECTING => policy is CONNECTING. |
| 498 | * CHECK: sd->curr_connectivity_state == CONNECTING. |
| 499 | * |
| 500 | * 3) RULE: ALL subchannels are SHUTDOWN => policy is SHUTDOWN. |
| 501 | * CHECK: p->num_subchannels = 0. |
| 502 | * |
| 503 | * 4) RULE: ALL subchannels are TRANSIENT_FAILURE => policy is |
| 504 | * TRANSIENT_FAILURE. |
| 505 | * CHECK: p->num_transient_failures == p->num_subchannels. |
| 506 | * |
| 507 | * 5) RULE: ALL subchannels are IDLE => policy is IDLE. |
| 508 | * CHECK: p->num_idle == p->num_subchannels. |
| 509 | */ |
| 510 | round_robin_lb_policy *p = sd->policy; |
| 511 | if (!is_ready_list_empty(p)) { /* 1) READY */ |
| 512 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, GRPC_CHANNEL_READY, |
| 513 | GRPC_ERROR_NONE, "rr_ready"); |
| 514 | return GRPC_CHANNEL_READY; |
| 515 | } else if (sd->curr_connectivity_state == |
| 516 | GRPC_CHANNEL_CONNECTING) { /* 2) CONNECTING */ |
| 517 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
| 518 | GRPC_CHANNEL_CONNECTING, GRPC_ERROR_NONE, |
| 519 | "rr_connecting"); |
| 520 | return GRPC_CHANNEL_CONNECTING; |
| 521 | } else if (p->num_subchannels == 0) { /* 3) SHUTDOWN */ |
| 522 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
| 523 | GRPC_CHANNEL_SHUTDOWN, GRPC_ERROR_REF(error), |
| 524 | "rr_shutdown"); |
| 525 | return GRPC_CHANNEL_SHUTDOWN; |
| 526 | } else if (p->num_transient_failures == |
| 527 | p->num_subchannels) { /* 4) TRANSIENT_FAILURE */ |
| 528 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
| 529 | GRPC_CHANNEL_TRANSIENT_FAILURE, |
| 530 | GRPC_ERROR_REF(error), "rr_transient_failure"); |
| 531 | return GRPC_CHANNEL_TRANSIENT_FAILURE; |
| 532 | } else if (p->num_idle == p->num_subchannels) { /* 5) IDLE */ |
| 533 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, GRPC_CHANNEL_IDLE, |
| 534 | GRPC_ERROR_NONE, "rr_idle"); |
| 535 | return GRPC_CHANNEL_IDLE; |
| 536 | } |
| 537 | /* no change */ |
| 538 | return sd->curr_connectivity_state; |
| 539 | } |
| 540 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 541 | static void rr_connectivity_changed(grpc_exec_ctx *exec_ctx, void *arg, |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 542 | grpc_error *error) { |
Craig Tiller | e2a6510 | 2015-11-30 17:51:49 -0800 | [diff] [blame] | 543 | subchannel_data *sd = arg; |
| 544 | round_robin_lb_policy *p = sd->policy; |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 545 | pending_pick *pp; |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 546 | |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 547 | GRPC_ERROR_REF(error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 548 | gpr_mu_lock(&p->mu); |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 549 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 550 | if (p->shutdown) { |
David Garcia Quintas | a9cf87d | 2016-11-07 17:43:15 -0800 | [diff] [blame] | 551 | gpr_mu_unlock(&p->mu); |
| 552 | GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &p->base, "rr_connectivity"); |
| 553 | GRPC_ERROR_UNREF(error); |
| 554 | return; |
| 555 | } |
| 556 | switch (sd->curr_connectivity_state) { |
David Garcia Quintas | ea6689d | 2016-11-08 09:46:41 -0800 | [diff] [blame] | 557 | case GRPC_CHANNEL_INIT: |
Jan Tattermusch | b0fb2d2 | 2016-11-16 14:04:05 +0100 | [diff] [blame] | 558 | GPR_UNREACHABLE_CODE(return ); |
David Garcia Quintas | a9cf87d | 2016-11-07 17:43:15 -0800 | [diff] [blame] | 559 | case GRPC_CHANNEL_READY: |
| 560 | /* add the newly connected subchannel to the list of connected ones. |
| 561 | * Note that it goes to the "end of the line". */ |
| 562 | sd->ready_list_node = add_connected_sc_locked(p, sd); |
| 563 | /* at this point we know there's at least one suitable subchannel. Go |
| 564 | * ahead and pick one and notify the pending suitors in |
| 565 | * p->pending_picks. This preemtively replicates rr_pick()'s actions. */ |
| 566 | ready_list *selected = peek_next_connected_locked(p); |
| 567 | GPR_ASSERT(selected != NULL); |
| 568 | if (p->pending_picks != NULL) { |
| 569 | /* if the selected subchannel is going to be used for the pending |
| 570 | * picks, update the last picked pointer */ |
| 571 | advance_last_picked_locked(p); |
| 572 | } |
| 573 | while ((pp = p->pending_picks)) { |
| 574 | p->pending_picks = pp->next; |
| 575 | *pp->target = GRPC_CONNECTED_SUBCHANNEL_REF( |
| 576 | grpc_subchannel_get_connected_subchannel(selected->subchannel), |
| 577 | "rr_picked"); |
| 578 | if (pp->user_data != NULL) { |
| 579 | *pp->user_data = selected->user_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 580 | } |
David Garcia Quintas | a9cf87d | 2016-11-07 17:43:15 -0800 | [diff] [blame] | 581 | if (grpc_lb_round_robin_trace) { |
| 582 | gpr_log(GPR_DEBUG, |
| 583 | "[RR CONN CHANGED] TARGET <-- SUBCHANNEL %p (NODE %p)", |
| 584 | (void *)selected->subchannel, (void *)selected); |
| 585 | } |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 586 | grpc_closure_sched(exec_ctx, pp->on_complete, GRPC_ERROR_NONE); |
David Garcia Quintas | a9cf87d | 2016-11-07 17:43:15 -0800 | [diff] [blame] | 587 | gpr_free(pp); |
| 588 | } |
David Garcia Quintas | e9448df | 2016-11-09 15:55:17 -0800 | [diff] [blame] | 589 | update_lb_connectivity_status(exec_ctx, sd, error); |
| 590 | sd->prev_connectivity_state = sd->curr_connectivity_state; |
David Garcia Quintas | a9cf87d | 2016-11-07 17:43:15 -0800 | [diff] [blame] | 591 | /* renew notification: reuses the "rr_connectivity" weak ref */ |
| 592 | grpc_subchannel_notify_on_state_change( |
| 593 | exec_ctx, sd->subchannel, p->base.interested_parties, |
| 594 | &sd->curr_connectivity_state, &sd->connectivity_changed_closure); |
David Garcia Quintas | a9cf87d | 2016-11-07 17:43:15 -0800 | [diff] [blame] | 595 | break; |
| 596 | case GRPC_CHANNEL_IDLE: |
| 597 | ++p->num_idle; |
| 598 | /* fallthrough */ |
| 599 | case GRPC_CHANNEL_CONNECTING: |
| 600 | update_state_counters(sd); |
David Garcia Quintas | e9448df | 2016-11-09 15:55:17 -0800 | [diff] [blame] | 601 | update_lb_connectivity_status(exec_ctx, sd, error); |
| 602 | sd->prev_connectivity_state = sd->curr_connectivity_state; |
David Garcia Quintas | a9cf87d | 2016-11-07 17:43:15 -0800 | [diff] [blame] | 603 | /* renew notification: reuses the "rr_connectivity" weak ref */ |
| 604 | grpc_subchannel_notify_on_state_change( |
| 605 | exec_ctx, sd->subchannel, p->base.interested_parties, |
| 606 | &sd->curr_connectivity_state, &sd->connectivity_changed_closure); |
David Garcia Quintas | a9cf87d | 2016-11-07 17:43:15 -0800 | [diff] [blame] | 607 | break; |
| 608 | case GRPC_CHANNEL_TRANSIENT_FAILURE: |
| 609 | ++p->num_transient_failures; |
| 610 | /* remove from ready list if still present */ |
| 611 | if (sd->ready_list_node != NULL) { |
| 612 | remove_disconnected_sc_locked(p, sd->ready_list_node); |
| 613 | sd->ready_list_node = NULL; |
| 614 | } |
David Garcia Quintas | e9448df | 2016-11-09 15:55:17 -0800 | [diff] [blame] | 615 | update_lb_connectivity_status(exec_ctx, sd, error); |
| 616 | sd->prev_connectivity_state = sd->curr_connectivity_state; |
David Garcia Quintas | a9cf87d | 2016-11-07 17:43:15 -0800 | [diff] [blame] | 617 | /* renew notification: reuses the "rr_connectivity" weak ref */ |
| 618 | grpc_subchannel_notify_on_state_change( |
| 619 | exec_ctx, sd->subchannel, p->base.interested_parties, |
| 620 | &sd->curr_connectivity_state, &sd->connectivity_changed_closure); |
David Garcia Quintas | a9cf87d | 2016-11-07 17:43:15 -0800 | [diff] [blame] | 621 | break; |
| 622 | case GRPC_CHANNEL_SHUTDOWN: |
| 623 | update_state_counters(sd); |
| 624 | if (sd->ready_list_node != NULL) { |
| 625 | remove_disconnected_sc_locked(p, sd->ready_list_node); |
| 626 | sd->ready_list_node = NULL; |
| 627 | } |
| 628 | --p->num_subchannels; |
| 629 | GPR_SWAP(subchannel_data *, p->subchannels[sd->index], |
| 630 | p->subchannels[p->num_subchannels]); |
| 631 | GRPC_SUBCHANNEL_UNREF(exec_ctx, sd->subchannel, "rr_subchannel_shutdown"); |
| 632 | p->subchannels[sd->index]->index = sd->index; |
| 633 | if (update_lb_connectivity_status(exec_ctx, sd, error) == |
| 634 | GRPC_CHANNEL_SHUTDOWN) { |
| 635 | /* the policy is shutting down. Flush all the pending picks... */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 636 | while ((pp = p->pending_picks)) { |
| 637 | p->pending_picks = pp->next; |
David Garcia Quintas | a9cf87d | 2016-11-07 17:43:15 -0800 | [diff] [blame] | 638 | *pp->target = NULL; |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 639 | grpc_closure_sched(exec_ctx, pp->on_complete, GRPC_ERROR_NONE); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 640 | gpr_free(pp); |
| 641 | } |
David Garcia Quintas | a9cf87d | 2016-11-07 17:43:15 -0800 | [diff] [blame] | 642 | } |
| 643 | gpr_free(sd); |
| 644 | /* unref the "rr_connectivity" weak ref from start_picking */ |
| 645 | GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &p->base, "rr_connectivity"); |
| 646 | break; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 647 | } |
David Garcia Quintas | a9cf87d | 2016-11-07 17:43:15 -0800 | [diff] [blame] | 648 | gpr_mu_unlock(&p->mu); |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 649 | GRPC_ERROR_UNREF(error); |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 650 | } |
| 651 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 652 | static grpc_connectivity_state rr_check_connectivity(grpc_exec_ctx *exec_ctx, |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 653 | grpc_lb_policy *pol, |
| 654 | grpc_error **error) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 655 | round_robin_lb_policy *p = (round_robin_lb_policy *)pol; |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 656 | grpc_connectivity_state st; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 657 | gpr_mu_lock(&p->mu); |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 658 | st = grpc_connectivity_state_get(&p->state_tracker, error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 659 | gpr_mu_unlock(&p->mu); |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 660 | return st; |
| 661 | } |
| 662 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 663 | static void rr_notify_on_state_change(grpc_exec_ctx *exec_ctx, |
| 664 | grpc_lb_policy *pol, |
| 665 | grpc_connectivity_state *current, |
| 666 | grpc_closure *notify) { |
| 667 | round_robin_lb_policy *p = (round_robin_lb_policy *)pol; |
| 668 | gpr_mu_lock(&p->mu); |
| 669 | grpc_connectivity_state_notify_on_state_change(exec_ctx, &p->state_tracker, |
| 670 | current, notify); |
| 671 | gpr_mu_unlock(&p->mu); |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 672 | } |
| 673 | |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 674 | static void rr_ping_one(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, |
| 675 | grpc_closure *closure) { |
| 676 | round_robin_lb_policy *p = (round_robin_lb_policy *)pol; |
| 677 | ready_list *selected; |
| 678 | grpc_connected_subchannel *target; |
| 679 | gpr_mu_lock(&p->mu); |
| 680 | if ((selected = peek_next_connected_locked(p))) { |
| 681 | gpr_mu_unlock(&p->mu); |
Craig Tiller | 693d394 | 2016-10-27 16:51:25 -0700 | [diff] [blame] | 682 | target = GRPC_CONNECTED_SUBCHANNEL_REF( |
| 683 | grpc_subchannel_get_connected_subchannel(selected->subchannel), |
David Garcia Quintas | a9cf87d | 2016-11-07 17:43:15 -0800 | [diff] [blame] | 684 | "rr_picked"); |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 685 | grpc_connected_subchannel_ping(exec_ctx, target, closure); |
David Garcia Quintas | a9cf87d | 2016-11-07 17:43:15 -0800 | [diff] [blame] | 686 | GRPC_CONNECTED_SUBCHANNEL_UNREF(exec_ctx, target, "rr_picked"); |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 687 | } else { |
| 688 | gpr_mu_unlock(&p->mu); |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 689 | grpc_closure_sched(exec_ctx, closure, |
| 690 | GRPC_ERROR_CREATE("Round Robin not connected")); |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 691 | } |
| 692 | } |
| 693 | |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 694 | static const grpc_lb_policy_vtable round_robin_lb_policy_vtable = { |
Craig Tiller | c5ff781 | 2016-03-28 12:45:55 -0700 | [diff] [blame] | 695 | rr_destroy, rr_shutdown, rr_pick, |
| 696 | rr_cancel_pick, rr_cancel_picks, rr_ping_one, |
| 697 | rr_exit_idle, rr_check_connectivity, rr_notify_on_state_change}; |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 698 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 699 | static void round_robin_factory_ref(grpc_lb_policy_factory *factory) {} |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 700 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 701 | static void round_robin_factory_unref(grpc_lb_policy_factory *factory) {} |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 702 | |
David Garcia Quintas | 0200870 | 2016-06-02 21:40:45 -0700 | [diff] [blame] | 703 | static grpc_lb_policy *round_robin_create(grpc_exec_ctx *exec_ctx, |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 704 | grpc_lb_policy_factory *factory, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 705 | grpc_lb_policy_args *args) { |
David Garcia Quintas | 86fcfcc | 2016-03-31 23:22:28 -0700 | [diff] [blame] | 706 | GPR_ASSERT(args->client_channel_factory != NULL); |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 707 | |
Mark D. Roth | e011b1e | 2016-09-07 08:28:00 -0700 | [diff] [blame] | 708 | /* Find the number of backend addresses. We ignore balancer |
| 709 | * addresses, since we don't know how to handle them. */ |
Mark D. Roth | 201db7d | 2016-12-12 09:36:02 -0800 | [diff] [blame] | 710 | const grpc_arg *arg = |
| 711 | grpc_channel_args_find(args->args, GRPC_ARG_LB_ADDRESSES); |
Mark D. Roth | 5bd7be0 | 2016-10-21 14:19:50 -0700 | [diff] [blame] | 712 | GPR_ASSERT(arg != NULL && arg->type == GRPC_ARG_POINTER); |
Mark D. Roth | 557c990 | 2016-10-24 11:12:05 -0700 | [diff] [blame] | 713 | grpc_lb_addresses *addresses = arg->value.pointer.p; |
Mark D. Roth | f655c85 | 2016-09-06 10:40:38 -0700 | [diff] [blame] | 714 | size_t num_addrs = 0; |
Mark D. Roth | 5bd7be0 | 2016-10-21 14:19:50 -0700 | [diff] [blame] | 715 | for (size_t i = 0; i < addresses->num_addresses; i++) { |
| 716 | if (!addresses->addresses[i].is_balancer) ++num_addrs; |
Mark D. Roth | f655c85 | 2016-09-06 10:40:38 -0700 | [diff] [blame] | 717 | } |
| 718 | if (num_addrs == 0) return NULL; |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 719 | |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 720 | round_robin_lb_policy *p = gpr_malloc(sizeof(*p)); |
| 721 | memset(p, 0, sizeof(*p)); |
| 722 | |
Mark D. Roth | c5c3878 | 2016-09-16 08:51:01 -0700 | [diff] [blame] | 723 | p->num_addresses = num_addrs; |
Mark D. Roth | 989cdcd | 2016-09-06 13:28:28 -0700 | [diff] [blame] | 724 | p->subchannels = gpr_malloc(sizeof(*p->subchannels) * num_addrs); |
Mark D. Roth | f655c85 | 2016-09-06 10:40:38 -0700 | [diff] [blame] | 725 | memset(p->subchannels, 0, sizeof(*p->subchannels) * num_addrs); |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 726 | |
| 727 | grpc_subchannel_args sc_args; |
| 728 | size_t subchannel_idx = 0; |
Mark D. Roth | 5bd7be0 | 2016-10-21 14:19:50 -0700 | [diff] [blame] | 729 | for (size_t i = 0; i < addresses->num_addresses; i++) { |
Mark D. Roth | e011b1e | 2016-09-07 08:28:00 -0700 | [diff] [blame] | 730 | /* Skip balancer addresses, since we only know how to handle backends. */ |
Mark D. Roth | 5bd7be0 | 2016-10-21 14:19:50 -0700 | [diff] [blame] | 731 | if (addresses->addresses[i].is_balancer) continue; |
Mark D. Roth | f655c85 | 2016-09-06 10:40:38 -0700 | [diff] [blame] | 732 | |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 733 | memset(&sc_args, 0, sizeof(grpc_subchannel_args)); |
Mark D. Roth | df8f122 | 2017-01-13 22:59:39 +0000 | [diff] [blame] | 734 | grpc_arg addr_arg = |
| 735 | grpc_create_subchannel_address_arg(&addresses->addresses[i].address); |
Mark D. Roth | 0748f39 | 2017-01-13 09:22:44 -0800 | [diff] [blame] | 736 | grpc_channel_args *new_args = |
| 737 | grpc_channel_args_copy_and_add(args->args, &addr_arg, 1); |
| 738 | gpr_free(addr_arg.value.string); |
| 739 | sc_args.args = new_args; |
David Garcia Quintas | 86fcfcc | 2016-03-31 23:22:28 -0700 | [diff] [blame] | 740 | grpc_subchannel *subchannel = grpc_client_channel_factory_create_subchannel( |
| 741 | exec_ctx, args->client_channel_factory, &sc_args); |
David Garcia Quintas | 0129150 | 2017-02-07 13:26:41 -0800 | [diff] [blame] | 742 | if (grpc_lb_round_robin_trace) { |
| 743 | char *address_uri = |
| 744 | grpc_sockaddr_to_uri(&addresses->addresses[i].address); |
| 745 | gpr_log(GPR_DEBUG, "Created subchannel %p for address uri %s", |
| 746 | (void *)subchannel, address_uri); |
| 747 | gpr_free(address_uri); |
| 748 | } |
Mark D. Roth | 0748f39 | 2017-01-13 09:22:44 -0800 | [diff] [blame] | 749 | grpc_channel_args_destroy(exec_ctx, new_args); |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 750 | |
| 751 | if (subchannel != NULL) { |
| 752 | subchannel_data *sd = gpr_malloc(sizeof(*sd)); |
| 753 | memset(sd, 0, sizeof(*sd)); |
| 754 | p->subchannels[subchannel_idx] = sd; |
| 755 | sd->policy = p; |
| 756 | sd->index = subchannel_idx; |
| 757 | sd->subchannel = subchannel; |
David Garcia Quintas | 7ec2913 | 2016-11-01 04:09:05 +0100 | [diff] [blame] | 758 | sd->user_data_vtable = addresses->user_data_vtable; |
David Garcia Quintas | 5bdcd23 | 2016-11-02 15:47:02 -0700 | [diff] [blame] | 759 | if (sd->user_data_vtable != NULL) { |
| 760 | sd->user_data = |
| 761 | sd->user_data_vtable->copy(addresses->addresses[i].user_data); |
| 762 | } |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 763 | ++subchannel_idx; |
| 764 | grpc_closure_init(&sd->connectivity_changed_closure, |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 765 | rr_connectivity_changed, sd, grpc_schedule_on_exec_ctx); |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 766 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 767 | } |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 768 | if (subchannel_idx == 0) { |
David Garcia Quintas | 5b0e946 | 2016-08-15 19:38:39 -0700 | [diff] [blame] | 769 | /* couldn't create any subchannel. Bail out */ |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 770 | gpr_free(p->subchannels); |
| 771 | gpr_free(p); |
| 772 | return NULL; |
| 773 | } |
| 774 | p->num_subchannels = subchannel_idx; |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 775 | |
| 776 | /* The (dummy node) root of the ready list */ |
| 777 | p->ready_list.subchannel = NULL; |
| 778 | p->ready_list.prev = NULL; |
| 779 | p->ready_list.next = NULL; |
| 780 | p->ready_list_last_pick = &p->ready_list; |
| 781 | |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 782 | grpc_lb_policy_init(&p->base, &round_robin_lb_policy_vtable); |
| 783 | grpc_connectivity_state_init(&p->state_tracker, GRPC_CHANNEL_IDLE, |
| 784 | "round_robin"); |
David Garcia Quintas | a9cf87d | 2016-11-07 17:43:15 -0800 | [diff] [blame] | 785 | |
| 786 | if (grpc_lb_round_robin_trace) { |
| 787 | gpr_log(GPR_DEBUG, "Created RR policy at %p with %lu subchannels", |
| 788 | (void *)p, (unsigned long)p->num_subchannels); |
| 789 | } |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 790 | gpr_mu_init(&p->mu); |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 791 | return &p->base; |
| 792 | } |
| 793 | |
| 794 | static const grpc_lb_policy_factory_vtable round_robin_factory_vtable = { |
David Garcia Quintas | 0200870 | 2016-06-02 21:40:45 -0700 | [diff] [blame] | 795 | round_robin_factory_ref, round_robin_factory_unref, round_robin_create, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 796 | "round_robin"}; |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 797 | |
| 798 | static grpc_lb_policy_factory round_robin_lb_policy_factory = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 799 | &round_robin_factory_vtable}; |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 800 | |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 801 | static grpc_lb_policy_factory *round_robin_lb_factory_create() { |
David Garcia Quintas | 4fb049b | 2015-09-03 17:26:06 -0700 | [diff] [blame] | 802 | return &round_robin_lb_policy_factory; |
| 803 | } |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 804 | |
| 805 | /* Plugin registration */ |
| 806 | |
| 807 | void grpc_lb_policy_round_robin_init() { |
Craig Tiller | 3113ef4 | 2016-03-29 09:03:14 -0700 | [diff] [blame] | 808 | grpc_register_lb_policy(round_robin_lb_factory_create()); |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 809 | grpc_register_tracer("round_robin", &grpc_lb_round_robin_trace); |
| 810 | } |
| 811 | |
| 812 | void grpc_lb_policy_round_robin_shutdown() {} |