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