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