Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
murgatroid99 | 3466c4b | 2016-01-12 10:26:04 -0800 | [diff] [blame] | 3 | * Copyright 2015-2016, Google Inc. |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -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 | |
Vijay Pai | e9ef536 | 2016-02-25 18:10:24 -0800 | [diff] [blame] | 34 | #include "src/core/client_config/lb_policies/pick_first.h" |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 35 | #include "src/core/client_config/lb_policy_factory.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 | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame^] | 45 | uint32_t initial_metadata_flags; |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 46 | grpc_connected_subchannel **target; |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 47 | grpc_closure *on_complete; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 48 | } pending_pick; |
| 49 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 50 | typedef struct { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 51 | /** base policy: must be first */ |
| 52 | grpc_lb_policy base; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 53 | /** all our subchannels */ |
| 54 | grpc_subchannel **subchannels; |
| 55 | size_t num_subchannels; |
| 56 | |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 57 | grpc_closure connectivity_changed; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 58 | |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 59 | /** the selected channel (a grpc_connected_subchannel) */ |
| 60 | gpr_atm selected; |
Craig Tiller | 320bee0 | 2016-01-06 17:33:45 -0800 | [diff] [blame] | 61 | |
| 62 | /** mutex protecting remaining members */ |
| 63 | gpr_mu mu; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 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 | 81afdda | 2016-01-11 17:09:18 -0800 | [diff] [blame] | 79 | #define GET_SELECTED(p) \ |
Craig Tiller | d9d474a | 2016-01-26 06:50:51 -0800 | [diff] [blame] | 80 | ((grpc_connected_subchannel *)gpr_atm_acq_load(&(p)->selected)) |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 81 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 82 | void pf_destroy(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) { |
| 83 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 84 | grpc_connected_subchannel *selected = GET_SELECTED(p); |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 85 | size_t i; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 86 | GPR_ASSERT(p->pending_picks == NULL); |
| 87 | for (i = 0; i < p->num_subchannels; i++) { |
| 88 | GRPC_SUBCHANNEL_UNREF(exec_ctx, p->subchannels[i], "pick_first"); |
| 89 | } |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 90 | if (selected != NULL) { |
| 91 | GRPC_CONNECTED_SUBCHANNEL_UNREF(exec_ctx, selected, "picked_first"); |
Craig Tiller | 89a768e | 2015-10-06 09:55:59 -0700 | [diff] [blame] | 92 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 93 | grpc_connectivity_state_destroy(exec_ctx, &p->state_tracker); |
| 94 | gpr_free(p->subchannels); |
| 95 | gpr_mu_destroy(&p->mu); |
| 96 | gpr_free(p); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 99 | void pf_shutdown(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) { |
| 100 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
Craig Tiller | d2cc459 | 2015-07-01 07:50:47 -0700 | [diff] [blame] | 101 | pending_pick *pp; |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 102 | grpc_connected_subchannel *selected; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 103 | gpr_mu_lock(&p->mu); |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 104 | selected = GET_SELECTED(p); |
Craig Tiller | a14215a | 2015-07-17 17:21:08 -0700 | [diff] [blame] | 105 | p->shutdown = 1; |
Craig Tiller | 5795da7 | 2015-09-17 15:27:13 -0700 | [diff] [blame] | 106 | pp = p->pending_picks; |
| 107 | p->pending_picks = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 108 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
| 109 | GRPC_CHANNEL_FATAL_FAILURE, "shutdown"); |
Craig Tiller | f036a64 | 2015-12-01 17:00:40 -0800 | [diff] [blame] | 110 | /* cancel subscription */ |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 111 | if (selected != NULL) { |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 112 | grpc_connected_subchannel_notify_on_state_change( |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 113 | exec_ctx, selected, NULL, NULL, &p->connectivity_changed); |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 114 | } else { |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 115 | grpc_subchannel_notify_on_state_change( |
| 116 | exec_ctx, p->subchannels[p->checking_subchannel], NULL, NULL, |
| 117 | &p->connectivity_changed); |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 118 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 119 | gpr_mu_unlock(&p->mu); |
| 120 | while (pp != NULL) { |
| 121 | pending_pick *next = pp->next; |
| 122 | *pp->target = NULL; |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 123 | grpc_pollset_set_del_pollset(exec_ctx, p->base.interested_parties, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 124 | pp->pollset); |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 125 | grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, true, NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 126 | gpr_free(pp); |
| 127 | pp = next; |
| 128 | } |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 131 | 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] | 132 | grpc_connected_subchannel **target) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 133 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
| 134 | pending_pick *pp; |
| 135 | gpr_mu_lock(&p->mu); |
| 136 | pp = p->pending_picks; |
| 137 | p->pending_picks = NULL; |
| 138 | while (pp != NULL) { |
| 139 | pending_pick *next = pp->next; |
| 140 | if (pp->target == target) { |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 141 | grpc_pollset_set_del_pollset(exec_ctx, p->base.interested_parties, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 142 | pp->pollset); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 143 | *target = NULL; |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 144 | grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, false, NULL); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 145 | gpr_free(pp); |
| 146 | } else { |
| 147 | pp->next = p->pending_picks; |
| 148 | p->pending_picks = pp; |
| 149 | } |
| 150 | pp = next; |
| 151 | } |
| 152 | gpr_mu_unlock(&p->mu); |
| 153 | } |
| 154 | |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame^] | 155 | static void pf_cancel_picks(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, |
| 156 | uint32_t initial_metadata_flags_mask, |
| 157 | uint32_t initial_metadata_flags_eq) { |
| 158 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
| 159 | pending_pick *pp; |
| 160 | gpr_mu_lock(&p->mu); |
| 161 | pp = p->pending_picks; |
| 162 | p->pending_picks = NULL; |
| 163 | while (pp != NULL) { |
| 164 | pending_pick *next = pp->next; |
| 165 | if ((pp->initial_metadata_flags & initial_metadata_flags_mask) == |
| 166 | initial_metadata_flags_eq) { |
| 167 | grpc_pollset_set_del_pollset(exec_ctx, p->base.interested_parties, |
| 168 | pp->pollset); |
| 169 | *pp->target = NULL; |
| 170 | grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, false, NULL); |
| 171 | gpr_free(pp); |
| 172 | } else { |
| 173 | pp->next = p->pending_picks; |
| 174 | p->pending_picks = pp; |
| 175 | } |
| 176 | pp = next; |
| 177 | } |
| 178 | gpr_mu_unlock(&p->mu); |
| 179 | } |
| 180 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 181 | 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] | 182 | p->started_picking = 1; |
| 183 | p->checking_subchannel = 0; |
| 184 | p->checking_connectivity = GRPC_CHANNEL_IDLE; |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 185 | GRPC_LB_POLICY_WEAK_REF(&p->base, "pick_first_connectivity"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 186 | grpc_subchannel_notify_on_state_change( |
| 187 | exec_ctx, p->subchannels[p->checking_subchannel], |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 188 | p->base.interested_parties, &p->checking_connectivity, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 189 | &p->connectivity_changed); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 192 | void pf_exit_idle(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) { |
| 193 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
| 194 | gpr_mu_lock(&p->mu); |
| 195 | if (!p->started_picking) { |
| 196 | start_picking(exec_ctx, p); |
| 197 | } |
| 198 | gpr_mu_unlock(&p->mu); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 201 | 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] | 202 | grpc_metadata_batch *initial_metadata, |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame^] | 203 | uint32_t initial_metadata_flags, grpc_connected_subchannel **target, |
| 204 | grpc_closure *on_complete) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 205 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 206 | pending_pick *pp; |
Craig Tiller | 320bee0 | 2016-01-06 17:33:45 -0800 | [diff] [blame] | 207 | |
| 208 | /* Check atomically for a selected channel */ |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 209 | grpc_connected_subchannel *selected = GET_SELECTED(p); |
| 210 | if (selected != NULL) { |
| 211 | *target = selected; |
| 212 | return 1; |
| 213 | } |
Craig Tiller | 320bee0 | 2016-01-06 17:33:45 -0800 | [diff] [blame] | 214 | |
| 215 | /* No subchannel selected yet, so acquire lock and then attempt again */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 216 | gpr_mu_lock(&p->mu); |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 217 | selected = GET_SELECTED(p); |
| 218 | if (selected) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 219 | gpr_mu_unlock(&p->mu); |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 220 | *target = selected; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 221 | return 1; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 222 | } else { |
| 223 | if (!p->started_picking) { |
| 224 | start_picking(exec_ctx, p); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 225 | } |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 226 | grpc_pollset_set_add_pollset(exec_ctx, p->base.interested_parties, pollset); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 227 | pp = gpr_malloc(sizeof(*pp)); |
| 228 | pp->next = p->pending_picks; |
| 229 | pp->pollset = pollset; |
| 230 | pp->target = target; |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame^] | 231 | pp->initial_metadata_flags = initial_metadata_flags; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 232 | pp->on_complete = on_complete; |
| 233 | p->pending_picks = pp; |
| 234 | gpr_mu_unlock(&p->mu); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 235 | return 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 236 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Craig Tiller | 1f41b6b | 2015-10-09 15:07:02 -0700 | [diff] [blame] | 239 | static void destroy_subchannels(grpc_exec_ctx *exec_ctx, void *arg, |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 240 | bool iomgr_success) { |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 241 | pick_first_lb_policy *p = arg; |
| 242 | size_t i; |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 243 | size_t num_subchannels = p->num_subchannels; |
| 244 | grpc_subchannel **subchannels; |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 245 | |
| 246 | gpr_mu_lock(&p->mu); |
| 247 | subchannels = p->subchannels; |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 248 | p->num_subchannels = 0; |
| 249 | p->subchannels = NULL; |
| 250 | gpr_mu_unlock(&p->mu); |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 251 | GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &p->base, "destroy_subchannels"); |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 252 | |
| 253 | for (i = 0; i < num_subchannels; i++) { |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 254 | GRPC_SUBCHANNEL_UNREF(exec_ctx, subchannels[i], "pick_first"); |
| 255 | } |
| 256 | |
| 257 | gpr_free(subchannels); |
| 258 | } |
| 259 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 260 | static void pf_connectivity_changed(grpc_exec_ctx *exec_ctx, void *arg, |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 261 | bool iomgr_success) { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 262 | pick_first_lb_policy *p = arg; |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 263 | grpc_subchannel *selected_subchannel; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 264 | pending_pick *pp; |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 265 | grpc_connected_subchannel *selected; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 266 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 267 | gpr_mu_lock(&p->mu); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 268 | |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 269 | selected = GET_SELECTED(p); |
| 270 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 271 | if (p->shutdown) { |
| 272 | gpr_mu_unlock(&p->mu); |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 273 | GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &p->base, "pick_first_connectivity"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 274 | return; |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 275 | } else if (selected != NULL) { |
Craig Tiller | cb2609f | 2015-11-24 17:19:19 -0800 | [diff] [blame] | 276 | if (p->checking_connectivity == GRPC_CHANNEL_TRANSIENT_FAILURE) { |
| 277 | /* if the selected channel goes bad, we're done */ |
| 278 | p->checking_connectivity = GRPC_CHANNEL_FATAL_FAILURE; |
| 279 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 280 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
| 281 | p->checking_connectivity, "selected_changed"); |
| 282 | if (p->checking_connectivity != GRPC_CHANNEL_FATAL_FAILURE) { |
Craig Tiller | ab33b48 | 2015-11-21 08:11:04 -0800 | [diff] [blame] | 283 | grpc_connected_subchannel_notify_on_state_change( |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 284 | exec_ctx, selected, p->base.interested_parties, |
Craig Tiller | a6bebf4 | 2015-12-01 17:02:35 -0800 | [diff] [blame] | 285 | &p->checking_connectivity, &p->connectivity_changed); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 286 | } else { |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 287 | GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &p->base, "pick_first_connectivity"); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 288 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 289 | } else { |
| 290 | loop: |
| 291 | switch (p->checking_connectivity) { |
| 292 | case GRPC_CHANNEL_READY: |
| 293 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
| 294 | GRPC_CHANNEL_READY, "connecting_ready"); |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 295 | selected_subchannel = p->subchannels[p->checking_subchannel]; |
Craig Tiller | 81afdda | 2016-01-11 17:09:18 -0800 | [diff] [blame] | 296 | selected = |
| 297 | grpc_subchannel_get_connected_subchannel(selected_subchannel); |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 298 | GPR_ASSERT(selected != NULL); |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 299 | GRPC_CONNECTED_SUBCHANNEL_REF(selected, "picked_first"); |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 300 | /* drop the pick list: we are connected now */ |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 301 | GRPC_LB_POLICY_WEAK_REF(&p->base, "destroy_subchannels"); |
Craig Tiller | d9d474a | 2016-01-26 06:50:51 -0800 | [diff] [blame] | 302 | gpr_atm_rel_store(&p->selected, (gpr_atm)selected); |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 303 | grpc_exec_ctx_enqueue( |
| 304 | exec_ctx, grpc_closure_create(destroy_subchannels, p), true, NULL); |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 305 | /* update any calls that were waiting for a pick */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 306 | while ((pp = p->pending_picks)) { |
| 307 | p->pending_picks = pp->next; |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 308 | *pp->target = selected; |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 309 | grpc_pollset_set_del_pollset(exec_ctx, p->base.interested_parties, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 310 | pp->pollset); |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 311 | grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, true, NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 312 | gpr_free(pp); |
| 313 | } |
Craig Tiller | ab33b48 | 2015-11-21 08:11:04 -0800 | [diff] [blame] | 314 | grpc_connected_subchannel_notify_on_state_change( |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 315 | exec_ctx, selected, p->base.interested_parties, |
Craig Tiller | a6bebf4 | 2015-12-01 17:02:35 -0800 | [diff] [blame] | 316 | &p->checking_connectivity, &p->connectivity_changed); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 317 | break; |
| 318 | case GRPC_CHANNEL_TRANSIENT_FAILURE: |
| 319 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
| 320 | GRPC_CHANNEL_TRANSIENT_FAILURE, |
| 321 | "connecting_transient_failure"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 322 | p->checking_subchannel = |
| 323 | (p->checking_subchannel + 1) % p->num_subchannels; |
| 324 | p->checking_connectivity = grpc_subchannel_check_connectivity( |
| 325 | p->subchannels[p->checking_subchannel]); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 326 | if (p->checking_connectivity == GRPC_CHANNEL_TRANSIENT_FAILURE) { |
| 327 | grpc_subchannel_notify_on_state_change( |
| 328 | exec_ctx, p->subchannels[p->checking_subchannel], |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 329 | p->base.interested_parties, &p->checking_connectivity, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 330 | &p->connectivity_changed); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 331 | } else { |
| 332 | goto loop; |
| 333 | } |
| 334 | break; |
| 335 | case GRPC_CHANNEL_CONNECTING: |
| 336 | case GRPC_CHANNEL_IDLE: |
| 337 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
| 338 | GRPC_CHANNEL_CONNECTING, |
| 339 | "connecting_changed"); |
| 340 | grpc_subchannel_notify_on_state_change( |
| 341 | exec_ctx, p->subchannels[p->checking_subchannel], |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 342 | p->base.interested_parties, &p->checking_connectivity, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 343 | &p->connectivity_changed); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 344 | break; |
| 345 | case GRPC_CHANNEL_FATAL_FAILURE: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 346 | p->num_subchannels--; |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 347 | GPR_SWAP(grpc_subchannel *, p->subchannels[p->checking_subchannel], |
| 348 | p->subchannels[p->num_subchannels]); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 349 | GRPC_SUBCHANNEL_UNREF(exec_ctx, p->subchannels[p->num_subchannels], |
| 350 | "pick_first"); |
| 351 | if (p->num_subchannels == 0) { |
| 352 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
| 353 | GRPC_CHANNEL_FATAL_FAILURE, |
| 354 | "no_more_channels"); |
| 355 | while ((pp = p->pending_picks)) { |
| 356 | p->pending_picks = pp->next; |
| 357 | *pp->target = NULL; |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 358 | grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, true, NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 359 | gpr_free(pp); |
| 360 | } |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 361 | GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &p->base, |
| 362 | "pick_first_connectivity"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 363 | } else { |
| 364 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
| 365 | GRPC_CHANNEL_TRANSIENT_FAILURE, |
| 366 | "subchannel_failed"); |
| 367 | p->checking_subchannel %= p->num_subchannels; |
| 368 | p->checking_connectivity = grpc_subchannel_check_connectivity( |
| 369 | p->subchannels[p->checking_subchannel]); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 370 | goto loop; |
| 371 | } |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 372 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 373 | } |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 374 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 375 | gpr_mu_unlock(&p->mu); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 378 | static grpc_connectivity_state pf_check_connectivity(grpc_exec_ctx *exec_ctx, |
| 379 | grpc_lb_policy *pol) { |
| 380 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 381 | grpc_connectivity_state st; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 382 | gpr_mu_lock(&p->mu); |
| 383 | st = grpc_connectivity_state_check(&p->state_tracker); |
| 384 | gpr_mu_unlock(&p->mu); |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 385 | return st; |
| 386 | } |
| 387 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 388 | void pf_notify_on_state_change(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, |
| 389 | grpc_connectivity_state *current, |
| 390 | grpc_closure *notify) { |
| 391 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
| 392 | gpr_mu_lock(&p->mu); |
| 393 | grpc_connectivity_state_notify_on_state_change(exec_ctx, &p->state_tracker, |
| 394 | current, notify); |
| 395 | gpr_mu_unlock(&p->mu); |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 396 | } |
| 397 | |
Craig Tiller | e2c6237 | 2015-12-07 16:11:03 -0800 | [diff] [blame] | 398 | void pf_ping_one(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, |
| 399 | grpc_closure *closure) { |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 400 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
Craig Tiller | 093193e | 2016-01-07 07:14:44 -0800 | [diff] [blame] | 401 | grpc_connected_subchannel *selected = GET_SELECTED(p); |
| 402 | if (selected) { |
| 403 | grpc_connected_subchannel_ping(exec_ctx, selected, closure); |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 404 | } else { |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 405 | grpc_exec_ctx_enqueue(exec_ctx, closure, false, NULL); |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 406 | } |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 407 | } |
| 408 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 409 | static const grpc_lb_policy_vtable pick_first_lb_policy_vtable = { |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame^] | 410 | pf_destroy, pf_shutdown, pf_pick, pf_cancel_pick, pf_cancel_picks, |
| 411 | pf_ping_one, pf_exit_idle, pf_check_connectivity, |
| 412 | pf_notify_on_state_change}; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 413 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 414 | static void pick_first_factory_ref(grpc_lb_policy_factory *factory) {} |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 415 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 416 | static void pick_first_factory_unref(grpc_lb_policy_factory *factory) {} |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 417 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 418 | static grpc_lb_policy *create_pick_first(grpc_lb_policy_factory *factory, |
| 419 | grpc_lb_policy_args *args) { |
| 420 | pick_first_lb_policy *p = gpr_malloc(sizeof(*p)); |
| 421 | GPR_ASSERT(args->num_subchannels > 0); |
| 422 | memset(p, 0, sizeof(*p)); |
| 423 | grpc_lb_policy_init(&p->base, &pick_first_lb_policy_vtable); |
| 424 | p->subchannels = |
| 425 | gpr_malloc(sizeof(grpc_subchannel *) * args->num_subchannels); |
David Garcia Quintas | c7705c7 | 2015-09-09 17:21:11 -0700 | [diff] [blame] | 426 | p->num_subchannels = args->num_subchannels; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 427 | grpc_connectivity_state_init(&p->state_tracker, GRPC_CHANNEL_IDLE, |
| 428 | "pick_first"); |
| 429 | memcpy(p->subchannels, args->subchannels, |
| 430 | sizeof(grpc_subchannel *) * args->num_subchannels); |
| 431 | grpc_closure_init(&p->connectivity_changed, pf_connectivity_changed, p); |
| 432 | gpr_mu_init(&p->mu); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 433 | return &p->base; |
| 434 | } |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 435 | |
| 436 | static const grpc_lb_policy_factory_vtable pick_first_factory_vtable = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 437 | pick_first_factory_ref, pick_first_factory_unref, create_pick_first, |
| 438 | "pick_first"}; |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 439 | |
| 440 | static grpc_lb_policy_factory pick_first_lb_policy_factory = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 441 | &pick_first_factory_vtable}; |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 442 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 443 | grpc_lb_policy_factory *grpc_pick_first_lb_factory_create() { |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 444 | return &pick_first_lb_policy_factory; |
| 445 | } |