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