Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
Craig Tiller | d4c9833 | 2016-03-31 13:45:47 -0700 | [diff] [blame] | 34 | #include "src/core/ext/client_config/client_channel.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 35 | |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame^] | 36 | #include <stdbool.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 37 | #include <stdio.h> |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 38 | #include <string.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 39 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 40 | #include <grpc/support/alloc.h> |
| 41 | #include <grpc/support/log.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 42 | #include <grpc/support/sync.h> |
| 43 | #include <grpc/support/useful.h> |
| 44 | |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame^] | 45 | #include "src/core/ext/client_config/subchannel.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 46 | #include "src/core/lib/channel/channel_args.h" |
| 47 | #include "src/core/lib/channel/connected_channel.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 48 | #include "src/core/lib/iomgr/iomgr.h" |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame^] | 49 | #include "src/core/lib/iomgr/polling_entity.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 50 | #include "src/core/lib/profiling/timers.h" |
| 51 | #include "src/core/lib/support/string.h" |
| 52 | #include "src/core/lib/surface/channel.h" |
| 53 | #include "src/core/lib/transport/connectivity_state.h" |
Craig Tiller | 8910ac6 | 2015-10-08 16:49:15 -0700 | [diff] [blame] | 54 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 55 | /* Client channel implementation */ |
| 56 | |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame^] | 57 | #define GET_CALL(call_data) \ |
| 58 | ((grpc_subchannel_call *)(gpr_atm_acq_load(&(call_data)->subchannel_call))) |
| 59 | |
| 60 | #define CANCELLED_CALL ((grpc_subchannel_call *)1) |
| 61 | |
| 62 | /** Picks a subchannel. |
| 63 | Returns true if subchannel is available immediately (in which case on_ready |
| 64 | should not be called), or false otherwise (in which case on_ready should be |
| 65 | called when the subchannel is available) */ |
| 66 | typedef bool (*pick_subchannel_cb)( |
| 67 | grpc_exec_ctx *exec_ctx, void *arg, grpc_metadata_batch *initial_metadata, |
| 68 | uint32_t initial_metadata_flags, |
| 69 | grpc_connected_subchannel **connected_subchannel, grpc_closure *on_ready); |
| 70 | |
| 71 | typedef enum { |
| 72 | GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING, |
| 73 | GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL |
| 74 | } subchannel_creation_phase; |
| 75 | |
| 76 | /** Call data. Holds a pointer to grpc_subchannel_call and the |
| 77 | associated machinery to create such a pointer. |
| 78 | Handles queueing of stream ops until a call object is ready, waiting |
| 79 | for initial metadata before trying to create a call object, |
| 80 | and handling cancellation gracefully. */ |
| 81 | typedef struct client_channel_call_data { |
| 82 | /** either 0 for no call, 1 for cancelled, or a pointer to a |
| 83 | grpc_subchannel_call */ |
| 84 | gpr_atm subchannel_call; |
| 85 | /** Helper function to choose the subchannel on which to create |
| 86 | the call object. Channel filter delegates to the load |
| 87 | balancing policy (once it's ready). */ |
| 88 | pick_subchannel_cb pick_subchannel; |
| 89 | void *pick_subchannel_arg; |
| 90 | |
| 91 | gpr_mu mu; |
| 92 | |
| 93 | subchannel_creation_phase creation_phase; |
| 94 | grpc_connected_subchannel *connected_subchannel; |
| 95 | grpc_polling_entity *pollent; |
| 96 | |
| 97 | grpc_transport_stream_op *waiting_ops; |
| 98 | size_t waiting_ops_count; |
| 99 | size_t waiting_ops_capacity; |
| 100 | |
| 101 | grpc_closure next_step; |
| 102 | |
| 103 | grpc_call_stack *owning_call; |
| 104 | } call_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 105 | |
Craig Tiller | 800dacb | 2015-10-06 09:10:26 -0700 | [diff] [blame] | 106 | typedef struct client_channel_channel_data { |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 107 | /** resolver for this channel */ |
| 108 | grpc_resolver *resolver; |
Craig Tiller | 20a3c35 | 2015-08-05 08:39:50 -0700 | [diff] [blame] | 109 | /** have we started resolving this channel */ |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame^] | 110 | bool started_resolving; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 111 | |
Craig Tiller | 9d94b60 | 2015-07-01 14:23:18 -0700 | [diff] [blame] | 112 | /** mutex protecting client configuration, including all |
| 113 | variables below in this data structure */ |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 114 | gpr_mu mu; |
| 115 | /** currently active load balancer - guarded by mu */ |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 116 | grpc_lb_policy *lb_policy; |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 117 | /** incoming resolver result - set by resolver.next(), guarded by mu */ |
| 118 | grpc_resolver_result *resolver_result; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 119 | /** a list of closures that are all waiting for config to come in */ |
Craig Tiller | d9ccbbf | 2015-09-22 09:30:00 -0700 | [diff] [blame] | 120 | grpc_closure_list waiting_for_config_closures; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 121 | /** resolver callback */ |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 122 | grpc_closure on_resolver_result_changed; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 123 | /** connectivity state being tracked */ |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 124 | grpc_connectivity_state_tracker state_tracker; |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 125 | /** when an lb_policy arrives, should we try to exit idle */ |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame^] | 126 | bool exit_idle_when_lb_policy_arrives; |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 127 | /** owning stack */ |
| 128 | grpc_channel_stack *owning_stack; |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 129 | /** interested parties (owned) */ |
| 130 | grpc_pollset_set *interested_parties; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 131 | } channel_data; |
| 132 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 133 | /** We create one watcher for each new lb_policy that is returned from a |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame^] | 134 | resolver, to watch for state changes from the lb_policy. When a state |
| 135 | change is seen, we update the channel, and create a new watcher. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 136 | typedef struct { |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 137 | channel_data *chand; |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 138 | grpc_closure on_changed; |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 139 | grpc_connectivity_state state; |
| 140 | grpc_lb_policy *lb_policy; |
| 141 | } lb_policy_connectivity_watcher; |
| 142 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 143 | typedef struct { |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 144 | grpc_closure closure; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 145 | grpc_call_element *elem; |
| 146 | } waiting_call; |
| 147 | |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame^] | 148 | static void add_waiting_locked(call_data *calld, grpc_transport_stream_op *op) { |
| 149 | GPR_TIMER_BEGIN("add_waiting_locked", 0); |
| 150 | if (calld->waiting_ops_count == calld->waiting_ops_capacity) { |
| 151 | calld->waiting_ops_capacity = GPR_MAX(3, 2 * calld->waiting_ops_capacity); |
| 152 | calld->waiting_ops = |
| 153 | gpr_realloc(calld->waiting_ops, |
| 154 | calld->waiting_ops_capacity * sizeof(*calld->waiting_ops)); |
| 155 | } |
| 156 | calld->waiting_ops[calld->waiting_ops_count++] = *op; |
| 157 | GPR_TIMER_END("add_waiting_locked", 0); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 158 | } |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 159 | |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame^] | 160 | static void fail_locked(grpc_exec_ctx *exec_ctx, call_data *calld, |
| 161 | grpc_error *error) { |
| 162 | size_t i; |
| 163 | for (i = 0; i < calld->waiting_ops_count; i++) { |
| 164 | grpc_transport_stream_op_finish_with_failure( |
| 165 | exec_ctx, &calld->waiting_ops[i], GRPC_ERROR_REF(error)); |
| 166 | } |
| 167 | calld->waiting_ops_count = 0; |
| 168 | GRPC_ERROR_UNREF(error); |
| 169 | } |
| 170 | |
| 171 | typedef struct { |
| 172 | grpc_transport_stream_op *ops; |
| 173 | size_t nops; |
| 174 | grpc_subchannel_call *call; |
| 175 | } retry_ops_args; |
| 176 | |
| 177 | static void retry_ops(grpc_exec_ctx *exec_ctx, void *args, grpc_error *error) { |
| 178 | retry_ops_args *a = args; |
| 179 | size_t i; |
| 180 | for (i = 0; i < a->nops; i++) { |
| 181 | grpc_subchannel_call_process_op(exec_ctx, a->call, &a->ops[i]); |
| 182 | } |
| 183 | GRPC_SUBCHANNEL_CALL_UNREF(exec_ctx, a->call, "retry_ops"); |
| 184 | gpr_free(a->ops); |
| 185 | gpr_free(a); |
| 186 | } |
| 187 | |
| 188 | static void retry_waiting_locked(grpc_exec_ctx *exec_ctx, call_data *calld) { |
| 189 | retry_ops_args *a = gpr_malloc(sizeof(*a)); |
| 190 | a->ops = calld->waiting_ops; |
| 191 | a->nops = calld->waiting_ops_count; |
| 192 | a->call = GET_CALL(calld); |
| 193 | if (a->call == CANCELLED_CALL) { |
| 194 | gpr_free(a); |
| 195 | fail_locked(exec_ctx, calld, GRPC_ERROR_CANCELLED); |
| 196 | return; |
| 197 | } |
| 198 | calld->waiting_ops = NULL; |
| 199 | calld->waiting_ops_count = 0; |
| 200 | calld->waiting_ops_capacity = 0; |
| 201 | GRPC_SUBCHANNEL_CALL_REF(a->call, "retry_ops"); |
| 202 | grpc_exec_ctx_sched(exec_ctx, grpc_closure_create(retry_ops, a), |
| 203 | GRPC_ERROR_NONE, NULL); |
| 204 | } |
| 205 | |
| 206 | static void subchannel_ready(grpc_exec_ctx *exec_ctx, void *arg, |
| 207 | grpc_error *error) { |
| 208 | call_data *calld = arg; |
| 209 | gpr_mu_lock(&calld->mu); |
| 210 | GPR_ASSERT(calld->creation_phase == |
| 211 | GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL); |
| 212 | calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING; |
| 213 | if (calld->connected_subchannel == NULL) { |
| 214 | gpr_atm_no_barrier_store(&calld->subchannel_call, 1); |
| 215 | fail_locked(exec_ctx, calld, GRPC_ERROR_CREATE_REFERENCING( |
| 216 | "Failed to create subchannel", &error, 1)); |
| 217 | } else if (1 == gpr_atm_acq_load(&calld->subchannel_call)) { |
| 218 | /* already cancelled before subchannel became ready */ |
| 219 | fail_locked(exec_ctx, calld, |
| 220 | GRPC_ERROR_CREATE_REFERENCING( |
| 221 | "Cancelled before creating subchannel", &error, 1)); |
| 222 | } else { |
| 223 | grpc_subchannel_call *subchannel_call = NULL; |
| 224 | grpc_error *new_error = grpc_connected_subchannel_create_call( |
| 225 | exec_ctx, calld->connected_subchannel, calld->pollent, |
| 226 | &subchannel_call); |
| 227 | if (new_error != GRPC_ERROR_NONE) { |
| 228 | new_error = grpc_error_add_child(new_error, error); |
| 229 | subchannel_call = CANCELLED_CALL; |
| 230 | fail_locked(exec_ctx, calld, new_error); |
| 231 | } |
| 232 | gpr_atm_rel_store(&calld->subchannel_call, |
| 233 | (gpr_atm)(uintptr_t)subchannel_call); |
| 234 | retry_waiting_locked(exec_ctx, calld); |
| 235 | } |
| 236 | gpr_mu_unlock(&calld->mu); |
| 237 | GRPC_CALL_STACK_UNREF(exec_ctx, calld->owning_call, "pick_subchannel"); |
| 238 | } |
| 239 | |
| 240 | static char *cc_get_peer(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) { |
| 241 | call_data *calld = elem->call_data; |
| 242 | grpc_subchannel_call *subchannel_call = GET_CALL(calld); |
| 243 | if (subchannel_call == NULL || subchannel_call == CANCELLED_CALL) { |
| 244 | return NULL; |
| 245 | } else { |
| 246 | return grpc_subchannel_call_get_peer(exec_ctx, subchannel_call); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | // The logic here is fairly complicated, due to (a) the fact that we |
| 251 | // need to handle the case where we receive the send op before the |
| 252 | // initial metadata op, and (b) the need for efficiency, especially in |
| 253 | // the streaming case. |
| 254 | // TODO(ctiller): Explain this more thoroughly. |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 255 | static void cc_start_transport_stream_op(grpc_exec_ctx *exec_ctx, |
| 256 | grpc_call_element *elem, |
| 257 | grpc_transport_stream_op *op) { |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame^] | 258 | call_data *calld = elem->call_data; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 259 | GRPC_CALL_LOG_OP(GPR_INFO, elem, op); |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame^] | 260 | /* try to (atomically) get the call */ |
| 261 | grpc_subchannel_call *call = GET_CALL(calld); |
| 262 | GPR_TIMER_BEGIN("cc_start_transport_stream_op", 0); |
| 263 | if (call == CANCELLED_CALL) { |
| 264 | grpc_transport_stream_op_finish_with_failure(exec_ctx, op, |
| 265 | GRPC_ERROR_CANCELLED); |
| 266 | GPR_TIMER_END("cc_start_transport_stream_op", 0); |
| 267 | return; |
| 268 | } |
| 269 | if (call != NULL) { |
| 270 | grpc_subchannel_call_process_op(exec_ctx, call, op); |
| 271 | GPR_TIMER_END("cc_start_transport_stream_op", 0); |
| 272 | return; |
| 273 | } |
| 274 | /* we failed; lock and figure out what to do */ |
| 275 | gpr_mu_lock(&calld->mu); |
| 276 | retry: |
| 277 | /* need to recheck that another thread hasn't set the call */ |
| 278 | call = GET_CALL(calld); |
| 279 | if (call == CANCELLED_CALL) { |
| 280 | gpr_mu_unlock(&calld->mu); |
| 281 | grpc_transport_stream_op_finish_with_failure(exec_ctx, op, |
| 282 | GRPC_ERROR_CANCELLED); |
| 283 | GPR_TIMER_END("cc_start_transport_stream_op", 0); |
| 284 | return; |
| 285 | } |
| 286 | if (call != NULL) { |
| 287 | gpr_mu_unlock(&calld->mu); |
| 288 | grpc_subchannel_call_process_op(exec_ctx, call, op); |
| 289 | GPR_TIMER_END("cc_start_transport_stream_op", 0); |
| 290 | return; |
| 291 | } |
| 292 | /* if this is a cancellation, then we can raise our cancelled flag */ |
| 293 | if (op->cancel_error != GRPC_ERROR_NONE) { |
| 294 | if (!gpr_atm_rel_cas(&calld->subchannel_call, 0, |
| 295 | (gpr_atm)(uintptr_t)CANCELLED_CALL)) { |
| 296 | goto retry; |
| 297 | } else { |
| 298 | switch (calld->creation_phase) { |
| 299 | case GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING: |
| 300 | fail_locked(exec_ctx, calld, GRPC_ERROR_REF(op->cancel_error)); |
| 301 | break; |
| 302 | case GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL: |
| 303 | calld->pick_subchannel(exec_ctx, calld->pick_subchannel_arg, NULL, 0, |
| 304 | &calld->connected_subchannel, NULL); |
| 305 | break; |
| 306 | } |
| 307 | gpr_mu_unlock(&calld->mu); |
| 308 | grpc_transport_stream_op_finish_with_failure(exec_ctx, op, |
| 309 | GRPC_ERROR_CANCELLED); |
| 310 | GPR_TIMER_END("cc_start_transport_stream_op", 0); |
| 311 | return; |
| 312 | } |
| 313 | } |
| 314 | /* if we don't have a subchannel, try to get one */ |
| 315 | if (calld->creation_phase == GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING && |
| 316 | calld->connected_subchannel == NULL && |
| 317 | op->send_initial_metadata != NULL) { |
| 318 | calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL; |
| 319 | grpc_closure_init(&calld->next_step, subchannel_ready, calld); |
| 320 | GRPC_CALL_STACK_REF(calld->owning_call, "pick_subchannel"); |
| 321 | if (calld->pick_subchannel( |
| 322 | exec_ctx, calld->pick_subchannel_arg, op->send_initial_metadata, |
| 323 | op->send_initial_metadata_flags, &calld->connected_subchannel, |
| 324 | &calld->next_step)) { |
| 325 | calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING; |
| 326 | GRPC_CALL_STACK_UNREF(exec_ctx, calld->owning_call, "pick_subchannel"); |
| 327 | } |
| 328 | } |
| 329 | /* if we've got a subchannel, then let's ask it to create a call */ |
| 330 | if (calld->creation_phase == GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING && |
| 331 | calld->connected_subchannel != NULL) { |
| 332 | grpc_subchannel_call *subchannel_call = NULL; |
| 333 | grpc_error *error = grpc_connected_subchannel_create_call( |
| 334 | exec_ctx, calld->connected_subchannel, calld->pollent, |
| 335 | &subchannel_call); |
| 336 | if (error != GRPC_ERROR_NONE) { |
| 337 | subchannel_call = CANCELLED_CALL; |
| 338 | fail_locked(exec_ctx, calld, GRPC_ERROR_REF(error)); |
| 339 | grpc_transport_stream_op_finish_with_failure(exec_ctx, op, error); |
| 340 | } |
| 341 | gpr_atm_rel_store(&calld->subchannel_call, |
| 342 | (gpr_atm)(uintptr_t)subchannel_call); |
| 343 | retry_waiting_locked(exec_ctx, calld); |
| 344 | goto retry; |
| 345 | } |
| 346 | /* nothing to be done but wait */ |
| 347 | add_waiting_locked(calld, op); |
| 348 | gpr_mu_unlock(&calld->mu); |
| 349 | GPR_TIMER_END("cc_start_transport_stream_op", 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 350 | } |
| 351 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 352 | static void watch_lb_policy(grpc_exec_ctx *exec_ctx, channel_data *chand, |
| 353 | grpc_lb_policy *lb_policy, |
| 354 | grpc_connectivity_state current_state); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 355 | |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 356 | static void set_channel_connectivity_state_locked(grpc_exec_ctx *exec_ctx, |
| 357 | channel_data *chand, |
| 358 | grpc_connectivity_state state, |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 359 | grpc_error *error, |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 360 | const char *reason) { |
| 361 | if ((state == GRPC_CHANNEL_TRANSIENT_FAILURE || |
Craig Tiller | 48ed92e | 2016-06-02 11:07:12 -0700 | [diff] [blame] | 362 | state == GRPC_CHANNEL_SHUTDOWN) && |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 363 | chand->lb_policy != NULL) { |
| 364 | /* cancel fail-fast picks */ |
| 365 | grpc_lb_policy_cancel_picks( |
| 366 | exec_ctx, chand->lb_policy, |
| 367 | /* mask= */ GRPC_INITIAL_METADATA_IGNORE_CONNECTIVITY, |
| 368 | /* check= */ 0); |
| 369 | } |
Craig Tiller | 9ccf5f1 | 2016-05-07 21:41:01 -0700 | [diff] [blame] | 370 | grpc_connectivity_state_set(exec_ctx, &chand->state_tracker, state, error, |
| 371 | reason); |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 372 | } |
| 373 | |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 374 | static void on_lb_policy_state_changed_locked(grpc_exec_ctx *exec_ctx, |
| 375 | lb_policy_connectivity_watcher *w, |
| 376 | grpc_error *error) { |
Craig Tiller | cb2609f | 2015-11-24 17:19:19 -0800 | [diff] [blame] | 377 | grpc_connectivity_state publish_state = w->state; |
Craig Tiller | 5795da7 | 2015-09-17 15:27:13 -0700 | [diff] [blame] | 378 | /* check if the notification is for a stale policy */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 379 | if (w->lb_policy != w->chand->lb_policy) return; |
Craig Tiller | 5795da7 | 2015-09-17 15:27:13 -0700 | [diff] [blame] | 380 | |
Craig Tiller | 48ed92e | 2016-06-02 11:07:12 -0700 | [diff] [blame] | 381 | if (publish_state == GRPC_CHANNEL_SHUTDOWN && w->chand->resolver != NULL) { |
Craig Tiller | cb2609f | 2015-11-24 17:19:19 -0800 | [diff] [blame] | 382 | publish_state = GRPC_CHANNEL_TRANSIENT_FAILURE; |
| 383 | grpc_resolver_channel_saw_error(exec_ctx, w->chand->resolver); |
Craig Tiller | f62c4d5 | 2015-12-04 07:43:07 -0800 | [diff] [blame] | 384 | GRPC_LB_POLICY_UNREF(exec_ctx, w->chand->lb_policy, "channel"); |
| 385 | w->chand->lb_policy = NULL; |
Craig Tiller | cb2609f | 2015-11-24 17:19:19 -0800 | [diff] [blame] | 386 | } |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 387 | set_channel_connectivity_state_locked(exec_ctx, w->chand, publish_state, |
Craig Tiller | fc353d6 | 2016-05-10 12:58:03 -0700 | [diff] [blame] | 388 | GRPC_ERROR_REF(error), "lb_changed"); |
Craig Tiller | 48ed92e | 2016-06-02 11:07:12 -0700 | [diff] [blame] | 389 | if (w->state != GRPC_CHANNEL_SHUTDOWN) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 390 | watch_lb_policy(exec_ctx, w->chand, w->lb_policy, w->state); |
| 391 | } |
Craig Tiller | 5795da7 | 2015-09-17 15:27:13 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 394 | static void on_lb_policy_state_changed(grpc_exec_ctx *exec_ctx, void *arg, |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 395 | grpc_error *error) { |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 396 | lb_policy_connectivity_watcher *w = arg; |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 397 | |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 398 | gpr_mu_lock(&w->chand->mu); |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 399 | on_lb_policy_state_changed_locked(exec_ctx, w, error); |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 400 | gpr_mu_unlock(&w->chand->mu); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 401 | |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 402 | GRPC_CHANNEL_STACK_UNREF(exec_ctx, w->chand->owning_stack, "watch_lb_policy"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 403 | gpr_free(w); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 404 | } |
| 405 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 406 | static void watch_lb_policy(grpc_exec_ctx *exec_ctx, channel_data *chand, |
| 407 | grpc_lb_policy *lb_policy, |
| 408 | grpc_connectivity_state current_state) { |
| 409 | lb_policy_connectivity_watcher *w = gpr_malloc(sizeof(*w)); |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 410 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "watch_lb_policy"); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 411 | |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 412 | w->chand = chand; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 413 | grpc_closure_init(&w->on_changed, on_lb_policy_state_changed, w); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 414 | w->state = current_state; |
| 415 | w->lb_policy = lb_policy; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 416 | grpc_lb_policy_notify_on_state_change(exec_ctx, lb_policy, &w->state, |
| 417 | &w->on_changed); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 420 | static void cc_on_resolver_result_changed(grpc_exec_ctx *exec_ctx, void *arg, |
| 421 | grpc_error *error) { |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 422 | channel_data *chand = arg; |
| 423 | grpc_lb_policy *lb_policy = NULL; |
| 424 | grpc_lb_policy *old_lb_policy; |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 425 | grpc_connectivity_state state = GRPC_CHANNEL_TRANSIENT_FAILURE; |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame^] | 426 | bool exit_idle = false; |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 427 | grpc_error *state_error = GRPC_ERROR_CREATE("No load balancing policy"); |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 428 | |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 429 | if (chand->resolver_result != NULL) { |
Mark D. Roth | a275aea | 2016-08-23 12:30:45 -0700 | [diff] [blame] | 430 | lb_policy = grpc_resolver_result_get_lb_policy(chand->resolver_result); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 431 | if (lb_policy != NULL) { |
| 432 | GRPC_LB_POLICY_REF(lb_policy, "channel"); |
| 433 | GRPC_LB_POLICY_REF(lb_policy, "config_change"); |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 434 | GRPC_ERROR_UNREF(state_error); |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 435 | state = |
| 436 | grpc_lb_policy_check_connectivity(exec_ctx, lb_policy, &state_error); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 437 | } |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 438 | |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 439 | grpc_resolver_result_unref(exec_ctx, chand->resolver_result); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 442 | chand->resolver_result = NULL; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 443 | |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 444 | if (lb_policy != NULL) { |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 445 | grpc_pollset_set_add_pollset_set(exec_ctx, lb_policy->interested_parties, |
| 446 | chand->interested_parties); |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 447 | } |
| 448 | |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 449 | gpr_mu_lock(&chand->mu); |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 450 | old_lb_policy = chand->lb_policy; |
| 451 | chand->lb_policy = lb_policy; |
Craig Tiller | 0ede545 | 2016-04-23 12:21:45 -0700 | [diff] [blame] | 452 | if (lb_policy != NULL) { |
| 453 | grpc_exec_ctx_enqueue_list(exec_ctx, &chand->waiting_for_config_closures, |
| 454 | NULL); |
| 455 | } else if (chand->resolver == NULL /* disconnected */) { |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 456 | grpc_closure_list_fail_all( |
| 457 | &chand->waiting_for_config_closures, |
| 458 | GRPC_ERROR_CREATE_REFERENCING("Channel disconnected", &error, 1)); |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 459 | grpc_exec_ctx_enqueue_list(exec_ctx, &chand->waiting_for_config_closures, |
| 460 | NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 461 | } |
| 462 | if (lb_policy != NULL && chand->exit_idle_when_lb_policy_arrives) { |
| 463 | GRPC_LB_POLICY_REF(lb_policy, "exit_idle"); |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame^] | 464 | exit_idle = true; |
| 465 | chand->exit_idle_when_lb_policy_arrives = false; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 466 | } |
Craig Tiller | 9846503 | 2015-06-29 14:36:42 -0700 | [diff] [blame] | 467 | |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 468 | if (error == GRPC_ERROR_NONE && chand->resolver) { |
Craig Tiller | 9ccf5f1 | 2016-05-07 21:41:01 -0700 | [diff] [blame] | 469 | set_channel_connectivity_state_locked( |
| 470 | exec_ctx, chand, state, GRPC_ERROR_REF(state_error), "new_lb+resolver"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 471 | if (lb_policy != NULL) { |
| 472 | watch_lb_policy(exec_ctx, chand, lb_policy, state); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 473 | } |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 474 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver"); |
Mark D. Roth | a275aea | 2016-08-23 12:30:45 -0700 | [diff] [blame] | 475 | grpc_resolver_next(exec_ctx, chand->resolver, &chand->resolver_result, |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 476 | &chand->on_resolver_result_changed); |
| 477 | gpr_mu_unlock(&chand->mu); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 478 | } else { |
Craig Tiller | 76a5c0e | 2016-03-09 09:05:30 -0800 | [diff] [blame] | 479 | if (chand->resolver != NULL) { |
| 480 | grpc_resolver_shutdown(exec_ctx, chand->resolver); |
| 481 | GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel"); |
| 482 | chand->resolver = NULL; |
| 483 | } |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 484 | grpc_error *refs[] = {error, state_error}; |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 485 | set_channel_connectivity_state_locked( |
Craig Tiller | d925c93 | 2016-06-06 08:38:50 -0700 | [diff] [blame] | 486 | exec_ctx, chand, GRPC_CHANNEL_SHUTDOWN, |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 487 | GRPC_ERROR_CREATE_REFERENCING("Got config after disconnection", refs, |
| 488 | GPR_ARRAY_SIZE(refs)), |
| 489 | "resolver_gone"); |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 490 | gpr_mu_unlock(&chand->mu); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 491 | } |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 492 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 493 | if (exit_idle) { |
| 494 | grpc_lb_policy_exit_idle(exec_ctx, lb_policy); |
| 495 | GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "exit_idle"); |
| 496 | } |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 497 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 498 | if (old_lb_policy != NULL) { |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 499 | grpc_pollset_set_del_pollset_set( |
| 500 | exec_ctx, old_lb_policy->interested_parties, chand->interested_parties); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 501 | GRPC_LB_POLICY_UNREF(exec_ctx, old_lb_policy, "channel"); |
| 502 | } |
Craig Tiller | 000cd8f | 2015-09-18 07:20:29 -0700 | [diff] [blame] | 503 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 504 | if (lb_policy != NULL) { |
| 505 | GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "config_change"); |
| 506 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 507 | |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 508 | GRPC_CHANNEL_STACK_UNREF(exec_ctx, chand->owning_stack, "resolver"); |
Craig Tiller | 9ccf5f1 | 2016-05-07 21:41:01 -0700 | [diff] [blame] | 509 | GRPC_ERROR_UNREF(state_error); |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 510 | } |
| 511 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 512 | static void cc_start_transport_op(grpc_exec_ctx *exec_ctx, |
| 513 | grpc_channel_element *elem, |
| 514 | grpc_transport_op *op) { |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 515 | channel_data *chand = elem->channel_data; |
Craig Tiller | 000cd8f | 2015-09-18 07:20:29 -0700 | [diff] [blame] | 516 | |
Craig Tiller | 332f1b3 | 2016-05-24 13:21:21 -0700 | [diff] [blame] | 517 | grpc_exec_ctx_sched(exec_ctx, op->on_consumed, GRPC_ERROR_NONE, NULL); |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 518 | |
Craig Tiller | d7f12e3 | 2016-03-03 10:08:31 -0800 | [diff] [blame] | 519 | GPR_ASSERT(op->set_accept_stream == false); |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 520 | if (op->bind_pollset != NULL) { |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 521 | grpc_pollset_set_add_pollset(exec_ctx, chand->interested_parties, |
Craig Tiller | e2c6237 | 2015-12-07 16:11:03 -0800 | [diff] [blame] | 522 | op->bind_pollset); |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 523 | } |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 524 | |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 525 | gpr_mu_lock(&chand->mu); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 526 | if (op->on_connectivity_state_change != NULL) { |
| 527 | grpc_connectivity_state_notify_on_state_change( |
| 528 | exec_ctx, &chand->state_tracker, op->connectivity_state, |
| 529 | op->on_connectivity_state_change); |
| 530 | op->on_connectivity_state_change = NULL; |
| 531 | op->connectivity_state = NULL; |
| 532 | } |
| 533 | |
Craig Tiller | 26dab31 | 2015-12-07 14:43:47 -0800 | [diff] [blame] | 534 | if (op->send_ping != NULL) { |
Craig Tiller | 87b71e2 | 2015-12-07 15:14:14 -0800 | [diff] [blame] | 535 | if (chand->lb_policy == NULL) { |
Craig Tiller | 332f1b3 | 2016-05-24 13:21:21 -0700 | [diff] [blame] | 536 | grpc_exec_ctx_sched(exec_ctx, op->send_ping, |
| 537 | GRPC_ERROR_CREATE("Ping with no load balancing"), |
| 538 | NULL); |
Craig Tiller | 26dab31 | 2015-12-07 14:43:47 -0800 | [diff] [blame] | 539 | } else { |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 540 | grpc_lb_policy_ping_one(exec_ctx, chand->lb_policy, op->send_ping); |
Craig Tiller | 26dab31 | 2015-12-07 14:43:47 -0800 | [diff] [blame] | 541 | op->bind_pollset = NULL; |
| 542 | } |
| 543 | op->send_ping = NULL; |
| 544 | } |
| 545 | |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 546 | if (op->disconnect_with_error != GRPC_ERROR_NONE) { |
| 547 | if (chand->resolver != NULL) { |
| 548 | set_channel_connectivity_state_locked( |
Craig Tiller | d925c93 | 2016-06-06 08:38:50 -0700 | [diff] [blame] | 549 | exec_ctx, chand, GRPC_CHANNEL_SHUTDOWN, |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 550 | GRPC_ERROR_REF(op->disconnect_with_error), "disconnect"); |
| 551 | grpc_resolver_shutdown(exec_ctx, chand->resolver); |
| 552 | GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel"); |
| 553 | chand->resolver = NULL; |
| 554 | if (!chand->started_resolving) { |
| 555 | grpc_closure_list_fail_all(&chand->waiting_for_config_closures, |
| 556 | GRPC_ERROR_REF(op->disconnect_with_error)); |
Craig Tiller | 9ccf5f1 | 2016-05-07 21:41:01 -0700 | [diff] [blame] | 557 | grpc_exec_ctx_enqueue_list(exec_ctx, |
| 558 | &chand->waiting_for_config_closures, NULL); |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 559 | } |
| 560 | if (chand->lb_policy != NULL) { |
| 561 | grpc_pollset_set_del_pollset_set(exec_ctx, |
| 562 | chand->lb_policy->interested_parties, |
| 563 | chand->interested_parties); |
| 564 | GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel"); |
| 565 | chand->lb_policy = NULL; |
| 566 | } |
Craig Tiller | b12d22a | 2016-04-23 12:50:21 -0700 | [diff] [blame] | 567 | } |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 568 | GRPC_ERROR_UNREF(op->disconnect_with_error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 569 | } |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 570 | gpr_mu_unlock(&chand->mu); |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 571 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 572 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 573 | typedef struct { |
| 574 | grpc_metadata_batch *initial_metadata; |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 575 | uint32_t initial_metadata_flags; |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 576 | grpc_connected_subchannel **connected_subchannel; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 577 | grpc_closure *on_ready; |
| 578 | grpc_call_element *elem; |
| 579 | grpc_closure closure; |
| 580 | } continue_picking_args; |
| 581 | |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame^] | 582 | static bool cc_pick_subchannel(grpc_exec_ctx *exec_ctx, void *arg, |
| 583 | grpc_metadata_batch *initial_metadata, |
| 584 | uint32_t initial_metadata_flags, |
| 585 | grpc_connected_subchannel **connected_subchannel, |
| 586 | grpc_closure *on_ready); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 587 | |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 588 | static void continue_picking(grpc_exec_ctx *exec_ctx, void *arg, |
| 589 | grpc_error *error) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 590 | continue_picking_args *cpa = arg; |
Craig Tiller | 0ede545 | 2016-04-23 12:21:45 -0700 | [diff] [blame] | 591 | if (cpa->connected_subchannel == NULL) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 592 | /* cancelled, do nothing */ |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 593 | } else if (error != GRPC_ERROR_NONE) { |
Craig Tiller | 332f1b3 | 2016-05-24 13:21:21 -0700 | [diff] [blame] | 594 | grpc_exec_ctx_sched(exec_ctx, cpa->on_ready, GRPC_ERROR_REF(error), NULL); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 595 | } else if (cc_pick_subchannel(exec_ctx, cpa->elem, cpa->initial_metadata, |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 596 | cpa->initial_metadata_flags, |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 597 | cpa->connected_subchannel, cpa->on_ready)) { |
Craig Tiller | 332f1b3 | 2016-05-24 13:21:21 -0700 | [diff] [blame] | 598 | grpc_exec_ctx_sched(exec_ctx, cpa->on_ready, GRPC_ERROR_NONE, NULL); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 599 | } |
| 600 | gpr_free(cpa); |
| 601 | } |
| 602 | |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame^] | 603 | static bool cc_pick_subchannel(grpc_exec_ctx *exec_ctx, void *elemp, |
| 604 | grpc_metadata_batch *initial_metadata, |
| 605 | uint32_t initial_metadata_flags, |
| 606 | grpc_connected_subchannel **connected_subchannel, |
| 607 | grpc_closure *on_ready) { |
Craig Tiller | bfc9adc | 2016-06-27 13:16:22 -0700 | [diff] [blame] | 608 | GPR_TIMER_BEGIN("cc_pick_subchannel", 0); |
| 609 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 610 | grpc_call_element *elem = elemp; |
| 611 | channel_data *chand = elem->channel_data; |
| 612 | call_data *calld = elem->call_data; |
| 613 | continue_picking_args *cpa; |
| 614 | grpc_closure *closure; |
| 615 | |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 616 | GPR_ASSERT(connected_subchannel); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 617 | |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 618 | gpr_mu_lock(&chand->mu); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 619 | if (initial_metadata == NULL) { |
| 620 | if (chand->lb_policy != NULL) { |
Craig Tiller | ab33b48 | 2015-11-21 08:11:04 -0800 | [diff] [blame] | 621 | grpc_lb_policy_cancel_pick(exec_ctx, chand->lb_policy, |
| 622 | connected_subchannel); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 623 | } |
| 624 | for (closure = chand->waiting_for_config_closures.head; closure != NULL; |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 625 | closure = closure->next_data.next) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 626 | cpa = closure->cb_arg; |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 627 | if (cpa->connected_subchannel == connected_subchannel) { |
| 628 | cpa->connected_subchannel = NULL; |
Craig Tiller | 332f1b3 | 2016-05-24 13:21:21 -0700 | [diff] [blame] | 629 | grpc_exec_ctx_sched(exec_ctx, cpa->on_ready, |
| 630 | GRPC_ERROR_CREATE("Pick cancelled"), NULL); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 631 | } |
| 632 | } |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 633 | gpr_mu_unlock(&chand->mu); |
Craig Tiller | bfc9adc | 2016-06-27 13:16:22 -0700 | [diff] [blame] | 634 | GPR_TIMER_END("cc_pick_subchannel", 0); |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame^] | 635 | return true; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 636 | } |
| 637 | if (chand->lb_policy != NULL) { |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 638 | grpc_lb_policy *lb_policy = chand->lb_policy; |
| 639 | int r; |
| 640 | GRPC_LB_POLICY_REF(lb_policy, "cc_pick_subchannel"); |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 641 | gpr_mu_unlock(&chand->mu); |
David Garcia Quintas | 2a50dfe | 2016-05-31 15:09:12 -0700 | [diff] [blame] | 642 | r = grpc_lb_policy_pick(exec_ctx, lb_policy, calld->pollent, |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 643 | initial_metadata, initial_metadata_flags, |
| 644 | connected_subchannel, on_ready); |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 645 | GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "cc_pick_subchannel"); |
Craig Tiller | bfc9adc | 2016-06-27 13:16:22 -0700 | [diff] [blame] | 646 | GPR_TIMER_END("cc_pick_subchannel", 0); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 647 | return r; |
| 648 | } |
| 649 | if (chand->resolver != NULL && !chand->started_resolving) { |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame^] | 650 | chand->started_resolving = true; |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 651 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver"); |
Mark D. Roth | a275aea | 2016-08-23 12:30:45 -0700 | [diff] [blame] | 652 | grpc_resolver_next(exec_ctx, chand->resolver, &chand->resolver_result, |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 653 | &chand->on_resolver_result_changed); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 654 | } |
Craig Tiller | 0eab697 | 2016-04-23 12:59:57 -0700 | [diff] [blame] | 655 | if (chand->resolver != NULL) { |
| 656 | cpa = gpr_malloc(sizeof(*cpa)); |
| 657 | cpa->initial_metadata = initial_metadata; |
| 658 | cpa->initial_metadata_flags = initial_metadata_flags; |
| 659 | cpa->connected_subchannel = connected_subchannel; |
| 660 | cpa->on_ready = on_ready; |
| 661 | cpa->elem = elem; |
| 662 | grpc_closure_init(&cpa->closure, continue_picking, cpa); |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 663 | grpc_closure_list_append(&chand->waiting_for_config_closures, &cpa->closure, |
| 664 | GRPC_ERROR_NONE); |
Craig Tiller | 0eab697 | 2016-04-23 12:59:57 -0700 | [diff] [blame] | 665 | } else { |
Craig Tiller | 332f1b3 | 2016-05-24 13:21:21 -0700 | [diff] [blame] | 666 | grpc_exec_ctx_sched(exec_ctx, on_ready, GRPC_ERROR_CREATE("Disconnected"), |
| 667 | NULL); |
Craig Tiller | 0eab697 | 2016-04-23 12:59:57 -0700 | [diff] [blame] | 668 | } |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 669 | gpr_mu_unlock(&chand->mu); |
Craig Tiller | bfc9adc | 2016-06-27 13:16:22 -0700 | [diff] [blame] | 670 | |
| 671 | GPR_TIMER_END("cc_pick_subchannel", 0); |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame^] | 672 | return false; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 673 | } |
| 674 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 675 | /* Constructor for call_data */ |
Mark D. Roth | 76d2442 | 2016-06-23 13:22:10 -0700 | [diff] [blame] | 676 | static grpc_error *init_call_elem(grpc_exec_ctx *exec_ctx, |
Mark D. Roth | 0badbe8 | 2016-06-23 10:15:12 -0700 | [diff] [blame] | 677 | grpc_call_element *elem, |
| 678 | grpc_call_element_args *args) { |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame^] | 679 | call_data *calld = elem->call_data; |
| 680 | gpr_atm_rel_store(&calld->subchannel_call, 0); |
| 681 | calld->pick_subchannel = cc_pick_subchannel; |
| 682 | calld->pick_subchannel_arg = elem; |
| 683 | gpr_mu_init(&calld->mu); |
| 684 | calld->connected_subchannel = NULL; |
| 685 | calld->waiting_ops = NULL; |
| 686 | calld->waiting_ops_count = 0; |
| 687 | calld->waiting_ops_capacity = 0; |
| 688 | calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING; |
| 689 | calld->owning_call = args->call_stack; |
| 690 | calld->pollent = NULL; |
Mark D. Roth | 0badbe8 | 2016-06-23 10:15:12 -0700 | [diff] [blame] | 691 | return GRPC_ERROR_NONE; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | /* Destructor for call_data */ |
Craig Tiller | 2c8063c | 2016-03-22 22:12:15 -0700 | [diff] [blame] | 695 | static void destroy_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
David Garcia Quintas | 01c4d99 | 2016-07-07 20:11:27 -0700 | [diff] [blame] | 696 | const grpc_call_final_info *final_info, |
Craig Tiller | 2c8063c | 2016-03-22 22:12:15 -0700 | [diff] [blame] | 697 | void *and_free_memory) { |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame^] | 698 | call_data *calld = elem->call_data; |
| 699 | grpc_subchannel_call *call = GET_CALL(calld); |
| 700 | if (call != NULL && call != CANCELLED_CALL) { |
| 701 | GRPC_SUBCHANNEL_CALL_UNREF(exec_ctx, call, "client_channel_destroy_call"); |
| 702 | } |
| 703 | GPR_ASSERT(calld->creation_phase == GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING); |
| 704 | gpr_mu_destroy(&calld->mu); |
| 705 | GPR_ASSERT(calld->waiting_ops_count == 0); |
| 706 | gpr_free(calld->waiting_ops); |
Craig Tiller | 2c8063c | 2016-03-22 22:12:15 -0700 | [diff] [blame] | 707 | gpr_free(and_free_memory); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | /* Constructor for channel_data */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 711 | static void init_channel_elem(grpc_exec_ctx *exec_ctx, |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 712 | grpc_channel_element *elem, |
| 713 | grpc_channel_element_args *args) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 714 | channel_data *chand = elem->channel_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 715 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 716 | memset(chand, 0, sizeof(*chand)); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 717 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 718 | GPR_ASSERT(args->is_last); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 719 | GPR_ASSERT(elem->filter == &grpc_client_channel_filter); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 720 | |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 721 | gpr_mu_init(&chand->mu); |
| 722 | grpc_closure_init(&chand->on_resolver_result_changed, |
| 723 | cc_on_resolver_result_changed, chand); |
Craig Tiller | 11beb9a | 2015-11-24 10:29:32 -0800 | [diff] [blame] | 724 | chand->owning_stack = args->channel_stack; |
Craig Tiller | 9846503 | 2015-06-29 14:36:42 -0700 | [diff] [blame] | 725 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 726 | grpc_connectivity_state_init(&chand->state_tracker, GRPC_CHANNEL_IDLE, |
| 727 | "client_channel"); |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 728 | chand->interested_parties = grpc_pollset_set_create(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | /* Destructor for channel_data */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 732 | static void destroy_channel_elem(grpc_exec_ctx *exec_ctx, |
| 733 | grpc_channel_element *elem) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 734 | channel_data *chand = elem->channel_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 735 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 736 | if (chand->resolver != NULL) { |
| 737 | grpc_resolver_shutdown(exec_ctx, chand->resolver); |
| 738 | GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel"); |
| 739 | } |
| 740 | if (chand->lb_policy != NULL) { |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 741 | grpc_pollset_set_del_pollset_set(exec_ctx, |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 742 | chand->lb_policy->interested_parties, |
| 743 | chand->interested_parties); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 744 | GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel"); |
| 745 | } |
| 746 | grpc_connectivity_state_destroy(exec_ctx, &chand->state_tracker); |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 747 | grpc_pollset_set_destroy(chand->interested_parties); |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 748 | gpr_mu_destroy(&chand->mu); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 749 | } |
| 750 | |
David Garcia Quintas | f72eb97 | 2016-05-03 18:28:09 -0700 | [diff] [blame] | 751 | static void cc_set_pollset_or_pollset_set(grpc_exec_ctx *exec_ctx, |
| 752 | grpc_call_element *elem, |
David Garcia Quintas | 2a50dfe | 2016-05-31 15:09:12 -0700 | [diff] [blame] | 753 | grpc_polling_entity *pollent) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 754 | call_data *calld = elem->call_data; |
David Garcia Quintas | 2a50dfe | 2016-05-31 15:09:12 -0700 | [diff] [blame] | 755 | calld->pollent = pollent; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 756 | } |
| 757 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 758 | const grpc_channel_filter grpc_client_channel_filter = { |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 759 | cc_start_transport_stream_op, |
| 760 | cc_start_transport_op, |
| 761 | sizeof(call_data), |
| 762 | init_call_elem, |
David Garcia Quintas | 4afce7e | 2016-04-18 16:25:17 -0700 | [diff] [blame] | 763 | cc_set_pollset_or_pollset_set, |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 764 | destroy_call_elem, |
| 765 | sizeof(channel_data), |
| 766 | init_channel_elem, |
| 767 | destroy_channel_elem, |
| 768 | cc_get_peer, |
| 769 | "client-channel", |
Craig Tiller | 87d5b19 | 2015-04-16 14:37:57 -0700 | [diff] [blame] | 770 | }; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 771 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 772 | void grpc_client_channel_set_resolver(grpc_exec_ctx *exec_ctx, |
| 773 | grpc_channel_stack *channel_stack, |
| 774 | grpc_resolver *resolver) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 775 | /* post construction initialization: set the transport setup pointer */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 776 | grpc_channel_element *elem = grpc_channel_stack_last_element(channel_stack); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 777 | channel_data *chand = elem->channel_data; |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 778 | gpr_mu_lock(&chand->mu); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 779 | GPR_ASSERT(!chand->resolver); |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 780 | chand->resolver = resolver; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 781 | GRPC_RESOLVER_REF(resolver, "channel"); |
| 782 | if (!grpc_closure_list_empty(chand->waiting_for_config_closures) || |
| 783 | chand->exit_idle_when_lb_policy_arrives) { |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame^] | 784 | chand->started_resolving = true; |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 785 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver"); |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 786 | grpc_resolver_next(exec_ctx, resolver, &chand->resolver_result, |
| 787 | &chand->on_resolver_result_changed); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 788 | } |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 789 | gpr_mu_unlock(&chand->mu); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 790 | } |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 791 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 792 | grpc_connectivity_state grpc_client_channel_check_connectivity_state( |
| 793 | grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, int try_to_connect) { |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 794 | channel_data *chand = elem->channel_data; |
| 795 | grpc_connectivity_state out; |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 796 | gpr_mu_lock(&chand->mu); |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 797 | out = grpc_connectivity_state_check(&chand->state_tracker, NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 798 | if (out == GRPC_CHANNEL_IDLE && try_to_connect) { |
| 799 | if (chand->lb_policy != NULL) { |
| 800 | grpc_lb_policy_exit_idle(exec_ctx, chand->lb_policy); |
| 801 | } else { |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame^] | 802 | chand->exit_idle_when_lb_policy_arrives = true; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 803 | if (!chand->started_resolving && chand->resolver != NULL) { |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 804 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver"); |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame^] | 805 | chand->started_resolving = true; |
Mark D. Roth | a275aea | 2016-08-23 12:30:45 -0700 | [diff] [blame] | 806 | grpc_resolver_next(exec_ctx, chand->resolver, &chand->resolver_result, |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 807 | &chand->on_resolver_result_changed); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 808 | } |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 809 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 810 | } |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 811 | gpr_mu_unlock(&chand->mu); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 812 | return out; |
| 813 | } |
| 814 | |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 815 | typedef struct { |
| 816 | channel_data *chand; |
| 817 | grpc_pollset *pollset; |
| 818 | grpc_closure *on_complete; |
| 819 | grpc_closure my_closure; |
| 820 | } external_connectivity_watcher; |
| 821 | |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 822 | static void on_external_watch_complete(grpc_exec_ctx *exec_ctx, void *arg, |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 823 | grpc_error *error) { |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 824 | external_connectivity_watcher *w = arg; |
| 825 | grpc_closure *follow_up = w->on_complete; |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 826 | grpc_pollset_set_del_pollset(exec_ctx, w->chand->interested_parties, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 827 | w->pollset); |
| 828 | GRPC_CHANNEL_STACK_UNREF(exec_ctx, w->chand->owning_stack, |
| 829 | "external_connectivity_watcher"); |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 830 | gpr_free(w); |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 831 | follow_up->cb(exec_ctx, follow_up->cb_arg, error); |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 832 | } |
| 833 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 834 | void grpc_client_channel_watch_connectivity_state( |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 835 | grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, grpc_pollset *pollset, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 836 | grpc_connectivity_state *state, grpc_closure *on_complete) { |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 837 | channel_data *chand = elem->channel_data; |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 838 | external_connectivity_watcher *w = gpr_malloc(sizeof(*w)); |
| 839 | w->chand = chand; |
| 840 | w->pollset = pollset; |
| 841 | w->on_complete = on_complete; |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 842 | grpc_pollset_set_add_pollset(exec_ctx, chand->interested_parties, pollset); |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 843 | grpc_closure_init(&w->my_closure, on_external_watch_complete, w); |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 844 | GRPC_CHANNEL_STACK_REF(w->chand->owning_stack, |
| 845 | "external_connectivity_watcher"); |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 846 | gpr_mu_lock(&chand->mu); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 847 | grpc_connectivity_state_notify_on_state_change( |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 848 | exec_ctx, &chand->state_tracker, state, &w->my_closure); |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 849 | gpr_mu_unlock(&chand->mu); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 850 | } |