Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2015, Google Inc. |
| 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 | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 34 | #include "src/core/client_config/lb_policy_factory.h" |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 35 | #include "src/core/client_config/lb_policies/pick_first.h" |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 36 | |
| 37 | #include <string.h> |
| 38 | |
| 39 | #include <grpc/support/alloc.h> |
Craig Tiller | 08a1cf8 | 2015-06-29 09:37:52 -0700 | [diff] [blame] | 40 | #include "src/core/transport/connectivity_state.h" |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 41 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 42 | typedef struct pending_pick { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 43 | struct pending_pick *next; |
| 44 | grpc_pollset *pollset; |
| 45 | grpc_subchannel **target; |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 46 | grpc_closure *on_complete; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 47 | } pending_pick; |
| 48 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 49 | typedef struct { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 50 | /** base policy: must be first */ |
| 51 | grpc_lb_policy base; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 52 | /** all our subchannels */ |
| 53 | grpc_subchannel **subchannels; |
| 54 | size_t num_subchannels; |
| 55 | |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 56 | grpc_closure connectivity_changed; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 57 | |
| 58 | /** mutex protecting remaining members */ |
| 59 | gpr_mu mu; |
| 60 | /** the selected channel |
| 61 | TODO(ctiller): this should be atomically set so we don't |
| 62 | need to take a mutex in the common case */ |
| 63 | grpc_subchannel *selected; |
| 64 | /** have we started picking? */ |
| 65 | int started_picking; |
Craig Tiller | a14215a | 2015-07-17 17:21:08 -0700 | [diff] [blame] | 66 | /** are we shut down? */ |
| 67 | int shutdown; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 68 | /** which subchannel are we watching? */ |
| 69 | size_t checking_subchannel; |
| 70 | /** what is the connectivity of that channel? */ |
| 71 | grpc_connectivity_state checking_connectivity; |
| 72 | /** list of picks that are waiting on connectivity */ |
| 73 | pending_pick *pending_picks; |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 74 | |
| 75 | /** our connectivity state tracker */ |
| 76 | grpc_connectivity_state_tracker state_tracker; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 77 | } pick_first_lb_policy; |
| 78 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 79 | static void del_interested_parties_locked(grpc_exec_ctx *exec_ctx, |
| 80 | pick_first_lb_policy *p) { |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 81 | pending_pick *pp; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 82 | for (pp = p->pending_picks; pp; pp = pp->next) { |
| 83 | grpc_subchannel_del_interested_party( |
| 84 | exec_ctx, p->subchannels[p->checking_subchannel], pp->pollset); |
| 85 | } |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 88 | static void add_interested_parties_locked(grpc_exec_ctx *exec_ctx, |
| 89 | pick_first_lb_policy *p) { |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 90 | pending_pick *pp; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 91 | for (pp = p->pending_picks; pp; pp = pp->next) { |
| 92 | grpc_subchannel_add_interested_party( |
| 93 | exec_ctx, p->subchannels[p->checking_subchannel], pp->pollset); |
| 94 | } |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 97 | void pf_destroy(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) { |
| 98 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 99 | size_t i; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 100 | GPR_ASSERT(p->pending_picks == NULL); |
| 101 | for (i = 0; i < p->num_subchannels; i++) { |
| 102 | GRPC_SUBCHANNEL_UNREF(exec_ctx, p->subchannels[i], "pick_first"); |
| 103 | } |
| 104 | grpc_connectivity_state_destroy(exec_ctx, &p->state_tracker); |
| 105 | gpr_free(p->subchannels); |
| 106 | gpr_mu_destroy(&p->mu); |
| 107 | gpr_free(p); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 110 | void pf_shutdown(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) { |
| 111 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
Craig Tiller | d2cc459 | 2015-07-01 07:50:47 -0700 | [diff] [blame] | 112 | pending_pick *pp; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 113 | gpr_mu_lock(&p->mu); |
| 114 | del_interested_parties_locked(exec_ctx, p); |
Craig Tiller | a14215a | 2015-07-17 17:21:08 -0700 | [diff] [blame] | 115 | p->shutdown = 1; |
Craig Tiller | 5795da7 | 2015-09-17 15:27:13 -0700 | [diff] [blame] | 116 | pp = p->pending_picks; |
| 117 | p->pending_picks = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 118 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
| 119 | GRPC_CHANNEL_FATAL_FAILURE, "shutdown"); |
| 120 | gpr_mu_unlock(&p->mu); |
| 121 | while (pp != NULL) { |
| 122 | pending_pick *next = pp->next; |
| 123 | *pp->target = NULL; |
| 124 | grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, 1); |
| 125 | gpr_free(pp); |
| 126 | pp = next; |
| 127 | } |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 130 | static void start_picking(grpc_exec_ctx *exec_ctx, pick_first_lb_policy *p) { |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 131 | p->started_picking = 1; |
| 132 | p->checking_subchannel = 0; |
| 133 | p->checking_connectivity = GRPC_CHANNEL_IDLE; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 134 | GRPC_LB_POLICY_REF(&p->base, "pick_first_connectivity"); |
| 135 | grpc_subchannel_notify_on_state_change( |
| 136 | exec_ctx, p->subchannels[p->checking_subchannel], |
| 137 | &p->checking_connectivity, &p->connectivity_changed); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 140 | void pf_exit_idle(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) { |
| 141 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
| 142 | gpr_mu_lock(&p->mu); |
| 143 | if (!p->started_picking) { |
| 144 | start_picking(exec_ctx, p); |
| 145 | } |
| 146 | gpr_mu_unlock(&p->mu); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 149 | void pf_pick(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, |
| 150 | grpc_pollset *pollset, grpc_metadata_batch *initial_metadata, |
| 151 | grpc_subchannel **target, grpc_closure *on_complete) { |
| 152 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 153 | pending_pick *pp; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 154 | gpr_mu_lock(&p->mu); |
| 155 | if (p->selected) { |
| 156 | gpr_mu_unlock(&p->mu); |
| 157 | *target = p->selected; |
| 158 | grpc_exec_ctx_enqueue(exec_ctx, on_complete, 1); |
| 159 | } else { |
| 160 | if (!p->started_picking) { |
| 161 | start_picking(exec_ctx, p); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 162 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 163 | grpc_subchannel_add_interested_party( |
| 164 | exec_ctx, p->subchannels[p->checking_subchannel], pollset); |
| 165 | pp = gpr_malloc(sizeof(*pp)); |
| 166 | pp->next = p->pending_picks; |
| 167 | pp->pollset = pollset; |
| 168 | pp->target = target; |
| 169 | pp->on_complete = on_complete; |
| 170 | p->pending_picks = pp; |
| 171 | gpr_mu_unlock(&p->mu); |
| 172 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame^] | 175 | static void destroy_subchannels(grpc_exec_ctx *exec_ctx, void *arg, |
| 176 | int iomgr_success) { |
| 177 | pick_first_lb_policy *p = arg; |
| 178 | size_t i; |
| 179 | grpc_transport_op op; |
| 180 | size_t num_subchannels = p->num_subchannels; |
| 181 | grpc_subchannel **subchannels; |
| 182 | grpc_subchannel *exclude_subchannel; |
| 183 | |
| 184 | gpr_mu_lock(&p->mu); |
| 185 | subchannels = p->subchannels; |
| 186 | exclude_subchannel = p->selected; |
| 187 | p->num_subchannels = 0; |
| 188 | p->subchannels = NULL; |
| 189 | gpr_mu_unlock(&p->mu); |
| 190 | GRPC_LB_POLICY_UNREF(exec_ctx, &p->base, "destroy_subchannels"); |
| 191 | |
| 192 | for (i = 0; i < num_subchannels; i++) { |
| 193 | if (subchannels[i] == exclude_subchannel) { |
| 194 | exclude_subchannel = NULL; |
| 195 | continue; |
| 196 | } |
| 197 | memset(&op, 0, sizeof(op)); |
| 198 | op.disconnect = 1; |
| 199 | grpc_subchannel_process_transport_op(exec_ctx, subchannels[i], &op); |
| 200 | GRPC_SUBCHANNEL_UNREF(exec_ctx, subchannels[i], "pick_first"); |
| 201 | } |
| 202 | |
| 203 | gpr_free(subchannels); |
| 204 | } |
| 205 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 206 | static void pf_connectivity_changed(grpc_exec_ctx *exec_ctx, void *arg, |
| 207 | int iomgr_success) { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 208 | pick_first_lb_policy *p = arg; |
| 209 | pending_pick *pp; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 210 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 211 | gpr_mu_lock(&p->mu); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 212 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 213 | if (p->shutdown) { |
| 214 | gpr_mu_unlock(&p->mu); |
| 215 | GRPC_LB_POLICY_UNREF(exec_ctx, &p->base, "pick_first_connectivity"); |
| 216 | return; |
| 217 | } else if (p->selected != NULL) { |
| 218 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
| 219 | p->checking_connectivity, "selected_changed"); |
| 220 | if (p->checking_connectivity != GRPC_CHANNEL_FATAL_FAILURE) { |
| 221 | grpc_subchannel_notify_on_state_change(exec_ctx, p->selected, |
| 222 | &p->checking_connectivity, |
| 223 | &p->connectivity_changed); |
| 224 | } else { |
| 225 | GRPC_LB_POLICY_UNREF(exec_ctx, &p->base, "pick_first_connectivity"); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 226 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 227 | } else { |
| 228 | loop: |
| 229 | switch (p->checking_connectivity) { |
| 230 | case GRPC_CHANNEL_READY: |
| 231 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
| 232 | GRPC_CHANNEL_READY, "connecting_ready"); |
| 233 | p->selected = p->subchannels[p->checking_subchannel]; |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame^] | 234 | /* drop the pick list: we are connected now */ |
| 235 | GRPC_LB_POLICY_REF(&p->base, "destroy_subchannels"); |
| 236 | grpc_exec_ctx_enqueue(exec_ctx, grpc_closure_create(destroy_subchannels, p), 1); |
| 237 | /* update any calls that were waiting for a pick */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 238 | while ((pp = p->pending_picks)) { |
| 239 | p->pending_picks = pp->next; |
| 240 | *pp->target = p->selected; |
| 241 | grpc_subchannel_del_interested_party(exec_ctx, p->selected, |
| 242 | pp->pollset); |
| 243 | grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, 1); |
| 244 | gpr_free(pp); |
| 245 | } |
| 246 | grpc_subchannel_notify_on_state_change(exec_ctx, p->selected, |
| 247 | &p->checking_connectivity, |
| 248 | &p->connectivity_changed); |
| 249 | break; |
| 250 | case GRPC_CHANNEL_TRANSIENT_FAILURE: |
| 251 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
| 252 | GRPC_CHANNEL_TRANSIENT_FAILURE, |
| 253 | "connecting_transient_failure"); |
| 254 | del_interested_parties_locked(exec_ctx, p); |
| 255 | p->checking_subchannel = |
| 256 | (p->checking_subchannel + 1) % p->num_subchannels; |
| 257 | p->checking_connectivity = grpc_subchannel_check_connectivity( |
| 258 | p->subchannels[p->checking_subchannel]); |
| 259 | add_interested_parties_locked(exec_ctx, p); |
| 260 | if (p->checking_connectivity == GRPC_CHANNEL_TRANSIENT_FAILURE) { |
| 261 | grpc_subchannel_notify_on_state_change( |
| 262 | exec_ctx, p->subchannels[p->checking_subchannel], |
| 263 | &p->checking_connectivity, &p->connectivity_changed); |
| 264 | } else { |
| 265 | goto loop; |
| 266 | } |
| 267 | break; |
| 268 | case GRPC_CHANNEL_CONNECTING: |
| 269 | case GRPC_CHANNEL_IDLE: |
| 270 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
| 271 | GRPC_CHANNEL_CONNECTING, |
| 272 | "connecting_changed"); |
| 273 | grpc_subchannel_notify_on_state_change( |
| 274 | exec_ctx, p->subchannels[p->checking_subchannel], |
| 275 | &p->checking_connectivity, &p->connectivity_changed); |
| 276 | break; |
| 277 | case GRPC_CHANNEL_FATAL_FAILURE: |
| 278 | del_interested_parties_locked(exec_ctx, p); |
| 279 | GPR_SWAP(grpc_subchannel *, p->subchannels[p->checking_subchannel], |
| 280 | p->subchannels[p->num_subchannels - 1]); |
| 281 | p->num_subchannels--; |
| 282 | GRPC_SUBCHANNEL_UNREF(exec_ctx, p->subchannels[p->num_subchannels], |
| 283 | "pick_first"); |
| 284 | if (p->num_subchannels == 0) { |
| 285 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
| 286 | GRPC_CHANNEL_FATAL_FAILURE, |
| 287 | "no_more_channels"); |
| 288 | while ((pp = p->pending_picks)) { |
| 289 | p->pending_picks = pp->next; |
| 290 | *pp->target = NULL; |
| 291 | grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, 1); |
| 292 | gpr_free(pp); |
| 293 | } |
| 294 | GRPC_LB_POLICY_UNREF(exec_ctx, &p->base, "pick_first_connectivity"); |
| 295 | } else { |
| 296 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
| 297 | GRPC_CHANNEL_TRANSIENT_FAILURE, |
| 298 | "subchannel_failed"); |
| 299 | p->checking_subchannel %= p->num_subchannels; |
| 300 | p->checking_connectivity = grpc_subchannel_check_connectivity( |
| 301 | p->subchannels[p->checking_subchannel]); |
| 302 | add_interested_parties_locked(exec_ctx, p); |
| 303 | goto loop; |
| 304 | } |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 305 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 306 | } |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 307 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 308 | gpr_mu_unlock(&p->mu); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 311 | static void pf_broadcast(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, |
| 312 | grpc_transport_op *op) { |
| 313 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 314 | size_t i; |
| 315 | size_t n; |
| 316 | grpc_subchannel **subchannels; |
| 317 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 318 | gpr_mu_lock(&p->mu); |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 319 | n = p->num_subchannels; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 320 | subchannels = gpr_malloc(n * sizeof(*subchannels)); |
| 321 | for (i = 0; i < n; i++) { |
| 322 | subchannels[i] = p->subchannels[i]; |
| 323 | GRPC_SUBCHANNEL_REF(subchannels[i], "pf_broadcast"); |
| 324 | } |
| 325 | gpr_mu_unlock(&p->mu); |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 326 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 327 | for (i = 0; i < n; i++) { |
| 328 | grpc_subchannel_process_transport_op(exec_ctx, subchannels[i], op); |
| 329 | GRPC_SUBCHANNEL_UNREF(exec_ctx, subchannels[i], "pf_broadcast"); |
| 330 | } |
| 331 | gpr_free(subchannels); |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 334 | static grpc_connectivity_state pf_check_connectivity(grpc_exec_ctx *exec_ctx, |
| 335 | grpc_lb_policy *pol) { |
| 336 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 337 | grpc_connectivity_state st; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 338 | gpr_mu_lock(&p->mu); |
| 339 | st = grpc_connectivity_state_check(&p->state_tracker); |
| 340 | gpr_mu_unlock(&p->mu); |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 341 | return st; |
| 342 | } |
| 343 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 344 | void pf_notify_on_state_change(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, |
| 345 | grpc_connectivity_state *current, |
| 346 | grpc_closure *notify) { |
| 347 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
| 348 | gpr_mu_lock(&p->mu); |
| 349 | grpc_connectivity_state_notify_on_state_change(exec_ctx, &p->state_tracker, |
| 350 | current, notify); |
| 351 | gpr_mu_unlock(&p->mu); |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 354 | static const grpc_lb_policy_vtable pick_first_lb_policy_vtable = { |
Craig Tiller | 71a0f9d | 2015-09-28 17:22:01 -0700 | [diff] [blame] | 355 | pf_destroy, pf_shutdown, pf_pick, pf_exit_idle, pf_broadcast, |
| 356 | pf_check_connectivity, pf_notify_on_state_change}; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 357 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 358 | static void pick_first_factory_ref(grpc_lb_policy_factory *factory) {} |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 359 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 360 | static void pick_first_factory_unref(grpc_lb_policy_factory *factory) {} |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 361 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 362 | static grpc_lb_policy *create_pick_first(grpc_lb_policy_factory *factory, |
| 363 | grpc_lb_policy_args *args) { |
| 364 | pick_first_lb_policy *p = gpr_malloc(sizeof(*p)); |
| 365 | GPR_ASSERT(args->num_subchannels > 0); |
| 366 | memset(p, 0, sizeof(*p)); |
| 367 | grpc_lb_policy_init(&p->base, &pick_first_lb_policy_vtable); |
| 368 | p->subchannels = |
| 369 | gpr_malloc(sizeof(grpc_subchannel *) * args->num_subchannels); |
David Garcia Quintas | c7705c7 | 2015-09-09 17:21:11 -0700 | [diff] [blame] | 370 | p->num_subchannels = args->num_subchannels; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 371 | grpc_connectivity_state_init(&p->state_tracker, GRPC_CHANNEL_IDLE, |
| 372 | "pick_first"); |
| 373 | memcpy(p->subchannels, args->subchannels, |
| 374 | sizeof(grpc_subchannel *) * args->num_subchannels); |
| 375 | grpc_closure_init(&p->connectivity_changed, pf_connectivity_changed, p); |
| 376 | gpr_mu_init(&p->mu); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 377 | return &p->base; |
| 378 | } |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 379 | |
| 380 | static const grpc_lb_policy_factory_vtable pick_first_factory_vtable = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 381 | pick_first_factory_ref, pick_first_factory_unref, create_pick_first, |
| 382 | "pick_first"}; |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 383 | |
| 384 | static grpc_lb_policy_factory pick_first_lb_policy_factory = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 385 | &pick_first_factory_vtable}; |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 386 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 387 | grpc_lb_policy_factory *grpc_pick_first_lb_factory_create() { |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 388 | return &pick_first_lb_policy_factory; |
| 389 | } |