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> |
Mark D. Roth | 5bd7be0 | 2016-10-21 14:19:50 -0700 | [diff] [blame] | 37 | |
Mark D. Roth | 2137cd8 | 2016-09-14 09:04:00 -0700 | [diff] [blame] | 38 | #include "src/core/ext/client_channel/lb_policy_registry.h" |
Mark D. Roth | 557c990 | 2016-10-24 11:12:05 -0700 | [diff] [blame^] | 39 | #include "src/core/lib/channel/channel_args.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 40 | #include "src/core/lib/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; |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 44 | uint32_t initial_metadata_flags; |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 45 | grpc_connected_subchannel **target; |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 46 | grpc_closure *on_complete; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 47 | } pending_pick; |
| 48 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 49 | typedef struct { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 50 | /** base policy: must be first */ |
| 51 | grpc_lb_policy base; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 52 | /** all our subchannels */ |
| 53 | grpc_subchannel **subchannels; |
| 54 | size_t num_subchannels; |
| 55 | |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 56 | grpc_closure connectivity_changed; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 57 | |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 58 | /** the selected channel (a grpc_connected_subchannel) */ |
| 59 | gpr_atm selected; |
Craig Tiller | 320bee0 | 2016-01-06 17:33:45 -0800 | [diff] [blame] | 60 | |
| 61 | /** mutex protecting remaining members */ |
| 62 | gpr_mu mu; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 63 | /** have we started picking? */ |
| 64 | int started_picking; |
Craig Tiller | a14215a | 2015-07-17 17:21:08 -0700 | [diff] [blame] | 65 | /** are we shut down? */ |
| 66 | int shutdown; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 67 | /** which subchannel are we watching? */ |
| 68 | size_t checking_subchannel; |
| 69 | /** what is the connectivity of that channel? */ |
| 70 | grpc_connectivity_state checking_connectivity; |
| 71 | /** list of picks that are waiting on connectivity */ |
| 72 | pending_pick *pending_picks; |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 73 | |
| 74 | /** our connectivity state tracker */ |
| 75 | grpc_connectivity_state_tracker state_tracker; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 76 | } pick_first_lb_policy; |
| 77 | |
Craig Tiller | 81afdda | 2016-01-11 17:09:18 -0800 | [diff] [blame] | 78 | #define GET_SELECTED(p) \ |
Craig Tiller | d9d474a | 2016-01-26 06:50:51 -0800 | [diff] [blame] | 79 | ((grpc_connected_subchannel *)gpr_atm_acq_load(&(p)->selected)) |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 80 | |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 81 | 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] | 82 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 83 | grpc_connected_subchannel *selected = GET_SELECTED(p); |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 84 | size_t i; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 85 | GPR_ASSERT(p->pending_picks == NULL); |
| 86 | for (i = 0; i < p->num_subchannels; i++) { |
| 87 | GRPC_SUBCHANNEL_UNREF(exec_ctx, p->subchannels[i], "pick_first"); |
| 88 | } |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 89 | if (selected != NULL) { |
| 90 | GRPC_CONNECTED_SUBCHANNEL_UNREF(exec_ctx, selected, "picked_first"); |
Craig Tiller | 89a768e | 2015-10-06 09:55:59 -0700 | [diff] [blame] | 91 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 92 | grpc_connectivity_state_destroy(exec_ctx, &p->state_tracker); |
| 93 | gpr_free(p->subchannels); |
| 94 | gpr_mu_destroy(&p->mu); |
| 95 | gpr_free(p); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 98 | 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] | 99 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
Craig Tiller | d2cc459 | 2015-07-01 07:50:47 -0700 | [diff] [blame] | 100 | pending_pick *pp; |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 101 | grpc_connected_subchannel *selected; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 102 | gpr_mu_lock(&p->mu); |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 103 | selected = GET_SELECTED(p); |
Craig Tiller | a14215a | 2015-07-17 17:21:08 -0700 | [diff] [blame] | 104 | p->shutdown = 1; |
Craig Tiller | 5795da7 | 2015-09-17 15:27:13 -0700 | [diff] [blame] | 105 | pp = p->pending_picks; |
| 106 | p->pending_picks = NULL; |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 107 | grpc_connectivity_state_set( |
Craig Tiller | d925c93 | 2016-06-06 08:38:50 -0700 | [diff] [blame] | 108 | exec_ctx, &p->state_tracker, GRPC_CHANNEL_SHUTDOWN, |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 109 | GRPC_ERROR_CREATE("Channel shutdown"), "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 | e2b86c7 | 2016-04-14 17:34:01 -0700 | [diff] [blame] | 114 | } else if (p->num_subchannels > 0) { |
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 | 332f1b3 | 2016-05-24 13:21:21 -0700 | [diff] [blame] | 123 | grpc_exec_ctx_sched(exec_ctx, pp->on_complete, GRPC_ERROR_NONE, 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, |
Mark D. Roth | 5f84400 | 2016-09-08 08:20:53 -0700 | [diff] [blame] | 130 | grpc_connected_subchannel **target, |
| 131 | grpc_error *error) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 132 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
| 133 | pending_pick *pp; |
| 134 | gpr_mu_lock(&p->mu); |
| 135 | pp = p->pending_picks; |
| 136 | p->pending_picks = NULL; |
| 137 | while (pp != NULL) { |
| 138 | pending_pick *next = pp->next; |
| 139 | if (pp->target == target) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 140 | *target = NULL; |
Mark D. Roth | 932b10c | 2016-09-09 08:44:30 -0700 | [diff] [blame] | 141 | grpc_exec_ctx_sched( |
| 142 | exec_ctx, pp->on_complete, |
| 143 | GRPC_ERROR_CREATE_REFERENCING("Pick Cancelled", &error, 1), NULL); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 144 | gpr_free(pp); |
| 145 | } else { |
| 146 | pp->next = p->pending_picks; |
| 147 | p->pending_picks = pp; |
| 148 | } |
| 149 | pp = next; |
| 150 | } |
| 151 | gpr_mu_unlock(&p->mu); |
Mark D. Roth | 5f84400 | 2016-09-08 08:20:53 -0700 | [diff] [blame] | 152 | GRPC_ERROR_UNREF(error); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 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, |
Mark D. Roth | e65ff11 | 2016-09-09 13:48:38 -0700 | [diff] [blame] | 157 | uint32_t initial_metadata_flags_eq, |
| 158 | grpc_error *error) { |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 159 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
| 160 | pending_pick *pp; |
| 161 | gpr_mu_lock(&p->mu); |
| 162 | pp = p->pending_picks; |
| 163 | p->pending_picks = NULL; |
| 164 | while (pp != NULL) { |
| 165 | pending_pick *next = pp->next; |
| 166 | if ((pp->initial_metadata_flags & initial_metadata_flags_mask) == |
| 167 | initial_metadata_flags_eq) { |
Mark D. Roth | 58f52b7 | 2016-09-09 13:55:18 -0700 | [diff] [blame] | 168 | grpc_exec_ctx_sched( |
| 169 | exec_ctx, pp->on_complete, |
| 170 | GRPC_ERROR_CREATE_REFERENCING("Pick Cancelled", &error, 1), NULL); |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 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); |
Mark D. Roth | e65ff11 | 2016-09-09 13:48:38 -0700 | [diff] [blame] | 179 | GRPC_ERROR_UNREF(error); |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 182 | 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] | 183 | p->started_picking = 1; |
| 184 | p->checking_subchannel = 0; |
| 185 | p->checking_connectivity = GRPC_CHANNEL_IDLE; |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 186 | GRPC_LB_POLICY_WEAK_REF(&p->base, "pick_first_connectivity"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 187 | grpc_subchannel_notify_on_state_change( |
| 188 | exec_ctx, p->subchannels[p->checking_subchannel], |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 189 | p->base.interested_parties, &p->checking_connectivity, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 190 | &p->connectivity_changed); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 193 | 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] | 194 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
| 195 | gpr_mu_lock(&p->mu); |
| 196 | if (!p->started_picking) { |
| 197 | start_picking(exec_ctx, p); |
| 198 | } |
| 199 | gpr_mu_unlock(&p->mu); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 202 | static int pf_pick(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, |
David Garcia Quintas | 8aace51 | 2016-08-15 14:55:12 -0700 | [diff] [blame] | 203 | const grpc_lb_policy_pick_args *pick_args, |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame] | 204 | grpc_connected_subchannel **target, void **user_data, |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 205 | grpc_closure *on_complete) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 206 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 207 | pending_pick *pp; |
Craig Tiller | 320bee0 | 2016-01-06 17:33:45 -0800 | [diff] [blame] | 208 | |
| 209 | /* Check atomically for a selected channel */ |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 210 | grpc_connected_subchannel *selected = GET_SELECTED(p); |
| 211 | if (selected != NULL) { |
| 212 | *target = selected; |
| 213 | return 1; |
| 214 | } |
Craig Tiller | 320bee0 | 2016-01-06 17:33:45 -0800 | [diff] [blame] | 215 | |
| 216 | /* No subchannel selected yet, so acquire lock and then attempt again */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 217 | gpr_mu_lock(&p->mu); |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 218 | selected = GET_SELECTED(p); |
| 219 | if (selected) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 220 | gpr_mu_unlock(&p->mu); |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 221 | *target = selected; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 222 | return 1; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 223 | } else { |
| 224 | if (!p->started_picking) { |
| 225 | start_picking(exec_ctx, p); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 226 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 227 | pp = gpr_malloc(sizeof(*pp)); |
| 228 | pp->next = p->pending_picks; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 229 | pp->target = target; |
David Garcia Quintas | 8aace51 | 2016-08-15 14:55:12 -0700 | [diff] [blame] | 230 | pp->initial_metadata_flags = pick_args->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 | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 239 | grpc_error *error) { |
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 | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 260 | grpc_error *error) { |
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 | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 266 | GRPC_ERROR_REF(error); |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 267 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 268 | gpr_mu_lock(&p->mu); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 269 | |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 270 | selected = GET_SELECTED(p); |
| 271 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 272 | if (p->shutdown) { |
| 273 | gpr_mu_unlock(&p->mu); |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 274 | GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &p->base, "pick_first_connectivity"); |
Craig Tiller | ae12593 | 2016-05-13 16:34:29 -0700 | [diff] [blame] | 275 | GRPC_ERROR_UNREF(error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 276 | return; |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 277 | } else if (selected != NULL) { |
Craig Tiller | cb2609f | 2015-11-24 17:19:19 -0800 | [diff] [blame] | 278 | if (p->checking_connectivity == GRPC_CHANNEL_TRANSIENT_FAILURE) { |
| 279 | /* if the selected channel goes bad, we're done */ |
Craig Tiller | 48ed92e | 2016-06-02 11:07:12 -0700 | [diff] [blame] | 280 | p->checking_connectivity = GRPC_CHANNEL_SHUTDOWN; |
Craig Tiller | cb2609f | 2015-11-24 17:19:19 -0800 | [diff] [blame] | 281 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 282 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 283 | p->checking_connectivity, GRPC_ERROR_REF(error), |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 284 | "selected_changed"); |
Craig Tiller | 48ed92e | 2016-06-02 11:07:12 -0700 | [diff] [blame] | 285 | if (p->checking_connectivity != GRPC_CHANNEL_SHUTDOWN) { |
Craig Tiller | ab33b48 | 2015-11-21 08:11:04 -0800 | [diff] [blame] | 286 | grpc_connected_subchannel_notify_on_state_change( |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 287 | exec_ctx, selected, p->base.interested_parties, |
Craig Tiller | a6bebf4 | 2015-12-01 17:02:35 -0800 | [diff] [blame] | 288 | &p->checking_connectivity, &p->connectivity_changed); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 289 | } else { |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 290 | GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &p->base, "pick_first_connectivity"); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 291 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 292 | } else { |
| 293 | loop: |
| 294 | switch (p->checking_connectivity) { |
| 295 | case GRPC_CHANNEL_READY: |
| 296 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 297 | GRPC_CHANNEL_READY, GRPC_ERROR_NONE, |
| 298 | "connecting_ready"); |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 299 | selected_subchannel = p->subchannels[p->checking_subchannel]; |
Craig Tiller | 81afdda | 2016-01-11 17:09:18 -0800 | [diff] [blame] | 300 | selected = |
| 301 | grpc_subchannel_get_connected_subchannel(selected_subchannel); |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 302 | GPR_ASSERT(selected != NULL); |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 303 | GRPC_CONNECTED_SUBCHANNEL_REF(selected, "picked_first"); |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 304 | /* drop the pick list: we are connected now */ |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 305 | GRPC_LB_POLICY_WEAK_REF(&p->base, "destroy_subchannels"); |
Craig Tiller | d9d474a | 2016-01-26 06:50:51 -0800 | [diff] [blame] | 306 | gpr_atm_rel_store(&p->selected, (gpr_atm)selected); |
Craig Tiller | 332f1b3 | 2016-05-24 13:21:21 -0700 | [diff] [blame] | 307 | grpc_exec_ctx_sched(exec_ctx, |
| 308 | grpc_closure_create(destroy_subchannels, p), |
| 309 | GRPC_ERROR_NONE, NULL); |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 310 | /* update any calls that were waiting for a pick */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 311 | while ((pp = p->pending_picks)) { |
| 312 | p->pending_picks = pp->next; |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 313 | *pp->target = selected; |
Craig Tiller | 332f1b3 | 2016-05-24 13:21:21 -0700 | [diff] [blame] | 314 | grpc_exec_ctx_sched(exec_ctx, pp->on_complete, GRPC_ERROR_NONE, NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 315 | gpr_free(pp); |
| 316 | } |
Craig Tiller | ab33b48 | 2015-11-21 08:11:04 -0800 | [diff] [blame] | 317 | grpc_connected_subchannel_notify_on_state_change( |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 318 | exec_ctx, selected, p->base.interested_parties, |
Craig Tiller | a6bebf4 | 2015-12-01 17:02:35 -0800 | [diff] [blame] | 319 | &p->checking_connectivity, &p->connectivity_changed); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 320 | break; |
| 321 | case GRPC_CHANNEL_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; |
Craig Tiller | 131b6de | 2016-03-31 17:05:28 -0700 | [diff] [blame] | 324 | if (p->checking_subchannel == 0) { |
| 325 | /* only trigger transient failure when we've tried all alternatives */ |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 326 | grpc_connectivity_state_set( |
| 327 | exec_ctx, &p->state_tracker, GRPC_CHANNEL_TRANSIENT_FAILURE, |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 328 | GRPC_ERROR_REF(error), "connecting_transient_failure"); |
Craig Tiller | 131b6de | 2016-03-31 17:05:28 -0700 | [diff] [blame] | 329 | } |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 330 | GRPC_ERROR_UNREF(error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 331 | p->checking_connectivity = grpc_subchannel_check_connectivity( |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 332 | p->subchannels[p->checking_subchannel], &error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 333 | if (p->checking_connectivity == GRPC_CHANNEL_TRANSIENT_FAILURE) { |
| 334 | grpc_subchannel_notify_on_state_change( |
| 335 | exec_ctx, p->subchannels[p->checking_subchannel], |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 336 | p->base.interested_parties, &p->checking_connectivity, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 337 | &p->connectivity_changed); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 338 | } else { |
| 339 | goto loop; |
| 340 | } |
| 341 | break; |
| 342 | case GRPC_CHANNEL_CONNECTING: |
| 343 | case GRPC_CHANNEL_IDLE: |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 344 | grpc_connectivity_state_set( |
| 345 | exec_ctx, &p->state_tracker, GRPC_CHANNEL_CONNECTING, |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 346 | GRPC_ERROR_REF(error), "connecting_changed"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 347 | grpc_subchannel_notify_on_state_change( |
| 348 | exec_ctx, p->subchannels[p->checking_subchannel], |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 349 | p->base.interested_parties, &p->checking_connectivity, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 350 | &p->connectivity_changed); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 351 | break; |
Craig Tiller | 48ed92e | 2016-06-02 11:07:12 -0700 | [diff] [blame] | 352 | case GRPC_CHANNEL_SHUTDOWN: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 353 | p->num_subchannels--; |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 354 | GPR_SWAP(grpc_subchannel *, p->subchannels[p->checking_subchannel], |
| 355 | p->subchannels[p->num_subchannels]); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 356 | GRPC_SUBCHANNEL_UNREF(exec_ctx, p->subchannels[p->num_subchannels], |
| 357 | "pick_first"); |
| 358 | if (p->num_subchannels == 0) { |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 359 | grpc_connectivity_state_set( |
Craig Tiller | d925c93 | 2016-06-06 08:38:50 -0700 | [diff] [blame] | 360 | exec_ctx, &p->state_tracker, GRPC_CHANNEL_SHUTDOWN, |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 361 | GRPC_ERROR_CREATE_REFERENCING("Pick first exhausted channels", |
| 362 | &error, 1), |
| 363 | "no_more_channels"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 364 | while ((pp = p->pending_picks)) { |
| 365 | p->pending_picks = pp->next; |
| 366 | *pp->target = NULL; |
Craig Tiller | 332f1b3 | 2016-05-24 13:21:21 -0700 | [diff] [blame] | 367 | grpc_exec_ctx_sched(exec_ctx, pp->on_complete, GRPC_ERROR_NONE, |
| 368 | NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 369 | gpr_free(pp); |
| 370 | } |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 371 | GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &p->base, |
| 372 | "pick_first_connectivity"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 373 | } else { |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 374 | grpc_connectivity_state_set( |
| 375 | exec_ctx, &p->state_tracker, GRPC_CHANNEL_TRANSIENT_FAILURE, |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 376 | GRPC_ERROR_REF(error), "subchannel_failed"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 377 | p->checking_subchannel %= p->num_subchannels; |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 378 | GRPC_ERROR_UNREF(error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 379 | p->checking_connectivity = grpc_subchannel_check_connectivity( |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 380 | p->subchannels[p->checking_subchannel], &error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 381 | goto loop; |
| 382 | } |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 383 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 384 | } |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 385 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 386 | gpr_mu_unlock(&p->mu); |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 387 | |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 388 | GRPC_ERROR_UNREF(error); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 391 | static grpc_connectivity_state pf_check_connectivity(grpc_exec_ctx *exec_ctx, |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 392 | grpc_lb_policy *pol, |
| 393 | grpc_error **error) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 394 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 395 | grpc_connectivity_state st; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 396 | gpr_mu_lock(&p->mu); |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 397 | st = grpc_connectivity_state_check(&p->state_tracker, error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 398 | gpr_mu_unlock(&p->mu); |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 399 | return st; |
| 400 | } |
| 401 | |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 402 | static void pf_notify_on_state_change(grpc_exec_ctx *exec_ctx, |
| 403 | grpc_lb_policy *pol, |
| 404 | grpc_connectivity_state *current, |
| 405 | grpc_closure *notify) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 406 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
| 407 | gpr_mu_lock(&p->mu); |
| 408 | grpc_connectivity_state_notify_on_state_change(exec_ctx, &p->state_tracker, |
| 409 | current, notify); |
| 410 | gpr_mu_unlock(&p->mu); |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 413 | static void pf_ping_one(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, |
| 414 | grpc_closure *closure) { |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 415 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
Craig Tiller | 093193e | 2016-01-07 07:14:44 -0800 | [diff] [blame] | 416 | grpc_connected_subchannel *selected = GET_SELECTED(p); |
| 417 | if (selected) { |
| 418 | grpc_connected_subchannel_ping(exec_ctx, selected, closure); |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 419 | } else { |
Craig Tiller | 332f1b3 | 2016-05-24 13:21:21 -0700 | [diff] [blame] | 420 | grpc_exec_ctx_sched(exec_ctx, closure, GRPC_ERROR_CREATE("Not connected"), |
| 421 | NULL); |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 422 | } |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 423 | } |
| 424 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 425 | static const grpc_lb_policy_vtable pick_first_lb_policy_vtable = { |
Craig Tiller | c5ff781 | 2016-03-28 12:45:55 -0700 | [diff] [blame] | 426 | pf_destroy, pf_shutdown, pf_pick, |
| 427 | pf_cancel_pick, pf_cancel_picks, pf_ping_one, |
| 428 | pf_exit_idle, pf_check_connectivity, pf_notify_on_state_change}; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 429 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 430 | static void pick_first_factory_ref(grpc_lb_policy_factory *factory) {} |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 431 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 432 | static void pick_first_factory_unref(grpc_lb_policy_factory *factory) {} |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 433 | |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 434 | static grpc_lb_policy *create_pick_first(grpc_exec_ctx *exec_ctx, |
| 435 | grpc_lb_policy_factory *factory, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 436 | grpc_lb_policy_args *args) { |
David Garcia Quintas | 86fcfcc | 2016-03-31 23:22:28 -0700 | [diff] [blame] | 437 | GPR_ASSERT(args->client_channel_factory != NULL); |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 438 | |
Mark D. Roth | 5bd7be0 | 2016-10-21 14:19:50 -0700 | [diff] [blame] | 439 | /* Get server name. */ |
Mark D. Roth | 557c990 | 2016-10-24 11:12:05 -0700 | [diff] [blame^] | 440 | const grpc_arg *arg = |
Mark D. Roth | 5bd7be0 | 2016-10-21 14:19:50 -0700 | [diff] [blame] | 441 | grpc_channel_args_find(args->args, GRPC_ARG_SERVER_NAME); |
Mark D. Roth | 557c990 | 2016-10-24 11:12:05 -0700 | [diff] [blame^] | 442 | const char *server_name = |
| 443 | arg != NULL && arg->type == GRPC_ARG_STRING ? arg->value.string : NULL; |
Mark D. Roth | 5bd7be0 | 2016-10-21 14:19:50 -0700 | [diff] [blame] | 444 | |
Mark D. Roth | e011b1e | 2016-09-07 08:28:00 -0700 | [diff] [blame] | 445 | /* Find the number of backend addresses. We ignore balancer |
| 446 | * addresses, since we don't know how to handle them. */ |
Mark D. Roth | 5bd7be0 | 2016-10-21 14:19:50 -0700 | [diff] [blame] | 447 | arg = grpc_channel_args_find(args->args, GRPC_ARG_LB_ADDRESSES); |
| 448 | GPR_ASSERT(arg != NULL && arg->type == GRPC_ARG_POINTER); |
Mark D. Roth | 557c990 | 2016-10-24 11:12:05 -0700 | [diff] [blame^] | 449 | grpc_lb_addresses *addresses = arg->value.pointer.p; |
Mark D. Roth | f655c85 | 2016-09-06 10:40:38 -0700 | [diff] [blame] | 450 | size_t num_addrs = 0; |
Mark D. Roth | 5bd7be0 | 2016-10-21 14:19:50 -0700 | [diff] [blame] | 451 | for (size_t i = 0; i < addresses->num_addresses; i++) { |
| 452 | if (!addresses->addresses[i].is_balancer) ++num_addrs; |
Mark D. Roth | f655c85 | 2016-09-06 10:40:38 -0700 | [diff] [blame] | 453 | } |
| 454 | if (num_addrs == 0) return NULL; |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 455 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 456 | pick_first_lb_policy *p = gpr_malloc(sizeof(*p)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 457 | memset(p, 0, sizeof(*p)); |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 458 | |
Mark D. Roth | 989cdcd | 2016-09-06 13:28:28 -0700 | [diff] [blame] | 459 | p->subchannels = gpr_malloc(sizeof(grpc_subchannel *) * num_addrs); |
Mark D. Roth | f655c85 | 2016-09-06 10:40:38 -0700 | [diff] [blame] | 460 | memset(p->subchannels, 0, sizeof(*p->subchannels) * num_addrs); |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 461 | grpc_subchannel_args sc_args; |
| 462 | size_t subchannel_idx = 0; |
Mark D. Roth | 5bd7be0 | 2016-10-21 14:19:50 -0700 | [diff] [blame] | 463 | for (size_t i = 0; i < addresses->num_addresses; i++) { |
Mark D. Roth | e011b1e | 2016-09-07 08:28:00 -0700 | [diff] [blame] | 464 | /* Skip balancer addresses, since we only know how to handle backends. */ |
Mark D. Roth | 5bd7be0 | 2016-10-21 14:19:50 -0700 | [diff] [blame] | 465 | if (addresses->addresses[i].is_balancer) continue; |
Mark D. Roth | f655c85 | 2016-09-06 10:40:38 -0700 | [diff] [blame] | 466 | |
Mark D. Roth | 5bd7be0 | 2016-10-21 14:19:50 -0700 | [diff] [blame] | 467 | if (addresses->addresses[i].user_data != NULL) { |
David Garcia Quintas | 5ebb7af | 2016-09-15 10:02:16 -0700 | [diff] [blame] | 468 | gpr_log(GPR_ERROR, |
| 469 | "This LB policy doesn't support user data. It will be ignored"); |
| 470 | } |
David Garcia Quintas | f47d6fb | 2016-09-14 12:59:17 -0700 | [diff] [blame] | 471 | |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 472 | memset(&sc_args, 0, sizeof(grpc_subchannel_args)); |
David Garcia Quintas | 129bb46 | 2016-09-28 09:50:23 -0700 | [diff] [blame] | 473 | /* server_name will be copied as part of the subchannel creation. This makes |
Mark D. Roth | 5bd7be0 | 2016-10-21 14:19:50 -0700 | [diff] [blame] | 474 | * the copying of server_name (a borrowed pointer) OK. */ |
| 475 | sc_args.server_name = server_name; |
Mark D. Roth | 557c990 | 2016-10-24 11:12:05 -0700 | [diff] [blame^] | 476 | sc_args.addr = (struct sockaddr *)(&addresses->addresses[i].address.addr); |
Mark D. Roth | 5bd7be0 | 2016-10-21 14:19:50 -0700 | [diff] [blame] | 477 | sc_args.addr_len = addresses->addresses[i].address.len; |
Mark D. Roth | 98abfd3 | 2016-10-21 08:10:51 -0700 | [diff] [blame] | 478 | sc_args.args = args->args; |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 479 | |
David Garcia Quintas | 86fcfcc | 2016-03-31 23:22:28 -0700 | [diff] [blame] | 480 | grpc_subchannel *subchannel = grpc_client_channel_factory_create_subchannel( |
| 481 | exec_ctx, args->client_channel_factory, &sc_args); |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 482 | |
| 483 | if (subchannel != NULL) { |
| 484 | p->subchannels[subchannel_idx++] = subchannel; |
| 485 | } |
| 486 | } |
| 487 | if (subchannel_idx == 0) { |
| 488 | gpr_free(p->subchannels); |
| 489 | gpr_free(p); |
| 490 | return NULL; |
| 491 | } |
| 492 | p->num_subchannels = subchannel_idx; |
| 493 | |
| 494 | grpc_lb_policy_init(&p->base, &pick_first_lb_policy_vtable); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 495 | grpc_closure_init(&p->connectivity_changed, pf_connectivity_changed, p); |
| 496 | gpr_mu_init(&p->mu); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 497 | return &p->base; |
| 498 | } |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 499 | |
| 500 | static const grpc_lb_policy_factory_vtable pick_first_factory_vtable = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 501 | pick_first_factory_ref, pick_first_factory_unref, create_pick_first, |
| 502 | "pick_first"}; |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 503 | |
| 504 | static grpc_lb_policy_factory pick_first_lb_policy_factory = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 505 | &pick_first_factory_vtable}; |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 506 | |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 507 | static grpc_lb_policy_factory *pick_first_lb_factory_create() { |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 508 | return &pick_first_lb_policy_factory; |
| 509 | } |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 510 | |
| 511 | /* Plugin registration */ |
| 512 | |
| 513 | void grpc_lb_policy_pick_first_init() { |
Craig Tiller | 3113ef4 | 2016-03-29 09:03:14 -0700 | [diff] [blame] | 514 | grpc_register_lb_policy(pick_first_lb_factory_create()); |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | void grpc_lb_policy_pick_first_shutdown() {} |