Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame^] | 3 | * Copyright 2015 gRPC authors. |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame^] | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame^] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame^] | 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
Craig Tiller | 3bc8ebd | 2015-06-24 15:41:15 -0700 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 19 | #include <string.h> |
| 20 | |
| 21 | #include <grpc/support/alloc.h> |
Mark D. Roth | 5bd7be0 | 2016-10-21 14:19:50 -0700 | [diff] [blame] | 22 | |
Craig Tiller | 9eb0fde | 2017-03-31 16:59:30 -0700 | [diff] [blame] | 23 | #include "src/core/ext/filters/client_channel/lb_policy_registry.h" |
| 24 | #include "src/core/ext/filters/client_channel/subchannel.h" |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 25 | #include "src/core/ext/filters/client_channel/subchannel_index.h" |
Mark D. Roth | 557c990 | 2016-10-24 11:12:05 -0700 | [diff] [blame] | 26 | #include "src/core/lib/channel/channel_args.h" |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 27 | #include "src/core/lib/iomgr/combiner.h" |
Mark D. Roth | 0748f39 | 2017-01-13 09:22:44 -0800 | [diff] [blame] | 28 | #include "src/core/lib/iomgr/sockaddr_utils.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 29 | #include "src/core/lib/transport/connectivity_state.h" |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 30 | |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 31 | grpc_tracer_flag grpc_lb_pick_first_trace = GRPC_TRACER_INITIALIZER(false); |
| 32 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 33 | typedef struct pending_pick { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 34 | struct pending_pick *next; |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 35 | uint32_t initial_metadata_flags; |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 36 | grpc_connected_subchannel **target; |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 37 | grpc_closure *on_complete; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 38 | } pending_pick; |
| 39 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 40 | typedef struct { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 41 | /** base policy: must be first */ |
| 42 | grpc_lb_policy base; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 43 | /** all our subchannels */ |
| 44 | grpc_subchannel **subchannels; |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 45 | grpc_subchannel **new_subchannels; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 46 | size_t num_subchannels; |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 47 | size_t num_new_subchannels; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 48 | |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 49 | grpc_closure connectivity_changed; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 50 | |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 51 | /** remaining members are protected by the combiner */ |
Craig Tiller | 320bee0 | 2016-01-06 17:33:45 -0800 | [diff] [blame] | 52 | |
Craig Tiller | 46dd790 | 2017-02-23 09:42:16 -0800 | [diff] [blame] | 53 | /** the selected channel */ |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 54 | grpc_connected_subchannel *selected; |
| 55 | |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 56 | /** the subchannel key for \a selected, or NULL if \a selected not set */ |
| 57 | const grpc_subchannel_key *selected_key; |
| 58 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 59 | /** have we started picking? */ |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 60 | bool started_picking; |
Craig Tiller | a14215a | 2015-07-17 17:21:08 -0700 | [diff] [blame] | 61 | /** are we shut down? */ |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 62 | bool shutdown; |
| 63 | /** are we updating the selected subchannel? */ |
| 64 | bool updating_selected; |
| 65 | /** are we updating the subchannel candidates? */ |
| 66 | bool updating_subchannels; |
| 67 | /** args from the latest update received while already updating, or NULL */ |
| 68 | grpc_lb_policy_args *pending_update_args; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 69 | /** which subchannel are we watching? */ |
| 70 | size_t checking_subchannel; |
| 71 | /** what is the connectivity of that channel? */ |
| 72 | grpc_connectivity_state checking_connectivity; |
| 73 | /** list of picks that are waiting on connectivity */ |
| 74 | pending_pick *pending_picks; |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 75 | |
| 76 | /** our connectivity state tracker */ |
| 77 | grpc_connectivity_state_tracker state_tracker; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 78 | } pick_first_lb_policy; |
| 79 | |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 80 | static void pf_destroy(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 81 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 82 | GPR_ASSERT(p->pending_picks == NULL); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 83 | for (size_t i = 0; i < p->num_subchannels; i++) { |
| 84 | GRPC_SUBCHANNEL_UNREF(exec_ctx, p->subchannels[i], "pick_first_destroy"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 85 | } |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 86 | if (p->selected != NULL) { |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 87 | GRPC_CONNECTED_SUBCHANNEL_UNREF(exec_ctx, p->selected, |
| 88 | "picked_first_destroy"); |
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); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 91 | if (p->pending_update_args != NULL) { |
| 92 | grpc_channel_args_destroy(exec_ctx, p->pending_update_args->args); |
| 93 | gpr_free(p->pending_update_args); |
| 94 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 95 | gpr_free(p->subchannels); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 96 | gpr_free(p->new_subchannels); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 97 | gpr_free(p); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 100 | 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] | 101 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
Craig Tiller | d2cc459 | 2015-07-01 07:50:47 -0700 | [diff] [blame] | 102 | pending_pick *pp; |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 103 | p->shutdown = true; |
Craig Tiller | 5795da7 | 2015-09-17 15:27:13 -0700 | [diff] [blame] | 104 | pp = p->pending_picks; |
| 105 | p->pending_picks = NULL; |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 106 | grpc_connectivity_state_set( |
Craig Tiller | d925c93 | 2016-06-06 08:38:50 -0700 | [diff] [blame] | 107 | exec_ctx, &p->state_tracker, GRPC_CHANNEL_SHUTDOWN, |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 108 | GRPC_ERROR_CREATE_FROM_STATIC_STRING("Channel shutdown"), "shutdown"); |
Craig Tiller | f036a64 | 2015-12-01 17:00:40 -0800 | [diff] [blame] | 109 | /* cancel subscription */ |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 110 | if (p->selected != NULL) { |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 111 | grpc_connected_subchannel_notify_on_state_change( |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 112 | exec_ctx, p->selected, NULL, NULL, &p->connectivity_changed); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 113 | } else if (p->num_subchannels > 0 && p->started_picking) { |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 114 | grpc_subchannel_notify_on_state_change( |
| 115 | exec_ctx, p->subchannels[p->checking_subchannel], NULL, NULL, |
| 116 | &p->connectivity_changed); |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 117 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 118 | while (pp != NULL) { |
| 119 | pending_pick *next = pp->next; |
| 120 | *pp->target = NULL; |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 121 | grpc_closure_sched(exec_ctx, pp->on_complete, GRPC_ERROR_NONE); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 122 | gpr_free(pp); |
| 123 | pp = next; |
| 124 | } |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 127 | static void pf_cancel_pick_locked(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, |
| 128 | grpc_connected_subchannel **target, |
| 129 | grpc_error *error) { |
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; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 132 | pp = p->pending_picks; |
| 133 | p->pending_picks = NULL; |
| 134 | while (pp != NULL) { |
| 135 | pending_pick *next = pp->next; |
| 136 | if (pp->target == target) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 137 | *target = NULL; |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 138 | grpc_closure_sched(exec_ctx, pp->on_complete, |
| 139 | GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING( |
| 140 | "Pick Cancelled", &error, 1)); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 141 | gpr_free(pp); |
| 142 | } else { |
| 143 | pp->next = p->pending_picks; |
| 144 | p->pending_picks = pp; |
| 145 | } |
| 146 | pp = next; |
| 147 | } |
Mark D. Roth | 5f84400 | 2016-09-08 08:20:53 -0700 | [diff] [blame] | 148 | GRPC_ERROR_UNREF(error); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 151 | static void pf_cancel_picks_locked(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, |
| 152 | uint32_t initial_metadata_flags_mask, |
| 153 | uint32_t initial_metadata_flags_eq, |
| 154 | grpc_error *error) { |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 155 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
| 156 | pending_pick *pp; |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 157 | pp = p->pending_picks; |
| 158 | p->pending_picks = NULL; |
| 159 | while (pp != NULL) { |
| 160 | pending_pick *next = pp->next; |
| 161 | if ((pp->initial_metadata_flags & initial_metadata_flags_mask) == |
| 162 | initial_metadata_flags_eq) { |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 163 | grpc_closure_sched(exec_ctx, pp->on_complete, |
| 164 | GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING( |
| 165 | "Pick Cancelled", &error, 1)); |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 166 | gpr_free(pp); |
| 167 | } else { |
| 168 | pp->next = p->pending_picks; |
| 169 | p->pending_picks = pp; |
| 170 | } |
| 171 | pp = next; |
| 172 | } |
Mark D. Roth | e65ff11 | 2016-09-09 13:48:38 -0700 | [diff] [blame] | 173 | GRPC_ERROR_UNREF(error); |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 174 | } |
| 175 | |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 176 | static void start_picking_locked(grpc_exec_ctx *exec_ctx, |
| 177 | pick_first_lb_policy *p) { |
| 178 | p->started_picking = true; |
| 179 | if (p->subchannels != NULL) { |
| 180 | GPR_ASSERT(p->num_subchannels > 0); |
| 181 | p->checking_subchannel = 0; |
| 182 | p->checking_connectivity = GRPC_CHANNEL_IDLE; |
| 183 | GRPC_LB_POLICY_WEAK_REF(&p->base, "pick_first_connectivity"); |
| 184 | grpc_subchannel_notify_on_state_change( |
| 185 | exec_ctx, p->subchannels[p->checking_subchannel], |
| 186 | p->base.interested_parties, &p->checking_connectivity, |
| 187 | &p->connectivity_changed); |
| 188 | } |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 191 | 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] | 192 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 193 | if (!p->started_picking) { |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 194 | start_picking_locked(exec_ctx, p); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 195 | } |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 198 | static int pf_pick_locked(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, |
| 199 | const grpc_lb_policy_pick_args *pick_args, |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 200 | grpc_connected_subchannel **target, |
| 201 | grpc_call_context_element *context, void **user_data, |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 202 | grpc_closure *on_complete) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 203 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 204 | pending_pick *pp; |
Craig Tiller | 320bee0 | 2016-01-06 17:33:45 -0800 | [diff] [blame] | 205 | |
| 206 | /* Check atomically for a selected channel */ |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 207 | if (p->selected != NULL) { |
| 208 | *target = GRPC_CONNECTED_SUBCHANNEL_REF(p->selected, "picked"); |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 209 | return 1; |
| 210 | } |
Craig Tiller | 320bee0 | 2016-01-06 17:33:45 -0800 | [diff] [blame] | 211 | |
Craig Tiller | 46dd790 | 2017-02-23 09:42:16 -0800 | [diff] [blame] | 212 | /* No subchannel selected yet, so try again */ |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 213 | if (!p->started_picking) { |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 214 | start_picking_locked(exec_ctx, p); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 215 | } |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 216 | pp = gpr_malloc(sizeof(*pp)); |
| 217 | pp->next = p->pending_picks; |
| 218 | pp->target = target; |
| 219 | pp->initial_metadata_flags = pick_args->initial_metadata_flags; |
| 220 | pp->on_complete = on_complete; |
| 221 | p->pending_picks = pp; |
| 222 | return 0; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 225 | static void destroy_subchannels_locked(grpc_exec_ctx *exec_ctx, |
| 226 | pick_first_lb_policy *p) { |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 227 | size_t num_subchannels = p->num_subchannels; |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 228 | grpc_subchannel **subchannels = p->subchannels; |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 229 | |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 230 | p->num_subchannels = 0; |
| 231 | p->subchannels = NULL; |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 232 | GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &p->base, "destroy_subchannels"); |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 233 | |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 234 | for (size_t i = 0; i < num_subchannels; i++) { |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 235 | GRPC_SUBCHANNEL_UNREF(exec_ctx, subchannels[i], "pick_first"); |
| 236 | } |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 237 | gpr_free(subchannels); |
| 238 | } |
| 239 | |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 240 | static grpc_connectivity_state pf_check_connectivity_locked( |
| 241 | grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, grpc_error **error) { |
| 242 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
| 243 | return grpc_connectivity_state_get(&p->state_tracker, error); |
| 244 | } |
| 245 | |
| 246 | static void pf_notify_on_state_change_locked(grpc_exec_ctx *exec_ctx, |
| 247 | grpc_lb_policy *pol, |
| 248 | grpc_connectivity_state *current, |
| 249 | grpc_closure *notify) { |
| 250 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
| 251 | grpc_connectivity_state_notify_on_state_change(exec_ctx, &p->state_tracker, |
| 252 | current, notify); |
| 253 | } |
| 254 | |
| 255 | static void pf_ping_one_locked(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, |
| 256 | grpc_closure *closure) { |
| 257 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
| 258 | if (p->selected) { |
| 259 | grpc_connected_subchannel_ping(exec_ctx, p->selected, closure); |
| 260 | } else { |
| 261 | grpc_closure_sched(exec_ctx, closure, |
| 262 | GRPC_ERROR_CREATE_FROM_STATIC_STRING("Not connected")); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | /* unsubscribe all subchannels */ |
| 267 | static void stop_connectivity_watchers(grpc_exec_ctx *exec_ctx, |
| 268 | pick_first_lb_policy *p) { |
| 269 | if (p->num_subchannels > 0) { |
| 270 | GPR_ASSERT(p->selected == NULL); |
| 271 | grpc_subchannel_notify_on_state_change( |
| 272 | exec_ctx, p->subchannels[p->checking_subchannel], NULL, NULL, |
| 273 | &p->connectivity_changed); |
| 274 | p->updating_subchannels = true; |
| 275 | } else if (p->selected != NULL) { |
| 276 | grpc_connected_subchannel_notify_on_state_change( |
| 277 | exec_ctx, p->selected, NULL, NULL, &p->connectivity_changed); |
| 278 | p->updating_selected = true; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | /* true upon success */ |
| 283 | static void pf_update_locked(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy, |
| 284 | const grpc_lb_policy_args *args) { |
| 285 | pick_first_lb_policy *p = (pick_first_lb_policy *)policy; |
| 286 | /* Find the number of backend addresses. We ignore balancer |
| 287 | * addresses, since we don't know how to handle them. */ |
| 288 | const grpc_arg *arg = |
| 289 | grpc_channel_args_find(args->args, GRPC_ARG_LB_ADDRESSES); |
| 290 | if (arg == NULL || arg->type != GRPC_ARG_POINTER) { |
| 291 | if (p->subchannels == NULL) { |
| 292 | // If we don't have a current subchannel list, go into TRANSIENT FAILURE. |
| 293 | grpc_connectivity_state_set( |
| 294 | exec_ctx, &p->state_tracker, GRPC_CHANNEL_TRANSIENT_FAILURE, |
| 295 | GRPC_ERROR_CREATE_FROM_STATIC_STRING("Missing update in args"), |
| 296 | "pf_update_missing"); |
| 297 | } else { |
| 298 | // otherwise, keep using the current subchannel list (ignore this update). |
| 299 | gpr_log(GPR_ERROR, |
| 300 | "No valid LB addresses channel arg for Pick First %p update, " |
| 301 | "ignoring.", |
| 302 | (void *)p); |
| 303 | } |
| 304 | return; |
| 305 | } |
| 306 | const grpc_lb_addresses *addresses = arg->value.pointer.p; |
| 307 | size_t num_addrs = 0; |
| 308 | for (size_t i = 0; i < addresses->num_addresses; i++) { |
| 309 | if (!addresses->addresses[i].is_balancer) ++num_addrs; |
| 310 | } |
| 311 | if (num_addrs == 0) { |
| 312 | // Empty update. Unsubscribe from all current subchannels and put the |
| 313 | // channel in TRANSIENT_FAILURE. |
| 314 | grpc_connectivity_state_set( |
| 315 | exec_ctx, &p->state_tracker, GRPC_CHANNEL_TRANSIENT_FAILURE, |
| 316 | GRPC_ERROR_CREATE_FROM_STATIC_STRING("Empty update"), |
| 317 | "pf_update_empty"); |
| 318 | stop_connectivity_watchers(exec_ctx, p); |
| 319 | return; |
| 320 | } |
| 321 | if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) { |
| 322 | gpr_log(GPR_INFO, "Pick First %p received update with %lu addresses", |
| 323 | (void *)p, (unsigned long)num_addrs); |
| 324 | } |
| 325 | grpc_subchannel_args *sc_args = gpr_zalloc(sizeof(*sc_args) * num_addrs); |
| 326 | /* We remove the following keys in order for subchannel keys belonging to |
| 327 | * subchannels point to the same address to match. */ |
| 328 | static const char *keys_to_remove[] = {GRPC_ARG_SUBCHANNEL_ADDRESS, |
| 329 | GRPC_ARG_LB_ADDRESSES}; |
| 330 | size_t sc_args_count = 0; |
| 331 | |
| 332 | /* Create list of subchannel args for new addresses in \a args. */ |
| 333 | for (size_t i = 0; i < addresses->num_addresses; i++) { |
| 334 | if (addresses->addresses[i].is_balancer) continue; |
| 335 | if (addresses->addresses[i].user_data != NULL) { |
| 336 | gpr_log(GPR_ERROR, |
| 337 | "This LB policy doesn't support user data. It will be ignored"); |
| 338 | } |
| 339 | grpc_arg addr_arg = |
| 340 | grpc_create_subchannel_address_arg(&addresses->addresses[i].address); |
| 341 | grpc_channel_args *new_args = grpc_channel_args_copy_and_add_and_remove( |
| 342 | args->args, keys_to_remove, GPR_ARRAY_SIZE(keys_to_remove), &addr_arg, |
| 343 | 1); |
| 344 | gpr_free(addr_arg.value.string); |
| 345 | sc_args[sc_args_count++].args = new_args; |
| 346 | } |
| 347 | |
| 348 | /* Check if p->selected is amongst them. If so, we are done. */ |
| 349 | if (p->selected != NULL) { |
| 350 | GPR_ASSERT(p->selected_key != NULL); |
| 351 | for (size_t i = 0; i < sc_args_count; i++) { |
| 352 | grpc_subchannel_key *ith_sc_key = grpc_subchannel_key_create(&sc_args[i]); |
| 353 | const bool found_selected = |
| 354 | grpc_subchannel_key_compare(p->selected_key, ith_sc_key) == 0; |
| 355 | grpc_subchannel_key_destroy(exec_ctx, ith_sc_key); |
| 356 | if (found_selected) { |
| 357 | // The currently selected subchannel is in the update: we are done. |
| 358 | if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) { |
| 359 | gpr_log(GPR_INFO, |
| 360 | "Pick First %p found already selected subchannel %p amongst " |
| 361 | "updates. Update done.", |
| 362 | (void *)p, (void *)p->selected); |
| 363 | } |
| 364 | for (size_t j = 0; j < sc_args_count; j++) { |
| 365 | grpc_channel_args_destroy(exec_ctx, |
| 366 | (grpc_channel_args *)sc_args[j].args); |
| 367 | } |
| 368 | gpr_free(sc_args); |
| 369 | return; |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | // We only check for already running updates here because if the previous |
| 374 | // steps were successful, the update can be considered done without any |
| 375 | // interference (ie, no callbacks were scheduled). |
| 376 | if (p->updating_selected || p->updating_subchannels) { |
| 377 | if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) { |
| 378 | gpr_log(GPR_INFO, |
| 379 | "Update already in progress for pick first %p. Deferring update.", |
| 380 | (void *)p); |
| 381 | } |
| 382 | if (p->pending_update_args != NULL) { |
| 383 | grpc_channel_args_destroy(exec_ctx, p->pending_update_args->args); |
| 384 | gpr_free(p->pending_update_args); |
| 385 | } |
| 386 | p->pending_update_args = gpr_zalloc(sizeof(*p->pending_update_args)); |
| 387 | p->pending_update_args->client_channel_factory = |
| 388 | args->client_channel_factory; |
| 389 | p->pending_update_args->args = grpc_channel_args_copy(args->args); |
| 390 | p->pending_update_args->combiner = args->combiner; |
| 391 | return; |
| 392 | } |
| 393 | /* Create the subchannels for the new subchannel args/addresses. */ |
| 394 | grpc_subchannel **new_subchannels = |
| 395 | gpr_zalloc(sizeof(*new_subchannels) * sc_args_count); |
| 396 | size_t num_new_subchannels = 0; |
| 397 | for (size_t i = 0; i < sc_args_count; i++) { |
| 398 | grpc_subchannel *subchannel = grpc_client_channel_factory_create_subchannel( |
| 399 | exec_ctx, args->client_channel_factory, &sc_args[i]); |
| 400 | if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) { |
| 401 | char *address_uri = |
| 402 | grpc_sockaddr_to_uri(&addresses->addresses[i].address); |
| 403 | gpr_log(GPR_INFO, |
| 404 | "Pick First %p created subchannel %p for address uri %s", |
| 405 | (void *)p, (void *)subchannel, address_uri); |
| 406 | gpr_free(address_uri); |
| 407 | } |
| 408 | grpc_channel_args_destroy(exec_ctx, (grpc_channel_args *)sc_args[i].args); |
| 409 | if (subchannel != NULL) new_subchannels[num_new_subchannels++] = subchannel; |
| 410 | } |
| 411 | gpr_free(sc_args); |
| 412 | if (num_new_subchannels == 0) { |
| 413 | gpr_free(new_subchannels); |
| 414 | // Empty update. Unsubscribe from all current subchannels and put the |
| 415 | // channel in TRANSIENT_FAILURE. |
| 416 | grpc_connectivity_state_set( |
| 417 | exec_ctx, &p->state_tracker, GRPC_CHANNEL_TRANSIENT_FAILURE, |
| 418 | GRPC_ERROR_CREATE_FROM_STATIC_STRING("No valid addresses in update"), |
| 419 | "pf_update_no_valid_addresses"); |
| 420 | stop_connectivity_watchers(exec_ctx, p); |
| 421 | return; |
| 422 | } |
| 423 | |
| 424 | /* Destroy the current subchannels. Repurpose pf_shutdown/destroy. */ |
| 425 | stop_connectivity_watchers(exec_ctx, p); |
| 426 | |
| 427 | /* Save new subchannels. The switch over will happen in |
| 428 | * pf_connectivity_changed_locked */ |
| 429 | if (p->updating_selected || p->updating_subchannels) { |
| 430 | p->num_new_subchannels = num_new_subchannels; |
| 431 | p->new_subchannels = new_subchannels; |
| 432 | } else { /* nothing is updating. Get things moving from here */ |
| 433 | p->num_subchannels = num_new_subchannels; |
| 434 | p->subchannels = new_subchannels; |
| 435 | p->new_subchannels = NULL; |
| 436 | p->num_new_subchannels = 0; |
| 437 | if (p->started_picking) { |
| 438 | p->checking_subchannel = 0; |
| 439 | p->checking_connectivity = GRPC_CHANNEL_IDLE; |
| 440 | grpc_subchannel_notify_on_state_change( |
| 441 | exec_ctx, p->subchannels[p->checking_subchannel], |
| 442 | p->base.interested_parties, &p->checking_connectivity, |
| 443 | &p->connectivity_changed); |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 448 | static void pf_connectivity_changed_locked(grpc_exec_ctx *exec_ctx, void *arg, |
| 449 | grpc_error *error) { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 450 | pick_first_lb_policy *p = arg; |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 451 | grpc_subchannel *selected_subchannel; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 452 | pending_pick *pp; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 453 | |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 454 | bool restart = false; |
| 455 | if (p->updating_selected && error == GRPC_ERROR_CANCELLED) { |
| 456 | /* Captured the unsubscription for p->selected */ |
| 457 | GPR_ASSERT(p->selected != NULL); |
| 458 | GRPC_CONNECTED_SUBCHANNEL_UNREF(exec_ctx, p->selected, |
| 459 | "pf_update_connectivity"); |
| 460 | p->updating_selected = false; |
| 461 | if (p->num_new_subchannels == 0) { |
| 462 | p->selected = NULL; |
| 463 | return; |
| 464 | } |
| 465 | restart = true; |
| 466 | } |
| 467 | if (p->updating_subchannels && error == GRPC_ERROR_CANCELLED) { |
| 468 | /* Captured the unsubscription for the checking subchannel */ |
| 469 | GPR_ASSERT(p->selected == NULL); |
| 470 | for (size_t i = 0; i < p->num_subchannels; i++) { |
| 471 | GRPC_SUBCHANNEL_UNREF(exec_ctx, p->subchannels[i], |
| 472 | "pf_update_connectivity"); |
| 473 | } |
| 474 | gpr_free(p->subchannels); |
| 475 | p->subchannels = NULL; |
| 476 | p->num_subchannels = 0; |
| 477 | p->updating_subchannels = false; |
| 478 | if (p->num_new_subchannels == 0) return; |
| 479 | restart = true; |
| 480 | } |
| 481 | if (restart) { |
| 482 | p->selected = NULL; |
| 483 | p->selected_key = NULL; |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 484 | |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 485 | GPR_ASSERT(p->new_subchannels != NULL); |
| 486 | GPR_ASSERT(p->num_new_subchannels > 0); |
| 487 | p->num_subchannels = p->num_new_subchannels; |
| 488 | p->subchannels = p->new_subchannels; |
| 489 | p->num_new_subchannels = 0; |
| 490 | p->new_subchannels = NULL; |
| 491 | |
| 492 | if (p->started_picking) { |
| 493 | /* If we were picking, continue to do so over the new subchannels, |
| 494 | * starting from the 0th index. */ |
| 495 | p->checking_subchannel = 0; |
| 496 | p->checking_connectivity = GRPC_CHANNEL_IDLE; |
| 497 | /* reuses the weak ref from start_picking_locked */ |
| 498 | grpc_subchannel_notify_on_state_change( |
| 499 | exec_ctx, p->subchannels[p->checking_subchannel], |
| 500 | p->base.interested_parties, &p->checking_connectivity, |
| 501 | &p->connectivity_changed); |
| 502 | } |
| 503 | if (p->pending_update_args != NULL) { |
| 504 | const grpc_lb_policy_args *args = p->pending_update_args; |
| 505 | p->pending_update_args = NULL; |
| 506 | pf_update_locked(exec_ctx, &p->base, args); |
| 507 | } |
| 508 | return; |
| 509 | } |
| 510 | GRPC_ERROR_REF(error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 511 | if (p->shutdown) { |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 512 | GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &p->base, "pick_first_connectivity"); |
Craig Tiller | ae12593 | 2016-05-13 16:34:29 -0700 | [diff] [blame] | 513 | GRPC_ERROR_UNREF(error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 514 | return; |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 515 | } else if (p->selected != NULL) { |
Craig Tiller | cb2609f | 2015-11-24 17:19:19 -0800 | [diff] [blame] | 516 | if (p->checking_connectivity == GRPC_CHANNEL_TRANSIENT_FAILURE) { |
| 517 | /* if the selected channel goes bad, we're done */ |
Craig Tiller | 48ed92e | 2016-06-02 11:07:12 -0700 | [diff] [blame] | 518 | p->checking_connectivity = GRPC_CHANNEL_SHUTDOWN; |
Craig Tiller | cb2609f | 2015-11-24 17:19:19 -0800 | [diff] [blame] | 519 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 520 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 521 | p->checking_connectivity, GRPC_ERROR_REF(error), |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 522 | "selected_changed"); |
Craig Tiller | 48ed92e | 2016-06-02 11:07:12 -0700 | [diff] [blame] | 523 | if (p->checking_connectivity != GRPC_CHANNEL_SHUTDOWN) { |
Craig Tiller | ab33b48 | 2015-11-21 08:11:04 -0800 | [diff] [blame] | 524 | grpc_connected_subchannel_notify_on_state_change( |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 525 | exec_ctx, p->selected, p->base.interested_parties, |
Craig Tiller | a6bebf4 | 2015-12-01 17:02:35 -0800 | [diff] [blame] | 526 | &p->checking_connectivity, &p->connectivity_changed); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 527 | } else { |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 528 | GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &p->base, "pick_first_connectivity"); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 529 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 530 | } else { |
| 531 | loop: |
| 532 | switch (p->checking_connectivity) { |
David Garcia Quintas | ea6689d | 2016-11-08 09:46:41 -0800 | [diff] [blame] | 533 | case GRPC_CHANNEL_INIT: |
Jan Tattermusch | b0fb2d2 | 2016-11-16 14:04:05 +0100 | [diff] [blame] | 534 | GPR_UNREACHABLE_CODE(return ); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 535 | case GRPC_CHANNEL_READY: |
| 536 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 537 | GRPC_CHANNEL_READY, GRPC_ERROR_NONE, |
| 538 | "connecting_ready"); |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 539 | selected_subchannel = p->subchannels[p->checking_subchannel]; |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 540 | p->selected = GRPC_CONNECTED_SUBCHANNEL_REF( |
| 541 | grpc_subchannel_get_connected_subchannel(selected_subchannel), |
| 542 | "picked_first"); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 543 | |
| 544 | if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) { |
| 545 | gpr_log(GPR_INFO, "Selected subchannel %p", (void *)p->selected); |
| 546 | } |
| 547 | p->selected_key = grpc_subchannel_get_key(selected_subchannel); |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 548 | /* drop the pick list: we are connected now */ |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 549 | GRPC_LB_POLICY_WEAK_REF(&p->base, "destroy_subchannels"); |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 550 | destroy_subchannels_locked(exec_ctx, p); |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 551 | /* update any calls that were waiting for a pick */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 552 | while ((pp = p->pending_picks)) { |
| 553 | p->pending_picks = pp->next; |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 554 | *pp->target = GRPC_CONNECTED_SUBCHANNEL_REF(p->selected, "picked"); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 555 | if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) { |
| 556 | gpr_log(GPR_INFO, |
| 557 | "Servicing pending pick with selected subchannel %p", |
| 558 | (void *)p->selected); |
| 559 | } |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 560 | grpc_closure_sched(exec_ctx, pp->on_complete, GRPC_ERROR_NONE); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 561 | gpr_free(pp); |
| 562 | } |
Craig Tiller | ab33b48 | 2015-11-21 08:11:04 -0800 | [diff] [blame] | 563 | grpc_connected_subchannel_notify_on_state_change( |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 564 | exec_ctx, p->selected, p->base.interested_parties, |
Craig Tiller | a6bebf4 | 2015-12-01 17:02:35 -0800 | [diff] [blame] | 565 | &p->checking_connectivity, &p->connectivity_changed); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 566 | break; |
| 567 | case GRPC_CHANNEL_TRANSIENT_FAILURE: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 568 | p->checking_subchannel = |
| 569 | (p->checking_subchannel + 1) % p->num_subchannels; |
Craig Tiller | 131b6de | 2016-03-31 17:05:28 -0700 | [diff] [blame] | 570 | if (p->checking_subchannel == 0) { |
| 571 | /* only trigger transient failure when we've tried all alternatives */ |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 572 | grpc_connectivity_state_set( |
| 573 | exec_ctx, &p->state_tracker, GRPC_CHANNEL_TRANSIENT_FAILURE, |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 574 | GRPC_ERROR_REF(error), "connecting_transient_failure"); |
Craig Tiller | 131b6de | 2016-03-31 17:05:28 -0700 | [diff] [blame] | 575 | } |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 576 | GRPC_ERROR_UNREF(error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 577 | p->checking_connectivity = grpc_subchannel_check_connectivity( |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 578 | p->subchannels[p->checking_subchannel], &error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 579 | if (p->checking_connectivity == GRPC_CHANNEL_TRANSIENT_FAILURE) { |
| 580 | grpc_subchannel_notify_on_state_change( |
| 581 | exec_ctx, p->subchannels[p->checking_subchannel], |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 582 | p->base.interested_parties, &p->checking_connectivity, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 583 | &p->connectivity_changed); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 584 | } else { |
| 585 | goto loop; |
| 586 | } |
| 587 | break; |
| 588 | case GRPC_CHANNEL_CONNECTING: |
| 589 | case GRPC_CHANNEL_IDLE: |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 590 | grpc_connectivity_state_set( |
| 591 | exec_ctx, &p->state_tracker, GRPC_CHANNEL_CONNECTING, |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 592 | GRPC_ERROR_REF(error), "connecting_changed"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 593 | grpc_subchannel_notify_on_state_change( |
| 594 | exec_ctx, p->subchannels[p->checking_subchannel], |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 595 | p->base.interested_parties, &p->checking_connectivity, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 596 | &p->connectivity_changed); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 597 | break; |
Craig Tiller | 48ed92e | 2016-06-02 11:07:12 -0700 | [diff] [blame] | 598 | case GRPC_CHANNEL_SHUTDOWN: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 599 | p->num_subchannels--; |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 600 | GPR_SWAP(grpc_subchannel *, p->subchannels[p->checking_subchannel], |
| 601 | p->subchannels[p->num_subchannels]); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 602 | GRPC_SUBCHANNEL_UNREF(exec_ctx, p->subchannels[p->num_subchannels], |
| 603 | "pick_first"); |
| 604 | if (p->num_subchannels == 0) { |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 605 | grpc_connectivity_state_set( |
Craig Tiller | d925c93 | 2016-06-06 08:38:50 -0700 | [diff] [blame] | 606 | exec_ctx, &p->state_tracker, GRPC_CHANNEL_SHUTDOWN, |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 607 | GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING( |
| 608 | "Pick first exhausted channels", &error, 1), |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 609 | "no_more_channels"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 610 | while ((pp = p->pending_picks)) { |
| 611 | p->pending_picks = pp->next; |
| 612 | *pp->target = NULL; |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 613 | grpc_closure_sched(exec_ctx, pp->on_complete, GRPC_ERROR_NONE); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 614 | gpr_free(pp); |
| 615 | } |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 616 | GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &p->base, |
| 617 | "pick_first_connectivity"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 618 | } else { |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 619 | grpc_connectivity_state_set( |
| 620 | exec_ctx, &p->state_tracker, GRPC_CHANNEL_TRANSIENT_FAILURE, |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 621 | GRPC_ERROR_REF(error), "subchannel_failed"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 622 | p->checking_subchannel %= p->num_subchannels; |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 623 | GRPC_ERROR_UNREF(error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 624 | p->checking_connectivity = grpc_subchannel_check_connectivity( |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 625 | p->subchannels[p->checking_subchannel], &error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 626 | goto loop; |
| 627 | } |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 628 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 629 | } |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 630 | |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 631 | GRPC_ERROR_UNREF(error); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | static const grpc_lb_policy_vtable pick_first_lb_policy_vtable = { |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 635 | pf_destroy, |
| 636 | pf_shutdown_locked, |
| 637 | pf_pick_locked, |
| 638 | pf_cancel_pick_locked, |
| 639 | pf_cancel_picks_locked, |
| 640 | pf_ping_one_locked, |
| 641 | pf_exit_idle_locked, |
| 642 | pf_check_connectivity_locked, |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 643 | pf_notify_on_state_change_locked, |
| 644 | pf_update_locked}; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 645 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 646 | static void pick_first_factory_ref(grpc_lb_policy_factory *factory) {} |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 647 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 648 | static void pick_first_factory_unref(grpc_lb_policy_factory *factory) {} |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 649 | |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 650 | static grpc_lb_policy *create_pick_first(grpc_exec_ctx *exec_ctx, |
| 651 | grpc_lb_policy_factory *factory, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 652 | grpc_lb_policy_args *args) { |
David Garcia Quintas | 86fcfcc | 2016-03-31 23:22:28 -0700 | [diff] [blame] | 653 | GPR_ASSERT(args->client_channel_factory != NULL); |
Craig Tiller | 6f41788 | 2017-02-16 14:09:39 -0800 | [diff] [blame] | 654 | pick_first_lb_policy *p = gpr_zalloc(sizeof(*p)); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 655 | pf_update_locked(exec_ctx, &p->base, args); |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 656 | grpc_lb_policy_init(&p->base, &pick_first_lb_policy_vtable, args->combiner); |
| 657 | grpc_closure_init(&p->connectivity_changed, pf_connectivity_changed_locked, p, |
| 658 | grpc_combiner_scheduler(args->combiner, false)); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 659 | return &p->base; |
| 660 | } |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 661 | |
| 662 | static const grpc_lb_policy_factory_vtable pick_first_factory_vtable = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 663 | pick_first_factory_ref, pick_first_factory_unref, create_pick_first, |
| 664 | "pick_first"}; |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 665 | |
| 666 | static grpc_lb_policy_factory pick_first_lb_policy_factory = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 667 | &pick_first_factory_vtable}; |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 668 | |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 669 | static grpc_lb_policy_factory *pick_first_lb_factory_create() { |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 670 | return &pick_first_lb_policy_factory; |
| 671 | } |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 672 | |
| 673 | /* Plugin registration */ |
| 674 | |
| 675 | void grpc_lb_policy_pick_first_init() { |
Craig Tiller | 3113ef4 | 2016-03-29 09:03:14 -0700 | [diff] [blame] | 676 | grpc_register_lb_policy(pick_first_lb_factory_create()); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 677 | grpc_register_tracer("pick_first", &grpc_lb_pick_first_trace); |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | void grpc_lb_policy_pick_first_shutdown() {} |