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