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 | 0748f39 | 2017-01-13 09:22:44 -0800 | [diff] [blame] | 39 | #include "src/core/ext/client_channel/subchannel.h" |
Mark D. Roth | 557c990 | 2016-10-24 11:12:05 -0700 | [diff] [blame] | 40 | #include "src/core/lib/channel/channel_args.h" |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 41 | #include "src/core/lib/iomgr/combiner.h" |
Mark D. Roth | 0748f39 | 2017-01-13 09:22:44 -0800 | [diff] [blame] | 42 | #include "src/core/lib/iomgr/sockaddr_utils.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 43 | #include "src/core/lib/transport/connectivity_state.h" |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 44 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 45 | typedef struct pending_pick { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 46 | struct pending_pick *next; |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 47 | uint32_t initial_metadata_flags; |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 48 | grpc_connected_subchannel **target; |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 49 | grpc_closure *on_complete; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 50 | } pending_pick; |
| 51 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 52 | typedef struct { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 53 | /** base policy: must be first */ |
| 54 | grpc_lb_policy base; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 55 | /** all our subchannels */ |
| 56 | grpc_subchannel **subchannels; |
| 57 | size_t num_subchannels; |
| 58 | |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 59 | grpc_closure connectivity_changed; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 60 | |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 61 | /** remaining members are protected by the combiner */ |
Craig Tiller | 320bee0 | 2016-01-06 17:33:45 -0800 | [diff] [blame] | 62 | |
Craig Tiller | 46dd790 | 2017-02-23 09:42:16 -0800 | [diff] [blame] | 63 | /** the selected channel */ |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 64 | grpc_connected_subchannel *selected; |
| 65 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 66 | /** have we started picking? */ |
| 67 | int started_picking; |
Craig Tiller | a14215a | 2015-07-17 17:21:08 -0700 | [diff] [blame] | 68 | /** are we shut down? */ |
| 69 | int shutdown; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 70 | /** which subchannel are we watching? */ |
| 71 | size_t checking_subchannel; |
| 72 | /** what is the connectivity of that channel? */ |
| 73 | grpc_connectivity_state checking_connectivity; |
| 74 | /** list of picks that are waiting on connectivity */ |
| 75 | pending_pick *pending_picks; |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 76 | |
| 77 | /** our connectivity state tracker */ |
| 78 | grpc_connectivity_state_tracker state_tracker; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 79 | } pick_first_lb_policy; |
| 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 | 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 | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 88 | if (p->selected != NULL) { |
| 89 | GRPC_CONNECTED_SUBCHANNEL_UNREF(exec_ctx, p->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); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 93 | gpr_free(p); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 96 | static void pf_shutdown_locked(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 | a14215a | 2015-07-17 17:21:08 -0700 | [diff] [blame] | 99 | p->shutdown = 1; |
Craig Tiller | 5795da7 | 2015-09-17 15:27:13 -0700 | [diff] [blame] | 100 | pp = p->pending_picks; |
| 101 | p->pending_picks = NULL; |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 102 | grpc_connectivity_state_set( |
Craig Tiller | d925c93 | 2016-06-06 08:38:50 -0700 | [diff] [blame] | 103 | exec_ctx, &p->state_tracker, GRPC_CHANNEL_SHUTDOWN, |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 104 | GRPC_ERROR_CREATE("Channel shutdown"), "shutdown"); |
Craig Tiller | f036a64 | 2015-12-01 17:00:40 -0800 | [diff] [blame] | 105 | /* cancel subscription */ |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 106 | if (p->selected != NULL) { |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 107 | grpc_connected_subchannel_notify_on_state_change( |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 108 | exec_ctx, p->selected, NULL, NULL, &p->connectivity_changed); |
Craig Tiller | e2b86c7 | 2016-04-14 17:34:01 -0700 | [diff] [blame] | 109 | } else if (p->num_subchannels > 0) { |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 110 | grpc_subchannel_notify_on_state_change( |
| 111 | exec_ctx, p->subchannels[p->checking_subchannel], NULL, NULL, |
| 112 | &p->connectivity_changed); |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 113 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 114 | while (pp != NULL) { |
| 115 | pending_pick *next = pp->next; |
| 116 | *pp->target = NULL; |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 117 | grpc_closure_sched(exec_ctx, pp->on_complete, GRPC_ERROR_NONE); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 118 | gpr_free(pp); |
| 119 | pp = next; |
| 120 | } |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 123 | static void pf_cancel_pick_locked(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, |
| 124 | grpc_connected_subchannel **target, |
| 125 | grpc_error *error) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 126 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
| 127 | pending_pick *pp; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 128 | pp = p->pending_picks; |
| 129 | p->pending_picks = NULL; |
| 130 | while (pp != NULL) { |
| 131 | pending_pick *next = pp->next; |
| 132 | if (pp->target == target) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 133 | *target = NULL; |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 134 | grpc_closure_sched( |
Mark D. Roth | 932b10c | 2016-09-09 08:44:30 -0700 | [diff] [blame] | 135 | exec_ctx, pp->on_complete, |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 136 | GRPC_ERROR_CREATE_REFERENCING("Pick Cancelled", &error, 1)); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 137 | gpr_free(pp); |
| 138 | } else { |
| 139 | pp->next = p->pending_picks; |
| 140 | p->pending_picks = pp; |
| 141 | } |
| 142 | pp = next; |
| 143 | } |
Mark D. Roth | 5f84400 | 2016-09-08 08:20:53 -0700 | [diff] [blame] | 144 | GRPC_ERROR_UNREF(error); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 147 | static void pf_cancel_picks_locked(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, |
| 148 | uint32_t initial_metadata_flags_mask, |
| 149 | uint32_t initial_metadata_flags_eq, |
| 150 | grpc_error *error) { |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 151 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
| 152 | pending_pick *pp; |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 153 | pp = p->pending_picks; |
| 154 | p->pending_picks = NULL; |
| 155 | while (pp != NULL) { |
| 156 | pending_pick *next = pp->next; |
| 157 | if ((pp->initial_metadata_flags & initial_metadata_flags_mask) == |
| 158 | initial_metadata_flags_eq) { |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 159 | grpc_closure_sched( |
Mark D. Roth | 58f52b7 | 2016-09-09 13:55:18 -0700 | [diff] [blame] | 160 | exec_ctx, pp->on_complete, |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 161 | GRPC_ERROR_CREATE_REFERENCING("Pick Cancelled", &error, 1)); |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 162 | gpr_free(pp); |
| 163 | } else { |
| 164 | pp->next = p->pending_picks; |
| 165 | p->pending_picks = pp; |
| 166 | } |
| 167 | pp = next; |
| 168 | } |
Mark D. Roth | e65ff11 | 2016-09-09 13:48:38 -0700 | [diff] [blame] | 169 | GRPC_ERROR_UNREF(error); |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 172 | 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] | 173 | p->started_picking = 1; |
| 174 | p->checking_subchannel = 0; |
| 175 | p->checking_connectivity = GRPC_CHANNEL_IDLE; |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 176 | GRPC_LB_POLICY_WEAK_REF(&p->base, "pick_first_connectivity"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 177 | grpc_subchannel_notify_on_state_change( |
| 178 | exec_ctx, p->subchannels[p->checking_subchannel], |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 179 | p->base.interested_parties, &p->checking_connectivity, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 180 | &p->connectivity_changed); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 183 | static void pf_exit_idle_locked(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 184 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 185 | if (!p->started_picking) { |
| 186 | start_picking(exec_ctx, p); |
| 187 | } |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 190 | static int pf_pick_locked(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, |
| 191 | const grpc_lb_policy_pick_args *pick_args, |
| 192 | grpc_connected_subchannel **target, void **user_data, |
| 193 | grpc_closure *on_complete) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 194 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 195 | pending_pick *pp; |
Craig Tiller | 320bee0 | 2016-01-06 17:33:45 -0800 | [diff] [blame] | 196 | |
| 197 | /* Check atomically for a selected channel */ |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 198 | if (p->selected != NULL) { |
| 199 | *target = GRPC_CONNECTED_SUBCHANNEL_REF(p->selected, "picked"); |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 200 | return 1; |
| 201 | } |
Craig Tiller | 320bee0 | 2016-01-06 17:33:45 -0800 | [diff] [blame] | 202 | |
Craig Tiller | 46dd790 | 2017-02-23 09:42:16 -0800 | [diff] [blame] | 203 | /* No subchannel selected yet, so try again */ |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 204 | if (!p->started_picking) { |
| 205 | start_picking(exec_ctx, p); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 206 | } |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 207 | pp = gpr_malloc(sizeof(*pp)); |
| 208 | pp->next = p->pending_picks; |
| 209 | pp->target = target; |
| 210 | pp->initial_metadata_flags = pick_args->initial_metadata_flags; |
| 211 | pp->on_complete = on_complete; |
| 212 | p->pending_picks = pp; |
| 213 | return 0; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 216 | static void destroy_subchannels_locked(grpc_exec_ctx *exec_ctx, |
| 217 | pick_first_lb_policy *p) { |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 218 | size_t i; |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 219 | size_t num_subchannels = p->num_subchannels; |
| 220 | grpc_subchannel **subchannels; |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 221 | |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 222 | subchannels = p->subchannels; |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 223 | p->num_subchannels = 0; |
| 224 | p->subchannels = NULL; |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 225 | GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &p->base, "destroy_subchannels"); |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 226 | |
| 227 | for (i = 0; i < num_subchannels; i++) { |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 228 | GRPC_SUBCHANNEL_UNREF(exec_ctx, subchannels[i], "pick_first"); |
| 229 | } |
| 230 | |
| 231 | gpr_free(subchannels); |
| 232 | } |
| 233 | |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 234 | static void pf_connectivity_changed_locked(grpc_exec_ctx *exec_ctx, void *arg, |
| 235 | grpc_error *error) { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 236 | pick_first_lb_policy *p = arg; |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 237 | grpc_subchannel *selected_subchannel; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 238 | pending_pick *pp; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 239 | |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 240 | GRPC_ERROR_REF(error); |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 241 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 242 | if (p->shutdown) { |
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 | ae12593 | 2016-05-13 16:34:29 -0700 | [diff] [blame] | 244 | GRPC_ERROR_UNREF(error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 245 | return; |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 246 | } else if (p->selected != NULL) { |
Craig Tiller | cb2609f | 2015-11-24 17:19:19 -0800 | [diff] [blame] | 247 | if (p->checking_connectivity == GRPC_CHANNEL_TRANSIENT_FAILURE) { |
| 248 | /* if the selected channel goes bad, we're done */ |
Craig Tiller | 48ed92e | 2016-06-02 11:07:12 -0700 | [diff] [blame] | 249 | p->checking_connectivity = GRPC_CHANNEL_SHUTDOWN; |
Craig Tiller | cb2609f | 2015-11-24 17:19:19 -0800 | [diff] [blame] | 250 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 251 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 252 | p->checking_connectivity, GRPC_ERROR_REF(error), |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 253 | "selected_changed"); |
Craig Tiller | 48ed92e | 2016-06-02 11:07:12 -0700 | [diff] [blame] | 254 | if (p->checking_connectivity != GRPC_CHANNEL_SHUTDOWN) { |
Craig Tiller | ab33b48 | 2015-11-21 08:11:04 -0800 | [diff] [blame] | 255 | grpc_connected_subchannel_notify_on_state_change( |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 256 | exec_ctx, p->selected, p->base.interested_parties, |
Craig Tiller | a6bebf4 | 2015-12-01 17:02:35 -0800 | [diff] [blame] | 257 | &p->checking_connectivity, &p->connectivity_changed); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 258 | } else { |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 259 | GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &p->base, "pick_first_connectivity"); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 260 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 261 | } else { |
| 262 | loop: |
| 263 | switch (p->checking_connectivity) { |
David Garcia Quintas | ea6689d | 2016-11-08 09:46:41 -0800 | [diff] [blame] | 264 | case GRPC_CHANNEL_INIT: |
Jan Tattermusch | b0fb2d2 | 2016-11-16 14:04:05 +0100 | [diff] [blame] | 265 | GPR_UNREACHABLE_CODE(return ); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 266 | case GRPC_CHANNEL_READY: |
| 267 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 268 | GRPC_CHANNEL_READY, GRPC_ERROR_NONE, |
| 269 | "connecting_ready"); |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 270 | selected_subchannel = p->subchannels[p->checking_subchannel]; |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 271 | p->selected = GRPC_CONNECTED_SUBCHANNEL_REF( |
| 272 | grpc_subchannel_get_connected_subchannel(selected_subchannel), |
| 273 | "picked_first"); |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 274 | /* drop the pick list: we are connected now */ |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 275 | GRPC_LB_POLICY_WEAK_REF(&p->base, "destroy_subchannels"); |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 276 | destroy_subchannels_locked(exec_ctx, p); |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 277 | /* update any calls that were waiting for a pick */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 278 | while ((pp = p->pending_picks)) { |
| 279 | p->pending_picks = pp->next; |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 280 | *pp->target = GRPC_CONNECTED_SUBCHANNEL_REF(p->selected, "picked"); |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 281 | grpc_closure_sched(exec_ctx, pp->on_complete, GRPC_ERROR_NONE); |
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 | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 285 | exec_ctx, p->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: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 289 | p->checking_subchannel = |
| 290 | (p->checking_subchannel + 1) % p->num_subchannels; |
Craig Tiller | 131b6de | 2016-03-31 17:05:28 -0700 | [diff] [blame] | 291 | if (p->checking_subchannel == 0) { |
| 292 | /* only trigger transient failure when we've tried all alternatives */ |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 293 | grpc_connectivity_state_set( |
| 294 | exec_ctx, &p->state_tracker, GRPC_CHANNEL_TRANSIENT_FAILURE, |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 295 | GRPC_ERROR_REF(error), "connecting_transient_failure"); |
Craig Tiller | 131b6de | 2016-03-31 17:05:28 -0700 | [diff] [blame] | 296 | } |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 297 | GRPC_ERROR_UNREF(error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 298 | p->checking_connectivity = grpc_subchannel_check_connectivity( |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 299 | p->subchannels[p->checking_subchannel], &error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 300 | if (p->checking_connectivity == GRPC_CHANNEL_TRANSIENT_FAILURE) { |
| 301 | grpc_subchannel_notify_on_state_change( |
| 302 | exec_ctx, p->subchannels[p->checking_subchannel], |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 303 | p->base.interested_parties, &p->checking_connectivity, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 304 | &p->connectivity_changed); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 305 | } else { |
| 306 | goto loop; |
| 307 | } |
| 308 | break; |
| 309 | case GRPC_CHANNEL_CONNECTING: |
| 310 | case GRPC_CHANNEL_IDLE: |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 311 | grpc_connectivity_state_set( |
| 312 | exec_ctx, &p->state_tracker, GRPC_CHANNEL_CONNECTING, |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 313 | GRPC_ERROR_REF(error), "connecting_changed"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 314 | grpc_subchannel_notify_on_state_change( |
| 315 | exec_ctx, p->subchannels[p->checking_subchannel], |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 316 | p->base.interested_parties, &p->checking_connectivity, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 317 | &p->connectivity_changed); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 318 | break; |
Craig Tiller | 48ed92e | 2016-06-02 11:07:12 -0700 | [diff] [blame] | 319 | case GRPC_CHANNEL_SHUTDOWN: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 320 | p->num_subchannels--; |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 321 | GPR_SWAP(grpc_subchannel *, p->subchannels[p->checking_subchannel], |
| 322 | p->subchannels[p->num_subchannels]); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 323 | GRPC_SUBCHANNEL_UNREF(exec_ctx, p->subchannels[p->num_subchannels], |
| 324 | "pick_first"); |
| 325 | if (p->num_subchannels == 0) { |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 326 | grpc_connectivity_state_set( |
Craig Tiller | d925c93 | 2016-06-06 08:38:50 -0700 | [diff] [blame] | 327 | exec_ctx, &p->state_tracker, GRPC_CHANNEL_SHUTDOWN, |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 328 | GRPC_ERROR_CREATE_REFERENCING("Pick first exhausted channels", |
| 329 | &error, 1), |
| 330 | "no_more_channels"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 331 | while ((pp = p->pending_picks)) { |
| 332 | p->pending_picks = pp->next; |
| 333 | *pp->target = NULL; |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 334 | grpc_closure_sched(exec_ctx, pp->on_complete, GRPC_ERROR_NONE); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 335 | gpr_free(pp); |
| 336 | } |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 337 | GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &p->base, |
| 338 | "pick_first_connectivity"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 339 | } else { |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 340 | grpc_connectivity_state_set( |
| 341 | exec_ctx, &p->state_tracker, GRPC_CHANNEL_TRANSIENT_FAILURE, |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 342 | GRPC_ERROR_REF(error), "subchannel_failed"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 343 | p->checking_subchannel %= p->num_subchannels; |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 344 | GRPC_ERROR_UNREF(error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 345 | p->checking_connectivity = grpc_subchannel_check_connectivity( |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 346 | p->subchannels[p->checking_subchannel], &error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 347 | goto loop; |
| 348 | } |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 349 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 350 | } |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 351 | |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 352 | GRPC_ERROR_UNREF(error); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 355 | static grpc_connectivity_state pf_check_connectivity_locked( |
| 356 | grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, grpc_error **error) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 357 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 358 | return grpc_connectivity_state_get(&p->state_tracker, error); |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 361 | static void pf_notify_on_state_change_locked(grpc_exec_ctx *exec_ctx, |
| 362 | grpc_lb_policy *pol, |
| 363 | grpc_connectivity_state *current, |
| 364 | grpc_closure *notify) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 365 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 366 | grpc_connectivity_state_notify_on_state_change(exec_ctx, &p->state_tracker, |
| 367 | current, notify); |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 370 | static void pf_ping_one_locked(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, |
| 371 | grpc_closure *closure) { |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 372 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 373 | if (p->selected) { |
| 374 | grpc_connected_subchannel_ping(exec_ctx, p->selected, closure); |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 375 | } else { |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 376 | grpc_closure_sched(exec_ctx, closure, GRPC_ERROR_CREATE("Not connected")); |
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 | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 381 | pf_destroy, |
| 382 | pf_shutdown_locked, |
| 383 | pf_pick_locked, |
| 384 | pf_cancel_pick_locked, |
| 385 | pf_cancel_picks_locked, |
| 386 | pf_ping_one_locked, |
| 387 | pf_exit_idle_locked, |
| 388 | pf_check_connectivity_locked, |
| 389 | pf_notify_on_state_change_locked}; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 390 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 391 | static void pick_first_factory_ref(grpc_lb_policy_factory *factory) {} |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 392 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 393 | static void pick_first_factory_unref(grpc_lb_policy_factory *factory) {} |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 394 | |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 395 | static grpc_lb_policy *create_pick_first(grpc_exec_ctx *exec_ctx, |
| 396 | grpc_lb_policy_factory *factory, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 397 | grpc_lb_policy_args *args) { |
David Garcia Quintas | 86fcfcc | 2016-03-31 23:22:28 -0700 | [diff] [blame] | 398 | GPR_ASSERT(args->client_channel_factory != NULL); |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 399 | |
Mark D. Roth | e011b1e | 2016-09-07 08:28:00 -0700 | [diff] [blame] | 400 | /* Find the number of backend addresses. We ignore balancer |
| 401 | * addresses, since we don't know how to handle them. */ |
Mark D. Roth | 201db7d | 2016-12-12 09:36:02 -0800 | [diff] [blame] | 402 | const grpc_arg *arg = |
| 403 | grpc_channel_args_find(args->args, GRPC_ARG_LB_ADDRESSES); |
Mark D. Roth | 5bd7be0 | 2016-10-21 14:19:50 -0700 | [diff] [blame] | 404 | GPR_ASSERT(arg != NULL && arg->type == GRPC_ARG_POINTER); |
Mark D. Roth | 557c990 | 2016-10-24 11:12:05 -0700 | [diff] [blame] | 405 | grpc_lb_addresses *addresses = arg->value.pointer.p; |
Mark D. Roth | f655c85 | 2016-09-06 10:40:38 -0700 | [diff] [blame] | 406 | size_t num_addrs = 0; |
Mark D. Roth | 5bd7be0 | 2016-10-21 14:19:50 -0700 | [diff] [blame] | 407 | for (size_t i = 0; i < addresses->num_addresses; i++) { |
| 408 | if (!addresses->addresses[i].is_balancer) ++num_addrs; |
Mark D. Roth | f655c85 | 2016-09-06 10:40:38 -0700 | [diff] [blame] | 409 | } |
| 410 | if (num_addrs == 0) return NULL; |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 411 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 412 | pick_first_lb_policy *p = gpr_malloc(sizeof(*p)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 413 | memset(p, 0, sizeof(*p)); |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 414 | |
Mark D. Roth | 989cdcd | 2016-09-06 13:28:28 -0700 | [diff] [blame] | 415 | p->subchannels = gpr_malloc(sizeof(grpc_subchannel *) * num_addrs); |
Mark D. Roth | f655c85 | 2016-09-06 10:40:38 -0700 | [diff] [blame] | 416 | memset(p->subchannels, 0, sizeof(*p->subchannels) * num_addrs); |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 417 | grpc_subchannel_args sc_args; |
| 418 | size_t subchannel_idx = 0; |
Mark D. Roth | 5bd7be0 | 2016-10-21 14:19:50 -0700 | [diff] [blame] | 419 | for (size_t i = 0; i < addresses->num_addresses; i++) { |
Mark D. Roth | e011b1e | 2016-09-07 08:28:00 -0700 | [diff] [blame] | 420 | /* Skip balancer addresses, since we only know how to handle backends. */ |
Mark D. Roth | 5bd7be0 | 2016-10-21 14:19:50 -0700 | [diff] [blame] | 421 | if (addresses->addresses[i].is_balancer) continue; |
Mark D. Roth | f655c85 | 2016-09-06 10:40:38 -0700 | [diff] [blame] | 422 | |
Mark D. Roth | 5bd7be0 | 2016-10-21 14:19:50 -0700 | [diff] [blame] | 423 | if (addresses->addresses[i].user_data != NULL) { |
David Garcia Quintas | 5ebb7af | 2016-09-15 10:02:16 -0700 | [diff] [blame] | 424 | gpr_log(GPR_ERROR, |
| 425 | "This LB policy doesn't support user data. It will be ignored"); |
| 426 | } |
David Garcia Quintas | f47d6fb | 2016-09-14 12:59:17 -0700 | [diff] [blame] | 427 | |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 428 | memset(&sc_args, 0, sizeof(grpc_subchannel_args)); |
Mark D. Roth | df8f122 | 2017-01-13 22:59:39 +0000 | [diff] [blame] | 429 | grpc_arg addr_arg = |
| 430 | grpc_create_subchannel_address_arg(&addresses->addresses[i].address); |
Mark D. Roth | 0748f39 | 2017-01-13 09:22:44 -0800 | [diff] [blame] | 431 | grpc_channel_args *new_args = |
| 432 | grpc_channel_args_copy_and_add(args->args, &addr_arg, 1); |
| 433 | gpr_free(addr_arg.value.string); |
| 434 | sc_args.args = new_args; |
David Garcia Quintas | 86fcfcc | 2016-03-31 23:22:28 -0700 | [diff] [blame] | 435 | grpc_subchannel *subchannel = grpc_client_channel_factory_create_subchannel( |
| 436 | exec_ctx, args->client_channel_factory, &sc_args); |
Mark D. Roth | 0748f39 | 2017-01-13 09:22:44 -0800 | [diff] [blame] | 437 | grpc_channel_args_destroy(exec_ctx, new_args); |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 438 | |
| 439 | if (subchannel != NULL) { |
| 440 | p->subchannels[subchannel_idx++] = subchannel; |
| 441 | } |
| 442 | } |
| 443 | if (subchannel_idx == 0) { |
| 444 | gpr_free(p->subchannels); |
| 445 | gpr_free(p); |
| 446 | return NULL; |
| 447 | } |
| 448 | p->num_subchannels = subchannel_idx; |
| 449 | |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 450 | grpc_lb_policy_init(&p->base, &pick_first_lb_policy_vtable, args->combiner); |
| 451 | grpc_closure_init(&p->connectivity_changed, pf_connectivity_changed_locked, p, |
| 452 | grpc_combiner_scheduler(args->combiner, false)); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 453 | return &p->base; |
| 454 | } |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 455 | |
| 456 | static const grpc_lb_policy_factory_vtable pick_first_factory_vtable = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 457 | pick_first_factory_ref, pick_first_factory_unref, create_pick_first, |
| 458 | "pick_first"}; |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 459 | |
| 460 | static grpc_lb_policy_factory pick_first_lb_policy_factory = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 461 | &pick_first_factory_vtable}; |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 462 | |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 463 | static grpc_lb_policy_factory *pick_first_lb_factory_create() { |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 464 | return &pick_first_lb_policy_factory; |
| 465 | } |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 466 | |
| 467 | /* Plugin registration */ |
| 468 | |
| 469 | void grpc_lb_policy_pick_first_init() { |
Craig Tiller | 3113ef4 | 2016-03-29 09:03:14 -0700 | [diff] [blame] | 470 | grpc_register_lb_policy(pick_first_lb_factory_create()); |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | void grpc_lb_policy_pick_first_shutdown() {} |