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 | 06bce6e | 2017-07-10 07:58:49 -0700 | [diff] [blame^] | 31 | grpc_tracer_flag grpc_lb_pick_first_trace = GRPC_TRACER_INITIALIZER(false, "pick_first"); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 32 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 33 | typedef struct pending_pick { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 34 | struct pending_pick *next; |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 35 | uint32_t initial_metadata_flags; |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 36 | grpc_connected_subchannel **target; |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 37 | grpc_closure *on_complete; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 38 | } pending_pick; |
| 39 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 40 | typedef struct { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 41 | /** base policy: must be first */ |
| 42 | grpc_lb_policy base; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 43 | /** all our subchannels */ |
| 44 | grpc_subchannel **subchannels; |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 45 | grpc_subchannel **new_subchannels; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 46 | size_t num_subchannels; |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 47 | size_t num_new_subchannels; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 48 | |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 49 | grpc_closure connectivity_changed; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 50 | |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 51 | /** remaining members are protected by the combiner */ |
Craig Tiller | 320bee0 | 2016-01-06 17:33:45 -0800 | [diff] [blame] | 52 | |
Craig Tiller | 46dd790 | 2017-02-23 09:42:16 -0800 | [diff] [blame] | 53 | /** the selected channel */ |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 54 | grpc_connected_subchannel *selected; |
| 55 | |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 56 | /** the subchannel key for \a selected, or NULL if \a selected not set */ |
| 57 | const grpc_subchannel_key *selected_key; |
| 58 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 59 | /** have we started picking? */ |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 60 | bool started_picking; |
Craig Tiller | a14215a | 2015-07-17 17:21:08 -0700 | [diff] [blame] | 61 | /** are we shut down? */ |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 62 | bool shutdown; |
| 63 | /** are we updating the selected subchannel? */ |
| 64 | bool updating_selected; |
| 65 | /** are we updating the subchannel candidates? */ |
| 66 | bool updating_subchannels; |
| 67 | /** args from the latest update received while already updating, or NULL */ |
| 68 | grpc_lb_policy_args *pending_update_args; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 69 | /** which subchannel are we watching? */ |
| 70 | size_t checking_subchannel; |
| 71 | /** what is the connectivity of that channel? */ |
| 72 | grpc_connectivity_state checking_connectivity; |
| 73 | /** list of picks that are waiting on connectivity */ |
| 74 | pending_pick *pending_picks; |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 75 | |
| 76 | /** our connectivity state tracker */ |
| 77 | grpc_connectivity_state_tracker state_tracker; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 78 | } pick_first_lb_policy; |
| 79 | |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 80 | static void pf_destroy(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 81 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 82 | GPR_ASSERT(p->pending_picks == NULL); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 83 | for (size_t i = 0; i < p->num_subchannels; i++) { |
| 84 | GRPC_SUBCHANNEL_UNREF(exec_ctx, p->subchannels[i], "pick_first_destroy"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 85 | } |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 86 | if (p->selected != NULL) { |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 87 | GRPC_CONNECTED_SUBCHANNEL_UNREF(exec_ctx, p->selected, |
| 88 | "picked_first_destroy"); |
Craig Tiller | 89a768e | 2015-10-06 09:55:59 -0700 | [diff] [blame] | 89 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 90 | grpc_connectivity_state_destroy(exec_ctx, &p->state_tracker); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 91 | if (p->pending_update_args != NULL) { |
| 92 | grpc_channel_args_destroy(exec_ctx, p->pending_update_args->args); |
| 93 | gpr_free(p->pending_update_args); |
| 94 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 95 | gpr_free(p->subchannels); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 96 | gpr_free(p->new_subchannels); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 97 | gpr_free(p); |
David Garcia Quintas | af084dc | 2017-06-27 13:42:54 -0700 | [diff] [blame] | 98 | if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) { |
| 99 | gpr_log(GPR_DEBUG, "Pick First %p destroyed.", (void *)p); |
| 100 | } |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 103 | 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] | 104 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
Craig Tiller | d2cc459 | 2015-07-01 07:50:47 -0700 | [diff] [blame] | 105 | pending_pick *pp; |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 106 | p->shutdown = true; |
Craig Tiller | 5795da7 | 2015-09-17 15:27:13 -0700 | [diff] [blame] | 107 | pp = p->pending_picks; |
| 108 | p->pending_picks = NULL; |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 109 | grpc_connectivity_state_set( |
Craig Tiller | d925c93 | 2016-06-06 08:38:50 -0700 | [diff] [blame] | 110 | exec_ctx, &p->state_tracker, GRPC_CHANNEL_SHUTDOWN, |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 111 | GRPC_ERROR_CREATE_FROM_STATIC_STRING("Channel shutdown"), "shutdown"); |
Craig Tiller | f036a64 | 2015-12-01 17:00:40 -0800 | [diff] [blame] | 112 | /* cancel subscription */ |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 113 | if (p->selected != NULL) { |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 114 | grpc_connected_subchannel_notify_on_state_change( |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 115 | exec_ctx, p->selected, NULL, NULL, &p->connectivity_changed); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 116 | } else if (p->num_subchannels > 0 && p->started_picking) { |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 117 | grpc_subchannel_notify_on_state_change( |
| 118 | exec_ctx, p->subchannels[p->checking_subchannel], NULL, NULL, |
| 119 | &p->connectivity_changed); |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 120 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 121 | while (pp != NULL) { |
| 122 | pending_pick *next = pp->next; |
| 123 | *pp->target = NULL; |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 124 | GRPC_CLOSURE_SCHED(exec_ctx, pp->on_complete, GRPC_ERROR_NONE); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 125 | gpr_free(pp); |
| 126 | pp = next; |
| 127 | } |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 130 | static void pf_cancel_pick_locked(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, |
| 131 | grpc_connected_subchannel **target, |
| 132 | grpc_error *error) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 133 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
| 134 | pending_pick *pp; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 135 | pp = p->pending_picks; |
| 136 | p->pending_picks = NULL; |
| 137 | while (pp != NULL) { |
| 138 | pending_pick *next = pp->next; |
| 139 | if (pp->target == target) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 140 | *target = NULL; |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 141 | GRPC_CLOSURE_SCHED(exec_ctx, pp->on_complete, |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 142 | GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING( |
| 143 | "Pick Cancelled", &error, 1)); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 144 | gpr_free(pp); |
| 145 | } else { |
| 146 | pp->next = p->pending_picks; |
| 147 | p->pending_picks = pp; |
| 148 | } |
| 149 | pp = next; |
| 150 | } |
Mark D. Roth | 5f84400 | 2016-09-08 08:20:53 -0700 | [diff] [blame] | 151 | GRPC_ERROR_UNREF(error); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 152 | } |
| 153 | |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 154 | static void pf_cancel_picks_locked(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, |
| 155 | uint32_t initial_metadata_flags_mask, |
| 156 | uint32_t initial_metadata_flags_eq, |
| 157 | grpc_error *error) { |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 158 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
| 159 | pending_pick *pp; |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 160 | pp = p->pending_picks; |
| 161 | p->pending_picks = NULL; |
| 162 | while (pp != NULL) { |
| 163 | pending_pick *next = pp->next; |
| 164 | if ((pp->initial_metadata_flags & initial_metadata_flags_mask) == |
| 165 | initial_metadata_flags_eq) { |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 166 | GRPC_CLOSURE_SCHED(exec_ctx, pp->on_complete, |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 167 | GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING( |
| 168 | "Pick Cancelled", &error, 1)); |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 169 | gpr_free(pp); |
| 170 | } else { |
| 171 | pp->next = p->pending_picks; |
| 172 | p->pending_picks = pp; |
| 173 | } |
| 174 | pp = next; |
| 175 | } |
Mark D. Roth | e65ff11 | 2016-09-09 13:48:38 -0700 | [diff] [blame] | 176 | GRPC_ERROR_UNREF(error); |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 177 | } |
| 178 | |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 179 | static void start_picking_locked(grpc_exec_ctx *exec_ctx, |
| 180 | pick_first_lb_policy *p) { |
| 181 | p->started_picking = true; |
| 182 | if (p->subchannels != NULL) { |
| 183 | GPR_ASSERT(p->num_subchannels > 0); |
| 184 | p->checking_subchannel = 0; |
| 185 | p->checking_connectivity = GRPC_CHANNEL_IDLE; |
| 186 | GRPC_LB_POLICY_WEAK_REF(&p->base, "pick_first_connectivity"); |
| 187 | grpc_subchannel_notify_on_state_change( |
| 188 | exec_ctx, p->subchannels[p->checking_subchannel], |
| 189 | p->base.interested_parties, &p->checking_connectivity, |
| 190 | &p->connectivity_changed); |
| 191 | } |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 194 | 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] | 195 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 196 | if (!p->started_picking) { |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 197 | start_picking_locked(exec_ctx, p); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 198 | } |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 201 | static int pf_pick_locked(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, |
| 202 | const grpc_lb_policy_pick_args *pick_args, |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 203 | grpc_connected_subchannel **target, |
| 204 | grpc_call_context_element *context, void **user_data, |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 205 | grpc_closure *on_complete) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 206 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 207 | pending_pick *pp; |
Craig Tiller | 320bee0 | 2016-01-06 17:33:45 -0800 | [diff] [blame] | 208 | |
| 209 | /* Check atomically for a selected channel */ |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 210 | if (p->selected != NULL) { |
| 211 | *target = GRPC_CONNECTED_SUBCHANNEL_REF(p->selected, "picked"); |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 212 | return 1; |
| 213 | } |
Craig Tiller | 320bee0 | 2016-01-06 17:33:45 -0800 | [diff] [blame] | 214 | |
Craig Tiller | 46dd790 | 2017-02-23 09:42:16 -0800 | [diff] [blame] | 215 | /* No subchannel selected yet, so try again */ |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 216 | if (!p->started_picking) { |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 217 | start_picking_locked(exec_ctx, p); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 218 | } |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 219 | pp = gpr_malloc(sizeof(*pp)); |
| 220 | pp->next = p->pending_picks; |
| 221 | pp->target = target; |
| 222 | pp->initial_metadata_flags = pick_args->initial_metadata_flags; |
| 223 | pp->on_complete = on_complete; |
| 224 | p->pending_picks = pp; |
| 225 | return 0; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 228 | static void destroy_subchannels_locked(grpc_exec_ctx *exec_ctx, |
| 229 | pick_first_lb_policy *p) { |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 230 | size_t num_subchannels = p->num_subchannels; |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 231 | grpc_subchannel **subchannels = p->subchannels; |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 232 | |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 233 | p->num_subchannels = 0; |
| 234 | p->subchannels = NULL; |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 235 | GRPC_LB_POLICY_WEAK_UNREF(exec_ctx, &p->base, "destroy_subchannels"); |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 236 | |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 237 | for (size_t i = 0; i < num_subchannels; i++) { |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 238 | GRPC_SUBCHANNEL_UNREF(exec_ctx, subchannels[i], "pick_first"); |
| 239 | } |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 240 | gpr_free(subchannels); |
| 241 | } |
| 242 | |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 243 | static grpc_connectivity_state pf_check_connectivity_locked( |
| 244 | grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, grpc_error **error) { |
| 245 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
| 246 | return grpc_connectivity_state_get(&p->state_tracker, error); |
| 247 | } |
| 248 | |
| 249 | static void pf_notify_on_state_change_locked(grpc_exec_ctx *exec_ctx, |
| 250 | grpc_lb_policy *pol, |
| 251 | grpc_connectivity_state *current, |
| 252 | grpc_closure *notify) { |
| 253 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
| 254 | grpc_connectivity_state_notify_on_state_change(exec_ctx, &p->state_tracker, |
| 255 | current, notify); |
| 256 | } |
| 257 | |
| 258 | static void pf_ping_one_locked(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, |
| 259 | grpc_closure *closure) { |
| 260 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
| 261 | if (p->selected) { |
| 262 | grpc_connected_subchannel_ping(exec_ctx, p->selected, closure); |
| 263 | } else { |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 264 | GRPC_CLOSURE_SCHED(exec_ctx, closure, |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 265 | GRPC_ERROR_CREATE_FROM_STATIC_STRING("Not connected")); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | /* unsubscribe all subchannels */ |
| 270 | static void stop_connectivity_watchers(grpc_exec_ctx *exec_ctx, |
| 271 | pick_first_lb_policy *p) { |
| 272 | if (p->num_subchannels > 0) { |
| 273 | GPR_ASSERT(p->selected == NULL); |
David Garcia Quintas | af084dc | 2017-06-27 13:42:54 -0700 | [diff] [blame] | 274 | if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) { |
| 275 | gpr_log(GPR_DEBUG, "Pick First %p unsubscribing from subchannel %p", |
| 276 | (void *)p, (void *)p->subchannels[p->checking_subchannel]); |
| 277 | } |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 278 | grpc_subchannel_notify_on_state_change( |
| 279 | exec_ctx, p->subchannels[p->checking_subchannel], NULL, NULL, |
| 280 | &p->connectivity_changed); |
| 281 | p->updating_subchannels = true; |
| 282 | } else if (p->selected != NULL) { |
David Garcia Quintas | af084dc | 2017-06-27 13:42:54 -0700 | [diff] [blame] | 283 | if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) { |
| 284 | gpr_log(GPR_DEBUG, |
| 285 | "Pick First %p unsubscribing from selected subchannel %p", |
| 286 | (void *)p, (void *)p->selected); |
| 287 | } |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 288 | grpc_connected_subchannel_notify_on_state_change( |
| 289 | exec_ctx, p->selected, NULL, NULL, &p->connectivity_changed); |
| 290 | p->updating_selected = true; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | /* true upon success */ |
| 295 | static void pf_update_locked(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy, |
| 296 | const grpc_lb_policy_args *args) { |
| 297 | pick_first_lb_policy *p = (pick_first_lb_policy *)policy; |
| 298 | /* Find the number of backend addresses. We ignore balancer |
| 299 | * addresses, since we don't know how to handle them. */ |
| 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 | } |
| 318 | const grpc_lb_addresses *addresses = arg->value.pointer.p; |
| 319 | size_t num_addrs = 0; |
| 320 | for (size_t i = 0; i < addresses->num_addresses; i++) { |
| 321 | if (!addresses->addresses[i].is_balancer) ++num_addrs; |
| 322 | } |
| 323 | if (num_addrs == 0) { |
| 324 | // Empty update. Unsubscribe from all current subchannels and put the |
| 325 | // channel in TRANSIENT_FAILURE. |
| 326 | grpc_connectivity_state_set( |
| 327 | exec_ctx, &p->state_tracker, GRPC_CHANNEL_TRANSIENT_FAILURE, |
| 328 | GRPC_ERROR_CREATE_FROM_STATIC_STRING("Empty update"), |
| 329 | "pf_update_empty"); |
| 330 | stop_connectivity_watchers(exec_ctx, p); |
| 331 | return; |
| 332 | } |
| 333 | if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) { |
| 334 | gpr_log(GPR_INFO, "Pick First %p received update with %lu addresses", |
| 335 | (void *)p, (unsigned long)num_addrs); |
| 336 | } |
| 337 | grpc_subchannel_args *sc_args = gpr_zalloc(sizeof(*sc_args) * num_addrs); |
| 338 | /* We remove the following keys in order for subchannel keys belonging to |
| 339 | * subchannels point to the same address to match. */ |
| 340 | static const char *keys_to_remove[] = {GRPC_ARG_SUBCHANNEL_ADDRESS, |
| 341 | GRPC_ARG_LB_ADDRESSES}; |
| 342 | size_t sc_args_count = 0; |
| 343 | |
| 344 | /* Create list of subchannel args for new addresses in \a args. */ |
| 345 | for (size_t i = 0; i < addresses->num_addresses; i++) { |
| 346 | if (addresses->addresses[i].is_balancer) continue; |
| 347 | if (addresses->addresses[i].user_data != NULL) { |
| 348 | gpr_log(GPR_ERROR, |
| 349 | "This LB policy doesn't support user data. It will be ignored"); |
| 350 | } |
| 351 | grpc_arg addr_arg = |
| 352 | grpc_create_subchannel_address_arg(&addresses->addresses[i].address); |
| 353 | grpc_channel_args *new_args = grpc_channel_args_copy_and_add_and_remove( |
| 354 | args->args, keys_to_remove, GPR_ARRAY_SIZE(keys_to_remove), &addr_arg, |
| 355 | 1); |
| 356 | gpr_free(addr_arg.value.string); |
| 357 | sc_args[sc_args_count++].args = new_args; |
| 358 | } |
| 359 | |
| 360 | /* Check if p->selected is amongst them. If so, we are done. */ |
| 361 | if (p->selected != NULL) { |
| 362 | GPR_ASSERT(p->selected_key != NULL); |
| 363 | for (size_t i = 0; i < sc_args_count; i++) { |
| 364 | grpc_subchannel_key *ith_sc_key = grpc_subchannel_key_create(&sc_args[i]); |
| 365 | const bool found_selected = |
| 366 | grpc_subchannel_key_compare(p->selected_key, ith_sc_key) == 0; |
| 367 | grpc_subchannel_key_destroy(exec_ctx, ith_sc_key); |
| 368 | if (found_selected) { |
| 369 | // The currently selected subchannel is in the update: we are done. |
| 370 | if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) { |
| 371 | gpr_log(GPR_INFO, |
| 372 | "Pick First %p found already selected subchannel %p amongst " |
| 373 | "updates. Update done.", |
| 374 | (void *)p, (void *)p->selected); |
| 375 | } |
| 376 | for (size_t j = 0; j < sc_args_count; j++) { |
| 377 | grpc_channel_args_destroy(exec_ctx, |
| 378 | (grpc_channel_args *)sc_args[j].args); |
| 379 | } |
| 380 | gpr_free(sc_args); |
| 381 | return; |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | // We only check for already running updates here because if the previous |
| 386 | // steps were successful, the update can be considered done without any |
| 387 | // interference (ie, no callbacks were scheduled). |
| 388 | if (p->updating_selected || p->updating_subchannels) { |
| 389 | if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) { |
| 390 | gpr_log(GPR_INFO, |
| 391 | "Update already in progress for pick first %p. Deferring update.", |
| 392 | (void *)p); |
| 393 | } |
| 394 | if (p->pending_update_args != NULL) { |
| 395 | grpc_channel_args_destroy(exec_ctx, p->pending_update_args->args); |
| 396 | gpr_free(p->pending_update_args); |
| 397 | } |
| 398 | p->pending_update_args = gpr_zalloc(sizeof(*p->pending_update_args)); |
| 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) { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 462 | pick_first_lb_policy *p = 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); |
Craig Tiller | 6f41788 | 2017-02-16 14:09:39 -0800 | [diff] [blame] | 684 | pick_first_lb_policy *p = 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); |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 690 | GRPC_CLOSURE_INIT(&p->connectivity_changed, pf_connectivity_changed_locked, p, |
Craig Tiller | ee4b145 | 2017-05-12 10:56:03 -0700 | [diff] [blame] | 691 | grpc_combiner_scheduler(args->combiner)); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 692 | return &p->base; |
| 693 | } |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 694 | |
| 695 | static const grpc_lb_policy_factory_vtable pick_first_factory_vtable = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 696 | pick_first_factory_ref, pick_first_factory_unref, create_pick_first, |
| 697 | "pick_first"}; |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 698 | |
| 699 | static grpc_lb_policy_factory pick_first_lb_policy_factory = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 700 | &pick_first_factory_vtable}; |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 701 | |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 702 | static grpc_lb_policy_factory *pick_first_lb_factory_create() { |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 703 | return &pick_first_lb_policy_factory; |
| 704 | } |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 705 | |
| 706 | /* Plugin registration */ |
| 707 | |
| 708 | void grpc_lb_policy_pick_first_init() { |
Craig Tiller | 3113ef4 | 2016-03-29 09:03:14 -0700 | [diff] [blame] | 709 | grpc_register_lb_policy(pick_first_lb_factory_create()); |
ncteisen | 06bce6e | 2017-07-10 07:58:49 -0700 | [diff] [blame^] | 710 | grpc_register_tracer(&grpc_lb_pick_first_trace); |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | void grpc_lb_policy_pick_first_shutdown() {} |