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 | |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 23 | #include "src/core/ext/filters/client_channel/lb_policy/subchannel_list.h" |
Craig Tiller | 9eb0fde | 2017-03-31 16:59:30 -0700 | [diff] [blame] | 24 | #include "src/core/ext/filters/client_channel/lb_policy_registry.h" |
| 25 | #include "src/core/ext/filters/client_channel/subchannel.h" |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 26 | #include "src/core/ext/filters/client_channel/subchannel_index.h" |
Mark D. Roth | 557c990 | 2016-10-24 11:12:05 -0700 | [diff] [blame] | 27 | #include "src/core/lib/channel/channel_args.h" |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 28 | #include "src/core/lib/iomgr/combiner.h" |
Mark D. Roth | 0748f39 | 2017-01-13 09:22:44 -0800 | [diff] [blame] | 29 | #include "src/core/lib/iomgr/sockaddr_utils.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 30 | #include "src/core/lib/transport/connectivity_state.h" |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 31 | |
ncteisen | 7712c7c | 2017-07-12 23:11:27 -0700 | [diff] [blame] | 32 | grpc_tracer_flag grpc_lb_pick_first_trace = |
| 33 | GRPC_TRACER_INITIALIZER(false, "pick_first"); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 34 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 35 | typedef struct pending_pick { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 36 | struct pending_pick *next; |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 37 | uint32_t initial_metadata_flags; |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 38 | grpc_connected_subchannel **target; |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 39 | grpc_closure *on_complete; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 40 | } pending_pick; |
| 41 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 42 | typedef struct { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 43 | /** base policy: must be first */ |
| 44 | grpc_lb_policy base; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 45 | /** all our subchannels */ |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 46 | grpc_lb_subchannel_list *subchannel_list; |
Mark D. Roth | 5132d0e | 2017-10-13 13:20:52 -0700 | [diff] [blame] | 47 | /** latest pending subchannel list */ |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 48 | grpc_lb_subchannel_list *latest_pending_subchannel_list; |
Mark D. Roth | 5132d0e | 2017-10-13 13:20:52 -0700 | [diff] [blame] | 49 | /** selected subchannel in \a subchannel_list */ |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 50 | grpc_lb_subchannel_data *selected; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 51 | /** have we started picking? */ |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 52 | bool started_picking; |
Craig Tiller | a14215a | 2015-07-17 17:21:08 -0700 | [diff] [blame] | 53 | /** are we shut down? */ |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 54 | bool shutdown; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 55 | /** list of picks that are waiting on connectivity */ |
| 56 | pending_pick *pending_picks; |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 57 | /** our connectivity state tracker */ |
| 58 | grpc_connectivity_state_tracker state_tracker; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 59 | } pick_first_lb_policy; |
| 60 | |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 61 | 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] | 62 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 63 | GPR_ASSERT(p->subchannel_list == NULL); |
| 64 | GPR_ASSERT(p->latest_pending_subchannel_list == NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 65 | GPR_ASSERT(p->pending_picks == NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 66 | grpc_connectivity_state_destroy(exec_ctx, &p->state_tracker); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 67 | gpr_free(p); |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 68 | grpc_subchannel_index_unref(); |
David Garcia Quintas | af084dc | 2017-06-27 13:42:54 -0700 | [diff] [blame] | 69 | if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) { |
| 70 | gpr_log(GPR_DEBUG, "Pick First %p destroyed.", (void *)p); |
| 71 | } |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 74 | static void shutdown_locked(grpc_exec_ctx *exec_ctx, pick_first_lb_policy *p, |
| 75 | grpc_error *error) { |
| 76 | if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) { |
| 77 | gpr_log(GPR_DEBUG, "Pick First %p Shutting down", p); |
| 78 | } |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 79 | p->shutdown = true; |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 80 | pending_pick *pp; |
| 81 | while ((pp = p->pending_picks) != NULL) { |
| 82 | p->pending_picks = pp->next; |
| 83 | *pp->target = NULL; |
| 84 | GRPC_CLOSURE_SCHED(exec_ctx, pp->on_complete, GRPC_ERROR_REF(error)); |
| 85 | gpr_free(pp); |
| 86 | } |
Mark D. Roth | 62ca6ce | 2017-10-10 10:01:51 -0700 | [diff] [blame] | 87 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
| 88 | GRPC_CHANNEL_SHUTDOWN, GRPC_ERROR_REF(error), |
| 89 | "shutdown"); |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 90 | if (p->subchannel_list != NULL) { |
| 91 | grpc_lb_subchannel_list_shutdown_and_unref(exec_ctx, p->subchannel_list, |
| 92 | "pf_shutdown"); |
| 93 | p->subchannel_list = NULL; |
Craig Tiller | 4861304 | 2015-11-29 14:45:11 -0800 | [diff] [blame] | 94 | } |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 95 | if (p->latest_pending_subchannel_list != NULL) { |
| 96 | grpc_lb_subchannel_list_shutdown_and_unref( |
| 97 | exec_ctx, p->latest_pending_subchannel_list, "pf_shutdown"); |
| 98 | p->latest_pending_subchannel_list = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 99 | } |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 100 | GRPC_ERROR_UNREF(error); |
| 101 | } |
| 102 | |
| 103 | static void pf_shutdown_locked(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol) { |
| 104 | shutdown_locked(exec_ctx, (pick_first_lb_policy *)pol, |
| 105 | GRPC_ERROR_CREATE_FROM_STATIC_STRING("Channel shutdown")); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 108 | static void pf_cancel_pick_locked(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, |
| 109 | grpc_connected_subchannel **target, |
| 110 | grpc_error *error) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 111 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 112 | pending_pick *pp = p->pending_picks; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 113 | p->pending_picks = NULL; |
| 114 | while (pp != NULL) { |
| 115 | pending_pick *next = pp->next; |
| 116 | if (pp->target == target) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 117 | *target = NULL; |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 118 | GRPC_CLOSURE_SCHED(exec_ctx, pp->on_complete, |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 119 | GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING( |
| 120 | "Pick Cancelled", &error, 1)); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 121 | gpr_free(pp); |
| 122 | } else { |
| 123 | pp->next = p->pending_picks; |
| 124 | p->pending_picks = pp; |
| 125 | } |
| 126 | pp = next; |
| 127 | } |
Mark D. Roth | 5f84400 | 2016-09-08 08:20:53 -0700 | [diff] [blame] | 128 | GRPC_ERROR_UNREF(error); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 131 | static void pf_cancel_picks_locked(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, |
| 132 | uint32_t initial_metadata_flags_mask, |
| 133 | uint32_t initial_metadata_flags_eq, |
| 134 | grpc_error *error) { |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 135 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 136 | pending_pick *pp = p->pending_picks; |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 137 | p->pending_picks = NULL; |
| 138 | while (pp != NULL) { |
| 139 | pending_pick *next = pp->next; |
| 140 | if ((pp->initial_metadata_flags & initial_metadata_flags_mask) == |
| 141 | initial_metadata_flags_eq) { |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 142 | GRPC_CLOSURE_SCHED(exec_ctx, pp->on_complete, |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 143 | GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING( |
| 144 | "Pick Cancelled", &error, 1)); |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 145 | gpr_free(pp); |
| 146 | } else { |
| 147 | pp->next = p->pending_picks; |
| 148 | p->pending_picks = pp; |
| 149 | } |
| 150 | pp = next; |
| 151 | } |
Mark D. Roth | e65ff11 | 2016-09-09 13:48:38 -0700 | [diff] [blame] | 152 | GRPC_ERROR_UNREF(error); |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 153 | } |
| 154 | |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 155 | static void start_picking_locked(grpc_exec_ctx *exec_ctx, |
| 156 | pick_first_lb_policy *p) { |
| 157 | p->started_picking = true; |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 158 | if (p->subchannel_list != NULL && p->subchannel_list->num_subchannels > 0) { |
| 159 | p->subchannel_list->checking_subchannel = 0; |
| 160 | grpc_lb_subchannel_list_ref_for_connectivity_watch( |
| 161 | p->subchannel_list, "connectivity_watch+start_picking"); |
| 162 | grpc_lb_subchannel_data_start_connectivity_watch( |
| 163 | exec_ctx, &p->subchannel_list->subchannels[0]); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 164 | } |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 167 | 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] | 168 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 169 | if (!p->started_picking) { |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 170 | start_picking_locked(exec_ctx, p); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 171 | } |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 174 | static int pf_pick_locked(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, |
| 175 | const grpc_lb_policy_pick_args *pick_args, |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 176 | grpc_connected_subchannel **target, |
| 177 | grpc_call_context_element *context, void **user_data, |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 178 | grpc_closure *on_complete) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 179 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 180 | // If we have a selected subchannel already, return synchronously. |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 181 | if (p->selected != NULL) { |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 182 | *target = GRPC_CONNECTED_SUBCHANNEL_REF(p->selected->connected_subchannel, |
| 183 | "picked"); |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 184 | return 1; |
| 185 | } |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 186 | // No subchannel selected yet, so handle asynchronously. |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 187 | if (!p->started_picking) { |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 188 | start_picking_locked(exec_ctx, p); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 189 | } |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 190 | pending_pick *pp = (pending_pick *)gpr_malloc(sizeof(*pp)); |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 191 | pp->next = p->pending_picks; |
| 192 | pp->target = target; |
| 193 | pp->initial_metadata_flags = pick_args->initial_metadata_flags; |
| 194 | pp->on_complete = on_complete; |
| 195 | p->pending_picks = pp; |
| 196 | return 0; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 199 | static void destroy_unselected_subchannels_locked(grpc_exec_ctx *exec_ctx, |
| 200 | pick_first_lb_policy *p) { |
| 201 | for (size_t i = 0; i < p->subchannel_list->num_subchannels; ++i) { |
| 202 | grpc_lb_subchannel_data *sd = &p->subchannel_list->subchannels[i]; |
| 203 | if (p->selected != sd) { |
| 204 | grpc_lb_subchannel_data_unref_subchannel(exec_ctx, sd, |
| 205 | "selected_different_subchannel"); |
| 206 | } |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 207 | } |
Craig Tiller | b09d84d | 2015-10-06 09:12:16 -0700 | [diff] [blame] | 208 | } |
| 209 | |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 210 | static grpc_connectivity_state pf_check_connectivity_locked( |
| 211 | grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, grpc_error **error) { |
| 212 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
| 213 | return grpc_connectivity_state_get(&p->state_tracker, error); |
| 214 | } |
| 215 | |
| 216 | static void pf_notify_on_state_change_locked(grpc_exec_ctx *exec_ctx, |
| 217 | grpc_lb_policy *pol, |
| 218 | grpc_connectivity_state *current, |
| 219 | grpc_closure *notify) { |
| 220 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
| 221 | grpc_connectivity_state_notify_on_state_change(exec_ctx, &p->state_tracker, |
| 222 | current, notify); |
| 223 | } |
| 224 | |
| 225 | static void pf_ping_one_locked(grpc_exec_ctx *exec_ctx, grpc_lb_policy *pol, |
| 226 | grpc_closure *closure) { |
| 227 | pick_first_lb_policy *p = (pick_first_lb_policy *)pol; |
| 228 | if (p->selected) { |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 229 | grpc_connected_subchannel_ping(exec_ctx, p->selected->connected_subchannel, |
| 230 | closure); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 231 | } else { |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 232 | GRPC_CLOSURE_SCHED(exec_ctx, closure, |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 233 | GRPC_ERROR_CREATE_FROM_STATIC_STRING("Not connected")); |
| 234 | } |
| 235 | } |
| 236 | |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 237 | static void pf_connectivity_changed_locked(grpc_exec_ctx *exec_ctx, void *arg, |
| 238 | grpc_error *error); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 239 | |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 240 | static void pf_update_locked(grpc_exec_ctx *exec_ctx, grpc_lb_policy *policy, |
| 241 | const grpc_lb_policy_args *args) { |
| 242 | pick_first_lb_policy *p = (pick_first_lb_policy *)policy; |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 243 | const grpc_arg *arg = |
| 244 | grpc_channel_args_find(args->args, GRPC_ARG_LB_ADDRESSES); |
| 245 | if (arg == NULL || arg->type != GRPC_ARG_POINTER) { |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 246 | if (p->subchannel_list == NULL) { |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 247 | // If we don't have a current subchannel list, go into TRANSIENT FAILURE. |
| 248 | grpc_connectivity_state_set( |
| 249 | exec_ctx, &p->state_tracker, GRPC_CHANNEL_TRANSIENT_FAILURE, |
| 250 | GRPC_ERROR_CREATE_FROM_STATIC_STRING("Missing update in args"), |
| 251 | "pf_update_missing"); |
| 252 | } else { |
| 253 | // otherwise, keep using the current subchannel list (ignore this update). |
| 254 | gpr_log(GPR_ERROR, |
| 255 | "No valid LB addresses channel arg for Pick First %p update, " |
| 256 | "ignoring.", |
| 257 | (void *)p); |
| 258 | } |
| 259 | return; |
| 260 | } |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 261 | const grpc_lb_addresses *addresses = |
| 262 | (const grpc_lb_addresses *)arg->value.pointer.p; |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 263 | if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) { |
| 264 | gpr_log(GPR_INFO, "Pick First %p received update with %lu addresses", |
Juanli Shen | 8af54b8 | 2017-08-30 15:55:10 -0700 | [diff] [blame] | 265 | (void *)p, (unsigned long)addresses->num_addresses); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 266 | } |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 267 | grpc_lb_subchannel_list *subchannel_list = grpc_lb_subchannel_list_create( |
Mark D. Roth | 901bb4f | 2017-10-11 08:49:07 -0700 | [diff] [blame] | 268 | exec_ctx, &p->base, &grpc_lb_pick_first_trace, addresses, args, |
| 269 | pf_connectivity_changed_locked); |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 270 | if (subchannel_list->num_subchannels == 0) { |
| 271 | // Empty update or no valid subchannels. Unsubscribe from all current |
| 272 | // subchannels and put the channel in TRANSIENT_FAILURE. |
| 273 | grpc_connectivity_state_set( |
| 274 | exec_ctx, &p->state_tracker, GRPC_CHANNEL_TRANSIENT_FAILURE, |
| 275 | GRPC_ERROR_CREATE_FROM_STATIC_STRING("Empty update"), |
| 276 | "pf_update_empty"); |
| 277 | if (p->subchannel_list != NULL) { |
| 278 | grpc_lb_subchannel_list_shutdown_and_unref(exec_ctx, p->subchannel_list, |
| 279 | "sl_shutdown_empty_update"); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 280 | } |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 281 | p->subchannel_list = subchannel_list; // Empty list. |
| 282 | p->selected = NULL; |
| 283 | return; |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 284 | } |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 285 | if (p->selected == NULL) { |
| 286 | // We don't yet have a selected subchannel, so replace the current |
| 287 | // subchannel list immediately. |
| 288 | if (p->subchannel_list != NULL) { |
Mark D. Roth | 62ca6ce | 2017-10-10 10:01:51 -0700 | [diff] [blame] | 289 | grpc_lb_subchannel_list_shutdown_and_unref(exec_ctx, p->subchannel_list, |
| 290 | "pf_update_before_selected"); |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 291 | } |
| 292 | p->subchannel_list = subchannel_list; |
| 293 | } else { |
| 294 | // We do have a selected subchannel. |
| 295 | // Check if it's present in the new list. If so, we're done. |
| 296 | for (size_t i = 0; i < subchannel_list->num_subchannels; ++i) { |
| 297 | grpc_lb_subchannel_data *sd = &subchannel_list->subchannels[i]; |
| 298 | if (sd->subchannel == p->selected->subchannel) { |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 299 | // The currently selected subchannel is in the update: we are done. |
| 300 | if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) { |
| 301 | gpr_log(GPR_INFO, |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 302 | "Pick First %p found already selected subchannel %p " |
| 303 | "at update index %" PRIdPTR " of %" PRIdPTR "; update done", |
| 304 | p, p->selected->subchannel, i, |
| 305 | subchannel_list->num_subchannels); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 306 | } |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 307 | grpc_lb_subchannel_list_ref_for_connectivity_watch( |
| 308 | subchannel_list, "connectivity_watch+replace_selected"); |
| 309 | grpc_lb_subchannel_data_start_connectivity_watch(exec_ctx, sd); |
| 310 | if (p->subchannel_list != NULL) { |
| 311 | grpc_lb_subchannel_list_shutdown_and_unref( |
| 312 | exec_ctx, p->subchannel_list, "pf_update_includes_selected"); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 313 | } |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 314 | p->subchannel_list = subchannel_list; |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 315 | p->selected = sd; |
| 316 | destroy_unselected_subchannels_locked(exec_ctx, p); |
| 317 | // If there was a previously pending update (which may or may |
| 318 | // not have contained the currently selected subchannel), drop |
| 319 | // it, so that it doesn't override what we've done here. |
| 320 | if (p->latest_pending_subchannel_list != NULL) { |
| 321 | grpc_lb_subchannel_list_shutdown_and_unref( |
| 322 | exec_ctx, p->latest_pending_subchannel_list, |
| 323 | "pf_update_includes_selected+outdated"); |
| 324 | p->latest_pending_subchannel_list = NULL; |
| 325 | } |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 326 | return; |
| 327 | } |
| 328 | } |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 329 | // Not keeping the previous selected subchannel, so set the latest |
| 330 | // pending subchannel list to the new subchannel list. We will wait |
| 331 | // for it to report READY before swapping it into the current |
| 332 | // subchannel list. |
| 333 | if (p->latest_pending_subchannel_list != NULL) { |
| 334 | if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) { |
| 335 | gpr_log(GPR_DEBUG, |
| 336 | "Pick First %p Shutting down latest pending subchannel list " |
| 337 | "%p, about to be replaced by newer latest %p", |
| 338 | (void *)p, (void *)p->latest_pending_subchannel_list, |
| 339 | (void *)subchannel_list); |
| 340 | } |
| 341 | grpc_lb_subchannel_list_shutdown_and_unref( |
| 342 | exec_ctx, p->latest_pending_subchannel_list, |
| 343 | "sl_outdated_dont_smash"); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 344 | } |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 345 | p->latest_pending_subchannel_list = subchannel_list; |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 346 | } |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 347 | // If we've started picking, start trying to connect to the first |
| 348 | // subchannel in the new list. |
Mark D. Roth | 5132d0e | 2017-10-13 13:20:52 -0700 | [diff] [blame] | 349 | if (p->started_picking) { |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 350 | grpc_lb_subchannel_list_ref_for_connectivity_watch( |
| 351 | subchannel_list, "connectivity_watch+update"); |
| 352 | grpc_lb_subchannel_data_start_connectivity_watch( |
| 353 | exec_ctx, &subchannel_list->subchannels[0]); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 357 | static void pf_connectivity_changed_locked(grpc_exec_ctx *exec_ctx, void *arg, |
| 358 | grpc_error *error) { |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 359 | grpc_lb_subchannel_data *sd = (grpc_lb_subchannel_data *)arg; |
| 360 | pick_first_lb_policy *p = (pick_first_lb_policy *)sd->subchannel_list->policy; |
David Garcia Quintas | af084dc | 2017-06-27 13:42:54 -0700 | [diff] [blame] | 361 | if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) { |
Mark D. Roth | 62ca6ce | 2017-10-10 10:01:51 -0700 | [diff] [blame] | 362 | gpr_log(GPR_DEBUG, |
| 363 | "Pick First %p connectivity changed for subchannel %p (%" PRIdPTR |
| 364 | " of %" PRIdPTR |
| 365 | "), subchannel_list %p: state=%s p->shutdown=%d " |
| 366 | "sd->subchannel_list->shutting_down=%d error=%s", |
| 367 | (void *)p, (void *)sd->subchannel, |
| 368 | sd->subchannel_list->checking_subchannel, |
| 369 | sd->subchannel_list->num_subchannels, (void *)sd->subchannel_list, |
| 370 | grpc_connectivity_state_name(sd->pending_connectivity_state_unsafe), |
| 371 | p->shutdown, sd->subchannel_list->shutting_down, |
| 372 | grpc_error_string(error)); |
David Garcia Quintas | af084dc | 2017-06-27 13:42:54 -0700 | [diff] [blame] | 373 | } |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 374 | // If the policy is shutting down, unref and return. |
| 375 | if (p->shutdown) { |
| 376 | grpc_lb_subchannel_data_stop_connectivity_watch(exec_ctx, sd); |
| 377 | grpc_lb_subchannel_data_unref_subchannel(exec_ctx, sd, "pf_shutdown"); |
| 378 | grpc_lb_subchannel_list_unref_for_connectivity_watch( |
| 379 | exec_ctx, sd->subchannel_list, "pf_shutdown"); |
| 380 | return; |
| 381 | } |
| 382 | // If the subchannel list is shutting down, stop watching. |
| 383 | if (sd->subchannel_list->shutting_down || error == GRPC_ERROR_CANCELLED) { |
| 384 | grpc_lb_subchannel_data_stop_connectivity_watch(exec_ctx, sd); |
| 385 | grpc_lb_subchannel_data_unref_subchannel(exec_ctx, sd, "pf_sl_shutdown"); |
| 386 | grpc_lb_subchannel_list_unref_for_connectivity_watch( |
| 387 | exec_ctx, sd->subchannel_list, "pf_sl_shutdown"); |
| 388 | return; |
| 389 | } |
| 390 | // If we're still here, the notification must be for a subchannel in |
| 391 | // either the current or latest pending subchannel lists. |
| 392 | GPR_ASSERT(sd->subchannel_list == p->subchannel_list || |
| 393 | sd->subchannel_list == p->latest_pending_subchannel_list); |
Mark D. Roth | 5132d0e | 2017-10-13 13:20:52 -0700 | [diff] [blame] | 394 | // Update state. |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 395 | sd->curr_connectivity_state = sd->pending_connectivity_state_unsafe; |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 396 | // Handle updates for the currently selected subchannel. |
| 397 | if (p->selected == sd) { |
| 398 | // If the new state is anything other than READY and there is a |
| 399 | // pending update, switch to the pending update. |
| 400 | if (sd->curr_connectivity_state != GRPC_CHANNEL_READY && |
| 401 | p->latest_pending_subchannel_list != NULL) { |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 402 | p->selected = NULL; |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 403 | grpc_lb_subchannel_list_shutdown_and_unref( |
| 404 | exec_ctx, p->subchannel_list, "selected_not_ready+switch_to_update"); |
| 405 | p->subchannel_list = p->latest_pending_subchannel_list; |
| 406 | p->latest_pending_subchannel_list = NULL; |
Mark D. Roth | 62ca6ce | 2017-10-10 10:01:51 -0700 | [diff] [blame] | 407 | grpc_connectivity_state_set( |
Mark D. Roth | 99f54e1 | 2017-10-16 09:55:53 -0700 | [diff] [blame] | 408 | exec_ctx, &p->state_tracker, GRPC_CHANNEL_TRANSIENT_FAILURE, |
Mark D. Roth | 62ca6ce | 2017-10-10 10:01:51 -0700 | [diff] [blame] | 409 | GRPC_ERROR_REF(error), "selected_not_ready+switch_to_update"); |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 410 | } else { |
Mark D. Roth | 62ca6ce | 2017-10-10 10:01:51 -0700 | [diff] [blame] | 411 | if (sd->curr_connectivity_state == GRPC_CHANNEL_TRANSIENT_FAILURE) { |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 412 | /* if the selected channel goes bad, we're done */ |
| 413 | sd->curr_connectivity_state = GRPC_CHANNEL_SHUTDOWN; |
| 414 | } |
| 415 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
| 416 | sd->curr_connectivity_state, |
| 417 | GRPC_ERROR_REF(error), "selected_changed"); |
| 418 | if (sd->curr_connectivity_state != GRPC_CHANNEL_SHUTDOWN) { |
| 419 | // Renew notification. |
| 420 | grpc_lb_subchannel_data_start_connectivity_watch(exec_ctx, sd); |
| 421 | } else { |
| 422 | grpc_lb_subchannel_data_stop_connectivity_watch(exec_ctx, sd); |
| 423 | grpc_lb_subchannel_list_unref_for_connectivity_watch( |
| 424 | exec_ctx, sd->subchannel_list, "pf_selected_shutdown"); |
Mark D. Roth | aadf9f4 | 2017-10-19 07:53:53 -0700 | [diff] [blame] | 425 | shutdown_locked(exec_ctx, p, GRPC_ERROR_REF(error)); |
David Garcia Quintas | af084dc | 2017-06-27 13:42:54 -0700 | [diff] [blame] | 426 | } |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 427 | } |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 428 | return; |
| 429 | } |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 430 | // If we get here, there are two possible cases: |
| 431 | // 1. We do not currently have a selected subchannel, and the update is |
| 432 | // for a subchannel in p->subchannel_list that we're trying to |
| 433 | // connect to. The goal here is to find a subchannel that we can |
| 434 | // select. |
| 435 | // 2. We do currently have a selected subchannel, and the update is |
| 436 | // for a subchannel in p->latest_pending_subchannel_list. The |
| 437 | // goal here is to find a subchannel from the update that we can |
| 438 | // select in place of the current one. |
Mark D. Roth | 7ba40ba | 2017-10-10 13:17:13 -0700 | [diff] [blame] | 439 | if (sd->curr_connectivity_state == GRPC_CHANNEL_TRANSIENT_FAILURE || |
| 440 | sd->curr_connectivity_state == GRPC_CHANNEL_SHUTDOWN) { |
| 441 | grpc_lb_subchannel_data_stop_connectivity_watch(exec_ctx, sd); |
| 442 | } |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 443 | while (true) { |
| 444 | switch (sd->curr_connectivity_state) { |
David Garcia Quintas | ea6689d | 2016-11-08 09:46:41 -0800 | [diff] [blame] | 445 | case GRPC_CHANNEL_INIT: |
Jan Tattermusch | b0fb2d2 | 2016-11-16 14:04:05 +0100 | [diff] [blame] | 446 | GPR_UNREACHABLE_CODE(return ); |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 447 | case GRPC_CHANNEL_READY: { |
| 448 | // Case 2. Promote p->latest_pending_subchannel_list to |
| 449 | // p->subchannel_list. |
| 450 | if (sd->subchannel_list == p->latest_pending_subchannel_list) { |
| 451 | GPR_ASSERT(p->subchannel_list != NULL); |
| 452 | grpc_lb_subchannel_list_shutdown_and_unref( |
| 453 | exec_ctx, p->subchannel_list, "finish_update"); |
| 454 | p->subchannel_list = p->latest_pending_subchannel_list; |
| 455 | p->latest_pending_subchannel_list = NULL; |
| 456 | } |
| 457 | // Cases 1 and 2. |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 458 | grpc_connectivity_state_set(exec_ctx, &p->state_tracker, |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 459 | GRPC_CHANNEL_READY, GRPC_ERROR_NONE, |
| 460 | "connecting_ready"); |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 461 | sd->connected_subchannel = GRPC_CONNECTED_SUBCHANNEL_REF( |
| 462 | grpc_subchannel_get_connected_subchannel(sd->subchannel), |
| 463 | "connected"); |
| 464 | p->selected = sd; |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 465 | if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) { |
Mark D. Roth | 62ca6ce | 2017-10-10 10:01:51 -0700 | [diff] [blame] | 466 | gpr_log(GPR_INFO, "Pick First %p selected subchannel %p", (void *)p, |
| 467 | (void *)sd->subchannel); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 468 | } |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 469 | // Drop all other subchannels, since we are now connected. |
| 470 | destroy_unselected_subchannels_locked(exec_ctx, p); |
| 471 | // Update any calls that were waiting for a pick. |
| 472 | pending_pick *pp; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 473 | while ((pp = p->pending_picks)) { |
| 474 | p->pending_picks = pp->next; |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 475 | *pp->target = GRPC_CONNECTED_SUBCHANNEL_REF( |
| 476 | p->selected->connected_subchannel, "picked"); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 477 | if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) { |
| 478 | gpr_log(GPR_INFO, |
| 479 | "Servicing pending pick with selected subchannel %p", |
| 480 | (void *)p->selected); |
| 481 | } |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 482 | GRPC_CLOSURE_SCHED(exec_ctx, pp->on_complete, GRPC_ERROR_NONE); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 483 | gpr_free(pp); |
| 484 | } |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 485 | // Renew notification. |
| 486 | grpc_lb_subchannel_data_start_connectivity_watch(exec_ctx, sd); |
| 487 | return; |
| 488 | } |
| 489 | case GRPC_CHANNEL_TRANSIENT_FAILURE: { |
Mark D. Roth | 7ba40ba | 2017-10-10 13:17:13 -0700 | [diff] [blame] | 490 | do { |
| 491 | sd->subchannel_list->checking_subchannel = |
| 492 | (sd->subchannel_list->checking_subchannel + 1) % |
| 493 | sd->subchannel_list->num_subchannels; |
| 494 | sd = &sd->subchannel_list |
| 495 | ->subchannels[sd->subchannel_list->checking_subchannel]; |
| 496 | } while (sd->subchannel == NULL); |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 497 | // Case 1: Only set state to TRANSIENT_FAILURE if we've tried |
| 498 | // all subchannels. |
| 499 | if (sd->subchannel_list->checking_subchannel == 0 && |
| 500 | sd->subchannel_list == p->subchannel_list) { |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 501 | grpc_connectivity_state_set( |
| 502 | exec_ctx, &p->state_tracker, GRPC_CHANNEL_TRANSIENT_FAILURE, |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 503 | GRPC_ERROR_REF(error), "connecting_transient_failure"); |
Craig Tiller | 131b6de | 2016-03-31 17:05:28 -0700 | [diff] [blame] | 504 | } |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 505 | sd->curr_connectivity_state = |
| 506 | grpc_subchannel_check_connectivity(sd->subchannel, &error); |
Mark D. Roth | 7ba40ba | 2017-10-10 13:17:13 -0700 | [diff] [blame] | 507 | GRPC_ERROR_UNREF(error); |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 508 | if (sd->curr_connectivity_state == GRPC_CHANNEL_TRANSIENT_FAILURE) { |
| 509 | // Reuses the connectivity refs from the previous watch. |
| 510 | grpc_lb_subchannel_data_start_connectivity_watch(exec_ctx, sd); |
| 511 | return; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 512 | } |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 513 | break; // Go back to top of loop. |
| 514 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 515 | case GRPC_CHANNEL_CONNECTING: |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 516 | case GRPC_CHANNEL_IDLE: { |
| 517 | // Only update connectivity state in case 1. |
| 518 | if (sd->subchannel_list == p->subchannel_list) { |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 519 | grpc_connectivity_state_set( |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 520 | exec_ctx, &p->state_tracker, GRPC_CHANNEL_CONNECTING, |
| 521 | GRPC_ERROR_REF(error), "connecting_changed"); |
| 522 | } |
| 523 | // Renew notification. |
| 524 | grpc_lb_subchannel_data_start_connectivity_watch(exec_ctx, sd); |
| 525 | return; |
| 526 | } |
| 527 | case GRPC_CHANNEL_SHUTDOWN: { |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 528 | grpc_lb_subchannel_data_unref_subchannel(exec_ctx, sd, |
| 529 | "pf_candidate_shutdown"); |
Mark D. Roth | 7ba40ba | 2017-10-10 13:17:13 -0700 | [diff] [blame] | 530 | // Advance to next subchannel and check its state. |
| 531 | grpc_lb_subchannel_data *original_sd = sd; |
| 532 | do { |
| 533 | sd->subchannel_list->checking_subchannel = |
| 534 | (sd->subchannel_list->checking_subchannel + 1) % |
| 535 | sd->subchannel_list->num_subchannels; |
| 536 | sd = &sd->subchannel_list |
| 537 | ->subchannels[sd->subchannel_list->checking_subchannel]; |
| 538 | } while (sd->subchannel == NULL && sd != original_sd); |
| 539 | if (sd == original_sd) { |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 540 | grpc_lb_subchannel_list_unref_for_connectivity_watch( |
| 541 | exec_ctx, sd->subchannel_list, "pf_candidate_shutdown"); |
| 542 | shutdown_locked(exec_ctx, p, |
| 543 | GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING( |
| 544 | "Pick first exhausted channels", &error, 1)); |
| 545 | return; |
| 546 | } |
| 547 | if (sd->subchannel_list == p->subchannel_list) { |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 548 | grpc_connectivity_state_set( |
| 549 | exec_ctx, &p->state_tracker, GRPC_CHANNEL_TRANSIENT_FAILURE, |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 550 | GRPC_ERROR_REF(error), "subchannel_failed"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 551 | } |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 552 | sd->curr_connectivity_state = |
| 553 | grpc_subchannel_check_connectivity(sd->subchannel, &error); |
Mark D. Roth | 7ba40ba | 2017-10-10 13:17:13 -0700 | [diff] [blame] | 554 | GRPC_ERROR_UNREF(error); |
Mark D. Roth | 5e9848e | 2017-10-06 13:59:32 -0700 | [diff] [blame] | 555 | if (sd->curr_connectivity_state == GRPC_CHANNEL_TRANSIENT_FAILURE) { |
| 556 | // Reuses the connectivity refs from the previous watch. |
| 557 | grpc_lb_subchannel_data_start_connectivity_watch(exec_ctx, sd); |
| 558 | return; |
| 559 | } |
| 560 | // For any other state, go back to top of loop. |
| 561 | // We will reuse the connectivity refs from the previous watch. |
| 562 | } |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 563 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 564 | } |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | static const grpc_lb_policy_vtable pick_first_lb_policy_vtable = { |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 568 | pf_destroy, |
| 569 | pf_shutdown_locked, |
| 570 | pf_pick_locked, |
| 571 | pf_cancel_pick_locked, |
| 572 | pf_cancel_picks_locked, |
| 573 | pf_ping_one_locked, |
| 574 | pf_exit_idle_locked, |
| 575 | pf_check_connectivity_locked, |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 576 | pf_notify_on_state_change_locked, |
| 577 | pf_update_locked}; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 578 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 579 | static void pick_first_factory_ref(grpc_lb_policy_factory *factory) {} |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 580 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 581 | static void pick_first_factory_unref(grpc_lb_policy_factory *factory) {} |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 582 | |
David Garcia Quintas | 67c0d04 | 2016-03-25 01:37:53 -0700 | [diff] [blame] | 583 | static grpc_lb_policy *create_pick_first(grpc_exec_ctx *exec_ctx, |
| 584 | grpc_lb_policy_factory *factory, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 585 | grpc_lb_policy_args *args) { |
David Garcia Quintas | 86fcfcc | 2016-03-31 23:22:28 -0700 | [diff] [blame] | 586 | GPR_ASSERT(args->client_channel_factory != NULL); |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 587 | 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] | 588 | if (GRPC_TRACER_ON(grpc_lb_pick_first_trace)) { |
| 589 | gpr_log(GPR_DEBUG, "Pick First %p created.", (void *)p); |
| 590 | } |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 591 | pf_update_locked(exec_ctx, &p->base, args); |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 592 | 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] | 593 | grpc_subchannel_index_ref(); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 594 | return &p->base; |
| 595 | } |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 596 | |
| 597 | static const grpc_lb_policy_factory_vtable pick_first_factory_vtable = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 598 | pick_first_factory_ref, pick_first_factory_unref, create_pick_first, |
| 599 | "pick_first"}; |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 600 | |
| 601 | static grpc_lb_policy_factory pick_first_lb_policy_factory = { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 602 | &pick_first_factory_vtable}; |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 603 | |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 604 | static grpc_lb_policy_factory *pick_first_lb_factory_create() { |
David Garcia Quintas | 5c4543d | 2015-09-03 15:49:56 -0700 | [diff] [blame] | 605 | return &pick_first_lb_policy_factory; |
| 606 | } |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 607 | |
| 608 | /* Plugin registration */ |
| 609 | |
Yash Tibrewal | 8306284 | 2017-09-21 18:56:08 -0700 | [diff] [blame] | 610 | extern "C" void grpc_lb_policy_pick_first_init() { |
Craig Tiller | 3113ef4 | 2016-03-29 09:03:14 -0700 | [diff] [blame] | 611 | grpc_register_lb_policy(pick_first_lb_factory_create()); |
ncteisen | 06bce6e | 2017-07-10 07:58:49 -0700 | [diff] [blame] | 612 | grpc_register_tracer(&grpc_lb_pick_first_trace); |
Craig Tiller | fb43385 | 2016-03-29 08:51:07 -0700 | [diff] [blame] | 613 | } |
| 614 | |
Yash Tibrewal | 8306284 | 2017-09-21 18:56:08 -0700 | [diff] [blame] | 615 | extern "C" void grpc_lb_policy_pick_first_shutdown() {} |