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 | |
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 | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 43 | grpc_connected_subchannel **target; |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 44 | grpc_closure *on_complete; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 45 | } pending_pick; |
| 46 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 47 | typedef struct { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 48 | /** base policy: must be first */ |
| 49 | grpc_lb_policy base; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 50 | /** all our subchannels */ |
| 51 | grpc_subchannel **subchannels; |
| 52 | size_t num_subchannels; |
| 53 | |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 54 | grpc_closure connectivity_changed; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 55 | |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 56 | /** the selected channel (a grpc_connected_subchannel) */ |
| 57 | gpr_atm selected; |
Craig Tiller | 320bee0 | 2016-01-06 17:33:45 -0800 | [diff] [blame] | 58 | |
| 59 | /** mutex protecting remaining members */ |
| 60 | gpr_mu mu; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 61 | /** have we started picking? */ |
| 62 | int started_picking; |
Craig Tiller | a14215a | 2015-07-17 17:21:08 -0700 | [diff] [blame] | 63 | /** are we shut down? */ |
| 64 | int shutdown; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 65 | /** which subchannel are we watching? */ |
| 66 | size_t checking_subchannel; |
| 67 | /** what is the connectivity of that channel? */ |
| 68 | grpc_connectivity_state checking_connectivity; |
| 69 | /** list of picks that are waiting on connectivity */ |
| 70 | pending_pick *pending_picks; |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 71 | |
| 72 | /** our connectivity state tracker */ |
| 73 | grpc_connectivity_state_tracker state_tracker; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 74 | } pick_first_lb_policy; |
| 75 | |
Craig Tiller | 81afdda | 2016-01-11 17:09:18 -0800 | [diff] [blame] | 76 | #define GET_SELECTED(p) \ |
Craig Tiller | d9d474a | 2016-01-26 06:50:51 -0800 | [diff] [blame] | 77 | ((grpc_connected_subchannel *)gpr_atm_acq_load(&(p)->selected)) |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 78 | |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 79 | 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] | 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 | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 96 | 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] | 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 | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 120 | grpc_pollset_set_del_pollset(exec_ctx, p->base.interested_parties, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 121 | pp->pollset); |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 122 | grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, true, NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 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 | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 138 | grpc_pollset_set_del_pollset(exec_ctx, p->base.interested_parties, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 139 | pp->pollset); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 140 | *target = NULL; |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 141 | grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, false, NULL); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 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 | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 159 | p->base.interested_parties, &p->checking_connectivity, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 160 | &p->connectivity_changed); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 163 | 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] | 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 | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 172 | static int pf_pick(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, |
| 173 | grpc_pollset *pollset, grpc_metadata_batch *initial_metadata, |
| 174 | grpc_connected_subchannel **target, |
| 175 | grpc_closure *on_complete) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 176 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 177 | pending_pick *pp; |
Craig Tiller | 320bee0 | 2016-01-06 17:33:45 -0800 | [diff] [blame] | 178 | |
| 179 | /* Check atomically for a selected channel */ |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 180 | grpc_connected_subchannel *selected = GET_SELECTED(p); |
| 181 | if (selected != NULL) { |
| 182 | *target = selected; |
| 183 | return 1; |
| 184 | } |
Craig Tiller | 320bee0 | 2016-01-06 17:33:45 -0800 | [diff] [blame] | 185 | |
| 186 | /* No subchannel selected yet, so acquire lock and then attempt again */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 187 | gpr_mu_lock(&p->mu); |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 188 | selected = GET_SELECTED(p); |
| 189 | if (selected) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 190 | gpr_mu_unlock(&p->mu); |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 191 | *target = selected; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 192 | return 1; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 193 | } else { |
| 194 | if (!p->started_picking) { |
| 195 | start_picking(exec_ctx, p); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 196 | } |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 197 | grpc_pollset_set_add_pollset(exec_ctx, p->base.interested_parties, pollset); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 198 | pp = gpr_malloc(sizeof(*pp)); |
| 199 | pp->next = p->pending_picks; |
| 200 | pp->pollset = pollset; |
| 201 | pp->target = target; |
| 202 | pp->on_complete = on_complete; |
| 203 | p->pending_picks = pp; |
| 204 | gpr_mu_unlock(&p->mu); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 205 | return 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 206 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Craig Tiller | 1f41b6b | 2015-10-09 15:07:02 -0700 | [diff] [blame] | 209 | static void destroy_subchannels(grpc_exec_ctx *exec_ctx, void *arg, |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 210 | bool iomgr_success) { |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 211 | pick_first_lb_policy *p = arg; |
| 212 | size_t i; |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 213 | size_t num_subchannels = p->num_subchannels; |
| 214 | grpc_subchannel **subchannels; |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 215 | |
| 216 | gpr_mu_lock(&p->mu); |
| 217 | subchannels = p->subchannels; |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 218 | p->num_subchannels = 0; |
| 219 | p->subchannels = NULL; |
| 220 | gpr_mu_unlock(&p->mu); |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 221 | GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &p->base, "destroy_subchannels"); |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 222 | |
| 223 | for (i = 0; i < num_subchannels; i++) { |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 224 | GRPC_SUBCHANNEL_UNREF(exec_ctx, subchannels[i], "pick_first"); |
| 225 | } |
| 226 | |
| 227 | gpr_free(subchannels); |
| 228 | } |
| 229 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 230 | static void pf_connectivity_changed(grpc_exec_ctx *exec_ctx, void *arg, |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 231 | bool iomgr_success) { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 232 | pick_first_lb_policy *p = arg; |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 233 | grpc_subchannel *selected_subchannel; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 234 | pending_pick *pp; |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 235 | grpc_connected_subchannel *selected; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 236 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 237 | gpr_mu_lock(&p->mu); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 238 | |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 239 | selected = GET_SELECTED(p); |
| 240 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 241 | if (p->shutdown) { |
| 242 | gpr_mu_unlock(&p->mu); |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 243 | GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &p->base, "pick_first_connectivity"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 244 | return; |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 245 | } else if (selected != NULL) { |
Craig Tiller | cb2609f | 2015-11-24 17:19:19 -0800 | [diff] [blame] | 246 | if (p->checking_connectivity == GRPC_CHANNEL_TRANSIENT_FAILURE) { |
| 247 | /* if the selected channel goes bad, we're done */ |
| 248 | p->checking_connectivity = GRPC_CHANNEL_FATAL_FAILURE; |
| 249 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 250 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
| 251 | p->checking_connectivity, "selected_changed"); |
| 252 | if (p->checking_connectivity != GRPC_CHANNEL_FATAL_FAILURE) { |
Craig Tiller | ab33b48 | 2015-11-21 08:11:04 -0800 | [diff] [blame] | 253 | grpc_connected_subchannel_notify_on_state_change( |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 254 | exec_ctx, selected, p->base.interested_parties, |
Craig Tiller | a6bebf4 | 2015-12-01 17:02:35 -0800 | [diff] [blame] | 255 | &p->checking_connectivity, &p->connectivity_changed); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 256 | } else { |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 257 | GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &p->base, "pick_first_connectivity"); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 258 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 259 | } else { |
| 260 | loop: |
| 261 | switch (p->checking_connectivity) { |
| 262 | case GRPC_CHANNEL_READY: |
| 263 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
| 264 | GRPC_CHANNEL_READY, "connecting_ready"); |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 265 | selected_subchannel = p->subchannels[p->checking_subchannel]; |
Craig Tiller | 81afdda | 2016-01-11 17:09:18 -0800 | [diff] [blame] | 266 | selected = |
| 267 | grpc_subchannel_get_connected_subchannel(selected_subchannel); |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 268 | GPR_ASSERT(selected != NULL); |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 269 | GRPC_CONNECTED_SUBCHANNEL_REF(selected, "picked_first"); |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 270 | /* drop the pick list: we are connected now */ |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 271 | GRPC_LB_POLICY_WEAK_REF(&p->base, "destroy_subchannels"); |
Craig Tiller | d9d474a | 2016-01-26 06:50:51 -0800 | [diff] [blame] | 272 | gpr_atm_rel_store(&p->selected, (gpr_atm)selected); |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 273 | grpc_exec_ctx_enqueue( |
| 274 | exec_ctx, grpc_closure_create(destroy_subchannels, p), true, NULL); |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 275 | /* update any calls that were waiting for a pick */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 276 | while ((pp = p->pending_picks)) { |
| 277 | p->pending_picks = pp->next; |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 278 | *pp->target = selected; |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 279 | grpc_pollset_set_del_pollset(exec_ctx, p->base.interested_parties, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 280 | pp->pollset); |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 281 | grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, true, NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 282 | gpr_free(pp); |
| 283 | } |
Craig Tiller | ab33b48 | 2015-11-21 08:11:04 -0800 | [diff] [blame] | 284 | grpc_connected_subchannel_notify_on_state_change( |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 285 | exec_ctx, selected, p->base.interested_parties, |
Craig Tiller | a6bebf4 | 2015-12-01 17:02:35 -0800 | [diff] [blame] | 286 | &p->checking_connectivity, &p->connectivity_changed); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 287 | break; |
| 288 | case GRPC_CHANNEL_TRANSIENT_FAILURE: |
| 289 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
| 290 | GRPC_CHANNEL_TRANSIENT_FAILURE, |
| 291 | "connecting_transient_failure"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 292 | p->checking_subchannel = |
| 293 | (p->checking_subchannel + 1) % p->num_subchannels; |
| 294 | p->checking_connectivity = grpc_subchannel_check_connectivity( |
| 295 | p->subchannels[p->checking_subchannel]); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 296 | if (p->checking_connectivity == GRPC_CHANNEL_TRANSIENT_FAILURE) { |
| 297 | grpc_subchannel_notify_on_state_change( |
| 298 | exec_ctx, p->subchannels[p->checking_subchannel], |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 299 | p->base.interested_parties, &p->checking_connectivity, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 300 | &p->connectivity_changed); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 301 | } else { |
| 302 | goto loop; |
| 303 | } |
| 304 | break; |
| 305 | case GRPC_CHANNEL_CONNECTING: |
| 306 | case GRPC_CHANNEL_IDLE: |
| 307 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
| 308 | GRPC_CHANNEL_CONNECTING, |
| 309 | "connecting_changed"); |
| 310 | grpc_subchannel_notify_on_state_change( |
| 311 | exec_ctx, p->subchannels[p->checking_subchannel], |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 312 | p->base.interested_parties, &p->checking_connectivity, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 313 | &p->connectivity_changed); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 314 | break; |
| 315 | case GRPC_CHANNEL_FATAL_FAILURE: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 316 | p->num_subchannels--; |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 317 | GPR_SWAP(grpc_subchannel *, p->subchannels[p->checking_subchannel], |
| 318 | p->subchannels[p->num_subchannels]); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 319 | GRPC_SUBCHANNEL_UNREF(exec_ctx, p->subchannels[p->num_subchannels], |
| 320 | "pick_first"); |
| 321 | if (p->num_subchannels == 0) { |
| 322 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
| 323 | GRPC_CHANNEL_FATAL_FAILURE, |
| 324 | "no_more_channels"); |
| 325 | while ((pp = p->pending_picks)) { |
| 326 | p->pending_picks = pp->next; |
| 327 | *pp->target = NULL; |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 328 | grpc_exec_ctx_enqueue(exec_ctx, pp->on_complete, true, NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 329 | gpr_free(pp); |
| 330 | } |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 331 | GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &p->base, |
| 332 | "pick_first_connectivity"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 333 | } else { |
| 334 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
| 335 | GRPC_CHANNEL_TRANSIENT_FAILURE, |
| 336 | "subchannel_failed"); |
| 337 | p->checking_subchannel %= p->num_subchannels; |
| 338 | p->checking_connectivity = grpc_subchannel_check_connectivity( |
| 339 | p->subchannels[p->checking_subchannel]); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 340 | goto loop; |
| 341 | } |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 342 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 343 | } |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 344 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 345 | gpr_mu_unlock(&p->mu); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 348 | static grpc_connectivity_state pf_check_connectivity(grpc_exec_ctx *exec_ctx, |
| 349 | grpc_lb_policy *pol) { |
| 350 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 351 | grpc_connectivity_state st; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 352 | gpr_mu_lock(&p->mu); |
| 353 | st = grpc_connectivity_state_check(&p->state_tracker); |
| 354 | gpr_mu_unlock(&p->mu); |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 355 | return st; |
| 356 | } |
| 357 | |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 358 | static void pf_notify_on_state_change(grpc_exec_ctx *exec_ctx, |
| 359 | grpc_lb_policy *pol, |
| 360 | grpc_connectivity_state *current, |
| 361 | grpc_closure *notify) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 362 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
| 363 | gpr_mu_lock(&p->mu); |
| 364 | grpc_connectivity_state_notify_on_state_change(exec_ctx, &p->state_tracker, |
| 365 | current, notify); |
| 366 | gpr_mu_unlock(&p->mu); |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 369 | static void pf_ping_one(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, |
| 370 | grpc_closure *closure) { |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 371 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
Craig Tiller | 093193e | 2016-01-07 07:14:44 -0800 | [diff] [blame] | 372 | grpc_connected_subchannel *selected = GET_SELECTED(p); |
| 373 | if (selected) { |
| 374 | grpc_connected_subchannel_ping(exec_ctx, selected, closure); |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 375 | } else { |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 376 | grpc_exec_ctx_enqueue(exec_ctx, closure, false, NULL); |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 377 | } |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 378 | } |
| 379 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 380 | static const grpc_lb_policy_vtable pick_first_lb_policy_vtable = { |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 381 | pf_destroy, |
| 382 | pf_shutdown, |
| 383 | pf_pick, |
| 384 | pf_cancel_pick, |
| 385 | pf_ping_one, |
| 386 | pf_exit_idle, |
| 387 | pf_check_connectivity, |
| 388 | pf_notify_on_state_change}; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 389 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 390 | static void pick_first_factory_ref(grpc_lb_policy_factory *factory) {} |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 391 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 392 | static void pick_first_factory_unref(grpc_lb_policy_factory *factory) {} |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 393 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 394 | static grpc_lb_policy *create_pick_first(grpc_lb_policy_factory *factory, |
| 395 | grpc_lb_policy_args *args) { |
Craig Tiller | 1e55bd4 | 2016-03-11 09:47:43 -0800 | [diff] [blame] | 396 | if (args->num_subchannels == 0) return NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 397 | pick_first_lb_policy *p = gpr_malloc(sizeof(*p)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 398 | memset(p, 0, sizeof(*p)); |
| 399 | grpc_lb_policy_init(&p->base, &pick_first_lb_policy_vtable); |
| 400 | p->subchannels = |
| 401 | gpr_malloc(sizeof(grpc_subchannel *) * args->num_subchannels); |
David Garcia Quintas | c7705c7 | 2015-09-09 17:21:11 -0700 | [diff] [blame] | 402 | p->num_subchannels = args->num_subchannels; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 403 | grpc_connectivity_state_init(&p->state_tracker, GRPC_CHANNEL_IDLE, |
| 404 | "pick_first"); |
| 405 | memcpy(p->subchannels, args->subchannels, |
| 406 | sizeof(grpc_subchannel *) * args->num_subchannels); |
| 407 | grpc_closure_init(&p->connectivity_changed, pf_connectivity_changed, p); |
| 408 | gpr_mu_init(&p->mu); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 409 | return &p->base; |
| 410 | } |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 411 | |
| 412 | static const grpc_lb_policy_factory_vtable pick_first_factory_vtable = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 413 | pick_first_factory_ref, pick_first_factory_unref, create_pick_first, |
| 414 | "pick_first"}; |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 415 | |
| 416 | static grpc_lb_policy_factory pick_first_lb_policy_factory = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 417 | &pick_first_factory_vtable}; |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 418 | |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 419 | static grpc_lb_policy_factory *pick_first_lb_factory_create() { |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 420 | return &pick_first_lb_policy_factory; |
| 421 | } |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 422 | |
| 423 | /* Plugin registration */ |
| 424 | |
| 425 | void grpc_lb_policy_pick_first_init() { |
Craig Tiller | 3113ef4 | 2016-03-29 09:03:14 -0700 | [diff] [blame^] | 426 | grpc_register_lb_policy(pick_first_lb_factory_create()); |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | void grpc_lb_policy_pick_first_shutdown() {} |